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 = SIMPLEQ_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_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 got_reflist_head simplerefs; /* SIMPLEQ */
438 struct tog_reflist_head refs; /* TAILQ */
439 struct tog_reflist_entry *first_displayed_entry;
440 struct tog_reflist_entry *last_displayed_entry;
441 struct tog_reflist_entry *selected_entry;
442 int nrefs, ndisplayed, selected, show_ids;
443 struct got_repository *repo;
444 struct tog_reflist_entry *matched_entry;
445 struct tog_colors colors;
446 };
448 /*
449 * We implement two types of views: parent views and child views.
451 * The 'Tab' key switches focus between a parent view and its child view.
452 * Child views are shown side-by-side to their parent view, provided
453 * there is enough screen estate.
455 * When a new view is opened from within a parent view, this new view
456 * becomes a child view of the parent view, replacing any existing child.
458 * When a new view is opened from within a child view, this new view
459 * becomes a parent view which will obscure the views below until the
460 * user quits the new parent view by typing 'q'.
462 * This list of views contains parent views only.
463 * Child views are only pointed to by their parent view.
464 */
465 TAILQ_HEAD(tog_view_list_head, tog_view);
467 struct tog_view {
468 TAILQ_ENTRY(tog_view) entry;
469 WINDOW *window;
470 PANEL *panel;
471 int nlines, ncols, begin_y, begin_x;
472 int lines, cols; /* copies of LINES and COLS */
473 int focussed; /* Only set on one parent or child view at a time. */
474 int dying;
475 struct tog_view *parent;
476 struct tog_view *child;
478 /*
479 * This flag is initially set on parent views when a new child view
480 * is created. It gets toggled when the 'Tab' key switches focus
481 * between parent and child.
482 * The flag indicates whether focus should be passed on to our child
483 * view if this parent view gets picked for focus after another parent
484 * view was closed. This prevents child views from losing focus in such
485 * situations.
486 */
487 int focus_child;
489 /* type-specific state */
490 enum tog_view_type type;
491 union {
492 struct tog_diff_view_state diff;
493 struct tog_log_view_state log;
494 struct tog_blame_view_state blame;
495 struct tog_tree_view_state tree;
496 struct tog_ref_view_state ref;
497 } state;
499 const struct got_error *(*show)(struct tog_view *);
500 const struct got_error *(*input)(struct tog_view **,
501 struct tog_view *, int);
502 const struct got_error *(*close)(struct tog_view *);
504 const struct got_error *(*search_start)(struct tog_view *);
505 const struct got_error *(*search_next)(struct tog_view *);
506 int 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->nlines < 1)
790 return NULL;
792 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
793 wclrtoeol(view->window);
795 nocbreak();
796 echo();
797 ret = wgetnstr(view->window, pattern, sizeof(pattern));
798 cbreak();
799 noecho();
800 if (ret == ERR)
801 return NULL;
803 if (view->searching) {
804 regfree(&view->regex);
805 view->searching = 0;
808 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
809 err = view->search_start(view);
810 if (err) {
811 regfree(&view->regex);
812 return err;
814 view->searching = TOG_SEARCH_FORWARD;
815 view->search_next_done = 0;
816 view->search_next(view);
819 return NULL;
822 static const struct got_error *
823 view_input(struct tog_view **new, int *done, struct tog_view *view,
824 struct tog_view_list_head *views)
826 const struct got_error *err = NULL;
827 struct tog_view *v;
828 int ch, errcode;
830 *new = NULL;
832 /* Clear "no matches" indicator. */
833 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
834 view->search_next_done == TOG_SEARCH_HAVE_NONE)
835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
837 if (view->searching && !view->search_next_done) {
838 errcode = pthread_mutex_unlock(&tog_mutex);
839 if (errcode)
840 return got_error_set_errno(errcode,
841 "pthread_mutex_unlock");
842 pthread_yield();
843 errcode = pthread_mutex_lock(&tog_mutex);
844 if (errcode)
845 return got_error_set_errno(errcode,
846 "pthread_mutex_lock");
847 view->search_next(view);
848 return NULL;
851 nodelay(stdscr, FALSE);
852 /* Allow threads to make progress while we are waiting for input. */
853 errcode = pthread_mutex_unlock(&tog_mutex);
854 if (errcode)
855 return got_error_set_errno(errcode, "pthread_mutex_unlock");
856 ch = wgetch(view->window);
857 errcode = pthread_mutex_lock(&tog_mutex);
858 if (errcode)
859 return got_error_set_errno(errcode, "pthread_mutex_lock");
860 nodelay(stdscr, TRUE);
862 if (tog_sigwinch_received || tog_sigcont_received) {
863 tog_resizeterm();
864 tog_sigwinch_received = 0;
865 tog_sigcont_received = 0;
866 TAILQ_FOREACH(v, views, entry) {
867 err = view_resize(v);
868 if (err)
869 return err;
870 err = v->input(new, v, KEY_RESIZE);
871 if (err)
872 return err;
873 if (v->child) {
874 err = view_resize(v->child);
875 if (err)
876 return err;
877 err = v->child->input(new, v->child,
878 KEY_RESIZE);
879 if (err)
880 return err;
885 switch (ch) {
886 case ERR:
887 break;
888 case '\t':
889 if (view->child) {
890 view->focussed = 0;
891 view->child->focussed = 1;
892 view->focus_child = 1;
893 } else if (view->parent) {
894 view->focussed = 0;
895 view->parent->focussed = 1;
896 view->parent->focus_child = 0;
898 break;
899 case 'q':
900 err = view->input(new, view, ch);
901 view->dying = 1;
902 break;
903 case 'Q':
904 *done = 1;
905 break;
906 case 'f':
907 if (view_is_parent_view(view)) {
908 if (view->child == NULL)
909 break;
910 if (view_is_splitscreen(view->child)) {
911 view->focussed = 0;
912 view->child->focussed = 1;
913 err = view_fullscreen(view->child);
914 } else
915 err = view_splitscreen(view->child);
916 if (err)
917 break;
918 err = view->child->input(new, view->child,
919 KEY_RESIZE);
920 } else {
921 if (view_is_splitscreen(view)) {
922 view->parent->focussed = 0;
923 view->focussed = 1;
924 err = view_fullscreen(view);
925 } else {
926 err = view_splitscreen(view);
928 if (err)
929 break;
930 err = view->input(new, view, KEY_RESIZE);
932 break;
933 case KEY_RESIZE:
934 break;
935 case '/':
936 if (view->search_start)
937 view_search_start(view);
938 else
939 err = view->input(new, view, ch);
940 break;
941 case 'N':
942 case 'n':
943 if (view->search_next) {
944 view->searching = (ch == 'n' ?
945 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
946 view->search_next_done = 0;
947 view->search_next(view);
948 } else
949 err = view->input(new, view, ch);
950 break;
951 default:
952 err = view->input(new, view, ch);
953 break;
956 return err;
959 void
960 view_vborder(struct tog_view *view)
962 PANEL *panel;
963 const struct tog_view *view_above;
965 if (view->parent)
966 return view_vborder(view->parent);
968 panel = panel_above(view->panel);
969 if (panel == NULL)
970 return;
972 view_above = panel_userptr(panel);
973 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
974 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
977 int
978 view_needs_focus_indication(struct tog_view *view)
980 if (view_is_parent_view(view)) {
981 if (view->child == NULL || view->child->focussed)
982 return 0;
983 if (!view_is_splitscreen(view->child))
984 return 0;
985 } else if (!view_is_splitscreen(view))
986 return 0;
988 return view->focussed;
991 static const struct got_error *
992 view_loop(struct tog_view *view)
994 const struct got_error *err = NULL;
995 struct tog_view_list_head views;
996 struct tog_view *new_view;
997 int fast_refresh = 10;
998 int done = 0, errcode;
1000 errcode = pthread_mutex_lock(&tog_mutex);
1001 if (errcode)
1002 return got_error_set_errno(errcode, "pthread_mutex_lock");
1004 TAILQ_INIT(&views);
1005 TAILQ_INSERT_HEAD(&views, view, entry);
1007 view->focussed = 1;
1008 err = view->show(view);
1009 if (err)
1010 return err;
1011 update_panels();
1012 doupdate();
1013 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1014 /* Refresh fast during initialization, then become slower. */
1015 if (fast_refresh && fast_refresh-- == 0)
1016 halfdelay(10); /* switch to once per second */
1018 err = view_input(&new_view, &done, view, &views);
1019 if (err)
1020 break;
1021 if (view->dying) {
1022 struct tog_view *v, *prev = NULL;
1024 if (view_is_parent_view(view))
1025 prev = TAILQ_PREV(view, tog_view_list_head,
1026 entry);
1027 else if (view->parent)
1028 prev = view->parent;
1030 if (view->parent) {
1031 view->parent->child = NULL;
1032 view->parent->focus_child = 0;
1033 } else
1034 TAILQ_REMOVE(&views, view, entry);
1036 err = view_close(view);
1037 if (err)
1038 goto done;
1040 view = NULL;
1041 TAILQ_FOREACH(v, &views, entry) {
1042 if (v->focussed)
1043 break;
1045 if (view == NULL && new_view == NULL) {
1046 /* No view has focus. Try to pick one. */
1047 if (prev)
1048 view = prev;
1049 else if (!TAILQ_EMPTY(&views)) {
1050 view = TAILQ_LAST(&views,
1051 tog_view_list_head);
1053 if (view) {
1054 if (view->focus_child) {
1055 view->child->focussed = 1;
1056 view = view->child;
1057 } else
1058 view->focussed = 1;
1062 if (new_view) {
1063 struct tog_view *v, *t;
1064 /* Only allow one parent view per type. */
1065 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1066 if (v->type != new_view->type)
1067 continue;
1068 TAILQ_REMOVE(&views, v, entry);
1069 err = view_close(v);
1070 if (err)
1071 goto done;
1072 break;
1074 TAILQ_INSERT_TAIL(&views, new_view, entry);
1075 view = new_view;
1077 if (view) {
1078 if (view_is_parent_view(view)) {
1079 if (view->child && view->child->focussed)
1080 view = view->child;
1081 } else {
1082 if (view->parent && view->parent->focussed)
1083 view = view->parent;
1085 show_panel(view->panel);
1086 if (view->child && view_is_splitscreen(view->child))
1087 show_panel(view->child->panel);
1088 if (view->parent && view_is_splitscreen(view)) {
1089 err = view->parent->show(view->parent);
1090 if (err)
1091 goto done;
1093 err = view->show(view);
1094 if (err)
1095 goto done;
1096 if (view->child) {
1097 err = view->child->show(view->child);
1098 if (err)
1099 goto done;
1101 update_panels();
1102 doupdate();
1105 done:
1106 while (!TAILQ_EMPTY(&views)) {
1107 view = TAILQ_FIRST(&views);
1108 TAILQ_REMOVE(&views, view, entry);
1109 view_close(view);
1112 errcode = pthread_mutex_unlock(&tog_mutex);
1113 if (errcode && err == NULL)
1114 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1116 return err;
1119 __dead static void
1120 usage_log(void)
1122 endwin();
1123 fprintf(stderr,
1124 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1125 getprogname());
1126 exit(1);
1129 /* Create newly allocated wide-character string equivalent to a byte string. */
1130 static const struct got_error *
1131 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1133 char *vis = NULL;
1134 const struct got_error *err = NULL;
1136 *ws = NULL;
1137 *wlen = mbstowcs(NULL, s, 0);
1138 if (*wlen == (size_t)-1) {
1139 int vislen;
1140 if (errno != EILSEQ)
1141 return got_error_from_errno("mbstowcs");
1143 /* byte string invalid in current encoding; try to "fix" it */
1144 err = got_mbsavis(&vis, &vislen, s);
1145 if (err)
1146 return err;
1147 *wlen = mbstowcs(NULL, vis, 0);
1148 if (*wlen == (size_t)-1) {
1149 err = got_error_from_errno("mbstowcs"); /* give up */
1150 goto done;
1154 *ws = calloc(*wlen + 1, sizeof(**ws));
1155 if (*ws == NULL) {
1156 err = got_error_from_errno("calloc");
1157 goto done;
1160 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1161 err = got_error_from_errno("mbstowcs");
1162 done:
1163 free(vis);
1164 if (err) {
1165 free(*ws);
1166 *ws = NULL;
1167 *wlen = 0;
1169 return err;
1172 /* Format a line for display, ensuring that it won't overflow a width limit. */
1173 static const struct got_error *
1174 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1175 int col_tab_align)
1177 const struct got_error *err = NULL;
1178 int cols = 0;
1179 wchar_t *wline = NULL;
1180 size_t wlen;
1181 int i;
1183 *wlinep = NULL;
1184 *widthp = 0;
1186 err = mbs2ws(&wline, &wlen, line);
1187 if (err)
1188 return err;
1190 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1191 wline[wlen - 1] = L'\0';
1192 wlen--;
1194 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1195 wline[wlen - 1] = L'\0';
1196 wlen--;
1199 i = 0;
1200 while (i < wlen) {
1201 int width = wcwidth(wline[i]);
1203 if (width == 0) {
1204 i++;
1205 continue;
1208 if (width == 1 || width == 2) {
1209 if (cols + width > wlimit)
1210 break;
1211 cols += width;
1212 i++;
1213 } else if (width == -1) {
1214 if (wline[i] == L'\t') {
1215 width = TABSIZE -
1216 ((cols + col_tab_align) % TABSIZE);
1217 } else {
1218 width = 1;
1219 wline[i] = L'.';
1221 if (cols + width > wlimit)
1222 break;
1223 cols += width;
1224 i++;
1225 } else {
1226 err = got_error_from_errno("wcwidth");
1227 goto done;
1230 wline[i] = L'\0';
1231 if (widthp)
1232 *widthp = cols;
1233 done:
1234 if (err)
1235 free(wline);
1236 else
1237 *wlinep = wline;
1238 return err;
1241 static const struct got_error*
1242 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1243 struct got_object_id *id, struct got_repository *repo)
1245 static const struct got_error *err = NULL;
1246 struct got_reflist_entry *re;
1247 char *s;
1248 const char *name;
1250 *refs_str = NULL;
1252 SIMPLEQ_FOREACH(re, refs, entry) {
1253 struct got_tag_object *tag = NULL;
1254 struct got_object_id *ref_id;
1255 int cmp;
1257 name = got_ref_get_name(re->ref);
1258 if (strcmp(name, GOT_REF_HEAD) == 0)
1259 continue;
1260 if (strncmp(name, "refs/", 5) == 0)
1261 name += 5;
1262 if (strncmp(name, "got/", 4) == 0)
1263 continue;
1264 if (strncmp(name, "heads/", 6) == 0)
1265 name += 6;
1266 if (strncmp(name, "remotes/", 8) == 0) {
1267 name += 8;
1268 s = strstr(name, "/" GOT_REF_HEAD);
1269 if (s != NULL && s[strlen(s)] == '\0')
1270 continue;
1272 err = got_ref_resolve(&ref_id, repo, re->ref);
1273 if (err)
1274 break;
1275 if (strncmp(name, "tags/", 5) == 0) {
1276 err = got_object_open_as_tag(&tag, repo, ref_id);
1277 if (err) {
1278 if (err->code != GOT_ERR_OBJ_TYPE) {
1279 free(ref_id);
1280 break;
1282 /* Ref points at something other than a tag. */
1283 err = NULL;
1284 tag = NULL;
1287 cmp = got_object_id_cmp(tag ?
1288 got_object_tag_get_object_id(tag) : ref_id, id);
1289 free(ref_id);
1290 if (tag)
1291 got_object_tag_close(tag);
1292 if (cmp != 0)
1293 continue;
1294 s = *refs_str;
1295 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1296 s ? ", " : "", name) == -1) {
1297 err = got_error_from_errno("asprintf");
1298 free(s);
1299 *refs_str = NULL;
1300 break;
1302 free(s);
1305 return err;
1308 static const struct got_error *
1309 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1310 int col_tab_align)
1312 char *smallerthan, *at;
1314 smallerthan = strchr(author, '<');
1315 if (smallerthan && smallerthan[1] != '\0')
1316 author = smallerthan + 1;
1317 at = strchr(author, '@');
1318 if (at)
1319 *at = '\0';
1320 return format_line(wauthor, author_width, author, limit, col_tab_align);
1323 static const struct got_error *
1324 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1325 struct got_object_id *id, const size_t date_display_cols,
1326 int author_display_cols)
1328 struct tog_log_view_state *s = &view->state.log;
1329 const struct got_error *err = NULL;
1330 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1331 char *logmsg0 = NULL, *logmsg = NULL;
1332 char *author = NULL;
1333 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1334 int author_width, logmsg_width;
1335 char *newline, *line = NULL;
1336 int col, limit;
1337 const int avail = view->ncols;
1338 struct tm tm;
1339 time_t committer_time;
1340 struct tog_color *tc;
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 if (localtime_r(&committer_time, &tm) == NULL)
1344 return got_error_from_errno("localtime_r");
1345 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1346 >= sizeof(datebuf))
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 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, 1, 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 error = tog_load_refs(repo);
2761 if (error)
2762 goto done;
2764 if (start_commit == NULL) {
2765 error = got_repo_match_object_id(&start_id, &label,
2766 worktree ? got_worktree_get_head_ref_name(worktree) :
2767 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2768 if (error)
2769 goto done;
2770 head_ref_name = label;
2771 } else {
2772 error = got_ref_open(&ref, repo, start_commit, 0);
2773 if (error == NULL)
2774 head_ref_name = got_ref_get_name(ref);
2775 else if (error->code != GOT_ERR_NOT_REF)
2776 goto done;
2777 error = got_repo_match_object_id(&start_id, NULL,
2778 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2779 if (error)
2780 goto done;
2783 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2784 if (view == NULL) {
2785 error = got_error_from_errno("view_open");
2786 goto done;
2788 error = open_log_view(view, start_id, repo, head_ref_name,
2789 in_repo_path, log_branches);
2790 if (error)
2791 goto done;
2792 if (worktree) {
2793 /* Release work tree lock. */
2794 got_worktree_close(worktree);
2795 worktree = NULL;
2797 error = view_loop(view);
2798 done:
2799 free(in_repo_path);
2800 free(repo_path);
2801 free(cwd);
2802 free(start_id);
2803 free(label);
2804 if (ref)
2805 got_ref_close(ref);
2806 if (repo)
2807 got_repo_close(repo);
2808 if (worktree)
2809 got_worktree_close(worktree);
2810 tog_free_refs();
2811 return error;
2814 __dead static void
2815 usage_diff(void)
2817 endwin();
2818 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2819 "[-w] object1 object2\n", getprogname());
2820 exit(1);
2823 static int
2824 match_line(const char *line, regex_t *regex, size_t nmatch,
2825 regmatch_t *regmatch)
2827 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2830 struct tog_color *
2831 match_color(struct tog_colors *colors, const char *line)
2833 struct tog_color *tc = NULL;
2835 SIMPLEQ_FOREACH(tc, colors, entry) {
2836 if (match_line(line, &tc->regex, 0, NULL))
2837 return tc;
2840 return NULL;
2843 static const struct got_error *
2844 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2845 WINDOW *window, regmatch_t *regmatch)
2847 const struct got_error *err = NULL;
2848 wchar_t *wline;
2849 int width;
2850 char *s;
2852 *wtotal = 0;
2854 s = strndup(line, regmatch->rm_so);
2855 if (s == NULL)
2856 return got_error_from_errno("strndup");
2858 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2859 if (err) {
2860 free(s);
2861 return err;
2863 waddwstr(window, wline);
2864 free(wline);
2865 free(s);
2866 wlimit -= width;
2867 *wtotal += width;
2869 if (wlimit > 0) {
2870 s = strndup(line + regmatch->rm_so,
2871 regmatch->rm_eo - regmatch->rm_so);
2872 if (s == NULL) {
2873 err = got_error_from_errno("strndup");
2874 free(s);
2875 return err;
2877 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2878 if (err) {
2879 free(s);
2880 return err;
2882 wattr_on(window, A_STANDOUT, NULL);
2883 waddwstr(window, wline);
2884 wattr_off(window, A_STANDOUT, NULL);
2885 free(wline);
2886 free(s);
2887 wlimit -= width;
2888 *wtotal += width;
2891 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2892 err = format_line(&wline, &width,
2893 line + regmatch->rm_eo, wlimit, col_tab_align);
2894 if (err)
2895 return err;
2896 waddwstr(window, wline);
2897 free(wline);
2898 *wtotal += width;
2901 return NULL;
2904 static const struct got_error *
2905 draw_file(struct tog_view *view, const char *header)
2907 struct tog_diff_view_state *s = &view->state.diff;
2908 regmatch_t *regmatch = &view->regmatch;
2909 const struct got_error *err;
2910 int nprinted = 0;
2911 char *line;
2912 size_t linesize = 0;
2913 ssize_t linelen;
2914 struct tog_color *tc;
2915 wchar_t *wline;
2916 int width;
2917 int max_lines = view->nlines;
2918 int nlines = s->nlines;
2919 off_t line_offset;
2921 line_offset = s->line_offsets[s->first_displayed_line - 1];
2922 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2923 return got_error_from_errno("fseek");
2925 werase(view->window);
2927 if (header) {
2928 if (asprintf(&line, "[%d/%d] %s",
2929 s->first_displayed_line - 1 + s->selected_line, nlines,
2930 header) == -1)
2931 return got_error_from_errno("asprintf");
2932 err = format_line(&wline, &width, line, view->ncols, 0);
2933 free(line);
2934 if (err)
2935 return err;
2937 if (view_needs_focus_indication(view))
2938 wstandout(view->window);
2939 waddwstr(view->window, wline);
2940 free(wline);
2941 wline = NULL;
2942 if (view_needs_focus_indication(view))
2943 wstandend(view->window);
2944 if (width <= view->ncols - 1)
2945 waddch(view->window, '\n');
2947 if (max_lines <= 1)
2948 return NULL;
2949 max_lines--;
2952 s->eof = 0;
2953 line = NULL;
2954 while (max_lines > 0 && nprinted < max_lines) {
2955 linelen = getline(&line, &linesize, s->f);
2956 if (linelen == -1) {
2957 if (feof(s->f)) {
2958 s->eof = 1;
2959 break;
2961 free(line);
2962 return got_ferror(s->f, GOT_ERR_IO);
2965 tc = match_color(&s->colors, line);
2966 if (tc)
2967 wattr_on(view->window,
2968 COLOR_PAIR(tc->colorpair), NULL);
2969 if (s->first_displayed_line + nprinted == s->matched_line &&
2970 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2971 err = add_matched_line(&width, line, view->ncols, 0,
2972 view->window, regmatch);
2973 if (err) {
2974 free(line);
2975 return err;
2977 } else {
2978 err = format_line(&wline, &width, line, view->ncols, 0);
2979 if (err) {
2980 free(line);
2981 return err;
2983 waddwstr(view->window, wline);
2984 free(wline);
2985 wline = NULL;
2987 if (tc)
2988 wattr_off(view->window,
2989 COLOR_PAIR(tc->colorpair), NULL);
2990 if (width <= view->ncols - 1)
2991 waddch(view->window, '\n');
2992 nprinted++;
2994 free(line);
2995 if (nprinted >= 1)
2996 s->last_displayed_line = s->first_displayed_line +
2997 (nprinted - 1);
2998 else
2999 s->last_displayed_line = s->first_displayed_line;
3001 view_vborder(view);
3003 if (s->eof) {
3004 while (nprinted < view->nlines) {
3005 waddch(view->window, '\n');
3006 nprinted++;
3009 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3010 if (err) {
3011 return err;
3014 wstandout(view->window);
3015 waddwstr(view->window, wline);
3016 free(wline);
3017 wline = NULL;
3018 wstandend(view->window);
3021 return NULL;
3024 static char *
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3028 char *p, *s;
3030 tm = gmtime_r(time, &mytm);
3031 if (tm == NULL)
3032 return NULL;
3033 s = asctime_r(tm, datebuf);
3034 if (s == NULL)
3035 return NULL;
3036 p = strchr(s, '\n');
3037 if (p)
3038 *p = '\0';
3039 return s;
3042 static const struct got_error *
3043 get_changed_paths(struct got_pathlist_head *paths,
3044 struct got_commit_object *commit, struct got_repository *repo)
3046 const struct got_error *err = NULL;
3047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3049 struct got_object_qid *qid;
3051 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3052 if (qid != NULL) {
3053 struct got_commit_object *pcommit;
3054 err = got_object_open_as_commit(&pcommit, repo,
3055 qid->id);
3056 if (err)
3057 return err;
3059 tree_id1 = got_object_commit_get_tree_id(pcommit);
3060 got_object_commit_close(pcommit);
3064 if (tree_id1) {
3065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3066 if (err)
3067 goto done;
3070 tree_id2 = got_object_commit_get_tree_id(commit);
3071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3072 if (err)
3073 goto done;
3075 err = got_diff_tree(tree1, tree2, "", "", repo,
3076 got_diff_tree_collect_changed_paths, paths, 0);
3077 done:
3078 if (tree1)
3079 got_object_tree_close(tree1);
3080 if (tree2)
3081 got_object_tree_close(tree2);
3082 return err;
3085 static const struct got_error *
3086 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3088 off_t *p;
3090 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3091 if (p == NULL)
3092 return got_error_from_errno("reallocarray");
3093 *line_offsets = p;
3094 (*line_offsets)[*nlines] = off;
3095 (*nlines)++;
3096 return NULL;
3099 static const struct got_error *
3100 write_commit_info(off_t **line_offsets, size_t *nlines,
3101 struct got_object_id *commit_id, struct got_reflist_head *refs,
3102 struct got_repository *repo, FILE *outfile)
3104 const struct got_error *err = NULL;
3105 char datebuf[26], *datestr;
3106 struct got_commit_object *commit;
3107 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3108 time_t committer_time;
3109 const char *author, *committer;
3110 char *refs_str = NULL;
3111 struct got_pathlist_head changed_paths;
3112 struct got_pathlist_entry *pe;
3113 off_t outoff = 0;
3114 int n;
3116 TAILQ_INIT(&changed_paths);
3118 if (refs) {
3119 err = build_refs_str(&refs_str, refs, commit_id, repo);
3120 if (err)
3121 return err;
3124 err = got_object_open_as_commit(&commit, repo, commit_id);
3125 if (err)
3126 return err;
3128 err = got_object_id_str(&id_str, commit_id);
3129 if (err) {
3130 err = got_error_from_errno("got_object_id_str");
3131 goto done;
3134 err = add_line_offset(line_offsets, nlines, 0);
3135 if (err)
3136 goto done;
3138 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3139 refs_str ? refs_str : "", refs_str ? ")" : "");
3140 if (n < 0) {
3141 err = got_error_from_errno("fprintf");
3142 goto done;
3144 outoff += n;
3145 err = add_line_offset(line_offsets, nlines, outoff);
3146 if (err)
3147 goto done;
3149 n = fprintf(outfile, "from: %s\n",
3150 got_object_commit_get_author(commit));
3151 if (n < 0) {
3152 err = got_error_from_errno("fprintf");
3153 goto done;
3155 outoff += n;
3156 err = add_line_offset(line_offsets, nlines, outoff);
3157 if (err)
3158 goto done;
3160 committer_time = got_object_commit_get_committer_time(commit);
3161 datestr = get_datestr(&committer_time, datebuf);
3162 if (datestr) {
3163 n = fprintf(outfile, "date: %s UTC\n", datestr);
3164 if (n < 0) {
3165 err = got_error_from_errno("fprintf");
3166 goto done;
3168 outoff += n;
3169 err = add_line_offset(line_offsets, nlines, outoff);
3170 if (err)
3171 goto done;
3173 author = got_object_commit_get_author(commit);
3174 committer = got_object_commit_get_committer(commit);
3175 if (strcmp(author, committer) != 0) {
3176 n = fprintf(outfile, "via: %s\n", committer);
3177 if (n < 0) {
3178 err = got_error_from_errno("fprintf");
3179 goto done;
3181 outoff += n;
3182 err = add_line_offset(line_offsets, nlines, outoff);
3183 if (err)
3184 goto done;
3186 err = got_object_commit_get_logmsg(&logmsg, commit);
3187 if (err)
3188 goto done;
3189 s = logmsg;
3190 while ((line = strsep(&s, "\n")) != NULL) {
3191 n = fprintf(outfile, "%s\n", line);
3192 if (n < 0) {
3193 err = got_error_from_errno("fprintf");
3194 goto done;
3196 outoff += n;
3197 err = add_line_offset(line_offsets, nlines, outoff);
3198 if (err)
3199 goto done;
3202 err = get_changed_paths(&changed_paths, commit, repo);
3203 if (err)
3204 goto done;
3205 TAILQ_FOREACH(pe, &changed_paths, entry) {
3206 struct got_diff_changed_path *cp = pe->data;
3207 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3208 if (n < 0) {
3209 err = got_error_from_errno("fprintf");
3210 goto done;
3212 outoff += n;
3213 err = add_line_offset(line_offsets, nlines, outoff);
3214 if (err)
3215 goto done;
3216 free((char *)pe->path);
3217 free(pe->data);
3220 fputc('\n', outfile);
3221 outoff++;
3222 err = add_line_offset(line_offsets, nlines, outoff);
3223 done:
3224 got_pathlist_free(&changed_paths);
3225 free(id_str);
3226 free(logmsg);
3227 free(refs_str);
3228 got_object_commit_close(commit);
3229 if (err) {
3230 free(*line_offsets);
3231 *line_offsets = NULL;
3232 *nlines = 0;
3234 return err;
3237 static const struct got_error *
3238 create_diff(struct tog_diff_view_state *s)
3240 const struct got_error *err = NULL;
3241 FILE *f = NULL;
3242 int obj_type;
3244 free(s->line_offsets);
3245 s->line_offsets = malloc(sizeof(off_t));
3246 if (s->line_offsets == NULL)
3247 return got_error_from_errno("malloc");
3248 s->nlines = 0;
3250 f = got_opentemp();
3251 if (f == NULL) {
3252 err = got_error_from_errno("got_opentemp");
3253 goto done;
3255 if (s->f && fclose(s->f) != 0) {
3256 err = got_error_from_errno("fclose");
3257 goto done;
3259 s->f = f;
3261 if (s->id1)
3262 err = got_object_get_type(&obj_type, s->repo, s->id1);
3263 else
3264 err = got_object_get_type(&obj_type, s->repo, s->id2);
3265 if (err)
3266 goto done;
3268 switch (obj_type) {
3269 case GOT_OBJ_TYPE_BLOB:
3270 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3271 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3272 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3273 break;
3274 case GOT_OBJ_TYPE_TREE:
3275 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3276 s->id1, s->id2, "", "", s->diff_context,
3277 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3278 break;
3279 case GOT_OBJ_TYPE_COMMIT: {
3280 const struct got_object_id_queue *parent_ids;
3281 struct got_object_qid *pid;
3282 struct got_commit_object *commit2;
3283 struct got_reflist_head *refs;
3285 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3286 if (err)
3287 goto done;
3288 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3289 /* Show commit info if we're diffing to a parent/root commit. */
3290 if (s->id1 == NULL) {
3291 err = write_commit_info(&s->line_offsets, &s->nlines,
3292 s->id2, refs, s->repo, s->f);
3293 if (err)
3294 goto done;
3295 } else {
3296 parent_ids = got_object_commit_get_parent_ids(commit2);
3297 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3298 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3299 err = write_commit_info(
3300 &s->line_offsets, &s->nlines,
3301 s->id2, refs, s->repo, s->f);
3302 if (err)
3303 goto done;
3304 break;
3308 got_object_commit_close(commit2);
3310 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3311 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3312 s->force_text_diff, s->repo, s->f);
3313 break;
3315 default:
3316 err = got_error(GOT_ERR_OBJ_TYPE);
3317 break;
3319 if (err)
3320 goto done;
3321 done:
3322 if (s->f && fflush(s->f) != 0 && err == NULL)
3323 err = got_error_from_errno("fflush");
3324 return err;
3327 static void
3328 diff_view_indicate_progress(struct tog_view *view)
3330 mvwaddstr(view->window, 0, 0, "diffing...");
3331 update_panels();
3332 doupdate();
3335 static const struct got_error *
3336 search_start_diff_view(struct tog_view *view)
3338 struct tog_diff_view_state *s = &view->state.diff;
3340 s->matched_line = 0;
3341 return NULL;
3344 static const struct got_error *
3345 search_next_diff_view(struct tog_view *view)
3347 struct tog_diff_view_state *s = &view->state.diff;
3348 int lineno;
3349 char *line = NULL;
3350 size_t linesize = 0;
3351 ssize_t linelen;
3353 if (!view->searching) {
3354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3355 return NULL;
3358 if (s->matched_line) {
3359 if (view->searching == TOG_SEARCH_FORWARD)
3360 lineno = s->matched_line + 1;
3361 else
3362 lineno = s->matched_line - 1;
3363 } else {
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 lineno = 1;
3366 else
3367 lineno = s->nlines;
3370 while (1) {
3371 off_t offset;
3373 if (lineno <= 0 || lineno > s->nlines) {
3374 if (s->matched_line == 0) {
3375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3376 break;
3379 if (view->searching == TOG_SEARCH_FORWARD)
3380 lineno = 1;
3381 else
3382 lineno = s->nlines;
3385 offset = s->line_offsets[lineno - 1];
3386 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3387 free(line);
3388 return got_error_from_errno("fseeko");
3390 linelen = getline(&line, &linesize, s->f);
3391 if (linelen != -1 &&
3392 match_line(line, &view->regex, 1, &view->regmatch)) {
3393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3394 s->matched_line = lineno;
3395 break;
3397 if (view->searching == TOG_SEARCH_FORWARD)
3398 lineno++;
3399 else
3400 lineno--;
3402 free(line);
3404 if (s->matched_line) {
3405 s->first_displayed_line = s->matched_line;
3406 s->selected_line = 1;
3409 return NULL;
3412 static const struct got_error *
3413 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3414 struct got_object_id *id2, const char *label1, const char *label2,
3415 int diff_context, int ignore_whitespace, int force_text_diff,
3416 struct tog_view *log_view, struct got_repository *repo)
3418 const struct got_error *err;
3419 struct tog_diff_view_state *s = &view->state.diff;
3421 if (id1 != NULL && id2 != NULL) {
3422 int type1, type2;
3423 err = got_object_get_type(&type1, repo, id1);
3424 if (err)
3425 return err;
3426 err = got_object_get_type(&type2, repo, id2);
3427 if (err)
3428 return err;
3430 if (type1 != type2)
3431 return got_error(GOT_ERR_OBJ_TYPE);
3433 s->first_displayed_line = 1;
3434 s->last_displayed_line = view->nlines;
3435 s->selected_line = 1;
3436 s->repo = repo;
3437 s->id1 = id1;
3438 s->id2 = id2;
3439 s->label1 = label1;
3440 s->label2 = label2;
3442 if (id1) {
3443 s->id1 = got_object_id_dup(id1);
3444 if (s->id1 == NULL)
3445 return got_error_from_errno("got_object_id_dup");
3446 } else
3447 s->id1 = NULL;
3449 s->id2 = got_object_id_dup(id2);
3450 if (s->id2 == NULL) {
3451 free(s->id1);
3452 s->id1 = NULL;
3453 return got_error_from_errno("got_object_id_dup");
3455 s->f = NULL;
3456 s->first_displayed_line = 1;
3457 s->last_displayed_line = view->nlines;
3458 s->diff_context = diff_context;
3459 s->ignore_whitespace = ignore_whitespace;
3460 s->force_text_diff = force_text_diff;
3461 s->log_view = log_view;
3462 s->repo = repo;
3464 SIMPLEQ_INIT(&s->colors);
3465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3466 err = add_color(&s->colors,
3467 "^-", TOG_COLOR_DIFF_MINUS,
3468 get_color_value("TOG_COLOR_DIFF_MINUS"));
3469 if (err)
3470 return err;
3471 err = add_color(&s->colors, "^\\+",
3472 TOG_COLOR_DIFF_PLUS,
3473 get_color_value("TOG_COLOR_DIFF_PLUS"));
3474 if (err) {
3475 free_colors(&s->colors);
3476 return err;
3478 err = add_color(&s->colors,
3479 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3480 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3481 if (err) {
3482 free_colors(&s->colors);
3483 return err;
3486 err = add_color(&s->colors,
3487 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3488 TOG_COLOR_DIFF_META,
3489 get_color_value("TOG_COLOR_DIFF_META"));
3490 if (err) {
3491 free_colors(&s->colors);
3492 return err;
3495 err = add_color(&s->colors,
3496 "^(from|via): ", TOG_COLOR_AUTHOR,
3497 get_color_value("TOG_COLOR_AUTHOR"));
3498 if (err) {
3499 free_colors(&s->colors);
3500 return err;
3503 err = add_color(&s->colors,
3504 "^date: ", TOG_COLOR_DATE,
3505 get_color_value("TOG_COLOR_DATE"));
3506 if (err) {
3507 free_colors(&s->colors);
3508 return err;
3512 if (log_view && view_is_splitscreen(view))
3513 show_log_view(log_view); /* draw vborder */
3514 diff_view_indicate_progress(view);
3516 s->line_offsets = NULL;
3517 s->nlines = 0;
3518 err = create_diff(s);
3519 if (err) {
3520 free(s->id1);
3521 s->id1 = NULL;
3522 free(s->id2);
3523 s->id2 = NULL;
3524 free_colors(&s->colors);
3525 return err;
3528 view->show = show_diff_view;
3529 view->input = input_diff_view;
3530 view->close = close_diff_view;
3531 view->search_start = search_start_diff_view;
3532 view->search_next = search_next_diff_view;
3534 return NULL;
3537 static const struct got_error *
3538 close_diff_view(struct tog_view *view)
3540 const struct got_error *err = NULL;
3541 struct tog_diff_view_state *s = &view->state.diff;
3543 free(s->id1);
3544 s->id1 = NULL;
3545 free(s->id2);
3546 s->id2 = NULL;
3547 if (s->f && fclose(s->f) == EOF)
3548 err = got_error_from_errno("fclose");
3549 free_colors(&s->colors);
3550 free(s->line_offsets);
3551 s->line_offsets = NULL;
3552 s->nlines = 0;
3553 return err;
3556 static const struct got_error *
3557 show_diff_view(struct tog_view *view)
3559 const struct got_error *err;
3560 struct tog_diff_view_state *s = &view->state.diff;
3561 char *id_str1 = NULL, *id_str2, *header;
3562 const char *label1, *label2;
3564 if (s->id1) {
3565 err = got_object_id_str(&id_str1, s->id1);
3566 if (err)
3567 return err;
3568 label1 = s->label1 ? : id_str1;
3569 } else
3570 label1 = "/dev/null";
3572 err = got_object_id_str(&id_str2, s->id2);
3573 if (err)
3574 return err;
3575 label2 = s->label2 ? : id_str2;
3577 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3578 err = got_error_from_errno("asprintf");
3579 free(id_str1);
3580 free(id_str2);
3581 return err;
3583 free(id_str1);
3584 free(id_str2);
3586 return draw_file(view, header);
3589 static const struct got_error *
3590 set_selected_commit(struct tog_diff_view_state *s,
3591 struct commit_queue_entry *entry)
3593 const struct got_error *err;
3594 const struct got_object_id_queue *parent_ids;
3595 struct got_commit_object *selected_commit;
3596 struct got_object_qid *pid;
3598 free(s->id2);
3599 s->id2 = got_object_id_dup(entry->id);
3600 if (s->id2 == NULL)
3601 return got_error_from_errno("got_object_id_dup");
3603 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3604 if (err)
3605 return err;
3606 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3607 free(s->id1);
3608 pid = SIMPLEQ_FIRST(parent_ids);
3609 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3610 got_object_commit_close(selected_commit);
3611 return NULL;
3614 static const struct got_error *
3615 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3617 const struct got_error *err = NULL;
3618 struct tog_diff_view_state *s = &view->state.diff;
3619 struct tog_log_view_state *ls;
3620 struct commit_queue_entry *old_selected_entry;
3621 char *line = NULL;
3622 size_t linesize = 0;
3623 ssize_t linelen;
3624 int i;
3626 switch (ch) {
3627 case 'a':
3628 case 'w':
3629 if (ch == 'a')
3630 s->force_text_diff = !s->force_text_diff;
3631 if (ch == 'w')
3632 s->ignore_whitespace = !s->ignore_whitespace;
3633 wclear(view->window);
3634 s->first_displayed_line = 1;
3635 s->last_displayed_line = view->nlines;
3636 diff_view_indicate_progress(view);
3637 err = create_diff(s);
3638 break;
3639 case 'k':
3640 case KEY_UP:
3641 if (s->first_displayed_line > 1)
3642 s->first_displayed_line--;
3643 break;
3644 case KEY_PPAGE:
3645 case CTRL('b'):
3646 if (s->first_displayed_line == 1)
3647 break;
3648 i = 0;
3649 while (i++ < view->nlines - 1 &&
3650 s->first_displayed_line > 1)
3651 s->first_displayed_line--;
3652 break;
3653 case 'j':
3654 case KEY_DOWN:
3655 if (!s->eof)
3656 s->first_displayed_line++;
3657 break;
3658 case KEY_NPAGE:
3659 case CTRL('f'):
3660 case ' ':
3661 if (s->eof)
3662 break;
3663 i = 0;
3664 while (!s->eof && i++ < view->nlines - 1) {
3665 linelen = getline(&line, &linesize, s->f);
3666 s->first_displayed_line++;
3667 if (linelen == -1) {
3668 if (feof(s->f)) {
3669 s->eof = 1;
3670 } else
3671 err = got_ferror(s->f, GOT_ERR_IO);
3672 break;
3675 free(line);
3676 break;
3677 case '[':
3678 if (s->diff_context > 0) {
3679 s->diff_context--;
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3682 if (s->first_displayed_line + view->nlines - 1 >
3683 s->nlines) {
3684 s->first_displayed_line = 1;
3685 s->last_displayed_line = view->nlines;
3688 break;
3689 case ']':
3690 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3691 s->diff_context++;
3692 diff_view_indicate_progress(view);
3693 err = create_diff(s);
3695 break;
3696 case '<':
3697 case ',':
3698 if (s->log_view == NULL)
3699 break;
3700 ls = &s->log_view->state.log;
3701 old_selected_entry = ls->selected_entry;
3703 err = input_log_view(NULL, s->log_view, KEY_UP);
3704 if (err)
3705 break;
3707 if (old_selected_entry == ls->selected_entry)
3708 break;
3710 err = set_selected_commit(s, ls->selected_entry);
3711 if (err)
3712 break;
3714 s->first_displayed_line = 1;
3715 s->last_displayed_line = view->nlines;
3717 diff_view_indicate_progress(view);
3718 err = create_diff(s);
3719 break;
3720 case '>':
3721 case '.':
3722 if (s->log_view == NULL)
3723 break;
3724 ls = &s->log_view->state.log;
3725 old_selected_entry = ls->selected_entry;
3727 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3728 if (err)
3729 break;
3731 if (old_selected_entry == ls->selected_entry)
3732 break;
3734 err = set_selected_commit(s, ls->selected_entry);
3735 if (err)
3736 break;
3738 s->first_displayed_line = 1;
3739 s->last_displayed_line = view->nlines;
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3743 break;
3744 default:
3745 break;
3748 return err;
3751 static const struct got_error *
3752 cmd_diff(int argc, char *argv[])
3754 const struct got_error *error = NULL;
3755 struct got_repository *repo = NULL;
3756 struct got_worktree *worktree = NULL;
3757 struct got_object_id *id1 = NULL, *id2 = NULL;
3758 char *repo_path = NULL, *cwd = NULL;
3759 char *id_str1 = NULL, *id_str2 = NULL;
3760 char *label1 = NULL, *label2 = NULL;
3761 int diff_context = 3, ignore_whitespace = 0;
3762 int ch, force_text_diff = 0;
3763 const char *errstr;
3764 struct tog_view *view;
3766 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3767 switch (ch) {
3768 case 'a':
3769 force_text_diff = 1;
3770 break;
3771 case 'C':
3772 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3773 &errstr);
3774 if (errstr != NULL)
3775 err(1, "-C option %s", errstr);
3776 break;
3777 case 'r':
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 return got_error_from_errno2("realpath",
3781 optarg);
3782 got_path_strip_trailing_slashes(repo_path);
3783 break;
3784 case 'w':
3785 ignore_whitespace = 1;
3786 break;
3787 default:
3788 usage_diff();
3789 /* NOTREACHED */
3793 argc -= optind;
3794 argv += optind;
3796 if (argc == 0) {
3797 usage_diff(); /* TODO show local worktree changes */
3798 } else if (argc == 2) {
3799 id_str1 = argv[0];
3800 id_str2 = argv[1];
3801 } else
3802 usage_diff();
3804 if (repo_path == NULL) {
3805 cwd = getcwd(NULL, 0);
3806 if (cwd == NULL)
3807 return got_error_from_errno("getcwd");
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 goto done;
3811 if (worktree)
3812 repo_path =
3813 strdup(got_worktree_get_repo_path(worktree));
3814 else
3815 repo_path = strdup(cwd);
3816 if (repo_path == NULL) {
3817 error = got_error_from_errno("strdup");
3818 goto done;
3822 error = got_repo_open(&repo, repo_path, NULL);
3823 if (error)
3824 goto done;
3826 init_curses();
3828 error = apply_unveil(got_repo_get_path(repo), NULL);
3829 if (error)
3830 goto done;
3832 error = tog_load_refs(repo);
3833 if (error)
3834 goto done;
3836 error = got_repo_match_object_id(&id1, &label1, id_str1,
3837 GOT_OBJ_TYPE_ANY, 1, repo);
3838 if (error)
3839 goto done;
3841 error = got_repo_match_object_id(&id2, &label2, id_str2,
3842 GOT_OBJ_TYPE_ANY, 1, repo);
3843 if (error)
3844 goto done;
3846 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3847 if (view == NULL) {
3848 error = got_error_from_errno("view_open");
3849 goto done;
3851 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3852 ignore_whitespace, force_text_diff, NULL, repo);
3853 if (error)
3854 goto done;
3855 error = view_loop(view);
3856 done:
3857 free(label1);
3858 free(label2);
3859 free(repo_path);
3860 free(cwd);
3861 if (repo)
3862 got_repo_close(repo);
3863 if (worktree)
3864 got_worktree_close(worktree);
3865 tog_free_refs();
3866 return error;
3869 __dead static void
3870 usage_blame(void)
3872 endwin();
3873 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3874 getprogname());
3875 exit(1);
3878 struct tog_blame_line {
3879 int annotated;
3880 struct got_object_id *id;
3883 static const struct got_error *
3884 draw_blame(struct tog_view *view)
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 struct tog_blame *blame = &s->blame;
3888 regmatch_t *regmatch = &view->regmatch;
3889 const struct got_error *err;
3890 int lineno = 0, nprinted = 0;
3891 char *line = NULL;
3892 size_t linesize = 0;
3893 ssize_t linelen;
3894 wchar_t *wline;
3895 int width;
3896 struct tog_blame_line *blame_line;
3897 struct got_object_id *prev_id = NULL;
3898 char *id_str;
3899 struct tog_color *tc;
3901 err = got_object_id_str(&id_str, s->blamed_commit->id);
3902 if (err)
3903 return err;
3905 rewind(blame->f);
3906 werase(view->window);
3908 if (asprintf(&line, "commit %s", id_str) == -1) {
3909 err = got_error_from_errno("asprintf");
3910 free(id_str);
3911 return err;
3914 err = format_line(&wline, &width, line, view->ncols, 0);
3915 free(line);
3916 line = NULL;
3917 if (err)
3918 return err;
3919 if (view_needs_focus_indication(view))
3920 wstandout(view->window);
3921 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3922 if (tc)
3923 wattr_on(view->window,
3924 COLOR_PAIR(tc->colorpair), NULL);
3925 waddwstr(view->window, wline);
3926 if (tc)
3927 wattr_off(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 if (view_needs_focus_indication(view))
3930 wstandend(view->window);
3931 free(wline);
3932 wline = NULL;
3933 if (width < view->ncols - 1)
3934 waddch(view->window, '\n');
3936 if (asprintf(&line, "[%d/%d] %s%s",
3937 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3938 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3939 free(id_str);
3940 return got_error_from_errno("asprintf");
3942 free(id_str);
3943 err = format_line(&wline, &width, line, view->ncols, 0);
3944 free(line);
3945 line = NULL;
3946 if (err)
3947 return err;
3948 waddwstr(view->window, wline);
3949 free(wline);
3950 wline = NULL;
3951 if (width < view->ncols - 1)
3952 waddch(view->window, '\n');
3954 s->eof = 0;
3955 while (nprinted < view->nlines - 2) {
3956 linelen = getline(&line, &linesize, blame->f);
3957 if (linelen == -1) {
3958 if (feof(blame->f)) {
3959 s->eof = 1;
3960 break;
3962 free(line);
3963 return got_ferror(blame->f, GOT_ERR_IO);
3965 if (++lineno < s->first_displayed_line)
3966 continue;
3968 if (view->focussed && nprinted == s->selected_line - 1)
3969 wstandout(view->window);
3971 if (blame->nlines > 0) {
3972 blame_line = &blame->lines[lineno - 1];
3973 if (blame_line->annotated && prev_id &&
3974 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3975 !(view->focussed &&
3976 nprinted == s->selected_line - 1)) {
3977 waddstr(view->window, " ");
3978 } else if (blame_line->annotated) {
3979 char *id_str;
3980 err = got_object_id_str(&id_str, blame_line->id);
3981 if (err) {
3982 free(line);
3983 return err;
3985 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3986 if (tc)
3987 wattr_on(view->window,
3988 COLOR_PAIR(tc->colorpair), NULL);
3989 wprintw(view->window, "%.8s", id_str);
3990 if (tc)
3991 wattr_off(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 free(id_str);
3994 prev_id = blame_line->id;
3995 } else {
3996 waddstr(view->window, "........");
3997 prev_id = NULL;
3999 } else {
4000 waddstr(view->window, "........");
4001 prev_id = NULL;
4004 if (view->focussed && nprinted == s->selected_line - 1)
4005 wstandend(view->window);
4006 waddstr(view->window, " ");
4008 if (view->ncols <= 9) {
4009 width = 9;
4010 wline = wcsdup(L"");
4011 if (wline == NULL) {
4012 err = got_error_from_errno("wcsdup");
4013 free(line);
4014 return err;
4016 } else if (s->first_displayed_line + nprinted ==
4017 s->matched_line &&
4018 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4019 err = add_matched_line(&width, line, view->ncols - 9, 9,
4020 view->window, regmatch);
4021 if (err) {
4022 free(line);
4023 return err;
4025 width += 9;
4026 } else {
4027 err = format_line(&wline, &width, line,
4028 view->ncols - 9, 9);
4029 waddwstr(view->window, wline);
4030 free(wline);
4031 wline = NULL;
4032 width += 9;
4035 if (width <= view->ncols - 1)
4036 waddch(view->window, '\n');
4037 if (++nprinted == 1)
4038 s->first_displayed_line = lineno;
4040 free(line);
4041 s->last_displayed_line = lineno;
4043 view_vborder(view);
4045 return NULL;
4048 static const struct got_error *
4049 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4051 const struct got_error *err = NULL;
4052 struct tog_blame_cb_args *a = arg;
4053 struct tog_blame_line *line;
4054 int errcode;
4056 if (nlines != a->nlines ||
4057 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4058 return got_error(GOT_ERR_RANGE);
4060 errcode = pthread_mutex_lock(&tog_mutex);
4061 if (errcode)
4062 return got_error_set_errno(errcode, "pthread_mutex_lock");
4064 if (*a->quit) { /* user has quit the blame view */
4065 err = got_error(GOT_ERR_ITER_COMPLETED);
4066 goto done;
4069 if (lineno == -1)
4070 goto done; /* no change in this commit */
4072 line = &a->lines[lineno - 1];
4073 if (line->annotated)
4074 goto done;
4076 line->id = got_object_id_dup(id);
4077 if (line->id == NULL) {
4078 err = got_error_from_errno("got_object_id_dup");
4079 goto done;
4081 line->annotated = 1;
4082 done:
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4084 if (errcode)
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4086 return err;
4089 static void *
4090 blame_thread(void *arg)
4092 const struct got_error *err;
4093 struct tog_blame_thread_args *ta = arg;
4094 struct tog_blame_cb_args *a = ta->cb_args;
4095 int errcode;
4097 err = block_signals_used_by_main_thread();
4098 if (err)
4099 return (void *)err;
4101 err = got_blame(ta->path, a->commit_id, ta->repo,
4102 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4103 if (err && err->code == GOT_ERR_CANCELLED)
4104 err = NULL;
4106 errcode = pthread_mutex_lock(&tog_mutex);
4107 if (errcode)
4108 return (void *)got_error_set_errno(errcode,
4109 "pthread_mutex_lock");
4111 got_repo_close(ta->repo);
4112 ta->repo = NULL;
4113 *ta->complete = 1;
4115 errcode = pthread_mutex_unlock(&tog_mutex);
4116 if (errcode && err == NULL)
4117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4119 return (void *)err;
4122 static struct got_object_id *
4123 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4124 int first_displayed_line, int selected_line)
4126 struct tog_blame_line *line;
4128 if (nlines <= 0)
4129 return NULL;
4131 line = &lines[first_displayed_line - 1 + selected_line - 1];
4132 if (!line->annotated)
4133 return NULL;
4135 return line->id;
4138 static const struct got_error *
4139 stop_blame(struct tog_blame *blame)
4141 const struct got_error *err = NULL;
4142 int i;
4144 if (blame->thread) {
4145 int errcode;
4146 errcode = pthread_mutex_unlock(&tog_mutex);
4147 if (errcode)
4148 return got_error_set_errno(errcode,
4149 "pthread_mutex_unlock");
4150 errcode = pthread_join(blame->thread, (void **)&err);
4151 if (errcode)
4152 return got_error_set_errno(errcode, "pthread_join");
4153 errcode = pthread_mutex_lock(&tog_mutex);
4154 if (errcode)
4155 return got_error_set_errno(errcode,
4156 "pthread_mutex_lock");
4157 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4158 err = NULL;
4159 blame->thread = NULL;
4161 if (blame->thread_args.repo) {
4162 got_repo_close(blame->thread_args.repo);
4163 blame->thread_args.repo = NULL;
4165 if (blame->f) {
4166 if (fclose(blame->f) != 0 && err == NULL)
4167 err = got_error_from_errno("fclose");
4168 blame->f = NULL;
4170 if (blame->lines) {
4171 for (i = 0; i < blame->nlines; i++)
4172 free(blame->lines[i].id);
4173 free(blame->lines);
4174 blame->lines = NULL;
4176 free(blame->cb_args.commit_id);
4177 blame->cb_args.commit_id = NULL;
4179 return err;
4182 static const struct got_error *
4183 cancel_blame_view(void *arg)
4185 const struct got_error *err = NULL;
4186 int *done = arg;
4187 int errcode;
4189 errcode = pthread_mutex_lock(&tog_mutex);
4190 if (errcode)
4191 return got_error_set_errno(errcode,
4192 "pthread_mutex_unlock");
4194 if (*done)
4195 err = got_error(GOT_ERR_CANCELLED);
4197 errcode = pthread_mutex_unlock(&tog_mutex);
4198 if (errcode)
4199 return got_error_set_errno(errcode,
4200 "pthread_mutex_lock");
4202 return err;
4205 static const struct got_error *
4206 run_blame(struct tog_view *view)
4208 struct tog_blame_view_state *s = &view->state.blame;
4209 struct tog_blame *blame = &s->blame;
4210 const struct got_error *err = NULL;
4211 struct got_blob_object *blob = NULL;
4212 struct got_repository *thread_repo = NULL;
4213 struct got_object_id *obj_id = NULL;
4214 int obj_type;
4216 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4217 s->path);
4218 if (err)
4219 return err;
4221 err = got_object_get_type(&obj_type, s->repo, obj_id);
4222 if (err)
4223 goto done;
4225 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4226 err = got_error(GOT_ERR_OBJ_TYPE);
4227 goto done;
4230 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4231 if (err)
4232 goto done;
4233 blame->f = got_opentemp();
4234 if (blame->f == NULL) {
4235 err = got_error_from_errno("got_opentemp");
4236 goto done;
4238 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4239 &blame->line_offsets, blame->f, blob);
4240 if (err || blame->nlines == 0)
4241 goto done;
4243 /* Don't include \n at EOF in the blame line count. */
4244 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4245 blame->nlines--;
4247 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4248 if (blame->lines == NULL) {
4249 err = got_error_from_errno("calloc");
4250 goto done;
4253 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4254 if (err)
4255 goto done;
4257 blame->cb_args.view = view;
4258 blame->cb_args.lines = blame->lines;
4259 blame->cb_args.nlines = blame->nlines;
4260 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4261 if (blame->cb_args.commit_id == NULL) {
4262 err = got_error_from_errno("got_object_id_dup");
4263 goto done;
4265 blame->cb_args.quit = &s->done;
4267 blame->thread_args.path = s->path;
4268 blame->thread_args.repo = thread_repo;
4269 blame->thread_args.cb_args = &blame->cb_args;
4270 blame->thread_args.complete = &s->blame_complete;
4271 blame->thread_args.cancel_cb = cancel_blame_view;
4272 blame->thread_args.cancel_arg = &s->done;
4273 s->blame_complete = 0;
4275 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4276 s->first_displayed_line = 1;
4277 s->last_displayed_line = view->nlines;
4278 s->selected_line = 1;
4281 done:
4282 if (blob)
4283 got_object_blob_close(blob);
4284 free(obj_id);
4285 if (err)
4286 stop_blame(blame);
4287 return err;
4290 static const struct got_error *
4291 open_blame_view(struct tog_view *view, char *path,
4292 struct got_object_id *commit_id, struct got_repository *repo)
4294 const struct got_error *err = NULL;
4295 struct tog_blame_view_state *s = &view->state.blame;
4297 SIMPLEQ_INIT(&s->blamed_commits);
4299 s->path = strdup(path);
4300 if (s->path == NULL)
4301 return got_error_from_errno("strdup");
4303 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4304 if (err) {
4305 free(s->path);
4306 return err;
4309 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4310 s->first_displayed_line = 1;
4311 s->last_displayed_line = view->nlines;
4312 s->selected_line = 1;
4313 s->blame_complete = 0;
4314 s->repo = repo;
4315 s->commit_id = commit_id;
4316 memset(&s->blame, 0, sizeof(s->blame));
4318 SIMPLEQ_INIT(&s->colors);
4319 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4320 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4321 get_color_value("TOG_COLOR_COMMIT"));
4322 if (err)
4323 return err;
4326 view->show = show_blame_view;
4327 view->input = input_blame_view;
4328 view->close = close_blame_view;
4329 view->search_start = search_start_blame_view;
4330 view->search_next = search_next_blame_view;
4332 return run_blame(view);
4335 static const struct got_error *
4336 close_blame_view(struct tog_view *view)
4338 const struct got_error *err = NULL;
4339 struct tog_blame_view_state *s = &view->state.blame;
4341 if (s->blame.thread)
4342 err = stop_blame(&s->blame);
4344 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4345 struct got_object_qid *blamed_commit;
4346 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4347 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4348 got_object_qid_free(blamed_commit);
4351 free(s->path);
4352 free_colors(&s->colors);
4354 return err;
4357 static const struct got_error *
4358 search_start_blame_view(struct tog_view *view)
4360 struct tog_blame_view_state *s = &view->state.blame;
4362 s->matched_line = 0;
4363 return NULL;
4366 static const struct got_error *
4367 search_next_blame_view(struct tog_view *view)
4369 struct tog_blame_view_state *s = &view->state.blame;
4370 int lineno;
4371 char *line = NULL;
4372 size_t linesize = 0;
4373 ssize_t linelen;
4375 if (!view->searching) {
4376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4377 return NULL;
4380 if (s->matched_line) {
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno = s->matched_line + 1;
4383 else
4384 lineno = s->matched_line - 1;
4385 } else {
4386 if (view->searching == TOG_SEARCH_FORWARD)
4387 lineno = 1;
4388 else
4389 lineno = s->blame.nlines;
4392 while (1) {
4393 off_t offset;
4395 if (lineno <= 0 || lineno > s->blame.nlines) {
4396 if (s->matched_line == 0) {
4397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4398 break;
4401 if (view->searching == TOG_SEARCH_FORWARD)
4402 lineno = 1;
4403 else
4404 lineno = s->blame.nlines;
4407 offset = s->blame.line_offsets[lineno - 1];
4408 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4409 free(line);
4410 return got_error_from_errno("fseeko");
4412 linelen = getline(&line, &linesize, s->blame.f);
4413 if (linelen != -1 &&
4414 match_line(line, &view->regex, 1, &view->regmatch)) {
4415 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4416 s->matched_line = lineno;
4417 break;
4419 if (view->searching == TOG_SEARCH_FORWARD)
4420 lineno++;
4421 else
4422 lineno--;
4424 free(line);
4426 if (s->matched_line) {
4427 s->first_displayed_line = s->matched_line;
4428 s->selected_line = 1;
4431 return NULL;
4434 static const struct got_error *
4435 show_blame_view(struct tog_view *view)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_view_state *s = &view->state.blame;
4439 int errcode;
4441 if (s->blame.thread == NULL) {
4442 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4443 &s->blame.thread_args);
4444 if (errcode)
4445 return got_error_set_errno(errcode, "pthread_create");
4447 halfdelay(1); /* fast refresh while annotating */
4450 if (s->blame_complete)
4451 halfdelay(10); /* disable fast refresh */
4453 err = draw_blame(view);
4455 view_vborder(view);
4456 return err;
4459 static const struct got_error *
4460 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4462 const struct got_error *err = NULL, *thread_err = NULL;
4463 struct tog_view *diff_view;
4464 struct tog_blame_view_state *s = &view->state.blame;
4465 int begin_x = 0;
4467 switch (ch) {
4468 case 'q':
4469 s->done = 1;
4470 break;
4471 case 'k':
4472 case KEY_UP:
4473 if (s->selected_line > 1)
4474 s->selected_line--;
4475 else if (s->selected_line == 1 &&
4476 s->first_displayed_line > 1)
4477 s->first_displayed_line--;
4478 break;
4479 case KEY_PPAGE:
4480 case CTRL('b'):
4481 if (s->first_displayed_line == 1) {
4482 s->selected_line = 1;
4483 break;
4485 if (s->first_displayed_line > view->nlines - 2)
4486 s->first_displayed_line -=
4487 (view->nlines - 2);
4488 else
4489 s->first_displayed_line = 1;
4490 break;
4491 case 'j':
4492 case KEY_DOWN:
4493 if (s->selected_line < view->nlines - 2 &&
4494 s->first_displayed_line +
4495 s->selected_line <= s->blame.nlines)
4496 s->selected_line++;
4497 else if (s->last_displayed_line <
4498 s->blame.nlines)
4499 s->first_displayed_line++;
4500 break;
4501 case 'b':
4502 case 'p': {
4503 struct got_object_id *id = NULL;
4504 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4505 s->first_displayed_line, s->selected_line);
4506 if (id == NULL)
4507 break;
4508 if (ch == 'p') {
4509 struct got_commit_object *commit;
4510 struct got_object_qid *pid;
4511 struct got_object_id *blob_id = NULL;
4512 int obj_type;
4513 err = got_object_open_as_commit(&commit,
4514 s->repo, id);
4515 if (err)
4516 break;
4517 pid = SIMPLEQ_FIRST(
4518 got_object_commit_get_parent_ids(commit));
4519 if (pid == NULL) {
4520 got_object_commit_close(commit);
4521 break;
4523 /* Check if path history ends here. */
4524 err = got_object_id_by_path(&blob_id, s->repo,
4525 pid->id, s->path);
4526 if (err) {
4527 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4528 err = NULL;
4529 got_object_commit_close(commit);
4530 break;
4532 err = got_object_get_type(&obj_type, s->repo,
4533 blob_id);
4534 free(blob_id);
4535 /* Can't blame non-blob type objects. */
4536 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4537 got_object_commit_close(commit);
4538 break;
4540 err = got_object_qid_alloc(&s->blamed_commit,
4541 pid->id);
4542 got_object_commit_close(commit);
4543 } else {
4544 if (got_object_id_cmp(id,
4545 s->blamed_commit->id) == 0)
4546 break;
4547 err = got_object_qid_alloc(&s->blamed_commit,
4548 id);
4550 if (err)
4551 break;
4552 s->done = 1;
4553 thread_err = stop_blame(&s->blame);
4554 s->done = 0;
4555 if (thread_err)
4556 break;
4557 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4558 s->blamed_commit, entry);
4559 err = run_blame(view);
4560 if (err)
4561 break;
4562 break;
4564 case 'B': {
4565 struct got_object_qid *first;
4566 first = SIMPLEQ_FIRST(&s->blamed_commits);
4567 if (!got_object_id_cmp(first->id, s->commit_id))
4568 break;
4569 s->done = 1;
4570 thread_err = stop_blame(&s->blame);
4571 s->done = 0;
4572 if (thread_err)
4573 break;
4574 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4575 got_object_qid_free(s->blamed_commit);
4576 s->blamed_commit =
4577 SIMPLEQ_FIRST(&s->blamed_commits);
4578 err = run_blame(view);
4579 if (err)
4580 break;
4581 break;
4583 case KEY_ENTER:
4584 case '\r': {
4585 struct got_object_id *id = NULL;
4586 struct got_object_qid *pid;
4587 struct got_commit_object *commit = NULL;
4588 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4589 s->first_displayed_line, s->selected_line);
4590 if (id == NULL)
4591 break;
4592 err = got_object_open_as_commit(&commit, s->repo, id);
4593 if (err)
4594 break;
4595 pid = SIMPLEQ_FIRST(
4596 got_object_commit_get_parent_ids(commit));
4597 if (view_is_parent_view(view))
4598 begin_x = view_split_begin_x(view->begin_x);
4599 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4600 if (diff_view == NULL) {
4601 got_object_commit_close(commit);
4602 err = got_error_from_errno("view_open");
4603 break;
4605 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4606 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4607 got_object_commit_close(commit);
4608 if (err) {
4609 view_close(diff_view);
4610 break;
4612 view->focussed = 0;
4613 diff_view->focussed = 1;
4614 if (view_is_parent_view(view)) {
4615 err = view_close_child(view);
4616 if (err)
4617 break;
4618 view_set_child(view, diff_view);
4619 view->focus_child = 1;
4620 } else
4621 *new_view = diff_view;
4622 if (err)
4623 break;
4624 break;
4626 case KEY_NPAGE:
4627 case CTRL('f'):
4628 case ' ':
4629 if (s->last_displayed_line >= s->blame.nlines &&
4630 s->selected_line >= MIN(s->blame.nlines,
4631 view->nlines - 2)) {
4632 break;
4634 if (s->last_displayed_line >= s->blame.nlines &&
4635 s->selected_line < view->nlines - 2) {
4636 s->selected_line = MIN(s->blame.nlines,
4637 view->nlines - 2);
4638 break;
4640 if (s->last_displayed_line + view->nlines - 2
4641 <= s->blame.nlines)
4642 s->first_displayed_line +=
4643 view->nlines - 2;
4644 else
4645 s->first_displayed_line =
4646 s->blame.nlines -
4647 (view->nlines - 3);
4648 break;
4649 case KEY_RESIZE:
4650 if (s->selected_line > view->nlines - 2) {
4651 s->selected_line = MIN(s->blame.nlines,
4652 view->nlines - 2);
4654 break;
4655 default:
4656 break;
4658 return thread_err ? thread_err : err;
4661 static const struct got_error *
4662 cmd_blame(int argc, char *argv[])
4664 const struct got_error *error;
4665 struct got_repository *repo = NULL;
4666 struct got_worktree *worktree = NULL;
4667 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4668 char *link_target = NULL;
4669 struct got_object_id *commit_id = NULL;
4670 char *commit_id_str = NULL;
4671 int ch;
4672 struct tog_view *view;
4674 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4675 switch (ch) {
4676 case 'c':
4677 commit_id_str = optarg;
4678 break;
4679 case 'r':
4680 repo_path = realpath(optarg, NULL);
4681 if (repo_path == NULL)
4682 return got_error_from_errno2("realpath",
4683 optarg);
4684 break;
4685 default:
4686 usage_blame();
4687 /* NOTREACHED */
4691 argc -= optind;
4692 argv += optind;
4694 if (argc != 1)
4695 usage_blame();
4697 if (repo_path == NULL) {
4698 cwd = getcwd(NULL, 0);
4699 if (cwd == NULL)
4700 return got_error_from_errno("getcwd");
4701 error = got_worktree_open(&worktree, cwd);
4702 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4703 goto done;
4704 if (worktree)
4705 repo_path =
4706 strdup(got_worktree_get_repo_path(worktree));
4707 else
4708 repo_path = strdup(cwd);
4709 if (repo_path == NULL) {
4710 error = got_error_from_errno("strdup");
4711 goto done;
4715 error = got_repo_open(&repo, repo_path, NULL);
4716 if (error != NULL)
4717 goto done;
4719 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4720 worktree);
4721 if (error)
4722 goto done;
4724 init_curses();
4726 error = apply_unveil(got_repo_get_path(repo), NULL);
4727 if (error)
4728 goto done;
4730 error = tog_load_refs(repo);
4731 if (error)
4732 goto done;
4734 if (commit_id_str == NULL) {
4735 struct got_reference *head_ref;
4736 error = got_ref_open(&head_ref, repo, worktree ?
4737 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4738 if (error != NULL)
4739 goto done;
4740 error = got_ref_resolve(&commit_id, repo, head_ref);
4741 got_ref_close(head_ref);
4742 } else {
4743 error = got_repo_match_object_id(&commit_id, NULL,
4744 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4746 if (error != NULL)
4747 goto done;
4749 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4750 if (view == NULL) {
4751 error = got_error_from_errno("view_open");
4752 goto done;
4755 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4756 commit_id, repo);
4757 if (error)
4758 goto done;
4760 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4761 commit_id, repo);
4762 if (error)
4763 goto done;
4764 if (worktree) {
4765 /* Release work tree lock. */
4766 got_worktree_close(worktree);
4767 worktree = NULL;
4769 error = view_loop(view);
4770 done:
4771 free(repo_path);
4772 free(in_repo_path);
4773 free(link_target);
4774 free(cwd);
4775 free(commit_id);
4776 if (worktree)
4777 got_worktree_close(worktree);
4778 if (repo)
4779 got_repo_close(repo);
4780 tog_free_refs();
4781 return error;
4784 static const struct got_error *
4785 draw_tree_entries(struct tog_view *view, const char *parent_path)
4787 struct tog_tree_view_state *s = &view->state.tree;
4788 const struct got_error *err = NULL;
4789 struct got_tree_entry *te;
4790 wchar_t *wline;
4791 struct tog_color *tc;
4792 int width, n, i, nentries;
4793 int limit = view->nlines;
4795 s->ndisplayed = 0;
4797 werase(view->window);
4799 if (limit == 0)
4800 return NULL;
4802 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4803 if (err)
4804 return err;
4805 if (view_needs_focus_indication(view))
4806 wstandout(view->window);
4807 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4808 if (tc)
4809 wattr_on(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 waddwstr(view->window, wline);
4812 if (tc)
4813 wattr_off(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 if (view_needs_focus_indication(view))
4816 wstandend(view->window);
4817 free(wline);
4818 wline = NULL;
4819 if (width < view->ncols - 1)
4820 waddch(view->window, '\n');
4821 if (--limit <= 0)
4822 return NULL;
4823 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4824 if (err)
4825 return err;
4826 waddwstr(view->window, wline);
4827 free(wline);
4828 wline = NULL;
4829 if (width < view->ncols - 1)
4830 waddch(view->window, '\n');
4831 if (--limit <= 0)
4832 return NULL;
4833 waddch(view->window, '\n');
4834 if (--limit <= 0)
4835 return NULL;
4837 if (s->first_displayed_entry == NULL) {
4838 te = got_object_tree_get_first_entry(s->tree);
4839 if (s->selected == 0) {
4840 if (view->focussed)
4841 wstandout(view->window);
4842 s->selected_entry = NULL;
4844 waddstr(view->window, " ..\n"); /* parent directory */
4845 if (s->selected == 0 && view->focussed)
4846 wstandend(view->window);
4847 s->ndisplayed++;
4848 if (--limit <= 0)
4849 return NULL;
4850 n = 1;
4851 } else {
4852 n = 0;
4853 te = s->first_displayed_entry;
4856 nentries = got_object_tree_get_nentries(s->tree);
4857 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4858 char *line = NULL, *id_str = NULL, *link_target = NULL;
4859 const char *modestr = "";
4860 mode_t mode;
4862 te = got_object_tree_get_entry(s->tree, i);
4863 mode = got_tree_entry_get_mode(te);
4865 if (s->show_ids) {
4866 err = got_object_id_str(&id_str,
4867 got_tree_entry_get_id(te));
4868 if (err)
4869 return got_error_from_errno(
4870 "got_object_id_str");
4872 if (got_object_tree_entry_is_submodule(te))
4873 modestr = "$";
4874 else if (S_ISLNK(mode)) {
4875 int i;
4877 err = got_tree_entry_get_symlink_target(&link_target,
4878 te, s->repo);
4879 if (err) {
4880 free(id_str);
4881 return err;
4883 for (i = 0; i < strlen(link_target); i++) {
4884 if (!isprint((unsigned char)link_target[i]))
4885 link_target[i] = '?';
4887 modestr = "@";
4889 else if (S_ISDIR(mode))
4890 modestr = "/";
4891 else if (mode & S_IXUSR)
4892 modestr = "*";
4893 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4894 got_tree_entry_get_name(te), modestr,
4895 link_target ? " -> ": "",
4896 link_target ? link_target : "") == -1) {
4897 free(id_str);
4898 free(link_target);
4899 return got_error_from_errno("asprintf");
4901 free(id_str);
4902 free(link_target);
4903 err = format_line(&wline, &width, line, view->ncols, 0);
4904 if (err) {
4905 free(line);
4906 break;
4908 if (n == s->selected) {
4909 if (view->focussed)
4910 wstandout(view->window);
4911 s->selected_entry = te;
4913 tc = match_color(&s->colors, line);
4914 if (tc)
4915 wattr_on(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 waddwstr(view->window, wline);
4918 if (tc)
4919 wattr_off(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 if (width < view->ncols - 1)
4922 waddch(view->window, '\n');
4923 if (n == s->selected && view->focussed)
4924 wstandend(view->window);
4925 free(line);
4926 free(wline);
4927 wline = NULL;
4928 n++;
4929 s->ndisplayed++;
4930 s->last_displayed_entry = te;
4931 if (--limit <= 0)
4932 break;
4935 return err;
4938 static void
4939 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4941 struct got_tree_entry *te;
4942 int isroot = s->tree == s->root;
4943 int i = 0;
4945 if (s->first_displayed_entry == NULL)
4946 return;
4948 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4949 while (i++ < maxscroll) {
4950 if (te == NULL) {
4951 if (!isroot)
4952 s->first_displayed_entry = NULL;
4953 break;
4955 s->first_displayed_entry = te;
4956 te = got_tree_entry_get_prev(s->tree, te);
4960 static void
4961 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4963 struct got_tree_entry *next, *last;
4964 int n = 0;
4966 if (s->first_displayed_entry)
4967 next = got_tree_entry_get_next(s->tree,
4968 s->first_displayed_entry);
4969 else
4970 next = got_object_tree_get_first_entry(s->tree);
4972 last = s->last_displayed_entry;
4973 while (next && last && n++ < maxscroll) {
4974 last = got_tree_entry_get_next(s->tree, last);
4975 if (last) {
4976 s->first_displayed_entry = next;
4977 next = got_tree_entry_get_next(s->tree, next);
4982 static const struct got_error *
4983 tree_entry_path(char **path, struct tog_parent_trees *parents,
4984 struct got_tree_entry *te)
4986 const struct got_error *err = NULL;
4987 struct tog_parent_tree *pt;
4988 size_t len = 2; /* for leading slash and NUL */
4990 TAILQ_FOREACH(pt, parents, entry)
4991 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4992 + 1 /* slash */;
4993 if (te)
4994 len += strlen(got_tree_entry_get_name(te));
4996 *path = calloc(1, len);
4997 if (path == NULL)
4998 return got_error_from_errno("calloc");
5000 (*path)[0] = '/';
5001 pt = TAILQ_LAST(parents, tog_parent_trees);
5002 while (pt) {
5003 const char *name = got_tree_entry_get_name(pt->selected_entry);
5004 if (strlcat(*path, name, len) >= len) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5006 goto done;
5008 if (strlcat(*path, "/", len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5010 goto done;
5012 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5014 if (te) {
5015 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5017 goto done;
5020 done:
5021 if (err) {
5022 free(*path);
5023 *path = NULL;
5025 return err;
5028 static const struct got_error *
5029 blame_tree_entry(struct tog_view **new_view, int begin_x,
5030 struct got_tree_entry *te, struct tog_parent_trees *parents,
5031 struct got_object_id *commit_id, struct got_repository *repo)
5033 const struct got_error *err = NULL;
5034 char *path;
5035 struct tog_view *blame_view;
5037 *new_view = NULL;
5039 err = tree_entry_path(&path, parents, te);
5040 if (err)
5041 return err;
5043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5044 if (blame_view == NULL) {
5045 err = got_error_from_errno("view_open");
5046 goto done;
5049 err = open_blame_view(blame_view, path, commit_id, repo);
5050 if (err) {
5051 if (err->code == GOT_ERR_CANCELLED)
5052 err = NULL;
5053 view_close(blame_view);
5054 } else
5055 *new_view = blame_view;
5056 done:
5057 free(path);
5058 return err;
5061 static const struct got_error *
5062 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5063 struct tog_tree_view_state *s)
5065 struct tog_view *log_view;
5066 const struct got_error *err = NULL;
5067 char *path;
5069 *new_view = NULL;
5071 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5072 if (log_view == NULL)
5073 return got_error_from_errno("view_open");
5075 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5076 if (err)
5077 return err;
5079 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5080 path, 0);
5081 if (err)
5082 view_close(log_view);
5083 else
5084 *new_view = log_view;
5085 free(path);
5086 return err;
5089 static const struct got_error *
5090 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5091 struct got_object_id *commit_id, const char *head_ref_name,
5092 struct got_repository *repo)
5094 const struct got_error *err = NULL;
5095 char *commit_id_str = NULL;
5096 struct tog_tree_view_state *s = &view->state.tree;
5098 TAILQ_INIT(&s->parents);
5100 err = got_object_id_str(&commit_id_str, commit_id);
5101 if (err != NULL)
5102 goto done;
5104 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5105 err = got_error_from_errno("asprintf");
5106 goto done;
5109 s->root = s->tree = root;
5110 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5112 s->commit_id = got_object_id_dup(commit_id);
5113 if (s->commit_id == NULL) {
5114 err = got_error_from_errno("got_object_id_dup");
5115 goto done;
5117 if (head_ref_name) {
5118 s->head_ref_name = strdup(head_ref_name);
5119 if (s->head_ref_name == NULL) {
5120 err = got_error_from_errno("strdup");
5121 goto done;
5124 s->repo = repo;
5126 SIMPLEQ_INIT(&s->colors);
5128 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5129 err = add_color(&s->colors, "\\$$",
5130 TOG_COLOR_TREE_SUBMODULE,
5131 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5132 if (err)
5133 goto done;
5134 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5135 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5136 if (err) {
5137 free_colors(&s->colors);
5138 goto done;
5140 err = add_color(&s->colors, "/$",
5141 TOG_COLOR_TREE_DIRECTORY,
5142 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5143 if (err) {
5144 free_colors(&s->colors);
5145 goto done;
5148 err = add_color(&s->colors, "\\*$",
5149 TOG_COLOR_TREE_EXECUTABLE,
5150 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5151 if (err) {
5152 free_colors(&s->colors);
5153 goto done;
5156 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5157 get_color_value("TOG_COLOR_COMMIT"));
5158 if (err) {
5159 free_colors(&s->colors);
5160 goto done;
5164 view->show = show_tree_view;
5165 view->input = input_tree_view;
5166 view->close = close_tree_view;
5167 view->search_start = search_start_tree_view;
5168 view->search_next = search_next_tree_view;
5169 done:
5170 free(commit_id_str);
5171 if (err) {
5172 free(s->tree_label);
5173 s->tree_label = NULL;
5175 return err;
5178 static const struct got_error *
5179 close_tree_view(struct tog_view *view)
5181 struct tog_tree_view_state *s = &view->state.tree;
5183 free_colors(&s->colors);
5184 free(s->tree_label);
5185 s->tree_label = NULL;
5186 free(s->commit_id);
5187 s->commit_id = NULL;
5188 free(s->head_ref_name);
5189 s->head_ref_name = NULL;
5190 while (!TAILQ_EMPTY(&s->parents)) {
5191 struct tog_parent_tree *parent;
5192 parent = TAILQ_FIRST(&s->parents);
5193 TAILQ_REMOVE(&s->parents, parent, entry);
5194 free(parent);
5197 if (s->tree != s->root)
5198 got_object_tree_close(s->tree);
5199 got_object_tree_close(s->root);
5200 return NULL;
5203 static const struct got_error *
5204 search_start_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5208 s->matched_entry = NULL;
5209 return NULL;
5212 static int
5213 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5215 regmatch_t regmatch;
5217 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5218 0) == 0;
5221 static const struct got_error *
5222 search_next_tree_view(struct tog_view *view)
5224 struct tog_tree_view_state *s = &view->state.tree;
5225 struct got_tree_entry *te = NULL;
5227 if (!view->searching) {
5228 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5229 return NULL;
5232 if (s->matched_entry) {
5233 if (view->searching == TOG_SEARCH_FORWARD) {
5234 if (s->selected_entry)
5235 te = got_tree_entry_get_next(s->tree,
5236 s->selected_entry);
5237 else
5238 te = got_object_tree_get_first_entry(s->tree);
5239 } else {
5240 if (s->selected_entry == NULL)
5241 te = got_object_tree_get_last_entry(s->tree);
5242 else
5243 te = got_tree_entry_get_prev(s->tree,
5244 s->selected_entry);
5246 } else {
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_object_tree_get_first_entry(s->tree);
5249 else
5250 te = got_object_tree_get_last_entry(s->tree);
5253 while (1) {
5254 if (te == NULL) {
5255 if (s->matched_entry == NULL) {
5256 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5257 return NULL;
5259 if (view->searching == TOG_SEARCH_FORWARD)
5260 te = got_object_tree_get_first_entry(s->tree);
5261 else
5262 te = got_object_tree_get_last_entry(s->tree);
5265 if (match_tree_entry(te, &view->regex)) {
5266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5267 s->matched_entry = te;
5268 break;
5271 if (view->searching == TOG_SEARCH_FORWARD)
5272 te = got_tree_entry_get_next(s->tree, te);
5273 else
5274 te = got_tree_entry_get_prev(s->tree, te);
5277 if (s->matched_entry) {
5278 s->first_displayed_entry = s->matched_entry;
5279 s->selected = 0;
5282 return NULL;
5285 static const struct got_error *
5286 show_tree_view(struct tog_view *view)
5288 const struct got_error *err = NULL;
5289 struct tog_tree_view_state *s = &view->state.tree;
5290 char *parent_path;
5292 err = tree_entry_path(&parent_path, &s->parents, NULL);
5293 if (err)
5294 return err;
5296 err = draw_tree_entries(view, parent_path);
5297 free(parent_path);
5299 view_vborder(view);
5300 return err;
5303 static const struct got_error *
5304 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5306 const struct got_error *err = NULL;
5307 struct tog_tree_view_state *s = &view->state.tree;
5308 struct tog_view *log_view, *ref_view;
5309 int begin_x = 0;
5311 switch (ch) {
5312 case 'i':
5313 s->show_ids = !s->show_ids;
5314 break;
5315 case 'l':
5316 if (!s->selected_entry)
5317 break;
5318 if (view_is_parent_view(view))
5319 begin_x = view_split_begin_x(view->begin_x);
5320 err = log_selected_tree_entry(&log_view, begin_x, s);
5321 view->focussed = 0;
5322 log_view->focussed = 1;
5323 if (view_is_parent_view(view)) {
5324 err = view_close_child(view);
5325 if (err)
5326 return err;
5327 view_set_child(view, log_view);
5328 view->focus_child = 1;
5329 } else
5330 *new_view = log_view;
5331 break;
5332 case 'r':
5333 if (view_is_parent_view(view))
5334 begin_x = view_split_begin_x(view->begin_x);
5335 ref_view = view_open(view->nlines, view->ncols,
5336 view->begin_y, begin_x, TOG_VIEW_REF);
5337 if (ref_view == NULL)
5338 return got_error_from_errno("view_open");
5339 err = open_ref_view(ref_view, s->repo);
5340 if (err) {
5341 view_close(ref_view);
5342 return err;
5344 view->focussed = 0;
5345 ref_view->focussed = 1;
5346 if (view_is_parent_view(view)) {
5347 err = view_close_child(view);
5348 if (err)
5349 return err;
5350 view_set_child(view, ref_view);
5351 view->focus_child = 1;
5352 } else
5353 *new_view = ref_view;
5354 break;
5355 case 'k':
5356 case KEY_UP:
5357 if (s->selected > 0) {
5358 s->selected--;
5359 break;
5361 tree_scroll_up(s, 1);
5362 break;
5363 case KEY_PPAGE:
5364 case CTRL('b'):
5365 if (s->tree == s->root) {
5366 if (got_object_tree_get_first_entry(s->tree) ==
5367 s->first_displayed_entry)
5368 s->selected = 0;
5369 } else {
5370 if (s->first_displayed_entry == NULL)
5371 s->selected = 0;
5373 tree_scroll_up(s, MAX(0, view->nlines - 3));
5374 break;
5375 case 'j':
5376 case KEY_DOWN:
5377 if (s->selected < s->ndisplayed - 1) {
5378 s->selected++;
5379 break;
5381 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5382 == NULL)
5383 /* can't scroll any further */
5384 break;
5385 tree_scroll_down(s, 1);
5386 break;
5387 case KEY_NPAGE:
5388 case CTRL('f'):
5389 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5390 == NULL) {
5391 /* can't scroll any further; move cursor down */
5392 if (s->selected < s->ndisplayed - 1)
5393 s->selected = s->ndisplayed - 1;
5394 break;
5396 tree_scroll_down(s, view->nlines - 3);
5397 break;
5398 case KEY_ENTER:
5399 case '\r':
5400 case KEY_BACKSPACE:
5401 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5402 struct tog_parent_tree *parent;
5403 /* user selected '..' */
5404 if (s->tree == s->root)
5405 break;
5406 parent = TAILQ_FIRST(&s->parents);
5407 TAILQ_REMOVE(&s->parents, parent,
5408 entry);
5409 got_object_tree_close(s->tree);
5410 s->tree = parent->tree;
5411 s->first_displayed_entry =
5412 parent->first_displayed_entry;
5413 s->selected_entry =
5414 parent->selected_entry;
5415 s->selected = parent->selected;
5416 free(parent);
5417 } else if (S_ISDIR(got_tree_entry_get_mode(
5418 s->selected_entry))) {
5419 struct got_tree_object *subtree;
5420 err = got_object_open_as_tree(&subtree, s->repo,
5421 got_tree_entry_get_id(s->selected_entry));
5422 if (err)
5423 break;
5424 err = tree_view_visit_subtree(s, subtree);
5425 if (err) {
5426 got_object_tree_close(subtree);
5427 break;
5429 } else if (S_ISREG(got_tree_entry_get_mode(
5430 s->selected_entry))) {
5431 struct tog_view *blame_view;
5432 int begin_x = view_is_parent_view(view) ?
5433 view_split_begin_x(view->begin_x) : 0;
5435 err = blame_tree_entry(&blame_view, begin_x,
5436 s->selected_entry, &s->parents,
5437 s->commit_id, s->repo);
5438 if (err)
5439 break;
5440 view->focussed = 0;
5441 blame_view->focussed = 1;
5442 if (view_is_parent_view(view)) {
5443 err = view_close_child(view);
5444 if (err)
5445 return err;
5446 view_set_child(view, blame_view);
5447 view->focus_child = 1;
5448 } else
5449 *new_view = blame_view;
5451 break;
5452 case KEY_RESIZE:
5453 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5454 s->selected = view->nlines - 4;
5455 break;
5456 default:
5457 break;
5460 return err;
5463 __dead static void
5464 usage_tree(void)
5466 endwin();
5467 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5468 getprogname());
5469 exit(1);
5472 static const struct got_error *
5473 cmd_tree(int argc, char *argv[])
5475 const struct got_error *error;
5476 struct got_repository *repo = NULL;
5477 struct got_worktree *worktree = NULL;
5478 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5479 struct got_object_id *commit_id = NULL;
5480 const char *commit_id_arg = NULL;
5481 char *label = NULL;
5482 struct got_commit_object *commit = NULL;
5483 struct got_tree_object *tree = NULL;
5484 struct got_reference *ref = NULL;
5485 const char *head_ref_name = NULL;
5486 int ch;
5487 struct tog_view *view;
5489 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5490 switch (ch) {
5491 case 'c':
5492 commit_id_arg = optarg;
5493 break;
5494 case 'r':
5495 repo_path = realpath(optarg, NULL);
5496 if (repo_path == NULL)
5497 return got_error_from_errno2("realpath",
5498 optarg);
5499 break;
5500 default:
5501 usage_tree();
5502 /* NOTREACHED */
5506 argc -= optind;
5507 argv += optind;
5509 if (argc > 1)
5510 usage_tree();
5512 if (repo_path == NULL) {
5513 cwd = getcwd(NULL, 0);
5514 if (cwd == NULL)
5515 return got_error_from_errno("getcwd");
5516 error = got_worktree_open(&worktree, cwd);
5517 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5518 goto done;
5519 if (worktree)
5520 repo_path =
5521 strdup(got_worktree_get_repo_path(worktree));
5522 else
5523 repo_path = strdup(cwd);
5524 if (repo_path == NULL) {
5525 error = got_error_from_errno("strdup");
5526 goto done;
5530 error = got_repo_open(&repo, repo_path, NULL);
5531 if (error != NULL)
5532 goto done;
5534 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5535 repo, worktree);
5536 if (error)
5537 goto done;
5539 init_curses();
5541 error = apply_unveil(got_repo_get_path(repo), NULL);
5542 if (error)
5543 goto done;
5545 error = tog_load_refs(repo);
5546 if (error)
5547 goto done;
5549 if (commit_id_arg == NULL) {
5550 error = got_repo_match_object_id(&commit_id, &label,
5551 worktree ? got_worktree_get_head_ref_name(worktree) :
5552 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5553 if (error)
5554 goto done;
5555 head_ref_name = label;
5556 } else {
5557 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5558 if (error == NULL)
5559 head_ref_name = got_ref_get_name(ref);
5560 else if (error->code != GOT_ERR_NOT_REF)
5561 goto done;
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5564 if (error)
5565 goto done;
5568 error = got_object_open_as_commit(&commit, repo, commit_id);
5569 if (error)
5570 goto done;
5572 error = got_object_open_as_tree(&tree, repo,
5573 got_object_commit_get_tree_id(commit));
5574 if (error)
5575 goto done;
5577 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5578 if (view == NULL) {
5579 error = got_error_from_errno("view_open");
5580 goto done;
5582 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5583 if (error)
5584 goto done;
5585 if (!got_path_is_root_dir(in_repo_path)) {
5586 error = tree_view_walk_path(&view->state.tree, commit_id,
5587 in_repo_path);
5588 if (error)
5589 goto done;
5592 if (worktree) {
5593 /* Release work tree lock. */
5594 got_worktree_close(worktree);
5595 worktree = NULL;
5597 error = view_loop(view);
5598 done:
5599 free(repo_path);
5600 free(cwd);
5601 free(commit_id);
5602 free(label);
5603 if (ref)
5604 got_ref_close(ref);
5605 if (commit)
5606 got_object_commit_close(commit);
5607 if (tree)
5608 got_object_tree_close(tree);
5609 if (repo)
5610 got_repo_close(repo);
5611 tog_free_refs();
5612 return error;
5615 static const struct got_error *
5616 ref_view_load_refs(struct tog_ref_view_state *s)
5618 const struct got_error *err;
5619 struct got_reflist_entry *sre;
5620 struct tog_reflist_entry *re;
5622 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5623 got_ref_cmp_by_name, NULL);
5624 if (err)
5625 return err;
5627 s->nrefs = 0;
5628 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5629 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5630 continue;
5632 re = malloc(sizeof(*re));
5633 if (re == NULL)
5634 return got_error_from_errno("malloc");
5636 re->ref = sre->ref;
5637 re->idx = s->nrefs++;
5638 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5641 return NULL;
5644 void
5645 ref_view_free_refs(struct tog_ref_view_state *s)
5647 struct tog_reflist_entry *re;
5649 while (!TAILQ_EMPTY(&s->refs)) {
5650 re = TAILQ_FIRST(&s->refs);
5651 TAILQ_REMOVE(&s->refs, re, entry);
5652 free(re);
5654 got_ref_list_free(&s->simplerefs);
5657 static const struct got_error *
5658 open_ref_view(struct tog_view *view, struct got_repository *repo)
5660 const struct got_error *err = NULL;
5661 struct tog_ref_view_state *s = &view->state.ref;
5663 s->selected_entry = 0;
5664 s->repo = repo;
5666 SIMPLEQ_INIT(&s->simplerefs);
5667 TAILQ_INIT(&s->refs);
5668 SIMPLEQ_INIT(&s->colors);
5670 err = ref_view_load_refs(s);
5671 if (err)
5672 return err;
5674 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5676 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5677 err = add_color(&s->colors, "^refs/heads/",
5678 TOG_COLOR_REFS_HEADS,
5679 get_color_value("TOG_COLOR_REFS_HEADS"));
5680 if (err)
5681 goto done;
5683 err = add_color(&s->colors, "^refs/tags/",
5684 TOG_COLOR_REFS_TAGS,
5685 get_color_value("TOG_COLOR_REFS_TAGS"));
5686 if (err)
5687 goto done;
5689 err = add_color(&s->colors, "^refs/remotes/",
5690 TOG_COLOR_REFS_REMOTES,
5691 get_color_value("TOG_COLOR_REFS_REMOTES"));
5692 if (err)
5693 goto done;
5696 view->show = show_ref_view;
5697 view->input = input_ref_view;
5698 view->close = close_ref_view;
5699 view->search_start = search_start_ref_view;
5700 view->search_next = search_next_ref_view;
5701 done:
5702 if (err)
5703 free_colors(&s->colors);
5704 return err;
5707 static const struct got_error *
5708 close_ref_view(struct tog_view *view)
5710 struct tog_ref_view_state *s = &view->state.ref;
5712 ref_view_free_refs(s);
5713 free_colors(&s->colors);
5715 return NULL;
5718 static const struct got_error *
5719 resolve_reflist_entry(struct got_object_id **commit_id,
5720 struct tog_reflist_entry *re, struct got_repository *repo)
5722 const struct got_error *err = NULL;
5723 struct got_object_id *obj_id;
5724 struct got_tag_object *tag = NULL;
5725 int obj_type;
5727 *commit_id = NULL;
5729 err = got_ref_resolve(&obj_id, repo, re->ref);
5730 if (err)
5731 return err;
5733 err = got_object_get_type(&obj_type, repo, obj_id);
5734 if (err)
5735 goto done;
5737 switch (obj_type) {
5738 case GOT_OBJ_TYPE_COMMIT:
5739 *commit_id = obj_id;
5740 break;
5741 case GOT_OBJ_TYPE_TAG:
5742 err = got_object_open_as_tag(&tag, repo, obj_id);
5743 if (err)
5744 goto done;
5745 free(obj_id);
5746 err = got_object_get_type(&obj_type, repo,
5747 got_object_tag_get_object_id(tag));
5748 if (err)
5749 goto done;
5750 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5751 err = got_error(GOT_ERR_OBJ_TYPE);
5752 goto done;
5754 *commit_id = got_object_id_dup(
5755 got_object_tag_get_object_id(tag));
5756 if (*commit_id == NULL) {
5757 err = got_error_from_errno("got_object_id_dup");
5758 goto done;
5760 break;
5761 default:
5762 err = got_error(GOT_ERR_OBJ_TYPE);
5763 break;
5766 done:
5767 if (tag)
5768 got_object_tag_close(tag);
5769 if (err) {
5770 free(*commit_id);
5771 *commit_id = NULL;
5773 return err;
5776 static const struct got_error *
5777 log_ref_entry(struct tog_view **new_view, int begin_x,
5778 struct tog_reflist_entry *re, struct got_repository *repo)
5780 struct tog_view *log_view;
5781 const struct got_error *err = NULL;
5782 struct got_object_id *commit_id = NULL;
5784 *new_view = NULL;
5786 err = resolve_reflist_entry(&commit_id, re, repo);
5787 if (err) {
5788 if (err->code != GOT_ERR_OBJ_TYPE)
5789 return err;
5790 else
5791 return NULL;
5794 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5795 if (log_view == NULL) {
5796 err = got_error_from_errno("view_open");
5797 goto done;
5800 err = open_log_view(log_view, commit_id, repo,
5801 got_ref_get_name(re->ref), "", 0);
5802 done:
5803 if (err)
5804 view_close(log_view);
5805 else
5806 *new_view = log_view;
5807 free(commit_id);
5808 return err;
5811 static void
5812 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5814 struct tog_reflist_entry *re;
5815 int i = 0;
5817 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5818 return;
5820 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5821 while (i++ < maxscroll) {
5822 if (re == NULL)
5823 break;
5824 s->first_displayed_entry = re;
5825 re = TAILQ_PREV(re, tog_reflist_head, entry);
5829 static void
5830 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5832 struct tog_reflist_entry *next, *last;
5833 int n = 0;
5835 if (s->first_displayed_entry)
5836 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5837 else
5838 next = TAILQ_FIRST(&s->refs);
5840 last = s->last_displayed_entry;
5841 while (next && last && n++ < maxscroll) {
5842 last = TAILQ_NEXT(last, entry);
5843 if (last) {
5844 s->first_displayed_entry = next;
5845 next = TAILQ_NEXT(next, entry);
5850 static const struct got_error *
5851 search_start_ref_view(struct tog_view *view)
5853 struct tog_ref_view_state *s = &view->state.ref;
5855 s->matched_entry = NULL;
5856 return NULL;
5859 static int
5860 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5862 regmatch_t regmatch;
5864 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5865 0) == 0;
5868 static const struct got_error *
5869 search_next_ref_view(struct tog_view *view)
5871 struct tog_ref_view_state *s = &view->state.ref;
5872 struct tog_reflist_entry *re = NULL;
5874 if (!view->searching) {
5875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5876 return NULL;
5879 if (s->matched_entry) {
5880 if (view->searching == TOG_SEARCH_FORWARD) {
5881 if (s->selected_entry)
5882 re = TAILQ_NEXT(s->selected_entry, entry);
5883 else
5884 re = TAILQ_PREV(s->selected_entry,
5885 tog_reflist_head, entry);
5886 } else {
5887 if (s->selected_entry == NULL)
5888 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5889 else
5890 re = TAILQ_PREV(s->selected_entry,
5891 tog_reflist_head, entry);
5893 } else {
5894 if (view->searching == TOG_SEARCH_FORWARD)
5895 re = TAILQ_FIRST(&s->refs);
5896 else
5897 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5900 while (1) {
5901 if (re == NULL) {
5902 if (s->matched_entry == NULL) {
5903 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5904 return NULL;
5906 if (view->searching == TOG_SEARCH_FORWARD)
5907 re = TAILQ_FIRST(&s->refs);
5908 else
5909 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5912 if (match_reflist_entry(re, &view->regex)) {
5913 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5914 s->matched_entry = re;
5915 break;
5918 if (view->searching == TOG_SEARCH_FORWARD)
5919 re = TAILQ_NEXT(re, entry);
5920 else
5921 re = TAILQ_PREV(re, tog_reflist_head, entry);
5924 if (s->matched_entry) {
5925 s->first_displayed_entry = s->matched_entry;
5926 s->selected = 0;
5929 return NULL;
5932 static const struct got_error *
5933 show_ref_view(struct tog_view *view)
5935 const struct got_error *err = NULL;
5936 struct tog_ref_view_state *s = &view->state.ref;
5937 struct tog_reflist_entry *re;
5938 char *line = NULL;
5939 wchar_t *wline;
5940 struct tog_color *tc;
5941 int width, n;
5942 int limit = view->nlines;
5944 werase(view->window);
5946 s->ndisplayed = 0;
5948 if (limit == 0)
5949 return NULL;
5951 re = s->first_displayed_entry;
5953 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5954 s->nrefs) == -1)
5955 return got_error_from_errno("asprintf");
5957 err = format_line(&wline, &width, line, view->ncols, 0);
5958 if (err) {
5959 free(line);
5960 return err;
5962 if (view_needs_focus_indication(view))
5963 wstandout(view->window);
5964 waddwstr(view->window, wline);
5965 if (view_needs_focus_indication(view))
5966 wstandend(view->window);
5967 free(wline);
5968 wline = NULL;
5969 free(line);
5970 line = NULL;
5971 if (width < view->ncols - 1)
5972 waddch(view->window, '\n');
5973 if (--limit <= 0)
5974 return NULL;
5976 n = 0;
5977 while (re && limit > 0) {
5978 char *line = NULL;
5980 if (got_ref_is_symbolic(re->ref)) {
5981 if (asprintf(&line, "%s -> %s",
5982 got_ref_get_name(re->ref),
5983 got_ref_get_symref_target(re->ref)) == -1)
5984 return got_error_from_errno("asprintf");
5985 } else if (s->show_ids) {
5986 struct got_object_id *id;
5987 char *id_str;
5988 err = got_ref_resolve(&id, s->repo, re->ref);
5989 if (err)
5990 return err;
5991 err = got_object_id_str(&id_str, id);
5992 if (err) {
5993 free(id);
5994 return err;
5996 if (asprintf(&line, "%s: %s",
5997 got_ref_get_name(re->ref), id_str) == -1) {
5998 err = got_error_from_errno("asprintf");
5999 free(id);
6000 free(id_str);
6001 return err;
6003 free(id);
6004 free(id_str);
6005 } else {
6006 line = strdup(got_ref_get_name(re->ref));
6007 if (line == NULL)
6008 return got_error_from_errno("strdup");
6011 err = format_line(&wline, &width, line, view->ncols, 0);
6012 if (err) {
6013 free(line);
6014 return err;
6016 if (n == s->selected) {
6017 if (view->focussed)
6018 wstandout(view->window);
6019 s->selected_entry = re;
6021 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6022 if (tc)
6023 wattr_on(view->window,
6024 COLOR_PAIR(tc->colorpair), NULL);
6025 waddwstr(view->window, wline);
6026 if (tc)
6027 wattr_off(view->window,
6028 COLOR_PAIR(tc->colorpair), NULL);
6029 if (width < view->ncols - 1)
6030 waddch(view->window, '\n');
6031 if (n == s->selected && view->focussed)
6032 wstandend(view->window);
6033 free(line);
6034 free(wline);
6035 wline = NULL;
6036 n++;
6037 s->ndisplayed++;
6038 s->last_displayed_entry = re;
6040 limit--;
6041 re = TAILQ_NEXT(re, entry);
6044 view_vborder(view);
6045 return err;
6048 static const struct got_error *
6049 browse_ref_tree(struct tog_view **new_view, int begin_x,
6050 struct tog_reflist_entry *re, struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6054 struct got_tree_object *tree = NULL;
6055 struct tog_view *tree_view;
6057 *new_view = NULL;
6059 err = resolve_reflist_entry(&commit_id, re, repo);
6060 if (err) {
6061 if (err->code != GOT_ERR_OBJ_TYPE)
6062 return err;
6063 else
6064 return NULL;
6067 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6068 if (err)
6069 goto done;
6071 err = got_object_open_as_tree(&tree, repo, tree_id);
6072 if (err)
6073 goto done;
6075 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6076 if (tree_view == NULL) {
6077 err = got_error_from_errno("view_open");
6078 goto done;
6081 err = open_tree_view(tree_view, tree, commit_id,
6082 got_ref_get_name(re->ref), repo);
6083 if (err)
6084 goto done;
6086 *new_view = tree_view;
6087 done:
6088 free(commit_id);
6089 free(tree_id);
6090 if (err) {
6091 if (tree)
6092 got_object_tree_close(tree);
6094 return err;
6096 static const struct got_error *
6097 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6099 const struct got_error *err = NULL;
6100 struct tog_ref_view_state *s = &view->state.ref;
6101 struct tog_view *log_view, *tree_view;
6102 int begin_x = 0;
6104 switch (ch) {
6105 case 'i':
6106 s->show_ids = !s->show_ids;
6107 break;
6108 case KEY_ENTER:
6109 case '\r':
6110 if (!s->selected_entry)
6111 break;
6112 if (view_is_parent_view(view))
6113 begin_x = view_split_begin_x(view->begin_x);
6114 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6115 s->repo);
6116 view->focussed = 0;
6117 log_view->focussed = 1;
6118 if (view_is_parent_view(view)) {
6119 err = view_close_child(view);
6120 if (err)
6121 return err;
6122 view_set_child(view, log_view);
6123 view->focus_child = 1;
6124 } else
6125 *new_view = log_view;
6126 break;
6127 case 't':
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 = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6133 s->repo);
6134 if (err || tree_view == NULL)
6135 break;
6136 view->focussed = 0;
6137 tree_view->focussed = 1;
6138 if (view_is_parent_view(view)) {
6139 err = view_close_child(view);
6140 if (err)
6141 return err;
6142 view_set_child(view, tree_view);
6143 view->focus_child = 1;
6144 } else
6145 *new_view = tree_view;
6146 break;
6147 case 'k':
6148 case KEY_UP:
6149 if (s->selected > 0) {
6150 s->selected--;
6151 break;
6153 ref_scroll_up(s, 1);
6154 break;
6155 case KEY_PPAGE:
6156 case CTRL('b'):
6157 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6158 s->selected = 0;
6159 ref_scroll_up(s, MAX(0, view->nlines - 1));
6160 break;
6161 case 'j':
6162 case KEY_DOWN:
6163 if (s->selected < s->ndisplayed - 1) {
6164 s->selected++;
6165 break;
6167 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6168 /* can't scroll any further */
6169 break;
6170 ref_scroll_down(s, 1);
6171 break;
6172 case KEY_NPAGE:
6173 case CTRL('f'):
6174 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6175 /* can't scroll any further; move cursor down */
6176 if (s->selected < s->ndisplayed - 1)
6177 s->selected = s->ndisplayed - 1;
6178 break;
6180 ref_scroll_down(s, view->nlines - 1);
6181 break;
6182 case CTRL('l'):
6183 ref_view_free_refs(s);
6184 err = ref_view_load_refs(s);
6185 break;
6186 case KEY_RESIZE:
6187 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6188 s->selected = view->nlines - 2;
6189 break;
6190 default:
6191 break;
6194 return err;
6197 __dead static void
6198 usage_ref(void)
6200 endwin();
6201 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6202 getprogname());
6203 exit(1);
6206 static const struct got_error *
6207 cmd_ref(int argc, char *argv[])
6209 const struct got_error *error;
6210 struct got_repository *repo = NULL;
6211 struct got_worktree *worktree = NULL;
6212 char *cwd = NULL, *repo_path = NULL;
6213 int ch;
6214 struct tog_view *view;
6216 while ((ch = getopt(argc, argv, "r:")) != -1) {
6217 switch (ch) {
6218 case 'r':
6219 repo_path = realpath(optarg, NULL);
6220 if (repo_path == NULL)
6221 return got_error_from_errno2("realpath",
6222 optarg);
6223 break;
6224 default:
6225 usage_ref();
6226 /* NOTREACHED */
6230 argc -= optind;
6231 argv += optind;
6233 if (argc > 1)
6234 usage_ref();
6236 if (repo_path == NULL) {
6237 cwd = getcwd(NULL, 0);
6238 if (cwd == NULL)
6239 return got_error_from_errno("getcwd");
6240 error = got_worktree_open(&worktree, cwd);
6241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6242 goto done;
6243 if (worktree)
6244 repo_path =
6245 strdup(got_worktree_get_repo_path(worktree));
6246 else
6247 repo_path = strdup(cwd);
6248 if (repo_path == NULL) {
6249 error = got_error_from_errno("strdup");
6250 goto done;
6254 error = got_repo_open(&repo, repo_path, NULL);
6255 if (error != NULL)
6256 goto done;
6258 init_curses();
6260 error = apply_unveil(got_repo_get_path(repo), NULL);
6261 if (error)
6262 goto done;
6264 error = tog_load_refs(repo);
6265 if (error)
6266 goto done;
6268 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6269 if (view == NULL) {
6270 error = got_error_from_errno("view_open");
6271 goto done;
6274 error = open_ref_view(view, repo);
6275 if (error)
6276 goto done;
6278 if (worktree) {
6279 /* Release work tree lock. */
6280 got_worktree_close(worktree);
6281 worktree = NULL;
6283 error = view_loop(view);
6284 done:
6285 free(repo_path);
6286 free(cwd);
6287 if (repo)
6288 got_repo_close(repo);
6289 tog_free_refs();
6290 return error;
6293 static void
6294 list_commands(FILE *fp)
6296 size_t i;
6298 fprintf(fp, "commands:");
6299 for (i = 0; i < nitems(tog_commands); i++) {
6300 struct tog_cmd *cmd = &tog_commands[i];
6301 fprintf(fp, " %s", cmd->name);
6303 fputc('\n', fp);
6306 __dead static void
6307 usage(int hflag, int status)
6309 FILE *fp = (status == 0) ? stdout : stderr;
6311 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6312 getprogname());
6313 if (hflag) {
6314 fprintf(fp, "lazy usage: %s path\n", getprogname());
6315 list_commands(fp);
6317 exit(status);
6320 static char **
6321 make_argv(int argc, ...)
6323 va_list ap;
6324 char **argv;
6325 int i;
6327 va_start(ap, argc);
6329 argv = calloc(argc, sizeof(char *));
6330 if (argv == NULL)
6331 err(1, "calloc");
6332 for (i = 0; i < argc; i++) {
6333 argv[i] = strdup(va_arg(ap, char *));
6334 if (argv[i] == NULL)
6335 err(1, "strdup");
6338 va_end(ap);
6339 return argv;
6343 * Try to convert 'tog path' into a 'tog log path' command.
6344 * The user could simply have mistyped the command rather than knowingly
6345 * provided a path. So check whether argv[0] can in fact be resolved
6346 * to a path in the HEAD commit and print a special error if not.
6347 * This hack is for mpi@ <3
6349 static const struct got_error *
6350 tog_log_with_path(int argc, char *argv[])
6352 const struct got_error *error = NULL;
6353 struct tog_cmd *cmd = NULL;
6354 struct got_repository *repo = NULL;
6355 struct got_worktree *worktree = NULL;
6356 struct got_object_id *commit_id = NULL, *id = NULL;
6357 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6358 char *commit_id_str = NULL, **cmd_argv = NULL;
6360 cwd = getcwd(NULL, 0);
6361 if (cwd == NULL)
6362 return got_error_from_errno("getcwd");
6364 error = got_worktree_open(&worktree, cwd);
6365 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6366 goto done;
6368 if (worktree)
6369 repo_path = strdup(got_worktree_get_repo_path(worktree));
6370 else
6371 repo_path = strdup(cwd);
6372 if (repo_path == NULL) {
6373 error = got_error_from_errno("strdup");
6374 goto done;
6377 error = got_repo_open(&repo, repo_path, NULL);
6378 if (error != NULL)
6379 goto done;
6381 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6382 repo, worktree);
6383 if (error)
6384 goto done;
6386 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6387 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6388 GOT_OBJ_TYPE_COMMIT, 1, repo);
6389 if (error)
6390 goto done;
6392 if (worktree) {
6393 got_worktree_close(worktree);
6394 worktree = NULL;
6397 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6398 if (error) {
6399 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6400 goto done;
6401 fprintf(stderr, "%s: '%s' is no known command or path\n",
6402 getprogname(), argv[0]);
6403 usage(1, 1);
6404 /* not reached */
6407 got_repo_close(repo);
6408 repo = NULL;
6410 error = got_object_id_str(&commit_id_str, commit_id);
6411 if (error)
6412 goto done;
6414 cmd = &tog_commands[0]; /* log */
6415 argc = 4;
6416 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6417 error = cmd->cmd_main(argc, cmd_argv);
6418 done:
6419 if (repo)
6420 got_repo_close(repo);
6421 if (worktree)
6422 got_worktree_close(worktree);
6423 free(id);
6424 free(commit_id_str);
6425 free(commit_id);
6426 free(cwd);
6427 free(repo_path);
6428 free(in_repo_path);
6429 if (cmd_argv) {
6430 int i;
6431 for (i = 0; i < argc; i++)
6432 free(cmd_argv[i]);
6433 free(cmd_argv);
6435 return error;
6438 int
6439 main(int argc, char *argv[])
6441 const struct got_error *error = NULL;
6442 struct tog_cmd *cmd = NULL;
6443 int ch, hflag = 0, Vflag = 0;
6444 char **cmd_argv = NULL;
6445 static struct option longopts[] = {
6446 { "version", no_argument, NULL, 'V' },
6447 { NULL, 0, NULL, 0}
6450 setlocale(LC_CTYPE, "");
6452 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6453 switch (ch) {
6454 case 'h':
6455 hflag = 1;
6456 break;
6457 case 'V':
6458 Vflag = 1;
6459 break;
6460 default:
6461 usage(hflag, 1);
6462 /* NOTREACHED */
6466 argc -= optind;
6467 argv += optind;
6468 optind = 1;
6469 optreset = 1;
6471 if (Vflag) {
6472 got_version_print_str();
6473 return 0;
6476 #ifndef PROFILE
6477 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6478 NULL) == -1)
6479 err(1, "pledge");
6480 #endif
6482 if (argc == 0) {
6483 if (hflag)
6484 usage(hflag, 0);
6485 /* Build an argument vector which runs a default command. */
6486 cmd = &tog_commands[0];
6487 argc = 1;
6488 cmd_argv = make_argv(argc, cmd->name);
6489 } else {
6490 size_t i;
6492 /* Did the user specify a command? */
6493 for (i = 0; i < nitems(tog_commands); i++) {
6494 if (strncmp(tog_commands[i].name, argv[0],
6495 strlen(argv[0])) == 0) {
6496 cmd = &tog_commands[i];
6497 break;
6502 if (cmd == NULL) {
6503 if (argc != 1)
6504 usage(0, 1);
6505 /* No command specified; try log with a path */
6506 error = tog_log_with_path(argc, argv);
6507 } else {
6508 if (hflag)
6509 cmd->cmd_usage();
6510 else
6511 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6514 endwin();
6515 putchar('\n');
6516 if (cmd_argv) {
6517 int i;
6518 for (i = 0; i < argc; i++)
6519 free(cmd_argv[i]);
6520 free(cmd_argv);
6523 if (error && error->code != GOT_ERR_CANCELLED)
6524 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6525 return 0;