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 const 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 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
402 };
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
407 int idx;
408 };
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
422 };
424 /*
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
450 int dying;
451 struct tog_view *parent;
452 struct tog_view *child;
454 /*
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
461 * situations.
462 */
463 int focus_child;
465 /* type-specific state */
466 enum tog_view_type type;
467 union {
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
473 } state;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
482 int searching;
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
489 regex_t regex;
490 regmatch_t regmatch;
491 };
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, struct got_repository *);
525 static const struct got_error *show_tree_view(struct tog_view *);
526 static const struct got_error *input_tree_view(struct tog_view **,
527 struct tog_view *, int);
528 static const struct got_error *close_tree_view(struct tog_view *);
529 static const struct got_error *search_start_tree_view(struct tog_view *);
530 static const struct got_error *search_next_tree_view(struct tog_view *);
532 static const struct got_error *open_ref_view(struct tog_view *,
533 struct got_repository *);
534 static const struct got_error *show_ref_view(struct tog_view *);
535 static const struct got_error *input_ref_view(struct tog_view **,
536 struct tog_view *, int);
537 static const struct got_error *close_ref_view(struct tog_view *);
538 static const struct got_error *search_start_ref_view(struct tog_view *);
539 static const struct got_error *search_next_ref_view(struct tog_view *);
541 static volatile sig_atomic_t tog_sigwinch_received;
542 static volatile sig_atomic_t tog_sigpipe_received;
543 static volatile sig_atomic_t tog_sigcont_received;
545 static void
546 tog_sigwinch(int signo)
548 tog_sigwinch_received = 1;
551 static void
552 tog_sigpipe(int signo)
554 tog_sigpipe_received = 1;
557 static void
558 tog_sigcont(int signo)
560 tog_sigcont_received = 1;
563 static const struct got_error *
564 view_close(struct tog_view *view)
566 const struct got_error *err = NULL;
568 if (view->child) {
569 view_close(view->child);
570 view->child = NULL;
572 if (view->close)
573 err = view->close(view);
574 if (view->panel)
575 del_panel(view->panel);
576 if (view->window)
577 delwin(view->window);
578 free(view);
579 return err;
582 static struct tog_view *
583 view_open(int nlines, int ncols, int begin_y, int begin_x,
584 enum tog_view_type type)
586 struct tog_view *view = calloc(1, sizeof(*view));
588 if (view == NULL)
589 return NULL;
591 view->type = type;
592 view->lines = LINES;
593 view->cols = COLS;
594 view->nlines = nlines ? nlines : LINES - begin_y;
595 view->ncols = ncols ? ncols : COLS - begin_x;
596 view->begin_y = begin_y;
597 view->begin_x = begin_x;
598 view->window = newwin(nlines, ncols, begin_y, begin_x);
599 if (view->window == NULL) {
600 view_close(view);
601 return NULL;
603 view->panel = new_panel(view->window);
604 if (view->panel == NULL ||
605 set_panel_userptr(view->panel, view) != OK) {
606 view_close(view);
607 return NULL;
610 keypad(view->window, TRUE);
611 return view;
614 static int
615 view_split_begin_x(int begin_x)
617 if (begin_x > 0 || COLS < 120)
618 return 0;
619 return (COLS - MAX(COLS / 2, 80));
622 static const struct got_error *view_resize(struct tog_view *);
624 static const struct got_error *
625 view_splitscreen(struct tog_view *view)
627 const struct got_error *err = NULL;
629 view->begin_y = 0;
630 view->begin_x = view_split_begin_x(0);
631 view->nlines = LINES;
632 view->ncols = COLS - view->begin_x;
633 view->lines = LINES;
634 view->cols = COLS;
635 err = view_resize(view);
636 if (err)
637 return err;
639 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
640 return got_error_from_errno("mvwin");
642 return NULL;
645 static const struct got_error *
646 view_fullscreen(struct tog_view *view)
648 const struct got_error *err = NULL;
650 view->begin_x = 0;
651 view->begin_y = 0;
652 view->nlines = LINES;
653 view->ncols = COLS;
654 view->lines = LINES;
655 view->cols = COLS;
656 err = view_resize(view);
657 if (err)
658 return err;
660 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
661 return got_error_from_errno("mvwin");
663 return NULL;
666 static int
667 view_is_parent_view(struct tog_view *view)
669 return view->parent == NULL;
672 static const struct got_error *
673 view_resize(struct tog_view *view)
675 int nlines, ncols;
677 if (view->lines > LINES)
678 nlines = view->nlines - (view->lines - LINES);
679 else
680 nlines = view->nlines + (LINES - view->lines);
682 if (view->cols > COLS)
683 ncols = view->ncols - (view->cols - COLS);
684 else
685 ncols = view->ncols + (COLS - view->cols);
687 if (wresize(view->window, nlines, ncols) == ERR)
688 return got_error_from_errno("wresize");
689 if (replace_panel(view->panel, view->window) == ERR)
690 return got_error_from_errno("replace_panel");
691 wclear(view->window);
693 view->nlines = nlines;
694 view->ncols = ncols;
695 view->lines = LINES;
696 view->cols = COLS;
698 if (view->child) {
699 view->child->begin_x = view_split_begin_x(view->begin_x);
700 if (view->child->begin_x == 0) {
701 view_fullscreen(view->child);
702 if (view->child->focussed)
703 show_panel(view->child->panel);
704 else
705 show_panel(view->panel);
706 } else {
707 view_splitscreen(view->child);
708 show_panel(view->child->panel);
712 return NULL;
715 static const struct got_error *
716 view_close_child(struct tog_view *view)
718 const struct got_error *err = NULL;
720 if (view->child == NULL)
721 return NULL;
723 err = view_close(view->child);
724 view->child = NULL;
725 return err;
728 static void
729 view_set_child(struct tog_view *view, struct tog_view *child)
731 view->child = child;
732 child->parent = view;
735 static int
736 view_is_splitscreen(struct tog_view *view)
738 return view->begin_x > 0;
741 static void
742 tog_resizeterm(void)
744 int cols, lines;
745 struct winsize size;
747 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
748 cols = 80; /* Default */
749 lines = 24;
750 } else {
751 cols = size.ws_col;
752 lines = size.ws_row;
754 resize_term(lines, cols);
757 static const struct got_error *
758 view_search_start(struct tog_view *view)
760 const struct got_error *err = NULL;
761 char pattern[1024];
762 int ret;
764 if (view->nlines < 1)
765 return NULL;
767 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
768 wclrtoeol(view->window);
770 nocbreak();
771 echo();
772 ret = wgetnstr(view->window, pattern, sizeof(pattern));
773 cbreak();
774 noecho();
775 if (ret == ERR)
776 return NULL;
778 if (view->searching) {
779 regfree(&view->regex);
780 view->searching = 0;
783 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
784 err = view->search_start(view);
785 if (err) {
786 regfree(&view->regex);
787 return err;
789 view->searching = TOG_SEARCH_FORWARD;
790 view->search_next_done = 0;
791 view->search_next(view);
794 return NULL;
797 static const struct got_error *
798 view_input(struct tog_view **new, int *done, struct tog_view *view,
799 struct tog_view_list_head *views)
801 const struct got_error *err = NULL;
802 struct tog_view *v;
803 int ch, errcode;
805 *new = NULL;
807 /* Clear "no matches" indicator. */
808 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
809 view->search_next_done == TOG_SEARCH_HAVE_NONE)
810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
812 if (view->searching && !view->search_next_done) {
813 errcode = pthread_mutex_unlock(&tog_mutex);
814 if (errcode)
815 return got_error_set_errno(errcode,
816 "pthread_mutex_unlock");
817 pthread_yield();
818 errcode = pthread_mutex_lock(&tog_mutex);
819 if (errcode)
820 return got_error_set_errno(errcode,
821 "pthread_mutex_lock");
822 view->search_next(view);
823 return NULL;
826 nodelay(stdscr, FALSE);
827 /* Allow threads to make progress while we are waiting for input. */
828 errcode = pthread_mutex_unlock(&tog_mutex);
829 if (errcode)
830 return got_error_set_errno(errcode, "pthread_mutex_unlock");
831 ch = wgetch(view->window);
832 errcode = pthread_mutex_lock(&tog_mutex);
833 if (errcode)
834 return got_error_set_errno(errcode, "pthread_mutex_lock");
835 nodelay(stdscr, TRUE);
837 if (tog_sigwinch_received || tog_sigcont_received) {
838 tog_resizeterm();
839 tog_sigwinch_received = 0;
840 tog_sigcont_received = 0;
841 TAILQ_FOREACH(v, views, entry) {
842 err = view_resize(v);
843 if (err)
844 return err;
845 err = v->input(new, v, KEY_RESIZE);
846 if (err)
847 return err;
851 switch (ch) {
852 case ERR:
853 break;
854 case '\t':
855 if (view->child) {
856 view->focussed = 0;
857 view->child->focussed = 1;
858 view->focus_child = 1;
859 } else if (view->parent) {
860 view->focussed = 0;
861 view->parent->focussed = 1;
862 view->parent->focus_child = 0;
864 break;
865 case 'q':
866 err = view->input(new, view, ch);
867 view->dying = 1;
868 break;
869 case 'Q':
870 *done = 1;
871 break;
872 case 'f':
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL)
875 break;
876 if (view_is_splitscreen(view->child)) {
877 view->focussed = 0;
878 view->child->focussed = 1;
879 err = view_fullscreen(view->child);
880 } else
881 err = view_splitscreen(view->child);
882 if (err)
883 break;
884 err = view->child->input(new, view->child,
885 KEY_RESIZE);
886 } else {
887 if (view_is_splitscreen(view)) {
888 view->parent->focussed = 0;
889 view->focussed = 1;
890 err = view_fullscreen(view);
891 } else {
892 err = view_splitscreen(view);
894 if (err)
895 break;
896 err = view->input(new, view, KEY_RESIZE);
898 break;
899 case KEY_RESIZE:
900 break;
901 case '/':
902 if (view->search_start)
903 view_search_start(view);
904 else
905 err = view->input(new, view, ch);
906 break;
907 case 'N':
908 case 'n':
909 if (view->search_next) {
910 view->searching = (ch == 'n' ?
911 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
912 view->search_next_done = 0;
913 view->search_next(view);
914 } else
915 err = view->input(new, view, ch);
916 break;
917 default:
918 err = view->input(new, view, ch);
919 break;
922 return err;
925 void
926 view_vborder(struct tog_view *view)
928 PANEL *panel;
929 struct tog_view *view_above;
931 if (view->parent)
932 return view_vborder(view->parent);
934 panel = panel_above(view->panel);
935 if (panel == NULL)
936 return;
938 view_above = panel_userptr(panel);
939 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
940 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
943 int
944 view_needs_focus_indication(struct tog_view *view)
946 if (view_is_parent_view(view)) {
947 if (view->child == NULL || view->child->focussed)
948 return 0;
949 if (!view_is_splitscreen(view->child))
950 return 0;
951 } else if (!view_is_splitscreen(view))
952 return 0;
954 return view->focussed;
957 static const struct got_error *
958 view_loop(struct tog_view *view)
960 const struct got_error *err = NULL;
961 struct tog_view_list_head views;
962 struct tog_view *new_view, *main_view;
963 int fast_refresh = 10;
964 int done = 0, errcode;
966 errcode = pthread_mutex_lock(&tog_mutex);
967 if (errcode)
968 return got_error_set_errno(errcode, "pthread_mutex_lock");
970 TAILQ_INIT(&views);
971 TAILQ_INSERT_HEAD(&views, view, entry);
973 main_view = view;
974 view->focussed = 1;
975 err = view->show(view);
976 if (err)
977 return err;
978 update_panels();
979 doupdate();
980 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
981 /* Refresh fast during initialization, then become slower. */
982 if (fast_refresh && fast_refresh-- == 0)
983 halfdelay(10); /* switch to once per second */
985 err = view_input(&new_view, &done, view, &views);
986 if (err)
987 break;
988 if (view->dying) {
989 struct tog_view *v, *prev = NULL;
991 if (view_is_parent_view(view))
992 prev = TAILQ_PREV(view, tog_view_list_head,
993 entry);
994 else if (view->parent)
995 prev = view->parent;
997 if (view->parent) {
998 view->parent->child = NULL;
999 view->parent->focus_child = 0;
1000 } else
1001 TAILQ_REMOVE(&views, view, entry);
1003 err = view_close(view);
1004 if (err || (view == main_view && new_view == NULL))
1005 goto done;
1007 view = NULL;
1008 TAILQ_FOREACH(v, &views, entry) {
1009 if (v->focussed)
1010 break;
1012 if (view == NULL && new_view == NULL) {
1013 /* No view has focus. Try to pick one. */
1014 if (prev)
1015 view = prev;
1016 else if (!TAILQ_EMPTY(&views)) {
1017 view = TAILQ_LAST(&views,
1018 tog_view_list_head);
1020 if (view) {
1021 if (view->focus_child) {
1022 view->child->focussed = 1;
1023 view = view->child;
1024 } else
1025 view->focussed = 1;
1029 if (new_view) {
1030 struct tog_view *v, *t;
1031 /* Only allow one parent view per type. */
1032 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1033 if (v->type != new_view->type)
1034 continue;
1035 TAILQ_REMOVE(&views, v, entry);
1036 err = view_close(v);
1037 if (err)
1038 goto done;
1039 break;
1041 TAILQ_INSERT_TAIL(&views, new_view, entry);
1042 view = new_view;
1044 if (view) {
1045 if (view_is_parent_view(view)) {
1046 if (view->child && view->child->focussed)
1047 view = view->child;
1048 } else {
1049 if (view->parent && view->parent->focussed)
1050 view = view->parent;
1052 show_panel(view->panel);
1053 if (view->child && view_is_splitscreen(view->child))
1054 show_panel(view->child->panel);
1055 if (view->parent && view_is_splitscreen(view)) {
1056 err = view->parent->show(view->parent);
1057 if (err)
1058 goto done;
1060 err = view->show(view);
1061 if (err)
1062 goto done;
1063 if (view->child) {
1064 err = view->child->show(view->child);
1065 if (err)
1066 goto done;
1068 update_panels();
1069 doupdate();
1072 done:
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1076 view_close(view);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1083 return err;
1086 __dead static void
1087 usage_log(void)
1089 endwin();
1090 fprintf(stderr,
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 getprogname());
1093 exit(1);
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 char *vis = NULL;
1101 const struct got_error *err = NULL;
1103 *ws = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1106 int vislen;
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1112 if (err)
1113 return err;
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1117 goto done;
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1122 if (*ws == NULL) {
1123 err = got_error_from_errno("calloc");
1124 goto done;
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1129 done:
1130 free(vis);
1131 if (err) {
1132 free(*ws);
1133 *ws = NULL;
1134 *wlen = 0;
1136 return err;
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 int col_tab_align)
1144 const struct got_error *err = NULL;
1145 int cols = 0;
1146 wchar_t *wline = NULL;
1147 size_t wlen;
1148 int i;
1150 *wlinep = NULL;
1151 *widthp = 0;
1153 err = mbs2ws(&wline, &wlen, line);
1154 if (err)
1155 return err;
1157 i = 0;
1158 while (i < wlen) {
1159 int width = wcwidth(wline[i]);
1161 if (width == 0) {
1162 i++;
1163 continue;
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1168 break;
1169 cols += width;
1170 i++;
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1173 width = TABSIZE -
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1176 break;
1177 cols += width;
1179 i++;
1180 } else {
1181 err = got_error_from_errno("wcwidth");
1182 goto done;
1185 wline[i] = L'\0';
1186 if (widthp)
1187 *widthp = cols;
1188 done:
1189 if (err)
1190 free(wline);
1191 else
1192 *wlinep = wline;
1193 return err;
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1202 char *s;
1203 const char *name;
1205 *refs_str = NULL;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1210 int cmp;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1214 continue;
1215 if (strncmp(name, "refs/", 5) == 0)
1216 name += 5;
1217 if (strncmp(name, "got/", 4) == 0)
1218 continue;
1219 if (strncmp(name, "heads/", 6) == 0)
1220 name += 6;
1221 if (strncmp(name, "remotes/", 8) == 0) {
1222 name += 8;
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1225 continue;
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1228 if (err)
1229 break;
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1232 if (err) {
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1234 free(ref_id);
1235 break;
1237 /* Ref points at something other than a tag. */
1238 err = NULL;
1239 tag = NULL;
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1244 free(ref_id);
1245 if (tag)
1246 got_object_tag_close(tag);
1247 if (cmp != 0)
1248 continue;
1249 s = *refs_str;
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1253 free(s);
1254 *refs_str = NULL;
1255 break;
1257 free(s);
1260 return err;
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 int col_tab_align)
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1273 if (at)
1274 *at = '\0';
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1291 int col, limit;
1292 const int avail = view->ncols;
1293 struct tm tm;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 >= sizeof(datebuf))
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1306 else
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(&s->colors, TOG_COLOR_DATE);
1309 if (tc)
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1313 if (tc)
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1316 col = limit;
1317 if (col > avail)
1318 goto done;
1320 if (avail >= 120) {
1321 char *id_str;
1322 err = got_object_id_str(&id_str, id);
1323 if (err)
1324 goto done;
1325 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1326 if (tc)
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1330 if (tc)
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 free(id_str);
1334 col += 9;
1335 if (col > avail)
1336 goto done;
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1342 goto done;
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1345 if (err)
1346 goto done;
1347 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1348 if (tc)
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1352 if (tc)
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1358 col++;
1359 author_width++;
1361 if (col > avail)
1362 goto done;
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1365 if (err)
1366 goto done;
1367 logmsg = logmsg0;
1368 while (*logmsg == '\n')
1369 logmsg++;
1370 newline = strchr(logmsg, '\n');
1371 if (newline)
1372 *newline = '\0';
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1375 if (err)
1376 goto done;
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1381 col++;
1383 done:
1384 free(logmsg0);
1385 free(wlogmsg);
1386 free(author);
1387 free(wauthor);
1388 free(line);
1389 return err;
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1399 if (entry == NULL)
1400 return NULL;
1402 entry->id = id;
1403 entry->commit = commit;
1404 return entry;
1407 static void
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1417 free(entry);
1420 static void
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1435 *have_match = 0;
1437 err = got_object_id_str(&id_str, id);
1438 if (err)
1439 return err;
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1442 if (err)
1443 goto done;
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 &regmatch, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 &regmatch, 0) == 0 ||
1449 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1450 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1451 *have_match = 1;
1452 done:
1453 free(id_str);
1454 free(logmsg);
1455 return err;
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1464 int nqueued = 0;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1476 int errcode;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1480 break;
1482 err = got_object_open_as_commit(&commit, repo, id);
1483 if (err)
1484 break;
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1488 break;
1491 errcode = pthread_mutex_lock(&tog_mutex);
1492 if (errcode) {
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1495 break;
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 nqueued++;
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 int have_match;
1505 err = match_commit(&have_match, id, commit, regex);
1506 if (err)
1507 break;
1508 if (have_match)
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1516 if (err)
1517 break;
1520 return err;
1523 static void
1524 select_commit(struct tog_log_view_state *s)
1526 struct commit_queue_entry *entry;
1527 int ncommits = 0;
1529 entry = s->first_displayed_entry;
1530 while (entry) {
1531 if (ncommits == s->selected) {
1532 s->selected_entry = entry;
1533 break;
1535 entry = TAILQ_NEXT(entry, entry);
1536 ncommits++;
1540 static const struct got_error *
1541 draw_commits(struct tog_view *view)
1543 const struct got_error *err = NULL;
1544 struct tog_log_view_state *s = &view->state.log;
1545 struct commit_queue_entry *entry = s->selected_entry;
1546 const int limit = view->nlines;
1547 int width;
1548 int ncommits, author_cols = 4;
1549 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1550 char *refs_str = NULL;
1551 wchar_t *wline;
1552 struct tog_color *tc;
1553 static const size_t date_display_cols = 12;
1555 if (s->selected_entry &&
1556 !(view->searching && view->search_next_done == 0)) {
1557 err = got_object_id_str(&id_str, s->selected_entry->id);
1558 if (err)
1559 return err;
1560 err = build_refs_str(&refs_str, &s->refs,
1561 s->selected_entry->id, s->repo);
1562 if (err)
1563 goto done;
1566 if (s->thread_args.commits_needed == 0)
1567 halfdelay(10); /* disable fast refresh */
1569 if (s->thread_args.commits_needed > 0) {
1570 if (asprintf(&ncommits_str, " [%d/%d] %s",
1571 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1572 (view->searching && !view->search_next_done) ?
1573 "searching..." : "loading...") == -1) {
1574 err = got_error_from_errno("asprintf");
1575 goto done;
1577 } else {
1578 const char *search_str = NULL;
1580 if (view->searching) {
1581 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1582 search_str = "no more matches";
1583 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1584 search_str = "no matches found";
1585 else if (!view->search_next_done)
1586 search_str = "searching...";
1589 if (asprintf(&ncommits_str, " [%d/%d] %s",
1590 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1591 search_str ? search_str :
1592 (refs_str ? refs_str : "")) == -1) {
1593 err = got_error_from_errno("asprintf");
1594 goto done;
1598 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1599 if (asprintf(&header, "commit %s %s%s",
1600 id_str ? id_str : "........................................",
1601 s->in_repo_path, ncommits_str) == -1) {
1602 err = got_error_from_errno("asprintf");
1603 header = NULL;
1604 goto done;
1606 } else if (asprintf(&header, "commit %s%s",
1607 id_str ? id_str : "........................................",
1608 ncommits_str) == -1) {
1609 err = got_error_from_errno("asprintf");
1610 header = NULL;
1611 goto done;
1613 err = format_line(&wline, &width, header, view->ncols, 0);
1614 if (err)
1615 goto done;
1617 werase(view->window);
1619 if (view_needs_focus_indication(view))
1620 wstandout(view->window);
1621 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1622 if (tc)
1623 wattr_on(view->window,
1624 COLOR_PAIR(tc->colorpair), NULL);
1625 waddwstr(view->window, wline);
1626 if (tc)
1627 wattr_off(view->window,
1628 COLOR_PAIR(tc->colorpair), NULL);
1629 while (width < view->ncols) {
1630 waddch(view->window, ' ');
1631 width++;
1633 if (view_needs_focus_indication(view))
1634 wstandend(view->window);
1635 free(wline);
1636 if (limit <= 1)
1637 goto done;
1639 /* Grow author column size if necessary. */
1640 entry = s->first_displayed_entry;
1641 ncommits = 0;
1642 while (entry) {
1643 char *author;
1644 wchar_t *wauthor;
1645 int width;
1646 if (ncommits >= limit - 1)
1647 break;
1648 author = strdup(got_object_commit_get_author(entry->commit));
1649 if (author == NULL) {
1650 err = got_error_from_errno("strdup");
1651 goto done;
1653 err = format_author(&wauthor, &width, author, COLS,
1654 date_display_cols);
1655 if (author_cols < width)
1656 author_cols = width;
1657 free(wauthor);
1658 free(author);
1659 ncommits++;
1660 entry = TAILQ_NEXT(entry, entry);
1663 entry = s->first_displayed_entry;
1664 s->last_displayed_entry = s->first_displayed_entry;
1665 ncommits = 0;
1666 while (entry) {
1667 if (ncommits >= limit - 1)
1668 break;
1669 if (ncommits == s->selected)
1670 wstandout(view->window);
1671 err = draw_commit(view, entry->commit, entry->id,
1672 date_display_cols, author_cols);
1673 if (ncommits == s->selected)
1674 wstandend(view->window);
1675 if (err)
1676 goto done;
1677 ncommits++;
1678 s->last_displayed_entry = entry;
1679 entry = TAILQ_NEXT(entry, entry);
1682 view_vborder(view);
1683 done:
1684 free(id_str);
1685 free(refs_str);
1686 free(ncommits_str);
1687 free(header);
1688 return err;
1691 static void
1692 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1694 struct commit_queue_entry *entry;
1695 int nscrolled = 0;
1697 entry = TAILQ_FIRST(&s->commits.head);
1698 if (s->first_displayed_entry == entry)
1699 return;
1701 entry = s->first_displayed_entry;
1702 while (entry && nscrolled < maxscroll) {
1703 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1704 if (entry) {
1705 s->first_displayed_entry = entry;
1706 nscrolled++;
1711 static const struct got_error *
1712 trigger_log_thread(struct tog_view *view, int wait)
1714 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1715 int errcode;
1717 halfdelay(1); /* fast refresh while loading commits */
1719 while (ta->commits_needed > 0) {
1720 if (ta->log_complete)
1721 break;
1723 /* Wake the log thread. */
1724 errcode = pthread_cond_signal(&ta->need_commits);
1725 if (errcode)
1726 return got_error_set_errno(errcode,
1727 "pthread_cond_signal");
1730 * The mutex will be released while the view loop waits
1731 * in wgetch(), at which time the log thread will run.
1733 if (!wait)
1734 break;
1736 /* Display progress update in log view. */
1737 show_log_view(view);
1738 update_panels();
1739 doupdate();
1741 /* Wait right here while next commit is being loaded. */
1742 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1743 if (errcode)
1744 return got_error_set_errno(errcode,
1745 "pthread_cond_wait");
1747 /* Display progress update in log view. */
1748 show_log_view(view);
1749 update_panels();
1750 doupdate();
1753 return NULL;
1756 static const struct got_error *
1757 log_scroll_down(struct tog_view *view, int maxscroll)
1759 struct tog_log_view_state *s = &view->state.log;
1760 const struct got_error *err = NULL;
1761 struct commit_queue_entry *pentry;
1762 int nscrolled = 0, ncommits_needed;
1764 if (s->last_displayed_entry == NULL)
1765 return NULL;
1767 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1768 if (s->commits.ncommits < ncommits_needed &&
1769 !s->thread_args.log_complete) {
1771 * Ask the log thread for required amount of commits.
1773 s->thread_args.commits_needed += maxscroll;
1774 err = trigger_log_thread(view, 1);
1775 if (err)
1776 return err;
1779 do {
1780 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1781 if (pentry == NULL)
1782 break;
1784 s->last_displayed_entry = pentry;
1786 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1787 if (pentry == NULL)
1788 break;
1789 s->first_displayed_entry = pentry;
1790 } while (++nscrolled < maxscroll);
1792 return err;
1795 static const struct got_error *
1796 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1797 struct got_commit_object *commit, struct got_object_id *commit_id,
1798 struct tog_view *log_view, struct got_repository *repo)
1800 const struct got_error *err;
1801 struct got_object_qid *parent_id;
1802 struct tog_view *diff_view;
1804 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1805 if (diff_view == NULL)
1806 return got_error_from_errno("view_open");
1808 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1809 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1810 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1811 if (err == NULL)
1812 *new_view = diff_view;
1813 return err;
1816 static const struct got_error *
1817 tree_view_visit_subtree(struct tog_tree_view_state *s,
1818 struct got_tree_object *subtree)
1820 struct tog_parent_tree *parent;
1822 parent = calloc(1, sizeof(*parent));
1823 if (parent == NULL)
1824 return got_error_from_errno("calloc");
1826 parent->tree = s->tree;
1827 parent->first_displayed_entry = s->first_displayed_entry;
1828 parent->selected_entry = s->selected_entry;
1829 parent->selected = s->selected;
1830 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1831 s->tree = subtree;
1832 s->selected = 0;
1833 s->first_displayed_entry = NULL;
1834 return NULL;
1837 static const struct got_error *
1838 tree_view_walk_path(struct tog_tree_view_state *s,
1839 struct got_object_id *commit_id, const char *path)
1841 const struct got_error *err = NULL;
1842 struct got_tree_object *tree = NULL;
1843 const char *p;
1844 char *slash, *subpath = NULL;
1846 /* Walk the path and open corresponding tree objects. */
1847 p = path;
1848 while (*p) {
1849 struct got_tree_entry *te;
1850 struct got_object_id *tree_id;
1851 char *te_name;
1853 while (p[0] == '/')
1854 p++;
1856 /* Ensure the correct subtree entry is selected. */
1857 slash = strchr(p, '/');
1858 if (slash == NULL)
1859 te_name = strdup(p);
1860 else
1861 te_name = strndup(p, slash - p);
1862 if (te_name == NULL) {
1863 err = got_error_from_errno("strndup");
1864 break;
1866 te = got_object_tree_find_entry(s->tree, te_name);
1867 if (te == NULL) {
1868 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 free(te_name);
1870 break;
1872 free(te_name);
1873 s->first_displayed_entry = s->selected_entry = te;
1875 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1876 break; /* jump to this file's entry */
1878 slash = strchr(p, '/');
1879 if (slash)
1880 subpath = strndup(path, slash - path);
1881 else
1882 subpath = strdup(path);
1883 if (subpath == NULL) {
1884 err = got_error_from_errno("strdup");
1885 break;
1888 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1889 subpath);
1890 if (err)
1891 break;
1893 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1894 free(tree_id);
1895 if (err)
1896 break;
1898 err = tree_view_visit_subtree(s, tree);
1899 if (err) {
1900 got_object_tree_close(tree);
1901 break;
1903 if (slash == NULL)
1904 break;
1905 free(subpath);
1906 subpath = NULL;
1907 p = slash;
1910 free(subpath);
1911 return err;
1914 static const struct got_error *
1915 browse_commit_tree(struct tog_view **new_view, int begin_x,
1916 struct commit_queue_entry *entry, const char *path,
1917 struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 struct got_tree_object *tree;
1921 struct tog_tree_view_state *s;
1922 struct tog_view *tree_view;
1924 err = got_object_open_as_tree(&tree, repo,
1925 got_object_commit_get_tree_id(entry->commit));
1926 if (err)
1927 return err;
1929 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1930 if (tree_view == NULL)
1931 return got_error_from_errno("view_open");
1933 err = open_tree_view(tree_view, tree, entry->id, repo);
1934 if (err) {
1935 got_object_tree_close(tree);
1936 return err;
1938 s = &tree_view->state.tree;
1940 *new_view = tree_view;
1942 if (got_path_is_root_dir(path))
1943 return NULL;
1945 return tree_view_walk_path(s, entry->id, path);
1948 static const struct got_error *
1949 block_signals_used_by_main_thread(void)
1951 sigset_t sigset;
1952 int errcode;
1954 if (sigemptyset(&sigset) == -1)
1955 return got_error_from_errno("sigemptyset");
1957 /* tog handles SIGWINCH and SIGCONT */
1958 if (sigaddset(&sigset, SIGWINCH) == -1)
1959 return got_error_from_errno("sigaddset");
1960 if (sigaddset(&sigset, SIGCONT) == -1)
1961 return got_error_from_errno("sigaddset");
1963 /* ncurses handles SIGTSTP */
1964 if (sigaddset(&sigset, SIGTSTP) == -1)
1965 return got_error_from_errno("sigaddset");
1967 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1968 if (errcode)
1969 return got_error_set_errno(errcode, "pthread_sigmask");
1971 return NULL;
1974 static void *
1975 log_thread(void *arg)
1977 const struct got_error *err = NULL;
1978 int errcode = 0;
1979 struct tog_log_thread_args *a = arg;
1980 int done = 0;
1982 err = block_signals_used_by_main_thread();
1983 if (err)
1984 return (void *)err;
1986 while (!done && !err && !tog_sigpipe_received) {
1987 err = queue_commits(a->graph, a->commits, 1, a->repo,
1988 a->in_repo_path, a->searching, a->search_next_done,
1989 a->regex);
1990 if (err) {
1991 if (err->code != GOT_ERR_ITER_COMPLETED)
1992 return (void *)err;
1993 err = NULL;
1994 done = 1;
1995 } else if (a->commits_needed > 0)
1996 a->commits_needed--;
1998 errcode = pthread_mutex_lock(&tog_mutex);
1999 if (errcode) {
2000 err = got_error_set_errno(errcode,
2001 "pthread_mutex_lock");
2002 break;
2003 } else if (*a->quit)
2004 done = 1;
2005 else if (*a->first_displayed_entry == NULL) {
2006 *a->first_displayed_entry =
2007 TAILQ_FIRST(&a->commits->head);
2008 *a->selected_entry = *a->first_displayed_entry;
2011 errcode = pthread_cond_signal(&a->commit_loaded);
2012 if (errcode) {
2013 err = got_error_set_errno(errcode,
2014 "pthread_cond_signal");
2015 pthread_mutex_unlock(&tog_mutex);
2016 break;
2019 if (done)
2020 a->commits_needed = 0;
2021 else {
2022 if (a->commits_needed == 0) {
2023 errcode = pthread_cond_wait(&a->need_commits,
2024 &tog_mutex);
2025 if (errcode)
2026 err = got_error_set_errno(errcode,
2027 "pthread_cond_wait");
2031 errcode = pthread_mutex_unlock(&tog_mutex);
2032 if (errcode && err == NULL)
2033 err = got_error_set_errno(errcode,
2034 "pthread_mutex_unlock");
2036 a->log_complete = 1;
2037 return (void *)err;
2040 static const struct got_error *
2041 stop_log_thread(struct tog_log_view_state *s)
2043 const struct got_error *err = NULL;
2044 int errcode;
2046 if (s->thread) {
2047 s->quit = 1;
2048 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2049 if (errcode)
2050 return got_error_set_errno(errcode,
2051 "pthread_cond_signal");
2052 errcode = pthread_mutex_unlock(&tog_mutex);
2053 if (errcode)
2054 return got_error_set_errno(errcode,
2055 "pthread_mutex_unlock");
2056 errcode = pthread_join(s->thread, (void **)&err);
2057 if (errcode)
2058 return got_error_set_errno(errcode, "pthread_join");
2059 errcode = pthread_mutex_lock(&tog_mutex);
2060 if (errcode)
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_lock");
2063 s->thread = NULL;
2066 if (s->thread_args.repo) {
2067 got_repo_close(s->thread_args.repo);
2068 s->thread_args.repo = NULL;
2071 if (s->thread_args.graph) {
2072 got_commit_graph_close(s->thread_args.graph);
2073 s->thread_args.graph = NULL;
2076 return err;
2079 static const struct got_error *
2080 close_log_view(struct tog_view *view)
2082 const struct got_error *err = NULL;
2083 struct tog_log_view_state *s = &view->state.log;
2084 int errcode;
2086 err = stop_log_thread(s);
2088 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2093 if (errcode && err == NULL)
2094 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2096 free_commits(&s->commits);
2097 free(s->in_repo_path);
2098 s->in_repo_path = NULL;
2099 free(s->start_id);
2100 s->start_id = NULL;
2101 got_ref_list_free(&s->refs);
2102 return err;
2105 static const struct got_error *
2106 search_start_log_view(struct tog_view *view)
2108 struct tog_log_view_state *s = &view->state.log;
2110 s->matched_entry = NULL;
2111 s->search_entry = NULL;
2112 return NULL;
2115 static const struct got_error *
2116 search_next_log_view(struct tog_view *view)
2118 const struct got_error *err = NULL;
2119 struct tog_log_view_state *s = &view->state.log;
2120 struct commit_queue_entry *entry;
2122 /* Display progress update in log view. */
2123 show_log_view(view);
2124 update_panels();
2125 doupdate();
2127 if (s->search_entry) {
2128 int errcode, ch;
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2130 if (errcode)
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_unlock");
2133 ch = wgetch(view->window);
2134 errcode = pthread_mutex_lock(&tog_mutex);
2135 if (errcode)
2136 return got_error_set_errno(errcode,
2137 "pthread_mutex_lock");
2138 if (ch == KEY_BACKSPACE) {
2139 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 return NULL;
2142 if (view->searching == TOG_SEARCH_FORWARD)
2143 entry = TAILQ_NEXT(s->search_entry, entry);
2144 else
2145 entry = TAILQ_PREV(s->search_entry,
2146 commit_queue_head, entry);
2147 } else if (s->matched_entry) {
2148 if (view->searching == TOG_SEARCH_FORWARD)
2149 entry = TAILQ_NEXT(s->matched_entry, entry);
2150 else
2151 entry = TAILQ_PREV(s->matched_entry,
2152 commit_queue_head, entry);
2153 } else {
2154 if (view->searching == TOG_SEARCH_FORWARD)
2155 entry = TAILQ_FIRST(&s->commits.head);
2156 else
2157 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2160 while (1) {
2161 int have_match = 0;
2163 if (entry == NULL) {
2164 if (s->thread_args.log_complete ||
2165 view->searching == TOG_SEARCH_BACKWARD) {
2166 view->search_next_done =
2167 (s->matched_entry == NULL ?
2168 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2169 s->search_entry = NULL;
2170 return NULL;
2173 * Poke the log thread for more commits and return,
2174 * allowing the main loop to make progress. Search
2175 * will resume at s->search_entry once we come back.
2177 s->thread_args.commits_needed++;
2178 return trigger_log_thread(view, 0);
2181 err = match_commit(&have_match, entry->id, entry->commit,
2182 &view->regex);
2183 if (err)
2184 break;
2185 if (have_match) {
2186 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 s->matched_entry = entry;
2188 break;
2191 s->search_entry = entry;
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_NEXT(entry, entry);
2194 else
2195 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2198 if (s->matched_entry) {
2199 int cur = s->selected_entry->idx;
2200 while (cur < s->matched_entry->idx) {
2201 err = input_log_view(NULL, view, KEY_DOWN);
2202 if (err)
2203 return err;
2204 cur++;
2206 while (cur > s->matched_entry->idx) {
2207 err = input_log_view(NULL, view, KEY_UP);
2208 if (err)
2209 return err;
2210 cur--;
2214 s->search_entry = NULL;
2216 return NULL;
2219 static const struct got_error *
2220 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2221 struct got_repository *repo, const char *head_ref_name,
2222 const char *in_repo_path, int log_branches)
2224 const struct got_error *err = NULL;
2225 struct tog_log_view_state *s = &view->state.log;
2226 struct got_repository *thread_repo = NULL;
2227 struct got_commit_graph *thread_graph = NULL;
2228 int errcode;
2230 SIMPLEQ_INIT(&s->refs);
2232 if (in_repo_path != s->in_repo_path) {
2233 free(s->in_repo_path);
2234 s->in_repo_path = strdup(in_repo_path);
2235 if (s->in_repo_path == NULL)
2236 return got_error_from_errno("strdup");
2239 /* The commit queue only contains commits being displayed. */
2240 TAILQ_INIT(&s->commits.head);
2241 s->commits.ncommits = 0;
2243 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2244 if (err)
2245 goto done;
2247 s->repo = repo;
2248 s->head_ref_name = head_ref_name;
2249 s->start_id = got_object_id_dup(start_id);
2250 if (s->start_id == NULL) {
2251 err = got_error_from_errno("got_object_id_dup");
2252 goto done;
2254 s->log_branches = log_branches;
2256 SIMPLEQ_INIT(&s->colors);
2257 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2258 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2259 get_color_value("TOG_COLOR_COMMIT"));
2260 if (err)
2261 goto done;
2262 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2263 get_color_value("TOG_COLOR_AUTHOR"));
2264 if (err) {
2265 free_colors(&s->colors);
2266 goto done;
2268 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2269 get_color_value("TOG_COLOR_DATE"));
2270 if (err) {
2271 free_colors(&s->colors);
2272 goto done;
2276 view->show = show_log_view;
2277 view->input = input_log_view;
2278 view->close = close_log_view;
2279 view->search_start = search_start_log_view;
2280 view->search_next = search_next_log_view;
2282 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2283 if (err)
2284 goto done;
2285 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2286 !s->log_branches);
2287 if (err)
2288 goto done;
2289 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2290 s->repo, NULL, NULL);
2291 if (err)
2292 goto done;
2294 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2295 if (errcode) {
2296 err = got_error_set_errno(errcode, "pthread_cond_init");
2297 goto done;
2299 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2300 if (errcode) {
2301 err = got_error_set_errno(errcode, "pthread_cond_init");
2302 goto done;
2305 s->thread_args.commits_needed = view->nlines;
2306 s->thread_args.graph = thread_graph;
2307 s->thread_args.commits = &s->commits;
2308 s->thread_args.in_repo_path = s->in_repo_path;
2309 s->thread_args.start_id = s->start_id;
2310 s->thread_args.repo = thread_repo;
2311 s->thread_args.log_complete = 0;
2312 s->thread_args.quit = &s->quit;
2313 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2314 s->thread_args.selected_entry = &s->selected_entry;
2315 s->thread_args.searching = &view->searching;
2316 s->thread_args.search_next_done = &view->search_next_done;
2317 s->thread_args.regex = &view->regex;
2318 done:
2319 if (err)
2320 close_log_view(view);
2321 return err;
2324 static const struct got_error *
2325 show_log_view(struct tog_view *view)
2327 const struct got_error *err;
2328 struct tog_log_view_state *s = &view->state.log;
2330 if (s->thread == NULL) {
2331 int errcode = pthread_create(&s->thread, NULL, log_thread,
2332 &s->thread_args);
2333 if (errcode)
2334 return got_error_set_errno(errcode, "pthread_create");
2335 if (s->thread_args.commits_needed > 0) {
2336 err = trigger_log_thread(view, 1);
2337 if (err)
2338 return err;
2342 return draw_commits(view);
2345 static const struct got_error *
2346 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2348 const struct got_error *err = NULL;
2349 struct tog_log_view_state *s = &view->state.log;
2350 char *parent_path, *in_repo_path = NULL;
2351 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2352 struct tog_view *ref_view = NULL;
2353 int begin_x = 0;
2354 struct got_object_id *start_id;
2356 switch (ch) {
2357 case 'q':
2358 s->quit = 1;
2359 break;
2360 case 'k':
2361 case KEY_UP:
2362 case '<':
2363 case ',':
2364 if (s->first_displayed_entry == NULL)
2365 break;
2366 if (s->selected > 0)
2367 s->selected--;
2368 else
2369 log_scroll_up(s, 1);
2370 select_commit(s);
2371 break;
2372 case KEY_PPAGE:
2373 case CTRL('b'):
2374 if (s->first_displayed_entry == NULL)
2375 break;
2376 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2377 s->selected = 0;
2378 else
2379 log_scroll_up(s, view->nlines - 1);
2380 select_commit(s);
2381 break;
2382 case 'j':
2383 case KEY_DOWN:
2384 case '>':
2385 case '.':
2386 if (s->first_displayed_entry == NULL)
2387 break;
2388 if (s->selected < MIN(view->nlines - 2,
2389 s->commits.ncommits - 1))
2390 s->selected++;
2391 else {
2392 err = log_scroll_down(view, 1);
2393 if (err)
2394 break;
2396 select_commit(s);
2397 break;
2398 case KEY_NPAGE:
2399 case CTRL('f'): {
2400 struct commit_queue_entry *first;
2401 first = s->first_displayed_entry;
2402 if (first == NULL)
2403 break;
2404 err = log_scroll_down(view, view->nlines - 1);
2405 if (err)
2406 break;
2407 if (first == s->first_displayed_entry &&
2408 s->selected < MIN(view->nlines - 2,
2409 s->commits.ncommits - 1)) {
2410 /* can't scroll further down */
2411 s->selected = MIN(view->nlines - 2,
2412 s->commits.ncommits - 1);
2414 select_commit(s);
2415 break;
2417 case KEY_RESIZE:
2418 if (s->selected > view->nlines - 2)
2419 s->selected = view->nlines - 2;
2420 if (s->selected > s->commits.ncommits - 1)
2421 s->selected = s->commits.ncommits - 1;
2422 select_commit(s);
2423 if (s->commits.ncommits < view->nlines - 1 &&
2424 !s->thread_args.log_complete) {
2425 s->thread_args.commits_needed += (view->nlines - 1) -
2426 s->commits.ncommits;
2427 err = trigger_log_thread(view, 1);
2429 break;
2430 case KEY_ENTER:
2431 case ' ':
2432 case '\r':
2433 if (s->selected_entry == NULL)
2434 break;
2435 if (view_is_parent_view(view))
2436 begin_x = view_split_begin_x(view->begin_x);
2437 err = open_diff_view_for_commit(&diff_view, begin_x,
2438 s->selected_entry->commit, s->selected_entry->id,
2439 view, s->repo);
2440 if (err)
2441 break;
2442 view->focussed = 0;
2443 diff_view->focussed = 1;
2444 if (view_is_parent_view(view)) {
2445 err = view_close_child(view);
2446 if (err)
2447 return err;
2448 view_set_child(view, diff_view);
2449 view->focus_child = 1;
2450 } else
2451 *new_view = diff_view;
2452 break;
2453 case 't':
2454 if (s->selected_entry == NULL)
2455 break;
2456 if (view_is_parent_view(view))
2457 begin_x = view_split_begin_x(view->begin_x);
2458 err = browse_commit_tree(&tree_view, begin_x,
2459 s->selected_entry, s->in_repo_path, s->repo);
2460 if (err)
2461 break;
2462 view->focussed = 0;
2463 tree_view->focussed = 1;
2464 if (view_is_parent_view(view)) {
2465 err = view_close_child(view);
2466 if (err)
2467 return err;
2468 view_set_child(view, tree_view);
2469 view->focus_child = 1;
2470 } else
2471 *new_view = tree_view;
2472 break;
2473 case KEY_BACKSPACE:
2474 if (got_path_cmp(s->in_repo_path, "/",
2475 strlen(s->in_repo_path), 1) == 0)
2476 break;
2477 err = got_path_dirname(&parent_path, s->in_repo_path);
2478 if (err)
2479 return err;
2480 err = stop_log_thread(s);
2481 if (err) {
2482 free(parent_path);
2483 return err;
2485 lv = view_open(view->nlines, view->ncols,
2486 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2487 if (lv == NULL) {
2488 free(parent_path);
2489 return got_error_from_errno("view_open");
2491 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2492 parent_path, s->log_branches);
2493 free(parent_path);
2494 if (err)
2495 return err;;
2496 view->focussed = 0;
2497 lv->focussed = 1;
2498 if (view_is_parent_view(view))
2499 *new_view = lv;
2500 else
2501 view_set_child(view->parent, lv);
2502 break;
2503 case CTRL('l'):
2504 err = stop_log_thread(s);
2505 if (err)
2506 return err;
2507 lv = view_open(view->nlines, view->ncols,
2508 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2509 if (lv == NULL)
2510 return got_error_from_errno("view_open");
2511 err = got_repo_match_object_id(&start_id, NULL,
2512 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2513 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2514 if (err) {
2515 view_close(lv);
2516 return err;
2518 in_repo_path = strdup(s->in_repo_path);
2519 if (in_repo_path == NULL) {
2520 free(start_id);
2521 view_close(lv);
2522 return got_error_from_errno("strdup");
2524 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2525 in_repo_path, s->log_branches);
2526 if (err) {
2527 free(start_id);
2528 view_close(lv);
2529 return err;;
2531 view->dying = 1;
2532 *new_view = lv;
2533 break;
2534 case 'B':
2535 s->log_branches = !s->log_branches;
2536 err = stop_log_thread(s);
2537 if (err)
2538 return err;
2539 lv = view_open(view->nlines, view->ncols,
2540 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2541 if (lv == NULL)
2542 return got_error_from_errno("view_open");
2543 err = open_log_view(lv, s->start_id, s->repo,
2544 s->head_ref_name, s->in_repo_path, s->log_branches);
2545 if (err) {
2546 view_close(lv);
2547 return err;;
2549 view->dying = 1;
2550 *new_view = lv;
2551 break;
2552 case 'r':
2553 if (view_is_parent_view(view))
2554 begin_x = view_split_begin_x(view->begin_x);
2555 ref_view = view_open(view->nlines, view->ncols,
2556 view->begin_y, begin_x, TOG_VIEW_REF);
2557 if (ref_view == NULL)
2558 return got_error_from_errno("view_open");
2559 err = open_ref_view(ref_view, s->repo);
2560 if (err) {
2561 view_close(ref_view);
2562 return err;
2564 view->focussed = 0;
2565 ref_view->focussed = 1;
2566 if (view_is_parent_view(view)) {
2567 err = view_close_child(view);
2568 if (err)
2569 return err;
2570 view_set_child(view, ref_view);
2571 view->focus_child = 1;
2572 } else
2573 *new_view = ref_view;
2574 break;
2575 default:
2576 break;
2579 return err;
2582 static const struct got_error *
2583 apply_unveil(const char *repo_path, const char *worktree_path)
2585 const struct got_error *error;
2587 #ifdef PROFILE
2588 if (unveil("gmon.out", "rwc") != 0)
2589 return got_error_from_errno2("unveil", "gmon.out");
2590 #endif
2591 if (repo_path && unveil(repo_path, "r") != 0)
2592 return got_error_from_errno2("unveil", repo_path);
2594 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2595 return got_error_from_errno2("unveil", worktree_path);
2597 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2598 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2600 error = got_privsep_unveil_exec_helpers();
2601 if (error != NULL)
2602 return error;
2604 if (unveil(NULL, NULL) != 0)
2605 return got_error_from_errno("unveil");
2607 return NULL;
2610 static void
2611 init_curses(void)
2613 initscr();
2614 cbreak();
2615 halfdelay(1); /* Do fast refresh while initial view is loading. */
2616 noecho();
2617 nonl();
2618 intrflush(stdscr, FALSE);
2619 keypad(stdscr, TRUE);
2620 curs_set(0);
2621 if (getenv("TOG_COLORS") != NULL) {
2622 start_color();
2623 use_default_colors();
2625 signal(SIGWINCH, tog_sigwinch);
2626 signal(SIGPIPE, tog_sigpipe);
2627 signal(SIGCONT, tog_sigcont);
2630 static const struct got_error *
2631 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2632 struct got_repository *repo, struct got_worktree *worktree)
2634 const struct got_error *err = NULL;
2636 if (argc == 0) {
2637 *in_repo_path = strdup("/");
2638 if (*in_repo_path == NULL)
2639 return got_error_from_errno("strdup");
2640 return NULL;
2643 if (worktree) {
2644 const char *prefix = got_worktree_get_path_prefix(worktree);
2645 char *p;
2647 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2648 if (err)
2649 return err;
2650 if (asprintf(in_repo_path, "%s%s%s", prefix,
2651 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2652 p) == -1) {
2653 err = got_error_from_errno("asprintf");
2654 *in_repo_path = NULL;
2656 free(p);
2657 } else
2658 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2660 return err;
2663 static const struct got_error *
2664 cmd_log(int argc, char *argv[])
2666 const struct got_error *error;
2667 struct got_repository *repo = NULL;
2668 struct got_worktree *worktree = NULL;
2669 struct got_object_id *start_id = NULL;
2670 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2671 char *start_commit = NULL, *head_ref_name = NULL;
2672 int ch, log_branches = 0;
2673 struct tog_view *view;
2675 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2676 switch (ch) {
2677 case 'b':
2678 log_branches = 1;
2679 break;
2680 case 'c':
2681 start_commit = optarg;
2682 break;
2683 case 'r':
2684 repo_path = realpath(optarg, NULL);
2685 if (repo_path == NULL)
2686 return got_error_from_errno2("realpath",
2687 optarg);
2688 break;
2689 default:
2690 usage_log();
2691 /* NOTREACHED */
2695 argc -= optind;
2696 argv += optind;
2698 if (argc > 1)
2699 usage_log();
2701 cwd = getcwd(NULL, 0);
2702 if (cwd == NULL)
2703 return got_error_from_errno("getcwd");
2705 error = got_worktree_open(&worktree, cwd);
2706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2707 goto done;
2709 if (repo_path == NULL) {
2710 if (worktree)
2711 repo_path =
2712 strdup(got_worktree_get_repo_path(worktree));
2713 else
2714 repo_path = strdup(cwd);
2716 if (repo_path == NULL) {
2717 error = got_error_from_errno("strdup");
2718 goto done;
2721 error = got_repo_open(&repo, repo_path, NULL);
2722 if (error != NULL)
2723 goto done;
2725 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2726 repo, worktree);
2727 if (error)
2728 goto done;
2730 init_curses();
2732 error = apply_unveil(got_repo_get_path(repo),
2733 worktree ? got_worktree_get_root_path(worktree) : NULL);
2734 if (error)
2735 goto done;
2737 if (start_commit == NULL)
2738 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2739 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2740 GOT_OBJ_TYPE_COMMIT, 1, repo);
2741 else
2742 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2743 GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 if (error != NULL)
2745 goto done;
2747 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2748 if (view == NULL) {
2749 error = got_error_from_errno("view_open");
2750 goto done;
2752 if (worktree) {
2753 head_ref_name = strdup(
2754 got_worktree_get_head_ref_name(worktree));
2755 if (head_ref_name == NULL) {
2756 error = got_error_from_errno("strdup");
2757 goto done;
2760 error = open_log_view(view, start_id, repo, head_ref_name,
2761 in_repo_path, log_branches);
2762 if (error)
2763 goto done;
2764 if (worktree) {
2765 /* Release work tree lock. */
2766 got_worktree_close(worktree);
2767 worktree = NULL;
2769 error = view_loop(view);
2770 done:
2771 free(in_repo_path);
2772 free(repo_path);
2773 free(cwd);
2774 free(start_id);
2775 free(head_ref_name);
2776 if (repo)
2777 got_repo_close(repo);
2778 if (worktree)
2779 got_worktree_close(worktree);
2780 return error;
2783 __dead static void
2784 usage_diff(void)
2786 endwin();
2787 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2788 "[-w] object1 object2\n", getprogname());
2789 exit(1);
2792 static char *
2793 parse_next_line(FILE *f, size_t *len)
2795 char *line;
2796 size_t linelen;
2797 size_t lineno;
2798 const char delim[3] = { '\0', '\0', '\0'};
2800 line = fparseln(f, &linelen, &lineno, delim, 0);
2801 if (len)
2802 *len = linelen;
2803 return line;
2806 static int
2807 match_line(const char *line, regex_t *regex, size_t nmatch,
2808 regmatch_t *regmatch)
2810 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2813 struct tog_color *
2814 match_color(struct tog_colors *colors, const char *line)
2816 struct tog_color *tc = NULL;
2818 SIMPLEQ_FOREACH(tc, colors, entry) {
2819 if (match_line(line, &tc->regex, 0, NULL))
2820 return tc;
2823 return NULL;
2826 static const struct got_error *
2827 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2828 WINDOW *window, regmatch_t *regmatch)
2830 const struct got_error *err = NULL;
2831 wchar_t *wline;
2832 int width;
2833 char *s;
2835 *wtotal = 0;
2837 s = strndup(line, regmatch->rm_so);
2838 if (s == NULL)
2839 return got_error_from_errno("strndup");
2841 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2842 if (err) {
2843 free(s);
2844 return err;
2846 waddwstr(window, wline);
2847 free(wline);
2848 free(s);
2849 wlimit -= width;
2850 *wtotal += width;
2852 if (wlimit > 0) {
2853 s = strndup(line + regmatch->rm_so,
2854 regmatch->rm_eo - regmatch->rm_so);
2855 if (s == NULL) {
2856 err = got_error_from_errno("strndup");
2857 free(s);
2858 return err;
2860 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2861 if (err) {
2862 free(s);
2863 return err;
2865 wattr_on(window, A_STANDOUT, NULL);
2866 waddwstr(window, wline);
2867 wattr_off(window, A_STANDOUT, NULL);
2868 free(wline);
2869 free(s);
2870 wlimit -= width;
2871 *wtotal += width;
2874 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2875 err = format_line(&wline, &width,
2876 line + regmatch->rm_eo, wlimit, col_tab_align);
2877 if (err)
2878 return err;
2879 waddwstr(window, wline);
2880 free(wline);
2881 *wtotal += width;
2884 return NULL;
2887 static const struct got_error *
2888 draw_file(struct tog_view *view, const char *header)
2890 struct tog_diff_view_state *s = &view->state.diff;
2891 regmatch_t *regmatch = &view->regmatch;
2892 const struct got_error *err;
2893 int nprinted = 0;
2894 char *line;
2895 struct tog_color *tc;
2896 size_t len;
2897 wchar_t *wline;
2898 int width;
2899 int max_lines = view->nlines;
2900 int nlines = s->nlines;
2901 off_t line_offset;
2903 line_offset = s->line_offsets[s->first_displayed_line - 1];
2904 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2905 return got_error_from_errno("fseek");
2907 werase(view->window);
2909 if (header) {
2910 if (asprintf(&line, "[%d/%d] %s",
2911 s->first_displayed_line - 1 + s->selected_line, nlines,
2912 header) == -1)
2913 return got_error_from_errno("asprintf");
2914 err = format_line(&wline, &width, line, view->ncols, 0);
2915 free(line);
2916 if (err)
2917 return err;
2919 if (view_needs_focus_indication(view))
2920 wstandout(view->window);
2921 waddwstr(view->window, wline);
2922 free(wline);
2923 wline = NULL;
2924 if (view_needs_focus_indication(view))
2925 wstandend(view->window);
2926 if (width <= view->ncols - 1)
2927 waddch(view->window, '\n');
2929 if (max_lines <= 1)
2930 return NULL;
2931 max_lines--;
2934 s->eof = 0;
2935 while (max_lines > 0 && nprinted < max_lines) {
2936 line = parse_next_line(s->f, &len);
2937 if (line == NULL) {
2938 s->eof = 1;
2939 break;
2942 tc = match_color(&s->colors, line);
2943 if (tc)
2944 wattr_on(view->window,
2945 COLOR_PAIR(tc->colorpair), NULL);
2946 if (s->first_displayed_line + nprinted == s->matched_line &&
2947 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2948 err = add_matched_line(&width, line, view->ncols, 0,
2949 view->window, regmatch);
2950 if (err) {
2951 free(line);
2952 return err;
2954 } else {
2955 err = format_line(&wline, &width, line, view->ncols, 0);
2956 if (err) {
2957 free(line);
2958 return err;
2960 waddwstr(view->window, wline);
2961 free(wline);
2962 wline = NULL;
2964 if (tc)
2965 wattr_off(view->window,
2966 COLOR_PAIR(tc->colorpair), NULL);
2967 if (width <= view->ncols - 1)
2968 waddch(view->window, '\n');
2969 nprinted++;
2970 free(line);
2972 if (nprinted >= 1)
2973 s->last_displayed_line = s->first_displayed_line +
2974 (nprinted - 1);
2975 else
2976 s->last_displayed_line = s->first_displayed_line;
2978 view_vborder(view);
2980 if (s->eof) {
2981 while (nprinted < view->nlines) {
2982 waddch(view->window, '\n');
2983 nprinted++;
2986 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 if (err) {
2988 return err;
2991 wstandout(view->window);
2992 waddwstr(view->window, wline);
2993 free(wline);
2994 wline = NULL;
2995 wstandend(view->window);
2998 return NULL;
3001 static char *
3002 get_datestr(time_t *time, char *datebuf)
3004 struct tm mytm, *tm;
3005 char *p, *s;
3007 tm = gmtime_r(time, &mytm);
3008 if (tm == NULL)
3009 return NULL;
3010 s = asctime_r(tm, datebuf);
3011 if (s == NULL)
3012 return NULL;
3013 p = strchr(s, '\n');
3014 if (p)
3015 *p = '\0';
3016 return s;
3019 static const struct got_error *
3020 get_changed_paths(struct got_pathlist_head *paths,
3021 struct got_commit_object *commit, struct got_repository *repo)
3023 const struct got_error *err = NULL;
3024 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3025 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3026 struct got_object_qid *qid;
3028 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 if (qid != NULL) {
3030 struct got_commit_object *pcommit;
3031 err = got_object_open_as_commit(&pcommit, repo,
3032 qid->id);
3033 if (err)
3034 return err;
3036 tree_id1 = got_object_commit_get_tree_id(pcommit);
3037 got_object_commit_close(pcommit);
3041 if (tree_id1) {
3042 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 if (err)
3044 goto done;
3047 tree_id2 = got_object_commit_get_tree_id(commit);
3048 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 if (err)
3050 goto done;
3052 err = got_diff_tree(tree1, tree2, "", "", repo,
3053 got_diff_tree_collect_changed_paths, paths, 0);
3054 done:
3055 if (tree1)
3056 got_object_tree_close(tree1);
3057 if (tree2)
3058 got_object_tree_close(tree2);
3059 return err;
3062 static const struct got_error *
3063 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3065 off_t *p;
3067 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 if (p == NULL)
3069 return got_error_from_errno("reallocarray");
3070 *line_offsets = p;
3071 (*line_offsets)[*nlines] = off;
3072 (*nlines)++;
3073 return NULL;
3076 static const struct got_error *
3077 write_commit_info(off_t **line_offsets, size_t *nlines,
3078 struct got_object_id *commit_id, struct got_reflist_head *refs,
3079 struct got_repository *repo, FILE *outfile)
3081 const struct got_error *err = NULL;
3082 char datebuf[26], *datestr;
3083 struct got_commit_object *commit;
3084 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3085 time_t committer_time;
3086 const char *author, *committer;
3087 char *refs_str = NULL;
3088 struct got_pathlist_head changed_paths;
3089 struct got_pathlist_entry *pe;
3090 off_t outoff = 0;
3091 int n;
3093 TAILQ_INIT(&changed_paths);
3095 if (refs) {
3096 err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 if (err)
3098 return err;
3101 err = got_object_open_as_commit(&commit, repo, commit_id);
3102 if (err)
3103 return err;
3105 err = got_object_id_str(&id_str, commit_id);
3106 if (err) {
3107 err = got_error_from_errno("got_object_id_str");
3108 goto done;
3111 err = add_line_offset(line_offsets, nlines, 0);
3112 if (err)
3113 goto done;
3115 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3116 refs_str ? refs_str : "", refs_str ? ")" : "");
3117 if (n < 0) {
3118 err = got_error_from_errno("fprintf");
3119 goto done;
3121 outoff += n;
3122 err = add_line_offset(line_offsets, nlines, outoff);
3123 if (err)
3124 goto done;
3126 n = fprintf(outfile, "from: %s\n",
3127 got_object_commit_get_author(commit));
3128 if (n < 0) {
3129 err = got_error_from_errno("fprintf");
3130 goto done;
3132 outoff += n;
3133 err = add_line_offset(line_offsets, nlines, outoff);
3134 if (err)
3135 goto done;
3137 committer_time = got_object_commit_get_committer_time(commit);
3138 datestr = get_datestr(&committer_time, datebuf);
3139 if (datestr) {
3140 n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 if (n < 0) {
3142 err = got_error_from_errno("fprintf");
3143 goto done;
3145 outoff += n;
3146 err = add_line_offset(line_offsets, nlines, outoff);
3147 if (err)
3148 goto done;
3150 author = got_object_commit_get_author(commit);
3151 committer = got_object_commit_get_committer(commit);
3152 if (strcmp(author, committer) != 0) {
3153 n = fprintf(outfile, "via: %s\n", committer);
3154 if (n < 0) {
3155 err = got_error_from_errno("fprintf");
3156 goto done;
3158 outoff += n;
3159 err = add_line_offset(line_offsets, nlines, outoff);
3160 if (err)
3161 goto done;
3163 err = got_object_commit_get_logmsg(&logmsg, commit);
3164 if (err)
3165 goto done;
3166 s = logmsg;
3167 while ((line = strsep(&s, "\n")) != NULL) {
3168 n = fprintf(outfile, "%s\n", line);
3169 if (n < 0) {
3170 err = got_error_from_errno("fprintf");
3171 goto done;
3173 outoff += n;
3174 err = add_line_offset(line_offsets, nlines, outoff);
3175 if (err)
3176 goto done;
3179 err = get_changed_paths(&changed_paths, commit, repo);
3180 if (err)
3181 goto done;
3182 TAILQ_FOREACH(pe, &changed_paths, entry) {
3183 struct got_diff_changed_path *cp = pe->data;
3184 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 if (n < 0) {
3186 err = got_error_from_errno("fprintf");
3187 goto done;
3189 outoff += n;
3190 err = add_line_offset(line_offsets, nlines, outoff);
3191 if (err)
3192 goto done;
3193 free((char *)pe->path);
3194 free(pe->data);
3197 fputc('\n', outfile);
3198 outoff++;
3199 err = add_line_offset(line_offsets, nlines, outoff);
3200 done:
3201 got_pathlist_free(&changed_paths);
3202 free(id_str);
3203 free(logmsg);
3204 free(refs_str);
3205 got_object_commit_close(commit);
3206 if (err) {
3207 free(*line_offsets);
3208 *line_offsets = NULL;
3209 *nlines = 0;
3211 return err;
3214 static const struct got_error *
3215 create_diff(struct tog_diff_view_state *s)
3217 const struct got_error *err = NULL;
3218 FILE *f = NULL;
3219 int obj_type;
3221 free(s->line_offsets);
3222 s->line_offsets = malloc(sizeof(off_t));
3223 if (s->line_offsets == NULL)
3224 return got_error_from_errno("malloc");
3225 s->nlines = 0;
3227 f = got_opentemp();
3228 if (f == NULL) {
3229 err = got_error_from_errno("got_opentemp");
3230 goto done;
3232 if (s->f && fclose(s->f) != 0) {
3233 err = got_error_from_errno("fclose");
3234 goto done;
3236 s->f = f;
3238 if (s->id1)
3239 err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 else
3241 err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 if (err)
3243 goto done;
3245 switch (obj_type) {
3246 case GOT_OBJ_TYPE_BLOB:
3247 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3248 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3249 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 break;
3251 case GOT_OBJ_TYPE_TREE:
3252 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3253 s->id1, s->id2, "", "", s->diff_context,
3254 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 break;
3256 case GOT_OBJ_TYPE_COMMIT: {
3257 const struct got_object_id_queue *parent_ids;
3258 struct got_object_qid *pid;
3259 struct got_commit_object *commit2;
3261 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3262 if (err)
3263 goto done;
3264 /* Show commit info if we're diffing to a parent/root commit. */
3265 if (s->id1 == NULL) {
3266 err = write_commit_info(&s->line_offsets, &s->nlines,
3267 s->id2, &s->refs, s->repo, s->f);
3268 if (err)
3269 goto done;
3270 } else {
3271 parent_ids = got_object_commit_get_parent_ids(commit2);
3272 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3273 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3274 err = write_commit_info(
3275 &s->line_offsets, &s->nlines,
3276 s->id2, &s->refs, s->repo, s->f);
3277 if (err)
3278 goto done;
3279 break;
3283 got_object_commit_close(commit2);
3285 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3286 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3287 s->force_text_diff, s->repo, s->f);
3288 break;
3290 default:
3291 err = got_error(GOT_ERR_OBJ_TYPE);
3292 break;
3294 if (err)
3295 goto done;
3296 done:
3297 if (s->f && fflush(s->f) != 0 && err == NULL)
3298 err = got_error_from_errno("fflush");
3299 return err;
3302 static void
3303 diff_view_indicate_progress(struct tog_view *view)
3305 mvwaddstr(view->window, 0, 0, "diffing...");
3306 update_panels();
3307 doupdate();
3310 static const struct got_error *
3311 search_start_diff_view(struct tog_view *view)
3313 struct tog_diff_view_state *s = &view->state.diff;
3315 s->matched_line = 0;
3316 return NULL;
3319 static const struct got_error *
3320 search_next_diff_view(struct tog_view *view)
3322 struct tog_diff_view_state *s = &view->state.diff;
3323 int lineno;
3325 if (!view->searching) {
3326 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 return NULL;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3333 else
3334 lineno = s->matched_line - 1;
3335 } else {
3336 if (view->searching == TOG_SEARCH_FORWARD)
3337 lineno = 1;
3338 else
3339 lineno = s->nlines;
3342 while (1) {
3343 char *line = NULL;
3344 off_t offset;
3345 size_t len;
3347 if (lineno <= 0 || lineno > s->nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 free(line);
3351 break;
3354 if (view->searching == TOG_SEARCH_FORWARD)
3355 lineno = 1;
3356 else
3357 lineno = s->nlines;
3360 offset = s->line_offsets[lineno - 1];
3361 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 free(line);
3363 return got_error_from_errno("fseeko");
3365 free(line);
3366 line = parse_next_line(s->f, &len);
3367 if (line &&
3368 match_line(line, &view->regex, 1, &view->regmatch)) {
3369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3370 s->matched_line = lineno;
3371 free(line);
3372 break;
3374 free(line);
3375 if (view->searching == TOG_SEARCH_FORWARD)
3376 lineno++;
3377 else
3378 lineno--;
3381 if (s->matched_line) {
3382 s->first_displayed_line = s->matched_line;
3383 s->selected_line = 1;
3386 return NULL;
3389 static const struct got_error *
3390 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3391 struct got_object_id *id2, const char *label1, const char *label2,
3392 int diff_context, int ignore_whitespace, int force_text_diff,
3393 struct tog_view *log_view, struct got_repository *repo)
3395 const struct got_error *err;
3396 struct tog_diff_view_state *s = &view->state.diff;
3398 SIMPLEQ_INIT(&s->refs);
3400 if (id1 != NULL && id2 != NULL) {
3401 int type1, type2;
3402 err = got_object_get_type(&type1, repo, id1);
3403 if (err)
3404 return err;
3405 err = got_object_get_type(&type2, repo, id2);
3406 if (err)
3407 return err;
3409 if (type1 != type2)
3410 return got_error(GOT_ERR_OBJ_TYPE);
3412 s->first_displayed_line = 1;
3413 s->last_displayed_line = view->nlines;
3414 s->selected_line = 1;
3415 s->repo = repo;
3416 s->id1 = id1;
3417 s->id2 = id2;
3418 s->label1 = label1;
3419 s->label2 = label2;
3421 if (id1) {
3422 s->id1 = got_object_id_dup(id1);
3423 if (s->id1 == NULL)
3424 return got_error_from_errno("got_object_id_dup");
3425 } else
3426 s->id1 = NULL;
3428 s->id2 = got_object_id_dup(id2);
3429 if (s->id2 == NULL) {
3430 free(s->id1);
3431 s->id1 = NULL;
3432 return got_error_from_errno("got_object_id_dup");
3434 s->f = NULL;
3435 s->first_displayed_line = 1;
3436 s->last_displayed_line = view->nlines;
3437 s->diff_context = diff_context;
3438 s->ignore_whitespace = ignore_whitespace;
3439 s->force_text_diff = force_text_diff;
3440 s->log_view = log_view;
3441 s->repo = repo;
3443 SIMPLEQ_INIT(&s->colors);
3444 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3445 err = add_color(&s->colors,
3446 "^-", TOG_COLOR_DIFF_MINUS,
3447 get_color_value("TOG_COLOR_DIFF_MINUS"));
3448 if (err)
3449 return err;
3450 err = add_color(&s->colors, "^\\+",
3451 TOG_COLOR_DIFF_PLUS,
3452 get_color_value("TOG_COLOR_DIFF_PLUS"));
3453 if (err) {
3454 free_colors(&s->colors);
3455 return err;
3457 err = add_color(&s->colors,
3458 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3459 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3460 if (err) {
3461 free_colors(&s->colors);
3462 return err;
3465 err = add_color(&s->colors,
3466 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3467 TOG_COLOR_DIFF_META,
3468 get_color_value("TOG_COLOR_DIFF_META"));
3469 if (err) {
3470 free_colors(&s->colors);
3471 return err;
3474 err = add_color(&s->colors,
3475 "^(from|via): ", TOG_COLOR_AUTHOR,
3476 get_color_value("TOG_COLOR_AUTHOR"));
3477 if (err) {
3478 free_colors(&s->colors);
3479 return err;
3482 err = add_color(&s->colors,
3483 "^date: ", TOG_COLOR_DATE,
3484 get_color_value("TOG_COLOR_DATE"));
3485 if (err) {
3486 free_colors(&s->colors);
3487 return err;
3491 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3492 if (err) {
3493 free(s->id1);
3494 s->id1 = NULL;
3495 free(s->id2);
3496 s->id2 = NULL;
3497 free_colors(&s->colors);
3498 return err;
3501 if (log_view && view_is_splitscreen(view))
3502 show_log_view(log_view); /* draw vborder */
3503 diff_view_indicate_progress(view);
3505 s->line_offsets = NULL;
3506 s->nlines = 0;
3507 err = create_diff(s);
3508 if (err) {
3509 free(s->id1);
3510 s->id1 = NULL;
3511 free(s->id2);
3512 s->id2 = NULL;
3513 free_colors(&s->colors);
3514 got_ref_list_free(&s->refs);
3515 return err;
3518 view->show = show_diff_view;
3519 view->input = input_diff_view;
3520 view->close = close_diff_view;
3521 view->search_start = search_start_diff_view;
3522 view->search_next = search_next_diff_view;
3524 return NULL;
3527 static const struct got_error *
3528 close_diff_view(struct tog_view *view)
3530 const struct got_error *err = NULL;
3531 struct tog_diff_view_state *s = &view->state.diff;
3533 free(s->id1);
3534 s->id1 = NULL;
3535 free(s->id2);
3536 s->id2 = NULL;
3537 if (s->f && fclose(s->f) == EOF)
3538 err = got_error_from_errno("fclose");
3539 free_colors(&s->colors);
3540 free(s->line_offsets);
3541 s->line_offsets = NULL;
3542 s->nlines = 0;
3543 got_ref_list_free(&s->refs);
3544 return err;
3547 static const struct got_error *
3548 show_diff_view(struct tog_view *view)
3550 const struct got_error *err;
3551 struct tog_diff_view_state *s = &view->state.diff;
3552 char *id_str1 = NULL, *id_str2, *header;
3553 const char *label1, *label2;
3555 if (s->id1) {
3556 err = got_object_id_str(&id_str1, s->id1);
3557 if (err)
3558 return err;
3559 label1 = s->label1 ? : id_str1;
3560 } else
3561 label1 = "/dev/null";
3563 err = got_object_id_str(&id_str2, s->id2);
3564 if (err)
3565 return err;
3566 label2 = s->label2 ? : id_str2;
3568 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3569 err = got_error_from_errno("asprintf");
3570 free(id_str1);
3571 free(id_str2);
3572 return err;
3574 free(id_str1);
3575 free(id_str2);
3577 return draw_file(view, header);
3580 static const struct got_error *
3581 set_selected_commit(struct tog_diff_view_state *s,
3582 struct commit_queue_entry *entry)
3584 const struct got_error *err;
3585 const struct got_object_id_queue *parent_ids;
3586 struct got_commit_object *selected_commit;
3587 struct got_object_qid *pid;
3589 free(s->id2);
3590 s->id2 = got_object_id_dup(entry->id);
3591 if (s->id2 == NULL)
3592 return got_error_from_errno("got_object_id_dup");
3594 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3595 if (err)
3596 return err;
3597 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3598 free(s->id1);
3599 pid = SIMPLEQ_FIRST(parent_ids);
3600 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3601 got_object_commit_close(selected_commit);
3602 return NULL;
3605 static const struct got_error *
3606 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3608 const struct got_error *err = NULL;
3609 struct tog_diff_view_state *s = &view->state.diff;
3610 struct tog_log_view_state *ls;
3611 int i;
3613 switch (ch) {
3614 case 'a':
3615 case 'w':
3616 if (ch == 'a')
3617 s->force_text_diff = !s->force_text_diff;
3618 if (ch == 'w')
3619 s->ignore_whitespace = !s->ignore_whitespace;
3620 wclear(view->window);
3621 s->first_displayed_line = 1;
3622 s->last_displayed_line = view->nlines;
3623 diff_view_indicate_progress(view);
3624 err = create_diff(s);
3625 break;
3626 case 'k':
3627 case KEY_UP:
3628 if (s->first_displayed_line > 1)
3629 s->first_displayed_line--;
3630 break;
3631 case KEY_PPAGE:
3632 case CTRL('b'):
3633 if (s->first_displayed_line == 1)
3634 break;
3635 i = 0;
3636 while (i++ < view->nlines - 1 &&
3637 s->first_displayed_line > 1)
3638 s->first_displayed_line--;
3639 break;
3640 case 'j':
3641 case KEY_DOWN:
3642 if (!s->eof)
3643 s->first_displayed_line++;
3644 break;
3645 case KEY_NPAGE:
3646 case CTRL('f'):
3647 case ' ':
3648 if (s->eof)
3649 break;
3650 i = 0;
3651 while (!s->eof && i++ < view->nlines - 1) {
3652 char *line;
3653 line = parse_next_line(s->f, NULL);
3654 s->first_displayed_line++;
3655 if (line == NULL)
3656 break;
3658 break;
3659 case '[':
3660 if (s->diff_context > 0) {
3661 s->diff_context--;
3662 diff_view_indicate_progress(view);
3663 err = create_diff(s);
3664 if (s->first_displayed_line + view->nlines - 1 >
3665 s->nlines) {
3666 s->first_displayed_line = 1;
3667 s->last_displayed_line = view->nlines;
3670 break;
3671 case ']':
3672 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3673 s->diff_context++;
3674 diff_view_indicate_progress(view);
3675 err = create_diff(s);
3677 break;
3678 case '<':
3679 case ',':
3680 if (s->log_view == NULL)
3681 break;
3682 ls = &s->log_view->state.log;
3684 err = input_log_view(NULL, s->log_view, KEY_UP);
3685 if (err)
3686 break;
3688 err = set_selected_commit(s, ls->selected_entry);
3689 if (err)
3690 break;
3692 s->first_displayed_line = 1;
3693 s->last_displayed_line = view->nlines;
3695 diff_view_indicate_progress(view);
3696 err = create_diff(s);
3697 break;
3698 case '>':
3699 case '.':
3700 if (s->log_view == NULL)
3701 break;
3702 ls = &s->log_view->state.log;
3704 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3705 if (err)
3706 break;
3708 err = set_selected_commit(s, ls->selected_entry);
3709 if (err)
3710 break;
3712 s->first_displayed_line = 1;
3713 s->last_displayed_line = view->nlines;
3715 diff_view_indicate_progress(view);
3716 err = create_diff(s);
3717 break;
3718 default:
3719 break;
3722 return err;
3725 static const struct got_error *
3726 cmd_diff(int argc, char *argv[])
3728 const struct got_error *error = NULL;
3729 struct got_repository *repo = NULL;
3730 struct got_worktree *worktree = NULL;
3731 struct got_object_id *id1 = NULL, *id2 = NULL;
3732 char *repo_path = NULL, *cwd = NULL;
3733 char *id_str1 = NULL, *id_str2 = NULL;
3734 char *label1 = NULL, *label2 = NULL;
3735 int diff_context = 3, ignore_whitespace = 0;
3736 int ch, force_text_diff = 0;
3737 const char *errstr;
3738 struct tog_view *view;
3740 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3741 switch (ch) {
3742 case 'a':
3743 force_text_diff = 1;
3744 break;
3745 case 'C':
3746 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3747 &errstr);
3748 if (errstr != NULL)
3749 err(1, "-C option %s", errstr);
3750 break;
3751 case 'r':
3752 repo_path = realpath(optarg, NULL);
3753 if (repo_path == NULL)
3754 return got_error_from_errno2("realpath",
3755 optarg);
3756 got_path_strip_trailing_slashes(repo_path);
3757 break;
3758 case 'w':
3759 ignore_whitespace = 1;
3760 break;
3761 default:
3762 usage_diff();
3763 /* NOTREACHED */
3767 argc -= optind;
3768 argv += optind;
3770 if (argc == 0) {
3771 usage_diff(); /* TODO show local worktree changes */
3772 } else if (argc == 2) {
3773 id_str1 = argv[0];
3774 id_str2 = argv[1];
3775 } else
3776 usage_diff();
3778 cwd = getcwd(NULL, 0);
3779 if (cwd == NULL)
3780 return got_error_from_errno("getcwd");
3782 error = got_worktree_open(&worktree, cwd);
3783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3784 goto done;
3786 if (repo_path == NULL) {
3787 if (worktree)
3788 repo_path =
3789 strdup(got_worktree_get_repo_path(worktree));
3790 else
3791 repo_path = strdup(cwd);
3793 if (repo_path == NULL) {
3794 error = got_error_from_errno("strdup");
3795 goto done;
3798 error = got_repo_open(&repo, repo_path, NULL);
3799 if (error)
3800 goto done;
3802 init_curses();
3804 error = apply_unveil(got_repo_get_path(repo), NULL);
3805 if (error)
3806 goto done;
3808 error = got_repo_match_object_id(&id1, &label1, id_str1,
3809 GOT_OBJ_TYPE_ANY, 1, repo);
3810 if (error)
3811 goto done;
3813 error = got_repo_match_object_id(&id2, &label2, id_str2,
3814 GOT_OBJ_TYPE_ANY, 1, repo);
3815 if (error)
3816 goto done;
3818 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3819 if (view == NULL) {
3820 error = got_error_from_errno("view_open");
3821 goto done;
3823 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3824 ignore_whitespace, force_text_diff, NULL, repo);
3825 if (error)
3826 goto done;
3827 error = view_loop(view);
3828 done:
3829 free(label1);
3830 free(label2);
3831 free(repo_path);
3832 free(cwd);
3833 if (repo)
3834 got_repo_close(repo);
3835 if (worktree)
3836 got_worktree_close(worktree);
3837 return error;
3840 __dead static void
3841 usage_blame(void)
3843 endwin();
3844 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3845 getprogname());
3846 exit(1);
3849 struct tog_blame_line {
3850 int annotated;
3851 struct got_object_id *id;
3854 static const struct got_error *
3855 draw_blame(struct tog_view *view)
3857 struct tog_blame_view_state *s = &view->state.blame;
3858 struct tog_blame *blame = &s->blame;
3859 regmatch_t *regmatch = &view->regmatch;
3860 const struct got_error *err;
3861 int lineno = 0, nprinted = 0;
3862 char *line;
3863 size_t len;
3864 wchar_t *wline;
3865 int width;
3866 struct tog_blame_line *blame_line;
3867 struct got_object_id *prev_id = NULL;
3868 char *id_str;
3869 struct tog_color *tc;
3871 err = got_object_id_str(&id_str, s->blamed_commit->id);
3872 if (err)
3873 return err;
3875 rewind(blame->f);
3876 werase(view->window);
3878 if (asprintf(&line, "commit %s", id_str) == -1) {
3879 err = got_error_from_errno("asprintf");
3880 free(id_str);
3881 return err;
3884 err = format_line(&wline, &width, line, view->ncols, 0);
3885 free(line);
3886 line = NULL;
3887 if (err)
3888 return err;
3889 if (view_needs_focus_indication(view))
3890 wstandout(view->window);
3891 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3892 if (tc)
3893 wattr_on(view->window,
3894 COLOR_PAIR(tc->colorpair), NULL);
3895 waddwstr(view->window, wline);
3896 if (tc)
3897 wattr_off(view->window,
3898 COLOR_PAIR(tc->colorpair), NULL);
3899 if (view_needs_focus_indication(view))
3900 wstandend(view->window);
3901 free(wline);
3902 wline = NULL;
3903 if (width < view->ncols - 1)
3904 waddch(view->window, '\n');
3906 if (asprintf(&line, "[%d/%d] %s%s",
3907 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3908 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3909 free(id_str);
3910 return got_error_from_errno("asprintf");
3912 free(id_str);
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3914 free(line);
3915 line = NULL;
3916 if (err)
3917 return err;
3918 waddwstr(view->window, wline);
3919 free(wline);
3920 wline = NULL;
3921 if (width < view->ncols - 1)
3922 waddch(view->window, '\n');
3924 s->eof = 0;
3925 while (nprinted < view->nlines - 2) {
3926 line = parse_next_line(blame->f, &len);
3927 if (line == NULL) {
3928 s->eof = 1;
3929 break;
3931 if (++lineno < s->first_displayed_line) {
3932 free(line);
3933 continue;
3936 if (view->focussed && nprinted == s->selected_line - 1)
3937 wstandout(view->window);
3939 if (blame->nlines > 0) {
3940 blame_line = &blame->lines[lineno - 1];
3941 if (blame_line->annotated && prev_id &&
3942 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3943 !(view->focussed &&
3944 nprinted == s->selected_line - 1)) {
3945 waddstr(view->window, " ");
3946 } else if (blame_line->annotated) {
3947 char *id_str;
3948 err = got_object_id_str(&id_str, blame_line->id);
3949 if (err) {
3950 free(line);
3951 return err;
3953 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3954 if (tc)
3955 wattr_on(view->window,
3956 COLOR_PAIR(tc->colorpair), NULL);
3957 wprintw(view->window, "%.8s", id_str);
3958 if (tc)
3959 wattr_off(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3961 free(id_str);
3962 prev_id = blame_line->id;
3963 } else {
3964 waddstr(view->window, "........");
3965 prev_id = NULL;
3967 } else {
3968 waddstr(view->window, "........");
3969 prev_id = NULL;
3972 if (view->focussed && nprinted == s->selected_line - 1)
3973 wstandend(view->window);
3974 waddstr(view->window, " ");
3976 if (view->ncols <= 9) {
3977 width = 9;
3978 wline = wcsdup(L"");
3979 if (wline == NULL) {
3980 err = got_error_from_errno("wcsdup");
3981 free(line);
3982 return err;
3984 } else if (s->first_displayed_line + nprinted ==
3985 s->matched_line &&
3986 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3987 err = add_matched_line(&width, line, view->ncols - 9, 9,
3988 view->window, regmatch);
3989 if (err) {
3990 free(line);
3991 return err;
3993 width += 9;
3994 } else {
3995 err = format_line(&wline, &width, line,
3996 view->ncols - 9, 9);
3997 waddwstr(view->window, wline);
3998 free(wline);
3999 wline = NULL;
4000 width += 9;
4003 if (width <= view->ncols - 1)
4004 waddch(view->window, '\n');
4005 if (++nprinted == 1)
4006 s->first_displayed_line = lineno;
4007 free(line);
4009 s->last_displayed_line = lineno;
4011 view_vborder(view);
4013 return NULL;
4016 static const struct got_error *
4017 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4019 const struct got_error *err = NULL;
4020 struct tog_blame_cb_args *a = arg;
4021 struct tog_blame_line *line;
4022 int errcode;
4024 if (nlines != a->nlines ||
4025 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4026 return got_error(GOT_ERR_RANGE);
4028 errcode = pthread_mutex_lock(&tog_mutex);
4029 if (errcode)
4030 return got_error_set_errno(errcode, "pthread_mutex_lock");
4032 if (*a->quit) { /* user has quit the blame view */
4033 err = got_error(GOT_ERR_ITER_COMPLETED);
4034 goto done;
4037 if (lineno == -1)
4038 goto done; /* no change in this commit */
4040 line = &a->lines[lineno - 1];
4041 if (line->annotated)
4042 goto done;
4044 line->id = got_object_id_dup(id);
4045 if (line->id == NULL) {
4046 err = got_error_from_errno("got_object_id_dup");
4047 goto done;
4049 line->annotated = 1;
4050 done:
4051 errcode = pthread_mutex_unlock(&tog_mutex);
4052 if (errcode)
4053 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4054 return err;
4057 static void *
4058 blame_thread(void *arg)
4060 const struct got_error *err;
4061 struct tog_blame_thread_args *ta = arg;
4062 struct tog_blame_cb_args *a = ta->cb_args;
4063 int errcode;
4065 err = block_signals_used_by_main_thread();
4066 if (err)
4067 return (void *)err;
4069 err = got_blame(ta->path, a->commit_id, ta->repo,
4070 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4071 if (err && err->code == GOT_ERR_CANCELLED)
4072 err = NULL;
4074 errcode = pthread_mutex_lock(&tog_mutex);
4075 if (errcode)
4076 return (void *)got_error_set_errno(errcode,
4077 "pthread_mutex_lock");
4079 got_repo_close(ta->repo);
4080 ta->repo = NULL;
4081 *ta->complete = 1;
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4084 if (errcode && err == NULL)
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 return (void *)err;
4090 static struct got_object_id *
4091 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4092 int first_displayed_line, int selected_line)
4094 struct tog_blame_line *line;
4096 if (nlines <= 0)
4097 return NULL;
4099 line = &lines[first_displayed_line - 1 + selected_line - 1];
4100 if (!line->annotated)
4101 return NULL;
4103 return line->id;
4106 static const struct got_error *
4107 stop_blame(struct tog_blame *blame)
4109 const struct got_error *err = NULL;
4110 int i;
4112 if (blame->thread) {
4113 int errcode;
4114 errcode = pthread_mutex_unlock(&tog_mutex);
4115 if (errcode)
4116 return got_error_set_errno(errcode,
4117 "pthread_mutex_unlock");
4118 errcode = pthread_join(blame->thread, (void **)&err);
4119 if (errcode)
4120 return got_error_set_errno(errcode, "pthread_join");
4121 errcode = pthread_mutex_lock(&tog_mutex);
4122 if (errcode)
4123 return got_error_set_errno(errcode,
4124 "pthread_mutex_lock");
4125 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4126 err = NULL;
4127 blame->thread = NULL;
4129 if (blame->thread_args.repo) {
4130 got_repo_close(blame->thread_args.repo);
4131 blame->thread_args.repo = NULL;
4133 if (blame->f) {
4134 if (fclose(blame->f) != 0 && err == NULL)
4135 err = got_error_from_errno("fclose");
4136 blame->f = NULL;
4138 if (blame->lines) {
4139 for (i = 0; i < blame->nlines; i++)
4140 free(blame->lines[i].id);
4141 free(blame->lines);
4142 blame->lines = NULL;
4144 free(blame->cb_args.commit_id);
4145 blame->cb_args.commit_id = NULL;
4147 return err;
4150 static const struct got_error *
4151 cancel_blame_view(void *arg)
4153 const struct got_error *err = NULL;
4154 int *done = arg;
4155 int errcode;
4157 errcode = pthread_mutex_lock(&tog_mutex);
4158 if (errcode)
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_unlock");
4162 if (*done)
4163 err = got_error(GOT_ERR_CANCELLED);
4165 errcode = pthread_mutex_unlock(&tog_mutex);
4166 if (errcode)
4167 return got_error_set_errno(errcode,
4168 "pthread_mutex_lock");
4170 return err;
4173 static const struct got_error *
4174 run_blame(struct tog_view *view)
4176 struct tog_blame_view_state *s = &view->state.blame;
4177 struct tog_blame *blame = &s->blame;
4178 const struct got_error *err = NULL;
4179 struct got_blob_object *blob = NULL;
4180 struct got_repository *thread_repo = NULL;
4181 struct got_object_id *obj_id = NULL;
4182 int obj_type;
4184 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4185 s->path);
4186 if (err)
4187 return err;
4189 err = got_object_get_type(&obj_type, s->repo, obj_id);
4190 if (err)
4191 goto done;
4193 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4194 err = got_error(GOT_ERR_OBJ_TYPE);
4195 goto done;
4198 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4199 if (err)
4200 goto done;
4201 blame->f = got_opentemp();
4202 if (blame->f == NULL) {
4203 err = got_error_from_errno("got_opentemp");
4204 goto done;
4206 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4207 &blame->line_offsets, blame->f, blob);
4208 if (err || blame->nlines == 0)
4209 goto done;
4211 /* Don't include \n at EOF in the blame line count. */
4212 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4213 blame->nlines--;
4215 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4216 if (blame->lines == NULL) {
4217 err = got_error_from_errno("calloc");
4218 goto done;
4221 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4222 if (err)
4223 goto done;
4225 blame->cb_args.view = view;
4226 blame->cb_args.lines = blame->lines;
4227 blame->cb_args.nlines = blame->nlines;
4228 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4229 if (blame->cb_args.commit_id == NULL) {
4230 err = got_error_from_errno("got_object_id_dup");
4231 goto done;
4233 blame->cb_args.quit = &s->done;
4235 blame->thread_args.path = s->path;
4236 blame->thread_args.repo = thread_repo;
4237 blame->thread_args.cb_args = &blame->cb_args;
4238 blame->thread_args.complete = &s->blame_complete;
4239 blame->thread_args.cancel_cb = cancel_blame_view;
4240 blame->thread_args.cancel_arg = &s->done;
4241 s->blame_complete = 0;
4243 done:
4244 if (blob)
4245 got_object_blob_close(blob);
4246 free(obj_id);
4247 if (err)
4248 stop_blame(blame);
4249 return err;
4252 static const struct got_error *
4253 open_blame_view(struct tog_view *view, char *path,
4254 struct got_object_id *commit_id, struct got_repository *repo)
4256 const struct got_error *err = NULL;
4257 struct tog_blame_view_state *s = &view->state.blame;
4259 SIMPLEQ_INIT(&s->blamed_commits);
4261 s->path = strdup(path);
4262 if (s->path == NULL)
4263 return got_error_from_errno("strdup");
4265 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4266 if (err) {
4267 free(s->path);
4268 return err;
4271 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4272 s->first_displayed_line = 1;
4273 s->last_displayed_line = view->nlines;
4274 s->selected_line = 1;
4275 s->blame_complete = 0;
4276 s->repo = repo;
4277 s->commit_id = commit_id;
4278 memset(&s->blame, 0, sizeof(s->blame));
4280 SIMPLEQ_INIT(&s->colors);
4281 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4282 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4283 get_color_value("TOG_COLOR_COMMIT"));
4284 if (err)
4285 return err;
4288 view->show = show_blame_view;
4289 view->input = input_blame_view;
4290 view->close = close_blame_view;
4291 view->search_start = search_start_blame_view;
4292 view->search_next = search_next_blame_view;
4294 return run_blame(view);
4297 static const struct got_error *
4298 close_blame_view(struct tog_view *view)
4300 const struct got_error *err = NULL;
4301 struct tog_blame_view_state *s = &view->state.blame;
4303 if (s->blame.thread)
4304 err = stop_blame(&s->blame);
4306 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4307 struct got_object_qid *blamed_commit;
4308 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4309 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4310 got_object_qid_free(blamed_commit);
4313 free(s->path);
4314 free_colors(&s->colors);
4316 return err;
4319 static const struct got_error *
4320 search_start_blame_view(struct tog_view *view)
4322 struct tog_blame_view_state *s = &view->state.blame;
4324 s->matched_line = 0;
4325 return NULL;
4328 static const struct got_error *
4329 search_next_blame_view(struct tog_view *view)
4331 struct tog_blame_view_state *s = &view->state.blame;
4332 int lineno;
4334 if (!view->searching) {
4335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4336 return NULL;
4339 if (s->matched_line) {
4340 if (view->searching == TOG_SEARCH_FORWARD)
4341 lineno = s->matched_line + 1;
4342 else
4343 lineno = s->matched_line - 1;
4344 } else {
4345 if (view->searching == TOG_SEARCH_FORWARD)
4346 lineno = 1;
4347 else
4348 lineno = s->blame.nlines;
4351 while (1) {
4352 char *line = NULL;
4353 off_t offset;
4354 size_t len;
4356 if (lineno <= 0 || lineno > s->blame.nlines) {
4357 if (s->matched_line == 0) {
4358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4359 free(line);
4360 break;
4363 if (view->searching == TOG_SEARCH_FORWARD)
4364 lineno = 1;
4365 else
4366 lineno = s->blame.nlines;
4369 offset = s->blame.line_offsets[lineno - 1];
4370 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4371 free(line);
4372 return got_error_from_errno("fseeko");
4374 free(line);
4375 line = parse_next_line(s->blame.f, &len);
4376 if (line &&
4377 match_line(line, &view->regex, 1, &view->regmatch)) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 s->matched_line = lineno;
4380 free(line);
4381 break;
4383 free(line);
4384 if (view->searching == TOG_SEARCH_FORWARD)
4385 lineno++;
4386 else
4387 lineno--;
4390 if (s->matched_line) {
4391 s->first_displayed_line = s->matched_line;
4392 s->selected_line = 1;
4395 return NULL;
4398 static const struct got_error *
4399 show_blame_view(struct tog_view *view)
4401 const struct got_error *err = NULL;
4402 struct tog_blame_view_state *s = &view->state.blame;
4403 int errcode;
4405 if (s->blame.thread == NULL) {
4406 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4407 &s->blame.thread_args);
4408 if (errcode)
4409 return got_error_set_errno(errcode, "pthread_create");
4411 halfdelay(1); /* fast refresh while annotating */
4414 if (s->blame_complete)
4415 halfdelay(10); /* disable fast refresh */
4417 err = draw_blame(view);
4419 view_vborder(view);
4420 return err;
4423 static const struct got_error *
4424 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4426 const struct got_error *err = NULL, *thread_err = NULL;
4427 struct tog_view *diff_view;
4428 struct tog_blame_view_state *s = &view->state.blame;
4429 int begin_x = 0;
4431 switch (ch) {
4432 case 'q':
4433 s->done = 1;
4434 break;
4435 case 'k':
4436 case KEY_UP:
4437 if (s->selected_line > 1)
4438 s->selected_line--;
4439 else if (s->selected_line == 1 &&
4440 s->first_displayed_line > 1)
4441 s->first_displayed_line--;
4442 break;
4443 case KEY_PPAGE:
4444 case CTRL('b'):
4445 if (s->first_displayed_line == 1) {
4446 s->selected_line = 1;
4447 break;
4449 if (s->first_displayed_line > view->nlines - 2)
4450 s->first_displayed_line -=
4451 (view->nlines - 2);
4452 else
4453 s->first_displayed_line = 1;
4454 break;
4455 case 'j':
4456 case KEY_DOWN:
4457 if (s->selected_line < view->nlines - 2 &&
4458 s->first_displayed_line +
4459 s->selected_line <= s->blame.nlines)
4460 s->selected_line++;
4461 else if (s->last_displayed_line <
4462 s->blame.nlines)
4463 s->first_displayed_line++;
4464 break;
4465 case 'b':
4466 case 'p': {
4467 struct got_object_id *id = NULL;
4468 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4469 s->first_displayed_line, s->selected_line);
4470 if (id == NULL)
4471 break;
4472 if (ch == 'p') {
4473 struct got_commit_object *commit;
4474 struct got_object_qid *pid;
4475 struct got_object_id *blob_id = NULL;
4476 int obj_type;
4477 err = got_object_open_as_commit(&commit,
4478 s->repo, id);
4479 if (err)
4480 break;
4481 pid = SIMPLEQ_FIRST(
4482 got_object_commit_get_parent_ids(commit));
4483 if (pid == NULL) {
4484 got_object_commit_close(commit);
4485 break;
4487 /* Check if path history ends here. */
4488 err = got_object_id_by_path(&blob_id, s->repo,
4489 pid->id, s->path);
4490 if (err) {
4491 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4492 err = NULL;
4493 got_object_commit_close(commit);
4494 break;
4496 err = got_object_get_type(&obj_type, s->repo,
4497 blob_id);
4498 free(blob_id);
4499 /* Can't blame non-blob type objects. */
4500 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4501 got_object_commit_close(commit);
4502 break;
4504 err = got_object_qid_alloc(&s->blamed_commit,
4505 pid->id);
4506 got_object_commit_close(commit);
4507 } else {
4508 if (got_object_id_cmp(id,
4509 s->blamed_commit->id) == 0)
4510 break;
4511 err = got_object_qid_alloc(&s->blamed_commit,
4512 id);
4514 if (err)
4515 break;
4516 s->done = 1;
4517 thread_err = stop_blame(&s->blame);
4518 s->done = 0;
4519 if (thread_err)
4520 break;
4521 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4522 s->blamed_commit, entry);
4523 err = run_blame(view);
4524 if (err)
4525 break;
4526 break;
4528 case 'B': {
4529 struct got_object_qid *first;
4530 first = SIMPLEQ_FIRST(&s->blamed_commits);
4531 if (!got_object_id_cmp(first->id, s->commit_id))
4532 break;
4533 s->done = 1;
4534 thread_err = stop_blame(&s->blame);
4535 s->done = 0;
4536 if (thread_err)
4537 break;
4538 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4539 got_object_qid_free(s->blamed_commit);
4540 s->blamed_commit =
4541 SIMPLEQ_FIRST(&s->blamed_commits);
4542 err = run_blame(view);
4543 if (err)
4544 break;
4545 break;
4547 case KEY_ENTER:
4548 case '\r': {
4549 struct got_object_id *id = NULL;
4550 struct got_object_qid *pid;
4551 struct got_commit_object *commit = NULL;
4552 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4553 s->first_displayed_line, s->selected_line);
4554 if (id == NULL)
4555 break;
4556 err = got_object_open_as_commit(&commit, s->repo, id);
4557 if (err)
4558 break;
4559 pid = SIMPLEQ_FIRST(
4560 got_object_commit_get_parent_ids(commit));
4561 if (view_is_parent_view(view))
4562 begin_x = view_split_begin_x(view->begin_x);
4563 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4564 if (diff_view == NULL) {
4565 got_object_commit_close(commit);
4566 err = got_error_from_errno("view_open");
4567 break;
4569 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4570 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4571 got_object_commit_close(commit);
4572 if (err) {
4573 view_close(diff_view);
4574 break;
4576 view->focussed = 0;
4577 diff_view->focussed = 1;
4578 if (view_is_parent_view(view)) {
4579 err = view_close_child(view);
4580 if (err)
4581 break;
4582 view_set_child(view, diff_view);
4583 view->focus_child = 1;
4584 } else
4585 *new_view = diff_view;
4586 if (err)
4587 break;
4588 break;
4590 case KEY_NPAGE:
4591 case CTRL('f'):
4592 case ' ':
4593 if (s->last_displayed_line >= s->blame.nlines &&
4594 s->selected_line >= MIN(s->blame.nlines,
4595 view->nlines - 2)) {
4596 break;
4598 if (s->last_displayed_line >= s->blame.nlines &&
4599 s->selected_line < view->nlines - 2) {
4600 s->selected_line = MIN(s->blame.nlines,
4601 view->nlines - 2);
4602 break;
4604 if (s->last_displayed_line + view->nlines - 2
4605 <= s->blame.nlines)
4606 s->first_displayed_line +=
4607 view->nlines - 2;
4608 else
4609 s->first_displayed_line =
4610 s->blame.nlines -
4611 (view->nlines - 3);
4612 break;
4613 case KEY_RESIZE:
4614 if (s->selected_line > view->nlines - 2) {
4615 s->selected_line = MIN(s->blame.nlines,
4616 view->nlines - 2);
4618 break;
4619 default:
4620 break;
4622 return thread_err ? thread_err : err;
4625 static const struct got_error *
4626 cmd_blame(int argc, char *argv[])
4628 const struct got_error *error;
4629 struct got_repository *repo = NULL;
4630 struct got_worktree *worktree = NULL;
4631 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4632 char *link_target = NULL;
4633 struct got_object_id *commit_id = NULL;
4634 char *commit_id_str = NULL;
4635 int ch;
4636 struct tog_view *view;
4638 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4639 switch (ch) {
4640 case 'c':
4641 commit_id_str = optarg;
4642 break;
4643 case 'r':
4644 repo_path = realpath(optarg, NULL);
4645 if (repo_path == NULL)
4646 return got_error_from_errno2("realpath",
4647 optarg);
4648 break;
4649 default:
4650 usage_blame();
4651 /* NOTREACHED */
4655 argc -= optind;
4656 argv += optind;
4658 if (argc != 1)
4659 usage_blame();
4661 cwd = getcwd(NULL, 0);
4662 if (cwd == NULL)
4663 return got_error_from_errno("getcwd");
4665 error = got_worktree_open(&worktree, cwd);
4666 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4667 goto done;
4669 if (repo_path == NULL) {
4670 if (worktree)
4671 repo_path =
4672 strdup(got_worktree_get_repo_path(worktree));
4673 else
4674 repo_path = strdup(cwd);
4676 if (repo_path == NULL) {
4677 error = got_error_from_errno("strdup");
4678 goto done;
4681 error = got_repo_open(&repo, repo_path, NULL);
4682 if (error != NULL)
4683 goto done;
4685 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4686 worktree);
4687 if (error)
4688 goto done;
4690 init_curses();
4692 error = apply_unveil(got_repo_get_path(repo), NULL);
4693 if (error)
4694 goto done;
4696 if (commit_id_str == NULL) {
4697 struct got_reference *head_ref;
4698 error = got_ref_open(&head_ref, repo, worktree ?
4699 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4700 if (error != NULL)
4701 goto done;
4702 error = got_ref_resolve(&commit_id, repo, head_ref);
4703 got_ref_close(head_ref);
4704 } else {
4705 error = got_repo_match_object_id(&commit_id, NULL,
4706 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4708 if (error != NULL)
4709 goto done;
4711 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4712 if (view == NULL) {
4713 error = got_error_from_errno("view_open");
4714 goto done;
4717 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4718 commit_id, repo);
4719 if (error)
4720 goto done;
4722 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4723 commit_id, repo);
4724 if (error)
4725 goto done;
4726 if (worktree) {
4727 /* Release work tree lock. */
4728 got_worktree_close(worktree);
4729 worktree = NULL;
4731 error = view_loop(view);
4732 done:
4733 free(repo_path);
4734 free(in_repo_path);
4735 free(link_target);
4736 free(cwd);
4737 free(commit_id);
4738 if (worktree)
4739 got_worktree_close(worktree);
4740 if (repo)
4741 got_repo_close(repo);
4742 return error;
4745 static const struct got_error *
4746 draw_tree_entries(struct tog_view *view, const char *parent_path)
4748 struct tog_tree_view_state *s = &view->state.tree;
4749 const struct got_error *err = NULL;
4750 struct got_tree_entry *te;
4751 wchar_t *wline;
4752 struct tog_color *tc;
4753 int width, n, i, nentries;
4754 int limit = view->nlines;
4756 s->ndisplayed = 0;
4758 werase(view->window);
4760 if (limit == 0)
4761 return NULL;
4763 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4764 if (err)
4765 return err;
4766 if (view_needs_focus_indication(view))
4767 wstandout(view->window);
4768 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4769 if (tc)
4770 wattr_on(view->window,
4771 COLOR_PAIR(tc->colorpair), NULL);
4772 waddwstr(view->window, wline);
4773 if (tc)
4774 wattr_off(view->window,
4775 COLOR_PAIR(tc->colorpair), NULL);
4776 if (view_needs_focus_indication(view))
4777 wstandend(view->window);
4778 free(wline);
4779 wline = NULL;
4780 if (width < view->ncols - 1)
4781 waddch(view->window, '\n');
4782 if (--limit <= 0)
4783 return NULL;
4784 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4785 if (err)
4786 return err;
4787 waddwstr(view->window, wline);
4788 free(wline);
4789 wline = NULL;
4790 if (width < view->ncols - 1)
4791 waddch(view->window, '\n');
4792 if (--limit <= 0)
4793 return NULL;
4794 waddch(view->window, '\n');
4795 if (--limit <= 0)
4796 return NULL;
4798 if (s->first_displayed_entry == NULL) {
4799 te = got_object_tree_get_first_entry(s->tree);
4800 if (s->selected == 0) {
4801 if (view->focussed)
4802 wstandout(view->window);
4803 s->selected_entry = NULL;
4805 waddstr(view->window, " ..\n"); /* parent directory */
4806 if (s->selected == 0 && view->focussed)
4807 wstandend(view->window);
4808 s->ndisplayed++;
4809 if (--limit <= 0)
4810 return NULL;
4811 n = 1;
4812 } else {
4813 n = 0;
4814 te = s->first_displayed_entry;
4817 nentries = got_object_tree_get_nentries(s->tree);
4818 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4819 char *line = NULL, *id_str = NULL, *link_target = NULL;
4820 const char *modestr = "";
4821 mode_t mode;
4823 te = got_object_tree_get_entry(s->tree, i);
4824 mode = got_tree_entry_get_mode(te);
4826 if (s->show_ids) {
4827 err = got_object_id_str(&id_str,
4828 got_tree_entry_get_id(te));
4829 if (err)
4830 return got_error_from_errno(
4831 "got_object_id_str");
4833 if (got_object_tree_entry_is_submodule(te))
4834 modestr = "$";
4835 else if (S_ISLNK(mode)) {
4836 int i;
4838 err = got_tree_entry_get_symlink_target(&link_target,
4839 te, s->repo);
4840 if (err) {
4841 free(id_str);
4842 return err;
4844 for (i = 0; i < strlen(link_target); i++) {
4845 if (!isprint((unsigned char)link_target[i]))
4846 link_target[i] = '?';
4848 modestr = "@";
4850 else if (S_ISDIR(mode))
4851 modestr = "/";
4852 else if (mode & S_IXUSR)
4853 modestr = "*";
4854 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4855 got_tree_entry_get_name(te), modestr,
4856 link_target ? " -> ": "",
4857 link_target ? link_target : "") == -1) {
4858 free(id_str);
4859 free(link_target);
4860 return got_error_from_errno("asprintf");
4862 free(id_str);
4863 free(link_target);
4864 err = format_line(&wline, &width, line, view->ncols, 0);
4865 if (err) {
4866 free(line);
4867 break;
4869 if (n == s->selected) {
4870 if (view->focussed)
4871 wstandout(view->window);
4872 s->selected_entry = te;
4874 tc = match_color(&s->colors, line);
4875 if (tc)
4876 wattr_on(view->window,
4877 COLOR_PAIR(tc->colorpair), NULL);
4878 waddwstr(view->window, wline);
4879 if (tc)
4880 wattr_off(view->window,
4881 COLOR_PAIR(tc->colorpair), NULL);
4882 if (width < view->ncols - 1)
4883 waddch(view->window, '\n');
4884 if (n == s->selected && view->focussed)
4885 wstandend(view->window);
4886 free(line);
4887 free(wline);
4888 wline = NULL;
4889 n++;
4890 s->ndisplayed++;
4891 s->last_displayed_entry = te;
4892 if (--limit <= 0)
4893 break;
4896 return err;
4899 static void
4900 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4902 struct got_tree_entry *te;
4903 int isroot = s->tree == s->root;
4904 int i = 0;
4906 if (s->first_displayed_entry == NULL)
4907 return;
4909 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4910 while (i++ < maxscroll) {
4911 if (te == NULL) {
4912 if (!isroot)
4913 s->first_displayed_entry = NULL;
4914 break;
4916 s->first_displayed_entry = te;
4917 te = got_tree_entry_get_prev(s->tree, te);
4921 static void
4922 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4924 struct got_tree_entry *next, *last;
4925 int n = 0;
4927 if (s->first_displayed_entry)
4928 next = got_tree_entry_get_next(s->tree,
4929 s->first_displayed_entry);
4930 else
4931 next = got_object_tree_get_first_entry(s->tree);
4933 last = s->last_displayed_entry;
4934 while (next && last && n++ < maxscroll) {
4935 last = got_tree_entry_get_next(s->tree, last);
4936 if (last) {
4937 s->first_displayed_entry = next;
4938 next = got_tree_entry_get_next(s->tree, next);
4943 static const struct got_error *
4944 tree_entry_path(char **path, struct tog_parent_trees *parents,
4945 struct got_tree_entry *te)
4947 const struct got_error *err = NULL;
4948 struct tog_parent_tree *pt;
4949 size_t len = 2; /* for leading slash and NUL */
4951 TAILQ_FOREACH(pt, parents, entry)
4952 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4953 + 1 /* slash */;
4954 if (te)
4955 len += strlen(got_tree_entry_get_name(te));
4957 *path = calloc(1, len);
4958 if (path == NULL)
4959 return got_error_from_errno("calloc");
4961 (*path)[0] = '/';
4962 pt = TAILQ_LAST(parents, tog_parent_trees);
4963 while (pt) {
4964 const char *name = got_tree_entry_get_name(pt->selected_entry);
4965 if (strlcat(*path, name, len) >= len) {
4966 err = got_error(GOT_ERR_NO_SPACE);
4967 goto done;
4969 if (strlcat(*path, "/", len) >= len) {
4970 err = got_error(GOT_ERR_NO_SPACE);
4971 goto done;
4973 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4975 if (te) {
4976 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4977 err = got_error(GOT_ERR_NO_SPACE);
4978 goto done;
4981 done:
4982 if (err) {
4983 free(*path);
4984 *path = NULL;
4986 return err;
4989 static const struct got_error *
4990 blame_tree_entry(struct tog_view **new_view, int begin_x,
4991 struct got_tree_entry *te, struct tog_parent_trees *parents,
4992 struct got_object_id *commit_id, struct got_repository *repo)
4994 const struct got_error *err = NULL;
4995 char *path;
4996 struct tog_view *blame_view;
4998 *new_view = NULL;
5000 err = tree_entry_path(&path, parents, te);
5001 if (err)
5002 return err;
5004 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5005 if (blame_view == NULL) {
5006 err = got_error_from_errno("view_open");
5007 goto done;
5010 err = open_blame_view(blame_view, path, commit_id, repo);
5011 if (err) {
5012 if (err->code == GOT_ERR_CANCELLED)
5013 err = NULL;
5014 view_close(blame_view);
5015 } else
5016 *new_view = blame_view;
5017 done:
5018 free(path);
5019 return err;
5022 static const struct got_error *
5023 log_tree_entry(struct tog_view **new_view, int begin_x,
5024 struct got_tree_entry *te, struct tog_parent_trees *parents,
5025 struct got_object_id *commit_id, struct got_repository *repo)
5027 struct tog_view *log_view;
5028 const struct got_error *err = NULL;
5029 char *path;
5031 *new_view = NULL;
5033 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5034 if (log_view == NULL)
5035 return got_error_from_errno("view_open");
5037 err = tree_entry_path(&path, parents, te);
5038 if (err)
5039 return err;
5041 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5042 if (err)
5043 view_close(log_view);
5044 else
5045 *new_view = log_view;
5046 free(path);
5047 return err;
5050 static const struct got_error *
5051 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5052 struct got_object_id *commit_id, struct got_repository *repo)
5054 const struct got_error *err = NULL;
5055 char *commit_id_str = NULL;
5056 struct tog_tree_view_state *s = &view->state.tree;
5058 TAILQ_INIT(&s->parents);
5060 err = got_object_id_str(&commit_id_str, commit_id);
5061 if (err != NULL)
5062 goto done;
5064 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5065 err = got_error_from_errno("asprintf");
5066 goto done;
5069 s->root = s->tree = root;
5070 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5071 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5072 s->commit_id = got_object_id_dup(commit_id);
5073 if (s->commit_id == NULL) {
5074 err = got_error_from_errno("got_object_id_dup");
5075 goto done;
5077 s->repo = repo;
5079 SIMPLEQ_INIT(&s->colors);
5081 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5082 err = add_color(&s->colors, "\\$$",
5083 TOG_COLOR_TREE_SUBMODULE,
5084 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5085 if (err)
5086 goto done;
5087 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5088 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5089 if (err) {
5090 free_colors(&s->colors);
5091 goto done;
5093 err = add_color(&s->colors, "/$",
5094 TOG_COLOR_TREE_DIRECTORY,
5095 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5096 if (err) {
5097 free_colors(&s->colors);
5098 goto done;
5101 err = add_color(&s->colors, "\\*$",
5102 TOG_COLOR_TREE_EXECUTABLE,
5103 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5104 if (err) {
5105 free_colors(&s->colors);
5106 goto done;
5109 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5110 get_color_value("TOG_COLOR_COMMIT"));
5111 if (err) {
5112 free_colors(&s->colors);
5113 goto done;
5117 view->show = show_tree_view;
5118 view->input = input_tree_view;
5119 view->close = close_tree_view;
5120 view->search_start = search_start_tree_view;
5121 view->search_next = search_next_tree_view;
5122 done:
5123 free(commit_id_str);
5124 if (err) {
5125 free(s->tree_label);
5126 s->tree_label = NULL;
5128 return err;
5131 static const struct got_error *
5132 close_tree_view(struct tog_view *view)
5134 struct tog_tree_view_state *s = &view->state.tree;
5136 free_colors(&s->colors);
5137 free(s->tree_label);
5138 s->tree_label = NULL;
5139 free(s->commit_id);
5140 s->commit_id = NULL;
5141 while (!TAILQ_EMPTY(&s->parents)) {
5142 struct tog_parent_tree *parent;
5143 parent = TAILQ_FIRST(&s->parents);
5144 TAILQ_REMOVE(&s->parents, parent, entry);
5145 free(parent);
5148 if (s->tree != s->root)
5149 got_object_tree_close(s->tree);
5150 got_object_tree_close(s->root);
5151 return NULL;
5154 static const struct got_error *
5155 search_start_tree_view(struct tog_view *view)
5157 struct tog_tree_view_state *s = &view->state.tree;
5159 s->matched_entry = NULL;
5160 return NULL;
5163 static int
5164 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5166 regmatch_t regmatch;
5168 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5169 0) == 0;
5172 static const struct got_error *
5173 search_next_tree_view(struct tog_view *view)
5175 struct tog_tree_view_state *s = &view->state.tree;
5176 struct got_tree_entry *te = NULL;
5178 if (!view->searching) {
5179 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5180 return NULL;
5183 if (s->matched_entry) {
5184 if (view->searching == TOG_SEARCH_FORWARD) {
5185 if (s->selected_entry)
5186 te = got_tree_entry_get_next(s->tree,
5187 s->selected_entry);
5188 else
5189 te = got_object_tree_get_first_entry(s->tree);
5190 } else {
5191 if (s->selected_entry == NULL)
5192 te = got_object_tree_get_last_entry(s->tree);
5193 else
5194 te = got_tree_entry_get_prev(s->tree,
5195 s->selected_entry);
5197 } else {
5198 if (view->searching == TOG_SEARCH_FORWARD)
5199 te = got_object_tree_get_first_entry(s->tree);
5200 else
5201 te = got_object_tree_get_last_entry(s->tree);
5204 while (1) {
5205 if (te == NULL) {
5206 if (s->matched_entry == NULL) {
5207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5208 return NULL;
5210 if (view->searching == TOG_SEARCH_FORWARD)
5211 te = got_object_tree_get_first_entry(s->tree);
5212 else
5213 te = got_object_tree_get_last_entry(s->tree);
5216 if (match_tree_entry(te, &view->regex)) {
5217 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5218 s->matched_entry = te;
5219 break;
5222 if (view->searching == TOG_SEARCH_FORWARD)
5223 te = got_tree_entry_get_next(s->tree, te);
5224 else
5225 te = got_tree_entry_get_prev(s->tree, te);
5228 if (s->matched_entry) {
5229 s->first_displayed_entry = s->matched_entry;
5230 s->selected = 0;
5233 return NULL;
5236 static const struct got_error *
5237 show_tree_view(struct tog_view *view)
5239 const struct got_error *err = NULL;
5240 struct tog_tree_view_state *s = &view->state.tree;
5241 char *parent_path;
5243 err = tree_entry_path(&parent_path, &s->parents, NULL);
5244 if (err)
5245 return err;
5247 err = draw_tree_entries(view, parent_path);
5248 free(parent_path);
5250 view_vborder(view);
5251 return err;
5254 static const struct got_error *
5255 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5257 const struct got_error *err = NULL;
5258 struct tog_tree_view_state *s = &view->state.tree;
5259 struct tog_view *log_view, *ref_view;
5260 int begin_x = 0;
5262 switch (ch) {
5263 case 'i':
5264 s->show_ids = !s->show_ids;
5265 break;
5266 case 'l':
5267 if (!s->selected_entry)
5268 break;
5269 if (view_is_parent_view(view))
5270 begin_x = view_split_begin_x(view->begin_x);
5271 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5272 &s->parents, s->commit_id, s->repo);
5273 view->focussed = 0;
5274 log_view->focussed = 1;
5275 if (view_is_parent_view(view)) {
5276 err = view_close_child(view);
5277 if (err)
5278 return err;
5279 view_set_child(view, log_view);
5280 view->focus_child = 1;
5281 } else
5282 *new_view = log_view;
5283 break;
5284 case 'r':
5285 if (view_is_parent_view(view))
5286 begin_x = view_split_begin_x(view->begin_x);
5287 ref_view = view_open(view->nlines, view->ncols,
5288 view->begin_y, begin_x, TOG_VIEW_REF);
5289 if (ref_view == NULL)
5290 return got_error_from_errno("view_open");
5291 err = open_ref_view(ref_view, s->repo);
5292 if (err) {
5293 view_close(ref_view);
5294 return err;
5296 view->focussed = 0;
5297 ref_view->focussed = 1;
5298 if (view_is_parent_view(view)) {
5299 err = view_close_child(view);
5300 if (err)
5301 return err;
5302 view_set_child(view, ref_view);
5303 view->focus_child = 1;
5304 } else
5305 *new_view = ref_view;
5306 break;
5307 case 'k':
5308 case KEY_UP:
5309 if (s->selected > 0) {
5310 s->selected--;
5311 break;
5313 tree_scroll_up(s, 1);
5314 break;
5315 case KEY_PPAGE:
5316 case CTRL('b'):
5317 if (s->tree == s->root) {
5318 if (got_object_tree_get_first_entry(s->tree) ==
5319 s->first_displayed_entry)
5320 s->selected = 0;
5321 } else {
5322 if (s->first_displayed_entry == NULL)
5323 s->selected = 0;
5325 tree_scroll_up(s, MAX(0, view->nlines - 3));
5326 break;
5327 case 'j':
5328 case KEY_DOWN:
5329 if (s->selected < s->ndisplayed - 1) {
5330 s->selected++;
5331 break;
5333 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5334 == NULL)
5335 /* can't scroll any further */
5336 break;
5337 tree_scroll_down(s, 1);
5338 break;
5339 case KEY_NPAGE:
5340 case CTRL('f'):
5341 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5342 == NULL) {
5343 /* can't scroll any further; move cursor down */
5344 if (s->selected < s->ndisplayed - 1)
5345 s->selected = s->ndisplayed - 1;
5346 break;
5348 tree_scroll_down(s, view->nlines - 3);
5349 break;
5350 case KEY_ENTER:
5351 case '\r':
5352 case KEY_BACKSPACE:
5353 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5354 struct tog_parent_tree *parent;
5355 /* user selected '..' */
5356 if (s->tree == s->root)
5357 break;
5358 parent = TAILQ_FIRST(&s->parents);
5359 TAILQ_REMOVE(&s->parents, parent,
5360 entry);
5361 got_object_tree_close(s->tree);
5362 s->tree = parent->tree;
5363 s->first_displayed_entry =
5364 parent->first_displayed_entry;
5365 s->selected_entry =
5366 parent->selected_entry;
5367 s->selected = parent->selected;
5368 free(parent);
5369 } else if (S_ISDIR(got_tree_entry_get_mode(
5370 s->selected_entry))) {
5371 struct got_tree_object *subtree;
5372 err = got_object_open_as_tree(&subtree, s->repo,
5373 got_tree_entry_get_id(s->selected_entry));
5374 if (err)
5375 break;
5376 err = tree_view_visit_subtree(s, subtree);
5377 if (err) {
5378 got_object_tree_close(subtree);
5379 break;
5381 } else if (S_ISREG(got_tree_entry_get_mode(
5382 s->selected_entry))) {
5383 struct tog_view *blame_view;
5384 int begin_x = view_is_parent_view(view) ?
5385 view_split_begin_x(view->begin_x) : 0;
5387 err = blame_tree_entry(&blame_view, begin_x,
5388 s->selected_entry, &s->parents,
5389 s->commit_id, s->repo);
5390 if (err)
5391 break;
5392 view->focussed = 0;
5393 blame_view->focussed = 1;
5394 if (view_is_parent_view(view)) {
5395 err = view_close_child(view);
5396 if (err)
5397 return err;
5398 view_set_child(view, blame_view);
5399 view->focus_child = 1;
5400 } else
5401 *new_view = blame_view;
5403 break;
5404 case KEY_RESIZE:
5405 if (s->selected > view->nlines)
5406 s->selected = s->ndisplayed - 1;
5407 break;
5408 default:
5409 break;
5412 return err;
5415 __dead static void
5416 usage_tree(void)
5418 endwin();
5419 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5420 getprogname());
5421 exit(1);
5424 static const struct got_error *
5425 cmd_tree(int argc, char *argv[])
5427 const struct got_error *error;
5428 struct got_repository *repo = NULL;
5429 struct got_worktree *worktree = NULL;
5430 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5431 struct got_object_id *commit_id = NULL;
5432 char *commit_id_arg = NULL;
5433 struct got_commit_object *commit = NULL;
5434 struct got_tree_object *tree = NULL;
5435 int ch;
5436 struct tog_view *view;
5438 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5439 switch (ch) {
5440 case 'c':
5441 commit_id_arg = optarg;
5442 break;
5443 case 'r':
5444 repo_path = realpath(optarg, NULL);
5445 if (repo_path == NULL)
5446 return got_error_from_errno2("realpath",
5447 optarg);
5448 break;
5449 default:
5450 usage_tree();
5451 /* NOTREACHED */
5455 argc -= optind;
5456 argv += optind;
5458 if (argc > 1)
5459 usage_tree();
5461 cwd = getcwd(NULL, 0);
5462 if (cwd == NULL)
5463 return got_error_from_errno("getcwd");
5465 error = got_worktree_open(&worktree, cwd);
5466 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5467 goto done;
5469 if (repo_path == NULL) {
5470 if (worktree)
5471 repo_path =
5472 strdup(got_worktree_get_repo_path(worktree));
5473 else
5474 repo_path = strdup(cwd);
5476 if (repo_path == NULL) {
5477 error = got_error_from_errno("strdup");
5478 goto done;
5481 error = got_repo_open(&repo, repo_path, NULL);
5482 if (error != NULL)
5483 goto done;
5485 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5486 repo, worktree);
5487 if (error)
5488 goto done;
5490 init_curses();
5492 error = apply_unveil(got_repo_get_path(repo), NULL);
5493 if (error)
5494 goto done;
5496 error = got_repo_match_object_id(&commit_id, NULL,
5497 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5498 GOT_OBJ_TYPE_COMMIT, 1, repo);
5499 if (error)
5500 goto done;
5502 error = got_object_open_as_commit(&commit, repo, commit_id);
5503 if (error)
5504 goto done;
5506 error = got_object_open_as_tree(&tree, repo,
5507 got_object_commit_get_tree_id(commit));
5508 if (error)
5509 goto done;
5511 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5512 if (view == NULL) {
5513 error = got_error_from_errno("view_open");
5514 goto done;
5516 error = open_tree_view(view, tree, commit_id, repo);
5517 if (error)
5518 goto done;
5519 if (!got_path_is_root_dir(in_repo_path)) {
5520 error = tree_view_walk_path(&view->state.tree, commit_id,
5521 in_repo_path);
5522 if (error)
5523 goto done;
5526 if (worktree) {
5527 /* Release work tree lock. */
5528 got_worktree_close(worktree);
5529 worktree = NULL;
5531 error = view_loop(view);
5532 done:
5533 free(repo_path);
5534 free(cwd);
5535 free(commit_id);
5536 if (commit)
5537 got_object_commit_close(commit);
5538 if (tree)
5539 got_object_tree_close(tree);
5540 if (repo)
5541 got_repo_close(repo);
5542 return error;
5545 static const struct got_error *
5546 ref_view_load_refs(struct tog_ref_view_state *s)
5548 const struct got_error *err;
5549 struct got_reflist_entry *sre;
5550 struct tog_reflist_entry *re;
5552 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5553 got_ref_cmp_by_name, NULL);
5554 if (err)
5555 return err;
5557 s->nrefs = 0;
5558 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5559 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5560 continue;
5562 re = malloc(sizeof(*re));
5563 if (re == NULL)
5564 return got_error_from_errno("malloc");
5566 re->ref = sre->ref;
5567 re->idx = s->nrefs++;
5568 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5571 return NULL;
5574 void
5575 ref_view_free_refs(struct tog_ref_view_state *s)
5577 struct tog_reflist_entry *re;
5579 while (!TAILQ_EMPTY(&s->refs)) {
5580 re = TAILQ_FIRST(&s->refs);
5581 TAILQ_REMOVE(&s->refs, re, entry);
5582 free(re);
5584 got_ref_list_free(&s->simplerefs);
5587 static const struct got_error *
5588 open_ref_view(struct tog_view *view, struct got_repository *repo)
5590 const struct got_error *err = NULL;
5591 struct tog_ref_view_state *s = &view->state.ref;
5593 s->selected_entry = 0;
5594 s->repo = repo;
5596 SIMPLEQ_INIT(&s->simplerefs);
5597 TAILQ_INIT(&s->refs);
5598 SIMPLEQ_INIT(&s->colors);
5600 err = ref_view_load_refs(s);
5601 if (err)
5602 return err;
5604 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5606 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5607 err = add_color(&s->colors, "^refs/heads/",
5608 TOG_COLOR_REFS_HEADS,
5609 get_color_value("TOG_COLOR_REFS_HEADS"));
5610 if (err)
5611 goto done;
5613 err = add_color(&s->colors, "^refs/tags/",
5614 TOG_COLOR_REFS_TAGS,
5615 get_color_value("TOG_COLOR_REFS_TAGS"));
5616 if (err)
5617 goto done;
5619 err = add_color(&s->colors, "^refs/remotes/",
5620 TOG_COLOR_REFS_REMOTES,
5621 get_color_value("TOG_COLOR_REFS_REMOTES"));
5622 if (err)
5623 goto done;
5626 view->show = show_ref_view;
5627 view->input = input_ref_view;
5628 view->close = close_ref_view;
5629 view->search_start = search_start_ref_view;
5630 view->search_next = search_next_ref_view;
5631 done:
5632 if (err)
5633 free_colors(&s->colors);
5634 return err;
5637 static const struct got_error *
5638 close_ref_view(struct tog_view *view)
5640 struct tog_ref_view_state *s = &view->state.ref;
5642 ref_view_free_refs(s);
5643 free_colors(&s->colors);
5645 return NULL;
5648 static const struct got_error *
5649 resolve_reflist_entry(struct got_object_id **commit_id,
5650 struct tog_reflist_entry *re, struct got_repository *repo)
5652 const struct got_error *err = NULL;
5653 struct got_object_id *obj_id;
5654 struct got_tag_object *tag = NULL;
5655 int obj_type;
5657 *commit_id = NULL;
5659 err = got_ref_resolve(&obj_id, repo, re->ref);
5660 if (err)
5661 return err;
5663 err = got_object_get_type(&obj_type, repo, obj_id);
5664 if (err)
5665 goto done;
5667 switch (obj_type) {
5668 case GOT_OBJ_TYPE_COMMIT:
5669 *commit_id = obj_id;
5670 break;
5671 case GOT_OBJ_TYPE_TAG:
5672 err = got_object_open_as_tag(&tag, repo, obj_id);
5673 if (err)
5674 goto done;
5675 free(obj_id);
5676 err = got_object_get_type(&obj_type, repo,
5677 got_object_tag_get_object_id(tag));
5678 if (err)
5679 goto done;
5680 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5681 err = got_error(GOT_ERR_OBJ_TYPE);
5682 goto done;
5684 *commit_id = got_object_id_dup(
5685 got_object_tag_get_object_id(tag));
5686 if (*commit_id == NULL) {
5687 err = got_error_from_errno("got_object_id_dup");
5688 goto done;
5690 break;
5691 default:
5692 err = got_error(GOT_ERR_OBJ_TYPE);
5693 break;
5696 done:
5697 if (tag)
5698 got_object_tag_close(tag);
5699 if (err) {
5700 free(*commit_id);
5701 *commit_id = NULL;
5703 return err;
5706 static const struct got_error *
5707 log_ref_entry(struct tog_view **new_view, int begin_x,
5708 struct tog_reflist_entry *re, struct got_repository *repo)
5710 struct tog_view *log_view;
5711 const struct got_error *err = NULL;
5712 struct got_object_id *commit_id = NULL;
5714 *new_view = NULL;
5716 err = resolve_reflist_entry(&commit_id, re, repo);
5717 if (err) {
5718 if (err->code != GOT_ERR_OBJ_TYPE)
5719 return err;
5720 else
5721 return NULL;
5724 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5725 if (log_view == NULL) {
5726 err = got_error_from_errno("view_open");
5727 goto done;
5730 err = open_log_view(log_view, commit_id, repo,
5731 got_ref_get_name(re->ref), "", 0);
5732 done:
5733 if (err)
5734 view_close(log_view);
5735 else
5736 *new_view = log_view;
5737 free(commit_id);
5738 return err;
5741 static void
5742 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5744 struct tog_reflist_entry *re;
5745 int i = 0;
5747 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5748 return;
5750 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5751 while (i++ < maxscroll) {
5752 if (re == NULL)
5753 break;
5754 s->first_displayed_entry = re;
5755 re = TAILQ_PREV(re, tog_reflist_head, entry);
5759 static void
5760 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5762 struct tog_reflist_entry *next, *last;
5763 int n = 0;
5765 if (s->first_displayed_entry)
5766 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5767 else
5768 next = TAILQ_FIRST(&s->refs);
5770 last = s->last_displayed_entry;
5771 while (next && last && n++ < maxscroll) {
5772 last = TAILQ_NEXT(last, entry);
5773 if (last) {
5774 s->first_displayed_entry = next;
5775 next = TAILQ_NEXT(next, entry);
5780 static const struct got_error *
5781 search_start_ref_view(struct tog_view *view)
5783 struct tog_ref_view_state *s = &view->state.ref;
5785 s->matched_entry = NULL;
5786 return NULL;
5789 static int
5790 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5792 regmatch_t regmatch;
5794 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5795 0) == 0;
5798 static const struct got_error *
5799 search_next_ref_view(struct tog_view *view)
5801 struct tog_ref_view_state *s = &view->state.ref;
5802 struct tog_reflist_entry *re = NULL;
5804 if (!view->searching) {
5805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5806 return NULL;
5809 if (s->matched_entry) {
5810 if (view->searching == TOG_SEARCH_FORWARD) {
5811 if (s->selected_entry)
5812 re = TAILQ_NEXT(s->selected_entry, entry);
5813 else
5814 re = TAILQ_PREV(s->selected_entry,
5815 tog_reflist_head, entry);
5816 } else {
5817 if (s->selected_entry == NULL)
5818 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5819 else
5820 re = TAILQ_PREV(s->selected_entry,
5821 tog_reflist_head, entry);
5823 } else {
5824 if (view->searching == TOG_SEARCH_FORWARD)
5825 re = TAILQ_FIRST(&s->refs);
5826 else
5827 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5830 while (1) {
5831 if (re == NULL) {
5832 if (s->matched_entry == NULL) {
5833 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5834 return NULL;
5836 if (view->searching == TOG_SEARCH_FORWARD)
5837 re = TAILQ_FIRST(&s->refs);
5838 else
5839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5842 if (match_reflist_entry(re, &view->regex)) {
5843 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5844 s->matched_entry = re;
5845 break;
5848 if (view->searching == TOG_SEARCH_FORWARD)
5849 re = TAILQ_NEXT(re, entry);
5850 else
5851 re = TAILQ_PREV(re, tog_reflist_head, entry);
5854 if (s->matched_entry) {
5855 s->first_displayed_entry = s->matched_entry;
5856 s->selected = 0;
5859 return NULL;
5862 static const struct got_error *
5863 show_ref_view(struct tog_view *view)
5865 const struct got_error *err = NULL;
5866 struct tog_ref_view_state *s = &view->state.ref;
5867 struct tog_reflist_entry *re;
5868 char *line = NULL;
5869 wchar_t *wline;
5870 struct tog_color *tc;
5871 int width, n;
5872 int limit = view->nlines;
5874 werase(view->window);
5876 s->ndisplayed = 0;
5878 if (limit == 0)
5879 return NULL;
5881 re = s->first_displayed_entry;
5883 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5884 s->nrefs) == -1)
5885 return got_error_from_errno("asprintf");
5887 err = format_line(&wline, &width, line, view->ncols, 0);
5888 if (err) {
5889 free(line);
5890 return err;
5892 if (view_needs_focus_indication(view))
5893 wstandout(view->window);
5894 waddwstr(view->window, wline);
5895 if (view_needs_focus_indication(view))
5896 wstandend(view->window);
5897 free(wline);
5898 wline = NULL;
5899 free(line);
5900 line = NULL;
5901 if (width < view->ncols - 1)
5902 waddch(view->window, '\n');
5903 if (--limit <= 0)
5904 return NULL;
5906 n = 0;
5907 while (re && limit > 0) {
5908 char *line = NULL;
5910 if (got_ref_is_symbolic(re->ref)) {
5911 if (asprintf(&line, "%s -> %s",
5912 got_ref_get_name(re->ref),
5913 got_ref_get_symref_target(re->ref)) == -1)
5914 return got_error_from_errno("asprintf");
5915 } else if (s->show_ids) {
5916 struct got_object_id *id;
5917 char *id_str;
5918 err = got_ref_resolve(&id, s->repo, re->ref);
5919 if (err)
5920 return err;
5921 err = got_object_id_str(&id_str, id);
5922 if (err) {
5923 free(id);
5924 return err;
5926 if (asprintf(&line, "%s: %s",
5927 got_ref_get_name(re->ref), id_str) == -1) {
5928 err = got_error_from_errno("asprintf");
5929 free(id);
5930 free(id_str);
5931 return err;
5933 free(id);
5934 free(id_str);
5935 } else {
5936 line = strdup(got_ref_get_name(re->ref));
5937 if (line == NULL)
5938 return got_error_from_errno("strdup");
5941 err = format_line(&wline, &width, line, view->ncols, 0);
5942 if (err) {
5943 free(line);
5944 return err;
5946 if (n == s->selected) {
5947 if (view->focussed)
5948 wstandout(view->window);
5949 s->selected_entry = re;
5951 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5952 if (tc)
5953 wattr_on(view->window,
5954 COLOR_PAIR(tc->colorpair), NULL);
5955 waddwstr(view->window, wline);
5956 if (tc)
5957 wattr_off(view->window,
5958 COLOR_PAIR(tc->colorpair), NULL);
5959 if (width < view->ncols - 1)
5960 waddch(view->window, '\n');
5961 if (n == s->selected && view->focussed)
5962 wstandend(view->window);
5963 free(line);
5964 free(wline);
5965 wline = NULL;
5966 n++;
5967 s->ndisplayed++;
5968 s->last_displayed_entry = re;
5970 limit--;
5971 re = TAILQ_NEXT(re, entry);
5974 view_vborder(view);
5975 return err;
5978 static const struct got_error *
5979 browse_ref_tree(struct tog_view **new_view, int begin_x,
5980 struct tog_reflist_entry *re, struct got_repository *repo)
5982 const struct got_error *err = NULL;
5983 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5984 struct got_tree_object *tree = NULL;
5985 struct tog_view *tree_view;
5987 *new_view = NULL;
5989 err = resolve_reflist_entry(&commit_id, re, repo);
5990 if (err) {
5991 if (err->code != GOT_ERR_OBJ_TYPE)
5992 return err;
5993 else
5994 return NULL;
5997 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
5998 if (err)
5999 goto done;
6001 err = got_object_open_as_tree(&tree, repo, tree_id);
6002 if (err)
6003 goto done;
6005 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6006 if (tree_view == NULL) {
6007 err = got_error_from_errno("view_open");
6008 goto done;
6011 err = open_tree_view(tree_view, tree, commit_id, repo);
6012 if (err)
6013 goto done;
6015 *new_view = tree_view;
6016 done:
6017 free(commit_id);
6018 free(tree_id);
6019 if (err) {
6020 if (tree)
6021 got_object_tree_close(tree);
6023 return err;
6025 static const struct got_error *
6026 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6028 const struct got_error *err = NULL;
6029 struct tog_ref_view_state *s = &view->state.ref;
6030 struct tog_view *log_view, *tree_view;
6031 int begin_x = 0;
6033 switch (ch) {
6034 case 'i':
6035 s->show_ids = !s->show_ids;
6036 break;
6037 case KEY_ENTER:
6038 case '\r':
6039 if (!s->selected_entry)
6040 break;
6041 if (view_is_parent_view(view))
6042 begin_x = view_split_begin_x(view->begin_x);
6043 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6044 s->repo);
6045 view->focussed = 0;
6046 log_view->focussed = 1;
6047 if (view_is_parent_view(view)) {
6048 err = view_close_child(view);
6049 if (err)
6050 return err;
6051 view_set_child(view, log_view);
6052 view->focus_child = 1;
6053 } else
6054 *new_view = log_view;
6055 break;
6056 case 't':
6057 if (!s->selected_entry)
6058 break;
6059 if (view_is_parent_view(view))
6060 begin_x = view_split_begin_x(view->begin_x);
6061 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6062 s->repo);
6063 if (err || tree_view == NULL)
6064 break;
6065 view->focussed = 0;
6066 tree_view->focussed = 1;
6067 if (view_is_parent_view(view)) {
6068 err = view_close_child(view);
6069 if (err)
6070 return err;
6071 view_set_child(view, tree_view);
6072 view->focus_child = 1;
6073 } else
6074 *new_view = tree_view;
6075 break;
6076 case 'k':
6077 case KEY_UP:
6078 if (s->selected > 0) {
6079 s->selected--;
6080 break;
6082 ref_scroll_up(s, 1);
6083 break;
6084 case KEY_PPAGE:
6085 case CTRL('b'):
6086 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6087 s->selected = 0;
6088 ref_scroll_up(s, MAX(0, view->nlines - 1));
6089 break;
6090 case 'j':
6091 case KEY_DOWN:
6092 if (s->selected < s->ndisplayed - 1) {
6093 s->selected++;
6094 break;
6096 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6097 /* can't scroll any further */
6098 break;
6099 ref_scroll_down(s, 1);
6100 break;
6101 case KEY_NPAGE:
6102 case CTRL('f'):
6103 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6104 /* can't scroll any further; move cursor down */
6105 if (s->selected < s->ndisplayed - 1)
6106 s->selected = s->ndisplayed - 1;
6107 break;
6109 ref_scroll_down(s, view->nlines - 1);
6110 break;
6111 case CTRL('l'):
6112 ref_view_free_refs(s);
6113 err = ref_view_load_refs(s);
6114 break;
6115 case KEY_RESIZE:
6116 if (s->selected > view->nlines)
6117 s->selected = s->ndisplayed - 1;
6118 break;
6119 default:
6120 break;
6123 return err;
6126 __dead static void
6127 usage_ref(void)
6129 endwin();
6130 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6131 getprogname());
6132 exit(1);
6135 static const struct got_error *
6136 cmd_ref(int argc, char *argv[])
6138 const struct got_error *error;
6139 struct got_repository *repo = NULL;
6140 struct got_worktree *worktree = NULL;
6141 char *cwd = NULL, *repo_path = NULL;
6142 int ch;
6143 struct tog_view *view;
6145 while ((ch = getopt(argc, argv, "r:")) != -1) {
6146 switch (ch) {
6147 case 'r':
6148 repo_path = realpath(optarg, NULL);
6149 if (repo_path == NULL)
6150 return got_error_from_errno2("realpath",
6151 optarg);
6152 break;
6153 default:
6154 usage_ref();
6155 /* NOTREACHED */
6159 argc -= optind;
6160 argv += optind;
6162 if (argc > 1)
6163 usage_ref();
6165 cwd = getcwd(NULL, 0);
6166 if (cwd == NULL)
6167 return got_error_from_errno("getcwd");
6169 error = got_worktree_open(&worktree, cwd);
6170 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6171 goto done;
6173 if (repo_path == NULL) {
6174 if (worktree)
6175 repo_path =
6176 strdup(got_worktree_get_repo_path(worktree));
6177 else
6178 repo_path = strdup(cwd);
6180 if (repo_path == NULL) {
6181 error = got_error_from_errno("strdup");
6182 goto done;
6185 error = got_repo_open(&repo, repo_path, NULL);
6186 if (error != NULL)
6187 goto done;
6189 init_curses();
6191 error = apply_unveil(got_repo_get_path(repo), NULL);
6192 if (error)
6193 goto done;
6195 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6196 if (view == NULL) {
6197 error = got_error_from_errno("view_open");
6198 goto done;
6201 error = open_ref_view(view, repo);
6202 if (error)
6203 goto done;
6205 if (worktree) {
6206 /* Release work tree lock. */
6207 got_worktree_close(worktree);
6208 worktree = NULL;
6210 error = view_loop(view);
6211 done:
6212 free(repo_path);
6213 free(cwd);
6214 if (repo)
6215 got_repo_close(repo);
6216 return error;
6219 static void
6220 list_commands(FILE *fp)
6222 int i;
6224 fprintf(fp, "commands:");
6225 for (i = 0; i < nitems(tog_commands); i++) {
6226 struct tog_cmd *cmd = &tog_commands[i];
6227 fprintf(fp, " %s", cmd->name);
6229 fputc('\n', fp);
6232 __dead static void
6233 usage(int hflag, int status)
6235 FILE *fp = (status == 0) ? stdout : stderr;
6237 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6238 getprogname());
6239 if (hflag) {
6240 fprintf(fp, "lazy usage: %s path\n", getprogname());
6241 list_commands(fp);
6243 exit(status);
6246 static char **
6247 make_argv(int argc, ...)
6249 va_list ap;
6250 char **argv;
6251 int i;
6253 va_start(ap, argc);
6255 argv = calloc(argc, sizeof(char *));
6256 if (argv == NULL)
6257 err(1, "calloc");
6258 for (i = 0; i < argc; i++) {
6259 argv[i] = strdup(va_arg(ap, char *));
6260 if (argv[i] == NULL)
6261 err(1, "strdup");
6264 va_end(ap);
6265 return argv;
6269 * Try to convert 'tog path' into a 'tog log path' command.
6270 * The user could simply have mistyped the command rather than knowingly
6271 * provided a path. So check whether argv[0] can in fact be resolved
6272 * to a path in the HEAD commit and print a special error if not.
6273 * This hack is for mpi@ <3
6275 static const struct got_error *
6276 tog_log_with_path(int argc, char *argv[])
6278 const struct got_error *error = NULL;
6279 struct tog_cmd *cmd = NULL;
6280 struct got_repository *repo = NULL;
6281 struct got_worktree *worktree = NULL;
6282 struct got_object_id *commit_id = NULL, *id = NULL;
6283 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6284 char *commit_id_str = NULL, **cmd_argv = NULL;
6286 cwd = getcwd(NULL, 0);
6287 if (cwd == NULL)
6288 return got_error_from_errno("getcwd");
6290 error = got_worktree_open(&worktree, cwd);
6291 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6292 goto done;
6294 if (worktree)
6295 repo_path = strdup(got_worktree_get_repo_path(worktree));
6296 else
6297 repo_path = strdup(cwd);
6298 if (repo_path == NULL) {
6299 error = got_error_from_errno("strdup");
6300 goto done;
6303 error = got_repo_open(&repo, repo_path, NULL);
6304 if (error != NULL)
6305 goto done;
6307 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6308 repo, worktree);
6309 if (error)
6310 goto done;
6312 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6313 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6314 GOT_OBJ_TYPE_COMMIT, 1, repo);
6315 if (error)
6316 goto done;
6318 if (worktree) {
6319 got_worktree_close(worktree);
6320 worktree = NULL;
6323 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6324 if (error) {
6325 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6326 goto done;
6327 fprintf(stderr, "%s: '%s' is no known command or path\n",
6328 getprogname(), argv[0]);
6329 usage(1, 1);
6330 /* not reached */
6333 got_repo_close(repo);
6334 repo = NULL;
6336 error = got_object_id_str(&commit_id_str, commit_id);
6337 if (error)
6338 goto done;
6340 cmd = &tog_commands[0]; /* log */
6341 argc = 4;
6342 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6343 error = cmd->cmd_main(argc, cmd_argv);
6344 done:
6345 if (repo)
6346 got_repo_close(repo);
6347 if (worktree)
6348 got_worktree_close(worktree);
6349 free(id);
6350 free(commit_id_str);
6351 free(commit_id);
6352 free(cwd);
6353 free(repo_path);
6354 free(in_repo_path);
6355 if (cmd_argv) {
6356 int i;
6357 for (i = 0; i < argc; i++)
6358 free(cmd_argv[i]);
6359 free(cmd_argv);
6361 return error;
6364 int
6365 main(int argc, char *argv[])
6367 const struct got_error *error = NULL;
6368 struct tog_cmd *cmd = NULL;
6369 int ch, hflag = 0, Vflag = 0;
6370 char **cmd_argv = NULL;
6371 static struct option longopts[] = {
6372 { "version", no_argument, NULL, 'V' },
6373 { NULL, 0, NULL, 0}
6376 setlocale(LC_CTYPE, "");
6378 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6379 switch (ch) {
6380 case 'h':
6381 hflag = 1;
6382 break;
6383 case 'V':
6384 Vflag = 1;
6385 break;
6386 default:
6387 usage(hflag, 1);
6388 /* NOTREACHED */
6392 argc -= optind;
6393 argv += optind;
6394 optind = 1;
6395 optreset = 1;
6397 if (Vflag) {
6398 got_version_print_str();
6399 return 0;
6402 #ifndef PROFILE
6403 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6404 NULL) == -1)
6405 err(1, "pledge");
6406 #endif
6408 if (argc == 0) {
6409 if (hflag)
6410 usage(hflag, 0);
6411 /* Build an argument vector which runs a default command. */
6412 cmd = &tog_commands[0];
6413 argc = 1;
6414 cmd_argv = make_argv(argc, cmd->name);
6415 } else {
6416 int i;
6418 /* Did the user specify a command? */
6419 for (i = 0; i < nitems(tog_commands); i++) {
6420 if (strncmp(tog_commands[i].name, argv[0],
6421 strlen(argv[0])) == 0) {
6422 cmd = &tog_commands[i];
6423 break;
6428 if (cmd == NULL) {
6429 if (argc != 1)
6430 usage(0, 1);
6431 /* No command specified; try log with a path */
6432 error = tog_log_with_path(argc, argv);
6433 } else {
6434 if (hflag)
6435 cmd->cmd_usage();
6436 else
6437 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6440 endwin();
6441 putchar('\n');
6442 if (cmd_argv) {
6443 int i;
6444 for (i = 0; i < argc; i++)
6445 free(cmd_argv[i]);
6446 free(cmd_argv);
6449 if (error && error->code != GOT_ERR_CANCELLED)
6450 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6451 return 0;