Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <string.h>
33 #include <err.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
41 #include <sched.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 STAILQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
137 if (err)
138 return err;
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
141 repo);
144 static void
145 tog_free_refs(void)
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
160 int regerr = 0;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
163 return NULL;
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
168 if (tc == NULL)
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 if (regerr) {
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
176 sizeof(regerr_msg));
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 regerr_msg);
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
180 free(tc);
181 return err;
183 tc->colorpair = idx;
184 STAILQ_INSERT_HEAD(colors, tc, entry);
185 return NULL;
188 static void
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!STAILQ_EMPTY(colors)) {
194 tc = STAILQ_FIRST(colors);
195 STAILQ_REMOVE_HEAD(colors, entry);
196 regfree(&tc->regex);
197 free(tc);
201 struct tog_color *
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 STAILQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
208 return tc;
211 return NULL;
214 static int
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 return COLOR_CYAN;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 return COLOR_YELLOW;
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 return COLOR_GREEN;
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 return COLOR_CYAN;
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 return COLOR_GREEN;
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 return COLOR_CYAN;
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 return COLOR_YELLOW;
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 return COLOR_GREEN;
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 return COLOR_YELLOW;
246 return -1;
249 static int
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
254 if (val == NULL)
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
258 return COLOR_BLACK;
259 if (strcasecmp(val, "red") == 0)
260 return COLOR_RED;
261 if (strcasecmp(val, "green") == 0)
262 return COLOR_GREEN;
263 if (strcasecmp(val, "yellow") == 0)
264 return COLOR_YELLOW;
265 if (strcasecmp(val, "blue") == 0)
266 return COLOR_BLUE;
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
270 return COLOR_CYAN;
271 if (strcasecmp(val, "white") == 0)
272 return COLOR_WHITE;
273 if (strcasecmp(val, "default") == 0)
274 return -1;
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
283 FILE *f;
284 int first_displayed_line;
285 int last_displayed_line;
286 int eof;
287 int diff_context;
288 int ignore_whitespace;
289 int force_text_diff;
290 struct got_repository *repo;
291 struct tog_colors colors;
292 size_t nlines;
293 off_t *line_offsets;
294 int matched_line;
295 int selected_line;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
299 };
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
306 int commits_needed;
307 int load_all;
308 struct got_commit_graph *graph;
309 struct commit_queue *commits;
310 const char *in_repo_path;
311 struct got_object_id *start_id;
312 struct got_repository *repo;
313 int log_complete;
314 sig_atomic_t *quit;
315 struct commit_queue_entry **first_displayed_entry;
316 struct commit_queue_entry **selected_entry;
317 int *searching;
318 int *search_next_done;
319 regex_t *regex;
320 };
322 struct tog_log_view_state {
323 struct commit_queue commits;
324 struct commit_queue_entry *first_displayed_entry;
325 struct commit_queue_entry *last_displayed_entry;
326 struct commit_queue_entry *selected_entry;
327 int selected;
328 char *in_repo_path;
329 char *head_ref_name;
330 int log_branches;
331 struct got_repository *repo;
332 struct got_object_id *start_id;
333 sig_atomic_t quit;
334 pthread_t thread;
335 struct tog_log_thread_args thread_args;
336 struct commit_queue_entry *matched_entry;
337 struct commit_queue_entry *search_entry;
338 struct tog_colors colors;
339 };
341 #define TOG_COLOR_DIFF_MINUS 1
342 #define TOG_COLOR_DIFF_PLUS 2
343 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
344 #define TOG_COLOR_DIFF_META 4
345 #define TOG_COLOR_TREE_SUBMODULE 5
346 #define TOG_COLOR_TREE_SYMLINK 6
347 #define TOG_COLOR_TREE_DIRECTORY 7
348 #define TOG_COLOR_TREE_EXECUTABLE 8
349 #define TOG_COLOR_COMMIT 9
350 #define TOG_COLOR_AUTHOR 10
351 #define TOG_COLOR_DATE 11
352 #define TOG_COLOR_REFS_HEADS 12
353 #define TOG_COLOR_REFS_TAGS 13
354 #define TOG_COLOR_REFS_REMOTES 14
356 struct tog_blame_cb_args {
357 struct tog_blame_line *lines; /* one per line */
358 int nlines;
360 struct tog_view *view;
361 struct got_object_id *commit_id;
362 int *quit;
363 };
365 struct tog_blame_thread_args {
366 const char *path;
367 struct got_repository *repo;
368 struct tog_blame_cb_args *cb_args;
369 int *complete;
370 got_cancel_cb cancel_cb;
371 void *cancel_arg;
372 };
374 struct tog_blame {
375 FILE *f;
376 off_t filesize;
377 struct tog_blame_line *lines;
378 int nlines;
379 off_t *line_offsets;
380 pthread_t thread;
381 struct tog_blame_thread_args thread_args;
382 struct tog_blame_cb_args cb_args;
383 const char *path;
384 };
386 struct tog_blame_view_state {
387 int first_displayed_line;
388 int last_displayed_line;
389 int selected_line;
390 int blame_complete;
391 int eof;
392 int done;
393 struct got_object_id_queue blamed_commits;
394 struct got_object_qid *blamed_commit;
395 char *path;
396 struct got_repository *repo;
397 struct got_object_id *commit_id;
398 struct tog_blame blame;
399 int matched_line;
400 struct tog_colors colors;
401 };
403 struct tog_parent_tree {
404 TAILQ_ENTRY(tog_parent_tree) entry;
405 struct got_tree_object *tree;
406 struct got_tree_entry *first_displayed_entry;
407 struct got_tree_entry *selected_entry;
408 int selected;
409 };
411 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
413 struct tog_tree_view_state {
414 char *tree_label;
415 struct got_object_id *commit_id;/* commit which this tree belongs to */
416 struct got_tree_object *root; /* the commit's root tree entry */
417 struct got_tree_object *tree; /* currently displayed (sub-)tree */
418 struct got_tree_entry *first_displayed_entry;
419 struct got_tree_entry *last_displayed_entry;
420 struct got_tree_entry *selected_entry;
421 int ndisplayed, selected, show_ids;
422 struct tog_parent_trees parents; /* parent trees of current sub-tree */
423 char *head_ref_name;
424 struct got_repository *repo;
425 struct got_tree_entry *matched_entry;
426 struct tog_colors colors;
427 };
429 struct tog_reflist_entry {
430 TAILQ_ENTRY(tog_reflist_entry) entry;
431 struct got_reference *ref;
432 int idx;
433 };
435 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
437 struct tog_ref_view_state {
438 struct tog_reflist_head refs;
439 struct tog_reflist_entry *first_displayed_entry;
440 struct tog_reflist_entry *last_displayed_entry;
441 struct tog_reflist_entry *selected_entry;
442 int nrefs, ndisplayed, selected, show_ids;
443 struct got_repository *repo;
444 struct tog_reflist_entry *matched_entry;
445 struct tog_colors colors;
446 };
448 /*
449 * We implement two types of views: parent views and child views.
451 * The 'Tab' key switches focus between a parent view and its child view.
452 * Child views are shown side-by-side to their parent view, provided
453 * there is enough screen estate.
455 * When a new view is opened from within a parent view, this new view
456 * becomes a child view of the parent view, replacing any existing child.
458 * When a new view is opened from within a child view, this new view
459 * becomes a parent view which will obscure the views below until the
460 * user quits the new parent view by typing 'q'.
462 * This list of views contains parent views only.
463 * Child views are only pointed to by their parent view.
464 */
465 TAILQ_HEAD(tog_view_list_head, tog_view);
467 struct tog_view {
468 TAILQ_ENTRY(tog_view) entry;
469 WINDOW *window;
470 PANEL *panel;
471 int nlines, ncols, begin_y, begin_x;
472 int lines, cols; /* copies of LINES and COLS */
473 int focussed; /* Only set on one parent or child view at a time. */
474 int dying;
475 struct tog_view *parent;
476 struct tog_view *child;
478 /*
479 * This flag is initially set on parent views when a new child view
480 * is created. It gets toggled when the 'Tab' key switches focus
481 * between parent and child.
482 * The flag indicates whether focus should be passed on to our child
483 * view if this parent view gets picked for focus after another parent
484 * view was closed. This prevents child views from losing focus in such
485 * situations.
486 */
487 int focus_child;
489 /* type-specific state */
490 enum tog_view_type type;
491 union {
492 struct tog_diff_view_state diff;
493 struct tog_log_view_state log;
494 struct tog_blame_view_state blame;
495 struct tog_tree_view_state tree;
496 struct tog_ref_view_state ref;
497 } state;
499 const struct got_error *(*show)(struct tog_view *);
500 const struct got_error *(*input)(struct tog_view **,
501 struct tog_view *, int);
502 const struct got_error *(*close)(struct tog_view *);
504 const struct got_error *(*search_start)(struct tog_view *);
505 const struct got_error *(*search_next)(struct tog_view *);
506 int search_started;
507 int searching;
508 #define TOG_SEARCH_FORWARD 1
509 #define TOG_SEARCH_BACKWARD 2
510 int search_next_done;
511 #define TOG_SEARCH_HAVE_MORE 1
512 #define TOG_SEARCH_NO_MORE 2
513 #define TOG_SEARCH_HAVE_NONE 3
514 regex_t regex;
515 regmatch_t regmatch;
516 };
518 static const struct got_error *open_diff_view(struct tog_view *,
519 struct got_object_id *, struct got_object_id *,
520 const char *, const char *, int, int, int, struct tog_view *,
521 struct got_repository *);
522 static const struct got_error *show_diff_view(struct tog_view *);
523 static const struct got_error *input_diff_view(struct tog_view **,
524 struct tog_view *, int);
525 static const struct got_error* close_diff_view(struct tog_view *);
526 static const struct got_error *search_start_diff_view(struct tog_view *);
527 static const struct got_error *search_next_diff_view(struct tog_view *);
529 static const struct got_error *open_log_view(struct tog_view *,
530 struct got_object_id *, struct got_repository *,
531 const char *, const char *, int);
532 static const struct got_error * show_log_view(struct tog_view *);
533 static const struct got_error *input_log_view(struct tog_view **,
534 struct tog_view *, int);
535 static const struct got_error *close_log_view(struct tog_view *);
536 static const struct got_error *search_start_log_view(struct tog_view *);
537 static const struct got_error *search_next_log_view(struct tog_view *);
539 static const struct got_error *open_blame_view(struct tog_view *, char *,
540 struct got_object_id *, struct got_repository *);
541 static const struct got_error *show_blame_view(struct tog_view *);
542 static const struct got_error *input_blame_view(struct tog_view **,
543 struct tog_view *, int);
544 static const struct got_error *close_blame_view(struct tog_view *);
545 static const struct got_error *search_start_blame_view(struct tog_view *);
546 static const struct got_error *search_next_blame_view(struct tog_view *);
548 static const struct got_error *open_tree_view(struct tog_view *,
549 struct got_object_id *, const char *, struct got_repository *);
550 static const struct got_error *show_tree_view(struct tog_view *);
551 static const struct got_error *input_tree_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error *close_tree_view(struct tog_view *);
554 static const struct got_error *search_start_tree_view(struct tog_view *);
555 static const struct got_error *search_next_tree_view(struct tog_view *);
557 static const struct got_error *open_ref_view(struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_ref_view(struct tog_view *);
560 static const struct got_error *input_ref_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error *close_ref_view(struct tog_view *);
563 static const struct got_error *search_start_ref_view(struct tog_view *);
564 static const struct got_error *search_next_ref_view(struct tog_view *);
566 static volatile sig_atomic_t tog_sigwinch_received;
567 static volatile sig_atomic_t tog_sigpipe_received;
568 static volatile sig_atomic_t tog_sigcont_received;
570 static void
571 tog_sigwinch(int signo)
573 tog_sigwinch_received = 1;
576 static void
577 tog_sigpipe(int signo)
579 tog_sigpipe_received = 1;
582 static void
583 tog_sigcont(int signo)
585 tog_sigcont_received = 1;
588 static const struct got_error *
589 view_close(struct tog_view *view)
591 const struct got_error *err = NULL;
593 if (view->child) {
594 view_close(view->child);
595 view->child = NULL;
597 if (view->close)
598 err = view->close(view);
599 if (view->panel)
600 del_panel(view->panel);
601 if (view->window)
602 delwin(view->window);
603 free(view);
604 return err;
607 static struct tog_view *
608 view_open(int nlines, int ncols, int begin_y, int begin_x,
609 enum tog_view_type type)
611 struct tog_view *view = calloc(1, sizeof(*view));
613 if (view == NULL)
614 return NULL;
616 view->type = type;
617 view->lines = LINES;
618 view->cols = COLS;
619 view->nlines = nlines ? nlines : LINES - begin_y;
620 view->ncols = ncols ? ncols : COLS - begin_x;
621 view->begin_y = begin_y;
622 view->begin_x = begin_x;
623 view->window = newwin(nlines, ncols, begin_y, begin_x);
624 if (view->window == NULL) {
625 view_close(view);
626 return NULL;
628 view->panel = new_panel(view->window);
629 if (view->panel == NULL ||
630 set_panel_userptr(view->panel, view) != OK) {
631 view_close(view);
632 return NULL;
635 keypad(view->window, TRUE);
636 return view;
639 static int
640 view_split_begin_x(int begin_x)
642 if (begin_x > 0 || COLS < 120)
643 return 0;
644 return (COLS - MAX(COLS / 2, 80));
647 static const struct got_error *view_resize(struct tog_view *);
649 static const struct got_error *
650 view_splitscreen(struct tog_view *view)
652 const struct got_error *err = NULL;
654 view->begin_y = 0;
655 view->begin_x = view_split_begin_x(0);
656 view->nlines = LINES;
657 view->ncols = COLS - view->begin_x;
658 view->lines = LINES;
659 view->cols = COLS;
660 err = view_resize(view);
661 if (err)
662 return err;
664 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 return got_error_from_errno("mvwin");
667 return NULL;
670 static const struct got_error *
671 view_fullscreen(struct tog_view *view)
673 const struct got_error *err = NULL;
675 view->begin_x = 0;
676 view->begin_y = 0;
677 view->nlines = LINES;
678 view->ncols = COLS;
679 view->lines = LINES;
680 view->cols = COLS;
681 err = view_resize(view);
682 if (err)
683 return err;
685 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 return got_error_from_errno("mvwin");
688 return NULL;
691 static int
692 view_is_parent_view(struct tog_view *view)
694 return view->parent == NULL;
697 static const struct got_error *
698 view_resize(struct tog_view *view)
700 int nlines, ncols;
702 if (view->lines > LINES)
703 nlines = view->nlines - (view->lines - LINES);
704 else
705 nlines = view->nlines + (LINES - view->lines);
707 if (view->cols > COLS)
708 ncols = view->ncols - (view->cols - COLS);
709 else
710 ncols = view->ncols + (COLS - view->cols);
712 if (wresize(view->window, nlines, ncols) == ERR)
713 return got_error_from_errno("wresize");
714 if (replace_panel(view->panel, view->window) == ERR)
715 return got_error_from_errno("replace_panel");
716 wclear(view->window);
718 view->nlines = nlines;
719 view->ncols = ncols;
720 view->lines = LINES;
721 view->cols = COLS;
723 if (view->child) {
724 view->child->begin_x = view_split_begin_x(view->begin_x);
725 if (view->child->begin_x == 0) {
726 view_fullscreen(view->child);
727 if (view->child->focussed)
728 show_panel(view->child->panel);
729 else
730 show_panel(view->panel);
731 } else {
732 view_splitscreen(view->child);
733 show_panel(view->child->panel);
737 return NULL;
740 static const struct got_error *
741 view_close_child(struct tog_view *view)
743 const struct got_error *err = NULL;
745 if (view->child == NULL)
746 return NULL;
748 err = view_close(view->child);
749 view->child = NULL;
750 return err;
753 static void
754 view_set_child(struct tog_view *view, struct tog_view *child)
756 view->child = child;
757 child->parent = view;
760 static int
761 view_is_splitscreen(struct tog_view *view)
763 return view->begin_x > 0;
766 static void
767 tog_resizeterm(void)
769 int cols, lines;
770 struct winsize size;
772 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 cols = 80; /* Default */
774 lines = 24;
775 } else {
776 cols = size.ws_col;
777 lines = size.ws_row;
779 resize_term(lines, cols);
782 static const struct got_error *
783 view_search_start(struct tog_view *view)
785 const struct got_error *err = NULL;
786 char pattern[1024];
787 int ret;
789 if (view->search_started) {
790 regfree(&view->regex);
791 view->searching = 0;
792 memset(&view->regmatch, 0, sizeof(view->regmatch));
794 view->search_started = 0;
796 if (view->nlines < 1)
797 return NULL;
799 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 wclrtoeol(view->window);
802 nocbreak();
803 echo();
804 ret = wgetnstr(view->window, pattern, sizeof(pattern));
805 cbreak();
806 noecho();
807 if (ret == ERR)
808 return NULL;
810 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 err = view->search_start(view);
812 if (err) {
813 regfree(&view->regex);
814 return err;
816 view->search_started = 1;
817 view->searching = TOG_SEARCH_FORWARD;
818 view->search_next_done = 0;
819 view->search_next(view);
822 return NULL;
825 static const struct got_error *
826 view_input(struct tog_view **new, int *done, struct tog_view *view,
827 struct tog_view_list_head *views)
829 const struct got_error *err = NULL;
830 struct tog_view *v;
831 int ch, errcode;
833 *new = NULL;
835 /* Clear "no matches" indicator. */
836 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 if (view->searching && !view->search_next_done) {
841 errcode = pthread_mutex_unlock(&tog_mutex);
842 if (errcode)
843 return got_error_set_errno(errcode,
844 "pthread_mutex_unlock");
845 sched_yield();
846 errcode = pthread_mutex_lock(&tog_mutex);
847 if (errcode)
848 return got_error_set_errno(errcode,
849 "pthread_mutex_lock");
850 view->search_next(view);
851 return NULL;
854 nodelay(stdscr, FALSE);
855 /* Allow threads to make progress while we are waiting for input. */
856 errcode = pthread_mutex_unlock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 ch = wgetch(view->window);
860 errcode = pthread_mutex_lock(&tog_mutex);
861 if (errcode)
862 return got_error_set_errno(errcode, "pthread_mutex_lock");
863 nodelay(stdscr, TRUE);
865 if (tog_sigwinch_received || tog_sigcont_received) {
866 tog_resizeterm();
867 tog_sigwinch_received = 0;
868 tog_sigcont_received = 0;
869 TAILQ_FOREACH(v, views, entry) {
870 err = view_resize(v);
871 if (err)
872 return err;
873 err = v->input(new, v, KEY_RESIZE);
874 if (err)
875 return err;
876 if (v->child) {
877 err = view_resize(v->child);
878 if (err)
879 return err;
880 err = v->child->input(new, v->child,
881 KEY_RESIZE);
882 if (err)
883 return err;
888 switch (ch) {
889 case '\t':
890 if (view->child) {
891 view->focussed = 0;
892 view->child->focussed = 1;
893 view->focus_child = 1;
894 } else if (view->parent) {
895 view->focussed = 0;
896 view->parent->focussed = 1;
897 view->parent->focus_child = 0;
899 break;
900 case 'q':
901 err = view->input(new, view, ch);
902 view->dying = 1;
903 break;
904 case 'Q':
905 *done = 1;
906 break;
907 case 'f':
908 if (view_is_parent_view(view)) {
909 if (view->child == NULL)
910 break;
911 if (view_is_splitscreen(view->child)) {
912 view->focussed = 0;
913 view->child->focussed = 1;
914 err = view_fullscreen(view->child);
915 } else
916 err = view_splitscreen(view->child);
917 if (err)
918 break;
919 err = view->child->input(new, view->child,
920 KEY_RESIZE);
921 } else {
922 if (view_is_splitscreen(view)) {
923 view->parent->focussed = 0;
924 view->focussed = 1;
925 err = view_fullscreen(view);
926 } else {
927 err = view_splitscreen(view);
929 if (err)
930 break;
931 err = view->input(new, view, KEY_RESIZE);
933 break;
934 case KEY_RESIZE:
935 break;
936 case '/':
937 if (view->search_start)
938 view_search_start(view);
939 else
940 err = view->input(new, view, ch);
941 break;
942 case 'N':
943 case 'n':
944 if (view->search_started && view->search_next) {
945 view->searching = (ch == 'n' ?
946 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
947 view->search_next_done = 0;
948 view->search_next(view);
949 } else
950 err = view->input(new, view, ch);
951 break;
952 default:
953 err = view->input(new, view, ch);
954 break;
957 return err;
960 void
961 view_vborder(struct tog_view *view)
963 PANEL *panel;
964 const struct tog_view *view_above;
966 if (view->parent)
967 return view_vborder(view->parent);
969 panel = panel_above(view->panel);
970 if (panel == NULL)
971 return;
973 view_above = panel_userptr(panel);
974 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
978 int
979 view_needs_focus_indication(struct tog_view *view)
981 if (view_is_parent_view(view)) {
982 if (view->child == NULL || view->child->focussed)
983 return 0;
984 if (!view_is_splitscreen(view->child))
985 return 0;
986 } else if (!view_is_splitscreen(view))
987 return 0;
989 return view->focussed;
992 static const struct got_error *
993 view_loop(struct tog_view *view)
995 const struct got_error *err = NULL;
996 struct tog_view_list_head views;
997 struct tog_view *new_view;
998 int fast_refresh = 10;
999 int done = 0, errcode;
1001 errcode = pthread_mutex_lock(&tog_mutex);
1002 if (errcode)
1003 return got_error_set_errno(errcode, "pthread_mutex_lock");
1005 TAILQ_INIT(&views);
1006 TAILQ_INSERT_HEAD(&views, view, entry);
1008 view->focussed = 1;
1009 err = view->show(view);
1010 if (err)
1011 return err;
1012 update_panels();
1013 doupdate();
1014 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1015 /* Refresh fast during initialization, then become slower. */
1016 if (fast_refresh && fast_refresh-- == 0)
1017 halfdelay(10); /* switch to once per second */
1019 err = view_input(&new_view, &done, view, &views);
1020 if (err)
1021 break;
1022 if (view->dying) {
1023 struct tog_view *v, *prev = NULL;
1025 if (view_is_parent_view(view))
1026 prev = TAILQ_PREV(view, tog_view_list_head,
1027 entry);
1028 else if (view->parent)
1029 prev = view->parent;
1031 if (view->parent) {
1032 view->parent->child = NULL;
1033 view->parent->focus_child = 0;
1034 } else
1035 TAILQ_REMOVE(&views, view, entry);
1037 err = view_close(view);
1038 if (err)
1039 goto done;
1041 view = NULL;
1042 TAILQ_FOREACH(v, &views, entry) {
1043 if (v->focussed)
1044 break;
1046 if (view == NULL && new_view == NULL) {
1047 /* No view has focus. Try to pick one. */
1048 if (prev)
1049 view = prev;
1050 else if (!TAILQ_EMPTY(&views)) {
1051 view = TAILQ_LAST(&views,
1052 tog_view_list_head);
1054 if (view) {
1055 if (view->focus_child) {
1056 view->child->focussed = 1;
1057 view = view->child;
1058 } else
1059 view->focussed = 1;
1063 if (new_view) {
1064 struct tog_view *v, *t;
1065 /* Only allow one parent view per type. */
1066 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1067 if (v->type != new_view->type)
1068 continue;
1069 TAILQ_REMOVE(&views, v, entry);
1070 err = view_close(v);
1071 if (err)
1072 goto done;
1073 break;
1075 TAILQ_INSERT_TAIL(&views, new_view, entry);
1076 view = new_view;
1078 if (view) {
1079 if (view_is_parent_view(view)) {
1080 if (view->child && view->child->focussed)
1081 view = view->child;
1082 } else {
1083 if (view->parent && view->parent->focussed)
1084 view = view->parent;
1086 show_panel(view->panel);
1087 if (view->child && view_is_splitscreen(view->child))
1088 show_panel(view->child->panel);
1089 if (view->parent && view_is_splitscreen(view)) {
1090 err = view->parent->show(view->parent);
1091 if (err)
1092 goto done;
1094 err = view->show(view);
1095 if (err)
1096 goto done;
1097 if (view->child) {
1098 err = view->child->show(view->child);
1099 if (err)
1100 goto done;
1102 update_panels();
1103 doupdate();
1106 done:
1107 while (!TAILQ_EMPTY(&views)) {
1108 view = TAILQ_FIRST(&views);
1109 TAILQ_REMOVE(&views, view, entry);
1110 view_close(view);
1113 errcode = pthread_mutex_unlock(&tog_mutex);
1114 if (errcode && err == NULL)
1115 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1117 return err;
1120 __dead static void
1121 usage_log(void)
1123 endwin();
1124 fprintf(stderr,
1125 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1126 getprogname());
1127 exit(1);
1130 /* Create newly allocated wide-character string equivalent to a byte string. */
1131 static const struct got_error *
1132 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1134 char *vis = NULL;
1135 const struct got_error *err = NULL;
1137 *ws = NULL;
1138 *wlen = mbstowcs(NULL, s, 0);
1139 if (*wlen == (size_t)-1) {
1140 int vislen;
1141 if (errno != EILSEQ)
1142 return got_error_from_errno("mbstowcs");
1144 /* byte string invalid in current encoding; try to "fix" it */
1145 err = got_mbsavis(&vis, &vislen, s);
1146 if (err)
1147 return err;
1148 *wlen = mbstowcs(NULL, vis, 0);
1149 if (*wlen == (size_t)-1) {
1150 err = got_error_from_errno("mbstowcs"); /* give up */
1151 goto done;
1155 *ws = calloc(*wlen + 1, sizeof(**ws));
1156 if (*ws == NULL) {
1157 err = got_error_from_errno("calloc");
1158 goto done;
1161 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1162 err = got_error_from_errno("mbstowcs");
1163 done:
1164 free(vis);
1165 if (err) {
1166 free(*ws);
1167 *ws = NULL;
1168 *wlen = 0;
1170 return err;
1173 /* Format a line for display, ensuring that it won't overflow a width limit. */
1174 static const struct got_error *
1175 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1176 int col_tab_align)
1178 const struct got_error *err = NULL;
1179 int cols = 0;
1180 wchar_t *wline = NULL;
1181 size_t wlen;
1182 int i;
1184 *wlinep = NULL;
1185 *widthp = 0;
1187 err = mbs2ws(&wline, &wlen, line);
1188 if (err)
1189 return err;
1191 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1192 wline[wlen - 1] = L'\0';
1193 wlen--;
1195 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1196 wline[wlen - 1] = L'\0';
1197 wlen--;
1200 i = 0;
1201 while (i < wlen) {
1202 int width = wcwidth(wline[i]);
1204 if (width == 0) {
1205 i++;
1206 continue;
1209 if (width == 1 || width == 2) {
1210 if (cols + width > wlimit)
1211 break;
1212 cols += width;
1213 i++;
1214 } else if (width == -1) {
1215 if (wline[i] == L'\t') {
1216 width = TABSIZE -
1217 ((cols + col_tab_align) % TABSIZE);
1218 } else {
1219 width = 1;
1220 wline[i] = L'.';
1222 if (cols + width > wlimit)
1223 break;
1224 cols += width;
1225 i++;
1226 } else {
1227 err = got_error_from_errno("wcwidth");
1228 goto done;
1231 wline[i] = L'\0';
1232 if (widthp)
1233 *widthp = cols;
1234 done:
1235 if (err)
1236 free(wline);
1237 else
1238 *wlinep = wline;
1239 return err;
1242 static const struct got_error*
1243 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1244 struct got_object_id *id, struct got_repository *repo)
1246 static const struct got_error *err = NULL;
1247 struct got_reflist_entry *re;
1248 char *s;
1249 const char *name;
1251 *refs_str = NULL;
1253 TAILQ_FOREACH(re, refs, entry) {
1254 struct got_tag_object *tag = NULL;
1255 struct got_object_id *ref_id;
1256 int cmp;
1258 name = got_ref_get_name(re->ref);
1259 if (strcmp(name, GOT_REF_HEAD) == 0)
1260 continue;
1261 if (strncmp(name, "refs/", 5) == 0)
1262 name += 5;
1263 if (strncmp(name, "got/", 4) == 0)
1264 continue;
1265 if (strncmp(name, "heads/", 6) == 0)
1266 name += 6;
1267 if (strncmp(name, "remotes/", 8) == 0) {
1268 name += 8;
1269 s = strstr(name, "/" GOT_REF_HEAD);
1270 if (s != NULL && s[strlen(s)] == '\0')
1271 continue;
1273 err = got_ref_resolve(&ref_id, repo, re->ref);
1274 if (err)
1275 break;
1276 if (strncmp(name, "tags/", 5) == 0) {
1277 err = got_object_open_as_tag(&tag, repo, ref_id);
1278 if (err) {
1279 if (err->code != GOT_ERR_OBJ_TYPE) {
1280 free(ref_id);
1281 break;
1283 /* Ref points at something other than a tag. */
1284 err = NULL;
1285 tag = NULL;
1288 cmp = got_object_id_cmp(tag ?
1289 got_object_tag_get_object_id(tag) : ref_id, id);
1290 free(ref_id);
1291 if (tag)
1292 got_object_tag_close(tag);
1293 if (cmp != 0)
1294 continue;
1295 s = *refs_str;
1296 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1297 s ? ", " : "", name) == -1) {
1298 err = got_error_from_errno("asprintf");
1299 free(s);
1300 *refs_str = NULL;
1301 break;
1303 free(s);
1306 return err;
1309 static const struct got_error *
1310 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1311 int col_tab_align)
1313 char *smallerthan;
1315 smallerthan = strchr(author, '<');
1316 if (smallerthan && smallerthan[1] != '\0')
1317 author = smallerthan + 1;
1318 author[strcspn(author, "@>")] = '\0';
1319 return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 static const struct got_error *
1323 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 struct got_object_id *id, const size_t date_display_cols,
1325 int author_display_cols)
1327 struct tog_log_view_state *s = &view->state.log;
1328 const struct got_error *err = NULL;
1329 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 char *logmsg0 = NULL, *logmsg = NULL;
1331 char *author = NULL;
1332 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 int author_width, logmsg_width;
1334 char *newline, *line = NULL;
1335 int col, limit;
1336 const int avail = view->ncols;
1337 struct tm tm;
1338 time_t committer_time;
1339 struct tog_color *tc;
1341 committer_time = got_object_commit_get_committer_time(commit);
1342 if (gmtime_r(&committer_time, &tm) == NULL)
1343 return got_error_from_errno("gmtime_r");
1344 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1345 return got_error(GOT_ERR_NO_SPACE);
1347 if (avail <= date_display_cols)
1348 limit = MIN(sizeof(datebuf) - 1, avail);
1349 else
1350 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1351 tc = get_color(&s->colors, TOG_COLOR_DATE);
1352 if (tc)
1353 wattr_on(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 waddnstr(view->window, datebuf, limit);
1356 if (tc)
1357 wattr_off(view->window,
1358 COLOR_PAIR(tc->colorpair), NULL);
1359 col = limit;
1360 if (col > avail)
1361 goto done;
1363 if (avail >= 120) {
1364 char *id_str;
1365 err = got_object_id_str(&id_str, id);
1366 if (err)
1367 goto done;
1368 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1369 if (tc)
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 wprintw(view->window, "%.8s ", id_str);
1373 if (tc)
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1376 free(id_str);
1377 col += 9;
1378 if (col > avail)
1379 goto done;
1382 author = strdup(got_object_commit_get_author(commit));
1383 if (author == NULL) {
1384 err = got_error_from_errno("strdup");
1385 goto done;
1387 err = format_author(&wauthor, &author_width, author, avail - col, col);
1388 if (err)
1389 goto done;
1390 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1391 if (tc)
1392 wattr_on(view->window,
1393 COLOR_PAIR(tc->colorpair), NULL);
1394 waddwstr(view->window, wauthor);
1395 if (tc)
1396 wattr_off(view->window,
1397 COLOR_PAIR(tc->colorpair), NULL);
1398 col += author_width;
1399 while (col < avail && author_width < author_display_cols + 2) {
1400 waddch(view->window, ' ');
1401 col++;
1402 author_width++;
1404 if (col > avail)
1405 goto done;
1407 err = got_object_commit_get_logmsg(&logmsg0, commit);
1408 if (err)
1409 goto done;
1410 logmsg = logmsg0;
1411 while (*logmsg == '\n')
1412 logmsg++;
1413 newline = strchr(logmsg, '\n');
1414 if (newline)
1415 *newline = '\0';
1416 limit = avail - col;
1417 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1418 if (err)
1419 goto done;
1420 waddwstr(view->window, wlogmsg);
1421 col += logmsg_width;
1422 while (col < avail) {
1423 waddch(view->window, ' ');
1424 col++;
1426 done:
1427 free(logmsg0);
1428 free(wlogmsg);
1429 free(author);
1430 free(wauthor);
1431 free(line);
1432 return err;
1435 static struct commit_queue_entry *
1436 alloc_commit_queue_entry(struct got_commit_object *commit,
1437 struct got_object_id *id)
1439 struct commit_queue_entry *entry;
1441 entry = calloc(1, sizeof(*entry));
1442 if (entry == NULL)
1443 return NULL;
1445 entry->id = id;
1446 entry->commit = commit;
1447 return entry;
1450 static void
1451 pop_commit(struct commit_queue *commits)
1453 struct commit_queue_entry *entry;
1455 entry = TAILQ_FIRST(&commits->head);
1456 TAILQ_REMOVE(&commits->head, entry, entry);
1457 got_object_commit_close(entry->commit);
1458 commits->ncommits--;
1459 /* Don't free entry->id! It is owned by the commit graph. */
1460 free(entry);
1463 static void
1464 free_commits(struct commit_queue *commits)
1466 while (!TAILQ_EMPTY(&commits->head))
1467 pop_commit(commits);
1470 static const struct got_error *
1471 match_commit(int *have_match, struct got_object_id *id,
1472 struct got_commit_object *commit, regex_t *regex)
1474 const struct got_error *err = NULL;
1475 regmatch_t regmatch;
1476 char *id_str = NULL, *logmsg = NULL;
1478 *have_match = 0;
1480 err = got_object_id_str(&id_str, id);
1481 if (err)
1482 return err;
1484 err = got_object_commit_get_logmsg(&logmsg, commit);
1485 if (err)
1486 goto done;
1488 if (regexec(regex, got_object_commit_get_author(commit), 1,
1489 &regmatch, 0) == 0 ||
1490 regexec(regex, got_object_commit_get_committer(commit), 1,
1491 &regmatch, 0) == 0 ||
1492 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1493 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1494 *have_match = 1;
1495 done:
1496 free(id_str);
1497 free(logmsg);
1498 return err;
1501 static const struct got_error *
1502 queue_commits(struct tog_log_thread_args *a)
1504 const struct got_error *err = NULL;
1507 * We keep all commits open throughout the lifetime of the log
1508 * view in order to avoid having to re-fetch commits from disk
1509 * while updating the display.
1511 do {
1512 struct got_object_id *id;
1513 struct got_commit_object *commit;
1514 struct commit_queue_entry *entry;
1515 int errcode;
1517 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1518 NULL, NULL);
1519 if (err || id == NULL)
1520 break;
1522 err = got_object_open_as_commit(&commit, a->repo, id);
1523 if (err)
1524 break;
1525 entry = alloc_commit_queue_entry(commit, id);
1526 if (entry == NULL) {
1527 err = got_error_from_errno("alloc_commit_queue_entry");
1528 break;
1531 errcode = pthread_mutex_lock(&tog_mutex);
1532 if (errcode) {
1533 err = got_error_set_errno(errcode,
1534 "pthread_mutex_lock");
1535 break;
1538 entry->idx = a->commits->ncommits;
1539 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1540 a->commits->ncommits++;
1542 if (*a->searching == TOG_SEARCH_FORWARD &&
1543 !*a->search_next_done) {
1544 int have_match;
1545 err = match_commit(&have_match, id, commit, a->regex);
1546 if (err)
1547 break;
1548 if (have_match)
1549 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1552 errcode = pthread_mutex_unlock(&tog_mutex);
1553 if (errcode && err == NULL)
1554 err = got_error_set_errno(errcode,
1555 "pthread_mutex_unlock");
1556 if (err)
1557 break;
1558 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1560 return err;
1563 static void
1564 select_commit(struct tog_log_view_state *s)
1566 struct commit_queue_entry *entry;
1567 int ncommits = 0;
1569 entry = s->first_displayed_entry;
1570 while (entry) {
1571 if (ncommits == s->selected) {
1572 s->selected_entry = entry;
1573 break;
1575 entry = TAILQ_NEXT(entry, entry);
1576 ncommits++;
1580 static const struct got_error *
1581 draw_commits(struct tog_view *view)
1583 const struct got_error *err = NULL;
1584 struct tog_log_view_state *s = &view->state.log;
1585 struct commit_queue_entry *entry = s->selected_entry;
1586 const int limit = view->nlines;
1587 int width;
1588 int ncommits, author_cols = 4;
1589 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1590 char *refs_str = NULL;
1591 wchar_t *wline;
1592 struct tog_color *tc;
1593 static const size_t date_display_cols = 12;
1595 if (s->selected_entry &&
1596 !(view->searching && view->search_next_done == 0)) {
1597 struct got_reflist_head *refs;
1598 err = got_object_id_str(&id_str, s->selected_entry->id);
1599 if (err)
1600 return err;
1601 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1602 s->selected_entry->id);
1603 if (refs) {
1604 err = build_refs_str(&refs_str, refs,
1605 s->selected_entry->id, s->repo);
1606 if (err)
1607 goto done;
1611 if (s->thread_args.commits_needed == 0)
1612 halfdelay(10); /* disable fast refresh */
1614 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1615 if (asprintf(&ncommits_str, " [%d/%d] %s",
1616 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1617 (view->searching && !view->search_next_done) ?
1618 "searching..." : "loading...") == -1) {
1619 err = got_error_from_errno("asprintf");
1620 goto done;
1622 } else {
1623 const char *search_str = NULL;
1625 if (view->searching) {
1626 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1627 search_str = "no more matches";
1628 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1629 search_str = "no matches found";
1630 else if (!view->search_next_done)
1631 search_str = "searching...";
1634 if (asprintf(&ncommits_str, " [%d/%d] %s",
1635 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1636 search_str ? search_str :
1637 (refs_str ? refs_str : "")) == -1) {
1638 err = got_error_from_errno("asprintf");
1639 goto done;
1643 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1644 if (asprintf(&header, "commit %s %s%s",
1645 id_str ? id_str : "........................................",
1646 s->in_repo_path, ncommits_str) == -1) {
1647 err = got_error_from_errno("asprintf");
1648 header = NULL;
1649 goto done;
1651 } else if (asprintf(&header, "commit %s%s",
1652 id_str ? id_str : "........................................",
1653 ncommits_str) == -1) {
1654 err = got_error_from_errno("asprintf");
1655 header = NULL;
1656 goto done;
1658 err = format_line(&wline, &width, header, view->ncols, 0);
1659 if (err)
1660 goto done;
1662 werase(view->window);
1664 if (view_needs_focus_indication(view))
1665 wstandout(view->window);
1666 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1667 if (tc)
1668 wattr_on(view->window,
1669 COLOR_PAIR(tc->colorpair), NULL);
1670 waddwstr(view->window, wline);
1671 if (tc)
1672 wattr_off(view->window,
1673 COLOR_PAIR(tc->colorpair), NULL);
1674 while (width < view->ncols) {
1675 waddch(view->window, ' ');
1676 width++;
1678 if (view_needs_focus_indication(view))
1679 wstandend(view->window);
1680 free(wline);
1681 if (limit <= 1)
1682 goto done;
1684 /* Grow author column size if necessary. */
1685 entry = s->first_displayed_entry;
1686 ncommits = 0;
1687 while (entry) {
1688 char *author;
1689 wchar_t *wauthor;
1690 int width;
1691 if (ncommits >= limit - 1)
1692 break;
1693 author = strdup(got_object_commit_get_author(entry->commit));
1694 if (author == NULL) {
1695 err = got_error_from_errno("strdup");
1696 goto done;
1698 err = format_author(&wauthor, &width, author, COLS,
1699 date_display_cols);
1700 if (author_cols < width)
1701 author_cols = width;
1702 free(wauthor);
1703 free(author);
1704 ncommits++;
1705 entry = TAILQ_NEXT(entry, entry);
1708 entry = s->first_displayed_entry;
1709 s->last_displayed_entry = s->first_displayed_entry;
1710 ncommits = 0;
1711 while (entry) {
1712 if (ncommits >= limit - 1)
1713 break;
1714 if (ncommits == s->selected)
1715 wstandout(view->window);
1716 err = draw_commit(view, entry->commit, entry->id,
1717 date_display_cols, author_cols);
1718 if (ncommits == s->selected)
1719 wstandend(view->window);
1720 if (err)
1721 goto done;
1722 ncommits++;
1723 s->last_displayed_entry = entry;
1724 entry = TAILQ_NEXT(entry, entry);
1727 view_vborder(view);
1728 done:
1729 free(id_str);
1730 free(refs_str);
1731 free(ncommits_str);
1732 free(header);
1733 return err;
1736 static void
1737 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1739 struct commit_queue_entry *entry;
1740 int nscrolled = 0;
1742 entry = TAILQ_FIRST(&s->commits.head);
1743 if (s->first_displayed_entry == entry)
1744 return;
1746 entry = s->first_displayed_entry;
1747 while (entry && nscrolled < maxscroll) {
1748 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1749 if (entry) {
1750 s->first_displayed_entry = entry;
1751 nscrolled++;
1756 static const struct got_error *
1757 trigger_log_thread(struct tog_view *view, int wait)
1759 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1760 int errcode;
1762 halfdelay(1); /* fast refresh while loading commits */
1764 while (ta->commits_needed > 0 || ta->load_all) {
1765 if (ta->log_complete)
1766 break;
1768 /* Wake the log thread. */
1769 errcode = pthread_cond_signal(&ta->need_commits);
1770 if (errcode)
1771 return got_error_set_errno(errcode,
1772 "pthread_cond_signal");
1775 * The mutex will be released while the view loop waits
1776 * in wgetch(), at which time the log thread will run.
1778 if (!wait)
1779 break;
1781 /* Display progress update in log view. */
1782 show_log_view(view);
1783 update_panels();
1784 doupdate();
1786 /* Wait right here while next commit is being loaded. */
1787 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1788 if (errcode)
1789 return got_error_set_errno(errcode,
1790 "pthread_cond_wait");
1792 /* Display progress update in log view. */
1793 show_log_view(view);
1794 update_panels();
1795 doupdate();
1798 return NULL;
1801 static const struct got_error *
1802 log_scroll_down(struct tog_view *view, int maxscroll)
1804 struct tog_log_view_state *s = &view->state.log;
1805 const struct got_error *err = NULL;
1806 struct commit_queue_entry *pentry;
1807 int nscrolled = 0, ncommits_needed;
1809 if (s->last_displayed_entry == NULL)
1810 return NULL;
1812 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1813 if (s->commits.ncommits < ncommits_needed &&
1814 !s->thread_args.log_complete) {
1816 * Ask the log thread for required amount of commits.
1818 s->thread_args.commits_needed += maxscroll;
1819 err = trigger_log_thread(view, 1);
1820 if (err)
1821 return err;
1824 do {
1825 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1826 if (pentry == NULL)
1827 break;
1829 s->last_displayed_entry = pentry;
1831 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1832 if (pentry == NULL)
1833 break;
1834 s->first_displayed_entry = pentry;
1835 } while (++nscrolled < maxscroll);
1837 return err;
1840 static const struct got_error *
1841 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1842 struct got_commit_object *commit, struct got_object_id *commit_id,
1843 struct tog_view *log_view, struct got_repository *repo)
1845 const struct got_error *err;
1846 struct got_object_qid *parent_id;
1847 struct tog_view *diff_view;
1849 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1850 if (diff_view == NULL)
1851 return got_error_from_errno("view_open");
1853 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1854 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1855 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1856 if (err == NULL)
1857 *new_view = diff_view;
1858 return err;
1861 static const struct got_error *
1862 tree_view_visit_subtree(struct tog_tree_view_state *s,
1863 struct got_tree_object *subtree)
1865 struct tog_parent_tree *parent;
1867 parent = calloc(1, sizeof(*parent));
1868 if (parent == NULL)
1869 return got_error_from_errno("calloc");
1871 parent->tree = s->tree;
1872 parent->first_displayed_entry = s->first_displayed_entry;
1873 parent->selected_entry = s->selected_entry;
1874 parent->selected = s->selected;
1875 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1876 s->tree = subtree;
1877 s->selected = 0;
1878 s->first_displayed_entry = NULL;
1879 return NULL;
1882 static const struct got_error *
1883 tree_view_walk_path(struct tog_tree_view_state *s,
1884 struct got_object_id *commit_id, const char *path)
1886 const struct got_error *err = NULL;
1887 struct got_tree_object *tree = NULL;
1888 const char *p;
1889 char *slash, *subpath = NULL;
1891 /* Walk the path and open corresponding tree objects. */
1892 p = path;
1893 while (*p) {
1894 struct got_tree_entry *te;
1895 struct got_object_id *tree_id;
1896 char *te_name;
1898 while (p[0] == '/')
1899 p++;
1901 /* Ensure the correct subtree entry is selected. */
1902 slash = strchr(p, '/');
1903 if (slash == NULL)
1904 te_name = strdup(p);
1905 else
1906 te_name = strndup(p, slash - p);
1907 if (te_name == NULL) {
1908 err = got_error_from_errno("strndup");
1909 break;
1911 te = got_object_tree_find_entry(s->tree, te_name);
1912 if (te == NULL) {
1913 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1914 free(te_name);
1915 break;
1917 free(te_name);
1918 s->first_displayed_entry = s->selected_entry = te;
1920 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1921 break; /* jump to this file's entry */
1923 slash = strchr(p, '/');
1924 if (slash)
1925 subpath = strndup(path, slash - path);
1926 else
1927 subpath = strdup(path);
1928 if (subpath == NULL) {
1929 err = got_error_from_errno("strdup");
1930 break;
1933 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1934 subpath);
1935 if (err)
1936 break;
1938 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1939 free(tree_id);
1940 if (err)
1941 break;
1943 err = tree_view_visit_subtree(s, tree);
1944 if (err) {
1945 got_object_tree_close(tree);
1946 break;
1948 if (slash == NULL)
1949 break;
1950 free(subpath);
1951 subpath = NULL;
1952 p = slash;
1955 free(subpath);
1956 return err;
1959 static const struct got_error *
1960 browse_commit_tree(struct tog_view **new_view, int begin_x,
1961 struct commit_queue_entry *entry, const char *path,
1962 const char *head_ref_name, struct got_repository *repo)
1964 const struct got_error *err = NULL;
1965 struct tog_tree_view_state *s;
1966 struct tog_view *tree_view;
1968 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1969 if (tree_view == NULL)
1970 return got_error_from_errno("view_open");
1972 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1973 if (err)
1974 return err;
1975 s = &tree_view->state.tree;
1977 *new_view = tree_view;
1979 if (got_path_is_root_dir(path))
1980 return NULL;
1982 return tree_view_walk_path(s, entry->id, path);
1985 static const struct got_error *
1986 block_signals_used_by_main_thread(void)
1988 sigset_t sigset;
1989 int errcode;
1991 if (sigemptyset(&sigset) == -1)
1992 return got_error_from_errno("sigemptyset");
1994 /* tog handles SIGWINCH and SIGCONT */
1995 if (sigaddset(&sigset, SIGWINCH) == -1)
1996 return got_error_from_errno("sigaddset");
1997 if (sigaddset(&sigset, SIGCONT) == -1)
1998 return got_error_from_errno("sigaddset");
2000 /* ncurses handles SIGTSTP */
2001 if (sigaddset(&sigset, SIGTSTP) == -1)
2002 return got_error_from_errno("sigaddset");
2004 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2005 if (errcode)
2006 return got_error_set_errno(errcode, "pthread_sigmask");
2008 return NULL;
2011 static void *
2012 log_thread(void *arg)
2014 const struct got_error *err = NULL;
2015 int errcode = 0;
2016 struct tog_log_thread_args *a = arg;
2017 int done = 0;
2019 err = block_signals_used_by_main_thread();
2020 if (err)
2021 return (void *)err;
2023 while (!done && !err && !tog_sigpipe_received) {
2024 err = queue_commits(a);
2025 if (err) {
2026 if (err->code != GOT_ERR_ITER_COMPLETED)
2027 return (void *)err;
2028 err = NULL;
2029 done = 1;
2030 } else if (a->commits_needed > 0 && !a->load_all)
2031 a->commits_needed--;
2033 errcode = pthread_mutex_lock(&tog_mutex);
2034 if (errcode) {
2035 err = got_error_set_errno(errcode,
2036 "pthread_mutex_lock");
2037 break;
2038 } else if (*a->quit)
2039 done = 1;
2040 else if (*a->first_displayed_entry == NULL) {
2041 *a->first_displayed_entry =
2042 TAILQ_FIRST(&a->commits->head);
2043 *a->selected_entry = *a->first_displayed_entry;
2046 errcode = pthread_cond_signal(&a->commit_loaded);
2047 if (errcode) {
2048 err = got_error_set_errno(errcode,
2049 "pthread_cond_signal");
2050 pthread_mutex_unlock(&tog_mutex);
2051 break;
2054 if (done)
2055 a->commits_needed = 0;
2056 else {
2057 if (a->commits_needed == 0 && !a->load_all) {
2058 errcode = pthread_cond_wait(&a->need_commits,
2059 &tog_mutex);
2060 if (errcode)
2061 err = got_error_set_errno(errcode,
2062 "pthread_cond_wait");
2063 if (*a->quit)
2064 done = 1;
2068 errcode = pthread_mutex_unlock(&tog_mutex);
2069 if (errcode && err == NULL)
2070 err = got_error_set_errno(errcode,
2071 "pthread_mutex_unlock");
2073 a->log_complete = 1;
2074 return (void *)err;
2077 static const struct got_error *
2078 stop_log_thread(struct tog_log_view_state *s)
2080 const struct got_error *err = NULL;
2081 int errcode;
2083 if (s->thread) {
2084 s->quit = 1;
2085 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2086 if (errcode)
2087 return got_error_set_errno(errcode,
2088 "pthread_cond_signal");
2089 errcode = pthread_mutex_unlock(&tog_mutex);
2090 if (errcode)
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_unlock");
2093 errcode = pthread_join(s->thread, (void **)&err);
2094 if (errcode)
2095 return got_error_set_errno(errcode, "pthread_join");
2096 errcode = pthread_mutex_lock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_lock");
2100 s->thread = NULL;
2103 if (s->thread_args.repo) {
2104 err = got_repo_close(s->thread_args.repo);
2105 s->thread_args.repo = NULL;
2108 if (s->thread_args.graph) {
2109 got_commit_graph_close(s->thread_args.graph);
2110 s->thread_args.graph = NULL;
2113 return err;
2116 static const struct got_error *
2117 close_log_view(struct tog_view *view)
2119 const struct got_error *err = NULL;
2120 struct tog_log_view_state *s = &view->state.log;
2121 int errcode;
2123 err = stop_log_thread(s);
2125 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2129 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2130 if (errcode && err == NULL)
2131 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2133 free_commits(&s->commits);
2134 free(s->in_repo_path);
2135 s->in_repo_path = NULL;
2136 free(s->start_id);
2137 s->start_id = NULL;
2138 free(s->head_ref_name);
2139 s->head_ref_name = NULL;
2140 return err;
2143 static const struct got_error *
2144 search_start_log_view(struct tog_view *view)
2146 struct tog_log_view_state *s = &view->state.log;
2148 s->matched_entry = NULL;
2149 s->search_entry = NULL;
2150 return NULL;
2153 static const struct got_error *
2154 search_next_log_view(struct tog_view *view)
2156 const struct got_error *err = NULL;
2157 struct tog_log_view_state *s = &view->state.log;
2158 struct commit_queue_entry *entry;
2160 /* Display progress update in log view. */
2161 show_log_view(view);
2162 update_panels();
2163 doupdate();
2165 if (s->search_entry) {
2166 int errcode, ch;
2167 errcode = pthread_mutex_unlock(&tog_mutex);
2168 if (errcode)
2169 return got_error_set_errno(errcode,
2170 "pthread_mutex_unlock");
2171 ch = wgetch(view->window);
2172 errcode = pthread_mutex_lock(&tog_mutex);
2173 if (errcode)
2174 return got_error_set_errno(errcode,
2175 "pthread_mutex_lock");
2176 if (ch == KEY_BACKSPACE) {
2177 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2178 return NULL;
2180 if (view->searching == TOG_SEARCH_FORWARD)
2181 entry = TAILQ_NEXT(s->search_entry, entry);
2182 else
2183 entry = TAILQ_PREV(s->search_entry,
2184 commit_queue_head, entry);
2185 } else if (s->matched_entry) {
2186 if (view->searching == TOG_SEARCH_FORWARD)
2187 entry = TAILQ_NEXT(s->matched_entry, entry);
2188 else
2189 entry = TAILQ_PREV(s->matched_entry,
2190 commit_queue_head, entry);
2191 } else {
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_FIRST(&s->commits.head);
2194 else
2195 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2198 while (1) {
2199 int have_match = 0;
2201 if (entry == NULL) {
2202 if (s->thread_args.log_complete ||
2203 view->searching == TOG_SEARCH_BACKWARD) {
2204 view->search_next_done =
2205 (s->matched_entry == NULL ?
2206 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2207 s->search_entry = NULL;
2208 return NULL;
2211 * Poke the log thread for more commits and return,
2212 * allowing the main loop to make progress. Search
2213 * will resume at s->search_entry once we come back.
2215 s->thread_args.commits_needed++;
2216 return trigger_log_thread(view, 0);
2219 err = match_commit(&have_match, entry->id, entry->commit,
2220 &view->regex);
2221 if (err)
2222 break;
2223 if (have_match) {
2224 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2225 s->matched_entry = entry;
2226 break;
2229 s->search_entry = entry;
2230 if (view->searching == TOG_SEARCH_FORWARD)
2231 entry = TAILQ_NEXT(entry, entry);
2232 else
2233 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2236 if (s->matched_entry) {
2237 int cur = s->selected_entry->idx;
2238 while (cur < s->matched_entry->idx) {
2239 err = input_log_view(NULL, view, KEY_DOWN);
2240 if (err)
2241 return err;
2242 cur++;
2244 while (cur > s->matched_entry->idx) {
2245 err = input_log_view(NULL, view, KEY_UP);
2246 if (err)
2247 return err;
2248 cur--;
2252 s->search_entry = NULL;
2254 return NULL;
2257 static const struct got_error *
2258 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2259 struct got_repository *repo, const char *head_ref_name,
2260 const char *in_repo_path, int log_branches)
2262 const struct got_error *err = NULL;
2263 struct tog_log_view_state *s = &view->state.log;
2264 struct got_repository *thread_repo = NULL;
2265 struct got_commit_graph *thread_graph = NULL;
2266 int errcode;
2268 if (in_repo_path != s->in_repo_path) {
2269 free(s->in_repo_path);
2270 s->in_repo_path = strdup(in_repo_path);
2271 if (s->in_repo_path == NULL)
2272 return got_error_from_errno("strdup");
2275 /* The commit queue only contains commits being displayed. */
2276 TAILQ_INIT(&s->commits.head);
2277 s->commits.ncommits = 0;
2279 s->repo = repo;
2280 if (head_ref_name) {
2281 s->head_ref_name = strdup(head_ref_name);
2282 if (s->head_ref_name == NULL) {
2283 err = got_error_from_errno("strdup");
2284 goto done;
2287 s->start_id = got_object_id_dup(start_id);
2288 if (s->start_id == NULL) {
2289 err = got_error_from_errno("got_object_id_dup");
2290 goto done;
2292 s->log_branches = log_branches;
2294 STAILQ_INIT(&s->colors);
2295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2296 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2297 get_color_value("TOG_COLOR_COMMIT"));
2298 if (err)
2299 goto done;
2300 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2301 get_color_value("TOG_COLOR_AUTHOR"));
2302 if (err) {
2303 free_colors(&s->colors);
2304 goto done;
2306 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2307 get_color_value("TOG_COLOR_DATE"));
2308 if (err) {
2309 free_colors(&s->colors);
2310 goto done;
2314 view->show = show_log_view;
2315 view->input = input_log_view;
2316 view->close = close_log_view;
2317 view->search_start = search_start_log_view;
2318 view->search_next = search_next_log_view;
2320 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2321 if (err)
2322 goto done;
2323 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2324 !s->log_branches);
2325 if (err)
2326 goto done;
2327 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2328 s->repo, NULL, NULL);
2329 if (err)
2330 goto done;
2332 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2333 if (errcode) {
2334 err = got_error_set_errno(errcode, "pthread_cond_init");
2335 goto done;
2337 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2338 if (errcode) {
2339 err = got_error_set_errno(errcode, "pthread_cond_init");
2340 goto done;
2343 s->thread_args.commits_needed = view->nlines;
2344 s->thread_args.graph = thread_graph;
2345 s->thread_args.commits = &s->commits;
2346 s->thread_args.in_repo_path = s->in_repo_path;
2347 s->thread_args.start_id = s->start_id;
2348 s->thread_args.repo = thread_repo;
2349 s->thread_args.log_complete = 0;
2350 s->thread_args.quit = &s->quit;
2351 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2352 s->thread_args.selected_entry = &s->selected_entry;
2353 s->thread_args.searching = &view->searching;
2354 s->thread_args.search_next_done = &view->search_next_done;
2355 s->thread_args.regex = &view->regex;
2356 done:
2357 if (err)
2358 close_log_view(view);
2359 return err;
2362 static const struct got_error *
2363 show_log_view(struct tog_view *view)
2365 const struct got_error *err;
2366 struct tog_log_view_state *s = &view->state.log;
2368 if (s->thread == NULL) {
2369 int errcode = pthread_create(&s->thread, NULL, log_thread,
2370 &s->thread_args);
2371 if (errcode)
2372 return got_error_set_errno(errcode, "pthread_create");
2373 if (s->thread_args.commits_needed > 0) {
2374 err = trigger_log_thread(view, 1);
2375 if (err)
2376 return err;
2380 return draw_commits(view);
2383 static const struct got_error *
2384 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2386 const struct got_error *err = NULL;
2387 struct tog_log_view_state *s = &view->state.log;
2388 struct tog_view *diff_view = NULL, *tree_view = NULL;
2389 struct tog_view *ref_view = NULL;
2390 struct commit_queue_entry *entry;
2391 int begin_x = 0, n;
2393 if (s->thread_args.load_all) {
2394 if (ch == KEY_BACKSPACE)
2395 s->thread_args.load_all = 0;
2396 else if (s->thread_args.log_complete) {
2397 s->thread_args.load_all = 0;
2398 log_scroll_down(view, s->commits.ncommits);
2399 s->selected = MIN(view->nlines - 2,
2400 s->commits.ncommits - 1);
2401 select_commit(s);
2403 return NULL;
2406 switch (ch) {
2407 case 'q':
2408 s->quit = 1;
2409 break;
2410 case 'k':
2411 case KEY_UP:
2412 case '<':
2413 case ',':
2414 if (s->first_displayed_entry == NULL)
2415 break;
2416 if (s->selected > 0)
2417 s->selected--;
2418 else
2419 log_scroll_up(s, 1);
2420 select_commit(s);
2421 break;
2422 case 'g':
2423 case KEY_HOME:
2424 s->selected = 0;
2425 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2426 select_commit(s);
2427 break;
2428 case KEY_PPAGE:
2429 case CTRL('b'):
2430 if (s->first_displayed_entry == NULL)
2431 break;
2432 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2433 s->selected = 0;
2434 else
2435 log_scroll_up(s, view->nlines - 1);
2436 select_commit(s);
2437 break;
2438 case 'j':
2439 case KEY_DOWN:
2440 case '>':
2441 case '.':
2442 if (s->first_displayed_entry == NULL)
2443 break;
2444 if (s->selected < MIN(view->nlines - 2,
2445 s->commits.ncommits - 1))
2446 s->selected++;
2447 else {
2448 err = log_scroll_down(view, 1);
2449 if (err)
2450 break;
2452 select_commit(s);
2453 break;
2454 case 'G':
2455 case KEY_END: {
2456 /* We don't know yet how many commits, so we're forced to
2457 * traverse them all. */
2458 if (!s->thread_args.log_complete) {
2459 s->thread_args.load_all = 1;
2460 return trigger_log_thread(view, 0);
2463 s->selected = 0;
2464 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2465 for (n = 0; n < view->nlines - 1; n++) {
2466 if (entry == NULL)
2467 break;
2468 s->first_displayed_entry = entry;
2469 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2471 if (n > 0)
2472 s->selected = n - 1;
2473 select_commit(s);
2474 break;
2476 case KEY_NPAGE:
2477 case CTRL('f'): {
2478 struct commit_queue_entry *first;
2479 first = s->first_displayed_entry;
2480 if (first == NULL)
2481 break;
2482 err = log_scroll_down(view, view->nlines - 1);
2483 if (err)
2484 break;
2485 if (first == s->first_displayed_entry &&
2486 s->selected < MIN(view->nlines - 2,
2487 s->commits.ncommits - 1)) {
2488 /* can't scroll further down */
2489 s->selected = MIN(view->nlines - 2,
2490 s->commits.ncommits - 1);
2492 select_commit(s);
2493 break;
2495 case KEY_RESIZE:
2496 if (s->selected > view->nlines - 2)
2497 s->selected = view->nlines - 2;
2498 if (s->selected > s->commits.ncommits - 1)
2499 s->selected = s->commits.ncommits - 1;
2500 select_commit(s);
2501 if (s->commits.ncommits < view->nlines - 1 &&
2502 !s->thread_args.log_complete) {
2503 s->thread_args.commits_needed += (view->nlines - 1) -
2504 s->commits.ncommits;
2505 err = trigger_log_thread(view, 1);
2507 break;
2508 case KEY_ENTER:
2509 case ' ':
2510 case '\r':
2511 if (s->selected_entry == NULL)
2512 break;
2513 if (view_is_parent_view(view))
2514 begin_x = view_split_begin_x(view->begin_x);
2515 err = open_diff_view_for_commit(&diff_view, begin_x,
2516 s->selected_entry->commit, s->selected_entry->id,
2517 view, s->repo);
2518 if (err)
2519 break;
2520 view->focussed = 0;
2521 diff_view->focussed = 1;
2522 if (view_is_parent_view(view)) {
2523 err = view_close_child(view);
2524 if (err)
2525 return err;
2526 view_set_child(view, diff_view);
2527 view->focus_child = 1;
2528 } else
2529 *new_view = diff_view;
2530 break;
2531 case 't':
2532 if (s->selected_entry == NULL)
2533 break;
2534 if (view_is_parent_view(view))
2535 begin_x = view_split_begin_x(view->begin_x);
2536 err = browse_commit_tree(&tree_view, begin_x,
2537 s->selected_entry, s->in_repo_path, s->head_ref_name,
2538 s->repo);
2539 if (err)
2540 break;
2541 view->focussed = 0;
2542 tree_view->focussed = 1;
2543 if (view_is_parent_view(view)) {
2544 err = view_close_child(view);
2545 if (err)
2546 return err;
2547 view_set_child(view, tree_view);
2548 view->focus_child = 1;
2549 } else
2550 *new_view = tree_view;
2551 break;
2552 case KEY_BACKSPACE:
2553 case CTRL('l'):
2554 case 'B':
2555 if (ch == KEY_BACKSPACE &&
2556 got_path_is_root_dir(s->in_repo_path))
2557 break;
2558 err = stop_log_thread(s);
2559 if (err)
2560 return err;
2561 if (ch == KEY_BACKSPACE) {
2562 char *parent_path;
2563 err = got_path_dirname(&parent_path, s->in_repo_path);
2564 if (err)
2565 return err;
2566 free(s->in_repo_path);
2567 s->in_repo_path = parent_path;
2568 s->thread_args.in_repo_path = s->in_repo_path;
2569 } else if (ch == CTRL('l')) {
2570 struct got_object_id *start_id;
2571 err = got_repo_match_object_id(&start_id, NULL,
2572 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2573 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2574 if (err)
2575 return err;
2576 free(s->start_id);
2577 s->start_id = start_id;
2578 s->thread_args.start_id = s->start_id;
2579 } else /* 'B' */
2580 s->log_branches = !s->log_branches;
2582 err = got_repo_open(&s->thread_args.repo,
2583 got_repo_get_path(s->repo), NULL);
2584 if (err)
2585 return err;
2586 tog_free_refs();
2587 err = tog_load_refs(s->repo);
2588 if (err)
2589 return err;
2590 err = got_commit_graph_open(&s->thread_args.graph,
2591 s->in_repo_path, !s->log_branches);
2592 if (err)
2593 return err;
2594 err = got_commit_graph_iter_start(s->thread_args.graph,
2595 s->start_id, s->repo, NULL, NULL);
2596 if (err)
2597 return err;
2598 free_commits(&s->commits);
2599 s->first_displayed_entry = NULL;
2600 s->last_displayed_entry = NULL;
2601 s->selected_entry = NULL;
2602 s->selected = 0;
2603 s->thread_args.log_complete = 0;
2604 s->quit = 0;
2605 s->thread_args.commits_needed = view->nlines;
2606 break;
2607 case 'r':
2608 if (view_is_parent_view(view))
2609 begin_x = view_split_begin_x(view->begin_x);
2610 ref_view = view_open(view->nlines, view->ncols,
2611 view->begin_y, begin_x, TOG_VIEW_REF);
2612 if (ref_view == NULL)
2613 return got_error_from_errno("view_open");
2614 err = open_ref_view(ref_view, s->repo);
2615 if (err) {
2616 view_close(ref_view);
2617 return err;
2619 view->focussed = 0;
2620 ref_view->focussed = 1;
2621 if (view_is_parent_view(view)) {
2622 err = view_close_child(view);
2623 if (err)
2624 return err;
2625 view_set_child(view, ref_view);
2626 view->focus_child = 1;
2627 } else
2628 *new_view = ref_view;
2629 break;
2630 default:
2631 break;
2634 return err;
2637 static const struct got_error *
2638 apply_unveil(const char *repo_path, const char *worktree_path)
2640 const struct got_error *error;
2642 #ifdef PROFILE
2643 if (unveil("gmon.out", "rwc") != 0)
2644 return got_error_from_errno2("unveil", "gmon.out");
2645 #endif
2646 if (repo_path && unveil(repo_path, "r") != 0)
2647 return got_error_from_errno2("unveil", repo_path);
2649 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2650 return got_error_from_errno2("unveil", worktree_path);
2652 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2653 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2655 error = got_privsep_unveil_exec_helpers();
2656 if (error != NULL)
2657 return error;
2659 if (unveil(NULL, NULL) != 0)
2660 return got_error_from_errno("unveil");
2662 return NULL;
2665 static void
2666 init_curses(void)
2668 initscr();
2669 cbreak();
2670 halfdelay(1); /* Do fast refresh while initial view is loading. */
2671 noecho();
2672 nonl();
2673 intrflush(stdscr, FALSE);
2674 keypad(stdscr, TRUE);
2675 curs_set(0);
2676 if (getenv("TOG_COLORS") != NULL) {
2677 start_color();
2678 use_default_colors();
2680 signal(SIGWINCH, tog_sigwinch);
2681 signal(SIGPIPE, tog_sigpipe);
2682 signal(SIGCONT, tog_sigcont);
2685 static const struct got_error *
2686 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2687 struct got_repository *repo, struct got_worktree *worktree)
2689 const struct got_error *err = NULL;
2691 if (argc == 0) {
2692 *in_repo_path = strdup("/");
2693 if (*in_repo_path == NULL)
2694 return got_error_from_errno("strdup");
2695 return NULL;
2698 if (worktree) {
2699 const char *prefix = got_worktree_get_path_prefix(worktree);
2700 char *p;
2702 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2703 if (err)
2704 return err;
2705 if (asprintf(in_repo_path, "%s%s%s", prefix,
2706 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2707 p) == -1) {
2708 err = got_error_from_errno("asprintf");
2709 *in_repo_path = NULL;
2711 free(p);
2712 } else
2713 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2715 return err;
2718 static const struct got_error *
2719 cmd_log(int argc, char *argv[])
2721 const struct got_error *error;
2722 struct got_repository *repo = NULL;
2723 struct got_worktree *worktree = NULL;
2724 struct got_object_id *start_id = NULL;
2725 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2726 char *start_commit = NULL, *label = NULL;
2727 struct got_reference *ref = NULL;
2728 const char *head_ref_name = NULL;
2729 int ch, log_branches = 0;
2730 struct tog_view *view;
2732 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2733 switch (ch) {
2734 case 'b':
2735 log_branches = 1;
2736 break;
2737 case 'c':
2738 start_commit = optarg;
2739 break;
2740 case 'r':
2741 repo_path = realpath(optarg, NULL);
2742 if (repo_path == NULL)
2743 return got_error_from_errno2("realpath",
2744 optarg);
2745 break;
2746 default:
2747 usage_log();
2748 /* NOTREACHED */
2752 argc -= optind;
2753 argv += optind;
2755 if (argc > 1)
2756 usage_log();
2758 if (repo_path == NULL) {
2759 cwd = getcwd(NULL, 0);
2760 if (cwd == NULL)
2761 return got_error_from_errno("getcwd");
2762 error = got_worktree_open(&worktree, cwd);
2763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2764 goto done;
2765 if (worktree)
2766 repo_path =
2767 strdup(got_worktree_get_repo_path(worktree));
2768 else
2769 repo_path = strdup(cwd);
2770 if (repo_path == NULL) {
2771 error = got_error_from_errno("strdup");
2772 goto done;
2776 error = got_repo_open(&repo, repo_path, NULL);
2777 if (error != NULL)
2778 goto done;
2780 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2781 repo, worktree);
2782 if (error)
2783 goto done;
2785 init_curses();
2787 error = apply_unveil(got_repo_get_path(repo),
2788 worktree ? got_worktree_get_root_path(worktree) : NULL);
2789 if (error)
2790 goto done;
2792 /* already loaded by tog_log_with_path()? */
2793 if (TAILQ_EMPTY(&tog_refs)) {
2794 error = tog_load_refs(repo);
2795 if (error)
2796 goto done;
2799 if (start_commit == NULL) {
2800 error = got_repo_match_object_id(&start_id, &label,
2801 worktree ? got_worktree_get_head_ref_name(worktree) :
2802 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2803 if (error)
2804 goto done;
2805 head_ref_name = label;
2806 } else {
2807 error = got_ref_open(&ref, repo, start_commit, 0);
2808 if (error == NULL)
2809 head_ref_name = got_ref_get_name(ref);
2810 else if (error->code != GOT_ERR_NOT_REF)
2811 goto done;
2812 error = got_repo_match_object_id(&start_id, NULL,
2813 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2814 if (error)
2815 goto done;
2818 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2819 if (view == NULL) {
2820 error = got_error_from_errno("view_open");
2821 goto done;
2823 error = open_log_view(view, start_id, repo, head_ref_name,
2824 in_repo_path, log_branches);
2825 if (error)
2826 goto done;
2827 if (worktree) {
2828 /* Release work tree lock. */
2829 got_worktree_close(worktree);
2830 worktree = NULL;
2832 error = view_loop(view);
2833 done:
2834 free(in_repo_path);
2835 free(repo_path);
2836 free(cwd);
2837 free(start_id);
2838 free(label);
2839 if (ref)
2840 got_ref_close(ref);
2841 if (repo) {
2842 const struct got_error *close_err = got_repo_close(repo);
2843 if (error == NULL)
2844 error = close_err;
2846 if (worktree)
2847 got_worktree_close(worktree);
2848 tog_free_refs();
2849 return error;
2852 __dead static void
2853 usage_diff(void)
2855 endwin();
2856 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2857 "[-w] object1 object2\n", getprogname());
2858 exit(1);
2861 static int
2862 match_line(const char *line, regex_t *regex, size_t nmatch,
2863 regmatch_t *regmatch)
2865 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2868 struct tog_color *
2869 match_color(struct tog_colors *colors, const char *line)
2871 struct tog_color *tc = NULL;
2873 STAILQ_FOREACH(tc, colors, entry) {
2874 if (match_line(line, &tc->regex, 0, NULL))
2875 return tc;
2878 return NULL;
2881 static const struct got_error *
2882 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2883 WINDOW *window, regmatch_t *regmatch)
2885 const struct got_error *err = NULL;
2886 wchar_t *wline;
2887 int width;
2888 char *s;
2890 *wtotal = 0;
2892 s = strndup(line, regmatch->rm_so);
2893 if (s == NULL)
2894 return got_error_from_errno("strndup");
2896 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2897 if (err) {
2898 free(s);
2899 return err;
2901 waddwstr(window, wline);
2902 free(wline);
2903 free(s);
2904 wlimit -= width;
2905 *wtotal += width;
2907 if (wlimit > 0) {
2908 s = strndup(line + regmatch->rm_so,
2909 regmatch->rm_eo - regmatch->rm_so);
2910 if (s == NULL) {
2911 err = got_error_from_errno("strndup");
2912 free(s);
2913 return err;
2915 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2916 if (err) {
2917 free(s);
2918 return err;
2920 wattr_on(window, A_STANDOUT, NULL);
2921 waddwstr(window, wline);
2922 wattr_off(window, A_STANDOUT, NULL);
2923 free(wline);
2924 free(s);
2925 wlimit -= width;
2926 *wtotal += width;
2929 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2930 err = format_line(&wline, &width,
2931 line + regmatch->rm_eo, wlimit, col_tab_align);
2932 if (err)
2933 return err;
2934 waddwstr(window, wline);
2935 free(wline);
2936 *wtotal += width;
2939 return NULL;
2942 static const struct got_error *
2943 draw_file(struct tog_view *view, const char *header)
2945 struct tog_diff_view_state *s = &view->state.diff;
2946 regmatch_t *regmatch = &view->regmatch;
2947 const struct got_error *err;
2948 int nprinted = 0;
2949 char *line;
2950 size_t linesize = 0;
2951 ssize_t linelen;
2952 struct tog_color *tc;
2953 wchar_t *wline;
2954 int width;
2955 int max_lines = view->nlines;
2956 int nlines = s->nlines;
2957 off_t line_offset;
2959 line_offset = s->line_offsets[s->first_displayed_line - 1];
2960 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2961 return got_error_from_errno("fseek");
2963 werase(view->window);
2965 if (header) {
2966 if (asprintf(&line, "[%d/%d] %s",
2967 s->first_displayed_line - 1 + s->selected_line, nlines,
2968 header) == -1)
2969 return got_error_from_errno("asprintf");
2970 err = format_line(&wline, &width, line, view->ncols, 0);
2971 free(line);
2972 if (err)
2973 return err;
2975 if (view_needs_focus_indication(view))
2976 wstandout(view->window);
2977 waddwstr(view->window, wline);
2978 free(wline);
2979 wline = NULL;
2980 if (view_needs_focus_indication(view))
2981 wstandend(view->window);
2982 if (width <= view->ncols - 1)
2983 waddch(view->window, '\n');
2985 if (max_lines <= 1)
2986 return NULL;
2987 max_lines--;
2990 s->eof = 0;
2991 line = NULL;
2992 while (max_lines > 0 && nprinted < max_lines) {
2993 linelen = getline(&line, &linesize, s->f);
2994 if (linelen == -1) {
2995 if (feof(s->f)) {
2996 s->eof = 1;
2997 break;
2999 free(line);
3000 return got_ferror(s->f, GOT_ERR_IO);
3003 tc = match_color(&s->colors, line);
3004 if (tc)
3005 wattr_on(view->window,
3006 COLOR_PAIR(tc->colorpair), NULL);
3007 if (s->first_displayed_line + nprinted == s->matched_line &&
3008 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3009 err = add_matched_line(&width, line, view->ncols, 0,
3010 view->window, regmatch);
3011 if (err) {
3012 free(line);
3013 return err;
3015 } else {
3016 err = format_line(&wline, &width, line, view->ncols, 0);
3017 if (err) {
3018 free(line);
3019 return err;
3021 waddwstr(view->window, wline);
3022 free(wline);
3023 wline = NULL;
3025 if (tc)
3026 wattr_off(view->window,
3027 COLOR_PAIR(tc->colorpair), NULL);
3028 if (width <= view->ncols - 1)
3029 waddch(view->window, '\n');
3030 nprinted++;
3032 free(line);
3033 if (nprinted >= 1)
3034 s->last_displayed_line = s->first_displayed_line +
3035 (nprinted - 1);
3036 else
3037 s->last_displayed_line = s->first_displayed_line;
3039 view_vborder(view);
3041 if (s->eof) {
3042 while (nprinted < view->nlines) {
3043 waddch(view->window, '\n');
3044 nprinted++;
3047 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3048 if (err) {
3049 return err;
3052 wstandout(view->window);
3053 waddwstr(view->window, wline);
3054 free(wline);
3055 wline = NULL;
3056 wstandend(view->window);
3059 return NULL;
3062 static char *
3063 get_datestr(time_t *time, char *datebuf)
3065 struct tm mytm, *tm;
3066 char *p, *s;
3068 tm = gmtime_r(time, &mytm);
3069 if (tm == NULL)
3070 return NULL;
3071 s = asctime_r(tm, datebuf);
3072 if (s == NULL)
3073 return NULL;
3074 p = strchr(s, '\n');
3075 if (p)
3076 *p = '\0';
3077 return s;
3080 static const struct got_error *
3081 get_changed_paths(struct got_pathlist_head *paths,
3082 struct got_commit_object *commit, struct got_repository *repo)
3084 const struct got_error *err = NULL;
3085 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3086 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3087 struct got_object_qid *qid;
3089 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3090 if (qid != NULL) {
3091 struct got_commit_object *pcommit;
3092 err = got_object_open_as_commit(&pcommit, repo,
3093 qid->id);
3094 if (err)
3095 return err;
3097 tree_id1 = got_object_id_dup(
3098 got_object_commit_get_tree_id(pcommit));
3099 if (tree_id1 == NULL) {
3100 got_object_commit_close(pcommit);
3101 return got_error_from_errno("got_object_id_dup");
3103 got_object_commit_close(pcommit);
3107 if (tree_id1) {
3108 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3109 if (err)
3110 goto done;
3113 tree_id2 = got_object_commit_get_tree_id(commit);
3114 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3115 if (err)
3116 goto done;
3118 err = got_diff_tree(tree1, tree2, "", "", repo,
3119 got_diff_tree_collect_changed_paths, paths, 0);
3120 done:
3121 if (tree1)
3122 got_object_tree_close(tree1);
3123 if (tree2)
3124 got_object_tree_close(tree2);
3125 free(tree_id1);
3126 return err;
3129 static const struct got_error *
3130 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3132 off_t *p;
3134 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3135 if (p == NULL)
3136 return got_error_from_errno("reallocarray");
3137 *line_offsets = p;
3138 (*line_offsets)[*nlines] = off;
3139 (*nlines)++;
3140 return NULL;
3143 static const struct got_error *
3144 write_commit_info(off_t **line_offsets, size_t *nlines,
3145 struct got_object_id *commit_id, struct got_reflist_head *refs,
3146 struct got_repository *repo, FILE *outfile)
3148 const struct got_error *err = NULL;
3149 char datebuf[26], *datestr;
3150 struct got_commit_object *commit;
3151 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3152 time_t committer_time;
3153 const char *author, *committer;
3154 char *refs_str = NULL;
3155 struct got_pathlist_head changed_paths;
3156 struct got_pathlist_entry *pe;
3157 off_t outoff = 0;
3158 int n;
3160 TAILQ_INIT(&changed_paths);
3162 if (refs) {
3163 err = build_refs_str(&refs_str, refs, commit_id, repo);
3164 if (err)
3165 return err;
3168 err = got_object_open_as_commit(&commit, repo, commit_id);
3169 if (err)
3170 return err;
3172 err = got_object_id_str(&id_str, commit_id);
3173 if (err) {
3174 err = got_error_from_errno("got_object_id_str");
3175 goto done;
3178 err = add_line_offset(line_offsets, nlines, 0);
3179 if (err)
3180 goto done;
3182 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3183 refs_str ? refs_str : "", refs_str ? ")" : "");
3184 if (n < 0) {
3185 err = got_error_from_errno("fprintf");
3186 goto done;
3188 outoff += n;
3189 err = add_line_offset(line_offsets, nlines, outoff);
3190 if (err)
3191 goto done;
3193 n = fprintf(outfile, "from: %s\n",
3194 got_object_commit_get_author(commit));
3195 if (n < 0) {
3196 err = got_error_from_errno("fprintf");
3197 goto done;
3199 outoff += n;
3200 err = add_line_offset(line_offsets, nlines, outoff);
3201 if (err)
3202 goto done;
3204 committer_time = got_object_commit_get_committer_time(commit);
3205 datestr = get_datestr(&committer_time, datebuf);
3206 if (datestr) {
3207 n = fprintf(outfile, "date: %s UTC\n", datestr);
3208 if (n < 0) {
3209 err = got_error_from_errno("fprintf");
3210 goto done;
3212 outoff += n;
3213 err = add_line_offset(line_offsets, nlines, outoff);
3214 if (err)
3215 goto done;
3217 author = got_object_commit_get_author(commit);
3218 committer = got_object_commit_get_committer(commit);
3219 if (strcmp(author, committer) != 0) {
3220 n = fprintf(outfile, "via: %s\n", committer);
3221 if (n < 0) {
3222 err = got_error_from_errno("fprintf");
3223 goto done;
3225 outoff += n;
3226 err = add_line_offset(line_offsets, nlines, outoff);
3227 if (err)
3228 goto done;
3230 if (got_object_commit_get_nparents(commit) > 1) {
3231 const struct got_object_id_queue *parent_ids;
3232 struct got_object_qid *qid;
3233 int pn = 1;
3234 parent_ids = got_object_commit_get_parent_ids(commit);
3235 STAILQ_FOREACH(qid, parent_ids, entry) {
3236 err = got_object_id_str(&id_str, qid->id);
3237 if (err)
3238 goto done;
3239 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3240 if (n < 0) {
3241 err = got_error_from_errno("fprintf");
3242 goto done;
3244 outoff += n;
3245 err = add_line_offset(line_offsets, nlines, outoff);
3246 if (err)
3247 goto done;
3248 free(id_str);
3249 id_str = NULL;
3253 err = got_object_commit_get_logmsg(&logmsg, commit);
3254 if (err)
3255 goto done;
3256 s = logmsg;
3257 while ((line = strsep(&s, "\n")) != NULL) {
3258 n = fprintf(outfile, "%s\n", line);
3259 if (n < 0) {
3260 err = got_error_from_errno("fprintf");
3261 goto done;
3263 outoff += n;
3264 err = add_line_offset(line_offsets, nlines, outoff);
3265 if (err)
3266 goto done;
3269 err = get_changed_paths(&changed_paths, commit, repo);
3270 if (err)
3271 goto done;
3272 TAILQ_FOREACH(pe, &changed_paths, entry) {
3273 struct got_diff_changed_path *cp = pe->data;
3274 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3275 if (n < 0) {
3276 err = got_error_from_errno("fprintf");
3277 goto done;
3279 outoff += n;
3280 err = add_line_offset(line_offsets, nlines, outoff);
3281 if (err)
3282 goto done;
3283 free((char *)pe->path);
3284 free(pe->data);
3287 fputc('\n', outfile);
3288 outoff++;
3289 err = add_line_offset(line_offsets, nlines, outoff);
3290 done:
3291 got_pathlist_free(&changed_paths);
3292 free(id_str);
3293 free(logmsg);
3294 free(refs_str);
3295 got_object_commit_close(commit);
3296 if (err) {
3297 free(*line_offsets);
3298 *line_offsets = NULL;
3299 *nlines = 0;
3301 return err;
3304 static const struct got_error *
3305 create_diff(struct tog_diff_view_state *s)
3307 const struct got_error *err = NULL;
3308 FILE *f = NULL;
3309 int obj_type;
3311 free(s->line_offsets);
3312 s->line_offsets = malloc(sizeof(off_t));
3313 if (s->line_offsets == NULL)
3314 return got_error_from_errno("malloc");
3315 s->nlines = 0;
3317 f = got_opentemp();
3318 if (f == NULL) {
3319 err = got_error_from_errno("got_opentemp");
3320 goto done;
3322 if (s->f && fclose(s->f) == EOF) {
3323 err = got_error_from_errno("fclose");
3324 goto done;
3326 s->f = f;
3328 if (s->id1)
3329 err = got_object_get_type(&obj_type, s->repo, s->id1);
3330 else
3331 err = got_object_get_type(&obj_type, s->repo, s->id2);
3332 if (err)
3333 goto done;
3335 switch (obj_type) {
3336 case GOT_OBJ_TYPE_BLOB:
3337 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3338 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3339 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3340 break;
3341 case GOT_OBJ_TYPE_TREE:
3342 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3343 s->id1, s->id2, "", "", s->diff_context,
3344 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3345 break;
3346 case GOT_OBJ_TYPE_COMMIT: {
3347 const struct got_object_id_queue *parent_ids;
3348 struct got_object_qid *pid;
3349 struct got_commit_object *commit2;
3350 struct got_reflist_head *refs;
3352 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3353 if (err)
3354 goto done;
3355 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3356 /* Show commit info if we're diffing to a parent/root commit. */
3357 if (s->id1 == NULL) {
3358 err = write_commit_info(&s->line_offsets, &s->nlines,
3359 s->id2, refs, s->repo, s->f);
3360 if (err)
3361 goto done;
3362 } else {
3363 parent_ids = got_object_commit_get_parent_ids(commit2);
3364 STAILQ_FOREACH(pid, parent_ids, entry) {
3365 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3366 err = write_commit_info(
3367 &s->line_offsets, &s->nlines,
3368 s->id2, refs, s->repo, s->f);
3369 if (err)
3370 goto done;
3371 break;
3375 got_object_commit_close(commit2);
3377 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3378 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3379 s->force_text_diff, s->repo, s->f);
3380 break;
3382 default:
3383 err = got_error(GOT_ERR_OBJ_TYPE);
3384 break;
3386 if (err)
3387 goto done;
3388 done:
3389 if (s->f && fflush(s->f) != 0 && err == NULL)
3390 err = got_error_from_errno("fflush");
3391 return err;
3394 static void
3395 diff_view_indicate_progress(struct tog_view *view)
3397 mvwaddstr(view->window, 0, 0, "diffing...");
3398 update_panels();
3399 doupdate();
3402 static const struct got_error *
3403 search_start_diff_view(struct tog_view *view)
3405 struct tog_diff_view_state *s = &view->state.diff;
3407 s->matched_line = 0;
3408 return NULL;
3411 static const struct got_error *
3412 search_next_diff_view(struct tog_view *view)
3414 struct tog_diff_view_state *s = &view->state.diff;
3415 int lineno;
3416 char *line = NULL;
3417 size_t linesize = 0;
3418 ssize_t linelen;
3420 if (!view->searching) {
3421 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3422 return NULL;
3425 if (s->matched_line) {
3426 if (view->searching == TOG_SEARCH_FORWARD)
3427 lineno = s->matched_line + 1;
3428 else
3429 lineno = s->matched_line - 1;
3430 } else {
3431 if (view->searching == TOG_SEARCH_FORWARD)
3432 lineno = 1;
3433 else
3434 lineno = s->nlines;
3437 while (1) {
3438 off_t offset;
3440 if (lineno <= 0 || lineno > s->nlines) {
3441 if (s->matched_line == 0) {
3442 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3443 break;
3446 if (view->searching == TOG_SEARCH_FORWARD)
3447 lineno = 1;
3448 else
3449 lineno = s->nlines;
3452 offset = s->line_offsets[lineno - 1];
3453 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3454 free(line);
3455 return got_error_from_errno("fseeko");
3457 linelen = getline(&line, &linesize, s->f);
3458 if (linelen != -1 &&
3459 match_line(line, &view->regex, 1, &view->regmatch)) {
3460 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3461 s->matched_line = lineno;
3462 break;
3464 if (view->searching == TOG_SEARCH_FORWARD)
3465 lineno++;
3466 else
3467 lineno--;
3469 free(line);
3471 if (s->matched_line) {
3472 s->first_displayed_line = s->matched_line;
3473 s->selected_line = 1;
3476 return NULL;
3479 static const struct got_error *
3480 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3481 struct got_object_id *id2, const char *label1, const char *label2,
3482 int diff_context, int ignore_whitespace, int force_text_diff,
3483 struct tog_view *log_view, struct got_repository *repo)
3485 const struct got_error *err;
3486 struct tog_diff_view_state *s = &view->state.diff;
3488 if (id1 != NULL && id2 != NULL) {
3489 int type1, type2;
3490 err = got_object_get_type(&type1, repo, id1);
3491 if (err)
3492 return err;
3493 err = got_object_get_type(&type2, repo, id2);
3494 if (err)
3495 return err;
3497 if (type1 != type2)
3498 return got_error(GOT_ERR_OBJ_TYPE);
3500 s->first_displayed_line = 1;
3501 s->last_displayed_line = view->nlines;
3502 s->selected_line = 1;
3503 s->repo = repo;
3504 s->id1 = id1;
3505 s->id2 = id2;
3506 s->label1 = label1;
3507 s->label2 = label2;
3509 if (id1) {
3510 s->id1 = got_object_id_dup(id1);
3511 if (s->id1 == NULL)
3512 return got_error_from_errno("got_object_id_dup");
3513 } else
3514 s->id1 = NULL;
3516 s->id2 = got_object_id_dup(id2);
3517 if (s->id2 == NULL) {
3518 free(s->id1);
3519 s->id1 = NULL;
3520 return got_error_from_errno("got_object_id_dup");
3522 s->f = NULL;
3523 s->first_displayed_line = 1;
3524 s->last_displayed_line = view->nlines;
3525 s->diff_context = diff_context;
3526 s->ignore_whitespace = ignore_whitespace;
3527 s->force_text_diff = force_text_diff;
3528 s->log_view = log_view;
3529 s->repo = repo;
3531 STAILQ_INIT(&s->colors);
3532 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3533 err = add_color(&s->colors,
3534 "^-", TOG_COLOR_DIFF_MINUS,
3535 get_color_value("TOG_COLOR_DIFF_MINUS"));
3536 if (err)
3537 return err;
3538 err = add_color(&s->colors, "^\\+",
3539 TOG_COLOR_DIFF_PLUS,
3540 get_color_value("TOG_COLOR_DIFF_PLUS"));
3541 if (err) {
3542 free_colors(&s->colors);
3543 return err;
3545 err = add_color(&s->colors,
3546 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3547 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3548 if (err) {
3549 free_colors(&s->colors);
3550 return err;
3553 err = add_color(&s->colors,
3554 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3555 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3556 get_color_value("TOG_COLOR_DIFF_META"));
3557 if (err) {
3558 free_colors(&s->colors);
3559 return err;
3562 err = add_color(&s->colors,
3563 "^(from|via): ", TOG_COLOR_AUTHOR,
3564 get_color_value("TOG_COLOR_AUTHOR"));
3565 if (err) {
3566 free_colors(&s->colors);
3567 return err;
3570 err = add_color(&s->colors,
3571 "^date: ", TOG_COLOR_DATE,
3572 get_color_value("TOG_COLOR_DATE"));
3573 if (err) {
3574 free_colors(&s->colors);
3575 return err;
3579 if (log_view && view_is_splitscreen(view))
3580 show_log_view(log_view); /* draw vborder */
3581 diff_view_indicate_progress(view);
3583 s->line_offsets = NULL;
3584 s->nlines = 0;
3585 err = create_diff(s);
3586 if (err) {
3587 free(s->id1);
3588 s->id1 = NULL;
3589 free(s->id2);
3590 s->id2 = NULL;
3591 free_colors(&s->colors);
3592 return err;
3595 view->show = show_diff_view;
3596 view->input = input_diff_view;
3597 view->close = close_diff_view;
3598 view->search_start = search_start_diff_view;
3599 view->search_next = search_next_diff_view;
3601 return NULL;
3604 static const struct got_error *
3605 close_diff_view(struct tog_view *view)
3607 const struct got_error *err = NULL;
3608 struct tog_diff_view_state *s = &view->state.diff;
3610 free(s->id1);
3611 s->id1 = NULL;
3612 free(s->id2);
3613 s->id2 = NULL;
3614 if (s->f && fclose(s->f) == EOF)
3615 err = got_error_from_errno("fclose");
3616 free_colors(&s->colors);
3617 free(s->line_offsets);
3618 s->line_offsets = NULL;
3619 s->nlines = 0;
3620 return err;
3623 static const struct got_error *
3624 show_diff_view(struct tog_view *view)
3626 const struct got_error *err;
3627 struct tog_diff_view_state *s = &view->state.diff;
3628 char *id_str1 = NULL, *id_str2, *header;
3629 const char *label1, *label2;
3631 if (s->id1) {
3632 err = got_object_id_str(&id_str1, s->id1);
3633 if (err)
3634 return err;
3635 label1 = s->label1 ? : id_str1;
3636 } else
3637 label1 = "/dev/null";
3639 err = got_object_id_str(&id_str2, s->id2);
3640 if (err)
3641 return err;
3642 label2 = s->label2 ? : id_str2;
3644 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3645 err = got_error_from_errno("asprintf");
3646 free(id_str1);
3647 free(id_str2);
3648 return err;
3650 free(id_str1);
3651 free(id_str2);
3653 err = draw_file(view, header);
3654 free(header);
3655 return err;
3658 static const struct got_error *
3659 set_selected_commit(struct tog_diff_view_state *s,
3660 struct commit_queue_entry *entry)
3662 const struct got_error *err;
3663 const struct got_object_id_queue *parent_ids;
3664 struct got_commit_object *selected_commit;
3665 struct got_object_qid *pid;
3667 free(s->id2);
3668 s->id2 = got_object_id_dup(entry->id);
3669 if (s->id2 == NULL)
3670 return got_error_from_errno("got_object_id_dup");
3672 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3673 if (err)
3674 return err;
3675 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3676 free(s->id1);
3677 pid = STAILQ_FIRST(parent_ids);
3678 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3679 got_object_commit_close(selected_commit);
3680 return NULL;
3683 static const struct got_error *
3684 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3686 const struct got_error *err = NULL;
3687 struct tog_diff_view_state *s = &view->state.diff;
3688 struct tog_log_view_state *ls;
3689 struct commit_queue_entry *old_selected_entry;
3690 char *line = NULL;
3691 size_t linesize = 0;
3692 ssize_t linelen;
3693 int i;
3695 switch (ch) {
3696 case 'a':
3697 case 'w':
3698 if (ch == 'a')
3699 s->force_text_diff = !s->force_text_diff;
3700 if (ch == 'w')
3701 s->ignore_whitespace = !s->ignore_whitespace;
3702 wclear(view->window);
3703 s->first_displayed_line = 1;
3704 s->last_displayed_line = view->nlines;
3705 diff_view_indicate_progress(view);
3706 err = create_diff(s);
3707 break;
3708 case 'g':
3709 case KEY_HOME:
3710 s->first_displayed_line = 1;
3711 break;
3712 case 'G':
3713 case KEY_END:
3714 if (s->eof)
3715 break;
3717 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3718 s->eof = 1;
3719 break;
3720 case 'k':
3721 case KEY_UP:
3722 if (s->first_displayed_line > 1)
3723 s->first_displayed_line--;
3724 break;
3725 case KEY_PPAGE:
3726 case CTRL('b'):
3727 if (s->first_displayed_line == 1)
3728 break;
3729 i = 0;
3730 while (i++ < view->nlines - 1 &&
3731 s->first_displayed_line > 1)
3732 s->first_displayed_line--;
3733 break;
3734 case 'j':
3735 case KEY_DOWN:
3736 if (!s->eof)
3737 s->first_displayed_line++;
3738 break;
3739 case KEY_NPAGE:
3740 case CTRL('f'):
3741 case ' ':
3742 if (s->eof)
3743 break;
3744 i = 0;
3745 while (!s->eof && i++ < view->nlines - 1) {
3746 linelen = getline(&line, &linesize, s->f);
3747 s->first_displayed_line++;
3748 if (linelen == -1) {
3749 if (feof(s->f)) {
3750 s->eof = 1;
3751 } else
3752 err = got_ferror(s->f, GOT_ERR_IO);
3753 break;
3756 free(line);
3757 break;
3758 case '[':
3759 if (s->diff_context > 0) {
3760 s->diff_context--;
3761 diff_view_indicate_progress(view);
3762 err = create_diff(s);
3763 if (s->first_displayed_line + view->nlines - 1 >
3764 s->nlines) {
3765 s->first_displayed_line = 1;
3766 s->last_displayed_line = view->nlines;
3769 break;
3770 case ']':
3771 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3772 s->diff_context++;
3773 diff_view_indicate_progress(view);
3774 err = create_diff(s);
3776 break;
3777 case '<':
3778 case ',':
3779 if (s->log_view == NULL)
3780 break;
3781 ls = &s->log_view->state.log;
3782 old_selected_entry = ls->selected_entry;
3784 err = input_log_view(NULL, s->log_view, KEY_UP);
3785 if (err)
3786 break;
3788 if (old_selected_entry == ls->selected_entry)
3789 break;
3791 err = set_selected_commit(s, ls->selected_entry);
3792 if (err)
3793 break;
3795 s->first_displayed_line = 1;
3796 s->last_displayed_line = view->nlines;
3798 diff_view_indicate_progress(view);
3799 err = create_diff(s);
3800 break;
3801 case '>':
3802 case '.':
3803 if (s->log_view == NULL)
3804 break;
3805 ls = &s->log_view->state.log;
3806 old_selected_entry = ls->selected_entry;
3808 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3809 if (err)
3810 break;
3812 if (old_selected_entry == ls->selected_entry)
3813 break;
3815 err = set_selected_commit(s, ls->selected_entry);
3816 if (err)
3817 break;
3819 s->first_displayed_line = 1;
3820 s->last_displayed_line = view->nlines;
3822 diff_view_indicate_progress(view);
3823 err = create_diff(s);
3824 break;
3825 default:
3826 break;
3829 return err;
3832 static const struct got_error *
3833 cmd_diff(int argc, char *argv[])
3835 const struct got_error *error = NULL;
3836 struct got_repository *repo = NULL;
3837 struct got_worktree *worktree = NULL;
3838 struct got_object_id *id1 = NULL, *id2 = NULL;
3839 char *repo_path = NULL, *cwd = NULL;
3840 char *id_str1 = NULL, *id_str2 = NULL;
3841 char *label1 = NULL, *label2 = NULL;
3842 int diff_context = 3, ignore_whitespace = 0;
3843 int ch, force_text_diff = 0;
3844 const char *errstr;
3845 struct tog_view *view;
3847 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3848 switch (ch) {
3849 case 'a':
3850 force_text_diff = 1;
3851 break;
3852 case 'C':
3853 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3854 &errstr);
3855 if (errstr != NULL)
3856 err(1, "-C option %s", errstr);
3857 break;
3858 case 'r':
3859 repo_path = realpath(optarg, NULL);
3860 if (repo_path == NULL)
3861 return got_error_from_errno2("realpath",
3862 optarg);
3863 got_path_strip_trailing_slashes(repo_path);
3864 break;
3865 case 'w':
3866 ignore_whitespace = 1;
3867 break;
3868 default:
3869 usage_diff();
3870 /* NOTREACHED */
3874 argc -= optind;
3875 argv += optind;
3877 if (argc == 0) {
3878 usage_diff(); /* TODO show local worktree changes */
3879 } else if (argc == 2) {
3880 id_str1 = argv[0];
3881 id_str2 = argv[1];
3882 } else
3883 usage_diff();
3885 if (repo_path == NULL) {
3886 cwd = getcwd(NULL, 0);
3887 if (cwd == NULL)
3888 return got_error_from_errno("getcwd");
3889 error = got_worktree_open(&worktree, cwd);
3890 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3891 goto done;
3892 if (worktree)
3893 repo_path =
3894 strdup(got_worktree_get_repo_path(worktree));
3895 else
3896 repo_path = strdup(cwd);
3897 if (repo_path == NULL) {
3898 error = got_error_from_errno("strdup");
3899 goto done;
3903 error = got_repo_open(&repo, repo_path, NULL);
3904 if (error)
3905 goto done;
3907 init_curses();
3909 error = apply_unveil(got_repo_get_path(repo), NULL);
3910 if (error)
3911 goto done;
3913 error = tog_load_refs(repo);
3914 if (error)
3915 goto done;
3917 error = got_repo_match_object_id(&id1, &label1, id_str1,
3918 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3919 if (error)
3920 goto done;
3922 error = got_repo_match_object_id(&id2, &label2, id_str2,
3923 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3924 if (error)
3925 goto done;
3927 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3928 if (view == NULL) {
3929 error = got_error_from_errno("view_open");
3930 goto done;
3932 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3933 ignore_whitespace, force_text_diff, NULL, repo);
3934 if (error)
3935 goto done;
3936 error = view_loop(view);
3937 done:
3938 free(label1);
3939 free(label2);
3940 free(repo_path);
3941 free(cwd);
3942 if (repo) {
3943 const struct got_error *close_err = got_repo_close(repo);
3944 if (error == NULL)
3945 error = close_err;
3947 if (worktree)
3948 got_worktree_close(worktree);
3949 tog_free_refs();
3950 return error;
3953 __dead static void
3954 usage_blame(void)
3956 endwin();
3957 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3958 getprogname());
3959 exit(1);
3962 struct tog_blame_line {
3963 int annotated;
3964 struct got_object_id *id;
3967 static const struct got_error *
3968 draw_blame(struct tog_view *view)
3970 struct tog_blame_view_state *s = &view->state.blame;
3971 struct tog_blame *blame = &s->blame;
3972 regmatch_t *regmatch = &view->regmatch;
3973 const struct got_error *err;
3974 int lineno = 0, nprinted = 0;
3975 char *line = NULL;
3976 size_t linesize = 0;
3977 ssize_t linelen;
3978 wchar_t *wline;
3979 int width;
3980 struct tog_blame_line *blame_line;
3981 struct got_object_id *prev_id = NULL;
3982 char *id_str;
3983 struct tog_color *tc;
3985 err = got_object_id_str(&id_str, s->blamed_commit->id);
3986 if (err)
3987 return err;
3989 rewind(blame->f);
3990 werase(view->window);
3992 if (asprintf(&line, "commit %s", id_str) == -1) {
3993 err = got_error_from_errno("asprintf");
3994 free(id_str);
3995 return err;
3998 err = format_line(&wline, &width, line, view->ncols, 0);
3999 free(line);
4000 line = NULL;
4001 if (err)
4002 return err;
4003 if (view_needs_focus_indication(view))
4004 wstandout(view->window);
4005 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4006 if (tc)
4007 wattr_on(view->window,
4008 COLOR_PAIR(tc->colorpair), NULL);
4009 waddwstr(view->window, wline);
4010 if (tc)
4011 wattr_off(view->window,
4012 COLOR_PAIR(tc->colorpair), NULL);
4013 if (view_needs_focus_indication(view))
4014 wstandend(view->window);
4015 free(wline);
4016 wline = NULL;
4017 if (width < view->ncols - 1)
4018 waddch(view->window, '\n');
4020 if (asprintf(&line, "[%d/%d] %s%s",
4021 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4022 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4023 free(id_str);
4024 return got_error_from_errno("asprintf");
4026 free(id_str);
4027 err = format_line(&wline, &width, line, view->ncols, 0);
4028 free(line);
4029 line = NULL;
4030 if (err)
4031 return err;
4032 waddwstr(view->window, wline);
4033 free(wline);
4034 wline = NULL;
4035 if (width < view->ncols - 1)
4036 waddch(view->window, '\n');
4038 s->eof = 0;
4039 while (nprinted < view->nlines - 2) {
4040 linelen = getline(&line, &linesize, blame->f);
4041 if (linelen == -1) {
4042 if (feof(blame->f)) {
4043 s->eof = 1;
4044 break;
4046 free(line);
4047 return got_ferror(blame->f, GOT_ERR_IO);
4049 if (++lineno < s->first_displayed_line)
4050 continue;
4052 if (view->focussed && nprinted == s->selected_line - 1)
4053 wstandout(view->window);
4055 if (blame->nlines > 0) {
4056 blame_line = &blame->lines[lineno - 1];
4057 if (blame_line->annotated && prev_id &&
4058 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4059 !(view->focussed &&
4060 nprinted == s->selected_line - 1)) {
4061 waddstr(view->window, " ");
4062 } else if (blame_line->annotated) {
4063 char *id_str;
4064 err = got_object_id_str(&id_str, blame_line->id);
4065 if (err) {
4066 free(line);
4067 return err;
4069 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4070 if (tc)
4071 wattr_on(view->window,
4072 COLOR_PAIR(tc->colorpair), NULL);
4073 wprintw(view->window, "%.8s", id_str);
4074 if (tc)
4075 wattr_off(view->window,
4076 COLOR_PAIR(tc->colorpair), NULL);
4077 free(id_str);
4078 prev_id = blame_line->id;
4079 } else {
4080 waddstr(view->window, "........");
4081 prev_id = NULL;
4083 } else {
4084 waddstr(view->window, "........");
4085 prev_id = NULL;
4088 if (view->focussed && nprinted == s->selected_line - 1)
4089 wstandend(view->window);
4090 waddstr(view->window, " ");
4092 if (view->ncols <= 9) {
4093 width = 9;
4094 wline = wcsdup(L"");
4095 if (wline == NULL) {
4096 err = got_error_from_errno("wcsdup");
4097 free(line);
4098 return err;
4100 } else if (s->first_displayed_line + nprinted ==
4101 s->matched_line &&
4102 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4103 err = add_matched_line(&width, line, view->ncols - 9, 9,
4104 view->window, regmatch);
4105 if (err) {
4106 free(line);
4107 return err;
4109 width += 9;
4110 } else {
4111 err = format_line(&wline, &width, line,
4112 view->ncols - 9, 9);
4113 waddwstr(view->window, wline);
4114 free(wline);
4115 wline = NULL;
4116 width += 9;
4119 if (width <= view->ncols - 1)
4120 waddch(view->window, '\n');
4121 if (++nprinted == 1)
4122 s->first_displayed_line = lineno;
4124 free(line);
4125 s->last_displayed_line = lineno;
4127 view_vborder(view);
4129 return NULL;
4132 static const struct got_error *
4133 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4135 const struct got_error *err = NULL;
4136 struct tog_blame_cb_args *a = arg;
4137 struct tog_blame_line *line;
4138 int errcode;
4140 if (nlines != a->nlines ||
4141 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4142 return got_error(GOT_ERR_RANGE);
4144 errcode = pthread_mutex_lock(&tog_mutex);
4145 if (errcode)
4146 return got_error_set_errno(errcode, "pthread_mutex_lock");
4148 if (*a->quit) { /* user has quit the blame view */
4149 err = got_error(GOT_ERR_ITER_COMPLETED);
4150 goto done;
4153 if (lineno == -1)
4154 goto done; /* no change in this commit */
4156 line = &a->lines[lineno - 1];
4157 if (line->annotated)
4158 goto done;
4160 line->id = got_object_id_dup(id);
4161 if (line->id == NULL) {
4162 err = got_error_from_errno("got_object_id_dup");
4163 goto done;
4165 line->annotated = 1;
4166 done:
4167 errcode = pthread_mutex_unlock(&tog_mutex);
4168 if (errcode)
4169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4170 return err;
4173 static void *
4174 blame_thread(void *arg)
4176 const struct got_error *err, *close_err;
4177 struct tog_blame_thread_args *ta = arg;
4178 struct tog_blame_cb_args *a = ta->cb_args;
4179 int errcode;
4181 err = block_signals_used_by_main_thread();
4182 if (err)
4183 return (void *)err;
4185 err = got_blame(ta->path, a->commit_id, ta->repo,
4186 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4187 if (err && err->code == GOT_ERR_CANCELLED)
4188 err = NULL;
4190 errcode = pthread_mutex_lock(&tog_mutex);
4191 if (errcode)
4192 return (void *)got_error_set_errno(errcode,
4193 "pthread_mutex_lock");
4195 close_err = got_repo_close(ta->repo);
4196 if (err == NULL)
4197 err = close_err;
4198 ta->repo = NULL;
4199 *ta->complete = 1;
4201 errcode = pthread_mutex_unlock(&tog_mutex);
4202 if (errcode && err == NULL)
4203 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4205 return (void *)err;
4208 static struct got_object_id *
4209 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4210 int first_displayed_line, int selected_line)
4212 struct tog_blame_line *line;
4214 if (nlines <= 0)
4215 return NULL;
4217 line = &lines[first_displayed_line - 1 + selected_line - 1];
4218 if (!line->annotated)
4219 return NULL;
4221 return line->id;
4224 static const struct got_error *
4225 stop_blame(struct tog_blame *blame)
4227 const struct got_error *err = NULL;
4228 int i;
4230 if (blame->thread) {
4231 int errcode;
4232 errcode = pthread_mutex_unlock(&tog_mutex);
4233 if (errcode)
4234 return got_error_set_errno(errcode,
4235 "pthread_mutex_unlock");
4236 errcode = pthread_join(blame->thread, (void **)&err);
4237 if (errcode)
4238 return got_error_set_errno(errcode, "pthread_join");
4239 errcode = pthread_mutex_lock(&tog_mutex);
4240 if (errcode)
4241 return got_error_set_errno(errcode,
4242 "pthread_mutex_lock");
4243 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4244 err = NULL;
4245 blame->thread = NULL;
4247 if (blame->thread_args.repo) {
4248 const struct got_error *close_err;
4249 close_err = got_repo_close(blame->thread_args.repo);
4250 if (err == NULL)
4251 err = close_err;
4252 blame->thread_args.repo = NULL;
4254 if (blame->f) {
4255 if (fclose(blame->f) == EOF && err == NULL)
4256 err = got_error_from_errno("fclose");
4257 blame->f = NULL;
4259 if (blame->lines) {
4260 for (i = 0; i < blame->nlines; i++)
4261 free(blame->lines[i].id);
4262 free(blame->lines);
4263 blame->lines = NULL;
4265 free(blame->cb_args.commit_id);
4266 blame->cb_args.commit_id = NULL;
4268 return err;
4271 static const struct got_error *
4272 cancel_blame_view(void *arg)
4274 const struct got_error *err = NULL;
4275 int *done = arg;
4276 int errcode;
4278 errcode = pthread_mutex_lock(&tog_mutex);
4279 if (errcode)
4280 return got_error_set_errno(errcode,
4281 "pthread_mutex_unlock");
4283 if (*done)
4284 err = got_error(GOT_ERR_CANCELLED);
4286 errcode = pthread_mutex_unlock(&tog_mutex);
4287 if (errcode)
4288 return got_error_set_errno(errcode,
4289 "pthread_mutex_lock");
4291 return err;
4294 static const struct got_error *
4295 run_blame(struct tog_view *view)
4297 struct tog_blame_view_state *s = &view->state.blame;
4298 struct tog_blame *blame = &s->blame;
4299 const struct got_error *err = NULL;
4300 struct got_blob_object *blob = NULL;
4301 struct got_repository *thread_repo = NULL;
4302 struct got_object_id *obj_id = NULL;
4303 int obj_type;
4305 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4306 s->path);
4307 if (err)
4308 return err;
4310 err = got_object_get_type(&obj_type, s->repo, obj_id);
4311 if (err)
4312 goto done;
4314 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4315 err = got_error(GOT_ERR_OBJ_TYPE);
4316 goto done;
4319 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4320 if (err)
4321 goto done;
4322 blame->f = got_opentemp();
4323 if (blame->f == NULL) {
4324 err = got_error_from_errno("got_opentemp");
4325 goto done;
4327 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4328 &blame->line_offsets, blame->f, blob);
4329 if (err)
4330 goto done;
4331 if (blame->nlines == 0) {
4332 s->blame_complete = 1;
4333 goto done;
4336 /* Don't include \n at EOF in the blame line count. */
4337 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4338 blame->nlines--;
4340 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4341 if (blame->lines == NULL) {
4342 err = got_error_from_errno("calloc");
4343 goto done;
4346 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4347 if (err)
4348 goto done;
4350 blame->cb_args.view = view;
4351 blame->cb_args.lines = blame->lines;
4352 blame->cb_args.nlines = blame->nlines;
4353 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4354 if (blame->cb_args.commit_id == NULL) {
4355 err = got_error_from_errno("got_object_id_dup");
4356 goto done;
4358 blame->cb_args.quit = &s->done;
4360 blame->thread_args.path = s->path;
4361 blame->thread_args.repo = thread_repo;
4362 blame->thread_args.cb_args = &blame->cb_args;
4363 blame->thread_args.complete = &s->blame_complete;
4364 blame->thread_args.cancel_cb = cancel_blame_view;
4365 blame->thread_args.cancel_arg = &s->done;
4366 s->blame_complete = 0;
4368 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4369 s->first_displayed_line = 1;
4370 s->last_displayed_line = view->nlines;
4371 s->selected_line = 1;
4374 done:
4375 if (blob)
4376 got_object_blob_close(blob);
4377 free(obj_id);
4378 if (err)
4379 stop_blame(blame);
4380 return err;
4383 static const struct got_error *
4384 open_blame_view(struct tog_view *view, char *path,
4385 struct got_object_id *commit_id, struct got_repository *repo)
4387 const struct got_error *err = NULL;
4388 struct tog_blame_view_state *s = &view->state.blame;
4390 STAILQ_INIT(&s->blamed_commits);
4392 s->path = strdup(path);
4393 if (s->path == NULL)
4394 return got_error_from_errno("strdup");
4396 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4397 if (err) {
4398 free(s->path);
4399 return err;
4402 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4403 s->first_displayed_line = 1;
4404 s->last_displayed_line = view->nlines;
4405 s->selected_line = 1;
4406 s->blame_complete = 0;
4407 s->repo = repo;
4408 s->commit_id = commit_id;
4409 memset(&s->blame, 0, sizeof(s->blame));
4411 STAILQ_INIT(&s->colors);
4412 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4413 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4414 get_color_value("TOG_COLOR_COMMIT"));
4415 if (err)
4416 return err;
4419 view->show = show_blame_view;
4420 view->input = input_blame_view;
4421 view->close = close_blame_view;
4422 view->search_start = search_start_blame_view;
4423 view->search_next = search_next_blame_view;
4425 return run_blame(view);
4428 static const struct got_error *
4429 close_blame_view(struct tog_view *view)
4431 const struct got_error *err = NULL;
4432 struct tog_blame_view_state *s = &view->state.blame;
4434 if (s->blame.thread)
4435 err = stop_blame(&s->blame);
4437 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4438 struct got_object_qid *blamed_commit;
4439 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4440 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4441 got_object_qid_free(blamed_commit);
4444 free(s->path);
4445 free_colors(&s->colors);
4447 return err;
4450 static const struct got_error *
4451 search_start_blame_view(struct tog_view *view)
4453 struct tog_blame_view_state *s = &view->state.blame;
4455 s->matched_line = 0;
4456 return NULL;
4459 static const struct got_error *
4460 search_next_blame_view(struct tog_view *view)
4462 struct tog_blame_view_state *s = &view->state.blame;
4463 int lineno;
4464 char *line = NULL;
4465 size_t linesize = 0;
4466 ssize_t linelen;
4468 if (!view->searching) {
4469 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4470 return NULL;
4473 if (s->matched_line) {
4474 if (view->searching == TOG_SEARCH_FORWARD)
4475 lineno = s->matched_line + 1;
4476 else
4477 lineno = s->matched_line - 1;
4478 } else {
4479 if (view->searching == TOG_SEARCH_FORWARD)
4480 lineno = 1;
4481 else
4482 lineno = s->blame.nlines;
4485 while (1) {
4486 off_t offset;
4488 if (lineno <= 0 || lineno > s->blame.nlines) {
4489 if (s->matched_line == 0) {
4490 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4491 break;
4494 if (view->searching == TOG_SEARCH_FORWARD)
4495 lineno = 1;
4496 else
4497 lineno = s->blame.nlines;
4500 offset = s->blame.line_offsets[lineno - 1];
4501 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4502 free(line);
4503 return got_error_from_errno("fseeko");
4505 linelen = getline(&line, &linesize, s->blame.f);
4506 if (linelen != -1 &&
4507 match_line(line, &view->regex, 1, &view->regmatch)) {
4508 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4509 s->matched_line = lineno;
4510 break;
4512 if (view->searching == TOG_SEARCH_FORWARD)
4513 lineno++;
4514 else
4515 lineno--;
4517 free(line);
4519 if (s->matched_line) {
4520 s->first_displayed_line = s->matched_line;
4521 s->selected_line = 1;
4524 return NULL;
4527 static const struct got_error *
4528 show_blame_view(struct tog_view *view)
4530 const struct got_error *err = NULL;
4531 struct tog_blame_view_state *s = &view->state.blame;
4532 int errcode;
4534 if (s->blame.thread == NULL && !s->blame_complete) {
4535 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4536 &s->blame.thread_args);
4537 if (errcode)
4538 return got_error_set_errno(errcode, "pthread_create");
4540 halfdelay(1); /* fast refresh while annotating */
4543 if (s->blame_complete)
4544 halfdelay(10); /* disable fast refresh */
4546 err = draw_blame(view);
4548 view_vborder(view);
4549 return err;
4552 static const struct got_error *
4553 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4555 const struct got_error *err = NULL, *thread_err = NULL;
4556 struct tog_view *diff_view;
4557 struct tog_blame_view_state *s = &view->state.blame;
4558 int begin_x = 0;
4560 switch (ch) {
4561 case 'q':
4562 s->done = 1;
4563 break;
4564 case 'g':
4565 case KEY_HOME:
4566 s->selected_line = 1;
4567 s->first_displayed_line = 1;
4568 break;
4569 case 'G':
4570 case KEY_END:
4571 if (s->blame.nlines < view->nlines - 2) {
4572 s->selected_line = s->blame.nlines;
4573 s->first_displayed_line = 1;
4574 } else {
4575 s->selected_line = view->nlines - 2;
4576 s->first_displayed_line = s->blame.nlines -
4577 (view->nlines - 3);
4579 break;
4580 case 'k':
4581 case KEY_UP:
4582 if (s->selected_line > 1)
4583 s->selected_line--;
4584 else if (s->selected_line == 1 &&
4585 s->first_displayed_line > 1)
4586 s->first_displayed_line--;
4587 break;
4588 case KEY_PPAGE:
4589 case CTRL('b'):
4590 if (s->first_displayed_line == 1) {
4591 s->selected_line = 1;
4592 break;
4594 if (s->first_displayed_line > view->nlines - 2)
4595 s->first_displayed_line -=
4596 (view->nlines - 2);
4597 else
4598 s->first_displayed_line = 1;
4599 break;
4600 case 'j':
4601 case KEY_DOWN:
4602 if (s->selected_line < view->nlines - 2 &&
4603 s->first_displayed_line +
4604 s->selected_line <= s->blame.nlines)
4605 s->selected_line++;
4606 else if (s->last_displayed_line <
4607 s->blame.nlines)
4608 s->first_displayed_line++;
4609 break;
4610 case 'b':
4611 case 'p': {
4612 struct got_object_id *id = NULL;
4613 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4614 s->first_displayed_line, s->selected_line);
4615 if (id == NULL)
4616 break;
4617 if (ch == 'p') {
4618 struct got_commit_object *commit;
4619 struct got_object_qid *pid;
4620 struct got_object_id *blob_id = NULL;
4621 int obj_type;
4622 err = got_object_open_as_commit(&commit,
4623 s->repo, id);
4624 if (err)
4625 break;
4626 pid = STAILQ_FIRST(
4627 got_object_commit_get_parent_ids(commit));
4628 if (pid == NULL) {
4629 got_object_commit_close(commit);
4630 break;
4632 /* Check if path history ends here. */
4633 err = got_object_id_by_path(&blob_id, s->repo,
4634 pid->id, s->path);
4635 if (err) {
4636 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4637 err = NULL;
4638 got_object_commit_close(commit);
4639 break;
4641 err = got_object_get_type(&obj_type, s->repo,
4642 blob_id);
4643 free(blob_id);
4644 /* Can't blame non-blob type objects. */
4645 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4646 got_object_commit_close(commit);
4647 break;
4649 err = got_object_qid_alloc(&s->blamed_commit,
4650 pid->id);
4651 got_object_commit_close(commit);
4652 } else {
4653 if (got_object_id_cmp(id,
4654 s->blamed_commit->id) == 0)
4655 break;
4656 err = got_object_qid_alloc(&s->blamed_commit,
4657 id);
4659 if (err)
4660 break;
4661 s->done = 1;
4662 thread_err = stop_blame(&s->blame);
4663 s->done = 0;
4664 if (thread_err)
4665 break;
4666 STAILQ_INSERT_HEAD(&s->blamed_commits,
4667 s->blamed_commit, entry);
4668 err = run_blame(view);
4669 if (err)
4670 break;
4671 break;
4673 case 'B': {
4674 struct got_object_qid *first;
4675 first = STAILQ_FIRST(&s->blamed_commits);
4676 if (!got_object_id_cmp(first->id, s->commit_id))
4677 break;
4678 s->done = 1;
4679 thread_err = stop_blame(&s->blame);
4680 s->done = 0;
4681 if (thread_err)
4682 break;
4683 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4684 got_object_qid_free(s->blamed_commit);
4685 s->blamed_commit =
4686 STAILQ_FIRST(&s->blamed_commits);
4687 err = run_blame(view);
4688 if (err)
4689 break;
4690 break;
4692 case KEY_ENTER:
4693 case '\r': {
4694 struct got_object_id *id = NULL;
4695 struct got_object_qid *pid;
4696 struct got_commit_object *commit = NULL;
4697 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4698 s->first_displayed_line, s->selected_line);
4699 if (id == NULL)
4700 break;
4701 err = got_object_open_as_commit(&commit, s->repo, id);
4702 if (err)
4703 break;
4704 pid = STAILQ_FIRST(
4705 got_object_commit_get_parent_ids(commit));
4706 if (view_is_parent_view(view))
4707 begin_x = view_split_begin_x(view->begin_x);
4708 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4709 if (diff_view == NULL) {
4710 got_object_commit_close(commit);
4711 err = got_error_from_errno("view_open");
4712 break;
4714 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4715 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4716 got_object_commit_close(commit);
4717 if (err) {
4718 view_close(diff_view);
4719 break;
4721 view->focussed = 0;
4722 diff_view->focussed = 1;
4723 if (view_is_parent_view(view)) {
4724 err = view_close_child(view);
4725 if (err)
4726 break;
4727 view_set_child(view, diff_view);
4728 view->focus_child = 1;
4729 } else
4730 *new_view = diff_view;
4731 if (err)
4732 break;
4733 break;
4735 case KEY_NPAGE:
4736 case CTRL('f'):
4737 case ' ':
4738 if (s->last_displayed_line >= s->blame.nlines &&
4739 s->selected_line >= MIN(s->blame.nlines,
4740 view->nlines - 2)) {
4741 break;
4743 if (s->last_displayed_line >= s->blame.nlines &&
4744 s->selected_line < view->nlines - 2) {
4745 s->selected_line = MIN(s->blame.nlines,
4746 view->nlines - 2);
4747 break;
4749 if (s->last_displayed_line + view->nlines - 2
4750 <= s->blame.nlines)
4751 s->first_displayed_line +=
4752 view->nlines - 2;
4753 else
4754 s->first_displayed_line =
4755 s->blame.nlines -
4756 (view->nlines - 3);
4757 break;
4758 case KEY_RESIZE:
4759 if (s->selected_line > view->nlines - 2) {
4760 s->selected_line = MIN(s->blame.nlines,
4761 view->nlines - 2);
4763 break;
4764 default:
4765 break;
4767 return thread_err ? thread_err : err;
4770 static const struct got_error *
4771 cmd_blame(int argc, char *argv[])
4773 const struct got_error *error;
4774 struct got_repository *repo = NULL;
4775 struct got_worktree *worktree = NULL;
4776 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4777 char *link_target = NULL;
4778 struct got_object_id *commit_id = NULL;
4779 char *commit_id_str = NULL;
4780 int ch;
4781 struct tog_view *view;
4783 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4784 switch (ch) {
4785 case 'c':
4786 commit_id_str = optarg;
4787 break;
4788 case 'r':
4789 repo_path = realpath(optarg, NULL);
4790 if (repo_path == NULL)
4791 return got_error_from_errno2("realpath",
4792 optarg);
4793 break;
4794 default:
4795 usage_blame();
4796 /* NOTREACHED */
4800 argc -= optind;
4801 argv += optind;
4803 if (argc != 1)
4804 usage_blame();
4806 if (repo_path == NULL) {
4807 cwd = getcwd(NULL, 0);
4808 if (cwd == NULL)
4809 return got_error_from_errno("getcwd");
4810 error = got_worktree_open(&worktree, cwd);
4811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4812 goto done;
4813 if (worktree)
4814 repo_path =
4815 strdup(got_worktree_get_repo_path(worktree));
4816 else
4817 repo_path = strdup(cwd);
4818 if (repo_path == NULL) {
4819 error = got_error_from_errno("strdup");
4820 goto done;
4824 error = got_repo_open(&repo, repo_path, NULL);
4825 if (error != NULL)
4826 goto done;
4828 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4829 worktree);
4830 if (error)
4831 goto done;
4833 init_curses();
4835 error = apply_unveil(got_repo_get_path(repo), NULL);
4836 if (error)
4837 goto done;
4839 error = tog_load_refs(repo);
4840 if (error)
4841 goto done;
4843 if (commit_id_str == NULL) {
4844 struct got_reference *head_ref;
4845 error = got_ref_open(&head_ref, repo, worktree ?
4846 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4847 if (error != NULL)
4848 goto done;
4849 error = got_ref_resolve(&commit_id, repo, head_ref);
4850 got_ref_close(head_ref);
4851 } else {
4852 error = got_repo_match_object_id(&commit_id, NULL,
4853 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4855 if (error != NULL)
4856 goto done;
4858 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4859 if (view == NULL) {
4860 error = got_error_from_errno("view_open");
4861 goto done;
4864 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4865 commit_id, repo);
4866 if (error)
4867 goto done;
4869 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4870 commit_id, repo);
4871 if (error)
4872 goto done;
4873 if (worktree) {
4874 /* Release work tree lock. */
4875 got_worktree_close(worktree);
4876 worktree = NULL;
4878 error = view_loop(view);
4879 done:
4880 free(repo_path);
4881 free(in_repo_path);
4882 free(link_target);
4883 free(cwd);
4884 free(commit_id);
4885 if (worktree)
4886 got_worktree_close(worktree);
4887 if (repo) {
4888 const struct got_error *close_err = got_repo_close(repo);
4889 if (error == NULL)
4890 error = close_err;
4892 tog_free_refs();
4893 return error;
4896 static const struct got_error *
4897 draw_tree_entries(struct tog_view *view, const char *parent_path)
4899 struct tog_tree_view_state *s = &view->state.tree;
4900 const struct got_error *err = NULL;
4901 struct got_tree_entry *te;
4902 wchar_t *wline;
4903 struct tog_color *tc;
4904 int width, n, i, nentries;
4905 int limit = view->nlines;
4907 s->ndisplayed = 0;
4909 werase(view->window);
4911 if (limit == 0)
4912 return NULL;
4914 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4915 if (err)
4916 return err;
4917 if (view_needs_focus_indication(view))
4918 wstandout(view->window);
4919 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4920 if (tc)
4921 wattr_on(view->window,
4922 COLOR_PAIR(tc->colorpair), NULL);
4923 waddwstr(view->window, wline);
4924 if (tc)
4925 wattr_off(view->window,
4926 COLOR_PAIR(tc->colorpair), NULL);
4927 if (view_needs_focus_indication(view))
4928 wstandend(view->window);
4929 free(wline);
4930 wline = NULL;
4931 if (width < view->ncols - 1)
4932 waddch(view->window, '\n');
4933 if (--limit <= 0)
4934 return NULL;
4935 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4936 if (err)
4937 return err;
4938 waddwstr(view->window, wline);
4939 free(wline);
4940 wline = NULL;
4941 if (width < view->ncols - 1)
4942 waddch(view->window, '\n');
4943 if (--limit <= 0)
4944 return NULL;
4945 waddch(view->window, '\n');
4946 if (--limit <= 0)
4947 return NULL;
4949 if (s->first_displayed_entry == NULL) {
4950 te = got_object_tree_get_first_entry(s->tree);
4951 if (s->selected == 0) {
4952 if (view->focussed)
4953 wstandout(view->window);
4954 s->selected_entry = NULL;
4956 waddstr(view->window, " ..\n"); /* parent directory */
4957 if (s->selected == 0 && view->focussed)
4958 wstandend(view->window);
4959 s->ndisplayed++;
4960 if (--limit <= 0)
4961 return NULL;
4962 n = 1;
4963 } else {
4964 n = 0;
4965 te = s->first_displayed_entry;
4968 nentries = got_object_tree_get_nentries(s->tree);
4969 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4970 char *line = NULL, *id_str = NULL, *link_target = NULL;
4971 const char *modestr = "";
4972 mode_t mode;
4974 te = got_object_tree_get_entry(s->tree, i);
4975 mode = got_tree_entry_get_mode(te);
4977 if (s->show_ids) {
4978 err = got_object_id_str(&id_str,
4979 got_tree_entry_get_id(te));
4980 if (err)
4981 return got_error_from_errno(
4982 "got_object_id_str");
4984 if (got_object_tree_entry_is_submodule(te))
4985 modestr = "$";
4986 else if (S_ISLNK(mode)) {
4987 int i;
4989 err = got_tree_entry_get_symlink_target(&link_target,
4990 te, s->repo);
4991 if (err) {
4992 free(id_str);
4993 return err;
4995 for (i = 0; i < strlen(link_target); i++) {
4996 if (!isprint((unsigned char)link_target[i]))
4997 link_target[i] = '?';
4999 modestr = "@";
5001 else if (S_ISDIR(mode))
5002 modestr = "/";
5003 else if (mode & S_IXUSR)
5004 modestr = "*";
5005 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5006 got_tree_entry_get_name(te), modestr,
5007 link_target ? " -> ": "",
5008 link_target ? link_target : "") == -1) {
5009 free(id_str);
5010 free(link_target);
5011 return got_error_from_errno("asprintf");
5013 free(id_str);
5014 free(link_target);
5015 err = format_line(&wline, &width, line, view->ncols, 0);
5016 if (err) {
5017 free(line);
5018 break;
5020 if (n == s->selected) {
5021 if (view->focussed)
5022 wstandout(view->window);
5023 s->selected_entry = te;
5025 tc = match_color(&s->colors, line);
5026 if (tc)
5027 wattr_on(view->window,
5028 COLOR_PAIR(tc->colorpair), NULL);
5029 waddwstr(view->window, wline);
5030 if (tc)
5031 wattr_off(view->window,
5032 COLOR_PAIR(tc->colorpair), NULL);
5033 if (width < view->ncols - 1)
5034 waddch(view->window, '\n');
5035 if (n == s->selected && view->focussed)
5036 wstandend(view->window);
5037 free(line);
5038 free(wline);
5039 wline = NULL;
5040 n++;
5041 s->ndisplayed++;
5042 s->last_displayed_entry = te;
5043 if (--limit <= 0)
5044 break;
5047 return err;
5050 static void
5051 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5053 struct got_tree_entry *te;
5054 int isroot = s->tree == s->root;
5055 int i = 0;
5057 if (s->first_displayed_entry == NULL)
5058 return;
5060 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5061 while (i++ < maxscroll) {
5062 if (te == NULL) {
5063 if (!isroot)
5064 s->first_displayed_entry = NULL;
5065 break;
5067 s->first_displayed_entry = te;
5068 te = got_tree_entry_get_prev(s->tree, te);
5072 static void
5073 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5075 struct got_tree_entry *next, *last;
5076 int n = 0;
5078 if (s->first_displayed_entry)
5079 next = got_tree_entry_get_next(s->tree,
5080 s->first_displayed_entry);
5081 else
5082 next = got_object_tree_get_first_entry(s->tree);
5084 last = s->last_displayed_entry;
5085 while (next && last && n++ < maxscroll) {
5086 last = got_tree_entry_get_next(s->tree, last);
5087 if (last) {
5088 s->first_displayed_entry = next;
5089 next = got_tree_entry_get_next(s->tree, next);
5094 static const struct got_error *
5095 tree_entry_path(char **path, struct tog_parent_trees *parents,
5096 struct got_tree_entry *te)
5098 const struct got_error *err = NULL;
5099 struct tog_parent_tree *pt;
5100 size_t len = 2; /* for leading slash and NUL */
5102 TAILQ_FOREACH(pt, parents, entry)
5103 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5104 + 1 /* slash */;
5105 if (te)
5106 len += strlen(got_tree_entry_get_name(te));
5108 *path = calloc(1, len);
5109 if (path == NULL)
5110 return got_error_from_errno("calloc");
5112 (*path)[0] = '/';
5113 pt = TAILQ_LAST(parents, tog_parent_trees);
5114 while (pt) {
5115 const char *name = got_tree_entry_get_name(pt->selected_entry);
5116 if (strlcat(*path, name, len) >= len) {
5117 err = got_error(GOT_ERR_NO_SPACE);
5118 goto done;
5120 if (strlcat(*path, "/", len) >= len) {
5121 err = got_error(GOT_ERR_NO_SPACE);
5122 goto done;
5124 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5126 if (te) {
5127 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5128 err = got_error(GOT_ERR_NO_SPACE);
5129 goto done;
5132 done:
5133 if (err) {
5134 free(*path);
5135 *path = NULL;
5137 return err;
5140 static const struct got_error *
5141 blame_tree_entry(struct tog_view **new_view, int begin_x,
5142 struct got_tree_entry *te, struct tog_parent_trees *parents,
5143 struct got_object_id *commit_id, struct got_repository *repo)
5145 const struct got_error *err = NULL;
5146 char *path;
5147 struct tog_view *blame_view;
5149 *new_view = NULL;
5151 err = tree_entry_path(&path, parents, te);
5152 if (err)
5153 return err;
5155 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5156 if (blame_view == NULL) {
5157 err = got_error_from_errno("view_open");
5158 goto done;
5161 err = open_blame_view(blame_view, path, commit_id, repo);
5162 if (err) {
5163 if (err->code == GOT_ERR_CANCELLED)
5164 err = NULL;
5165 view_close(blame_view);
5166 } else
5167 *new_view = blame_view;
5168 done:
5169 free(path);
5170 return err;
5173 static const struct got_error *
5174 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5175 struct tog_tree_view_state *s)
5177 struct tog_view *log_view;
5178 const struct got_error *err = NULL;
5179 char *path;
5181 *new_view = NULL;
5183 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5184 if (log_view == NULL)
5185 return got_error_from_errno("view_open");
5187 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5188 if (err)
5189 return err;
5191 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5192 path, 0);
5193 if (err)
5194 view_close(log_view);
5195 else
5196 *new_view = log_view;
5197 free(path);
5198 return err;
5201 static const struct got_error *
5202 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5203 const char *head_ref_name, struct got_repository *repo)
5205 const struct got_error *err = NULL;
5206 char *commit_id_str = NULL;
5207 struct tog_tree_view_state *s = &view->state.tree;
5208 struct got_commit_object *commit = NULL;
5210 TAILQ_INIT(&s->parents);
5211 STAILQ_INIT(&s->colors);
5213 s->commit_id = got_object_id_dup(commit_id);
5214 if (s->commit_id == NULL)
5215 return got_error_from_errno("got_object_id_dup");
5217 err = got_object_open_as_commit(&commit, repo, commit_id);
5218 if (err)
5219 goto done;
5222 * The root is opened here and will be closed when the view is closed.
5223 * Any visited subtrees and their path-wise parents are opened and
5224 * closed on demand.
5226 err = got_object_open_as_tree(&s->root, repo,
5227 got_object_commit_get_tree_id(commit));
5228 if (err)
5229 goto done;
5230 s->tree = s->root;
5232 err = got_object_id_str(&commit_id_str, commit_id);
5233 if (err != NULL)
5234 goto done;
5236 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5237 err = got_error_from_errno("asprintf");
5238 goto done;
5241 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5242 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5243 if (head_ref_name) {
5244 s->head_ref_name = strdup(head_ref_name);
5245 if (s->head_ref_name == NULL) {
5246 err = got_error_from_errno("strdup");
5247 goto done;
5250 s->repo = repo;
5252 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5253 err = add_color(&s->colors, "\\$$",
5254 TOG_COLOR_TREE_SUBMODULE,
5255 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5256 if (err)
5257 goto done;
5258 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5259 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5260 if (err)
5261 goto done;
5262 err = add_color(&s->colors, "/$",
5263 TOG_COLOR_TREE_DIRECTORY,
5264 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5265 if (err)
5266 goto done;
5268 err = add_color(&s->colors, "\\*$",
5269 TOG_COLOR_TREE_EXECUTABLE,
5270 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5271 if (err)
5272 goto done;
5274 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5275 get_color_value("TOG_COLOR_COMMIT"));
5276 if (err)
5277 goto done;
5280 view->show = show_tree_view;
5281 view->input = input_tree_view;
5282 view->close = close_tree_view;
5283 view->search_start = search_start_tree_view;
5284 view->search_next = search_next_tree_view;
5285 done:
5286 free(commit_id_str);
5287 if (commit)
5288 got_object_commit_close(commit);
5289 if (err)
5290 close_tree_view(view);
5291 return err;
5294 static const struct got_error *
5295 close_tree_view(struct tog_view *view)
5297 struct tog_tree_view_state *s = &view->state.tree;
5299 free_colors(&s->colors);
5300 free(s->tree_label);
5301 s->tree_label = NULL;
5302 free(s->commit_id);
5303 s->commit_id = NULL;
5304 free(s->head_ref_name);
5305 s->head_ref_name = NULL;
5306 while (!TAILQ_EMPTY(&s->parents)) {
5307 struct tog_parent_tree *parent;
5308 parent = TAILQ_FIRST(&s->parents);
5309 TAILQ_REMOVE(&s->parents, parent, entry);
5310 if (parent->tree != s->root)
5311 got_object_tree_close(parent->tree);
5312 free(parent);
5315 if (s->tree != NULL && s->tree != s->root)
5316 got_object_tree_close(s->tree);
5317 if (s->root)
5318 got_object_tree_close(s->root);
5319 return NULL;
5322 static const struct got_error *
5323 search_start_tree_view(struct tog_view *view)
5325 struct tog_tree_view_state *s = &view->state.tree;
5327 s->matched_entry = NULL;
5328 return NULL;
5331 static int
5332 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5334 regmatch_t regmatch;
5336 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5337 0) == 0;
5340 static const struct got_error *
5341 search_next_tree_view(struct tog_view *view)
5343 struct tog_tree_view_state *s = &view->state.tree;
5344 struct got_tree_entry *te = NULL;
5346 if (!view->searching) {
5347 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5348 return NULL;
5351 if (s->matched_entry) {
5352 if (view->searching == TOG_SEARCH_FORWARD) {
5353 if (s->selected_entry)
5354 te = got_tree_entry_get_next(s->tree,
5355 s->selected_entry);
5356 else
5357 te = got_object_tree_get_first_entry(s->tree);
5358 } else {
5359 if (s->selected_entry == NULL)
5360 te = got_object_tree_get_last_entry(s->tree);
5361 else
5362 te = got_tree_entry_get_prev(s->tree,
5363 s->selected_entry);
5365 } else {
5366 if (view->searching == TOG_SEARCH_FORWARD)
5367 te = got_object_tree_get_first_entry(s->tree);
5368 else
5369 te = got_object_tree_get_last_entry(s->tree);
5372 while (1) {
5373 if (te == NULL) {
5374 if (s->matched_entry == NULL) {
5375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5376 return NULL;
5378 if (view->searching == TOG_SEARCH_FORWARD)
5379 te = got_object_tree_get_first_entry(s->tree);
5380 else
5381 te = got_object_tree_get_last_entry(s->tree);
5384 if (match_tree_entry(te, &view->regex)) {
5385 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5386 s->matched_entry = te;
5387 break;
5390 if (view->searching == TOG_SEARCH_FORWARD)
5391 te = got_tree_entry_get_next(s->tree, te);
5392 else
5393 te = got_tree_entry_get_prev(s->tree, te);
5396 if (s->matched_entry) {
5397 s->first_displayed_entry = s->matched_entry;
5398 s->selected = 0;
5401 return NULL;
5404 static const struct got_error *
5405 show_tree_view(struct tog_view *view)
5407 const struct got_error *err = NULL;
5408 struct tog_tree_view_state *s = &view->state.tree;
5409 char *parent_path;
5411 err = tree_entry_path(&parent_path, &s->parents, NULL);
5412 if (err)
5413 return err;
5415 err = draw_tree_entries(view, parent_path);
5416 free(parent_path);
5418 view_vborder(view);
5419 return err;
5422 static const struct got_error *
5423 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5425 const struct got_error *err = NULL;
5426 struct tog_tree_view_state *s = &view->state.tree;
5427 struct tog_view *log_view, *ref_view;
5428 struct got_tree_entry *te;
5429 int begin_x = 0, n;
5431 switch (ch) {
5432 case 'i':
5433 s->show_ids = !s->show_ids;
5434 break;
5435 case 'l':
5436 if (!s->selected_entry)
5437 break;
5438 if (view_is_parent_view(view))
5439 begin_x = view_split_begin_x(view->begin_x);
5440 err = log_selected_tree_entry(&log_view, begin_x, s);
5441 view->focussed = 0;
5442 log_view->focussed = 1;
5443 if (view_is_parent_view(view)) {
5444 err = view_close_child(view);
5445 if (err)
5446 return err;
5447 view_set_child(view, log_view);
5448 view->focus_child = 1;
5449 } else
5450 *new_view = log_view;
5451 break;
5452 case 'r':
5453 if (view_is_parent_view(view))
5454 begin_x = view_split_begin_x(view->begin_x);
5455 ref_view = view_open(view->nlines, view->ncols,
5456 view->begin_y, begin_x, TOG_VIEW_REF);
5457 if (ref_view == NULL)
5458 return got_error_from_errno("view_open");
5459 err = open_ref_view(ref_view, s->repo);
5460 if (err) {
5461 view_close(ref_view);
5462 return err;
5464 view->focussed = 0;
5465 ref_view->focussed = 1;
5466 if (view_is_parent_view(view)) {
5467 err = view_close_child(view);
5468 if (err)
5469 return err;
5470 view_set_child(view, ref_view);
5471 view->focus_child = 1;
5472 } else
5473 *new_view = ref_view;
5474 break;
5475 case 'g':
5476 case KEY_HOME:
5477 s->selected = 0;
5478 if (s->tree == s->root)
5479 s->first_displayed_entry =
5480 got_object_tree_get_first_entry(s->tree);
5481 else
5482 s->first_displayed_entry = NULL;
5483 break;
5484 case 'G':
5485 case KEY_END:
5486 s->selected = 0;
5487 te = got_object_tree_get_last_entry(s->tree);
5488 for (n = 0; n < view->nlines - 3; n++) {
5489 if (te == NULL) {
5490 if(s->tree != s->root) {
5491 s->first_displayed_entry = NULL;
5492 n++;
5494 break;
5496 s->first_displayed_entry = te;
5497 te = got_tree_entry_get_prev(s->tree, te);
5499 if (n > 0)
5500 s->selected = n - 1;
5501 break;
5502 case 'k':
5503 case KEY_UP:
5504 if (s->selected > 0) {
5505 s->selected--;
5506 break;
5508 tree_scroll_up(s, 1);
5509 break;
5510 case KEY_PPAGE:
5511 case CTRL('b'):
5512 if (s->tree == s->root) {
5513 if (got_object_tree_get_first_entry(s->tree) ==
5514 s->first_displayed_entry)
5515 s->selected = 0;
5516 } else {
5517 if (s->first_displayed_entry == NULL)
5518 s->selected = 0;
5520 tree_scroll_up(s, MAX(0, view->nlines - 3));
5521 break;
5522 case 'j':
5523 case KEY_DOWN:
5524 if (s->selected < s->ndisplayed - 1) {
5525 s->selected++;
5526 break;
5528 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5529 == NULL)
5530 /* can't scroll any further */
5531 break;
5532 tree_scroll_down(s, 1);
5533 break;
5534 case KEY_NPAGE:
5535 case CTRL('f'):
5536 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5537 == NULL) {
5538 /* can't scroll any further; move cursor down */
5539 if (s->selected < s->ndisplayed - 1)
5540 s->selected = s->ndisplayed - 1;
5541 break;
5543 tree_scroll_down(s, view->nlines - 3);
5544 break;
5545 case KEY_ENTER:
5546 case '\r':
5547 case KEY_BACKSPACE:
5548 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5549 struct tog_parent_tree *parent;
5550 /* user selected '..' */
5551 if (s->tree == s->root)
5552 break;
5553 parent = TAILQ_FIRST(&s->parents);
5554 TAILQ_REMOVE(&s->parents, parent,
5555 entry);
5556 got_object_tree_close(s->tree);
5557 s->tree = parent->tree;
5558 s->first_displayed_entry =
5559 parent->first_displayed_entry;
5560 s->selected_entry =
5561 parent->selected_entry;
5562 s->selected = parent->selected;
5563 free(parent);
5564 } else if (S_ISDIR(got_tree_entry_get_mode(
5565 s->selected_entry))) {
5566 struct got_tree_object *subtree;
5567 err = got_object_open_as_tree(&subtree, s->repo,
5568 got_tree_entry_get_id(s->selected_entry));
5569 if (err)
5570 break;
5571 err = tree_view_visit_subtree(s, subtree);
5572 if (err) {
5573 got_object_tree_close(subtree);
5574 break;
5576 } else if (S_ISREG(got_tree_entry_get_mode(
5577 s->selected_entry))) {
5578 struct tog_view *blame_view;
5579 int begin_x = view_is_parent_view(view) ?
5580 view_split_begin_x(view->begin_x) : 0;
5582 err = blame_tree_entry(&blame_view, begin_x,
5583 s->selected_entry, &s->parents,
5584 s->commit_id, s->repo);
5585 if (err)
5586 break;
5587 view->focussed = 0;
5588 blame_view->focussed = 1;
5589 if (view_is_parent_view(view)) {
5590 err = view_close_child(view);
5591 if (err)
5592 return err;
5593 view_set_child(view, blame_view);
5594 view->focus_child = 1;
5595 } else
5596 *new_view = blame_view;
5598 break;
5599 case KEY_RESIZE:
5600 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5601 s->selected = view->nlines - 4;
5602 break;
5603 default:
5604 break;
5607 return err;
5610 __dead static void
5611 usage_tree(void)
5613 endwin();
5614 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5615 getprogname());
5616 exit(1);
5619 static const struct got_error *
5620 cmd_tree(int argc, char *argv[])
5622 const struct got_error *error;
5623 struct got_repository *repo = NULL;
5624 struct got_worktree *worktree = NULL;
5625 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5626 struct got_object_id *commit_id = NULL;
5627 const char *commit_id_arg = NULL;
5628 char *label = NULL;
5629 struct got_reference *ref = NULL;
5630 const char *head_ref_name = NULL;
5631 int ch;
5632 struct tog_view *view;
5634 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5635 switch (ch) {
5636 case 'c':
5637 commit_id_arg = optarg;
5638 break;
5639 case 'r':
5640 repo_path = realpath(optarg, NULL);
5641 if (repo_path == NULL)
5642 return got_error_from_errno2("realpath",
5643 optarg);
5644 break;
5645 default:
5646 usage_tree();
5647 /* NOTREACHED */
5651 argc -= optind;
5652 argv += optind;
5654 if (argc > 1)
5655 usage_tree();
5657 if (repo_path == NULL) {
5658 cwd = getcwd(NULL, 0);
5659 if (cwd == NULL)
5660 return got_error_from_errno("getcwd");
5661 error = got_worktree_open(&worktree, cwd);
5662 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5663 goto done;
5664 if (worktree)
5665 repo_path =
5666 strdup(got_worktree_get_repo_path(worktree));
5667 else
5668 repo_path = strdup(cwd);
5669 if (repo_path == NULL) {
5670 error = got_error_from_errno("strdup");
5671 goto done;
5675 error = got_repo_open(&repo, repo_path, NULL);
5676 if (error != NULL)
5677 goto done;
5679 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5680 repo, worktree);
5681 if (error)
5682 goto done;
5684 init_curses();
5686 error = apply_unveil(got_repo_get_path(repo), NULL);
5687 if (error)
5688 goto done;
5690 error = tog_load_refs(repo);
5691 if (error)
5692 goto done;
5694 if (commit_id_arg == NULL) {
5695 error = got_repo_match_object_id(&commit_id, &label,
5696 worktree ? got_worktree_get_head_ref_name(worktree) :
5697 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5698 if (error)
5699 goto done;
5700 head_ref_name = label;
5701 } else {
5702 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5703 if (error == NULL)
5704 head_ref_name = got_ref_get_name(ref);
5705 else if (error->code != GOT_ERR_NOT_REF)
5706 goto done;
5707 error = got_repo_match_object_id(&commit_id, NULL,
5708 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5709 if (error)
5710 goto done;
5713 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5714 if (view == NULL) {
5715 error = got_error_from_errno("view_open");
5716 goto done;
5718 error = open_tree_view(view, commit_id, head_ref_name, repo);
5719 if (error)
5720 goto done;
5721 if (!got_path_is_root_dir(in_repo_path)) {
5722 error = tree_view_walk_path(&view->state.tree, commit_id,
5723 in_repo_path);
5724 if (error)
5725 goto done;
5728 if (worktree) {
5729 /* Release work tree lock. */
5730 got_worktree_close(worktree);
5731 worktree = NULL;
5733 error = view_loop(view);
5734 done:
5735 free(repo_path);
5736 free(cwd);
5737 free(commit_id);
5738 free(label);
5739 if (ref)
5740 got_ref_close(ref);
5741 if (repo) {
5742 const struct got_error *close_err = got_repo_close(repo);
5743 if (error == NULL)
5744 error = close_err;
5746 tog_free_refs();
5747 return error;
5750 static const struct got_error *
5751 ref_view_load_refs(struct tog_ref_view_state *s)
5753 struct got_reflist_entry *sre;
5754 struct tog_reflist_entry *re;
5756 s->nrefs = 0;
5757 TAILQ_FOREACH(sre, &tog_refs, entry) {
5758 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5759 continue;
5761 re = malloc(sizeof(*re));
5762 if (re == NULL)
5763 return got_error_from_errno("malloc");
5765 re->ref = got_ref_dup(sre->ref);
5766 if (re->ref == NULL)
5767 return got_error_from_errno("got_ref_dup");
5768 re->idx = s->nrefs++;
5769 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5772 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5773 return NULL;
5776 void
5777 ref_view_free_refs(struct tog_ref_view_state *s)
5779 struct tog_reflist_entry *re;
5781 while (!TAILQ_EMPTY(&s->refs)) {
5782 re = TAILQ_FIRST(&s->refs);
5783 TAILQ_REMOVE(&s->refs, re, entry);
5784 got_ref_close(re->ref);
5785 free(re);
5789 static const struct got_error *
5790 open_ref_view(struct tog_view *view, struct got_repository *repo)
5792 const struct got_error *err = NULL;
5793 struct tog_ref_view_state *s = &view->state.ref;
5795 s->selected_entry = 0;
5796 s->repo = repo;
5798 TAILQ_INIT(&s->refs);
5799 STAILQ_INIT(&s->colors);
5801 err = ref_view_load_refs(s);
5802 if (err)
5803 return err;
5805 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5806 err = add_color(&s->colors, "^refs/heads/",
5807 TOG_COLOR_REFS_HEADS,
5808 get_color_value("TOG_COLOR_REFS_HEADS"));
5809 if (err)
5810 goto done;
5812 err = add_color(&s->colors, "^refs/tags/",
5813 TOG_COLOR_REFS_TAGS,
5814 get_color_value("TOG_COLOR_REFS_TAGS"));
5815 if (err)
5816 goto done;
5818 err = add_color(&s->colors, "^refs/remotes/",
5819 TOG_COLOR_REFS_REMOTES,
5820 get_color_value("TOG_COLOR_REFS_REMOTES"));
5821 if (err)
5822 goto done;
5825 view->show = show_ref_view;
5826 view->input = input_ref_view;
5827 view->close = close_ref_view;
5828 view->search_start = search_start_ref_view;
5829 view->search_next = search_next_ref_view;
5830 done:
5831 if (err)
5832 free_colors(&s->colors);
5833 return err;
5836 static const struct got_error *
5837 close_ref_view(struct tog_view *view)
5839 struct tog_ref_view_state *s = &view->state.ref;
5841 ref_view_free_refs(s);
5842 free_colors(&s->colors);
5844 return NULL;
5847 static const struct got_error *
5848 resolve_reflist_entry(struct got_object_id **commit_id,
5849 struct tog_reflist_entry *re, struct got_repository *repo)
5851 const struct got_error *err = NULL;
5852 struct got_object_id *obj_id;
5853 struct got_tag_object *tag = NULL;
5854 int obj_type;
5856 *commit_id = NULL;
5858 err = got_ref_resolve(&obj_id, repo, re->ref);
5859 if (err)
5860 return err;
5862 err = got_object_get_type(&obj_type, repo, obj_id);
5863 if (err)
5864 goto done;
5866 switch (obj_type) {
5867 case GOT_OBJ_TYPE_COMMIT:
5868 *commit_id = obj_id;
5869 break;
5870 case GOT_OBJ_TYPE_TAG:
5871 err = got_object_open_as_tag(&tag, repo, obj_id);
5872 if (err)
5873 goto done;
5874 free(obj_id);
5875 err = got_object_get_type(&obj_type, repo,
5876 got_object_tag_get_object_id(tag));
5877 if (err)
5878 goto done;
5879 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5880 err = got_error(GOT_ERR_OBJ_TYPE);
5881 goto done;
5883 *commit_id = got_object_id_dup(
5884 got_object_tag_get_object_id(tag));
5885 if (*commit_id == NULL) {
5886 err = got_error_from_errno("got_object_id_dup");
5887 goto done;
5889 break;
5890 default:
5891 err = got_error(GOT_ERR_OBJ_TYPE);
5892 break;
5895 done:
5896 if (tag)
5897 got_object_tag_close(tag);
5898 if (err) {
5899 free(*commit_id);
5900 *commit_id = NULL;
5902 return err;
5905 static const struct got_error *
5906 log_ref_entry(struct tog_view **new_view, int begin_x,
5907 struct tog_reflist_entry *re, struct got_repository *repo)
5909 struct tog_view *log_view;
5910 const struct got_error *err = NULL;
5911 struct got_object_id *commit_id = NULL;
5913 *new_view = NULL;
5915 err = resolve_reflist_entry(&commit_id, re, repo);
5916 if (err) {
5917 if (err->code != GOT_ERR_OBJ_TYPE)
5918 return err;
5919 else
5920 return NULL;
5923 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5924 if (log_view == NULL) {
5925 err = got_error_from_errno("view_open");
5926 goto done;
5929 err = open_log_view(log_view, commit_id, repo,
5930 got_ref_get_name(re->ref), "", 0);
5931 done:
5932 if (err)
5933 view_close(log_view);
5934 else
5935 *new_view = log_view;
5936 free(commit_id);
5937 return err;
5940 static void
5941 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5943 struct tog_reflist_entry *re;
5944 int i = 0;
5946 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5947 return;
5949 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5950 while (i++ < maxscroll) {
5951 if (re == NULL)
5952 break;
5953 s->first_displayed_entry = re;
5954 re = TAILQ_PREV(re, tog_reflist_head, entry);
5958 static void
5959 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5961 struct tog_reflist_entry *next, *last;
5962 int n = 0;
5964 if (s->first_displayed_entry)
5965 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5966 else
5967 next = TAILQ_FIRST(&s->refs);
5969 last = s->last_displayed_entry;
5970 while (next && last && n++ < maxscroll) {
5971 last = TAILQ_NEXT(last, entry);
5972 if (last) {
5973 s->first_displayed_entry = next;
5974 next = TAILQ_NEXT(next, entry);
5979 static const struct got_error *
5980 search_start_ref_view(struct tog_view *view)
5982 struct tog_ref_view_state *s = &view->state.ref;
5984 s->matched_entry = NULL;
5985 return NULL;
5988 static int
5989 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5991 regmatch_t regmatch;
5993 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5994 0) == 0;
5997 static const struct got_error *
5998 search_next_ref_view(struct tog_view *view)
6000 struct tog_ref_view_state *s = &view->state.ref;
6001 struct tog_reflist_entry *re = NULL;
6003 if (!view->searching) {
6004 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6005 return NULL;
6008 if (s->matched_entry) {
6009 if (view->searching == TOG_SEARCH_FORWARD) {
6010 if (s->selected_entry)
6011 re = TAILQ_NEXT(s->selected_entry, entry);
6012 else
6013 re = TAILQ_PREV(s->selected_entry,
6014 tog_reflist_head, entry);
6015 } else {
6016 if (s->selected_entry == NULL)
6017 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6018 else
6019 re = TAILQ_PREV(s->selected_entry,
6020 tog_reflist_head, entry);
6022 } else {
6023 if (view->searching == TOG_SEARCH_FORWARD)
6024 re = TAILQ_FIRST(&s->refs);
6025 else
6026 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6029 while (1) {
6030 if (re == NULL) {
6031 if (s->matched_entry == NULL) {
6032 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6033 return NULL;
6035 if (view->searching == TOG_SEARCH_FORWARD)
6036 re = TAILQ_FIRST(&s->refs);
6037 else
6038 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6041 if (match_reflist_entry(re, &view->regex)) {
6042 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6043 s->matched_entry = re;
6044 break;
6047 if (view->searching == TOG_SEARCH_FORWARD)
6048 re = TAILQ_NEXT(re, entry);
6049 else
6050 re = TAILQ_PREV(re, tog_reflist_head, entry);
6053 if (s->matched_entry) {
6054 s->first_displayed_entry = s->matched_entry;
6055 s->selected = 0;
6058 return NULL;
6061 static const struct got_error *
6062 show_ref_view(struct tog_view *view)
6064 const struct got_error *err = NULL;
6065 struct tog_ref_view_state *s = &view->state.ref;
6066 struct tog_reflist_entry *re;
6067 char *line = NULL;
6068 wchar_t *wline;
6069 struct tog_color *tc;
6070 int width, n;
6071 int limit = view->nlines;
6073 werase(view->window);
6075 s->ndisplayed = 0;
6077 if (limit == 0)
6078 return NULL;
6080 re = s->first_displayed_entry;
6082 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6083 s->nrefs) == -1)
6084 return got_error_from_errno("asprintf");
6086 err = format_line(&wline, &width, line, view->ncols, 0);
6087 if (err) {
6088 free(line);
6089 return err;
6091 if (view_needs_focus_indication(view))
6092 wstandout(view->window);
6093 waddwstr(view->window, wline);
6094 if (view_needs_focus_indication(view))
6095 wstandend(view->window);
6096 free(wline);
6097 wline = NULL;
6098 free(line);
6099 line = NULL;
6100 if (width < view->ncols - 1)
6101 waddch(view->window, '\n');
6102 if (--limit <= 0)
6103 return NULL;
6105 n = 0;
6106 while (re && limit > 0) {
6107 char *line = NULL;
6109 if (got_ref_is_symbolic(re->ref)) {
6110 if (asprintf(&line, "%s -> %s",
6111 got_ref_get_name(re->ref),
6112 got_ref_get_symref_target(re->ref)) == -1)
6113 return got_error_from_errno("asprintf");
6114 } else if (s->show_ids) {
6115 struct got_object_id *id;
6116 char *id_str;
6117 err = got_ref_resolve(&id, s->repo, re->ref);
6118 if (err)
6119 return err;
6120 err = got_object_id_str(&id_str, id);
6121 if (err) {
6122 free(id);
6123 return err;
6125 if (asprintf(&line, "%s: %s",
6126 got_ref_get_name(re->ref), id_str) == -1) {
6127 err = got_error_from_errno("asprintf");
6128 free(id);
6129 free(id_str);
6130 return err;
6132 free(id);
6133 free(id_str);
6134 } else {
6135 line = strdup(got_ref_get_name(re->ref));
6136 if (line == NULL)
6137 return got_error_from_errno("strdup");
6140 err = format_line(&wline, &width, line, view->ncols, 0);
6141 if (err) {
6142 free(line);
6143 return err;
6145 if (n == s->selected) {
6146 if (view->focussed)
6147 wstandout(view->window);
6148 s->selected_entry = re;
6150 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6151 if (tc)
6152 wattr_on(view->window,
6153 COLOR_PAIR(tc->colorpair), NULL);
6154 waddwstr(view->window, wline);
6155 if (tc)
6156 wattr_off(view->window,
6157 COLOR_PAIR(tc->colorpair), NULL);
6158 if (width < view->ncols - 1)
6159 waddch(view->window, '\n');
6160 if (n == s->selected && view->focussed)
6161 wstandend(view->window);
6162 free(line);
6163 free(wline);
6164 wline = NULL;
6165 n++;
6166 s->ndisplayed++;
6167 s->last_displayed_entry = re;
6169 limit--;
6170 re = TAILQ_NEXT(re, entry);
6173 view_vborder(view);
6174 return err;
6177 static const struct got_error *
6178 browse_ref_tree(struct tog_view **new_view, int begin_x,
6179 struct tog_reflist_entry *re, struct got_repository *repo)
6181 const struct got_error *err = NULL;
6182 struct got_object_id *commit_id = NULL;
6183 struct tog_view *tree_view;
6185 *new_view = NULL;
6187 err = resolve_reflist_entry(&commit_id, re, repo);
6188 if (err) {
6189 if (err->code != GOT_ERR_OBJ_TYPE)
6190 return err;
6191 else
6192 return NULL;
6196 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6197 if (tree_view == NULL) {
6198 err = got_error_from_errno("view_open");
6199 goto done;
6202 err = open_tree_view(tree_view, commit_id,
6203 got_ref_get_name(re->ref), repo);
6204 if (err)
6205 goto done;
6207 *new_view = tree_view;
6208 done:
6209 free(commit_id);
6210 return err;
6212 static const struct got_error *
6213 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6215 const struct got_error *err = NULL;
6216 struct tog_ref_view_state *s = &view->state.ref;
6217 struct tog_view *log_view, *tree_view;
6218 struct tog_reflist_entry *re;
6219 int begin_x = 0, n;
6221 switch (ch) {
6222 case 'i':
6223 s->show_ids = !s->show_ids;
6224 break;
6225 case KEY_ENTER:
6226 case '\r':
6227 if (!s->selected_entry)
6228 break;
6229 if (view_is_parent_view(view))
6230 begin_x = view_split_begin_x(view->begin_x);
6231 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6232 s->repo);
6233 view->focussed = 0;
6234 log_view->focussed = 1;
6235 if (view_is_parent_view(view)) {
6236 err = view_close_child(view);
6237 if (err)
6238 return err;
6239 view_set_child(view, log_view);
6240 view->focus_child = 1;
6241 } else
6242 *new_view = log_view;
6243 break;
6244 case 't':
6245 if (!s->selected_entry)
6246 break;
6247 if (view_is_parent_view(view))
6248 begin_x = view_split_begin_x(view->begin_x);
6249 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6250 s->repo);
6251 if (err || tree_view == NULL)
6252 break;
6253 view->focussed = 0;
6254 tree_view->focussed = 1;
6255 if (view_is_parent_view(view)) {
6256 err = view_close_child(view);
6257 if (err)
6258 return err;
6259 view_set_child(view, tree_view);
6260 view->focus_child = 1;
6261 } else
6262 *new_view = tree_view;
6263 break;
6264 case 'g':
6265 case KEY_HOME:
6266 s->selected = 0;
6267 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6268 break;
6269 case 'G':
6270 case KEY_END:
6271 s->selected = 0;
6272 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6273 for (n = 0; n < view->nlines - 1; n++) {
6274 if (re == NULL)
6275 break;
6276 s->first_displayed_entry = re;
6277 re = TAILQ_PREV(re, tog_reflist_head, entry);
6279 if (n > 0)
6280 s->selected = n - 1;
6281 break;
6282 case 'k':
6283 case KEY_UP:
6284 if (s->selected > 0) {
6285 s->selected--;
6286 break;
6288 ref_scroll_up(s, 1);
6289 break;
6290 case KEY_PPAGE:
6291 case CTRL('b'):
6292 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6293 s->selected = 0;
6294 ref_scroll_up(s, MAX(0, view->nlines - 1));
6295 break;
6296 case 'j':
6297 case KEY_DOWN:
6298 if (s->selected < s->ndisplayed - 1) {
6299 s->selected++;
6300 break;
6302 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6303 /* can't scroll any further */
6304 break;
6305 ref_scroll_down(s, 1);
6306 break;
6307 case KEY_NPAGE:
6308 case CTRL('f'):
6309 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6310 /* can't scroll any further; move cursor down */
6311 if (s->selected < s->ndisplayed - 1)
6312 s->selected = s->ndisplayed - 1;
6313 break;
6315 ref_scroll_down(s, view->nlines - 1);
6316 break;
6317 case CTRL('l'):
6318 tog_free_refs();
6319 err = tog_load_refs(s->repo);
6320 if (err)
6321 break;
6322 ref_view_free_refs(s);
6323 err = ref_view_load_refs(s);
6324 break;
6325 case KEY_RESIZE:
6326 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6327 s->selected = view->nlines - 2;
6328 break;
6329 default:
6330 break;
6333 return err;
6336 __dead static void
6337 usage_ref(void)
6339 endwin();
6340 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6341 getprogname());
6342 exit(1);
6345 static const struct got_error *
6346 cmd_ref(int argc, char *argv[])
6348 const struct got_error *error;
6349 struct got_repository *repo = NULL;
6350 struct got_worktree *worktree = NULL;
6351 char *cwd = NULL, *repo_path = NULL;
6352 int ch;
6353 struct tog_view *view;
6355 while ((ch = getopt(argc, argv, "r:")) != -1) {
6356 switch (ch) {
6357 case 'r':
6358 repo_path = realpath(optarg, NULL);
6359 if (repo_path == NULL)
6360 return got_error_from_errno2("realpath",
6361 optarg);
6362 break;
6363 default:
6364 usage_ref();
6365 /* NOTREACHED */
6369 argc -= optind;
6370 argv += optind;
6372 if (argc > 1)
6373 usage_ref();
6375 if (repo_path == NULL) {
6376 cwd = getcwd(NULL, 0);
6377 if (cwd == NULL)
6378 return got_error_from_errno("getcwd");
6379 error = got_worktree_open(&worktree, cwd);
6380 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6381 goto done;
6382 if (worktree)
6383 repo_path =
6384 strdup(got_worktree_get_repo_path(worktree));
6385 else
6386 repo_path = strdup(cwd);
6387 if (repo_path == NULL) {
6388 error = got_error_from_errno("strdup");
6389 goto done;
6393 error = got_repo_open(&repo, repo_path, NULL);
6394 if (error != NULL)
6395 goto done;
6397 init_curses();
6399 error = apply_unveil(got_repo_get_path(repo), NULL);
6400 if (error)
6401 goto done;
6403 error = tog_load_refs(repo);
6404 if (error)
6405 goto done;
6407 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6408 if (view == NULL) {
6409 error = got_error_from_errno("view_open");
6410 goto done;
6413 error = open_ref_view(view, repo);
6414 if (error)
6415 goto done;
6417 if (worktree) {
6418 /* Release work tree lock. */
6419 got_worktree_close(worktree);
6420 worktree = NULL;
6422 error = view_loop(view);
6423 done:
6424 free(repo_path);
6425 free(cwd);
6426 if (repo) {
6427 const struct got_error *close_err = got_repo_close(repo);
6428 if (close_err)
6429 error = close_err;
6431 tog_free_refs();
6432 return error;
6435 static void
6436 list_commands(FILE *fp)
6438 size_t i;
6440 fprintf(fp, "commands:");
6441 for (i = 0; i < nitems(tog_commands); i++) {
6442 struct tog_cmd *cmd = &tog_commands[i];
6443 fprintf(fp, " %s", cmd->name);
6445 fputc('\n', fp);
6448 __dead static void
6449 usage(int hflag, int status)
6451 FILE *fp = (status == 0) ? stdout : stderr;
6453 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6454 getprogname());
6455 if (hflag) {
6456 fprintf(fp, "lazy usage: %s path\n", getprogname());
6457 list_commands(fp);
6459 exit(status);
6462 static char **
6463 make_argv(int argc, ...)
6465 va_list ap;
6466 char **argv;
6467 int i;
6469 va_start(ap, argc);
6471 argv = calloc(argc, sizeof(char *));
6472 if (argv == NULL)
6473 err(1, "calloc");
6474 for (i = 0; i < argc; i++) {
6475 argv[i] = strdup(va_arg(ap, char *));
6476 if (argv[i] == NULL)
6477 err(1, "strdup");
6480 va_end(ap);
6481 return argv;
6485 * Try to convert 'tog path' into a 'tog log path' command.
6486 * The user could simply have mistyped the command rather than knowingly
6487 * provided a path. So check whether argv[0] can in fact be resolved
6488 * to a path in the HEAD commit and print a special error if not.
6489 * This hack is for mpi@ <3
6491 static const struct got_error *
6492 tog_log_with_path(int argc, char *argv[])
6494 const struct got_error *error = NULL, *close_err;
6495 struct tog_cmd *cmd = NULL;
6496 struct got_repository *repo = NULL;
6497 struct got_worktree *worktree = NULL;
6498 struct got_object_id *commit_id = NULL, *id = NULL;
6499 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6500 char *commit_id_str = NULL, **cmd_argv = NULL;
6502 cwd = getcwd(NULL, 0);
6503 if (cwd == NULL)
6504 return got_error_from_errno("getcwd");
6506 error = got_worktree_open(&worktree, cwd);
6507 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6508 goto done;
6510 if (worktree)
6511 repo_path = strdup(got_worktree_get_repo_path(worktree));
6512 else
6513 repo_path = strdup(cwd);
6514 if (repo_path == NULL) {
6515 error = got_error_from_errno("strdup");
6516 goto done;
6519 error = got_repo_open(&repo, repo_path, NULL);
6520 if (error != NULL)
6521 goto done;
6523 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6524 repo, worktree);
6525 if (error)
6526 goto done;
6528 error = tog_load_refs(repo);
6529 if (error)
6530 goto done;
6531 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6532 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6533 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6534 if (error)
6535 goto done;
6537 if (worktree) {
6538 got_worktree_close(worktree);
6539 worktree = NULL;
6542 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6543 if (error) {
6544 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6545 goto done;
6546 fprintf(stderr, "%s: '%s' is no known command or path\n",
6547 getprogname(), argv[0]);
6548 usage(1, 1);
6549 /* not reached */
6552 close_err = got_repo_close(repo);
6553 if (error == NULL)
6554 error = close_err;
6555 repo = NULL;
6557 error = got_object_id_str(&commit_id_str, commit_id);
6558 if (error)
6559 goto done;
6561 cmd = &tog_commands[0]; /* log */
6562 argc = 4;
6563 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6564 error = cmd->cmd_main(argc, cmd_argv);
6565 done:
6566 if (repo) {
6567 close_err = got_repo_close(repo);
6568 if (error == NULL)
6569 error = close_err;
6571 if (worktree)
6572 got_worktree_close(worktree);
6573 free(id);
6574 free(commit_id_str);
6575 free(commit_id);
6576 free(cwd);
6577 free(repo_path);
6578 free(in_repo_path);
6579 if (cmd_argv) {
6580 int i;
6581 for (i = 0; i < argc; i++)
6582 free(cmd_argv[i]);
6583 free(cmd_argv);
6585 tog_free_refs();
6586 return error;
6589 int
6590 main(int argc, char *argv[])
6592 const struct got_error *error = NULL;
6593 struct tog_cmd *cmd = NULL;
6594 int ch, hflag = 0, Vflag = 0;
6595 char **cmd_argv = NULL;
6596 static struct option longopts[] = {
6597 { "version", no_argument, NULL, 'V' },
6598 { NULL, 0, NULL, 0}
6601 setlocale(LC_CTYPE, "");
6603 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6604 switch (ch) {
6605 case 'h':
6606 hflag = 1;
6607 break;
6608 case 'V':
6609 Vflag = 1;
6610 break;
6611 default:
6612 usage(hflag, 1);
6613 /* NOTREACHED */
6617 argc -= optind;
6618 argv += optind;
6619 optind = 1;
6620 optreset = 1;
6622 if (Vflag) {
6623 got_version_print_str();
6624 return 0;
6627 #ifndef PROFILE
6628 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6629 NULL) == -1)
6630 err(1, "pledge");
6631 #endif
6633 if (argc == 0) {
6634 if (hflag)
6635 usage(hflag, 0);
6636 /* Build an argument vector which runs a default command. */
6637 cmd = &tog_commands[0];
6638 argc = 1;
6639 cmd_argv = make_argv(argc, cmd->name);
6640 } else {
6641 size_t i;
6643 /* Did the user specify a command? */
6644 for (i = 0; i < nitems(tog_commands); i++) {
6645 if (strncmp(tog_commands[i].name, argv[0],
6646 strlen(argv[0])) == 0) {
6647 cmd = &tog_commands[i];
6648 break;
6653 if (cmd == NULL) {
6654 if (argc != 1)
6655 usage(0, 1);
6656 /* No command specified; try log with a path */
6657 error = tog_log_with_path(argc, argv);
6658 } else {
6659 if (hflag)
6660 cmd->cmd_usage();
6661 else
6662 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6665 endwin();
6666 putchar('\n');
6667 if (cmd_argv) {
6668 int i;
6669 for (i = 0; i < argc; i++)
6670 free(cmd_argv[i]);
6671 free(cmd_argv);
6674 if (error && error->code != GOT_ERR_CANCELLED)
6675 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6676 return 0;