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>
37 #include <libgen.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_diff.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
46 #include "got_utf8.h"
47 #include "got_blame.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct tog_cmd {
58 const char *name;
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
61 const char *descr;
62 };
64 __dead static void usage(void);
65 __dead static void usage_log(void);
66 __dead static void usage_diff(void);
67 __dead static void usage_blame(void);
68 __dead static void usage_tree(void);
70 static const struct got_error* cmd_log(int, char *[]);
71 static const struct got_error* cmd_diff(int, char *[]);
72 static const struct got_error* cmd_blame(int, char *[]);
73 static const struct got_error* cmd_tree(int, char *[]);
75 static struct tog_cmd tog_commands[] = {
76 { "log", cmd_log, usage_log,
77 "show repository history" },
78 { "diff", cmd_diff, usage_diff,
79 "compare files and directories" },
80 { "blame", cmd_blame, usage_blame,
81 "show line-by-line file history" },
82 { "tree", cmd_tree, usage_tree,
83 "browse trees in repository" },
84 };
86 enum tog_view_type {
87 TOG_VIEW_DIFF,
88 TOG_VIEW_LOG,
89 TOG_VIEW_BLAME,
90 TOG_VIEW_TREE
91 };
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
95 FILE *f;
96 int first_displayed_line;
97 int last_displayed_line;
98 int eof;
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 };
106 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 struct commit_queue {
108 int ncommits;
109 struct commit_queue_head head;
110 };
112 struct tog_log_view_state {
113 struct got_commit_graph *graph;
114 struct commit_queue commits;
115 struct commit_queue_entry *first_displayed_entry;
116 struct commit_queue_entry *last_displayed_entry;
117 struct commit_queue_entry *selected_entry;
118 int selected;
119 char *in_repo_path;
120 struct got_repository *repo;
121 struct got_object_id *start_id;
122 };
124 struct tog_blame_cb_args {
125 pthread_mutex_t *mutex;
126 struct tog_blame_line *lines; /* one per line */
127 int nlines;
129 struct tog_view *view;
130 struct got_object_id *commit_id;
131 FILE *f;
132 const char *path;
133 int *first_displayed_line;
134 int *last_displayed_line;
135 int *selected_line;
136 int *quit;
137 int *eof;
138 };
140 struct tog_blame_thread_args {
141 const char *path;
142 struct got_repository *repo;
143 struct tog_blame_cb_args *cb_args;
144 int *complete;
145 };
147 struct tog_blame {
148 FILE *f;
149 size_t filesize;
150 struct tog_blame_line *lines;
151 size_t nlines;
152 pthread_t thread;
153 struct tog_blame_thread_args thread_args;
154 struct tog_blame_cb_args cb_args;
155 const char *path;
156 };
158 struct tog_blame_view_state {
159 int first_displayed_line;
160 int last_displayed_line;
161 int selected_line;
162 int blame_complete;
163 int eof;
164 int done;
165 pthread_mutex_t mutex;
166 struct got_object_id_queue blamed_commits;
167 struct got_object_qid *blamed_commit;
168 char *path;
169 struct got_repository *repo;
170 struct got_object_id *commit_id;
171 struct tog_blame blame;
172 };
174 struct tog_parent_tree {
175 TAILQ_ENTRY(tog_parent_tree) entry;
176 struct got_tree_object *tree;
177 struct got_tree_entry *first_displayed_entry;
178 struct got_tree_entry *selected_entry;
179 int selected;
180 };
182 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 struct tog_tree_view_state {
185 char *tree_label;
186 struct got_tree_object *root;
187 struct got_tree_object *tree;
188 const struct got_tree_entries *entries;
189 struct got_tree_entry *first_displayed_entry;
190 struct got_tree_entry *last_displayed_entry;
191 struct got_tree_entry *selected_entry;
192 int nentries, ndisplayed, selected, show_ids;
193 struct tog_parent_trees parents;
194 struct got_object_id *commit_id;
195 struct got_repository *repo;
196 };
198 TAILQ_HEAD(tog_view_list_head, tog_view);
199 struct tog_view {
200 TAILQ_ENTRY(tog_view) entry;
201 WINDOW *window;
202 PANEL *panel;
203 int nlines, ncols, begin_y, begin_x;
204 int lines, cols; /* copies of LINES and COLS */
205 int focussed;
206 struct tog_view *parent;
207 struct tog_view *child;
209 /* type-specific state */
210 enum tog_view_type type;
211 union {
212 struct tog_diff_view_state diff;
213 struct tog_log_view_state log;
214 struct tog_blame_view_state blame;
215 struct tog_tree_view_state tree;
216 } state;
218 const struct got_error *(*show)(struct tog_view *);
219 const struct got_error *(*input)(struct tog_view **,
220 struct tog_view **, struct tog_view *, int);
221 const struct got_error *(*set_child)(struct tog_view *,
222 struct tog_view *);
223 const struct got_error *(*close)(struct tog_view *);
224 };
226 static const struct got_error *open_diff_view(struct tog_view *,
227 struct got_object *, struct got_object *, struct got_repository *);
228 static const struct got_error *show_diff_view(struct tog_view *);
229 static const struct got_error *input_diff_view(struct tog_view **,
230 struct tog_view **, struct tog_view *, int);
231 static const struct got_error* close_diff_view(struct tog_view *);
233 static const struct got_error *open_log_view(struct tog_view *,
234 struct got_object_id *, struct got_repository *, const char *);
235 static const struct got_error * show_log_view(struct tog_view *);
236 static const struct got_error *input_log_view(struct tog_view **,
237 struct tog_view **, struct tog_view *, int);
238 static const struct got_error *close_log_view(struct tog_view *);
239 static const struct got_error* set_child_log_view(struct tog_view *,
240 struct tog_view *);
242 static const struct got_error *open_blame_view(struct tog_view *, char *,
243 struct got_object_id *, struct got_repository *);
244 static const struct got_error *show_blame_view(struct tog_view *);
245 static const struct got_error *input_blame_view(struct tog_view **,
246 struct tog_view **, struct tog_view *, int);
247 static const struct got_error *close_blame_view(struct tog_view *);
249 static const struct got_error *open_tree_view(struct tog_view *,
250 struct got_tree_object *, struct got_object_id *, struct got_repository *);
251 static const struct got_error *show_tree_view(struct tog_view *);
252 static const struct got_error *input_tree_view(struct tog_view **,
253 struct tog_view **, struct tog_view *, int);
254 static const struct got_error *close_tree_view(struct tog_view *);
256 static const struct got_error *
257 view_close(struct tog_view *view)
259 const struct got_error *err = NULL;
261 if (view->child)
262 view->child->parent = NULL;
263 if (view->parent)
264 view->parent->child = NULL;
265 if (view->close)
266 err = view->close(view);
267 if (view->panel)
268 del_panel(view->panel);
269 if (view->window)
270 delwin(view->window);
271 free(view);
272 return err;
275 static struct tog_view *
276 view_open(int nlines, int ncols, int begin_y, int begin_x,
277 struct tog_view *parent, enum tog_view_type type)
279 struct tog_view *view = calloc(1, sizeof(*view));
281 if (view == NULL)
282 return NULL;
284 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
285 begin_x = parent->ncols - 80;
287 view->parent = parent;
288 if (parent)
289 parent->child = view;
290 view->type = type;
291 view->lines = LINES;
292 view->cols = COLS;
293 view->nlines = nlines ? nlines : LINES - begin_y;
294 view->ncols = ncols ? ncols : COLS - begin_x;
295 view->begin_y = begin_y;
296 view->begin_x = begin_x;
297 view->window = newwin(nlines, ncols, begin_y, begin_x);
298 if (view->window == NULL) {
299 view_close(view);
300 return NULL;
302 view->panel = new_panel(view->window);
303 if (view->panel == NULL) {
304 view_close(view);
305 return NULL;
308 keypad(view->window, TRUE);
309 return view;
312 static const struct got_error *
313 view_show(struct tog_view *view)
315 const struct got_error *err;
317 if (view->parent) {
318 err = view->parent->show(view->parent);
319 if (err)
320 return err;
321 show_panel(view->parent->panel);
324 err = view->show(view);
325 if (err)
326 return err;
327 show_panel(view->panel);
329 if (view->child && view->child->begin_x > view->begin_x) {
330 err = view->child->show(view->child);
331 if (err)
332 return err;
333 show_panel(view->child->panel);
336 update_panels();
337 doupdate();
339 return err;
342 static const struct got_error *
343 view_resize(struct tog_view *view)
345 int nlines, ncols;
347 while (view) {
348 if (view->lines > LINES)
349 nlines = view->nlines - (view->lines - LINES);
350 else
351 nlines = view->nlines + (LINES - view->lines);
353 if (view->cols > COLS)
354 ncols = view->ncols - (view->cols - COLS);
355 else
356 ncols = view->ncols + (COLS - view->cols);
358 if (wresize(view->window, nlines, ncols) == ERR)
359 return got_error_from_errno();
360 replace_panel(view->panel, view->window);
362 view->nlines = nlines;
363 view->ncols = ncols;
364 view->lines = LINES;
365 view->cols = COLS;
367 view = view->parent;
370 return NULL;
373 static const struct got_error *
374 view_input(struct tog_view **new, struct tog_view **dead,
375 struct tog_view **focus, int *done, struct tog_view *view,
376 struct tog_view_list_head *views)
378 const struct got_error *err = NULL;
379 struct tog_view *next, *prev;
380 int ch;
382 *new = NULL;
383 *dead = NULL;
385 nodelay(stdscr, FALSE);
386 ch = wgetch(view->window);
387 nodelay(stdscr, TRUE);
388 switch (ch) {
389 case ERR:
390 break;
391 case '\t':
392 next = TAILQ_NEXT(view, entry);
393 if (next)
394 *focus = next;
395 else
396 *focus = TAILQ_FIRST(views);
397 view->focussed = 0;
398 (*focus)->focussed = 1;
399 break;
400 case '~':
401 prev = TAILQ_PREV(view, tog_view_list_head, entry);
402 if (prev)
403 *focus = prev;
404 else
405 *focus = TAILQ_LAST(views, tog_view_list_head);
406 view->focussed = 0;
407 (*focus)->focussed = 1;
408 break;
409 case 'q':
410 err = view->input(new, dead, view, ch);
411 *dead = view;
412 break;
413 case 'Q':
414 *done = 1;
415 break;
416 case KEY_RESIZE:
417 err = view_resize(view);
418 if (err)
419 return err;
420 err = view->input(new, dead, view, ch);
421 break;
422 default:
423 err = view->input(new, dead, view, ch);
424 break;
427 return err;
430 static const struct got_error *
431 view_set_child(struct tog_view *view, struct tog_view *child)
433 const struct got_error *err;
435 if (view->set_child) {
436 err = view->set_child(view, child);
437 if (err)
438 return err;
441 view->child = child;
442 return NULL;
445 void
446 view_vborder(struct tog_view *view)
448 if (view->child == NULL)
449 return;
451 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
452 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
455 int
456 view_needs_focus_indication(struct tog_view *view)
458 if (!view->focussed)
459 return 0;
461 if (view->child && view->child->begin_x > view->begin_x)
462 return 1;
464 if (view->parent && view->begin_x > view->parent->begin_x)
465 return 1;
467 return 0;
470 static const struct got_error *
471 view_loop(struct tog_view *view)
473 const struct got_error *err = NULL;
474 struct tog_view_list_head views;
475 struct tog_view *new_view, *dead_view;
476 int done = 0;
478 TAILQ_INIT(&views);
479 TAILQ_INSERT_HEAD(&views, view, entry);
481 view->focussed = 1;
482 while (!TAILQ_EMPTY(&views) && !done) {
483 err = view_show(view);
484 if (err)
485 break;
486 err = view_input(&new_view, &dead_view, &view, &done,
487 view, &views);
488 if (err)
489 break;
490 if (dead_view) {
491 struct tog_view *v, *t;
492 TAILQ_REMOVE(&views, dead_view, entry);
493 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
494 if (v->parent == dead_view) {
495 TAILQ_REMOVE(&views, v, entry);
496 err = view_close(v);
497 if (err)
498 goto done;
501 if (dead_view->parent)
502 view = dead_view->parent;
503 else
504 view = TAILQ_LAST(&views, tog_view_list_head);
505 if (view)
506 view->focussed = 1;
507 err = view_close(dead_view);
508 if (err)
509 goto done;
511 if (new_view) {
512 struct tog_view *v, *t;
513 view->focussed = 0;
514 /* Only allow one view per type. */
515 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
516 if (v->type != new_view->type)
517 continue;
518 TAILQ_REMOVE(&views, v, entry);
519 err = view_close(v);
520 if (err)
521 goto done;
523 TAILQ_INSERT_TAIL(&views, new_view, entry);
524 if (new_view->parent) {
525 err = view_set_child(new_view->parent, new_view);
526 if (err)
527 goto done;
528 new_view->parent->focussed = 0;
530 view = new_view;
531 view->focussed = 1;
534 done:
535 while (!TAILQ_EMPTY(&views)) {
536 view = TAILQ_FIRST(&views);
537 TAILQ_REMOVE(&views, view, entry);
538 view_close(view);
540 return err;
543 __dead static void
544 usage_log(void)
546 endwin();
547 fprintf(stderr,
548 "usage: %s log [-c commit] [-r repository-path] [path]\n",
549 getprogname());
550 exit(1);
553 /* Create newly allocated wide-character string equivalent to a byte string. */
554 static const struct got_error *
555 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
557 char *vis = NULL;
558 const struct got_error *err = NULL;
560 *ws = NULL;
561 *wlen = mbstowcs(NULL, s, 0);
562 if (*wlen == (size_t)-1) {
563 int vislen;
564 if (errno != EILSEQ)
565 return got_error_from_errno();
567 /* byte string invalid in current encoding; try to "fix" it */
568 err = got_mbsavis(&vis, &vislen, s);
569 if (err)
570 return err;
571 *wlen = mbstowcs(NULL, vis, 0);
572 if (*wlen == (size_t)-1) {
573 err = got_error_from_errno(); /* give up */
574 goto done;
578 *ws = calloc(*wlen + 1, sizeof(*ws));
579 if (*ws == NULL) {
580 err = got_error_from_errno();
581 goto done;
584 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
585 err = got_error_from_errno();
586 done:
587 free(vis);
588 if (err) {
589 free(*ws);
590 *ws = NULL;
591 *wlen = 0;
593 return err;
596 /* Format a line for display, ensuring that it won't overflow a width limit. */
597 static const struct got_error *
598 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
600 const struct got_error *err = NULL;
601 int cols = 0;
602 wchar_t *wline = NULL;
603 size_t wlen;
604 int i;
606 *wlinep = NULL;
607 *widthp = 0;
609 err = mbs2ws(&wline, &wlen, line);
610 if (err)
611 return err;
613 i = 0;
614 while (i < wlen && cols < wlimit) {
615 int width = wcwidth(wline[i]);
616 switch (width) {
617 case 0:
618 i++;
619 break;
620 case 1:
621 case 2:
622 if (cols + width <= wlimit)
623 cols += width;
624 i++;
625 break;
626 case -1:
627 if (wline[i] == L'\t')
628 cols += TABSIZE - ((cols + 1) % TABSIZE);
629 i++;
630 break;
631 default:
632 err = got_error_from_errno();
633 goto done;
636 wline[i] = L'\0';
637 if (widthp)
638 *widthp = cols;
639 done:
640 if (err)
641 free(wline);
642 else
643 *wlinep = wline;
644 return err;
647 static const struct got_error *
648 draw_commit(struct tog_view *view, struct got_commit_object *commit,
649 struct got_object_id *id)
651 const struct got_error *err = NULL;
652 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
653 char *logmsg0 = NULL, *logmsg = NULL;
654 char *author0 = NULL, *author = NULL;
655 wchar_t *wlogmsg = NULL, *wauthor = NULL;
656 int author_width, logmsg_width;
657 char *newline, *smallerthan;
658 char *line = NULL;
659 int col, limit;
660 static const size_t date_display_cols = 9;
661 static const size_t author_display_cols = 16;
662 const int avail = view->ncols;
664 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
665 &commit->tm_committer) >= sizeof(datebuf))
666 return got_error(GOT_ERR_NO_SPACE);
668 if (avail < date_display_cols)
669 limit = MIN(sizeof(datebuf) - 1, avail);
670 else
671 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
672 waddnstr(view->window, datebuf, limit);
673 col = limit + 1;
674 if (col > avail)
675 goto done;
677 author0 = strdup(commit->author);
678 if (author0 == NULL) {
679 err = got_error_from_errno();
680 goto done;
682 author = author0;
683 smallerthan = strchr(author, '<');
684 if (smallerthan)
685 *smallerthan = '\0';
686 else {
687 char *at = strchr(author, '@');
688 if (at)
689 *at = '\0';
691 limit = avail - col;
692 err = format_line(&wauthor, &author_width, author, limit);
693 if (err)
694 goto done;
695 waddwstr(view->window, wauthor);
696 col += author_width;
697 while (col <= avail && author_width < author_display_cols + 1) {
698 waddch(view->window, ' ');
699 col++;
700 author_width++;
702 if (col > avail)
703 goto done;
705 logmsg0 = strdup(commit->logmsg);
706 if (logmsg0 == NULL) {
707 err = got_error_from_errno();
708 goto done;
710 logmsg = logmsg0;
711 while (*logmsg == '\n')
712 logmsg++;
713 newline = strchr(logmsg, '\n');
714 if (newline)
715 *newline = '\0';
716 limit = avail - col;
717 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
718 if (err)
719 goto done;
720 waddwstr(view->window, wlogmsg);
721 col += logmsg_width;
722 while (col <= avail) {
723 waddch(view->window, ' ');
724 col++;
726 done:
727 free(logmsg0);
728 free(wlogmsg);
729 free(author0);
730 free(wauthor);
731 free(line);
732 return err;
735 static struct commit_queue_entry *
736 alloc_commit_queue_entry(struct got_commit_object *commit,
737 struct got_object_id *id)
739 struct commit_queue_entry *entry;
741 entry = calloc(1, sizeof(*entry));
742 if (entry == NULL)
743 return NULL;
745 entry->id = id;
746 entry->commit = commit;
747 return entry;
750 static void
751 pop_commit(struct commit_queue *commits)
753 struct commit_queue_entry *entry;
755 entry = TAILQ_FIRST(&commits->head);
756 TAILQ_REMOVE(&commits->head, entry, entry);
757 got_object_commit_close(entry->commit);
758 commits->ncommits--;
759 /* Don't free entry->id! It is owned by the commit graph. */
760 free(entry);
763 static void
764 free_commits(struct commit_queue *commits)
766 while (!TAILQ_EMPTY(&commits->head))
767 pop_commit(commits);
770 static const struct got_error *
771 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
772 struct got_object_id *start_id, int minqueue,
773 struct got_repository *repo, const char *path)
775 const struct got_error *err = NULL;
776 int nqueued = 0;
778 if (start_id) {
779 err = got_commit_graph_iter_start(graph, start_id, repo);
780 if (err)
781 return err;
784 while (nqueued < minqueue) {
785 struct got_object_id *id;
786 struct got_commit_object *commit;
787 struct commit_queue_entry *entry;
789 err = got_commit_graph_iter_next(&id, graph);
790 if (err) {
791 if (err->code == GOT_ERR_ITER_COMPLETED) {
792 err = NULL;
793 break;
795 if (err->code != GOT_ERR_ITER_NEED_MORE)
796 break;
797 err = got_commit_graph_fetch_commits(graph,
798 minqueue, repo);
799 if (err)
800 return err;
801 continue;
804 if (id == NULL)
805 break;
807 err = got_object_open_as_commit(&commit, repo, id);
808 if (err)
809 break;
810 entry = alloc_commit_queue_entry(commit, id);
811 if (entry == NULL) {
812 err = got_error_from_errno();
813 break;
816 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
817 nqueued++;
818 commits->ncommits++;
821 return err;
824 static const struct got_error *
825 fetch_next_commit(struct commit_queue_entry **pentry,
826 struct commit_queue_entry *entry, struct commit_queue *commits,
827 struct got_commit_graph *graph, struct got_repository *repo,
828 const char *path)
830 const struct got_error *err = NULL;
832 *pentry = NULL;
834 err = queue_commits(graph, commits, NULL, 1, repo, path);
835 if (err)
836 return err;
838 /* Next entry to display should now be available. */
839 *pentry = TAILQ_NEXT(entry, entry);
840 return NULL;
843 static const struct got_error *
844 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
846 const struct got_error *err = NULL;
847 struct got_reference *head_ref;
849 *head_id = NULL;
851 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
852 if (err)
853 return err;
855 err = got_ref_resolve(head_id, repo, head_ref);
856 got_ref_close(head_ref);
857 if (err) {
858 *head_id = NULL;
859 return err;
862 return NULL;
865 static const struct got_error *
866 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
867 struct commit_queue_entry **selected, struct commit_queue_entry *first,
868 struct commit_queue *commits, int selected_idx, int limit,
869 struct got_commit_graph *graph, struct got_repository *repo,
870 const char *path)
872 const struct got_error *err = NULL;
873 struct commit_queue_entry *entry;
874 int ncommits, width;
875 char *id_str, *header;
876 wchar_t *wline;
878 entry = first;
879 ncommits = 0;
880 while (entry) {
881 if (ncommits == selected_idx) {
882 *selected = entry;
883 break;
885 entry = TAILQ_NEXT(entry, entry);
886 ncommits++;
889 err = got_object_id_str(&id_str, (*selected)->id);
890 if (err)
891 return err;
893 if (path && strcmp(path, "/") != 0) {
894 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
895 err = got_error_from_errno();
896 free(id_str);
897 return err;
899 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
900 err = got_error_from_errno();
901 free(id_str);
902 return err;
904 free(id_str);
905 err = format_line(&wline, &width, header, view->ncols);
906 if (err) {
907 free(header);
908 return err;
910 free(header);
912 werase(view->window);
914 if (view_needs_focus_indication(view))
915 wstandout(view->window);
916 waddwstr(view->window, wline);
917 if (view_needs_focus_indication(view))
918 wstandend(view->window);
919 if (width < view->ncols)
920 waddch(view->window, '\n');
921 free(wline);
922 if (limit <= 1)
923 return NULL;
925 entry = first;
926 *last = first;
927 ncommits = 0;
928 while (entry) {
929 if (ncommits >= limit - 1)
930 break;
931 if (ncommits == selected_idx)
932 wstandout(view->window);
933 err = draw_commit(view, entry->commit, entry->id);
934 if (ncommits == selected_idx)
935 wstandend(view->window);
936 if (err)
937 break;
938 ncommits++;
939 *last = entry;
940 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
941 err = queue_commits(graph, commits, NULL, 1,
942 repo, path);
943 if (err) {
944 if (err->code != GOT_ERR_ITER_COMPLETED)
945 return err;
946 err = NULL;
949 entry = TAILQ_NEXT(entry, entry);
952 view_vborder(view);
954 return err;
957 static void
958 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
959 struct commit_queue *commits)
961 struct commit_queue_entry *entry;
962 int nscrolled = 0;
964 entry = TAILQ_FIRST(&commits->head);
965 if (*first_displayed_entry == entry)
966 return;
968 entry = *first_displayed_entry;
969 while (entry && nscrolled < maxscroll) {
970 entry = TAILQ_PREV(entry, commit_queue_head, entry);
971 if (entry) {
972 *first_displayed_entry = entry;
973 nscrolled++;
978 static const struct got_error *
979 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
980 struct commit_queue_entry **last_displayed_entry,
981 struct commit_queue *commits, struct got_commit_graph *graph,
982 struct got_repository *repo, const char *path)
984 const struct got_error *err = NULL;
985 struct commit_queue_entry *pentry;
986 int nscrolled = 0;
988 do {
989 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
990 if (pentry == NULL) {
991 err = fetch_next_commit(&pentry, *last_displayed_entry,
992 commits, graph, repo, path);
993 if (err || pentry == NULL)
994 break;
996 *last_displayed_entry = pentry;
998 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
999 if (pentry == NULL)
1000 break;
1001 *first_displayed_entry = pentry;
1002 } while (++nscrolled < maxscroll);
1004 return err;
1007 static const struct got_error *
1008 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1009 struct commit_queue_entry *entry, struct got_repository *repo)
1011 const struct got_error *err;
1012 struct got_object *obj1 = NULL, *obj2 = NULL;
1013 struct got_object_qid *parent_id;
1014 struct tog_view *diff_view;
1016 err = got_object_open(&obj2, repo, entry->id);
1017 if (err)
1018 return err;
1020 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1021 if (parent_id) {
1022 err = got_object_open(&obj1, repo, parent_id->id);
1023 if (err)
1024 goto done;
1027 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1028 if (diff_view == NULL) {
1029 err = got_error_from_errno();
1030 goto done;
1033 err = open_diff_view(diff_view, obj1, obj2, repo);
1034 if (err == NULL)
1035 *new_view = diff_view;
1036 done:
1037 if (obj1)
1038 got_object_close(obj1);
1039 if (obj2)
1040 got_object_close(obj2);
1041 return err;
1044 static const struct got_error *
1045 browse_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 = NULL;
1049 struct got_tree_object *tree;
1050 struct tog_view *tree_view;
1052 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1053 if (err)
1054 return err;
1056 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1057 if (tree_view == NULL)
1058 return got_error_from_errno();
1060 err = open_tree_view(tree_view, tree, entry->id, repo);
1061 if (err)
1062 got_object_tree_close(tree);
1063 else
1064 *new_view = tree_view;
1065 return err;
1068 static const struct got_error *
1069 set_child_log_view(struct tog_view *view, struct tog_view *child)
1071 struct tog_log_view_state *s = &view->state.log;
1072 struct tog_diff_view_state *ds;
1073 struct commit_queue_entry *commit, *child_entry = NULL;
1074 int selected_idx = 0;
1076 if (child->type != TOG_VIEW_DIFF)
1077 return NULL;
1078 ds = &child->state.diff;
1080 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1081 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1082 child_entry = commit;
1083 break;
1086 if (child_entry == NULL)
1087 return NULL;
1089 commit = s->first_displayed_entry;
1090 while (commit) {
1091 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1092 s->selected_entry = child_entry;
1093 s->selected = selected_idx;
1094 break;
1096 if (commit == s->last_displayed_entry)
1097 break;
1098 selected_idx++;
1099 commit = TAILQ_NEXT(commit, entry);
1102 return show_log_view(view);
1105 static const struct got_error *
1106 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1107 struct got_repository *repo, const char *path)
1109 const struct got_error *err = NULL;
1110 struct tog_log_view_state *s = &view->state.log;
1112 err = got_repo_map_path(&s->in_repo_path, repo, path);
1113 if (err != NULL)
1114 goto done;
1116 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1117 0, repo);
1118 if (err)
1119 goto done;
1120 /* The commit queue only contains commits being displayed. */
1121 TAILQ_INIT(&s->commits.head);
1122 s->commits.ncommits = 0;
1125 * Open the initial batch of commits, sorted in commit graph order.
1126 * We keep all commits open throughout the lifetime of the log view
1127 * in order to avoid having to re-fetch commits from disk while
1128 * updating the display.
1130 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1131 repo, s->in_repo_path);
1132 if (err) {
1133 if (err->code != GOT_ERR_ITER_COMPLETED)
1134 goto done;
1135 err = NULL;
1138 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1139 s->selected_entry = s->first_displayed_entry;
1140 s->repo = repo;
1141 s->start_id = got_object_id_dup(start_id);
1142 if (s->start_id == NULL) {
1143 err = got_error_from_errno();
1144 goto done;
1147 view->show = show_log_view;
1148 view->input = input_log_view;
1149 view->close = close_log_view;
1150 view->set_child = set_child_log_view;
1151 done:
1152 return err;
1155 static const struct got_error *
1156 close_log_view(struct tog_view *view)
1158 struct tog_log_view_state *s = &view->state.log;
1160 if (s->graph)
1161 got_commit_graph_close(s->graph);
1162 free_commits(&s->commits);
1163 free(s->in_repo_path);
1164 free(s->start_id);
1165 return NULL;
1168 static const struct got_error *
1169 update_diff_view(struct tog_view *diff_view,
1170 struct got_object_id *commit_id, struct got_commit_object *commit,
1171 struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 struct tog_diff_view_state *ds;
1175 struct got_object *obj1 = NULL, *obj2 = NULL;
1176 struct got_object_qid *parent_id;
1178 ds = &diff_view->state.diff;
1179 if (got_object_id_cmp(ds->id2, commit_id) == 0)
1180 return NULL;
1182 err = got_object_open(&obj2, repo, commit_id);
1183 if (err)
1184 return err;
1186 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1187 if (parent_id) {
1188 err = got_object_open(&obj1, repo, parent_id->id);
1189 if (err)
1190 goto done;
1193 err = close_diff_view(diff_view);
1194 if (err)
1195 goto done;
1197 err = open_diff_view(diff_view, obj1, obj2, repo);
1198 if (err)
1199 goto done;
1200 done:
1201 if (obj1)
1202 got_object_close(obj1);
1203 if (obj2)
1204 got_object_close(obj2);
1205 return err;
1208 static const struct got_error *
1209 show_log_view(struct tog_view *view)
1211 const struct got_error *err = NULL;
1212 struct tog_log_view_state *s = &view->state.log;
1214 err = draw_commits(view, &s->last_displayed_entry,
1215 &s->selected_entry, s->first_displayed_entry,
1216 &s->commits, s->selected, view->nlines, s->graph,
1217 s->repo, s->in_repo_path);
1218 if (err)
1219 return err;
1221 if (view->child && view->child->type == TOG_VIEW_DIFF)
1222 err = update_diff_view(view->child, s->selected_entry->id,
1223 s->selected_entry->commit, s->repo);
1225 return err;
1228 static const struct got_error *
1229 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1230 struct tog_view *view, int ch)
1232 const struct got_error *err = NULL;
1233 struct tog_log_view_state *s = &view->state.log;
1234 char *parent_path;
1236 switch (ch) {
1237 case 'k':
1238 case KEY_UP:
1239 case '[':
1240 if (s->selected > 0)
1241 s->selected--;
1242 if (s->selected > 0)
1243 break;
1244 scroll_up(&s->first_displayed_entry, 1,
1245 &s->commits);
1246 break;
1247 case KEY_PPAGE:
1248 if (TAILQ_FIRST(&s->commits.head) ==
1249 s->first_displayed_entry) {
1250 s->selected = 0;
1251 break;
1253 scroll_up(&s->first_displayed_entry,
1254 view->nlines, &s->commits);
1255 break;
1256 case 'j':
1257 case KEY_DOWN:
1258 case ']':
1259 if (s->selected < MIN(view->nlines - 2,
1260 s->commits.ncommits - 1)) {
1261 s->selected++;
1262 break;
1264 err = scroll_down(&s->first_displayed_entry, 1,
1265 &s->last_displayed_entry, &s->commits,
1266 s->graph, s->repo, s->in_repo_path);
1267 if (err) {
1268 if (err->code != GOT_ERR_ITER_COMPLETED)
1269 break;
1270 err = NULL;
1272 break;
1273 case KEY_NPAGE: {
1274 struct commit_queue_entry *first;
1275 first = s->first_displayed_entry;
1276 err = scroll_down(&s->first_displayed_entry,
1277 view->nlines, &s->last_displayed_entry,
1278 &s->commits, s->graph, s->repo,
1279 s->in_repo_path);
1280 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1281 break;
1282 if (first == s->first_displayed_entry &&
1283 s->selected < MIN(view->nlines - 2,
1284 s->commits.ncommits - 1)) {
1285 /* can't scroll further down */
1286 s->selected = MIN(view->nlines - 2,
1287 s->commits.ncommits - 1);
1289 err = NULL;
1290 break;
1292 case KEY_RESIZE:
1293 if (s->selected > view->nlines - 2)
1294 s->selected = view->nlines - 2;
1295 if (s->selected > s->commits.ncommits - 1)
1296 s->selected = s->commits.ncommits - 1;
1297 break;
1298 case KEY_ENTER:
1299 case '\r':
1300 err = show_commit(new_view, view, s->selected_entry,
1301 s->repo);
1302 break;
1303 case 't':
1304 err = browse_commit(new_view, view, s->selected_entry,
1305 s->repo);
1306 break;
1307 case KEY_BACKSPACE:
1308 if (strcmp(s->in_repo_path, "/") == 0)
1309 break;
1310 parent_path = dirname(s->in_repo_path);
1311 if (parent_path && strcmp(parent_path, ".") != 0) {
1312 struct tog_view *lv;
1313 lv = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1314 if (lv == NULL)
1315 return got_error_from_errno();
1316 err = open_log_view(lv, s->start_id, s->repo,
1317 parent_path);
1318 if (err)
1319 break;
1320 *new_view = lv;
1322 break;
1323 default:
1324 break;
1327 return err;
1330 static const struct got_error *
1331 cmd_log(int argc, char *argv[])
1333 const struct got_error *error;
1334 struct got_repository *repo = NULL;
1335 struct got_object_id *start_id = NULL;
1336 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1337 char *start_commit = NULL;
1338 int ch;
1339 struct tog_view *view;
1341 #ifndef PROFILE
1342 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1343 == -1)
1344 err(1, "pledge");
1345 #endif
1347 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1348 switch (ch) {
1349 case 'c':
1350 start_commit = optarg;
1351 break;
1352 case 'r':
1353 repo_path = realpath(optarg, NULL);
1354 if (repo_path == NULL)
1355 err(1, "-r option");
1356 break;
1357 default:
1358 usage();
1359 /* NOTREACHED */
1363 argc -= optind;
1364 argv += optind;
1366 if (argc == 0)
1367 path = strdup("");
1368 else if (argc == 1)
1369 path = strdup(argv[0]);
1370 else
1371 usage_log();
1372 if (path == NULL)
1373 return got_error_from_errno();
1375 cwd = getcwd(NULL, 0);
1376 if (cwd == NULL) {
1377 error = got_error_from_errno();
1378 goto done;
1380 if (repo_path == NULL) {
1381 repo_path = strdup(cwd);
1382 if (repo_path == NULL) {
1383 error = got_error_from_errno();
1384 goto done;
1388 error = got_repo_open(&repo, repo_path);
1389 if (error != NULL)
1390 goto done;
1392 if (start_commit == NULL) {
1393 error = get_head_commit_id(&start_id, repo);
1394 if (error != NULL)
1395 goto done;
1396 } else {
1397 struct got_object *obj;
1398 error = got_object_open_by_id_str(&obj, repo, start_commit);
1399 if (error == NULL) {
1400 start_id = got_object_id_dup(got_object_get_id(obj));
1401 if (start_id == NULL)
1402 error = got_error_from_errno();
1403 goto done;
1406 if (error != NULL)
1407 goto done;
1409 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1410 if (view == NULL) {
1411 error = got_error_from_errno();
1412 goto done;
1414 error = open_log_view(view, start_id, repo, path);
1415 if (error)
1416 goto done;
1417 error = view_loop(view);
1418 done:
1419 free(repo_path);
1420 free(cwd);
1421 free(path);
1422 free(start_id);
1423 if (repo)
1424 got_repo_close(repo);
1425 return error;
1428 __dead static void
1429 usage_diff(void)
1431 endwin();
1432 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1433 getprogname());
1434 exit(1);
1437 static char *
1438 parse_next_line(FILE *f, size_t *len)
1440 char *line;
1441 size_t linelen;
1442 size_t lineno;
1443 const char delim[3] = { '\0', '\0', '\0'};
1445 line = fparseln(f, &linelen, &lineno, delim, 0);
1446 if (len)
1447 *len = linelen;
1448 return line;
1451 static const struct got_error *
1452 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1453 int *last_displayed_line, int *eof, int max_lines,
1454 char * header)
1456 const struct got_error *err;
1457 int nlines = 0, nprinted = 0;
1458 char *line;
1459 size_t len;
1460 wchar_t *wline;
1461 int width;
1463 rewind(f);
1464 werase(view->window);
1466 if (header) {
1467 err = format_line(&wline, &width, header, view->ncols);
1468 if (err) {
1469 return err;
1472 if (view_needs_focus_indication(view))
1473 wstandout(view->window);
1474 waddwstr(view->window, wline);
1475 if (view_needs_focus_indication(view))
1476 wstandend(view->window);
1477 if (width < view->ncols)
1478 waddch(view->window, '\n');
1480 if (max_lines <= 1)
1481 return NULL;
1482 max_lines--;
1485 *eof = 0;
1486 while (nprinted < max_lines) {
1487 line = parse_next_line(f, &len);
1488 if (line == NULL) {
1489 *eof = 1;
1490 break;
1492 if (++nlines < *first_displayed_line) {
1493 free(line);
1494 continue;
1497 err = format_line(&wline, &width, line, view->ncols);
1498 if (err) {
1499 free(line);
1500 return err;
1502 waddwstr(view->window, wline);
1503 if (width < view->ncols)
1504 waddch(view->window, '\n');
1505 if (++nprinted == 1)
1506 *first_displayed_line = nlines;
1507 free(line);
1508 free(wline);
1509 wline = NULL;
1511 *last_displayed_line = nlines;
1513 view_vborder(view);
1515 return NULL;
1518 static const struct got_error *
1519 open_diff_view(struct tog_view *view, struct got_object *obj1,
1520 struct got_object *obj2, struct got_repository *repo)
1522 const struct got_error *err;
1523 FILE *f;
1525 if (obj1 != NULL && obj2 != NULL &&
1526 got_object_get_type(obj1) != got_object_get_type(obj2))
1527 return got_error(GOT_ERR_OBJ_TYPE);
1529 f = got_opentemp();
1530 if (f == NULL)
1531 return got_error_from_errno();
1533 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1534 case GOT_OBJ_TYPE_BLOB:
1535 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1536 repo, f);
1537 break;
1538 case GOT_OBJ_TYPE_TREE:
1539 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1540 break;
1541 case GOT_OBJ_TYPE_COMMIT:
1542 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1543 break;
1544 default:
1545 return got_error(GOT_ERR_OBJ_TYPE);
1548 fflush(f);
1550 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1551 view->state.diff.id2 = got_object_get_id(obj2);
1552 view->state.diff.f = f;
1553 view->state.diff.first_displayed_line = 1;
1554 view->state.diff.last_displayed_line = view->nlines;
1556 view->show = show_diff_view;
1557 view->input = input_diff_view;
1558 view->close = close_diff_view;
1560 return NULL;
1563 static const struct got_error *
1564 close_diff_view(struct tog_view *view)
1566 const struct got_error *err = NULL;
1568 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1569 err = got_error_from_errno();
1570 return err;
1573 static const struct got_error *
1574 show_diff_view(struct tog_view *view)
1576 const struct got_error *err;
1577 struct tog_diff_view_state *s = &view->state.diff;
1578 char *id_str1 = NULL, *id_str2, *header;
1580 if (s->id1) {
1581 err = got_object_id_str(&id_str1, s->id1);
1582 if (err)
1583 return err;
1585 err = got_object_id_str(&id_str2, s->id2);
1586 if (err)
1587 return err;
1589 if (asprintf(&header, "diff: %s %s",
1590 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1591 err = got_error_from_errno();
1592 free(id_str1);
1593 free(id_str2);
1594 return err;
1596 free(id_str1);
1597 free(id_str2);
1599 return draw_file(view, s->f, &s->first_displayed_line,
1600 &s->last_displayed_line, &s->eof, view->nlines,
1601 header);
1604 static const struct got_error *
1605 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1606 struct tog_view *view, int ch)
1608 const struct got_error *err = NULL;
1609 struct tog_diff_view_state *s = &view->state.diff;
1610 int i;
1612 switch (ch) {
1613 case 'k':
1614 case KEY_UP:
1615 if (s->first_displayed_line > 1)
1616 s->first_displayed_line--;
1617 break;
1618 case KEY_PPAGE:
1619 i = 0;
1620 while (i++ < view->nlines - 1 &&
1621 s->first_displayed_line > 1)
1622 s->first_displayed_line--;
1623 break;
1624 case 'j':
1625 case KEY_DOWN:
1626 if (!s->eof)
1627 s->first_displayed_line++;
1628 break;
1629 case KEY_NPAGE:
1630 case ' ':
1631 i = 0;
1632 while (!s->eof && i++ < view->nlines - 1) {
1633 char *line;
1634 line = parse_next_line(s->f, NULL);
1635 s->first_displayed_line++;
1636 if (line == NULL)
1637 break;
1639 break;
1640 case '[':
1641 case ']': {
1642 struct tog_log_view_state *ls;
1643 struct commit_queue_entry *entry;
1644 struct tog_view *diff_view;
1646 if (view->parent == NULL)
1647 break;
1648 if (view->parent->type != TOG_VIEW_LOG)
1649 break;
1650 ls = &view->parent->state.log;
1652 if (ch == '[') {
1653 entry = TAILQ_PREV(ls->selected_entry,
1654 commit_queue_head, entry);
1655 } else {
1656 entry = TAILQ_NEXT(ls->selected_entry, entry);
1657 if (entry == NULL) {
1658 err = fetch_next_commit(&entry,
1659 ls->selected_entry,
1660 &ls->commits, ls->graph,
1661 ls->repo, ls->in_repo_path);
1662 if (err)
1663 break;
1666 if (entry == NULL)
1667 break;
1668 err = show_commit(&diff_view, view->parent,
1669 entry, ls->repo);
1670 if (err)
1671 break;
1672 *new_view = diff_view;
1673 *dead_view = view;
1674 break;
1676 default:
1677 break;
1680 return err;
1683 static const struct got_error *
1684 cmd_diff(int argc, char *argv[])
1686 const struct got_error *error = NULL;
1687 struct got_repository *repo = NULL;
1688 struct got_object *obj1 = NULL, *obj2 = NULL;
1689 char *repo_path = NULL;
1690 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1691 int ch;
1692 struct tog_view *view;
1694 #ifndef PROFILE
1695 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1696 == -1)
1697 err(1, "pledge");
1698 #endif
1700 while ((ch = getopt(argc, argv, "")) != -1) {
1701 switch (ch) {
1702 default:
1703 usage();
1704 /* NOTREACHED */
1708 argc -= optind;
1709 argv += optind;
1711 if (argc == 0) {
1712 usage_diff(); /* TODO show local worktree changes */
1713 } else if (argc == 2) {
1714 repo_path = getcwd(NULL, 0);
1715 if (repo_path == NULL)
1716 return got_error_from_errno();
1717 obj_id_str1 = argv[0];
1718 obj_id_str2 = argv[1];
1719 } else if (argc == 3) {
1720 repo_path = realpath(argv[0], NULL);
1721 if (repo_path == NULL)
1722 return got_error_from_errno();
1723 obj_id_str1 = argv[1];
1724 obj_id_str2 = argv[2];
1725 } else
1726 usage_diff();
1728 error = got_repo_open(&repo, repo_path);
1729 free(repo_path);
1730 if (error)
1731 goto done;
1733 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1734 if (error)
1735 goto done;
1737 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1738 if (error)
1739 goto done;
1741 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1742 if (view == NULL) {
1743 error = got_error_from_errno();
1744 goto done;
1746 error = open_diff_view(view, obj1, obj2, repo);
1747 if (error)
1748 goto done;
1749 error = view_loop(view);
1750 done:
1751 got_repo_close(repo);
1752 if (obj1)
1753 got_object_close(obj1);
1754 if (obj2)
1755 got_object_close(obj2);
1756 return error;
1759 __dead static void
1760 usage_blame(void)
1762 endwin();
1763 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1764 getprogname());
1765 exit(1);
1768 struct tog_blame_line {
1769 int annotated;
1770 struct got_object_id *id;
1773 static const struct got_error *
1774 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1775 const char *path, struct tog_blame_line *lines, int nlines,
1776 int blame_complete, int selected_line, int *first_displayed_line,
1777 int *last_displayed_line, int *eof, int max_lines)
1779 const struct got_error *err;
1780 int lineno = 0, nprinted = 0;
1781 char *line;
1782 size_t len;
1783 wchar_t *wline;
1784 int width, wlimit;
1785 struct tog_blame_line *blame_line;
1786 struct got_object_id *prev_id = NULL;
1787 char *id_str;
1789 err = got_object_id_str(&id_str, id);
1790 if (err)
1791 return err;
1793 rewind(f);
1794 werase(view->window);
1796 if (asprintf(&line, "commit: %s", id_str) == -1) {
1797 err = got_error_from_errno();
1798 free(id_str);
1799 return err;
1802 err = format_line(&wline, &width, line, view->ncols);
1803 free(line);
1804 line = NULL;
1805 if (view_needs_focus_indication(view))
1806 wstandout(view->window);
1807 waddwstr(view->window, wline);
1808 if (view_needs_focus_indication(view))
1809 wstandend(view->window);
1810 free(wline);
1811 wline = NULL;
1812 if (width < view->ncols)
1813 waddch(view->window, '\n');
1815 if (asprintf(&line, "[%d/%d] %s%s",
1816 *first_displayed_line - 1 + selected_line, nlines,
1817 blame_complete ? "" : "annotating ", path) == -1) {
1818 free(id_str);
1819 return got_error_from_errno();
1821 free(id_str);
1822 err = format_line(&wline, &width, line, view->ncols);
1823 free(line);
1824 line = NULL;
1825 if (err)
1826 return err;
1827 waddwstr(view->window, wline);
1828 free(wline);
1829 wline = NULL;
1830 if (width < view->ncols)
1831 waddch(view->window, '\n');
1833 *eof = 0;
1834 while (nprinted < max_lines - 2) {
1835 line = parse_next_line(f, &len);
1836 if (line == NULL) {
1837 *eof = 1;
1838 break;
1840 if (++lineno < *first_displayed_line) {
1841 free(line);
1842 continue;
1845 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1846 err = format_line(&wline, &width, line, wlimit);
1847 if (err) {
1848 free(line);
1849 return err;
1852 if (nprinted == selected_line - 1)
1853 wstandout(view->window);
1855 blame_line = &lines[lineno - 1];
1856 if (blame_line->annotated && prev_id &&
1857 got_object_id_cmp(prev_id, blame_line->id) == 0)
1858 waddstr(view->window, " ");
1859 else if (blame_line->annotated) {
1860 char *id_str;
1861 err = got_object_id_str(&id_str, blame_line->id);
1862 if (err) {
1863 free(line);
1864 free(wline);
1865 return err;
1867 wprintw(view->window, "%.8s ", id_str);
1868 free(id_str);
1869 prev_id = blame_line->id;
1870 } else {
1871 waddstr(view->window, "........ ");
1872 prev_id = NULL;
1875 waddwstr(view->window, wline);
1876 while (width < wlimit) {
1877 waddch(view->window, ' ');
1878 width++;
1880 if (nprinted == selected_line - 1)
1881 wstandend(view->window);
1882 if (++nprinted == 1)
1883 *first_displayed_line = lineno;
1884 free(line);
1885 free(wline);
1886 wline = NULL;
1888 *last_displayed_line = lineno;
1890 view_vborder(view);
1892 return NULL;
1895 static const struct got_error *
1896 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1898 const struct got_error *err = NULL;
1899 struct tog_blame_cb_args *a = arg;
1900 struct tog_blame_line *line;
1902 if (nlines != a->nlines ||
1903 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1904 return got_error(GOT_ERR_RANGE);
1906 if (pthread_mutex_lock(a->mutex) != 0)
1907 return got_error_from_errno();
1909 if (*a->quit) { /* user has quit the blame view */
1910 err = got_error(GOT_ERR_ITER_COMPLETED);
1911 goto done;
1914 if (lineno == -1)
1915 goto done; /* no change in this commit */
1917 line = &a->lines[lineno - 1];
1918 if (line->annotated)
1919 goto done;
1921 line->id = got_object_id_dup(id);
1922 if (line->id == NULL) {
1923 err = got_error_from_errno();
1924 goto done;
1926 line->annotated = 1;
1928 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1929 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1930 a->last_displayed_line, a->eof, a->view->nlines);
1931 done:
1932 if (pthread_mutex_unlock(a->mutex) != 0)
1933 return got_error_from_errno();
1934 return err;
1937 static void *
1938 blame_thread(void *arg)
1940 const struct got_error *err;
1941 struct tog_blame_thread_args *ta = arg;
1942 struct tog_blame_cb_args *a = ta->cb_args;
1944 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1945 blame_cb, ta->cb_args);
1947 if (pthread_mutex_lock(a->mutex) != 0)
1948 return (void *)got_error_from_errno();
1950 got_repo_close(ta->repo);
1951 ta->repo = NULL;
1952 *ta->complete = 1;
1953 if (!err)
1954 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1955 a->lines, a->nlines, 1, *a->selected_line,
1956 a->first_displayed_line, a->last_displayed_line, a->eof,
1957 a->view->nlines);
1959 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1960 err = got_error_from_errno();
1962 return (void *)err;
1965 static struct got_object_id *
1966 get_selected_commit_id(struct tog_blame_line *lines,
1967 int first_displayed_line, int selected_line)
1969 struct tog_blame_line *line;
1971 line = &lines[first_displayed_line - 1 + selected_line - 1];
1972 if (!line->annotated)
1973 return NULL;
1975 return line->id;
1978 static const struct got_error *
1979 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1980 struct tog_blame_line *lines, int first_displayed_line,
1981 int selected_line, struct got_repository *repo)
1983 const struct got_error *err = NULL;
1984 struct got_commit_object *commit = NULL;
1985 struct got_object_id *selected_id;
1986 struct got_object_qid *pid;
1988 *pobj = NULL;
1989 *obj = NULL;
1991 selected_id = get_selected_commit_id(lines,
1992 first_displayed_line, selected_line);
1993 if (selected_id == NULL)
1994 return NULL;
1996 err = got_object_open(obj, repo, selected_id);
1997 if (err)
1998 goto done;
2000 err = got_object_commit_open(&commit, repo, *obj);
2001 if (err)
2002 goto done;
2004 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2005 if (pid) {
2006 err = got_object_open(pobj, repo, pid->id);
2007 if (err)
2008 goto done;
2010 done:
2011 if (commit)
2012 got_object_commit_close(commit);
2013 return err;
2016 static const struct got_error *
2017 stop_blame(struct tog_blame *blame)
2019 const struct got_error *err = NULL;
2020 int i;
2022 if (blame->thread) {
2023 if (pthread_join(blame->thread, (void **)&err) != 0)
2024 err = got_error_from_errno();
2025 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2026 err = NULL;
2027 blame->thread = NULL;
2029 if (blame->thread_args.repo) {
2030 got_repo_close(blame->thread_args.repo);
2031 blame->thread_args.repo = NULL;
2033 if (blame->f) {
2034 fclose(blame->f);
2035 blame->f = NULL;
2037 for (i = 0; i < blame->nlines; i++)
2038 free(blame->lines[i].id);
2039 free(blame->lines);
2040 blame->lines = NULL;
2041 free(blame->cb_args.commit_id);
2042 blame->cb_args.commit_id = NULL;
2044 return err;
2047 static const struct got_error *
2048 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2049 struct tog_view *view, int *blame_complete,
2050 int *first_displayed_line, int *last_displayed_line,
2051 int *selected_line, int *done, int *eof, const char *path,
2052 struct got_object_id *commit_id,
2053 struct got_repository *repo)
2055 const struct got_error *err = NULL;
2056 struct got_blob_object *blob = NULL;
2057 struct got_repository *thread_repo = NULL;
2058 struct got_object_id *obj_id = NULL;
2059 struct got_object *obj = NULL;
2061 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2062 if (err)
2063 goto done;
2065 err = got_object_open(&obj, repo, obj_id);
2066 if (err)
2067 goto done;
2069 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2070 err = got_error(GOT_ERR_OBJ_TYPE);
2071 goto done;
2074 err = got_object_blob_open(&blob, repo, obj, 8192);
2075 if (err)
2076 goto done;
2077 blame->f = got_opentemp();
2078 if (blame->f == NULL) {
2079 err = got_error_from_errno();
2080 goto done;
2082 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2083 blame->f, blob);
2084 if (err)
2085 goto done;
2087 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2088 if (blame->lines == NULL) {
2089 err = got_error_from_errno();
2090 goto done;
2093 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2094 if (err)
2095 goto done;
2097 blame->cb_args.view = view;
2098 blame->cb_args.lines = blame->lines;
2099 blame->cb_args.nlines = blame->nlines;
2100 blame->cb_args.mutex = mutex;
2101 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2102 if (blame->cb_args.commit_id == NULL) {
2103 err = got_error_from_errno();
2104 goto done;
2106 blame->cb_args.f = blame->f;
2107 blame->cb_args.path = path;
2108 blame->cb_args.first_displayed_line = first_displayed_line;
2109 blame->cb_args.selected_line = selected_line;
2110 blame->cb_args.last_displayed_line = last_displayed_line;
2111 blame->cb_args.quit = done;
2112 blame->cb_args.eof = eof;
2114 blame->thread_args.path = path;
2115 blame->thread_args.repo = thread_repo;
2116 blame->thread_args.cb_args = &blame->cb_args;
2117 blame->thread_args.complete = blame_complete;
2118 *blame_complete = 0;
2120 if (pthread_create(&blame->thread, NULL, blame_thread,
2121 &blame->thread_args) != 0) {
2122 err = got_error_from_errno();
2123 goto done;
2126 done:
2127 if (blob)
2128 got_object_blob_close(blob);
2129 free(obj_id);
2130 if (obj)
2131 got_object_close(obj);
2132 if (err)
2133 stop_blame(blame);
2134 return err;
2137 static const struct got_error *
2138 open_blame_view(struct tog_view *view, char *path,
2139 struct got_object_id *commit_id, struct got_repository *repo)
2141 const struct got_error *err = NULL;
2142 struct tog_blame_view_state *s = &view->state.blame;
2144 SIMPLEQ_INIT(&s->blamed_commits);
2146 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2147 return got_error_from_errno();
2149 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2150 if (err)
2151 return err;
2153 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2154 s->first_displayed_line = 1;
2155 s->last_displayed_line = view->nlines;
2156 s->selected_line = 1;
2157 s->blame_complete = 0;
2158 s->path = path;
2159 if (s->path == NULL)
2160 return got_error_from_errno();
2161 s->repo = repo;
2162 s->commit_id = commit_id;
2163 memset(&s->blame, 0, sizeof(s->blame));
2165 view->show = show_blame_view;
2166 view->input = input_blame_view;
2167 view->close = close_blame_view;
2169 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2170 &s->first_displayed_line, &s->last_displayed_line,
2171 &s->selected_line, &s->done, &s->eof, s->path,
2172 s->blamed_commit->id, s->repo);
2175 static const struct got_error *
2176 close_blame_view(struct tog_view *view)
2178 const struct got_error *err = NULL;
2179 struct tog_blame_view_state *s = &view->state.blame;
2181 if (s->blame.thread)
2182 err = stop_blame(&s->blame);
2184 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2185 struct got_object_qid *blamed_commit;
2186 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2187 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2188 got_object_qid_free(blamed_commit);
2191 free(s->path);
2193 return err;
2196 static const struct got_error *
2197 show_blame_view(struct tog_view *view)
2199 const struct got_error *err = NULL;
2200 struct tog_blame_view_state *s = &view->state.blame;
2202 if (pthread_mutex_lock(&s->mutex) != 0)
2203 return got_error_from_errno();
2205 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2206 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2207 s->selected_line, &s->first_displayed_line,
2208 &s->last_displayed_line, &s->eof, view->nlines);
2210 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2211 err = got_error_from_errno();
2213 return err;
2216 static const struct got_error *
2217 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2218 struct tog_view *view, int ch)
2220 const struct got_error *err = NULL, *thread_err = NULL;
2221 struct got_object *obj = NULL, *pobj = NULL;
2222 struct tog_view *diff_view;
2223 struct tog_blame_view_state *s = &view->state.blame;
2225 if (pthread_mutex_lock(&s->mutex) != 0) {
2226 err = got_error_from_errno();
2227 goto done;
2230 switch (ch) {
2231 case 'q':
2232 s->done = 1;
2233 if (pthread_mutex_unlock(&s->mutex) != 0) {
2234 err = got_error_from_errno();
2235 goto done;
2237 return stop_blame(&s->blame);
2238 case 'k':
2239 case KEY_UP:
2240 if (s->selected_line > 1)
2241 s->selected_line--;
2242 else if (s->selected_line == 1 &&
2243 s->first_displayed_line > 1)
2244 s->first_displayed_line--;
2245 break;
2246 case KEY_PPAGE:
2247 if (s->first_displayed_line == 1) {
2248 s->selected_line = 1;
2249 break;
2251 if (s->first_displayed_line > view->nlines - 2)
2252 s->first_displayed_line -=
2253 (view->nlines - 2);
2254 else
2255 s->first_displayed_line = 1;
2256 break;
2257 case 'j':
2258 case KEY_DOWN:
2259 if (s->selected_line < view->nlines - 2 &&
2260 s->first_displayed_line +
2261 s->selected_line <= s->blame.nlines)
2262 s->selected_line++;
2263 else if (s->last_displayed_line <
2264 s->blame.nlines)
2265 s->first_displayed_line++;
2266 break;
2267 case 'b':
2268 case 'p': {
2269 struct got_object_id *id;
2270 id = get_selected_commit_id(s->blame.lines,
2271 s->first_displayed_line, s->selected_line);
2272 if (id == NULL || got_object_id_cmp(id,
2273 s->blamed_commit->id) == 0)
2274 break;
2275 err = open_selected_commit(&pobj, &obj,
2276 s->blame.lines, s->first_displayed_line,
2277 s->selected_line, s->repo);
2278 if (err)
2279 break;
2280 if (pobj == NULL && obj == NULL)
2281 break;
2282 if (ch == 'p' && pobj == NULL)
2283 break;
2284 s->done = 1;
2285 if (pthread_mutex_unlock(&s->mutex) != 0) {
2286 err = got_error_from_errno();
2287 goto done;
2289 thread_err = stop_blame(&s->blame);
2290 s->done = 0;
2291 if (pthread_mutex_lock(&s->mutex) != 0) {
2292 err = got_error_from_errno();
2293 goto done;
2295 if (thread_err)
2296 break;
2297 id = got_object_get_id(ch == 'b' ? obj : pobj);
2298 got_object_close(obj);
2299 obj = NULL;
2300 if (pobj) {
2301 got_object_close(pobj);
2302 pobj = NULL;
2304 err = got_object_qid_alloc(&s->blamed_commit, id);
2305 if (err)
2306 goto done;
2307 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2308 s->blamed_commit, entry);
2309 err = run_blame(&s->blame, &s->mutex, view,
2310 &s->blame_complete,
2311 &s->first_displayed_line,
2312 &s->last_displayed_line,
2313 &s->selected_line, &s->done, &s->eof,
2314 s->path, s->blamed_commit->id, s->repo);
2315 if (err)
2316 break;
2317 break;
2319 case 'B': {
2320 struct got_object_qid *first;
2321 first = SIMPLEQ_FIRST(&s->blamed_commits);
2322 if (!got_object_id_cmp(first->id, s->commit_id))
2323 break;
2324 s->done = 1;
2325 if (pthread_mutex_unlock(&s->mutex) != 0) {
2326 err = got_error_from_errno();
2327 goto done;
2329 thread_err = stop_blame(&s->blame);
2330 s->done = 0;
2331 if (pthread_mutex_lock(&s->mutex) != 0) {
2332 err = got_error_from_errno();
2333 goto done;
2335 if (thread_err)
2336 break;
2337 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2338 got_object_qid_free(s->blamed_commit);
2339 s->blamed_commit =
2340 SIMPLEQ_FIRST(&s->blamed_commits);
2341 err = run_blame(&s->blame, &s->mutex, view,
2342 &s->blame_complete,
2343 &s->first_displayed_line,
2344 &s->last_displayed_line,
2345 &s->selected_line, &s->done, &s->eof, s->path,
2346 s->blamed_commit->id, s->repo);
2347 if (err)
2348 break;
2349 break;
2351 case KEY_ENTER:
2352 case '\r':
2353 err = open_selected_commit(&pobj, &obj,
2354 s->blame.lines, s->first_displayed_line,
2355 s->selected_line, s->repo);
2356 if (err)
2357 break;
2358 if (pobj == NULL && obj == NULL)
2359 break;
2360 diff_view = view_open(0, 0, 0, 0, view,
2361 TOG_VIEW_DIFF);
2362 if (diff_view == NULL) {
2363 err = got_error_from_errno();
2364 break;
2366 err = open_diff_view(diff_view, pobj, obj,
2367 s->repo);
2368 if (err) {
2369 view_close(diff_view);
2370 break;
2372 *new_view = diff_view;
2373 if (pobj) {
2374 got_object_close(pobj);
2375 pobj = NULL;
2377 got_object_close(obj);
2378 obj = NULL;
2379 if (err)
2380 break;
2381 break;
2382 case KEY_NPAGE:
2383 case ' ':
2384 if (s->last_displayed_line >= s->blame.nlines &&
2385 s->selected_line < view->nlines - 2) {
2386 s->selected_line = MIN(s->blame.nlines,
2387 view->nlines - 2);
2388 break;
2390 if (s->last_displayed_line + view->nlines - 2
2391 <= s->blame.nlines)
2392 s->first_displayed_line +=
2393 view->nlines - 2;
2394 else
2395 s->first_displayed_line =
2396 s->blame.nlines -
2397 (view->nlines - 3);
2398 break;
2399 case KEY_RESIZE:
2400 if (s->selected_line > view->nlines - 2) {
2401 s->selected_line = MIN(s->blame.nlines,
2402 view->nlines - 2);
2404 break;
2405 default:
2406 break;
2409 if (pthread_mutex_unlock(&s->mutex) != 0)
2410 err = got_error_from_errno();
2411 done:
2412 if (pobj)
2413 got_object_close(pobj);
2414 return thread_err ? thread_err : err;
2417 static const struct got_error *
2418 cmd_blame(int argc, char *argv[])
2420 const struct got_error *error;
2421 struct got_repository *repo = NULL;
2422 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2423 struct got_object_id *commit_id = NULL;
2424 char *commit_id_str = NULL;
2425 int ch;
2426 struct tog_view *view;
2428 #ifndef PROFILE
2429 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2430 == -1)
2431 err(1, "pledge");
2432 #endif
2434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2435 switch (ch) {
2436 case 'c':
2437 commit_id_str = optarg;
2438 break;
2439 case 'r':
2440 repo_path = realpath(optarg, NULL);
2441 if (repo_path == NULL)
2442 err(1, "-r option");
2443 break;
2444 default:
2445 usage();
2446 /* NOTREACHED */
2450 argc -= optind;
2451 argv += optind;
2453 if (argc == 1)
2454 path = argv[0];
2455 else
2456 usage_blame();
2458 cwd = getcwd(NULL, 0);
2459 if (cwd == NULL) {
2460 error = got_error_from_errno();
2461 goto done;
2463 if (repo_path == NULL) {
2464 repo_path = strdup(cwd);
2465 if (repo_path == NULL) {
2466 error = got_error_from_errno();
2467 goto done;
2472 error = got_repo_open(&repo, repo_path);
2473 if (error != NULL)
2474 return error;
2476 error = got_repo_map_path(&in_repo_path, repo, path);
2477 if (error != NULL)
2478 goto done;
2480 if (commit_id_str == NULL) {
2481 struct got_reference *head_ref;
2482 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2483 if (error != NULL)
2484 goto done;
2485 error = got_ref_resolve(&commit_id, repo, head_ref);
2486 got_ref_close(head_ref);
2487 } else {
2488 struct got_object *obj;
2489 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2490 if (error != NULL)
2491 goto done;
2492 commit_id = got_object_id_dup(got_object_get_id(obj));
2493 if (commit_id == NULL)
2494 error = got_error_from_errno();
2495 got_object_close(obj);
2497 if (error != NULL)
2498 goto done;
2500 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2501 if (view == NULL) {
2502 error = got_error_from_errno();
2503 goto done;
2505 error = open_blame_view(view, in_repo_path, commit_id, repo);
2506 if (error)
2507 goto done;
2508 error = view_loop(view);
2509 done:
2510 free(repo_path);
2511 free(cwd);
2512 free(commit_id);
2513 if (repo)
2514 got_repo_close(repo);
2515 return error;
2518 static const struct got_error *
2519 draw_tree_entries(struct tog_view *view,
2520 struct got_tree_entry **first_displayed_entry,
2521 struct got_tree_entry **last_displayed_entry,
2522 struct got_tree_entry **selected_entry, int *ndisplayed,
2523 const char *label, int show_ids, const char *parent_path,
2524 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2526 const struct got_error *err = NULL;
2527 struct got_tree_entry *te;
2528 wchar_t *wline;
2529 int width, n;
2531 *ndisplayed = 0;
2533 werase(view->window);
2535 if (limit == 0)
2536 return NULL;
2538 err = format_line(&wline, &width, label, view->ncols);
2539 if (err)
2540 return err;
2541 if (view_needs_focus_indication(view))
2542 wstandout(view->window);
2543 waddwstr(view->window, wline);
2544 if (view_needs_focus_indication(view))
2545 wstandend(view->window);
2546 free(wline);
2547 wline = NULL;
2548 if (width < view->ncols)
2549 waddch(view->window, '\n');
2550 if (--limit <= 0)
2551 return NULL;
2552 err = format_line(&wline, &width, parent_path, view->ncols);
2553 if (err)
2554 return err;
2555 waddwstr(view->window, wline);
2556 free(wline);
2557 wline = NULL;
2558 if (width < view->ncols)
2559 waddch(view->window, '\n');
2560 if (--limit <= 0)
2561 return NULL;
2562 waddch(view->window, '\n');
2563 if (--limit <= 0)
2564 return NULL;
2566 te = SIMPLEQ_FIRST(&entries->head);
2567 if (*first_displayed_entry == NULL) {
2568 if (selected == 0) {
2569 wstandout(view->window);
2570 *selected_entry = NULL;
2572 waddstr(view->window, " ..\n"); /* parent directory */
2573 if (selected == 0)
2574 wstandend(view->window);
2575 (*ndisplayed)++;
2576 if (--limit <= 0)
2577 return NULL;
2578 n = 1;
2579 } else {
2580 n = 0;
2581 while (te != *first_displayed_entry)
2582 te = SIMPLEQ_NEXT(te, entry);
2585 while (te) {
2586 char *line = NULL, *id_str = NULL;
2588 if (show_ids) {
2589 err = got_object_id_str(&id_str, te->id);
2590 if (err)
2591 return got_error_from_errno();
2593 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2594 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2595 free(id_str);
2596 return got_error_from_errno();
2598 free(id_str);
2599 err = format_line(&wline, &width, line, view->ncols);
2600 if (err) {
2601 free(line);
2602 break;
2604 if (n == selected) {
2605 wstandout(view->window);
2606 *selected_entry = te;
2608 waddwstr(view->window, wline);
2609 if (width < view->ncols)
2610 waddch(view->window, '\n');
2611 if (n == selected)
2612 wstandend(view->window);
2613 free(line);
2614 free(wline);
2615 wline = NULL;
2616 n++;
2617 (*ndisplayed)++;
2618 *last_displayed_entry = te;
2619 if (--limit <= 0)
2620 break;
2621 te = SIMPLEQ_NEXT(te, entry);
2624 view_vborder(view);
2625 return err;
2628 static void
2629 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2630 const struct got_tree_entries *entries, int isroot)
2632 struct got_tree_entry *te, *prev;
2633 int i;
2635 if (*first_displayed_entry == NULL)
2636 return;
2638 te = SIMPLEQ_FIRST(&entries->head);
2639 if (*first_displayed_entry == te) {
2640 if (!isroot)
2641 *first_displayed_entry = NULL;
2642 return;
2645 /* XXX this is stupid... switch to TAILQ? */
2646 for (i = 0; i < maxscroll; i++) {
2647 while (te != *first_displayed_entry) {
2648 prev = te;
2649 te = SIMPLEQ_NEXT(te, entry);
2651 *first_displayed_entry = prev;
2652 te = SIMPLEQ_FIRST(&entries->head);
2654 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2655 *first_displayed_entry = NULL;
2658 static void
2659 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2660 struct got_tree_entry *last_displayed_entry,
2661 const struct got_tree_entries *entries)
2663 struct got_tree_entry *next;
2664 int n = 0;
2666 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2667 return;
2669 if (*first_displayed_entry)
2670 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2671 else
2672 next = SIMPLEQ_FIRST(&entries->head);
2673 while (next) {
2674 *first_displayed_entry = next;
2675 if (++n >= maxscroll)
2676 break;
2677 next = SIMPLEQ_NEXT(next, entry);
2681 static const struct got_error *
2682 tree_entry_path(char **path, struct tog_parent_trees *parents,
2683 struct got_tree_entry *te)
2685 const struct got_error *err = NULL;
2686 struct tog_parent_tree *pt;
2687 size_t len = 2; /* for leading slash and NUL */
2689 TAILQ_FOREACH(pt, parents, entry)
2690 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2691 if (te)
2692 len += strlen(te->name);
2694 *path = calloc(1, len);
2695 if (path == NULL)
2696 return got_error_from_errno();
2698 (*path)[0] = '/';
2699 pt = TAILQ_LAST(parents, tog_parent_trees);
2700 while (pt) {
2701 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2702 err = got_error(GOT_ERR_NO_SPACE);
2703 goto done;
2705 if (strlcat(*path, "/", len) >= len) {
2706 err = got_error(GOT_ERR_NO_SPACE);
2707 goto done;
2709 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2711 if (te) {
2712 if (strlcat(*path, te->name, len) >= len) {
2713 err = got_error(GOT_ERR_NO_SPACE);
2714 goto done;
2717 done:
2718 if (err) {
2719 free(*path);
2720 *path = NULL;
2722 return err;
2725 static const struct got_error *
2726 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2727 struct got_tree_entry *te, struct tog_parent_trees *parents,
2728 struct got_object_id *commit_id, struct got_repository *repo)
2730 const struct got_error *err = NULL;
2731 char *path;
2732 struct tog_view *blame_view;
2734 err = tree_entry_path(&path, parents, te);
2735 if (err)
2736 return err;
2738 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2739 if (blame_view == NULL)
2740 return got_error_from_errno();
2742 err = open_blame_view(blame_view, path, commit_id, repo);
2743 if (err) {
2744 view_close(blame_view);
2745 free(path);
2746 } else
2747 *new_view = blame_view;
2748 return err;
2751 static const struct got_error *
2752 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2753 struct got_tree_entry *te, struct tog_parent_trees *parents,
2754 struct got_object_id *commit_id, struct got_repository *repo)
2756 struct tog_view *log_view;
2757 const struct got_error *err = NULL;
2758 char *path;
2760 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2761 if (log_view == NULL)
2762 return got_error_from_errno();
2764 err = tree_entry_path(&path, parents, te);
2765 if (err)
2766 return err;
2768 err = open_log_view(log_view, commit_id, repo, path);
2769 if (err)
2770 view_close(log_view);
2771 else
2772 *new_view = log_view;
2773 free(path);
2774 return err;
2777 static const struct got_error *
2778 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2779 struct got_object_id *commit_id, struct got_repository *repo)
2781 const struct got_error *err = NULL;
2782 char *commit_id_str = NULL;
2783 struct tog_tree_view_state *s = &view->state.tree;
2785 TAILQ_INIT(&s->parents);
2787 err = got_object_id_str(&commit_id_str, commit_id);
2788 if (err != NULL)
2789 goto done;
2791 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2792 err = got_error_from_errno();
2793 goto done;
2796 s->root = s->tree = root;
2797 s->entries = got_object_tree_get_entries(root);
2798 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2799 s->commit_id = got_object_id_dup(commit_id);
2800 if (s->commit_id == NULL) {
2801 err = got_error_from_errno();
2802 goto done;
2804 s->repo = repo;
2806 view->show = show_tree_view;
2807 view->input = input_tree_view;
2808 view->close = close_tree_view;
2809 done:
2810 free(commit_id_str);
2811 if (err) {
2812 free(s->tree_label);
2813 s->tree_label = NULL;
2815 return err;
2818 static const struct got_error *
2819 close_tree_view(struct tog_view *view)
2821 struct tog_tree_view_state *s = &view->state.tree;
2823 free(s->tree_label);
2824 s->tree_label = NULL;
2825 free(s->commit_id);
2826 s->commit_id = NULL;
2827 while (!TAILQ_EMPTY(&s->parents)) {
2828 struct tog_parent_tree *parent;
2829 parent = TAILQ_FIRST(&s->parents);
2830 TAILQ_REMOVE(&s->parents, parent, entry);
2831 free(parent);
2834 if (s->tree != s->root)
2835 got_object_tree_close(s->tree);
2836 got_object_tree_close(s->root);
2838 return NULL;
2841 static const struct got_error *
2842 show_tree_view(struct tog_view *view)
2844 const struct got_error *err = NULL;
2845 struct tog_tree_view_state *s = &view->state.tree;
2846 char *parent_path;
2848 err = tree_entry_path(&parent_path, &s->parents, NULL);
2849 if (err)
2850 return err;
2852 err = draw_tree_entries(view, &s->first_displayed_entry,
2853 &s->last_displayed_entry, &s->selected_entry,
2854 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2855 s->entries, s->selected, view->nlines, s->tree == s->root);
2856 free(parent_path);
2857 return err;
2860 static const struct got_error *
2861 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2862 struct tog_view *view, int ch)
2864 const struct got_error *err = NULL;
2865 struct tog_tree_view_state *s = &view->state.tree;
2867 switch (ch) {
2868 case 'i':
2869 s->show_ids = !s->show_ids;
2870 break;
2871 case 'l':
2872 if (s->selected_entry) {
2873 err = log_tree_entry(new_view, view,
2874 s->selected_entry, &s->parents,
2875 s->commit_id, s->repo);
2877 break;
2878 case 'k':
2879 case KEY_UP:
2880 if (s->selected > 0)
2881 s->selected--;
2882 if (s->selected > 0)
2883 break;
2884 tree_scroll_up(&s->first_displayed_entry, 1,
2885 s->entries, s->tree == s->root);
2886 break;
2887 case KEY_PPAGE:
2888 if (SIMPLEQ_FIRST(&s->entries->head) ==
2889 s->first_displayed_entry) {
2890 if (s->tree != s->root)
2891 s->first_displayed_entry = NULL;
2892 s->selected = 0;
2893 break;
2895 tree_scroll_up(&s->first_displayed_entry,
2896 view->nlines, s->entries,
2897 s->tree == s->root);
2898 break;
2899 case 'j':
2900 case KEY_DOWN:
2901 if (s->selected < s->ndisplayed - 1) {
2902 s->selected++;
2903 break;
2905 tree_scroll_down(&s->first_displayed_entry, 1,
2906 s->last_displayed_entry, s->entries);
2907 break;
2908 case KEY_NPAGE:
2909 tree_scroll_down(&s->first_displayed_entry,
2910 view->nlines, s->last_displayed_entry,
2911 s->entries);
2912 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2913 entry))
2914 break;
2915 /* can't scroll any further; move cursor down */
2916 if (s->selected < s->ndisplayed - 1)
2917 s->selected = s->ndisplayed - 1;
2918 break;
2919 case KEY_ENTER:
2920 case '\r':
2921 if (s->selected_entry == NULL) {
2922 struct tog_parent_tree *parent;
2923 case KEY_BACKSPACE:
2924 /* user selected '..' */
2925 if (s->tree == s->root)
2926 break;
2927 parent = TAILQ_FIRST(&s->parents);
2928 TAILQ_REMOVE(&s->parents, parent,
2929 entry);
2930 got_object_tree_close(s->tree);
2931 s->tree = parent->tree;
2932 s->entries =
2933 got_object_tree_get_entries(s->tree);
2934 s->first_displayed_entry =
2935 parent->first_displayed_entry;
2936 s->selected_entry =
2937 parent->selected_entry;
2938 s->selected = parent->selected;
2939 free(parent);
2940 } else if (S_ISDIR(s->selected_entry->mode)) {
2941 struct tog_parent_tree *parent;
2942 struct got_tree_object *child;
2943 err = got_object_open_as_tree(&child,
2944 s->repo, s->selected_entry->id);
2945 if (err)
2946 break;
2947 parent = calloc(1, sizeof(*parent));
2948 if (parent == NULL) {
2949 err = got_error_from_errno();
2950 break;
2952 parent->tree = s->tree;
2953 parent->first_displayed_entry =
2954 s->first_displayed_entry;
2955 parent->selected_entry = s->selected_entry;
2956 parent->selected = s->selected;
2957 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2958 s->tree = child;
2959 s->entries =
2960 got_object_tree_get_entries(s->tree);
2961 s->selected = 0;
2962 s->first_displayed_entry = NULL;
2963 } else if (S_ISREG(s->selected_entry->mode)) {
2964 err = blame_tree_entry(new_view, view,
2965 s->selected_entry, &s->parents,
2966 s->commit_id, s->repo);
2967 if (err)
2968 break;
2970 break;
2971 case KEY_RESIZE:
2972 if (s->selected > view->nlines)
2973 s->selected = s->ndisplayed - 1;
2974 break;
2975 default:
2976 break;
2979 return err;
2982 __dead static void
2983 usage_tree(void)
2985 endwin();
2986 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2987 getprogname());
2988 exit(1);
2991 static const struct got_error *
2992 cmd_tree(int argc, char *argv[])
2994 const struct got_error *error;
2995 struct got_repository *repo = NULL;
2996 char *repo_path = NULL;
2997 struct got_object_id *commit_id = NULL;
2998 char *commit_id_arg = NULL;
2999 struct got_commit_object *commit = NULL;
3000 struct got_tree_object *tree = NULL;
3001 int ch;
3002 struct tog_view *view;
3004 #ifndef PROFILE
3005 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3006 == -1)
3007 err(1, "pledge");
3008 #endif
3010 while ((ch = getopt(argc, argv, "c:")) != -1) {
3011 switch (ch) {
3012 case 'c':
3013 commit_id_arg = optarg;
3014 break;
3015 default:
3016 usage();
3017 /* NOTREACHED */
3021 argc -= optind;
3022 argv += optind;
3024 if (argc == 0) {
3025 repo_path = getcwd(NULL, 0);
3026 if (repo_path == NULL)
3027 return got_error_from_errno();
3028 } else if (argc == 1) {
3029 repo_path = realpath(argv[0], NULL);
3030 if (repo_path == NULL)
3031 return got_error_from_errno();
3032 } else
3033 usage_log();
3035 error = got_repo_open(&repo, repo_path);
3036 free(repo_path);
3037 if (error != NULL)
3038 return error;
3040 if (commit_id_arg == NULL) {
3041 error = get_head_commit_id(&commit_id, repo);
3042 if (error != NULL)
3043 goto done;
3044 } else {
3045 struct got_object *obj;
3046 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3047 if (error == NULL) {
3048 commit_id = got_object_id_dup(got_object_get_id(obj));
3049 if (commit_id == NULL)
3050 error = got_error_from_errno();
3053 if (error != NULL)
3054 goto done;
3056 error = got_object_open_as_commit(&commit, repo, commit_id);
3057 if (error != NULL)
3058 goto done;
3060 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3061 if (error != NULL)
3062 goto done;
3064 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3065 if (view == NULL) {
3066 error = got_error_from_errno();
3067 goto done;
3069 error = open_tree_view(view, tree, commit_id, repo);
3070 if (error)
3071 goto done;
3072 error = view_loop(view);
3073 done:
3074 free(commit_id);
3075 if (commit)
3076 got_object_commit_close(commit);
3077 if (tree)
3078 got_object_tree_close(tree);
3079 if (repo)
3080 got_repo_close(repo);
3081 return error;
3083 static void
3084 init_curses(void)
3086 initscr();
3087 cbreak();
3088 noecho();
3089 nonl();
3090 intrflush(stdscr, FALSE);
3091 keypad(stdscr, TRUE);
3092 curs_set(0);
3095 __dead static void
3096 usage(void)
3098 int i;
3100 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3101 "Available commands:\n", getprogname());
3102 for (i = 0; i < nitems(tog_commands); i++) {
3103 struct tog_cmd *cmd = &tog_commands[i];
3104 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3106 exit(1);
3109 static char **
3110 make_argv(const char *arg0, const char *arg1)
3112 char **argv;
3113 int argc = (arg1 == NULL ? 1 : 2);
3115 argv = calloc(argc, sizeof(char *));
3116 if (argv == NULL)
3117 err(1, "calloc");
3118 argv[0] = strdup(arg0);
3119 if (argv[0] == NULL)
3120 err(1, "calloc");
3121 if (arg1) {
3122 argv[1] = strdup(arg1);
3123 if (argv[1] == NULL)
3124 err(1, "calloc");
3127 return argv;
3130 int
3131 main(int argc, char *argv[])
3133 const struct got_error *error = NULL;
3134 struct tog_cmd *cmd = NULL;
3135 int ch, hflag = 0;
3136 char **cmd_argv = NULL;
3138 setlocale(LC_ALL, "");
3140 while ((ch = getopt(argc, argv, "h")) != -1) {
3141 switch (ch) {
3142 case 'h':
3143 hflag = 1;
3144 break;
3145 default:
3146 usage();
3147 /* NOTREACHED */
3151 argc -= optind;
3152 argv += optind;
3153 optind = 0;
3154 optreset = 1;
3156 if (argc == 0) {
3157 if (hflag)
3158 usage();
3159 /* Build an argument vector which runs a default command. */
3160 cmd = &tog_commands[0];
3161 cmd_argv = make_argv(cmd->name, NULL);
3162 argc = 1;
3163 } else {
3164 int i;
3166 /* Did the user specific a command? */
3167 for (i = 0; i < nitems(tog_commands); i++) {
3168 if (strncmp(tog_commands[i].name, argv[0],
3169 strlen(argv[0])) == 0) {
3170 cmd = &tog_commands[i];
3171 if (hflag)
3172 tog_commands[i].cmd_usage();
3173 break;
3176 if (cmd == NULL) {
3177 /* Did the user specify a repository? */
3178 char *repo_path = realpath(argv[0], NULL);
3179 if (repo_path) {
3180 struct got_repository *repo;
3181 error = got_repo_open(&repo, repo_path);
3182 if (error == NULL)
3183 got_repo_close(repo);
3184 } else
3185 error = got_error_from_errno();
3186 if (error) {
3187 if (hflag) {
3188 fprintf(stderr, "%s: '%s' is not a "
3189 "known command\n", getprogname(),
3190 argv[0]);
3191 usage();
3193 fprintf(stderr, "%s: '%s' is neither a known "
3194 "command nor a path to a repository\n",
3195 getprogname(), argv[0]);
3196 free(repo_path);
3197 return 1;
3199 cmd = &tog_commands[0];
3200 cmd_argv = make_argv(cmd->name, repo_path);
3201 argc = 2;
3202 free(repo_path);
3206 init_curses();
3208 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3209 if (error)
3210 goto done;
3211 done:
3212 endwin();
3213 free(cmd_argv);
3214 if (error)
3215 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3216 return 0;