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 <util.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 SIMPLEQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
135 int regerr = 0;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
138 return NULL;
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
143 if (tc == NULL)
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 if (regerr) {
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
151 sizeof(regerr_msg));
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 regerr_msg);
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 free(tc);
156 return err;
158 tc->colorpair = idx;
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 return NULL;
163 static void
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 regfree(&tc->regex);
172 free(tc);
176 struct tog_color *
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
183 return tc;
186 return NULL;
189 static int
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 return COLOR_CYAN;
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 return COLOR_YELLOW;
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 return COLOR_GREEN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 return COLOR_GREEN;
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 return COLOR_GREEN;
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 return COLOR_CYAN;
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 return COLOR_YELLOW;
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 return COLOR_GREEN;
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 return COLOR_YELLOW;
221 return -1;
224 static int
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
229 if (val == NULL)
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
233 return COLOR_BLACK;
234 if (strcasecmp(val, "red") == 0)
235 return COLOR_RED;
236 if (strcasecmp(val, "green") == 0)
237 return COLOR_GREEN;
238 if (strcasecmp(val, "yellow") == 0)
239 return COLOR_YELLOW;
240 if (strcasecmp(val, "blue") == 0)
241 return COLOR_BLUE;
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
245 return COLOR_CYAN;
246 if (strcasecmp(val, "white") == 0)
247 return COLOR_WHITE;
248 if (strcasecmp(val, "default") == 0)
249 return -1;
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
258 FILE *f;
259 int first_displayed_line;
260 int last_displayed_line;
261 int eof;
262 int diff_context;
263 int ignore_whitespace;
264 int force_text_diff;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
268 size_t nlines;
269 off_t *line_offsets;
270 int matched_line;
271 int selected_line;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
275 };
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
282 int commits_needed;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
288 int log_complete;
289 sig_atomic_t *quit;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
292 int *searching;
293 int *search_next_done;
294 regex_t *regex;
295 };
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
302 int selected;
303 char *in_repo_path;
304 char *head_ref_name;
305 int log_branches;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
309 sig_atomic_t quit;
310 pthread_t thread;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
315 };
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
334 int nlines;
336 struct tog_view *view;
337 struct got_object_id *commit_id;
338 int *quit;
339 };
341 struct tog_blame_thread_args {
342 const char *path;
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
345 int *complete;
346 got_cancel_cb cancel_cb;
347 void *cancel_arg;
348 };
350 struct tog_blame {
351 FILE *f;
352 off_t filesize;
353 struct tog_blame_line *lines;
354 int nlines;
355 off_t *line_offsets;
356 pthread_t thread;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
359 const char *path;
360 };
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
365 int selected_line;
366 int blame_complete;
367 int eof;
368 int done;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
371 char *path;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
375 int matched_line;
376 struct tog_colors colors;
377 };
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
384 int selected;
385 };
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
390 char *tree_label;
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 char *head_ref_name;
400 struct got_repository *repo;
401 struct got_tree_entry *matched_entry;
402 struct tog_colors colors;
403 };
405 struct tog_reflist_entry {
406 TAILQ_ENTRY(tog_reflist_entry) entry;
407 struct got_reference *ref;
408 int idx;
409 };
411 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
413 struct tog_ref_view_state {
414 struct got_reflist_head simplerefs; /* SIMPLEQ */
415 struct tog_reflist_head refs; /* TAILQ */
416 struct tog_reflist_entry *first_displayed_entry;
417 struct tog_reflist_entry *last_displayed_entry;
418 struct tog_reflist_entry *selected_entry;
419 int nrefs, ndisplayed, selected, show_ids;
420 struct got_repository *repo;
421 struct tog_reflist_entry *matched_entry;
422 struct tog_colors colors;
423 };
425 /*
426 * We implement two types of views: parent views and child views.
428 * The 'Tab' key switches focus between a parent view and its child view.
429 * Child views are shown side-by-side to their parent view, provided
430 * there is enough screen estate.
432 * When a new view is opened from within a parent view, this new view
433 * becomes a child view of the parent view, replacing any existing child.
435 * When a new view is opened from within a child view, this new view
436 * becomes a parent view which will obscure the views below until the
437 * user quits the new parent view by typing 'q'.
439 * This list of views contains parent views only.
440 * Child views are only pointed to by their parent view.
441 */
442 TAILQ_HEAD(tog_view_list_head, tog_view);
444 struct tog_view {
445 TAILQ_ENTRY(tog_view) entry;
446 WINDOW *window;
447 PANEL *panel;
448 int nlines, ncols, begin_y, begin_x;
449 int lines, cols; /* copies of LINES and COLS */
450 int focussed; /* Only set on one parent or child view at a time. */
451 int dying;
452 struct tog_view *parent;
453 struct tog_view *child;
455 /*
456 * This flag is initially set on parent views when a new child view
457 * is created. It gets toggled when the 'Tab' key switches focus
458 * between parent and child.
459 * The flag indicates whether focus should be passed on to our child
460 * view if this parent view gets picked for focus after another parent
461 * view was closed. This prevents child views from losing focus in such
462 * situations.
463 */
464 int focus_child;
466 /* type-specific state */
467 enum tog_view_type type;
468 union {
469 struct tog_diff_view_state diff;
470 struct tog_log_view_state log;
471 struct tog_blame_view_state blame;
472 struct tog_tree_view_state tree;
473 struct tog_ref_view_state ref;
474 } state;
476 const struct got_error *(*show)(struct tog_view *);
477 const struct got_error *(*input)(struct tog_view **,
478 struct tog_view *, int);
479 const struct got_error *(*close)(struct tog_view *);
481 const struct got_error *(*search_start)(struct tog_view *);
482 const struct got_error *(*search_next)(struct tog_view *);
483 int searching;
484 #define TOG_SEARCH_FORWARD 1
485 #define TOG_SEARCH_BACKWARD 2
486 int search_next_done;
487 #define TOG_SEARCH_HAVE_MORE 1
488 #define TOG_SEARCH_NO_MORE 2
489 #define TOG_SEARCH_HAVE_NONE 3
490 regex_t regex;
491 regmatch_t regmatch;
492 };
494 static const struct got_error *open_diff_view(struct tog_view *,
495 struct got_object_id *, struct got_object_id *,
496 const char *, const char *, int, int, int, struct tog_view *,
497 struct got_repository *);
498 static const struct got_error *show_diff_view(struct tog_view *);
499 static const struct got_error *input_diff_view(struct tog_view **,
500 struct tog_view *, int);
501 static const struct got_error* close_diff_view(struct tog_view *);
502 static const struct got_error *search_start_diff_view(struct tog_view *);
503 static const struct got_error *search_next_diff_view(struct tog_view *);
505 static const struct got_error *open_log_view(struct tog_view *,
506 struct got_object_id *, struct got_repository *,
507 const char *, const char *, int);
508 static const struct got_error * show_log_view(struct tog_view *);
509 static const struct got_error *input_log_view(struct tog_view **,
510 struct tog_view *, int);
511 static const struct got_error *close_log_view(struct tog_view *);
512 static const struct got_error *search_start_log_view(struct tog_view *);
513 static const struct got_error *search_next_log_view(struct tog_view *);
515 static const struct got_error *open_blame_view(struct tog_view *, char *,
516 struct got_object_id *, struct got_repository *);
517 static const struct got_error *show_blame_view(struct tog_view *);
518 static const struct got_error *input_blame_view(struct tog_view **,
519 struct tog_view *, int);
520 static const struct got_error *close_blame_view(struct tog_view *);
521 static const struct got_error *search_start_blame_view(struct tog_view *);
522 static const struct got_error *search_next_blame_view(struct tog_view *);
524 static const struct got_error *open_tree_view(struct tog_view *,
525 struct got_tree_object *, struct got_object_id *, const char *,
526 struct got_repository *);
527 static const struct got_error *show_tree_view(struct tog_view *);
528 static const struct got_error *input_tree_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error *close_tree_view(struct tog_view *);
531 static const struct got_error *search_start_tree_view(struct tog_view *);
532 static const struct got_error *search_next_tree_view(struct tog_view *);
534 static const struct got_error *open_ref_view(struct tog_view *,
535 struct got_repository *);
536 static const struct got_error *show_ref_view(struct tog_view *);
537 static const struct got_error *input_ref_view(struct tog_view **,
538 struct tog_view *, int);
539 static const struct got_error *close_ref_view(struct tog_view *);
540 static const struct got_error *search_start_ref_view(struct tog_view *);
541 static const struct got_error *search_next_ref_view(struct tog_view *);
543 static volatile sig_atomic_t tog_sigwinch_received;
544 static volatile sig_atomic_t tog_sigpipe_received;
545 static volatile sig_atomic_t tog_sigcont_received;
547 static void
548 tog_sigwinch(int signo)
550 tog_sigwinch_received = 1;
553 static void
554 tog_sigpipe(int signo)
556 tog_sigpipe_received = 1;
559 static void
560 tog_sigcont(int signo)
562 tog_sigcont_received = 1;
565 static const struct got_error *
566 view_close(struct tog_view *view)
568 const struct got_error *err = NULL;
570 if (view->child) {
571 view_close(view->child);
572 view->child = NULL;
574 if (view->close)
575 err = view->close(view);
576 if (view->panel)
577 del_panel(view->panel);
578 if (view->window)
579 delwin(view->window);
580 free(view);
581 return err;
584 static struct tog_view *
585 view_open(int nlines, int ncols, int begin_y, int begin_x,
586 enum tog_view_type type)
588 struct tog_view *view = calloc(1, sizeof(*view));
590 if (view == NULL)
591 return NULL;
593 view->type = type;
594 view->lines = LINES;
595 view->cols = COLS;
596 view->nlines = nlines ? nlines : LINES - begin_y;
597 view->ncols = ncols ? ncols : COLS - begin_x;
598 view->begin_y = begin_y;
599 view->begin_x = begin_x;
600 view->window = newwin(nlines, ncols, begin_y, begin_x);
601 if (view->window == NULL) {
602 view_close(view);
603 return NULL;
605 view->panel = new_panel(view->window);
606 if (view->panel == NULL ||
607 set_panel_userptr(view->panel, view) != OK) {
608 view_close(view);
609 return NULL;
612 keypad(view->window, TRUE);
613 return view;
616 static int
617 view_split_begin_x(int begin_x)
619 if (begin_x > 0 || COLS < 120)
620 return 0;
621 return (COLS - MAX(COLS / 2, 80));
624 static const struct got_error *view_resize(struct tog_view *);
626 static const struct got_error *
627 view_splitscreen(struct tog_view *view)
629 const struct got_error *err = NULL;
631 view->begin_y = 0;
632 view->begin_x = view_split_begin_x(0);
633 view->nlines = LINES;
634 view->ncols = COLS - view->begin_x;
635 view->lines = LINES;
636 view->cols = COLS;
637 err = view_resize(view);
638 if (err)
639 return err;
641 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
642 return got_error_from_errno("mvwin");
644 return NULL;
647 static const struct got_error *
648 view_fullscreen(struct tog_view *view)
650 const struct got_error *err = NULL;
652 view->begin_x = 0;
653 view->begin_y = 0;
654 view->nlines = LINES;
655 view->ncols = COLS;
656 view->lines = LINES;
657 view->cols = COLS;
658 err = view_resize(view);
659 if (err)
660 return err;
662 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
663 return got_error_from_errno("mvwin");
665 return NULL;
668 static int
669 view_is_parent_view(struct tog_view *view)
671 return view->parent == NULL;
674 static const struct got_error *
675 view_resize(struct tog_view *view)
677 int nlines, ncols;
679 if (view->lines > LINES)
680 nlines = view->nlines - (view->lines - LINES);
681 else
682 nlines = view->nlines + (LINES - view->lines);
684 if (view->cols > COLS)
685 ncols = view->ncols - (view->cols - COLS);
686 else
687 ncols = view->ncols + (COLS - view->cols);
689 if (wresize(view->window, nlines, ncols) == ERR)
690 return got_error_from_errno("wresize");
691 if (replace_panel(view->panel, view->window) == ERR)
692 return got_error_from_errno("replace_panel");
693 wclear(view->window);
695 view->nlines = nlines;
696 view->ncols = ncols;
697 view->lines = LINES;
698 view->cols = COLS;
700 if (view->child) {
701 view->child->begin_x = view_split_begin_x(view->begin_x);
702 if (view->child->begin_x == 0) {
703 view_fullscreen(view->child);
704 if (view->child->focussed)
705 show_panel(view->child->panel);
706 else
707 show_panel(view->panel);
708 } else {
709 view_splitscreen(view->child);
710 show_panel(view->child->panel);
714 return NULL;
717 static const struct got_error *
718 view_close_child(struct tog_view *view)
720 const struct got_error *err = NULL;
722 if (view->child == NULL)
723 return NULL;
725 err = view_close(view->child);
726 view->child = NULL;
727 return err;
730 static void
731 view_set_child(struct tog_view *view, struct tog_view *child)
733 view->child = child;
734 child->parent = view;
737 static int
738 view_is_splitscreen(struct tog_view *view)
740 return view->begin_x > 0;
743 static void
744 tog_resizeterm(void)
746 int cols, lines;
747 struct winsize size;
749 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
750 cols = 80; /* Default */
751 lines = 24;
752 } else {
753 cols = size.ws_col;
754 lines = size.ws_row;
756 resize_term(lines, cols);
759 static const struct got_error *
760 view_search_start(struct tog_view *view)
762 const struct got_error *err = NULL;
763 char pattern[1024];
764 int ret;
766 if (view->nlines < 1)
767 return NULL;
769 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
770 wclrtoeol(view->window);
772 nocbreak();
773 echo();
774 ret = wgetnstr(view->window, pattern, sizeof(pattern));
775 cbreak();
776 noecho();
777 if (ret == ERR)
778 return NULL;
780 if (view->searching) {
781 regfree(&view->regex);
782 view->searching = 0;
785 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
786 err = view->search_start(view);
787 if (err) {
788 regfree(&view->regex);
789 return err;
791 view->searching = TOG_SEARCH_FORWARD;
792 view->search_next_done = 0;
793 view->search_next(view);
796 return NULL;
799 static const struct got_error *
800 view_input(struct tog_view **new, int *done, struct tog_view *view,
801 struct tog_view_list_head *views)
803 const struct got_error *err = NULL;
804 struct tog_view *v;
805 int ch, errcode;
807 *new = NULL;
809 /* Clear "no matches" indicator. */
810 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
811 view->search_next_done == TOG_SEARCH_HAVE_NONE)
812 view->search_next_done = TOG_SEARCH_HAVE_MORE;
814 if (view->searching && !view->search_next_done) {
815 errcode = pthread_mutex_unlock(&tog_mutex);
816 if (errcode)
817 return got_error_set_errno(errcode,
818 "pthread_mutex_unlock");
819 pthread_yield();
820 errcode = pthread_mutex_lock(&tog_mutex);
821 if (errcode)
822 return got_error_set_errno(errcode,
823 "pthread_mutex_lock");
824 view->search_next(view);
825 return NULL;
828 nodelay(stdscr, FALSE);
829 /* Allow threads to make progress while we are waiting for input. */
830 errcode = pthread_mutex_unlock(&tog_mutex);
831 if (errcode)
832 return got_error_set_errno(errcode, "pthread_mutex_unlock");
833 ch = wgetch(view->window);
834 errcode = pthread_mutex_lock(&tog_mutex);
835 if (errcode)
836 return got_error_set_errno(errcode, "pthread_mutex_lock");
837 nodelay(stdscr, TRUE);
839 if (tog_sigwinch_received || tog_sigcont_received) {
840 tog_resizeterm();
841 tog_sigwinch_received = 0;
842 tog_sigcont_received = 0;
843 TAILQ_FOREACH(v, views, entry) {
844 err = view_resize(v);
845 if (err)
846 return err;
847 err = v->input(new, v, KEY_RESIZE);
848 if (err)
849 return err;
850 if (v->child) {
851 err = view_resize(v->child);
852 if (err)
853 return err;
854 err = v->child->input(new, v->child,
855 KEY_RESIZE);
856 if (err)
857 return err;
862 switch (ch) {
863 case ERR:
864 break;
865 case '\t':
866 if (view->child) {
867 view->focussed = 0;
868 view->child->focussed = 1;
869 view->focus_child = 1;
870 } else if (view->parent) {
871 view->focussed = 0;
872 view->parent->focussed = 1;
873 view->parent->focus_child = 0;
875 break;
876 case 'q':
877 err = view->input(new, view, ch);
878 view->dying = 1;
879 break;
880 case 'Q':
881 *done = 1;
882 break;
883 case 'f':
884 if (view_is_parent_view(view)) {
885 if (view->child == NULL)
886 break;
887 if (view_is_splitscreen(view->child)) {
888 view->focussed = 0;
889 view->child->focussed = 1;
890 err = view_fullscreen(view->child);
891 } else
892 err = view_splitscreen(view->child);
893 if (err)
894 break;
895 err = view->child->input(new, view->child,
896 KEY_RESIZE);
897 } else {
898 if (view_is_splitscreen(view)) {
899 view->parent->focussed = 0;
900 view->focussed = 1;
901 err = view_fullscreen(view);
902 } else {
903 err = view_splitscreen(view);
905 if (err)
906 break;
907 err = view->input(new, view, KEY_RESIZE);
909 break;
910 case KEY_RESIZE:
911 break;
912 case '/':
913 if (view->search_start)
914 view_search_start(view);
915 else
916 err = view->input(new, view, ch);
917 break;
918 case 'N':
919 case 'n':
920 if (view->search_next) {
921 view->searching = (ch == 'n' ?
922 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
923 view->search_next_done = 0;
924 view->search_next(view);
925 } else
926 err = view->input(new, view, ch);
927 break;
928 default:
929 err = view->input(new, view, ch);
930 break;
933 return err;
936 void
937 view_vborder(struct tog_view *view)
939 PANEL *panel;
940 struct tog_view *view_above;
942 if (view->parent)
943 return view_vborder(view->parent);
945 panel = panel_above(view->panel);
946 if (panel == NULL)
947 return;
949 view_above = panel_userptr(panel);
950 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
951 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
954 int
955 view_needs_focus_indication(struct tog_view *view)
957 if (view_is_parent_view(view)) {
958 if (view->child == NULL || view->child->focussed)
959 return 0;
960 if (!view_is_splitscreen(view->child))
961 return 0;
962 } else if (!view_is_splitscreen(view))
963 return 0;
965 return view->focussed;
968 static const struct got_error *
969 view_loop(struct tog_view *view)
971 const struct got_error *err = NULL;
972 struct tog_view_list_head views;
973 struct tog_view *new_view;
974 int fast_refresh = 10;
975 int done = 0, errcode;
977 errcode = pthread_mutex_lock(&tog_mutex);
978 if (errcode)
979 return got_error_set_errno(errcode, "pthread_mutex_lock");
981 TAILQ_INIT(&views);
982 TAILQ_INSERT_HEAD(&views, view, entry);
984 view->focussed = 1;
985 err = view->show(view);
986 if (err)
987 return err;
988 update_panels();
989 doupdate();
990 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
991 /* Refresh fast during initialization, then become slower. */
992 if (fast_refresh && fast_refresh-- == 0)
993 halfdelay(10); /* switch to once per second */
995 err = view_input(&new_view, &done, view, &views);
996 if (err)
997 break;
998 if (view->dying) {
999 struct tog_view *v, *prev = NULL;
1001 if (view_is_parent_view(view))
1002 prev = TAILQ_PREV(view, tog_view_list_head,
1003 entry);
1004 else if (view->parent)
1005 prev = view->parent;
1007 if (view->parent) {
1008 view->parent->child = NULL;
1009 view->parent->focus_child = 0;
1010 } else
1011 TAILQ_REMOVE(&views, view, entry);
1013 err = view_close(view);
1014 if (err)
1015 goto done;
1017 view = NULL;
1018 TAILQ_FOREACH(v, &views, entry) {
1019 if (v->focussed)
1020 break;
1022 if (view == NULL && new_view == NULL) {
1023 /* No view has focus. Try to pick one. */
1024 if (prev)
1025 view = prev;
1026 else if (!TAILQ_EMPTY(&views)) {
1027 view = TAILQ_LAST(&views,
1028 tog_view_list_head);
1030 if (view) {
1031 if (view->focus_child) {
1032 view->child->focussed = 1;
1033 view = view->child;
1034 } else
1035 view->focussed = 1;
1039 if (new_view) {
1040 struct tog_view *v, *t;
1041 /* Only allow one parent view per type. */
1042 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1043 if (v->type != new_view->type)
1044 continue;
1045 TAILQ_REMOVE(&views, v, entry);
1046 err = view_close(v);
1047 if (err)
1048 goto done;
1049 break;
1051 TAILQ_INSERT_TAIL(&views, new_view, entry);
1052 view = new_view;
1054 if (view) {
1055 if (view_is_parent_view(view)) {
1056 if (view->child && view->child->focussed)
1057 view = view->child;
1058 } else {
1059 if (view->parent && view->parent->focussed)
1060 view = view->parent;
1062 show_panel(view->panel);
1063 if (view->child && view_is_splitscreen(view->child))
1064 show_panel(view->child->panel);
1065 if (view->parent && view_is_splitscreen(view)) {
1066 err = view->parent->show(view->parent);
1067 if (err)
1068 goto done;
1070 err = view->show(view);
1071 if (err)
1072 goto done;
1073 if (view->child) {
1074 err = view->child->show(view->child);
1075 if (err)
1076 goto done;
1078 update_panels();
1079 doupdate();
1082 done:
1083 while (!TAILQ_EMPTY(&views)) {
1084 view = TAILQ_FIRST(&views);
1085 TAILQ_REMOVE(&views, view, entry);
1086 view_close(view);
1089 errcode = pthread_mutex_unlock(&tog_mutex);
1090 if (errcode && err == NULL)
1091 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1093 return err;
1096 __dead static void
1097 usage_log(void)
1099 endwin();
1100 fprintf(stderr,
1101 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1102 getprogname());
1103 exit(1);
1106 /* Create newly allocated wide-character string equivalent to a byte string. */
1107 static const struct got_error *
1108 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1110 char *vis = NULL;
1111 const struct got_error *err = NULL;
1113 *ws = NULL;
1114 *wlen = mbstowcs(NULL, s, 0);
1115 if (*wlen == (size_t)-1) {
1116 int vislen;
1117 if (errno != EILSEQ)
1118 return got_error_from_errno("mbstowcs");
1120 /* byte string invalid in current encoding; try to "fix" it */
1121 err = got_mbsavis(&vis, &vislen, s);
1122 if (err)
1123 return err;
1124 *wlen = mbstowcs(NULL, vis, 0);
1125 if (*wlen == (size_t)-1) {
1126 err = got_error_from_errno("mbstowcs"); /* give up */
1127 goto done;
1131 *ws = calloc(*wlen + 1, sizeof(**ws));
1132 if (*ws == NULL) {
1133 err = got_error_from_errno("calloc");
1134 goto done;
1137 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1138 err = got_error_from_errno("mbstowcs");
1139 done:
1140 free(vis);
1141 if (err) {
1142 free(*ws);
1143 *ws = NULL;
1144 *wlen = 0;
1146 return err;
1149 /* Format a line for display, ensuring that it won't overflow a width limit. */
1150 static const struct got_error *
1151 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1152 int col_tab_align)
1154 const struct got_error *err = NULL;
1155 int cols = 0;
1156 wchar_t *wline = NULL;
1157 size_t wlen;
1158 int i;
1160 *wlinep = NULL;
1161 *widthp = 0;
1163 err = mbs2ws(&wline, &wlen, line);
1164 if (err)
1165 return err;
1167 i = 0;
1168 while (i < wlen) {
1169 int width = wcwidth(wline[i]);
1171 if (width == 0) {
1172 i++;
1173 continue;
1176 if (width == 1 || width == 2) {
1177 if (cols + width > wlimit)
1178 break;
1179 cols += width;
1180 i++;
1181 } else if (width == -1) {
1182 if (wline[i] == L'\t') {
1183 width = TABSIZE -
1184 ((cols + col_tab_align) % TABSIZE);
1185 if (cols + width > wlimit)
1186 break;
1187 cols += width;
1189 i++;
1190 } else {
1191 err = got_error_from_errno("wcwidth");
1192 goto done;
1195 wline[i] = L'\0';
1196 if (widthp)
1197 *widthp = cols;
1198 done:
1199 if (err)
1200 free(wline);
1201 else
1202 *wlinep = wline;
1203 return err;
1206 static const struct got_error*
1207 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1208 struct got_object_id *id, struct got_repository *repo)
1210 static const struct got_error *err = NULL;
1211 struct got_reflist_entry *re;
1212 char *s;
1213 const char *name;
1215 *refs_str = NULL;
1217 SIMPLEQ_FOREACH(re, refs, entry) {
1218 struct got_tag_object *tag = NULL;
1219 struct got_object_id *ref_id;
1220 int cmp;
1222 name = got_ref_get_name(re->ref);
1223 if (strcmp(name, GOT_REF_HEAD) == 0)
1224 continue;
1225 if (strncmp(name, "refs/", 5) == 0)
1226 name += 5;
1227 if (strncmp(name, "got/", 4) == 0)
1228 continue;
1229 if (strncmp(name, "heads/", 6) == 0)
1230 name += 6;
1231 if (strncmp(name, "remotes/", 8) == 0) {
1232 name += 8;
1233 s = strstr(name, "/" GOT_REF_HEAD);
1234 if (s != NULL && s[strlen(s)] == '\0')
1235 continue;
1237 err = got_ref_resolve(&ref_id, repo, re->ref);
1238 if (err)
1239 break;
1240 if (strncmp(name, "tags/", 5) == 0) {
1241 err = got_object_open_as_tag(&tag, repo, ref_id);
1242 if (err) {
1243 if (err->code != GOT_ERR_OBJ_TYPE) {
1244 free(ref_id);
1245 break;
1247 /* Ref points at something other than a tag. */
1248 err = NULL;
1249 tag = NULL;
1252 cmp = got_object_id_cmp(tag ?
1253 got_object_tag_get_object_id(tag) : ref_id, id);
1254 free(ref_id);
1255 if (tag)
1256 got_object_tag_close(tag);
1257 if (cmp != 0)
1258 continue;
1259 s = *refs_str;
1260 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1261 s ? ", " : "", name) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 free(s);
1264 *refs_str = NULL;
1265 break;
1267 free(s);
1270 return err;
1273 static const struct got_error *
1274 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1275 int col_tab_align)
1277 char *smallerthan, *at;
1279 smallerthan = strchr(author, '<');
1280 if (smallerthan && smallerthan[1] != '\0')
1281 author = smallerthan + 1;
1282 at = strchr(author, '@');
1283 if (at)
1284 *at = '\0';
1285 return format_line(wauthor, author_width, author, limit, col_tab_align);
1288 static const struct got_error *
1289 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1290 struct got_object_id *id, const size_t date_display_cols,
1291 int author_display_cols)
1293 struct tog_log_view_state *s = &view->state.log;
1294 const struct got_error *err = NULL;
1295 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1296 char *logmsg0 = NULL, *logmsg = NULL;
1297 char *author = NULL;
1298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1299 int author_width, logmsg_width;
1300 char *newline, *line = NULL;
1301 int col, limit;
1302 const int avail = view->ncols;
1303 struct tm tm;
1304 time_t committer_time;
1305 struct tog_color *tc;
1307 committer_time = got_object_commit_get_committer_time(commit);
1308 if (localtime_r(&committer_time, &tm) == NULL)
1309 return got_error_from_errno("localtime_r");
1310 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1311 >= sizeof(datebuf))
1312 return got_error(GOT_ERR_NO_SPACE);
1314 if (avail <= date_display_cols)
1315 limit = MIN(sizeof(datebuf) - 1, avail);
1316 else
1317 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1318 tc = get_color(&s->colors, TOG_COLOR_DATE);
1319 if (tc)
1320 wattr_on(view->window,
1321 COLOR_PAIR(tc->colorpair), NULL);
1322 waddnstr(view->window, datebuf, limit);
1323 if (tc)
1324 wattr_off(view->window,
1325 COLOR_PAIR(tc->colorpair), NULL);
1326 col = limit;
1327 if (col > avail)
1328 goto done;
1330 if (avail >= 120) {
1331 char *id_str;
1332 err = got_object_id_str(&id_str, id);
1333 if (err)
1334 goto done;
1335 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1336 if (tc)
1337 wattr_on(view->window,
1338 COLOR_PAIR(tc->colorpair), NULL);
1339 wprintw(view->window, "%.8s ", id_str);
1340 if (tc)
1341 wattr_off(view->window,
1342 COLOR_PAIR(tc->colorpair), NULL);
1343 free(id_str);
1344 col += 9;
1345 if (col > avail)
1346 goto done;
1349 author = strdup(got_object_commit_get_author(commit));
1350 if (author == NULL) {
1351 err = got_error_from_errno("strdup");
1352 goto done;
1354 err = format_author(&wauthor, &author_width, author, avail - col, col);
1355 if (err)
1356 goto done;
1357 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1358 if (tc)
1359 wattr_on(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 waddwstr(view->window, wauthor);
1362 if (tc)
1363 wattr_off(view->window,
1364 COLOR_PAIR(tc->colorpair), NULL);
1365 col += author_width;
1366 while (col < avail && author_width < author_display_cols + 2) {
1367 waddch(view->window, ' ');
1368 col++;
1369 author_width++;
1371 if (col > avail)
1372 goto done;
1374 err = got_object_commit_get_logmsg(&logmsg0, commit);
1375 if (err)
1376 goto done;
1377 logmsg = logmsg0;
1378 while (*logmsg == '\n')
1379 logmsg++;
1380 newline = strchr(logmsg, '\n');
1381 if (newline)
1382 *newline = '\0';
1383 limit = avail - col;
1384 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1385 if (err)
1386 goto done;
1387 waddwstr(view->window, wlogmsg);
1388 col += logmsg_width;
1389 while (col < avail) {
1390 waddch(view->window, ' ');
1391 col++;
1393 done:
1394 free(logmsg0);
1395 free(wlogmsg);
1396 free(author);
1397 free(wauthor);
1398 free(line);
1399 return err;
1402 static struct commit_queue_entry *
1403 alloc_commit_queue_entry(struct got_commit_object *commit,
1404 struct got_object_id *id)
1406 struct commit_queue_entry *entry;
1408 entry = calloc(1, sizeof(*entry));
1409 if (entry == NULL)
1410 return NULL;
1412 entry->id = id;
1413 entry->commit = commit;
1414 return entry;
1417 static void
1418 pop_commit(struct commit_queue *commits)
1420 struct commit_queue_entry *entry;
1422 entry = TAILQ_FIRST(&commits->head);
1423 TAILQ_REMOVE(&commits->head, entry, entry);
1424 got_object_commit_close(entry->commit);
1425 commits->ncommits--;
1426 /* Don't free entry->id! It is owned by the commit graph. */
1427 free(entry);
1430 static void
1431 free_commits(struct commit_queue *commits)
1433 while (!TAILQ_EMPTY(&commits->head))
1434 pop_commit(commits);
1437 static const struct got_error *
1438 match_commit(int *have_match, struct got_object_id *id,
1439 struct got_commit_object *commit, regex_t *regex)
1441 const struct got_error *err = NULL;
1442 regmatch_t regmatch;
1443 char *id_str = NULL, *logmsg = NULL;
1445 *have_match = 0;
1447 err = got_object_id_str(&id_str, id);
1448 if (err)
1449 return err;
1451 err = got_object_commit_get_logmsg(&logmsg, commit);
1452 if (err)
1453 goto done;
1455 if (regexec(regex, got_object_commit_get_author(commit), 1,
1456 &regmatch, 0) == 0 ||
1457 regexec(regex, got_object_commit_get_committer(commit), 1,
1458 &regmatch, 0) == 0 ||
1459 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1460 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1461 *have_match = 1;
1462 done:
1463 free(id_str);
1464 free(logmsg);
1465 return err;
1468 static const struct got_error *
1469 queue_commits(struct tog_log_thread_args *a)
1471 const struct got_error *err = NULL;
1474 * We keep all commits open throughout the lifetime of the log
1475 * view in order to avoid having to re-fetch commits from disk
1476 * while updating the display.
1478 do {
1479 struct got_object_id *id;
1480 struct got_commit_object *commit;
1481 struct commit_queue_entry *entry;
1482 int errcode;
1484 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1485 NULL, NULL);
1486 if (err || id == NULL)
1487 break;
1489 err = got_object_open_as_commit(&commit, a->repo, id);
1490 if (err)
1491 break;
1492 entry = alloc_commit_queue_entry(commit, id);
1493 if (entry == NULL) {
1494 err = got_error_from_errno("alloc_commit_queue_entry");
1495 break;
1498 errcode = pthread_mutex_lock(&tog_mutex);
1499 if (errcode) {
1500 err = got_error_set_errno(errcode,
1501 "pthread_mutex_lock");
1502 break;
1505 entry->idx = a->commits->ncommits;
1506 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1507 a->commits->ncommits++;
1509 if (*a->searching == TOG_SEARCH_FORWARD &&
1510 !*a->search_next_done) {
1511 int have_match;
1512 err = match_commit(&have_match, id, commit, a->regex);
1513 if (err)
1514 break;
1515 if (have_match)
1516 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1519 errcode = pthread_mutex_unlock(&tog_mutex);
1520 if (errcode && err == NULL)
1521 err = got_error_set_errno(errcode,
1522 "pthread_mutex_unlock");
1523 if (err)
1524 break;
1525 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1527 return err;
1530 static void
1531 select_commit(struct tog_log_view_state *s)
1533 struct commit_queue_entry *entry;
1534 int ncommits = 0;
1536 entry = s->first_displayed_entry;
1537 while (entry) {
1538 if (ncommits == s->selected) {
1539 s->selected_entry = entry;
1540 break;
1542 entry = TAILQ_NEXT(entry, entry);
1543 ncommits++;
1547 static const struct got_error *
1548 draw_commits(struct tog_view *view)
1550 const struct got_error *err = NULL;
1551 struct tog_log_view_state *s = &view->state.log;
1552 struct commit_queue_entry *entry = s->selected_entry;
1553 const int limit = view->nlines;
1554 int width;
1555 int ncommits, author_cols = 4;
1556 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1557 char *refs_str = NULL;
1558 wchar_t *wline;
1559 struct tog_color *tc;
1560 static const size_t date_display_cols = 12;
1562 if (s->selected_entry &&
1563 !(view->searching && view->search_next_done == 0)) {
1564 err = got_object_id_str(&id_str, s->selected_entry->id);
1565 if (err)
1566 return err;
1567 err = build_refs_str(&refs_str, &s->refs,
1568 s->selected_entry->id, s->repo);
1569 if (err)
1570 goto done;
1573 if (s->thread_args.commits_needed == 0)
1574 halfdelay(10); /* disable fast refresh */
1576 if (s->thread_args.commits_needed > 0) {
1577 if (asprintf(&ncommits_str, " [%d/%d] %s",
1578 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1579 (view->searching && !view->search_next_done) ?
1580 "searching..." : "loading...") == -1) {
1581 err = got_error_from_errno("asprintf");
1582 goto done;
1584 } else {
1585 const char *search_str = NULL;
1587 if (view->searching) {
1588 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1589 search_str = "no more matches";
1590 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1591 search_str = "no matches found";
1592 else if (!view->search_next_done)
1593 search_str = "searching...";
1596 if (asprintf(&ncommits_str, " [%d/%d] %s",
1597 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1598 search_str ? search_str :
1599 (refs_str ? refs_str : "")) == -1) {
1600 err = got_error_from_errno("asprintf");
1601 goto done;
1605 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1606 if (asprintf(&header, "commit %s %s%s",
1607 id_str ? id_str : "........................................",
1608 s->in_repo_path, ncommits_str) == -1) {
1609 err = got_error_from_errno("asprintf");
1610 header = NULL;
1611 goto done;
1613 } else if (asprintf(&header, "commit %s%s",
1614 id_str ? id_str : "........................................",
1615 ncommits_str) == -1) {
1616 err = got_error_from_errno("asprintf");
1617 header = NULL;
1618 goto done;
1620 err = format_line(&wline, &width, header, view->ncols, 0);
1621 if (err)
1622 goto done;
1624 werase(view->window);
1626 if (view_needs_focus_indication(view))
1627 wstandout(view->window);
1628 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1629 if (tc)
1630 wattr_on(view->window,
1631 COLOR_PAIR(tc->colorpair), NULL);
1632 waddwstr(view->window, wline);
1633 if (tc)
1634 wattr_off(view->window,
1635 COLOR_PAIR(tc->colorpair), NULL);
1636 while (width < view->ncols) {
1637 waddch(view->window, ' ');
1638 width++;
1640 if (view_needs_focus_indication(view))
1641 wstandend(view->window);
1642 free(wline);
1643 if (limit <= 1)
1644 goto done;
1646 /* Grow author column size if necessary. */
1647 entry = s->first_displayed_entry;
1648 ncommits = 0;
1649 while (entry) {
1650 char *author;
1651 wchar_t *wauthor;
1652 int width;
1653 if (ncommits >= limit - 1)
1654 break;
1655 author = strdup(got_object_commit_get_author(entry->commit));
1656 if (author == NULL) {
1657 err = got_error_from_errno("strdup");
1658 goto done;
1660 err = format_author(&wauthor, &width, author, COLS,
1661 date_display_cols);
1662 if (author_cols < width)
1663 author_cols = width;
1664 free(wauthor);
1665 free(author);
1666 ncommits++;
1667 entry = TAILQ_NEXT(entry, entry);
1670 entry = s->first_displayed_entry;
1671 s->last_displayed_entry = s->first_displayed_entry;
1672 ncommits = 0;
1673 while (entry) {
1674 if (ncommits >= limit - 1)
1675 break;
1676 if (ncommits == s->selected)
1677 wstandout(view->window);
1678 err = draw_commit(view, entry->commit, entry->id,
1679 date_display_cols, author_cols);
1680 if (ncommits == s->selected)
1681 wstandend(view->window);
1682 if (err)
1683 goto done;
1684 ncommits++;
1685 s->last_displayed_entry = entry;
1686 entry = TAILQ_NEXT(entry, entry);
1689 view_vborder(view);
1690 done:
1691 free(id_str);
1692 free(refs_str);
1693 free(ncommits_str);
1694 free(header);
1695 return err;
1698 static void
1699 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1701 struct commit_queue_entry *entry;
1702 int nscrolled = 0;
1704 entry = TAILQ_FIRST(&s->commits.head);
1705 if (s->first_displayed_entry == entry)
1706 return;
1708 entry = s->first_displayed_entry;
1709 while (entry && nscrolled < maxscroll) {
1710 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1711 if (entry) {
1712 s->first_displayed_entry = entry;
1713 nscrolled++;
1718 static const struct got_error *
1719 trigger_log_thread(struct tog_view *view, int wait)
1721 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1722 int errcode;
1724 halfdelay(1); /* fast refresh while loading commits */
1726 while (ta->commits_needed > 0) {
1727 if (ta->log_complete)
1728 break;
1730 /* Wake the log thread. */
1731 errcode = pthread_cond_signal(&ta->need_commits);
1732 if (errcode)
1733 return got_error_set_errno(errcode,
1734 "pthread_cond_signal");
1737 * The mutex will be released while the view loop waits
1738 * in wgetch(), at which time the log thread will run.
1740 if (!wait)
1741 break;
1743 /* Display progress update in log view. */
1744 show_log_view(view);
1745 update_panels();
1746 doupdate();
1748 /* Wait right here while next commit is being loaded. */
1749 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1750 if (errcode)
1751 return got_error_set_errno(errcode,
1752 "pthread_cond_wait");
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1756 update_panels();
1757 doupdate();
1760 return NULL;
1763 static const struct got_error *
1764 log_scroll_down(struct tog_view *view, int maxscroll)
1766 struct tog_log_view_state *s = &view->state.log;
1767 const struct got_error *err = NULL;
1768 struct commit_queue_entry *pentry;
1769 int nscrolled = 0, ncommits_needed;
1771 if (s->last_displayed_entry == NULL)
1772 return NULL;
1774 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1775 if (s->commits.ncommits < ncommits_needed &&
1776 !s->thread_args.log_complete) {
1778 * Ask the log thread for required amount of commits.
1780 s->thread_args.commits_needed += maxscroll;
1781 err = trigger_log_thread(view, 1);
1782 if (err)
1783 return err;
1786 do {
1787 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1788 if (pentry == NULL)
1789 break;
1791 s->last_displayed_entry = pentry;
1793 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1794 if (pentry == NULL)
1795 break;
1796 s->first_displayed_entry = pentry;
1797 } while (++nscrolled < maxscroll);
1799 return err;
1802 static const struct got_error *
1803 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1804 struct got_commit_object *commit, struct got_object_id *commit_id,
1805 struct tog_view *log_view, struct got_repository *repo)
1807 const struct got_error *err;
1808 struct got_object_qid *parent_id;
1809 struct tog_view *diff_view;
1811 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1812 if (diff_view == NULL)
1813 return got_error_from_errno("view_open");
1815 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1816 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1817 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1818 if (err == NULL)
1819 *new_view = diff_view;
1820 return err;
1823 static const struct got_error *
1824 tree_view_visit_subtree(struct tog_tree_view_state *s,
1825 struct got_tree_object *subtree)
1827 struct tog_parent_tree *parent;
1829 parent = calloc(1, sizeof(*parent));
1830 if (parent == NULL)
1831 return got_error_from_errno("calloc");
1833 parent->tree = s->tree;
1834 parent->first_displayed_entry = s->first_displayed_entry;
1835 parent->selected_entry = s->selected_entry;
1836 parent->selected = s->selected;
1837 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1838 s->tree = subtree;
1839 s->selected = 0;
1840 s->first_displayed_entry = NULL;
1841 return NULL;
1844 static const struct got_error *
1845 tree_view_walk_path(struct tog_tree_view_state *s,
1846 struct got_object_id *commit_id, const char *path)
1848 const struct got_error *err = NULL;
1849 struct got_tree_object *tree = NULL;
1850 const char *p;
1851 char *slash, *subpath = NULL;
1853 /* Walk the path and open corresponding tree objects. */
1854 p = path;
1855 while (*p) {
1856 struct got_tree_entry *te;
1857 struct got_object_id *tree_id;
1858 char *te_name;
1860 while (p[0] == '/')
1861 p++;
1863 /* Ensure the correct subtree entry is selected. */
1864 slash = strchr(p, '/');
1865 if (slash == NULL)
1866 te_name = strdup(p);
1867 else
1868 te_name = strndup(p, slash - p);
1869 if (te_name == NULL) {
1870 err = got_error_from_errno("strndup");
1871 break;
1873 te = got_object_tree_find_entry(s->tree, te_name);
1874 if (te == NULL) {
1875 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1876 free(te_name);
1877 break;
1879 free(te_name);
1880 s->first_displayed_entry = s->selected_entry = te;
1882 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1883 break; /* jump to this file's entry */
1885 slash = strchr(p, '/');
1886 if (slash)
1887 subpath = strndup(path, slash - path);
1888 else
1889 subpath = strdup(path);
1890 if (subpath == NULL) {
1891 err = got_error_from_errno("strdup");
1892 break;
1895 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1896 subpath);
1897 if (err)
1898 break;
1900 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1901 free(tree_id);
1902 if (err)
1903 break;
1905 err = tree_view_visit_subtree(s, tree);
1906 if (err) {
1907 got_object_tree_close(tree);
1908 break;
1910 if (slash == NULL)
1911 break;
1912 free(subpath);
1913 subpath = NULL;
1914 p = slash;
1917 free(subpath);
1918 return err;
1921 static const struct got_error *
1922 browse_commit_tree(struct tog_view **new_view, int begin_x,
1923 struct commit_queue_entry *entry, const char *path,
1924 const char *head_ref_name, struct got_repository *repo)
1926 const struct got_error *err = NULL;
1927 struct got_tree_object *tree;
1928 struct tog_tree_view_state *s;
1929 struct tog_view *tree_view;
1931 err = got_object_open_as_tree(&tree, repo,
1932 got_object_commit_get_tree_id(entry->commit));
1933 if (err)
1934 return err;
1936 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1937 if (tree_view == NULL)
1938 return got_error_from_errno("view_open");
1940 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1941 if (err) {
1942 got_object_tree_close(tree);
1943 return err;
1945 s = &tree_view->state.tree;
1947 *new_view = tree_view;
1949 if (got_path_is_root_dir(path))
1950 return NULL;
1952 return tree_view_walk_path(s, entry->id, path);
1955 static const struct got_error *
1956 block_signals_used_by_main_thread(void)
1958 sigset_t sigset;
1959 int errcode;
1961 if (sigemptyset(&sigset) == -1)
1962 return got_error_from_errno("sigemptyset");
1964 /* tog handles SIGWINCH and SIGCONT */
1965 if (sigaddset(&sigset, SIGWINCH) == -1)
1966 return got_error_from_errno("sigaddset");
1967 if (sigaddset(&sigset, SIGCONT) == -1)
1968 return got_error_from_errno("sigaddset");
1970 /* ncurses handles SIGTSTP */
1971 if (sigaddset(&sigset, SIGTSTP) == -1)
1972 return got_error_from_errno("sigaddset");
1974 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1975 if (errcode)
1976 return got_error_set_errno(errcode, "pthread_sigmask");
1978 return NULL;
1981 static void *
1982 log_thread(void *arg)
1984 const struct got_error *err = NULL;
1985 int errcode = 0;
1986 struct tog_log_thread_args *a = arg;
1987 int done = 0;
1989 err = block_signals_used_by_main_thread();
1990 if (err)
1991 return (void *)err;
1993 while (!done && !err && !tog_sigpipe_received) {
1994 err = queue_commits(a);
1995 if (err) {
1996 if (err->code != GOT_ERR_ITER_COMPLETED)
1997 return (void *)err;
1998 err = NULL;
1999 done = 1;
2000 } else if (a->commits_needed > 0)
2001 a->commits_needed--;
2003 errcode = pthread_mutex_lock(&tog_mutex);
2004 if (errcode) {
2005 err = got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2007 break;
2008 } else if (*a->quit)
2009 done = 1;
2010 else if (*a->first_displayed_entry == NULL) {
2011 *a->first_displayed_entry =
2012 TAILQ_FIRST(&a->commits->head);
2013 *a->selected_entry = *a->first_displayed_entry;
2016 errcode = pthread_cond_signal(&a->commit_loaded);
2017 if (errcode) {
2018 err = got_error_set_errno(errcode,
2019 "pthread_cond_signal");
2020 pthread_mutex_unlock(&tog_mutex);
2021 break;
2024 if (done)
2025 a->commits_needed = 0;
2026 else {
2027 if (a->commits_needed == 0) {
2028 errcode = pthread_cond_wait(&a->need_commits,
2029 &tog_mutex);
2030 if (errcode)
2031 err = got_error_set_errno(errcode,
2032 "pthread_cond_wait");
2033 if (*a->quit)
2034 done = 1;
2038 errcode = pthread_mutex_unlock(&tog_mutex);
2039 if (errcode && err == NULL)
2040 err = got_error_set_errno(errcode,
2041 "pthread_mutex_unlock");
2043 a->log_complete = 1;
2044 return (void *)err;
2047 static const struct got_error *
2048 stop_log_thread(struct tog_log_view_state *s)
2050 const struct got_error *err = NULL;
2051 int errcode;
2053 if (s->thread) {
2054 s->quit = 1;
2055 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2056 if (errcode)
2057 return got_error_set_errno(errcode,
2058 "pthread_cond_signal");
2059 errcode = pthread_mutex_unlock(&tog_mutex);
2060 if (errcode)
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_unlock");
2063 errcode = pthread_join(s->thread, (void **)&err);
2064 if (errcode)
2065 return got_error_set_errno(errcode, "pthread_join");
2066 errcode = pthread_mutex_lock(&tog_mutex);
2067 if (errcode)
2068 return got_error_set_errno(errcode,
2069 "pthread_mutex_lock");
2070 s->thread = NULL;
2073 if (s->thread_args.repo) {
2074 got_repo_close(s->thread_args.repo);
2075 s->thread_args.repo = NULL;
2078 if (s->thread_args.graph) {
2079 got_commit_graph_close(s->thread_args.graph);
2080 s->thread_args.graph = NULL;
2083 return err;
2086 static const struct got_error *
2087 close_log_view(struct tog_view *view)
2089 const struct got_error *err = NULL;
2090 struct tog_log_view_state *s = &view->state.log;
2091 int errcode;
2093 err = stop_log_thread(s);
2095 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2096 if (errcode && err == NULL)
2097 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2099 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2100 if (errcode && err == NULL)
2101 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2103 free_commits(&s->commits);
2104 free(s->in_repo_path);
2105 s->in_repo_path = NULL;
2106 free(s->start_id);
2107 s->start_id = NULL;
2108 free(s->head_ref_name);
2109 s->head_ref_name = NULL;
2110 got_ref_list_free(&s->refs);
2111 return err;
2114 static const struct got_error *
2115 search_start_log_view(struct tog_view *view)
2117 struct tog_log_view_state *s = &view->state.log;
2119 s->matched_entry = NULL;
2120 s->search_entry = NULL;
2121 return NULL;
2124 static const struct got_error *
2125 search_next_log_view(struct tog_view *view)
2127 const struct got_error *err = NULL;
2128 struct tog_log_view_state *s = &view->state.log;
2129 struct commit_queue_entry *entry;
2131 /* Display progress update in log view. */
2132 show_log_view(view);
2133 update_panels();
2134 doupdate();
2136 if (s->search_entry) {
2137 int errcode, ch;
2138 errcode = pthread_mutex_unlock(&tog_mutex);
2139 if (errcode)
2140 return got_error_set_errno(errcode,
2141 "pthread_mutex_unlock");
2142 ch = wgetch(view->window);
2143 errcode = pthread_mutex_lock(&tog_mutex);
2144 if (errcode)
2145 return got_error_set_errno(errcode,
2146 "pthread_mutex_lock");
2147 if (ch == KEY_BACKSPACE) {
2148 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2149 return NULL;
2151 if (view->searching == TOG_SEARCH_FORWARD)
2152 entry = TAILQ_NEXT(s->search_entry, entry);
2153 else
2154 entry = TAILQ_PREV(s->search_entry,
2155 commit_queue_head, entry);
2156 } else if (s->matched_entry) {
2157 if (view->searching == TOG_SEARCH_FORWARD)
2158 entry = TAILQ_NEXT(s->matched_entry, entry);
2159 else
2160 entry = TAILQ_PREV(s->matched_entry,
2161 commit_queue_head, entry);
2162 } else {
2163 if (view->searching == TOG_SEARCH_FORWARD)
2164 entry = TAILQ_FIRST(&s->commits.head);
2165 else
2166 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2169 while (1) {
2170 int have_match = 0;
2172 if (entry == NULL) {
2173 if (s->thread_args.log_complete ||
2174 view->searching == TOG_SEARCH_BACKWARD) {
2175 view->search_next_done =
2176 (s->matched_entry == NULL ?
2177 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2178 s->search_entry = NULL;
2179 return NULL;
2182 * Poke the log thread for more commits and return,
2183 * allowing the main loop to make progress. Search
2184 * will resume at s->search_entry once we come back.
2186 s->thread_args.commits_needed++;
2187 return trigger_log_thread(view, 0);
2190 err = match_commit(&have_match, entry->id, entry->commit,
2191 &view->regex);
2192 if (err)
2193 break;
2194 if (have_match) {
2195 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2196 s->matched_entry = entry;
2197 break;
2200 s->search_entry = entry;
2201 if (view->searching == TOG_SEARCH_FORWARD)
2202 entry = TAILQ_NEXT(entry, entry);
2203 else
2204 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2207 if (s->matched_entry) {
2208 int cur = s->selected_entry->idx;
2209 while (cur < s->matched_entry->idx) {
2210 err = input_log_view(NULL, view, KEY_DOWN);
2211 if (err)
2212 return err;
2213 cur++;
2215 while (cur > s->matched_entry->idx) {
2216 err = input_log_view(NULL, view, KEY_UP);
2217 if (err)
2218 return err;
2219 cur--;
2223 s->search_entry = NULL;
2225 return NULL;
2228 static const struct got_error *
2229 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2230 struct got_repository *repo, const char *head_ref_name,
2231 const char *in_repo_path, int log_branches)
2233 const struct got_error *err = NULL;
2234 struct tog_log_view_state *s = &view->state.log;
2235 struct got_repository *thread_repo = NULL;
2236 struct got_commit_graph *thread_graph = NULL;
2237 int errcode;
2239 SIMPLEQ_INIT(&s->refs);
2241 if (in_repo_path != s->in_repo_path) {
2242 free(s->in_repo_path);
2243 s->in_repo_path = strdup(in_repo_path);
2244 if (s->in_repo_path == NULL)
2245 return got_error_from_errno("strdup");
2248 /* The commit queue only contains commits being displayed. */
2249 TAILQ_INIT(&s->commits.head);
2250 s->commits.ncommits = 0;
2252 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2253 if (err)
2254 goto done;
2256 s->repo = repo;
2257 if (head_ref_name) {
2258 s->head_ref_name = strdup(head_ref_name);
2259 if (s->head_ref_name == NULL) {
2260 err = got_error_from_errno("strdup");
2261 goto done;
2264 s->start_id = got_object_id_dup(start_id);
2265 if (s->start_id == NULL) {
2266 err = got_error_from_errno("got_object_id_dup");
2267 goto done;
2269 s->log_branches = log_branches;
2271 SIMPLEQ_INIT(&s->colors);
2272 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2273 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2274 get_color_value("TOG_COLOR_COMMIT"));
2275 if (err)
2276 goto done;
2277 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2278 get_color_value("TOG_COLOR_AUTHOR"));
2279 if (err) {
2280 free_colors(&s->colors);
2281 goto done;
2283 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2284 get_color_value("TOG_COLOR_DATE"));
2285 if (err) {
2286 free_colors(&s->colors);
2287 goto done;
2291 view->show = show_log_view;
2292 view->input = input_log_view;
2293 view->close = close_log_view;
2294 view->search_start = search_start_log_view;
2295 view->search_next = search_next_log_view;
2297 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2298 if (err)
2299 goto done;
2300 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2301 !s->log_branches);
2302 if (err)
2303 goto done;
2304 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2305 s->repo, NULL, NULL);
2306 if (err)
2307 goto done;
2309 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2310 if (errcode) {
2311 err = got_error_set_errno(errcode, "pthread_cond_init");
2312 goto done;
2314 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2315 if (errcode) {
2316 err = got_error_set_errno(errcode, "pthread_cond_init");
2317 goto done;
2320 s->thread_args.commits_needed = view->nlines;
2321 s->thread_args.graph = thread_graph;
2322 s->thread_args.commits = &s->commits;
2323 s->thread_args.in_repo_path = s->in_repo_path;
2324 s->thread_args.start_id = s->start_id;
2325 s->thread_args.repo = thread_repo;
2326 s->thread_args.log_complete = 0;
2327 s->thread_args.quit = &s->quit;
2328 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2329 s->thread_args.selected_entry = &s->selected_entry;
2330 s->thread_args.searching = &view->searching;
2331 s->thread_args.search_next_done = &view->search_next_done;
2332 s->thread_args.regex = &view->regex;
2333 done:
2334 if (err)
2335 close_log_view(view);
2336 return err;
2339 static const struct got_error *
2340 show_log_view(struct tog_view *view)
2342 const struct got_error *err;
2343 struct tog_log_view_state *s = &view->state.log;
2345 if (s->thread == NULL) {
2346 int errcode = pthread_create(&s->thread, NULL, log_thread,
2347 &s->thread_args);
2348 if (errcode)
2349 return got_error_set_errno(errcode, "pthread_create");
2350 if (s->thread_args.commits_needed > 0) {
2351 err = trigger_log_thread(view, 1);
2352 if (err)
2353 return err;
2357 return draw_commits(view);
2360 static const struct got_error *
2361 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2363 const struct got_error *err = NULL;
2364 struct tog_log_view_state *s = &view->state.log;
2365 struct tog_view *diff_view = NULL, *tree_view = NULL;
2366 struct tog_view *ref_view = NULL;
2367 int begin_x = 0;
2369 switch (ch) {
2370 case 'q':
2371 s->quit = 1;
2372 break;
2373 case 'k':
2374 case KEY_UP:
2375 case '<':
2376 case ',':
2377 if (s->first_displayed_entry == NULL)
2378 break;
2379 if (s->selected > 0)
2380 s->selected--;
2381 else
2382 log_scroll_up(s, 1);
2383 select_commit(s);
2384 break;
2385 case KEY_PPAGE:
2386 case CTRL('b'):
2387 if (s->first_displayed_entry == NULL)
2388 break;
2389 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2390 s->selected = 0;
2391 else
2392 log_scroll_up(s, view->nlines - 1);
2393 select_commit(s);
2394 break;
2395 case 'j':
2396 case KEY_DOWN:
2397 case '>':
2398 case '.':
2399 if (s->first_displayed_entry == NULL)
2400 break;
2401 if (s->selected < MIN(view->nlines - 2,
2402 s->commits.ncommits - 1))
2403 s->selected++;
2404 else {
2405 err = log_scroll_down(view, 1);
2406 if (err)
2407 break;
2409 select_commit(s);
2410 break;
2411 case KEY_NPAGE:
2412 case CTRL('f'): {
2413 struct commit_queue_entry *first;
2414 first = s->first_displayed_entry;
2415 if (first == NULL)
2416 break;
2417 err = log_scroll_down(view, view->nlines - 1);
2418 if (err)
2419 break;
2420 if (first == s->first_displayed_entry &&
2421 s->selected < MIN(view->nlines - 2,
2422 s->commits.ncommits - 1)) {
2423 /* can't scroll further down */
2424 s->selected = MIN(view->nlines - 2,
2425 s->commits.ncommits - 1);
2427 select_commit(s);
2428 break;
2430 case KEY_RESIZE:
2431 if (s->selected > view->nlines - 2)
2432 s->selected = view->nlines - 2;
2433 if (s->selected > s->commits.ncommits - 1)
2434 s->selected = s->commits.ncommits - 1;
2435 select_commit(s);
2436 if (s->commits.ncommits < view->nlines - 1 &&
2437 !s->thread_args.log_complete) {
2438 s->thread_args.commits_needed += (view->nlines - 1) -
2439 s->commits.ncommits;
2440 err = trigger_log_thread(view, 1);
2442 break;
2443 case KEY_ENTER:
2444 case ' ':
2445 case '\r':
2446 if (s->selected_entry == NULL)
2447 break;
2448 if (view_is_parent_view(view))
2449 begin_x = view_split_begin_x(view->begin_x);
2450 err = open_diff_view_for_commit(&diff_view, begin_x,
2451 s->selected_entry->commit, s->selected_entry->id,
2452 view, s->repo);
2453 if (err)
2454 break;
2455 view->focussed = 0;
2456 diff_view->focussed = 1;
2457 if (view_is_parent_view(view)) {
2458 err = view_close_child(view);
2459 if (err)
2460 return err;
2461 view_set_child(view, diff_view);
2462 view->focus_child = 1;
2463 } else
2464 *new_view = diff_view;
2465 break;
2466 case 't':
2467 if (s->selected_entry == NULL)
2468 break;
2469 if (view_is_parent_view(view))
2470 begin_x = view_split_begin_x(view->begin_x);
2471 err = browse_commit_tree(&tree_view, begin_x,
2472 s->selected_entry, s->in_repo_path, s->head_ref_name,
2473 s->repo);
2474 if (err)
2475 break;
2476 view->focussed = 0;
2477 tree_view->focussed = 1;
2478 if (view_is_parent_view(view)) {
2479 err = view_close_child(view);
2480 if (err)
2481 return err;
2482 view_set_child(view, tree_view);
2483 view->focus_child = 1;
2484 } else
2485 *new_view = tree_view;
2486 break;
2487 case KEY_BACKSPACE:
2488 case CTRL('l'):
2489 case 'B':
2490 if (ch == KEY_BACKSPACE &&
2491 got_path_is_root_dir(s->in_repo_path))
2492 break;
2493 err = stop_log_thread(s);
2494 if (err)
2495 return err;
2496 if (ch == KEY_BACKSPACE) {
2497 char *parent_path;
2498 err = got_path_dirname(&parent_path, s->in_repo_path);
2499 if (err)
2500 return err;
2501 free(s->in_repo_path);
2502 s->in_repo_path = parent_path;
2503 s->thread_args.in_repo_path = s->in_repo_path;
2504 } else if (ch == CTRL('l')) {
2505 struct got_object_id *start_id;
2506 err = got_repo_match_object_id(&start_id, NULL,
2507 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2508 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2509 if (err)
2510 return err;
2511 free(s->start_id);
2512 s->start_id = start_id;
2513 s->thread_args.start_id = s->start_id;
2514 } else /* 'B' */
2515 s->log_branches = !s->log_branches;
2517 err = got_repo_open(&s->thread_args.repo,
2518 got_repo_get_path(s->repo), NULL);
2519 if (err)
2520 return err;
2521 got_ref_list_free(&s->refs);
2522 err = got_ref_list(&s->refs, s->repo, NULL,
2523 got_ref_cmp_by_name, NULL);
2524 if (err)
2525 return err;
2526 err = got_commit_graph_open(&s->thread_args.graph,
2527 s->in_repo_path, !s->log_branches);
2528 if (err)
2529 return err;
2530 err = got_commit_graph_iter_start(s->thread_args.graph,
2531 s->start_id, s->repo, NULL, NULL);
2532 if (err)
2533 return err;
2534 free_commits(&s->commits);
2535 s->first_displayed_entry = NULL;
2536 s->last_displayed_entry = NULL;
2537 s->selected_entry = NULL;
2538 s->selected = 0;
2539 s->thread_args.log_complete = 0;
2540 s->quit = 0;
2541 s->thread_args.commits_needed = view->nlines;
2542 break;
2543 case 'r':
2544 if (view_is_parent_view(view))
2545 begin_x = view_split_begin_x(view->begin_x);
2546 ref_view = view_open(view->nlines, view->ncols,
2547 view->begin_y, begin_x, TOG_VIEW_REF);
2548 if (ref_view == NULL)
2549 return got_error_from_errno("view_open");
2550 err = open_ref_view(ref_view, s->repo);
2551 if (err) {
2552 view_close(ref_view);
2553 return err;
2555 view->focussed = 0;
2556 ref_view->focussed = 1;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2559 if (err)
2560 return err;
2561 view_set_child(view, ref_view);
2562 view->focus_child = 1;
2563 } else
2564 *new_view = ref_view;
2565 break;
2566 default:
2567 break;
2570 return err;
2573 static const struct got_error *
2574 apply_unveil(const char *repo_path, const char *worktree_path)
2576 const struct got_error *error;
2578 #ifdef PROFILE
2579 if (unveil("gmon.out", "rwc") != 0)
2580 return got_error_from_errno2("unveil", "gmon.out");
2581 #endif
2582 if (repo_path && unveil(repo_path, "r") != 0)
2583 return got_error_from_errno2("unveil", repo_path);
2585 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2586 return got_error_from_errno2("unveil", worktree_path);
2588 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2589 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2591 error = got_privsep_unveil_exec_helpers();
2592 if (error != NULL)
2593 return error;
2595 if (unveil(NULL, NULL) != 0)
2596 return got_error_from_errno("unveil");
2598 return NULL;
2601 static void
2602 init_curses(void)
2604 initscr();
2605 cbreak();
2606 halfdelay(1); /* Do fast refresh while initial view is loading. */
2607 noecho();
2608 nonl();
2609 intrflush(stdscr, FALSE);
2610 keypad(stdscr, TRUE);
2611 curs_set(0);
2612 if (getenv("TOG_COLORS") != NULL) {
2613 start_color();
2614 use_default_colors();
2616 signal(SIGWINCH, tog_sigwinch);
2617 signal(SIGPIPE, tog_sigpipe);
2618 signal(SIGCONT, tog_sigcont);
2621 static const struct got_error *
2622 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2623 struct got_repository *repo, struct got_worktree *worktree)
2625 const struct got_error *err = NULL;
2627 if (argc == 0) {
2628 *in_repo_path = strdup("/");
2629 if (*in_repo_path == NULL)
2630 return got_error_from_errno("strdup");
2631 return NULL;
2634 if (worktree) {
2635 const char *prefix = got_worktree_get_path_prefix(worktree);
2636 char *p;
2638 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2639 if (err)
2640 return err;
2641 if (asprintf(in_repo_path, "%s%s%s", prefix,
2642 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2643 p) == -1) {
2644 err = got_error_from_errno("asprintf");
2645 *in_repo_path = NULL;
2647 free(p);
2648 } else
2649 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2651 return err;
2654 static const struct got_error *
2655 cmd_log(int argc, char *argv[])
2657 const struct got_error *error;
2658 struct got_repository *repo = NULL;
2659 struct got_worktree *worktree = NULL;
2660 struct got_object_id *start_id = NULL;
2661 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2662 char *start_commit = NULL, *label = NULL;
2663 struct got_reference *ref = NULL;
2664 const char *head_ref_name = NULL;
2665 int ch, log_branches = 0;
2666 struct tog_view *view;
2668 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2669 switch (ch) {
2670 case 'b':
2671 log_branches = 1;
2672 break;
2673 case 'c':
2674 start_commit = optarg;
2675 break;
2676 case 'r':
2677 repo_path = realpath(optarg, NULL);
2678 if (repo_path == NULL)
2679 return got_error_from_errno2("realpath",
2680 optarg);
2681 break;
2682 default:
2683 usage_log();
2684 /* NOTREACHED */
2688 argc -= optind;
2689 argv += optind;
2691 if (argc > 1)
2692 usage_log();
2694 cwd = getcwd(NULL, 0);
2695 if (cwd == NULL)
2696 return got_error_from_errno("getcwd");
2698 error = got_worktree_open(&worktree, cwd);
2699 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2700 goto done;
2702 if (repo_path == NULL) {
2703 if (worktree)
2704 repo_path =
2705 strdup(got_worktree_get_repo_path(worktree));
2706 else
2707 repo_path = strdup(cwd);
2709 if (repo_path == NULL) {
2710 error = got_error_from_errno("strdup");
2711 goto done;
2714 error = got_repo_open(&repo, repo_path, NULL);
2715 if (error != NULL)
2716 goto done;
2718 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2719 repo, worktree);
2720 if (error)
2721 goto done;
2723 init_curses();
2725 error = apply_unveil(got_repo_get_path(repo),
2726 worktree ? got_worktree_get_root_path(worktree) : NULL);
2727 if (error)
2728 goto done;
2730 if (start_commit == NULL) {
2731 error = got_repo_match_object_id(&start_id, &label,
2732 worktree ? got_worktree_get_head_ref_name(worktree) :
2733 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2734 if (error)
2735 goto done;
2736 head_ref_name = label;
2737 } else {
2738 error = got_ref_open(&ref, repo, start_commit, 0);
2739 if (error == NULL)
2740 head_ref_name = got_ref_get_name(ref);
2741 else if (error->code != GOT_ERR_NOT_REF)
2742 goto done;
2743 error = got_repo_match_object_id(&start_id, NULL,
2744 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 if (error)
2746 goto done;
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 if (view == NULL) {
2751 error = got_error_from_errno("view_open");
2752 goto done;
2754 error = open_log_view(view, start_id, repo, head_ref_name,
2755 in_repo_path, log_branches);
2756 if (error)
2757 goto done;
2758 if (worktree) {
2759 /* Release work tree lock. */
2760 got_worktree_close(worktree);
2761 worktree = NULL;
2763 error = view_loop(view);
2764 done:
2765 free(in_repo_path);
2766 free(repo_path);
2767 free(cwd);
2768 free(start_id);
2769 free(label);
2770 if (ref)
2771 got_ref_close(ref);
2772 if (repo)
2773 got_repo_close(repo);
2774 if (worktree)
2775 got_worktree_close(worktree);
2776 return error;
2779 __dead static void
2780 usage_diff(void)
2782 endwin();
2783 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2784 "[-w] object1 object2\n", getprogname());
2785 exit(1);
2788 static char *
2789 parse_next_line(FILE *f, size_t *len)
2791 char *line;
2792 size_t linelen;
2793 size_t lineno;
2794 const char delim[3] = { '\0', '\0', '\0'};
2796 line = fparseln(f, &linelen, &lineno, delim, 0);
2797 if (len)
2798 *len = linelen;
2799 return line;
2802 static int
2803 match_line(const char *line, regex_t *regex, size_t nmatch,
2804 regmatch_t *regmatch)
2806 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2809 struct tog_color *
2810 match_color(struct tog_colors *colors, const char *line)
2812 struct tog_color *tc = NULL;
2814 SIMPLEQ_FOREACH(tc, colors, entry) {
2815 if (match_line(line, &tc->regex, 0, NULL))
2816 return tc;
2819 return NULL;
2822 static const struct got_error *
2823 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2824 WINDOW *window, regmatch_t *regmatch)
2826 const struct got_error *err = NULL;
2827 wchar_t *wline;
2828 int width;
2829 char *s;
2831 *wtotal = 0;
2833 s = strndup(line, regmatch->rm_so);
2834 if (s == NULL)
2835 return got_error_from_errno("strndup");
2837 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2838 if (err) {
2839 free(s);
2840 return err;
2842 waddwstr(window, wline);
2843 free(wline);
2844 free(s);
2845 wlimit -= width;
2846 *wtotal += width;
2848 if (wlimit > 0) {
2849 s = strndup(line + regmatch->rm_so,
2850 regmatch->rm_eo - regmatch->rm_so);
2851 if (s == NULL) {
2852 err = got_error_from_errno("strndup");
2853 free(s);
2854 return err;
2856 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2857 if (err) {
2858 free(s);
2859 return err;
2861 wattr_on(window, A_STANDOUT, NULL);
2862 waddwstr(window, wline);
2863 wattr_off(window, A_STANDOUT, NULL);
2864 free(wline);
2865 free(s);
2866 wlimit -= width;
2867 *wtotal += width;
2870 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2871 err = format_line(&wline, &width,
2872 line + regmatch->rm_eo, wlimit, col_tab_align);
2873 if (err)
2874 return err;
2875 waddwstr(window, wline);
2876 free(wline);
2877 *wtotal += width;
2880 return NULL;
2883 static const struct got_error *
2884 draw_file(struct tog_view *view, const char *header)
2886 struct tog_diff_view_state *s = &view->state.diff;
2887 regmatch_t *regmatch = &view->regmatch;
2888 const struct got_error *err;
2889 int nprinted = 0;
2890 char *line;
2891 struct tog_color *tc;
2892 size_t len;
2893 wchar_t *wline;
2894 int width;
2895 int max_lines = view->nlines;
2896 int nlines = s->nlines;
2897 off_t line_offset;
2899 line_offset = s->line_offsets[s->first_displayed_line - 1];
2900 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2901 return got_error_from_errno("fseek");
2903 werase(view->window);
2905 if (header) {
2906 if (asprintf(&line, "[%d/%d] %s",
2907 s->first_displayed_line - 1 + s->selected_line, nlines,
2908 header) == -1)
2909 return got_error_from_errno("asprintf");
2910 err = format_line(&wline, &width, line, view->ncols, 0);
2911 free(line);
2912 if (err)
2913 return err;
2915 if (view_needs_focus_indication(view))
2916 wstandout(view->window);
2917 waddwstr(view->window, wline);
2918 free(wline);
2919 wline = NULL;
2920 if (view_needs_focus_indication(view))
2921 wstandend(view->window);
2922 if (width <= view->ncols - 1)
2923 waddch(view->window, '\n');
2925 if (max_lines <= 1)
2926 return NULL;
2927 max_lines--;
2930 s->eof = 0;
2931 while (max_lines > 0 && nprinted < max_lines) {
2932 line = parse_next_line(s->f, &len);
2933 if (line == NULL) {
2934 s->eof = 1;
2935 break;
2938 tc = match_color(&s->colors, line);
2939 if (tc)
2940 wattr_on(view->window,
2941 COLOR_PAIR(tc->colorpair), NULL);
2942 if (s->first_displayed_line + nprinted == s->matched_line &&
2943 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2944 err = add_matched_line(&width, line, view->ncols, 0,
2945 view->window, regmatch);
2946 if (err) {
2947 free(line);
2948 return err;
2950 } else {
2951 err = format_line(&wline, &width, line, view->ncols, 0);
2952 if (err) {
2953 free(line);
2954 return err;
2956 waddwstr(view->window, wline);
2957 free(wline);
2958 wline = NULL;
2960 if (tc)
2961 wattr_off(view->window,
2962 COLOR_PAIR(tc->colorpair), NULL);
2963 if (width <= view->ncols - 1)
2964 waddch(view->window, '\n');
2965 nprinted++;
2966 free(line);
2968 if (nprinted >= 1)
2969 s->last_displayed_line = s->first_displayed_line +
2970 (nprinted - 1);
2971 else
2972 s->last_displayed_line = s->first_displayed_line;
2974 view_vborder(view);
2976 if (s->eof) {
2977 while (nprinted < view->nlines) {
2978 waddch(view->window, '\n');
2979 nprinted++;
2982 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2983 if (err) {
2984 return err;
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2989 free(wline);
2990 wline = NULL;
2991 wstandend(view->window);
2994 return NULL;
2997 static char *
2998 get_datestr(time_t *time, char *datebuf)
3000 struct tm mytm, *tm;
3001 char *p, *s;
3003 tm = gmtime_r(time, &mytm);
3004 if (tm == NULL)
3005 return NULL;
3006 s = asctime_r(tm, datebuf);
3007 if (s == NULL)
3008 return NULL;
3009 p = strchr(s, '\n');
3010 if (p)
3011 *p = '\0';
3012 return s;
3015 static const struct got_error *
3016 get_changed_paths(struct got_pathlist_head *paths,
3017 struct got_commit_object *commit, struct got_repository *repo)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3022 struct got_object_qid *qid;
3024 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3025 if (qid != NULL) {
3026 struct got_commit_object *pcommit;
3027 err = got_object_open_as_commit(&pcommit, repo,
3028 qid->id);
3029 if (err)
3030 return err;
3032 tree_id1 = got_object_commit_get_tree_id(pcommit);
3033 got_object_commit_close(pcommit);
3037 if (tree_id1) {
3038 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3039 if (err)
3040 goto done;
3043 tree_id2 = got_object_commit_get_tree_id(commit);
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3045 if (err)
3046 goto done;
3048 err = got_diff_tree(tree1, tree2, "", "", repo,
3049 got_diff_tree_collect_changed_paths, paths, 0);
3050 done:
3051 if (tree1)
3052 got_object_tree_close(tree1);
3053 if (tree2)
3054 got_object_tree_close(tree2);
3055 return err;
3058 static const struct got_error *
3059 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3061 off_t *p;
3063 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3064 if (p == NULL)
3065 return got_error_from_errno("reallocarray");
3066 *line_offsets = p;
3067 (*line_offsets)[*nlines] = off;
3068 (*nlines)++;
3069 return NULL;
3072 static const struct got_error *
3073 write_commit_info(off_t **line_offsets, size_t *nlines,
3074 struct got_object_id *commit_id, struct got_reflist_head *refs,
3075 struct got_repository *repo, FILE *outfile)
3077 const struct got_error *err = NULL;
3078 char datebuf[26], *datestr;
3079 struct got_commit_object *commit;
3080 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3081 time_t committer_time;
3082 const char *author, *committer;
3083 char *refs_str = NULL;
3084 struct got_pathlist_head changed_paths;
3085 struct got_pathlist_entry *pe;
3086 off_t outoff = 0;
3087 int n;
3089 TAILQ_INIT(&changed_paths);
3091 if (refs) {
3092 err = build_refs_str(&refs_str, refs, commit_id, repo);
3093 if (err)
3094 return err;
3097 err = got_object_open_as_commit(&commit, repo, commit_id);
3098 if (err)
3099 return err;
3101 err = got_object_id_str(&id_str, commit_id);
3102 if (err) {
3103 err = got_error_from_errno("got_object_id_str");
3104 goto done;
3107 err = add_line_offset(line_offsets, nlines, 0);
3108 if (err)
3109 goto done;
3111 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3112 refs_str ? refs_str : "", refs_str ? ")" : "");
3113 if (n < 0) {
3114 err = got_error_from_errno("fprintf");
3115 goto done;
3117 outoff += n;
3118 err = add_line_offset(line_offsets, nlines, outoff);
3119 if (err)
3120 goto done;
3122 n = fprintf(outfile, "from: %s\n",
3123 got_object_commit_get_author(commit));
3124 if (n < 0) {
3125 err = got_error_from_errno("fprintf");
3126 goto done;
3128 outoff += n;
3129 err = add_line_offset(line_offsets, nlines, outoff);
3130 if (err)
3131 goto done;
3133 committer_time = got_object_commit_get_committer_time(commit);
3134 datestr = get_datestr(&committer_time, datebuf);
3135 if (datestr) {
3136 n = fprintf(outfile, "date: %s UTC\n", datestr);
3137 if (n < 0) {
3138 err = got_error_from_errno("fprintf");
3139 goto done;
3141 outoff += n;
3142 err = add_line_offset(line_offsets, nlines, outoff);
3143 if (err)
3144 goto done;
3146 author = got_object_commit_get_author(commit);
3147 committer = got_object_commit_get_committer(commit);
3148 if (strcmp(author, committer) != 0) {
3149 n = fprintf(outfile, "via: %s\n", committer);
3150 if (n < 0) {
3151 err = got_error_from_errno("fprintf");
3152 goto done;
3154 outoff += n;
3155 err = add_line_offset(line_offsets, nlines, outoff);
3156 if (err)
3157 goto done;
3159 err = got_object_commit_get_logmsg(&logmsg, commit);
3160 if (err)
3161 goto done;
3162 s = logmsg;
3163 while ((line = strsep(&s, "\n")) != NULL) {
3164 n = fprintf(outfile, "%s\n", line);
3165 if (n < 0) {
3166 err = got_error_from_errno("fprintf");
3167 goto done;
3169 outoff += n;
3170 err = add_line_offset(line_offsets, nlines, outoff);
3171 if (err)
3172 goto done;
3175 err = get_changed_paths(&changed_paths, commit, repo);
3176 if (err)
3177 goto done;
3178 TAILQ_FOREACH(pe, &changed_paths, entry) {
3179 struct got_diff_changed_path *cp = pe->data;
3180 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
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;
3189 free((char *)pe->path);
3190 free(pe->data);
3193 fputc('\n', outfile);
3194 outoff++;
3195 err = add_line_offset(line_offsets, nlines, outoff);
3196 done:
3197 got_pathlist_free(&changed_paths);
3198 free(id_str);
3199 free(logmsg);
3200 free(refs_str);
3201 got_object_commit_close(commit);
3202 if (err) {
3203 free(*line_offsets);
3204 *line_offsets = NULL;
3205 *nlines = 0;
3207 return err;
3210 static const struct got_error *
3211 create_diff(struct tog_diff_view_state *s)
3213 const struct got_error *err = NULL;
3214 FILE *f = NULL;
3215 int obj_type;
3217 free(s->line_offsets);
3218 s->line_offsets = malloc(sizeof(off_t));
3219 if (s->line_offsets == NULL)
3220 return got_error_from_errno("malloc");
3221 s->nlines = 0;
3223 f = got_opentemp();
3224 if (f == NULL) {
3225 err = got_error_from_errno("got_opentemp");
3226 goto done;
3228 if (s->f && fclose(s->f) != 0) {
3229 err = got_error_from_errno("fclose");
3230 goto done;
3232 s->f = f;
3234 if (s->id1)
3235 err = got_object_get_type(&obj_type, s->repo, s->id1);
3236 else
3237 err = got_object_get_type(&obj_type, s->repo, s->id2);
3238 if (err)
3239 goto done;
3241 switch (obj_type) {
3242 case GOT_OBJ_TYPE_BLOB:
3243 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3244 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3245 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3246 break;
3247 case GOT_OBJ_TYPE_TREE:
3248 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, "", "", s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3251 break;
3252 case GOT_OBJ_TYPE_COMMIT: {
3253 const struct got_object_id_queue *parent_ids;
3254 struct got_object_qid *pid;
3255 struct got_commit_object *commit2;
3257 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3258 if (err)
3259 goto done;
3260 /* Show commit info if we're diffing to a parent/root commit. */
3261 if (s->id1 == NULL) {
3262 err = write_commit_info(&s->line_offsets, &s->nlines,
3263 s->id2, &s->refs, s->repo, s->f);
3264 if (err)
3265 goto done;
3266 } else {
3267 parent_ids = got_object_commit_get_parent_ids(commit2);
3268 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3269 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3270 err = write_commit_info(
3271 &s->line_offsets, &s->nlines,
3272 s->id2, &s->refs, s->repo, s->f);
3273 if (err)
3274 goto done;
3275 break;
3279 got_object_commit_close(commit2);
3281 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3283 s->force_text_diff, s->repo, s->f);
3284 break;
3286 default:
3287 err = got_error(GOT_ERR_OBJ_TYPE);
3288 break;
3290 if (err)
3291 goto done;
3292 done:
3293 if (s->f && fflush(s->f) != 0 && err == NULL)
3294 err = got_error_from_errno("fflush");
3295 return err;
3298 static void
3299 diff_view_indicate_progress(struct tog_view *view)
3301 mvwaddstr(view->window, 0, 0, "diffing...");
3302 update_panels();
3303 doupdate();
3306 static const struct got_error *
3307 search_start_diff_view(struct tog_view *view)
3309 struct tog_diff_view_state *s = &view->state.diff;
3311 s->matched_line = 0;
3312 return NULL;
3315 static const struct got_error *
3316 search_next_diff_view(struct tog_view *view)
3318 struct tog_diff_view_state *s = &view->state.diff;
3319 int lineno;
3321 if (!view->searching) {
3322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3323 return NULL;
3326 if (s->matched_line) {
3327 if (view->searching == TOG_SEARCH_FORWARD)
3328 lineno = s->matched_line + 1;
3329 else
3330 lineno = s->matched_line - 1;
3331 } else {
3332 if (view->searching == TOG_SEARCH_FORWARD)
3333 lineno = 1;
3334 else
3335 lineno = s->nlines;
3338 while (1) {
3339 char *line = NULL;
3340 off_t offset;
3341 size_t len;
3343 if (lineno <= 0 || lineno > s->nlines) {
3344 if (s->matched_line == 0) {
3345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3346 free(line);
3347 break;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3351 lineno = 1;
3352 else
3353 lineno = s->nlines;
3356 offset = s->line_offsets[lineno - 1];
3357 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3358 free(line);
3359 return got_error_from_errno("fseeko");
3361 free(line);
3362 line = parse_next_line(s->f, &len);
3363 if (line &&
3364 match_line(line, &view->regex, 1, &view->regmatch)) {
3365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3366 s->matched_line = lineno;
3367 free(line);
3368 break;
3370 free(line);
3371 if (view->searching == TOG_SEARCH_FORWARD)
3372 lineno++;
3373 else
3374 lineno--;
3377 if (s->matched_line) {
3378 s->first_displayed_line = s->matched_line;
3379 s->selected_line = 1;
3382 return NULL;
3385 static const struct got_error *
3386 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3387 struct got_object_id *id2, const char *label1, const char *label2,
3388 int diff_context, int ignore_whitespace, int force_text_diff,
3389 struct tog_view *log_view, struct got_repository *repo)
3391 const struct got_error *err;
3392 struct tog_diff_view_state *s = &view->state.diff;
3394 SIMPLEQ_INIT(&s->refs);
3396 if (id1 != NULL && id2 != NULL) {
3397 int type1, type2;
3398 err = got_object_get_type(&type1, repo, id1);
3399 if (err)
3400 return err;
3401 err = got_object_get_type(&type2, repo, id2);
3402 if (err)
3403 return err;
3405 if (type1 != type2)
3406 return got_error(GOT_ERR_OBJ_TYPE);
3408 s->first_displayed_line = 1;
3409 s->last_displayed_line = view->nlines;
3410 s->selected_line = 1;
3411 s->repo = repo;
3412 s->id1 = id1;
3413 s->id2 = id2;
3414 s->label1 = label1;
3415 s->label2 = label2;
3417 if (id1) {
3418 s->id1 = got_object_id_dup(id1);
3419 if (s->id1 == NULL)
3420 return got_error_from_errno("got_object_id_dup");
3421 } else
3422 s->id1 = NULL;
3424 s->id2 = got_object_id_dup(id2);
3425 if (s->id2 == NULL) {
3426 free(s->id1);
3427 s->id1 = NULL;
3428 return got_error_from_errno("got_object_id_dup");
3430 s->f = NULL;
3431 s->first_displayed_line = 1;
3432 s->last_displayed_line = view->nlines;
3433 s->diff_context = diff_context;
3434 s->ignore_whitespace = ignore_whitespace;
3435 s->force_text_diff = force_text_diff;
3436 s->log_view = log_view;
3437 s->repo = repo;
3439 SIMPLEQ_INIT(&s->colors);
3440 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3441 err = add_color(&s->colors,
3442 "^-", TOG_COLOR_DIFF_MINUS,
3443 get_color_value("TOG_COLOR_DIFF_MINUS"));
3444 if (err)
3445 return err;
3446 err = add_color(&s->colors, "^\\+",
3447 TOG_COLOR_DIFF_PLUS,
3448 get_color_value("TOG_COLOR_DIFF_PLUS"));
3449 if (err) {
3450 free_colors(&s->colors);
3451 return err;
3453 err = add_color(&s->colors,
3454 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3455 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3456 if (err) {
3457 free_colors(&s->colors);
3458 return err;
3461 err = add_color(&s->colors,
3462 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3463 TOG_COLOR_DIFF_META,
3464 get_color_value("TOG_COLOR_DIFF_META"));
3465 if (err) {
3466 free_colors(&s->colors);
3467 return err;
3470 err = add_color(&s->colors,
3471 "^(from|via): ", TOG_COLOR_AUTHOR,
3472 get_color_value("TOG_COLOR_AUTHOR"));
3473 if (err) {
3474 free_colors(&s->colors);
3475 return err;
3478 err = add_color(&s->colors,
3479 "^date: ", TOG_COLOR_DATE,
3480 get_color_value("TOG_COLOR_DATE"));
3481 if (err) {
3482 free_colors(&s->colors);
3483 return err;
3487 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3488 if (err) {
3489 free(s->id1);
3490 s->id1 = NULL;
3491 free(s->id2);
3492 s->id2 = NULL;
3493 free_colors(&s->colors);
3494 return err;
3497 if (log_view && view_is_splitscreen(view))
3498 show_log_view(log_view); /* draw vborder */
3499 diff_view_indicate_progress(view);
3501 s->line_offsets = NULL;
3502 s->nlines = 0;
3503 err = create_diff(s);
3504 if (err) {
3505 free(s->id1);
3506 s->id1 = NULL;
3507 free(s->id2);
3508 s->id2 = NULL;
3509 free_colors(&s->colors);
3510 got_ref_list_free(&s->refs);
3511 return err;
3514 view->show = show_diff_view;
3515 view->input = input_diff_view;
3516 view->close = close_diff_view;
3517 view->search_start = search_start_diff_view;
3518 view->search_next = search_next_diff_view;
3520 return NULL;
3523 static const struct got_error *
3524 close_diff_view(struct tog_view *view)
3526 const struct got_error *err = NULL;
3527 struct tog_diff_view_state *s = &view->state.diff;
3529 free(s->id1);
3530 s->id1 = NULL;
3531 free(s->id2);
3532 s->id2 = NULL;
3533 if (s->f && fclose(s->f) == EOF)
3534 err = got_error_from_errno("fclose");
3535 free_colors(&s->colors);
3536 free(s->line_offsets);
3537 s->line_offsets = NULL;
3538 s->nlines = 0;
3539 got_ref_list_free(&s->refs);
3540 return err;
3543 static const struct got_error *
3544 show_diff_view(struct tog_view *view)
3546 const struct got_error *err;
3547 struct tog_diff_view_state *s = &view->state.diff;
3548 char *id_str1 = NULL, *id_str2, *header;
3549 const char *label1, *label2;
3551 if (s->id1) {
3552 err = got_object_id_str(&id_str1, s->id1);
3553 if (err)
3554 return err;
3555 label1 = s->label1 ? : id_str1;
3556 } else
3557 label1 = "/dev/null";
3559 err = got_object_id_str(&id_str2, s->id2);
3560 if (err)
3561 return err;
3562 label2 = s->label2 ? : id_str2;
3564 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3565 err = got_error_from_errno("asprintf");
3566 free(id_str1);
3567 free(id_str2);
3568 return err;
3570 free(id_str1);
3571 free(id_str2);
3573 return draw_file(view, header);
3576 static const struct got_error *
3577 set_selected_commit(struct tog_diff_view_state *s,
3578 struct commit_queue_entry *entry)
3580 const struct got_error *err;
3581 const struct got_object_id_queue *parent_ids;
3582 struct got_commit_object *selected_commit;
3583 struct got_object_qid *pid;
3585 free(s->id2);
3586 s->id2 = got_object_id_dup(entry->id);
3587 if (s->id2 == NULL)
3588 return got_error_from_errno("got_object_id_dup");
3590 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3591 if (err)
3592 return err;
3593 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3594 free(s->id1);
3595 pid = SIMPLEQ_FIRST(parent_ids);
3596 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3597 got_object_commit_close(selected_commit);
3598 return NULL;
3601 static const struct got_error *
3602 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3604 const struct got_error *err = NULL;
3605 struct tog_diff_view_state *s = &view->state.diff;
3606 struct tog_log_view_state *ls;
3607 struct commit_queue_entry *old_selected_entry;
3608 int i;
3610 switch (ch) {
3611 case 'a':
3612 case 'w':
3613 if (ch == 'a')
3614 s->force_text_diff = !s->force_text_diff;
3615 if (ch == 'w')
3616 s->ignore_whitespace = !s->ignore_whitespace;
3617 wclear(view->window);
3618 s->first_displayed_line = 1;
3619 s->last_displayed_line = view->nlines;
3620 diff_view_indicate_progress(view);
3621 err = create_diff(s);
3622 break;
3623 case 'k':
3624 case KEY_UP:
3625 if (s->first_displayed_line > 1)
3626 s->first_displayed_line--;
3627 break;
3628 case KEY_PPAGE:
3629 case CTRL('b'):
3630 if (s->first_displayed_line == 1)
3631 break;
3632 i = 0;
3633 while (i++ < view->nlines - 1 &&
3634 s->first_displayed_line > 1)
3635 s->first_displayed_line--;
3636 break;
3637 case 'j':
3638 case KEY_DOWN:
3639 if (!s->eof)
3640 s->first_displayed_line++;
3641 break;
3642 case KEY_NPAGE:
3643 case CTRL('f'):
3644 case ' ':
3645 if (s->eof)
3646 break;
3647 i = 0;
3648 while (!s->eof && i++ < view->nlines - 1) {
3649 char *line;
3650 line = parse_next_line(s->f, NULL);
3651 s->first_displayed_line++;
3652 if (line == NULL)
3653 break;
3655 break;
3656 case '[':
3657 if (s->diff_context > 0) {
3658 s->diff_context--;
3659 diff_view_indicate_progress(view);
3660 err = create_diff(s);
3661 if (s->first_displayed_line + view->nlines - 1 >
3662 s->nlines) {
3663 s->first_displayed_line = 1;
3664 s->last_displayed_line = view->nlines;
3667 break;
3668 case ']':
3669 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3670 s->diff_context++;
3671 diff_view_indicate_progress(view);
3672 err = create_diff(s);
3674 break;
3675 case '<':
3676 case ',':
3677 if (s->log_view == NULL)
3678 break;
3679 ls = &s->log_view->state.log;
3680 old_selected_entry = ls->selected_entry;
3682 err = input_log_view(NULL, s->log_view, KEY_UP);
3683 if (err)
3684 break;
3686 if (old_selected_entry == ls->selected_entry)
3687 break;
3689 err = set_selected_commit(s, ls->selected_entry);
3690 if (err)
3691 break;
3693 s->first_displayed_line = 1;
3694 s->last_displayed_line = view->nlines;
3696 diff_view_indicate_progress(view);
3697 err = create_diff(s);
3698 break;
3699 case '>':
3700 case '.':
3701 if (s->log_view == NULL)
3702 break;
3703 ls = &s->log_view->state.log;
3704 old_selected_entry = ls->selected_entry;
3706 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3707 if (err)
3708 break;
3710 if (old_selected_entry == ls->selected_entry)
3711 break;
3713 err = set_selected_commit(s, ls->selected_entry);
3714 if (err)
3715 break;
3717 s->first_displayed_line = 1;
3718 s->last_displayed_line = view->nlines;
3720 diff_view_indicate_progress(view);
3721 err = create_diff(s);
3722 break;
3723 default:
3724 break;
3727 return err;
3730 static const struct got_error *
3731 cmd_diff(int argc, char *argv[])
3733 const struct got_error *error = NULL;
3734 struct got_repository *repo = NULL;
3735 struct got_worktree *worktree = NULL;
3736 struct got_object_id *id1 = NULL, *id2 = NULL;
3737 char *repo_path = NULL, *cwd = NULL;
3738 char *id_str1 = NULL, *id_str2 = NULL;
3739 char *label1 = NULL, *label2 = NULL;
3740 int diff_context = 3, ignore_whitespace = 0;
3741 int ch, force_text_diff = 0;
3742 const char *errstr;
3743 struct tog_view *view;
3745 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3746 switch (ch) {
3747 case 'a':
3748 force_text_diff = 1;
3749 break;
3750 case 'C':
3751 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3752 &errstr);
3753 if (errstr != NULL)
3754 err(1, "-C option %s", errstr);
3755 break;
3756 case 'r':
3757 repo_path = realpath(optarg, NULL);
3758 if (repo_path == NULL)
3759 return got_error_from_errno2("realpath",
3760 optarg);
3761 got_path_strip_trailing_slashes(repo_path);
3762 break;
3763 case 'w':
3764 ignore_whitespace = 1;
3765 break;
3766 default:
3767 usage_diff();
3768 /* NOTREACHED */
3772 argc -= optind;
3773 argv += optind;
3775 if (argc == 0) {
3776 usage_diff(); /* TODO show local worktree changes */
3777 } else if (argc == 2) {
3778 id_str1 = argv[0];
3779 id_str2 = argv[1];
3780 } else
3781 usage_diff();
3783 cwd = getcwd(NULL, 0);
3784 if (cwd == NULL)
3785 return got_error_from_errno("getcwd");
3787 error = got_worktree_open(&worktree, cwd);
3788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3789 goto done;
3791 if (repo_path == NULL) {
3792 if (worktree)
3793 repo_path =
3794 strdup(got_worktree_get_repo_path(worktree));
3795 else
3796 repo_path = strdup(cwd);
3798 if (repo_path == NULL) {
3799 error = got_error_from_errno("strdup");
3800 goto done;
3803 error = got_repo_open(&repo, repo_path, NULL);
3804 if (error)
3805 goto done;
3807 init_curses();
3809 error = apply_unveil(got_repo_get_path(repo), NULL);
3810 if (error)
3811 goto done;
3813 error = got_repo_match_object_id(&id1, &label1, id_str1,
3814 GOT_OBJ_TYPE_ANY, 1, repo);
3815 if (error)
3816 goto done;
3818 error = got_repo_match_object_id(&id2, &label2, id_str2,
3819 GOT_OBJ_TYPE_ANY, 1, repo);
3820 if (error)
3821 goto done;
3823 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3824 if (view == NULL) {
3825 error = got_error_from_errno("view_open");
3826 goto done;
3828 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3829 ignore_whitespace, force_text_diff, NULL, repo);
3830 if (error)
3831 goto done;
3832 error = view_loop(view);
3833 done:
3834 free(label1);
3835 free(label2);
3836 free(repo_path);
3837 free(cwd);
3838 if (repo)
3839 got_repo_close(repo);
3840 if (worktree)
3841 got_worktree_close(worktree);
3842 return error;
3845 __dead static void
3846 usage_blame(void)
3848 endwin();
3849 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3850 getprogname());
3851 exit(1);
3854 struct tog_blame_line {
3855 int annotated;
3856 struct got_object_id *id;
3859 static const struct got_error *
3860 draw_blame(struct tog_view *view)
3862 struct tog_blame_view_state *s = &view->state.blame;
3863 struct tog_blame *blame = &s->blame;
3864 regmatch_t *regmatch = &view->regmatch;
3865 const struct got_error *err;
3866 int lineno = 0, nprinted = 0;
3867 char *line;
3868 size_t len;
3869 wchar_t *wline;
3870 int width;
3871 struct tog_blame_line *blame_line;
3872 struct got_object_id *prev_id = NULL;
3873 char *id_str;
3874 struct tog_color *tc;
3876 err = got_object_id_str(&id_str, s->blamed_commit->id);
3877 if (err)
3878 return err;
3880 rewind(blame->f);
3881 werase(view->window);
3883 if (asprintf(&line, "commit %s", id_str) == -1) {
3884 err = got_error_from_errno("asprintf");
3885 free(id_str);
3886 return err;
3889 err = format_line(&wline, &width, line, view->ncols, 0);
3890 free(line);
3891 line = NULL;
3892 if (err)
3893 return err;
3894 if (view_needs_focus_indication(view))
3895 wstandout(view->window);
3896 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3897 if (tc)
3898 wattr_on(view->window,
3899 COLOR_PAIR(tc->colorpair), NULL);
3900 waddwstr(view->window, wline);
3901 if (tc)
3902 wattr_off(view->window,
3903 COLOR_PAIR(tc->colorpair), NULL);
3904 if (view_needs_focus_indication(view))
3905 wstandend(view->window);
3906 free(wline);
3907 wline = NULL;
3908 if (width < view->ncols - 1)
3909 waddch(view->window, '\n');
3911 if (asprintf(&line, "[%d/%d] %s%s",
3912 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3913 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3914 free(id_str);
3915 return got_error_from_errno("asprintf");
3917 free(id_str);
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3919 free(line);
3920 line = NULL;
3921 if (err)
3922 return err;
3923 waddwstr(view->window, wline);
3924 free(wline);
3925 wline = NULL;
3926 if (width < view->ncols - 1)
3927 waddch(view->window, '\n');
3929 s->eof = 0;
3930 while (nprinted < view->nlines - 2) {
3931 line = parse_next_line(blame->f, &len);
3932 if (line == NULL) {
3933 s->eof = 1;
3934 break;
3936 if (++lineno < s->first_displayed_line) {
3937 free(line);
3938 continue;
3941 if (view->focussed && nprinted == s->selected_line - 1)
3942 wstandout(view->window);
3944 if (blame->nlines > 0) {
3945 blame_line = &blame->lines[lineno - 1];
3946 if (blame_line->annotated && prev_id &&
3947 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3948 !(view->focussed &&
3949 nprinted == s->selected_line - 1)) {
3950 waddstr(view->window, " ");
3951 } else if (blame_line->annotated) {
3952 char *id_str;
3953 err = got_object_id_str(&id_str, blame_line->id);
3954 if (err) {
3955 free(line);
3956 return err;
3958 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3959 if (tc)
3960 wattr_on(view->window,
3961 COLOR_PAIR(tc->colorpair), NULL);
3962 wprintw(view->window, "%.8s", id_str);
3963 if (tc)
3964 wattr_off(view->window,
3965 COLOR_PAIR(tc->colorpair), NULL);
3966 free(id_str);
3967 prev_id = blame_line->id;
3968 } else {
3969 waddstr(view->window, "........");
3970 prev_id = NULL;
3972 } else {
3973 waddstr(view->window, "........");
3974 prev_id = NULL;
3977 if (view->focussed && nprinted == s->selected_line - 1)
3978 wstandend(view->window);
3979 waddstr(view->window, " ");
3981 if (view->ncols <= 9) {
3982 width = 9;
3983 wline = wcsdup(L"");
3984 if (wline == NULL) {
3985 err = got_error_from_errno("wcsdup");
3986 free(line);
3987 return err;
3989 } else if (s->first_displayed_line + nprinted ==
3990 s->matched_line &&
3991 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3992 err = add_matched_line(&width, line, view->ncols - 9, 9,
3993 view->window, regmatch);
3994 if (err) {
3995 free(line);
3996 return err;
3998 width += 9;
3999 } else {
4000 err = format_line(&wline, &width, line,
4001 view->ncols - 9, 9);
4002 waddwstr(view->window, wline);
4003 free(wline);
4004 wline = NULL;
4005 width += 9;
4008 if (width <= view->ncols - 1)
4009 waddch(view->window, '\n');
4010 if (++nprinted == 1)
4011 s->first_displayed_line = lineno;
4012 free(line);
4014 s->last_displayed_line = lineno;
4016 view_vborder(view);
4018 return NULL;
4021 static const struct got_error *
4022 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4024 const struct got_error *err = NULL;
4025 struct tog_blame_cb_args *a = arg;
4026 struct tog_blame_line *line;
4027 int errcode;
4029 if (nlines != a->nlines ||
4030 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4031 return got_error(GOT_ERR_RANGE);
4033 errcode = pthread_mutex_lock(&tog_mutex);
4034 if (errcode)
4035 return got_error_set_errno(errcode, "pthread_mutex_lock");
4037 if (*a->quit) { /* user has quit the blame view */
4038 err = got_error(GOT_ERR_ITER_COMPLETED);
4039 goto done;
4042 if (lineno == -1)
4043 goto done; /* no change in this commit */
4045 line = &a->lines[lineno - 1];
4046 if (line->annotated)
4047 goto done;
4049 line->id = got_object_id_dup(id);
4050 if (line->id == NULL) {
4051 err = got_error_from_errno("got_object_id_dup");
4052 goto done;
4054 line->annotated = 1;
4055 done:
4056 errcode = pthread_mutex_unlock(&tog_mutex);
4057 if (errcode)
4058 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4059 return err;
4062 static void *
4063 blame_thread(void *arg)
4065 const struct got_error *err;
4066 struct tog_blame_thread_args *ta = arg;
4067 struct tog_blame_cb_args *a = ta->cb_args;
4068 int errcode;
4070 err = block_signals_used_by_main_thread();
4071 if (err)
4072 return (void *)err;
4074 err = got_blame(ta->path, a->commit_id, ta->repo,
4075 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4076 if (err && err->code == GOT_ERR_CANCELLED)
4077 err = NULL;
4079 errcode = pthread_mutex_lock(&tog_mutex);
4080 if (errcode)
4081 return (void *)got_error_set_errno(errcode,
4082 "pthread_mutex_lock");
4084 got_repo_close(ta->repo);
4085 ta->repo = NULL;
4086 *ta->complete = 1;
4088 errcode = pthread_mutex_unlock(&tog_mutex);
4089 if (errcode && err == NULL)
4090 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4092 return (void *)err;
4095 static struct got_object_id *
4096 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4097 int first_displayed_line, int selected_line)
4099 struct tog_blame_line *line;
4101 if (nlines <= 0)
4102 return NULL;
4104 line = &lines[first_displayed_line - 1 + selected_line - 1];
4105 if (!line->annotated)
4106 return NULL;
4108 return line->id;
4111 static const struct got_error *
4112 stop_blame(struct tog_blame *blame)
4114 const struct got_error *err = NULL;
4115 int i;
4117 if (blame->thread) {
4118 int errcode;
4119 errcode = pthread_mutex_unlock(&tog_mutex);
4120 if (errcode)
4121 return got_error_set_errno(errcode,
4122 "pthread_mutex_unlock");
4123 errcode = pthread_join(blame->thread, (void **)&err);
4124 if (errcode)
4125 return got_error_set_errno(errcode, "pthread_join");
4126 errcode = pthread_mutex_lock(&tog_mutex);
4127 if (errcode)
4128 return got_error_set_errno(errcode,
4129 "pthread_mutex_lock");
4130 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4131 err = NULL;
4132 blame->thread = NULL;
4134 if (blame->thread_args.repo) {
4135 got_repo_close(blame->thread_args.repo);
4136 blame->thread_args.repo = NULL;
4138 if (blame->f) {
4139 if (fclose(blame->f) != 0 && err == NULL)
4140 err = got_error_from_errno("fclose");
4141 blame->f = NULL;
4143 if (blame->lines) {
4144 for (i = 0; i < blame->nlines; i++)
4145 free(blame->lines[i].id);
4146 free(blame->lines);
4147 blame->lines = NULL;
4149 free(blame->cb_args.commit_id);
4150 blame->cb_args.commit_id = NULL;
4152 return err;
4155 static const struct got_error *
4156 cancel_blame_view(void *arg)
4158 const struct got_error *err = NULL;
4159 int *done = arg;
4160 int errcode;
4162 errcode = pthread_mutex_lock(&tog_mutex);
4163 if (errcode)
4164 return got_error_set_errno(errcode,
4165 "pthread_mutex_unlock");
4167 if (*done)
4168 err = got_error(GOT_ERR_CANCELLED);
4170 errcode = pthread_mutex_unlock(&tog_mutex);
4171 if (errcode)
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_lock");
4175 return err;
4178 static const struct got_error *
4179 run_blame(struct tog_view *view)
4181 struct tog_blame_view_state *s = &view->state.blame;
4182 struct tog_blame *blame = &s->blame;
4183 const struct got_error *err = NULL;
4184 struct got_blob_object *blob = NULL;
4185 struct got_repository *thread_repo = NULL;
4186 struct got_object_id *obj_id = NULL;
4187 int obj_type;
4189 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4190 s->path);
4191 if (err)
4192 return err;
4194 err = got_object_get_type(&obj_type, s->repo, obj_id);
4195 if (err)
4196 goto done;
4198 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4199 err = got_error(GOT_ERR_OBJ_TYPE);
4200 goto done;
4203 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4204 if (err)
4205 goto done;
4206 blame->f = got_opentemp();
4207 if (blame->f == NULL) {
4208 err = got_error_from_errno("got_opentemp");
4209 goto done;
4211 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4212 &blame->line_offsets, blame->f, blob);
4213 if (err || blame->nlines == 0)
4214 goto done;
4216 /* Don't include \n at EOF in the blame line count. */
4217 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4218 blame->nlines--;
4220 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4221 if (blame->lines == NULL) {
4222 err = got_error_from_errno("calloc");
4223 goto done;
4226 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4227 if (err)
4228 goto done;
4230 blame->cb_args.view = view;
4231 blame->cb_args.lines = blame->lines;
4232 blame->cb_args.nlines = blame->nlines;
4233 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4234 if (blame->cb_args.commit_id == NULL) {
4235 err = got_error_from_errno("got_object_id_dup");
4236 goto done;
4238 blame->cb_args.quit = &s->done;
4240 blame->thread_args.path = s->path;
4241 blame->thread_args.repo = thread_repo;
4242 blame->thread_args.cb_args = &blame->cb_args;
4243 blame->thread_args.complete = &s->blame_complete;
4244 blame->thread_args.cancel_cb = cancel_blame_view;
4245 blame->thread_args.cancel_arg = &s->done;
4246 s->blame_complete = 0;
4248 done:
4249 if (blob)
4250 got_object_blob_close(blob);
4251 free(obj_id);
4252 if (err)
4253 stop_blame(blame);
4254 return err;
4257 static const struct got_error *
4258 open_blame_view(struct tog_view *view, char *path,
4259 struct got_object_id *commit_id, struct got_repository *repo)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_view_state *s = &view->state.blame;
4264 SIMPLEQ_INIT(&s->blamed_commits);
4266 s->path = strdup(path);
4267 if (s->path == NULL)
4268 return got_error_from_errno("strdup");
4270 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4271 if (err) {
4272 free(s->path);
4273 return err;
4276 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4277 s->first_displayed_line = 1;
4278 s->last_displayed_line = view->nlines;
4279 s->selected_line = 1;
4280 s->blame_complete = 0;
4281 s->repo = repo;
4282 s->commit_id = commit_id;
4283 memset(&s->blame, 0, sizeof(s->blame));
4285 SIMPLEQ_INIT(&s->colors);
4286 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4287 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4288 get_color_value("TOG_COLOR_COMMIT"));
4289 if (err)
4290 return err;
4293 view->show = show_blame_view;
4294 view->input = input_blame_view;
4295 view->close = close_blame_view;
4296 view->search_start = search_start_blame_view;
4297 view->search_next = search_next_blame_view;
4299 return run_blame(view);
4302 static const struct got_error *
4303 close_blame_view(struct tog_view *view)
4305 const struct got_error *err = NULL;
4306 struct tog_blame_view_state *s = &view->state.blame;
4308 if (s->blame.thread)
4309 err = stop_blame(&s->blame);
4311 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4312 struct got_object_qid *blamed_commit;
4313 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4314 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4315 got_object_qid_free(blamed_commit);
4318 free(s->path);
4319 free_colors(&s->colors);
4321 return err;
4324 static const struct got_error *
4325 search_start_blame_view(struct tog_view *view)
4327 struct tog_blame_view_state *s = &view->state.blame;
4329 s->matched_line = 0;
4330 return NULL;
4333 static const struct got_error *
4334 search_next_blame_view(struct tog_view *view)
4336 struct tog_blame_view_state *s = &view->state.blame;
4337 int lineno;
4339 if (!view->searching) {
4340 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4341 return NULL;
4344 if (s->matched_line) {
4345 if (view->searching == TOG_SEARCH_FORWARD)
4346 lineno = s->matched_line + 1;
4347 else
4348 lineno = s->matched_line - 1;
4349 } else {
4350 if (view->searching == TOG_SEARCH_FORWARD)
4351 lineno = 1;
4352 else
4353 lineno = s->blame.nlines;
4356 while (1) {
4357 char *line = NULL;
4358 off_t offset;
4359 size_t len;
4361 if (lineno <= 0 || lineno > s->blame.nlines) {
4362 if (s->matched_line == 0) {
4363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4364 free(line);
4365 break;
4368 if (view->searching == TOG_SEARCH_FORWARD)
4369 lineno = 1;
4370 else
4371 lineno = s->blame.nlines;
4374 offset = s->blame.line_offsets[lineno - 1];
4375 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4376 free(line);
4377 return got_error_from_errno("fseeko");
4379 free(line);
4380 line = parse_next_line(s->blame.f, &len);
4381 if (line &&
4382 match_line(line, &view->regex, 1, &view->regmatch)) {
4383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 s->matched_line = lineno;
4385 free(line);
4386 break;
4388 free(line);
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno++;
4391 else
4392 lineno--;
4395 if (s->matched_line) {
4396 s->first_displayed_line = s->matched_line;
4397 s->selected_line = 1;
4400 return NULL;
4403 static const struct got_error *
4404 show_blame_view(struct tog_view *view)
4406 const struct got_error *err = NULL;
4407 struct tog_blame_view_state *s = &view->state.blame;
4408 int errcode;
4410 if (s->blame.thread == NULL) {
4411 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4412 &s->blame.thread_args);
4413 if (errcode)
4414 return got_error_set_errno(errcode, "pthread_create");
4416 halfdelay(1); /* fast refresh while annotating */
4419 if (s->blame_complete)
4420 halfdelay(10); /* disable fast refresh */
4422 err = draw_blame(view);
4424 view_vborder(view);
4425 return err;
4428 static const struct got_error *
4429 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4431 const struct got_error *err = NULL, *thread_err = NULL;
4432 struct tog_view *diff_view;
4433 struct tog_blame_view_state *s = &view->state.blame;
4434 int begin_x = 0;
4436 switch (ch) {
4437 case 'q':
4438 s->done = 1;
4439 break;
4440 case 'k':
4441 case KEY_UP:
4442 if (s->selected_line > 1)
4443 s->selected_line--;
4444 else if (s->selected_line == 1 &&
4445 s->first_displayed_line > 1)
4446 s->first_displayed_line--;
4447 break;
4448 case KEY_PPAGE:
4449 case CTRL('b'):
4450 if (s->first_displayed_line == 1) {
4451 s->selected_line = 1;
4452 break;
4454 if (s->first_displayed_line > view->nlines - 2)
4455 s->first_displayed_line -=
4456 (view->nlines - 2);
4457 else
4458 s->first_displayed_line = 1;
4459 break;
4460 case 'j':
4461 case KEY_DOWN:
4462 if (s->selected_line < view->nlines - 2 &&
4463 s->first_displayed_line +
4464 s->selected_line <= s->blame.nlines)
4465 s->selected_line++;
4466 else if (s->last_displayed_line <
4467 s->blame.nlines)
4468 s->first_displayed_line++;
4469 break;
4470 case 'b':
4471 case 'p': {
4472 struct got_object_id *id = NULL;
4473 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4474 s->first_displayed_line, s->selected_line);
4475 if (id == NULL)
4476 break;
4477 if (ch == 'p') {
4478 struct got_commit_object *commit;
4479 struct got_object_qid *pid;
4480 struct got_object_id *blob_id = NULL;
4481 int obj_type;
4482 err = got_object_open_as_commit(&commit,
4483 s->repo, id);
4484 if (err)
4485 break;
4486 pid = SIMPLEQ_FIRST(
4487 got_object_commit_get_parent_ids(commit));
4488 if (pid == NULL) {
4489 got_object_commit_close(commit);
4490 break;
4492 /* Check if path history ends here. */
4493 err = got_object_id_by_path(&blob_id, s->repo,
4494 pid->id, s->path);
4495 if (err) {
4496 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4497 err = NULL;
4498 got_object_commit_close(commit);
4499 break;
4501 err = got_object_get_type(&obj_type, s->repo,
4502 blob_id);
4503 free(blob_id);
4504 /* Can't blame non-blob type objects. */
4505 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4506 got_object_commit_close(commit);
4507 break;
4509 err = got_object_qid_alloc(&s->blamed_commit,
4510 pid->id);
4511 got_object_commit_close(commit);
4512 } else {
4513 if (got_object_id_cmp(id,
4514 s->blamed_commit->id) == 0)
4515 break;
4516 err = got_object_qid_alloc(&s->blamed_commit,
4517 id);
4519 if (err)
4520 break;
4521 s->done = 1;
4522 thread_err = stop_blame(&s->blame);
4523 s->done = 0;
4524 if (thread_err)
4525 break;
4526 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4527 s->blamed_commit, entry);
4528 err = run_blame(view);
4529 if (err)
4530 break;
4531 break;
4533 case 'B': {
4534 struct got_object_qid *first;
4535 first = SIMPLEQ_FIRST(&s->blamed_commits);
4536 if (!got_object_id_cmp(first->id, s->commit_id))
4537 break;
4538 s->done = 1;
4539 thread_err = stop_blame(&s->blame);
4540 s->done = 0;
4541 if (thread_err)
4542 break;
4543 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4544 got_object_qid_free(s->blamed_commit);
4545 s->blamed_commit =
4546 SIMPLEQ_FIRST(&s->blamed_commits);
4547 err = run_blame(view);
4548 if (err)
4549 break;
4550 break;
4552 case KEY_ENTER:
4553 case '\r': {
4554 struct got_object_id *id = NULL;
4555 struct got_object_qid *pid;
4556 struct got_commit_object *commit = NULL;
4557 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4558 s->first_displayed_line, s->selected_line);
4559 if (id == NULL)
4560 break;
4561 err = got_object_open_as_commit(&commit, s->repo, id);
4562 if (err)
4563 break;
4564 pid = SIMPLEQ_FIRST(
4565 got_object_commit_get_parent_ids(commit));
4566 if (view_is_parent_view(view))
4567 begin_x = view_split_begin_x(view->begin_x);
4568 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4569 if (diff_view == NULL) {
4570 got_object_commit_close(commit);
4571 err = got_error_from_errno("view_open");
4572 break;
4574 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4575 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4576 got_object_commit_close(commit);
4577 if (err) {
4578 view_close(diff_view);
4579 break;
4581 view->focussed = 0;
4582 diff_view->focussed = 1;
4583 if (view_is_parent_view(view)) {
4584 err = view_close_child(view);
4585 if (err)
4586 break;
4587 view_set_child(view, diff_view);
4588 view->focus_child = 1;
4589 } else
4590 *new_view = diff_view;
4591 if (err)
4592 break;
4593 break;
4595 case KEY_NPAGE:
4596 case CTRL('f'):
4597 case ' ':
4598 if (s->last_displayed_line >= s->blame.nlines &&
4599 s->selected_line >= MIN(s->blame.nlines,
4600 view->nlines - 2)) {
4601 break;
4603 if (s->last_displayed_line >= s->blame.nlines &&
4604 s->selected_line < view->nlines - 2) {
4605 s->selected_line = MIN(s->blame.nlines,
4606 view->nlines - 2);
4607 break;
4609 if (s->last_displayed_line + view->nlines - 2
4610 <= s->blame.nlines)
4611 s->first_displayed_line +=
4612 view->nlines - 2;
4613 else
4614 s->first_displayed_line =
4615 s->blame.nlines -
4616 (view->nlines - 3);
4617 break;
4618 case KEY_RESIZE:
4619 if (s->selected_line > view->nlines - 2) {
4620 s->selected_line = MIN(s->blame.nlines,
4621 view->nlines - 2);
4623 break;
4624 default:
4625 break;
4627 return thread_err ? thread_err : err;
4630 static const struct got_error *
4631 cmd_blame(int argc, char *argv[])
4633 const struct got_error *error;
4634 struct got_repository *repo = NULL;
4635 struct got_worktree *worktree = NULL;
4636 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4637 char *link_target = NULL;
4638 struct got_object_id *commit_id = NULL;
4639 char *commit_id_str = NULL;
4640 int ch;
4641 struct tog_view *view;
4643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4644 switch (ch) {
4645 case 'c':
4646 commit_id_str = optarg;
4647 break;
4648 case 'r':
4649 repo_path = realpath(optarg, NULL);
4650 if (repo_path == NULL)
4651 return got_error_from_errno2("realpath",
4652 optarg);
4653 break;
4654 default:
4655 usage_blame();
4656 /* NOTREACHED */
4660 argc -= optind;
4661 argv += optind;
4663 if (argc != 1)
4664 usage_blame();
4666 cwd = getcwd(NULL, 0);
4667 if (cwd == NULL)
4668 return got_error_from_errno("getcwd");
4670 error = got_worktree_open(&worktree, cwd);
4671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4672 goto done;
4674 if (repo_path == NULL) {
4675 if (worktree)
4676 repo_path =
4677 strdup(got_worktree_get_repo_path(worktree));
4678 else
4679 repo_path = strdup(cwd);
4681 if (repo_path == NULL) {
4682 error = got_error_from_errno("strdup");
4683 goto done;
4686 error = got_repo_open(&repo, repo_path, NULL);
4687 if (error != NULL)
4688 goto done;
4690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4691 worktree);
4692 if (error)
4693 goto done;
4695 init_curses();
4697 error = apply_unveil(got_repo_get_path(repo), NULL);
4698 if (error)
4699 goto done;
4701 if (commit_id_str == NULL) {
4702 struct got_reference *head_ref;
4703 error = got_ref_open(&head_ref, repo, worktree ?
4704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4705 if (error != NULL)
4706 goto done;
4707 error = got_ref_resolve(&commit_id, repo, head_ref);
4708 got_ref_close(head_ref);
4709 } else {
4710 error = got_repo_match_object_id(&commit_id, NULL,
4711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4713 if (error != NULL)
4714 goto done;
4716 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4717 if (view == NULL) {
4718 error = got_error_from_errno("view_open");
4719 goto done;
4722 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4723 commit_id, repo);
4724 if (error)
4725 goto done;
4727 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4728 commit_id, repo);
4729 if (error)
4730 goto done;
4731 if (worktree) {
4732 /* Release work tree lock. */
4733 got_worktree_close(worktree);
4734 worktree = NULL;
4736 error = view_loop(view);
4737 done:
4738 free(repo_path);
4739 free(in_repo_path);
4740 free(link_target);
4741 free(cwd);
4742 free(commit_id);
4743 if (worktree)
4744 got_worktree_close(worktree);
4745 if (repo)
4746 got_repo_close(repo);
4747 return error;
4750 static const struct got_error *
4751 draw_tree_entries(struct tog_view *view, const char *parent_path)
4753 struct tog_tree_view_state *s = &view->state.tree;
4754 const struct got_error *err = NULL;
4755 struct got_tree_entry *te;
4756 wchar_t *wline;
4757 struct tog_color *tc;
4758 int width, n, i, nentries;
4759 int limit = view->nlines;
4761 s->ndisplayed = 0;
4763 werase(view->window);
4765 if (limit == 0)
4766 return NULL;
4768 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4769 if (err)
4770 return err;
4771 if (view_needs_focus_indication(view))
4772 wstandout(view->window);
4773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4774 if (tc)
4775 wattr_on(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 waddwstr(view->window, wline);
4778 if (tc)
4779 wattr_off(view->window,
4780 COLOR_PAIR(tc->colorpair), NULL);
4781 if (view_needs_focus_indication(view))
4782 wstandend(view->window);
4783 free(wline);
4784 wline = NULL;
4785 if (width < view->ncols - 1)
4786 waddch(view->window, '\n');
4787 if (--limit <= 0)
4788 return NULL;
4789 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4790 if (err)
4791 return err;
4792 waddwstr(view->window, wline);
4793 free(wline);
4794 wline = NULL;
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4797 if (--limit <= 0)
4798 return NULL;
4799 waddch(view->window, '\n');
4800 if (--limit <= 0)
4801 return NULL;
4803 if (s->first_displayed_entry == NULL) {
4804 te = got_object_tree_get_first_entry(s->tree);
4805 if (s->selected == 0) {
4806 if (view->focussed)
4807 wstandout(view->window);
4808 s->selected_entry = NULL;
4810 waddstr(view->window, " ..\n"); /* parent directory */
4811 if (s->selected == 0 && view->focussed)
4812 wstandend(view->window);
4813 s->ndisplayed++;
4814 if (--limit <= 0)
4815 return NULL;
4816 n = 1;
4817 } else {
4818 n = 0;
4819 te = s->first_displayed_entry;
4822 nentries = got_object_tree_get_nentries(s->tree);
4823 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4824 char *line = NULL, *id_str = NULL, *link_target = NULL;
4825 const char *modestr = "";
4826 mode_t mode;
4828 te = got_object_tree_get_entry(s->tree, i);
4829 mode = got_tree_entry_get_mode(te);
4831 if (s->show_ids) {
4832 err = got_object_id_str(&id_str,
4833 got_tree_entry_get_id(te));
4834 if (err)
4835 return got_error_from_errno(
4836 "got_object_id_str");
4838 if (got_object_tree_entry_is_submodule(te))
4839 modestr = "$";
4840 else if (S_ISLNK(mode)) {
4841 int i;
4843 err = got_tree_entry_get_symlink_target(&link_target,
4844 te, s->repo);
4845 if (err) {
4846 free(id_str);
4847 return err;
4849 for (i = 0; i < strlen(link_target); i++) {
4850 if (!isprint((unsigned char)link_target[i]))
4851 link_target[i] = '?';
4853 modestr = "@";
4855 else if (S_ISDIR(mode))
4856 modestr = "/";
4857 else if (mode & S_IXUSR)
4858 modestr = "*";
4859 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4860 got_tree_entry_get_name(te), modestr,
4861 link_target ? " -> ": "",
4862 link_target ? link_target : "") == -1) {
4863 free(id_str);
4864 free(link_target);
4865 return got_error_from_errno("asprintf");
4867 free(id_str);
4868 free(link_target);
4869 err = format_line(&wline, &width, line, view->ncols, 0);
4870 if (err) {
4871 free(line);
4872 break;
4874 if (n == s->selected) {
4875 if (view->focussed)
4876 wstandout(view->window);
4877 s->selected_entry = te;
4879 tc = match_color(&s->colors, line);
4880 if (tc)
4881 wattr_on(view->window,
4882 COLOR_PAIR(tc->colorpair), NULL);
4883 waddwstr(view->window, wline);
4884 if (tc)
4885 wattr_off(view->window,
4886 COLOR_PAIR(tc->colorpair), NULL);
4887 if (width < view->ncols - 1)
4888 waddch(view->window, '\n');
4889 if (n == s->selected && view->focussed)
4890 wstandend(view->window);
4891 free(line);
4892 free(wline);
4893 wline = NULL;
4894 n++;
4895 s->ndisplayed++;
4896 s->last_displayed_entry = te;
4897 if (--limit <= 0)
4898 break;
4901 return err;
4904 static void
4905 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4907 struct got_tree_entry *te;
4908 int isroot = s->tree == s->root;
4909 int i = 0;
4911 if (s->first_displayed_entry == NULL)
4912 return;
4914 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4915 while (i++ < maxscroll) {
4916 if (te == NULL) {
4917 if (!isroot)
4918 s->first_displayed_entry = NULL;
4919 break;
4921 s->first_displayed_entry = te;
4922 te = got_tree_entry_get_prev(s->tree, te);
4926 static void
4927 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4929 struct got_tree_entry *next, *last;
4930 int n = 0;
4932 if (s->first_displayed_entry)
4933 next = got_tree_entry_get_next(s->tree,
4934 s->first_displayed_entry);
4935 else
4936 next = got_object_tree_get_first_entry(s->tree);
4938 last = s->last_displayed_entry;
4939 while (next && last && n++ < maxscroll) {
4940 last = got_tree_entry_get_next(s->tree, last);
4941 if (last) {
4942 s->first_displayed_entry = next;
4943 next = got_tree_entry_get_next(s->tree, next);
4948 static const struct got_error *
4949 tree_entry_path(char **path, struct tog_parent_trees *parents,
4950 struct got_tree_entry *te)
4952 const struct got_error *err = NULL;
4953 struct tog_parent_tree *pt;
4954 size_t len = 2; /* for leading slash and NUL */
4956 TAILQ_FOREACH(pt, parents, entry)
4957 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4958 + 1 /* slash */;
4959 if (te)
4960 len += strlen(got_tree_entry_get_name(te));
4962 *path = calloc(1, len);
4963 if (path == NULL)
4964 return got_error_from_errno("calloc");
4966 (*path)[0] = '/';
4967 pt = TAILQ_LAST(parents, tog_parent_trees);
4968 while (pt) {
4969 const char *name = got_tree_entry_get_name(pt->selected_entry);
4970 if (strlcat(*path, name, len) >= len) {
4971 err = got_error(GOT_ERR_NO_SPACE);
4972 goto done;
4974 if (strlcat(*path, "/", len) >= len) {
4975 err = got_error(GOT_ERR_NO_SPACE);
4976 goto done;
4978 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4980 if (te) {
4981 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4983 goto done;
4986 done:
4987 if (err) {
4988 free(*path);
4989 *path = NULL;
4991 return err;
4994 static const struct got_error *
4995 blame_tree_entry(struct tog_view **new_view, int begin_x,
4996 struct got_tree_entry *te, struct tog_parent_trees *parents,
4997 struct got_object_id *commit_id, struct got_repository *repo)
4999 const struct got_error *err = NULL;
5000 char *path;
5001 struct tog_view *blame_view;
5003 *new_view = NULL;
5005 err = tree_entry_path(&path, parents, te);
5006 if (err)
5007 return err;
5009 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5010 if (blame_view == NULL) {
5011 err = got_error_from_errno("view_open");
5012 goto done;
5015 err = open_blame_view(blame_view, path, commit_id, repo);
5016 if (err) {
5017 if (err->code == GOT_ERR_CANCELLED)
5018 err = NULL;
5019 view_close(blame_view);
5020 } else
5021 *new_view = blame_view;
5022 done:
5023 free(path);
5024 return err;
5027 static const struct got_error *
5028 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5029 struct tog_tree_view_state *s)
5031 struct tog_view *log_view;
5032 const struct got_error *err = NULL;
5033 char *path;
5035 *new_view = NULL;
5037 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5038 if (log_view == NULL)
5039 return got_error_from_errno("view_open");
5041 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5042 if (err)
5043 return err;
5045 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5046 path, 0);
5047 if (err)
5048 view_close(log_view);
5049 else
5050 *new_view = log_view;
5051 free(path);
5052 return err;
5055 static const struct got_error *
5056 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5057 struct got_object_id *commit_id, const char *head_ref_name,
5058 struct got_repository *repo)
5060 const struct got_error *err = NULL;
5061 char *commit_id_str = NULL;
5062 struct tog_tree_view_state *s = &view->state.tree;
5064 TAILQ_INIT(&s->parents);
5066 err = got_object_id_str(&commit_id_str, commit_id);
5067 if (err != NULL)
5068 goto done;
5070 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5071 err = got_error_from_errno("asprintf");
5072 goto done;
5075 s->root = s->tree = root;
5076 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5077 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5078 s->commit_id = got_object_id_dup(commit_id);
5079 if (s->commit_id == NULL) {
5080 err = got_error_from_errno("got_object_id_dup");
5081 goto done;
5083 if (head_ref_name) {
5084 s->head_ref_name = strdup(head_ref_name);
5085 if (s->head_ref_name == NULL) {
5086 err = got_error_from_errno("strdup");
5087 goto done;
5090 s->repo = repo;
5092 SIMPLEQ_INIT(&s->colors);
5094 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 err = add_color(&s->colors, "\\$$",
5096 TOG_COLOR_TREE_SUBMODULE,
5097 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5098 if (err)
5099 goto done;
5100 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5101 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5102 if (err) {
5103 free_colors(&s->colors);
5104 goto done;
5106 err = add_color(&s->colors, "/$",
5107 TOG_COLOR_TREE_DIRECTORY,
5108 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5109 if (err) {
5110 free_colors(&s->colors);
5111 goto done;
5114 err = add_color(&s->colors, "\\*$",
5115 TOG_COLOR_TREE_EXECUTABLE,
5116 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5117 if (err) {
5118 free_colors(&s->colors);
5119 goto done;
5122 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5123 get_color_value("TOG_COLOR_COMMIT"));
5124 if (err) {
5125 free_colors(&s->colors);
5126 goto done;
5130 view->show = show_tree_view;
5131 view->input = input_tree_view;
5132 view->close = close_tree_view;
5133 view->search_start = search_start_tree_view;
5134 view->search_next = search_next_tree_view;
5135 done:
5136 free(commit_id_str);
5137 if (err) {
5138 free(s->tree_label);
5139 s->tree_label = NULL;
5141 return err;
5144 static const struct got_error *
5145 close_tree_view(struct tog_view *view)
5147 struct tog_tree_view_state *s = &view->state.tree;
5149 free_colors(&s->colors);
5150 free(s->tree_label);
5151 s->tree_label = NULL;
5152 free(s->commit_id);
5153 s->commit_id = NULL;
5154 free(s->head_ref_name);
5155 s->head_ref_name = NULL;
5156 while (!TAILQ_EMPTY(&s->parents)) {
5157 struct tog_parent_tree *parent;
5158 parent = TAILQ_FIRST(&s->parents);
5159 TAILQ_REMOVE(&s->parents, parent, entry);
5160 free(parent);
5163 if (s->tree != s->root)
5164 got_object_tree_close(s->tree);
5165 got_object_tree_close(s->root);
5166 return NULL;
5169 static const struct got_error *
5170 search_start_tree_view(struct tog_view *view)
5172 struct tog_tree_view_state *s = &view->state.tree;
5174 s->matched_entry = NULL;
5175 return NULL;
5178 static int
5179 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5181 regmatch_t regmatch;
5183 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5184 0) == 0;
5187 static const struct got_error *
5188 search_next_tree_view(struct tog_view *view)
5190 struct tog_tree_view_state *s = &view->state.tree;
5191 struct got_tree_entry *te = NULL;
5193 if (!view->searching) {
5194 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5195 return NULL;
5198 if (s->matched_entry) {
5199 if (view->searching == TOG_SEARCH_FORWARD) {
5200 if (s->selected_entry)
5201 te = got_tree_entry_get_next(s->tree,
5202 s->selected_entry);
5203 else
5204 te = got_object_tree_get_first_entry(s->tree);
5205 } else {
5206 if (s->selected_entry == NULL)
5207 te = got_object_tree_get_last_entry(s->tree);
5208 else
5209 te = got_tree_entry_get_prev(s->tree,
5210 s->selected_entry);
5212 } else {
5213 if (view->searching == TOG_SEARCH_FORWARD)
5214 te = got_object_tree_get_first_entry(s->tree);
5215 else
5216 te = got_object_tree_get_last_entry(s->tree);
5219 while (1) {
5220 if (te == NULL) {
5221 if (s->matched_entry == NULL) {
5222 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 return NULL;
5225 if (view->searching == TOG_SEARCH_FORWARD)
5226 te = got_object_tree_get_first_entry(s->tree);
5227 else
5228 te = got_object_tree_get_last_entry(s->tree);
5231 if (match_tree_entry(te, &view->regex)) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 s->matched_entry = te;
5234 break;
5237 if (view->searching == TOG_SEARCH_FORWARD)
5238 te = got_tree_entry_get_next(s->tree, te);
5239 else
5240 te = got_tree_entry_get_prev(s->tree, te);
5243 if (s->matched_entry) {
5244 s->first_displayed_entry = s->matched_entry;
5245 s->selected = 0;
5248 return NULL;
5251 static const struct got_error *
5252 show_tree_view(struct tog_view *view)
5254 const struct got_error *err = NULL;
5255 struct tog_tree_view_state *s = &view->state.tree;
5256 char *parent_path;
5258 err = tree_entry_path(&parent_path, &s->parents, NULL);
5259 if (err)
5260 return err;
5262 err = draw_tree_entries(view, parent_path);
5263 free(parent_path);
5265 view_vborder(view);
5266 return err;
5269 static const struct got_error *
5270 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5272 const struct got_error *err = NULL;
5273 struct tog_tree_view_state *s = &view->state.tree;
5274 struct tog_view *log_view, *ref_view;
5275 int begin_x = 0;
5277 switch (ch) {
5278 case 'i':
5279 s->show_ids = !s->show_ids;
5280 break;
5281 case 'l':
5282 if (!s->selected_entry)
5283 break;
5284 if (view_is_parent_view(view))
5285 begin_x = view_split_begin_x(view->begin_x);
5286 err = log_selected_tree_entry(&log_view, begin_x, s);
5287 view->focussed = 0;
5288 log_view->focussed = 1;
5289 if (view_is_parent_view(view)) {
5290 err = view_close_child(view);
5291 if (err)
5292 return err;
5293 view_set_child(view, log_view);
5294 view->focus_child = 1;
5295 } else
5296 *new_view = log_view;
5297 break;
5298 case 'r':
5299 if (view_is_parent_view(view))
5300 begin_x = view_split_begin_x(view->begin_x);
5301 ref_view = view_open(view->nlines, view->ncols,
5302 view->begin_y, begin_x, TOG_VIEW_REF);
5303 if (ref_view == NULL)
5304 return got_error_from_errno("view_open");
5305 err = open_ref_view(ref_view, s->repo);
5306 if (err) {
5307 view_close(ref_view);
5308 return err;
5310 view->focussed = 0;
5311 ref_view->focussed = 1;
5312 if (view_is_parent_view(view)) {
5313 err = view_close_child(view);
5314 if (err)
5315 return err;
5316 view_set_child(view, ref_view);
5317 view->focus_child = 1;
5318 } else
5319 *new_view = ref_view;
5320 break;
5321 case 'k':
5322 case KEY_UP:
5323 if (s->selected > 0) {
5324 s->selected--;
5325 break;
5327 tree_scroll_up(s, 1);
5328 break;
5329 case KEY_PPAGE:
5330 case CTRL('b'):
5331 if (s->tree == s->root) {
5332 if (got_object_tree_get_first_entry(s->tree) ==
5333 s->first_displayed_entry)
5334 s->selected = 0;
5335 } else {
5336 if (s->first_displayed_entry == NULL)
5337 s->selected = 0;
5339 tree_scroll_up(s, MAX(0, view->nlines - 3));
5340 break;
5341 case 'j':
5342 case KEY_DOWN:
5343 if (s->selected < s->ndisplayed - 1) {
5344 s->selected++;
5345 break;
5347 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5348 == NULL)
5349 /* can't scroll any further */
5350 break;
5351 tree_scroll_down(s, 1);
5352 break;
5353 case KEY_NPAGE:
5354 case CTRL('f'):
5355 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5356 == NULL) {
5357 /* can't scroll any further; move cursor down */
5358 if (s->selected < s->ndisplayed - 1)
5359 s->selected = s->ndisplayed - 1;
5360 break;
5362 tree_scroll_down(s, view->nlines - 3);
5363 break;
5364 case KEY_ENTER:
5365 case '\r':
5366 case KEY_BACKSPACE:
5367 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5368 struct tog_parent_tree *parent;
5369 /* user selected '..' */
5370 if (s->tree == s->root)
5371 break;
5372 parent = TAILQ_FIRST(&s->parents);
5373 TAILQ_REMOVE(&s->parents, parent,
5374 entry);
5375 got_object_tree_close(s->tree);
5376 s->tree = parent->tree;
5377 s->first_displayed_entry =
5378 parent->first_displayed_entry;
5379 s->selected_entry =
5380 parent->selected_entry;
5381 s->selected = parent->selected;
5382 free(parent);
5383 } else if (S_ISDIR(got_tree_entry_get_mode(
5384 s->selected_entry))) {
5385 struct got_tree_object *subtree;
5386 err = got_object_open_as_tree(&subtree, s->repo,
5387 got_tree_entry_get_id(s->selected_entry));
5388 if (err)
5389 break;
5390 err = tree_view_visit_subtree(s, subtree);
5391 if (err) {
5392 got_object_tree_close(subtree);
5393 break;
5395 } else if (S_ISREG(got_tree_entry_get_mode(
5396 s->selected_entry))) {
5397 struct tog_view *blame_view;
5398 int begin_x = view_is_parent_view(view) ?
5399 view_split_begin_x(view->begin_x) : 0;
5401 err = blame_tree_entry(&blame_view, begin_x,
5402 s->selected_entry, &s->parents,
5403 s->commit_id, s->repo);
5404 if (err)
5405 break;
5406 view->focussed = 0;
5407 blame_view->focussed = 1;
5408 if (view_is_parent_view(view)) {
5409 err = view_close_child(view);
5410 if (err)
5411 return err;
5412 view_set_child(view, blame_view);
5413 view->focus_child = 1;
5414 } else
5415 *new_view = blame_view;
5417 break;
5418 case KEY_RESIZE:
5419 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5420 s->selected = view->nlines - 4;
5421 break;
5422 default:
5423 break;
5426 return err;
5429 __dead static void
5430 usage_tree(void)
5432 endwin();
5433 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5434 getprogname());
5435 exit(1);
5438 static const struct got_error *
5439 cmd_tree(int argc, char *argv[])
5441 const struct got_error *error;
5442 struct got_repository *repo = NULL;
5443 struct got_worktree *worktree = NULL;
5444 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5445 struct got_object_id *commit_id = NULL;
5446 const char *commit_id_arg = NULL;
5447 char *label = NULL;
5448 struct got_commit_object *commit = NULL;
5449 struct got_tree_object *tree = NULL;
5450 struct got_reference *ref = NULL;
5451 const char *head_ref_name = NULL;
5452 int ch;
5453 struct tog_view *view;
5455 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5456 switch (ch) {
5457 case 'c':
5458 commit_id_arg = optarg;
5459 break;
5460 case 'r':
5461 repo_path = realpath(optarg, NULL);
5462 if (repo_path == NULL)
5463 return got_error_from_errno2("realpath",
5464 optarg);
5465 break;
5466 default:
5467 usage_tree();
5468 /* NOTREACHED */
5472 argc -= optind;
5473 argv += optind;
5475 if (argc > 1)
5476 usage_tree();
5478 cwd = getcwd(NULL, 0);
5479 if (cwd == NULL)
5480 return got_error_from_errno("getcwd");
5482 error = got_worktree_open(&worktree, cwd);
5483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5484 goto done;
5486 if (repo_path == NULL) {
5487 if (worktree)
5488 repo_path =
5489 strdup(got_worktree_get_repo_path(worktree));
5490 else
5491 repo_path = strdup(cwd);
5493 if (repo_path == NULL) {
5494 error = got_error_from_errno("strdup");
5495 goto done;
5498 error = got_repo_open(&repo, repo_path, NULL);
5499 if (error != NULL)
5500 goto done;
5502 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5503 repo, worktree);
5504 if (error)
5505 goto done;
5507 init_curses();
5509 error = apply_unveil(got_repo_get_path(repo), NULL);
5510 if (error)
5511 goto done;
5513 if (commit_id_arg == NULL) {
5514 error = got_repo_match_object_id(&commit_id, &label,
5515 worktree ? got_worktree_get_head_ref_name(worktree) :
5516 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5517 if (error)
5518 goto done;
5519 head_ref_name = label;
5520 } else {
5521 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5522 if (error == NULL)
5523 head_ref_name = got_ref_get_name(ref);
5524 else if (error->code != GOT_ERR_NOT_REF)
5525 goto done;
5526 error = got_repo_match_object_id(&commit_id, NULL,
5527 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5528 if (error)
5529 goto done;
5532 error = got_object_open_as_commit(&commit, repo, commit_id);
5533 if (error)
5534 goto done;
5536 error = got_object_open_as_tree(&tree, repo,
5537 got_object_commit_get_tree_id(commit));
5538 if (error)
5539 goto done;
5541 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5542 if (view == NULL) {
5543 error = got_error_from_errno("view_open");
5544 goto done;
5546 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5547 if (error)
5548 goto done;
5549 if (!got_path_is_root_dir(in_repo_path)) {
5550 error = tree_view_walk_path(&view->state.tree, commit_id,
5551 in_repo_path);
5552 if (error)
5553 goto done;
5556 if (worktree) {
5557 /* Release work tree lock. */
5558 got_worktree_close(worktree);
5559 worktree = NULL;
5561 error = view_loop(view);
5562 done:
5563 free(repo_path);
5564 free(cwd);
5565 free(commit_id);
5566 free(label);
5567 if (ref)
5568 got_ref_close(ref);
5569 if (commit)
5570 got_object_commit_close(commit);
5571 if (tree)
5572 got_object_tree_close(tree);
5573 if (repo)
5574 got_repo_close(repo);
5575 return error;
5578 static const struct got_error *
5579 ref_view_load_refs(struct tog_ref_view_state *s)
5581 const struct got_error *err;
5582 struct got_reflist_entry *sre;
5583 struct tog_reflist_entry *re;
5585 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5586 got_ref_cmp_by_name, NULL);
5587 if (err)
5588 return err;
5590 s->nrefs = 0;
5591 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5592 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5593 continue;
5595 re = malloc(sizeof(*re));
5596 if (re == NULL)
5597 return got_error_from_errno("malloc");
5599 re->ref = sre->ref;
5600 re->idx = s->nrefs++;
5601 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5604 return NULL;
5607 void
5608 ref_view_free_refs(struct tog_ref_view_state *s)
5610 struct tog_reflist_entry *re;
5612 while (!TAILQ_EMPTY(&s->refs)) {
5613 re = TAILQ_FIRST(&s->refs);
5614 TAILQ_REMOVE(&s->refs, re, entry);
5615 free(re);
5617 got_ref_list_free(&s->simplerefs);
5620 static const struct got_error *
5621 open_ref_view(struct tog_view *view, struct got_repository *repo)
5623 const struct got_error *err = NULL;
5624 struct tog_ref_view_state *s = &view->state.ref;
5626 s->selected_entry = 0;
5627 s->repo = repo;
5629 SIMPLEQ_INIT(&s->simplerefs);
5630 TAILQ_INIT(&s->refs);
5631 SIMPLEQ_INIT(&s->colors);
5633 err = ref_view_load_refs(s);
5634 if (err)
5635 return err;
5637 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5639 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5640 err = add_color(&s->colors, "^refs/heads/",
5641 TOG_COLOR_REFS_HEADS,
5642 get_color_value("TOG_COLOR_REFS_HEADS"));
5643 if (err)
5644 goto done;
5646 err = add_color(&s->colors, "^refs/tags/",
5647 TOG_COLOR_REFS_TAGS,
5648 get_color_value("TOG_COLOR_REFS_TAGS"));
5649 if (err)
5650 goto done;
5652 err = add_color(&s->colors, "^refs/remotes/",
5653 TOG_COLOR_REFS_REMOTES,
5654 get_color_value("TOG_COLOR_REFS_REMOTES"));
5655 if (err)
5656 goto done;
5659 view->show = show_ref_view;
5660 view->input = input_ref_view;
5661 view->close = close_ref_view;
5662 view->search_start = search_start_ref_view;
5663 view->search_next = search_next_ref_view;
5664 done:
5665 if (err)
5666 free_colors(&s->colors);
5667 return err;
5670 static const struct got_error *
5671 close_ref_view(struct tog_view *view)
5673 struct tog_ref_view_state *s = &view->state.ref;
5675 ref_view_free_refs(s);
5676 free_colors(&s->colors);
5678 return NULL;
5681 static const struct got_error *
5682 resolve_reflist_entry(struct got_object_id **commit_id,
5683 struct tog_reflist_entry *re, struct got_repository *repo)
5685 const struct got_error *err = NULL;
5686 struct got_object_id *obj_id;
5687 struct got_tag_object *tag = NULL;
5688 int obj_type;
5690 *commit_id = NULL;
5692 err = got_ref_resolve(&obj_id, repo, re->ref);
5693 if (err)
5694 return err;
5696 err = got_object_get_type(&obj_type, repo, obj_id);
5697 if (err)
5698 goto done;
5700 switch (obj_type) {
5701 case GOT_OBJ_TYPE_COMMIT:
5702 *commit_id = obj_id;
5703 break;
5704 case GOT_OBJ_TYPE_TAG:
5705 err = got_object_open_as_tag(&tag, repo, obj_id);
5706 if (err)
5707 goto done;
5708 free(obj_id);
5709 err = got_object_get_type(&obj_type, repo,
5710 got_object_tag_get_object_id(tag));
5711 if (err)
5712 goto done;
5713 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5714 err = got_error(GOT_ERR_OBJ_TYPE);
5715 goto done;
5717 *commit_id = got_object_id_dup(
5718 got_object_tag_get_object_id(tag));
5719 if (*commit_id == NULL) {
5720 err = got_error_from_errno("got_object_id_dup");
5721 goto done;
5723 break;
5724 default:
5725 err = got_error(GOT_ERR_OBJ_TYPE);
5726 break;
5729 done:
5730 if (tag)
5731 got_object_tag_close(tag);
5732 if (err) {
5733 free(*commit_id);
5734 *commit_id = NULL;
5736 return err;
5739 static const struct got_error *
5740 log_ref_entry(struct tog_view **new_view, int begin_x,
5741 struct tog_reflist_entry *re, struct got_repository *repo)
5743 struct tog_view *log_view;
5744 const struct got_error *err = NULL;
5745 struct got_object_id *commit_id = NULL;
5747 *new_view = NULL;
5749 err = resolve_reflist_entry(&commit_id, re, repo);
5750 if (err) {
5751 if (err->code != GOT_ERR_OBJ_TYPE)
5752 return err;
5753 else
5754 return NULL;
5757 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5758 if (log_view == NULL) {
5759 err = got_error_from_errno("view_open");
5760 goto done;
5763 err = open_log_view(log_view, commit_id, repo,
5764 got_ref_get_name(re->ref), "", 0);
5765 done:
5766 if (err)
5767 view_close(log_view);
5768 else
5769 *new_view = log_view;
5770 free(commit_id);
5771 return err;
5774 static void
5775 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5777 struct tog_reflist_entry *re;
5778 int i = 0;
5780 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5781 return;
5783 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5784 while (i++ < maxscroll) {
5785 if (re == NULL)
5786 break;
5787 s->first_displayed_entry = re;
5788 re = TAILQ_PREV(re, tog_reflist_head, entry);
5792 static void
5793 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5795 struct tog_reflist_entry *next, *last;
5796 int n = 0;
5798 if (s->first_displayed_entry)
5799 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5800 else
5801 next = TAILQ_FIRST(&s->refs);
5803 last = s->last_displayed_entry;
5804 while (next && last && n++ < maxscroll) {
5805 last = TAILQ_NEXT(last, entry);
5806 if (last) {
5807 s->first_displayed_entry = next;
5808 next = TAILQ_NEXT(next, entry);
5813 static const struct got_error *
5814 search_start_ref_view(struct tog_view *view)
5816 struct tog_ref_view_state *s = &view->state.ref;
5818 s->matched_entry = NULL;
5819 return NULL;
5822 static int
5823 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5825 regmatch_t regmatch;
5827 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5828 0) == 0;
5831 static const struct got_error *
5832 search_next_ref_view(struct tog_view *view)
5834 struct tog_ref_view_state *s = &view->state.ref;
5835 struct tog_reflist_entry *re = NULL;
5837 if (!view->searching) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5839 return NULL;
5842 if (s->matched_entry) {
5843 if (view->searching == TOG_SEARCH_FORWARD) {
5844 if (s->selected_entry)
5845 re = TAILQ_NEXT(s->selected_entry, entry);
5846 else
5847 re = TAILQ_PREV(s->selected_entry,
5848 tog_reflist_head, entry);
5849 } else {
5850 if (s->selected_entry == NULL)
5851 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5852 else
5853 re = TAILQ_PREV(s->selected_entry,
5854 tog_reflist_head, entry);
5856 } else {
5857 if (view->searching == TOG_SEARCH_FORWARD)
5858 re = TAILQ_FIRST(&s->refs);
5859 else
5860 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5863 while (1) {
5864 if (re == NULL) {
5865 if (s->matched_entry == NULL) {
5866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5867 return NULL;
5869 if (view->searching == TOG_SEARCH_FORWARD)
5870 re = TAILQ_FIRST(&s->refs);
5871 else
5872 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5875 if (match_reflist_entry(re, &view->regex)) {
5876 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 s->matched_entry = re;
5878 break;
5881 if (view->searching == TOG_SEARCH_FORWARD)
5882 re = TAILQ_NEXT(re, entry);
5883 else
5884 re = TAILQ_PREV(re, tog_reflist_head, entry);
5887 if (s->matched_entry) {
5888 s->first_displayed_entry = s->matched_entry;
5889 s->selected = 0;
5892 return NULL;
5895 static const struct got_error *
5896 show_ref_view(struct tog_view *view)
5898 const struct got_error *err = NULL;
5899 struct tog_ref_view_state *s = &view->state.ref;
5900 struct tog_reflist_entry *re;
5901 char *line = NULL;
5902 wchar_t *wline;
5903 struct tog_color *tc;
5904 int width, n;
5905 int limit = view->nlines;
5907 werase(view->window);
5909 s->ndisplayed = 0;
5911 if (limit == 0)
5912 return NULL;
5914 re = s->first_displayed_entry;
5916 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5917 s->nrefs) == -1)
5918 return got_error_from_errno("asprintf");
5920 err = format_line(&wline, &width, line, view->ncols, 0);
5921 if (err) {
5922 free(line);
5923 return err;
5925 if (view_needs_focus_indication(view))
5926 wstandout(view->window);
5927 waddwstr(view->window, wline);
5928 if (view_needs_focus_indication(view))
5929 wstandend(view->window);
5930 free(wline);
5931 wline = NULL;
5932 free(line);
5933 line = NULL;
5934 if (width < view->ncols - 1)
5935 waddch(view->window, '\n');
5936 if (--limit <= 0)
5937 return NULL;
5939 n = 0;
5940 while (re && limit > 0) {
5941 char *line = NULL;
5943 if (got_ref_is_symbolic(re->ref)) {
5944 if (asprintf(&line, "%s -> %s",
5945 got_ref_get_name(re->ref),
5946 got_ref_get_symref_target(re->ref)) == -1)
5947 return got_error_from_errno("asprintf");
5948 } else if (s->show_ids) {
5949 struct got_object_id *id;
5950 char *id_str;
5951 err = got_ref_resolve(&id, s->repo, re->ref);
5952 if (err)
5953 return err;
5954 err = got_object_id_str(&id_str, id);
5955 if (err) {
5956 free(id);
5957 return err;
5959 if (asprintf(&line, "%s: %s",
5960 got_ref_get_name(re->ref), id_str) == -1) {
5961 err = got_error_from_errno("asprintf");
5962 free(id);
5963 free(id_str);
5964 return err;
5966 free(id);
5967 free(id_str);
5968 } else {
5969 line = strdup(got_ref_get_name(re->ref));
5970 if (line == NULL)
5971 return got_error_from_errno("strdup");
5974 err = format_line(&wline, &width, line, view->ncols, 0);
5975 if (err) {
5976 free(line);
5977 return err;
5979 if (n == s->selected) {
5980 if (view->focussed)
5981 wstandout(view->window);
5982 s->selected_entry = re;
5984 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5985 if (tc)
5986 wattr_on(view->window,
5987 COLOR_PAIR(tc->colorpair), NULL);
5988 waddwstr(view->window, wline);
5989 if (tc)
5990 wattr_off(view->window,
5991 COLOR_PAIR(tc->colorpair), NULL);
5992 if (width < view->ncols - 1)
5993 waddch(view->window, '\n');
5994 if (n == s->selected && view->focussed)
5995 wstandend(view->window);
5996 free(line);
5997 free(wline);
5998 wline = NULL;
5999 n++;
6000 s->ndisplayed++;
6001 s->last_displayed_entry = re;
6003 limit--;
6004 re = TAILQ_NEXT(re, entry);
6007 view_vborder(view);
6008 return err;
6011 static const struct got_error *
6012 browse_ref_tree(struct tog_view **new_view, int begin_x,
6013 struct tog_reflist_entry *re, struct got_repository *repo)
6015 const struct got_error *err = NULL;
6016 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6017 struct got_tree_object *tree = NULL;
6018 struct tog_view *tree_view;
6020 *new_view = NULL;
6022 err = resolve_reflist_entry(&commit_id, re, repo);
6023 if (err) {
6024 if (err->code != GOT_ERR_OBJ_TYPE)
6025 return err;
6026 else
6027 return NULL;
6030 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6031 if (err)
6032 goto done;
6034 err = got_object_open_as_tree(&tree, repo, tree_id);
6035 if (err)
6036 goto done;
6038 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6039 if (tree_view == NULL) {
6040 err = got_error_from_errno("view_open");
6041 goto done;
6044 err = open_tree_view(tree_view, tree, commit_id,
6045 got_ref_get_name(re->ref), repo);
6046 if (err)
6047 goto done;
6049 *new_view = tree_view;
6050 done:
6051 free(commit_id);
6052 free(tree_id);
6053 if (err) {
6054 if (tree)
6055 got_object_tree_close(tree);
6057 return err;
6059 static const struct got_error *
6060 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6062 const struct got_error *err = NULL;
6063 struct tog_ref_view_state *s = &view->state.ref;
6064 struct tog_view *log_view, *tree_view;
6065 int begin_x = 0;
6067 switch (ch) {
6068 case 'i':
6069 s->show_ids = !s->show_ids;
6070 break;
6071 case KEY_ENTER:
6072 case '\r':
6073 if (!s->selected_entry)
6074 break;
6075 if (view_is_parent_view(view))
6076 begin_x = view_split_begin_x(view->begin_x);
6077 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6078 s->repo);
6079 view->focussed = 0;
6080 log_view->focussed = 1;
6081 if (view_is_parent_view(view)) {
6082 err = view_close_child(view);
6083 if (err)
6084 return err;
6085 view_set_child(view, log_view);
6086 view->focus_child = 1;
6087 } else
6088 *new_view = log_view;
6089 break;
6090 case 't':
6091 if (!s->selected_entry)
6092 break;
6093 if (view_is_parent_view(view))
6094 begin_x = view_split_begin_x(view->begin_x);
6095 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6096 s->repo);
6097 if (err || tree_view == NULL)
6098 break;
6099 view->focussed = 0;
6100 tree_view->focussed = 1;
6101 if (view_is_parent_view(view)) {
6102 err = view_close_child(view);
6103 if (err)
6104 return err;
6105 view_set_child(view, tree_view);
6106 view->focus_child = 1;
6107 } else
6108 *new_view = tree_view;
6109 break;
6110 case 'k':
6111 case KEY_UP:
6112 if (s->selected > 0) {
6113 s->selected--;
6114 break;
6116 ref_scroll_up(s, 1);
6117 break;
6118 case KEY_PPAGE:
6119 case CTRL('b'):
6120 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6121 s->selected = 0;
6122 ref_scroll_up(s, MAX(0, view->nlines - 1));
6123 break;
6124 case 'j':
6125 case KEY_DOWN:
6126 if (s->selected < s->ndisplayed - 1) {
6127 s->selected++;
6128 break;
6130 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6131 /* can't scroll any further */
6132 break;
6133 ref_scroll_down(s, 1);
6134 break;
6135 case KEY_NPAGE:
6136 case CTRL('f'):
6137 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6138 /* can't scroll any further; move cursor down */
6139 if (s->selected < s->ndisplayed - 1)
6140 s->selected = s->ndisplayed - 1;
6141 break;
6143 ref_scroll_down(s, view->nlines - 1);
6144 break;
6145 case CTRL('l'):
6146 ref_view_free_refs(s);
6147 err = ref_view_load_refs(s);
6148 break;
6149 case KEY_RESIZE:
6150 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6151 s->selected = view->nlines - 2;
6152 break;
6153 default:
6154 break;
6157 return err;
6160 __dead static void
6161 usage_ref(void)
6163 endwin();
6164 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6165 getprogname());
6166 exit(1);
6169 static const struct got_error *
6170 cmd_ref(int argc, char *argv[])
6172 const struct got_error *error;
6173 struct got_repository *repo = NULL;
6174 struct got_worktree *worktree = NULL;
6175 char *cwd = NULL, *repo_path = NULL;
6176 int ch;
6177 struct tog_view *view;
6179 while ((ch = getopt(argc, argv, "r:")) != -1) {
6180 switch (ch) {
6181 case 'r':
6182 repo_path = realpath(optarg, NULL);
6183 if (repo_path == NULL)
6184 return got_error_from_errno2("realpath",
6185 optarg);
6186 break;
6187 default:
6188 usage_ref();
6189 /* NOTREACHED */
6193 argc -= optind;
6194 argv += optind;
6196 if (argc > 1)
6197 usage_ref();
6199 cwd = getcwd(NULL, 0);
6200 if (cwd == NULL)
6201 return got_error_from_errno("getcwd");
6203 error = got_worktree_open(&worktree, cwd);
6204 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6205 goto done;
6207 if (repo_path == NULL) {
6208 if (worktree)
6209 repo_path =
6210 strdup(got_worktree_get_repo_path(worktree));
6211 else
6212 repo_path = strdup(cwd);
6214 if (repo_path == NULL) {
6215 error = got_error_from_errno("strdup");
6216 goto done;
6219 error = got_repo_open(&repo, repo_path, NULL);
6220 if (error != NULL)
6221 goto done;
6223 init_curses();
6225 error = apply_unveil(got_repo_get_path(repo), NULL);
6226 if (error)
6227 goto done;
6229 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6230 if (view == NULL) {
6231 error = got_error_from_errno("view_open");
6232 goto done;
6235 error = open_ref_view(view, repo);
6236 if (error)
6237 goto done;
6239 if (worktree) {
6240 /* Release work tree lock. */
6241 got_worktree_close(worktree);
6242 worktree = NULL;
6244 error = view_loop(view);
6245 done:
6246 free(repo_path);
6247 free(cwd);
6248 if (repo)
6249 got_repo_close(repo);
6250 return error;
6253 static void
6254 list_commands(FILE *fp)
6256 int i;
6258 fprintf(fp, "commands:");
6259 for (i = 0; i < nitems(tog_commands); i++) {
6260 struct tog_cmd *cmd = &tog_commands[i];
6261 fprintf(fp, " %s", cmd->name);
6263 fputc('\n', fp);
6266 __dead static void
6267 usage(int hflag, int status)
6269 FILE *fp = (status == 0) ? stdout : stderr;
6271 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6272 getprogname());
6273 if (hflag) {
6274 fprintf(fp, "lazy usage: %s path\n", getprogname());
6275 list_commands(fp);
6277 exit(status);
6280 static char **
6281 make_argv(int argc, ...)
6283 va_list ap;
6284 char **argv;
6285 int i;
6287 va_start(ap, argc);
6289 argv = calloc(argc, sizeof(char *));
6290 if (argv == NULL)
6291 err(1, "calloc");
6292 for (i = 0; i < argc; i++) {
6293 argv[i] = strdup(va_arg(ap, char *));
6294 if (argv[i] == NULL)
6295 err(1, "strdup");
6298 va_end(ap);
6299 return argv;
6303 * Try to convert 'tog path' into a 'tog log path' command.
6304 * The user could simply have mistyped the command rather than knowingly
6305 * provided a path. So check whether argv[0] can in fact be resolved
6306 * to a path in the HEAD commit and print a special error if not.
6307 * This hack is for mpi@ <3
6309 static const struct got_error *
6310 tog_log_with_path(int argc, char *argv[])
6312 const struct got_error *error = NULL;
6313 struct tog_cmd *cmd = NULL;
6314 struct got_repository *repo = NULL;
6315 struct got_worktree *worktree = NULL;
6316 struct got_object_id *commit_id = NULL, *id = NULL;
6317 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6318 char *commit_id_str = NULL, **cmd_argv = NULL;
6320 cwd = getcwd(NULL, 0);
6321 if (cwd == NULL)
6322 return got_error_from_errno("getcwd");
6324 error = got_worktree_open(&worktree, cwd);
6325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6326 goto done;
6328 if (worktree)
6329 repo_path = strdup(got_worktree_get_repo_path(worktree));
6330 else
6331 repo_path = strdup(cwd);
6332 if (repo_path == NULL) {
6333 error = got_error_from_errno("strdup");
6334 goto done;
6337 error = got_repo_open(&repo, repo_path, NULL);
6338 if (error != NULL)
6339 goto done;
6341 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6342 repo, worktree);
6343 if (error)
6344 goto done;
6346 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6347 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6348 GOT_OBJ_TYPE_COMMIT, 1, repo);
6349 if (error)
6350 goto done;
6352 if (worktree) {
6353 got_worktree_close(worktree);
6354 worktree = NULL;
6357 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6358 if (error) {
6359 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6360 goto done;
6361 fprintf(stderr, "%s: '%s' is no known command or path\n",
6362 getprogname(), argv[0]);
6363 usage(1, 1);
6364 /* not reached */
6367 got_repo_close(repo);
6368 repo = NULL;
6370 error = got_object_id_str(&commit_id_str, commit_id);
6371 if (error)
6372 goto done;
6374 cmd = &tog_commands[0]; /* log */
6375 argc = 4;
6376 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6377 error = cmd->cmd_main(argc, cmd_argv);
6378 done:
6379 if (repo)
6380 got_repo_close(repo);
6381 if (worktree)
6382 got_worktree_close(worktree);
6383 free(id);
6384 free(commit_id_str);
6385 free(commit_id);
6386 free(cwd);
6387 free(repo_path);
6388 free(in_repo_path);
6389 if (cmd_argv) {
6390 int i;
6391 for (i = 0; i < argc; i++)
6392 free(cmd_argv[i]);
6393 free(cmd_argv);
6395 return error;
6398 int
6399 main(int argc, char *argv[])
6401 const struct got_error *error = NULL;
6402 struct tog_cmd *cmd = NULL;
6403 int ch, hflag = 0, Vflag = 0;
6404 char **cmd_argv = NULL;
6405 static struct option longopts[] = {
6406 { "version", no_argument, NULL, 'V' },
6407 { NULL, 0, NULL, 0}
6410 setlocale(LC_CTYPE, "");
6412 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6413 switch (ch) {
6414 case 'h':
6415 hflag = 1;
6416 break;
6417 case 'V':
6418 Vflag = 1;
6419 break;
6420 default:
6421 usage(hflag, 1);
6422 /* NOTREACHED */
6426 argc -= optind;
6427 argv += optind;
6428 optind = 1;
6429 optreset = 1;
6431 if (Vflag) {
6432 got_version_print_str();
6433 return 0;
6436 #ifndef PROFILE
6437 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6438 NULL) == -1)
6439 err(1, "pledge");
6440 #endif
6442 if (argc == 0) {
6443 if (hflag)
6444 usage(hflag, 0);
6445 /* Build an argument vector which runs a default command. */
6446 cmd = &tog_commands[0];
6447 argc = 1;
6448 cmd_argv = make_argv(argc, cmd->name);
6449 } else {
6450 int i;
6452 /* Did the user specify a command? */
6453 for (i = 0; i < nitems(tog_commands); i++) {
6454 if (strncmp(tog_commands[i].name, argv[0],
6455 strlen(argv[0])) == 0) {
6456 cmd = &tog_commands[i];
6457 break;
6462 if (cmd == NULL) {
6463 if (argc != 1)
6464 usage(0, 1);
6465 /* No command specified; try log with a path */
6466 error = tog_log_with_path(argc, argv);
6467 } else {
6468 if (hflag)
6469 cmd->cmd_usage();
6470 else
6471 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6474 endwin();
6475 putchar('\n');
6476 if (cmd_argv) {
6477 int i;
6478 for (i = 0; i < argc; i++)
6479 free(cmd_argv[i]);
6480 free(cmd_argv);
6483 if (error && error->code != GOT_ERR_CANCELLED)
6484 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6485 return 0;