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;
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 'q':
366 err = view->input(new, dead, view, ch);
367 *dead = view;
368 break;
369 case 'Q':
370 *done = 1;
371 break;
372 case KEY_RESIZE:
373 err = view_resize(view);
374 if (err)
375 return err;
376 err = view->input(new, dead, view, ch);
377 break;
378 default:
379 err = view->input(new, dead, view, ch);
380 break;
383 return err;
386 static const struct got_error *
387 view_loop(struct tog_view *view)
389 const struct got_error *err = NULL;
390 struct tog_view_list_head views;
391 struct tog_view *new_view, *dead_view;
392 int done = 0;
394 TAILQ_INIT(&views);
395 TAILQ_INSERT_HEAD(&views, view, entry);
397 while (!TAILQ_EMPTY(&views) && !done) {
398 err = view_show(view);
399 if (err)
400 break;
401 err = view_input(&new_view, &dead_view, &view, &done,
402 view, &views);
403 if (err)
404 break;
405 if (new_view) {
406 TAILQ_INSERT_TAIL(&views, new_view, entry);
407 view = new_view;
409 if (dead_view) {
410 TAILQ_REMOVE(&views, dead_view, entry);
411 TAILQ_FOREACH(view, &views, entry) {
412 if (view->parent == dead_view)
413 view->parent = NULL;
415 if (dead_view->parent)
416 view = dead_view->parent;
417 else
418 view = TAILQ_LAST(&views, tog_view_list_head);
419 err = view_close(dead_view);
420 if (err)
421 goto done;
424 done:
425 while (!TAILQ_EMPTY(&views)) {
426 view = TAILQ_FIRST(&views);
427 TAILQ_REMOVE(&views, view, entry);
428 view_close(view);
430 return err;
433 __dead static void
434 usage_log(void)
436 endwin();
437 fprintf(stderr,
438 "usage: %s log [-c commit] [-r repository-path] [path]\n",
439 getprogname());
440 exit(1);
443 /* Create newly allocated wide-character string equivalent to a byte string. */
444 static const struct got_error *
445 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
447 char *vis = NULL;
448 const struct got_error *err = NULL;
450 *ws = NULL;
451 *wlen = mbstowcs(NULL, s, 0);
452 if (*wlen == (size_t)-1) {
453 int vislen;
454 if (errno != EILSEQ)
455 return got_error_from_errno();
457 /* byte string invalid in current encoding; try to "fix" it */
458 err = got_mbsavis(&vis, &vislen, s);
459 if (err)
460 return err;
461 *wlen = mbstowcs(NULL, vis, 0);
462 if (*wlen == (size_t)-1) {
463 err = got_error_from_errno(); /* give up */
464 goto done;
468 *ws = calloc(*wlen + 1, sizeof(*ws));
469 if (*ws == NULL) {
470 err = got_error_from_errno();
471 goto done;
474 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
475 err = got_error_from_errno();
476 done:
477 free(vis);
478 if (err) {
479 free(*ws);
480 *ws = NULL;
481 *wlen = 0;
483 return err;
486 /* Format a line for display, ensuring that it won't overflow a width limit. */
487 static const struct got_error *
488 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
490 const struct got_error *err = NULL;
491 int cols = 0;
492 wchar_t *wline = NULL;
493 size_t wlen;
494 int i;
496 *wlinep = NULL;
497 *widthp = 0;
499 err = mbs2ws(&wline, &wlen, line);
500 if (err)
501 return err;
503 i = 0;
504 while (i < wlen && cols < wlimit) {
505 int width = wcwidth(wline[i]);
506 switch (width) {
507 case 0:
508 i++;
509 break;
510 case 1:
511 case 2:
512 if (cols + width <= wlimit) {
513 cols += width;
514 i++;
516 break;
517 case -1:
518 if (wline[i] == L'\t')
519 cols += TABSIZE - ((cols + 1) % TABSIZE);
520 i++;
521 break;
522 default:
523 err = got_error_from_errno();
524 goto done;
527 wline[i] = L'\0';
528 if (widthp)
529 *widthp = cols;
530 done:
531 if (err)
532 free(wline);
533 else
534 *wlinep = wline;
535 return err;
538 static const struct got_error *
539 draw_commit(struct tog_view *view, struct got_commit_object *commit,
540 struct got_object_id *id)
542 const struct got_error *err = NULL;
543 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
544 char *logmsg0 = NULL, *logmsg = NULL;
545 char *author0 = NULL, *author = NULL;
546 wchar_t *wlogmsg = NULL, *wauthor = NULL;
547 int author_width, logmsg_width;
548 char *newline, *smallerthan;
549 char *line = NULL;
550 int col, limit;
551 static const size_t date_display_cols = 9;
552 static const size_t author_display_cols = 16;
553 const int avail = view->ncols;
555 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
556 &commit->tm_committer) >= sizeof(datebuf))
557 return got_error(GOT_ERR_NO_SPACE);
559 if (avail < date_display_cols)
560 limit = MIN(sizeof(datebuf) - 1, avail);
561 else
562 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
563 waddnstr(view->window, datebuf, limit);
564 col = limit + 1;
565 if (col > avail)
566 goto done;
568 author0 = strdup(commit->author);
569 if (author0 == NULL) {
570 err = got_error_from_errno();
571 goto done;
573 author = author0;
574 smallerthan = strchr(author, '<');
575 if (smallerthan)
576 *smallerthan = '\0';
577 else {
578 char *at = strchr(author, '@');
579 if (at)
580 *at = '\0';
582 limit = avail - col;
583 err = format_line(&wauthor, &author_width, author, limit);
584 if (err)
585 goto done;
586 waddwstr(view->window, wauthor);
587 col += author_width;
588 while (col <= avail && author_width < author_display_cols + 1) {
589 waddch(view->window, ' ');
590 col++;
591 author_width++;
593 if (col > avail)
594 goto done;
596 logmsg0 = strdup(commit->logmsg);
597 if (logmsg0 == NULL) {
598 err = got_error_from_errno();
599 goto done;
601 logmsg = logmsg0;
602 while (*logmsg == '\n')
603 logmsg++;
604 newline = strchr(logmsg, '\n');
605 if (newline)
606 *newline = '\0';
607 limit = avail - col;
608 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
609 if (err)
610 goto done;
611 waddwstr(view->window, wlogmsg);
612 col += logmsg_width;
613 while (col <= avail) {
614 waddch(view->window, ' ');
615 col++;
617 done:
618 free(logmsg0);
619 free(wlogmsg);
620 free(author0);
621 free(wauthor);
622 free(line);
623 return err;
626 static struct commit_queue_entry *
627 alloc_commit_queue_entry(struct got_commit_object *commit,
628 struct got_object_id *id)
630 struct commit_queue_entry *entry;
632 entry = calloc(1, sizeof(*entry));
633 if (entry == NULL)
634 return NULL;
636 entry->id = id;
637 entry->commit = commit;
638 return entry;
641 static void
642 pop_commit(struct commit_queue *commits)
644 struct commit_queue_entry *entry;
646 entry = TAILQ_FIRST(&commits->head);
647 TAILQ_REMOVE(&commits->head, entry, entry);
648 got_object_commit_close(entry->commit);
649 commits->ncommits--;
650 /* Don't free entry->id! It is owned by the commit graph. */
651 free(entry);
654 static void
655 free_commits(struct commit_queue *commits)
657 while (!TAILQ_EMPTY(&commits->head))
658 pop_commit(commits);
661 static const struct got_error *
662 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
663 struct got_object_id *start_id, int minqueue, int init,
664 struct got_repository *repo, const char *path)
666 const struct got_error *err = NULL;
667 struct got_object_id *id;
668 struct commit_queue_entry *entry;
669 int nfetched, nqueued = 0, found_obj = 0;
670 int is_root_path = strcmp(path, "/") == 0;
672 err = got_commit_graph_iter_start(graph, start_id);
673 if (err)
674 return err;
676 entry = TAILQ_LAST(&commits->head, commit_queue_head);
677 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
678 int nfetched;
680 /* Start ID's commit is already on the queue; skip over it. */
681 err = got_commit_graph_iter_next(&id, graph);
682 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
683 return err;
685 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
686 if (err)
687 return err;
690 while (1) {
691 struct got_commit_object *commit;
693 err = got_commit_graph_iter_next(&id, graph);
694 if (err) {
695 if (err->code != GOT_ERR_ITER_NEED_MORE)
696 break;
697 if (nqueued >= minqueue) {
698 err = NULL;
699 break;
701 err = got_commit_graph_fetch_commits(&nfetched,
702 graph, 1, repo);
703 if (err)
704 return err;
705 continue;
707 if (id == NULL)
708 break;
710 err = got_object_open_as_commit(&commit, repo, id);
711 if (err)
712 break;
714 if (!is_root_path) {
715 struct got_object *obj;
716 struct got_object_qid *pid;
717 int changed = 0;
719 err = got_object_open_by_path(&obj, repo, id, path);
720 if (err) {
721 got_object_commit_close(commit);
722 if (err->code == GOT_ERR_NO_OBJ &&
723 (found_obj || !init)) {
724 /* History stops here. */
725 err = got_error(GOT_ERR_ITER_COMPLETED);
727 break;
729 found_obj = 1;
731 pid = SIMPLEQ_FIRST(&commit->parent_ids);
732 if (pid != NULL) {
733 struct got_object *pobj;
734 err = got_object_open_by_path(&pobj, repo,
735 pid->id, path);
736 if (err) {
737 if (err->code != GOT_ERR_NO_OBJ) {
738 got_object_close(obj);
739 got_object_commit_close(commit);
740 break;
742 err = NULL;
743 changed = 1;
744 } else {
745 struct got_object_id *id, *pid;
746 id = got_object_get_id(obj);
747 if (id == NULL) {
748 err = got_error_from_errno();
749 got_object_close(obj);
750 got_object_close(pobj);
751 break;
753 pid = got_object_get_id(pobj);
754 if (pid == NULL) {
755 err = got_error_from_errno();
756 free(id);
757 got_object_close(obj);
758 got_object_close(pobj);
759 break;
761 changed =
762 (got_object_id_cmp(id, pid) != 0);
763 got_object_close(pobj);
764 free(id);
765 free(pid);
768 got_object_close(obj);
769 if (!changed) {
770 got_object_commit_close(commit);
771 continue;
775 entry = alloc_commit_queue_entry(commit, id);
776 if (entry == NULL) {
777 err = got_error_from_errno();
778 break;
780 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
781 nqueued++;
782 commits->ncommits++;
785 return err;
788 static const struct got_error *
789 fetch_next_commit(struct commit_queue_entry **pentry,
790 struct commit_queue_entry *entry, struct commit_queue *commits,
791 struct got_commit_graph *graph, struct got_repository *repo,
792 const char *path)
794 const struct got_error *err = NULL;
796 *pentry = NULL;
798 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
799 if (err)
800 return err;
802 /* Next entry to display should now be available. */
803 *pentry = TAILQ_NEXT(entry, entry);
804 if (*pentry == NULL)
805 return got_error(GOT_ERR_NO_OBJ);
807 return NULL;
810 static const struct got_error *
811 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
813 const struct got_error *err = NULL;
814 struct got_reference *head_ref;
816 *head_id = NULL;
818 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
819 if (err)
820 return err;
822 err = got_ref_resolve(head_id, repo, head_ref);
823 got_ref_close(head_ref);
824 if (err) {
825 *head_id = NULL;
826 return err;
829 return NULL;
832 static const struct got_error *
833 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
834 struct commit_queue_entry **selected, struct commit_queue_entry *first,
835 struct commit_queue *commits, int selected_idx, int limit,
836 struct got_commit_graph *graph, struct got_repository *repo,
837 const char *path)
839 const struct got_error *err = NULL;
840 struct commit_queue_entry *entry;
841 int ncommits, width;
842 char *id_str, *header;
843 wchar_t *wline;
845 entry = first;
846 ncommits = 0;
847 while (entry) {
848 if (ncommits == selected_idx) {
849 *selected = entry;
850 break;
852 entry = TAILQ_NEXT(entry, entry);
853 ncommits++;
856 err = got_object_id_str(&id_str, (*selected)->id);
857 if (err)
858 return err;
860 if (path && strcmp(path, "/") != 0) {
861 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
862 err = got_error_from_errno();
863 free(id_str);
864 return err;
866 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
867 err = got_error_from_errno();
868 free(id_str);
869 return err;
871 free(id_str);
872 err = format_line(&wline, &width, header, view->ncols);
873 if (err) {
874 free(header);
875 return err;
877 free(header);
879 werase(view->window);
881 waddwstr(view->window, wline);
882 if (width < view->ncols)
883 waddch(view->window, '\n');
884 free(wline);
885 if (limit <= 1)
886 return NULL;
888 entry = first;
889 *last = first;
890 ncommits = 0;
891 while (entry) {
892 if (ncommits >= limit - 1)
893 break;
894 if (ncommits == selected_idx)
895 wstandout(view->window);
896 err = draw_commit(view, entry->commit, entry->id);
897 if (ncommits == selected_idx)
898 wstandend(view->window);
899 if (err)
900 break;
901 ncommits++;
902 *last = entry;
903 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
904 err = queue_commits(graph, commits, entry->id, 1,
905 0, repo, path);
906 if (err) {
907 if (err->code != GOT_ERR_ITER_COMPLETED)
908 return err;
909 err = NULL;
912 entry = TAILQ_NEXT(entry, entry);
915 update_panels();
917 return err;
920 static void
921 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
922 struct commit_queue *commits)
924 struct commit_queue_entry *entry;
925 int nscrolled = 0;
927 entry = TAILQ_FIRST(&commits->head);
928 if (*first_displayed_entry == entry)
929 return;
931 entry = *first_displayed_entry;
932 while (entry && nscrolled < maxscroll) {
933 entry = TAILQ_PREV(entry, commit_queue_head, entry);
934 if (entry) {
935 *first_displayed_entry = entry;
936 nscrolled++;
941 static const struct got_error *
942 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
943 struct commit_queue_entry *last_displayed_entry,
944 struct commit_queue *commits, struct got_commit_graph *graph,
945 struct got_repository *repo, const char *path)
947 const struct got_error *err = NULL;
948 struct commit_queue_entry *pentry;
949 int nscrolled = 0;
951 do {
952 pentry = TAILQ_NEXT(last_displayed_entry, entry);
953 if (pentry == NULL) {
954 err = fetch_next_commit(&pentry, last_displayed_entry,
955 commits, graph, repo, path);
956 if (err || pentry == NULL)
957 break;
959 last_displayed_entry = pentry;
961 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
962 if (pentry == NULL)
963 break;
964 *first_displayed_entry = pentry;
965 } while (++nscrolled < maxscroll);
967 return err;
970 static const struct got_error *
971 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
972 struct commit_queue_entry *entry, struct got_repository *repo)
974 const struct got_error *err;
975 struct got_object *obj1 = NULL, *obj2 = NULL;
976 struct got_object_qid *parent_id;
977 struct tog_view *diff_view;
979 err = got_object_open(&obj2, repo, entry->id);
980 if (err)
981 return err;
983 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
984 if (parent_id) {
985 err = got_object_open(&obj1, repo, parent_id->id);
986 if (err)
987 goto done;
990 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
991 if (diff_view == NULL) {
992 err = got_error_from_errno();
993 goto done;
996 err = open_diff_view(diff_view, obj1, obj2, repo);
997 if (err == NULL)
998 *new_view = diff_view;
999 done:
1000 if (obj1)
1001 got_object_close(obj1);
1002 if (obj2)
1003 got_object_close(obj2);
1004 return err;
1007 static const struct got_error *
1008 browse_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 = NULL;
1012 struct got_tree_object *tree;
1013 struct tog_view *tree_view;
1015 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1016 if (err)
1017 return err;
1019 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1020 if (tree_view == NULL)
1021 return got_error_from_errno();
1023 err = open_tree_view(tree_view, tree, entry->id, repo);
1024 if (err)
1025 got_object_tree_close(tree);
1026 else
1027 *new_view = tree_view;
1028 return err;
1031 static const struct got_error *
1032 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1033 struct got_repository *repo, const char *path)
1035 const struct got_error *err = NULL;
1036 struct got_object_id *head_id = NULL;
1037 int nfetched;
1038 struct tog_log_view_state *s = &view->state.log;
1040 err = got_repo_map_path(&s->in_repo_path, repo, path);
1041 if (err != NULL)
1042 goto done;
1044 err = get_head_commit_id(&head_id, repo);
1045 if (err)
1046 return err;
1048 /* The graph contains all commits. */
1049 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1050 if (err)
1051 goto done;
1052 /* The commit queue contains a subset of commits filtered by path. */
1053 TAILQ_INIT(&s->commits.head);
1054 s->commits.ncommits = 0;
1056 /* Populate commit graph with a sufficient number of commits. */
1057 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1058 start_id, repo);
1059 if (err)
1060 goto done;
1063 * Open the initial batch of commits, sorted in commit graph order.
1064 * We keep all commits open throughout the lifetime of the log view
1065 * in order to avoid having to re-fetch commits from disk while
1066 * updating the display.
1068 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1069 repo, s->in_repo_path);
1070 if (err) {
1071 if (err->code != GOT_ERR_ITER_COMPLETED)
1072 goto done;
1073 err = NULL;
1076 s->first_displayed_entry =
1077 TAILQ_FIRST(&s->commits.head);
1078 s->selected_entry = s->first_displayed_entry;
1079 s->repo = repo;
1081 view->show = show_log_view;
1082 view->input = input_log_view;
1083 view->close = close_log_view;
1084 done:
1085 free(head_id);
1086 return err;
1089 static const struct got_error *
1090 close_log_view(struct tog_view *view)
1092 struct tog_log_view_state *s = &view->state.log;
1094 if (s->graph)
1095 got_commit_graph_close(s->graph);
1096 free_commits(&s->commits);
1097 free(s->in_repo_path);
1098 return NULL;
1101 static const struct got_error *
1102 show_log_view(struct tog_view *view)
1104 struct tog_log_view_state *s = &view->state.log;
1106 return draw_commits(view, &s->last_displayed_entry,
1107 &s->selected_entry, s->first_displayed_entry,
1108 &s->commits, s->selected, view->nlines, s->graph,
1109 s->repo, s->in_repo_path);
1112 static const struct got_error *
1113 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1114 struct tog_view *view, int ch)
1116 const struct got_error *err = NULL;
1117 struct tog_log_view_state *s = &view->state.log;
1119 switch (ch) {
1120 case 'k':
1121 case KEY_UP:
1122 if (s->selected > 0)
1123 s->selected--;
1124 if (s->selected > 0)
1125 break;
1126 scroll_up(&s->first_displayed_entry, 1,
1127 &s->commits);
1128 break;
1129 case KEY_PPAGE:
1130 if (TAILQ_FIRST(&s->commits.head) ==
1131 s->first_displayed_entry) {
1132 s->selected = 0;
1133 break;
1135 scroll_up(&s->first_displayed_entry,
1136 view->nlines, &s->commits);
1137 break;
1138 case 'j':
1139 case KEY_DOWN:
1140 if (s->selected < MIN(view->nlines - 2,
1141 s->commits.ncommits - 1)) {
1142 s->selected++;
1143 break;
1145 err = scroll_down(&s->first_displayed_entry, 1,
1146 s->last_displayed_entry, &s->commits,
1147 s->graph, s->repo, s->in_repo_path);
1148 if (err) {
1149 if (err->code != GOT_ERR_ITER_COMPLETED)
1150 break;
1151 err = NULL;
1153 break;
1154 case KEY_NPAGE: {
1155 struct commit_queue_entry *first;
1156 first = s->first_displayed_entry;
1157 err = scroll_down(&s->first_displayed_entry,
1158 view->nlines, s->last_displayed_entry,
1159 &s->commits, s->graph, s->repo,
1160 s->in_repo_path);
1161 if (err == NULL)
1162 break;
1163 if (err->code != GOT_ERR_ITER_COMPLETED)
1164 break;
1165 if (first == s->first_displayed_entry &&
1166 s->selected < MIN(view->nlines - 2,
1167 s->commits.ncommits - 1)) {
1168 /* can't scroll further down */
1169 s->selected = MIN(view->nlines - 2,
1170 s->commits.ncommits - 1);
1172 err = NULL;
1173 break;
1175 case KEY_RESIZE:
1176 if (s->selected > view->nlines - 2)
1177 s->selected = view->nlines - 2;
1178 if (s->selected > s->commits.ncommits - 1)
1179 s->selected = s->commits.ncommits - 1;
1180 break;
1181 case KEY_ENTER:
1182 case '\r':
1183 err = show_commit(new_view, view, s->selected_entry,
1184 s->repo);
1185 break;
1186 case 't':
1187 err = browse_commit(new_view, view, s->selected_entry,
1188 s->repo);
1189 break;
1190 default:
1191 break;
1194 return err;
1197 static const struct got_error *
1198 cmd_log(int argc, char *argv[])
1200 const struct got_error *error;
1201 struct got_repository *repo = NULL;
1202 struct got_object_id *start_id = NULL;
1203 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1204 char *start_commit = NULL;
1205 int ch;
1206 struct tog_view *view;
1208 #ifndef PROFILE
1209 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1210 err(1, "pledge");
1211 #endif
1213 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1214 switch (ch) {
1215 case 'c':
1216 start_commit = optarg;
1217 break;
1218 case 'r':
1219 repo_path = realpath(optarg, NULL);
1220 if (repo_path == NULL)
1221 err(1, "-r option");
1222 break;
1223 default:
1224 usage();
1225 /* NOTREACHED */
1229 argc -= optind;
1230 argv += optind;
1232 if (argc == 0)
1233 path = strdup("");
1234 else if (argc == 1)
1235 path = strdup(argv[0]);
1236 else
1237 usage_log();
1238 if (path == NULL)
1239 return got_error_from_errno();
1241 cwd = getcwd(NULL, 0);
1242 if (cwd == NULL) {
1243 error = got_error_from_errno();
1244 goto done;
1246 if (repo_path == NULL) {
1247 repo_path = strdup(cwd);
1248 if (repo_path == NULL) {
1249 error = got_error_from_errno();
1250 goto done;
1254 error = got_repo_open(&repo, repo_path);
1255 if (error != NULL)
1256 goto done;
1258 if (start_commit == NULL) {
1259 error = get_head_commit_id(&start_id, repo);
1260 if (error != NULL)
1261 goto done;
1262 } else {
1263 struct got_object *obj;
1264 error = got_object_open_by_id_str(&obj, repo, start_commit);
1265 if (error == NULL) {
1266 start_id = got_object_get_id(obj);
1267 if (start_id == NULL)
1268 error = got_error_from_errno();
1269 goto done;
1272 if (error != NULL)
1273 goto done;
1275 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1276 if (view == NULL) {
1277 error = got_error_from_errno();
1278 goto done;
1280 error = open_log_view(view, start_id, repo, path);
1281 if (error)
1282 goto done;
1283 error = view_loop(view);
1284 done:
1285 free(repo_path);
1286 free(cwd);
1287 free(path);
1288 free(start_id);
1289 if (repo)
1290 got_repo_close(repo);
1291 return error;
1294 __dead static void
1295 usage_diff(void)
1297 endwin();
1298 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1299 getprogname());
1300 exit(1);
1303 static char *
1304 parse_next_line(FILE *f, size_t *len)
1306 char *line;
1307 size_t linelen;
1308 size_t lineno;
1309 const char delim[3] = { '\0', '\0', '\0'};
1311 line = fparseln(f, &linelen, &lineno, delim, 0);
1312 if (len)
1313 *len = linelen;
1314 return line;
1317 static const struct got_error *
1318 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1319 int *last_displayed_line, int *eof, int max_lines)
1321 const struct got_error *err;
1322 int nlines = 0, nprinted = 0;
1323 char *line;
1324 size_t len;
1325 wchar_t *wline;
1326 int width;
1328 rewind(f);
1329 werase(view->window);
1331 *eof = 0;
1332 while (nprinted < max_lines) {
1333 line = parse_next_line(f, &len);
1334 if (line == NULL) {
1335 *eof = 1;
1336 break;
1338 if (++nlines < *first_displayed_line) {
1339 free(line);
1340 continue;
1343 err = format_line(&wline, &width, line, view->ncols);
1344 if (err) {
1345 free(line);
1346 free(wline);
1347 return err;
1349 waddwstr(view->window, wline);
1350 if (width < view->ncols)
1351 waddch(view->window, '\n');
1352 if (++nprinted == 1)
1353 *first_displayed_line = nlines;
1354 free(line);
1355 free(wline);
1356 wline = NULL;
1358 *last_displayed_line = nlines;
1360 update_panels();
1362 return NULL;
1365 static const struct got_error *
1366 open_diff_view(struct tog_view *view, struct got_object *obj1,
1367 struct got_object *obj2, struct got_repository *repo)
1369 const struct got_error *err;
1370 FILE *f;
1372 if (obj1 != NULL && obj2 != NULL &&
1373 got_object_get_type(obj1) != got_object_get_type(obj2))
1374 return got_error(GOT_ERR_OBJ_TYPE);
1376 f = got_opentemp();
1377 if (f == NULL)
1378 return got_error_from_errno();
1380 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1381 case GOT_OBJ_TYPE_BLOB:
1382 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1383 break;
1384 case GOT_OBJ_TYPE_TREE:
1385 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1386 break;
1387 case GOT_OBJ_TYPE_COMMIT:
1388 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1389 break;
1390 default:
1391 return got_error(GOT_ERR_OBJ_TYPE);
1394 fflush(f);
1396 view->state.diff.f = f;
1397 view->state.diff.first_displayed_line = 1;
1398 view->state.diff.last_displayed_line = view->nlines;
1400 view->show = show_diff_view;
1401 view->input = input_diff_view;
1402 view->close = close_diff_view;
1404 return NULL;
1407 static const struct got_error *
1408 close_diff_view(struct tog_view *view)
1410 const struct got_error *err = NULL;
1412 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1413 err = got_error_from_errno();
1415 return err;
1418 static const struct got_error *
1419 show_diff_view(struct tog_view *view)
1421 struct tog_diff_view_state *s = &view->state.diff;
1423 return draw_file(view, s->f, &s->first_displayed_line,
1424 &s->last_displayed_line, &s->eof, view->nlines);
1427 static const struct got_error *
1428 input_diff_view(struct tog_view **new, struct tog_view **dead,
1429 struct tog_view *view, int ch)
1431 struct tog_diff_view_state *s = &view->state.diff;
1432 int i;
1434 switch (ch) {
1435 case 'k':
1436 case KEY_UP:
1437 if (s->first_displayed_line > 1)
1438 s->first_displayed_line--;
1439 break;
1440 case KEY_PPAGE:
1441 case KEY_BACKSPACE:
1442 i = 0;
1443 while (i++ < view->nlines - 1 &&
1444 s->first_displayed_line > 1)
1445 s->first_displayed_line--;
1446 break;
1447 case 'j':
1448 case KEY_DOWN:
1449 if (!s->eof)
1450 s->first_displayed_line++;
1451 break;
1452 case KEY_NPAGE:
1453 case ' ':
1454 i = 0;
1455 while (!s->eof && i++ < view->nlines - 1) {
1456 char *line;
1457 line = parse_next_line(s->f, NULL);
1458 s->first_displayed_line++;
1459 if (line == NULL)
1460 break;
1462 break;
1463 default:
1464 break;
1467 return NULL;
1470 static const struct got_error *
1471 cmd_diff(int argc, char *argv[])
1473 const struct got_error *error = NULL;
1474 struct got_repository *repo = NULL;
1475 struct got_object *obj1 = NULL, *obj2 = NULL;
1476 char *repo_path = NULL;
1477 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1478 int ch;
1479 struct tog_view *view;
1481 #ifndef PROFILE
1482 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1483 err(1, "pledge");
1484 #endif
1486 while ((ch = getopt(argc, argv, "")) != -1) {
1487 switch (ch) {
1488 default:
1489 usage();
1490 /* NOTREACHED */
1494 argc -= optind;
1495 argv += optind;
1497 if (argc == 0) {
1498 usage_diff(); /* TODO show local worktree changes */
1499 } else if (argc == 2) {
1500 repo_path = getcwd(NULL, 0);
1501 if (repo_path == NULL)
1502 return got_error_from_errno();
1503 obj_id_str1 = argv[0];
1504 obj_id_str2 = argv[1];
1505 } else if (argc == 3) {
1506 repo_path = realpath(argv[0], NULL);
1507 if (repo_path == NULL)
1508 return got_error_from_errno();
1509 obj_id_str1 = argv[1];
1510 obj_id_str2 = argv[2];
1511 } else
1512 usage_diff();
1514 error = got_repo_open(&repo, repo_path);
1515 free(repo_path);
1516 if (error)
1517 goto done;
1519 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1520 if (error)
1521 goto done;
1523 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1524 if (error)
1525 goto done;
1527 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1528 if (view == NULL) {
1529 error = got_error_from_errno();
1530 goto done;
1532 error = open_diff_view(view, obj1, obj2, repo);
1533 if (error)
1534 goto done;
1535 error = view_loop(view);
1536 done:
1537 got_repo_close(repo);
1538 if (obj1)
1539 got_object_close(obj1);
1540 if (obj2)
1541 got_object_close(obj2);
1542 return error;
1545 __dead static void
1546 usage_blame(void)
1548 endwin();
1549 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1550 getprogname());
1551 exit(1);
1554 struct tog_blame_line {
1555 int annotated;
1556 struct got_object_id *id;
1559 static const struct got_error *
1560 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1561 const char *path, struct tog_blame_line *lines, int nlines,
1562 int blame_complete, int selected_line, int *first_displayed_line,
1563 int *last_displayed_line, int *eof, int max_lines)
1565 const struct got_error *err;
1566 int lineno = 0, nprinted = 0;
1567 char *line;
1568 size_t len;
1569 wchar_t *wline;
1570 int width, wlimit;
1571 struct tog_blame_line *blame_line;
1572 struct got_object_id *prev_id = NULL;
1573 char *id_str;
1575 err = got_object_id_str(&id_str, id);
1576 if (err)
1577 return err;
1579 rewind(f);
1580 werase(view->window);
1582 if (asprintf(&line, "commit: %s", id_str) == -1) {
1583 err = got_error_from_errno();
1584 free(id_str);
1585 return err;
1588 err = format_line(&wline, &width, line, view->ncols);
1589 free(line);
1590 line = NULL;
1591 waddwstr(view->window, wline);
1592 free(wline);
1593 wline = NULL;
1594 if (width < view->ncols)
1595 waddch(view->window, '\n');
1597 if (asprintf(&line, "[%d/%d] %s%s",
1598 *first_displayed_line - 1 + selected_line, nlines,
1599 blame_complete ? "" : "annotating ", path) == -1) {
1600 free(id_str);
1601 return got_error_from_errno();
1603 free(id_str);
1604 err = format_line(&wline, &width, line, view->ncols);
1605 free(line);
1606 line = NULL;
1607 if (err)
1608 return err;
1609 waddwstr(view->window, wline);
1610 free(wline);
1611 wline = NULL;
1612 if (width < view->ncols)
1613 waddch(view->window, '\n');
1615 *eof = 0;
1616 while (nprinted < max_lines - 2) {
1617 line = parse_next_line(f, &len);
1618 if (line == NULL) {
1619 *eof = 1;
1620 break;
1622 if (++lineno < *first_displayed_line) {
1623 free(line);
1624 continue;
1627 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1628 err = format_line(&wline, &width, line, wlimit);
1629 if (err) {
1630 free(line);
1631 return err;
1634 if (nprinted == selected_line - 1)
1635 wstandout(view->window);
1637 blame_line = &lines[lineno - 1];
1638 if (blame_line->annotated && prev_id &&
1639 got_object_id_cmp(prev_id, blame_line->id) == 0)
1640 waddstr(view->window, " ");
1641 else if (blame_line->annotated) {
1642 char *id_str;
1643 err = got_object_id_str(&id_str, blame_line->id);
1644 if (err) {
1645 free(line);
1646 free(wline);
1647 return err;
1649 wprintw(view->window, "%.8s ", id_str);
1650 free(id_str);
1651 prev_id = blame_line->id;
1652 } else {
1653 waddstr(view->window, "........ ");
1654 prev_id = NULL;
1657 waddwstr(view->window, wline);
1658 while (width < wlimit) {
1659 waddch(view->window, ' ');
1660 width++;
1662 if (nprinted == selected_line - 1)
1663 wstandend(view->window);
1664 if (++nprinted == 1)
1665 *first_displayed_line = lineno;
1666 free(line);
1667 free(wline);
1668 wline = NULL;
1670 *last_displayed_line = lineno;
1672 update_panels();
1674 return NULL;
1677 static const struct got_error *
1678 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1680 const struct got_error *err = NULL;
1681 struct tog_blame_cb_args *a = arg;
1682 struct tog_blame_line *line;
1684 if (nlines != a->nlines ||
1685 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1686 return got_error(GOT_ERR_RANGE);
1688 if (pthread_mutex_lock(a->mutex) != 0)
1689 return got_error_from_errno();
1691 if (*a->quit) { /* user has quit the blame view */
1692 err = got_error(GOT_ERR_ITER_COMPLETED);
1693 goto done;
1696 if (lineno == -1)
1697 goto done; /* no change in this commit */
1699 line = &a->lines[lineno - 1];
1700 if (line->annotated)
1701 goto done;
1703 line->id = got_object_id_dup(id);
1704 if (line->id == NULL) {
1705 err = got_error_from_errno();
1706 goto done;
1708 line->annotated = 1;
1710 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1711 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1712 a->last_displayed_line, a->eof, a->view->nlines);
1713 done:
1714 if (pthread_mutex_unlock(a->mutex) != 0)
1715 return got_error_from_errno();
1716 return err;
1719 static void *
1720 blame_thread(void *arg)
1722 const struct got_error *err;
1723 struct tog_blame_thread_args *ta = arg;
1724 struct tog_blame_cb_args *a = ta->cb_args;
1726 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1727 blame_cb, ta->cb_args);
1729 if (pthread_mutex_lock(a->mutex) != 0)
1730 return (void *)got_error_from_errno();
1732 got_repo_close(ta->repo);
1733 ta->repo = NULL;
1734 *ta->complete = 1;
1735 if (!err)
1736 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1737 a->lines, a->nlines, 1, *a->selected_line,
1738 a->first_displayed_line, a->last_displayed_line, a->eof,
1739 a->view->nlines);
1741 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1742 err = got_error_from_errno();
1744 return (void *)err;
1747 static struct got_object_id *
1748 get_selected_commit_id(struct tog_blame_line *lines,
1749 int first_displayed_line, int selected_line)
1751 struct tog_blame_line *line;
1753 line = &lines[first_displayed_line - 1 + selected_line - 1];
1754 if (!line->annotated)
1755 return NULL;
1757 return line->id;
1760 static const struct got_error *
1761 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1762 struct tog_blame_line *lines, int first_displayed_line,
1763 int selected_line, struct got_repository *repo)
1765 const struct got_error *err = NULL;
1766 struct got_commit_object *commit = NULL;
1767 struct got_object_id *selected_id;
1768 struct got_object_qid *pid;
1770 *pobj = NULL;
1771 *obj = NULL;
1773 selected_id = get_selected_commit_id(lines,
1774 first_displayed_line, selected_line);
1775 if (selected_id == NULL)
1776 return NULL;
1778 err = got_object_open(obj, repo, selected_id);
1779 if (err)
1780 goto done;
1782 err = got_object_commit_open(&commit, repo, *obj);
1783 if (err)
1784 goto done;
1786 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1787 if (pid) {
1788 err = got_object_open(pobj, repo, pid->id);
1789 if (err)
1790 goto done;
1792 done:
1793 if (commit)
1794 got_object_commit_close(commit);
1795 return err;
1798 static const struct got_error *
1799 stop_blame(struct tog_blame *blame)
1801 const struct got_error *err = NULL;
1802 int i;
1804 if (blame->thread) {
1805 if (pthread_join(blame->thread, (void **)&err) != 0)
1806 err = got_error_from_errno();
1807 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1808 err = NULL;
1809 blame->thread = NULL;
1811 if (blame->thread_args.repo) {
1812 got_repo_close(blame->thread_args.repo);
1813 blame->thread_args.repo = NULL;
1815 if (blame->f) {
1816 fclose(blame->f);
1817 blame->f = NULL;
1819 for (i = 0; i < blame->nlines; i++)
1820 free(blame->lines[i].id);
1821 free(blame->lines);
1822 blame->lines = NULL;
1823 free(blame->cb_args.commit_id);
1824 blame->cb_args.commit_id = NULL;
1826 return err;
1829 static const struct got_error *
1830 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1831 struct tog_view *view, int *blame_complete,
1832 int *first_displayed_line, int *last_displayed_line,
1833 int *selected_line, int *done, int *eof, const char *path,
1834 struct got_object_id *commit_id,
1835 struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_blob_object *blob = NULL;
1839 struct got_repository *thread_repo = NULL;
1840 struct got_object *obj;
1842 err = got_object_open_by_path(&obj, repo, commit_id, path);
1843 if (err)
1844 goto done;
1845 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1846 err = got_error(GOT_ERR_OBJ_TYPE);
1847 goto done;
1850 err = got_object_blob_open(&blob, repo, obj, 8192);
1851 if (err)
1852 goto done;
1853 blame->f = got_opentemp();
1854 if (blame->f == NULL) {
1855 err = got_error_from_errno();
1856 goto done;
1858 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1859 blame->f, blob);
1860 if (err)
1861 goto done;
1863 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1864 if (blame->lines == NULL) {
1865 err = got_error_from_errno();
1866 goto done;
1869 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1870 if (err)
1871 goto done;
1873 blame->cb_args.view = view;
1874 blame->cb_args.lines = blame->lines;
1875 blame->cb_args.nlines = blame->nlines;
1876 blame->cb_args.mutex = mutex;
1877 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1878 if (blame->cb_args.commit_id == NULL) {
1879 err = got_error_from_errno();
1880 goto done;
1882 blame->cb_args.f = blame->f;
1883 blame->cb_args.path = path;
1884 blame->cb_args.first_displayed_line = first_displayed_line;
1885 blame->cb_args.selected_line = selected_line;
1886 blame->cb_args.last_displayed_line = last_displayed_line;
1887 blame->cb_args.quit = done;
1888 blame->cb_args.eof = eof;
1890 blame->thread_args.path = path;
1891 blame->thread_args.repo = thread_repo;
1892 blame->thread_args.cb_args = &blame->cb_args;
1893 blame->thread_args.complete = blame_complete;
1894 *blame_complete = 0;
1896 if (pthread_create(&blame->thread, NULL, blame_thread,
1897 &blame->thread_args) != 0) {
1898 err = got_error_from_errno();
1899 goto done;
1902 done:
1903 if (blob)
1904 got_object_blob_close(blob);
1905 if (obj)
1906 got_object_close(obj);
1907 if (err)
1908 stop_blame(blame);
1909 return err;
1912 static const struct got_error *
1913 open_blame_view(struct tog_view *view, char *path,
1914 struct got_object_id *commit_id, struct got_repository *repo)
1916 const struct got_error *err = NULL;
1917 struct tog_blame_view_state *s = &view->state.blame;
1919 SIMPLEQ_INIT(&s->blamed_commits);
1921 if (pthread_mutex_init(&s->mutex, NULL) != 0)
1922 return got_error_from_errno();
1924 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1925 if (err)
1926 return err;
1928 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1929 s->first_displayed_line = 1;
1930 s->last_displayed_line = view->nlines;
1931 s->selected_line = 1;
1932 s->blame_complete = 0;
1933 s->path = path;
1934 if (s->path == NULL)
1935 return got_error_from_errno();
1936 s->repo = repo;
1937 s->commit_id = commit_id;
1938 memset(&s->blame, 0, sizeof(s->blame));
1940 view->show = show_blame_view;
1941 view->input = input_blame_view;
1942 view->close = close_blame_view;
1944 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1945 &s->first_displayed_line, &s->last_displayed_line,
1946 &s->selected_line, &s->done, &s->eof, s->path,
1947 s->blamed_commit->id, s->repo);
1950 static const struct got_error *
1951 close_blame_view(struct tog_view *view)
1953 const struct got_error *err = NULL;
1954 struct tog_blame_view_state *s = &view->state.blame;
1956 if (s->blame.thread)
1957 err = stop_blame(&s->blame);
1959 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1960 struct got_object_qid *blamed_commit;
1961 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1962 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1963 got_object_qid_free(blamed_commit);
1966 free(s->path);
1968 return err;
1971 static const struct got_error *
1972 show_blame_view(struct tog_view *view)
1974 const struct got_error *err = NULL;
1975 struct tog_blame_view_state *s = &view->state.blame;
1977 if (pthread_mutex_lock(&s->mutex) != 0)
1978 return got_error_from_errno();
1980 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1981 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1982 s->selected_line, &s->first_displayed_line,
1983 &s->last_displayed_line, &s->eof, view->nlines);
1985 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
1986 err = got_error_from_errno();
1988 return err;
1991 static const struct got_error *
1992 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
1993 struct tog_view *view, int ch)
1995 const struct got_error *err = NULL, *thread_err = NULL;
1996 struct got_object *obj = NULL, *pobj = NULL;
1997 struct tog_view *diff_view;
1998 struct tog_blame_view_state *s = &view->state.blame;
2000 if (pthread_mutex_lock(&s->mutex) != 0) {
2001 err = got_error_from_errno();
2002 goto done;
2005 switch (ch) {
2006 case 'q':
2007 s->done = 1;
2008 if (pthread_mutex_unlock(&s->mutex) != 0) {
2009 err = got_error_from_errno();
2010 goto done;
2012 return stop_blame(&s->blame);
2013 case 'k':
2014 case KEY_UP:
2015 if (s->selected_line > 1)
2016 s->selected_line--;
2017 else if (s->selected_line == 1 &&
2018 s->first_displayed_line > 1)
2019 s->first_displayed_line--;
2020 break;
2021 case KEY_PPAGE:
2022 case KEY_BACKSPACE:
2023 if (s->first_displayed_line == 1) {
2024 s->selected_line = 1;
2025 break;
2027 if (s->first_displayed_line > view->nlines - 2)
2028 s->first_displayed_line -=
2029 (view->nlines - 2);
2030 else
2031 s->first_displayed_line = 1;
2032 break;
2033 case 'j':
2034 case KEY_DOWN:
2035 if (s->selected_line < view->nlines - 2 &&
2036 s->first_displayed_line +
2037 s->selected_line <= s->blame.nlines)
2038 s->selected_line++;
2039 else if (s->last_displayed_line <
2040 s->blame.nlines)
2041 s->first_displayed_line++;
2042 break;
2043 case 'b':
2044 case 'p': {
2045 struct got_object_id *id;
2046 id = get_selected_commit_id(s->blame.lines,
2047 s->first_displayed_line, s->selected_line);
2048 if (id == NULL || got_object_id_cmp(id,
2049 s->blamed_commit->id) == 0)
2050 break;
2051 err = open_selected_commit(&pobj, &obj,
2052 s->blame.lines, s->first_displayed_line,
2053 s->selected_line, s->repo);
2054 if (err)
2055 break;
2056 if (pobj == NULL && obj == NULL)
2057 break;
2058 if (ch == 'p' && pobj == NULL)
2059 break;
2060 s->done = 1;
2061 if (pthread_mutex_unlock(&s->mutex) != 0) {
2062 err = got_error_from_errno();
2063 goto done;
2065 thread_err = stop_blame(&s->blame);
2066 s->done = 0;
2067 if (pthread_mutex_lock(&s->mutex) != 0) {
2068 err = got_error_from_errno();
2069 goto done;
2071 if (thread_err)
2072 break;
2073 id = got_object_get_id(ch == 'b' ? obj : pobj);
2074 got_object_close(obj);
2075 obj = NULL;
2076 if (pobj) {
2077 got_object_close(pobj);
2078 pobj = NULL;
2080 if (id == NULL) {
2081 err = got_error_from_errno();
2082 break;
2084 err = got_object_qid_alloc(
2085 &s->blamed_commit, id);
2086 free(id);
2087 if (err)
2088 goto done;
2089 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2090 s->blamed_commit, entry);
2091 err = run_blame(&s->blame, &s->mutex, view,
2092 &s->blame_complete,
2093 &s->first_displayed_line,
2094 &s->last_displayed_line,
2095 &s->selected_line, &s->done, &s->eof,
2096 s->path, s->blamed_commit->id, s->repo);
2097 if (err)
2098 break;
2099 break;
2101 case 'B': {
2102 struct got_object_qid *first;
2103 first = SIMPLEQ_FIRST(&s->blamed_commits);
2104 if (!got_object_id_cmp(first->id, s->commit_id))
2105 break;
2106 s->done = 1;
2107 if (pthread_mutex_unlock(&s->mutex) != 0) {
2108 err = got_error_from_errno();
2109 goto done;
2111 thread_err = stop_blame(&s->blame);
2112 s->done = 0;
2113 if (pthread_mutex_lock(&s->mutex) != 0) {
2114 err = got_error_from_errno();
2115 goto done;
2117 if (thread_err)
2118 break;
2119 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2120 got_object_qid_free(s->blamed_commit);
2121 s->blamed_commit =
2122 SIMPLEQ_FIRST(&s->blamed_commits);
2123 err = run_blame(&s->blame, &s->mutex, view,
2124 &s->blame_complete,
2125 &s->first_displayed_line,
2126 &s->last_displayed_line,
2127 &s->selected_line, &s->done, &s->eof, s->path,
2128 s->blamed_commit->id, s->repo);
2129 if (err)
2130 break;
2131 break;
2133 case KEY_ENTER:
2134 case '\r':
2135 err = open_selected_commit(&pobj, &obj,
2136 s->blame.lines, s->first_displayed_line,
2137 s->selected_line, s->repo);
2138 if (err)
2139 break;
2140 if (pobj == NULL && obj == NULL)
2141 break;
2142 diff_view = view_open(0, 0, 0, 0, view,
2143 TOG_VIEW_DIFF);
2144 if (diff_view == NULL) {
2145 err = got_error_from_errno();
2146 break;
2148 err = open_diff_view(diff_view, pobj, obj,
2149 s->repo);
2150 if (err) {
2151 view_close(diff_view);
2152 break;
2154 *new_view = diff_view;
2155 if (pobj) {
2156 got_object_close(pobj);
2157 pobj = NULL;
2159 got_object_close(obj);
2160 obj = NULL;
2161 if (err)
2162 break;
2163 break;
2164 case KEY_NPAGE:
2165 case ' ':
2166 if (s->last_displayed_line >= s->blame.nlines &&
2167 s->selected_line < view->nlines - 2) {
2168 s->selected_line = MIN(s->blame.nlines,
2169 view->nlines - 2);
2170 break;
2172 if (s->last_displayed_line + view->nlines - 2
2173 <= s->blame.nlines)
2174 s->first_displayed_line +=
2175 view->nlines - 2;
2176 else
2177 s->first_displayed_line =
2178 s->blame.nlines -
2179 (view->nlines - 3);
2180 break;
2181 case KEY_RESIZE:
2182 if (s->selected_line > view->nlines - 2) {
2183 s->selected_line = MIN(s->blame.nlines,
2184 view->nlines - 2);
2186 break;
2187 default:
2188 break;
2191 if (pthread_mutex_unlock(&s->mutex) != 0)
2192 err = got_error_from_errno();
2193 done:
2194 if (pobj)
2195 got_object_close(pobj);
2196 return thread_err ? thread_err : err;
2199 static const struct got_error *
2200 cmd_blame(int argc, char *argv[])
2202 const struct got_error *error;
2203 struct got_repository *repo = NULL;
2204 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2205 struct got_object_id *commit_id = NULL;
2206 char *commit_id_str = NULL;
2207 int ch;
2208 struct tog_view *view;
2210 #ifndef PROFILE
2211 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2212 err(1, "pledge");
2213 #endif
2215 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2216 switch (ch) {
2217 case 'c':
2218 commit_id_str = optarg;
2219 break;
2220 case 'r':
2221 repo_path = realpath(optarg, NULL);
2222 if (repo_path == NULL)
2223 err(1, "-r option");
2224 break;
2225 default:
2226 usage();
2227 /* NOTREACHED */
2231 argc -= optind;
2232 argv += optind;
2234 if (argc == 1)
2235 path = argv[0];
2236 else
2237 usage_blame();
2239 cwd = getcwd(NULL, 0);
2240 if (cwd == NULL) {
2241 error = got_error_from_errno();
2242 goto done;
2244 if (repo_path == NULL) {
2245 repo_path = strdup(cwd);
2246 if (repo_path == NULL) {
2247 error = got_error_from_errno();
2248 goto done;
2253 error = got_repo_open(&repo, repo_path);
2254 if (error != NULL)
2255 return error;
2257 error = got_repo_map_path(&in_repo_path, repo, path);
2258 if (error != NULL)
2259 goto done;
2261 if (commit_id_str == NULL) {
2262 struct got_reference *head_ref;
2263 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2264 if (error != NULL)
2265 goto done;
2266 error = got_ref_resolve(&commit_id, repo, head_ref);
2267 got_ref_close(head_ref);
2268 } else {
2269 struct got_object *obj;
2270 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2271 if (error != NULL)
2272 goto done;
2273 commit_id = got_object_get_id(obj);
2274 if (commit_id == NULL)
2275 error = got_error_from_errno();
2276 got_object_close(obj);
2278 if (error != NULL)
2279 goto done;
2281 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2282 if (view == NULL) {
2283 error = got_error_from_errno();
2284 goto done;
2286 error = open_blame_view(view, in_repo_path, commit_id, repo);
2287 if (error)
2288 goto done;
2289 error = view_loop(view);
2290 done:
2291 free(repo_path);
2292 free(cwd);
2293 free(commit_id);
2294 if (repo)
2295 got_repo_close(repo);
2296 return error;
2299 static const struct got_error *
2300 draw_tree_entries(struct tog_view *view,
2301 struct got_tree_entry **first_displayed_entry,
2302 struct got_tree_entry **last_displayed_entry,
2303 struct got_tree_entry **selected_entry, int *ndisplayed,
2304 const char *label, int show_ids, const char *parent_path,
2305 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2307 const struct got_error *err = NULL;
2308 struct got_tree_entry *te;
2309 wchar_t *wline;
2310 int width, n;
2312 *ndisplayed = 0;
2314 werase(view->window);
2316 if (limit == 0)
2317 return NULL;
2319 err = format_line(&wline, &width, label, view->ncols);
2320 if (err)
2321 return err;
2322 waddwstr(view->window, wline);
2323 free(wline);
2324 wline = NULL;
2325 if (width < view->ncols)
2326 waddch(view->window, '\n');
2327 if (--limit <= 0)
2328 return NULL;
2329 err = format_line(&wline, &width, parent_path, view->ncols);
2330 if (err)
2331 return err;
2332 waddwstr(view->window, wline);
2333 free(wline);
2334 wline = NULL;
2335 if (width < view->ncols)
2336 waddch(view->window, '\n');
2337 if (--limit <= 0)
2338 return NULL;
2339 waddch(view->window, '\n');
2340 if (--limit <= 0)
2341 return NULL;
2343 te = SIMPLEQ_FIRST(&entries->head);
2344 if (*first_displayed_entry == NULL) {
2345 if (selected == 0) {
2346 wstandout(view->window);
2347 *selected_entry = NULL;
2349 waddstr(view->window, " ..\n"); /* parent directory */
2350 if (selected == 0)
2351 wstandend(view->window);
2352 (*ndisplayed)++;
2353 if (--limit <= 0)
2354 return NULL;
2355 n = 1;
2356 } else {
2357 n = 0;
2358 while (te != *first_displayed_entry)
2359 te = SIMPLEQ_NEXT(te, entry);
2362 while (te) {
2363 char *line = NULL, *id_str = NULL;
2365 if (show_ids) {
2366 err = got_object_id_str(&id_str, te->id);
2367 if (err)
2368 return got_error_from_errno();
2370 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2371 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2372 free(id_str);
2373 return got_error_from_errno();
2375 free(id_str);
2376 err = format_line(&wline, &width, line, view->ncols);
2377 if (err) {
2378 free(line);
2379 break;
2381 if (n == selected) {
2382 wstandout(view->window);
2383 *selected_entry = te;
2385 waddwstr(view->window, wline);
2386 if (width < view->ncols)
2387 waddch(view->window, '\n');
2388 if (n == selected)
2389 wstandend(view->window);
2390 free(line);
2391 free(wline);
2392 wline = NULL;
2393 n++;
2394 (*ndisplayed)++;
2395 *last_displayed_entry = te;
2396 if (--limit <= 0)
2397 break;
2398 te = SIMPLEQ_NEXT(te, entry);
2401 return err;
2404 static void
2405 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2406 const struct got_tree_entries *entries, int isroot)
2408 struct got_tree_entry *te, *prev;
2409 int i;
2411 if (*first_displayed_entry == NULL)
2412 return;
2414 te = SIMPLEQ_FIRST(&entries->head);
2415 if (*first_displayed_entry == te) {
2416 if (!isroot)
2417 *first_displayed_entry = NULL;
2418 return;
2421 /* XXX this is stupid... switch to TAILQ? */
2422 for (i = 0; i < maxscroll; i++) {
2423 while (te != *first_displayed_entry) {
2424 prev = te;
2425 te = SIMPLEQ_NEXT(te, entry);
2427 *first_displayed_entry = prev;
2428 te = SIMPLEQ_FIRST(&entries->head);
2430 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2431 *first_displayed_entry = NULL;
2434 static void
2435 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2436 struct got_tree_entry *last_displayed_entry,
2437 const struct got_tree_entries *entries)
2439 struct got_tree_entry *next;
2440 int n = 0;
2442 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2443 return;
2445 if (*first_displayed_entry)
2446 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2447 else
2448 next = SIMPLEQ_FIRST(&entries->head);
2449 while (next) {
2450 *first_displayed_entry = next;
2451 if (++n >= maxscroll)
2452 break;
2453 next = SIMPLEQ_NEXT(next, entry);
2457 static const struct got_error *
2458 tree_entry_path(char **path, struct tog_parent_trees *parents,
2459 struct got_tree_entry *te)
2461 const struct got_error *err = NULL;
2462 struct tog_parent_tree *pt;
2463 size_t len = 2; /* for leading slash and NUL */
2465 TAILQ_FOREACH(pt, parents, entry)
2466 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2467 if (te)
2468 len += strlen(te->name);
2470 *path = calloc(1, len);
2471 if (path == NULL)
2472 return got_error_from_errno();
2474 (*path)[0] = '/';
2475 pt = TAILQ_LAST(parents, tog_parent_trees);
2476 while (pt) {
2477 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2478 err = got_error(GOT_ERR_NO_SPACE);
2479 goto done;
2481 if (strlcat(*path, "/", len) >= len) {
2482 err = got_error(GOT_ERR_NO_SPACE);
2483 goto done;
2485 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2487 if (te) {
2488 if (strlcat(*path, te->name, len) >= len) {
2489 err = got_error(GOT_ERR_NO_SPACE);
2490 goto done;
2493 done:
2494 if (err) {
2495 free(*path);
2496 *path = NULL;
2498 return err;
2501 static const struct got_error *
2502 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2503 struct got_tree_entry *te, struct tog_parent_trees *parents,
2504 struct got_object_id *commit_id, struct got_repository *repo)
2506 const struct got_error *err = NULL;
2507 char *path;
2508 struct tog_view *blame_view;
2510 err = tree_entry_path(&path, parents, te);
2511 if (err)
2512 return err;
2514 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2515 if (blame_view == NULL)
2516 return got_error_from_errno();
2518 err = open_blame_view(blame_view, path, commit_id, repo);
2519 if (err) {
2520 view_close(blame_view);
2521 free(path);
2522 } else
2523 *new_view = blame_view;
2524 return err;
2527 static const struct got_error *
2528 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2529 struct got_tree_entry *te, struct tog_parent_trees *parents,
2530 struct got_object_id *commit_id, struct got_repository *repo)
2532 struct tog_view *log_view;
2533 const struct got_error *err = NULL;
2534 char *path;
2536 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2537 if (log_view == NULL)
2538 return got_error_from_errno();
2540 err = tree_entry_path(&path, parents, te);
2541 if (err)
2542 return err;
2544 err = open_log_view(log_view, commit_id, repo, path);
2545 if (err)
2546 view_close(log_view);
2547 else
2548 *new_view = log_view;
2549 free(path);
2550 return err;
2553 static const struct got_error *
2554 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2555 struct got_object_id *commit_id, struct got_repository *repo)
2557 const struct got_error *err = NULL;
2558 char *commit_id_str = NULL;
2559 struct tog_tree_view_state *s = &view->state.tree;
2561 TAILQ_INIT(&s->parents);
2563 err = got_object_id_str(&commit_id_str, commit_id);
2564 if (err != NULL)
2565 goto done;
2567 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2568 err = got_error_from_errno();
2569 goto done;
2572 s->root = s->tree = root;
2573 s->entries = got_object_tree_get_entries(root);
2574 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2575 s->commit_id = commit_id;
2576 s->repo = repo;
2578 view->show = show_tree_view;
2579 view->input = input_tree_view;
2580 view->close = close_tree_view;
2581 done:
2582 free(commit_id_str);
2583 if (err)
2584 free(s->tree_label);
2585 return err;
2588 static const struct got_error *
2589 close_tree_view(struct tog_view *view)
2591 struct tog_tree_view_state *s = &view->state.tree;
2593 free(s->tree_label);
2594 while (!TAILQ_EMPTY(&s->parents)) {
2595 struct tog_parent_tree *parent;
2596 parent = TAILQ_FIRST(&s->parents);
2597 TAILQ_REMOVE(&s->parents, parent, entry);
2598 free(parent);
2601 if (s->tree != s->root)
2602 got_object_tree_close(s->tree);
2603 got_object_tree_close(s->root);
2605 return NULL;
2608 static const struct got_error *
2609 show_tree_view(struct tog_view *view)
2611 const struct got_error *err = NULL;
2612 struct tog_tree_view_state *s = &view->state.tree;
2613 char *parent_path;
2615 err = tree_entry_path(&parent_path, &s->parents, NULL);
2616 if (err)
2617 return err;
2619 err = draw_tree_entries(view, &s->first_displayed_entry,
2620 &s->last_displayed_entry, &s->selected_entry,
2621 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2622 s->entries, s->selected, view->nlines, s->tree == s->root);
2623 free(parent_path);
2624 return err;
2627 static const struct got_error *
2628 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2629 struct tog_view *view, int ch)
2631 const struct got_error *err = NULL;
2632 struct tog_tree_view_state *s = &view->state.tree;
2634 switch (ch) {
2635 case 'i':
2636 s->show_ids = !s->show_ids;
2637 break;
2638 case 'l':
2639 if (s->selected_entry) {
2640 err = log_tree_entry(new_view, view,
2641 s->selected_entry, &s->parents,
2642 s->commit_id, s->repo);
2644 break;
2645 case 'k':
2646 case KEY_UP:
2647 if (s->selected > 0)
2648 s->selected--;
2649 if (s->selected > 0)
2650 break;
2651 tree_scroll_up(&s->first_displayed_entry, 1,
2652 s->entries, s->tree == s->root);
2653 break;
2654 case KEY_PPAGE:
2655 if (SIMPLEQ_FIRST(&s->entries->head) ==
2656 s->first_displayed_entry) {
2657 if (s->tree != s->root)
2658 s->first_displayed_entry = NULL;
2659 s->selected = 0;
2660 break;
2662 tree_scroll_up(&s->first_displayed_entry,
2663 view->nlines, s->entries,
2664 s->tree == s->root);
2665 break;
2666 case 'j':
2667 case KEY_DOWN:
2668 if (s->selected < s->ndisplayed - 1) {
2669 s->selected++;
2670 break;
2672 tree_scroll_down(&s->first_displayed_entry, 1,
2673 s->last_displayed_entry, s->entries);
2674 break;
2675 case KEY_NPAGE:
2676 tree_scroll_down(&s->first_displayed_entry,
2677 view->nlines, s->last_displayed_entry,
2678 s->entries);
2679 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2680 entry))
2681 break;
2682 /* can't scroll any further; move cursor down */
2683 if (s->selected < s->ndisplayed - 1)
2684 s->selected = s->ndisplayed - 1;
2685 break;
2686 case KEY_ENTER:
2687 case '\r':
2688 if (s->selected_entry == NULL) {
2689 struct tog_parent_tree *parent;
2690 case KEY_BACKSPACE:
2691 /* user selected '..' */
2692 if (s->tree == s->root)
2693 break;
2694 parent = TAILQ_FIRST(&s->parents);
2695 TAILQ_REMOVE(&s->parents, parent,
2696 entry);
2697 got_object_tree_close(s->tree);
2698 s->tree = parent->tree;
2699 s->entries =
2700 got_object_tree_get_entries(s->tree);
2701 s->first_displayed_entry =
2702 parent->first_displayed_entry;
2703 s->selected_entry =
2704 parent->selected_entry;
2705 s->selected = parent->selected;
2706 free(parent);
2707 } else if (S_ISDIR(s->selected_entry->mode)) {
2708 struct tog_parent_tree *parent;
2709 struct got_tree_object *child;
2710 err = got_object_open_as_tree(&child,
2711 s->repo, s->selected_entry->id);
2712 if (err)
2713 break;
2714 parent = calloc(1, sizeof(*parent));
2715 if (parent == NULL) {
2716 err = got_error_from_errno();
2717 break;
2719 parent->tree = s->tree;
2720 parent->first_displayed_entry =
2721 s->first_displayed_entry;
2722 parent->selected_entry = s->selected_entry;
2723 parent->selected = s->selected;
2724 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2725 s->tree = child;
2726 s->entries =
2727 got_object_tree_get_entries(s->tree);
2728 s->selected = 0;
2729 s->first_displayed_entry = NULL;
2730 } else if (S_ISREG(s->selected_entry->mode)) {
2731 err = blame_tree_entry(new_view, view,
2732 s->selected_entry, &s->parents,
2733 s->commit_id, s->repo);
2734 if (err)
2735 break;
2737 break;
2738 case KEY_RESIZE:
2739 if (s->selected > view->nlines)
2740 s->selected = s->ndisplayed - 1;
2741 break;
2742 default:
2743 break;
2746 return err;
2749 __dead static void
2750 usage_tree(void)
2752 endwin();
2753 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2754 getprogname());
2755 exit(1);
2758 static const struct got_error *
2759 cmd_tree(int argc, char *argv[])
2761 const struct got_error *error;
2762 struct got_repository *repo = NULL;
2763 char *repo_path = NULL;
2764 struct got_object_id *commit_id = NULL;
2765 char *commit_id_arg = NULL;
2766 struct got_commit_object *commit = NULL;
2767 struct got_tree_object *tree = NULL;
2768 int ch;
2769 struct tog_view *view;
2771 #ifndef PROFILE
2772 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2773 err(1, "pledge");
2774 #endif
2776 while ((ch = getopt(argc, argv, "c:")) != -1) {
2777 switch (ch) {
2778 case 'c':
2779 commit_id_arg = optarg;
2780 break;
2781 default:
2782 usage();
2783 /* NOTREACHED */
2787 argc -= optind;
2788 argv += optind;
2790 if (argc == 0) {
2791 repo_path = getcwd(NULL, 0);
2792 if (repo_path == NULL)
2793 return got_error_from_errno();
2794 } else if (argc == 1) {
2795 repo_path = realpath(argv[0], NULL);
2796 if (repo_path == NULL)
2797 return got_error_from_errno();
2798 } else
2799 usage_log();
2801 error = got_repo_open(&repo, repo_path);
2802 free(repo_path);
2803 if (error != NULL)
2804 return error;
2806 if (commit_id_arg == NULL) {
2807 error = get_head_commit_id(&commit_id, repo);
2808 if (error != NULL)
2809 goto done;
2810 } else {
2811 struct got_object *obj;
2812 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2813 if (error == NULL) {
2814 commit_id = got_object_get_id(obj);
2815 if (commit_id == NULL)
2816 error = got_error_from_errno();
2819 if (error != NULL)
2820 goto done;
2822 error = got_object_open_as_commit(&commit, repo, commit_id);
2823 if (error != NULL)
2824 goto done;
2826 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2827 if (error != NULL)
2828 goto done;
2830 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2831 if (view == NULL) {
2832 error = got_error_from_errno();
2833 goto done;
2835 error = open_tree_view(view, tree, commit_id, repo);
2836 if (error)
2837 goto done;
2838 error = view_loop(view);
2839 done:
2840 free(commit_id);
2841 if (commit)
2842 got_object_commit_close(commit);
2843 if (tree)
2844 got_object_tree_close(tree);
2845 if (repo)
2846 got_repo_close(repo);
2847 return error;
2849 static void
2850 init_curses(void)
2852 initscr();
2853 cbreak();
2854 noecho();
2855 nonl();
2856 intrflush(stdscr, FALSE);
2857 keypad(stdscr, TRUE);
2858 curs_set(0);
2861 __dead static void
2862 usage(void)
2864 int i;
2866 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2867 "Available commands:\n", getprogname());
2868 for (i = 0; i < nitems(tog_commands); i++) {
2869 struct tog_cmd *cmd = &tog_commands[i];
2870 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2872 exit(1);
2875 static char **
2876 make_argv(const char *arg0, const char *arg1)
2878 char **argv;
2879 int argc = (arg1 == NULL ? 1 : 2);
2881 argv = calloc(argc, sizeof(char *));
2882 if (argv == NULL)
2883 err(1, "calloc");
2884 argv[0] = strdup(arg0);
2885 if (argv[0] == NULL)
2886 err(1, "calloc");
2887 if (arg1) {
2888 argv[1] = strdup(arg1);
2889 if (argv[1] == NULL)
2890 err(1, "calloc");
2893 return argv;
2896 int
2897 main(int argc, char *argv[])
2899 const struct got_error *error = NULL;
2900 struct tog_cmd *cmd = NULL;
2901 int ch, hflag = 0;
2902 char **cmd_argv = NULL;
2904 setlocale(LC_ALL, "");
2906 while ((ch = getopt(argc, argv, "h")) != -1) {
2907 switch (ch) {
2908 case 'h':
2909 hflag = 1;
2910 break;
2911 default:
2912 usage();
2913 /* NOTREACHED */
2917 argc -= optind;
2918 argv += optind;
2919 optind = 0;
2920 optreset = 1;
2922 if (argc == 0) {
2923 if (hflag)
2924 usage();
2925 /* Build an argument vector which runs a default command. */
2926 cmd = &tog_commands[0];
2927 cmd_argv = make_argv(cmd->name, NULL);
2928 argc = 1;
2929 } else {
2930 int i;
2932 /* Did the user specific a command? */
2933 for (i = 0; i < nitems(tog_commands); i++) {
2934 if (strncmp(tog_commands[i].name, argv[0],
2935 strlen(argv[0])) == 0) {
2936 cmd = &tog_commands[i];
2937 if (hflag)
2938 tog_commands[i].cmd_usage();
2939 break;
2942 if (cmd == NULL) {
2943 /* Did the user specify a repository? */
2944 char *repo_path = realpath(argv[0], NULL);
2945 if (repo_path) {
2946 struct got_repository *repo;
2947 error = got_repo_open(&repo, repo_path);
2948 if (error == NULL)
2949 got_repo_close(repo);
2950 } else
2951 error = got_error_from_errno();
2952 if (error) {
2953 if (hflag) {
2954 fprintf(stderr, "%s: '%s' is not a "
2955 "known command\n", getprogname(),
2956 argv[0]);
2957 usage();
2959 fprintf(stderr, "%s: '%s' is neither a known "
2960 "command nor a path to a repository\n",
2961 getprogname(), argv[0]);
2962 free(repo_path);
2963 return 1;
2965 cmd = &tog_commands[0];
2966 cmd_argv = make_argv(cmd->name, repo_path);
2967 argc = 2;
2968 free(repo_path);
2972 init_curses();
2974 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2975 if (error)
2976 goto done;
2977 done:
2978 endwin();
2979 free(cmd_argv);
2980 if (error)
2981 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2982 return 0;