Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 SIMPLEQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 SIMPLEQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
137 if (err)
138 return err;
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
141 repo);
144 static void
145 tog_free_refs(void)
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
160 int regerr = 0;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
163 return NULL;
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
168 if (tc == NULL)
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 if (regerr) {
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
176 sizeof(regerr_msg));
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 regerr_msg);
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
180 free(tc);
181 return err;
183 tc->colorpair = idx;
184 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
185 return NULL;
188 static void
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!SIMPLEQ_EMPTY(colors)) {
194 tc = SIMPLEQ_FIRST(colors);
195 SIMPLEQ_REMOVE_HEAD(colors, entry);
196 regfree(&tc->regex);
197 free(tc);
201 struct tog_color *
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 SIMPLEQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
208 return tc;
211 return NULL;
214 static int
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 return COLOR_CYAN;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 return COLOR_YELLOW;
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 return COLOR_GREEN;
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 return COLOR_CYAN;
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 return COLOR_GREEN;
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 return COLOR_CYAN;
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 return COLOR_YELLOW;
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 return COLOR_GREEN;
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 return COLOR_YELLOW;
246 return -1;
249 static int
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
254 if (val == NULL)
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
258 return COLOR_BLACK;
259 if (strcasecmp(val, "red") == 0)
260 return COLOR_RED;
261 if (strcasecmp(val, "green") == 0)
262 return COLOR_GREEN;
263 if (strcasecmp(val, "yellow") == 0)
264 return COLOR_YELLOW;
265 if (strcasecmp(val, "blue") == 0)
266 return COLOR_BLUE;
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
270 return COLOR_CYAN;
271 if (strcasecmp(val, "white") == 0)
272 return COLOR_WHITE;
273 if (strcasecmp(val, "default") == 0)
274 return -1;
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
283 FILE *f;
284 int first_displayed_line;
285 int last_displayed_line;
286 int eof;
287 int diff_context;
288 int ignore_whitespace;
289 int force_text_diff;
290 struct got_repository *repo;
291 struct tog_colors colors;
292 size_t nlines;
293 off_t *line_offsets;
294 int matched_line;
295 int selected_line;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
299 };
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
306 int commits_needed;
307 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
312 int log_complete;
313 sig_atomic_t *quit;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
316 int *searching;
317 int *search_next_done;
318 regex_t *regex;
319 };
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
326 int selected;
327 char *in_repo_path;
328 char *head_ref_name;
329 int log_branches;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
332 sig_atomic_t quit;
333 pthread_t thread;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
338 };
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
357 int nlines;
359 struct tog_view *view;
360 struct got_object_id *commit_id;
361 int *quit;
362 };
364 struct tog_blame_thread_args {
365 const char *path;
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
368 int *complete;
369 got_cancel_cb cancel_cb;
370 void *cancel_arg;
371 };
373 struct tog_blame {
374 FILE *f;
375 off_t filesize;
376 struct tog_blame_line *lines;
377 int nlines;
378 off_t *line_offsets;
379 pthread_t thread;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
382 const char *path;
383 };
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
388 int selected_line;
389 int blame_complete;
390 int eof;
391 int done;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
394 char *path;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
398 int matched_line;
399 struct tog_colors colors;
400 };
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
407 int selected;
408 };
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
413 char *tree_label;
414 struct got_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
422 char *head_ref_name;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
426 };
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
431 int idx;
432 };
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
445 };
447 /*
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
463 */
464 TAILQ_HEAD(tog_view_list_head, tog_view);
466 struct tog_view {
467 TAILQ_ENTRY(tog_view) entry;
468 WINDOW *window;
469 PANEL *panel;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
473 int dying;
474 struct tog_view *parent;
475 struct tog_view *child;
477 /*
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
484 * situations.
485 */
486 int focus_child;
488 /* type-specific state */
489 enum tog_view_type type;
490 union {
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
496 } state;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
505 int search_started;
506 int searching;
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
513 regex_t regex;
514 regmatch_t regmatch;
515 };
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_tree_object *, struct got_object_id *, const char *,
549 struct got_repository *);
550 static const struct got_error *show_tree_view(struct tog_view *);
551 static const struct got_error *input_tree_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error *close_tree_view(struct tog_view *);
554 static const struct got_error *search_start_tree_view(struct tog_view *);
555 static const struct got_error *search_next_tree_view(struct tog_view *);
557 static const struct got_error *open_ref_view(struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_ref_view(struct tog_view *);
560 static const struct got_error *input_ref_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error *close_ref_view(struct tog_view *);
563 static const struct got_error *search_start_ref_view(struct tog_view *);
564 static const struct got_error *search_next_ref_view(struct tog_view *);
566 static volatile sig_atomic_t tog_sigwinch_received;
567 static volatile sig_atomic_t tog_sigpipe_received;
568 static volatile sig_atomic_t tog_sigcont_received;
570 static void
571 tog_sigwinch(int signo)
573 tog_sigwinch_received = 1;
576 static void
577 tog_sigpipe(int signo)
579 tog_sigpipe_received = 1;
582 static void
583 tog_sigcont(int signo)
585 tog_sigcont_received = 1;
588 static const struct got_error *
589 view_close(struct tog_view *view)
591 const struct got_error *err = NULL;
593 if (view->child) {
594 view_close(view->child);
595 view->child = NULL;
597 if (view->close)
598 err = view->close(view);
599 if (view->panel)
600 del_panel(view->panel);
601 if (view->window)
602 delwin(view->window);
603 free(view);
604 return err;
607 static struct tog_view *
608 view_open(int nlines, int ncols, int begin_y, int begin_x,
609 enum tog_view_type type)
611 struct tog_view *view = calloc(1, sizeof(*view));
613 if (view == NULL)
614 return NULL;
616 view->type = type;
617 view->lines = LINES;
618 view->cols = COLS;
619 view->nlines = nlines ? nlines : LINES - begin_y;
620 view->ncols = ncols ? ncols : COLS - begin_x;
621 view->begin_y = begin_y;
622 view->begin_x = begin_x;
623 view->window = newwin(nlines, ncols, begin_y, begin_x);
624 if (view->window == NULL) {
625 view_close(view);
626 return NULL;
628 view->panel = new_panel(view->window);
629 if (view->panel == NULL ||
630 set_panel_userptr(view->panel, view) != OK) {
631 view_close(view);
632 return NULL;
635 keypad(view->window, TRUE);
636 return view;
639 static int
640 view_split_begin_x(int begin_x)
642 if (begin_x > 0 || COLS < 120)
643 return 0;
644 return (COLS - MAX(COLS / 2, 80));
647 static const struct got_error *view_resize(struct tog_view *);
649 static const struct got_error *
650 view_splitscreen(struct tog_view *view)
652 const struct got_error *err = NULL;
654 view->begin_y = 0;
655 view->begin_x = view_split_begin_x(0);
656 view->nlines = LINES;
657 view->ncols = COLS - view->begin_x;
658 view->lines = LINES;
659 view->cols = COLS;
660 err = view_resize(view);
661 if (err)
662 return err;
664 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 return got_error_from_errno("mvwin");
667 return NULL;
670 static const struct got_error *
671 view_fullscreen(struct tog_view *view)
673 const struct got_error *err = NULL;
675 view->begin_x = 0;
676 view->begin_y = 0;
677 view->nlines = LINES;
678 view->ncols = COLS;
679 view->lines = LINES;
680 view->cols = COLS;
681 err = view_resize(view);
682 if (err)
683 return err;
685 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 return got_error_from_errno("mvwin");
688 return NULL;
691 static int
692 view_is_parent_view(struct tog_view *view)
694 return view->parent == NULL;
697 static const struct got_error *
698 view_resize(struct tog_view *view)
700 int nlines, ncols;
702 if (view->lines > LINES)
703 nlines = view->nlines - (view->lines - LINES);
704 else
705 nlines = view->nlines + (LINES - view->lines);
707 if (view->cols > COLS)
708 ncols = view->ncols - (view->cols - COLS);
709 else
710 ncols = view->ncols + (COLS - view->cols);
712 if (wresize(view->window, nlines, ncols) == ERR)
713 return got_error_from_errno("wresize");
714 if (replace_panel(view->panel, view->window) == ERR)
715 return got_error_from_errno("replace_panel");
716 wclear(view->window);
718 view->nlines = nlines;
719 view->ncols = ncols;
720 view->lines = LINES;
721 view->cols = COLS;
723 if (view->child) {
724 view->child->begin_x = view_split_begin_x(view->begin_x);
725 if (view->child->begin_x == 0) {
726 view_fullscreen(view->child);
727 if (view->child->focussed)
728 show_panel(view->child->panel);
729 else
730 show_panel(view->panel);
731 } else {
732 view_splitscreen(view->child);
733 show_panel(view->child->panel);
737 return NULL;
740 static const struct got_error *
741 view_close_child(struct tog_view *view)
743 const struct got_error *err = NULL;
745 if (view->child == NULL)
746 return NULL;
748 err = view_close(view->child);
749 view->child = NULL;
750 return err;
753 static void
754 view_set_child(struct tog_view *view, struct tog_view *child)
756 view->child = child;
757 child->parent = view;
760 static int
761 view_is_splitscreen(struct tog_view *view)
763 return view->begin_x > 0;
766 static void
767 tog_resizeterm(void)
769 int cols, lines;
770 struct winsize size;
772 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 cols = 80; /* Default */
774 lines = 24;
775 } else {
776 cols = size.ws_col;
777 lines = size.ws_row;
779 resize_term(lines, cols);
782 static const struct got_error *
783 view_search_start(struct tog_view *view)
785 const struct got_error *err = NULL;
786 char pattern[1024];
787 int ret;
789 if (view->search_started) {
790 regfree(&view->regex);
791 view->searching = 0;
792 memset(&view->regmatch, 0, sizeof(view->regmatch));
794 view->search_started = 0;
796 if (view->nlines < 1)
797 return NULL;
799 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 wclrtoeol(view->window);
802 nocbreak();
803 echo();
804 ret = wgetnstr(view->window, pattern, sizeof(pattern));
805 cbreak();
806 noecho();
807 if (ret == ERR)
808 return NULL;
810 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 err = view->search_start(view);
812 if (err) {
813 regfree(&view->regex);
814 return err;
816 view->search_started = 1;
817 view->searching = TOG_SEARCH_FORWARD;
818 view->search_next_done = 0;
819 view->search_next(view);
822 return NULL;
825 static const struct got_error *
826 view_input(struct tog_view **new, int *done, struct tog_view *view,
827 struct tog_view_list_head *views)
829 const struct got_error *err = NULL;
830 struct tog_view *v;
831 int ch, errcode;
833 *new = NULL;
835 /* Clear "no matches" indicator. */
836 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 if (view->searching && !view->search_next_done) {
841 errcode = pthread_mutex_unlock(&tog_mutex);
842 if (errcode)
843 return got_error_set_errno(errcode,
844 "pthread_mutex_unlock");
845 pthread_yield();
846 errcode = pthread_mutex_lock(&tog_mutex);
847 if (errcode)
848 return got_error_set_errno(errcode,
849 "pthread_mutex_lock");
850 view->search_next(view);
851 return NULL;
854 nodelay(stdscr, FALSE);
855 /* Allow threads to make progress while we are waiting for input. */
856 errcode = pthread_mutex_unlock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 ch = wgetch(view->window);
860 errcode = pthread_mutex_lock(&tog_mutex);
861 if (errcode)
862 return got_error_set_errno(errcode, "pthread_mutex_lock");
863 nodelay(stdscr, TRUE);
865 if (tog_sigwinch_received || tog_sigcont_received) {
866 tog_resizeterm();
867 tog_sigwinch_received = 0;
868 tog_sigcont_received = 0;
869 TAILQ_FOREACH(v, views, entry) {
870 err = view_resize(v);
871 if (err)
872 return err;
873 err = v->input(new, v, KEY_RESIZE);
874 if (err)
875 return err;
876 if (v->child) {
877 err = view_resize(v->child);
878 if (err)
879 return err;
880 err = v->child->input(new, v->child,
881 KEY_RESIZE);
882 if (err)
883 return err;
888 switch (ch) {
889 case ERR:
890 break;
891 case '\t':
892 if (view->child) {
893 view->focussed = 0;
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
897 view->focussed = 0;
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
901 break;
902 case 'q':
903 err = view->input(new, view, ch);
904 view->dying = 1;
905 break;
906 case 'Q':
907 *done = 1;
908 break;
909 case 'f':
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
912 break;
913 if (view_is_splitscreen(view->child)) {
914 view->focussed = 0;
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
917 } else
918 err = view_splitscreen(view->child);
919 if (err)
920 break;
921 err = view->child->input(new, view->child,
922 KEY_RESIZE);
923 } else {
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
926 view->focussed = 1;
927 err = view_fullscreen(view);
928 } else {
929 err = view_splitscreen(view);
931 if (err)
932 break;
933 err = view->input(new, view, KEY_RESIZE);
935 break;
936 case KEY_RESIZE:
937 break;
938 case '/':
939 if (view->search_start)
940 view_search_start(view);
941 else
942 err = view->input(new, view, ch);
943 break;
944 case 'N':
945 case 'n':
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
951 } else
952 err = view->input(new, view, ch);
953 break;
954 default:
955 err = view->input(new, view, ch);
956 break;
959 return err;
962 void
963 view_vborder(struct tog_view *view)
965 PANEL *panel;
966 const struct tog_view *view_above;
968 if (view->parent)
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
972 if (panel == NULL)
973 return;
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 int
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
985 return 0;
986 if (!view_is_splitscreen(view->child))
987 return 0;
988 } else if (!view_is_splitscreen(view))
989 return 0;
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1004 if (errcode)
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 TAILQ_INIT(&views);
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1010 view->focussed = 1;
1011 err = view->show(view);
1012 if (err)
1013 return err;
1014 update_panels();
1015 doupdate();
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1022 if (err)
1023 break;
1024 if (view->dying) {
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1029 entry);
1030 else if (view->parent)
1031 prev = view->parent;
1033 if (view->parent) {
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1036 } else
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1040 if (err)
1041 goto done;
1043 view = NULL;
1044 TAILQ_FOREACH(v, &views, entry) {
1045 if (v->focussed)
1046 break;
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1050 if (prev)
1051 view = prev;
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1056 if (view) {
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1059 view = view->child;
1060 } else
1061 view->focussed = 1;
1065 if (new_view) {
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1070 continue;
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1073 if (err)
1074 goto done;
1075 break;
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1078 view = new_view;
1080 if (view) {
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1083 view = view->child;
1084 } else {
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1093 if (err)
1094 goto done;
1096 err = view->show(view);
1097 if (err)
1098 goto done;
1099 if (view->child) {
1100 err = view->child->show(view->child);
1101 if (err)
1102 goto done;
1104 update_panels();
1105 doupdate();
1108 done:
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1112 view_close(view);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1119 return err;
1122 __dead static void
1123 usage_log(void)
1125 endwin();
1126 fprintf(stderr,
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 getprogname());
1129 exit(1);
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 char *vis = NULL;
1137 const struct got_error *err = NULL;
1139 *ws = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1142 int vislen;
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1148 if (err)
1149 return err;
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1153 goto done;
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1158 if (*ws == NULL) {
1159 err = got_error_from_errno("calloc");
1160 goto done;
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1165 done:
1166 free(vis);
1167 if (err) {
1168 free(*ws);
1169 *ws = NULL;
1170 *wlen = 0;
1172 return err;
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 int col_tab_align)
1180 const struct got_error *err = NULL;
1181 int cols = 0;
1182 wchar_t *wline = NULL;
1183 size_t wlen;
1184 int i;
1186 *wlinep = NULL;
1187 *widthp = 0;
1189 err = mbs2ws(&wline, &wlen, line);
1190 if (err)
1191 return err;
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1195 wlen--;
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1199 wlen--;
1202 i = 0;
1203 while (i < wlen) {
1204 int width = wcwidth(wline[i]);
1206 if (width == 0) {
1207 i++;
1208 continue;
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1213 break;
1214 cols += width;
1215 i++;
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1218 width = TABSIZE -
1219 ((cols + col_tab_align) % TABSIZE);
1220 } else {
1221 width = 1;
1222 wline[i] = L'.';
1224 if (cols + width > wlimit)
1225 break;
1226 cols += width;
1227 i++;
1228 } else {
1229 err = got_error_from_errno("wcwidth");
1230 goto done;
1233 wline[i] = L'\0';
1234 if (widthp)
1235 *widthp = cols;
1236 done:
1237 if (err)
1238 free(wline);
1239 else
1240 *wlinep = wline;
1241 return err;
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1250 char *s;
1251 const char *name;
1253 *refs_str = NULL;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1258 int cmp;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1262 continue;
1263 if (strncmp(name, "refs/", 5) == 0)
1264 name += 5;
1265 if (strncmp(name, "got/", 4) == 0)
1266 continue;
1267 if (strncmp(name, "heads/", 6) == 0)
1268 name += 6;
1269 if (strncmp(name, "remotes/", 8) == 0) {
1270 name += 8;
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1273 continue;
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1276 if (err)
1277 break;
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1280 if (err) {
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1282 free(ref_id);
1283 break;
1285 /* Ref points at something other than a tag. */
1286 err = NULL;
1287 tag = NULL;
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1292 free(ref_id);
1293 if (tag)
1294 got_object_tag_close(tag);
1295 if (cmp != 0)
1296 continue;
1297 s = *refs_str;
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 free(s);
1302 *refs_str = NULL;
1303 break;
1305 free(s);
1308 return err;
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 int col_tab_align)
1315 char *smallerthan;
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1337 int col, limit;
1338 const int avail = view->ncols;
1339 struct tm tm;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (localtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("localtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 return got_error(GOT_ERR_NO_SPACE);
1349 if (avail <= date_display_cols)
1350 limit = MIN(sizeof(datebuf) - 1, avail);
1351 else
1352 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 if (tc)
1355 wattr_on(view->window,
1356 COLOR_PAIR(tc->colorpair), NULL);
1357 waddnstr(view->window, datebuf, limit);
1358 if (tc)
1359 wattr_off(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 col = limit;
1362 if (col > avail)
1363 goto done;
1365 if (avail >= 120) {
1366 char *id_str;
1367 err = got_object_id_str(&id_str, id);
1368 if (err)
1369 goto done;
1370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 if (tc)
1372 wattr_on(view->window,
1373 COLOR_PAIR(tc->colorpair), NULL);
1374 wprintw(view->window, "%.8s ", id_str);
1375 if (tc)
1376 wattr_off(view->window,
1377 COLOR_PAIR(tc->colorpair), NULL);
1378 free(id_str);
1379 col += 9;
1380 if (col > avail)
1381 goto done;
1384 author = strdup(got_object_commit_get_author(commit));
1385 if (author == NULL) {
1386 err = got_error_from_errno("strdup");
1387 goto done;
1389 err = format_author(&wauthor, &author_width, author, avail - col, col);
1390 if (err)
1391 goto done;
1392 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 if (tc)
1394 wattr_on(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 waddwstr(view->window, wauthor);
1397 if (tc)
1398 wattr_off(view->window,
1399 COLOR_PAIR(tc->colorpair), NULL);
1400 col += author_width;
1401 while (col < avail && author_width < author_display_cols + 2) {
1402 waddch(view->window, ' ');
1403 col++;
1404 author_width++;
1406 if (col > avail)
1407 goto done;
1409 err = got_object_commit_get_logmsg(&logmsg0, commit);
1410 if (err)
1411 goto done;
1412 logmsg = logmsg0;
1413 while (*logmsg == '\n')
1414 logmsg++;
1415 newline = strchr(logmsg, '\n');
1416 if (newline)
1417 *newline = '\0';
1418 limit = avail - col;
1419 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1420 if (err)
1421 goto done;
1422 waddwstr(view->window, wlogmsg);
1423 col += logmsg_width;
1424 while (col < avail) {
1425 waddch(view->window, ' ');
1426 col++;
1428 done:
1429 free(logmsg0);
1430 free(wlogmsg);
1431 free(author);
1432 free(wauthor);
1433 free(line);
1434 return err;
1437 static struct commit_queue_entry *
1438 alloc_commit_queue_entry(struct got_commit_object *commit,
1439 struct got_object_id *id)
1441 struct commit_queue_entry *entry;
1443 entry = calloc(1, sizeof(*entry));
1444 if (entry == NULL)
1445 return NULL;
1447 entry->id = id;
1448 entry->commit = commit;
1449 return entry;
1452 static void
1453 pop_commit(struct commit_queue *commits)
1455 struct commit_queue_entry *entry;
1457 entry = TAILQ_FIRST(&commits->head);
1458 TAILQ_REMOVE(&commits->head, entry, entry);
1459 got_object_commit_close(entry->commit);
1460 commits->ncommits--;
1461 /* Don't free entry->id! It is owned by the commit graph. */
1462 free(entry);
1465 static void
1466 free_commits(struct commit_queue *commits)
1468 while (!TAILQ_EMPTY(&commits->head))
1469 pop_commit(commits);
1472 static const struct got_error *
1473 match_commit(int *have_match, struct got_object_id *id,
1474 struct got_commit_object *commit, regex_t *regex)
1476 const struct got_error *err = NULL;
1477 regmatch_t regmatch;
1478 char *id_str = NULL, *logmsg = NULL;
1480 *have_match = 0;
1482 err = got_object_id_str(&id_str, id);
1483 if (err)
1484 return err;
1486 err = got_object_commit_get_logmsg(&logmsg, commit);
1487 if (err)
1488 goto done;
1490 if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 &regmatch, 0) == 0 ||
1492 regexec(regex, got_object_commit_get_committer(commit), 1,
1493 &regmatch, 0) == 0 ||
1494 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1495 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1496 *have_match = 1;
1497 done:
1498 free(id_str);
1499 free(logmsg);
1500 return err;
1503 static const struct got_error *
1504 queue_commits(struct tog_log_thread_args *a)
1506 const struct got_error *err = NULL;
1509 * We keep all commits open throughout the lifetime of the log
1510 * view in order to avoid having to re-fetch commits from disk
1511 * while updating the display.
1513 do {
1514 struct got_object_id *id;
1515 struct got_commit_object *commit;
1516 struct commit_queue_entry *entry;
1517 int errcode;
1519 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 NULL, NULL);
1521 if (err || id == NULL)
1522 break;
1524 err = got_object_open_as_commit(&commit, a->repo, id);
1525 if (err)
1526 break;
1527 entry = alloc_commit_queue_entry(commit, id);
1528 if (entry == NULL) {
1529 err = got_error_from_errno("alloc_commit_queue_entry");
1530 break;
1533 errcode = pthread_mutex_lock(&tog_mutex);
1534 if (errcode) {
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1537 break;
1540 entry->idx = a->commits->ncommits;
1541 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 a->commits->ncommits++;
1544 if (*a->searching == TOG_SEARCH_FORWARD &&
1545 !*a->search_next_done) {
1546 int have_match;
1547 err = match_commit(&have_match, id, commit, a->regex);
1548 if (err)
1549 break;
1550 if (have_match)
1551 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 errcode = pthread_mutex_unlock(&tog_mutex);
1555 if (errcode && err == NULL)
1556 err = got_error_set_errno(errcode,
1557 "pthread_mutex_unlock");
1558 if (err)
1559 break;
1560 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1562 return err;
1565 static void
1566 select_commit(struct tog_log_view_state *s)
1568 struct commit_queue_entry *entry;
1569 int ncommits = 0;
1571 entry = s->first_displayed_entry;
1572 while (entry) {
1573 if (ncommits == s->selected) {
1574 s->selected_entry = entry;
1575 break;
1577 entry = TAILQ_NEXT(entry, entry);
1578 ncommits++;
1582 static const struct got_error *
1583 draw_commits(struct tog_view *view)
1585 const struct got_error *err = NULL;
1586 struct tog_log_view_state *s = &view->state.log;
1587 struct commit_queue_entry *entry = s->selected_entry;
1588 const int limit = view->nlines;
1589 int width;
1590 int ncommits, author_cols = 4;
1591 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 char *refs_str = NULL;
1593 wchar_t *wline;
1594 struct tog_color *tc;
1595 static const size_t date_display_cols = 12;
1597 if (s->selected_entry &&
1598 !(view->searching && view->search_next_done == 0)) {
1599 struct got_reflist_head *refs;
1600 err = got_object_id_str(&id_str, s->selected_entry->id);
1601 if (err)
1602 return err;
1603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 s->selected_entry->id);
1605 if (refs) {
1606 err = build_refs_str(&refs_str, refs,
1607 s->selected_entry->id, s->repo);
1608 if (err)
1609 goto done;
1613 if (s->thread_args.commits_needed == 0)
1614 halfdelay(10); /* disable fast refresh */
1616 if (s->thread_args.commits_needed > 0) {
1617 if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 (view->searching && !view->search_next_done) ?
1620 "searching..." : "loading...") == -1) {
1621 err = got_error_from_errno("asprintf");
1622 goto done;
1624 } else {
1625 const char *search_str = NULL;
1627 if (view->searching) {
1628 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 search_str = "no more matches";
1630 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 search_str = "no matches found";
1632 else if (!view->search_next_done)
1633 search_str = "searching...";
1636 if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 search_str ? search_str :
1639 (refs_str ? refs_str : "")) == -1) {
1640 err = got_error_from_errno("asprintf");
1641 goto done;
1645 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 if (asprintf(&header, "commit %s %s%s",
1647 id_str ? id_str : "........................................",
1648 s->in_repo_path, ncommits_str) == -1) {
1649 err = got_error_from_errno("asprintf");
1650 header = NULL;
1651 goto done;
1653 } else if (asprintf(&header, "commit %s%s",
1654 id_str ? id_str : "........................................",
1655 ncommits_str) == -1) {
1656 err = got_error_from_errno("asprintf");
1657 header = NULL;
1658 goto done;
1660 err = format_line(&wline, &width, header, view->ncols, 0);
1661 if (err)
1662 goto done;
1664 werase(view->window);
1666 if (view_needs_focus_indication(view))
1667 wstandout(view->window);
1668 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 if (tc)
1670 wattr_on(view->window,
1671 COLOR_PAIR(tc->colorpair), NULL);
1672 waddwstr(view->window, wline);
1673 if (tc)
1674 wattr_off(view->window,
1675 COLOR_PAIR(tc->colorpair), NULL);
1676 while (width < view->ncols) {
1677 waddch(view->window, ' ');
1678 width++;
1680 if (view_needs_focus_indication(view))
1681 wstandend(view->window);
1682 free(wline);
1683 if (limit <= 1)
1684 goto done;
1686 /* Grow author column size if necessary. */
1687 entry = s->first_displayed_entry;
1688 ncommits = 0;
1689 while (entry) {
1690 char *author;
1691 wchar_t *wauthor;
1692 int width;
1693 if (ncommits >= limit - 1)
1694 break;
1695 author = strdup(got_object_commit_get_author(entry->commit));
1696 if (author == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 err = format_author(&wauthor, &width, author, COLS,
1701 date_display_cols);
1702 if (author_cols < width)
1703 author_cols = width;
1704 free(wauthor);
1705 free(author);
1706 ncommits++;
1707 entry = TAILQ_NEXT(entry, entry);
1710 entry = s->first_displayed_entry;
1711 s->last_displayed_entry = s->first_displayed_entry;
1712 ncommits = 0;
1713 while (entry) {
1714 if (ncommits >= limit - 1)
1715 break;
1716 if (ncommits == s->selected)
1717 wstandout(view->window);
1718 err = draw_commit(view, entry->commit, entry->id,
1719 date_display_cols, author_cols);
1720 if (ncommits == s->selected)
1721 wstandend(view->window);
1722 if (err)
1723 goto done;
1724 ncommits++;
1725 s->last_displayed_entry = entry;
1726 entry = TAILQ_NEXT(entry, entry);
1729 view_vborder(view);
1730 done:
1731 free(id_str);
1732 free(refs_str);
1733 free(ncommits_str);
1734 free(header);
1735 return err;
1738 static void
1739 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 struct commit_queue_entry *entry;
1742 int nscrolled = 0;
1744 entry = TAILQ_FIRST(&s->commits.head);
1745 if (s->first_displayed_entry == entry)
1746 return;
1748 entry = s->first_displayed_entry;
1749 while (entry && nscrolled < maxscroll) {
1750 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 if (entry) {
1752 s->first_displayed_entry = entry;
1753 nscrolled++;
1758 static const struct got_error *
1759 trigger_log_thread(struct tog_view *view, int wait)
1761 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1762 int errcode;
1764 halfdelay(1); /* fast refresh while loading commits */
1766 while (ta->commits_needed > 0) {
1767 if (ta->log_complete)
1768 break;
1770 /* Wake the log thread. */
1771 errcode = pthread_cond_signal(&ta->need_commits);
1772 if (errcode)
1773 return got_error_set_errno(errcode,
1774 "pthread_cond_signal");
1777 * The mutex will be released while the view loop waits
1778 * in wgetch(), at which time the log thread will run.
1780 if (!wait)
1781 break;
1783 /* Display progress update in log view. */
1784 show_log_view(view);
1785 update_panels();
1786 doupdate();
1788 /* Wait right here while next commit is being loaded. */
1789 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 if (errcode)
1791 return got_error_set_errno(errcode,
1792 "pthread_cond_wait");
1794 /* Display progress update in log view. */
1795 show_log_view(view);
1796 update_panels();
1797 doupdate();
1800 return NULL;
1803 static const struct got_error *
1804 log_scroll_down(struct tog_view *view, int maxscroll)
1806 struct tog_log_view_state *s = &view->state.log;
1807 const struct got_error *err = NULL;
1808 struct commit_queue_entry *pentry;
1809 int nscrolled = 0, ncommits_needed;
1811 if (s->last_displayed_entry == NULL)
1812 return NULL;
1814 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 if (s->commits.ncommits < ncommits_needed &&
1816 !s->thread_args.log_complete) {
1818 * Ask the log thread for required amount of commits.
1820 s->thread_args.commits_needed += maxscroll;
1821 err = trigger_log_thread(view, 1);
1822 if (err)
1823 return err;
1826 do {
1827 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1828 if (pentry == NULL)
1829 break;
1831 s->last_displayed_entry = pentry;
1833 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1834 if (pentry == NULL)
1835 break;
1836 s->first_displayed_entry = pentry;
1837 } while (++nscrolled < maxscroll);
1839 return err;
1842 static const struct got_error *
1843 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 struct got_commit_object *commit, struct got_object_id *commit_id,
1845 struct tog_view *log_view, struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_object_qid *parent_id;
1849 struct tog_view *diff_view;
1851 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 if (diff_view == NULL)
1853 return got_error_from_errno("view_open");
1855 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 if (err == NULL)
1859 *new_view = diff_view;
1860 return err;
1863 static const struct got_error *
1864 tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 struct got_tree_object *subtree)
1867 struct tog_parent_tree *parent;
1869 parent = calloc(1, sizeof(*parent));
1870 if (parent == NULL)
1871 return got_error_from_errno("calloc");
1873 parent->tree = s->tree;
1874 parent->first_displayed_entry = s->first_displayed_entry;
1875 parent->selected_entry = s->selected_entry;
1876 parent->selected = s->selected;
1877 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1878 s->tree = subtree;
1879 s->selected = 0;
1880 s->first_displayed_entry = NULL;
1881 return NULL;
1884 static const struct got_error *
1885 tree_view_walk_path(struct tog_tree_view_state *s,
1886 struct got_object_id *commit_id, const char *path)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree = NULL;
1890 const char *p;
1891 char *slash, *subpath = NULL;
1893 /* Walk the path and open corresponding tree objects. */
1894 p = path;
1895 while (*p) {
1896 struct got_tree_entry *te;
1897 struct got_object_id *tree_id;
1898 char *te_name;
1900 while (p[0] == '/')
1901 p++;
1903 /* Ensure the correct subtree entry is selected. */
1904 slash = strchr(p, '/');
1905 if (slash == NULL)
1906 te_name = strdup(p);
1907 else
1908 te_name = strndup(p, slash - p);
1909 if (te_name == NULL) {
1910 err = got_error_from_errno("strndup");
1911 break;
1913 te = got_object_tree_find_entry(s->tree, te_name);
1914 if (te == NULL) {
1915 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1916 free(te_name);
1917 break;
1919 free(te_name);
1920 s->first_displayed_entry = s->selected_entry = te;
1922 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 break; /* jump to this file's entry */
1925 slash = strchr(p, '/');
1926 if (slash)
1927 subpath = strndup(path, slash - path);
1928 else
1929 subpath = strdup(path);
1930 if (subpath == NULL) {
1931 err = got_error_from_errno("strdup");
1932 break;
1935 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1936 subpath);
1937 if (err)
1938 break;
1940 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1941 free(tree_id);
1942 if (err)
1943 break;
1945 err = tree_view_visit_subtree(s, tree);
1946 if (err) {
1947 got_object_tree_close(tree);
1948 break;
1950 if (slash == NULL)
1951 break;
1952 free(subpath);
1953 subpath = NULL;
1954 p = slash;
1957 free(subpath);
1958 return err;
1961 static const struct got_error *
1962 browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 struct commit_queue_entry *entry, const char *path,
1964 const char *head_ref_name, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_tree_object *tree;
1968 struct tog_tree_view_state *s;
1969 struct tog_view *tree_view;
1971 err = got_object_open_as_tree(&tree, repo,
1972 got_object_commit_get_tree_id(entry->commit));
1973 if (err)
1974 return err;
1976 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1977 if (tree_view == NULL)
1978 return got_error_from_errno("view_open");
1980 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1981 if (err) {
1982 got_object_tree_close(tree);
1983 return err;
1985 s = &tree_view->state.tree;
1987 *new_view = tree_view;
1989 if (got_path_is_root_dir(path))
1990 return NULL;
1992 return tree_view_walk_path(s, entry->id, path);
1995 static const struct got_error *
1996 block_signals_used_by_main_thread(void)
1998 sigset_t sigset;
1999 int errcode;
2001 if (sigemptyset(&sigset) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset, SIGWINCH) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset, SIGCONT) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset, SIGTSTP) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2015 if (errcode)
2016 return got_error_set_errno(errcode, "pthread_sigmask");
2018 return NULL;
2021 static void *
2022 log_thread(void *arg)
2024 const struct got_error *err = NULL;
2025 int errcode = 0;
2026 struct tog_log_thread_args *a = arg;
2027 int done = 0;
2029 err = block_signals_used_by_main_thread();
2030 if (err)
2031 return (void *)err;
2033 while (!done && !err && !tog_sigpipe_received) {
2034 err = queue_commits(a);
2035 if (err) {
2036 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 return (void *)err;
2038 err = NULL;
2039 done = 1;
2040 } else if (a->commits_needed > 0)
2041 a->commits_needed--;
2043 errcode = pthread_mutex_lock(&tog_mutex);
2044 if (errcode) {
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_lock");
2047 break;
2048 } else if (*a->quit)
2049 done = 1;
2050 else if (*a->first_displayed_entry == NULL) {
2051 *a->first_displayed_entry =
2052 TAILQ_FIRST(&a->commits->head);
2053 *a->selected_entry = *a->first_displayed_entry;
2056 errcode = pthread_cond_signal(&a->commit_loaded);
2057 if (errcode) {
2058 err = got_error_set_errno(errcode,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex);
2061 break;
2064 if (done)
2065 a->commits_needed = 0;
2066 else {
2067 if (a->commits_needed == 0) {
2068 errcode = pthread_cond_wait(&a->need_commits,
2069 &tog_mutex);
2070 if (errcode)
2071 err = got_error_set_errno(errcode,
2072 "pthread_cond_wait");
2073 if (*a->quit)
2074 done = 1;
2078 errcode = pthread_mutex_unlock(&tog_mutex);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode,
2081 "pthread_mutex_unlock");
2083 a->log_complete = 1;
2084 return (void *)err;
2087 static const struct got_error *
2088 stop_log_thread(struct tog_log_view_state *s)
2090 const struct got_error *err = NULL;
2091 int errcode;
2093 if (s->thread) {
2094 s->quit = 1;
2095 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2096 if (errcode)
2097 return got_error_set_errno(errcode,
2098 "pthread_cond_signal");
2099 errcode = pthread_mutex_unlock(&tog_mutex);
2100 if (errcode)
2101 return got_error_set_errno(errcode,
2102 "pthread_mutex_unlock");
2103 errcode = pthread_join(s->thread, (void **)&err);
2104 if (errcode)
2105 return got_error_set_errno(errcode, "pthread_join");
2106 errcode = pthread_mutex_lock(&tog_mutex);
2107 if (errcode)
2108 return got_error_set_errno(errcode,
2109 "pthread_mutex_lock");
2110 s->thread = NULL;
2113 if (s->thread_args.repo) {
2114 got_repo_close(s->thread_args.repo);
2115 s->thread_args.repo = NULL;
2118 if (s->thread_args.graph) {
2119 got_commit_graph_close(s->thread_args.graph);
2120 s->thread_args.graph = NULL;
2123 return err;
2126 static const struct got_error *
2127 close_log_view(struct tog_view *view)
2129 const struct got_error *err = NULL;
2130 struct tog_log_view_state *s = &view->state.log;
2131 int errcode;
2133 err = stop_log_thread(s);
2135 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2136 if (errcode && err == NULL)
2137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2139 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2140 if (errcode && err == NULL)
2141 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2143 free_commits(&s->commits);
2144 free(s->in_repo_path);
2145 s->in_repo_path = NULL;
2146 free(s->start_id);
2147 s->start_id = NULL;
2148 free(s->head_ref_name);
2149 s->head_ref_name = NULL;
2150 return err;
2153 static const struct got_error *
2154 search_start_log_view(struct tog_view *view)
2156 struct tog_log_view_state *s = &view->state.log;
2158 s->matched_entry = NULL;
2159 s->search_entry = NULL;
2160 return NULL;
2163 static const struct got_error *
2164 search_next_log_view(struct tog_view *view)
2166 const struct got_error *err = NULL;
2167 struct tog_log_view_state *s = &view->state.log;
2168 struct commit_queue_entry *entry;
2170 /* Display progress update in log view. */
2171 show_log_view(view);
2172 update_panels();
2173 doupdate();
2175 if (s->search_entry) {
2176 int errcode, ch;
2177 errcode = pthread_mutex_unlock(&tog_mutex);
2178 if (errcode)
2179 return got_error_set_errno(errcode,
2180 "pthread_mutex_unlock");
2181 ch = wgetch(view->window);
2182 errcode = pthread_mutex_lock(&tog_mutex);
2183 if (errcode)
2184 return got_error_set_errno(errcode,
2185 "pthread_mutex_lock");
2186 if (ch == KEY_BACKSPACE) {
2187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2188 return NULL;
2190 if (view->searching == TOG_SEARCH_FORWARD)
2191 entry = TAILQ_NEXT(s->search_entry, entry);
2192 else
2193 entry = TAILQ_PREV(s->search_entry,
2194 commit_queue_head, entry);
2195 } else if (s->matched_entry) {
2196 if (view->searching == TOG_SEARCH_FORWARD)
2197 entry = TAILQ_NEXT(s->matched_entry, entry);
2198 else
2199 entry = TAILQ_PREV(s->matched_entry,
2200 commit_queue_head, entry);
2201 } else {
2202 if (view->searching == TOG_SEARCH_FORWARD)
2203 entry = TAILQ_FIRST(&s->commits.head);
2204 else
2205 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 while (1) {
2209 int have_match = 0;
2211 if (entry == NULL) {
2212 if (s->thread_args.log_complete ||
2213 view->searching == TOG_SEARCH_BACKWARD) {
2214 view->search_next_done =
2215 (s->matched_entry == NULL ?
2216 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2217 s->search_entry = NULL;
2218 return NULL;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s->thread_args.commits_needed++;
2226 return trigger_log_thread(view, 0);
2229 err = match_commit(&have_match, entry->id, entry->commit,
2230 &view->regex);
2231 if (err)
2232 break;
2233 if (have_match) {
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 s->matched_entry = entry;
2236 break;
2239 s->search_entry = entry;
2240 if (view->searching == TOG_SEARCH_FORWARD)
2241 entry = TAILQ_NEXT(entry, entry);
2242 else
2243 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2246 if (s->matched_entry) {
2247 int cur = s->selected_entry->idx;
2248 while (cur < s->matched_entry->idx) {
2249 err = input_log_view(NULL, view, KEY_DOWN);
2250 if (err)
2251 return err;
2252 cur++;
2254 while (cur > s->matched_entry->idx) {
2255 err = input_log_view(NULL, view, KEY_UP);
2256 if (err)
2257 return err;
2258 cur--;
2262 s->search_entry = NULL;
2264 return NULL;
2267 static const struct got_error *
2268 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2269 struct got_repository *repo, const char *head_ref_name,
2270 const char *in_repo_path, int log_branches)
2272 const struct got_error *err = NULL;
2273 struct tog_log_view_state *s = &view->state.log;
2274 struct got_repository *thread_repo = NULL;
2275 struct got_commit_graph *thread_graph = NULL;
2276 int errcode;
2278 if (in_repo_path != s->in_repo_path) {
2279 free(s->in_repo_path);
2280 s->in_repo_path = strdup(in_repo_path);
2281 if (s->in_repo_path == NULL)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s->commits.head);
2287 s->commits.ncommits = 0;
2289 s->repo = repo;
2290 if (head_ref_name) {
2291 s->head_ref_name = strdup(head_ref_name);
2292 if (s->head_ref_name == NULL) {
2293 err = got_error_from_errno("strdup");
2294 goto done;
2297 s->start_id = got_object_id_dup(start_id);
2298 if (s->start_id == NULL) {
2299 err = got_error_from_errno("got_object_id_dup");
2300 goto done;
2302 s->log_branches = log_branches;
2304 SIMPLEQ_INIT(&s->colors);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2306 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2307 get_color_value("TOG_COLOR_COMMIT"));
2308 if (err)
2309 goto done;
2310 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2312 if (err) {
2313 free_colors(&s->colors);
2314 goto done;
2316 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2317 get_color_value("TOG_COLOR_DATE"));
2318 if (err) {
2319 free_colors(&s->colors);
2320 goto done;
2324 view->show = show_log_view;
2325 view->input = input_log_view;
2326 view->close = close_log_view;
2327 view->search_start = search_start_log_view;
2328 view->search_next = search_next_log_view;
2330 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2331 if (err)
2332 goto done;
2333 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 !s->log_branches);
2335 if (err)
2336 goto done;
2337 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2338 s->repo, NULL, NULL);
2339 if (err)
2340 goto done;
2342 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2343 if (errcode) {
2344 err = got_error_set_errno(errcode, "pthread_cond_init");
2345 goto done;
2347 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2348 if (errcode) {
2349 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 goto done;
2353 s->thread_args.commits_needed = view->nlines;
2354 s->thread_args.graph = thread_graph;
2355 s->thread_args.commits = &s->commits;
2356 s->thread_args.in_repo_path = s->in_repo_path;
2357 s->thread_args.start_id = s->start_id;
2358 s->thread_args.repo = thread_repo;
2359 s->thread_args.log_complete = 0;
2360 s->thread_args.quit = &s->quit;
2361 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2362 s->thread_args.selected_entry = &s->selected_entry;
2363 s->thread_args.searching = &view->searching;
2364 s->thread_args.search_next_done = &view->search_next_done;
2365 s->thread_args.regex = &view->regex;
2366 done:
2367 if (err)
2368 close_log_view(view);
2369 return err;
2372 static const struct got_error *
2373 show_log_view(struct tog_view *view)
2375 const struct got_error *err;
2376 struct tog_log_view_state *s = &view->state.log;
2378 if (s->thread == NULL) {
2379 int errcode = pthread_create(&s->thread, NULL, log_thread,
2380 &s->thread_args);
2381 if (errcode)
2382 return got_error_set_errno(errcode, "pthread_create");
2383 if (s->thread_args.commits_needed > 0) {
2384 err = trigger_log_thread(view, 1);
2385 if (err)
2386 return err;
2390 return draw_commits(view);
2393 static const struct got_error *
2394 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2396 const struct got_error *err = NULL;
2397 struct tog_log_view_state *s = &view->state.log;
2398 struct tog_view *diff_view = NULL, *tree_view = NULL;
2399 struct tog_view *ref_view = NULL;
2400 int begin_x = 0;
2402 switch (ch) {
2403 case 'q':
2404 s->quit = 1;
2405 break;
2406 case 'k':
2407 case KEY_UP:
2408 case '<':
2409 case ',':
2410 if (s->first_displayed_entry == NULL)
2411 break;
2412 if (s->selected > 0)
2413 s->selected--;
2414 else
2415 log_scroll_up(s, 1);
2416 select_commit(s);
2417 break;
2418 case KEY_PPAGE:
2419 case CTRL('b'):
2420 if (s->first_displayed_entry == NULL)
2421 break;
2422 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2423 s->selected = 0;
2424 else
2425 log_scroll_up(s, view->nlines - 1);
2426 select_commit(s);
2427 break;
2428 case 'j':
2429 case KEY_DOWN:
2430 case '>':
2431 case '.':
2432 if (s->first_displayed_entry == NULL)
2433 break;
2434 if (s->selected < MIN(view->nlines - 2,
2435 s->commits.ncommits - 1))
2436 s->selected++;
2437 else {
2438 err = log_scroll_down(view, 1);
2439 if (err)
2440 break;
2442 select_commit(s);
2443 break;
2444 case KEY_NPAGE:
2445 case CTRL('f'): {
2446 struct commit_queue_entry *first;
2447 first = s->first_displayed_entry;
2448 if (first == NULL)
2449 break;
2450 err = log_scroll_down(view, view->nlines - 1);
2451 if (err)
2452 break;
2453 if (first == s->first_displayed_entry &&
2454 s->selected < MIN(view->nlines - 2,
2455 s->commits.ncommits - 1)) {
2456 /* can't scroll further down */
2457 s->selected = MIN(view->nlines - 2,
2458 s->commits.ncommits - 1);
2460 select_commit(s);
2461 break;
2463 case KEY_RESIZE:
2464 if (s->selected > view->nlines - 2)
2465 s->selected = view->nlines - 2;
2466 if (s->selected > s->commits.ncommits - 1)
2467 s->selected = s->commits.ncommits - 1;
2468 select_commit(s);
2469 if (s->commits.ncommits < view->nlines - 1 &&
2470 !s->thread_args.log_complete) {
2471 s->thread_args.commits_needed += (view->nlines - 1) -
2472 s->commits.ncommits;
2473 err = trigger_log_thread(view, 1);
2475 break;
2476 case KEY_ENTER:
2477 case ' ':
2478 case '\r':
2479 if (s->selected_entry == NULL)
2480 break;
2481 if (view_is_parent_view(view))
2482 begin_x = view_split_begin_x(view->begin_x);
2483 err = open_diff_view_for_commit(&diff_view, begin_x,
2484 s->selected_entry->commit, s->selected_entry->id,
2485 view, s->repo);
2486 if (err)
2487 break;
2488 view->focussed = 0;
2489 diff_view->focussed = 1;
2490 if (view_is_parent_view(view)) {
2491 err = view_close_child(view);
2492 if (err)
2493 return err;
2494 view_set_child(view, diff_view);
2495 view->focus_child = 1;
2496 } else
2497 *new_view = diff_view;
2498 break;
2499 case 't':
2500 if (s->selected_entry == NULL)
2501 break;
2502 if (view_is_parent_view(view))
2503 begin_x = view_split_begin_x(view->begin_x);
2504 err = browse_commit_tree(&tree_view, begin_x,
2505 s->selected_entry, s->in_repo_path, s->head_ref_name,
2506 s->repo);
2507 if (err)
2508 break;
2509 view->focussed = 0;
2510 tree_view->focussed = 1;
2511 if (view_is_parent_view(view)) {
2512 err = view_close_child(view);
2513 if (err)
2514 return err;
2515 view_set_child(view, tree_view);
2516 view->focus_child = 1;
2517 } else
2518 *new_view = tree_view;
2519 break;
2520 case KEY_BACKSPACE:
2521 case CTRL('l'):
2522 case 'B':
2523 if (ch == KEY_BACKSPACE &&
2524 got_path_is_root_dir(s->in_repo_path))
2525 break;
2526 err = stop_log_thread(s);
2527 if (err)
2528 return err;
2529 if (ch == KEY_BACKSPACE) {
2530 char *parent_path;
2531 err = got_path_dirname(&parent_path, s->in_repo_path);
2532 if (err)
2533 return err;
2534 free(s->in_repo_path);
2535 s->in_repo_path = parent_path;
2536 s->thread_args.in_repo_path = s->in_repo_path;
2537 } else if (ch == CTRL('l')) {
2538 struct got_object_id *start_id;
2539 err = got_repo_match_object_id(&start_id, NULL,
2540 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2541 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2542 if (err)
2543 return err;
2544 free(s->start_id);
2545 s->start_id = start_id;
2546 s->thread_args.start_id = s->start_id;
2547 } else /* 'B' */
2548 s->log_branches = !s->log_branches;
2550 err = got_repo_open(&s->thread_args.repo,
2551 got_repo_get_path(s->repo), NULL);
2552 if (err)
2553 return err;
2554 tog_free_refs();
2555 err = tog_load_refs(s->repo);
2556 if (err)
2557 return err;
2558 err = got_commit_graph_open(&s->thread_args.graph,
2559 s->in_repo_path, !s->log_branches);
2560 if (err)
2561 return err;
2562 err = got_commit_graph_iter_start(s->thread_args.graph,
2563 s->start_id, s->repo, NULL, NULL);
2564 if (err)
2565 return err;
2566 free_commits(&s->commits);
2567 s->first_displayed_entry = NULL;
2568 s->last_displayed_entry = NULL;
2569 s->selected_entry = NULL;
2570 s->selected = 0;
2571 s->thread_args.log_complete = 0;
2572 s->quit = 0;
2573 s->thread_args.commits_needed = view->nlines;
2574 break;
2575 case 'r':
2576 if (view_is_parent_view(view))
2577 begin_x = view_split_begin_x(view->begin_x);
2578 ref_view = view_open(view->nlines, view->ncols,
2579 view->begin_y, begin_x, TOG_VIEW_REF);
2580 if (ref_view == NULL)
2581 return got_error_from_errno("view_open");
2582 err = open_ref_view(ref_view, s->repo);
2583 if (err) {
2584 view_close(ref_view);
2585 return err;
2587 view->focussed = 0;
2588 ref_view->focussed = 1;
2589 if (view_is_parent_view(view)) {
2590 err = view_close_child(view);
2591 if (err)
2592 return err;
2593 view_set_child(view, ref_view);
2594 view->focus_child = 1;
2595 } else
2596 *new_view = ref_view;
2597 break;
2598 default:
2599 break;
2602 return err;
2605 static const struct got_error *
2606 apply_unveil(const char *repo_path, const char *worktree_path)
2608 const struct got_error *error;
2610 #ifdef PROFILE
2611 if (unveil("gmon.out", "rwc") != 0)
2612 return got_error_from_errno2("unveil", "gmon.out");
2613 #endif
2614 if (repo_path && unveil(repo_path, "r") != 0)
2615 return got_error_from_errno2("unveil", repo_path);
2617 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2618 return got_error_from_errno2("unveil", worktree_path);
2620 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2621 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2623 error = got_privsep_unveil_exec_helpers();
2624 if (error != NULL)
2625 return error;
2627 if (unveil(NULL, NULL) != 0)
2628 return got_error_from_errno("unveil");
2630 return NULL;
2633 static void
2634 init_curses(void)
2636 initscr();
2637 cbreak();
2638 halfdelay(1); /* Do fast refresh while initial view is loading. */
2639 noecho();
2640 nonl();
2641 intrflush(stdscr, FALSE);
2642 keypad(stdscr, TRUE);
2643 curs_set(0);
2644 if (getenv("TOG_COLORS") != NULL) {
2645 start_color();
2646 use_default_colors();
2648 signal(SIGWINCH, tog_sigwinch);
2649 signal(SIGPIPE, tog_sigpipe);
2650 signal(SIGCONT, tog_sigcont);
2653 static const struct got_error *
2654 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2655 struct got_repository *repo, struct got_worktree *worktree)
2657 const struct got_error *err = NULL;
2659 if (argc == 0) {
2660 *in_repo_path = strdup("/");
2661 if (*in_repo_path == NULL)
2662 return got_error_from_errno("strdup");
2663 return NULL;
2666 if (worktree) {
2667 const char *prefix = got_worktree_get_path_prefix(worktree);
2668 char *p;
2670 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2671 if (err)
2672 return err;
2673 if (asprintf(in_repo_path, "%s%s%s", prefix,
2674 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2675 p) == -1) {
2676 err = got_error_from_errno("asprintf");
2677 *in_repo_path = NULL;
2679 free(p);
2680 } else
2681 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2683 return err;
2686 static const struct got_error *
2687 cmd_log(int argc, char *argv[])
2689 const struct got_error *error;
2690 struct got_repository *repo = NULL;
2691 struct got_worktree *worktree = NULL;
2692 struct got_object_id *start_id = NULL;
2693 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2694 char *start_commit = NULL, *label = NULL;
2695 struct got_reference *ref = NULL;
2696 const char *head_ref_name = NULL;
2697 int ch, log_branches = 0;
2698 struct tog_view *view;
2700 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2701 switch (ch) {
2702 case 'b':
2703 log_branches = 1;
2704 break;
2705 case 'c':
2706 start_commit = optarg;
2707 break;
2708 case 'r':
2709 repo_path = realpath(optarg, NULL);
2710 if (repo_path == NULL)
2711 return got_error_from_errno2("realpath",
2712 optarg);
2713 break;
2714 default:
2715 usage_log();
2716 /* NOTREACHED */
2720 argc -= optind;
2721 argv += optind;
2723 if (argc > 1)
2724 usage_log();
2726 if (repo_path == NULL) {
2727 cwd = getcwd(NULL, 0);
2728 if (cwd == NULL)
2729 return got_error_from_errno("getcwd");
2730 error = got_worktree_open(&worktree, cwd);
2731 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2732 goto done;
2733 if (worktree)
2734 repo_path =
2735 strdup(got_worktree_get_repo_path(worktree));
2736 else
2737 repo_path = strdup(cwd);
2738 if (repo_path == NULL) {
2739 error = got_error_from_errno("strdup");
2740 goto done;
2744 error = got_repo_open(&repo, repo_path, NULL);
2745 if (error != NULL)
2746 goto done;
2748 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2749 repo, worktree);
2750 if (error)
2751 goto done;
2753 init_curses();
2755 error = apply_unveil(got_repo_get_path(repo),
2756 worktree ? got_worktree_get_root_path(worktree) : NULL);
2757 if (error)
2758 goto done;
2760 /* already loaded by tog_log_with_path()? */
2761 if (TAILQ_EMPTY(&tog_refs)) {
2762 error = tog_load_refs(repo);
2763 if (error)
2764 goto done;
2767 if (start_commit == NULL) {
2768 error = got_repo_match_object_id(&start_id, &label,
2769 worktree ? got_worktree_get_head_ref_name(worktree) :
2770 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2771 if (error)
2772 goto done;
2773 head_ref_name = label;
2774 } else {
2775 error = got_ref_open(&ref, repo, start_commit, 0);
2776 if (error == NULL)
2777 head_ref_name = got_ref_get_name(ref);
2778 else if (error->code != GOT_ERR_NOT_REF)
2779 goto done;
2780 error = got_repo_match_object_id(&start_id, NULL,
2781 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2782 if (error)
2783 goto done;
2786 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2787 if (view == NULL) {
2788 error = got_error_from_errno("view_open");
2789 goto done;
2791 error = open_log_view(view, start_id, repo, head_ref_name,
2792 in_repo_path, log_branches);
2793 if (error)
2794 goto done;
2795 if (worktree) {
2796 /* Release work tree lock. */
2797 got_worktree_close(worktree);
2798 worktree = NULL;
2800 error = view_loop(view);
2801 done:
2802 free(in_repo_path);
2803 free(repo_path);
2804 free(cwd);
2805 free(start_id);
2806 free(label);
2807 if (ref)
2808 got_ref_close(ref);
2809 if (repo)
2810 got_repo_close(repo);
2811 if (worktree)
2812 got_worktree_close(worktree);
2813 tog_free_refs();
2814 return error;
2817 __dead static void
2818 usage_diff(void)
2820 endwin();
2821 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2822 "[-w] object1 object2\n", getprogname());
2823 exit(1);
2826 static int
2827 match_line(const char *line, regex_t *regex, size_t nmatch,
2828 regmatch_t *regmatch)
2830 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2833 struct tog_color *
2834 match_color(struct tog_colors *colors, const char *line)
2836 struct tog_color *tc = NULL;
2838 SIMPLEQ_FOREACH(tc, colors, entry) {
2839 if (match_line(line, &tc->regex, 0, NULL))
2840 return tc;
2843 return NULL;
2846 static const struct got_error *
2847 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2848 WINDOW *window, regmatch_t *regmatch)
2850 const struct got_error *err = NULL;
2851 wchar_t *wline;
2852 int width;
2853 char *s;
2855 *wtotal = 0;
2857 s = strndup(line, regmatch->rm_so);
2858 if (s == NULL)
2859 return got_error_from_errno("strndup");
2861 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2862 if (err) {
2863 free(s);
2864 return err;
2866 waddwstr(window, wline);
2867 free(wline);
2868 free(s);
2869 wlimit -= width;
2870 *wtotal += width;
2872 if (wlimit > 0) {
2873 s = strndup(line + regmatch->rm_so,
2874 regmatch->rm_eo - regmatch->rm_so);
2875 if (s == NULL) {
2876 err = got_error_from_errno("strndup");
2877 free(s);
2878 return err;
2880 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2881 if (err) {
2882 free(s);
2883 return err;
2885 wattr_on(window, A_STANDOUT, NULL);
2886 waddwstr(window, wline);
2887 wattr_off(window, A_STANDOUT, NULL);
2888 free(wline);
2889 free(s);
2890 wlimit -= width;
2891 *wtotal += width;
2894 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2895 err = format_line(&wline, &width,
2896 line + regmatch->rm_eo, wlimit, col_tab_align);
2897 if (err)
2898 return err;
2899 waddwstr(window, wline);
2900 free(wline);
2901 *wtotal += width;
2904 return NULL;
2907 static const struct got_error *
2908 draw_file(struct tog_view *view, const char *header)
2910 struct tog_diff_view_state *s = &view->state.diff;
2911 regmatch_t *regmatch = &view->regmatch;
2912 const struct got_error *err;
2913 int nprinted = 0;
2914 char *line;
2915 size_t linesize = 0;
2916 ssize_t linelen;
2917 struct tog_color *tc;
2918 wchar_t *wline;
2919 int width;
2920 int max_lines = view->nlines;
2921 int nlines = s->nlines;
2922 off_t line_offset;
2924 line_offset = s->line_offsets[s->first_displayed_line - 1];
2925 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2926 return got_error_from_errno("fseek");
2928 werase(view->window);
2930 if (header) {
2931 if (asprintf(&line, "[%d/%d] %s",
2932 s->first_displayed_line - 1 + s->selected_line, nlines,
2933 header) == -1)
2934 return got_error_from_errno("asprintf");
2935 err = format_line(&wline, &width, line, view->ncols, 0);
2936 free(line);
2937 if (err)
2938 return err;
2940 if (view_needs_focus_indication(view))
2941 wstandout(view->window);
2942 waddwstr(view->window, wline);
2943 free(wline);
2944 wline = NULL;
2945 if (view_needs_focus_indication(view))
2946 wstandend(view->window);
2947 if (width <= view->ncols - 1)
2948 waddch(view->window, '\n');
2950 if (max_lines <= 1)
2951 return NULL;
2952 max_lines--;
2955 s->eof = 0;
2956 line = NULL;
2957 while (max_lines > 0 && nprinted < max_lines) {
2958 linelen = getline(&line, &linesize, s->f);
2959 if (linelen == -1) {
2960 if (feof(s->f)) {
2961 s->eof = 1;
2962 break;
2964 free(line);
2965 return got_ferror(s->f, GOT_ERR_IO);
2968 tc = match_color(&s->colors, line);
2969 if (tc)
2970 wattr_on(view->window,
2971 COLOR_PAIR(tc->colorpair), NULL);
2972 if (s->first_displayed_line + nprinted == s->matched_line &&
2973 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2974 err = add_matched_line(&width, line, view->ncols, 0,
2975 view->window, regmatch);
2976 if (err) {
2977 free(line);
2978 return err;
2980 } else {
2981 err = format_line(&wline, &width, line, view->ncols, 0);
2982 if (err) {
2983 free(line);
2984 return err;
2986 waddwstr(view->window, wline);
2987 free(wline);
2988 wline = NULL;
2990 if (tc)
2991 wattr_off(view->window,
2992 COLOR_PAIR(tc->colorpair), NULL);
2993 if (width <= view->ncols - 1)
2994 waddch(view->window, '\n');
2995 nprinted++;
2997 free(line);
2998 if (nprinted >= 1)
2999 s->last_displayed_line = s->first_displayed_line +
3000 (nprinted - 1);
3001 else
3002 s->last_displayed_line = s->first_displayed_line;
3004 view_vborder(view);
3006 if (s->eof) {
3007 while (nprinted < view->nlines) {
3008 waddch(view->window, '\n');
3009 nprinted++;
3012 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3013 if (err) {
3014 return err;
3017 wstandout(view->window);
3018 waddwstr(view->window, wline);
3019 free(wline);
3020 wline = NULL;
3021 wstandend(view->window);
3024 return NULL;
3027 static char *
3028 get_datestr(time_t *time, char *datebuf)
3030 struct tm mytm, *tm;
3031 char *p, *s;
3033 tm = gmtime_r(time, &mytm);
3034 if (tm == NULL)
3035 return NULL;
3036 s = asctime_r(tm, datebuf);
3037 if (s == NULL)
3038 return NULL;
3039 p = strchr(s, '\n');
3040 if (p)
3041 *p = '\0';
3042 return s;
3045 static const struct got_error *
3046 get_changed_paths(struct got_pathlist_head *paths,
3047 struct got_commit_object *commit, struct got_repository *repo)
3049 const struct got_error *err = NULL;
3050 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3051 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3052 struct got_object_qid *qid;
3054 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3055 if (qid != NULL) {
3056 struct got_commit_object *pcommit;
3057 err = got_object_open_as_commit(&pcommit, repo,
3058 qid->id);
3059 if (err)
3060 return err;
3062 tree_id1 = got_object_commit_get_tree_id(pcommit);
3063 got_object_commit_close(pcommit);
3067 if (tree_id1) {
3068 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3069 if (err)
3070 goto done;
3073 tree_id2 = got_object_commit_get_tree_id(commit);
3074 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3075 if (err)
3076 goto done;
3078 err = got_diff_tree(tree1, tree2, "", "", repo,
3079 got_diff_tree_collect_changed_paths, paths, 0);
3080 done:
3081 if (tree1)
3082 got_object_tree_close(tree1);
3083 if (tree2)
3084 got_object_tree_close(tree2);
3085 return err;
3088 static const struct got_error *
3089 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3091 off_t *p;
3093 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3094 if (p == NULL)
3095 return got_error_from_errno("reallocarray");
3096 *line_offsets = p;
3097 (*line_offsets)[*nlines] = off;
3098 (*nlines)++;
3099 return NULL;
3102 static const struct got_error *
3103 write_commit_info(off_t **line_offsets, size_t *nlines,
3104 struct got_object_id *commit_id, struct got_reflist_head *refs,
3105 struct got_repository *repo, FILE *outfile)
3107 const struct got_error *err = NULL;
3108 char datebuf[26], *datestr;
3109 struct got_commit_object *commit;
3110 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3111 time_t committer_time;
3112 const char *author, *committer;
3113 char *refs_str = NULL;
3114 struct got_pathlist_head changed_paths;
3115 struct got_pathlist_entry *pe;
3116 off_t outoff = 0;
3117 int n;
3119 TAILQ_INIT(&changed_paths);
3121 if (refs) {
3122 err = build_refs_str(&refs_str, refs, commit_id, repo);
3123 if (err)
3124 return err;
3127 err = got_object_open_as_commit(&commit, repo, commit_id);
3128 if (err)
3129 return err;
3131 err = got_object_id_str(&id_str, commit_id);
3132 if (err) {
3133 err = got_error_from_errno("got_object_id_str");
3134 goto done;
3137 err = add_line_offset(line_offsets, nlines, 0);
3138 if (err)
3139 goto done;
3141 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3142 refs_str ? refs_str : "", refs_str ? ")" : "");
3143 if (n < 0) {
3144 err = got_error_from_errno("fprintf");
3145 goto done;
3147 outoff += n;
3148 err = add_line_offset(line_offsets, nlines, outoff);
3149 if (err)
3150 goto done;
3152 n = fprintf(outfile, "from: %s\n",
3153 got_object_commit_get_author(commit));
3154 if (n < 0) {
3155 err = got_error_from_errno("fprintf");
3156 goto done;
3158 outoff += n;
3159 err = add_line_offset(line_offsets, nlines, outoff);
3160 if (err)
3161 goto done;
3163 committer_time = got_object_commit_get_committer_time(commit);
3164 datestr = get_datestr(&committer_time, datebuf);
3165 if (datestr) {
3166 n = fprintf(outfile, "date: %s UTC\n", datestr);
3167 if (n < 0) {
3168 err = got_error_from_errno("fprintf");
3169 goto done;
3171 outoff += n;
3172 err = add_line_offset(line_offsets, nlines, outoff);
3173 if (err)
3174 goto done;
3176 author = got_object_commit_get_author(commit);
3177 committer = got_object_commit_get_committer(commit);
3178 if (strcmp(author, committer) != 0) {
3179 n = fprintf(outfile, "via: %s\n", committer);
3180 if (n < 0) {
3181 err = got_error_from_errno("fprintf");
3182 goto done;
3184 outoff += n;
3185 err = add_line_offset(line_offsets, nlines, outoff);
3186 if (err)
3187 goto done;
3189 err = got_object_commit_get_logmsg(&logmsg, commit);
3190 if (err)
3191 goto done;
3192 s = logmsg;
3193 while ((line = strsep(&s, "\n")) != NULL) {
3194 n = fprintf(outfile, "%s\n", line);
3195 if (n < 0) {
3196 err = got_error_from_errno("fprintf");
3197 goto done;
3199 outoff += n;
3200 err = add_line_offset(line_offsets, nlines, outoff);
3201 if (err)
3202 goto done;
3205 err = get_changed_paths(&changed_paths, commit, repo);
3206 if (err)
3207 goto done;
3208 TAILQ_FOREACH(pe, &changed_paths, entry) {
3209 struct got_diff_changed_path *cp = pe->data;
3210 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3211 if (n < 0) {
3212 err = got_error_from_errno("fprintf");
3213 goto done;
3215 outoff += n;
3216 err = add_line_offset(line_offsets, nlines, outoff);
3217 if (err)
3218 goto done;
3219 free((char *)pe->path);
3220 free(pe->data);
3223 fputc('\n', outfile);
3224 outoff++;
3225 err = add_line_offset(line_offsets, nlines, outoff);
3226 done:
3227 got_pathlist_free(&changed_paths);
3228 free(id_str);
3229 free(logmsg);
3230 free(refs_str);
3231 got_object_commit_close(commit);
3232 if (err) {
3233 free(*line_offsets);
3234 *line_offsets = NULL;
3235 *nlines = 0;
3237 return err;
3240 static const struct got_error *
3241 create_diff(struct tog_diff_view_state *s)
3243 const struct got_error *err = NULL;
3244 FILE *f = NULL;
3245 int obj_type;
3247 free(s->line_offsets);
3248 s->line_offsets = malloc(sizeof(off_t));
3249 if (s->line_offsets == NULL)
3250 return got_error_from_errno("malloc");
3251 s->nlines = 0;
3253 f = got_opentemp();
3254 if (f == NULL) {
3255 err = got_error_from_errno("got_opentemp");
3256 goto done;
3258 if (s->f && fclose(s->f) == EOF) {
3259 err = got_error_from_errno("fclose");
3260 goto done;
3262 s->f = f;
3264 if (s->id1)
3265 err = got_object_get_type(&obj_type, s->repo, s->id1);
3266 else
3267 err = got_object_get_type(&obj_type, s->repo, s->id2);
3268 if (err)
3269 goto done;
3271 switch (obj_type) {
3272 case GOT_OBJ_TYPE_BLOB:
3273 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3274 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3275 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3276 break;
3277 case GOT_OBJ_TYPE_TREE:
3278 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3279 s->id1, s->id2, "", "", s->diff_context,
3280 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3281 break;
3282 case GOT_OBJ_TYPE_COMMIT: {
3283 const struct got_object_id_queue *parent_ids;
3284 struct got_object_qid *pid;
3285 struct got_commit_object *commit2;
3286 struct got_reflist_head *refs;
3288 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3289 if (err)
3290 goto done;
3291 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3292 /* Show commit info if we're diffing to a parent/root commit. */
3293 if (s->id1 == NULL) {
3294 err = write_commit_info(&s->line_offsets, &s->nlines,
3295 s->id2, refs, s->repo, s->f);
3296 if (err)
3297 goto done;
3298 } else {
3299 parent_ids = got_object_commit_get_parent_ids(commit2);
3300 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3301 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3302 err = write_commit_info(
3303 &s->line_offsets, &s->nlines,
3304 s->id2, refs, s->repo, s->f);
3305 if (err)
3306 goto done;
3307 break;
3311 got_object_commit_close(commit2);
3313 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3314 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3315 s->force_text_diff, s->repo, s->f);
3316 break;
3318 default:
3319 err = got_error(GOT_ERR_OBJ_TYPE);
3320 break;
3322 if (err)
3323 goto done;
3324 done:
3325 if (s->f && fflush(s->f) != 0 && err == NULL)
3326 err = got_error_from_errno("fflush");
3327 return err;
3330 static void
3331 diff_view_indicate_progress(struct tog_view *view)
3333 mvwaddstr(view->window, 0, 0, "diffing...");
3334 update_panels();
3335 doupdate();
3338 static const struct got_error *
3339 search_start_diff_view(struct tog_view *view)
3341 struct tog_diff_view_state *s = &view->state.diff;
3343 s->matched_line = 0;
3344 return NULL;
3347 static const struct got_error *
3348 search_next_diff_view(struct tog_view *view)
3350 struct tog_diff_view_state *s = &view->state.diff;
3351 int lineno;
3352 char *line = NULL;
3353 size_t linesize = 0;
3354 ssize_t linelen;
3356 if (!view->searching) {
3357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 return NULL;
3361 if (s->matched_line) {
3362 if (view->searching == TOG_SEARCH_FORWARD)
3363 lineno = s->matched_line + 1;
3364 else
3365 lineno = s->matched_line - 1;
3366 } else {
3367 if (view->searching == TOG_SEARCH_FORWARD)
3368 lineno = 1;
3369 else
3370 lineno = s->nlines;
3373 while (1) {
3374 off_t offset;
3376 if (lineno <= 0 || lineno > s->nlines) {
3377 if (s->matched_line == 0) {
3378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3379 break;
3382 if (view->searching == TOG_SEARCH_FORWARD)
3383 lineno = 1;
3384 else
3385 lineno = s->nlines;
3388 offset = s->line_offsets[lineno - 1];
3389 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3390 free(line);
3391 return got_error_from_errno("fseeko");
3393 linelen = getline(&line, &linesize, s->f);
3394 if (linelen != -1 &&
3395 match_line(line, &view->regex, 1, &view->regmatch)) {
3396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3397 s->matched_line = lineno;
3398 break;
3400 if (view->searching == TOG_SEARCH_FORWARD)
3401 lineno++;
3402 else
3403 lineno--;
3405 free(line);
3407 if (s->matched_line) {
3408 s->first_displayed_line = s->matched_line;
3409 s->selected_line = 1;
3412 return NULL;
3415 static const struct got_error *
3416 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3417 struct got_object_id *id2, const char *label1, const char *label2,
3418 int diff_context, int ignore_whitespace, int force_text_diff,
3419 struct tog_view *log_view, struct got_repository *repo)
3421 const struct got_error *err;
3422 struct tog_diff_view_state *s = &view->state.diff;
3424 if (id1 != NULL && id2 != NULL) {
3425 int type1, type2;
3426 err = got_object_get_type(&type1, repo, id1);
3427 if (err)
3428 return err;
3429 err = got_object_get_type(&type2, repo, id2);
3430 if (err)
3431 return err;
3433 if (type1 != type2)
3434 return got_error(GOT_ERR_OBJ_TYPE);
3436 s->first_displayed_line = 1;
3437 s->last_displayed_line = view->nlines;
3438 s->selected_line = 1;
3439 s->repo = repo;
3440 s->id1 = id1;
3441 s->id2 = id2;
3442 s->label1 = label1;
3443 s->label2 = label2;
3445 if (id1) {
3446 s->id1 = got_object_id_dup(id1);
3447 if (s->id1 == NULL)
3448 return got_error_from_errno("got_object_id_dup");
3449 } else
3450 s->id1 = NULL;
3452 s->id2 = got_object_id_dup(id2);
3453 if (s->id2 == NULL) {
3454 free(s->id1);
3455 s->id1 = NULL;
3456 return got_error_from_errno("got_object_id_dup");
3458 s->f = NULL;
3459 s->first_displayed_line = 1;
3460 s->last_displayed_line = view->nlines;
3461 s->diff_context = diff_context;
3462 s->ignore_whitespace = ignore_whitespace;
3463 s->force_text_diff = force_text_diff;
3464 s->log_view = log_view;
3465 s->repo = repo;
3467 SIMPLEQ_INIT(&s->colors);
3468 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3469 err = add_color(&s->colors,
3470 "^-", TOG_COLOR_DIFF_MINUS,
3471 get_color_value("TOG_COLOR_DIFF_MINUS"));
3472 if (err)
3473 return err;
3474 err = add_color(&s->colors, "^\\+",
3475 TOG_COLOR_DIFF_PLUS,
3476 get_color_value("TOG_COLOR_DIFF_PLUS"));
3477 if (err) {
3478 free_colors(&s->colors);
3479 return err;
3481 err = add_color(&s->colors,
3482 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3483 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3484 if (err) {
3485 free_colors(&s->colors);
3486 return err;
3489 err = add_color(&s->colors,
3490 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3491 TOG_COLOR_DIFF_META,
3492 get_color_value("TOG_COLOR_DIFF_META"));
3493 if (err) {
3494 free_colors(&s->colors);
3495 return err;
3498 err = add_color(&s->colors,
3499 "^(from|via): ", TOG_COLOR_AUTHOR,
3500 get_color_value("TOG_COLOR_AUTHOR"));
3501 if (err) {
3502 free_colors(&s->colors);
3503 return err;
3506 err = add_color(&s->colors,
3507 "^date: ", TOG_COLOR_DATE,
3508 get_color_value("TOG_COLOR_DATE"));
3509 if (err) {
3510 free_colors(&s->colors);
3511 return err;
3515 if (log_view && view_is_splitscreen(view))
3516 show_log_view(log_view); /* draw vborder */
3517 diff_view_indicate_progress(view);
3519 s->line_offsets = NULL;
3520 s->nlines = 0;
3521 err = create_diff(s);
3522 if (err) {
3523 free(s->id1);
3524 s->id1 = NULL;
3525 free(s->id2);
3526 s->id2 = NULL;
3527 free_colors(&s->colors);
3528 return err;
3531 view->show = show_diff_view;
3532 view->input = input_diff_view;
3533 view->close = close_diff_view;
3534 view->search_start = search_start_diff_view;
3535 view->search_next = search_next_diff_view;
3537 return NULL;
3540 static const struct got_error *
3541 close_diff_view(struct tog_view *view)
3543 const struct got_error *err = NULL;
3544 struct tog_diff_view_state *s = &view->state.diff;
3546 free(s->id1);
3547 s->id1 = NULL;
3548 free(s->id2);
3549 s->id2 = NULL;
3550 if (s->f && fclose(s->f) == EOF)
3551 err = got_error_from_errno("fclose");
3552 free_colors(&s->colors);
3553 free(s->line_offsets);
3554 s->line_offsets = NULL;
3555 s->nlines = 0;
3556 return err;
3559 static const struct got_error *
3560 show_diff_view(struct tog_view *view)
3562 const struct got_error *err;
3563 struct tog_diff_view_state *s = &view->state.diff;
3564 char *id_str1 = NULL, *id_str2, *header;
3565 const char *label1, *label2;
3567 if (s->id1) {
3568 err = got_object_id_str(&id_str1, s->id1);
3569 if (err)
3570 return err;
3571 label1 = s->label1 ? : id_str1;
3572 } else
3573 label1 = "/dev/null";
3575 err = got_object_id_str(&id_str2, s->id2);
3576 if (err)
3577 return err;
3578 label2 = s->label2 ? : id_str2;
3580 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3581 err = got_error_from_errno("asprintf");
3582 free(id_str1);
3583 free(id_str2);
3584 return err;
3586 free(id_str1);
3587 free(id_str2);
3589 return draw_file(view, header);
3592 static const struct got_error *
3593 set_selected_commit(struct tog_diff_view_state *s,
3594 struct commit_queue_entry *entry)
3596 const struct got_error *err;
3597 const struct got_object_id_queue *parent_ids;
3598 struct got_commit_object *selected_commit;
3599 struct got_object_qid *pid;
3601 free(s->id2);
3602 s->id2 = got_object_id_dup(entry->id);
3603 if (s->id2 == NULL)
3604 return got_error_from_errno("got_object_id_dup");
3606 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3607 if (err)
3608 return err;
3609 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3610 free(s->id1);
3611 pid = SIMPLEQ_FIRST(parent_ids);
3612 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3613 got_object_commit_close(selected_commit);
3614 return NULL;
3617 static const struct got_error *
3618 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3620 const struct got_error *err = NULL;
3621 struct tog_diff_view_state *s = &view->state.diff;
3622 struct tog_log_view_state *ls;
3623 struct commit_queue_entry *old_selected_entry;
3624 char *line = NULL;
3625 size_t linesize = 0;
3626 ssize_t linelen;
3627 int i;
3629 switch (ch) {
3630 case 'a':
3631 case 'w':
3632 if (ch == 'a')
3633 s->force_text_diff = !s->force_text_diff;
3634 if (ch == 'w')
3635 s->ignore_whitespace = !s->ignore_whitespace;
3636 wclear(view->window);
3637 s->first_displayed_line = 1;
3638 s->last_displayed_line = view->nlines;
3639 diff_view_indicate_progress(view);
3640 err = create_diff(s);
3641 break;
3642 case 'k':
3643 case KEY_UP:
3644 if (s->first_displayed_line > 1)
3645 s->first_displayed_line--;
3646 break;
3647 case KEY_PPAGE:
3648 case CTRL('b'):
3649 if (s->first_displayed_line == 1)
3650 break;
3651 i = 0;
3652 while (i++ < view->nlines - 1 &&
3653 s->first_displayed_line > 1)
3654 s->first_displayed_line--;
3655 break;
3656 case 'j':
3657 case KEY_DOWN:
3658 if (!s->eof)
3659 s->first_displayed_line++;
3660 break;
3661 case KEY_NPAGE:
3662 case CTRL('f'):
3663 case ' ':
3664 if (s->eof)
3665 break;
3666 i = 0;
3667 while (!s->eof && i++ < view->nlines - 1) {
3668 linelen = getline(&line, &linesize, s->f);
3669 s->first_displayed_line++;
3670 if (linelen == -1) {
3671 if (feof(s->f)) {
3672 s->eof = 1;
3673 } else
3674 err = got_ferror(s->f, GOT_ERR_IO);
3675 break;
3678 free(line);
3679 break;
3680 case '[':
3681 if (s->diff_context > 0) {
3682 s->diff_context--;
3683 diff_view_indicate_progress(view);
3684 err = create_diff(s);
3685 if (s->first_displayed_line + view->nlines - 1 >
3686 s->nlines) {
3687 s->first_displayed_line = 1;
3688 s->last_displayed_line = view->nlines;
3691 break;
3692 case ']':
3693 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3694 s->diff_context++;
3695 diff_view_indicate_progress(view);
3696 err = create_diff(s);
3698 break;
3699 case '<':
3700 case ',':
3701 if (s->log_view == NULL)
3702 break;
3703 ls = &s->log_view->state.log;
3704 old_selected_entry = ls->selected_entry;
3706 err = input_log_view(NULL, s->log_view, KEY_UP);
3707 if (err)
3708 break;
3710 if (old_selected_entry == ls->selected_entry)
3711 break;
3713 err = set_selected_commit(s, ls->selected_entry);
3714 if (err)
3715 break;
3717 s->first_displayed_line = 1;
3718 s->last_displayed_line = view->nlines;
3720 diff_view_indicate_progress(view);
3721 err = create_diff(s);
3722 break;
3723 case '>':
3724 case '.':
3725 if (s->log_view == NULL)
3726 break;
3727 ls = &s->log_view->state.log;
3728 old_selected_entry = ls->selected_entry;
3730 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3731 if (err)
3732 break;
3734 if (old_selected_entry == ls->selected_entry)
3735 break;
3737 err = set_selected_commit(s, ls->selected_entry);
3738 if (err)
3739 break;
3741 s->first_displayed_line = 1;
3742 s->last_displayed_line = view->nlines;
3744 diff_view_indicate_progress(view);
3745 err = create_diff(s);
3746 break;
3747 default:
3748 break;
3751 return err;
3754 static const struct got_error *
3755 cmd_diff(int argc, char *argv[])
3757 const struct got_error *error = NULL;
3758 struct got_repository *repo = NULL;
3759 struct got_worktree *worktree = NULL;
3760 struct got_object_id *id1 = NULL, *id2 = NULL;
3761 char *repo_path = NULL, *cwd = NULL;
3762 char *id_str1 = NULL, *id_str2 = NULL;
3763 char *label1 = NULL, *label2 = NULL;
3764 int diff_context = 3, ignore_whitespace = 0;
3765 int ch, force_text_diff = 0;
3766 const char *errstr;
3767 struct tog_view *view;
3769 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3770 switch (ch) {
3771 case 'a':
3772 force_text_diff = 1;
3773 break;
3774 case 'C':
3775 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3776 &errstr);
3777 if (errstr != NULL)
3778 err(1, "-C option %s", errstr);
3779 break;
3780 case 'r':
3781 repo_path = realpath(optarg, NULL);
3782 if (repo_path == NULL)
3783 return got_error_from_errno2("realpath",
3784 optarg);
3785 got_path_strip_trailing_slashes(repo_path);
3786 break;
3787 case 'w':
3788 ignore_whitespace = 1;
3789 break;
3790 default:
3791 usage_diff();
3792 /* NOTREACHED */
3796 argc -= optind;
3797 argv += optind;
3799 if (argc == 0) {
3800 usage_diff(); /* TODO show local worktree changes */
3801 } else if (argc == 2) {
3802 id_str1 = argv[0];
3803 id_str2 = argv[1];
3804 } else
3805 usage_diff();
3807 if (repo_path == NULL) {
3808 cwd = getcwd(NULL, 0);
3809 if (cwd == NULL)
3810 return got_error_from_errno("getcwd");
3811 error = got_worktree_open(&worktree, cwd);
3812 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3813 goto done;
3814 if (worktree)
3815 repo_path =
3816 strdup(got_worktree_get_repo_path(worktree));
3817 else
3818 repo_path = strdup(cwd);
3819 if (repo_path == NULL) {
3820 error = got_error_from_errno("strdup");
3821 goto done;
3825 error = got_repo_open(&repo, repo_path, NULL);
3826 if (error)
3827 goto done;
3829 init_curses();
3831 error = apply_unveil(got_repo_get_path(repo), NULL);
3832 if (error)
3833 goto done;
3835 error = tog_load_refs(repo);
3836 if (error)
3837 goto done;
3839 error = got_repo_match_object_id(&id1, &label1, id_str1,
3840 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3841 if (error)
3842 goto done;
3844 error = got_repo_match_object_id(&id2, &label2, id_str2,
3845 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3846 if (error)
3847 goto done;
3849 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3850 if (view == NULL) {
3851 error = got_error_from_errno("view_open");
3852 goto done;
3854 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3855 ignore_whitespace, force_text_diff, NULL, repo);
3856 if (error)
3857 goto done;
3858 error = view_loop(view);
3859 done:
3860 free(label1);
3861 free(label2);
3862 free(repo_path);
3863 free(cwd);
3864 if (repo)
3865 got_repo_close(repo);
3866 if (worktree)
3867 got_worktree_close(worktree);
3868 tog_free_refs();
3869 return error;
3872 __dead static void
3873 usage_blame(void)
3875 endwin();
3876 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3877 getprogname());
3878 exit(1);
3881 struct tog_blame_line {
3882 int annotated;
3883 struct got_object_id *id;
3886 static const struct got_error *
3887 draw_blame(struct tog_view *view)
3889 struct tog_blame_view_state *s = &view->state.blame;
3890 struct tog_blame *blame = &s->blame;
3891 regmatch_t *regmatch = &view->regmatch;
3892 const struct got_error *err;
3893 int lineno = 0, nprinted = 0;
3894 char *line = NULL;
3895 size_t linesize = 0;
3896 ssize_t linelen;
3897 wchar_t *wline;
3898 int width;
3899 struct tog_blame_line *blame_line;
3900 struct got_object_id *prev_id = NULL;
3901 char *id_str;
3902 struct tog_color *tc;
3904 err = got_object_id_str(&id_str, s->blamed_commit->id);
3905 if (err)
3906 return err;
3908 rewind(blame->f);
3909 werase(view->window);
3911 if (asprintf(&line, "commit %s", id_str) == -1) {
3912 err = got_error_from_errno("asprintf");
3913 free(id_str);
3914 return err;
3917 err = format_line(&wline, &width, line, view->ncols, 0);
3918 free(line);
3919 line = NULL;
3920 if (err)
3921 return err;
3922 if (view_needs_focus_indication(view))
3923 wstandout(view->window);
3924 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3925 if (tc)
3926 wattr_on(view->window,
3927 COLOR_PAIR(tc->colorpair), NULL);
3928 waddwstr(view->window, wline);
3929 if (tc)
3930 wattr_off(view->window,
3931 COLOR_PAIR(tc->colorpair), NULL);
3932 if (view_needs_focus_indication(view))
3933 wstandend(view->window);
3934 free(wline);
3935 wline = NULL;
3936 if (width < view->ncols - 1)
3937 waddch(view->window, '\n');
3939 if (asprintf(&line, "[%d/%d] %s%s",
3940 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3941 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3942 free(id_str);
3943 return got_error_from_errno("asprintf");
3945 free(id_str);
3946 err = format_line(&wline, &width, line, view->ncols, 0);
3947 free(line);
3948 line = NULL;
3949 if (err)
3950 return err;
3951 waddwstr(view->window, wline);
3952 free(wline);
3953 wline = NULL;
3954 if (width < view->ncols - 1)
3955 waddch(view->window, '\n');
3957 s->eof = 0;
3958 while (nprinted < view->nlines - 2) {
3959 linelen = getline(&line, &linesize, blame->f);
3960 if (linelen == -1) {
3961 if (feof(blame->f)) {
3962 s->eof = 1;
3963 break;
3965 free(line);
3966 return got_ferror(blame->f, GOT_ERR_IO);
3968 if (++lineno < s->first_displayed_line)
3969 continue;
3971 if (view->focussed && nprinted == s->selected_line - 1)
3972 wstandout(view->window);
3974 if (blame->nlines > 0) {
3975 blame_line = &blame->lines[lineno - 1];
3976 if (blame_line->annotated && prev_id &&
3977 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3978 !(view->focussed &&
3979 nprinted == s->selected_line - 1)) {
3980 waddstr(view->window, " ");
3981 } else if (blame_line->annotated) {
3982 char *id_str;
3983 err = got_object_id_str(&id_str, blame_line->id);
3984 if (err) {
3985 free(line);
3986 return err;
3988 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3989 if (tc)
3990 wattr_on(view->window,
3991 COLOR_PAIR(tc->colorpair), NULL);
3992 wprintw(view->window, "%.8s", id_str);
3993 if (tc)
3994 wattr_off(view->window,
3995 COLOR_PAIR(tc->colorpair), NULL);
3996 free(id_str);
3997 prev_id = blame_line->id;
3998 } else {
3999 waddstr(view->window, "........");
4000 prev_id = NULL;
4002 } else {
4003 waddstr(view->window, "........");
4004 prev_id = NULL;
4007 if (view->focussed && nprinted == s->selected_line - 1)
4008 wstandend(view->window);
4009 waddstr(view->window, " ");
4011 if (view->ncols <= 9) {
4012 width = 9;
4013 wline = wcsdup(L"");
4014 if (wline == NULL) {
4015 err = got_error_from_errno("wcsdup");
4016 free(line);
4017 return err;
4019 } else if (s->first_displayed_line + nprinted ==
4020 s->matched_line &&
4021 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4022 err = add_matched_line(&width, line, view->ncols - 9, 9,
4023 view->window, regmatch);
4024 if (err) {
4025 free(line);
4026 return err;
4028 width += 9;
4029 } else {
4030 err = format_line(&wline, &width, line,
4031 view->ncols - 9, 9);
4032 waddwstr(view->window, wline);
4033 free(wline);
4034 wline = NULL;
4035 width += 9;
4038 if (width <= view->ncols - 1)
4039 waddch(view->window, '\n');
4040 if (++nprinted == 1)
4041 s->first_displayed_line = lineno;
4043 free(line);
4044 s->last_displayed_line = lineno;
4046 view_vborder(view);
4048 return NULL;
4051 static const struct got_error *
4052 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4054 const struct got_error *err = NULL;
4055 struct tog_blame_cb_args *a = arg;
4056 struct tog_blame_line *line;
4057 int errcode;
4059 if (nlines != a->nlines ||
4060 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4061 return got_error(GOT_ERR_RANGE);
4063 errcode = pthread_mutex_lock(&tog_mutex);
4064 if (errcode)
4065 return got_error_set_errno(errcode, "pthread_mutex_lock");
4067 if (*a->quit) { /* user has quit the blame view */
4068 err = got_error(GOT_ERR_ITER_COMPLETED);
4069 goto done;
4072 if (lineno == -1)
4073 goto done; /* no change in this commit */
4075 line = &a->lines[lineno - 1];
4076 if (line->annotated)
4077 goto done;
4079 line->id = got_object_id_dup(id);
4080 if (line->id == NULL) {
4081 err = got_error_from_errno("got_object_id_dup");
4082 goto done;
4084 line->annotated = 1;
4085 done:
4086 errcode = pthread_mutex_unlock(&tog_mutex);
4087 if (errcode)
4088 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4089 return err;
4092 static void *
4093 blame_thread(void *arg)
4095 const struct got_error *err;
4096 struct tog_blame_thread_args *ta = arg;
4097 struct tog_blame_cb_args *a = ta->cb_args;
4098 int errcode;
4100 err = block_signals_used_by_main_thread();
4101 if (err)
4102 return (void *)err;
4104 err = got_blame(ta->path, a->commit_id, ta->repo,
4105 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4106 if (err && err->code == GOT_ERR_CANCELLED)
4107 err = NULL;
4109 errcode = pthread_mutex_lock(&tog_mutex);
4110 if (errcode)
4111 return (void *)got_error_set_errno(errcode,
4112 "pthread_mutex_lock");
4114 got_repo_close(ta->repo);
4115 ta->repo = NULL;
4116 *ta->complete = 1;
4118 errcode = pthread_mutex_unlock(&tog_mutex);
4119 if (errcode && err == NULL)
4120 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4122 return (void *)err;
4125 static struct got_object_id *
4126 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4127 int first_displayed_line, int selected_line)
4129 struct tog_blame_line *line;
4131 if (nlines <= 0)
4132 return NULL;
4134 line = &lines[first_displayed_line - 1 + selected_line - 1];
4135 if (!line->annotated)
4136 return NULL;
4138 return line->id;
4141 static const struct got_error *
4142 stop_blame(struct tog_blame *blame)
4144 const struct got_error *err = NULL;
4145 int i;
4147 if (blame->thread) {
4148 int errcode;
4149 errcode = pthread_mutex_unlock(&tog_mutex);
4150 if (errcode)
4151 return got_error_set_errno(errcode,
4152 "pthread_mutex_unlock");
4153 errcode = pthread_join(blame->thread, (void **)&err);
4154 if (errcode)
4155 return got_error_set_errno(errcode, "pthread_join");
4156 errcode = pthread_mutex_lock(&tog_mutex);
4157 if (errcode)
4158 return got_error_set_errno(errcode,
4159 "pthread_mutex_lock");
4160 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4161 err = NULL;
4162 blame->thread = NULL;
4164 if (blame->thread_args.repo) {
4165 got_repo_close(blame->thread_args.repo);
4166 blame->thread_args.repo = NULL;
4168 if (blame->f) {
4169 if (fclose(blame->f) == EOF && err == NULL)
4170 err = got_error_from_errno("fclose");
4171 blame->f = NULL;
4173 if (blame->lines) {
4174 for (i = 0; i < blame->nlines; i++)
4175 free(blame->lines[i].id);
4176 free(blame->lines);
4177 blame->lines = NULL;
4179 free(blame->cb_args.commit_id);
4180 blame->cb_args.commit_id = NULL;
4182 return err;
4185 static const struct got_error *
4186 cancel_blame_view(void *arg)
4188 const struct got_error *err = NULL;
4189 int *done = arg;
4190 int errcode;
4192 errcode = pthread_mutex_lock(&tog_mutex);
4193 if (errcode)
4194 return got_error_set_errno(errcode,
4195 "pthread_mutex_unlock");
4197 if (*done)
4198 err = got_error(GOT_ERR_CANCELLED);
4200 errcode = pthread_mutex_unlock(&tog_mutex);
4201 if (errcode)
4202 return got_error_set_errno(errcode,
4203 "pthread_mutex_lock");
4205 return err;
4208 static const struct got_error *
4209 run_blame(struct tog_view *view)
4211 struct tog_blame_view_state *s = &view->state.blame;
4212 struct tog_blame *blame = &s->blame;
4213 const struct got_error *err = NULL;
4214 struct got_blob_object *blob = NULL;
4215 struct got_repository *thread_repo = NULL;
4216 struct got_object_id *obj_id = NULL;
4217 int obj_type;
4219 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4220 s->path);
4221 if (err)
4222 return err;
4224 err = got_object_get_type(&obj_type, s->repo, obj_id);
4225 if (err)
4226 goto done;
4228 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4229 err = got_error(GOT_ERR_OBJ_TYPE);
4230 goto done;
4233 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4234 if (err)
4235 goto done;
4236 blame->f = got_opentemp();
4237 if (blame->f == NULL) {
4238 err = got_error_from_errno("got_opentemp");
4239 goto done;
4241 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4242 &blame->line_offsets, blame->f, blob);
4243 if (err)
4244 goto done;
4245 if (blame->nlines == 0) {
4246 s->blame_complete = 1;
4247 goto done;
4250 /* Don't include \n at EOF in the blame line count. */
4251 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4252 blame->nlines--;
4254 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4255 if (blame->lines == NULL) {
4256 err = got_error_from_errno("calloc");
4257 goto done;
4260 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4261 if (err)
4262 goto done;
4264 blame->cb_args.view = view;
4265 blame->cb_args.lines = blame->lines;
4266 blame->cb_args.nlines = blame->nlines;
4267 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4268 if (blame->cb_args.commit_id == NULL) {
4269 err = got_error_from_errno("got_object_id_dup");
4270 goto done;
4272 blame->cb_args.quit = &s->done;
4274 blame->thread_args.path = s->path;
4275 blame->thread_args.repo = thread_repo;
4276 blame->thread_args.cb_args = &blame->cb_args;
4277 blame->thread_args.complete = &s->blame_complete;
4278 blame->thread_args.cancel_cb = cancel_blame_view;
4279 blame->thread_args.cancel_arg = &s->done;
4280 s->blame_complete = 0;
4282 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4283 s->first_displayed_line = 1;
4284 s->last_displayed_line = view->nlines;
4285 s->selected_line = 1;
4288 done:
4289 if (blob)
4290 got_object_blob_close(blob);
4291 free(obj_id);
4292 if (err)
4293 stop_blame(blame);
4294 return err;
4297 static const struct got_error *
4298 open_blame_view(struct tog_view *view, char *path,
4299 struct got_object_id *commit_id, struct got_repository *repo)
4301 const struct got_error *err = NULL;
4302 struct tog_blame_view_state *s = &view->state.blame;
4304 SIMPLEQ_INIT(&s->blamed_commits);
4306 s->path = strdup(path);
4307 if (s->path == NULL)
4308 return got_error_from_errno("strdup");
4310 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4311 if (err) {
4312 free(s->path);
4313 return err;
4316 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4317 s->first_displayed_line = 1;
4318 s->last_displayed_line = view->nlines;
4319 s->selected_line = 1;
4320 s->blame_complete = 0;
4321 s->repo = repo;
4322 s->commit_id = commit_id;
4323 memset(&s->blame, 0, sizeof(s->blame));
4325 SIMPLEQ_INIT(&s->colors);
4326 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4327 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4328 get_color_value("TOG_COLOR_COMMIT"));
4329 if (err)
4330 return err;
4333 view->show = show_blame_view;
4334 view->input = input_blame_view;
4335 view->close = close_blame_view;
4336 view->search_start = search_start_blame_view;
4337 view->search_next = search_next_blame_view;
4339 return run_blame(view);
4342 static const struct got_error *
4343 close_blame_view(struct tog_view *view)
4345 const struct got_error *err = NULL;
4346 struct tog_blame_view_state *s = &view->state.blame;
4348 if (s->blame.thread)
4349 err = stop_blame(&s->blame);
4351 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4352 struct got_object_qid *blamed_commit;
4353 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4354 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4355 got_object_qid_free(blamed_commit);
4358 free(s->path);
4359 free_colors(&s->colors);
4361 return err;
4364 static const struct got_error *
4365 search_start_blame_view(struct tog_view *view)
4367 struct tog_blame_view_state *s = &view->state.blame;
4369 s->matched_line = 0;
4370 return NULL;
4373 static const struct got_error *
4374 search_next_blame_view(struct tog_view *view)
4376 struct tog_blame_view_state *s = &view->state.blame;
4377 int lineno;
4378 char *line = NULL;
4379 size_t linesize = 0;
4380 ssize_t linelen;
4382 if (!view->searching) {
4383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 return NULL;
4387 if (s->matched_line) {
4388 if (view->searching == TOG_SEARCH_FORWARD)
4389 lineno = s->matched_line + 1;
4390 else
4391 lineno = s->matched_line - 1;
4392 } else {
4393 if (view->searching == TOG_SEARCH_FORWARD)
4394 lineno = 1;
4395 else
4396 lineno = s->blame.nlines;
4399 while (1) {
4400 off_t offset;
4402 if (lineno <= 0 || lineno > s->blame.nlines) {
4403 if (s->matched_line == 0) {
4404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 break;
4408 if (view->searching == TOG_SEARCH_FORWARD)
4409 lineno = 1;
4410 else
4411 lineno = s->blame.nlines;
4414 offset = s->blame.line_offsets[lineno - 1];
4415 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4416 free(line);
4417 return got_error_from_errno("fseeko");
4419 linelen = getline(&line, &linesize, s->blame.f);
4420 if (linelen != -1 &&
4421 match_line(line, &view->regex, 1, &view->regmatch)) {
4422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 s->matched_line = lineno;
4424 break;
4426 if (view->searching == TOG_SEARCH_FORWARD)
4427 lineno++;
4428 else
4429 lineno--;
4431 free(line);
4433 if (s->matched_line) {
4434 s->first_displayed_line = s->matched_line;
4435 s->selected_line = 1;
4438 return NULL;
4441 static const struct got_error *
4442 show_blame_view(struct tog_view *view)
4444 const struct got_error *err = NULL;
4445 struct tog_blame_view_state *s = &view->state.blame;
4446 int errcode;
4448 if (s->blame.thread == NULL && !s->blame_complete) {
4449 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4450 &s->blame.thread_args);
4451 if (errcode)
4452 return got_error_set_errno(errcode, "pthread_create");
4454 halfdelay(1); /* fast refresh while annotating */
4457 if (s->blame_complete)
4458 halfdelay(10); /* disable fast refresh */
4460 err = draw_blame(view);
4462 view_vborder(view);
4463 return err;
4466 static const struct got_error *
4467 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4469 const struct got_error *err = NULL, *thread_err = NULL;
4470 struct tog_view *diff_view;
4471 struct tog_blame_view_state *s = &view->state.blame;
4472 int begin_x = 0;
4474 switch (ch) {
4475 case 'q':
4476 s->done = 1;
4477 break;
4478 case 'k':
4479 case KEY_UP:
4480 if (s->selected_line > 1)
4481 s->selected_line--;
4482 else if (s->selected_line == 1 &&
4483 s->first_displayed_line > 1)
4484 s->first_displayed_line--;
4485 break;
4486 case KEY_PPAGE:
4487 case CTRL('b'):
4488 if (s->first_displayed_line == 1) {
4489 s->selected_line = 1;
4490 break;
4492 if (s->first_displayed_line > view->nlines - 2)
4493 s->first_displayed_line -=
4494 (view->nlines - 2);
4495 else
4496 s->first_displayed_line = 1;
4497 break;
4498 case 'j':
4499 case KEY_DOWN:
4500 if (s->selected_line < view->nlines - 2 &&
4501 s->first_displayed_line +
4502 s->selected_line <= s->blame.nlines)
4503 s->selected_line++;
4504 else if (s->last_displayed_line <
4505 s->blame.nlines)
4506 s->first_displayed_line++;
4507 break;
4508 case 'b':
4509 case 'p': {
4510 struct got_object_id *id = NULL;
4511 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4512 s->first_displayed_line, s->selected_line);
4513 if (id == NULL)
4514 break;
4515 if (ch == 'p') {
4516 struct got_commit_object *commit;
4517 struct got_object_qid *pid;
4518 struct got_object_id *blob_id = NULL;
4519 int obj_type;
4520 err = got_object_open_as_commit(&commit,
4521 s->repo, id);
4522 if (err)
4523 break;
4524 pid = SIMPLEQ_FIRST(
4525 got_object_commit_get_parent_ids(commit));
4526 if (pid == NULL) {
4527 got_object_commit_close(commit);
4528 break;
4530 /* Check if path history ends here. */
4531 err = got_object_id_by_path(&blob_id, s->repo,
4532 pid->id, s->path);
4533 if (err) {
4534 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4535 err = NULL;
4536 got_object_commit_close(commit);
4537 break;
4539 err = got_object_get_type(&obj_type, s->repo,
4540 blob_id);
4541 free(blob_id);
4542 /* Can't blame non-blob type objects. */
4543 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4544 got_object_commit_close(commit);
4545 break;
4547 err = got_object_qid_alloc(&s->blamed_commit,
4548 pid->id);
4549 got_object_commit_close(commit);
4550 } else {
4551 if (got_object_id_cmp(id,
4552 s->blamed_commit->id) == 0)
4553 break;
4554 err = got_object_qid_alloc(&s->blamed_commit,
4555 id);
4557 if (err)
4558 break;
4559 s->done = 1;
4560 thread_err = stop_blame(&s->blame);
4561 s->done = 0;
4562 if (thread_err)
4563 break;
4564 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4565 s->blamed_commit, entry);
4566 err = run_blame(view);
4567 if (err)
4568 break;
4569 break;
4571 case 'B': {
4572 struct got_object_qid *first;
4573 first = SIMPLEQ_FIRST(&s->blamed_commits);
4574 if (!got_object_id_cmp(first->id, s->commit_id))
4575 break;
4576 s->done = 1;
4577 thread_err = stop_blame(&s->blame);
4578 s->done = 0;
4579 if (thread_err)
4580 break;
4581 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4582 got_object_qid_free(s->blamed_commit);
4583 s->blamed_commit =
4584 SIMPLEQ_FIRST(&s->blamed_commits);
4585 err = run_blame(view);
4586 if (err)
4587 break;
4588 break;
4590 case KEY_ENTER:
4591 case '\r': {
4592 struct got_object_id *id = NULL;
4593 struct got_object_qid *pid;
4594 struct got_commit_object *commit = NULL;
4595 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4596 s->first_displayed_line, s->selected_line);
4597 if (id == NULL)
4598 break;
4599 err = got_object_open_as_commit(&commit, s->repo, id);
4600 if (err)
4601 break;
4602 pid = SIMPLEQ_FIRST(
4603 got_object_commit_get_parent_ids(commit));
4604 if (view_is_parent_view(view))
4605 begin_x = view_split_begin_x(view->begin_x);
4606 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4607 if (diff_view == NULL) {
4608 got_object_commit_close(commit);
4609 err = got_error_from_errno("view_open");
4610 break;
4612 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4613 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4614 got_object_commit_close(commit);
4615 if (err) {
4616 view_close(diff_view);
4617 break;
4619 view->focussed = 0;
4620 diff_view->focussed = 1;
4621 if (view_is_parent_view(view)) {
4622 err = view_close_child(view);
4623 if (err)
4624 break;
4625 view_set_child(view, diff_view);
4626 view->focus_child = 1;
4627 } else
4628 *new_view = diff_view;
4629 if (err)
4630 break;
4631 break;
4633 case KEY_NPAGE:
4634 case CTRL('f'):
4635 case ' ':
4636 if (s->last_displayed_line >= s->blame.nlines &&
4637 s->selected_line >= MIN(s->blame.nlines,
4638 view->nlines - 2)) {
4639 break;
4641 if (s->last_displayed_line >= s->blame.nlines &&
4642 s->selected_line < view->nlines - 2) {
4643 s->selected_line = MIN(s->blame.nlines,
4644 view->nlines - 2);
4645 break;
4647 if (s->last_displayed_line + view->nlines - 2
4648 <= s->blame.nlines)
4649 s->first_displayed_line +=
4650 view->nlines - 2;
4651 else
4652 s->first_displayed_line =
4653 s->blame.nlines -
4654 (view->nlines - 3);
4655 break;
4656 case KEY_RESIZE:
4657 if (s->selected_line > view->nlines - 2) {
4658 s->selected_line = MIN(s->blame.nlines,
4659 view->nlines - 2);
4661 break;
4662 default:
4663 break;
4665 return thread_err ? thread_err : err;
4668 static const struct got_error *
4669 cmd_blame(int argc, char *argv[])
4671 const struct got_error *error;
4672 struct got_repository *repo = NULL;
4673 struct got_worktree *worktree = NULL;
4674 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4675 char *link_target = NULL;
4676 struct got_object_id *commit_id = NULL;
4677 char *commit_id_str = NULL;
4678 int ch;
4679 struct tog_view *view;
4681 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4682 switch (ch) {
4683 case 'c':
4684 commit_id_str = optarg;
4685 break;
4686 case 'r':
4687 repo_path = realpath(optarg, NULL);
4688 if (repo_path == NULL)
4689 return got_error_from_errno2("realpath",
4690 optarg);
4691 break;
4692 default:
4693 usage_blame();
4694 /* NOTREACHED */
4698 argc -= optind;
4699 argv += optind;
4701 if (argc != 1)
4702 usage_blame();
4704 if (repo_path == NULL) {
4705 cwd = getcwd(NULL, 0);
4706 if (cwd == NULL)
4707 return got_error_from_errno("getcwd");
4708 error = got_worktree_open(&worktree, cwd);
4709 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4710 goto done;
4711 if (worktree)
4712 repo_path =
4713 strdup(got_worktree_get_repo_path(worktree));
4714 else
4715 repo_path = strdup(cwd);
4716 if (repo_path == NULL) {
4717 error = got_error_from_errno("strdup");
4718 goto done;
4722 error = got_repo_open(&repo, repo_path, NULL);
4723 if (error != NULL)
4724 goto done;
4726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4727 worktree);
4728 if (error)
4729 goto done;
4731 init_curses();
4733 error = apply_unveil(got_repo_get_path(repo), NULL);
4734 if (error)
4735 goto done;
4737 error = tog_load_refs(repo);
4738 if (error)
4739 goto done;
4741 if (commit_id_str == NULL) {
4742 struct got_reference *head_ref;
4743 error = got_ref_open(&head_ref, repo, worktree ?
4744 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4745 if (error != NULL)
4746 goto done;
4747 error = got_ref_resolve(&commit_id, repo, head_ref);
4748 got_ref_close(head_ref);
4749 } else {
4750 error = got_repo_match_object_id(&commit_id, NULL,
4751 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4753 if (error != NULL)
4754 goto done;
4756 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4757 if (view == NULL) {
4758 error = got_error_from_errno("view_open");
4759 goto done;
4762 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4763 commit_id, repo);
4764 if (error)
4765 goto done;
4767 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4768 commit_id, repo);
4769 if (error)
4770 goto done;
4771 if (worktree) {
4772 /* Release work tree lock. */
4773 got_worktree_close(worktree);
4774 worktree = NULL;
4776 error = view_loop(view);
4777 done:
4778 free(repo_path);
4779 free(in_repo_path);
4780 free(link_target);
4781 free(cwd);
4782 free(commit_id);
4783 if (worktree)
4784 got_worktree_close(worktree);
4785 if (repo)
4786 got_repo_close(repo);
4787 tog_free_refs();
4788 return error;
4791 static const struct got_error *
4792 draw_tree_entries(struct tog_view *view, const char *parent_path)
4794 struct tog_tree_view_state *s = &view->state.tree;
4795 const struct got_error *err = NULL;
4796 struct got_tree_entry *te;
4797 wchar_t *wline;
4798 struct tog_color *tc;
4799 int width, n, i, nentries;
4800 int limit = view->nlines;
4802 s->ndisplayed = 0;
4804 werase(view->window);
4806 if (limit == 0)
4807 return NULL;
4809 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4810 if (err)
4811 return err;
4812 if (view_needs_focus_indication(view))
4813 wstandout(view->window);
4814 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4815 if (tc)
4816 wattr_on(view->window,
4817 COLOR_PAIR(tc->colorpair), NULL);
4818 waddwstr(view->window, wline);
4819 if (tc)
4820 wattr_off(view->window,
4821 COLOR_PAIR(tc->colorpair), NULL);
4822 if (view_needs_focus_indication(view))
4823 wstandend(view->window);
4824 free(wline);
4825 wline = NULL;
4826 if (width < view->ncols - 1)
4827 waddch(view->window, '\n');
4828 if (--limit <= 0)
4829 return NULL;
4830 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4831 if (err)
4832 return err;
4833 waddwstr(view->window, wline);
4834 free(wline);
4835 wline = NULL;
4836 if (width < view->ncols - 1)
4837 waddch(view->window, '\n');
4838 if (--limit <= 0)
4839 return NULL;
4840 waddch(view->window, '\n');
4841 if (--limit <= 0)
4842 return NULL;
4844 if (s->first_displayed_entry == NULL) {
4845 te = got_object_tree_get_first_entry(s->tree);
4846 if (s->selected == 0) {
4847 if (view->focussed)
4848 wstandout(view->window);
4849 s->selected_entry = NULL;
4851 waddstr(view->window, " ..\n"); /* parent directory */
4852 if (s->selected == 0 && view->focussed)
4853 wstandend(view->window);
4854 s->ndisplayed++;
4855 if (--limit <= 0)
4856 return NULL;
4857 n = 1;
4858 } else {
4859 n = 0;
4860 te = s->first_displayed_entry;
4863 nentries = got_object_tree_get_nentries(s->tree);
4864 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4865 char *line = NULL, *id_str = NULL, *link_target = NULL;
4866 const char *modestr = "";
4867 mode_t mode;
4869 te = got_object_tree_get_entry(s->tree, i);
4870 mode = got_tree_entry_get_mode(te);
4872 if (s->show_ids) {
4873 err = got_object_id_str(&id_str,
4874 got_tree_entry_get_id(te));
4875 if (err)
4876 return got_error_from_errno(
4877 "got_object_id_str");
4879 if (got_object_tree_entry_is_submodule(te))
4880 modestr = "$";
4881 else if (S_ISLNK(mode)) {
4882 int i;
4884 err = got_tree_entry_get_symlink_target(&link_target,
4885 te, s->repo);
4886 if (err) {
4887 free(id_str);
4888 return err;
4890 for (i = 0; i < strlen(link_target); i++) {
4891 if (!isprint((unsigned char)link_target[i]))
4892 link_target[i] = '?';
4894 modestr = "@";
4896 else if (S_ISDIR(mode))
4897 modestr = "/";
4898 else if (mode & S_IXUSR)
4899 modestr = "*";
4900 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4901 got_tree_entry_get_name(te), modestr,
4902 link_target ? " -> ": "",
4903 link_target ? link_target : "") == -1) {
4904 free(id_str);
4905 free(link_target);
4906 return got_error_from_errno("asprintf");
4908 free(id_str);
4909 free(link_target);
4910 err = format_line(&wline, &width, line, view->ncols, 0);
4911 if (err) {
4912 free(line);
4913 break;
4915 if (n == s->selected) {
4916 if (view->focussed)
4917 wstandout(view->window);
4918 s->selected_entry = te;
4920 tc = match_color(&s->colors, line);
4921 if (tc)
4922 wattr_on(view->window,
4923 COLOR_PAIR(tc->colorpair), NULL);
4924 waddwstr(view->window, wline);
4925 if (tc)
4926 wattr_off(view->window,
4927 COLOR_PAIR(tc->colorpair), NULL);
4928 if (width < view->ncols - 1)
4929 waddch(view->window, '\n');
4930 if (n == s->selected && view->focussed)
4931 wstandend(view->window);
4932 free(line);
4933 free(wline);
4934 wline = NULL;
4935 n++;
4936 s->ndisplayed++;
4937 s->last_displayed_entry = te;
4938 if (--limit <= 0)
4939 break;
4942 return err;
4945 static void
4946 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4948 struct got_tree_entry *te;
4949 int isroot = s->tree == s->root;
4950 int i = 0;
4952 if (s->first_displayed_entry == NULL)
4953 return;
4955 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4956 while (i++ < maxscroll) {
4957 if (te == NULL) {
4958 if (!isroot)
4959 s->first_displayed_entry = NULL;
4960 break;
4962 s->first_displayed_entry = te;
4963 te = got_tree_entry_get_prev(s->tree, te);
4967 static void
4968 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4970 struct got_tree_entry *next, *last;
4971 int n = 0;
4973 if (s->first_displayed_entry)
4974 next = got_tree_entry_get_next(s->tree,
4975 s->first_displayed_entry);
4976 else
4977 next = got_object_tree_get_first_entry(s->tree);
4979 last = s->last_displayed_entry;
4980 while (next && last && n++ < maxscroll) {
4981 last = got_tree_entry_get_next(s->tree, last);
4982 if (last) {
4983 s->first_displayed_entry = next;
4984 next = got_tree_entry_get_next(s->tree, next);
4989 static const struct got_error *
4990 tree_entry_path(char **path, struct tog_parent_trees *parents,
4991 struct got_tree_entry *te)
4993 const struct got_error *err = NULL;
4994 struct tog_parent_tree *pt;
4995 size_t len = 2; /* for leading slash and NUL */
4997 TAILQ_FOREACH(pt, parents, entry)
4998 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4999 + 1 /* slash */;
5000 if (te)
5001 len += strlen(got_tree_entry_get_name(te));
5003 *path = calloc(1, len);
5004 if (path == NULL)
5005 return got_error_from_errno("calloc");
5007 (*path)[0] = '/';
5008 pt = TAILQ_LAST(parents, tog_parent_trees);
5009 while (pt) {
5010 const char *name = got_tree_entry_get_name(pt->selected_entry);
5011 if (strlcat(*path, name, len) >= len) {
5012 err = got_error(GOT_ERR_NO_SPACE);
5013 goto done;
5015 if (strlcat(*path, "/", len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5017 goto done;
5019 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5021 if (te) {
5022 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5024 goto done;
5027 done:
5028 if (err) {
5029 free(*path);
5030 *path = NULL;
5032 return err;
5035 static const struct got_error *
5036 blame_tree_entry(struct tog_view **new_view, int begin_x,
5037 struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 struct got_object_id *commit_id, struct got_repository *repo)
5040 const struct got_error *err = NULL;
5041 char *path;
5042 struct tog_view *blame_view;
5044 *new_view = NULL;
5046 err = tree_entry_path(&path, parents, te);
5047 if (err)
5048 return err;
5050 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5051 if (blame_view == NULL) {
5052 err = got_error_from_errno("view_open");
5053 goto done;
5056 err = open_blame_view(blame_view, path, commit_id, repo);
5057 if (err) {
5058 if (err->code == GOT_ERR_CANCELLED)
5059 err = NULL;
5060 view_close(blame_view);
5061 } else
5062 *new_view = blame_view;
5063 done:
5064 free(path);
5065 return err;
5068 static const struct got_error *
5069 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5070 struct tog_tree_view_state *s)
5072 struct tog_view *log_view;
5073 const struct got_error *err = NULL;
5074 char *path;
5076 *new_view = NULL;
5078 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5079 if (log_view == NULL)
5080 return got_error_from_errno("view_open");
5082 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5083 if (err)
5084 return err;
5086 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5087 path, 0);
5088 if (err)
5089 view_close(log_view);
5090 else
5091 *new_view = log_view;
5092 free(path);
5093 return err;
5096 static const struct got_error *
5097 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5098 struct got_object_id *commit_id, const char *head_ref_name,
5099 struct got_repository *repo)
5101 const struct got_error *err = NULL;
5102 char *commit_id_str = NULL;
5103 struct tog_tree_view_state *s = &view->state.tree;
5105 TAILQ_INIT(&s->parents);
5107 err = got_object_id_str(&commit_id_str, commit_id);
5108 if (err != NULL)
5109 goto done;
5111 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5112 err = got_error_from_errno("asprintf");
5113 goto done;
5116 s->root = s->tree = root;
5117 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5118 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5119 s->commit_id = got_object_id_dup(commit_id);
5120 if (s->commit_id == NULL) {
5121 err = got_error_from_errno("got_object_id_dup");
5122 goto done;
5124 if (head_ref_name) {
5125 s->head_ref_name = strdup(head_ref_name);
5126 if (s->head_ref_name == NULL) {
5127 err = got_error_from_errno("strdup");
5128 goto done;
5131 s->repo = repo;
5133 SIMPLEQ_INIT(&s->colors);
5135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5136 err = add_color(&s->colors, "\\$$",
5137 TOG_COLOR_TREE_SUBMODULE,
5138 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5139 if (err)
5140 goto done;
5141 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5142 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5143 if (err) {
5144 free_colors(&s->colors);
5145 goto done;
5147 err = add_color(&s->colors, "/$",
5148 TOG_COLOR_TREE_DIRECTORY,
5149 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5150 if (err) {
5151 free_colors(&s->colors);
5152 goto done;
5155 err = add_color(&s->colors, "\\*$",
5156 TOG_COLOR_TREE_EXECUTABLE,
5157 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5158 if (err) {
5159 free_colors(&s->colors);
5160 goto done;
5163 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5164 get_color_value("TOG_COLOR_COMMIT"));
5165 if (err) {
5166 free_colors(&s->colors);
5167 goto done;
5171 view->show = show_tree_view;
5172 view->input = input_tree_view;
5173 view->close = close_tree_view;
5174 view->search_start = search_start_tree_view;
5175 view->search_next = search_next_tree_view;
5176 done:
5177 free(commit_id_str);
5178 if (err) {
5179 free(s->tree_label);
5180 s->tree_label = NULL;
5182 return err;
5185 static const struct got_error *
5186 close_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5190 free_colors(&s->colors);
5191 free(s->tree_label);
5192 s->tree_label = NULL;
5193 free(s->commit_id);
5194 s->commit_id = NULL;
5195 free(s->head_ref_name);
5196 s->head_ref_name = NULL;
5197 while (!TAILQ_EMPTY(&s->parents)) {
5198 struct tog_parent_tree *parent;
5199 parent = TAILQ_FIRST(&s->parents);
5200 TAILQ_REMOVE(&s->parents, parent, entry);
5201 free(parent);
5204 if (s->tree != s->root)
5205 got_object_tree_close(s->tree);
5206 got_object_tree_close(s->root);
5207 return NULL;
5210 static const struct got_error *
5211 search_start_tree_view(struct tog_view *view)
5213 struct tog_tree_view_state *s = &view->state.tree;
5215 s->matched_entry = NULL;
5216 return NULL;
5219 static int
5220 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5222 regmatch_t regmatch;
5224 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5225 0) == 0;
5228 static const struct got_error *
5229 search_next_tree_view(struct tog_view *view)
5231 struct tog_tree_view_state *s = &view->state.tree;
5232 struct got_tree_entry *te = NULL;
5234 if (!view->searching) {
5235 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 return NULL;
5239 if (s->matched_entry) {
5240 if (view->searching == TOG_SEARCH_FORWARD) {
5241 if (s->selected_entry)
5242 te = got_tree_entry_get_next(s->tree,
5243 s->selected_entry);
5244 else
5245 te = got_object_tree_get_first_entry(s->tree);
5246 } else {
5247 if (s->selected_entry == NULL)
5248 te = got_object_tree_get_last_entry(s->tree);
5249 else
5250 te = got_tree_entry_get_prev(s->tree,
5251 s->selected_entry);
5253 } else {
5254 if (view->searching == TOG_SEARCH_FORWARD)
5255 te = got_object_tree_get_first_entry(s->tree);
5256 else
5257 te = got_object_tree_get_last_entry(s->tree);
5260 while (1) {
5261 if (te == NULL) {
5262 if (s->matched_entry == NULL) {
5263 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 return NULL;
5266 if (view->searching == TOG_SEARCH_FORWARD)
5267 te = got_object_tree_get_first_entry(s->tree);
5268 else
5269 te = got_object_tree_get_last_entry(s->tree);
5272 if (match_tree_entry(te, &view->regex)) {
5273 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5274 s->matched_entry = te;
5275 break;
5278 if (view->searching == TOG_SEARCH_FORWARD)
5279 te = got_tree_entry_get_next(s->tree, te);
5280 else
5281 te = got_tree_entry_get_prev(s->tree, te);
5284 if (s->matched_entry) {
5285 s->first_displayed_entry = s->matched_entry;
5286 s->selected = 0;
5289 return NULL;
5292 static const struct got_error *
5293 show_tree_view(struct tog_view *view)
5295 const struct got_error *err = NULL;
5296 struct tog_tree_view_state *s = &view->state.tree;
5297 char *parent_path;
5299 err = tree_entry_path(&parent_path, &s->parents, NULL);
5300 if (err)
5301 return err;
5303 err = draw_tree_entries(view, parent_path);
5304 free(parent_path);
5306 view_vborder(view);
5307 return err;
5310 static const struct got_error *
5311 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5313 const struct got_error *err = NULL;
5314 struct tog_tree_view_state *s = &view->state.tree;
5315 struct tog_view *log_view, *ref_view;
5316 int begin_x = 0;
5318 switch (ch) {
5319 case 'i':
5320 s->show_ids = !s->show_ids;
5321 break;
5322 case 'l':
5323 if (!s->selected_entry)
5324 break;
5325 if (view_is_parent_view(view))
5326 begin_x = view_split_begin_x(view->begin_x);
5327 err = log_selected_tree_entry(&log_view, begin_x, s);
5328 view->focussed = 0;
5329 log_view->focussed = 1;
5330 if (view_is_parent_view(view)) {
5331 err = view_close_child(view);
5332 if (err)
5333 return err;
5334 view_set_child(view, log_view);
5335 view->focus_child = 1;
5336 } else
5337 *new_view = log_view;
5338 break;
5339 case 'r':
5340 if (view_is_parent_view(view))
5341 begin_x = view_split_begin_x(view->begin_x);
5342 ref_view = view_open(view->nlines, view->ncols,
5343 view->begin_y, begin_x, TOG_VIEW_REF);
5344 if (ref_view == NULL)
5345 return got_error_from_errno("view_open");
5346 err = open_ref_view(ref_view, s->repo);
5347 if (err) {
5348 view_close(ref_view);
5349 return err;
5351 view->focussed = 0;
5352 ref_view->focussed = 1;
5353 if (view_is_parent_view(view)) {
5354 err = view_close_child(view);
5355 if (err)
5356 return err;
5357 view_set_child(view, ref_view);
5358 view->focus_child = 1;
5359 } else
5360 *new_view = ref_view;
5361 break;
5362 case 'k':
5363 case KEY_UP:
5364 if (s->selected > 0) {
5365 s->selected--;
5366 break;
5368 tree_scroll_up(s, 1);
5369 break;
5370 case KEY_PPAGE:
5371 case CTRL('b'):
5372 if (s->tree == s->root) {
5373 if (got_object_tree_get_first_entry(s->tree) ==
5374 s->first_displayed_entry)
5375 s->selected = 0;
5376 } else {
5377 if (s->first_displayed_entry == NULL)
5378 s->selected = 0;
5380 tree_scroll_up(s, MAX(0, view->nlines - 3));
5381 break;
5382 case 'j':
5383 case KEY_DOWN:
5384 if (s->selected < s->ndisplayed - 1) {
5385 s->selected++;
5386 break;
5388 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5389 == NULL)
5390 /* can't scroll any further */
5391 break;
5392 tree_scroll_down(s, 1);
5393 break;
5394 case KEY_NPAGE:
5395 case CTRL('f'):
5396 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5397 == NULL) {
5398 /* can't scroll any further; move cursor down */
5399 if (s->selected < s->ndisplayed - 1)
5400 s->selected = s->ndisplayed - 1;
5401 break;
5403 tree_scroll_down(s, view->nlines - 3);
5404 break;
5405 case KEY_ENTER:
5406 case '\r':
5407 case KEY_BACKSPACE:
5408 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5409 struct tog_parent_tree *parent;
5410 /* user selected '..' */
5411 if (s->tree == s->root)
5412 break;
5413 parent = TAILQ_FIRST(&s->parents);
5414 TAILQ_REMOVE(&s->parents, parent,
5415 entry);
5416 got_object_tree_close(s->tree);
5417 s->tree = parent->tree;
5418 s->first_displayed_entry =
5419 parent->first_displayed_entry;
5420 s->selected_entry =
5421 parent->selected_entry;
5422 s->selected = parent->selected;
5423 free(parent);
5424 } else if (S_ISDIR(got_tree_entry_get_mode(
5425 s->selected_entry))) {
5426 struct got_tree_object *subtree;
5427 err = got_object_open_as_tree(&subtree, s->repo,
5428 got_tree_entry_get_id(s->selected_entry));
5429 if (err)
5430 break;
5431 err = tree_view_visit_subtree(s, subtree);
5432 if (err) {
5433 got_object_tree_close(subtree);
5434 break;
5436 } else if (S_ISREG(got_tree_entry_get_mode(
5437 s->selected_entry))) {
5438 struct tog_view *blame_view;
5439 int begin_x = view_is_parent_view(view) ?
5440 view_split_begin_x(view->begin_x) : 0;
5442 err = blame_tree_entry(&blame_view, begin_x,
5443 s->selected_entry, &s->parents,
5444 s->commit_id, s->repo);
5445 if (err)
5446 break;
5447 view->focussed = 0;
5448 blame_view->focussed = 1;
5449 if (view_is_parent_view(view)) {
5450 err = view_close_child(view);
5451 if (err)
5452 return err;
5453 view_set_child(view, blame_view);
5454 view->focus_child = 1;
5455 } else
5456 *new_view = blame_view;
5458 break;
5459 case KEY_RESIZE:
5460 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5461 s->selected = view->nlines - 4;
5462 break;
5463 default:
5464 break;
5467 return err;
5470 __dead static void
5471 usage_tree(void)
5473 endwin();
5474 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5475 getprogname());
5476 exit(1);
5479 static const struct got_error *
5480 cmd_tree(int argc, char *argv[])
5482 const struct got_error *error;
5483 struct got_repository *repo = NULL;
5484 struct got_worktree *worktree = NULL;
5485 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5486 struct got_object_id *commit_id = NULL;
5487 const char *commit_id_arg = NULL;
5488 char *label = NULL;
5489 struct got_commit_object *commit = NULL;
5490 struct got_tree_object *tree = NULL;
5491 struct got_reference *ref = NULL;
5492 const char *head_ref_name = NULL;
5493 int ch;
5494 struct tog_view *view;
5496 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5497 switch (ch) {
5498 case 'c':
5499 commit_id_arg = optarg;
5500 break;
5501 case 'r':
5502 repo_path = realpath(optarg, NULL);
5503 if (repo_path == NULL)
5504 return got_error_from_errno2("realpath",
5505 optarg);
5506 break;
5507 default:
5508 usage_tree();
5509 /* NOTREACHED */
5513 argc -= optind;
5514 argv += optind;
5516 if (argc > 1)
5517 usage_tree();
5519 if (repo_path == NULL) {
5520 cwd = getcwd(NULL, 0);
5521 if (cwd == NULL)
5522 return got_error_from_errno("getcwd");
5523 error = got_worktree_open(&worktree, cwd);
5524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5525 goto done;
5526 if (worktree)
5527 repo_path =
5528 strdup(got_worktree_get_repo_path(worktree));
5529 else
5530 repo_path = strdup(cwd);
5531 if (repo_path == NULL) {
5532 error = got_error_from_errno("strdup");
5533 goto done;
5537 error = got_repo_open(&repo, repo_path, NULL);
5538 if (error != NULL)
5539 goto done;
5541 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5542 repo, worktree);
5543 if (error)
5544 goto done;
5546 init_curses();
5548 error = apply_unveil(got_repo_get_path(repo), NULL);
5549 if (error)
5550 goto done;
5552 error = tog_load_refs(repo);
5553 if (error)
5554 goto done;
5556 if (commit_id_arg == NULL) {
5557 error = got_repo_match_object_id(&commit_id, &label,
5558 worktree ? got_worktree_get_head_ref_name(worktree) :
5559 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5560 if (error)
5561 goto done;
5562 head_ref_name = label;
5563 } else {
5564 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5565 if (error == NULL)
5566 head_ref_name = got_ref_get_name(ref);
5567 else if (error->code != GOT_ERR_NOT_REF)
5568 goto done;
5569 error = got_repo_match_object_id(&commit_id, NULL,
5570 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5571 if (error)
5572 goto done;
5575 error = got_object_open_as_commit(&commit, repo, commit_id);
5576 if (error)
5577 goto done;
5579 error = got_object_open_as_tree(&tree, repo,
5580 got_object_commit_get_tree_id(commit));
5581 if (error)
5582 goto done;
5584 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5585 if (view == NULL) {
5586 error = got_error_from_errno("view_open");
5587 goto done;
5589 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5590 if (error)
5591 goto done;
5592 if (!got_path_is_root_dir(in_repo_path)) {
5593 error = tree_view_walk_path(&view->state.tree, commit_id,
5594 in_repo_path);
5595 if (error)
5596 goto done;
5599 if (worktree) {
5600 /* Release work tree lock. */
5601 got_worktree_close(worktree);
5602 worktree = NULL;
5604 error = view_loop(view);
5605 done:
5606 free(repo_path);
5607 free(cwd);
5608 free(commit_id);
5609 free(label);
5610 if (ref)
5611 got_ref_close(ref);
5612 if (commit)
5613 got_object_commit_close(commit);
5614 if (tree)
5615 got_object_tree_close(tree);
5616 if (repo)
5617 got_repo_close(repo);
5618 tog_free_refs();
5619 return error;
5622 static const struct got_error *
5623 ref_view_load_refs(struct tog_ref_view_state *s)
5625 struct got_reflist_entry *sre;
5626 struct tog_reflist_entry *re;
5628 s->nrefs = 0;
5629 TAILQ_FOREACH(sre, &tog_refs, entry) {
5630 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5631 continue;
5633 re = malloc(sizeof(*re));
5634 if (re == NULL)
5635 return got_error_from_errno("malloc");
5637 re->ref = got_ref_dup(sre->ref);
5638 if (re->ref == NULL)
5639 return got_error_from_errno("got_ref_dup");
5640 re->idx = s->nrefs++;
5641 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5644 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5645 return NULL;
5648 void
5649 ref_view_free_refs(struct tog_ref_view_state *s)
5651 struct tog_reflist_entry *re;
5653 while (!TAILQ_EMPTY(&s->refs)) {
5654 re = TAILQ_FIRST(&s->refs);
5655 TAILQ_REMOVE(&s->refs, re, entry);
5656 got_ref_close(re->ref);
5657 free(re);
5661 static const struct got_error *
5662 open_ref_view(struct tog_view *view, struct got_repository *repo)
5664 const struct got_error *err = NULL;
5665 struct tog_ref_view_state *s = &view->state.ref;
5667 s->selected_entry = 0;
5668 s->repo = repo;
5670 TAILQ_INIT(&s->refs);
5671 SIMPLEQ_INIT(&s->colors);
5673 err = ref_view_load_refs(s);
5674 if (err)
5675 return err;
5677 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5678 err = add_color(&s->colors, "^refs/heads/",
5679 TOG_COLOR_REFS_HEADS,
5680 get_color_value("TOG_COLOR_REFS_HEADS"));
5681 if (err)
5682 goto done;
5684 err = add_color(&s->colors, "^refs/tags/",
5685 TOG_COLOR_REFS_TAGS,
5686 get_color_value("TOG_COLOR_REFS_TAGS"));
5687 if (err)
5688 goto done;
5690 err = add_color(&s->colors, "^refs/remotes/",
5691 TOG_COLOR_REFS_REMOTES,
5692 get_color_value("TOG_COLOR_REFS_REMOTES"));
5693 if (err)
5694 goto done;
5697 view->show = show_ref_view;
5698 view->input = input_ref_view;
5699 view->close = close_ref_view;
5700 view->search_start = search_start_ref_view;
5701 view->search_next = search_next_ref_view;
5702 done:
5703 if (err)
5704 free_colors(&s->colors);
5705 return err;
5708 static const struct got_error *
5709 close_ref_view(struct tog_view *view)
5711 struct tog_ref_view_state *s = &view->state.ref;
5713 ref_view_free_refs(s);
5714 free_colors(&s->colors);
5716 return NULL;
5719 static const struct got_error *
5720 resolve_reflist_entry(struct got_object_id **commit_id,
5721 struct tog_reflist_entry *re, struct got_repository *repo)
5723 const struct got_error *err = NULL;
5724 struct got_object_id *obj_id;
5725 struct got_tag_object *tag = NULL;
5726 int obj_type;
5728 *commit_id = NULL;
5730 err = got_ref_resolve(&obj_id, repo, re->ref);
5731 if (err)
5732 return err;
5734 err = got_object_get_type(&obj_type, repo, obj_id);
5735 if (err)
5736 goto done;
5738 switch (obj_type) {
5739 case GOT_OBJ_TYPE_COMMIT:
5740 *commit_id = obj_id;
5741 break;
5742 case GOT_OBJ_TYPE_TAG:
5743 err = got_object_open_as_tag(&tag, repo, obj_id);
5744 if (err)
5745 goto done;
5746 free(obj_id);
5747 err = got_object_get_type(&obj_type, repo,
5748 got_object_tag_get_object_id(tag));
5749 if (err)
5750 goto done;
5751 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5752 err = got_error(GOT_ERR_OBJ_TYPE);
5753 goto done;
5755 *commit_id = got_object_id_dup(
5756 got_object_tag_get_object_id(tag));
5757 if (*commit_id == NULL) {
5758 err = got_error_from_errno("got_object_id_dup");
5759 goto done;
5761 break;
5762 default:
5763 err = got_error(GOT_ERR_OBJ_TYPE);
5764 break;
5767 done:
5768 if (tag)
5769 got_object_tag_close(tag);
5770 if (err) {
5771 free(*commit_id);
5772 *commit_id = NULL;
5774 return err;
5777 static const struct got_error *
5778 log_ref_entry(struct tog_view **new_view, int begin_x,
5779 struct tog_reflist_entry *re, struct got_repository *repo)
5781 struct tog_view *log_view;
5782 const struct got_error *err = NULL;
5783 struct got_object_id *commit_id = NULL;
5785 *new_view = NULL;
5787 err = resolve_reflist_entry(&commit_id, re, repo);
5788 if (err) {
5789 if (err->code != GOT_ERR_OBJ_TYPE)
5790 return err;
5791 else
5792 return NULL;
5795 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5796 if (log_view == NULL) {
5797 err = got_error_from_errno("view_open");
5798 goto done;
5801 err = open_log_view(log_view, commit_id, repo,
5802 got_ref_get_name(re->ref), "", 0);
5803 done:
5804 if (err)
5805 view_close(log_view);
5806 else
5807 *new_view = log_view;
5808 free(commit_id);
5809 return err;
5812 static void
5813 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5815 struct tog_reflist_entry *re;
5816 int i = 0;
5818 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5819 return;
5821 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5822 while (i++ < maxscroll) {
5823 if (re == NULL)
5824 break;
5825 s->first_displayed_entry = re;
5826 re = TAILQ_PREV(re, tog_reflist_head, entry);
5830 static void
5831 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5833 struct tog_reflist_entry *next, *last;
5834 int n = 0;
5836 if (s->first_displayed_entry)
5837 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5838 else
5839 next = TAILQ_FIRST(&s->refs);
5841 last = s->last_displayed_entry;
5842 while (next && last && n++ < maxscroll) {
5843 last = TAILQ_NEXT(last, entry);
5844 if (last) {
5845 s->first_displayed_entry = next;
5846 next = TAILQ_NEXT(next, entry);
5851 static const struct got_error *
5852 search_start_ref_view(struct tog_view *view)
5854 struct tog_ref_view_state *s = &view->state.ref;
5856 s->matched_entry = NULL;
5857 return NULL;
5860 static int
5861 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5863 regmatch_t regmatch;
5865 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5866 0) == 0;
5869 static const struct got_error *
5870 search_next_ref_view(struct tog_view *view)
5872 struct tog_ref_view_state *s = &view->state.ref;
5873 struct tog_reflist_entry *re = NULL;
5875 if (!view->searching) {
5876 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 return NULL;
5880 if (s->matched_entry) {
5881 if (view->searching == TOG_SEARCH_FORWARD) {
5882 if (s->selected_entry)
5883 re = TAILQ_NEXT(s->selected_entry, entry);
5884 else
5885 re = TAILQ_PREV(s->selected_entry,
5886 tog_reflist_head, entry);
5887 } else {
5888 if (s->selected_entry == NULL)
5889 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5890 else
5891 re = TAILQ_PREV(s->selected_entry,
5892 tog_reflist_head, entry);
5894 } else {
5895 if (view->searching == TOG_SEARCH_FORWARD)
5896 re = TAILQ_FIRST(&s->refs);
5897 else
5898 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5901 while (1) {
5902 if (re == NULL) {
5903 if (s->matched_entry == NULL) {
5904 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5905 return NULL;
5907 if (view->searching == TOG_SEARCH_FORWARD)
5908 re = TAILQ_FIRST(&s->refs);
5909 else
5910 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5913 if (match_reflist_entry(re, &view->regex)) {
5914 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5915 s->matched_entry = re;
5916 break;
5919 if (view->searching == TOG_SEARCH_FORWARD)
5920 re = TAILQ_NEXT(re, entry);
5921 else
5922 re = TAILQ_PREV(re, tog_reflist_head, entry);
5925 if (s->matched_entry) {
5926 s->first_displayed_entry = s->matched_entry;
5927 s->selected = 0;
5930 return NULL;
5933 static const struct got_error *
5934 show_ref_view(struct tog_view *view)
5936 const struct got_error *err = NULL;
5937 struct tog_ref_view_state *s = &view->state.ref;
5938 struct tog_reflist_entry *re;
5939 char *line = NULL;
5940 wchar_t *wline;
5941 struct tog_color *tc;
5942 int width, n;
5943 int limit = view->nlines;
5945 werase(view->window);
5947 s->ndisplayed = 0;
5949 if (limit == 0)
5950 return NULL;
5952 re = s->first_displayed_entry;
5954 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5955 s->nrefs) == -1)
5956 return got_error_from_errno("asprintf");
5958 err = format_line(&wline, &width, line, view->ncols, 0);
5959 if (err) {
5960 free(line);
5961 return err;
5963 if (view_needs_focus_indication(view))
5964 wstandout(view->window);
5965 waddwstr(view->window, wline);
5966 if (view_needs_focus_indication(view))
5967 wstandend(view->window);
5968 free(wline);
5969 wline = NULL;
5970 free(line);
5971 line = NULL;
5972 if (width < view->ncols - 1)
5973 waddch(view->window, '\n');
5974 if (--limit <= 0)
5975 return NULL;
5977 n = 0;
5978 while (re && limit > 0) {
5979 char *line = NULL;
5981 if (got_ref_is_symbolic(re->ref)) {
5982 if (asprintf(&line, "%s -> %s",
5983 got_ref_get_name(re->ref),
5984 got_ref_get_symref_target(re->ref)) == -1)
5985 return got_error_from_errno("asprintf");
5986 } else if (s->show_ids) {
5987 struct got_object_id *id;
5988 char *id_str;
5989 err = got_ref_resolve(&id, s->repo, re->ref);
5990 if (err)
5991 return err;
5992 err = got_object_id_str(&id_str, id);
5993 if (err) {
5994 free(id);
5995 return err;
5997 if (asprintf(&line, "%s: %s",
5998 got_ref_get_name(re->ref), id_str) == -1) {
5999 err = got_error_from_errno("asprintf");
6000 free(id);
6001 free(id_str);
6002 return err;
6004 free(id);
6005 free(id_str);
6006 } else {
6007 line = strdup(got_ref_get_name(re->ref));
6008 if (line == NULL)
6009 return got_error_from_errno("strdup");
6012 err = format_line(&wline, &width, line, view->ncols, 0);
6013 if (err) {
6014 free(line);
6015 return err;
6017 if (n == s->selected) {
6018 if (view->focussed)
6019 wstandout(view->window);
6020 s->selected_entry = re;
6022 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6023 if (tc)
6024 wattr_on(view->window,
6025 COLOR_PAIR(tc->colorpair), NULL);
6026 waddwstr(view->window, wline);
6027 if (tc)
6028 wattr_off(view->window,
6029 COLOR_PAIR(tc->colorpair), NULL);
6030 if (width < view->ncols - 1)
6031 waddch(view->window, '\n');
6032 if (n == s->selected && view->focussed)
6033 wstandend(view->window);
6034 free(line);
6035 free(wline);
6036 wline = NULL;
6037 n++;
6038 s->ndisplayed++;
6039 s->last_displayed_entry = re;
6041 limit--;
6042 re = TAILQ_NEXT(re, entry);
6045 view_vborder(view);
6046 return err;
6049 static const struct got_error *
6050 browse_ref_tree(struct tog_view **new_view, int begin_x,
6051 struct tog_reflist_entry *re, struct got_repository *repo)
6053 const struct got_error *err = NULL;
6054 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6055 struct got_tree_object *tree = NULL;
6056 struct tog_view *tree_view;
6058 *new_view = NULL;
6060 err = resolve_reflist_entry(&commit_id, re, repo);
6061 if (err) {
6062 if (err->code != GOT_ERR_OBJ_TYPE)
6063 return err;
6064 else
6065 return NULL;
6068 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6069 if (err)
6070 goto done;
6072 err = got_object_open_as_tree(&tree, repo, tree_id);
6073 if (err)
6074 goto done;
6076 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6077 if (tree_view == NULL) {
6078 err = got_error_from_errno("view_open");
6079 goto done;
6082 err = open_tree_view(tree_view, tree, commit_id,
6083 got_ref_get_name(re->ref), repo);
6084 if (err)
6085 goto done;
6087 *new_view = tree_view;
6088 done:
6089 free(commit_id);
6090 free(tree_id);
6091 if (err) {
6092 if (tree)
6093 got_object_tree_close(tree);
6095 return err;
6097 static const struct got_error *
6098 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6100 const struct got_error *err = NULL;
6101 struct tog_ref_view_state *s = &view->state.ref;
6102 struct tog_view *log_view, *tree_view;
6103 int begin_x = 0;
6105 switch (ch) {
6106 case 'i':
6107 s->show_ids = !s->show_ids;
6108 break;
6109 case KEY_ENTER:
6110 case '\r':
6111 if (!s->selected_entry)
6112 break;
6113 if (view_is_parent_view(view))
6114 begin_x = view_split_begin_x(view->begin_x);
6115 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6116 s->repo);
6117 view->focussed = 0;
6118 log_view->focussed = 1;
6119 if (view_is_parent_view(view)) {
6120 err = view_close_child(view);
6121 if (err)
6122 return err;
6123 view_set_child(view, log_view);
6124 view->focus_child = 1;
6125 } else
6126 *new_view = log_view;
6127 break;
6128 case 't':
6129 if (!s->selected_entry)
6130 break;
6131 if (view_is_parent_view(view))
6132 begin_x = view_split_begin_x(view->begin_x);
6133 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6134 s->repo);
6135 if (err || tree_view == NULL)
6136 break;
6137 view->focussed = 0;
6138 tree_view->focussed = 1;
6139 if (view_is_parent_view(view)) {
6140 err = view_close_child(view);
6141 if (err)
6142 return err;
6143 view_set_child(view, tree_view);
6144 view->focus_child = 1;
6145 } else
6146 *new_view = tree_view;
6147 break;
6148 case 'k':
6149 case KEY_UP:
6150 if (s->selected > 0) {
6151 s->selected--;
6152 break;
6154 ref_scroll_up(s, 1);
6155 break;
6156 case KEY_PPAGE:
6157 case CTRL('b'):
6158 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6159 s->selected = 0;
6160 ref_scroll_up(s, MAX(0, view->nlines - 1));
6161 break;
6162 case 'j':
6163 case KEY_DOWN:
6164 if (s->selected < s->ndisplayed - 1) {
6165 s->selected++;
6166 break;
6168 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6169 /* can't scroll any further */
6170 break;
6171 ref_scroll_down(s, 1);
6172 break;
6173 case KEY_NPAGE:
6174 case CTRL('f'):
6175 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6176 /* can't scroll any further; move cursor down */
6177 if (s->selected < s->ndisplayed - 1)
6178 s->selected = s->ndisplayed - 1;
6179 break;
6181 ref_scroll_down(s, view->nlines - 1);
6182 break;
6183 case CTRL('l'):
6184 tog_free_refs();
6185 err = tog_load_refs(s->repo);
6186 if (err)
6187 break;
6188 ref_view_free_refs(s);
6189 err = ref_view_load_refs(s);
6190 break;
6191 case KEY_RESIZE:
6192 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6193 s->selected = view->nlines - 2;
6194 break;
6195 default:
6196 break;
6199 return err;
6202 __dead static void
6203 usage_ref(void)
6205 endwin();
6206 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6207 getprogname());
6208 exit(1);
6211 static const struct got_error *
6212 cmd_ref(int argc, char *argv[])
6214 const struct got_error *error;
6215 struct got_repository *repo = NULL;
6216 struct got_worktree *worktree = NULL;
6217 char *cwd = NULL, *repo_path = NULL;
6218 int ch;
6219 struct tog_view *view;
6221 while ((ch = getopt(argc, argv, "r:")) != -1) {
6222 switch (ch) {
6223 case 'r':
6224 repo_path = realpath(optarg, NULL);
6225 if (repo_path == NULL)
6226 return got_error_from_errno2("realpath",
6227 optarg);
6228 break;
6229 default:
6230 usage_ref();
6231 /* NOTREACHED */
6235 argc -= optind;
6236 argv += optind;
6238 if (argc > 1)
6239 usage_ref();
6241 if (repo_path == NULL) {
6242 cwd = getcwd(NULL, 0);
6243 if (cwd == NULL)
6244 return got_error_from_errno("getcwd");
6245 error = got_worktree_open(&worktree, cwd);
6246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6247 goto done;
6248 if (worktree)
6249 repo_path =
6250 strdup(got_worktree_get_repo_path(worktree));
6251 else
6252 repo_path = strdup(cwd);
6253 if (repo_path == NULL) {
6254 error = got_error_from_errno("strdup");
6255 goto done;
6259 error = got_repo_open(&repo, repo_path, NULL);
6260 if (error != NULL)
6261 goto done;
6263 init_curses();
6265 error = apply_unveil(got_repo_get_path(repo), NULL);
6266 if (error)
6267 goto done;
6269 error = tog_load_refs(repo);
6270 if (error)
6271 goto done;
6273 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6274 if (view == NULL) {
6275 error = got_error_from_errno("view_open");
6276 goto done;
6279 error = open_ref_view(view, repo);
6280 if (error)
6281 goto done;
6283 if (worktree) {
6284 /* Release work tree lock. */
6285 got_worktree_close(worktree);
6286 worktree = NULL;
6288 error = view_loop(view);
6289 done:
6290 free(repo_path);
6291 free(cwd);
6292 if (repo)
6293 got_repo_close(repo);
6294 tog_free_refs();
6295 return error;
6298 static void
6299 list_commands(FILE *fp)
6301 size_t i;
6303 fprintf(fp, "commands:");
6304 for (i = 0; i < nitems(tog_commands); i++) {
6305 struct tog_cmd *cmd = &tog_commands[i];
6306 fprintf(fp, " %s", cmd->name);
6308 fputc('\n', fp);
6311 __dead static void
6312 usage(int hflag, int status)
6314 FILE *fp = (status == 0) ? stdout : stderr;
6316 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6317 getprogname());
6318 if (hflag) {
6319 fprintf(fp, "lazy usage: %s path\n", getprogname());
6320 list_commands(fp);
6322 exit(status);
6325 static char **
6326 make_argv(int argc, ...)
6328 va_list ap;
6329 char **argv;
6330 int i;
6332 va_start(ap, argc);
6334 argv = calloc(argc, sizeof(char *));
6335 if (argv == NULL)
6336 err(1, "calloc");
6337 for (i = 0; i < argc; i++) {
6338 argv[i] = strdup(va_arg(ap, char *));
6339 if (argv[i] == NULL)
6340 err(1, "strdup");
6343 va_end(ap);
6344 return argv;
6348 * Try to convert 'tog path' into a 'tog log path' command.
6349 * The user could simply have mistyped the command rather than knowingly
6350 * provided a path. So check whether argv[0] can in fact be resolved
6351 * to a path in the HEAD commit and print a special error if not.
6352 * This hack is for mpi@ <3
6354 static const struct got_error *
6355 tog_log_with_path(int argc, char *argv[])
6357 const struct got_error *error = NULL;
6358 struct tog_cmd *cmd = NULL;
6359 struct got_repository *repo = NULL;
6360 struct got_worktree *worktree = NULL;
6361 struct got_object_id *commit_id = NULL, *id = NULL;
6362 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6363 char *commit_id_str = NULL, **cmd_argv = NULL;
6365 cwd = getcwd(NULL, 0);
6366 if (cwd == NULL)
6367 return got_error_from_errno("getcwd");
6369 error = got_worktree_open(&worktree, cwd);
6370 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6371 goto done;
6373 if (worktree)
6374 repo_path = strdup(got_worktree_get_repo_path(worktree));
6375 else
6376 repo_path = strdup(cwd);
6377 if (repo_path == NULL) {
6378 error = got_error_from_errno("strdup");
6379 goto done;
6382 error = got_repo_open(&repo, repo_path, NULL);
6383 if (error != NULL)
6384 goto done;
6386 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6387 repo, worktree);
6388 if (error)
6389 goto done;
6391 error = tog_load_refs(repo);
6392 if (error)
6393 goto done;
6394 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6395 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6396 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6397 if (error)
6398 goto done;
6400 if (worktree) {
6401 got_worktree_close(worktree);
6402 worktree = NULL;
6405 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6406 if (error) {
6407 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6408 goto done;
6409 fprintf(stderr, "%s: '%s' is no known command or path\n",
6410 getprogname(), argv[0]);
6411 usage(1, 1);
6412 /* not reached */
6415 got_repo_close(repo);
6416 repo = NULL;
6418 error = got_object_id_str(&commit_id_str, commit_id);
6419 if (error)
6420 goto done;
6422 cmd = &tog_commands[0]; /* log */
6423 argc = 4;
6424 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6425 error = cmd->cmd_main(argc, cmd_argv);
6426 done:
6427 if (repo)
6428 got_repo_close(repo);
6429 if (worktree)
6430 got_worktree_close(worktree);
6431 free(id);
6432 free(commit_id_str);
6433 free(commit_id);
6434 free(cwd);
6435 free(repo_path);
6436 free(in_repo_path);
6437 if (cmd_argv) {
6438 int i;
6439 for (i = 0; i < argc; i++)
6440 free(cmd_argv[i]);
6441 free(cmd_argv);
6443 tog_free_refs();
6444 return error;
6447 int
6448 main(int argc, char *argv[])
6450 const struct got_error *error = NULL;
6451 struct tog_cmd *cmd = NULL;
6452 int ch, hflag = 0, Vflag = 0;
6453 char **cmd_argv = NULL;
6454 static struct option longopts[] = {
6455 { "version", no_argument, NULL, 'V' },
6456 { NULL, 0, NULL, 0}
6459 setlocale(LC_CTYPE, "");
6461 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6462 switch (ch) {
6463 case 'h':
6464 hflag = 1;
6465 break;
6466 case 'V':
6467 Vflag = 1;
6468 break;
6469 default:
6470 usage(hflag, 1);
6471 /* NOTREACHED */
6475 argc -= optind;
6476 argv += optind;
6477 optind = 1;
6478 optreset = 1;
6480 if (Vflag) {
6481 got_version_print_str();
6482 return 0;
6485 #ifndef PROFILE
6486 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6487 NULL) == -1)
6488 err(1, "pledge");
6489 #endif
6491 if (argc == 0) {
6492 if (hflag)
6493 usage(hflag, 0);
6494 /* Build an argument vector which runs a default command. */
6495 cmd = &tog_commands[0];
6496 argc = 1;
6497 cmd_argv = make_argv(argc, cmd->name);
6498 } else {
6499 size_t i;
6501 /* Did the user specify a command? */
6502 for (i = 0; i < nitems(tog_commands); i++) {
6503 if (strncmp(tog_commands[i].name, argv[0],
6504 strlen(argv[0])) == 0) {
6505 cmd = &tog_commands[i];
6506 break;
6511 if (cmd == NULL) {
6512 if (argc != 1)
6513 usage(0, 1);
6514 /* No command specified; try log with a path */
6515 error = tog_log_with_path(argc, argv);
6516 } else {
6517 if (hflag)
6518 cmd->cmd_usage();
6519 else
6520 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6523 endwin();
6524 putchar('\n');
6525 if (cmd_argv) {
6526 int i;
6527 for (i = 0; i < argc; i++)
6528 free(cmd_argv[i]);
6529 free(cmd_argv);
6532 if (error && error->code != GOT_ERR_CANCELLED)
6533 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6534 return 0;