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 FILE *f;
94 int first_displayed_line;
95 int last_displayed_line;
96 int eof;
97 };
99 struct commit_queue_entry {
100 TAILQ_ENTRY(commit_queue_entry) entry;
101 struct got_object_id *id;
102 struct got_commit_object *commit;
103 };
104 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
105 struct commit_queue {
106 int ncommits;
107 struct commit_queue_head head;
108 };
110 struct tog_log_view_state {
111 struct got_commit_graph *graph;
112 struct commit_queue commits;
113 struct commit_queue_entry *first_displayed_entry;
114 struct commit_queue_entry *last_displayed_entry;
115 struct commit_queue_entry *selected_entry;
116 int selected;
117 char *in_repo_path;
118 struct got_repository *repo;
119 };
121 struct tog_blame_cb_args {
122 pthread_mutex_t *mutex;
123 struct tog_blame_line *lines; /* one per line */
124 int nlines;
126 struct tog_view *view;
127 struct got_object_id *commit_id;
128 FILE *f;
129 const char *path;
130 int *first_displayed_line;
131 int *last_displayed_line;
132 int *selected_line;
133 int *quit;
134 int *eof;
135 };
137 struct tog_blame_thread_args {
138 const char *path;
139 struct got_repository *repo;
140 struct tog_blame_cb_args *cb_args;
141 int *complete;
142 };
144 struct tog_blame {
145 FILE *f;
146 size_t filesize;
147 struct tog_blame_line *lines;
148 size_t nlines;
149 pthread_t thread;
150 struct tog_blame_thread_args thread_args;
151 struct tog_blame_cb_args cb_args;
152 const char *path;
153 };
155 struct tog_blame_view_state {
156 int first_displayed_line;
157 int last_displayed_line;
158 int selected_line;
159 int blame_complete;
160 int eof;
161 int done;
162 pthread_mutex_t mutex;
163 struct got_object_id_queue blamed_commits;
164 struct got_object_qid *blamed_commit;
165 char *path;
166 struct got_repository *repo;
167 struct got_object_id *commit_id;
168 struct tog_blame blame;
169 };
171 struct tog_parent_tree {
172 TAILQ_ENTRY(tog_parent_tree) entry;
173 struct got_tree_object *tree;
174 struct got_tree_entry *first_displayed_entry;
175 struct got_tree_entry *selected_entry;
176 int selected;
177 };
179 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
181 struct tog_tree_view_state {
182 char *tree_label;
183 struct got_tree_object *root;
184 struct got_tree_object *tree;
185 const struct got_tree_entries *entries;
186 struct got_tree_entry *first_displayed_entry;
187 struct got_tree_entry *last_displayed_entry;
188 struct got_tree_entry *selected_entry;
189 int nentries, ndisplayed, selected, show_ids;
190 struct tog_parent_trees parents;
191 struct got_object_id *commit_id;
192 struct got_repository *repo;
193 };
195 struct tog_view {
196 TAILQ_ENTRY(tog_view) entry;
197 WINDOW *window;
198 PANEL *panel;
199 int nlines, ncols, begin_y, begin_x;
200 int lines, cols; /* copies of LINES and COLS */
201 struct tog_view *parent;
203 /* type-specific state */
204 enum tog_view_type type;
205 union {
206 struct tog_diff_view_state diff;
207 struct tog_log_view_state log;
208 struct tog_blame_view_state blame;
209 struct tog_tree_view_state tree;
210 } state;
212 const struct got_error *(*show)(struct tog_view *);
213 const struct got_error *(*input)(struct tog_view **,
214 struct tog_view **, struct tog_view *, int);
215 const struct got_error *(*close)(struct tog_view *);
216 };
217 TAILQ_HEAD(tog_view_list_head, tog_view);
219 static const struct got_error *open_diff_view(struct tog_view *,
220 struct got_object *, struct got_object *, struct got_repository *);
221 static const struct got_error *show_diff_view(struct tog_view *);
222 static const struct got_error *input_diff_view(struct tog_view **,
223 struct tog_view **, struct tog_view *, int);
224 static const struct got_error* close_diff_view(struct tog_view *);
226 static const struct got_error *open_log_view(struct tog_view *,
227 struct got_object_id *, struct got_repository *, const char *);
228 static const struct got_error * show_log_view(struct tog_view *);
229 static const struct got_error *input_log_view(struct tog_view **,
230 struct tog_view **, struct tog_view *, int);
231 static const struct got_error *close_log_view(struct tog_view *);
233 static const struct got_error *open_blame_view(struct tog_view *, char *,
234 struct got_object_id *, struct got_repository *);
235 static const struct got_error *show_blame_view(struct tog_view *);
236 static const struct got_error *input_blame_view(struct tog_view **,
237 struct tog_view **, struct tog_view *, int);
238 static const struct got_error *close_blame_view(struct tog_view *);
240 static const struct got_error *open_tree_view(struct tog_view *,
241 struct got_tree_object *, struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_tree_view(struct tog_view *);
243 static const struct got_error *input_tree_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_tree_view(struct tog_view *);
247 static const struct got_error *
248 view_close(struct tog_view *view)
250 const struct got_error *err = NULL;
252 if (view->close)
253 err = view->close(view);
254 if (view->panel)
255 del_panel(view->panel);
256 if (view->window)
257 delwin(view->window);
258 free(view);
259 return err;
262 static struct tog_view *
263 view_open(int nlines, int ncols, int begin_y, int begin_x,
264 struct tog_view *parent, enum tog_view_type type)
266 struct tog_view *view = calloc(1, sizeof(*view));
268 if (view == NULL)
269 return NULL;
271 view->parent = parent;
272 view->type = type;
273 view->lines = LINES;
274 view->cols = COLS;
275 view->nlines = nlines ? nlines : LINES - begin_y;
276 view->ncols = ncols ? ncols : COLS - begin_x;
277 view->begin_y = begin_y;
278 view->begin_x = begin_x;
279 view->window = newwin(nlines, ncols, begin_y, begin_x);
280 if (view->window == NULL) {
281 view_close(view);
282 return NULL;
284 view->panel = new_panel(view->window);
285 if (view->panel == NULL) {
286 view_close(view);
287 return NULL;
290 keypad(view->window, TRUE);
291 return view;
294 const struct got_error *
295 view_show(struct tog_view *view)
297 const struct got_error *err;
299 err = view->show(view);
300 if (err)
301 return err;
302 show_panel(view->panel);
303 update_panels();
304 doupdate();
306 return err;
309 const struct got_error *
310 view_resize(struct tog_view *view)
312 int nlines, ncols;
314 while (view) {
315 if (view->lines > LINES)
316 nlines = view->nlines - (view->lines - LINES);
317 else
318 nlines = view->nlines + (LINES - view->lines);
320 if (view->cols > COLS)
321 ncols = view->ncols - (view->cols - COLS);
322 else
323 ncols = view->ncols + (COLS - view->cols);
325 if (wresize(view->window, nlines, ncols) == ERR)
326 return got_error_from_errno();
327 replace_panel(view->panel, view->window);
329 view->nlines = nlines;
330 view->ncols = ncols;
331 view->lines = LINES;
332 view->cols = COLS;
334 view = view->parent;
337 return NULL;
340 static const struct got_error *
341 view_input(struct tog_view **new, struct tog_view **dead,
342 struct tog_view **focus, int *done, struct tog_view *view,
343 struct tog_view_list_head *views)
345 const struct got_error *err = NULL;
346 struct tog_view *next, *prev;
347 int ch;
349 *new = NULL;
350 *dead = NULL;
352 nodelay(stdscr, FALSE);
353 ch = wgetch(view->window);
354 nodelay(stdscr, TRUE);
355 switch (ch) {
356 case ERR:
357 break;
358 case '\t':
359 next = TAILQ_NEXT(view, entry);
360 if (next)
361 *focus = next;
362 else
363 *focus = TAILQ_FIRST(views);
364 break;
365 case KEY_BACKSPACE:
366 prev = TAILQ_PREV(view, tog_view_list_head, entry);
367 if (prev)
368 *focus = prev;
369 else
370 *focus = TAILQ_LAST(views, tog_view_list_head);
371 break;
372 case 'q':
373 err = view->input(new, dead, view, ch);
374 *dead = view;
375 break;
376 case 'Q':
377 *done = 1;
378 break;
379 case KEY_RESIZE:
380 err = view_resize(view);
381 if (err)
382 return err;
383 err = view->input(new, dead, view, ch);
384 break;
385 default:
386 err = view->input(new, dead, view, ch);
387 break;
390 return err;
393 static const struct got_error *
394 view_loop(struct tog_view *view)
396 const struct got_error *err = NULL;
397 struct tog_view_list_head views;
398 struct tog_view *new_view, *dead_view;
399 int done = 0;
401 TAILQ_INIT(&views);
402 TAILQ_INSERT_HEAD(&views, view, entry);
404 while (!TAILQ_EMPTY(&views) && !done) {
405 err = view_show(view);
406 if (err)
407 break;
408 err = view_input(&new_view, &dead_view, &view, &done,
409 view, &views);
410 if (err)
411 break;
412 if (new_view) {
413 TAILQ_INSERT_TAIL(&views, new_view, entry);
414 view = new_view;
416 if (dead_view) {
417 TAILQ_REMOVE(&views, dead_view, entry);
418 TAILQ_FOREACH(view, &views, entry) {
419 if (view->parent == dead_view)
420 view->parent = NULL;
422 if (dead_view->parent)
423 view = dead_view->parent;
424 else
425 view = TAILQ_LAST(&views, tog_view_list_head);
426 err = view_close(dead_view);
427 if (err)
428 goto done;
431 done:
432 while (!TAILQ_EMPTY(&views)) {
433 view = TAILQ_FIRST(&views);
434 TAILQ_REMOVE(&views, view, entry);
435 view_close(view);
437 return err;
440 __dead static void
441 usage_log(void)
443 endwin();
444 fprintf(stderr,
445 "usage: %s log [-c commit] [-r repository-path] [path]\n",
446 getprogname());
447 exit(1);
450 /* Create newly allocated wide-character string equivalent to a byte string. */
451 static const struct got_error *
452 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
454 char *vis = NULL;
455 const struct got_error *err = NULL;
457 *ws = NULL;
458 *wlen = mbstowcs(NULL, s, 0);
459 if (*wlen == (size_t)-1) {
460 int vislen;
461 if (errno != EILSEQ)
462 return got_error_from_errno();
464 /* byte string invalid in current encoding; try to "fix" it */
465 err = got_mbsavis(&vis, &vislen, s);
466 if (err)
467 return err;
468 *wlen = mbstowcs(NULL, vis, 0);
469 if (*wlen == (size_t)-1) {
470 err = got_error_from_errno(); /* give up */
471 goto done;
475 *ws = calloc(*wlen + 1, sizeof(*ws));
476 if (*ws == NULL) {
477 err = got_error_from_errno();
478 goto done;
481 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
482 err = got_error_from_errno();
483 done:
484 free(vis);
485 if (err) {
486 free(*ws);
487 *ws = NULL;
488 *wlen = 0;
490 return err;
493 /* Format a line for display, ensuring that it won't overflow a width limit. */
494 static const struct got_error *
495 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
497 const struct got_error *err = NULL;
498 int cols = 0;
499 wchar_t *wline = NULL;
500 size_t wlen;
501 int i;
503 *wlinep = NULL;
504 *widthp = 0;
506 err = mbs2ws(&wline, &wlen, line);
507 if (err)
508 return err;
510 i = 0;
511 while (i < wlen && cols < wlimit) {
512 int width = wcwidth(wline[i]);
513 switch (width) {
514 case 0:
515 i++;
516 break;
517 case 1:
518 case 2:
519 if (cols + width <= wlimit) {
520 cols += width;
521 i++;
523 break;
524 case -1:
525 if (wline[i] == L'\t')
526 cols += TABSIZE - ((cols + 1) % TABSIZE);
527 i++;
528 break;
529 default:
530 err = got_error_from_errno();
531 goto done;
534 wline[i] = L'\0';
535 if (widthp)
536 *widthp = cols;
537 done:
538 if (err)
539 free(wline);
540 else
541 *wlinep = wline;
542 return err;
545 static const struct got_error *
546 draw_commit(struct tog_view *view, struct got_commit_object *commit,
547 struct got_object_id *id)
549 const struct got_error *err = NULL;
550 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
551 char *logmsg0 = NULL, *logmsg = NULL;
552 char *author0 = NULL, *author = NULL;
553 wchar_t *wlogmsg = NULL, *wauthor = NULL;
554 int author_width, logmsg_width;
555 char *newline, *smallerthan;
556 char *line = NULL;
557 int col, limit;
558 static const size_t date_display_cols = 9;
559 static const size_t author_display_cols = 16;
560 const int avail = view->ncols;
562 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
563 &commit->tm_committer) >= sizeof(datebuf))
564 return got_error(GOT_ERR_NO_SPACE);
566 if (avail < date_display_cols)
567 limit = MIN(sizeof(datebuf) - 1, avail);
568 else
569 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
570 waddnstr(view->window, datebuf, limit);
571 col = limit + 1;
572 if (col > avail)
573 goto done;
575 author0 = strdup(commit->author);
576 if (author0 == NULL) {
577 err = got_error_from_errno();
578 goto done;
580 author = author0;
581 smallerthan = strchr(author, '<');
582 if (smallerthan)
583 *smallerthan = '\0';
584 else {
585 char *at = strchr(author, '@');
586 if (at)
587 *at = '\0';
589 limit = avail - col;
590 err = format_line(&wauthor, &author_width, author, limit);
591 if (err)
592 goto done;
593 waddwstr(view->window, wauthor);
594 col += author_width;
595 while (col <= avail && author_width < author_display_cols + 1) {
596 waddch(view->window, ' ');
597 col++;
598 author_width++;
600 if (col > avail)
601 goto done;
603 logmsg0 = strdup(commit->logmsg);
604 if (logmsg0 == NULL) {
605 err = got_error_from_errno();
606 goto done;
608 logmsg = logmsg0;
609 while (*logmsg == '\n')
610 logmsg++;
611 newline = strchr(logmsg, '\n');
612 if (newline)
613 *newline = '\0';
614 limit = avail - col;
615 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
616 if (err)
617 goto done;
618 waddwstr(view->window, wlogmsg);
619 col += logmsg_width;
620 while (col <= avail) {
621 waddch(view->window, ' ');
622 col++;
624 done:
625 free(logmsg0);
626 free(wlogmsg);
627 free(author0);
628 free(wauthor);
629 free(line);
630 return err;
633 static struct commit_queue_entry *
634 alloc_commit_queue_entry(struct got_commit_object *commit,
635 struct got_object_id *id)
637 struct commit_queue_entry *entry;
639 entry = calloc(1, sizeof(*entry));
640 if (entry == NULL)
641 return NULL;
643 entry->id = id;
644 entry->commit = commit;
645 return entry;
648 static void
649 pop_commit(struct commit_queue *commits)
651 struct commit_queue_entry *entry;
653 entry = TAILQ_FIRST(&commits->head);
654 TAILQ_REMOVE(&commits->head, entry, entry);
655 got_object_commit_close(entry->commit);
656 commits->ncommits--;
657 /* Don't free entry->id! It is owned by the commit graph. */
658 free(entry);
661 static void
662 free_commits(struct commit_queue *commits)
664 while (!TAILQ_EMPTY(&commits->head))
665 pop_commit(commits);
668 static const struct got_error *
669 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
670 struct got_object_id *start_id, int minqueue, int init,
671 struct got_repository *repo, const char *path)
673 const struct got_error *err = NULL;
674 struct got_object_id *id;
675 struct commit_queue_entry *entry;
676 int nfetched, nqueued = 0, found_obj = 0;
677 int is_root_path = strcmp(path, "/") == 0;
679 err = got_commit_graph_iter_start(graph, start_id);
680 if (err)
681 return err;
683 entry = TAILQ_LAST(&commits->head, commit_queue_head);
684 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
685 int nfetched;
687 /* Start ID's commit is already on the queue; skip over it. */
688 err = got_commit_graph_iter_next(&id, graph);
689 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
690 return err;
692 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
693 if (err)
694 return err;
697 while (1) {
698 struct got_commit_object *commit;
700 err = got_commit_graph_iter_next(&id, graph);
701 if (err) {
702 if (err->code != GOT_ERR_ITER_NEED_MORE)
703 break;
704 if (nqueued >= minqueue) {
705 err = NULL;
706 break;
708 err = got_commit_graph_fetch_commits(&nfetched,
709 graph, 1, repo);
710 if (err)
711 return err;
712 continue;
714 if (id == NULL)
715 break;
717 err = got_object_open_as_commit(&commit, repo, id);
718 if (err)
719 break;
721 if (!is_root_path) {
722 struct got_object *obj;
723 struct got_object_qid *pid;
724 int changed = 0;
726 err = got_object_open_by_path(&obj, repo, id, path);
727 if (err) {
728 got_object_commit_close(commit);
729 if (err->code == GOT_ERR_NO_OBJ &&
730 (found_obj || !init)) {
731 /* History stops here. */
732 err = got_error(GOT_ERR_ITER_COMPLETED);
734 break;
736 found_obj = 1;
738 pid = SIMPLEQ_FIRST(&commit->parent_ids);
739 if (pid != NULL) {
740 struct got_object *pobj;
741 err = got_object_open_by_path(&pobj, repo,
742 pid->id, path);
743 if (err) {
744 if (err->code != GOT_ERR_NO_OBJ) {
745 got_object_close(obj);
746 got_object_commit_close(commit);
747 break;
749 err = NULL;
750 changed = 1;
751 } else {
752 struct got_object_id *id, *pid;
753 id = got_object_get_id(obj);
754 if (id == NULL) {
755 err = got_error_from_errno();
756 got_object_close(obj);
757 got_object_close(pobj);
758 break;
760 pid = got_object_get_id(pobj);
761 if (pid == NULL) {
762 err = got_error_from_errno();
763 free(id);
764 got_object_close(obj);
765 got_object_close(pobj);
766 break;
768 changed =
769 (got_object_id_cmp(id, pid) != 0);
770 got_object_close(pobj);
771 free(id);
772 free(pid);
775 got_object_close(obj);
776 if (!changed) {
777 got_object_commit_close(commit);
778 continue;
782 entry = alloc_commit_queue_entry(commit, id);
783 if (entry == NULL) {
784 err = got_error_from_errno();
785 break;
787 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
788 nqueued++;
789 commits->ncommits++;
792 return err;
795 static const struct got_error *
796 fetch_next_commit(struct commit_queue_entry **pentry,
797 struct commit_queue_entry *entry, struct commit_queue *commits,
798 struct got_commit_graph *graph, struct got_repository *repo,
799 const char *path)
801 const struct got_error *err = NULL;
803 *pentry = NULL;
805 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
806 if (err)
807 return err;
809 /* Next entry to display should now be available. */
810 *pentry = TAILQ_NEXT(entry, entry);
811 if (*pentry == NULL)
812 return got_error(GOT_ERR_NO_OBJ);
814 return NULL;
817 static const struct got_error *
818 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
820 const struct got_error *err = NULL;
821 struct got_reference *head_ref;
823 *head_id = NULL;
825 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
826 if (err)
827 return err;
829 err = got_ref_resolve(head_id, repo, head_ref);
830 got_ref_close(head_ref);
831 if (err) {
832 *head_id = NULL;
833 return err;
836 return NULL;
839 static const struct got_error *
840 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
841 struct commit_queue_entry **selected, struct commit_queue_entry *first,
842 struct commit_queue *commits, int selected_idx, int limit,
843 struct got_commit_graph *graph, struct got_repository *repo,
844 const char *path)
846 const struct got_error *err = NULL;
847 struct commit_queue_entry *entry;
848 int ncommits, width;
849 char *id_str, *header;
850 wchar_t *wline;
852 entry = first;
853 ncommits = 0;
854 while (entry) {
855 if (ncommits == selected_idx) {
856 *selected = entry;
857 break;
859 entry = TAILQ_NEXT(entry, entry);
860 ncommits++;
863 err = got_object_id_str(&id_str, (*selected)->id);
864 if (err)
865 return err;
867 if (path && strcmp(path, "/") != 0) {
868 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
869 err = got_error_from_errno();
870 free(id_str);
871 return err;
873 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
874 err = got_error_from_errno();
875 free(id_str);
876 return err;
878 free(id_str);
879 err = format_line(&wline, &width, header, view->ncols);
880 if (err) {
881 free(header);
882 return err;
884 free(header);
886 werase(view->window);
888 waddwstr(view->window, wline);
889 if (width < view->ncols)
890 waddch(view->window, '\n');
891 free(wline);
892 if (limit <= 1)
893 return NULL;
895 entry = first;
896 *last = first;
897 ncommits = 0;
898 while (entry) {
899 if (ncommits >= limit - 1)
900 break;
901 if (ncommits == selected_idx)
902 wstandout(view->window);
903 err = draw_commit(view, entry->commit, entry->id);
904 if (ncommits == selected_idx)
905 wstandend(view->window);
906 if (err)
907 break;
908 ncommits++;
909 *last = entry;
910 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
911 err = queue_commits(graph, commits, entry->id, 1,
912 0, repo, path);
913 if (err) {
914 if (err->code != GOT_ERR_ITER_COMPLETED)
915 return err;
916 err = NULL;
919 entry = TAILQ_NEXT(entry, entry);
922 update_panels();
924 return err;
927 static void
928 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
929 struct commit_queue *commits)
931 struct commit_queue_entry *entry;
932 int nscrolled = 0;
934 entry = TAILQ_FIRST(&commits->head);
935 if (*first_displayed_entry == entry)
936 return;
938 entry = *first_displayed_entry;
939 while (entry && nscrolled < maxscroll) {
940 entry = TAILQ_PREV(entry, commit_queue_head, entry);
941 if (entry) {
942 *first_displayed_entry = entry;
943 nscrolled++;
948 static const struct got_error *
949 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
950 struct commit_queue_entry *last_displayed_entry,
951 struct commit_queue *commits, struct got_commit_graph *graph,
952 struct got_repository *repo, const char *path)
954 const struct got_error *err = NULL;
955 struct commit_queue_entry *pentry;
956 int nscrolled = 0;
958 do {
959 pentry = TAILQ_NEXT(last_displayed_entry, entry);
960 if (pentry == NULL) {
961 err = fetch_next_commit(&pentry, last_displayed_entry,
962 commits, graph, repo, path);
963 if (err || pentry == NULL)
964 break;
966 last_displayed_entry = pentry;
968 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
969 if (pentry == NULL)
970 break;
971 *first_displayed_entry = pentry;
972 } while (++nscrolled < maxscroll);
974 return err;
977 static const struct got_error *
978 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
979 struct commit_queue_entry *entry, struct got_repository *repo)
981 const struct got_error *err;
982 struct got_object *obj1 = NULL, *obj2 = NULL;
983 struct got_object_qid *parent_id;
984 struct tog_view *diff_view;
986 err = got_object_open(&obj2, repo, entry->id);
987 if (err)
988 return err;
990 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
991 if (parent_id) {
992 err = got_object_open(&obj1, repo, parent_id->id);
993 if (err)
994 goto done;
997 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
998 if (diff_view == NULL) {
999 err = got_error_from_errno();
1000 goto done;
1003 err = open_diff_view(diff_view, obj1, obj2, repo);
1004 if (err == NULL)
1005 *new_view = diff_view;
1006 done:
1007 if (obj1)
1008 got_object_close(obj1);
1009 if (obj2)
1010 got_object_close(obj2);
1011 return err;
1014 static const struct got_error *
1015 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1016 struct commit_queue_entry *entry, struct got_repository *repo)
1018 const struct got_error *err = NULL;
1019 struct got_tree_object *tree;
1020 struct tog_view *tree_view;
1022 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1023 if (err)
1024 return err;
1026 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1027 if (tree_view == NULL)
1028 return got_error_from_errno();
1030 err = open_tree_view(tree_view, tree, entry->id, repo);
1031 if (err)
1032 got_object_tree_close(tree);
1033 else
1034 *new_view = tree_view;
1035 return err;
1038 static const struct got_error *
1039 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1040 struct got_repository *repo, const char *path)
1042 const struct got_error *err = NULL;
1043 struct got_object_id *head_id = NULL;
1044 int nfetched;
1045 struct tog_log_view_state *s = &view->state.log;
1047 err = got_repo_map_path(&s->in_repo_path, repo, path);
1048 if (err != NULL)
1049 goto done;
1051 err = get_head_commit_id(&head_id, repo);
1052 if (err)
1053 return err;
1055 /* The graph contains all commits. */
1056 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1057 if (err)
1058 goto done;
1059 /* The commit queue contains a subset of commits filtered by path. */
1060 TAILQ_INIT(&s->commits.head);
1061 s->commits.ncommits = 0;
1063 /* Populate commit graph with a sufficient number of commits. */
1064 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1065 start_id, repo);
1066 if (err)
1067 goto done;
1070 * Open the initial batch of commits, sorted in commit graph order.
1071 * We keep all commits open throughout the lifetime of the log view
1072 * in order to avoid having to re-fetch commits from disk while
1073 * updating the display.
1075 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1076 repo, s->in_repo_path);
1077 if (err) {
1078 if (err->code != GOT_ERR_ITER_COMPLETED)
1079 goto done;
1080 err = NULL;
1083 s->first_displayed_entry =
1084 TAILQ_FIRST(&s->commits.head);
1085 s->selected_entry = s->first_displayed_entry;
1086 s->repo = repo;
1088 view->show = show_log_view;
1089 view->input = input_log_view;
1090 view->close = close_log_view;
1091 done:
1092 free(head_id);
1093 return err;
1096 static const struct got_error *
1097 close_log_view(struct tog_view *view)
1099 struct tog_log_view_state *s = &view->state.log;
1101 if (s->graph)
1102 got_commit_graph_close(s->graph);
1103 free_commits(&s->commits);
1104 free(s->in_repo_path);
1105 return NULL;
1108 static const struct got_error *
1109 show_log_view(struct tog_view *view)
1111 struct tog_log_view_state *s = &view->state.log;
1113 return draw_commits(view, &s->last_displayed_entry,
1114 &s->selected_entry, s->first_displayed_entry,
1115 &s->commits, s->selected, view->nlines, s->graph,
1116 s->repo, s->in_repo_path);
1119 static const struct got_error *
1120 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1121 struct tog_view *view, int ch)
1123 const struct got_error *err = NULL;
1124 struct tog_log_view_state *s = &view->state.log;
1126 switch (ch) {
1127 case 'k':
1128 case KEY_UP:
1129 if (s->selected > 0)
1130 s->selected--;
1131 if (s->selected > 0)
1132 break;
1133 scroll_up(&s->first_displayed_entry, 1,
1134 &s->commits);
1135 break;
1136 case KEY_PPAGE:
1137 if (TAILQ_FIRST(&s->commits.head) ==
1138 s->first_displayed_entry) {
1139 s->selected = 0;
1140 break;
1142 scroll_up(&s->first_displayed_entry,
1143 view->nlines, &s->commits);
1144 break;
1145 case 'j':
1146 case KEY_DOWN:
1147 if (s->selected < MIN(view->nlines - 2,
1148 s->commits.ncommits - 1)) {
1149 s->selected++;
1150 break;
1152 err = scroll_down(&s->first_displayed_entry, 1,
1153 s->last_displayed_entry, &s->commits,
1154 s->graph, s->repo, s->in_repo_path);
1155 if (err) {
1156 if (err->code != GOT_ERR_ITER_COMPLETED)
1157 break;
1158 err = NULL;
1160 break;
1161 case KEY_NPAGE: {
1162 struct commit_queue_entry *first;
1163 first = s->first_displayed_entry;
1164 err = scroll_down(&s->first_displayed_entry,
1165 view->nlines, s->last_displayed_entry,
1166 &s->commits, s->graph, s->repo,
1167 s->in_repo_path);
1168 if (err == NULL)
1169 break;
1170 if (err->code != GOT_ERR_ITER_COMPLETED)
1171 break;
1172 if (first == s->first_displayed_entry &&
1173 s->selected < MIN(view->nlines - 2,
1174 s->commits.ncommits - 1)) {
1175 /* can't scroll further down */
1176 s->selected = MIN(view->nlines - 2,
1177 s->commits.ncommits - 1);
1179 err = NULL;
1180 break;
1182 case KEY_RESIZE:
1183 if (s->selected > view->nlines - 2)
1184 s->selected = view->nlines - 2;
1185 if (s->selected > s->commits.ncommits - 1)
1186 s->selected = s->commits.ncommits - 1;
1187 break;
1188 case KEY_ENTER:
1189 case '\r':
1190 err = show_commit(new_view, view, s->selected_entry,
1191 s->repo);
1192 break;
1193 case 't':
1194 err = browse_commit(new_view, view, s->selected_entry,
1195 s->repo);
1196 break;
1197 default:
1198 break;
1201 return err;
1204 static const struct got_error *
1205 cmd_log(int argc, char *argv[])
1207 const struct got_error *error;
1208 struct got_repository *repo = NULL;
1209 struct got_object_id *start_id = NULL;
1210 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1211 char *start_commit = NULL;
1212 int ch;
1213 struct tog_view *view;
1215 #ifndef PROFILE
1216 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1217 err(1, "pledge");
1218 #endif
1220 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1221 switch (ch) {
1222 case 'c':
1223 start_commit = optarg;
1224 break;
1225 case 'r':
1226 repo_path = realpath(optarg, NULL);
1227 if (repo_path == NULL)
1228 err(1, "-r option");
1229 break;
1230 default:
1231 usage();
1232 /* NOTREACHED */
1236 argc -= optind;
1237 argv += optind;
1239 if (argc == 0)
1240 path = strdup("");
1241 else if (argc == 1)
1242 path = strdup(argv[0]);
1243 else
1244 usage_log();
1245 if (path == NULL)
1246 return got_error_from_errno();
1248 cwd = getcwd(NULL, 0);
1249 if (cwd == NULL) {
1250 error = got_error_from_errno();
1251 goto done;
1253 if (repo_path == NULL) {
1254 repo_path = strdup(cwd);
1255 if (repo_path == NULL) {
1256 error = got_error_from_errno();
1257 goto done;
1261 error = got_repo_open(&repo, repo_path);
1262 if (error != NULL)
1263 goto done;
1265 if (start_commit == NULL) {
1266 error = get_head_commit_id(&start_id, repo);
1267 if (error != NULL)
1268 goto done;
1269 } else {
1270 struct got_object *obj;
1271 error = got_object_open_by_id_str(&obj, repo, start_commit);
1272 if (error == NULL) {
1273 start_id = got_object_get_id(obj);
1274 if (start_id == NULL)
1275 error = got_error_from_errno();
1276 goto done;
1279 if (error != NULL)
1280 goto done;
1282 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1283 if (view == NULL) {
1284 error = got_error_from_errno();
1285 goto done;
1287 error = open_log_view(view, start_id, repo, path);
1288 if (error)
1289 goto done;
1290 error = view_loop(view);
1291 done:
1292 free(repo_path);
1293 free(cwd);
1294 free(path);
1295 free(start_id);
1296 if (repo)
1297 got_repo_close(repo);
1298 return error;
1301 __dead static void
1302 usage_diff(void)
1304 endwin();
1305 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1306 getprogname());
1307 exit(1);
1310 static char *
1311 parse_next_line(FILE *f, size_t *len)
1313 char *line;
1314 size_t linelen;
1315 size_t lineno;
1316 const char delim[3] = { '\0', '\0', '\0'};
1318 line = fparseln(f, &linelen, &lineno, delim, 0);
1319 if (len)
1320 *len = linelen;
1321 return line;
1324 static const struct got_error *
1325 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1326 int *last_displayed_line, int *eof, int max_lines)
1328 const struct got_error *err;
1329 int nlines = 0, nprinted = 0;
1330 char *line;
1331 size_t len;
1332 wchar_t *wline;
1333 int width;
1335 rewind(f);
1336 werase(view->window);
1338 *eof = 0;
1339 while (nprinted < max_lines) {
1340 line = parse_next_line(f, &len);
1341 if (line == NULL) {
1342 *eof = 1;
1343 break;
1345 if (++nlines < *first_displayed_line) {
1346 free(line);
1347 continue;
1350 err = format_line(&wline, &width, line, view->ncols);
1351 if (err) {
1352 free(line);
1353 free(wline);
1354 return err;
1356 waddwstr(view->window, wline);
1357 if (width < view->ncols)
1358 waddch(view->window, '\n');
1359 if (++nprinted == 1)
1360 *first_displayed_line = nlines;
1361 free(line);
1362 free(wline);
1363 wline = NULL;
1365 *last_displayed_line = nlines;
1367 update_panels();
1369 return NULL;
1372 static const struct got_error *
1373 open_diff_view(struct tog_view *view, struct got_object *obj1,
1374 struct got_object *obj2, struct got_repository *repo)
1376 const struct got_error *err;
1377 FILE *f;
1379 if (obj1 != NULL && obj2 != NULL &&
1380 got_object_get_type(obj1) != got_object_get_type(obj2))
1381 return got_error(GOT_ERR_OBJ_TYPE);
1383 f = got_opentemp();
1384 if (f == NULL)
1385 return got_error_from_errno();
1387 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1388 case GOT_OBJ_TYPE_BLOB:
1389 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1390 break;
1391 case GOT_OBJ_TYPE_TREE:
1392 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1393 break;
1394 case GOT_OBJ_TYPE_COMMIT:
1395 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1396 break;
1397 default:
1398 return got_error(GOT_ERR_OBJ_TYPE);
1401 fflush(f);
1403 view->state.diff.f = f;
1404 view->state.diff.first_displayed_line = 1;
1405 view->state.diff.last_displayed_line = view->nlines;
1407 view->show = show_diff_view;
1408 view->input = input_diff_view;
1409 view->close = close_diff_view;
1411 return NULL;
1414 static const struct got_error *
1415 close_diff_view(struct tog_view *view)
1417 const struct got_error *err = NULL;
1419 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1420 err = got_error_from_errno();
1422 return err;
1425 static const struct got_error *
1426 show_diff_view(struct tog_view *view)
1428 struct tog_diff_view_state *s = &view->state.diff;
1430 return draw_file(view, s->f, &s->first_displayed_line,
1431 &s->last_displayed_line, &s->eof, view->nlines);
1434 static const struct got_error *
1435 input_diff_view(struct tog_view **new, struct tog_view **dead,
1436 struct tog_view *view, int ch)
1438 struct tog_diff_view_state *s = &view->state.diff;
1439 int i;
1441 switch (ch) {
1442 case 'k':
1443 case KEY_UP:
1444 if (s->first_displayed_line > 1)
1445 s->first_displayed_line--;
1446 break;
1447 case KEY_PPAGE:
1448 i = 0;
1449 while (i++ < view->nlines - 1 &&
1450 s->first_displayed_line > 1)
1451 s->first_displayed_line--;
1452 break;
1453 case 'j':
1454 case KEY_DOWN:
1455 if (!s->eof)
1456 s->first_displayed_line++;
1457 break;
1458 case KEY_NPAGE:
1459 case ' ':
1460 i = 0;
1461 while (!s->eof && i++ < view->nlines - 1) {
1462 char *line;
1463 line = parse_next_line(s->f, NULL);
1464 s->first_displayed_line++;
1465 if (line == NULL)
1466 break;
1468 break;
1469 default:
1470 break;
1473 return NULL;
1476 static const struct got_error *
1477 cmd_diff(int argc, char *argv[])
1479 const struct got_error *error = NULL;
1480 struct got_repository *repo = NULL;
1481 struct got_object *obj1 = NULL, *obj2 = NULL;
1482 char *repo_path = NULL;
1483 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1484 int ch;
1485 struct tog_view *view;
1487 #ifndef PROFILE
1488 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1489 err(1, "pledge");
1490 #endif
1492 while ((ch = getopt(argc, argv, "")) != -1) {
1493 switch (ch) {
1494 default:
1495 usage();
1496 /* NOTREACHED */
1500 argc -= optind;
1501 argv += optind;
1503 if (argc == 0) {
1504 usage_diff(); /* TODO show local worktree changes */
1505 } else if (argc == 2) {
1506 repo_path = getcwd(NULL, 0);
1507 if (repo_path == NULL)
1508 return got_error_from_errno();
1509 obj_id_str1 = argv[0];
1510 obj_id_str2 = argv[1];
1511 } else if (argc == 3) {
1512 repo_path = realpath(argv[0], NULL);
1513 if (repo_path == NULL)
1514 return got_error_from_errno();
1515 obj_id_str1 = argv[1];
1516 obj_id_str2 = argv[2];
1517 } else
1518 usage_diff();
1520 error = got_repo_open(&repo, repo_path);
1521 free(repo_path);
1522 if (error)
1523 goto done;
1525 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1526 if (error)
1527 goto done;
1529 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1530 if (error)
1531 goto done;
1533 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1534 if (view == NULL) {
1535 error = got_error_from_errno();
1536 goto done;
1538 error = open_diff_view(view, obj1, obj2, repo);
1539 if (error)
1540 goto done;
1541 error = view_loop(view);
1542 done:
1543 got_repo_close(repo);
1544 if (obj1)
1545 got_object_close(obj1);
1546 if (obj2)
1547 got_object_close(obj2);
1548 return error;
1551 __dead static void
1552 usage_blame(void)
1554 endwin();
1555 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1556 getprogname());
1557 exit(1);
1560 struct tog_blame_line {
1561 int annotated;
1562 struct got_object_id *id;
1565 static const struct got_error *
1566 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1567 const char *path, struct tog_blame_line *lines, int nlines,
1568 int blame_complete, int selected_line, int *first_displayed_line,
1569 int *last_displayed_line, int *eof, int max_lines)
1571 const struct got_error *err;
1572 int lineno = 0, nprinted = 0;
1573 char *line;
1574 size_t len;
1575 wchar_t *wline;
1576 int width, wlimit;
1577 struct tog_blame_line *blame_line;
1578 struct got_object_id *prev_id = NULL;
1579 char *id_str;
1581 err = got_object_id_str(&id_str, id);
1582 if (err)
1583 return err;
1585 rewind(f);
1586 werase(view->window);
1588 if (asprintf(&line, "commit: %s", id_str) == -1) {
1589 err = got_error_from_errno();
1590 free(id_str);
1591 return err;
1594 err = format_line(&wline, &width, line, view->ncols);
1595 free(line);
1596 line = NULL;
1597 waddwstr(view->window, wline);
1598 free(wline);
1599 wline = NULL;
1600 if (width < view->ncols)
1601 waddch(view->window, '\n');
1603 if (asprintf(&line, "[%d/%d] %s%s",
1604 *first_displayed_line - 1 + selected_line, nlines,
1605 blame_complete ? "" : "annotating ", path) == -1) {
1606 free(id_str);
1607 return got_error_from_errno();
1609 free(id_str);
1610 err = format_line(&wline, &width, line, view->ncols);
1611 free(line);
1612 line = NULL;
1613 if (err)
1614 return err;
1615 waddwstr(view->window, wline);
1616 free(wline);
1617 wline = NULL;
1618 if (width < view->ncols)
1619 waddch(view->window, '\n');
1621 *eof = 0;
1622 while (nprinted < max_lines - 2) {
1623 line = parse_next_line(f, &len);
1624 if (line == NULL) {
1625 *eof = 1;
1626 break;
1628 if (++lineno < *first_displayed_line) {
1629 free(line);
1630 continue;
1633 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1634 err = format_line(&wline, &width, line, wlimit);
1635 if (err) {
1636 free(line);
1637 return err;
1640 if (nprinted == selected_line - 1)
1641 wstandout(view->window);
1643 blame_line = &lines[lineno - 1];
1644 if (blame_line->annotated && prev_id &&
1645 got_object_id_cmp(prev_id, blame_line->id) == 0)
1646 waddstr(view->window, " ");
1647 else if (blame_line->annotated) {
1648 char *id_str;
1649 err = got_object_id_str(&id_str, blame_line->id);
1650 if (err) {
1651 free(line);
1652 free(wline);
1653 return err;
1655 wprintw(view->window, "%.8s ", id_str);
1656 free(id_str);
1657 prev_id = blame_line->id;
1658 } else {
1659 waddstr(view->window, "........ ");
1660 prev_id = NULL;
1663 waddwstr(view->window, wline);
1664 while (width < wlimit) {
1665 waddch(view->window, ' ');
1666 width++;
1668 if (nprinted == selected_line - 1)
1669 wstandend(view->window);
1670 if (++nprinted == 1)
1671 *first_displayed_line = lineno;
1672 free(line);
1673 free(wline);
1674 wline = NULL;
1676 *last_displayed_line = lineno;
1678 update_panels();
1680 return NULL;
1683 static const struct got_error *
1684 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1686 const struct got_error *err = NULL;
1687 struct tog_blame_cb_args *a = arg;
1688 struct tog_blame_line *line;
1690 if (nlines != a->nlines ||
1691 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1692 return got_error(GOT_ERR_RANGE);
1694 if (pthread_mutex_lock(a->mutex) != 0)
1695 return got_error_from_errno();
1697 if (*a->quit) { /* user has quit the blame view */
1698 err = got_error(GOT_ERR_ITER_COMPLETED);
1699 goto done;
1702 if (lineno == -1)
1703 goto done; /* no change in this commit */
1705 line = &a->lines[lineno - 1];
1706 if (line->annotated)
1707 goto done;
1709 line->id = got_object_id_dup(id);
1710 if (line->id == NULL) {
1711 err = got_error_from_errno();
1712 goto done;
1714 line->annotated = 1;
1716 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1717 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1718 a->last_displayed_line, a->eof, a->view->nlines);
1719 done:
1720 if (pthread_mutex_unlock(a->mutex) != 0)
1721 return got_error_from_errno();
1722 return err;
1725 static void *
1726 blame_thread(void *arg)
1728 const struct got_error *err;
1729 struct tog_blame_thread_args *ta = arg;
1730 struct tog_blame_cb_args *a = ta->cb_args;
1732 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1733 blame_cb, ta->cb_args);
1735 if (pthread_mutex_lock(a->mutex) != 0)
1736 return (void *)got_error_from_errno();
1738 got_repo_close(ta->repo);
1739 ta->repo = NULL;
1740 *ta->complete = 1;
1741 if (!err)
1742 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1743 a->lines, a->nlines, 1, *a->selected_line,
1744 a->first_displayed_line, a->last_displayed_line, a->eof,
1745 a->view->nlines);
1747 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1748 err = got_error_from_errno();
1750 return (void *)err;
1753 static struct got_object_id *
1754 get_selected_commit_id(struct tog_blame_line *lines,
1755 int first_displayed_line, int selected_line)
1757 struct tog_blame_line *line;
1759 line = &lines[first_displayed_line - 1 + selected_line - 1];
1760 if (!line->annotated)
1761 return NULL;
1763 return line->id;
1766 static const struct got_error *
1767 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1768 struct tog_blame_line *lines, int first_displayed_line,
1769 int selected_line, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_commit_object *commit = NULL;
1773 struct got_object_id *selected_id;
1774 struct got_object_qid *pid;
1776 *pobj = NULL;
1777 *obj = NULL;
1779 selected_id = get_selected_commit_id(lines,
1780 first_displayed_line, selected_line);
1781 if (selected_id == NULL)
1782 return NULL;
1784 err = got_object_open(obj, repo, selected_id);
1785 if (err)
1786 goto done;
1788 err = got_object_commit_open(&commit, repo, *obj);
1789 if (err)
1790 goto done;
1792 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1793 if (pid) {
1794 err = got_object_open(pobj, repo, pid->id);
1795 if (err)
1796 goto done;
1798 done:
1799 if (commit)
1800 got_object_commit_close(commit);
1801 return err;
1804 static const struct got_error *
1805 stop_blame(struct tog_blame *blame)
1807 const struct got_error *err = NULL;
1808 int i;
1810 if (blame->thread) {
1811 if (pthread_join(blame->thread, (void **)&err) != 0)
1812 err = got_error_from_errno();
1813 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1814 err = NULL;
1815 blame->thread = NULL;
1817 if (blame->thread_args.repo) {
1818 got_repo_close(blame->thread_args.repo);
1819 blame->thread_args.repo = NULL;
1821 if (blame->f) {
1822 fclose(blame->f);
1823 blame->f = NULL;
1825 for (i = 0; i < blame->nlines; i++)
1826 free(blame->lines[i].id);
1827 free(blame->lines);
1828 blame->lines = NULL;
1829 free(blame->cb_args.commit_id);
1830 blame->cb_args.commit_id = NULL;
1832 return err;
1835 static const struct got_error *
1836 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1837 struct tog_view *view, int *blame_complete,
1838 int *first_displayed_line, int *last_displayed_line,
1839 int *selected_line, int *done, int *eof, const char *path,
1840 struct got_object_id *commit_id,
1841 struct got_repository *repo)
1843 const struct got_error *err = NULL;
1844 struct got_blob_object *blob = NULL;
1845 struct got_repository *thread_repo = NULL;
1846 struct got_object *obj;
1848 err = got_object_open_by_path(&obj, repo, commit_id, path);
1849 if (err)
1850 goto done;
1851 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1852 err = got_error(GOT_ERR_OBJ_TYPE);
1853 goto done;
1856 err = got_object_blob_open(&blob, repo, obj, 8192);
1857 if (err)
1858 goto done;
1859 blame->f = got_opentemp();
1860 if (blame->f == NULL) {
1861 err = got_error_from_errno();
1862 goto done;
1864 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1865 blame->f, blob);
1866 if (err)
1867 goto done;
1869 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1870 if (blame->lines == NULL) {
1871 err = got_error_from_errno();
1872 goto done;
1875 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1876 if (err)
1877 goto done;
1879 blame->cb_args.view = view;
1880 blame->cb_args.lines = blame->lines;
1881 blame->cb_args.nlines = blame->nlines;
1882 blame->cb_args.mutex = mutex;
1883 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1884 if (blame->cb_args.commit_id == NULL) {
1885 err = got_error_from_errno();
1886 goto done;
1888 blame->cb_args.f = blame->f;
1889 blame->cb_args.path = path;
1890 blame->cb_args.first_displayed_line = first_displayed_line;
1891 blame->cb_args.selected_line = selected_line;
1892 blame->cb_args.last_displayed_line = last_displayed_line;
1893 blame->cb_args.quit = done;
1894 blame->cb_args.eof = eof;
1896 blame->thread_args.path = path;
1897 blame->thread_args.repo = thread_repo;
1898 blame->thread_args.cb_args = &blame->cb_args;
1899 blame->thread_args.complete = blame_complete;
1900 *blame_complete = 0;
1902 if (pthread_create(&blame->thread, NULL, blame_thread,
1903 &blame->thread_args) != 0) {
1904 err = got_error_from_errno();
1905 goto done;
1908 done:
1909 if (blob)
1910 got_object_blob_close(blob);
1911 if (obj)
1912 got_object_close(obj);
1913 if (err)
1914 stop_blame(blame);
1915 return err;
1918 static const struct got_error *
1919 open_blame_view(struct tog_view *view, char *path,
1920 struct got_object_id *commit_id, struct got_repository *repo)
1922 const struct got_error *err = NULL;
1923 struct tog_blame_view_state *s = &view->state.blame;
1925 SIMPLEQ_INIT(&s->blamed_commits);
1927 if (pthread_mutex_init(&s->mutex, NULL) != 0)
1928 return got_error_from_errno();
1930 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1931 if (err)
1932 return err;
1934 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1935 s->first_displayed_line = 1;
1936 s->last_displayed_line = view->nlines;
1937 s->selected_line = 1;
1938 s->blame_complete = 0;
1939 s->path = path;
1940 if (s->path == NULL)
1941 return got_error_from_errno();
1942 s->repo = repo;
1943 s->commit_id = commit_id;
1944 memset(&s->blame, 0, sizeof(s->blame));
1946 view->show = show_blame_view;
1947 view->input = input_blame_view;
1948 view->close = close_blame_view;
1950 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1951 &s->first_displayed_line, &s->last_displayed_line,
1952 &s->selected_line, &s->done, &s->eof, s->path,
1953 s->blamed_commit->id, s->repo);
1956 static const struct got_error *
1957 close_blame_view(struct tog_view *view)
1959 const struct got_error *err = NULL;
1960 struct tog_blame_view_state *s = &view->state.blame;
1962 if (s->blame.thread)
1963 err = stop_blame(&s->blame);
1965 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1966 struct got_object_qid *blamed_commit;
1967 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1968 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1969 got_object_qid_free(blamed_commit);
1972 free(s->path);
1974 return err;
1977 static const struct got_error *
1978 show_blame_view(struct tog_view *view)
1980 const struct got_error *err = NULL;
1981 struct tog_blame_view_state *s = &view->state.blame;
1983 if (pthread_mutex_lock(&s->mutex) != 0)
1984 return got_error_from_errno();
1986 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1987 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1988 s->selected_line, &s->first_displayed_line,
1989 &s->last_displayed_line, &s->eof, view->nlines);
1991 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
1992 err = got_error_from_errno();
1994 return err;
1997 static const struct got_error *
1998 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
1999 struct tog_view *view, int ch)
2001 const struct got_error *err = NULL, *thread_err = NULL;
2002 struct got_object *obj = NULL, *pobj = NULL;
2003 struct tog_view *diff_view;
2004 struct tog_blame_view_state *s = &view->state.blame;
2006 if (pthread_mutex_lock(&s->mutex) != 0) {
2007 err = got_error_from_errno();
2008 goto done;
2011 switch (ch) {
2012 case 'q':
2013 s->done = 1;
2014 if (pthread_mutex_unlock(&s->mutex) != 0) {
2015 err = got_error_from_errno();
2016 goto done;
2018 return stop_blame(&s->blame);
2019 case 'k':
2020 case KEY_UP:
2021 if (s->selected_line > 1)
2022 s->selected_line--;
2023 else if (s->selected_line == 1 &&
2024 s->first_displayed_line > 1)
2025 s->first_displayed_line--;
2026 break;
2027 case KEY_PPAGE:
2028 if (s->first_displayed_line == 1) {
2029 s->selected_line = 1;
2030 break;
2032 if (s->first_displayed_line > view->nlines - 2)
2033 s->first_displayed_line -=
2034 (view->nlines - 2);
2035 else
2036 s->first_displayed_line = 1;
2037 break;
2038 case 'j':
2039 case KEY_DOWN:
2040 if (s->selected_line < view->nlines - 2 &&
2041 s->first_displayed_line +
2042 s->selected_line <= s->blame.nlines)
2043 s->selected_line++;
2044 else if (s->last_displayed_line <
2045 s->blame.nlines)
2046 s->first_displayed_line++;
2047 break;
2048 case 'b':
2049 case 'p': {
2050 struct got_object_id *id;
2051 id = get_selected_commit_id(s->blame.lines,
2052 s->first_displayed_line, s->selected_line);
2053 if (id == NULL || got_object_id_cmp(id,
2054 s->blamed_commit->id) == 0)
2055 break;
2056 err = open_selected_commit(&pobj, &obj,
2057 s->blame.lines, s->first_displayed_line,
2058 s->selected_line, s->repo);
2059 if (err)
2060 break;
2061 if (pobj == NULL && obj == NULL)
2062 break;
2063 if (ch == 'p' && pobj == NULL)
2064 break;
2065 s->done = 1;
2066 if (pthread_mutex_unlock(&s->mutex) != 0) {
2067 err = got_error_from_errno();
2068 goto done;
2070 thread_err = stop_blame(&s->blame);
2071 s->done = 0;
2072 if (pthread_mutex_lock(&s->mutex) != 0) {
2073 err = got_error_from_errno();
2074 goto done;
2076 if (thread_err)
2077 break;
2078 id = got_object_get_id(ch == 'b' ? obj : pobj);
2079 got_object_close(obj);
2080 obj = NULL;
2081 if (pobj) {
2082 got_object_close(pobj);
2083 pobj = NULL;
2085 if (id == NULL) {
2086 err = got_error_from_errno();
2087 break;
2089 err = got_object_qid_alloc(
2090 &s->blamed_commit, id);
2091 free(id);
2092 if (err)
2093 goto done;
2094 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2095 s->blamed_commit, entry);
2096 err = run_blame(&s->blame, &s->mutex, view,
2097 &s->blame_complete,
2098 &s->first_displayed_line,
2099 &s->last_displayed_line,
2100 &s->selected_line, &s->done, &s->eof,
2101 s->path, s->blamed_commit->id, s->repo);
2102 if (err)
2103 break;
2104 break;
2106 case 'B': {
2107 struct got_object_qid *first;
2108 first = SIMPLEQ_FIRST(&s->blamed_commits);
2109 if (!got_object_id_cmp(first->id, s->commit_id))
2110 break;
2111 s->done = 1;
2112 if (pthread_mutex_unlock(&s->mutex) != 0) {
2113 err = got_error_from_errno();
2114 goto done;
2116 thread_err = stop_blame(&s->blame);
2117 s->done = 0;
2118 if (pthread_mutex_lock(&s->mutex) != 0) {
2119 err = got_error_from_errno();
2120 goto done;
2122 if (thread_err)
2123 break;
2124 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2125 got_object_qid_free(s->blamed_commit);
2126 s->blamed_commit =
2127 SIMPLEQ_FIRST(&s->blamed_commits);
2128 err = run_blame(&s->blame, &s->mutex, view,
2129 &s->blame_complete,
2130 &s->first_displayed_line,
2131 &s->last_displayed_line,
2132 &s->selected_line, &s->done, &s->eof, s->path,
2133 s->blamed_commit->id, s->repo);
2134 if (err)
2135 break;
2136 break;
2138 case KEY_ENTER:
2139 case '\r':
2140 err = open_selected_commit(&pobj, &obj,
2141 s->blame.lines, s->first_displayed_line,
2142 s->selected_line, s->repo);
2143 if (err)
2144 break;
2145 if (pobj == NULL && obj == NULL)
2146 break;
2147 diff_view = view_open(0, 0, 0, 0, view,
2148 TOG_VIEW_DIFF);
2149 if (diff_view == NULL) {
2150 err = got_error_from_errno();
2151 break;
2153 err = open_diff_view(diff_view, pobj, obj,
2154 s->repo);
2155 if (err) {
2156 view_close(diff_view);
2157 break;
2159 *new_view = diff_view;
2160 if (pobj) {
2161 got_object_close(pobj);
2162 pobj = NULL;
2164 got_object_close(obj);
2165 obj = NULL;
2166 if (err)
2167 break;
2168 break;
2169 case KEY_NPAGE:
2170 case ' ':
2171 if (s->last_displayed_line >= s->blame.nlines &&
2172 s->selected_line < view->nlines - 2) {
2173 s->selected_line = MIN(s->blame.nlines,
2174 view->nlines - 2);
2175 break;
2177 if (s->last_displayed_line + view->nlines - 2
2178 <= s->blame.nlines)
2179 s->first_displayed_line +=
2180 view->nlines - 2;
2181 else
2182 s->first_displayed_line =
2183 s->blame.nlines -
2184 (view->nlines - 3);
2185 break;
2186 case KEY_RESIZE:
2187 if (s->selected_line > view->nlines - 2) {
2188 s->selected_line = MIN(s->blame.nlines,
2189 view->nlines - 2);
2191 break;
2192 default:
2193 break;
2196 if (pthread_mutex_unlock(&s->mutex) != 0)
2197 err = got_error_from_errno();
2198 done:
2199 if (pobj)
2200 got_object_close(pobj);
2201 return thread_err ? thread_err : err;
2204 static const struct got_error *
2205 cmd_blame(int argc, char *argv[])
2207 const struct got_error *error;
2208 struct got_repository *repo = NULL;
2209 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2210 struct got_object_id *commit_id = NULL;
2211 char *commit_id_str = NULL;
2212 int ch;
2213 struct tog_view *view;
2215 #ifndef PROFILE
2216 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2217 err(1, "pledge");
2218 #endif
2220 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2221 switch (ch) {
2222 case 'c':
2223 commit_id_str = optarg;
2224 break;
2225 case 'r':
2226 repo_path = realpath(optarg, NULL);
2227 if (repo_path == NULL)
2228 err(1, "-r option");
2229 break;
2230 default:
2231 usage();
2232 /* NOTREACHED */
2236 argc -= optind;
2237 argv += optind;
2239 if (argc == 1)
2240 path = argv[0];
2241 else
2242 usage_blame();
2244 cwd = getcwd(NULL, 0);
2245 if (cwd == NULL) {
2246 error = got_error_from_errno();
2247 goto done;
2249 if (repo_path == NULL) {
2250 repo_path = strdup(cwd);
2251 if (repo_path == NULL) {
2252 error = got_error_from_errno();
2253 goto done;
2258 error = got_repo_open(&repo, repo_path);
2259 if (error != NULL)
2260 return error;
2262 error = got_repo_map_path(&in_repo_path, repo, path);
2263 if (error != NULL)
2264 goto done;
2266 if (commit_id_str == NULL) {
2267 struct got_reference *head_ref;
2268 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2269 if (error != NULL)
2270 goto done;
2271 error = got_ref_resolve(&commit_id, repo, head_ref);
2272 got_ref_close(head_ref);
2273 } else {
2274 struct got_object *obj;
2275 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2276 if (error != NULL)
2277 goto done;
2278 commit_id = got_object_get_id(obj);
2279 if (commit_id == NULL)
2280 error = got_error_from_errno();
2281 got_object_close(obj);
2283 if (error != NULL)
2284 goto done;
2286 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2287 if (view == NULL) {
2288 error = got_error_from_errno();
2289 goto done;
2291 error = open_blame_view(view, in_repo_path, commit_id, repo);
2292 if (error)
2293 goto done;
2294 error = view_loop(view);
2295 done:
2296 free(repo_path);
2297 free(cwd);
2298 free(commit_id);
2299 if (repo)
2300 got_repo_close(repo);
2301 return error;
2304 static const struct got_error *
2305 draw_tree_entries(struct tog_view *view,
2306 struct got_tree_entry **first_displayed_entry,
2307 struct got_tree_entry **last_displayed_entry,
2308 struct got_tree_entry **selected_entry, int *ndisplayed,
2309 const char *label, int show_ids, const char *parent_path,
2310 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2312 const struct got_error *err = NULL;
2313 struct got_tree_entry *te;
2314 wchar_t *wline;
2315 int width, n;
2317 *ndisplayed = 0;
2319 werase(view->window);
2321 if (limit == 0)
2322 return NULL;
2324 err = format_line(&wline, &width, label, view->ncols);
2325 if (err)
2326 return err;
2327 waddwstr(view->window, wline);
2328 free(wline);
2329 wline = NULL;
2330 if (width < view->ncols)
2331 waddch(view->window, '\n');
2332 if (--limit <= 0)
2333 return NULL;
2334 err = format_line(&wline, &width, parent_path, view->ncols);
2335 if (err)
2336 return err;
2337 waddwstr(view->window, wline);
2338 free(wline);
2339 wline = NULL;
2340 if (width < view->ncols)
2341 waddch(view->window, '\n');
2342 if (--limit <= 0)
2343 return NULL;
2344 waddch(view->window, '\n');
2345 if (--limit <= 0)
2346 return NULL;
2348 te = SIMPLEQ_FIRST(&entries->head);
2349 if (*first_displayed_entry == NULL) {
2350 if (selected == 0) {
2351 wstandout(view->window);
2352 *selected_entry = NULL;
2354 waddstr(view->window, " ..\n"); /* parent directory */
2355 if (selected == 0)
2356 wstandend(view->window);
2357 (*ndisplayed)++;
2358 if (--limit <= 0)
2359 return NULL;
2360 n = 1;
2361 } else {
2362 n = 0;
2363 while (te != *first_displayed_entry)
2364 te = SIMPLEQ_NEXT(te, entry);
2367 while (te) {
2368 char *line = NULL, *id_str = NULL;
2370 if (show_ids) {
2371 err = got_object_id_str(&id_str, te->id);
2372 if (err)
2373 return got_error_from_errno();
2375 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2376 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2377 free(id_str);
2378 return got_error_from_errno();
2380 free(id_str);
2381 err = format_line(&wline, &width, line, view->ncols);
2382 if (err) {
2383 free(line);
2384 break;
2386 if (n == selected) {
2387 wstandout(view->window);
2388 *selected_entry = te;
2390 waddwstr(view->window, wline);
2391 if (width < view->ncols)
2392 waddch(view->window, '\n');
2393 if (n == selected)
2394 wstandend(view->window);
2395 free(line);
2396 free(wline);
2397 wline = NULL;
2398 n++;
2399 (*ndisplayed)++;
2400 *last_displayed_entry = te;
2401 if (--limit <= 0)
2402 break;
2403 te = SIMPLEQ_NEXT(te, entry);
2406 return err;
2409 static void
2410 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2411 const struct got_tree_entries *entries, int isroot)
2413 struct got_tree_entry *te, *prev;
2414 int i;
2416 if (*first_displayed_entry == NULL)
2417 return;
2419 te = SIMPLEQ_FIRST(&entries->head);
2420 if (*first_displayed_entry == te) {
2421 if (!isroot)
2422 *first_displayed_entry = NULL;
2423 return;
2426 /* XXX this is stupid... switch to TAILQ? */
2427 for (i = 0; i < maxscroll; i++) {
2428 while (te != *first_displayed_entry) {
2429 prev = te;
2430 te = SIMPLEQ_NEXT(te, entry);
2432 *first_displayed_entry = prev;
2433 te = SIMPLEQ_FIRST(&entries->head);
2435 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2436 *first_displayed_entry = NULL;
2439 static void
2440 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2441 struct got_tree_entry *last_displayed_entry,
2442 const struct got_tree_entries *entries)
2444 struct got_tree_entry *next;
2445 int n = 0;
2447 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2448 return;
2450 if (*first_displayed_entry)
2451 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2452 else
2453 next = SIMPLEQ_FIRST(&entries->head);
2454 while (next) {
2455 *first_displayed_entry = next;
2456 if (++n >= maxscroll)
2457 break;
2458 next = SIMPLEQ_NEXT(next, entry);
2462 static const struct got_error *
2463 tree_entry_path(char **path, struct tog_parent_trees *parents,
2464 struct got_tree_entry *te)
2466 const struct got_error *err = NULL;
2467 struct tog_parent_tree *pt;
2468 size_t len = 2; /* for leading slash and NUL */
2470 TAILQ_FOREACH(pt, parents, entry)
2471 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2472 if (te)
2473 len += strlen(te->name);
2475 *path = calloc(1, len);
2476 if (path == NULL)
2477 return got_error_from_errno();
2479 (*path)[0] = '/';
2480 pt = TAILQ_LAST(parents, tog_parent_trees);
2481 while (pt) {
2482 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2483 err = got_error(GOT_ERR_NO_SPACE);
2484 goto done;
2486 if (strlcat(*path, "/", len) >= len) {
2487 err = got_error(GOT_ERR_NO_SPACE);
2488 goto done;
2490 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2492 if (te) {
2493 if (strlcat(*path, te->name, len) >= len) {
2494 err = got_error(GOT_ERR_NO_SPACE);
2495 goto done;
2498 done:
2499 if (err) {
2500 free(*path);
2501 *path = NULL;
2503 return err;
2506 static const struct got_error *
2507 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2508 struct got_tree_entry *te, struct tog_parent_trees *parents,
2509 struct got_object_id *commit_id, struct got_repository *repo)
2511 const struct got_error *err = NULL;
2512 char *path;
2513 struct tog_view *blame_view;
2515 err = tree_entry_path(&path, parents, te);
2516 if (err)
2517 return err;
2519 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2520 if (blame_view == NULL)
2521 return got_error_from_errno();
2523 err = open_blame_view(blame_view, path, commit_id, repo);
2524 if (err) {
2525 view_close(blame_view);
2526 free(path);
2527 } else
2528 *new_view = blame_view;
2529 return err;
2532 static const struct got_error *
2533 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2534 struct got_tree_entry *te, struct tog_parent_trees *parents,
2535 struct got_object_id *commit_id, struct got_repository *repo)
2537 struct tog_view *log_view;
2538 const struct got_error *err = NULL;
2539 char *path;
2541 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2542 if (log_view == NULL)
2543 return got_error_from_errno();
2545 err = tree_entry_path(&path, parents, te);
2546 if (err)
2547 return err;
2549 err = open_log_view(log_view, commit_id, repo, path);
2550 if (err)
2551 view_close(log_view);
2552 else
2553 *new_view = log_view;
2554 free(path);
2555 return err;
2558 static const struct got_error *
2559 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2560 struct got_object_id *commit_id, struct got_repository *repo)
2562 const struct got_error *err = NULL;
2563 char *commit_id_str = NULL;
2564 struct tog_tree_view_state *s = &view->state.tree;
2566 TAILQ_INIT(&s->parents);
2568 err = got_object_id_str(&commit_id_str, commit_id);
2569 if (err != NULL)
2570 goto done;
2572 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2573 err = got_error_from_errno();
2574 goto done;
2577 s->root = s->tree = root;
2578 s->entries = got_object_tree_get_entries(root);
2579 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2580 s->commit_id = commit_id;
2581 s->repo = repo;
2583 view->show = show_tree_view;
2584 view->input = input_tree_view;
2585 view->close = close_tree_view;
2586 done:
2587 free(commit_id_str);
2588 if (err)
2589 free(s->tree_label);
2590 return err;
2593 static const struct got_error *
2594 close_tree_view(struct tog_view *view)
2596 struct tog_tree_view_state *s = &view->state.tree;
2598 free(s->tree_label);
2599 while (!TAILQ_EMPTY(&s->parents)) {
2600 struct tog_parent_tree *parent;
2601 parent = TAILQ_FIRST(&s->parents);
2602 TAILQ_REMOVE(&s->parents, parent, entry);
2603 free(parent);
2606 if (s->tree != s->root)
2607 got_object_tree_close(s->tree);
2608 got_object_tree_close(s->root);
2610 return NULL;
2613 static const struct got_error *
2614 show_tree_view(struct tog_view *view)
2616 const struct got_error *err = NULL;
2617 struct tog_tree_view_state *s = &view->state.tree;
2618 char *parent_path;
2620 err = tree_entry_path(&parent_path, &s->parents, NULL);
2621 if (err)
2622 return err;
2624 err = draw_tree_entries(view, &s->first_displayed_entry,
2625 &s->last_displayed_entry, &s->selected_entry,
2626 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2627 s->entries, s->selected, view->nlines, s->tree == s->root);
2628 free(parent_path);
2629 return err;
2632 static const struct got_error *
2633 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2634 struct tog_view *view, int ch)
2636 const struct got_error *err = NULL;
2637 struct tog_tree_view_state *s = &view->state.tree;
2639 switch (ch) {
2640 case 'i':
2641 s->show_ids = !s->show_ids;
2642 break;
2643 case 'l':
2644 if (s->selected_entry) {
2645 err = log_tree_entry(new_view, view,
2646 s->selected_entry, &s->parents,
2647 s->commit_id, s->repo);
2649 break;
2650 case 'k':
2651 case KEY_UP:
2652 if (s->selected > 0)
2653 s->selected--;
2654 if (s->selected > 0)
2655 break;
2656 tree_scroll_up(&s->first_displayed_entry, 1,
2657 s->entries, s->tree == s->root);
2658 break;
2659 case KEY_PPAGE:
2660 if (SIMPLEQ_FIRST(&s->entries->head) ==
2661 s->first_displayed_entry) {
2662 if (s->tree != s->root)
2663 s->first_displayed_entry = NULL;
2664 s->selected = 0;
2665 break;
2667 tree_scroll_up(&s->first_displayed_entry,
2668 view->nlines, s->entries,
2669 s->tree == s->root);
2670 break;
2671 case 'j':
2672 case KEY_DOWN:
2673 if (s->selected < s->ndisplayed - 1) {
2674 s->selected++;
2675 break;
2677 tree_scroll_down(&s->first_displayed_entry, 1,
2678 s->last_displayed_entry, s->entries);
2679 break;
2680 case KEY_NPAGE:
2681 tree_scroll_down(&s->first_displayed_entry,
2682 view->nlines, s->last_displayed_entry,
2683 s->entries);
2684 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2685 entry))
2686 break;
2687 /* can't scroll any further; move cursor down */
2688 if (s->selected < s->ndisplayed - 1)
2689 s->selected = s->ndisplayed - 1;
2690 break;
2691 case KEY_ENTER:
2692 case '\r':
2693 if (s->selected_entry == NULL) {
2694 struct tog_parent_tree *parent;
2695 case KEY_LEFT:
2696 /* user selected '..' */
2697 if (s->tree == s->root)
2698 break;
2699 parent = TAILQ_FIRST(&s->parents);
2700 TAILQ_REMOVE(&s->parents, parent,
2701 entry);
2702 got_object_tree_close(s->tree);
2703 s->tree = parent->tree;
2704 s->entries =
2705 got_object_tree_get_entries(s->tree);
2706 s->first_displayed_entry =
2707 parent->first_displayed_entry;
2708 s->selected_entry =
2709 parent->selected_entry;
2710 s->selected = parent->selected;
2711 free(parent);
2712 } else if (S_ISDIR(s->selected_entry->mode)) {
2713 struct tog_parent_tree *parent;
2714 struct got_tree_object *child;
2715 err = got_object_open_as_tree(&child,
2716 s->repo, s->selected_entry->id);
2717 if (err)
2718 break;
2719 parent = calloc(1, sizeof(*parent));
2720 if (parent == NULL) {
2721 err = got_error_from_errno();
2722 break;
2724 parent->tree = s->tree;
2725 parent->first_displayed_entry =
2726 s->first_displayed_entry;
2727 parent->selected_entry = s->selected_entry;
2728 parent->selected = s->selected;
2729 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2730 s->tree = child;
2731 s->entries =
2732 got_object_tree_get_entries(s->tree);
2733 s->selected = 0;
2734 s->first_displayed_entry = NULL;
2735 } else if (S_ISREG(s->selected_entry->mode)) {
2736 err = blame_tree_entry(new_view, view,
2737 s->selected_entry, &s->parents,
2738 s->commit_id, s->repo);
2739 if (err)
2740 break;
2742 break;
2743 case KEY_RESIZE:
2744 if (s->selected > view->nlines)
2745 s->selected = s->ndisplayed - 1;
2746 break;
2747 default:
2748 break;
2751 return err;
2754 __dead static void
2755 usage_tree(void)
2757 endwin();
2758 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2759 getprogname());
2760 exit(1);
2763 static const struct got_error *
2764 cmd_tree(int argc, char *argv[])
2766 const struct got_error *error;
2767 struct got_repository *repo = NULL;
2768 char *repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_arg = NULL;
2771 struct got_commit_object *commit = NULL;
2772 struct got_tree_object *tree = NULL;
2773 int ch;
2774 struct tog_view *view;
2776 #ifndef PROFILE
2777 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2778 err(1, "pledge");
2779 #endif
2781 while ((ch = getopt(argc, argv, "c:")) != -1) {
2782 switch (ch) {
2783 case 'c':
2784 commit_id_arg = optarg;
2785 break;
2786 default:
2787 usage();
2788 /* NOTREACHED */
2792 argc -= optind;
2793 argv += optind;
2795 if (argc == 0) {
2796 repo_path = getcwd(NULL, 0);
2797 if (repo_path == NULL)
2798 return got_error_from_errno();
2799 } else if (argc == 1) {
2800 repo_path = realpath(argv[0], NULL);
2801 if (repo_path == NULL)
2802 return got_error_from_errno();
2803 } else
2804 usage_log();
2806 error = got_repo_open(&repo, repo_path);
2807 free(repo_path);
2808 if (error != NULL)
2809 return error;
2811 if (commit_id_arg == NULL) {
2812 error = get_head_commit_id(&commit_id, repo);
2813 if (error != NULL)
2814 goto done;
2815 } else {
2816 struct got_object *obj;
2817 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2818 if (error == NULL) {
2819 commit_id = got_object_get_id(obj);
2820 if (commit_id == NULL)
2821 error = got_error_from_errno();
2824 if (error != NULL)
2825 goto done;
2827 error = got_object_open_as_commit(&commit, repo, commit_id);
2828 if (error != NULL)
2829 goto done;
2831 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2832 if (error != NULL)
2833 goto done;
2835 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2836 if (view == NULL) {
2837 error = got_error_from_errno();
2838 goto done;
2840 error = open_tree_view(view, tree, commit_id, repo);
2841 if (error)
2842 goto done;
2843 error = view_loop(view);
2844 done:
2845 free(commit_id);
2846 if (commit)
2847 got_object_commit_close(commit);
2848 if (tree)
2849 got_object_tree_close(tree);
2850 if (repo)
2851 got_repo_close(repo);
2852 return error;
2854 static void
2855 init_curses(void)
2857 initscr();
2858 cbreak();
2859 noecho();
2860 nonl();
2861 intrflush(stdscr, FALSE);
2862 keypad(stdscr, TRUE);
2863 curs_set(0);
2866 __dead static void
2867 usage(void)
2869 int i;
2871 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2872 "Available commands:\n", getprogname());
2873 for (i = 0; i < nitems(tog_commands); i++) {
2874 struct tog_cmd *cmd = &tog_commands[i];
2875 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2877 exit(1);
2880 static char **
2881 make_argv(const char *arg0, const char *arg1)
2883 char **argv;
2884 int argc = (arg1 == NULL ? 1 : 2);
2886 argv = calloc(argc, sizeof(char *));
2887 if (argv == NULL)
2888 err(1, "calloc");
2889 argv[0] = strdup(arg0);
2890 if (argv[0] == NULL)
2891 err(1, "calloc");
2892 if (arg1) {
2893 argv[1] = strdup(arg1);
2894 if (argv[1] == NULL)
2895 err(1, "calloc");
2898 return argv;
2901 int
2902 main(int argc, char *argv[])
2904 const struct got_error *error = NULL;
2905 struct tog_cmd *cmd = NULL;
2906 int ch, hflag = 0;
2907 char **cmd_argv = NULL;
2909 setlocale(LC_ALL, "");
2911 while ((ch = getopt(argc, argv, "h")) != -1) {
2912 switch (ch) {
2913 case 'h':
2914 hflag = 1;
2915 break;
2916 default:
2917 usage();
2918 /* NOTREACHED */
2922 argc -= optind;
2923 argv += optind;
2924 optind = 0;
2925 optreset = 1;
2927 if (argc == 0) {
2928 if (hflag)
2929 usage();
2930 /* Build an argument vector which runs a default command. */
2931 cmd = &tog_commands[0];
2932 cmd_argv = make_argv(cmd->name, NULL);
2933 argc = 1;
2934 } else {
2935 int i;
2937 /* Did the user specific a command? */
2938 for (i = 0; i < nitems(tog_commands); i++) {
2939 if (strncmp(tog_commands[i].name, argv[0],
2940 strlen(argv[0])) == 0) {
2941 cmd = &tog_commands[i];
2942 if (hflag)
2943 tog_commands[i].cmd_usage();
2944 break;
2947 if (cmd == NULL) {
2948 /* Did the user specify a repository? */
2949 char *repo_path = realpath(argv[0], NULL);
2950 if (repo_path) {
2951 struct got_repository *repo;
2952 error = got_repo_open(&repo, repo_path);
2953 if (error == NULL)
2954 got_repo_close(repo);
2955 } else
2956 error = got_error_from_errno();
2957 if (error) {
2958 if (hflag) {
2959 fprintf(stderr, "%s: '%s' is not a "
2960 "known command\n", getprogname(),
2961 argv[0]);
2962 usage();
2964 fprintf(stderr, "%s: '%s' is neither a known "
2965 "command nor a path to a repository\n",
2966 getprogname(), argv[0]);
2967 free(repo_path);
2968 return 1;
2970 cmd = &tog_commands[0];
2971 cmd_argv = make_argv(cmd->name, repo_path);
2972 argc = 2;
2973 free(repo_path);
2977 init_curses();
2979 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2980 if (error)
2981 goto done;
2982 done:
2983 endwin();
2984 free(cmd_argv);
2985 if (error)
2986 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2987 return 0;