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)
1347 >= sizeof(datebuf))
1348 return got_error(GOT_ERR_NO_SPACE);
1350 if (avail <= date_display_cols)
1351 limit = MIN(sizeof(datebuf) - 1, avail);
1352 else
1353 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1354 tc = get_color(&s->colors, TOG_COLOR_DATE);
1355 if (tc)
1356 wattr_on(view->window,
1357 COLOR_PAIR(tc->colorpair), NULL);
1358 waddnstr(view->window, datebuf, limit);
1359 if (tc)
1360 wattr_off(view->window,
1361 COLOR_PAIR(tc->colorpair), NULL);
1362 col = limit;
1363 if (col > avail)
1364 goto done;
1366 if (avail >= 120) {
1367 char *id_str;
1368 err = got_object_id_str(&id_str, id);
1369 if (err)
1370 goto done;
1371 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1372 if (tc)
1373 wattr_on(view->window,
1374 COLOR_PAIR(tc->colorpair), NULL);
1375 wprintw(view->window, "%.8s ", id_str);
1376 if (tc)
1377 wattr_off(view->window,
1378 COLOR_PAIR(tc->colorpair), NULL);
1379 free(id_str);
1380 col += 9;
1381 if (col > avail)
1382 goto done;
1385 author = strdup(got_object_commit_get_author(commit));
1386 if (author == NULL) {
1387 err = got_error_from_errno("strdup");
1388 goto done;
1390 err = format_author(&wauthor, &author_width, author, avail - col, col);
1391 if (err)
1392 goto done;
1393 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1394 if (tc)
1395 wattr_on(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1397 waddwstr(view->window, wauthor);
1398 if (tc)
1399 wattr_off(view->window,
1400 COLOR_PAIR(tc->colorpair), NULL);
1401 col += author_width;
1402 while (col < avail && author_width < author_display_cols + 2) {
1403 waddch(view->window, ' ');
1404 col++;
1405 author_width++;
1407 if (col > avail)
1408 goto done;
1410 err = got_object_commit_get_logmsg(&logmsg0, commit);
1411 if (err)
1412 goto done;
1413 logmsg = logmsg0;
1414 while (*logmsg == '\n')
1415 logmsg++;
1416 newline = strchr(logmsg, '\n');
1417 if (newline)
1418 *newline = '\0';
1419 limit = avail - col;
1420 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1421 if (err)
1422 goto done;
1423 waddwstr(view->window, wlogmsg);
1424 col += logmsg_width;
1425 while (col < avail) {
1426 waddch(view->window, ' ');
1427 col++;
1429 done:
1430 free(logmsg0);
1431 free(wlogmsg);
1432 free(author);
1433 free(wauthor);
1434 free(line);
1435 return err;
1438 static struct commit_queue_entry *
1439 alloc_commit_queue_entry(struct got_commit_object *commit,
1440 struct got_object_id *id)
1442 struct commit_queue_entry *entry;
1444 entry = calloc(1, sizeof(*entry));
1445 if (entry == NULL)
1446 return NULL;
1448 entry->id = id;
1449 entry->commit = commit;
1450 return entry;
1453 static void
1454 pop_commit(struct commit_queue *commits)
1456 struct commit_queue_entry *entry;
1458 entry = TAILQ_FIRST(&commits->head);
1459 TAILQ_REMOVE(&commits->head, entry, entry);
1460 got_object_commit_close(entry->commit);
1461 commits->ncommits--;
1462 /* Don't free entry->id! It is owned by the commit graph. */
1463 free(entry);
1466 static void
1467 free_commits(struct commit_queue *commits)
1469 while (!TAILQ_EMPTY(&commits->head))
1470 pop_commit(commits);
1473 static const struct got_error *
1474 match_commit(int *have_match, struct got_object_id *id,
1475 struct got_commit_object *commit, regex_t *regex)
1477 const struct got_error *err = NULL;
1478 regmatch_t regmatch;
1479 char *id_str = NULL, *logmsg = NULL;
1481 *have_match = 0;
1483 err = got_object_id_str(&id_str, id);
1484 if (err)
1485 return err;
1487 err = got_object_commit_get_logmsg(&logmsg, commit);
1488 if (err)
1489 goto done;
1491 if (regexec(regex, got_object_commit_get_author(commit), 1,
1492 &regmatch, 0) == 0 ||
1493 regexec(regex, got_object_commit_get_committer(commit), 1,
1494 &regmatch, 0) == 0 ||
1495 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1496 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1497 *have_match = 1;
1498 done:
1499 free(id_str);
1500 free(logmsg);
1501 return err;
1504 static const struct got_error *
1505 queue_commits(struct tog_log_thread_args *a)
1507 const struct got_error *err = NULL;
1510 * We keep all commits open throughout the lifetime of the log
1511 * view in order to avoid having to re-fetch commits from disk
1512 * while updating the display.
1514 do {
1515 struct got_object_id *id;
1516 struct got_commit_object *commit;
1517 struct commit_queue_entry *entry;
1518 int errcode;
1520 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1521 NULL, NULL);
1522 if (err || id == NULL)
1523 break;
1525 err = got_object_open_as_commit(&commit, a->repo, id);
1526 if (err)
1527 break;
1528 entry = alloc_commit_queue_entry(commit, id);
1529 if (entry == NULL) {
1530 err = got_error_from_errno("alloc_commit_queue_entry");
1531 break;
1534 errcode = pthread_mutex_lock(&tog_mutex);
1535 if (errcode) {
1536 err = got_error_set_errno(errcode,
1537 "pthread_mutex_lock");
1538 break;
1541 entry->idx = a->commits->ncommits;
1542 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1543 a->commits->ncommits++;
1545 if (*a->searching == TOG_SEARCH_FORWARD &&
1546 !*a->search_next_done) {
1547 int have_match;
1548 err = match_commit(&have_match, id, commit, a->regex);
1549 if (err)
1550 break;
1551 if (have_match)
1552 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1555 errcode = pthread_mutex_unlock(&tog_mutex);
1556 if (errcode && err == NULL)
1557 err = got_error_set_errno(errcode,
1558 "pthread_mutex_unlock");
1559 if (err)
1560 break;
1561 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1563 return err;
1566 static void
1567 select_commit(struct tog_log_view_state *s)
1569 struct commit_queue_entry *entry;
1570 int ncommits = 0;
1572 entry = s->first_displayed_entry;
1573 while (entry) {
1574 if (ncommits == s->selected) {
1575 s->selected_entry = entry;
1576 break;
1578 entry = TAILQ_NEXT(entry, entry);
1579 ncommits++;
1583 static const struct got_error *
1584 draw_commits(struct tog_view *view)
1586 const struct got_error *err = NULL;
1587 struct tog_log_view_state *s = &view->state.log;
1588 struct commit_queue_entry *entry = s->selected_entry;
1589 const int limit = view->nlines;
1590 int width;
1591 int ncommits, author_cols = 4;
1592 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1593 char *refs_str = NULL;
1594 wchar_t *wline;
1595 struct tog_color *tc;
1596 static const size_t date_display_cols = 12;
1598 if (s->selected_entry &&
1599 !(view->searching && view->search_next_done == 0)) {
1600 struct got_reflist_head *refs;
1601 err = got_object_id_str(&id_str, s->selected_entry->id);
1602 if (err)
1603 return err;
1604 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1605 s->selected_entry->id);
1606 if (refs) {
1607 err = build_refs_str(&refs_str, refs,
1608 s->selected_entry->id, s->repo);
1609 if (err)
1610 goto done;
1614 if (s->thread_args.commits_needed == 0)
1615 halfdelay(10); /* disable fast refresh */
1617 if (s->thread_args.commits_needed > 0) {
1618 if (asprintf(&ncommits_str, " [%d/%d] %s",
1619 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1620 (view->searching && !view->search_next_done) ?
1621 "searching..." : "loading...") == -1) {
1622 err = got_error_from_errno("asprintf");
1623 goto done;
1625 } else {
1626 const char *search_str = NULL;
1628 if (view->searching) {
1629 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1630 search_str = "no more matches";
1631 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1632 search_str = "no matches found";
1633 else if (!view->search_next_done)
1634 search_str = "searching...";
1637 if (asprintf(&ncommits_str, " [%d/%d] %s",
1638 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1639 search_str ? search_str :
1640 (refs_str ? refs_str : "")) == -1) {
1641 err = got_error_from_errno("asprintf");
1642 goto done;
1646 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1647 if (asprintf(&header, "commit %s %s%s",
1648 id_str ? id_str : "........................................",
1649 s->in_repo_path, ncommits_str) == -1) {
1650 err = got_error_from_errno("asprintf");
1651 header = NULL;
1652 goto done;
1654 } else if (asprintf(&header, "commit %s%s",
1655 id_str ? id_str : "........................................",
1656 ncommits_str) == -1) {
1657 err = got_error_from_errno("asprintf");
1658 header = NULL;
1659 goto done;
1661 err = format_line(&wline, &width, header, view->ncols, 0);
1662 if (err)
1663 goto done;
1665 werase(view->window);
1667 if (view_needs_focus_indication(view))
1668 wstandout(view->window);
1669 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1670 if (tc)
1671 wattr_on(view->window,
1672 COLOR_PAIR(tc->colorpair), NULL);
1673 waddwstr(view->window, wline);
1674 if (tc)
1675 wattr_off(view->window,
1676 COLOR_PAIR(tc->colorpair), NULL);
1677 while (width < view->ncols) {
1678 waddch(view->window, ' ');
1679 width++;
1681 if (view_needs_focus_indication(view))
1682 wstandend(view->window);
1683 free(wline);
1684 if (limit <= 1)
1685 goto done;
1687 /* Grow author column size if necessary. */
1688 entry = s->first_displayed_entry;
1689 ncommits = 0;
1690 while (entry) {
1691 char *author;
1692 wchar_t *wauthor;
1693 int width;
1694 if (ncommits >= limit - 1)
1695 break;
1696 author = strdup(got_object_commit_get_author(entry->commit));
1697 if (author == NULL) {
1698 err = got_error_from_errno("strdup");
1699 goto done;
1701 err = format_author(&wauthor, &width, author, COLS,
1702 date_display_cols);
1703 if (author_cols < width)
1704 author_cols = width;
1705 free(wauthor);
1706 free(author);
1707 ncommits++;
1708 entry = TAILQ_NEXT(entry, entry);
1711 entry = s->first_displayed_entry;
1712 s->last_displayed_entry = s->first_displayed_entry;
1713 ncommits = 0;
1714 while (entry) {
1715 if (ncommits >= limit - 1)
1716 break;
1717 if (ncommits == s->selected)
1718 wstandout(view->window);
1719 err = draw_commit(view, entry->commit, entry->id,
1720 date_display_cols, author_cols);
1721 if (ncommits == s->selected)
1722 wstandend(view->window);
1723 if (err)
1724 goto done;
1725 ncommits++;
1726 s->last_displayed_entry = entry;
1727 entry = TAILQ_NEXT(entry, entry);
1730 view_vborder(view);
1731 done:
1732 free(id_str);
1733 free(refs_str);
1734 free(ncommits_str);
1735 free(header);
1736 return err;
1739 static void
1740 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1742 struct commit_queue_entry *entry;
1743 int nscrolled = 0;
1745 entry = TAILQ_FIRST(&s->commits.head);
1746 if (s->first_displayed_entry == entry)
1747 return;
1749 entry = s->first_displayed_entry;
1750 while (entry && nscrolled < maxscroll) {
1751 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1752 if (entry) {
1753 s->first_displayed_entry = entry;
1754 nscrolled++;
1759 static const struct got_error *
1760 trigger_log_thread(struct tog_view *view, int wait)
1762 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1763 int errcode;
1765 halfdelay(1); /* fast refresh while loading commits */
1767 while (ta->commits_needed > 0) {
1768 if (ta->log_complete)
1769 break;
1771 /* Wake the log thread. */
1772 errcode = pthread_cond_signal(&ta->need_commits);
1773 if (errcode)
1774 return got_error_set_errno(errcode,
1775 "pthread_cond_signal");
1778 * The mutex will be released while the view loop waits
1779 * in wgetch(), at which time the log thread will run.
1781 if (!wait)
1782 break;
1784 /* Display progress update in log view. */
1785 show_log_view(view);
1786 update_panels();
1787 doupdate();
1789 /* Wait right here while next commit is being loaded. */
1790 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1791 if (errcode)
1792 return got_error_set_errno(errcode,
1793 "pthread_cond_wait");
1795 /* Display progress update in log view. */
1796 show_log_view(view);
1797 update_panels();
1798 doupdate();
1801 return NULL;
1804 static const struct got_error *
1805 log_scroll_down(struct tog_view *view, int maxscroll)
1807 struct tog_log_view_state *s = &view->state.log;
1808 const struct got_error *err = NULL;
1809 struct commit_queue_entry *pentry;
1810 int nscrolled = 0, ncommits_needed;
1812 if (s->last_displayed_entry == NULL)
1813 return NULL;
1815 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1816 if (s->commits.ncommits < ncommits_needed &&
1817 !s->thread_args.log_complete) {
1819 * Ask the log thread for required amount of commits.
1821 s->thread_args.commits_needed += maxscroll;
1822 err = trigger_log_thread(view, 1);
1823 if (err)
1824 return err;
1827 do {
1828 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1829 if (pentry == NULL)
1830 break;
1832 s->last_displayed_entry = pentry;
1834 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1835 if (pentry == NULL)
1836 break;
1837 s->first_displayed_entry = pentry;
1838 } while (++nscrolled < maxscroll);
1840 return err;
1843 static const struct got_error *
1844 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1845 struct got_commit_object *commit, struct got_object_id *commit_id,
1846 struct tog_view *log_view, struct got_repository *repo)
1848 const struct got_error *err;
1849 struct got_object_qid *parent_id;
1850 struct tog_view *diff_view;
1852 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1853 if (diff_view == NULL)
1854 return got_error_from_errno("view_open");
1856 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1857 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1858 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1859 if (err == NULL)
1860 *new_view = diff_view;
1861 return err;
1864 static const struct got_error *
1865 tree_view_visit_subtree(struct tog_tree_view_state *s,
1866 struct got_tree_object *subtree)
1868 struct tog_parent_tree *parent;
1870 parent = calloc(1, sizeof(*parent));
1871 if (parent == NULL)
1872 return got_error_from_errno("calloc");
1874 parent->tree = s->tree;
1875 parent->first_displayed_entry = s->first_displayed_entry;
1876 parent->selected_entry = s->selected_entry;
1877 parent->selected = s->selected;
1878 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1879 s->tree = subtree;
1880 s->selected = 0;
1881 s->first_displayed_entry = NULL;
1882 return NULL;
1885 static const struct got_error *
1886 tree_view_walk_path(struct tog_tree_view_state *s,
1887 struct got_object_id *commit_id, const char *path)
1889 const struct got_error *err = NULL;
1890 struct got_tree_object *tree = NULL;
1891 const char *p;
1892 char *slash, *subpath = NULL;
1894 /* Walk the path and open corresponding tree objects. */
1895 p = path;
1896 while (*p) {
1897 struct got_tree_entry *te;
1898 struct got_object_id *tree_id;
1899 char *te_name;
1901 while (p[0] == '/')
1902 p++;
1904 /* Ensure the correct subtree entry is selected. */
1905 slash = strchr(p, '/');
1906 if (slash == NULL)
1907 te_name = strdup(p);
1908 else
1909 te_name = strndup(p, slash - p);
1910 if (te_name == NULL) {
1911 err = got_error_from_errno("strndup");
1912 break;
1914 te = got_object_tree_find_entry(s->tree, te_name);
1915 if (te == NULL) {
1916 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1917 free(te_name);
1918 break;
1920 free(te_name);
1921 s->first_displayed_entry = s->selected_entry = te;
1923 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1924 break; /* jump to this file's entry */
1926 slash = strchr(p, '/');
1927 if (slash)
1928 subpath = strndup(path, slash - path);
1929 else
1930 subpath = strdup(path);
1931 if (subpath == NULL) {
1932 err = got_error_from_errno("strdup");
1933 break;
1936 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1937 subpath);
1938 if (err)
1939 break;
1941 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1942 free(tree_id);
1943 if (err)
1944 break;
1946 err = tree_view_visit_subtree(s, tree);
1947 if (err) {
1948 got_object_tree_close(tree);
1949 break;
1951 if (slash == NULL)
1952 break;
1953 free(subpath);
1954 subpath = NULL;
1955 p = slash;
1958 free(subpath);
1959 return err;
1962 static const struct got_error *
1963 browse_commit_tree(struct tog_view **new_view, int begin_x,
1964 struct commit_queue_entry *entry, const char *path,
1965 const char *head_ref_name, struct got_repository *repo)
1967 const struct got_error *err = NULL;
1968 struct got_tree_object *tree;
1969 struct tog_tree_view_state *s;
1970 struct tog_view *tree_view;
1972 err = got_object_open_as_tree(&tree, repo,
1973 got_object_commit_get_tree_id(entry->commit));
1974 if (err)
1975 return err;
1977 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1978 if (tree_view == NULL)
1979 return got_error_from_errno("view_open");
1981 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1982 if (err) {
1983 got_object_tree_close(tree);
1984 return err;
1986 s = &tree_view->state.tree;
1988 *new_view = tree_view;
1990 if (got_path_is_root_dir(path))
1991 return NULL;
1993 return tree_view_walk_path(s, entry->id, path);
1996 static const struct got_error *
1997 block_signals_used_by_main_thread(void)
1999 sigset_t sigset;
2000 int errcode;
2002 if (sigemptyset(&sigset) == -1)
2003 return got_error_from_errno("sigemptyset");
2005 /* tog handles SIGWINCH and SIGCONT */
2006 if (sigaddset(&sigset, SIGWINCH) == -1)
2007 return got_error_from_errno("sigaddset");
2008 if (sigaddset(&sigset, SIGCONT) == -1)
2009 return got_error_from_errno("sigaddset");
2011 /* ncurses handles SIGTSTP */
2012 if (sigaddset(&sigset, SIGTSTP) == -1)
2013 return got_error_from_errno("sigaddset");
2015 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2016 if (errcode)
2017 return got_error_set_errno(errcode, "pthread_sigmask");
2019 return NULL;
2022 static void *
2023 log_thread(void *arg)
2025 const struct got_error *err = NULL;
2026 int errcode = 0;
2027 struct tog_log_thread_args *a = arg;
2028 int done = 0;
2030 err = block_signals_used_by_main_thread();
2031 if (err)
2032 return (void *)err;
2034 while (!done && !err && !tog_sigpipe_received) {
2035 err = queue_commits(a);
2036 if (err) {
2037 if (err->code != GOT_ERR_ITER_COMPLETED)
2038 return (void *)err;
2039 err = NULL;
2040 done = 1;
2041 } else if (a->commits_needed > 0)
2042 a->commits_needed--;
2044 errcode = pthread_mutex_lock(&tog_mutex);
2045 if (errcode) {
2046 err = got_error_set_errno(errcode,
2047 "pthread_mutex_lock");
2048 break;
2049 } else if (*a->quit)
2050 done = 1;
2051 else if (*a->first_displayed_entry == NULL) {
2052 *a->first_displayed_entry =
2053 TAILQ_FIRST(&a->commits->head);
2054 *a->selected_entry = *a->first_displayed_entry;
2057 errcode = pthread_cond_signal(&a->commit_loaded);
2058 if (errcode) {
2059 err = got_error_set_errno(errcode,
2060 "pthread_cond_signal");
2061 pthread_mutex_unlock(&tog_mutex);
2062 break;
2065 if (done)
2066 a->commits_needed = 0;
2067 else {
2068 if (a->commits_needed == 0) {
2069 errcode = pthread_cond_wait(&a->need_commits,
2070 &tog_mutex);
2071 if (errcode)
2072 err = got_error_set_errno(errcode,
2073 "pthread_cond_wait");
2074 if (*a->quit)
2075 done = 1;
2079 errcode = pthread_mutex_unlock(&tog_mutex);
2080 if (errcode && err == NULL)
2081 err = got_error_set_errno(errcode,
2082 "pthread_mutex_unlock");
2084 a->log_complete = 1;
2085 return (void *)err;
2088 static const struct got_error *
2089 stop_log_thread(struct tog_log_view_state *s)
2091 const struct got_error *err = NULL;
2092 int errcode;
2094 if (s->thread) {
2095 s->quit = 1;
2096 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_cond_signal");
2100 errcode = pthread_mutex_unlock(&tog_mutex);
2101 if (errcode)
2102 return got_error_set_errno(errcode,
2103 "pthread_mutex_unlock");
2104 errcode = pthread_join(s->thread, (void **)&err);
2105 if (errcode)
2106 return got_error_set_errno(errcode, "pthread_join");
2107 errcode = pthread_mutex_lock(&tog_mutex);
2108 if (errcode)
2109 return got_error_set_errno(errcode,
2110 "pthread_mutex_lock");
2111 s->thread = NULL;
2114 if (s->thread_args.repo) {
2115 got_repo_close(s->thread_args.repo);
2116 s->thread_args.repo = NULL;
2119 if (s->thread_args.graph) {
2120 got_commit_graph_close(s->thread_args.graph);
2121 s->thread_args.graph = NULL;
2124 return err;
2127 static const struct got_error *
2128 close_log_view(struct tog_view *view)
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2132 int errcode;
2134 err = stop_log_thread(s);
2136 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2141 if (errcode && err == NULL)
2142 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2144 free_commits(&s->commits);
2145 free(s->in_repo_path);
2146 s->in_repo_path = NULL;
2147 free(s->start_id);
2148 s->start_id = NULL;
2149 free(s->head_ref_name);
2150 s->head_ref_name = NULL;
2151 return err;
2154 static const struct got_error *
2155 search_start_log_view(struct tog_view *view)
2157 struct tog_log_view_state *s = &view->state.log;
2159 s->matched_entry = NULL;
2160 s->search_entry = NULL;
2161 return NULL;
2164 static const struct got_error *
2165 search_next_log_view(struct tog_view *view)
2167 const struct got_error *err = NULL;
2168 struct tog_log_view_state *s = &view->state.log;
2169 struct commit_queue_entry *entry;
2171 /* Display progress update in log view. */
2172 show_log_view(view);
2173 update_panels();
2174 doupdate();
2176 if (s->search_entry) {
2177 int errcode, ch;
2178 errcode = pthread_mutex_unlock(&tog_mutex);
2179 if (errcode)
2180 return got_error_set_errno(errcode,
2181 "pthread_mutex_unlock");
2182 ch = wgetch(view->window);
2183 errcode = pthread_mutex_lock(&tog_mutex);
2184 if (errcode)
2185 return got_error_set_errno(errcode,
2186 "pthread_mutex_lock");
2187 if (ch == KEY_BACKSPACE) {
2188 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2189 return NULL;
2191 if (view->searching == TOG_SEARCH_FORWARD)
2192 entry = TAILQ_NEXT(s->search_entry, entry);
2193 else
2194 entry = TAILQ_PREV(s->search_entry,
2195 commit_queue_head, entry);
2196 } else if (s->matched_entry) {
2197 if (view->searching == TOG_SEARCH_FORWARD)
2198 entry = TAILQ_NEXT(s->matched_entry, entry);
2199 else
2200 entry = TAILQ_PREV(s->matched_entry,
2201 commit_queue_head, entry);
2202 } else {
2203 if (view->searching == TOG_SEARCH_FORWARD)
2204 entry = TAILQ_FIRST(&s->commits.head);
2205 else
2206 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2209 while (1) {
2210 int have_match = 0;
2212 if (entry == NULL) {
2213 if (s->thread_args.log_complete ||
2214 view->searching == TOG_SEARCH_BACKWARD) {
2215 view->search_next_done =
2216 (s->matched_entry == NULL ?
2217 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2218 s->search_entry = NULL;
2219 return NULL;
2222 * Poke the log thread for more commits and return,
2223 * allowing the main loop to make progress. Search
2224 * will resume at s->search_entry once we come back.
2226 s->thread_args.commits_needed++;
2227 return trigger_log_thread(view, 0);
2230 err = match_commit(&have_match, entry->id, entry->commit,
2231 &view->regex);
2232 if (err)
2233 break;
2234 if (have_match) {
2235 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2236 s->matched_entry = entry;
2237 break;
2240 s->search_entry = entry;
2241 if (view->searching == TOG_SEARCH_FORWARD)
2242 entry = TAILQ_NEXT(entry, entry);
2243 else
2244 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2247 if (s->matched_entry) {
2248 int cur = s->selected_entry->idx;
2249 while (cur < s->matched_entry->idx) {
2250 err = input_log_view(NULL, view, KEY_DOWN);
2251 if (err)
2252 return err;
2253 cur++;
2255 while (cur > s->matched_entry->idx) {
2256 err = input_log_view(NULL, view, KEY_UP);
2257 if (err)
2258 return err;
2259 cur--;
2263 s->search_entry = NULL;
2265 return NULL;
2268 static const struct got_error *
2269 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2270 struct got_repository *repo, const char *head_ref_name,
2271 const char *in_repo_path, int log_branches)
2273 const struct got_error *err = NULL;
2274 struct tog_log_view_state *s = &view->state.log;
2275 struct got_repository *thread_repo = NULL;
2276 struct got_commit_graph *thread_graph = NULL;
2277 int errcode;
2279 if (in_repo_path != s->in_repo_path) {
2280 free(s->in_repo_path);
2281 s->in_repo_path = strdup(in_repo_path);
2282 if (s->in_repo_path == NULL)
2283 return got_error_from_errno("strdup");
2286 /* The commit queue only contains commits being displayed. */
2287 TAILQ_INIT(&s->commits.head);
2288 s->commits.ncommits = 0;
2290 s->repo = repo;
2291 if (head_ref_name) {
2292 s->head_ref_name = strdup(head_ref_name);
2293 if (s->head_ref_name == NULL) {
2294 err = got_error_from_errno("strdup");
2295 goto done;
2298 s->start_id = got_object_id_dup(start_id);
2299 if (s->start_id == NULL) {
2300 err = got_error_from_errno("got_object_id_dup");
2301 goto done;
2303 s->log_branches = log_branches;
2305 SIMPLEQ_INIT(&s->colors);
2306 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2307 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2308 get_color_value("TOG_COLOR_COMMIT"));
2309 if (err)
2310 goto done;
2311 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2312 get_color_value("TOG_COLOR_AUTHOR"));
2313 if (err) {
2314 free_colors(&s->colors);
2315 goto done;
2317 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2318 get_color_value("TOG_COLOR_DATE"));
2319 if (err) {
2320 free_colors(&s->colors);
2321 goto done;
2325 view->show = show_log_view;
2326 view->input = input_log_view;
2327 view->close = close_log_view;
2328 view->search_start = search_start_log_view;
2329 view->search_next = search_next_log_view;
2331 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2332 if (err)
2333 goto done;
2334 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2335 !s->log_branches);
2336 if (err)
2337 goto done;
2338 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2339 s->repo, NULL, NULL);
2340 if (err)
2341 goto done;
2343 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2344 if (errcode) {
2345 err = got_error_set_errno(errcode, "pthread_cond_init");
2346 goto done;
2348 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2349 if (errcode) {
2350 err = got_error_set_errno(errcode, "pthread_cond_init");
2351 goto done;
2354 s->thread_args.commits_needed = view->nlines;
2355 s->thread_args.graph = thread_graph;
2356 s->thread_args.commits = &s->commits;
2357 s->thread_args.in_repo_path = s->in_repo_path;
2358 s->thread_args.start_id = s->start_id;
2359 s->thread_args.repo = thread_repo;
2360 s->thread_args.log_complete = 0;
2361 s->thread_args.quit = &s->quit;
2362 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2363 s->thread_args.selected_entry = &s->selected_entry;
2364 s->thread_args.searching = &view->searching;
2365 s->thread_args.search_next_done = &view->search_next_done;
2366 s->thread_args.regex = &view->regex;
2367 done:
2368 if (err)
2369 close_log_view(view);
2370 return err;
2373 static const struct got_error *
2374 show_log_view(struct tog_view *view)
2376 const struct got_error *err;
2377 struct tog_log_view_state *s = &view->state.log;
2379 if (s->thread == NULL) {
2380 int errcode = pthread_create(&s->thread, NULL, log_thread,
2381 &s->thread_args);
2382 if (errcode)
2383 return got_error_set_errno(errcode, "pthread_create");
2384 if (s->thread_args.commits_needed > 0) {
2385 err = trigger_log_thread(view, 1);
2386 if (err)
2387 return err;
2391 return draw_commits(view);
2394 static const struct got_error *
2395 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2397 const struct got_error *err = NULL;
2398 struct tog_log_view_state *s = &view->state.log;
2399 struct tog_view *diff_view = NULL, *tree_view = NULL;
2400 struct tog_view *ref_view = NULL;
2401 int begin_x = 0;
2403 switch (ch) {
2404 case 'q':
2405 s->quit = 1;
2406 break;
2407 case 'k':
2408 case KEY_UP:
2409 case '<':
2410 case ',':
2411 if (s->first_displayed_entry == NULL)
2412 break;
2413 if (s->selected > 0)
2414 s->selected--;
2415 else
2416 log_scroll_up(s, 1);
2417 select_commit(s);
2418 break;
2419 case KEY_PPAGE:
2420 case CTRL('b'):
2421 if (s->first_displayed_entry == NULL)
2422 break;
2423 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2424 s->selected = 0;
2425 else
2426 log_scroll_up(s, view->nlines - 1);
2427 select_commit(s);
2428 break;
2429 case 'j':
2430 case KEY_DOWN:
2431 case '>':
2432 case '.':
2433 if (s->first_displayed_entry == NULL)
2434 break;
2435 if (s->selected < MIN(view->nlines - 2,
2436 s->commits.ncommits - 1))
2437 s->selected++;
2438 else {
2439 err = log_scroll_down(view, 1);
2440 if (err)
2441 break;
2443 select_commit(s);
2444 break;
2445 case KEY_NPAGE:
2446 case CTRL('f'): {
2447 struct commit_queue_entry *first;
2448 first = s->first_displayed_entry;
2449 if (first == NULL)
2450 break;
2451 err = log_scroll_down(view, view->nlines - 1);
2452 if (err)
2453 break;
2454 if (first == s->first_displayed_entry &&
2455 s->selected < MIN(view->nlines - 2,
2456 s->commits.ncommits - 1)) {
2457 /* can't scroll further down */
2458 s->selected = MIN(view->nlines - 2,
2459 s->commits.ncommits - 1);
2461 select_commit(s);
2462 break;
2464 case KEY_RESIZE:
2465 if (s->selected > view->nlines - 2)
2466 s->selected = view->nlines - 2;
2467 if (s->selected > s->commits.ncommits - 1)
2468 s->selected = s->commits.ncommits - 1;
2469 select_commit(s);
2470 if (s->commits.ncommits < view->nlines - 1 &&
2471 !s->thread_args.log_complete) {
2472 s->thread_args.commits_needed += (view->nlines - 1) -
2473 s->commits.ncommits;
2474 err = trigger_log_thread(view, 1);
2476 break;
2477 case KEY_ENTER:
2478 case ' ':
2479 case '\r':
2480 if (s->selected_entry == NULL)
2481 break;
2482 if (view_is_parent_view(view))
2483 begin_x = view_split_begin_x(view->begin_x);
2484 err = open_diff_view_for_commit(&diff_view, begin_x,
2485 s->selected_entry->commit, s->selected_entry->id,
2486 view, s->repo);
2487 if (err)
2488 break;
2489 view->focussed = 0;
2490 diff_view->focussed = 1;
2491 if (view_is_parent_view(view)) {
2492 err = view_close_child(view);
2493 if (err)
2494 return err;
2495 view_set_child(view, diff_view);
2496 view->focus_child = 1;
2497 } else
2498 *new_view = diff_view;
2499 break;
2500 case 't':
2501 if (s->selected_entry == NULL)
2502 break;
2503 if (view_is_parent_view(view))
2504 begin_x = view_split_begin_x(view->begin_x);
2505 err = browse_commit_tree(&tree_view, begin_x,
2506 s->selected_entry, s->in_repo_path, s->head_ref_name,
2507 s->repo);
2508 if (err)
2509 break;
2510 view->focussed = 0;
2511 tree_view->focussed = 1;
2512 if (view_is_parent_view(view)) {
2513 err = view_close_child(view);
2514 if (err)
2515 return err;
2516 view_set_child(view, tree_view);
2517 view->focus_child = 1;
2518 } else
2519 *new_view = tree_view;
2520 break;
2521 case KEY_BACKSPACE:
2522 case CTRL('l'):
2523 case 'B':
2524 if (ch == KEY_BACKSPACE &&
2525 got_path_is_root_dir(s->in_repo_path))
2526 break;
2527 err = stop_log_thread(s);
2528 if (err)
2529 return err;
2530 if (ch == KEY_BACKSPACE) {
2531 char *parent_path;
2532 err = got_path_dirname(&parent_path, s->in_repo_path);
2533 if (err)
2534 return err;
2535 free(s->in_repo_path);
2536 s->in_repo_path = parent_path;
2537 s->thread_args.in_repo_path = s->in_repo_path;
2538 } else if (ch == CTRL('l')) {
2539 struct got_object_id *start_id;
2540 err = got_repo_match_object_id(&start_id, NULL,
2541 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2542 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2543 if (err)
2544 return err;
2545 free(s->start_id);
2546 s->start_id = start_id;
2547 s->thread_args.start_id = s->start_id;
2548 } else /* 'B' */
2549 s->log_branches = !s->log_branches;
2551 err = got_repo_open(&s->thread_args.repo,
2552 got_repo_get_path(s->repo), NULL);
2553 if (err)
2554 return err;
2555 tog_free_refs();
2556 err = tog_load_refs(s->repo);
2557 if (err)
2558 return err;
2559 err = got_commit_graph_open(&s->thread_args.graph,
2560 s->in_repo_path, !s->log_branches);
2561 if (err)
2562 return err;
2563 err = got_commit_graph_iter_start(s->thread_args.graph,
2564 s->start_id, s->repo, NULL, NULL);
2565 if (err)
2566 return err;
2567 free_commits(&s->commits);
2568 s->first_displayed_entry = NULL;
2569 s->last_displayed_entry = NULL;
2570 s->selected_entry = NULL;
2571 s->selected = 0;
2572 s->thread_args.log_complete = 0;
2573 s->quit = 0;
2574 s->thread_args.commits_needed = view->nlines;
2575 break;
2576 case 'r':
2577 if (view_is_parent_view(view))
2578 begin_x = view_split_begin_x(view->begin_x);
2579 ref_view = view_open(view->nlines, view->ncols,
2580 view->begin_y, begin_x, TOG_VIEW_REF);
2581 if (ref_view == NULL)
2582 return got_error_from_errno("view_open");
2583 err = open_ref_view(ref_view, s->repo);
2584 if (err) {
2585 view_close(ref_view);
2586 return err;
2588 view->focussed = 0;
2589 ref_view->focussed = 1;
2590 if (view_is_parent_view(view)) {
2591 err = view_close_child(view);
2592 if (err)
2593 return err;
2594 view_set_child(view, ref_view);
2595 view->focus_child = 1;
2596 } else
2597 *new_view = ref_view;
2598 break;
2599 default:
2600 break;
2603 return err;
2606 static const struct got_error *
2607 apply_unveil(const char *repo_path, const char *worktree_path)
2609 const struct got_error *error;
2611 #ifdef PROFILE
2612 if (unveil("gmon.out", "rwc") != 0)
2613 return got_error_from_errno2("unveil", "gmon.out");
2614 #endif
2615 if (repo_path && unveil(repo_path, "r") != 0)
2616 return got_error_from_errno2("unveil", repo_path);
2618 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2619 return got_error_from_errno2("unveil", worktree_path);
2621 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2622 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2624 error = got_privsep_unveil_exec_helpers();
2625 if (error != NULL)
2626 return error;
2628 if (unveil(NULL, NULL) != 0)
2629 return got_error_from_errno("unveil");
2631 return NULL;
2634 static void
2635 init_curses(void)
2637 initscr();
2638 cbreak();
2639 halfdelay(1); /* Do fast refresh while initial view is loading. */
2640 noecho();
2641 nonl();
2642 intrflush(stdscr, FALSE);
2643 keypad(stdscr, TRUE);
2644 curs_set(0);
2645 if (getenv("TOG_COLORS") != NULL) {
2646 start_color();
2647 use_default_colors();
2649 signal(SIGWINCH, tog_sigwinch);
2650 signal(SIGPIPE, tog_sigpipe);
2651 signal(SIGCONT, tog_sigcont);
2654 static const struct got_error *
2655 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2656 struct got_repository *repo, struct got_worktree *worktree)
2658 const struct got_error *err = NULL;
2660 if (argc == 0) {
2661 *in_repo_path = strdup("/");
2662 if (*in_repo_path == NULL)
2663 return got_error_from_errno("strdup");
2664 return NULL;
2667 if (worktree) {
2668 const char *prefix = got_worktree_get_path_prefix(worktree);
2669 char *p;
2671 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2672 if (err)
2673 return err;
2674 if (asprintf(in_repo_path, "%s%s%s", prefix,
2675 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2676 p) == -1) {
2677 err = got_error_from_errno("asprintf");
2678 *in_repo_path = NULL;
2680 free(p);
2681 } else
2682 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2684 return err;
2687 static const struct got_error *
2688 cmd_log(int argc, char *argv[])
2690 const struct got_error *error;
2691 struct got_repository *repo = NULL;
2692 struct got_worktree *worktree = NULL;
2693 struct got_object_id *start_id = NULL;
2694 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2695 char *start_commit = NULL, *label = NULL;
2696 struct got_reference *ref = NULL;
2697 const char *head_ref_name = NULL;
2698 int ch, log_branches = 0;
2699 struct tog_view *view;
2701 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2702 switch (ch) {
2703 case 'b':
2704 log_branches = 1;
2705 break;
2706 case 'c':
2707 start_commit = optarg;
2708 break;
2709 case 'r':
2710 repo_path = realpath(optarg, NULL);
2711 if (repo_path == NULL)
2712 return got_error_from_errno2("realpath",
2713 optarg);
2714 break;
2715 default:
2716 usage_log();
2717 /* NOTREACHED */
2721 argc -= optind;
2722 argv += optind;
2724 if (argc > 1)
2725 usage_log();
2727 if (repo_path == NULL) {
2728 cwd = getcwd(NULL, 0);
2729 if (cwd == NULL)
2730 return got_error_from_errno("getcwd");
2731 error = got_worktree_open(&worktree, cwd);
2732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2733 goto done;
2734 if (worktree)
2735 repo_path =
2736 strdup(got_worktree_get_repo_path(worktree));
2737 else
2738 repo_path = strdup(cwd);
2739 if (repo_path == NULL) {
2740 error = got_error_from_errno("strdup");
2741 goto done;
2745 error = got_repo_open(&repo, repo_path, NULL);
2746 if (error != NULL)
2747 goto done;
2749 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2750 repo, worktree);
2751 if (error)
2752 goto done;
2754 init_curses();
2756 error = apply_unveil(got_repo_get_path(repo),
2757 worktree ? got_worktree_get_root_path(worktree) : NULL);
2758 if (error)
2759 goto done;
2761 /* already loaded by tog_log_with_path()? */
2762 if (TAILQ_EMPTY(&tog_refs)) {
2763 error = tog_load_refs(repo);
2764 if (error)
2765 goto done;
2768 if (start_commit == NULL) {
2769 error = got_repo_match_object_id(&start_id, &label,
2770 worktree ? got_worktree_get_head_ref_name(worktree) :
2771 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2772 if (error)
2773 goto done;
2774 head_ref_name = label;
2775 } else {
2776 error = got_ref_open(&ref, repo, start_commit, 0);
2777 if (error == NULL)
2778 head_ref_name = got_ref_get_name(ref);
2779 else if (error->code != GOT_ERR_NOT_REF)
2780 goto done;
2781 error = got_repo_match_object_id(&start_id, NULL,
2782 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2783 if (error)
2784 goto done;
2787 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2788 if (view == NULL) {
2789 error = got_error_from_errno("view_open");
2790 goto done;
2792 error = open_log_view(view, start_id, repo, head_ref_name,
2793 in_repo_path, log_branches);
2794 if (error)
2795 goto done;
2796 if (worktree) {
2797 /* Release work tree lock. */
2798 got_worktree_close(worktree);
2799 worktree = NULL;
2801 error = view_loop(view);
2802 done:
2803 free(in_repo_path);
2804 free(repo_path);
2805 free(cwd);
2806 free(start_id);
2807 free(label);
2808 if (ref)
2809 got_ref_close(ref);
2810 if (repo)
2811 got_repo_close(repo);
2812 if (worktree)
2813 got_worktree_close(worktree);
2814 tog_free_refs();
2815 return error;
2818 __dead static void
2819 usage_diff(void)
2821 endwin();
2822 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2823 "[-w] object1 object2\n", getprogname());
2824 exit(1);
2827 static int
2828 match_line(const char *line, regex_t *regex, size_t nmatch,
2829 regmatch_t *regmatch)
2831 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2834 struct tog_color *
2835 match_color(struct tog_colors *colors, const char *line)
2837 struct tog_color *tc = NULL;
2839 SIMPLEQ_FOREACH(tc, colors, entry) {
2840 if (match_line(line, &tc->regex, 0, NULL))
2841 return tc;
2844 return NULL;
2847 static const struct got_error *
2848 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2849 WINDOW *window, regmatch_t *regmatch)
2851 const struct got_error *err = NULL;
2852 wchar_t *wline;
2853 int width;
2854 char *s;
2856 *wtotal = 0;
2858 s = strndup(line, regmatch->rm_so);
2859 if (s == NULL)
2860 return got_error_from_errno("strndup");
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 if (err) {
2864 free(s);
2865 return err;
2867 waddwstr(window, wline);
2868 free(wline);
2869 free(s);
2870 wlimit -= width;
2871 *wtotal += width;
2873 if (wlimit > 0) {
2874 s = strndup(line + regmatch->rm_so,
2875 regmatch->rm_eo - regmatch->rm_so);
2876 if (s == NULL) {
2877 err = got_error_from_errno("strndup");
2878 free(s);
2879 return err;
2881 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2882 if (err) {
2883 free(s);
2884 return err;
2886 wattr_on(window, A_STANDOUT, NULL);
2887 waddwstr(window, wline);
2888 wattr_off(window, A_STANDOUT, NULL);
2889 free(wline);
2890 free(s);
2891 wlimit -= width;
2892 *wtotal += width;
2895 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2896 err = format_line(&wline, &width,
2897 line + regmatch->rm_eo, wlimit, col_tab_align);
2898 if (err)
2899 return err;
2900 waddwstr(window, wline);
2901 free(wline);
2902 *wtotal += width;
2905 return NULL;
2908 static const struct got_error *
2909 draw_file(struct tog_view *view, const char *header)
2911 struct tog_diff_view_state *s = &view->state.diff;
2912 regmatch_t *regmatch = &view->regmatch;
2913 const struct got_error *err;
2914 int nprinted = 0;
2915 char *line;
2916 size_t linesize = 0;
2917 ssize_t linelen;
2918 struct tog_color *tc;
2919 wchar_t *wline;
2920 int width;
2921 int max_lines = view->nlines;
2922 int nlines = s->nlines;
2923 off_t line_offset;
2925 line_offset = s->line_offsets[s->first_displayed_line - 1];
2926 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2927 return got_error_from_errno("fseek");
2929 werase(view->window);
2931 if (header) {
2932 if (asprintf(&line, "[%d/%d] %s",
2933 s->first_displayed_line - 1 + s->selected_line, nlines,
2934 header) == -1)
2935 return got_error_from_errno("asprintf");
2936 err = format_line(&wline, &width, line, view->ncols, 0);
2937 free(line);
2938 if (err)
2939 return err;
2941 if (view_needs_focus_indication(view))
2942 wstandout(view->window);
2943 waddwstr(view->window, wline);
2944 free(wline);
2945 wline = NULL;
2946 if (view_needs_focus_indication(view))
2947 wstandend(view->window);
2948 if (width <= view->ncols - 1)
2949 waddch(view->window, '\n');
2951 if (max_lines <= 1)
2952 return NULL;
2953 max_lines--;
2956 s->eof = 0;
2957 line = NULL;
2958 while (max_lines > 0 && nprinted < max_lines) {
2959 linelen = getline(&line, &linesize, s->f);
2960 if (linelen == -1) {
2961 if (feof(s->f)) {
2962 s->eof = 1;
2963 break;
2965 free(line);
2966 return got_ferror(s->f, GOT_ERR_IO);
2969 tc = match_color(&s->colors, line);
2970 if (tc)
2971 wattr_on(view->window,
2972 COLOR_PAIR(tc->colorpair), NULL);
2973 if (s->first_displayed_line + nprinted == s->matched_line &&
2974 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2975 err = add_matched_line(&width, line, view->ncols, 0,
2976 view->window, regmatch);
2977 if (err) {
2978 free(line);
2979 return err;
2981 } else {
2982 err = format_line(&wline, &width, line, view->ncols, 0);
2983 if (err) {
2984 free(line);
2985 return err;
2987 waddwstr(view->window, wline);
2988 free(wline);
2989 wline = NULL;
2991 if (tc)
2992 wattr_off(view->window,
2993 COLOR_PAIR(tc->colorpair), NULL);
2994 if (width <= view->ncols - 1)
2995 waddch(view->window, '\n');
2996 nprinted++;
2998 free(line);
2999 if (nprinted >= 1)
3000 s->last_displayed_line = s->first_displayed_line +
3001 (nprinted - 1);
3002 else
3003 s->last_displayed_line = s->first_displayed_line;
3005 view_vborder(view);
3007 if (s->eof) {
3008 while (nprinted < view->nlines) {
3009 waddch(view->window, '\n');
3010 nprinted++;
3013 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3014 if (err) {
3015 return err;
3018 wstandout(view->window);
3019 waddwstr(view->window, wline);
3020 free(wline);
3021 wline = NULL;
3022 wstandend(view->window);
3025 return NULL;
3028 static char *
3029 get_datestr(time_t *time, char *datebuf)
3031 struct tm mytm, *tm;
3032 char *p, *s;
3034 tm = gmtime_r(time, &mytm);
3035 if (tm == NULL)
3036 return NULL;
3037 s = asctime_r(tm, datebuf);
3038 if (s == NULL)
3039 return NULL;
3040 p = strchr(s, '\n');
3041 if (p)
3042 *p = '\0';
3043 return s;
3046 static const struct got_error *
3047 get_changed_paths(struct got_pathlist_head *paths,
3048 struct got_commit_object *commit, struct got_repository *repo)
3050 const struct got_error *err = NULL;
3051 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3052 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3053 struct got_object_qid *qid;
3055 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3056 if (qid != NULL) {
3057 struct got_commit_object *pcommit;
3058 err = got_object_open_as_commit(&pcommit, repo,
3059 qid->id);
3060 if (err)
3061 return err;
3063 tree_id1 = got_object_commit_get_tree_id(pcommit);
3064 got_object_commit_close(pcommit);
3068 if (tree_id1) {
3069 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3070 if (err)
3071 goto done;
3074 tree_id2 = got_object_commit_get_tree_id(commit);
3075 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3076 if (err)
3077 goto done;
3079 err = got_diff_tree(tree1, tree2, "", "", repo,
3080 got_diff_tree_collect_changed_paths, paths, 0);
3081 done:
3082 if (tree1)
3083 got_object_tree_close(tree1);
3084 if (tree2)
3085 got_object_tree_close(tree2);
3086 return err;
3089 static const struct got_error *
3090 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3092 off_t *p;
3094 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3095 if (p == NULL)
3096 return got_error_from_errno("reallocarray");
3097 *line_offsets = p;
3098 (*line_offsets)[*nlines] = off;
3099 (*nlines)++;
3100 return NULL;
3103 static const struct got_error *
3104 write_commit_info(off_t **line_offsets, size_t *nlines,
3105 struct got_object_id *commit_id, struct got_reflist_head *refs,
3106 struct got_repository *repo, FILE *outfile)
3108 const struct got_error *err = NULL;
3109 char datebuf[26], *datestr;
3110 struct got_commit_object *commit;
3111 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3112 time_t committer_time;
3113 const char *author, *committer;
3114 char *refs_str = NULL;
3115 struct got_pathlist_head changed_paths;
3116 struct got_pathlist_entry *pe;
3117 off_t outoff = 0;
3118 int n;
3120 TAILQ_INIT(&changed_paths);
3122 if (refs) {
3123 err = build_refs_str(&refs_str, refs, commit_id, repo);
3124 if (err)
3125 return err;
3128 err = got_object_open_as_commit(&commit, repo, commit_id);
3129 if (err)
3130 return err;
3132 err = got_object_id_str(&id_str, commit_id);
3133 if (err) {
3134 err = got_error_from_errno("got_object_id_str");
3135 goto done;
3138 err = add_line_offset(line_offsets, nlines, 0);
3139 if (err)
3140 goto done;
3142 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3143 refs_str ? refs_str : "", refs_str ? ")" : "");
3144 if (n < 0) {
3145 err = got_error_from_errno("fprintf");
3146 goto done;
3148 outoff += n;
3149 err = add_line_offset(line_offsets, nlines, outoff);
3150 if (err)
3151 goto done;
3153 n = fprintf(outfile, "from: %s\n",
3154 got_object_commit_get_author(commit));
3155 if (n < 0) {
3156 err = got_error_from_errno("fprintf");
3157 goto done;
3159 outoff += n;
3160 err = add_line_offset(line_offsets, nlines, outoff);
3161 if (err)
3162 goto done;
3164 committer_time = got_object_commit_get_committer_time(commit);
3165 datestr = get_datestr(&committer_time, datebuf);
3166 if (datestr) {
3167 n = fprintf(outfile, "date: %s UTC\n", datestr);
3168 if (n < 0) {
3169 err = got_error_from_errno("fprintf");
3170 goto done;
3172 outoff += n;
3173 err = add_line_offset(line_offsets, nlines, outoff);
3174 if (err)
3175 goto done;
3177 author = got_object_commit_get_author(commit);
3178 committer = got_object_commit_get_committer(commit);
3179 if (strcmp(author, committer) != 0) {
3180 n = fprintf(outfile, "via: %s\n", committer);
3181 if (n < 0) {
3182 err = got_error_from_errno("fprintf");
3183 goto done;
3185 outoff += n;
3186 err = add_line_offset(line_offsets, nlines, outoff);
3187 if (err)
3188 goto done;
3190 err = got_object_commit_get_logmsg(&logmsg, commit);
3191 if (err)
3192 goto done;
3193 s = logmsg;
3194 while ((line = strsep(&s, "\n")) != NULL) {
3195 n = fprintf(outfile, "%s\n", line);
3196 if (n < 0) {
3197 err = got_error_from_errno("fprintf");
3198 goto done;
3200 outoff += n;
3201 err = add_line_offset(line_offsets, nlines, outoff);
3202 if (err)
3203 goto done;
3206 err = get_changed_paths(&changed_paths, commit, repo);
3207 if (err)
3208 goto done;
3209 TAILQ_FOREACH(pe, &changed_paths, entry) {
3210 struct got_diff_changed_path *cp = pe->data;
3211 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3212 if (n < 0) {
3213 err = got_error_from_errno("fprintf");
3214 goto done;
3216 outoff += n;
3217 err = add_line_offset(line_offsets, nlines, outoff);
3218 if (err)
3219 goto done;
3220 free((char *)pe->path);
3221 free(pe->data);
3224 fputc('\n', outfile);
3225 outoff++;
3226 err = add_line_offset(line_offsets, nlines, outoff);
3227 done:
3228 got_pathlist_free(&changed_paths);
3229 free(id_str);
3230 free(logmsg);
3231 free(refs_str);
3232 got_object_commit_close(commit);
3233 if (err) {
3234 free(*line_offsets);
3235 *line_offsets = NULL;
3236 *nlines = 0;
3238 return err;
3241 static const struct got_error *
3242 create_diff(struct tog_diff_view_state *s)
3244 const struct got_error *err = NULL;
3245 FILE *f = NULL;
3246 int obj_type;
3248 free(s->line_offsets);
3249 s->line_offsets = malloc(sizeof(off_t));
3250 if (s->line_offsets == NULL)
3251 return got_error_from_errno("malloc");
3252 s->nlines = 0;
3254 f = got_opentemp();
3255 if (f == NULL) {
3256 err = got_error_from_errno("got_opentemp");
3257 goto done;
3259 if (s->f && fclose(s->f) == EOF) {
3260 err = got_error_from_errno("fclose");
3261 goto done;
3263 s->f = f;
3265 if (s->id1)
3266 err = got_object_get_type(&obj_type, s->repo, s->id1);
3267 else
3268 err = got_object_get_type(&obj_type, s->repo, s->id2);
3269 if (err)
3270 goto done;
3272 switch (obj_type) {
3273 case GOT_OBJ_TYPE_BLOB:
3274 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3275 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3276 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3277 break;
3278 case GOT_OBJ_TYPE_TREE:
3279 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3280 s->id1, s->id2, "", "", s->diff_context,
3281 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3282 break;
3283 case GOT_OBJ_TYPE_COMMIT: {
3284 const struct got_object_id_queue *parent_ids;
3285 struct got_object_qid *pid;
3286 struct got_commit_object *commit2;
3287 struct got_reflist_head *refs;
3289 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3290 if (err)
3291 goto done;
3292 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3293 /* Show commit info if we're diffing to a parent/root commit. */
3294 if (s->id1 == NULL) {
3295 err = write_commit_info(&s->line_offsets, &s->nlines,
3296 s->id2, refs, s->repo, s->f);
3297 if (err)
3298 goto done;
3299 } else {
3300 parent_ids = got_object_commit_get_parent_ids(commit2);
3301 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3302 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3303 err = write_commit_info(
3304 &s->line_offsets, &s->nlines,
3305 s->id2, refs, s->repo, s->f);
3306 if (err)
3307 goto done;
3308 break;
3312 got_object_commit_close(commit2);
3314 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3315 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3316 s->force_text_diff, s->repo, s->f);
3317 break;
3319 default:
3320 err = got_error(GOT_ERR_OBJ_TYPE);
3321 break;
3323 if (err)
3324 goto done;
3325 done:
3326 if (s->f && fflush(s->f) != 0 && err == NULL)
3327 err = got_error_from_errno("fflush");
3328 return err;
3331 static void
3332 diff_view_indicate_progress(struct tog_view *view)
3334 mvwaddstr(view->window, 0, 0, "diffing...");
3335 update_panels();
3336 doupdate();
3339 static const struct got_error *
3340 search_start_diff_view(struct tog_view *view)
3342 struct tog_diff_view_state *s = &view->state.diff;
3344 s->matched_line = 0;
3345 return NULL;
3348 static const struct got_error *
3349 search_next_diff_view(struct tog_view *view)
3351 struct tog_diff_view_state *s = &view->state.diff;
3352 int lineno;
3353 char *line = NULL;
3354 size_t linesize = 0;
3355 ssize_t linelen;
3357 if (!view->searching) {
3358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3359 return NULL;
3362 if (s->matched_line) {
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 lineno = s->matched_line + 1;
3365 else
3366 lineno = s->matched_line - 1;
3367 } else {
3368 if (view->searching == TOG_SEARCH_FORWARD)
3369 lineno = 1;
3370 else
3371 lineno = s->nlines;
3374 while (1) {
3375 off_t offset;
3377 if (lineno <= 0 || lineno > s->nlines) {
3378 if (s->matched_line == 0) {
3379 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3380 break;
3383 if (view->searching == TOG_SEARCH_FORWARD)
3384 lineno = 1;
3385 else
3386 lineno = s->nlines;
3389 offset = s->line_offsets[lineno - 1];
3390 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3391 free(line);
3392 return got_error_from_errno("fseeko");
3394 linelen = getline(&line, &linesize, s->f);
3395 if (linelen != -1 &&
3396 match_line(line, &view->regex, 1, &view->regmatch)) {
3397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3398 s->matched_line = lineno;
3399 break;
3401 if (view->searching == TOG_SEARCH_FORWARD)
3402 lineno++;
3403 else
3404 lineno--;
3406 free(line);
3408 if (s->matched_line) {
3409 s->first_displayed_line = s->matched_line;
3410 s->selected_line = 1;
3413 return NULL;
3416 static const struct got_error *
3417 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3418 struct got_object_id *id2, const char *label1, const char *label2,
3419 int diff_context, int ignore_whitespace, int force_text_diff,
3420 struct tog_view *log_view, struct got_repository *repo)
3422 const struct got_error *err;
3423 struct tog_diff_view_state *s = &view->state.diff;
3425 if (id1 != NULL && id2 != NULL) {
3426 int type1, type2;
3427 err = got_object_get_type(&type1, repo, id1);
3428 if (err)
3429 return err;
3430 err = got_object_get_type(&type2, repo, id2);
3431 if (err)
3432 return err;
3434 if (type1 != type2)
3435 return got_error(GOT_ERR_OBJ_TYPE);
3437 s->first_displayed_line = 1;
3438 s->last_displayed_line = view->nlines;
3439 s->selected_line = 1;
3440 s->repo = repo;
3441 s->id1 = id1;
3442 s->id2 = id2;
3443 s->label1 = label1;
3444 s->label2 = label2;
3446 if (id1) {
3447 s->id1 = got_object_id_dup(id1);
3448 if (s->id1 == NULL)
3449 return got_error_from_errno("got_object_id_dup");
3450 } else
3451 s->id1 = NULL;
3453 s->id2 = got_object_id_dup(id2);
3454 if (s->id2 == NULL) {
3455 free(s->id1);
3456 s->id1 = NULL;
3457 return got_error_from_errno("got_object_id_dup");
3459 s->f = NULL;
3460 s->first_displayed_line = 1;
3461 s->last_displayed_line = view->nlines;
3462 s->diff_context = diff_context;
3463 s->ignore_whitespace = ignore_whitespace;
3464 s->force_text_diff = force_text_diff;
3465 s->log_view = log_view;
3466 s->repo = repo;
3468 SIMPLEQ_INIT(&s->colors);
3469 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3470 err = add_color(&s->colors,
3471 "^-", TOG_COLOR_DIFF_MINUS,
3472 get_color_value("TOG_COLOR_DIFF_MINUS"));
3473 if (err)
3474 return err;
3475 err = add_color(&s->colors, "^\\+",
3476 TOG_COLOR_DIFF_PLUS,
3477 get_color_value("TOG_COLOR_DIFF_PLUS"));
3478 if (err) {
3479 free_colors(&s->colors);
3480 return err;
3482 err = add_color(&s->colors,
3483 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3484 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3485 if (err) {
3486 free_colors(&s->colors);
3487 return err;
3490 err = add_color(&s->colors,
3491 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3492 TOG_COLOR_DIFF_META,
3493 get_color_value("TOG_COLOR_DIFF_META"));
3494 if (err) {
3495 free_colors(&s->colors);
3496 return err;
3499 err = add_color(&s->colors,
3500 "^(from|via): ", TOG_COLOR_AUTHOR,
3501 get_color_value("TOG_COLOR_AUTHOR"));
3502 if (err) {
3503 free_colors(&s->colors);
3504 return err;
3507 err = add_color(&s->colors,
3508 "^date: ", TOG_COLOR_DATE,
3509 get_color_value("TOG_COLOR_DATE"));
3510 if (err) {
3511 free_colors(&s->colors);
3512 return err;
3516 if (log_view && view_is_splitscreen(view))
3517 show_log_view(log_view); /* draw vborder */
3518 diff_view_indicate_progress(view);
3520 s->line_offsets = NULL;
3521 s->nlines = 0;
3522 err = create_diff(s);
3523 if (err) {
3524 free(s->id1);
3525 s->id1 = NULL;
3526 free(s->id2);
3527 s->id2 = NULL;
3528 free_colors(&s->colors);
3529 return err;
3532 view->show = show_diff_view;
3533 view->input = input_diff_view;
3534 view->close = close_diff_view;
3535 view->search_start = search_start_diff_view;
3536 view->search_next = search_next_diff_view;
3538 return NULL;
3541 static const struct got_error *
3542 close_diff_view(struct tog_view *view)
3544 const struct got_error *err = NULL;
3545 struct tog_diff_view_state *s = &view->state.diff;
3547 free(s->id1);
3548 s->id1 = NULL;
3549 free(s->id2);
3550 s->id2 = NULL;
3551 if (s->f && fclose(s->f) == EOF)
3552 err = got_error_from_errno("fclose");
3553 free_colors(&s->colors);
3554 free(s->line_offsets);
3555 s->line_offsets = NULL;
3556 s->nlines = 0;
3557 return err;
3560 static const struct got_error *
3561 show_diff_view(struct tog_view *view)
3563 const struct got_error *err;
3564 struct tog_diff_view_state *s = &view->state.diff;
3565 char *id_str1 = NULL, *id_str2, *header;
3566 const char *label1, *label2;
3568 if (s->id1) {
3569 err = got_object_id_str(&id_str1, s->id1);
3570 if (err)
3571 return err;
3572 label1 = s->label1 ? : id_str1;
3573 } else
3574 label1 = "/dev/null";
3576 err = got_object_id_str(&id_str2, s->id2);
3577 if (err)
3578 return err;
3579 label2 = s->label2 ? : id_str2;
3581 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3582 err = got_error_from_errno("asprintf");
3583 free(id_str1);
3584 free(id_str2);
3585 return err;
3587 free(id_str1);
3588 free(id_str2);
3590 return draw_file(view, header);
3593 static const struct got_error *
3594 set_selected_commit(struct tog_diff_view_state *s,
3595 struct commit_queue_entry *entry)
3597 const struct got_error *err;
3598 const struct got_object_id_queue *parent_ids;
3599 struct got_commit_object *selected_commit;
3600 struct got_object_qid *pid;
3602 free(s->id2);
3603 s->id2 = got_object_id_dup(entry->id);
3604 if (s->id2 == NULL)
3605 return got_error_from_errno("got_object_id_dup");
3607 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3608 if (err)
3609 return err;
3610 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3611 free(s->id1);
3612 pid = SIMPLEQ_FIRST(parent_ids);
3613 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3614 got_object_commit_close(selected_commit);
3615 return NULL;
3618 static const struct got_error *
3619 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3621 const struct got_error *err = NULL;
3622 struct tog_diff_view_state *s = &view->state.diff;
3623 struct tog_log_view_state *ls;
3624 struct commit_queue_entry *old_selected_entry;
3625 char *line = NULL;
3626 size_t linesize = 0;
3627 ssize_t linelen;
3628 int i;
3630 switch (ch) {
3631 case 'a':
3632 case 'w':
3633 if (ch == 'a')
3634 s->force_text_diff = !s->force_text_diff;
3635 if (ch == 'w')
3636 s->ignore_whitespace = !s->ignore_whitespace;
3637 wclear(view->window);
3638 s->first_displayed_line = 1;
3639 s->last_displayed_line = view->nlines;
3640 diff_view_indicate_progress(view);
3641 err = create_diff(s);
3642 break;
3643 case 'k':
3644 case KEY_UP:
3645 if (s->first_displayed_line > 1)
3646 s->first_displayed_line--;
3647 break;
3648 case KEY_PPAGE:
3649 case CTRL('b'):
3650 if (s->first_displayed_line == 1)
3651 break;
3652 i = 0;
3653 while (i++ < view->nlines - 1 &&
3654 s->first_displayed_line > 1)
3655 s->first_displayed_line--;
3656 break;
3657 case 'j':
3658 case KEY_DOWN:
3659 if (!s->eof)
3660 s->first_displayed_line++;
3661 break;
3662 case KEY_NPAGE:
3663 case CTRL('f'):
3664 case ' ':
3665 if (s->eof)
3666 break;
3667 i = 0;
3668 while (!s->eof && i++ < view->nlines - 1) {
3669 linelen = getline(&line, &linesize, s->f);
3670 s->first_displayed_line++;
3671 if (linelen == -1) {
3672 if (feof(s->f)) {
3673 s->eof = 1;
3674 } else
3675 err = got_ferror(s->f, GOT_ERR_IO);
3676 break;
3679 free(line);
3680 break;
3681 case '[':
3682 if (s->diff_context > 0) {
3683 s->diff_context--;
3684 diff_view_indicate_progress(view);
3685 err = create_diff(s);
3686 if (s->first_displayed_line + view->nlines - 1 >
3687 s->nlines) {
3688 s->first_displayed_line = 1;
3689 s->last_displayed_line = view->nlines;
3692 break;
3693 case ']':
3694 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3695 s->diff_context++;
3696 diff_view_indicate_progress(view);
3697 err = create_diff(s);
3699 break;
3700 case '<':
3701 case ',':
3702 if (s->log_view == NULL)
3703 break;
3704 ls = &s->log_view->state.log;
3705 old_selected_entry = ls->selected_entry;
3707 err = input_log_view(NULL, s->log_view, KEY_UP);
3708 if (err)
3709 break;
3711 if (old_selected_entry == ls->selected_entry)
3712 break;
3714 err = set_selected_commit(s, ls->selected_entry);
3715 if (err)
3716 break;
3718 s->first_displayed_line = 1;
3719 s->last_displayed_line = view->nlines;
3721 diff_view_indicate_progress(view);
3722 err = create_diff(s);
3723 break;
3724 case '>':
3725 case '.':
3726 if (s->log_view == NULL)
3727 break;
3728 ls = &s->log_view->state.log;
3729 old_selected_entry = ls->selected_entry;
3731 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3732 if (err)
3733 break;
3735 if (old_selected_entry == ls->selected_entry)
3736 break;
3738 err = set_selected_commit(s, ls->selected_entry);
3739 if (err)
3740 break;
3742 s->first_displayed_line = 1;
3743 s->last_displayed_line = view->nlines;
3745 diff_view_indicate_progress(view);
3746 err = create_diff(s);
3747 break;
3748 default:
3749 break;
3752 return err;
3755 static const struct got_error *
3756 cmd_diff(int argc, char *argv[])
3758 const struct got_error *error = NULL;
3759 struct got_repository *repo = NULL;
3760 struct got_worktree *worktree = NULL;
3761 struct got_object_id *id1 = NULL, *id2 = NULL;
3762 char *repo_path = NULL, *cwd = NULL;
3763 char *id_str1 = NULL, *id_str2 = NULL;
3764 char *label1 = NULL, *label2 = NULL;
3765 int diff_context = 3, ignore_whitespace = 0;
3766 int ch, force_text_diff = 0;
3767 const char *errstr;
3768 struct tog_view *view;
3770 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3771 switch (ch) {
3772 case 'a':
3773 force_text_diff = 1;
3774 break;
3775 case 'C':
3776 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3777 &errstr);
3778 if (errstr != NULL)
3779 err(1, "-C option %s", errstr);
3780 break;
3781 case 'r':
3782 repo_path = realpath(optarg, NULL);
3783 if (repo_path == NULL)
3784 return got_error_from_errno2("realpath",
3785 optarg);
3786 got_path_strip_trailing_slashes(repo_path);
3787 break;
3788 case 'w':
3789 ignore_whitespace = 1;
3790 break;
3791 default:
3792 usage_diff();
3793 /* NOTREACHED */
3797 argc -= optind;
3798 argv += optind;
3800 if (argc == 0) {
3801 usage_diff(); /* TODO show local worktree changes */
3802 } else if (argc == 2) {
3803 id_str1 = argv[0];
3804 id_str2 = argv[1];
3805 } else
3806 usage_diff();
3808 if (repo_path == NULL) {
3809 cwd = getcwd(NULL, 0);
3810 if (cwd == NULL)
3811 return got_error_from_errno("getcwd");
3812 error = got_worktree_open(&worktree, cwd);
3813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3814 goto done;
3815 if (worktree)
3816 repo_path =
3817 strdup(got_worktree_get_repo_path(worktree));
3818 else
3819 repo_path = strdup(cwd);
3820 if (repo_path == NULL) {
3821 error = got_error_from_errno("strdup");
3822 goto done;
3826 error = got_repo_open(&repo, repo_path, NULL);
3827 if (error)
3828 goto done;
3830 init_curses();
3832 error = apply_unveil(got_repo_get_path(repo), NULL);
3833 if (error)
3834 goto done;
3836 error = tog_load_refs(repo);
3837 if (error)
3838 goto done;
3840 error = got_repo_match_object_id(&id1, &label1, id_str1,
3841 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3842 if (error)
3843 goto done;
3845 error = got_repo_match_object_id(&id2, &label2, id_str2,
3846 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3847 if (error)
3848 goto done;
3850 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3851 if (view == NULL) {
3852 error = got_error_from_errno("view_open");
3853 goto done;
3855 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3856 ignore_whitespace, force_text_diff, NULL, repo);
3857 if (error)
3858 goto done;
3859 error = view_loop(view);
3860 done:
3861 free(label1);
3862 free(label2);
3863 free(repo_path);
3864 free(cwd);
3865 if (repo)
3866 got_repo_close(repo);
3867 if (worktree)
3868 got_worktree_close(worktree);
3869 tog_free_refs();
3870 return error;
3873 __dead static void
3874 usage_blame(void)
3876 endwin();
3877 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 getprogname());
3879 exit(1);
3882 struct tog_blame_line {
3883 int annotated;
3884 struct got_object_id *id;
3887 static const struct got_error *
3888 draw_blame(struct tog_view *view)
3890 struct tog_blame_view_state *s = &view->state.blame;
3891 struct tog_blame *blame = &s->blame;
3892 regmatch_t *regmatch = &view->regmatch;
3893 const struct got_error *err;
3894 int lineno = 0, nprinted = 0;
3895 char *line = NULL;
3896 size_t linesize = 0;
3897 ssize_t linelen;
3898 wchar_t *wline;
3899 int width;
3900 struct tog_blame_line *blame_line;
3901 struct got_object_id *prev_id = NULL;
3902 char *id_str;
3903 struct tog_color *tc;
3905 err = got_object_id_str(&id_str, s->blamed_commit->id);
3906 if (err)
3907 return err;
3909 rewind(blame->f);
3910 werase(view->window);
3912 if (asprintf(&line, "commit %s", id_str) == -1) {
3913 err = got_error_from_errno("asprintf");
3914 free(id_str);
3915 return err;
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3919 free(line);
3920 line = NULL;
3921 if (err)
3922 return err;
3923 if (view_needs_focus_indication(view))
3924 wstandout(view->window);
3925 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3926 if (tc)
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 waddwstr(view->window, wline);
3930 if (tc)
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 if (view_needs_focus_indication(view))
3934 wstandend(view->window);
3935 free(wline);
3936 wline = NULL;
3937 if (width < view->ncols - 1)
3938 waddch(view->window, '\n');
3940 if (asprintf(&line, "[%d/%d] %s%s",
3941 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3942 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3943 free(id_str);
3944 return got_error_from_errno("asprintf");
3946 free(id_str);
3947 err = format_line(&wline, &width, line, view->ncols, 0);
3948 free(line);
3949 line = NULL;
3950 if (err)
3951 return err;
3952 waddwstr(view->window, wline);
3953 free(wline);
3954 wline = NULL;
3955 if (width < view->ncols - 1)
3956 waddch(view->window, '\n');
3958 s->eof = 0;
3959 while (nprinted < view->nlines - 2) {
3960 linelen = getline(&line, &linesize, blame->f);
3961 if (linelen == -1) {
3962 if (feof(blame->f)) {
3963 s->eof = 1;
3964 break;
3966 free(line);
3967 return got_ferror(blame->f, GOT_ERR_IO);
3969 if (++lineno < s->first_displayed_line)
3970 continue;
3972 if (view->focussed && nprinted == s->selected_line - 1)
3973 wstandout(view->window);
3975 if (blame->nlines > 0) {
3976 blame_line = &blame->lines[lineno - 1];
3977 if (blame_line->annotated && prev_id &&
3978 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3979 !(view->focussed &&
3980 nprinted == s->selected_line - 1)) {
3981 waddstr(view->window, " ");
3982 } else if (blame_line->annotated) {
3983 char *id_str;
3984 err = got_object_id_str(&id_str, blame_line->id);
3985 if (err) {
3986 free(line);
3987 return err;
3989 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3990 if (tc)
3991 wattr_on(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 wprintw(view->window, "%.8s", id_str);
3994 if (tc)
3995 wattr_off(view->window,
3996 COLOR_PAIR(tc->colorpair), NULL);
3997 free(id_str);
3998 prev_id = blame_line->id;
3999 } else {
4000 waddstr(view->window, "........");
4001 prev_id = NULL;
4003 } else {
4004 waddstr(view->window, "........");
4005 prev_id = NULL;
4008 if (view->focussed && nprinted == s->selected_line - 1)
4009 wstandend(view->window);
4010 waddstr(view->window, " ");
4012 if (view->ncols <= 9) {
4013 width = 9;
4014 wline = wcsdup(L"");
4015 if (wline == NULL) {
4016 err = got_error_from_errno("wcsdup");
4017 free(line);
4018 return err;
4020 } else if (s->first_displayed_line + nprinted ==
4021 s->matched_line &&
4022 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4023 err = add_matched_line(&width, line, view->ncols - 9, 9,
4024 view->window, regmatch);
4025 if (err) {
4026 free(line);
4027 return err;
4029 width += 9;
4030 } else {
4031 err = format_line(&wline, &width, line,
4032 view->ncols - 9, 9);
4033 waddwstr(view->window, wline);
4034 free(wline);
4035 wline = NULL;
4036 width += 9;
4039 if (width <= view->ncols - 1)
4040 waddch(view->window, '\n');
4041 if (++nprinted == 1)
4042 s->first_displayed_line = lineno;
4044 free(line);
4045 s->last_displayed_line = lineno;
4047 view_vborder(view);
4049 return NULL;
4052 static const struct got_error *
4053 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4055 const struct got_error *err = NULL;
4056 struct tog_blame_cb_args *a = arg;
4057 struct tog_blame_line *line;
4058 int errcode;
4060 if (nlines != a->nlines ||
4061 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4062 return got_error(GOT_ERR_RANGE);
4064 errcode = pthread_mutex_lock(&tog_mutex);
4065 if (errcode)
4066 return got_error_set_errno(errcode, "pthread_mutex_lock");
4068 if (*a->quit) { /* user has quit the blame view */
4069 err = got_error(GOT_ERR_ITER_COMPLETED);
4070 goto done;
4073 if (lineno == -1)
4074 goto done; /* no change in this commit */
4076 line = &a->lines[lineno - 1];
4077 if (line->annotated)
4078 goto done;
4080 line->id = got_object_id_dup(id);
4081 if (line->id == NULL) {
4082 err = got_error_from_errno("got_object_id_dup");
4083 goto done;
4085 line->annotated = 1;
4086 done:
4087 errcode = pthread_mutex_unlock(&tog_mutex);
4088 if (errcode)
4089 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4090 return err;
4093 static void *
4094 blame_thread(void *arg)
4096 const struct got_error *err;
4097 struct tog_blame_thread_args *ta = arg;
4098 struct tog_blame_cb_args *a = ta->cb_args;
4099 int errcode;
4101 err = block_signals_used_by_main_thread();
4102 if (err)
4103 return (void *)err;
4105 err = got_blame(ta->path, a->commit_id, ta->repo,
4106 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4107 if (err && err->code == GOT_ERR_CANCELLED)
4108 err = NULL;
4110 errcode = pthread_mutex_lock(&tog_mutex);
4111 if (errcode)
4112 return (void *)got_error_set_errno(errcode,
4113 "pthread_mutex_lock");
4115 got_repo_close(ta->repo);
4116 ta->repo = NULL;
4117 *ta->complete = 1;
4119 errcode = pthread_mutex_unlock(&tog_mutex);
4120 if (errcode && err == NULL)
4121 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4123 return (void *)err;
4126 static struct got_object_id *
4127 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4128 int first_displayed_line, int selected_line)
4130 struct tog_blame_line *line;
4132 if (nlines <= 0)
4133 return NULL;
4135 line = &lines[first_displayed_line - 1 + selected_line - 1];
4136 if (!line->annotated)
4137 return NULL;
4139 return line->id;
4142 static const struct got_error *
4143 stop_blame(struct tog_blame *blame)
4145 const struct got_error *err = NULL;
4146 int i;
4148 if (blame->thread) {
4149 int errcode;
4150 errcode = pthread_mutex_unlock(&tog_mutex);
4151 if (errcode)
4152 return got_error_set_errno(errcode,
4153 "pthread_mutex_unlock");
4154 errcode = pthread_join(blame->thread, (void **)&err);
4155 if (errcode)
4156 return got_error_set_errno(errcode, "pthread_join");
4157 errcode = pthread_mutex_lock(&tog_mutex);
4158 if (errcode)
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_lock");
4161 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4162 err = NULL;
4163 blame->thread = NULL;
4165 if (blame->thread_args.repo) {
4166 got_repo_close(blame->thread_args.repo);
4167 blame->thread_args.repo = NULL;
4169 if (blame->f) {
4170 if (fclose(blame->f) == EOF && err == NULL)
4171 err = got_error_from_errno("fclose");
4172 blame->f = NULL;
4174 if (blame->lines) {
4175 for (i = 0; i < blame->nlines; i++)
4176 free(blame->lines[i].id);
4177 free(blame->lines);
4178 blame->lines = NULL;
4180 free(blame->cb_args.commit_id);
4181 blame->cb_args.commit_id = NULL;
4183 return err;
4186 static const struct got_error *
4187 cancel_blame_view(void *arg)
4189 const struct got_error *err = NULL;
4190 int *done = arg;
4191 int errcode;
4193 errcode = pthread_mutex_lock(&tog_mutex);
4194 if (errcode)
4195 return got_error_set_errno(errcode,
4196 "pthread_mutex_unlock");
4198 if (*done)
4199 err = got_error(GOT_ERR_CANCELLED);
4201 errcode = pthread_mutex_unlock(&tog_mutex);
4202 if (errcode)
4203 return got_error_set_errno(errcode,
4204 "pthread_mutex_lock");
4206 return err;
4209 static const struct got_error *
4210 run_blame(struct tog_view *view)
4212 struct tog_blame_view_state *s = &view->state.blame;
4213 struct tog_blame *blame = &s->blame;
4214 const struct got_error *err = NULL;
4215 struct got_blob_object *blob = NULL;
4216 struct got_repository *thread_repo = NULL;
4217 struct got_object_id *obj_id = NULL;
4218 int obj_type;
4220 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4221 s->path);
4222 if (err)
4223 return err;
4225 err = got_object_get_type(&obj_type, s->repo, obj_id);
4226 if (err)
4227 goto done;
4229 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4230 err = got_error(GOT_ERR_OBJ_TYPE);
4231 goto done;
4234 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4235 if (err)
4236 goto done;
4237 blame->f = got_opentemp();
4238 if (blame->f == NULL) {
4239 err = got_error_from_errno("got_opentemp");
4240 goto done;
4242 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4243 &blame->line_offsets, blame->f, blob);
4244 if (err)
4245 goto done;
4246 if (blame->nlines == 0) {
4247 s->blame_complete = 1;
4248 goto done;
4251 /* Don't include \n at EOF in the blame line count. */
4252 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4253 blame->nlines--;
4255 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4256 if (blame->lines == NULL) {
4257 err = got_error_from_errno("calloc");
4258 goto done;
4261 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4262 if (err)
4263 goto done;
4265 blame->cb_args.view = view;
4266 blame->cb_args.lines = blame->lines;
4267 blame->cb_args.nlines = blame->nlines;
4268 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4269 if (blame->cb_args.commit_id == NULL) {
4270 err = got_error_from_errno("got_object_id_dup");
4271 goto done;
4273 blame->cb_args.quit = &s->done;
4275 blame->thread_args.path = s->path;
4276 blame->thread_args.repo = thread_repo;
4277 blame->thread_args.cb_args = &blame->cb_args;
4278 blame->thread_args.complete = &s->blame_complete;
4279 blame->thread_args.cancel_cb = cancel_blame_view;
4280 blame->thread_args.cancel_arg = &s->done;
4281 s->blame_complete = 0;
4283 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4284 s->first_displayed_line = 1;
4285 s->last_displayed_line = view->nlines;
4286 s->selected_line = 1;
4289 done:
4290 if (blob)
4291 got_object_blob_close(blob);
4292 free(obj_id);
4293 if (err)
4294 stop_blame(blame);
4295 return err;
4298 static const struct got_error *
4299 open_blame_view(struct tog_view *view, char *path,
4300 struct got_object_id *commit_id, struct got_repository *repo)
4302 const struct got_error *err = NULL;
4303 struct tog_blame_view_state *s = &view->state.blame;
4305 SIMPLEQ_INIT(&s->blamed_commits);
4307 s->path = strdup(path);
4308 if (s->path == NULL)
4309 return got_error_from_errno("strdup");
4311 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4312 if (err) {
4313 free(s->path);
4314 return err;
4317 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4318 s->first_displayed_line = 1;
4319 s->last_displayed_line = view->nlines;
4320 s->selected_line = 1;
4321 s->blame_complete = 0;
4322 s->repo = repo;
4323 s->commit_id = commit_id;
4324 memset(&s->blame, 0, sizeof(s->blame));
4326 SIMPLEQ_INIT(&s->colors);
4327 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4328 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4329 get_color_value("TOG_COLOR_COMMIT"));
4330 if (err)
4331 return err;
4334 view->show = show_blame_view;
4335 view->input = input_blame_view;
4336 view->close = close_blame_view;
4337 view->search_start = search_start_blame_view;
4338 view->search_next = search_next_blame_view;
4340 return run_blame(view);
4343 static const struct got_error *
4344 close_blame_view(struct tog_view *view)
4346 const struct got_error *err = NULL;
4347 struct tog_blame_view_state *s = &view->state.blame;
4349 if (s->blame.thread)
4350 err = stop_blame(&s->blame);
4352 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4353 struct got_object_qid *blamed_commit;
4354 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4355 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4356 got_object_qid_free(blamed_commit);
4359 free(s->path);
4360 free_colors(&s->colors);
4362 return err;
4365 static const struct got_error *
4366 search_start_blame_view(struct tog_view *view)
4368 struct tog_blame_view_state *s = &view->state.blame;
4370 s->matched_line = 0;
4371 return NULL;
4374 static const struct got_error *
4375 search_next_blame_view(struct tog_view *view)
4377 struct tog_blame_view_state *s = &view->state.blame;
4378 int lineno;
4379 char *line = NULL;
4380 size_t linesize = 0;
4381 ssize_t linelen;
4383 if (!view->searching) {
4384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 return NULL;
4388 if (s->matched_line) {
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno = s->matched_line + 1;
4391 else
4392 lineno = s->matched_line - 1;
4393 } else {
4394 if (view->searching == TOG_SEARCH_FORWARD)
4395 lineno = 1;
4396 else
4397 lineno = s->blame.nlines;
4400 while (1) {
4401 off_t offset;
4403 if (lineno <= 0 || lineno > s->blame.nlines) {
4404 if (s->matched_line == 0) {
4405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4406 break;
4409 if (view->searching == TOG_SEARCH_FORWARD)
4410 lineno = 1;
4411 else
4412 lineno = s->blame.nlines;
4415 offset = s->blame.line_offsets[lineno - 1];
4416 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4417 free(line);
4418 return got_error_from_errno("fseeko");
4420 linelen = getline(&line, &linesize, s->blame.f);
4421 if (linelen != -1 &&
4422 match_line(line, &view->regex, 1, &view->regmatch)) {
4423 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4424 s->matched_line = lineno;
4425 break;
4427 if (view->searching == TOG_SEARCH_FORWARD)
4428 lineno++;
4429 else
4430 lineno--;
4432 free(line);
4434 if (s->matched_line) {
4435 s->first_displayed_line = s->matched_line;
4436 s->selected_line = 1;
4439 return NULL;
4442 static const struct got_error *
4443 show_blame_view(struct tog_view *view)
4445 const struct got_error *err = NULL;
4446 struct tog_blame_view_state *s = &view->state.blame;
4447 int errcode;
4449 if (s->blame.thread == NULL && !s->blame_complete) {
4450 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4451 &s->blame.thread_args);
4452 if (errcode)
4453 return got_error_set_errno(errcode, "pthread_create");
4455 halfdelay(1); /* fast refresh while annotating */
4458 if (s->blame_complete)
4459 halfdelay(10); /* disable fast refresh */
4461 err = draw_blame(view);
4463 view_vborder(view);
4464 return err;
4467 static const struct got_error *
4468 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4470 const struct got_error *err = NULL, *thread_err = NULL;
4471 struct tog_view *diff_view;
4472 struct tog_blame_view_state *s = &view->state.blame;
4473 int begin_x = 0;
4475 switch (ch) {
4476 case 'q':
4477 s->done = 1;
4478 break;
4479 case 'k':
4480 case KEY_UP:
4481 if (s->selected_line > 1)
4482 s->selected_line--;
4483 else if (s->selected_line == 1 &&
4484 s->first_displayed_line > 1)
4485 s->first_displayed_line--;
4486 break;
4487 case KEY_PPAGE:
4488 case CTRL('b'):
4489 if (s->first_displayed_line == 1) {
4490 s->selected_line = 1;
4491 break;
4493 if (s->first_displayed_line > view->nlines - 2)
4494 s->first_displayed_line -=
4495 (view->nlines - 2);
4496 else
4497 s->first_displayed_line = 1;
4498 break;
4499 case 'j':
4500 case KEY_DOWN:
4501 if (s->selected_line < view->nlines - 2 &&
4502 s->first_displayed_line +
4503 s->selected_line <= s->blame.nlines)
4504 s->selected_line++;
4505 else if (s->last_displayed_line <
4506 s->blame.nlines)
4507 s->first_displayed_line++;
4508 break;
4509 case 'b':
4510 case 'p': {
4511 struct got_object_id *id = NULL;
4512 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4513 s->first_displayed_line, s->selected_line);
4514 if (id == NULL)
4515 break;
4516 if (ch == 'p') {
4517 struct got_commit_object *commit;
4518 struct got_object_qid *pid;
4519 struct got_object_id *blob_id = NULL;
4520 int obj_type;
4521 err = got_object_open_as_commit(&commit,
4522 s->repo, id);
4523 if (err)
4524 break;
4525 pid = SIMPLEQ_FIRST(
4526 got_object_commit_get_parent_ids(commit));
4527 if (pid == NULL) {
4528 got_object_commit_close(commit);
4529 break;
4531 /* Check if path history ends here. */
4532 err = got_object_id_by_path(&blob_id, s->repo,
4533 pid->id, s->path);
4534 if (err) {
4535 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4536 err = NULL;
4537 got_object_commit_close(commit);
4538 break;
4540 err = got_object_get_type(&obj_type, s->repo,
4541 blob_id);
4542 free(blob_id);
4543 /* Can't blame non-blob type objects. */
4544 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4545 got_object_commit_close(commit);
4546 break;
4548 err = got_object_qid_alloc(&s->blamed_commit,
4549 pid->id);
4550 got_object_commit_close(commit);
4551 } else {
4552 if (got_object_id_cmp(id,
4553 s->blamed_commit->id) == 0)
4554 break;
4555 err = got_object_qid_alloc(&s->blamed_commit,
4556 id);
4558 if (err)
4559 break;
4560 s->done = 1;
4561 thread_err = stop_blame(&s->blame);
4562 s->done = 0;
4563 if (thread_err)
4564 break;
4565 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4566 s->blamed_commit, entry);
4567 err = run_blame(view);
4568 if (err)
4569 break;
4570 break;
4572 case 'B': {
4573 struct got_object_qid *first;
4574 first = SIMPLEQ_FIRST(&s->blamed_commits);
4575 if (!got_object_id_cmp(first->id, s->commit_id))
4576 break;
4577 s->done = 1;
4578 thread_err = stop_blame(&s->blame);
4579 s->done = 0;
4580 if (thread_err)
4581 break;
4582 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4583 got_object_qid_free(s->blamed_commit);
4584 s->blamed_commit =
4585 SIMPLEQ_FIRST(&s->blamed_commits);
4586 err = run_blame(view);
4587 if (err)
4588 break;
4589 break;
4591 case KEY_ENTER:
4592 case '\r': {
4593 struct got_object_id *id = NULL;
4594 struct got_object_qid *pid;
4595 struct got_commit_object *commit = NULL;
4596 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4597 s->first_displayed_line, s->selected_line);
4598 if (id == NULL)
4599 break;
4600 err = got_object_open_as_commit(&commit, s->repo, id);
4601 if (err)
4602 break;
4603 pid = SIMPLEQ_FIRST(
4604 got_object_commit_get_parent_ids(commit));
4605 if (view_is_parent_view(view))
4606 begin_x = view_split_begin_x(view->begin_x);
4607 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4608 if (diff_view == NULL) {
4609 got_object_commit_close(commit);
4610 err = got_error_from_errno("view_open");
4611 break;
4613 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4614 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4615 got_object_commit_close(commit);
4616 if (err) {
4617 view_close(diff_view);
4618 break;
4620 view->focussed = 0;
4621 diff_view->focussed = 1;
4622 if (view_is_parent_view(view)) {
4623 err = view_close_child(view);
4624 if (err)
4625 break;
4626 view_set_child(view, diff_view);
4627 view->focus_child = 1;
4628 } else
4629 *new_view = diff_view;
4630 if (err)
4631 break;
4632 break;
4634 case KEY_NPAGE:
4635 case CTRL('f'):
4636 case ' ':
4637 if (s->last_displayed_line >= s->blame.nlines &&
4638 s->selected_line >= MIN(s->blame.nlines,
4639 view->nlines - 2)) {
4640 break;
4642 if (s->last_displayed_line >= s->blame.nlines &&
4643 s->selected_line < view->nlines - 2) {
4644 s->selected_line = MIN(s->blame.nlines,
4645 view->nlines - 2);
4646 break;
4648 if (s->last_displayed_line + view->nlines - 2
4649 <= s->blame.nlines)
4650 s->first_displayed_line +=
4651 view->nlines - 2;
4652 else
4653 s->first_displayed_line =
4654 s->blame.nlines -
4655 (view->nlines - 3);
4656 break;
4657 case KEY_RESIZE:
4658 if (s->selected_line > view->nlines - 2) {
4659 s->selected_line = MIN(s->blame.nlines,
4660 view->nlines - 2);
4662 break;
4663 default:
4664 break;
4666 return thread_err ? thread_err : err;
4669 static const struct got_error *
4670 cmd_blame(int argc, char *argv[])
4672 const struct got_error *error;
4673 struct got_repository *repo = NULL;
4674 struct got_worktree *worktree = NULL;
4675 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4676 char *link_target = NULL;
4677 struct got_object_id *commit_id = NULL;
4678 char *commit_id_str = NULL;
4679 int ch;
4680 struct tog_view *view;
4682 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4683 switch (ch) {
4684 case 'c':
4685 commit_id_str = optarg;
4686 break;
4687 case 'r':
4688 repo_path = realpath(optarg, NULL);
4689 if (repo_path == NULL)
4690 return got_error_from_errno2("realpath",
4691 optarg);
4692 break;
4693 default:
4694 usage_blame();
4695 /* NOTREACHED */
4699 argc -= optind;
4700 argv += optind;
4702 if (argc != 1)
4703 usage_blame();
4705 if (repo_path == NULL) {
4706 cwd = getcwd(NULL, 0);
4707 if (cwd == NULL)
4708 return got_error_from_errno("getcwd");
4709 error = got_worktree_open(&worktree, cwd);
4710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4711 goto done;
4712 if (worktree)
4713 repo_path =
4714 strdup(got_worktree_get_repo_path(worktree));
4715 else
4716 repo_path = strdup(cwd);
4717 if (repo_path == NULL) {
4718 error = got_error_from_errno("strdup");
4719 goto done;
4723 error = got_repo_open(&repo, repo_path, NULL);
4724 if (error != NULL)
4725 goto done;
4727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4728 worktree);
4729 if (error)
4730 goto done;
4732 init_curses();
4734 error = apply_unveil(got_repo_get_path(repo), NULL);
4735 if (error)
4736 goto done;
4738 error = tog_load_refs(repo);
4739 if (error)
4740 goto done;
4742 if (commit_id_str == NULL) {
4743 struct got_reference *head_ref;
4744 error = got_ref_open(&head_ref, repo, worktree ?
4745 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4746 if (error != NULL)
4747 goto done;
4748 error = got_ref_resolve(&commit_id, repo, head_ref);
4749 got_ref_close(head_ref);
4750 } else {
4751 error = got_repo_match_object_id(&commit_id, NULL,
4752 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4754 if (error != NULL)
4755 goto done;
4757 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4758 if (view == NULL) {
4759 error = got_error_from_errno("view_open");
4760 goto done;
4763 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4764 commit_id, repo);
4765 if (error)
4766 goto done;
4768 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4769 commit_id, repo);
4770 if (error)
4771 goto done;
4772 if (worktree) {
4773 /* Release work tree lock. */
4774 got_worktree_close(worktree);
4775 worktree = NULL;
4777 error = view_loop(view);
4778 done:
4779 free(repo_path);
4780 free(in_repo_path);
4781 free(link_target);
4782 free(cwd);
4783 free(commit_id);
4784 if (worktree)
4785 got_worktree_close(worktree);
4786 if (repo)
4787 got_repo_close(repo);
4788 tog_free_refs();
4789 return error;
4792 static const struct got_error *
4793 draw_tree_entries(struct tog_view *view, const char *parent_path)
4795 struct tog_tree_view_state *s = &view->state.tree;
4796 const struct got_error *err = NULL;
4797 struct got_tree_entry *te;
4798 wchar_t *wline;
4799 struct tog_color *tc;
4800 int width, n, i, nentries;
4801 int limit = view->nlines;
4803 s->ndisplayed = 0;
4805 werase(view->window);
4807 if (limit == 0)
4808 return NULL;
4810 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4811 if (err)
4812 return err;
4813 if (view_needs_focus_indication(view))
4814 wstandout(view->window);
4815 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4816 if (tc)
4817 wattr_on(view->window,
4818 COLOR_PAIR(tc->colorpair), NULL);
4819 waddwstr(view->window, wline);
4820 if (tc)
4821 wattr_off(view->window,
4822 COLOR_PAIR(tc->colorpair), NULL);
4823 if (view_needs_focus_indication(view))
4824 wstandend(view->window);
4825 free(wline);
4826 wline = NULL;
4827 if (width < view->ncols - 1)
4828 waddch(view->window, '\n');
4829 if (--limit <= 0)
4830 return NULL;
4831 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4832 if (err)
4833 return err;
4834 waddwstr(view->window, wline);
4835 free(wline);
4836 wline = NULL;
4837 if (width < view->ncols - 1)
4838 waddch(view->window, '\n');
4839 if (--limit <= 0)
4840 return NULL;
4841 waddch(view->window, '\n');
4842 if (--limit <= 0)
4843 return NULL;
4845 if (s->first_displayed_entry == NULL) {
4846 te = got_object_tree_get_first_entry(s->tree);
4847 if (s->selected == 0) {
4848 if (view->focussed)
4849 wstandout(view->window);
4850 s->selected_entry = NULL;
4852 waddstr(view->window, " ..\n"); /* parent directory */
4853 if (s->selected == 0 && view->focussed)
4854 wstandend(view->window);
4855 s->ndisplayed++;
4856 if (--limit <= 0)
4857 return NULL;
4858 n = 1;
4859 } else {
4860 n = 0;
4861 te = s->first_displayed_entry;
4864 nentries = got_object_tree_get_nentries(s->tree);
4865 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4866 char *line = NULL, *id_str = NULL, *link_target = NULL;
4867 const char *modestr = "";
4868 mode_t mode;
4870 te = got_object_tree_get_entry(s->tree, i);
4871 mode = got_tree_entry_get_mode(te);
4873 if (s->show_ids) {
4874 err = got_object_id_str(&id_str,
4875 got_tree_entry_get_id(te));
4876 if (err)
4877 return got_error_from_errno(
4878 "got_object_id_str");
4880 if (got_object_tree_entry_is_submodule(te))
4881 modestr = "$";
4882 else if (S_ISLNK(mode)) {
4883 int i;
4885 err = got_tree_entry_get_symlink_target(&link_target,
4886 te, s->repo);
4887 if (err) {
4888 free(id_str);
4889 return err;
4891 for (i = 0; i < strlen(link_target); i++) {
4892 if (!isprint((unsigned char)link_target[i]))
4893 link_target[i] = '?';
4895 modestr = "@";
4897 else if (S_ISDIR(mode))
4898 modestr = "/";
4899 else if (mode & S_IXUSR)
4900 modestr = "*";
4901 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4902 got_tree_entry_get_name(te), modestr,
4903 link_target ? " -> ": "",
4904 link_target ? link_target : "") == -1) {
4905 free(id_str);
4906 free(link_target);
4907 return got_error_from_errno("asprintf");
4909 free(id_str);
4910 free(link_target);
4911 err = format_line(&wline, &width, line, view->ncols, 0);
4912 if (err) {
4913 free(line);
4914 break;
4916 if (n == s->selected) {
4917 if (view->focussed)
4918 wstandout(view->window);
4919 s->selected_entry = te;
4921 tc = match_color(&s->colors, line);
4922 if (tc)
4923 wattr_on(view->window,
4924 COLOR_PAIR(tc->colorpair), NULL);
4925 waddwstr(view->window, wline);
4926 if (tc)
4927 wattr_off(view->window,
4928 COLOR_PAIR(tc->colorpair), NULL);
4929 if (width < view->ncols - 1)
4930 waddch(view->window, '\n');
4931 if (n == s->selected && view->focussed)
4932 wstandend(view->window);
4933 free(line);
4934 free(wline);
4935 wline = NULL;
4936 n++;
4937 s->ndisplayed++;
4938 s->last_displayed_entry = te;
4939 if (--limit <= 0)
4940 break;
4943 return err;
4946 static void
4947 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4949 struct got_tree_entry *te;
4950 int isroot = s->tree == s->root;
4951 int i = 0;
4953 if (s->first_displayed_entry == NULL)
4954 return;
4956 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4957 while (i++ < maxscroll) {
4958 if (te == NULL) {
4959 if (!isroot)
4960 s->first_displayed_entry = NULL;
4961 break;
4963 s->first_displayed_entry = te;
4964 te = got_tree_entry_get_prev(s->tree, te);
4968 static void
4969 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4971 struct got_tree_entry *next, *last;
4972 int n = 0;
4974 if (s->first_displayed_entry)
4975 next = got_tree_entry_get_next(s->tree,
4976 s->first_displayed_entry);
4977 else
4978 next = got_object_tree_get_first_entry(s->tree);
4980 last = s->last_displayed_entry;
4981 while (next && last && n++ < maxscroll) {
4982 last = got_tree_entry_get_next(s->tree, last);
4983 if (last) {
4984 s->first_displayed_entry = next;
4985 next = got_tree_entry_get_next(s->tree, next);
4990 static const struct got_error *
4991 tree_entry_path(char **path, struct tog_parent_trees *parents,
4992 struct got_tree_entry *te)
4994 const struct got_error *err = NULL;
4995 struct tog_parent_tree *pt;
4996 size_t len = 2; /* for leading slash and NUL */
4998 TAILQ_FOREACH(pt, parents, entry)
4999 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5000 + 1 /* slash */;
5001 if (te)
5002 len += strlen(got_tree_entry_get_name(te));
5004 *path = calloc(1, len);
5005 if (path == NULL)
5006 return got_error_from_errno("calloc");
5008 (*path)[0] = '/';
5009 pt = TAILQ_LAST(parents, tog_parent_trees);
5010 while (pt) {
5011 const char *name = got_tree_entry_get_name(pt->selected_entry);
5012 if (strlcat(*path, name, len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5014 goto done;
5016 if (strlcat(*path, "/", len) >= len) {
5017 err = got_error(GOT_ERR_NO_SPACE);
5018 goto done;
5020 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5022 if (te) {
5023 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5024 err = got_error(GOT_ERR_NO_SPACE);
5025 goto done;
5028 done:
5029 if (err) {
5030 free(*path);
5031 *path = NULL;
5033 return err;
5036 static const struct got_error *
5037 blame_tree_entry(struct tog_view **new_view, int begin_x,
5038 struct got_tree_entry *te, struct tog_parent_trees *parents,
5039 struct got_object_id *commit_id, struct got_repository *repo)
5041 const struct got_error *err = NULL;
5042 char *path;
5043 struct tog_view *blame_view;
5045 *new_view = NULL;
5047 err = tree_entry_path(&path, parents, te);
5048 if (err)
5049 return err;
5051 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5052 if (blame_view == NULL) {
5053 err = got_error_from_errno("view_open");
5054 goto done;
5057 err = open_blame_view(blame_view, path, commit_id, repo);
5058 if (err) {
5059 if (err->code == GOT_ERR_CANCELLED)
5060 err = NULL;
5061 view_close(blame_view);
5062 } else
5063 *new_view = blame_view;
5064 done:
5065 free(path);
5066 return err;
5069 static const struct got_error *
5070 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5071 struct tog_tree_view_state *s)
5073 struct tog_view *log_view;
5074 const struct got_error *err = NULL;
5075 char *path;
5077 *new_view = NULL;
5079 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 if (log_view == NULL)
5081 return got_error_from_errno("view_open");
5083 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5084 if (err)
5085 return err;
5087 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5088 path, 0);
5089 if (err)
5090 view_close(log_view);
5091 else
5092 *new_view = log_view;
5093 free(path);
5094 return err;
5097 static const struct got_error *
5098 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5099 struct got_object_id *commit_id, const char *head_ref_name,
5100 struct got_repository *repo)
5102 const struct got_error *err = NULL;
5103 char *commit_id_str = NULL;
5104 struct tog_tree_view_state *s = &view->state.tree;
5106 TAILQ_INIT(&s->parents);
5108 err = got_object_id_str(&commit_id_str, commit_id);
5109 if (err != NULL)
5110 goto done;
5112 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5113 err = got_error_from_errno("asprintf");
5114 goto done;
5117 s->root = s->tree = root;
5118 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5119 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5120 s->commit_id = got_object_id_dup(commit_id);
5121 if (s->commit_id == NULL) {
5122 err = got_error_from_errno("got_object_id_dup");
5123 goto done;
5125 if (head_ref_name) {
5126 s->head_ref_name = strdup(head_ref_name);
5127 if (s->head_ref_name == NULL) {
5128 err = got_error_from_errno("strdup");
5129 goto done;
5132 s->repo = repo;
5134 SIMPLEQ_INIT(&s->colors);
5136 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5137 err = add_color(&s->colors, "\\$$",
5138 TOG_COLOR_TREE_SUBMODULE,
5139 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5140 if (err)
5141 goto done;
5142 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5143 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5144 if (err) {
5145 free_colors(&s->colors);
5146 goto done;
5148 err = add_color(&s->colors, "/$",
5149 TOG_COLOR_TREE_DIRECTORY,
5150 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5151 if (err) {
5152 free_colors(&s->colors);
5153 goto done;
5156 err = add_color(&s->colors, "\\*$",
5157 TOG_COLOR_TREE_EXECUTABLE,
5158 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5159 if (err) {
5160 free_colors(&s->colors);
5161 goto done;
5164 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5165 get_color_value("TOG_COLOR_COMMIT"));
5166 if (err) {
5167 free_colors(&s->colors);
5168 goto done;
5172 view->show = show_tree_view;
5173 view->input = input_tree_view;
5174 view->close = close_tree_view;
5175 view->search_start = search_start_tree_view;
5176 view->search_next = search_next_tree_view;
5177 done:
5178 free(commit_id_str);
5179 if (err) {
5180 free(s->tree_label);
5181 s->tree_label = NULL;
5183 return err;
5186 static const struct got_error *
5187 close_tree_view(struct tog_view *view)
5189 struct tog_tree_view_state *s = &view->state.tree;
5191 free_colors(&s->colors);
5192 free(s->tree_label);
5193 s->tree_label = NULL;
5194 free(s->commit_id);
5195 s->commit_id = NULL;
5196 free(s->head_ref_name);
5197 s->head_ref_name = NULL;
5198 while (!TAILQ_EMPTY(&s->parents)) {
5199 struct tog_parent_tree *parent;
5200 parent = TAILQ_FIRST(&s->parents);
5201 TAILQ_REMOVE(&s->parents, parent, entry);
5202 free(parent);
5205 if (s->tree != s->root)
5206 got_object_tree_close(s->tree);
5207 got_object_tree_close(s->root);
5208 return NULL;
5211 static const struct got_error *
5212 search_start_tree_view(struct tog_view *view)
5214 struct tog_tree_view_state *s = &view->state.tree;
5216 s->matched_entry = NULL;
5217 return NULL;
5220 static int
5221 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5223 regmatch_t regmatch;
5225 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5226 0) == 0;
5229 static const struct got_error *
5230 search_next_tree_view(struct tog_view *view)
5232 struct tog_tree_view_state *s = &view->state.tree;
5233 struct got_tree_entry *te = NULL;
5235 if (!view->searching) {
5236 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5237 return NULL;
5240 if (s->matched_entry) {
5241 if (view->searching == TOG_SEARCH_FORWARD) {
5242 if (s->selected_entry)
5243 te = got_tree_entry_get_next(s->tree,
5244 s->selected_entry);
5245 else
5246 te = got_object_tree_get_first_entry(s->tree);
5247 } else {
5248 if (s->selected_entry == NULL)
5249 te = got_object_tree_get_last_entry(s->tree);
5250 else
5251 te = got_tree_entry_get_prev(s->tree,
5252 s->selected_entry);
5254 } else {
5255 if (view->searching == TOG_SEARCH_FORWARD)
5256 te = got_object_tree_get_first_entry(s->tree);
5257 else
5258 te = got_object_tree_get_last_entry(s->tree);
5261 while (1) {
5262 if (te == NULL) {
5263 if (s->matched_entry == NULL) {
5264 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5265 return NULL;
5267 if (view->searching == TOG_SEARCH_FORWARD)
5268 te = got_object_tree_get_first_entry(s->tree);
5269 else
5270 te = got_object_tree_get_last_entry(s->tree);
5273 if (match_tree_entry(te, &view->regex)) {
5274 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5275 s->matched_entry = te;
5276 break;
5279 if (view->searching == TOG_SEARCH_FORWARD)
5280 te = got_tree_entry_get_next(s->tree, te);
5281 else
5282 te = got_tree_entry_get_prev(s->tree, te);
5285 if (s->matched_entry) {
5286 s->first_displayed_entry = s->matched_entry;
5287 s->selected = 0;
5290 return NULL;
5293 static const struct got_error *
5294 show_tree_view(struct tog_view *view)
5296 const struct got_error *err = NULL;
5297 struct tog_tree_view_state *s = &view->state.tree;
5298 char *parent_path;
5300 err = tree_entry_path(&parent_path, &s->parents, NULL);
5301 if (err)
5302 return err;
5304 err = draw_tree_entries(view, parent_path);
5305 free(parent_path);
5307 view_vborder(view);
5308 return err;
5311 static const struct got_error *
5312 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5314 const struct got_error *err = NULL;
5315 struct tog_tree_view_state *s = &view->state.tree;
5316 struct tog_view *log_view, *ref_view;
5317 int begin_x = 0;
5319 switch (ch) {
5320 case 'i':
5321 s->show_ids = !s->show_ids;
5322 break;
5323 case 'l':
5324 if (!s->selected_entry)
5325 break;
5326 if (view_is_parent_view(view))
5327 begin_x = view_split_begin_x(view->begin_x);
5328 err = log_selected_tree_entry(&log_view, begin_x, s);
5329 view->focussed = 0;
5330 log_view->focussed = 1;
5331 if (view_is_parent_view(view)) {
5332 err = view_close_child(view);
5333 if (err)
5334 return err;
5335 view_set_child(view, log_view);
5336 view->focus_child = 1;
5337 } else
5338 *new_view = log_view;
5339 break;
5340 case 'r':
5341 if (view_is_parent_view(view))
5342 begin_x = view_split_begin_x(view->begin_x);
5343 ref_view = view_open(view->nlines, view->ncols,
5344 view->begin_y, begin_x, TOG_VIEW_REF);
5345 if (ref_view == NULL)
5346 return got_error_from_errno("view_open");
5347 err = open_ref_view(ref_view, s->repo);
5348 if (err) {
5349 view_close(ref_view);
5350 return err;
5352 view->focussed = 0;
5353 ref_view->focussed = 1;
5354 if (view_is_parent_view(view)) {
5355 err = view_close_child(view);
5356 if (err)
5357 return err;
5358 view_set_child(view, ref_view);
5359 view->focus_child = 1;
5360 } else
5361 *new_view = ref_view;
5362 break;
5363 case 'k':
5364 case KEY_UP:
5365 if (s->selected > 0) {
5366 s->selected--;
5367 break;
5369 tree_scroll_up(s, 1);
5370 break;
5371 case KEY_PPAGE:
5372 case CTRL('b'):
5373 if (s->tree == s->root) {
5374 if (got_object_tree_get_first_entry(s->tree) ==
5375 s->first_displayed_entry)
5376 s->selected = 0;
5377 } else {
5378 if (s->first_displayed_entry == NULL)
5379 s->selected = 0;
5381 tree_scroll_up(s, MAX(0, view->nlines - 3));
5382 break;
5383 case 'j':
5384 case KEY_DOWN:
5385 if (s->selected < s->ndisplayed - 1) {
5386 s->selected++;
5387 break;
5389 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5390 == NULL)
5391 /* can't scroll any further */
5392 break;
5393 tree_scroll_down(s, 1);
5394 break;
5395 case KEY_NPAGE:
5396 case CTRL('f'):
5397 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5398 == NULL) {
5399 /* can't scroll any further; move cursor down */
5400 if (s->selected < s->ndisplayed - 1)
5401 s->selected = s->ndisplayed - 1;
5402 break;
5404 tree_scroll_down(s, view->nlines - 3);
5405 break;
5406 case KEY_ENTER:
5407 case '\r':
5408 case KEY_BACKSPACE:
5409 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5410 struct tog_parent_tree *parent;
5411 /* user selected '..' */
5412 if (s->tree == s->root)
5413 break;
5414 parent = TAILQ_FIRST(&s->parents);
5415 TAILQ_REMOVE(&s->parents, parent,
5416 entry);
5417 got_object_tree_close(s->tree);
5418 s->tree = parent->tree;
5419 s->first_displayed_entry =
5420 parent->first_displayed_entry;
5421 s->selected_entry =
5422 parent->selected_entry;
5423 s->selected = parent->selected;
5424 free(parent);
5425 } else if (S_ISDIR(got_tree_entry_get_mode(
5426 s->selected_entry))) {
5427 struct got_tree_object *subtree;
5428 err = got_object_open_as_tree(&subtree, s->repo,
5429 got_tree_entry_get_id(s->selected_entry));
5430 if (err)
5431 break;
5432 err = tree_view_visit_subtree(s, subtree);
5433 if (err) {
5434 got_object_tree_close(subtree);
5435 break;
5437 } else if (S_ISREG(got_tree_entry_get_mode(
5438 s->selected_entry))) {
5439 struct tog_view *blame_view;
5440 int begin_x = view_is_parent_view(view) ?
5441 view_split_begin_x(view->begin_x) : 0;
5443 err = blame_tree_entry(&blame_view, begin_x,
5444 s->selected_entry, &s->parents,
5445 s->commit_id, s->repo);
5446 if (err)
5447 break;
5448 view->focussed = 0;
5449 blame_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5452 if (err)
5453 return err;
5454 view_set_child(view, blame_view);
5455 view->focus_child = 1;
5456 } else
5457 *new_view = blame_view;
5459 break;
5460 case KEY_RESIZE:
5461 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5462 s->selected = view->nlines - 4;
5463 break;
5464 default:
5465 break;
5468 return err;
5471 __dead static void
5472 usage_tree(void)
5474 endwin();
5475 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5476 getprogname());
5477 exit(1);
5480 static const struct got_error *
5481 cmd_tree(int argc, char *argv[])
5483 const struct got_error *error;
5484 struct got_repository *repo = NULL;
5485 struct got_worktree *worktree = NULL;
5486 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5487 struct got_object_id *commit_id = NULL;
5488 const char *commit_id_arg = NULL;
5489 char *label = NULL;
5490 struct got_commit_object *commit = NULL;
5491 struct got_tree_object *tree = NULL;
5492 struct got_reference *ref = NULL;
5493 const char *head_ref_name = NULL;
5494 int ch;
5495 struct tog_view *view;
5497 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5498 switch (ch) {
5499 case 'c':
5500 commit_id_arg = optarg;
5501 break;
5502 case 'r':
5503 repo_path = realpath(optarg, NULL);
5504 if (repo_path == NULL)
5505 return got_error_from_errno2("realpath",
5506 optarg);
5507 break;
5508 default:
5509 usage_tree();
5510 /* NOTREACHED */
5514 argc -= optind;
5515 argv += optind;
5517 if (argc > 1)
5518 usage_tree();
5520 if (repo_path == NULL) {
5521 cwd = getcwd(NULL, 0);
5522 if (cwd == NULL)
5523 return got_error_from_errno("getcwd");
5524 error = got_worktree_open(&worktree, cwd);
5525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5526 goto done;
5527 if (worktree)
5528 repo_path =
5529 strdup(got_worktree_get_repo_path(worktree));
5530 else
5531 repo_path = strdup(cwd);
5532 if (repo_path == NULL) {
5533 error = got_error_from_errno("strdup");
5534 goto done;
5538 error = got_repo_open(&repo, repo_path, NULL);
5539 if (error != NULL)
5540 goto done;
5542 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5543 repo, worktree);
5544 if (error)
5545 goto done;
5547 init_curses();
5549 error = apply_unveil(got_repo_get_path(repo), NULL);
5550 if (error)
5551 goto done;
5553 error = tog_load_refs(repo);
5554 if (error)
5555 goto done;
5557 if (commit_id_arg == NULL) {
5558 error = got_repo_match_object_id(&commit_id, &label,
5559 worktree ? got_worktree_get_head_ref_name(worktree) :
5560 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5561 if (error)
5562 goto done;
5563 head_ref_name = label;
5564 } else {
5565 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5566 if (error == NULL)
5567 head_ref_name = got_ref_get_name(ref);
5568 else if (error->code != GOT_ERR_NOT_REF)
5569 goto done;
5570 error = got_repo_match_object_id(&commit_id, NULL,
5571 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5572 if (error)
5573 goto done;
5576 error = got_object_open_as_commit(&commit, repo, commit_id);
5577 if (error)
5578 goto done;
5580 error = got_object_open_as_tree(&tree, repo,
5581 got_object_commit_get_tree_id(commit));
5582 if (error)
5583 goto done;
5585 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5586 if (view == NULL) {
5587 error = got_error_from_errno("view_open");
5588 goto done;
5590 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5591 if (error)
5592 goto done;
5593 if (!got_path_is_root_dir(in_repo_path)) {
5594 error = tree_view_walk_path(&view->state.tree, commit_id,
5595 in_repo_path);
5596 if (error)
5597 goto done;
5600 if (worktree) {
5601 /* Release work tree lock. */
5602 got_worktree_close(worktree);
5603 worktree = NULL;
5605 error = view_loop(view);
5606 done:
5607 free(repo_path);
5608 free(cwd);
5609 free(commit_id);
5610 free(label);
5611 if (ref)
5612 got_ref_close(ref);
5613 if (commit)
5614 got_object_commit_close(commit);
5615 if (tree)
5616 got_object_tree_close(tree);
5617 if (repo)
5618 got_repo_close(repo);
5619 tog_free_refs();
5620 return error;
5623 static const struct got_error *
5624 ref_view_load_refs(struct tog_ref_view_state *s)
5626 struct got_reflist_entry *sre;
5627 struct tog_reflist_entry *re;
5629 s->nrefs = 0;
5630 TAILQ_FOREACH(sre, &tog_refs, entry) {
5631 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5632 continue;
5634 re = malloc(sizeof(*re));
5635 if (re == NULL)
5636 return got_error_from_errno("malloc");
5638 re->ref = got_ref_dup(sre->ref);
5639 if (re->ref == NULL)
5640 return got_error_from_errno("got_ref_dup");
5641 re->idx = s->nrefs++;
5642 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5645 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5646 return NULL;
5649 void
5650 ref_view_free_refs(struct tog_ref_view_state *s)
5652 struct tog_reflist_entry *re;
5654 while (!TAILQ_EMPTY(&s->refs)) {
5655 re = TAILQ_FIRST(&s->refs);
5656 TAILQ_REMOVE(&s->refs, re, entry);
5657 got_ref_close(re->ref);
5658 free(re);
5662 static const struct got_error *
5663 open_ref_view(struct tog_view *view, struct got_repository *repo)
5665 const struct got_error *err = NULL;
5666 struct tog_ref_view_state *s = &view->state.ref;
5668 s->selected_entry = 0;
5669 s->repo = repo;
5671 TAILQ_INIT(&s->refs);
5672 SIMPLEQ_INIT(&s->colors);
5674 err = ref_view_load_refs(s);
5675 if (err)
5676 return err;
5678 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5679 err = add_color(&s->colors, "^refs/heads/",
5680 TOG_COLOR_REFS_HEADS,
5681 get_color_value("TOG_COLOR_REFS_HEADS"));
5682 if (err)
5683 goto done;
5685 err = add_color(&s->colors, "^refs/tags/",
5686 TOG_COLOR_REFS_TAGS,
5687 get_color_value("TOG_COLOR_REFS_TAGS"));
5688 if (err)
5689 goto done;
5691 err = add_color(&s->colors, "^refs/remotes/",
5692 TOG_COLOR_REFS_REMOTES,
5693 get_color_value("TOG_COLOR_REFS_REMOTES"));
5694 if (err)
5695 goto done;
5698 view->show = show_ref_view;
5699 view->input = input_ref_view;
5700 view->close = close_ref_view;
5701 view->search_start = search_start_ref_view;
5702 view->search_next = search_next_ref_view;
5703 done:
5704 if (err)
5705 free_colors(&s->colors);
5706 return err;
5709 static const struct got_error *
5710 close_ref_view(struct tog_view *view)
5712 struct tog_ref_view_state *s = &view->state.ref;
5714 ref_view_free_refs(s);
5715 free_colors(&s->colors);
5717 return NULL;
5720 static const struct got_error *
5721 resolve_reflist_entry(struct got_object_id **commit_id,
5722 struct tog_reflist_entry *re, struct got_repository *repo)
5724 const struct got_error *err = NULL;
5725 struct got_object_id *obj_id;
5726 struct got_tag_object *tag = NULL;
5727 int obj_type;
5729 *commit_id = NULL;
5731 err = got_ref_resolve(&obj_id, repo, re->ref);
5732 if (err)
5733 return err;
5735 err = got_object_get_type(&obj_type, repo, obj_id);
5736 if (err)
5737 goto done;
5739 switch (obj_type) {
5740 case GOT_OBJ_TYPE_COMMIT:
5741 *commit_id = obj_id;
5742 break;
5743 case GOT_OBJ_TYPE_TAG:
5744 err = got_object_open_as_tag(&tag, repo, obj_id);
5745 if (err)
5746 goto done;
5747 free(obj_id);
5748 err = got_object_get_type(&obj_type, repo,
5749 got_object_tag_get_object_id(tag));
5750 if (err)
5751 goto done;
5752 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5753 err = got_error(GOT_ERR_OBJ_TYPE);
5754 goto done;
5756 *commit_id = got_object_id_dup(
5757 got_object_tag_get_object_id(tag));
5758 if (*commit_id == NULL) {
5759 err = got_error_from_errno("got_object_id_dup");
5760 goto done;
5762 break;
5763 default:
5764 err = got_error(GOT_ERR_OBJ_TYPE);
5765 break;
5768 done:
5769 if (tag)
5770 got_object_tag_close(tag);
5771 if (err) {
5772 free(*commit_id);
5773 *commit_id = NULL;
5775 return err;
5778 static const struct got_error *
5779 log_ref_entry(struct tog_view **new_view, int begin_x,
5780 struct tog_reflist_entry *re, struct got_repository *repo)
5782 struct tog_view *log_view;
5783 const struct got_error *err = NULL;
5784 struct got_object_id *commit_id = NULL;
5786 *new_view = NULL;
5788 err = resolve_reflist_entry(&commit_id, re, repo);
5789 if (err) {
5790 if (err->code != GOT_ERR_OBJ_TYPE)
5791 return err;
5792 else
5793 return NULL;
5796 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5797 if (log_view == NULL) {
5798 err = got_error_from_errno("view_open");
5799 goto done;
5802 err = open_log_view(log_view, commit_id, repo,
5803 got_ref_get_name(re->ref), "", 0);
5804 done:
5805 if (err)
5806 view_close(log_view);
5807 else
5808 *new_view = log_view;
5809 free(commit_id);
5810 return err;
5813 static void
5814 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5816 struct tog_reflist_entry *re;
5817 int i = 0;
5819 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5820 return;
5822 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5823 while (i++ < maxscroll) {
5824 if (re == NULL)
5825 break;
5826 s->first_displayed_entry = re;
5827 re = TAILQ_PREV(re, tog_reflist_head, entry);
5831 static void
5832 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5834 struct tog_reflist_entry *next, *last;
5835 int n = 0;
5837 if (s->first_displayed_entry)
5838 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5839 else
5840 next = TAILQ_FIRST(&s->refs);
5842 last = s->last_displayed_entry;
5843 while (next && last && n++ < maxscroll) {
5844 last = TAILQ_NEXT(last, entry);
5845 if (last) {
5846 s->first_displayed_entry = next;
5847 next = TAILQ_NEXT(next, entry);
5852 static const struct got_error *
5853 search_start_ref_view(struct tog_view *view)
5855 struct tog_ref_view_state *s = &view->state.ref;
5857 s->matched_entry = NULL;
5858 return NULL;
5861 static int
5862 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5864 regmatch_t regmatch;
5866 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5867 0) == 0;
5870 static const struct got_error *
5871 search_next_ref_view(struct tog_view *view)
5873 struct tog_ref_view_state *s = &view->state.ref;
5874 struct tog_reflist_entry *re = NULL;
5876 if (!view->searching) {
5877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5878 return NULL;
5881 if (s->matched_entry) {
5882 if (view->searching == TOG_SEARCH_FORWARD) {
5883 if (s->selected_entry)
5884 re = TAILQ_NEXT(s->selected_entry, entry);
5885 else
5886 re = TAILQ_PREV(s->selected_entry,
5887 tog_reflist_head, entry);
5888 } else {
5889 if (s->selected_entry == NULL)
5890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5891 else
5892 re = TAILQ_PREV(s->selected_entry,
5893 tog_reflist_head, entry);
5895 } else {
5896 if (view->searching == TOG_SEARCH_FORWARD)
5897 re = TAILQ_FIRST(&s->refs);
5898 else
5899 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5902 while (1) {
5903 if (re == NULL) {
5904 if (s->matched_entry == NULL) {
5905 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5906 return NULL;
5908 if (view->searching == TOG_SEARCH_FORWARD)
5909 re = TAILQ_FIRST(&s->refs);
5910 else
5911 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5914 if (match_reflist_entry(re, &view->regex)) {
5915 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5916 s->matched_entry = re;
5917 break;
5920 if (view->searching == TOG_SEARCH_FORWARD)
5921 re = TAILQ_NEXT(re, entry);
5922 else
5923 re = TAILQ_PREV(re, tog_reflist_head, entry);
5926 if (s->matched_entry) {
5927 s->first_displayed_entry = s->matched_entry;
5928 s->selected = 0;
5931 return NULL;
5934 static const struct got_error *
5935 show_ref_view(struct tog_view *view)
5937 const struct got_error *err = NULL;
5938 struct tog_ref_view_state *s = &view->state.ref;
5939 struct tog_reflist_entry *re;
5940 char *line = NULL;
5941 wchar_t *wline;
5942 struct tog_color *tc;
5943 int width, n;
5944 int limit = view->nlines;
5946 werase(view->window);
5948 s->ndisplayed = 0;
5950 if (limit == 0)
5951 return NULL;
5953 re = s->first_displayed_entry;
5955 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5956 s->nrefs) == -1)
5957 return got_error_from_errno("asprintf");
5959 err = format_line(&wline, &width, line, view->ncols, 0);
5960 if (err) {
5961 free(line);
5962 return err;
5964 if (view_needs_focus_indication(view))
5965 wstandout(view->window);
5966 waddwstr(view->window, wline);
5967 if (view_needs_focus_indication(view))
5968 wstandend(view->window);
5969 free(wline);
5970 wline = NULL;
5971 free(line);
5972 line = NULL;
5973 if (width < view->ncols - 1)
5974 waddch(view->window, '\n');
5975 if (--limit <= 0)
5976 return NULL;
5978 n = 0;
5979 while (re && limit > 0) {
5980 char *line = NULL;
5982 if (got_ref_is_symbolic(re->ref)) {
5983 if (asprintf(&line, "%s -> %s",
5984 got_ref_get_name(re->ref),
5985 got_ref_get_symref_target(re->ref)) == -1)
5986 return got_error_from_errno("asprintf");
5987 } else if (s->show_ids) {
5988 struct got_object_id *id;
5989 char *id_str;
5990 err = got_ref_resolve(&id, s->repo, re->ref);
5991 if (err)
5992 return err;
5993 err = got_object_id_str(&id_str, id);
5994 if (err) {
5995 free(id);
5996 return err;
5998 if (asprintf(&line, "%s: %s",
5999 got_ref_get_name(re->ref), id_str) == -1) {
6000 err = got_error_from_errno("asprintf");
6001 free(id);
6002 free(id_str);
6003 return err;
6005 free(id);
6006 free(id_str);
6007 } else {
6008 line = strdup(got_ref_get_name(re->ref));
6009 if (line == NULL)
6010 return got_error_from_errno("strdup");
6013 err = format_line(&wline, &width, line, view->ncols, 0);
6014 if (err) {
6015 free(line);
6016 return err;
6018 if (n == s->selected) {
6019 if (view->focussed)
6020 wstandout(view->window);
6021 s->selected_entry = re;
6023 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6024 if (tc)
6025 wattr_on(view->window,
6026 COLOR_PAIR(tc->colorpair), NULL);
6027 waddwstr(view->window, wline);
6028 if (tc)
6029 wattr_off(view->window,
6030 COLOR_PAIR(tc->colorpair), NULL);
6031 if (width < view->ncols - 1)
6032 waddch(view->window, '\n');
6033 if (n == s->selected && view->focussed)
6034 wstandend(view->window);
6035 free(line);
6036 free(wline);
6037 wline = NULL;
6038 n++;
6039 s->ndisplayed++;
6040 s->last_displayed_entry = re;
6042 limit--;
6043 re = TAILQ_NEXT(re, entry);
6046 view_vborder(view);
6047 return err;
6050 static const struct got_error *
6051 browse_ref_tree(struct tog_view **new_view, int begin_x,
6052 struct tog_reflist_entry *re, struct got_repository *repo)
6054 const struct got_error *err = NULL;
6055 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6056 struct got_tree_object *tree = NULL;
6057 struct tog_view *tree_view;
6059 *new_view = NULL;
6061 err = resolve_reflist_entry(&commit_id, re, repo);
6062 if (err) {
6063 if (err->code != GOT_ERR_OBJ_TYPE)
6064 return err;
6065 else
6066 return NULL;
6069 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6070 if (err)
6071 goto done;
6073 err = got_object_open_as_tree(&tree, repo, tree_id);
6074 if (err)
6075 goto done;
6077 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6078 if (tree_view == NULL) {
6079 err = got_error_from_errno("view_open");
6080 goto done;
6083 err = open_tree_view(tree_view, tree, commit_id,
6084 got_ref_get_name(re->ref), repo);
6085 if (err)
6086 goto done;
6088 *new_view = tree_view;
6089 done:
6090 free(commit_id);
6091 free(tree_id);
6092 if (err) {
6093 if (tree)
6094 got_object_tree_close(tree);
6096 return err;
6098 static const struct got_error *
6099 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6101 const struct got_error *err = NULL;
6102 struct tog_ref_view_state *s = &view->state.ref;
6103 struct tog_view *log_view, *tree_view;
6104 int begin_x = 0;
6106 switch (ch) {
6107 case 'i':
6108 s->show_ids = !s->show_ids;
6109 break;
6110 case KEY_ENTER:
6111 case '\r':
6112 if (!s->selected_entry)
6113 break;
6114 if (view_is_parent_view(view))
6115 begin_x = view_split_begin_x(view->begin_x);
6116 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6117 s->repo);
6118 view->focussed = 0;
6119 log_view->focussed = 1;
6120 if (view_is_parent_view(view)) {
6121 err = view_close_child(view);
6122 if (err)
6123 return err;
6124 view_set_child(view, log_view);
6125 view->focus_child = 1;
6126 } else
6127 *new_view = log_view;
6128 break;
6129 case 't':
6130 if (!s->selected_entry)
6131 break;
6132 if (view_is_parent_view(view))
6133 begin_x = view_split_begin_x(view->begin_x);
6134 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6135 s->repo);
6136 if (err || tree_view == NULL)
6137 break;
6138 view->focussed = 0;
6139 tree_view->focussed = 1;
6140 if (view_is_parent_view(view)) {
6141 err = view_close_child(view);
6142 if (err)
6143 return err;
6144 view_set_child(view, tree_view);
6145 view->focus_child = 1;
6146 } else
6147 *new_view = tree_view;
6148 break;
6149 case 'k':
6150 case KEY_UP:
6151 if (s->selected > 0) {
6152 s->selected--;
6153 break;
6155 ref_scroll_up(s, 1);
6156 break;
6157 case KEY_PPAGE:
6158 case CTRL('b'):
6159 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6160 s->selected = 0;
6161 ref_scroll_up(s, MAX(0, view->nlines - 1));
6162 break;
6163 case 'j':
6164 case KEY_DOWN:
6165 if (s->selected < s->ndisplayed - 1) {
6166 s->selected++;
6167 break;
6169 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6170 /* can't scroll any further */
6171 break;
6172 ref_scroll_down(s, 1);
6173 break;
6174 case KEY_NPAGE:
6175 case CTRL('f'):
6176 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6177 /* can't scroll any further; move cursor down */
6178 if (s->selected < s->ndisplayed - 1)
6179 s->selected = s->ndisplayed - 1;
6180 break;
6182 ref_scroll_down(s, view->nlines - 1);
6183 break;
6184 case CTRL('l'):
6185 tog_free_refs();
6186 err = tog_load_refs(s->repo);
6187 if (err)
6188 break;
6189 ref_view_free_refs(s);
6190 err = ref_view_load_refs(s);
6191 break;
6192 case KEY_RESIZE:
6193 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6194 s->selected = view->nlines - 2;
6195 break;
6196 default:
6197 break;
6200 return err;
6203 __dead static void
6204 usage_ref(void)
6206 endwin();
6207 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6208 getprogname());
6209 exit(1);
6212 static const struct got_error *
6213 cmd_ref(int argc, char *argv[])
6215 const struct got_error *error;
6216 struct got_repository *repo = NULL;
6217 struct got_worktree *worktree = NULL;
6218 char *cwd = NULL, *repo_path = NULL;
6219 int ch;
6220 struct tog_view *view;
6222 while ((ch = getopt(argc, argv, "r:")) != -1) {
6223 switch (ch) {
6224 case 'r':
6225 repo_path = realpath(optarg, NULL);
6226 if (repo_path == NULL)
6227 return got_error_from_errno2("realpath",
6228 optarg);
6229 break;
6230 default:
6231 usage_ref();
6232 /* NOTREACHED */
6236 argc -= optind;
6237 argv += optind;
6239 if (argc > 1)
6240 usage_ref();
6242 if (repo_path == NULL) {
6243 cwd = getcwd(NULL, 0);
6244 if (cwd == NULL)
6245 return got_error_from_errno("getcwd");
6246 error = got_worktree_open(&worktree, cwd);
6247 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6248 goto done;
6249 if (worktree)
6250 repo_path =
6251 strdup(got_worktree_get_repo_path(worktree));
6252 else
6253 repo_path = strdup(cwd);
6254 if (repo_path == NULL) {
6255 error = got_error_from_errno("strdup");
6256 goto done;
6260 error = got_repo_open(&repo, repo_path, NULL);
6261 if (error != NULL)
6262 goto done;
6264 init_curses();
6266 error = apply_unveil(got_repo_get_path(repo), NULL);
6267 if (error)
6268 goto done;
6270 error = tog_load_refs(repo);
6271 if (error)
6272 goto done;
6274 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6275 if (view == NULL) {
6276 error = got_error_from_errno("view_open");
6277 goto done;
6280 error = open_ref_view(view, repo);
6281 if (error)
6282 goto done;
6284 if (worktree) {
6285 /* Release work tree lock. */
6286 got_worktree_close(worktree);
6287 worktree = NULL;
6289 error = view_loop(view);
6290 done:
6291 free(repo_path);
6292 free(cwd);
6293 if (repo)
6294 got_repo_close(repo);
6295 tog_free_refs();
6296 return error;
6299 static void
6300 list_commands(FILE *fp)
6302 size_t i;
6304 fprintf(fp, "commands:");
6305 for (i = 0; i < nitems(tog_commands); i++) {
6306 struct tog_cmd *cmd = &tog_commands[i];
6307 fprintf(fp, " %s", cmd->name);
6309 fputc('\n', fp);
6312 __dead static void
6313 usage(int hflag, int status)
6315 FILE *fp = (status == 0) ? stdout : stderr;
6317 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6318 getprogname());
6319 if (hflag) {
6320 fprintf(fp, "lazy usage: %s path\n", getprogname());
6321 list_commands(fp);
6323 exit(status);
6326 static char **
6327 make_argv(int argc, ...)
6329 va_list ap;
6330 char **argv;
6331 int i;
6333 va_start(ap, argc);
6335 argv = calloc(argc, sizeof(char *));
6336 if (argv == NULL)
6337 err(1, "calloc");
6338 for (i = 0; i < argc; i++) {
6339 argv[i] = strdup(va_arg(ap, char *));
6340 if (argv[i] == NULL)
6341 err(1, "strdup");
6344 va_end(ap);
6345 return argv;
6349 * Try to convert 'tog path' into a 'tog log path' command.
6350 * The user could simply have mistyped the command rather than knowingly
6351 * provided a path. So check whether argv[0] can in fact be resolved
6352 * to a path in the HEAD commit and print a special error if not.
6353 * This hack is for mpi@ <3
6355 static const struct got_error *
6356 tog_log_with_path(int argc, char *argv[])
6358 const struct got_error *error = NULL;
6359 struct tog_cmd *cmd = NULL;
6360 struct got_repository *repo = NULL;
6361 struct got_worktree *worktree = NULL;
6362 struct got_object_id *commit_id = NULL, *id = NULL;
6363 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 char *commit_id_str = NULL, **cmd_argv = NULL;
6366 cwd = getcwd(NULL, 0);
6367 if (cwd == NULL)
6368 return got_error_from_errno("getcwd");
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6372 goto done;
6374 if (worktree)
6375 repo_path = strdup(got_worktree_get_repo_path(worktree));
6376 else
6377 repo_path = strdup(cwd);
6378 if (repo_path == NULL) {
6379 error = got_error_from_errno("strdup");
6380 goto done;
6383 error = got_repo_open(&repo, repo_path, NULL);
6384 if (error != NULL)
6385 goto done;
6387 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6388 repo, worktree);
6389 if (error)
6390 goto done;
6392 error = tog_load_refs(repo);
6393 if (error)
6394 goto done;
6395 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6396 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6397 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6398 if (error)
6399 goto done;
6401 if (worktree) {
6402 got_worktree_close(worktree);
6403 worktree = NULL;
6406 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6407 if (error) {
6408 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6409 goto done;
6410 fprintf(stderr, "%s: '%s' is no known command or path\n",
6411 getprogname(), argv[0]);
6412 usage(1, 1);
6413 /* not reached */
6416 got_repo_close(repo);
6417 repo = NULL;
6419 error = got_object_id_str(&commit_id_str, commit_id);
6420 if (error)
6421 goto done;
6423 cmd = &tog_commands[0]; /* log */
6424 argc = 4;
6425 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6426 error = cmd->cmd_main(argc, cmd_argv);
6427 done:
6428 if (repo)
6429 got_repo_close(repo);
6430 if (worktree)
6431 got_worktree_close(worktree);
6432 free(id);
6433 free(commit_id_str);
6434 free(commit_id);
6435 free(cwd);
6436 free(repo_path);
6437 free(in_repo_path);
6438 if (cmd_argv) {
6439 int i;
6440 for (i = 0; i < argc; i++)
6441 free(cmd_argv[i]);
6442 free(cmd_argv);
6444 tog_free_refs();
6445 return error;
6448 int
6449 main(int argc, char *argv[])
6451 const struct got_error *error = NULL;
6452 struct tog_cmd *cmd = NULL;
6453 int ch, hflag = 0, Vflag = 0;
6454 char **cmd_argv = NULL;
6455 static struct option longopts[] = {
6456 { "version", no_argument, NULL, 'V' },
6457 { NULL, 0, NULL, 0}
6460 setlocale(LC_CTYPE, "");
6462 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6463 switch (ch) {
6464 case 'h':
6465 hflag = 1;
6466 break;
6467 case 'V':
6468 Vflag = 1;
6469 break;
6470 default:
6471 usage(hflag, 1);
6472 /* NOTREACHED */
6476 argc -= optind;
6477 argv += optind;
6478 optind = 1;
6479 optreset = 1;
6481 if (Vflag) {
6482 got_version_print_str();
6483 return 0;
6486 #ifndef PROFILE
6487 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6488 NULL) == -1)
6489 err(1, "pledge");
6490 #endif
6492 if (argc == 0) {
6493 if (hflag)
6494 usage(hflag, 0);
6495 /* Build an argument vector which runs a default command. */
6496 cmd = &tog_commands[0];
6497 argc = 1;
6498 cmd_argv = make_argv(argc, cmd->name);
6499 } else {
6500 size_t i;
6502 /* Did the user specify a command? */
6503 for (i = 0; i < nitems(tog_commands); i++) {
6504 if (strncmp(tog_commands[i].name, argv[0],
6505 strlen(argv[0])) == 0) {
6506 cmd = &tog_commands[i];
6507 break;
6512 if (cmd == NULL) {
6513 if (argc != 1)
6514 usage(0, 1);
6515 /* No command specified; try log with a path */
6516 error = tog_log_with_path(argc, argv);
6517 } else {
6518 if (hflag)
6519 cmd->cmd_usage();
6520 else
6521 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6524 endwin();
6525 putchar('\n');
6526 if (cmd_argv) {
6527 int i;
6528 for (i = 0; i < argc; i++)
6529 free(cmd_argv[i]);
6530 free(cmd_argv);
6533 if (error && error->code != GOT_ERR_CANCELLED)
6534 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6535 return 0;