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
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.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 SIMPLEQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 SIMPLEQ_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 SIMPLEQ_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 (!SIMPLEQ_EMPTY(colors)) {
194 tc = SIMPLEQ_FIRST(colors);
195 SIMPLEQ_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 SIMPLEQ_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 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
312 int log_complete;
313 sig_atomic_t *quit;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
316 int *searching;
317 int *search_next_done;
318 regex_t *regex;
319 };
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
326 int selected;
327 char *in_repo_path;
328 char *head_ref_name;
329 int log_branches;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
332 sig_atomic_t quit;
333 pthread_t thread;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
338 };
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
357 int nlines;
359 struct tog_view *view;
360 struct got_object_id *commit_id;
361 int *quit;
362 };
364 struct tog_blame_thread_args {
365 const char *path;
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
368 int *complete;
369 got_cancel_cb cancel_cb;
370 void *cancel_arg;
371 };
373 struct tog_blame {
374 FILE *f;
375 off_t filesize;
376 struct tog_blame_line *lines;
377 int nlines;
378 off_t *line_offsets;
379 pthread_t thread;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
382 const char *path;
383 };
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
388 int selected_line;
389 int blame_complete;
390 int eof;
391 int done;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
394 char *path;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
398 int matched_line;
399 struct tog_colors colors;
400 };
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
407 int selected;
408 };
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
413 char *tree_label;
414 struct got_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
422 char *head_ref_name;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
426 };
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
431 int idx;
432 };
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
445 };
447 /*
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
463 */
464 TAILQ_HEAD(tog_view_list_head, tog_view);
466 struct tog_view {
467 TAILQ_ENTRY(tog_view) entry;
468 WINDOW *window;
469 PANEL *panel;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
473 int dying;
474 struct tog_view *parent;
475 struct tog_view *child;
477 /*
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
484 * situations.
485 */
486 int focus_child;
488 /* type-specific state */
489 enum tog_view_type type;
490 union {
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
496 } state;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
505 int search_started;
506 int searching;
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
513 regex_t regex;
514 regmatch_t regmatch;
515 };
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_tree_object *, struct got_object_id *, const char *,
549 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 pthread_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 ERR:
890 break;
891 case '\t':
892 if (view->child) {
893 view->focussed = 0;
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
897 view->focussed = 0;
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
901 break;
902 case 'q':
903 err = view->input(new, view, ch);
904 view->dying = 1;
905 break;
906 case 'Q':
907 *done = 1;
908 break;
909 case 'f':
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
912 break;
913 if (view_is_splitscreen(view->child)) {
914 view->focussed = 0;
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
917 } else
918 err = view_splitscreen(view->child);
919 if (err)
920 break;
921 err = view->child->input(new, view->child,
922 KEY_RESIZE);
923 } else {
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
926 view->focussed = 1;
927 err = view_fullscreen(view);
928 } else {
929 err = view_splitscreen(view);
931 if (err)
932 break;
933 err = view->input(new, view, KEY_RESIZE);
935 break;
936 case KEY_RESIZE:
937 break;
938 case '/':
939 if (view->search_start)
940 view_search_start(view);
941 else
942 err = view->input(new, view, ch);
943 break;
944 case 'N':
945 case 'n':
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
951 } else
952 err = view->input(new, view, ch);
953 break;
954 default:
955 err = view->input(new, view, ch);
956 break;
959 return err;
962 void
963 view_vborder(struct tog_view *view)
965 PANEL *panel;
966 const struct tog_view *view_above;
968 if (view->parent)
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
972 if (panel == NULL)
973 return;
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 int
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
985 return 0;
986 if (!view_is_splitscreen(view->child))
987 return 0;
988 } else if (!view_is_splitscreen(view))
989 return 0;
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1004 if (errcode)
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 TAILQ_INIT(&views);
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1010 view->focussed = 1;
1011 err = view->show(view);
1012 if (err)
1013 return err;
1014 update_panels();
1015 doupdate();
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1022 if (err)
1023 break;
1024 if (view->dying) {
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1029 entry);
1030 else if (view->parent)
1031 prev = view->parent;
1033 if (view->parent) {
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1036 } else
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1040 if (err)
1041 goto done;
1043 view = NULL;
1044 TAILQ_FOREACH(v, &views, entry) {
1045 if (v->focussed)
1046 break;
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1050 if (prev)
1051 view = prev;
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1056 if (view) {
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1059 view = view->child;
1060 } else
1061 view->focussed = 1;
1065 if (new_view) {
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1070 continue;
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1073 if (err)
1074 goto done;
1075 break;
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1078 view = new_view;
1080 if (view) {
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1083 view = view->child;
1084 } else {
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1093 if (err)
1094 goto done;
1096 err = view->show(view);
1097 if (err)
1098 goto done;
1099 if (view->child) {
1100 err = view->child->show(view->child);
1101 if (err)
1102 goto done;
1104 update_panels();
1105 doupdate();
1108 done:
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1112 view_close(view);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1119 return err;
1122 __dead static void
1123 usage_log(void)
1125 endwin();
1126 fprintf(stderr,
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 getprogname());
1129 exit(1);
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 char *vis = NULL;
1137 const struct got_error *err = NULL;
1139 *ws = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1142 int vislen;
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1148 if (err)
1149 return err;
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1153 goto done;
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1158 if (*ws == NULL) {
1159 err = got_error_from_errno("calloc");
1160 goto done;
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1165 done:
1166 free(vis);
1167 if (err) {
1168 free(*ws);
1169 *ws = NULL;
1170 *wlen = 0;
1172 return err;
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 int col_tab_align)
1180 const struct got_error *err = NULL;
1181 int cols = 0;
1182 wchar_t *wline = NULL;
1183 size_t wlen;
1184 int i;
1186 *wlinep = NULL;
1187 *widthp = 0;
1189 err = mbs2ws(&wline, &wlen, line);
1190 if (err)
1191 return err;
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1195 wlen--;
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1199 wlen--;
1202 i = 0;
1203 while (i < wlen) {
1204 int width = wcwidth(wline[i]);
1206 if (width == 0) {
1207 i++;
1208 continue;
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1213 break;
1214 cols += width;
1215 i++;
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1218 width = TABSIZE -
1219 ((cols + col_tab_align) % TABSIZE);
1220 } else {
1221 width = 1;
1222 wline[i] = L'.';
1224 if (cols + width > wlimit)
1225 break;
1226 cols += width;
1227 i++;
1228 } else {
1229 err = got_error_from_errno("wcwidth");
1230 goto done;
1233 wline[i] = L'\0';
1234 if (widthp)
1235 *widthp = cols;
1236 done:
1237 if (err)
1238 free(wline);
1239 else
1240 *wlinep = wline;
1241 return err;
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1250 char *s;
1251 const char *name;
1253 *refs_str = NULL;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1258 int cmp;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1262 continue;
1263 if (strncmp(name, "refs/", 5) == 0)
1264 name += 5;
1265 if (strncmp(name, "got/", 4) == 0)
1266 continue;
1267 if (strncmp(name, "heads/", 6) == 0)
1268 name += 6;
1269 if (strncmp(name, "remotes/", 8) == 0) {
1270 name += 8;
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1273 continue;
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1276 if (err)
1277 break;
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1280 if (err) {
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1282 free(ref_id);
1283 break;
1285 /* Ref points at something other than a tag. */
1286 err = NULL;
1287 tag = NULL;
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1292 free(ref_id);
1293 if (tag)
1294 got_object_tag_close(tag);
1295 if (cmp != 0)
1296 continue;
1297 s = *refs_str;
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 free(s);
1302 *refs_str = NULL;
1303 break;
1305 free(s);
1308 return err;
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 int col_tab_align)
1315 char *smallerthan;
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1337 int col, limit;
1338 const int avail = view->ncols;
1339 struct tm tm;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (localtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("localtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 return got_error(GOT_ERR_NO_SPACE);
1349 if (avail <= date_display_cols)
1350 limit = MIN(sizeof(datebuf) - 1, avail);
1351 else
1352 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 if (tc)
1355 wattr_on(view->window,
1356 COLOR_PAIR(tc->colorpair), NULL);
1357 waddnstr(view->window, datebuf, limit);
1358 if (tc)
1359 wattr_off(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 col = limit;
1362 if (col > avail)
1363 goto done;
1365 if (avail >= 120) {
1366 char *id_str;
1367 err = got_object_id_str(&id_str, id);
1368 if (err)
1369 goto done;
1370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 if (tc)
1372 wattr_on(view->window,
1373 COLOR_PAIR(tc->colorpair), NULL);
1374 wprintw(view->window, "%.8s ", id_str);
1375 if (tc)
1376 wattr_off(view->window,
1377 COLOR_PAIR(tc->colorpair), NULL);
1378 free(id_str);
1379 col += 9;
1380 if (col > avail)
1381 goto done;
1384 author = strdup(got_object_commit_get_author(commit));
1385 if (author == NULL) {
1386 err = got_error_from_errno("strdup");
1387 goto done;
1389 err = format_author(&wauthor, &author_width, author, avail - col, col);
1390 if (err)
1391 goto done;
1392 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 if (tc)
1394 wattr_on(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 waddwstr(view->window, wauthor);
1397 if (tc)
1398 wattr_off(view->window,
1399 COLOR_PAIR(tc->colorpair), NULL);
1400 col += author_width;
1401 while (col < avail && author_width < author_display_cols + 2) {
1402 waddch(view->window, ' ');
1403 col++;
1404 author_width++;
1406 if (col > avail)
1407 goto done;
1409 err = got_object_commit_get_logmsg(&logmsg0, commit);
1410 if (err)
1411 goto done;
1412 logmsg = logmsg0;
1413 while (*logmsg == '\n')
1414 logmsg++;
1415 newline = strchr(logmsg, '\n');
1416 if (newline)
1417 *newline = '\0';
1418 limit = avail - col;
1419 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1420 if (err)
1421 goto done;
1422 waddwstr(view->window, wlogmsg);
1423 col += logmsg_width;
1424 while (col < avail) {
1425 waddch(view->window, ' ');
1426 col++;
1428 done:
1429 free(logmsg0);
1430 free(wlogmsg);
1431 free(author);
1432 free(wauthor);
1433 free(line);
1434 return err;
1437 static struct commit_queue_entry *
1438 alloc_commit_queue_entry(struct got_commit_object *commit,
1439 struct got_object_id *id)
1441 struct commit_queue_entry *entry;
1443 entry = calloc(1, sizeof(*entry));
1444 if (entry == NULL)
1445 return NULL;
1447 entry->id = id;
1448 entry->commit = commit;
1449 return entry;
1452 static void
1453 pop_commit(struct commit_queue *commits)
1455 struct commit_queue_entry *entry;
1457 entry = TAILQ_FIRST(&commits->head);
1458 TAILQ_REMOVE(&commits->head, entry, entry);
1459 got_object_commit_close(entry->commit);
1460 commits->ncommits--;
1461 /* Don't free entry->id! It is owned by the commit graph. */
1462 free(entry);
1465 static void
1466 free_commits(struct commit_queue *commits)
1468 while (!TAILQ_EMPTY(&commits->head))
1469 pop_commit(commits);
1472 static const struct got_error *
1473 match_commit(int *have_match, struct got_object_id *id,
1474 struct got_commit_object *commit, regex_t *regex)
1476 const struct got_error *err = NULL;
1477 regmatch_t regmatch;
1478 char *id_str = NULL, *logmsg = NULL;
1480 *have_match = 0;
1482 err = got_object_id_str(&id_str, id);
1483 if (err)
1484 return err;
1486 err = got_object_commit_get_logmsg(&logmsg, commit);
1487 if (err)
1488 goto done;
1490 if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 &regmatch, 0) == 0 ||
1492 regexec(regex, got_object_commit_get_committer(commit), 1,
1493 &regmatch, 0) == 0 ||
1494 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1495 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1496 *have_match = 1;
1497 done:
1498 free(id_str);
1499 free(logmsg);
1500 return err;
1503 static const struct got_error *
1504 queue_commits(struct tog_log_thread_args *a)
1506 const struct got_error *err = NULL;
1509 * We keep all commits open throughout the lifetime of the log
1510 * view in order to avoid having to re-fetch commits from disk
1511 * while updating the display.
1513 do {
1514 struct got_object_id *id;
1515 struct got_commit_object *commit;
1516 struct commit_queue_entry *entry;
1517 int errcode;
1519 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 NULL, NULL);
1521 if (err || id == NULL)
1522 break;
1524 err = got_object_open_as_commit(&commit, a->repo, id);
1525 if (err)
1526 break;
1527 entry = alloc_commit_queue_entry(commit, id);
1528 if (entry == NULL) {
1529 err = got_error_from_errno("alloc_commit_queue_entry");
1530 break;
1533 errcode = pthread_mutex_lock(&tog_mutex);
1534 if (errcode) {
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1537 break;
1540 entry->idx = a->commits->ncommits;
1541 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 a->commits->ncommits++;
1544 if (*a->searching == TOG_SEARCH_FORWARD &&
1545 !*a->search_next_done) {
1546 int have_match;
1547 err = match_commit(&have_match, id, commit, a->regex);
1548 if (err)
1549 break;
1550 if (have_match)
1551 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 errcode = pthread_mutex_unlock(&tog_mutex);
1555 if (errcode && err == NULL)
1556 err = got_error_set_errno(errcode,
1557 "pthread_mutex_unlock");
1558 if (err)
1559 break;
1560 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1562 return err;
1565 static void
1566 select_commit(struct tog_log_view_state *s)
1568 struct commit_queue_entry *entry;
1569 int ncommits = 0;
1571 entry = s->first_displayed_entry;
1572 while (entry) {
1573 if (ncommits == s->selected) {
1574 s->selected_entry = entry;
1575 break;
1577 entry = TAILQ_NEXT(entry, entry);
1578 ncommits++;
1582 static const struct got_error *
1583 draw_commits(struct tog_view *view)
1585 const struct got_error *err = NULL;
1586 struct tog_log_view_state *s = &view->state.log;
1587 struct commit_queue_entry *entry = s->selected_entry;
1588 const int limit = view->nlines;
1589 int width;
1590 int ncommits, author_cols = 4;
1591 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 char *refs_str = NULL;
1593 wchar_t *wline;
1594 struct tog_color *tc;
1595 static const size_t date_display_cols = 12;
1597 if (s->selected_entry &&
1598 !(view->searching && view->search_next_done == 0)) {
1599 struct got_reflist_head *refs;
1600 err = got_object_id_str(&id_str, s->selected_entry->id);
1601 if (err)
1602 return err;
1603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 s->selected_entry->id);
1605 if (refs) {
1606 err = build_refs_str(&refs_str, refs,
1607 s->selected_entry->id, s->repo);
1608 if (err)
1609 goto done;
1613 if (s->thread_args.commits_needed == 0)
1614 halfdelay(10); /* disable fast refresh */
1616 if (s->thread_args.commits_needed > 0) {
1617 if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 (view->searching && !view->search_next_done) ?
1620 "searching..." : "loading...") == -1) {
1621 err = got_error_from_errno("asprintf");
1622 goto done;
1624 } else {
1625 const char *search_str = NULL;
1627 if (view->searching) {
1628 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 search_str = "no more matches";
1630 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 search_str = "no matches found";
1632 else if (!view->search_next_done)
1633 search_str = "searching...";
1636 if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 search_str ? search_str :
1639 (refs_str ? refs_str : "")) == -1) {
1640 err = got_error_from_errno("asprintf");
1641 goto done;
1645 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 if (asprintf(&header, "commit %s %s%s",
1647 id_str ? id_str : "........................................",
1648 s->in_repo_path, ncommits_str) == -1) {
1649 err = got_error_from_errno("asprintf");
1650 header = NULL;
1651 goto done;
1653 } else if (asprintf(&header, "commit %s%s",
1654 id_str ? id_str : "........................................",
1655 ncommits_str) == -1) {
1656 err = got_error_from_errno("asprintf");
1657 header = NULL;
1658 goto done;
1660 err = format_line(&wline, &width, header, view->ncols, 0);
1661 if (err)
1662 goto done;
1664 werase(view->window);
1666 if (view_needs_focus_indication(view))
1667 wstandout(view->window);
1668 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 if (tc)
1670 wattr_on(view->window,
1671 COLOR_PAIR(tc->colorpair), NULL);
1672 waddwstr(view->window, wline);
1673 if (tc)
1674 wattr_off(view->window,
1675 COLOR_PAIR(tc->colorpair), NULL);
1676 while (width < view->ncols) {
1677 waddch(view->window, ' ');
1678 width++;
1680 if (view_needs_focus_indication(view))
1681 wstandend(view->window);
1682 free(wline);
1683 if (limit <= 1)
1684 goto done;
1686 /* Grow author column size if necessary. */
1687 entry = s->first_displayed_entry;
1688 ncommits = 0;
1689 while (entry) {
1690 char *author;
1691 wchar_t *wauthor;
1692 int width;
1693 if (ncommits >= limit - 1)
1694 break;
1695 author = strdup(got_object_commit_get_author(entry->commit));
1696 if (author == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 err = format_author(&wauthor, &width, author, COLS,
1701 date_display_cols);
1702 if (author_cols < width)
1703 author_cols = width;
1704 free(wauthor);
1705 free(author);
1706 ncommits++;
1707 entry = TAILQ_NEXT(entry, entry);
1710 entry = s->first_displayed_entry;
1711 s->last_displayed_entry = s->first_displayed_entry;
1712 ncommits = 0;
1713 while (entry) {
1714 if (ncommits >= limit - 1)
1715 break;
1716 if (ncommits == s->selected)
1717 wstandout(view->window);
1718 err = draw_commit(view, entry->commit, entry->id,
1719 date_display_cols, author_cols);
1720 if (ncommits == s->selected)
1721 wstandend(view->window);
1722 if (err)
1723 goto done;
1724 ncommits++;
1725 s->last_displayed_entry = entry;
1726 entry = TAILQ_NEXT(entry, entry);
1729 view_vborder(view);
1730 done:
1731 free(id_str);
1732 free(refs_str);
1733 free(ncommits_str);
1734 free(header);
1735 return err;
1738 static void
1739 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 struct commit_queue_entry *entry;
1742 int nscrolled = 0;
1744 entry = TAILQ_FIRST(&s->commits.head);
1745 if (s->first_displayed_entry == entry)
1746 return;
1748 entry = s->first_displayed_entry;
1749 while (entry && nscrolled < maxscroll) {
1750 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 if (entry) {
1752 s->first_displayed_entry = entry;
1753 nscrolled++;
1758 static const struct got_error *
1759 trigger_log_thread(struct tog_view *view, int wait)
1761 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1762 int errcode;
1764 halfdelay(1); /* fast refresh while loading commits */
1766 while (ta->commits_needed > 0) {
1767 if (ta->log_complete)
1768 break;
1770 /* Wake the log thread. */
1771 errcode = pthread_cond_signal(&ta->need_commits);
1772 if (errcode)
1773 return got_error_set_errno(errcode,
1774 "pthread_cond_signal");
1777 * The mutex will be released while the view loop waits
1778 * in wgetch(), at which time the log thread will run.
1780 if (!wait)
1781 break;
1783 /* Display progress update in log view. */
1784 show_log_view(view);
1785 update_panels();
1786 doupdate();
1788 /* Wait right here while next commit is being loaded. */
1789 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 if (errcode)
1791 return got_error_set_errno(errcode,
1792 "pthread_cond_wait");
1794 /* Display progress update in log view. */
1795 show_log_view(view);
1796 update_panels();
1797 doupdate();
1800 return NULL;
1803 static const struct got_error *
1804 log_scroll_down(struct tog_view *view, int maxscroll)
1806 struct tog_log_view_state *s = &view->state.log;
1807 const struct got_error *err = NULL;
1808 struct commit_queue_entry *pentry;
1809 int nscrolled = 0, ncommits_needed;
1811 if (s->last_displayed_entry == NULL)
1812 return NULL;
1814 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 if (s->commits.ncommits < ncommits_needed &&
1816 !s->thread_args.log_complete) {
1818 * Ask the log thread for required amount of commits.
1820 s->thread_args.commits_needed += maxscroll;
1821 err = trigger_log_thread(view, 1);
1822 if (err)
1823 return err;
1826 do {
1827 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1828 if (pentry == NULL)
1829 break;
1831 s->last_displayed_entry = pentry;
1833 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1834 if (pentry == NULL)
1835 break;
1836 s->first_displayed_entry = pentry;
1837 } while (++nscrolled < maxscroll);
1839 return err;
1842 static const struct got_error *
1843 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 struct got_commit_object *commit, struct got_object_id *commit_id,
1845 struct tog_view *log_view, struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_object_qid *parent_id;
1849 struct tog_view *diff_view;
1851 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 if (diff_view == NULL)
1853 return got_error_from_errno("view_open");
1855 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 if (err == NULL)
1859 *new_view = diff_view;
1860 return err;
1863 static const struct got_error *
1864 tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 struct got_tree_object *subtree)
1867 struct tog_parent_tree *parent;
1869 parent = calloc(1, sizeof(*parent));
1870 if (parent == NULL)
1871 return got_error_from_errno("calloc");
1873 parent->tree = s->tree;
1874 parent->first_displayed_entry = s->first_displayed_entry;
1875 parent->selected_entry = s->selected_entry;
1876 parent->selected = s->selected;
1877 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1878 s->tree = subtree;
1879 s->selected = 0;
1880 s->first_displayed_entry = NULL;
1881 return NULL;
1884 static const struct got_error *
1885 tree_view_walk_path(struct tog_tree_view_state *s,
1886 struct got_object_id *commit_id, const char *path)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree = NULL;
1890 const char *p;
1891 char *slash, *subpath = NULL;
1893 /* Walk the path and open corresponding tree objects. */
1894 p = path;
1895 while (*p) {
1896 struct got_tree_entry *te;
1897 struct got_object_id *tree_id;
1898 char *te_name;
1900 while (p[0] == '/')
1901 p++;
1903 /* Ensure the correct subtree entry is selected. */
1904 slash = strchr(p, '/');
1905 if (slash == NULL)
1906 te_name = strdup(p);
1907 else
1908 te_name = strndup(p, slash - p);
1909 if (te_name == NULL) {
1910 err = got_error_from_errno("strndup");
1911 break;
1913 te = got_object_tree_find_entry(s->tree, te_name);
1914 if (te == NULL) {
1915 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1916 free(te_name);
1917 break;
1919 free(te_name);
1920 s->first_displayed_entry = s->selected_entry = te;
1922 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 break; /* jump to this file's entry */
1925 slash = strchr(p, '/');
1926 if (slash)
1927 subpath = strndup(path, slash - path);
1928 else
1929 subpath = strdup(path);
1930 if (subpath == NULL) {
1931 err = got_error_from_errno("strdup");
1932 break;
1935 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1936 subpath);
1937 if (err)
1938 break;
1940 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1941 free(tree_id);
1942 if (err)
1943 break;
1945 err = tree_view_visit_subtree(s, tree);
1946 if (err) {
1947 got_object_tree_close(tree);
1948 break;
1950 if (slash == NULL)
1951 break;
1952 free(subpath);
1953 subpath = NULL;
1954 p = slash;
1957 free(subpath);
1958 return err;
1961 static const struct got_error *
1962 browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 struct commit_queue_entry *entry, const char *path,
1964 const char *head_ref_name, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_tree_object *tree;
1968 struct tog_tree_view_state *s;
1969 struct tog_view *tree_view;
1971 err = got_object_open_as_tree(&tree, repo,
1972 got_object_commit_get_tree_id(entry->commit));
1973 if (err)
1974 return err;
1976 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1977 if (tree_view == NULL)
1978 return got_error_from_errno("view_open");
1980 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1981 if (err) {
1982 got_object_tree_close(tree);
1983 return err;
1985 s = &tree_view->state.tree;
1987 *new_view = tree_view;
1989 if (got_path_is_root_dir(path))
1990 return NULL;
1992 return tree_view_walk_path(s, entry->id, path);
1995 static const struct got_error *
1996 block_signals_used_by_main_thread(void)
1998 sigset_t sigset;
1999 int errcode;
2001 if (sigemptyset(&sigset) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset, SIGWINCH) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset, SIGCONT) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset, SIGTSTP) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2015 if (errcode)
2016 return got_error_set_errno(errcode, "pthread_sigmask");
2018 return NULL;
2021 static void *
2022 log_thread(void *arg)
2024 const struct got_error *err = NULL;
2025 int errcode = 0;
2026 struct tog_log_thread_args *a = arg;
2027 int done = 0;
2029 err = block_signals_used_by_main_thread();
2030 if (err)
2031 return (void *)err;
2033 while (!done && !err && !tog_sigpipe_received) {
2034 err = queue_commits(a);
2035 if (err) {
2036 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 return (void *)err;
2038 err = NULL;
2039 done = 1;
2040 } else if (a->commits_needed > 0)
2041 a->commits_needed--;
2043 errcode = pthread_mutex_lock(&tog_mutex);
2044 if (errcode) {
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_lock");
2047 break;
2048 } else if (*a->quit)
2049 done = 1;
2050 else if (*a->first_displayed_entry == NULL) {
2051 *a->first_displayed_entry =
2052 TAILQ_FIRST(&a->commits->head);
2053 *a->selected_entry = *a->first_displayed_entry;
2056 errcode = pthread_cond_signal(&a->commit_loaded);
2057 if (errcode) {
2058 err = got_error_set_errno(errcode,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex);
2061 break;
2064 if (done)
2065 a->commits_needed = 0;
2066 else {
2067 if (a->commits_needed == 0) {
2068 errcode = pthread_cond_wait(&a->need_commits,
2069 &tog_mutex);
2070 if (errcode)
2071 err = got_error_set_errno(errcode,
2072 "pthread_cond_wait");
2073 if (*a->quit)
2074 done = 1;
2078 errcode = pthread_mutex_unlock(&tog_mutex);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode,
2081 "pthread_mutex_unlock");
2083 a->log_complete = 1;
2084 return (void *)err;
2087 static const struct got_error *
2088 stop_log_thread(struct tog_log_view_state *s)
2090 const struct got_error *err = NULL;
2091 int errcode;
2093 if (s->thread) {
2094 s->quit = 1;
2095 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2096 if (errcode)
2097 return got_error_set_errno(errcode,
2098 "pthread_cond_signal");
2099 errcode = pthread_mutex_unlock(&tog_mutex);
2100 if (errcode)
2101 return got_error_set_errno(errcode,
2102 "pthread_mutex_unlock");
2103 errcode = pthread_join(s->thread, (void **)&err);
2104 if (errcode)
2105 return got_error_set_errno(errcode, "pthread_join");
2106 errcode = pthread_mutex_lock(&tog_mutex);
2107 if (errcode)
2108 return got_error_set_errno(errcode,
2109 "pthread_mutex_lock");
2110 s->thread = NULL;
2113 if (s->thread_args.repo) {
2114 err = got_repo_close(s->thread_args.repo);
2115 s->thread_args.repo = NULL;
2118 if (s->thread_args.graph) {
2119 got_commit_graph_close(s->thread_args.graph);
2120 s->thread_args.graph = NULL;
2123 return err;
2126 static const struct got_error *
2127 close_log_view(struct tog_view *view)
2129 const struct got_error *err = NULL;
2130 struct tog_log_view_state *s = &view->state.log;
2131 int errcode;
2133 err = stop_log_thread(s);
2135 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2136 if (errcode && err == NULL)
2137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2139 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2140 if (errcode && err == NULL)
2141 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2143 free_commits(&s->commits);
2144 free(s->in_repo_path);
2145 s->in_repo_path = NULL;
2146 free(s->start_id);
2147 s->start_id = NULL;
2148 free(s->head_ref_name);
2149 s->head_ref_name = NULL;
2150 return err;
2153 static const struct got_error *
2154 search_start_log_view(struct tog_view *view)
2156 struct tog_log_view_state *s = &view->state.log;
2158 s->matched_entry = NULL;
2159 s->search_entry = NULL;
2160 return NULL;
2163 static const struct got_error *
2164 search_next_log_view(struct tog_view *view)
2166 const struct got_error *err = NULL;
2167 struct tog_log_view_state *s = &view->state.log;
2168 struct commit_queue_entry *entry;
2170 /* Display progress update in log view. */
2171 show_log_view(view);
2172 update_panels();
2173 doupdate();
2175 if (s->search_entry) {
2176 int errcode, ch;
2177 errcode = pthread_mutex_unlock(&tog_mutex);
2178 if (errcode)
2179 return got_error_set_errno(errcode,
2180 "pthread_mutex_unlock");
2181 ch = wgetch(view->window);
2182 errcode = pthread_mutex_lock(&tog_mutex);
2183 if (errcode)
2184 return got_error_set_errno(errcode,
2185 "pthread_mutex_lock");
2186 if (ch == KEY_BACKSPACE) {
2187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2188 return NULL;
2190 if (view->searching == TOG_SEARCH_FORWARD)
2191 entry = TAILQ_NEXT(s->search_entry, entry);
2192 else
2193 entry = TAILQ_PREV(s->search_entry,
2194 commit_queue_head, entry);
2195 } else if (s->matched_entry) {
2196 if (view->searching == TOG_SEARCH_FORWARD)
2197 entry = TAILQ_NEXT(s->matched_entry, entry);
2198 else
2199 entry = TAILQ_PREV(s->matched_entry,
2200 commit_queue_head, entry);
2201 } else {
2202 if (view->searching == TOG_SEARCH_FORWARD)
2203 entry = TAILQ_FIRST(&s->commits.head);
2204 else
2205 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 while (1) {
2209 int have_match = 0;
2211 if (entry == NULL) {
2212 if (s->thread_args.log_complete ||
2213 view->searching == TOG_SEARCH_BACKWARD) {
2214 view->search_next_done =
2215 (s->matched_entry == NULL ?
2216 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2217 s->search_entry = NULL;
2218 return NULL;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s->thread_args.commits_needed++;
2226 return trigger_log_thread(view, 0);
2229 err = match_commit(&have_match, entry->id, entry->commit,
2230 &view->regex);
2231 if (err)
2232 break;
2233 if (have_match) {
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 s->matched_entry = entry;
2236 break;
2239 s->search_entry = entry;
2240 if (view->searching == TOG_SEARCH_FORWARD)
2241 entry = TAILQ_NEXT(entry, entry);
2242 else
2243 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2246 if (s->matched_entry) {
2247 int cur = s->selected_entry->idx;
2248 while (cur < s->matched_entry->idx) {
2249 err = input_log_view(NULL, view, KEY_DOWN);
2250 if (err)
2251 return err;
2252 cur++;
2254 while (cur > s->matched_entry->idx) {
2255 err = input_log_view(NULL, view, KEY_UP);
2256 if (err)
2257 return err;
2258 cur--;
2262 s->search_entry = NULL;
2264 return NULL;
2267 static const struct got_error *
2268 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2269 struct got_repository *repo, const char *head_ref_name,
2270 const char *in_repo_path, int log_branches)
2272 const struct got_error *err = NULL;
2273 struct tog_log_view_state *s = &view->state.log;
2274 struct got_repository *thread_repo = NULL;
2275 struct got_commit_graph *thread_graph = NULL;
2276 int errcode;
2278 if (in_repo_path != s->in_repo_path) {
2279 free(s->in_repo_path);
2280 s->in_repo_path = strdup(in_repo_path);
2281 if (s->in_repo_path == NULL)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s->commits.head);
2287 s->commits.ncommits = 0;
2289 s->repo = repo;
2290 if (head_ref_name) {
2291 s->head_ref_name = strdup(head_ref_name);
2292 if (s->head_ref_name == NULL) {
2293 err = got_error_from_errno("strdup");
2294 goto done;
2297 s->start_id = got_object_id_dup(start_id);
2298 if (s->start_id == NULL) {
2299 err = got_error_from_errno("got_object_id_dup");
2300 goto done;
2302 s->log_branches = log_branches;
2304 SIMPLEQ_INIT(&s->colors);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2306 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2307 get_color_value("TOG_COLOR_COMMIT"));
2308 if (err)
2309 goto done;
2310 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2312 if (err) {
2313 free_colors(&s->colors);
2314 goto done;
2316 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2317 get_color_value("TOG_COLOR_DATE"));
2318 if (err) {
2319 free_colors(&s->colors);
2320 goto done;
2324 view->show = show_log_view;
2325 view->input = input_log_view;
2326 view->close = close_log_view;
2327 view->search_start = search_start_log_view;
2328 view->search_next = search_next_log_view;
2330 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2331 if (err)
2332 goto done;
2333 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 !s->log_branches);
2335 if (err)
2336 goto done;
2337 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2338 s->repo, NULL, NULL);
2339 if (err)
2340 goto done;
2342 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2343 if (errcode) {
2344 err = got_error_set_errno(errcode, "pthread_cond_init");
2345 goto done;
2347 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2348 if (errcode) {
2349 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 goto done;
2353 s->thread_args.commits_needed = view->nlines;
2354 s->thread_args.graph = thread_graph;
2355 s->thread_args.commits = &s->commits;
2356 s->thread_args.in_repo_path = s->in_repo_path;
2357 s->thread_args.start_id = s->start_id;
2358 s->thread_args.repo = thread_repo;
2359 s->thread_args.log_complete = 0;
2360 s->thread_args.quit = &s->quit;
2361 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2362 s->thread_args.selected_entry = &s->selected_entry;
2363 s->thread_args.searching = &view->searching;
2364 s->thread_args.search_next_done = &view->search_next_done;
2365 s->thread_args.regex = &view->regex;
2366 done:
2367 if (err)
2368 close_log_view(view);
2369 return err;
2372 static const struct got_error *
2373 show_log_view(struct tog_view *view)
2375 const struct got_error *err;
2376 struct tog_log_view_state *s = &view->state.log;
2378 if (s->thread == NULL) {
2379 int errcode = pthread_create(&s->thread, NULL, log_thread,
2380 &s->thread_args);
2381 if (errcode)
2382 return got_error_set_errno(errcode, "pthread_create");
2383 if (s->thread_args.commits_needed > 0) {
2384 err = trigger_log_thread(view, 1);
2385 if (err)
2386 return err;
2390 return draw_commits(view);
2393 static const struct got_error *
2394 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2396 const struct got_error *err = NULL;
2397 struct tog_log_view_state *s = &view->state.log;
2398 struct tog_view *diff_view = NULL, *tree_view = NULL;
2399 struct tog_view *ref_view = NULL;
2400 int begin_x = 0;
2402 switch (ch) {
2403 case 'q':
2404 s->quit = 1;
2405 break;
2406 case 'k':
2407 case KEY_UP:
2408 case '<':
2409 case ',':
2410 if (s->first_displayed_entry == NULL)
2411 break;
2412 if (s->selected > 0)
2413 s->selected--;
2414 else
2415 log_scroll_up(s, 1);
2416 select_commit(s);
2417 break;
2418 case KEY_PPAGE:
2419 case CTRL('b'):
2420 if (s->first_displayed_entry == NULL)
2421 break;
2422 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2423 s->selected = 0;
2424 else
2425 log_scroll_up(s, view->nlines - 1);
2426 select_commit(s);
2427 break;
2428 case 'j':
2429 case KEY_DOWN:
2430 case '>':
2431 case '.':
2432 if (s->first_displayed_entry == NULL)
2433 break;
2434 if (s->selected < MIN(view->nlines - 2,
2435 s->commits.ncommits - 1))
2436 s->selected++;
2437 else {
2438 err = log_scroll_down(view, 1);
2439 if (err)
2440 break;
2442 select_commit(s);
2443 break;
2444 case KEY_NPAGE:
2445 case CTRL('f'): {
2446 struct commit_queue_entry *first;
2447 first = s->first_displayed_entry;
2448 if (first == NULL)
2449 break;
2450 err = log_scroll_down(view, view->nlines - 1);
2451 if (err)
2452 break;
2453 if (first == s->first_displayed_entry &&
2454 s->selected < MIN(view->nlines - 2,
2455 s->commits.ncommits - 1)) {
2456 /* can't scroll further down */
2457 s->selected = MIN(view->nlines - 2,
2458 s->commits.ncommits - 1);
2460 select_commit(s);
2461 break;
2463 case KEY_RESIZE:
2464 if (s->selected > view->nlines - 2)
2465 s->selected = view->nlines - 2;
2466 if (s->selected > s->commits.ncommits - 1)
2467 s->selected = s->commits.ncommits - 1;
2468 select_commit(s);
2469 if (s->commits.ncommits < view->nlines - 1 &&
2470 !s->thread_args.log_complete) {
2471 s->thread_args.commits_needed += (view->nlines - 1) -
2472 s->commits.ncommits;
2473 err = trigger_log_thread(view, 1);
2475 break;
2476 case KEY_ENTER:
2477 case ' ':
2478 case '\r':
2479 if (s->selected_entry == NULL)
2480 break;
2481 if (view_is_parent_view(view))
2482 begin_x = view_split_begin_x(view->begin_x);
2483 err = open_diff_view_for_commit(&diff_view, begin_x,
2484 s->selected_entry->commit, s->selected_entry->id,
2485 view, s->repo);
2486 if (err)
2487 break;
2488 view->focussed = 0;
2489 diff_view->focussed = 1;
2490 if (view_is_parent_view(view)) {
2491 err = view_close_child(view);
2492 if (err)
2493 return err;
2494 view_set_child(view, diff_view);
2495 view->focus_child = 1;
2496 } else
2497 *new_view = diff_view;
2498 break;
2499 case 't':
2500 if (s->selected_entry == NULL)
2501 break;
2502 if (view_is_parent_view(view))
2503 begin_x = view_split_begin_x(view->begin_x);
2504 err = browse_commit_tree(&tree_view, begin_x,
2505 s->selected_entry, s->in_repo_path, s->head_ref_name,
2506 s->repo);
2507 if (err)
2508 break;
2509 view->focussed = 0;
2510 tree_view->focussed = 1;
2511 if (view_is_parent_view(view)) {
2512 err = view_close_child(view);
2513 if (err)
2514 return err;
2515 view_set_child(view, tree_view);
2516 view->focus_child = 1;
2517 } else
2518 *new_view = tree_view;
2519 break;
2520 case KEY_BACKSPACE:
2521 case CTRL('l'):
2522 case 'B':
2523 if (ch == KEY_BACKSPACE &&
2524 got_path_is_root_dir(s->in_repo_path))
2525 break;
2526 err = stop_log_thread(s);
2527 if (err)
2528 return err;
2529 if (ch == KEY_BACKSPACE) {
2530 char *parent_path;
2531 err = got_path_dirname(&parent_path, s->in_repo_path);
2532 if (err)
2533 return err;
2534 free(s->in_repo_path);
2535 s->in_repo_path = parent_path;
2536 s->thread_args.in_repo_path = s->in_repo_path;
2537 } else if (ch == CTRL('l')) {
2538 struct got_object_id *start_id;
2539 err = got_repo_match_object_id(&start_id, NULL,
2540 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2541 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2542 if (err)
2543 return err;
2544 free(s->start_id);
2545 s->start_id = start_id;
2546 s->thread_args.start_id = s->start_id;
2547 } else /* 'B' */
2548 s->log_branches = !s->log_branches;
2550 err = got_repo_open(&s->thread_args.repo,
2551 got_repo_get_path(s->repo), NULL);
2552 if (err)
2553 return err;
2554 tog_free_refs();
2555 err = tog_load_refs(s->repo);
2556 if (err)
2557 return err;
2558 err = got_commit_graph_open(&s->thread_args.graph,
2559 s->in_repo_path, !s->log_branches);
2560 if (err)
2561 return err;
2562 err = got_commit_graph_iter_start(s->thread_args.graph,
2563 s->start_id, s->repo, NULL, NULL);
2564 if (err)
2565 return err;
2566 free_commits(&s->commits);
2567 s->first_displayed_entry = NULL;
2568 s->last_displayed_entry = NULL;
2569 s->selected_entry = NULL;
2570 s->selected = 0;
2571 s->thread_args.log_complete = 0;
2572 s->quit = 0;
2573 s->thread_args.commits_needed = view->nlines;
2574 break;
2575 case 'r':
2576 if (view_is_parent_view(view))
2577 begin_x = view_split_begin_x(view->begin_x);
2578 ref_view = view_open(view->nlines, view->ncols,
2579 view->begin_y, begin_x, TOG_VIEW_REF);
2580 if (ref_view == NULL)
2581 return got_error_from_errno("view_open");
2582 err = open_ref_view(ref_view, s->repo);
2583 if (err) {
2584 view_close(ref_view);
2585 return err;
2587 view->focussed = 0;
2588 ref_view->focussed = 1;
2589 if (view_is_parent_view(view)) {
2590 err = view_close_child(view);
2591 if (err)
2592 return err;
2593 view_set_child(view, ref_view);
2594 view->focus_child = 1;
2595 } else
2596 *new_view = ref_view;
2597 break;
2598 default:
2599 break;
2602 return err;
2605 static const struct got_error *
2606 apply_unveil(const char *repo_path, const char *worktree_path)
2608 const struct got_error *error;
2610 #ifdef PROFILE
2611 if (unveil("gmon.out", "rwc") != 0)
2612 return got_error_from_errno2("unveil", "gmon.out");
2613 #endif
2614 if (repo_path && unveil(repo_path, "r") != 0)
2615 return got_error_from_errno2("unveil", repo_path);
2617 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2618 return got_error_from_errno2("unveil", worktree_path);
2620 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2621 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2623 error = got_privsep_unveil_exec_helpers();
2624 if (error != NULL)
2625 return error;
2627 if (unveil(NULL, NULL) != 0)
2628 return got_error_from_errno("unveil");
2630 return NULL;
2633 static void
2634 init_curses(void)
2636 initscr();
2637 cbreak();
2638 halfdelay(1); /* Do fast refresh while initial view is loading. */
2639 noecho();
2640 nonl();
2641 intrflush(stdscr, FALSE);
2642 keypad(stdscr, TRUE);
2643 curs_set(0);
2644 if (getenv("TOG_COLORS") != NULL) {
2645 start_color();
2646 use_default_colors();
2648 signal(SIGWINCH, tog_sigwinch);
2649 signal(SIGPIPE, tog_sigpipe);
2650 signal(SIGCONT, tog_sigcont);
2653 static const struct got_error *
2654 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2655 struct got_repository *repo, struct got_worktree *worktree)
2657 const struct got_error *err = NULL;
2659 if (argc == 0) {
2660 *in_repo_path = strdup("/");
2661 if (*in_repo_path == NULL)
2662 return got_error_from_errno("strdup");
2663 return NULL;
2666 if (worktree) {
2667 const char *prefix = got_worktree_get_path_prefix(worktree);
2668 char *p;
2670 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2671 if (err)
2672 return err;
2673 if (asprintf(in_repo_path, "%s%s%s", prefix,
2674 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2675 p) == -1) {
2676 err = got_error_from_errno("asprintf");
2677 *in_repo_path = NULL;
2679 free(p);
2680 } else
2681 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2683 return err;
2686 static const struct got_error *
2687 cmd_log(int argc, char *argv[])
2689 const struct got_error *error;
2690 struct got_repository *repo = NULL;
2691 struct got_worktree *worktree = NULL;
2692 struct got_object_id *start_id = NULL;
2693 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2694 char *start_commit = NULL, *label = NULL;
2695 struct got_reference *ref = NULL;
2696 const char *head_ref_name = NULL;
2697 int ch, log_branches = 0;
2698 struct tog_view *view;
2700 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2701 switch (ch) {
2702 case 'b':
2703 log_branches = 1;
2704 break;
2705 case 'c':
2706 start_commit = optarg;
2707 break;
2708 case 'r':
2709 repo_path = realpath(optarg, NULL);
2710 if (repo_path == NULL)
2711 return got_error_from_errno2("realpath",
2712 optarg);
2713 break;
2714 default:
2715 usage_log();
2716 /* NOTREACHED */
2720 argc -= optind;
2721 argv += optind;
2723 if (argc > 1)
2724 usage_log();
2726 if (repo_path == NULL) {
2727 cwd = getcwd(NULL, 0);
2728 if (cwd == NULL)
2729 return got_error_from_errno("getcwd");
2730 error = got_worktree_open(&worktree, cwd);
2731 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2732 goto done;
2733 if (worktree)
2734 repo_path =
2735 strdup(got_worktree_get_repo_path(worktree));
2736 else
2737 repo_path = strdup(cwd);
2738 if (repo_path == NULL) {
2739 error = got_error_from_errno("strdup");
2740 goto done;
2744 error = got_repo_open(&repo, repo_path, NULL);
2745 if (error != NULL)
2746 goto done;
2748 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2749 repo, worktree);
2750 if (error)
2751 goto done;
2753 init_curses();
2755 error = apply_unveil(got_repo_get_path(repo),
2756 worktree ? got_worktree_get_root_path(worktree) : NULL);
2757 if (error)
2758 goto done;
2760 /* already loaded by tog_log_with_path()? */
2761 if (TAILQ_EMPTY(&tog_refs)) {
2762 error = tog_load_refs(repo);
2763 if (error)
2764 goto done;
2767 if (start_commit == NULL) {
2768 error = got_repo_match_object_id(&start_id, &label,
2769 worktree ? got_worktree_get_head_ref_name(worktree) :
2770 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2771 if (error)
2772 goto done;
2773 head_ref_name = label;
2774 } else {
2775 error = got_ref_open(&ref, repo, start_commit, 0);
2776 if (error == NULL)
2777 head_ref_name = got_ref_get_name(ref);
2778 else if (error->code != GOT_ERR_NOT_REF)
2779 goto done;
2780 error = got_repo_match_object_id(&start_id, NULL,
2781 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2782 if (error)
2783 goto done;
2786 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2787 if (view == NULL) {
2788 error = got_error_from_errno("view_open");
2789 goto done;
2791 error = open_log_view(view, start_id, repo, head_ref_name,
2792 in_repo_path, log_branches);
2793 if (error)
2794 goto done;
2795 if (worktree) {
2796 /* Release work tree lock. */
2797 got_worktree_close(worktree);
2798 worktree = NULL;
2800 error = view_loop(view);
2801 done:
2802 free(in_repo_path);
2803 free(repo_path);
2804 free(cwd);
2805 free(start_id);
2806 free(label);
2807 if (ref)
2808 got_ref_close(ref);
2809 if (repo) {
2810 const struct got_error *close_err = got_repo_close(repo);
2811 if (error == NULL)
2812 error = close_err;
2814 if (worktree)
2815 got_worktree_close(worktree);
2816 tog_free_refs();
2817 return error;
2820 __dead static void
2821 usage_diff(void)
2823 endwin();
2824 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2825 "[-w] object1 object2\n", getprogname());
2826 exit(1);
2829 static int
2830 match_line(const char *line, regex_t *regex, size_t nmatch,
2831 regmatch_t *regmatch)
2833 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2836 struct tog_color *
2837 match_color(struct tog_colors *colors, const char *line)
2839 struct tog_color *tc = NULL;
2841 SIMPLEQ_FOREACH(tc, colors, entry) {
2842 if (match_line(line, &tc->regex, 0, NULL))
2843 return tc;
2846 return NULL;
2849 static const struct got_error *
2850 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2851 WINDOW *window, regmatch_t *regmatch)
2853 const struct got_error *err = NULL;
2854 wchar_t *wline;
2855 int width;
2856 char *s;
2858 *wtotal = 0;
2860 s = strndup(line, regmatch->rm_so);
2861 if (s == NULL)
2862 return got_error_from_errno("strndup");
2864 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2865 if (err) {
2866 free(s);
2867 return err;
2869 waddwstr(window, wline);
2870 free(wline);
2871 free(s);
2872 wlimit -= width;
2873 *wtotal += width;
2875 if (wlimit > 0) {
2876 s = strndup(line + regmatch->rm_so,
2877 regmatch->rm_eo - regmatch->rm_so);
2878 if (s == NULL) {
2879 err = got_error_from_errno("strndup");
2880 free(s);
2881 return err;
2883 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2884 if (err) {
2885 free(s);
2886 return err;
2888 wattr_on(window, A_STANDOUT, NULL);
2889 waddwstr(window, wline);
2890 wattr_off(window, A_STANDOUT, NULL);
2891 free(wline);
2892 free(s);
2893 wlimit -= width;
2894 *wtotal += width;
2897 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2898 err = format_line(&wline, &width,
2899 line + regmatch->rm_eo, wlimit, col_tab_align);
2900 if (err)
2901 return err;
2902 waddwstr(window, wline);
2903 free(wline);
2904 *wtotal += width;
2907 return NULL;
2910 static const struct got_error *
2911 draw_file(struct tog_view *view, const char *header)
2913 struct tog_diff_view_state *s = &view->state.diff;
2914 regmatch_t *regmatch = &view->regmatch;
2915 const struct got_error *err;
2916 int nprinted = 0;
2917 char *line;
2918 size_t linesize = 0;
2919 ssize_t linelen;
2920 struct tog_color *tc;
2921 wchar_t *wline;
2922 int width;
2923 int max_lines = view->nlines;
2924 int nlines = s->nlines;
2925 off_t line_offset;
2927 line_offset = s->line_offsets[s->first_displayed_line - 1];
2928 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2929 return got_error_from_errno("fseek");
2931 werase(view->window);
2933 if (header) {
2934 if (asprintf(&line, "[%d/%d] %s",
2935 s->first_displayed_line - 1 + s->selected_line, nlines,
2936 header) == -1)
2937 return got_error_from_errno("asprintf");
2938 err = format_line(&wline, &width, line, view->ncols, 0);
2939 free(line);
2940 if (err)
2941 return err;
2943 if (view_needs_focus_indication(view))
2944 wstandout(view->window);
2945 waddwstr(view->window, wline);
2946 free(wline);
2947 wline = NULL;
2948 if (view_needs_focus_indication(view))
2949 wstandend(view->window);
2950 if (width <= view->ncols - 1)
2951 waddch(view->window, '\n');
2953 if (max_lines <= 1)
2954 return NULL;
2955 max_lines--;
2958 s->eof = 0;
2959 line = NULL;
2960 while (max_lines > 0 && nprinted < max_lines) {
2961 linelen = getline(&line, &linesize, s->f);
2962 if (linelen == -1) {
2963 if (feof(s->f)) {
2964 s->eof = 1;
2965 break;
2967 free(line);
2968 return got_ferror(s->f, GOT_ERR_IO);
2971 tc = match_color(&s->colors, line);
2972 if (tc)
2973 wattr_on(view->window,
2974 COLOR_PAIR(tc->colorpair), NULL);
2975 if (s->first_displayed_line + nprinted == s->matched_line &&
2976 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2977 err = add_matched_line(&width, line, view->ncols, 0,
2978 view->window, regmatch);
2979 if (err) {
2980 free(line);
2981 return err;
2983 } else {
2984 err = format_line(&wline, &width, line, view->ncols, 0);
2985 if (err) {
2986 free(line);
2987 return err;
2989 waddwstr(view->window, wline);
2990 free(wline);
2991 wline = NULL;
2993 if (tc)
2994 wattr_off(view->window,
2995 COLOR_PAIR(tc->colorpair), NULL);
2996 if (width <= view->ncols - 1)
2997 waddch(view->window, '\n');
2998 nprinted++;
3000 free(line);
3001 if (nprinted >= 1)
3002 s->last_displayed_line = s->first_displayed_line +
3003 (nprinted - 1);
3004 else
3005 s->last_displayed_line = s->first_displayed_line;
3007 view_vborder(view);
3009 if (s->eof) {
3010 while (nprinted < view->nlines) {
3011 waddch(view->window, '\n');
3012 nprinted++;
3015 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3016 if (err) {
3017 return err;
3020 wstandout(view->window);
3021 waddwstr(view->window, wline);
3022 free(wline);
3023 wline = NULL;
3024 wstandend(view->window);
3027 return NULL;
3030 static char *
3031 get_datestr(time_t *time, char *datebuf)
3033 struct tm mytm, *tm;
3034 char *p, *s;
3036 tm = gmtime_r(time, &mytm);
3037 if (tm == NULL)
3038 return NULL;
3039 s = asctime_r(tm, datebuf);
3040 if (s == NULL)
3041 return NULL;
3042 p = strchr(s, '\n');
3043 if (p)
3044 *p = '\0';
3045 return s;
3048 static const struct got_error *
3049 get_changed_paths(struct got_pathlist_head *paths,
3050 struct got_commit_object *commit, struct got_repository *repo)
3052 const struct got_error *err = NULL;
3053 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3054 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3055 struct got_object_qid *qid;
3057 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3058 if (qid != NULL) {
3059 struct got_commit_object *pcommit;
3060 err = got_object_open_as_commit(&pcommit, repo,
3061 qid->id);
3062 if (err)
3063 return err;
3065 tree_id1 = got_object_commit_get_tree_id(pcommit);
3066 got_object_commit_close(pcommit);
3070 if (tree_id1) {
3071 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3072 if (err)
3073 goto done;
3076 tree_id2 = got_object_commit_get_tree_id(commit);
3077 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3078 if (err)
3079 goto done;
3081 err = got_diff_tree(tree1, tree2, "", "", repo,
3082 got_diff_tree_collect_changed_paths, paths, 0);
3083 done:
3084 if (tree1)
3085 got_object_tree_close(tree1);
3086 if (tree2)
3087 got_object_tree_close(tree2);
3088 return err;
3091 static const struct got_error *
3092 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3094 off_t *p;
3096 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3097 if (p == NULL)
3098 return got_error_from_errno("reallocarray");
3099 *line_offsets = p;
3100 (*line_offsets)[*nlines] = off;
3101 (*nlines)++;
3102 return NULL;
3105 static const struct got_error *
3106 write_commit_info(off_t **line_offsets, size_t *nlines,
3107 struct got_object_id *commit_id, struct got_reflist_head *refs,
3108 struct got_repository *repo, FILE *outfile)
3110 const struct got_error *err = NULL;
3111 char datebuf[26], *datestr;
3112 struct got_commit_object *commit;
3113 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3114 time_t committer_time;
3115 const char *author, *committer;
3116 char *refs_str = NULL;
3117 struct got_pathlist_head changed_paths;
3118 struct got_pathlist_entry *pe;
3119 off_t outoff = 0;
3120 int n;
3122 TAILQ_INIT(&changed_paths);
3124 if (refs) {
3125 err = build_refs_str(&refs_str, refs, commit_id, repo);
3126 if (err)
3127 return err;
3130 err = got_object_open_as_commit(&commit, repo, commit_id);
3131 if (err)
3132 return err;
3134 err = got_object_id_str(&id_str, commit_id);
3135 if (err) {
3136 err = got_error_from_errno("got_object_id_str");
3137 goto done;
3140 err = add_line_offset(line_offsets, nlines, 0);
3141 if (err)
3142 goto done;
3144 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3145 refs_str ? refs_str : "", refs_str ? ")" : "");
3146 if (n < 0) {
3147 err = got_error_from_errno("fprintf");
3148 goto done;
3150 outoff += n;
3151 err = add_line_offset(line_offsets, nlines, outoff);
3152 if (err)
3153 goto done;
3155 n = fprintf(outfile, "from: %s\n",
3156 got_object_commit_get_author(commit));
3157 if (n < 0) {
3158 err = got_error_from_errno("fprintf");
3159 goto done;
3161 outoff += n;
3162 err = add_line_offset(line_offsets, nlines, outoff);
3163 if (err)
3164 goto done;
3166 committer_time = got_object_commit_get_committer_time(commit);
3167 datestr = get_datestr(&committer_time, datebuf);
3168 if (datestr) {
3169 n = fprintf(outfile, "date: %s UTC\n", datestr);
3170 if (n < 0) {
3171 err = got_error_from_errno("fprintf");
3172 goto done;
3174 outoff += n;
3175 err = add_line_offset(line_offsets, nlines, outoff);
3176 if (err)
3177 goto done;
3179 author = got_object_commit_get_author(commit);
3180 committer = got_object_commit_get_committer(commit);
3181 if (strcmp(author, committer) != 0) {
3182 n = fprintf(outfile, "via: %s\n", committer);
3183 if (n < 0) {
3184 err = got_error_from_errno("fprintf");
3185 goto done;
3187 outoff += n;
3188 err = add_line_offset(line_offsets, nlines, outoff);
3189 if (err)
3190 goto done;
3192 err = got_object_commit_get_logmsg(&logmsg, commit);
3193 if (err)
3194 goto done;
3195 s = logmsg;
3196 while ((line = strsep(&s, "\n")) != NULL) {
3197 n = fprintf(outfile, "%s\n", line);
3198 if (n < 0) {
3199 err = got_error_from_errno("fprintf");
3200 goto done;
3202 outoff += n;
3203 err = add_line_offset(line_offsets, nlines, outoff);
3204 if (err)
3205 goto done;
3208 err = get_changed_paths(&changed_paths, commit, repo);
3209 if (err)
3210 goto done;
3211 TAILQ_FOREACH(pe, &changed_paths, entry) {
3212 struct got_diff_changed_path *cp = pe->data;
3213 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3214 if (n < 0) {
3215 err = got_error_from_errno("fprintf");
3216 goto done;
3218 outoff += n;
3219 err = add_line_offset(line_offsets, nlines, outoff);
3220 if (err)
3221 goto done;
3222 free((char *)pe->path);
3223 free(pe->data);
3226 fputc('\n', outfile);
3227 outoff++;
3228 err = add_line_offset(line_offsets, nlines, outoff);
3229 done:
3230 got_pathlist_free(&changed_paths);
3231 free(id_str);
3232 free(logmsg);
3233 free(refs_str);
3234 got_object_commit_close(commit);
3235 if (err) {
3236 free(*line_offsets);
3237 *line_offsets = NULL;
3238 *nlines = 0;
3240 return err;
3243 static const struct got_error *
3244 create_diff(struct tog_diff_view_state *s)
3246 const struct got_error *err = NULL;
3247 FILE *f = NULL;
3248 int obj_type;
3250 free(s->line_offsets);
3251 s->line_offsets = malloc(sizeof(off_t));
3252 if (s->line_offsets == NULL)
3253 return got_error_from_errno("malloc");
3254 s->nlines = 0;
3256 f = got_opentemp();
3257 if (f == NULL) {
3258 err = got_error_from_errno("got_opentemp");
3259 goto done;
3261 if (s->f && fclose(s->f) == EOF) {
3262 err = got_error_from_errno("fclose");
3263 goto done;
3265 s->f = f;
3267 if (s->id1)
3268 err = got_object_get_type(&obj_type, s->repo, s->id1);
3269 else
3270 err = got_object_get_type(&obj_type, s->repo, s->id2);
3271 if (err)
3272 goto done;
3274 switch (obj_type) {
3275 case GOT_OBJ_TYPE_BLOB:
3276 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3277 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3278 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3279 break;
3280 case GOT_OBJ_TYPE_TREE:
3281 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, "", "", s->diff_context,
3283 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3284 break;
3285 case GOT_OBJ_TYPE_COMMIT: {
3286 const struct got_object_id_queue *parent_ids;
3287 struct got_object_qid *pid;
3288 struct got_commit_object *commit2;
3289 struct got_reflist_head *refs;
3291 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3292 if (err)
3293 goto done;
3294 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3295 /* Show commit info if we're diffing to a parent/root commit. */
3296 if (s->id1 == NULL) {
3297 err = write_commit_info(&s->line_offsets, &s->nlines,
3298 s->id2, refs, s->repo, s->f);
3299 if (err)
3300 goto done;
3301 } else {
3302 parent_ids = got_object_commit_get_parent_ids(commit2);
3303 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3304 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3305 err = write_commit_info(
3306 &s->line_offsets, &s->nlines,
3307 s->id2, refs, s->repo, s->f);
3308 if (err)
3309 goto done;
3310 break;
3314 got_object_commit_close(commit2);
3316 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3317 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3318 s->force_text_diff, s->repo, s->f);
3319 break;
3321 default:
3322 err = got_error(GOT_ERR_OBJ_TYPE);
3323 break;
3325 if (err)
3326 goto done;
3327 done:
3328 if (s->f && fflush(s->f) != 0 && err == NULL)
3329 err = got_error_from_errno("fflush");
3330 return err;
3333 static void
3334 diff_view_indicate_progress(struct tog_view *view)
3336 mvwaddstr(view->window, 0, 0, "diffing...");
3337 update_panels();
3338 doupdate();
3341 static const struct got_error *
3342 search_start_diff_view(struct tog_view *view)
3344 struct tog_diff_view_state *s = &view->state.diff;
3346 s->matched_line = 0;
3347 return NULL;
3350 static const struct got_error *
3351 search_next_diff_view(struct tog_view *view)
3353 struct tog_diff_view_state *s = &view->state.diff;
3354 int lineno;
3355 char *line = NULL;
3356 size_t linesize = 0;
3357 ssize_t linelen;
3359 if (!view->searching) {
3360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3361 return NULL;
3364 if (s->matched_line) {
3365 if (view->searching == TOG_SEARCH_FORWARD)
3366 lineno = s->matched_line + 1;
3367 else
3368 lineno = s->matched_line - 1;
3369 } else {
3370 if (view->searching == TOG_SEARCH_FORWARD)
3371 lineno = 1;
3372 else
3373 lineno = s->nlines;
3376 while (1) {
3377 off_t offset;
3379 if (lineno <= 0 || lineno > s->nlines) {
3380 if (s->matched_line == 0) {
3381 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3382 break;
3385 if (view->searching == TOG_SEARCH_FORWARD)
3386 lineno = 1;
3387 else
3388 lineno = s->nlines;
3391 offset = s->line_offsets[lineno - 1];
3392 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3393 free(line);
3394 return got_error_from_errno("fseeko");
3396 linelen = getline(&line, &linesize, s->f);
3397 if (linelen != -1 &&
3398 match_line(line, &view->regex, 1, &view->regmatch)) {
3399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3400 s->matched_line = lineno;
3401 break;
3403 if (view->searching == TOG_SEARCH_FORWARD)
3404 lineno++;
3405 else
3406 lineno--;
3408 free(line);
3410 if (s->matched_line) {
3411 s->first_displayed_line = s->matched_line;
3412 s->selected_line = 1;
3415 return NULL;
3418 static const struct got_error *
3419 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3420 struct got_object_id *id2, const char *label1, const char *label2,
3421 int diff_context, int ignore_whitespace, int force_text_diff,
3422 struct tog_view *log_view, struct got_repository *repo)
3424 const struct got_error *err;
3425 struct tog_diff_view_state *s = &view->state.diff;
3427 if (id1 != NULL && id2 != NULL) {
3428 int type1, type2;
3429 err = got_object_get_type(&type1, repo, id1);
3430 if (err)
3431 return err;
3432 err = got_object_get_type(&type2, repo, id2);
3433 if (err)
3434 return err;
3436 if (type1 != type2)
3437 return got_error(GOT_ERR_OBJ_TYPE);
3439 s->first_displayed_line = 1;
3440 s->last_displayed_line = view->nlines;
3441 s->selected_line = 1;
3442 s->repo = repo;
3443 s->id1 = id1;
3444 s->id2 = id2;
3445 s->label1 = label1;
3446 s->label2 = label2;
3448 if (id1) {
3449 s->id1 = got_object_id_dup(id1);
3450 if (s->id1 == NULL)
3451 return got_error_from_errno("got_object_id_dup");
3452 } else
3453 s->id1 = NULL;
3455 s->id2 = got_object_id_dup(id2);
3456 if (s->id2 == NULL) {
3457 free(s->id1);
3458 s->id1 = NULL;
3459 return got_error_from_errno("got_object_id_dup");
3461 s->f = NULL;
3462 s->first_displayed_line = 1;
3463 s->last_displayed_line = view->nlines;
3464 s->diff_context = diff_context;
3465 s->ignore_whitespace = ignore_whitespace;
3466 s->force_text_diff = force_text_diff;
3467 s->log_view = log_view;
3468 s->repo = repo;
3470 SIMPLEQ_INIT(&s->colors);
3471 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3472 err = add_color(&s->colors,
3473 "^-", TOG_COLOR_DIFF_MINUS,
3474 get_color_value("TOG_COLOR_DIFF_MINUS"));
3475 if (err)
3476 return err;
3477 err = add_color(&s->colors, "^\\+",
3478 TOG_COLOR_DIFF_PLUS,
3479 get_color_value("TOG_COLOR_DIFF_PLUS"));
3480 if (err) {
3481 free_colors(&s->colors);
3482 return err;
3484 err = add_color(&s->colors,
3485 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3486 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3487 if (err) {
3488 free_colors(&s->colors);
3489 return err;
3492 err = add_color(&s->colors,
3493 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3494 TOG_COLOR_DIFF_META,
3495 get_color_value("TOG_COLOR_DIFF_META"));
3496 if (err) {
3497 free_colors(&s->colors);
3498 return err;
3501 err = add_color(&s->colors,
3502 "^(from|via): ", TOG_COLOR_AUTHOR,
3503 get_color_value("TOG_COLOR_AUTHOR"));
3504 if (err) {
3505 free_colors(&s->colors);
3506 return err;
3509 err = add_color(&s->colors,
3510 "^date: ", TOG_COLOR_DATE,
3511 get_color_value("TOG_COLOR_DATE"));
3512 if (err) {
3513 free_colors(&s->colors);
3514 return err;
3518 if (log_view && view_is_splitscreen(view))
3519 show_log_view(log_view); /* draw vborder */
3520 diff_view_indicate_progress(view);
3522 s->line_offsets = NULL;
3523 s->nlines = 0;
3524 err = create_diff(s);
3525 if (err) {
3526 free(s->id1);
3527 s->id1 = NULL;
3528 free(s->id2);
3529 s->id2 = NULL;
3530 free_colors(&s->colors);
3531 return err;
3534 view->show = show_diff_view;
3535 view->input = input_diff_view;
3536 view->close = close_diff_view;
3537 view->search_start = search_start_diff_view;
3538 view->search_next = search_next_diff_view;
3540 return NULL;
3543 static const struct got_error *
3544 close_diff_view(struct tog_view *view)
3546 const struct got_error *err = NULL;
3547 struct tog_diff_view_state *s = &view->state.diff;
3549 free(s->id1);
3550 s->id1 = NULL;
3551 free(s->id2);
3552 s->id2 = NULL;
3553 if (s->f && fclose(s->f) == EOF)
3554 err = got_error_from_errno("fclose");
3555 free_colors(&s->colors);
3556 free(s->line_offsets);
3557 s->line_offsets = NULL;
3558 s->nlines = 0;
3559 return err;
3562 static const struct got_error *
3563 show_diff_view(struct tog_view *view)
3565 const struct got_error *err;
3566 struct tog_diff_view_state *s = &view->state.diff;
3567 char *id_str1 = NULL, *id_str2, *header;
3568 const char *label1, *label2;
3570 if (s->id1) {
3571 err = got_object_id_str(&id_str1, s->id1);
3572 if (err)
3573 return err;
3574 label1 = s->label1 ? : id_str1;
3575 } else
3576 label1 = "/dev/null";
3578 err = got_object_id_str(&id_str2, s->id2);
3579 if (err)
3580 return err;
3581 label2 = s->label2 ? : id_str2;
3583 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3584 err = got_error_from_errno("asprintf");
3585 free(id_str1);
3586 free(id_str2);
3587 return err;
3589 free(id_str1);
3590 free(id_str2);
3592 return draw_file(view, header);
3595 static const struct got_error *
3596 set_selected_commit(struct tog_diff_view_state *s,
3597 struct commit_queue_entry *entry)
3599 const struct got_error *err;
3600 const struct got_object_id_queue *parent_ids;
3601 struct got_commit_object *selected_commit;
3602 struct got_object_qid *pid;
3604 free(s->id2);
3605 s->id2 = got_object_id_dup(entry->id);
3606 if (s->id2 == NULL)
3607 return got_error_from_errno("got_object_id_dup");
3609 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3610 if (err)
3611 return err;
3612 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3613 free(s->id1);
3614 pid = SIMPLEQ_FIRST(parent_ids);
3615 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3616 got_object_commit_close(selected_commit);
3617 return NULL;
3620 static const struct got_error *
3621 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3623 const struct got_error *err = NULL;
3624 struct tog_diff_view_state *s = &view->state.diff;
3625 struct tog_log_view_state *ls;
3626 struct commit_queue_entry *old_selected_entry;
3627 char *line = NULL;
3628 size_t linesize = 0;
3629 ssize_t linelen;
3630 int i;
3632 switch (ch) {
3633 case 'a':
3634 case 'w':
3635 if (ch == 'a')
3636 s->force_text_diff = !s->force_text_diff;
3637 if (ch == 'w')
3638 s->ignore_whitespace = !s->ignore_whitespace;
3639 wclear(view->window);
3640 s->first_displayed_line = 1;
3641 s->last_displayed_line = view->nlines;
3642 diff_view_indicate_progress(view);
3643 err = create_diff(s);
3644 break;
3645 case 'k':
3646 case KEY_UP:
3647 if (s->first_displayed_line > 1)
3648 s->first_displayed_line--;
3649 break;
3650 case KEY_PPAGE:
3651 case CTRL('b'):
3652 if (s->first_displayed_line == 1)
3653 break;
3654 i = 0;
3655 while (i++ < view->nlines - 1 &&
3656 s->first_displayed_line > 1)
3657 s->first_displayed_line--;
3658 break;
3659 case 'j':
3660 case KEY_DOWN:
3661 if (!s->eof)
3662 s->first_displayed_line++;
3663 break;
3664 case KEY_NPAGE:
3665 case CTRL('f'):
3666 case ' ':
3667 if (s->eof)
3668 break;
3669 i = 0;
3670 while (!s->eof && i++ < view->nlines - 1) {
3671 linelen = getline(&line, &linesize, s->f);
3672 s->first_displayed_line++;
3673 if (linelen == -1) {
3674 if (feof(s->f)) {
3675 s->eof = 1;
3676 } else
3677 err = got_ferror(s->f, GOT_ERR_IO);
3678 break;
3681 free(line);
3682 break;
3683 case '[':
3684 if (s->diff_context > 0) {
3685 s->diff_context--;
3686 diff_view_indicate_progress(view);
3687 err = create_diff(s);
3688 if (s->first_displayed_line + view->nlines - 1 >
3689 s->nlines) {
3690 s->first_displayed_line = 1;
3691 s->last_displayed_line = view->nlines;
3694 break;
3695 case ']':
3696 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3697 s->diff_context++;
3698 diff_view_indicate_progress(view);
3699 err = create_diff(s);
3701 break;
3702 case '<':
3703 case ',':
3704 if (s->log_view == NULL)
3705 break;
3706 ls = &s->log_view->state.log;
3707 old_selected_entry = ls->selected_entry;
3709 err = input_log_view(NULL, s->log_view, KEY_UP);
3710 if (err)
3711 break;
3713 if (old_selected_entry == ls->selected_entry)
3714 break;
3716 err = set_selected_commit(s, ls->selected_entry);
3717 if (err)
3718 break;
3720 s->first_displayed_line = 1;
3721 s->last_displayed_line = view->nlines;
3723 diff_view_indicate_progress(view);
3724 err = create_diff(s);
3725 break;
3726 case '>':
3727 case '.':
3728 if (s->log_view == NULL)
3729 break;
3730 ls = &s->log_view->state.log;
3731 old_selected_entry = ls->selected_entry;
3733 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3734 if (err)
3735 break;
3737 if (old_selected_entry == ls->selected_entry)
3738 break;
3740 err = set_selected_commit(s, ls->selected_entry);
3741 if (err)
3742 break;
3744 s->first_displayed_line = 1;
3745 s->last_displayed_line = view->nlines;
3747 diff_view_indicate_progress(view);
3748 err = create_diff(s);
3749 break;
3750 default:
3751 break;
3754 return err;
3757 static const struct got_error *
3758 cmd_diff(int argc, char *argv[])
3760 const struct got_error *error = NULL;
3761 struct got_repository *repo = NULL;
3762 struct got_worktree *worktree = NULL;
3763 struct got_object_id *id1 = NULL, *id2 = NULL;
3764 char *repo_path = NULL, *cwd = NULL;
3765 char *id_str1 = NULL, *id_str2 = NULL;
3766 char *label1 = NULL, *label2 = NULL;
3767 int diff_context = 3, ignore_whitespace = 0;
3768 int ch, force_text_diff = 0;
3769 const char *errstr;
3770 struct tog_view *view;
3772 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3773 switch (ch) {
3774 case 'a':
3775 force_text_diff = 1;
3776 break;
3777 case 'C':
3778 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3779 &errstr);
3780 if (errstr != NULL)
3781 err(1, "-C option %s", errstr);
3782 break;
3783 case 'r':
3784 repo_path = realpath(optarg, NULL);
3785 if (repo_path == NULL)
3786 return got_error_from_errno2("realpath",
3787 optarg);
3788 got_path_strip_trailing_slashes(repo_path);
3789 break;
3790 case 'w':
3791 ignore_whitespace = 1;
3792 break;
3793 default:
3794 usage_diff();
3795 /* NOTREACHED */
3799 argc -= optind;
3800 argv += optind;
3802 if (argc == 0) {
3803 usage_diff(); /* TODO show local worktree changes */
3804 } else if (argc == 2) {
3805 id_str1 = argv[0];
3806 id_str2 = argv[1];
3807 } else
3808 usage_diff();
3810 if (repo_path == NULL) {
3811 cwd = getcwd(NULL, 0);
3812 if (cwd == NULL)
3813 return got_error_from_errno("getcwd");
3814 error = got_worktree_open(&worktree, cwd);
3815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3816 goto done;
3817 if (worktree)
3818 repo_path =
3819 strdup(got_worktree_get_repo_path(worktree));
3820 else
3821 repo_path = strdup(cwd);
3822 if (repo_path == NULL) {
3823 error = got_error_from_errno("strdup");
3824 goto done;
3828 error = got_repo_open(&repo, repo_path, NULL);
3829 if (error)
3830 goto done;
3832 init_curses();
3834 error = apply_unveil(got_repo_get_path(repo), NULL);
3835 if (error)
3836 goto done;
3838 error = tog_load_refs(repo);
3839 if (error)
3840 goto done;
3842 error = got_repo_match_object_id(&id1, &label1, id_str1,
3843 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3844 if (error)
3845 goto done;
3847 error = got_repo_match_object_id(&id2, &label2, id_str2,
3848 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3849 if (error)
3850 goto done;
3852 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3853 if (view == NULL) {
3854 error = got_error_from_errno("view_open");
3855 goto done;
3857 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3858 ignore_whitespace, force_text_diff, NULL, repo);
3859 if (error)
3860 goto done;
3861 error = view_loop(view);
3862 done:
3863 free(label1);
3864 free(label2);
3865 free(repo_path);
3866 free(cwd);
3867 if (repo) {
3868 const struct got_error *close_err = got_repo_close(repo);
3869 if (error == NULL)
3870 error = close_err;
3872 if (worktree)
3873 got_worktree_close(worktree);
3874 tog_free_refs();
3875 return error;
3878 __dead static void
3879 usage_blame(void)
3881 endwin();
3882 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3883 getprogname());
3884 exit(1);
3887 struct tog_blame_line {
3888 int annotated;
3889 struct got_object_id *id;
3892 static const struct got_error *
3893 draw_blame(struct tog_view *view)
3895 struct tog_blame_view_state *s = &view->state.blame;
3896 struct tog_blame *blame = &s->blame;
3897 regmatch_t *regmatch = &view->regmatch;
3898 const struct got_error *err;
3899 int lineno = 0, nprinted = 0;
3900 char *line = NULL;
3901 size_t linesize = 0;
3902 ssize_t linelen;
3903 wchar_t *wline;
3904 int width;
3905 struct tog_blame_line *blame_line;
3906 struct got_object_id *prev_id = NULL;
3907 char *id_str;
3908 struct tog_color *tc;
3910 err = got_object_id_str(&id_str, s->blamed_commit->id);
3911 if (err)
3912 return err;
3914 rewind(blame->f);
3915 werase(view->window);
3917 if (asprintf(&line, "commit %s", id_str) == -1) {
3918 err = got_error_from_errno("asprintf");
3919 free(id_str);
3920 return err;
3923 err = format_line(&wline, &width, line, view->ncols, 0);
3924 free(line);
3925 line = NULL;
3926 if (err)
3927 return err;
3928 if (view_needs_focus_indication(view))
3929 wstandout(view->window);
3930 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3931 if (tc)
3932 wattr_on(view->window,
3933 COLOR_PAIR(tc->colorpair), NULL);
3934 waddwstr(view->window, wline);
3935 if (tc)
3936 wattr_off(view->window,
3937 COLOR_PAIR(tc->colorpair), NULL);
3938 if (view_needs_focus_indication(view))
3939 wstandend(view->window);
3940 free(wline);
3941 wline = NULL;
3942 if (width < view->ncols - 1)
3943 waddch(view->window, '\n');
3945 if (asprintf(&line, "[%d/%d] %s%s",
3946 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3947 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3948 free(id_str);
3949 return got_error_from_errno("asprintf");
3951 free(id_str);
3952 err = format_line(&wline, &width, line, view->ncols, 0);
3953 free(line);
3954 line = NULL;
3955 if (err)
3956 return err;
3957 waddwstr(view->window, wline);
3958 free(wline);
3959 wline = NULL;
3960 if (width < view->ncols - 1)
3961 waddch(view->window, '\n');
3963 s->eof = 0;
3964 while (nprinted < view->nlines - 2) {
3965 linelen = getline(&line, &linesize, blame->f);
3966 if (linelen == -1) {
3967 if (feof(blame->f)) {
3968 s->eof = 1;
3969 break;
3971 free(line);
3972 return got_ferror(blame->f, GOT_ERR_IO);
3974 if (++lineno < s->first_displayed_line)
3975 continue;
3977 if (view->focussed && nprinted == s->selected_line - 1)
3978 wstandout(view->window);
3980 if (blame->nlines > 0) {
3981 blame_line = &blame->lines[lineno - 1];
3982 if (blame_line->annotated && prev_id &&
3983 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3984 !(view->focussed &&
3985 nprinted == s->selected_line - 1)) {
3986 waddstr(view->window, " ");
3987 } else if (blame_line->annotated) {
3988 char *id_str;
3989 err = got_object_id_str(&id_str, blame_line->id);
3990 if (err) {
3991 free(line);
3992 return err;
3994 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3995 if (tc)
3996 wattr_on(view->window,
3997 COLOR_PAIR(tc->colorpair), NULL);
3998 wprintw(view->window, "%.8s", id_str);
3999 if (tc)
4000 wattr_off(view->window,
4001 COLOR_PAIR(tc->colorpair), NULL);
4002 free(id_str);
4003 prev_id = blame_line->id;
4004 } else {
4005 waddstr(view->window, "........");
4006 prev_id = NULL;
4008 } else {
4009 waddstr(view->window, "........");
4010 prev_id = NULL;
4013 if (view->focussed && nprinted == s->selected_line - 1)
4014 wstandend(view->window);
4015 waddstr(view->window, " ");
4017 if (view->ncols <= 9) {
4018 width = 9;
4019 wline = wcsdup(L"");
4020 if (wline == NULL) {
4021 err = got_error_from_errno("wcsdup");
4022 free(line);
4023 return err;
4025 } else if (s->first_displayed_line + nprinted ==
4026 s->matched_line &&
4027 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4028 err = add_matched_line(&width, line, view->ncols - 9, 9,
4029 view->window, regmatch);
4030 if (err) {
4031 free(line);
4032 return err;
4034 width += 9;
4035 } else {
4036 err = format_line(&wline, &width, line,
4037 view->ncols - 9, 9);
4038 waddwstr(view->window, wline);
4039 free(wline);
4040 wline = NULL;
4041 width += 9;
4044 if (width <= view->ncols - 1)
4045 waddch(view->window, '\n');
4046 if (++nprinted == 1)
4047 s->first_displayed_line = lineno;
4049 free(line);
4050 s->last_displayed_line = lineno;
4052 view_vborder(view);
4054 return NULL;
4057 static const struct got_error *
4058 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4060 const struct got_error *err = NULL;
4061 struct tog_blame_cb_args *a = arg;
4062 struct tog_blame_line *line;
4063 int errcode;
4065 if (nlines != a->nlines ||
4066 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4067 return got_error(GOT_ERR_RANGE);
4069 errcode = pthread_mutex_lock(&tog_mutex);
4070 if (errcode)
4071 return got_error_set_errno(errcode, "pthread_mutex_lock");
4073 if (*a->quit) { /* user has quit the blame view */
4074 err = got_error(GOT_ERR_ITER_COMPLETED);
4075 goto done;
4078 if (lineno == -1)
4079 goto done; /* no change in this commit */
4081 line = &a->lines[lineno - 1];
4082 if (line->annotated)
4083 goto done;
4085 line->id = got_object_id_dup(id);
4086 if (line->id == NULL) {
4087 err = got_error_from_errno("got_object_id_dup");
4088 goto done;
4090 line->annotated = 1;
4091 done:
4092 errcode = pthread_mutex_unlock(&tog_mutex);
4093 if (errcode)
4094 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4095 return err;
4098 static void *
4099 blame_thread(void *arg)
4101 const struct got_error *err, *close_err;
4102 struct tog_blame_thread_args *ta = arg;
4103 struct tog_blame_cb_args *a = ta->cb_args;
4104 int errcode;
4106 err = block_signals_used_by_main_thread();
4107 if (err)
4108 return (void *)err;
4110 err = got_blame(ta->path, a->commit_id, ta->repo,
4111 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4112 if (err && err->code == GOT_ERR_CANCELLED)
4113 err = NULL;
4115 errcode = pthread_mutex_lock(&tog_mutex);
4116 if (errcode)
4117 return (void *)got_error_set_errno(errcode,
4118 "pthread_mutex_lock");
4120 close_err = got_repo_close(ta->repo);
4121 if (err == NULL)
4122 err = close_err;
4123 ta->repo = NULL;
4124 *ta->complete = 1;
4126 errcode = pthread_mutex_unlock(&tog_mutex);
4127 if (errcode && err == NULL)
4128 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4130 return (void *)err;
4133 static struct got_object_id *
4134 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4135 int first_displayed_line, int selected_line)
4137 struct tog_blame_line *line;
4139 if (nlines <= 0)
4140 return NULL;
4142 line = &lines[first_displayed_line - 1 + selected_line - 1];
4143 if (!line->annotated)
4144 return NULL;
4146 return line->id;
4149 static const struct got_error *
4150 stop_blame(struct tog_blame *blame)
4152 const struct got_error *err = NULL;
4153 int i;
4155 if (blame->thread) {
4156 int errcode;
4157 errcode = pthread_mutex_unlock(&tog_mutex);
4158 if (errcode)
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_unlock");
4161 errcode = pthread_join(blame->thread, (void **)&err);
4162 if (errcode)
4163 return got_error_set_errno(errcode, "pthread_join");
4164 errcode = pthread_mutex_lock(&tog_mutex);
4165 if (errcode)
4166 return got_error_set_errno(errcode,
4167 "pthread_mutex_lock");
4168 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4169 err = NULL;
4170 blame->thread = NULL;
4172 if (blame->thread_args.repo) {
4173 const struct got_error *close_err;
4174 close_err = got_repo_close(blame->thread_args.repo);
4175 if (err == NULL)
4176 err = close_err;
4177 blame->thread_args.repo = NULL;
4179 if (blame->f) {
4180 if (fclose(blame->f) == EOF && err == NULL)
4181 err = got_error_from_errno("fclose");
4182 blame->f = NULL;
4184 if (blame->lines) {
4185 for (i = 0; i < blame->nlines; i++)
4186 free(blame->lines[i].id);
4187 free(blame->lines);
4188 blame->lines = NULL;
4190 free(blame->cb_args.commit_id);
4191 blame->cb_args.commit_id = NULL;
4193 return err;
4196 static const struct got_error *
4197 cancel_blame_view(void *arg)
4199 const struct got_error *err = NULL;
4200 int *done = arg;
4201 int errcode;
4203 errcode = pthread_mutex_lock(&tog_mutex);
4204 if (errcode)
4205 return got_error_set_errno(errcode,
4206 "pthread_mutex_unlock");
4208 if (*done)
4209 err = got_error(GOT_ERR_CANCELLED);
4211 errcode = pthread_mutex_unlock(&tog_mutex);
4212 if (errcode)
4213 return got_error_set_errno(errcode,
4214 "pthread_mutex_lock");
4216 return err;
4219 static const struct got_error *
4220 run_blame(struct tog_view *view)
4222 struct tog_blame_view_state *s = &view->state.blame;
4223 struct tog_blame *blame = &s->blame;
4224 const struct got_error *err = NULL;
4225 struct got_blob_object *blob = NULL;
4226 struct got_repository *thread_repo = NULL;
4227 struct got_object_id *obj_id = NULL;
4228 int obj_type;
4230 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4231 s->path);
4232 if (err)
4233 return err;
4235 err = got_object_get_type(&obj_type, s->repo, obj_id);
4236 if (err)
4237 goto done;
4239 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4240 err = got_error(GOT_ERR_OBJ_TYPE);
4241 goto done;
4244 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4245 if (err)
4246 goto done;
4247 blame->f = got_opentemp();
4248 if (blame->f == NULL) {
4249 err = got_error_from_errno("got_opentemp");
4250 goto done;
4252 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4253 &blame->line_offsets, blame->f, blob);
4254 if (err)
4255 goto done;
4256 if (blame->nlines == 0) {
4257 s->blame_complete = 1;
4258 goto done;
4261 /* Don't include \n at EOF in the blame line count. */
4262 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4263 blame->nlines--;
4265 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4266 if (blame->lines == NULL) {
4267 err = got_error_from_errno("calloc");
4268 goto done;
4271 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4272 if (err)
4273 goto done;
4275 blame->cb_args.view = view;
4276 blame->cb_args.lines = blame->lines;
4277 blame->cb_args.nlines = blame->nlines;
4278 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4279 if (blame->cb_args.commit_id == NULL) {
4280 err = got_error_from_errno("got_object_id_dup");
4281 goto done;
4283 blame->cb_args.quit = &s->done;
4285 blame->thread_args.path = s->path;
4286 blame->thread_args.repo = thread_repo;
4287 blame->thread_args.cb_args = &blame->cb_args;
4288 blame->thread_args.complete = &s->blame_complete;
4289 blame->thread_args.cancel_cb = cancel_blame_view;
4290 blame->thread_args.cancel_arg = &s->done;
4291 s->blame_complete = 0;
4293 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4294 s->first_displayed_line = 1;
4295 s->last_displayed_line = view->nlines;
4296 s->selected_line = 1;
4299 done:
4300 if (blob)
4301 got_object_blob_close(blob);
4302 free(obj_id);
4303 if (err)
4304 stop_blame(blame);
4305 return err;
4308 static const struct got_error *
4309 open_blame_view(struct tog_view *view, char *path,
4310 struct got_object_id *commit_id, struct got_repository *repo)
4312 const struct got_error *err = NULL;
4313 struct tog_blame_view_state *s = &view->state.blame;
4315 SIMPLEQ_INIT(&s->blamed_commits);
4317 s->path = strdup(path);
4318 if (s->path == NULL)
4319 return got_error_from_errno("strdup");
4321 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4322 if (err) {
4323 free(s->path);
4324 return err;
4327 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4328 s->first_displayed_line = 1;
4329 s->last_displayed_line = view->nlines;
4330 s->selected_line = 1;
4331 s->blame_complete = 0;
4332 s->repo = repo;
4333 s->commit_id = commit_id;
4334 memset(&s->blame, 0, sizeof(s->blame));
4336 SIMPLEQ_INIT(&s->colors);
4337 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4338 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4339 get_color_value("TOG_COLOR_COMMIT"));
4340 if (err)
4341 return err;
4344 view->show = show_blame_view;
4345 view->input = input_blame_view;
4346 view->close = close_blame_view;
4347 view->search_start = search_start_blame_view;
4348 view->search_next = search_next_blame_view;
4350 return run_blame(view);
4353 static const struct got_error *
4354 close_blame_view(struct tog_view *view)
4356 const struct got_error *err = NULL;
4357 struct tog_blame_view_state *s = &view->state.blame;
4359 if (s->blame.thread)
4360 err = stop_blame(&s->blame);
4362 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4363 struct got_object_qid *blamed_commit;
4364 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4365 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4366 got_object_qid_free(blamed_commit);
4369 free(s->path);
4370 free_colors(&s->colors);
4372 return err;
4375 static const struct got_error *
4376 search_start_blame_view(struct tog_view *view)
4378 struct tog_blame_view_state *s = &view->state.blame;
4380 s->matched_line = 0;
4381 return NULL;
4384 static const struct got_error *
4385 search_next_blame_view(struct tog_view *view)
4387 struct tog_blame_view_state *s = &view->state.blame;
4388 int lineno;
4389 char *line = NULL;
4390 size_t linesize = 0;
4391 ssize_t linelen;
4393 if (!view->searching) {
4394 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4395 return NULL;
4398 if (s->matched_line) {
4399 if (view->searching == TOG_SEARCH_FORWARD)
4400 lineno = s->matched_line + 1;
4401 else
4402 lineno = s->matched_line - 1;
4403 } else {
4404 if (view->searching == TOG_SEARCH_FORWARD)
4405 lineno = 1;
4406 else
4407 lineno = s->blame.nlines;
4410 while (1) {
4411 off_t offset;
4413 if (lineno <= 0 || lineno > s->blame.nlines) {
4414 if (s->matched_line == 0) {
4415 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4416 break;
4419 if (view->searching == TOG_SEARCH_FORWARD)
4420 lineno = 1;
4421 else
4422 lineno = s->blame.nlines;
4425 offset = s->blame.line_offsets[lineno - 1];
4426 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4427 free(line);
4428 return got_error_from_errno("fseeko");
4430 linelen = getline(&line, &linesize, s->blame.f);
4431 if (linelen != -1 &&
4432 match_line(line, &view->regex, 1, &view->regmatch)) {
4433 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4434 s->matched_line = lineno;
4435 break;
4437 if (view->searching == TOG_SEARCH_FORWARD)
4438 lineno++;
4439 else
4440 lineno--;
4442 free(line);
4444 if (s->matched_line) {
4445 s->first_displayed_line = s->matched_line;
4446 s->selected_line = 1;
4449 return NULL;
4452 static const struct got_error *
4453 show_blame_view(struct tog_view *view)
4455 const struct got_error *err = NULL;
4456 struct tog_blame_view_state *s = &view->state.blame;
4457 int errcode;
4459 if (s->blame.thread == NULL && !s->blame_complete) {
4460 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4461 &s->blame.thread_args);
4462 if (errcode)
4463 return got_error_set_errno(errcode, "pthread_create");
4465 halfdelay(1); /* fast refresh while annotating */
4468 if (s->blame_complete)
4469 halfdelay(10); /* disable fast refresh */
4471 err = draw_blame(view);
4473 view_vborder(view);
4474 return err;
4477 static const struct got_error *
4478 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4480 const struct got_error *err = NULL, *thread_err = NULL;
4481 struct tog_view *diff_view;
4482 struct tog_blame_view_state *s = &view->state.blame;
4483 int begin_x = 0;
4485 switch (ch) {
4486 case 'q':
4487 s->done = 1;
4488 break;
4489 case 'k':
4490 case KEY_UP:
4491 if (s->selected_line > 1)
4492 s->selected_line--;
4493 else if (s->selected_line == 1 &&
4494 s->first_displayed_line > 1)
4495 s->first_displayed_line--;
4496 break;
4497 case KEY_PPAGE:
4498 case CTRL('b'):
4499 if (s->first_displayed_line == 1) {
4500 s->selected_line = 1;
4501 break;
4503 if (s->first_displayed_line > view->nlines - 2)
4504 s->first_displayed_line -=
4505 (view->nlines - 2);
4506 else
4507 s->first_displayed_line = 1;
4508 break;
4509 case 'j':
4510 case KEY_DOWN:
4511 if (s->selected_line < view->nlines - 2 &&
4512 s->first_displayed_line +
4513 s->selected_line <= s->blame.nlines)
4514 s->selected_line++;
4515 else if (s->last_displayed_line <
4516 s->blame.nlines)
4517 s->first_displayed_line++;
4518 break;
4519 case 'b':
4520 case 'p': {
4521 struct got_object_id *id = NULL;
4522 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4523 s->first_displayed_line, s->selected_line);
4524 if (id == NULL)
4525 break;
4526 if (ch == 'p') {
4527 struct got_commit_object *commit;
4528 struct got_object_qid *pid;
4529 struct got_object_id *blob_id = NULL;
4530 int obj_type;
4531 err = got_object_open_as_commit(&commit,
4532 s->repo, id);
4533 if (err)
4534 break;
4535 pid = SIMPLEQ_FIRST(
4536 got_object_commit_get_parent_ids(commit));
4537 if (pid == NULL) {
4538 got_object_commit_close(commit);
4539 break;
4541 /* Check if path history ends here. */
4542 err = got_object_id_by_path(&blob_id, s->repo,
4543 pid->id, s->path);
4544 if (err) {
4545 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4546 err = NULL;
4547 got_object_commit_close(commit);
4548 break;
4550 err = got_object_get_type(&obj_type, s->repo,
4551 blob_id);
4552 free(blob_id);
4553 /* Can't blame non-blob type objects. */
4554 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4555 got_object_commit_close(commit);
4556 break;
4558 err = got_object_qid_alloc(&s->blamed_commit,
4559 pid->id);
4560 got_object_commit_close(commit);
4561 } else {
4562 if (got_object_id_cmp(id,
4563 s->blamed_commit->id) == 0)
4564 break;
4565 err = got_object_qid_alloc(&s->blamed_commit,
4566 id);
4568 if (err)
4569 break;
4570 s->done = 1;
4571 thread_err = stop_blame(&s->blame);
4572 s->done = 0;
4573 if (thread_err)
4574 break;
4575 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4576 s->blamed_commit, entry);
4577 err = run_blame(view);
4578 if (err)
4579 break;
4580 break;
4582 case 'B': {
4583 struct got_object_qid *first;
4584 first = SIMPLEQ_FIRST(&s->blamed_commits);
4585 if (!got_object_id_cmp(first->id, s->commit_id))
4586 break;
4587 s->done = 1;
4588 thread_err = stop_blame(&s->blame);
4589 s->done = 0;
4590 if (thread_err)
4591 break;
4592 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4593 got_object_qid_free(s->blamed_commit);
4594 s->blamed_commit =
4595 SIMPLEQ_FIRST(&s->blamed_commits);
4596 err = run_blame(view);
4597 if (err)
4598 break;
4599 break;
4601 case KEY_ENTER:
4602 case '\r': {
4603 struct got_object_id *id = NULL;
4604 struct got_object_qid *pid;
4605 struct got_commit_object *commit = NULL;
4606 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4607 s->first_displayed_line, s->selected_line);
4608 if (id == NULL)
4609 break;
4610 err = got_object_open_as_commit(&commit, s->repo, id);
4611 if (err)
4612 break;
4613 pid = SIMPLEQ_FIRST(
4614 got_object_commit_get_parent_ids(commit));
4615 if (view_is_parent_view(view))
4616 begin_x = view_split_begin_x(view->begin_x);
4617 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4618 if (diff_view == NULL) {
4619 got_object_commit_close(commit);
4620 err = got_error_from_errno("view_open");
4621 break;
4623 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4624 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4625 got_object_commit_close(commit);
4626 if (err) {
4627 view_close(diff_view);
4628 break;
4630 view->focussed = 0;
4631 diff_view->focussed = 1;
4632 if (view_is_parent_view(view)) {
4633 err = view_close_child(view);
4634 if (err)
4635 break;
4636 view_set_child(view, diff_view);
4637 view->focus_child = 1;
4638 } else
4639 *new_view = diff_view;
4640 if (err)
4641 break;
4642 break;
4644 case KEY_NPAGE:
4645 case CTRL('f'):
4646 case ' ':
4647 if (s->last_displayed_line >= s->blame.nlines &&
4648 s->selected_line >= MIN(s->blame.nlines,
4649 view->nlines - 2)) {
4650 break;
4652 if (s->last_displayed_line >= s->blame.nlines &&
4653 s->selected_line < view->nlines - 2) {
4654 s->selected_line = MIN(s->blame.nlines,
4655 view->nlines - 2);
4656 break;
4658 if (s->last_displayed_line + view->nlines - 2
4659 <= s->blame.nlines)
4660 s->first_displayed_line +=
4661 view->nlines - 2;
4662 else
4663 s->first_displayed_line =
4664 s->blame.nlines -
4665 (view->nlines - 3);
4666 break;
4667 case KEY_RESIZE:
4668 if (s->selected_line > view->nlines - 2) {
4669 s->selected_line = MIN(s->blame.nlines,
4670 view->nlines - 2);
4672 break;
4673 default:
4674 break;
4676 return thread_err ? thread_err : err;
4679 static const struct got_error *
4680 cmd_blame(int argc, char *argv[])
4682 const struct got_error *error;
4683 struct got_repository *repo = NULL;
4684 struct got_worktree *worktree = NULL;
4685 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4686 char *link_target = NULL;
4687 struct got_object_id *commit_id = NULL;
4688 char *commit_id_str = NULL;
4689 int ch;
4690 struct tog_view *view;
4692 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4693 switch (ch) {
4694 case 'c':
4695 commit_id_str = optarg;
4696 break;
4697 case 'r':
4698 repo_path = realpath(optarg, NULL);
4699 if (repo_path == NULL)
4700 return got_error_from_errno2("realpath",
4701 optarg);
4702 break;
4703 default:
4704 usage_blame();
4705 /* NOTREACHED */
4709 argc -= optind;
4710 argv += optind;
4712 if (argc != 1)
4713 usage_blame();
4715 if (repo_path == NULL) {
4716 cwd = getcwd(NULL, 0);
4717 if (cwd == NULL)
4718 return got_error_from_errno("getcwd");
4719 error = got_worktree_open(&worktree, cwd);
4720 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4721 goto done;
4722 if (worktree)
4723 repo_path =
4724 strdup(got_worktree_get_repo_path(worktree));
4725 else
4726 repo_path = strdup(cwd);
4727 if (repo_path == NULL) {
4728 error = got_error_from_errno("strdup");
4729 goto done;
4733 error = got_repo_open(&repo, repo_path, NULL);
4734 if (error != NULL)
4735 goto done;
4737 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4738 worktree);
4739 if (error)
4740 goto done;
4742 init_curses();
4744 error = apply_unveil(got_repo_get_path(repo), NULL);
4745 if (error)
4746 goto done;
4748 error = tog_load_refs(repo);
4749 if (error)
4750 goto done;
4752 if (commit_id_str == NULL) {
4753 struct got_reference *head_ref;
4754 error = got_ref_open(&head_ref, repo, worktree ?
4755 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4756 if (error != NULL)
4757 goto done;
4758 error = got_ref_resolve(&commit_id, repo, head_ref);
4759 got_ref_close(head_ref);
4760 } else {
4761 error = got_repo_match_object_id(&commit_id, NULL,
4762 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4764 if (error != NULL)
4765 goto done;
4767 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4768 if (view == NULL) {
4769 error = got_error_from_errno("view_open");
4770 goto done;
4773 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4774 commit_id, repo);
4775 if (error)
4776 goto done;
4778 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4779 commit_id, repo);
4780 if (error)
4781 goto done;
4782 if (worktree) {
4783 /* Release work tree lock. */
4784 got_worktree_close(worktree);
4785 worktree = NULL;
4787 error = view_loop(view);
4788 done:
4789 free(repo_path);
4790 free(in_repo_path);
4791 free(link_target);
4792 free(cwd);
4793 free(commit_id);
4794 if (worktree)
4795 got_worktree_close(worktree);
4796 if (repo) {
4797 const struct got_error *close_err = got_repo_close(repo);
4798 if (error == NULL)
4799 error = close_err;
4801 tog_free_refs();
4802 return error;
4805 static const struct got_error *
4806 draw_tree_entries(struct tog_view *view, const char *parent_path)
4808 struct tog_tree_view_state *s = &view->state.tree;
4809 const struct got_error *err = NULL;
4810 struct got_tree_entry *te;
4811 wchar_t *wline;
4812 struct tog_color *tc;
4813 int width, n, i, nentries;
4814 int limit = view->nlines;
4816 s->ndisplayed = 0;
4818 werase(view->window);
4820 if (limit == 0)
4821 return NULL;
4823 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4824 if (err)
4825 return err;
4826 if (view_needs_focus_indication(view))
4827 wstandout(view->window);
4828 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4829 if (tc)
4830 wattr_on(view->window,
4831 COLOR_PAIR(tc->colorpair), NULL);
4832 waddwstr(view->window, wline);
4833 if (tc)
4834 wattr_off(view->window,
4835 COLOR_PAIR(tc->colorpair), NULL);
4836 if (view_needs_focus_indication(view))
4837 wstandend(view->window);
4838 free(wline);
4839 wline = NULL;
4840 if (width < view->ncols - 1)
4841 waddch(view->window, '\n');
4842 if (--limit <= 0)
4843 return NULL;
4844 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4845 if (err)
4846 return err;
4847 waddwstr(view->window, wline);
4848 free(wline);
4849 wline = NULL;
4850 if (width < view->ncols - 1)
4851 waddch(view->window, '\n');
4852 if (--limit <= 0)
4853 return NULL;
4854 waddch(view->window, '\n');
4855 if (--limit <= 0)
4856 return NULL;
4858 if (s->first_displayed_entry == NULL) {
4859 te = got_object_tree_get_first_entry(s->tree);
4860 if (s->selected == 0) {
4861 if (view->focussed)
4862 wstandout(view->window);
4863 s->selected_entry = NULL;
4865 waddstr(view->window, " ..\n"); /* parent directory */
4866 if (s->selected == 0 && view->focussed)
4867 wstandend(view->window);
4868 s->ndisplayed++;
4869 if (--limit <= 0)
4870 return NULL;
4871 n = 1;
4872 } else {
4873 n = 0;
4874 te = s->first_displayed_entry;
4877 nentries = got_object_tree_get_nentries(s->tree);
4878 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4879 char *line = NULL, *id_str = NULL, *link_target = NULL;
4880 const char *modestr = "";
4881 mode_t mode;
4883 te = got_object_tree_get_entry(s->tree, i);
4884 mode = got_tree_entry_get_mode(te);
4886 if (s->show_ids) {
4887 err = got_object_id_str(&id_str,
4888 got_tree_entry_get_id(te));
4889 if (err)
4890 return got_error_from_errno(
4891 "got_object_id_str");
4893 if (got_object_tree_entry_is_submodule(te))
4894 modestr = "$";
4895 else if (S_ISLNK(mode)) {
4896 int i;
4898 err = got_tree_entry_get_symlink_target(&link_target,
4899 te, s->repo);
4900 if (err) {
4901 free(id_str);
4902 return err;
4904 for (i = 0; i < strlen(link_target); i++) {
4905 if (!isprint((unsigned char)link_target[i]))
4906 link_target[i] = '?';
4908 modestr = "@";
4910 else if (S_ISDIR(mode))
4911 modestr = "/";
4912 else if (mode & S_IXUSR)
4913 modestr = "*";
4914 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4915 got_tree_entry_get_name(te), modestr,
4916 link_target ? " -> ": "",
4917 link_target ? link_target : "") == -1) {
4918 free(id_str);
4919 free(link_target);
4920 return got_error_from_errno("asprintf");
4922 free(id_str);
4923 free(link_target);
4924 err = format_line(&wline, &width, line, view->ncols, 0);
4925 if (err) {
4926 free(line);
4927 break;
4929 if (n == s->selected) {
4930 if (view->focussed)
4931 wstandout(view->window);
4932 s->selected_entry = te;
4934 tc = match_color(&s->colors, line);
4935 if (tc)
4936 wattr_on(view->window,
4937 COLOR_PAIR(tc->colorpair), NULL);
4938 waddwstr(view->window, wline);
4939 if (tc)
4940 wattr_off(view->window,
4941 COLOR_PAIR(tc->colorpair), NULL);
4942 if (width < view->ncols - 1)
4943 waddch(view->window, '\n');
4944 if (n == s->selected && view->focussed)
4945 wstandend(view->window);
4946 free(line);
4947 free(wline);
4948 wline = NULL;
4949 n++;
4950 s->ndisplayed++;
4951 s->last_displayed_entry = te;
4952 if (--limit <= 0)
4953 break;
4956 return err;
4959 static void
4960 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4962 struct got_tree_entry *te;
4963 int isroot = s->tree == s->root;
4964 int i = 0;
4966 if (s->first_displayed_entry == NULL)
4967 return;
4969 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4970 while (i++ < maxscroll) {
4971 if (te == NULL) {
4972 if (!isroot)
4973 s->first_displayed_entry = NULL;
4974 break;
4976 s->first_displayed_entry = te;
4977 te = got_tree_entry_get_prev(s->tree, te);
4981 static void
4982 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4984 struct got_tree_entry *next, *last;
4985 int n = 0;
4987 if (s->first_displayed_entry)
4988 next = got_tree_entry_get_next(s->tree,
4989 s->first_displayed_entry);
4990 else
4991 next = got_object_tree_get_first_entry(s->tree);
4993 last = s->last_displayed_entry;
4994 while (next && last && n++ < maxscroll) {
4995 last = got_tree_entry_get_next(s->tree, last);
4996 if (last) {
4997 s->first_displayed_entry = next;
4998 next = got_tree_entry_get_next(s->tree, next);
5003 static const struct got_error *
5004 tree_entry_path(char **path, struct tog_parent_trees *parents,
5005 struct got_tree_entry *te)
5007 const struct got_error *err = NULL;
5008 struct tog_parent_tree *pt;
5009 size_t len = 2; /* for leading slash and NUL */
5011 TAILQ_FOREACH(pt, parents, entry)
5012 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5013 + 1 /* slash */;
5014 if (te)
5015 len += strlen(got_tree_entry_get_name(te));
5017 *path = calloc(1, len);
5018 if (path == NULL)
5019 return got_error_from_errno("calloc");
5021 (*path)[0] = '/';
5022 pt = TAILQ_LAST(parents, tog_parent_trees);
5023 while (pt) {
5024 const char *name = got_tree_entry_get_name(pt->selected_entry);
5025 if (strlcat(*path, name, len) >= len) {
5026 err = got_error(GOT_ERR_NO_SPACE);
5027 goto done;
5029 if (strlcat(*path, "/", len) >= len) {
5030 err = got_error(GOT_ERR_NO_SPACE);
5031 goto done;
5033 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5035 if (te) {
5036 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5037 err = got_error(GOT_ERR_NO_SPACE);
5038 goto done;
5041 done:
5042 if (err) {
5043 free(*path);
5044 *path = NULL;
5046 return err;
5049 static const struct got_error *
5050 blame_tree_entry(struct tog_view **new_view, int begin_x,
5051 struct got_tree_entry *te, struct tog_parent_trees *parents,
5052 struct got_object_id *commit_id, struct got_repository *repo)
5054 const struct got_error *err = NULL;
5055 char *path;
5056 struct tog_view *blame_view;
5058 *new_view = NULL;
5060 err = tree_entry_path(&path, parents, te);
5061 if (err)
5062 return err;
5064 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5065 if (blame_view == NULL) {
5066 err = got_error_from_errno("view_open");
5067 goto done;
5070 err = open_blame_view(blame_view, path, commit_id, repo);
5071 if (err) {
5072 if (err->code == GOT_ERR_CANCELLED)
5073 err = NULL;
5074 view_close(blame_view);
5075 } else
5076 *new_view = blame_view;
5077 done:
5078 free(path);
5079 return err;
5082 static const struct got_error *
5083 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5084 struct tog_tree_view_state *s)
5086 struct tog_view *log_view;
5087 const struct got_error *err = NULL;
5088 char *path;
5090 *new_view = NULL;
5092 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5093 if (log_view == NULL)
5094 return got_error_from_errno("view_open");
5096 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5097 if (err)
5098 return err;
5100 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5101 path, 0);
5102 if (err)
5103 view_close(log_view);
5104 else
5105 *new_view = log_view;
5106 free(path);
5107 return err;
5110 static const struct got_error *
5111 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5112 struct got_object_id *commit_id, const char *head_ref_name,
5113 struct got_repository *repo)
5115 const struct got_error *err = NULL;
5116 char *commit_id_str = NULL;
5117 struct tog_tree_view_state *s = &view->state.tree;
5119 TAILQ_INIT(&s->parents);
5121 err = got_object_id_str(&commit_id_str, commit_id);
5122 if (err != NULL)
5123 goto done;
5125 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5126 err = got_error_from_errno("asprintf");
5127 goto done;
5130 s->root = s->tree = root;
5131 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5132 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5133 s->commit_id = got_object_id_dup(commit_id);
5134 if (s->commit_id == NULL) {
5135 err = got_error_from_errno("got_object_id_dup");
5136 goto done;
5138 if (head_ref_name) {
5139 s->head_ref_name = strdup(head_ref_name);
5140 if (s->head_ref_name == NULL) {
5141 err = got_error_from_errno("strdup");
5142 goto done;
5145 s->repo = repo;
5147 SIMPLEQ_INIT(&s->colors);
5149 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5150 err = add_color(&s->colors, "\\$$",
5151 TOG_COLOR_TREE_SUBMODULE,
5152 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5153 if (err)
5154 goto done;
5155 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5156 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5157 if (err) {
5158 free_colors(&s->colors);
5159 goto done;
5161 err = add_color(&s->colors, "/$",
5162 TOG_COLOR_TREE_DIRECTORY,
5163 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5164 if (err) {
5165 free_colors(&s->colors);
5166 goto done;
5169 err = add_color(&s->colors, "\\*$",
5170 TOG_COLOR_TREE_EXECUTABLE,
5171 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5172 if (err) {
5173 free_colors(&s->colors);
5174 goto done;
5177 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5178 get_color_value("TOG_COLOR_COMMIT"));
5179 if (err) {
5180 free_colors(&s->colors);
5181 goto done;
5185 view->show = show_tree_view;
5186 view->input = input_tree_view;
5187 view->close = close_tree_view;
5188 view->search_start = search_start_tree_view;
5189 view->search_next = search_next_tree_view;
5190 done:
5191 free(commit_id_str);
5192 if (err) {
5193 free(s->tree_label);
5194 s->tree_label = NULL;
5196 return err;
5199 static const struct got_error *
5200 close_tree_view(struct tog_view *view)
5202 struct tog_tree_view_state *s = &view->state.tree;
5204 free_colors(&s->colors);
5205 free(s->tree_label);
5206 s->tree_label = NULL;
5207 free(s->commit_id);
5208 s->commit_id = NULL;
5209 free(s->head_ref_name);
5210 s->head_ref_name = NULL;
5211 while (!TAILQ_EMPTY(&s->parents)) {
5212 struct tog_parent_tree *parent;
5213 parent = TAILQ_FIRST(&s->parents);
5214 TAILQ_REMOVE(&s->parents, parent, entry);
5215 free(parent);
5218 if (s->tree != s->root)
5219 got_object_tree_close(s->tree);
5220 got_object_tree_close(s->root);
5221 return NULL;
5224 static const struct got_error *
5225 search_start_tree_view(struct tog_view *view)
5227 struct tog_tree_view_state *s = &view->state.tree;
5229 s->matched_entry = NULL;
5230 return NULL;
5233 static int
5234 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5236 regmatch_t regmatch;
5238 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5239 0) == 0;
5242 static const struct got_error *
5243 search_next_tree_view(struct tog_view *view)
5245 struct tog_tree_view_state *s = &view->state.tree;
5246 struct got_tree_entry *te = NULL;
5248 if (!view->searching) {
5249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5250 return NULL;
5253 if (s->matched_entry) {
5254 if (view->searching == TOG_SEARCH_FORWARD) {
5255 if (s->selected_entry)
5256 te = got_tree_entry_get_next(s->tree,
5257 s->selected_entry);
5258 else
5259 te = got_object_tree_get_first_entry(s->tree);
5260 } else {
5261 if (s->selected_entry == NULL)
5262 te = got_object_tree_get_last_entry(s->tree);
5263 else
5264 te = got_tree_entry_get_prev(s->tree,
5265 s->selected_entry);
5267 } else {
5268 if (view->searching == TOG_SEARCH_FORWARD)
5269 te = got_object_tree_get_first_entry(s->tree);
5270 else
5271 te = got_object_tree_get_last_entry(s->tree);
5274 while (1) {
5275 if (te == NULL) {
5276 if (s->matched_entry == NULL) {
5277 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5278 return NULL;
5280 if (view->searching == TOG_SEARCH_FORWARD)
5281 te = got_object_tree_get_first_entry(s->tree);
5282 else
5283 te = got_object_tree_get_last_entry(s->tree);
5286 if (match_tree_entry(te, &view->regex)) {
5287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5288 s->matched_entry = te;
5289 break;
5292 if (view->searching == TOG_SEARCH_FORWARD)
5293 te = got_tree_entry_get_next(s->tree, te);
5294 else
5295 te = got_tree_entry_get_prev(s->tree, te);
5298 if (s->matched_entry) {
5299 s->first_displayed_entry = s->matched_entry;
5300 s->selected = 0;
5303 return NULL;
5306 static const struct got_error *
5307 show_tree_view(struct tog_view *view)
5309 const struct got_error *err = NULL;
5310 struct tog_tree_view_state *s = &view->state.tree;
5311 char *parent_path;
5313 err = tree_entry_path(&parent_path, &s->parents, NULL);
5314 if (err)
5315 return err;
5317 err = draw_tree_entries(view, parent_path);
5318 free(parent_path);
5320 view_vborder(view);
5321 return err;
5324 static const struct got_error *
5325 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5327 const struct got_error *err = NULL;
5328 struct tog_tree_view_state *s = &view->state.tree;
5329 struct tog_view *log_view, *ref_view;
5330 int begin_x = 0;
5332 switch (ch) {
5333 case 'i':
5334 s->show_ids = !s->show_ids;
5335 break;
5336 case 'l':
5337 if (!s->selected_entry)
5338 break;
5339 if (view_is_parent_view(view))
5340 begin_x = view_split_begin_x(view->begin_x);
5341 err = log_selected_tree_entry(&log_view, begin_x, s);
5342 view->focussed = 0;
5343 log_view->focussed = 1;
5344 if (view_is_parent_view(view)) {
5345 err = view_close_child(view);
5346 if (err)
5347 return err;
5348 view_set_child(view, log_view);
5349 view->focus_child = 1;
5350 } else
5351 *new_view = log_view;
5352 break;
5353 case 'r':
5354 if (view_is_parent_view(view))
5355 begin_x = view_split_begin_x(view->begin_x);
5356 ref_view = view_open(view->nlines, view->ncols,
5357 view->begin_y, begin_x, TOG_VIEW_REF);
5358 if (ref_view == NULL)
5359 return got_error_from_errno("view_open");
5360 err = open_ref_view(ref_view, s->repo);
5361 if (err) {
5362 view_close(ref_view);
5363 return err;
5365 view->focussed = 0;
5366 ref_view->focussed = 1;
5367 if (view_is_parent_view(view)) {
5368 err = view_close_child(view);
5369 if (err)
5370 return err;
5371 view_set_child(view, ref_view);
5372 view->focus_child = 1;
5373 } else
5374 *new_view = ref_view;
5375 break;
5376 case 'k':
5377 case KEY_UP:
5378 if (s->selected > 0) {
5379 s->selected--;
5380 break;
5382 tree_scroll_up(s, 1);
5383 break;
5384 case KEY_PPAGE:
5385 case CTRL('b'):
5386 if (s->tree == s->root) {
5387 if (got_object_tree_get_first_entry(s->tree) ==
5388 s->first_displayed_entry)
5389 s->selected = 0;
5390 } else {
5391 if (s->first_displayed_entry == NULL)
5392 s->selected = 0;
5394 tree_scroll_up(s, MAX(0, view->nlines - 3));
5395 break;
5396 case 'j':
5397 case KEY_DOWN:
5398 if (s->selected < s->ndisplayed - 1) {
5399 s->selected++;
5400 break;
5402 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5403 == NULL)
5404 /* can't scroll any further */
5405 break;
5406 tree_scroll_down(s, 1);
5407 break;
5408 case KEY_NPAGE:
5409 case CTRL('f'):
5410 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5411 == NULL) {
5412 /* can't scroll any further; move cursor down */
5413 if (s->selected < s->ndisplayed - 1)
5414 s->selected = s->ndisplayed - 1;
5415 break;
5417 tree_scroll_down(s, view->nlines - 3);
5418 break;
5419 case KEY_ENTER:
5420 case '\r':
5421 case KEY_BACKSPACE:
5422 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5423 struct tog_parent_tree *parent;
5424 /* user selected '..' */
5425 if (s->tree == s->root)
5426 break;
5427 parent = TAILQ_FIRST(&s->parents);
5428 TAILQ_REMOVE(&s->parents, parent,
5429 entry);
5430 got_object_tree_close(s->tree);
5431 s->tree = parent->tree;
5432 s->first_displayed_entry =
5433 parent->first_displayed_entry;
5434 s->selected_entry =
5435 parent->selected_entry;
5436 s->selected = parent->selected;
5437 free(parent);
5438 } else if (S_ISDIR(got_tree_entry_get_mode(
5439 s->selected_entry))) {
5440 struct got_tree_object *subtree;
5441 err = got_object_open_as_tree(&subtree, s->repo,
5442 got_tree_entry_get_id(s->selected_entry));
5443 if (err)
5444 break;
5445 err = tree_view_visit_subtree(s, subtree);
5446 if (err) {
5447 got_object_tree_close(subtree);
5448 break;
5450 } else if (S_ISREG(got_tree_entry_get_mode(
5451 s->selected_entry))) {
5452 struct tog_view *blame_view;
5453 int begin_x = view_is_parent_view(view) ?
5454 view_split_begin_x(view->begin_x) : 0;
5456 err = blame_tree_entry(&blame_view, begin_x,
5457 s->selected_entry, &s->parents,
5458 s->commit_id, s->repo);
5459 if (err)
5460 break;
5461 view->focussed = 0;
5462 blame_view->focussed = 1;
5463 if (view_is_parent_view(view)) {
5464 err = view_close_child(view);
5465 if (err)
5466 return err;
5467 view_set_child(view, blame_view);
5468 view->focus_child = 1;
5469 } else
5470 *new_view = blame_view;
5472 break;
5473 case KEY_RESIZE:
5474 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5475 s->selected = view->nlines - 4;
5476 break;
5477 default:
5478 break;
5481 return err;
5484 __dead static void
5485 usage_tree(void)
5487 endwin();
5488 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5489 getprogname());
5490 exit(1);
5493 static const struct got_error *
5494 cmd_tree(int argc, char *argv[])
5496 const struct got_error *error;
5497 struct got_repository *repo = NULL;
5498 struct got_worktree *worktree = NULL;
5499 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5500 struct got_object_id *commit_id = NULL;
5501 const char *commit_id_arg = NULL;
5502 char *label = NULL;
5503 struct got_commit_object *commit = NULL;
5504 struct got_tree_object *tree = NULL;
5505 struct got_reference *ref = NULL;
5506 const char *head_ref_name = NULL;
5507 int ch;
5508 struct tog_view *view;
5510 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5511 switch (ch) {
5512 case 'c':
5513 commit_id_arg = optarg;
5514 break;
5515 case 'r':
5516 repo_path = realpath(optarg, NULL);
5517 if (repo_path == NULL)
5518 return got_error_from_errno2("realpath",
5519 optarg);
5520 break;
5521 default:
5522 usage_tree();
5523 /* NOTREACHED */
5527 argc -= optind;
5528 argv += optind;
5530 if (argc > 1)
5531 usage_tree();
5533 if (repo_path == NULL) {
5534 cwd = getcwd(NULL, 0);
5535 if (cwd == NULL)
5536 return got_error_from_errno("getcwd");
5537 error = got_worktree_open(&worktree, cwd);
5538 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5539 goto done;
5540 if (worktree)
5541 repo_path =
5542 strdup(got_worktree_get_repo_path(worktree));
5543 else
5544 repo_path = strdup(cwd);
5545 if (repo_path == NULL) {
5546 error = got_error_from_errno("strdup");
5547 goto done;
5551 error = got_repo_open(&repo, repo_path, NULL);
5552 if (error != NULL)
5553 goto done;
5555 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5556 repo, worktree);
5557 if (error)
5558 goto done;
5560 init_curses();
5562 error = apply_unveil(got_repo_get_path(repo), NULL);
5563 if (error)
5564 goto done;
5566 error = tog_load_refs(repo);
5567 if (error)
5568 goto done;
5570 if (commit_id_arg == NULL) {
5571 error = got_repo_match_object_id(&commit_id, &label,
5572 worktree ? got_worktree_get_head_ref_name(worktree) :
5573 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5574 if (error)
5575 goto done;
5576 head_ref_name = label;
5577 } else {
5578 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5579 if (error == NULL)
5580 head_ref_name = got_ref_get_name(ref);
5581 else if (error->code != GOT_ERR_NOT_REF)
5582 goto done;
5583 error = got_repo_match_object_id(&commit_id, NULL,
5584 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5585 if (error)
5586 goto done;
5589 error = got_object_open_as_commit(&commit, repo, commit_id);
5590 if (error)
5591 goto done;
5593 error = got_object_open_as_tree(&tree, repo,
5594 got_object_commit_get_tree_id(commit));
5595 if (error)
5596 goto done;
5598 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5599 if (view == NULL) {
5600 error = got_error_from_errno("view_open");
5601 goto done;
5603 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5604 if (error)
5605 goto done;
5606 if (!got_path_is_root_dir(in_repo_path)) {
5607 error = tree_view_walk_path(&view->state.tree, commit_id,
5608 in_repo_path);
5609 if (error)
5610 goto done;
5613 if (worktree) {
5614 /* Release work tree lock. */
5615 got_worktree_close(worktree);
5616 worktree = NULL;
5618 error = view_loop(view);
5619 done:
5620 free(repo_path);
5621 free(cwd);
5622 free(commit_id);
5623 free(label);
5624 if (ref)
5625 got_ref_close(ref);
5626 if (commit)
5627 got_object_commit_close(commit);
5628 if (tree)
5629 got_object_tree_close(tree);
5630 if (repo) {
5631 const struct got_error *close_err = got_repo_close(repo);
5632 if (error == NULL)
5633 error = close_err;
5635 tog_free_refs();
5636 return error;
5639 static const struct got_error *
5640 ref_view_load_refs(struct tog_ref_view_state *s)
5642 struct got_reflist_entry *sre;
5643 struct tog_reflist_entry *re;
5645 s->nrefs = 0;
5646 TAILQ_FOREACH(sre, &tog_refs, entry) {
5647 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5648 continue;
5650 re = malloc(sizeof(*re));
5651 if (re == NULL)
5652 return got_error_from_errno("malloc");
5654 re->ref = got_ref_dup(sre->ref);
5655 if (re->ref == NULL)
5656 return got_error_from_errno("got_ref_dup");
5657 re->idx = s->nrefs++;
5658 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5661 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5662 return NULL;
5665 void
5666 ref_view_free_refs(struct tog_ref_view_state *s)
5668 struct tog_reflist_entry *re;
5670 while (!TAILQ_EMPTY(&s->refs)) {
5671 re = TAILQ_FIRST(&s->refs);
5672 TAILQ_REMOVE(&s->refs, re, entry);
5673 got_ref_close(re->ref);
5674 free(re);
5678 static const struct got_error *
5679 open_ref_view(struct tog_view *view, struct got_repository *repo)
5681 const struct got_error *err = NULL;
5682 struct tog_ref_view_state *s = &view->state.ref;
5684 s->selected_entry = 0;
5685 s->repo = repo;
5687 TAILQ_INIT(&s->refs);
5688 SIMPLEQ_INIT(&s->colors);
5690 err = ref_view_load_refs(s);
5691 if (err)
5692 return err;
5694 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5695 err = add_color(&s->colors, "^refs/heads/",
5696 TOG_COLOR_REFS_HEADS,
5697 get_color_value("TOG_COLOR_REFS_HEADS"));
5698 if (err)
5699 goto done;
5701 err = add_color(&s->colors, "^refs/tags/",
5702 TOG_COLOR_REFS_TAGS,
5703 get_color_value("TOG_COLOR_REFS_TAGS"));
5704 if (err)
5705 goto done;
5707 err = add_color(&s->colors, "^refs/remotes/",
5708 TOG_COLOR_REFS_REMOTES,
5709 get_color_value("TOG_COLOR_REFS_REMOTES"));
5710 if (err)
5711 goto done;
5714 view->show = show_ref_view;
5715 view->input = input_ref_view;
5716 view->close = close_ref_view;
5717 view->search_start = search_start_ref_view;
5718 view->search_next = search_next_ref_view;
5719 done:
5720 if (err)
5721 free_colors(&s->colors);
5722 return err;
5725 static const struct got_error *
5726 close_ref_view(struct tog_view *view)
5728 struct tog_ref_view_state *s = &view->state.ref;
5730 ref_view_free_refs(s);
5731 free_colors(&s->colors);
5733 return NULL;
5736 static const struct got_error *
5737 resolve_reflist_entry(struct got_object_id **commit_id,
5738 struct tog_reflist_entry *re, struct got_repository *repo)
5740 const struct got_error *err = NULL;
5741 struct got_object_id *obj_id;
5742 struct got_tag_object *tag = NULL;
5743 int obj_type;
5745 *commit_id = NULL;
5747 err = got_ref_resolve(&obj_id, repo, re->ref);
5748 if (err)
5749 return err;
5751 err = got_object_get_type(&obj_type, repo, obj_id);
5752 if (err)
5753 goto done;
5755 switch (obj_type) {
5756 case GOT_OBJ_TYPE_COMMIT:
5757 *commit_id = obj_id;
5758 break;
5759 case GOT_OBJ_TYPE_TAG:
5760 err = got_object_open_as_tag(&tag, repo, obj_id);
5761 if (err)
5762 goto done;
5763 free(obj_id);
5764 err = got_object_get_type(&obj_type, repo,
5765 got_object_tag_get_object_id(tag));
5766 if (err)
5767 goto done;
5768 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5769 err = got_error(GOT_ERR_OBJ_TYPE);
5770 goto done;
5772 *commit_id = got_object_id_dup(
5773 got_object_tag_get_object_id(tag));
5774 if (*commit_id == NULL) {
5775 err = got_error_from_errno("got_object_id_dup");
5776 goto done;
5778 break;
5779 default:
5780 err = got_error(GOT_ERR_OBJ_TYPE);
5781 break;
5784 done:
5785 if (tag)
5786 got_object_tag_close(tag);
5787 if (err) {
5788 free(*commit_id);
5789 *commit_id = NULL;
5791 return err;
5794 static const struct got_error *
5795 log_ref_entry(struct tog_view **new_view, int begin_x,
5796 struct tog_reflist_entry *re, struct got_repository *repo)
5798 struct tog_view *log_view;
5799 const struct got_error *err = NULL;
5800 struct got_object_id *commit_id = NULL;
5802 *new_view = NULL;
5804 err = resolve_reflist_entry(&commit_id, re, repo);
5805 if (err) {
5806 if (err->code != GOT_ERR_OBJ_TYPE)
5807 return err;
5808 else
5809 return NULL;
5812 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5813 if (log_view == NULL) {
5814 err = got_error_from_errno("view_open");
5815 goto done;
5818 err = open_log_view(log_view, commit_id, repo,
5819 got_ref_get_name(re->ref), "", 0);
5820 done:
5821 if (err)
5822 view_close(log_view);
5823 else
5824 *new_view = log_view;
5825 free(commit_id);
5826 return err;
5829 static void
5830 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5832 struct tog_reflist_entry *re;
5833 int i = 0;
5835 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5836 return;
5838 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5839 while (i++ < maxscroll) {
5840 if (re == NULL)
5841 break;
5842 s->first_displayed_entry = re;
5843 re = TAILQ_PREV(re, tog_reflist_head, entry);
5847 static void
5848 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5850 struct tog_reflist_entry *next, *last;
5851 int n = 0;
5853 if (s->first_displayed_entry)
5854 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5855 else
5856 next = TAILQ_FIRST(&s->refs);
5858 last = s->last_displayed_entry;
5859 while (next && last && n++ < maxscroll) {
5860 last = TAILQ_NEXT(last, entry);
5861 if (last) {
5862 s->first_displayed_entry = next;
5863 next = TAILQ_NEXT(next, entry);
5868 static const struct got_error *
5869 search_start_ref_view(struct tog_view *view)
5871 struct tog_ref_view_state *s = &view->state.ref;
5873 s->matched_entry = NULL;
5874 return NULL;
5877 static int
5878 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5880 regmatch_t regmatch;
5882 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5883 0) == 0;
5886 static const struct got_error *
5887 search_next_ref_view(struct tog_view *view)
5889 struct tog_ref_view_state *s = &view->state.ref;
5890 struct tog_reflist_entry *re = NULL;
5892 if (!view->searching) {
5893 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5894 return NULL;
5897 if (s->matched_entry) {
5898 if (view->searching == TOG_SEARCH_FORWARD) {
5899 if (s->selected_entry)
5900 re = TAILQ_NEXT(s->selected_entry, entry);
5901 else
5902 re = TAILQ_PREV(s->selected_entry,
5903 tog_reflist_head, entry);
5904 } else {
5905 if (s->selected_entry == NULL)
5906 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5907 else
5908 re = TAILQ_PREV(s->selected_entry,
5909 tog_reflist_head, entry);
5911 } else {
5912 if (view->searching == TOG_SEARCH_FORWARD)
5913 re = TAILQ_FIRST(&s->refs);
5914 else
5915 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5918 while (1) {
5919 if (re == NULL) {
5920 if (s->matched_entry == NULL) {
5921 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5922 return NULL;
5924 if (view->searching == TOG_SEARCH_FORWARD)
5925 re = TAILQ_FIRST(&s->refs);
5926 else
5927 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5930 if (match_reflist_entry(re, &view->regex)) {
5931 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5932 s->matched_entry = re;
5933 break;
5936 if (view->searching == TOG_SEARCH_FORWARD)
5937 re = TAILQ_NEXT(re, entry);
5938 else
5939 re = TAILQ_PREV(re, tog_reflist_head, entry);
5942 if (s->matched_entry) {
5943 s->first_displayed_entry = s->matched_entry;
5944 s->selected = 0;
5947 return NULL;
5950 static const struct got_error *
5951 show_ref_view(struct tog_view *view)
5953 const struct got_error *err = NULL;
5954 struct tog_ref_view_state *s = &view->state.ref;
5955 struct tog_reflist_entry *re;
5956 char *line = NULL;
5957 wchar_t *wline;
5958 struct tog_color *tc;
5959 int width, n;
5960 int limit = view->nlines;
5962 werase(view->window);
5964 s->ndisplayed = 0;
5966 if (limit == 0)
5967 return NULL;
5969 re = s->first_displayed_entry;
5971 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5972 s->nrefs) == -1)
5973 return got_error_from_errno("asprintf");
5975 err = format_line(&wline, &width, line, view->ncols, 0);
5976 if (err) {
5977 free(line);
5978 return err;
5980 if (view_needs_focus_indication(view))
5981 wstandout(view->window);
5982 waddwstr(view->window, wline);
5983 if (view_needs_focus_indication(view))
5984 wstandend(view->window);
5985 free(wline);
5986 wline = NULL;
5987 free(line);
5988 line = NULL;
5989 if (width < view->ncols - 1)
5990 waddch(view->window, '\n');
5991 if (--limit <= 0)
5992 return NULL;
5994 n = 0;
5995 while (re && limit > 0) {
5996 char *line = NULL;
5998 if (got_ref_is_symbolic(re->ref)) {
5999 if (asprintf(&line, "%s -> %s",
6000 got_ref_get_name(re->ref),
6001 got_ref_get_symref_target(re->ref)) == -1)
6002 return got_error_from_errno("asprintf");
6003 } else if (s->show_ids) {
6004 struct got_object_id *id;
6005 char *id_str;
6006 err = got_ref_resolve(&id, s->repo, re->ref);
6007 if (err)
6008 return err;
6009 err = got_object_id_str(&id_str, id);
6010 if (err) {
6011 free(id);
6012 return err;
6014 if (asprintf(&line, "%s: %s",
6015 got_ref_get_name(re->ref), id_str) == -1) {
6016 err = got_error_from_errno("asprintf");
6017 free(id);
6018 free(id_str);
6019 return err;
6021 free(id);
6022 free(id_str);
6023 } else {
6024 line = strdup(got_ref_get_name(re->ref));
6025 if (line == NULL)
6026 return got_error_from_errno("strdup");
6029 err = format_line(&wline, &width, line, view->ncols, 0);
6030 if (err) {
6031 free(line);
6032 return err;
6034 if (n == s->selected) {
6035 if (view->focussed)
6036 wstandout(view->window);
6037 s->selected_entry = re;
6039 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6040 if (tc)
6041 wattr_on(view->window,
6042 COLOR_PAIR(tc->colorpair), NULL);
6043 waddwstr(view->window, wline);
6044 if (tc)
6045 wattr_off(view->window,
6046 COLOR_PAIR(tc->colorpair), NULL);
6047 if (width < view->ncols - 1)
6048 waddch(view->window, '\n');
6049 if (n == s->selected && view->focussed)
6050 wstandend(view->window);
6051 free(line);
6052 free(wline);
6053 wline = NULL;
6054 n++;
6055 s->ndisplayed++;
6056 s->last_displayed_entry = re;
6058 limit--;
6059 re = TAILQ_NEXT(re, entry);
6062 view_vborder(view);
6063 return err;
6066 static const struct got_error *
6067 browse_ref_tree(struct tog_view **new_view, int begin_x,
6068 struct tog_reflist_entry *re, struct got_repository *repo)
6070 const struct got_error *err = NULL;
6071 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6072 struct got_tree_object *tree = NULL;
6073 struct tog_view *tree_view;
6075 *new_view = NULL;
6077 err = resolve_reflist_entry(&commit_id, re, repo);
6078 if (err) {
6079 if (err->code != GOT_ERR_OBJ_TYPE)
6080 return err;
6081 else
6082 return NULL;
6085 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6086 if (err)
6087 goto done;
6089 err = got_object_open_as_tree(&tree, repo, tree_id);
6090 if (err)
6091 goto done;
6093 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6094 if (tree_view == NULL) {
6095 err = got_error_from_errno("view_open");
6096 goto done;
6099 err = open_tree_view(tree_view, tree, commit_id,
6100 got_ref_get_name(re->ref), repo);
6101 if (err)
6102 goto done;
6104 *new_view = tree_view;
6105 done:
6106 free(commit_id);
6107 free(tree_id);
6108 if (err) {
6109 if (tree)
6110 got_object_tree_close(tree);
6112 return err;
6114 static const struct got_error *
6115 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6117 const struct got_error *err = NULL;
6118 struct tog_ref_view_state *s = &view->state.ref;
6119 struct tog_view *log_view, *tree_view;
6120 int begin_x = 0;
6122 switch (ch) {
6123 case 'i':
6124 s->show_ids = !s->show_ids;
6125 break;
6126 case KEY_ENTER:
6127 case '\r':
6128 if (!s->selected_entry)
6129 break;
6130 if (view_is_parent_view(view))
6131 begin_x = view_split_begin_x(view->begin_x);
6132 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6133 s->repo);
6134 view->focussed = 0;
6135 log_view->focussed = 1;
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6138 if (err)
6139 return err;
6140 view_set_child(view, log_view);
6141 view->focus_child = 1;
6142 } else
6143 *new_view = log_view;
6144 break;
6145 case 't':
6146 if (!s->selected_entry)
6147 break;
6148 if (view_is_parent_view(view))
6149 begin_x = view_split_begin_x(view->begin_x);
6150 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6151 s->repo);
6152 if (err || tree_view == NULL)
6153 break;
6154 view->focussed = 0;
6155 tree_view->focussed = 1;
6156 if (view_is_parent_view(view)) {
6157 err = view_close_child(view);
6158 if (err)
6159 return err;
6160 view_set_child(view, tree_view);
6161 view->focus_child = 1;
6162 } else
6163 *new_view = tree_view;
6164 break;
6165 case 'k':
6166 case KEY_UP:
6167 if (s->selected > 0) {
6168 s->selected--;
6169 break;
6171 ref_scroll_up(s, 1);
6172 break;
6173 case KEY_PPAGE:
6174 case CTRL('b'):
6175 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6176 s->selected = 0;
6177 ref_scroll_up(s, MAX(0, view->nlines - 1));
6178 break;
6179 case 'j':
6180 case KEY_DOWN:
6181 if (s->selected < s->ndisplayed - 1) {
6182 s->selected++;
6183 break;
6185 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6186 /* can't scroll any further */
6187 break;
6188 ref_scroll_down(s, 1);
6189 break;
6190 case KEY_NPAGE:
6191 case CTRL('f'):
6192 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6193 /* can't scroll any further; move cursor down */
6194 if (s->selected < s->ndisplayed - 1)
6195 s->selected = s->ndisplayed - 1;
6196 break;
6198 ref_scroll_down(s, view->nlines - 1);
6199 break;
6200 case CTRL('l'):
6201 tog_free_refs();
6202 err = tog_load_refs(s->repo);
6203 if (err)
6204 break;
6205 ref_view_free_refs(s);
6206 err = ref_view_load_refs(s);
6207 break;
6208 case KEY_RESIZE:
6209 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6210 s->selected = view->nlines - 2;
6211 break;
6212 default:
6213 break;
6216 return err;
6219 __dead static void
6220 usage_ref(void)
6222 endwin();
6223 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6224 getprogname());
6225 exit(1);
6228 static const struct got_error *
6229 cmd_ref(int argc, char *argv[])
6231 const struct got_error *error;
6232 struct got_repository *repo = NULL;
6233 struct got_worktree *worktree = NULL;
6234 char *cwd = NULL, *repo_path = NULL;
6235 int ch;
6236 struct tog_view *view;
6238 while ((ch = getopt(argc, argv, "r:")) != -1) {
6239 switch (ch) {
6240 case 'r':
6241 repo_path = realpath(optarg, NULL);
6242 if (repo_path == NULL)
6243 return got_error_from_errno2("realpath",
6244 optarg);
6245 break;
6246 default:
6247 usage_ref();
6248 /* NOTREACHED */
6252 argc -= optind;
6253 argv += optind;
6255 if (argc > 1)
6256 usage_ref();
6258 if (repo_path == NULL) {
6259 cwd = getcwd(NULL, 0);
6260 if (cwd == NULL)
6261 return got_error_from_errno("getcwd");
6262 error = got_worktree_open(&worktree, cwd);
6263 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6264 goto done;
6265 if (worktree)
6266 repo_path =
6267 strdup(got_worktree_get_repo_path(worktree));
6268 else
6269 repo_path = strdup(cwd);
6270 if (repo_path == NULL) {
6271 error = got_error_from_errno("strdup");
6272 goto done;
6276 error = got_repo_open(&repo, repo_path, NULL);
6277 if (error != NULL)
6278 goto done;
6280 init_curses();
6282 error = apply_unveil(got_repo_get_path(repo), NULL);
6283 if (error)
6284 goto done;
6286 error = tog_load_refs(repo);
6287 if (error)
6288 goto done;
6290 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6291 if (view == NULL) {
6292 error = got_error_from_errno("view_open");
6293 goto done;
6296 error = open_ref_view(view, repo);
6297 if (error)
6298 goto done;
6300 if (worktree) {
6301 /* Release work tree lock. */
6302 got_worktree_close(worktree);
6303 worktree = NULL;
6305 error = view_loop(view);
6306 done:
6307 free(repo_path);
6308 free(cwd);
6309 if (repo) {
6310 const struct got_error *close_err = got_repo_close(repo);
6311 if (close_err)
6312 error = close_err;
6314 tog_free_refs();
6315 return error;
6318 static void
6319 list_commands(FILE *fp)
6321 size_t i;
6323 fprintf(fp, "commands:");
6324 for (i = 0; i < nitems(tog_commands); i++) {
6325 struct tog_cmd *cmd = &tog_commands[i];
6326 fprintf(fp, " %s", cmd->name);
6328 fputc('\n', fp);
6331 __dead static void
6332 usage(int hflag, int status)
6334 FILE *fp = (status == 0) ? stdout : stderr;
6336 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6337 getprogname());
6338 if (hflag) {
6339 fprintf(fp, "lazy usage: %s path\n", getprogname());
6340 list_commands(fp);
6342 exit(status);
6345 static char **
6346 make_argv(int argc, ...)
6348 va_list ap;
6349 char **argv;
6350 int i;
6352 va_start(ap, argc);
6354 argv = calloc(argc, sizeof(char *));
6355 if (argv == NULL)
6356 err(1, "calloc");
6357 for (i = 0; i < argc; i++) {
6358 argv[i] = strdup(va_arg(ap, char *));
6359 if (argv[i] == NULL)
6360 err(1, "strdup");
6363 va_end(ap);
6364 return argv;
6368 * Try to convert 'tog path' into a 'tog log path' command.
6369 * The user could simply have mistyped the command rather than knowingly
6370 * provided a path. So check whether argv[0] can in fact be resolved
6371 * to a path in the HEAD commit and print a special error if not.
6372 * This hack is for mpi@ <3
6374 static const struct got_error *
6375 tog_log_with_path(int argc, char *argv[])
6377 const struct got_error *error = NULL, *close_err;
6378 struct tog_cmd *cmd = NULL;
6379 struct got_repository *repo = NULL;
6380 struct got_worktree *worktree = NULL;
6381 struct got_object_id *commit_id = NULL, *id = NULL;
6382 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6383 char *commit_id_str = NULL, **cmd_argv = NULL;
6385 cwd = getcwd(NULL, 0);
6386 if (cwd == NULL)
6387 return got_error_from_errno("getcwd");
6389 error = got_worktree_open(&worktree, cwd);
6390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6391 goto done;
6393 if (worktree)
6394 repo_path = strdup(got_worktree_get_repo_path(worktree));
6395 else
6396 repo_path = strdup(cwd);
6397 if (repo_path == NULL) {
6398 error = got_error_from_errno("strdup");
6399 goto done;
6402 error = got_repo_open(&repo, repo_path, NULL);
6403 if (error != NULL)
6404 goto done;
6406 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6407 repo, worktree);
6408 if (error)
6409 goto done;
6411 error = tog_load_refs(repo);
6412 if (error)
6413 goto done;
6414 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6415 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6416 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6417 if (error)
6418 goto done;
6420 if (worktree) {
6421 got_worktree_close(worktree);
6422 worktree = NULL;
6425 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6426 if (error) {
6427 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6428 goto done;
6429 fprintf(stderr, "%s: '%s' is no known command or path\n",
6430 getprogname(), argv[0]);
6431 usage(1, 1);
6432 /* not reached */
6435 close_err = got_repo_close(repo);
6436 if (error == NULL)
6437 error = close_err;
6438 repo = NULL;
6440 error = got_object_id_str(&commit_id_str, commit_id);
6441 if (error)
6442 goto done;
6444 cmd = &tog_commands[0]; /* log */
6445 argc = 4;
6446 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6447 error = cmd->cmd_main(argc, cmd_argv);
6448 done:
6449 if (repo) {
6450 close_err = got_repo_close(repo);
6451 if (error == NULL)
6452 error = close_err;
6454 if (worktree)
6455 got_worktree_close(worktree);
6456 free(id);
6457 free(commit_id_str);
6458 free(commit_id);
6459 free(cwd);
6460 free(repo_path);
6461 free(in_repo_path);
6462 if (cmd_argv) {
6463 int i;
6464 for (i = 0; i < argc; i++)
6465 free(cmd_argv[i]);
6466 free(cmd_argv);
6468 tog_free_refs();
6469 return error;
6472 int
6473 main(int argc, char *argv[])
6475 const struct got_error *error = NULL;
6476 struct tog_cmd *cmd = NULL;
6477 int ch, hflag = 0, Vflag = 0;
6478 char **cmd_argv = NULL;
6479 static struct option longopts[] = {
6480 { "version", no_argument, NULL, 'V' },
6481 { NULL, 0, NULL, 0}
6484 setlocale(LC_CTYPE, "");
6486 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6487 switch (ch) {
6488 case 'h':
6489 hflag = 1;
6490 break;
6491 case 'V':
6492 Vflag = 1;
6493 break;
6494 default:
6495 usage(hflag, 1);
6496 /* NOTREACHED */
6500 argc -= optind;
6501 argv += optind;
6502 optind = 1;
6503 optreset = 1;
6505 if (Vflag) {
6506 got_version_print_str();
6507 return 0;
6510 #ifndef PROFILE
6511 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6512 NULL) == -1)
6513 err(1, "pledge");
6514 #endif
6516 if (argc == 0) {
6517 if (hflag)
6518 usage(hflag, 0);
6519 /* Build an argument vector which runs a default command. */
6520 cmd = &tog_commands[0];
6521 argc = 1;
6522 cmd_argv = make_argv(argc, cmd->name);
6523 } else {
6524 size_t i;
6526 /* Did the user specify a command? */
6527 for (i = 0; i < nitems(tog_commands); i++) {
6528 if (strncmp(tog_commands[i].name, argv[0],
6529 strlen(argv[0])) == 0) {
6530 cmd = &tog_commands[i];
6531 break;
6536 if (cmd == NULL) {
6537 if (argc != 1)
6538 usage(0, 1);
6539 /* No command specified; try log with a path */
6540 error = tog_log_with_path(argc, argv);
6541 } else {
6542 if (hflag)
6543 cmd->cmd_usage();
6544 else
6545 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6548 endwin();
6549 putchar('\n');
6550 if (cmd_argv) {
6551 int i;
6552 for (i = 0; i < argc; i++)
6553 free(cmd_argv[i]);
6554 free(cmd_argv);
6557 if (error && error->code != GOT_ERR_CANCELLED)
6558 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6559 return 0;