2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
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"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
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 const 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 },
109 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 enum tog_keymap_type {
111 TOG_KEYMAP_KEYS = -2,
127 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
129 #define TOG_EOF_STRING "(END)"
131 struct commit_queue_entry {
132 TAILQ_ENTRY(commit_queue_entry) entry;
133 struct got_object_id *id;
134 struct got_commit_object *commit;
137 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 struct commit_queue {
140 struct commit_queue_head head;
144 STAILQ_ENTRY(tog_color) entry;
148 STAILQ_HEAD(tog_colors, tog_color);
150 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 static struct got_reflist_object_id_map *tog_refs_idmap;
152 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
154 static const struct got_error *
155 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 struct got_reference* re2)
158 const char *name1 = got_ref_get_name(re1);
159 const char *name2 = got_ref_get_name(re2);
160 int isbackup1, isbackup2;
162 /* Sort backup refs towards the bottom of the list. */
163 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 if (!isbackup1 && isbackup2) {
168 } else if (isbackup1 && !isbackup2) {
173 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
177 static const struct got_error *
178 tog_load_refs(struct got_repository *repo, int sort_by_date)
180 const struct got_error *err;
182 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
188 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
195 if (tog_refs_idmap) {
196 got_reflist_object_id_map_free(tog_refs_idmap);
197 tog_refs_idmap = NULL;
199 got_ref_list_free(&tog_refs);
202 static const struct got_error *
203 add_color(struct tog_colors *colors, const char *pattern,
204 int idx, short color)
206 const struct got_error *err = NULL;
207 struct tog_color *tc;
210 if (idx < 1 || idx > COLOR_PAIRS - 1)
213 init_pair(idx, color, -1);
215 tc = calloc(1, sizeof(*tc));
217 return got_error_from_errno("calloc");
218 regerr = regcomp(&tc->regex, pattern,
219 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 static char regerr_msg[512];
222 static char err_msg[512];
223 regerror(regerr, &tc->regex, regerr_msg,
225 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 err = got_error_msg(GOT_ERR_REGEX, err_msg);
232 STAILQ_INSERT_HEAD(colors, tc, entry);
237 free_colors(struct tog_colors *colors)
239 struct tog_color *tc;
241 while (!STAILQ_EMPTY(colors)) {
242 tc = STAILQ_FIRST(colors);
243 STAILQ_REMOVE_HEAD(colors, entry);
249 static struct tog_color *
250 get_color(struct tog_colors *colors, int colorpair)
252 struct tog_color *tc = NULL;
254 STAILQ_FOREACH(tc, colors, entry) {
255 if (tc->colorpair == colorpair)
263 default_color_value(const char *envvar)
265 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 return COLOR_MAGENTA;
275 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 return COLOR_MAGENTA;
291 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
300 get_color_value(const char *envvar)
302 const char *val = getenv(envvar);
305 return default_color_value(envvar);
307 if (strcasecmp(val, "black") == 0)
309 if (strcasecmp(val, "red") == 0)
311 if (strcasecmp(val, "green") == 0)
313 if (strcasecmp(val, "yellow") == 0)
315 if (strcasecmp(val, "blue") == 0)
317 if (strcasecmp(val, "magenta") == 0)
318 return COLOR_MAGENTA;
319 if (strcasecmp(val, "cyan") == 0)
321 if (strcasecmp(val, "white") == 0)
323 if (strcasecmp(val, "default") == 0)
326 return default_color_value(envvar);
329 struct tog_diff_view_state {
330 struct got_object_id *id1, *id2;
331 const char *label1, *label2;
335 int first_displayed_line;
336 int last_displayed_line;
339 int ignore_whitespace;
341 struct got_repository *repo;
342 struct got_diff_line *lines;
347 /* passed from log or blame view; may be NULL */
348 struct tog_view *parent_view;
351 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 static volatile sig_atomic_t tog_thread_error;
354 struct tog_log_thread_args {
355 pthread_cond_t need_commits;
356 pthread_cond_t commit_loaded;
359 struct got_commit_graph *graph;
360 struct commit_queue *real_commits;
361 const char *in_repo_path;
362 struct got_object_id *start_id;
363 struct got_repository *repo;
367 struct commit_queue_entry **first_displayed_entry;
368 struct commit_queue_entry **selected_entry;
370 int *search_next_done;
374 regex_t *limit_regex;
375 struct commit_queue *limit_commits;
378 struct tog_log_view_state {
379 struct commit_queue *commits;
380 struct commit_queue_entry *first_displayed_entry;
381 struct commit_queue_entry *last_displayed_entry;
382 struct commit_queue_entry *selected_entry;
383 struct commit_queue real_commits;
388 struct got_repository *repo;
389 struct got_object_id *start_id;
392 struct tog_log_thread_args thread_args;
393 struct commit_queue_entry *matched_entry;
394 struct commit_queue_entry *search_entry;
395 struct tog_colors colors;
399 struct commit_queue limit_commits;
402 #define TOG_COLOR_DIFF_MINUS 1
403 #define TOG_COLOR_DIFF_PLUS 2
404 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 #define TOG_COLOR_DIFF_META 4
406 #define TOG_COLOR_TREE_SUBMODULE 5
407 #define TOG_COLOR_TREE_SYMLINK 6
408 #define TOG_COLOR_TREE_DIRECTORY 7
409 #define TOG_COLOR_TREE_EXECUTABLE 8
410 #define TOG_COLOR_COMMIT 9
411 #define TOG_COLOR_AUTHOR 10
412 #define TOG_COLOR_DATE 11
413 #define TOG_COLOR_REFS_HEADS 12
414 #define TOG_COLOR_REFS_TAGS 13
415 #define TOG_COLOR_REFS_REMOTES 14
416 #define TOG_COLOR_REFS_BACKUP 15
418 struct tog_blame_cb_args {
419 struct tog_blame_line *lines; /* one per line */
422 struct tog_view *view;
423 struct got_object_id *commit_id;
427 struct tog_blame_thread_args {
429 struct got_repository *repo;
430 struct tog_blame_cb_args *cb_args;
432 got_cancel_cb cancel_cb;
439 struct tog_blame_line *lines;
443 struct tog_blame_thread_args thread_args;
444 struct tog_blame_cb_args cb_args;
449 struct tog_blame_view_state {
450 int first_displayed_line;
451 int last_displayed_line;
453 int last_diffed_line;
457 struct got_object_id_queue blamed_commits;
458 struct got_object_qid *blamed_commit;
460 struct got_repository *repo;
461 struct got_object_id *commit_id;
462 struct got_object_id *id_to_log;
463 struct tog_blame blame;
465 struct tog_colors colors;
468 struct tog_parent_tree {
469 TAILQ_ENTRY(tog_parent_tree) entry;
470 struct got_tree_object *tree;
471 struct got_tree_entry *first_displayed_entry;
472 struct got_tree_entry *selected_entry;
476 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
478 struct tog_tree_view_state {
480 struct got_object_id *commit_id;/* commit which this tree belongs to */
481 struct got_tree_object *root; /* the commit's root tree entry */
482 struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 struct got_tree_entry *first_displayed_entry;
484 struct got_tree_entry *last_displayed_entry;
485 struct got_tree_entry *selected_entry;
486 int ndisplayed, selected, show_ids;
487 struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 struct got_repository *repo;
490 struct got_tree_entry *matched_entry;
491 struct tog_colors colors;
494 struct tog_reflist_entry {
495 TAILQ_ENTRY(tog_reflist_entry) entry;
496 struct got_reference *ref;
500 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
502 struct tog_ref_view_state {
503 struct tog_reflist_head refs;
504 struct tog_reflist_entry *first_displayed_entry;
505 struct tog_reflist_entry *last_displayed_entry;
506 struct tog_reflist_entry *selected_entry;
507 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 struct got_repository *repo;
509 struct tog_reflist_entry *matched_entry;
510 struct tog_colors colors;
513 struct tog_help_view_state {
518 int first_displayed_line;
519 int last_displayed_line;
524 enum tog_keymap_type type;
527 #define GENERATE_HELP \
528 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 KEY_("k C-p Up", "Move cursor or page up one line"), \
531 KEY_("j C-n Down", "Move cursor or page down one line"), \
532 KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 KEY_("C-u u", "Scroll the view up one half page"), \
535 KEY_("C-d d", "Scroll the view down one half page"), \
536 KEY_("g Home", "Go to line N (default: first line)"), \
537 KEY_("G End", "Go to line N (default: last line)"), \
538 KEY_("l Right", "Scroll the view right"), \
539 KEY_("h Left", "Scroll the view left"), \
540 KEY_("$", "Scroll view to the rightmost position"), \
541 KEY_("0", "Scroll view to the leftmost position"), \
542 KEY_("-", "Decrease size of the focussed split"), \
543 KEY_("+", "Increase size of the focussed split"), \
544 KEY_("Tab", "Switch focus between views"), \
545 KEY_("F", "Toggle fullscreen mode"), \
546 KEY_("/", "Open prompt to enter search term"), \
547 KEY_("n", "Find next line/token matching the current search term"), \
548 KEY_("N", "Find previous line/token matching the current search term"),\
549 KEY_("q", "Quit the focussed view"), \
550 KEY_("Q", "Quit tog"), \
552 KEYMAP_("Log", TOG_KEYMAP_LOG), \
553 KEY_("< ,", "Move cursor up one commit"), \
554 KEY_("> .", "Move cursor down one commit"), \
555 KEY_("Enter", "Open diff view of the selected commit"), \
556 KEY_("B", "Reload the log view and toggle display of merged commits"), \
557 KEY_("R", "Open ref view of all repository references"), \
558 KEY_("T", "Display tree view of the repository from the selected" \
560 KEY_("@", "Toggle between displaying author and committer name"), \
561 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
562 KEY_("C-g Backspace", "Cancel current search or log operation"), \
563 KEY_("C-l", "Reload the log view with new commits in the repository"), \
565 KEYMAP_("Diff", TOG_KEYMAP_DIFF), \
566 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
567 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
568 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
569 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
571 KEY_("(", "Go to the previous file in the diff"), \
572 KEY_(")", "Go to the next file in the diff"), \
573 KEY_("{", "Go to the previous hunk in the diff"), \
574 KEY_("}", "Go to the next hunk in the diff"), \
575 KEY_("[", "Decrease the number of context lines"), \
576 KEY_("]", "Increase the number of context lines"), \
577 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
579 KEYMAP_("Blame", TOG_KEYMAP_BLAME), \
580 KEY_("Enter", "Display diff view of the selected line's commit"), \
581 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
582 KEY_("L", "Open log view for the currently selected annotated line"), \
583 KEY_("C", "Reload view with the previously blamed commit"), \
584 KEY_("c", "Reload view with the version of the file found in the" \
585 " selected line's commit"), \
586 KEY_("p", "Reload view with the version of the file found in the" \
587 " selected line's parent commit"), \
589 KEYMAP_("Tree", TOG_KEYMAP_TREE), \
590 KEY_("Enter", "Enter selected directory or open blame view of the" \
592 KEY_("L", "Open log view for the selected entry"), \
593 KEY_("R", "Open ref view of all repository references"), \
594 KEY_("i", "Show object IDs for all tree entries"), \
595 KEY_("Backspace", "Return to the parent directory"), \
597 KEYMAP_("Ref", TOG_KEYMAP_REF), \
598 KEY_("Enter", "Display log view of the selected reference"), \
599 KEY_("T", "Display tree view of the selected reference"), \
600 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
601 KEY_("m", "Toggle display of last modified date for each reference"), \
602 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
603 KEY_("C-l", "Reload view with all repository references")
608 enum tog_keymap_type type;
612 * We implement two types of views: parent views and child views.
614 * The 'Tab' key switches focus between a parent view and its child view.
615 * Child views are shown side-by-side to their parent view, provided
616 * there is enough screen estate.
618 * When a new view is opened from within a parent view, this new view
619 * becomes a child view of the parent view, replacing any existing child.
621 * When a new view is opened from within a child view, this new view
622 * becomes a parent view which will obscure the views below until the
623 * user quits the new parent view by typing 'q'.
625 * This list of views contains parent views only.
626 * Child views are only pointed to by their parent view.
628 TAILQ_HEAD(tog_view_list_head, tog_view);
631 TAILQ_ENTRY(tog_view) entry;
634 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
635 int resized_y, resized_x; /* begin_y/x based on user resizing */
636 int maxx, x; /* max column and current start column */
637 int lines, cols; /* copies of LINES and COLS */
638 int nscrolled, offset; /* lines scrolled and hsplit line offset */
639 int gline, hiline; /* navigate to and highlight this nG line */
640 int ch, count; /* current keymap and count prefix */
641 int resized; /* set when in a resize event */
642 int focussed; /* Only set on one parent or child view at a time. */
644 struct tog_view *parent;
645 struct tog_view *child;
648 * This flag is initially set on parent views when a new child view
649 * is created. It gets toggled when the 'Tab' key switches focus
650 * between parent and child.
651 * The flag indicates whether focus should be passed on to our child
652 * view if this parent view gets picked for focus after another parent
653 * view was closed. This prevents child views from losing focus in such
658 enum tog_view_mode mode;
659 /* type-specific state */
660 enum tog_view_type type;
662 struct tog_diff_view_state diff;
663 struct tog_log_view_state log;
664 struct tog_blame_view_state blame;
665 struct tog_tree_view_state tree;
666 struct tog_ref_view_state ref;
667 struct tog_help_view_state help;
670 const struct got_error *(*show)(struct tog_view *);
671 const struct got_error *(*input)(struct tog_view **,
672 struct tog_view *, int);
673 const struct got_error *(*reset)(struct tog_view *);
674 const struct got_error *(*resize)(struct tog_view *, int);
675 const struct got_error *(*close)(struct tog_view *);
677 const struct got_error *(*search_start)(struct tog_view *);
678 const struct got_error *(*search_next)(struct tog_view *);
679 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
680 int **, int **, int **, int **);
683 #define TOG_SEARCH_FORWARD 1
684 #define TOG_SEARCH_BACKWARD 2
685 int search_next_done;
686 #define TOG_SEARCH_HAVE_MORE 1
687 #define TOG_SEARCH_NO_MORE 2
688 #define TOG_SEARCH_HAVE_NONE 3
693 static const struct got_error *open_diff_view(struct tog_view *,
694 struct got_object_id *, struct got_object_id *,
695 const char *, const char *, int, int, int, struct tog_view *,
696 struct got_repository *);
697 static const struct got_error *show_diff_view(struct tog_view *);
698 static const struct got_error *input_diff_view(struct tog_view **,
699 struct tog_view *, int);
700 static const struct got_error *reset_diff_view(struct tog_view *);
701 static const struct got_error* close_diff_view(struct tog_view *);
702 static const struct got_error *search_start_diff_view(struct tog_view *);
703 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
704 size_t *, int **, int **, int **, int **);
705 static const struct got_error *search_next_view_match(struct tog_view *);
707 static const struct got_error *open_log_view(struct tog_view *,
708 struct got_object_id *, struct got_repository *,
709 const char *, const char *, int);
710 static const struct got_error * show_log_view(struct tog_view *);
711 static const struct got_error *input_log_view(struct tog_view **,
712 struct tog_view *, int);
713 static const struct got_error *resize_log_view(struct tog_view *, int);
714 static const struct got_error *close_log_view(struct tog_view *);
715 static const struct got_error *search_start_log_view(struct tog_view *);
716 static const struct got_error *search_next_log_view(struct tog_view *);
718 static const struct got_error *open_blame_view(struct tog_view *, char *,
719 struct got_object_id *, struct got_repository *);
720 static const struct got_error *show_blame_view(struct tog_view *);
721 static const struct got_error *input_blame_view(struct tog_view **,
722 struct tog_view *, int);
723 static const struct got_error *reset_blame_view(struct tog_view *);
724 static const struct got_error *close_blame_view(struct tog_view *);
725 static const struct got_error *search_start_blame_view(struct tog_view *);
726 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
727 size_t *, int **, int **, int **, int **);
729 static const struct got_error *open_tree_view(struct tog_view *,
730 struct got_object_id *, const char *, struct got_repository *);
731 static const struct got_error *show_tree_view(struct tog_view *);
732 static const struct got_error *input_tree_view(struct tog_view **,
733 struct tog_view *, int);
734 static const struct got_error *close_tree_view(struct tog_view *);
735 static const struct got_error *search_start_tree_view(struct tog_view *);
736 static const struct got_error *search_next_tree_view(struct tog_view *);
738 static const struct got_error *open_ref_view(struct tog_view *,
739 struct got_repository *);
740 static const struct got_error *show_ref_view(struct tog_view *);
741 static const struct got_error *input_ref_view(struct tog_view **,
742 struct tog_view *, int);
743 static const struct got_error *close_ref_view(struct tog_view *);
744 static const struct got_error *search_start_ref_view(struct tog_view *);
745 static const struct got_error *search_next_ref_view(struct tog_view *);
747 static const struct got_error *open_help_view(struct tog_view *,
749 static const struct got_error *show_help_view(struct tog_view *);
750 static const struct got_error *input_help_view(struct tog_view **,
751 struct tog_view *, int);
752 static const struct got_error *reset_help_view(struct tog_view *);
753 static const struct got_error* close_help_view(struct tog_view *);
754 static const struct got_error *search_start_help_view(struct tog_view *);
755 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
756 size_t *, int **, int **, int **, int **);
758 static volatile sig_atomic_t tog_sigwinch_received;
759 static volatile sig_atomic_t tog_sigpipe_received;
760 static volatile sig_atomic_t tog_sigcont_received;
761 static volatile sig_atomic_t tog_sigint_received;
762 static volatile sig_atomic_t tog_sigterm_received;
765 tog_sigwinch(int signo)
767 tog_sigwinch_received = 1;
771 tog_sigpipe(int signo)
773 tog_sigpipe_received = 1;
777 tog_sigcont(int signo)
779 tog_sigcont_received = 1;
783 tog_sigint(int signo)
785 tog_sigint_received = 1;
789 tog_sigterm(int signo)
791 tog_sigterm_received = 1;
795 tog_fatal_signal_received(void)
797 return (tog_sigpipe_received ||
798 tog_sigint_received || tog_sigint_received);
801 static const struct got_error *
802 view_close(struct tog_view *view)
804 const struct got_error *err = NULL, *child_err = NULL;
807 child_err = view_close(view->child);
811 err = view->close(view);
813 del_panel(view->panel);
815 delwin(view->window);
817 return err ? err : child_err;
820 static struct tog_view *
821 view_open(int nlines, int ncols, int begin_y, int begin_x,
822 enum tog_view_type type)
824 struct tog_view *view = calloc(1, sizeof(*view));
832 view->nlines = nlines ? nlines : LINES - begin_y;
833 view->ncols = ncols ? ncols : COLS - begin_x;
834 view->begin_y = begin_y;
835 view->begin_x = begin_x;
836 view->window = newwin(nlines, ncols, begin_y, begin_x);
837 if (view->window == NULL) {
841 view->panel = new_panel(view->window);
842 if (view->panel == NULL ||
843 set_panel_userptr(view->panel, view) != OK) {
848 keypad(view->window, TRUE);
853 view_split_begin_x(int begin_x)
855 if (begin_x > 0 || COLS < 120)
857 return (COLS - MAX(COLS / 2, 80));
860 /* XXX Stub till we decide what to do. */
862 view_split_begin_y(int lines)
864 return lines * HSPLIT_SCALE;
867 static const struct got_error *view_resize(struct tog_view *);
869 static const struct got_error *
870 view_splitscreen(struct tog_view *view)
872 const struct got_error *err = NULL;
874 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
875 if (view->resized_y && view->resized_y < view->lines)
876 view->begin_y = view->resized_y;
878 view->begin_y = view_split_begin_y(view->nlines);
880 } else if (!view->resized) {
881 if (view->resized_x && view->resized_x < view->cols - 1 &&
883 view->begin_x = view->resized_x;
885 view->begin_x = view_split_begin_x(0);
888 view->nlines = LINES - view->begin_y;
889 view->ncols = COLS - view->begin_x;
892 err = view_resize(view);
896 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
897 view->parent->nlines = view->begin_y;
899 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
900 return got_error_from_errno("mvwin");
905 static const struct got_error *
906 view_fullscreen(struct tog_view *view)
908 const struct got_error *err = NULL;
911 view->begin_y = view->resized ? view->begin_y : 0;
912 view->nlines = view->resized ? view->nlines : LINES;
916 err = view_resize(view);
920 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 return got_error_from_errno("mvwin");
927 view_is_parent_view(struct tog_view *view)
929 return view->parent == NULL;
933 view_is_splitscreen(struct tog_view *view)
935 return view->begin_x > 0 || view->begin_y > 0;
939 view_is_fullscreen(struct tog_view *view)
941 return view->nlines == LINES && view->ncols == COLS;
945 view_is_hsplit_top(struct tog_view *view)
947 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
948 view_is_splitscreen(view->child);
952 view_border(struct tog_view *view)
955 const struct tog_view *view_above;
958 return view_border(view->parent);
960 panel = panel_above(view->panel);
964 view_above = panel_userptr(panel);
965 if (view->mode == TOG_VIEW_SPLIT_HRZN)
966 mvwhline(view->window, view_above->begin_y - 1,
967 view->begin_x, got_locale_is_utf8() ?
968 ACS_HLINE : '-', view->ncols);
970 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
971 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
974 static const struct got_error *view_init_hsplit(struct tog_view *, int);
975 static const struct got_error *request_log_commits(struct tog_view *);
976 static const struct got_error *offset_selection_down(struct tog_view *);
977 static void offset_selection_up(struct tog_view *);
978 static void view_get_split(struct tog_view *, int *, int *);
980 static const struct got_error *
981 view_resize(struct tog_view *view)
983 const struct got_error *err = NULL;
984 int dif, nlines, ncols;
986 dif = LINES - view->lines; /* line difference */
988 if (view->lines > LINES)
989 nlines = view->nlines - (view->lines - LINES);
991 nlines = view->nlines + (LINES - view->lines);
992 if (view->cols > COLS)
993 ncols = view->ncols - (view->cols - COLS);
995 ncols = view->ncols + (COLS - view->cols);
998 int hs = view->child->begin_y;
1000 if (!view_is_fullscreen(view))
1001 view->child->begin_x = view_split_begin_x(view->begin_x);
1002 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1003 view->child->begin_x == 0) {
1006 view_fullscreen(view->child);
1007 if (view->child->focussed)
1008 show_panel(view->child->panel);
1010 show_panel(view->panel);
1012 ncols = view->child->begin_x;
1014 view_splitscreen(view->child);
1015 show_panel(view->child->panel);
1018 * XXX This is ugly and needs to be moved into the above
1019 * logic but "works" for now and my attempts at moving it
1020 * break either 'tab' or 'F' key maps in horizontal splits.
1023 err = view_splitscreen(view->child);
1026 if (dif < 0) { /* top split decreased */
1027 err = offset_selection_down(view);
1034 show_panel(view->child->panel);
1035 nlines = view->nlines;
1037 } else if (view->parent == NULL)
1040 if (view->resize && dif > 0) {
1041 err = view->resize(view, dif);
1046 if (wresize(view->window, nlines, ncols) == ERR)
1047 return got_error_from_errno("wresize");
1048 if (replace_panel(view->panel, view->window) == ERR)
1049 return got_error_from_errno("replace_panel");
1050 wclear(view->window);
1052 view->nlines = nlines;
1053 view->ncols = ncols;
1054 view->lines = LINES;
1060 static const struct got_error *
1061 resize_log_view(struct tog_view *view, int increase)
1063 struct tog_log_view_state *s = &view->state.log;
1064 const struct got_error *err = NULL;
1067 if (s->selected_entry)
1068 n = s->selected_entry->idx + view->lines - s->selected;
1071 * Request commits to account for the increased
1072 * height so we have enough to populate the view.
1074 if (s->commits->ncommits < n) {
1075 view->nscrolled = n - s->commits->ncommits + increase + 1;
1076 err = request_log_commits(view);
1083 view_adjust_offset(struct tog_view *view, int n)
1088 if (view->parent && view->parent->offset) {
1089 if (view->parent->offset + n >= 0)
1090 view->parent->offset += n;
1092 view->parent->offset = 0;
1093 } else if (view->offset) {
1094 if (view->offset - n >= 0)
1101 static const struct got_error *
1102 view_resize_split(struct tog_view *view, int resize)
1104 const struct got_error *err = NULL;
1105 struct tog_view *v = NULL;
1112 if (!v->child || !view_is_splitscreen(v->child))
1115 v->resized = v->child->resized = resize; /* lock for resize event */
1117 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1118 if (v->child->resized_y)
1119 v->child->begin_y = v->child->resized_y;
1121 v->child->begin_y -= resize;
1123 v->child->begin_y += resize;
1124 if (v->child->begin_y < 3) {
1126 v->child->begin_y = 3;
1127 } else if (v->child->begin_y > LINES - 1) {
1129 v->child->begin_y = LINES - 1;
1132 v->child->ncols = COLS;
1133 view_adjust_offset(view, resize);
1134 err = view_init_hsplit(v, v->child->begin_y);
1137 v->child->resized_y = v->child->begin_y;
1139 if (v->child->resized_x)
1140 v->child->begin_x = v->child->resized_x;
1142 v->child->begin_x -= resize;
1144 v->child->begin_x += resize;
1145 if (v->child->begin_x < 11) {
1147 v->child->begin_x = 11;
1148 } else if (v->child->begin_x > COLS - 1) {
1150 v->child->begin_x = COLS - 1;
1152 v->child->resized_x = v->child->begin_x;
1155 v->child->mode = v->mode;
1156 v->child->nlines = v->lines - v->child->begin_y;
1157 v->child->ncols = v->cols - v->child->begin_x;
1160 err = view_fullscreen(v);
1163 err = view_splitscreen(v->child);
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1168 err = offset_selection_down(v->child);
1174 err = v->resize(v, 0);
1175 else if (v->child->resize)
1176 err = v->child->resize(v->child, 0);
1178 v->resized = v->child->resized = 0;
1184 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1186 struct tog_view *v = src->child ? src->child : src;
1188 dst->resized_x = v->resized_x;
1189 dst->resized_y = v->resized_y;
1192 static const struct got_error *
1193 view_close_child(struct tog_view *view)
1195 const struct got_error *err = NULL;
1197 if (view->child == NULL)
1200 err = view_close(view->child);
1205 static const struct got_error *
1206 view_set_child(struct tog_view *view, struct tog_view *child)
1208 const struct got_error *err = NULL;
1210 view->child = child;
1211 child->parent = view;
1213 err = view_resize(view);
1217 if (view->child->resized_x || view->child->resized_y)
1218 err = view_resize_split(view, 0);
1223 static const struct got_error *view_dispatch_request(struct tog_view **,
1224 struct tog_view *, enum tog_view_type, int, int);
1226 static const struct got_error *
1227 view_request_new(struct tog_view **requested, struct tog_view *view,
1228 enum tog_view_type request)
1230 struct tog_view *new_view = NULL;
1231 const struct got_error *err;
1236 if (view_is_parent_view(view))
1237 view_get_split(view, &y, &x);
1239 err = view_dispatch_request(&new_view, view, request, y, x);
1243 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1244 err = view_init_hsplit(view, y);
1250 new_view->focussed = 1;
1251 new_view->mode = view->mode;
1252 new_view->nlines = view->lines - y;
1254 if (view_is_parent_view(view)) {
1255 view_transfer_size(new_view, view);
1256 err = view_close_child(view);
1259 err = view_set_child(view, new_view);
1262 view->focus_child = 1;
1264 *requested = new_view;
1270 tog_resizeterm(void)
1273 struct winsize size;
1275 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1276 cols = 80; /* Default */
1280 lines = size.ws_row;
1282 resize_term(lines, cols);
1285 static const struct got_error *
1286 view_search_start(struct tog_view *view)
1288 const struct got_error *err = NULL;
1289 struct tog_view *v = view;
1293 if (view->search_started) {
1294 regfree(&view->regex);
1295 view->searching = 0;
1296 memset(&view->regmatch, 0, sizeof(view->regmatch));
1298 view->search_started = 0;
1300 if (view->nlines < 1)
1303 if (view_is_hsplit_top(view))
1306 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1307 wclrtoeol(v->window);
1309 nodelay(view->window, FALSE); /* block for search term input */
1312 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1313 wrefresh(v->window);
1316 nodelay(view->window, TRUE);
1320 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1321 err = view->search_start(view);
1323 regfree(&view->regex);
1326 view->search_started = 1;
1327 view->searching = TOG_SEARCH_FORWARD;
1328 view->search_next_done = 0;
1329 view->search_next(view);
1335 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1336 static const struct got_error *
1337 switch_split(struct tog_view *view)
1339 const struct got_error *err = NULL;
1340 struct tog_view *v = NULL;
1347 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1348 v->mode = TOG_VIEW_SPLIT_VERT;
1350 v->mode = TOG_VIEW_SPLIT_HRZN;
1354 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1355 v->mode = TOG_VIEW_SPLIT_NONE;
1357 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1358 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1359 v->child->begin_y = v->child->resized_y;
1360 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1361 v->child->begin_x = v->child->resized_x;
1364 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1366 v->child->ncols = COLS;
1367 v->child->nscrolled = LINES - v->child->nlines;
1369 err = view_init_hsplit(v, v->child->begin_y);
1373 v->child->mode = v->mode;
1374 v->child->nlines = v->lines - v->child->begin_y;
1377 err = view_fullscreen(v);
1380 err = view_splitscreen(v->child);
1384 if (v->mode == TOG_VIEW_SPLIT_NONE)
1385 v->mode = TOG_VIEW_SPLIT_VERT;
1386 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1387 err = offset_selection_down(v);
1390 err = offset_selection_down(v->child);
1394 offset_selection_up(v);
1395 offset_selection_up(v->child);
1398 err = v->resize(v, 0);
1399 else if (v->child->resize)
1400 err = v->child->resize(v->child, 0);
1406 * Compute view->count from numeric input. Assign total to view->count and
1407 * return first non-numeric key entered.
1410 get_compound_key(struct tog_view *view, int c)
1412 struct tog_view *v = view;
1415 if (view_is_hsplit_top(view))
1417 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1421 cbreak(); /* block for input */
1422 nodelay(view->window, FALSE);
1423 wmove(v->window, v->nlines - 1, 0);
1424 wclrtoeol(v->window);
1425 waddch(v->window, ':');
1428 x = getcurx(v->window);
1429 if (x != ERR && x < view->ncols) {
1430 waddch(v->window, c);
1431 wrefresh(v->window);
1435 * Don't overflow. Max valid request should be the greatest
1436 * between the longest and total lines; cap at 10 million.
1441 n = n * 10 + (c - '0');
1442 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1444 if (c == 'G' || c == 'g') { /* nG key map */
1445 view->gline = view->hiline = n;
1450 /* Massage excessive or inapplicable values at the input handler. */
1456 static const struct got_error *
1457 view_input(struct tog_view **new, int *done, struct tog_view *view,
1458 struct tog_view_list_head *views)
1460 const struct got_error *err = NULL;
1466 /* Clear "no matches" indicator. */
1467 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1468 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1469 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1473 if (view->searching && !view->search_next_done) {
1474 errcode = pthread_mutex_unlock(&tog_mutex);
1476 return got_error_set_errno(errcode,
1477 "pthread_mutex_unlock");
1479 errcode = pthread_mutex_lock(&tog_mutex);
1481 return got_error_set_errno(errcode,
1482 "pthread_mutex_lock");
1483 view->search_next(view);
1487 /* Allow threads to make progress while we are waiting for input. */
1488 errcode = pthread_mutex_unlock(&tog_mutex);
1490 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1491 /* If we have an unfinished count, let C-g or backspace abort. */
1492 if (view->count && --view->count) {
1494 nodelay(view->window, TRUE);
1495 ch = wgetch(view->window);
1496 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1501 ch = wgetch(view->window);
1502 if (ch >= '1' && ch <= '9')
1503 view->ch = ch = get_compound_key(view, ch);
1505 if (view->hiline && ch != ERR && ch != 0)
1506 view->hiline = 0; /* key pressed, clear line highlight */
1507 nodelay(view->window, TRUE);
1508 errcode = pthread_mutex_lock(&tog_mutex);
1510 return got_error_set_errno(errcode, "pthread_mutex_lock");
1512 if (tog_sigwinch_received || tog_sigcont_received) {
1514 tog_sigwinch_received = 0;
1515 tog_sigcont_received = 0;
1516 TAILQ_FOREACH(v, views, entry) {
1517 err = view_resize(v);
1520 err = v->input(new, v, KEY_RESIZE);
1524 err = view_resize(v->child);
1527 err = v->child->input(new, v->child,
1531 if (v->child->resized_x || v->child->resized_y) {
1532 err = view_resize_split(v, 0);
1544 if (view->type == TOG_VIEW_HELP)
1545 err = view->reset(view);
1547 err = view_request_new(new, view, TOG_VIEW_HELP);
1553 view->child->focussed = 1;
1554 view->focus_child = 1;
1555 } else if (view->parent) {
1557 view->parent->focussed = 1;
1558 view->parent->focus_child = 0;
1559 if (!view_is_splitscreen(view)) {
1560 if (view->parent->resize) {
1561 err = view->parent->resize(view->parent,
1566 offset_selection_up(view->parent);
1567 err = view_fullscreen(view->parent);
1574 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1575 if (view->parent->resize) {
1576 /* might need more commits to fill fullscreen */
1577 err = view->parent->resize(view->parent, 0);
1581 offset_selection_up(view->parent);
1583 err = view->input(new, view, ch);
1591 if (view_is_parent_view(view)) {
1592 if (view->child == NULL)
1594 if (view_is_splitscreen(view->child)) {
1596 view->child->focussed = 1;
1597 err = view_fullscreen(view->child);
1599 err = view_splitscreen(view->child);
1601 err = view_resize_split(view, 0);
1605 err = view->child->input(new, view->child,
1608 if (view_is_splitscreen(view)) {
1609 view->parent->focussed = 0;
1611 err = view_fullscreen(view);
1613 err = view_splitscreen(view);
1614 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1615 err = view_resize(view->parent);
1617 err = view_resize_split(view, 0);
1621 err = view->input(new, view, KEY_RESIZE);
1626 err = view->resize(view, 0);
1631 err = offset_selection_down(view->parent);
1633 err = offset_selection_down(view);
1637 err = switch_split(view);
1640 err = view_resize_split(view, -1);
1643 err = view_resize_split(view, 1);
1649 if (view->search_start)
1650 view_search_start(view);
1652 err = view->input(new, view, ch);
1656 if (view->search_started && view->search_next) {
1657 view->searching = (ch == 'n' ?
1658 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1659 view->search_next_done = 0;
1660 view->search_next(view);
1662 err = view->input(new, view, ch);
1665 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1666 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1668 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1669 TAILQ_FOREACH(v, views, entry) {
1675 if (v->child && v->child->reset) {
1676 err = v->child->reset(v->child);
1683 err = view->input(new, view, ch);
1691 view_needs_focus_indication(struct tog_view *view)
1693 if (view_is_parent_view(view)) {
1694 if (view->child == NULL || view->child->focussed)
1696 if (!view_is_splitscreen(view->child))
1698 } else if (!view_is_splitscreen(view))
1701 return view->focussed;
1704 static const struct got_error *
1705 view_loop(struct tog_view *view)
1707 const struct got_error *err = NULL;
1708 struct tog_view_list_head views;
1709 struct tog_view *new_view;
1711 int fast_refresh = 10;
1712 int done = 0, errcode;
1714 mode = getenv("TOG_VIEW_SPLIT_MODE");
1715 if (!mode || !(*mode == 'h' || *mode == 'H'))
1716 view->mode = TOG_VIEW_SPLIT_VERT;
1718 view->mode = TOG_VIEW_SPLIT_HRZN;
1720 errcode = pthread_mutex_lock(&tog_mutex);
1722 return got_error_set_errno(errcode, "pthread_mutex_lock");
1725 TAILQ_INSERT_HEAD(&views, view, entry);
1728 err = view->show(view);
1733 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1734 !tog_fatal_signal_received()) {
1735 /* Refresh fast during initialization, then become slower. */
1736 if (fast_refresh && fast_refresh-- == 0)
1737 halfdelay(10); /* switch to once per second */
1739 err = view_input(&new_view, &done, view, &views);
1743 struct tog_view *v, *prev = NULL;
1745 if (view_is_parent_view(view))
1746 prev = TAILQ_PREV(view, tog_view_list_head,
1748 else if (view->parent)
1749 prev = view->parent;
1752 view->parent->child = NULL;
1753 view->parent->focus_child = 0;
1754 /* Restore fullscreen line height. */
1755 view->parent->nlines = view->parent->lines;
1756 err = view_resize(view->parent);
1759 /* Make resized splits persist. */
1760 view_transfer_size(view->parent, view);
1762 TAILQ_REMOVE(&views, view, entry);
1764 err = view_close(view);
1769 TAILQ_FOREACH(v, &views, entry) {
1773 if (view == NULL && new_view == NULL) {
1774 /* No view has focus. Try to pick one. */
1777 else if (!TAILQ_EMPTY(&views)) {
1778 view = TAILQ_LAST(&views,
1779 tog_view_list_head);
1782 if (view->focus_child) {
1783 view->child->focussed = 1;
1791 struct tog_view *v, *t;
1792 /* Only allow one parent view per type. */
1793 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1794 if (v->type != new_view->type)
1796 TAILQ_REMOVE(&views, v, entry);
1797 err = view_close(v);
1802 TAILQ_INSERT_TAIL(&views, new_view, entry);
1806 if (view_is_parent_view(view)) {
1807 if (view->child && view->child->focussed)
1810 if (view->parent && view->parent->focussed)
1811 view = view->parent;
1813 show_panel(view->panel);
1814 if (view->child && view_is_splitscreen(view->child))
1815 show_panel(view->child->panel);
1816 if (view->parent && view_is_splitscreen(view)) {
1817 err = view->parent->show(view->parent);
1821 err = view->show(view);
1825 err = view->child->show(view->child);
1834 while (!TAILQ_EMPTY(&views)) {
1835 const struct got_error *close_err;
1836 view = TAILQ_FIRST(&views);
1837 TAILQ_REMOVE(&views, view, entry);
1838 close_err = view_close(view);
1839 if (close_err && err == NULL)
1843 errcode = pthread_mutex_unlock(&tog_mutex);
1844 if (errcode && err == NULL)
1845 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1855 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1860 /* Create newly allocated wide-character string equivalent to a byte string. */
1861 static const struct got_error *
1862 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1865 const struct got_error *err = NULL;
1868 *wlen = mbstowcs(NULL, s, 0);
1869 if (*wlen == (size_t)-1) {
1871 if (errno != EILSEQ)
1872 return got_error_from_errno("mbstowcs");
1874 /* byte string invalid in current encoding; try to "fix" it */
1875 err = got_mbsavis(&vis, &vislen, s);
1878 *wlen = mbstowcs(NULL, vis, 0);
1879 if (*wlen == (size_t)-1) {
1880 err = got_error_from_errno("mbstowcs"); /* give up */
1885 *ws = calloc(*wlen + 1, sizeof(**ws));
1887 err = got_error_from_errno("calloc");
1891 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1892 err = got_error_from_errno("mbstowcs");
1903 static const struct got_error *
1904 expand_tab(char **ptr, const char *src)
1907 size_t len, n, idx = 0, sz = 0;
1910 n = len = strlen(src);
1911 dst = malloc(n + 1);
1913 return got_error_from_errno("malloc");
1915 while (idx < len && src[idx]) {
1916 const char c = src[idx];
1919 size_t nb = TABSIZE - sz % TABSIZE;
1922 p = realloc(dst, n + nb);
1925 return got_error_from_errno("realloc");
1930 memset(dst + sz, ' ', nb);
1933 dst[sz++] = src[idx];
1943 * Advance at most n columns from wline starting at offset off.
1944 * Return the index to the first character after the span operation.
1945 * Return the combined column width of all spanned wide character in
1949 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1951 int width, i, cols = 0;
1958 for (i = off; wline[i] != L'\0'; ++i) {
1959 if (wline[i] == L'\t')
1960 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1962 width = wcwidth(wline[i]);
1969 if (cols + width > n)
1979 * Format a line for display, ensuring that it won't overflow a width limit.
1980 * With scrolling, the width returned refers to the scrolled version of the
1981 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1983 static const struct got_error *
1984 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1985 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1987 const struct got_error *err = NULL;
1989 wchar_t *wline = NULL;
1998 err = expand_tab(&exstr, line);
2003 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2008 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2010 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2011 wline[wlen - 1] = L'\0';
2014 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2015 wline[wlen - 1] = L'\0';
2019 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2025 *scrollxp = scrollx;
2033 static const struct got_error*
2034 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2035 struct got_object_id *id, struct got_repository *repo)
2037 static const struct got_error *err = NULL;
2038 struct got_reflist_entry *re;
2044 TAILQ_FOREACH(re, refs, entry) {
2045 struct got_tag_object *tag = NULL;
2046 struct got_object_id *ref_id;
2049 name = got_ref_get_name(re->ref);
2050 if (strcmp(name, GOT_REF_HEAD) == 0)
2052 if (strncmp(name, "refs/", 5) == 0)
2054 if (strncmp(name, "got/", 4) == 0 &&
2055 strncmp(name, "got/backup/", 11) != 0)
2057 if (strncmp(name, "heads/", 6) == 0)
2059 if (strncmp(name, "remotes/", 8) == 0) {
2061 s = strstr(name, "/" GOT_REF_HEAD);
2062 if (s != NULL && s[strlen(s)] == '\0')
2065 err = got_ref_resolve(&ref_id, repo, re->ref);
2068 if (strncmp(name, "tags/", 5) == 0) {
2069 err = got_object_open_as_tag(&tag, repo, ref_id);
2071 if (err->code != GOT_ERR_OBJ_TYPE) {
2075 /* Ref points at something other than a tag. */
2080 cmp = got_object_id_cmp(tag ?
2081 got_object_tag_get_object_id(tag) : ref_id, id);
2084 got_object_tag_close(tag);
2088 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2089 s ? ", " : "", name) == -1) {
2090 err = got_error_from_errno("asprintf");
2101 static const struct got_error *
2102 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2107 smallerthan = strchr(author, '<');
2108 if (smallerthan && smallerthan[1] != '\0')
2109 author = smallerthan + 1;
2110 author[strcspn(author, "@>")] = '\0';
2111 return format_line(wauthor, author_width, NULL, author, 0, limit,
2115 static const struct got_error *
2116 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2117 struct got_object_id *id, const size_t date_display_cols,
2118 int author_display_cols)
2120 struct tog_log_view_state *s = &view->state.log;
2121 const struct got_error *err = NULL;
2122 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2123 char *logmsg0 = NULL, *logmsg = NULL;
2124 char *author = NULL;
2125 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2126 int author_width, logmsg_width;
2127 char *newline, *line = NULL;
2128 int col, limit, scrollx;
2129 const int avail = view->ncols;
2131 time_t committer_time;
2132 struct tog_color *tc;
2134 committer_time = got_object_commit_get_committer_time(commit);
2135 if (gmtime_r(&committer_time, &tm) == NULL)
2136 return got_error_from_errno("gmtime_r");
2137 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2138 return got_error(GOT_ERR_NO_SPACE);
2140 if (avail <= date_display_cols)
2141 limit = MIN(sizeof(datebuf) - 1, avail);
2143 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2144 tc = get_color(&s->colors, TOG_COLOR_DATE);
2146 wattr_on(view->window,
2147 COLOR_PAIR(tc->colorpair), NULL);
2148 waddnstr(view->window, datebuf, limit);
2150 wattr_off(view->window,
2151 COLOR_PAIR(tc->colorpair), NULL);
2158 err = got_object_id_str(&id_str, id);
2161 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2163 wattr_on(view->window,
2164 COLOR_PAIR(tc->colorpair), NULL);
2165 wprintw(view->window, "%.8s ", id_str);
2167 wattr_off(view->window,
2168 COLOR_PAIR(tc->colorpair), NULL);
2175 if (s->use_committer)
2176 author = strdup(got_object_commit_get_committer(commit));
2178 author = strdup(got_object_commit_get_author(commit));
2179 if (author == NULL) {
2180 err = got_error_from_errno("strdup");
2183 err = format_author(&wauthor, &author_width, author, avail - col, col);
2186 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2188 wattr_on(view->window,
2189 COLOR_PAIR(tc->colorpair), NULL);
2190 waddwstr(view->window, wauthor);
2191 col += author_width;
2192 while (col < avail && author_width < author_display_cols + 2) {
2193 waddch(view->window, ' ');
2198 wattr_off(view->window,
2199 COLOR_PAIR(tc->colorpair), NULL);
2203 err = got_object_commit_get_logmsg(&logmsg0, commit);
2207 while (*logmsg == '\n')
2209 newline = strchr(logmsg, '\n');
2212 limit = avail - col;
2213 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2214 limit--; /* for the border */
2215 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2219 waddwstr(view->window, &wlogmsg[scrollx]);
2220 col += MAX(logmsg_width, 0);
2221 while (col < avail) {
2222 waddch(view->window, ' ');
2234 static struct commit_queue_entry *
2235 alloc_commit_queue_entry(struct got_commit_object *commit,
2236 struct got_object_id *id)
2238 struct commit_queue_entry *entry;
2239 struct got_object_id *dup;
2241 entry = calloc(1, sizeof(*entry));
2245 dup = got_object_id_dup(id);
2252 entry->commit = commit;
2257 pop_commit(struct commit_queue *commits)
2259 struct commit_queue_entry *entry;
2261 entry = TAILQ_FIRST(&commits->head);
2262 TAILQ_REMOVE(&commits->head, entry, entry);
2263 got_object_commit_close(entry->commit);
2264 commits->ncommits--;
2270 free_commits(struct commit_queue *commits)
2272 while (!TAILQ_EMPTY(&commits->head))
2273 pop_commit(commits);
2276 static const struct got_error *
2277 match_commit(int *have_match, struct got_object_id *id,
2278 struct got_commit_object *commit, regex_t *regex)
2280 const struct got_error *err = NULL;
2281 regmatch_t regmatch;
2282 char *id_str = NULL, *logmsg = NULL;
2286 err = got_object_id_str(&id_str, id);
2290 err = got_object_commit_get_logmsg(&logmsg, commit);
2294 if (regexec(regex, got_object_commit_get_author(commit), 1,
2295 ®match, 0) == 0 ||
2296 regexec(regex, got_object_commit_get_committer(commit), 1,
2297 ®match, 0) == 0 ||
2298 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2299 regexec(regex, logmsg, 1, ®match, 0) == 0)
2307 static const struct got_error *
2308 queue_commits(struct tog_log_thread_args *a)
2310 const struct got_error *err = NULL;
2313 * We keep all commits open throughout the lifetime of the log
2314 * view in order to avoid having to re-fetch commits from disk
2315 * while updating the display.
2318 struct got_object_id id;
2319 struct got_commit_object *commit;
2320 struct commit_queue_entry *entry;
2321 int limit_match = 0;
2324 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2329 err = got_object_open_as_commit(&commit, a->repo, &id);
2332 entry = alloc_commit_queue_entry(commit, &id);
2333 if (entry == NULL) {
2334 err = got_error_from_errno("alloc_commit_queue_entry");
2338 errcode = pthread_mutex_lock(&tog_mutex);
2340 err = got_error_set_errno(errcode,
2341 "pthread_mutex_lock");
2345 entry->idx = a->real_commits->ncommits;
2346 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2347 a->real_commits->ncommits++;
2350 err = match_commit(&limit_match, &id, commit,
2356 struct commit_queue_entry *matched;
2358 matched = alloc_commit_queue_entry(
2359 entry->commit, entry->id);
2360 if (matched == NULL) {
2361 err = got_error_from_errno(
2362 "alloc_commit_queue_entry");
2366 err = got_object_commit_dup(&matched->commit,
2371 matched->idx = a->limit_commits->ncommits;
2372 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2374 a->limit_commits->ncommits++;
2378 * This is how we signal log_thread() that we
2379 * have found a match, and that it should be
2380 * counted as a new entry for the view.
2382 a->limit_match = limit_match;
2385 if (*a->searching == TOG_SEARCH_FORWARD &&
2386 !*a->search_next_done) {
2388 err = match_commit(&have_match, &id, commit, a->regex);
2393 if (limit_match && have_match)
2394 *a->search_next_done =
2395 TOG_SEARCH_HAVE_MORE;
2396 } else if (have_match)
2397 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2400 errcode = pthread_mutex_unlock(&tog_mutex);
2401 if (errcode && err == NULL)
2402 err = got_error_set_errno(errcode,
2403 "pthread_mutex_unlock");
2406 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2412 select_commit(struct tog_log_view_state *s)
2414 struct commit_queue_entry *entry;
2417 entry = s->first_displayed_entry;
2419 if (ncommits == s->selected) {
2420 s->selected_entry = entry;
2423 entry = TAILQ_NEXT(entry, entry);
2428 static const struct got_error *
2429 draw_commits(struct tog_view *view)
2431 const struct got_error *err = NULL;
2432 struct tog_log_view_state *s = &view->state.log;
2433 struct commit_queue_entry *entry = s->selected_entry;
2434 int limit = view->nlines;
2436 int ncommits, author_cols = 4;
2437 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2438 char *refs_str = NULL;
2440 struct tog_color *tc;
2441 static const size_t date_display_cols = 12;
2443 if (view_is_hsplit_top(view))
2444 --limit; /* account for border */
2446 if (s->selected_entry &&
2447 !(view->searching && view->search_next_done == 0)) {
2448 struct got_reflist_head *refs;
2449 err = got_object_id_str(&id_str, s->selected_entry->id);
2452 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2453 s->selected_entry->id);
2455 err = build_refs_str(&refs_str, refs,
2456 s->selected_entry->id, s->repo);
2462 if (s->thread_args.commits_needed == 0)
2463 halfdelay(10); /* disable fast refresh */
2465 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2466 if (asprintf(&ncommits_str, " [%d/%d] %s",
2467 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2468 (view->searching && !view->search_next_done) ?
2469 "searching..." : "loading...") == -1) {
2470 err = got_error_from_errno("asprintf");
2474 const char *search_str = NULL;
2475 const char *limit_str = NULL;
2477 if (view->searching) {
2478 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2479 search_str = "no more matches";
2480 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2481 search_str = "no matches found";
2482 else if (!view->search_next_done)
2483 search_str = "searching...";
2486 if (s->limit_view && s->commits->ncommits == 0)
2487 limit_str = "no matches found";
2489 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2490 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2491 search_str ? search_str : (refs_str ? refs_str : ""),
2492 limit_str ? limit_str : "") == -1) {
2493 err = got_error_from_errno("asprintf");
2498 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2499 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2500 "........................................",
2501 s->in_repo_path, ncommits_str) == -1) {
2502 err = got_error_from_errno("asprintf");
2506 } else if (asprintf(&header, "commit %s%s",
2507 id_str ? id_str : "........................................",
2508 ncommits_str) == -1) {
2509 err = got_error_from_errno("asprintf");
2513 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2517 werase(view->window);
2519 if (view_needs_focus_indication(view))
2520 wstandout(view->window);
2521 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2523 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2524 waddwstr(view->window, wline);
2525 while (width < view->ncols) {
2526 waddch(view->window, ' ');
2530 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2531 if (view_needs_focus_indication(view))
2532 wstandend(view->window);
2537 /* Grow author column size if necessary, and set view->maxx. */
2538 entry = s->first_displayed_entry;
2542 struct got_commit_object *c = entry->commit;
2543 char *author, *eol, *msg, *msg0;
2544 wchar_t *wauthor, *wmsg;
2546 if (ncommits >= limit - 1)
2548 if (s->use_committer)
2549 author = strdup(got_object_commit_get_committer(c));
2551 author = strdup(got_object_commit_get_author(c));
2552 if (author == NULL) {
2553 err = got_error_from_errno("strdup");
2556 err = format_author(&wauthor, &width, author, COLS,
2558 if (author_cols < width)
2559 author_cols = width;
2564 err = got_object_commit_get_logmsg(&msg0, c);
2568 while (*msg == '\n')
2570 if ((eol = strchr(msg, '\n')))
2572 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2573 date_display_cols + author_cols, 0);
2576 view->maxx = MAX(view->maxx, width);
2580 entry = TAILQ_NEXT(entry, entry);
2583 entry = s->first_displayed_entry;
2584 s->last_displayed_entry = s->first_displayed_entry;
2587 if (ncommits >= limit - 1)
2589 if (ncommits == s->selected)
2590 wstandout(view->window);
2591 err = draw_commit(view, entry->commit, entry->id,
2592 date_display_cols, author_cols);
2593 if (ncommits == s->selected)
2594 wstandend(view->window);
2598 s->last_displayed_entry = entry;
2599 entry = TAILQ_NEXT(entry, entry);
2612 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2614 struct commit_queue_entry *entry;
2617 entry = TAILQ_FIRST(&s->commits->head);
2618 if (s->first_displayed_entry == entry)
2621 entry = s->first_displayed_entry;
2622 while (entry && nscrolled < maxscroll) {
2623 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2625 s->first_displayed_entry = entry;
2631 static const struct got_error *
2632 trigger_log_thread(struct tog_view *view, int wait)
2634 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2637 halfdelay(1); /* fast refresh while loading commits */
2639 while (!ta->log_complete && !tog_thread_error &&
2640 (ta->commits_needed > 0 || ta->load_all)) {
2641 /* Wake the log thread. */
2642 errcode = pthread_cond_signal(&ta->need_commits);
2644 return got_error_set_errno(errcode,
2645 "pthread_cond_signal");
2648 * The mutex will be released while the view loop waits
2649 * in wgetch(), at which time the log thread will run.
2654 /* Display progress update in log view. */
2655 show_log_view(view);
2659 /* Wait right here while next commit is being loaded. */
2660 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2662 return got_error_set_errno(errcode,
2663 "pthread_cond_wait");
2665 /* Display progress update in log view. */
2666 show_log_view(view);
2674 static const struct got_error *
2675 request_log_commits(struct tog_view *view)
2677 struct tog_log_view_state *state = &view->state.log;
2678 const struct got_error *err = NULL;
2680 if (state->thread_args.log_complete)
2683 state->thread_args.commits_needed += view->nscrolled;
2684 err = trigger_log_thread(view, 1);
2685 view->nscrolled = 0;
2690 static const struct got_error *
2691 log_scroll_down(struct tog_view *view, int maxscroll)
2693 struct tog_log_view_state *s = &view->state.log;
2694 const struct got_error *err = NULL;
2695 struct commit_queue_entry *pentry;
2696 int nscrolled = 0, ncommits_needed;
2698 if (s->last_displayed_entry == NULL)
2701 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2702 if (s->commits->ncommits < ncommits_needed &&
2703 !s->thread_args.log_complete) {
2705 * Ask the log thread for required amount of commits.
2707 s->thread_args.commits_needed +=
2708 ncommits_needed - s->commits->ncommits;
2709 err = trigger_log_thread(view, 1);
2715 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2716 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2719 s->last_displayed_entry = pentry ?
2720 pentry : s->last_displayed_entry;;
2722 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2725 s->first_displayed_entry = pentry;
2726 } while (++nscrolled < maxscroll);
2728 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2729 view->nscrolled += nscrolled;
2731 view->nscrolled = 0;
2736 static const struct got_error *
2737 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2738 struct got_commit_object *commit, struct got_object_id *commit_id,
2739 struct tog_view *log_view, struct got_repository *repo)
2741 const struct got_error *err;
2742 struct got_object_qid *parent_id;
2743 struct tog_view *diff_view;
2745 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2746 if (diff_view == NULL)
2747 return got_error_from_errno("view_open");
2749 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2750 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2751 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2753 *new_view = diff_view;
2757 static const struct got_error *
2758 tree_view_visit_subtree(struct tog_tree_view_state *s,
2759 struct got_tree_object *subtree)
2761 struct tog_parent_tree *parent;
2763 parent = calloc(1, sizeof(*parent));
2765 return got_error_from_errno("calloc");
2767 parent->tree = s->tree;
2768 parent->first_displayed_entry = s->first_displayed_entry;
2769 parent->selected_entry = s->selected_entry;
2770 parent->selected = s->selected;
2771 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2774 s->first_displayed_entry = NULL;
2778 static const struct got_error *
2779 tree_view_walk_path(struct tog_tree_view_state *s,
2780 struct got_commit_object *commit, const char *path)
2782 const struct got_error *err = NULL;
2783 struct got_tree_object *tree = NULL;
2785 char *slash, *subpath = NULL;
2787 /* Walk the path and open corresponding tree objects. */
2790 struct got_tree_entry *te;
2791 struct got_object_id *tree_id;
2797 /* Ensure the correct subtree entry is selected. */
2798 slash = strchr(p, '/');
2800 te_name = strdup(p);
2802 te_name = strndup(p, slash - p);
2803 if (te_name == NULL) {
2804 err = got_error_from_errno("strndup");
2807 te = got_object_tree_find_entry(s->tree, te_name);
2809 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2814 s->first_displayed_entry = s->selected_entry = te;
2816 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2817 break; /* jump to this file's entry */
2819 slash = strchr(p, '/');
2821 subpath = strndup(path, slash - path);
2823 subpath = strdup(path);
2824 if (subpath == NULL) {
2825 err = got_error_from_errno("strdup");
2829 err = got_object_id_by_path(&tree_id, s->repo, commit,
2834 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2839 err = tree_view_visit_subtree(s, tree);
2841 got_object_tree_close(tree);
2855 static const struct got_error *
2856 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2857 struct commit_queue_entry *entry, const char *path,
2858 const char *head_ref_name, struct got_repository *repo)
2860 const struct got_error *err = NULL;
2861 struct tog_tree_view_state *s;
2862 struct tog_view *tree_view;
2864 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2865 if (tree_view == NULL)
2866 return got_error_from_errno("view_open");
2868 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2871 s = &tree_view->state.tree;
2873 *new_view = tree_view;
2875 if (got_path_is_root_dir(path))
2878 return tree_view_walk_path(s, entry->commit, path);
2881 static const struct got_error *
2882 block_signals_used_by_main_thread(void)
2887 if (sigemptyset(&sigset) == -1)
2888 return got_error_from_errno("sigemptyset");
2890 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2891 if (sigaddset(&sigset, SIGWINCH) == -1)
2892 return got_error_from_errno("sigaddset");
2893 if (sigaddset(&sigset, SIGCONT) == -1)
2894 return got_error_from_errno("sigaddset");
2895 if (sigaddset(&sigset, SIGINT) == -1)
2896 return got_error_from_errno("sigaddset");
2897 if (sigaddset(&sigset, SIGTERM) == -1)
2898 return got_error_from_errno("sigaddset");
2900 /* ncurses handles SIGTSTP */
2901 if (sigaddset(&sigset, SIGTSTP) == -1)
2902 return got_error_from_errno("sigaddset");
2904 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2906 return got_error_set_errno(errcode, "pthread_sigmask");
2912 log_thread(void *arg)
2914 const struct got_error *err = NULL;
2916 struct tog_log_thread_args *a = arg;
2920 * Sync startup with main thread such that we begin our
2921 * work once view_input() has released the mutex.
2923 errcode = pthread_mutex_lock(&tog_mutex);
2925 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2929 err = block_signals_used_by_main_thread();
2931 pthread_mutex_unlock(&tog_mutex);
2935 while (!done && !err && !tog_fatal_signal_received()) {
2936 errcode = pthread_mutex_unlock(&tog_mutex);
2938 err = got_error_set_errno(errcode,
2939 "pthread_mutex_unlock");
2942 err = queue_commits(a);
2944 if (err->code != GOT_ERR_ITER_COMPLETED)
2948 } else if (a->commits_needed > 0 && !a->load_all) {
2951 a->commits_needed--;
2953 a->commits_needed--;
2956 errcode = pthread_mutex_lock(&tog_mutex);
2958 err = got_error_set_errno(errcode,
2959 "pthread_mutex_lock");
2961 } else if (*a->quit)
2963 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2964 *a->first_displayed_entry =
2965 TAILQ_FIRST(&a->limit_commits->head);
2966 *a->selected_entry = *a->first_displayed_entry;
2967 } else if (*a->first_displayed_entry == NULL) {
2968 *a->first_displayed_entry =
2969 TAILQ_FIRST(&a->real_commits->head);
2970 *a->selected_entry = *a->first_displayed_entry;
2973 errcode = pthread_cond_signal(&a->commit_loaded);
2975 err = got_error_set_errno(errcode,
2976 "pthread_cond_signal");
2977 pthread_mutex_unlock(&tog_mutex);
2982 a->commits_needed = 0;
2984 if (a->commits_needed == 0 && !a->load_all) {
2985 errcode = pthread_cond_wait(&a->need_commits,
2988 err = got_error_set_errno(errcode,
2989 "pthread_cond_wait");
2990 pthread_mutex_unlock(&tog_mutex);
2998 a->log_complete = 1;
2999 errcode = pthread_mutex_unlock(&tog_mutex);
3001 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3004 tog_thread_error = 1;
3005 pthread_cond_signal(&a->commit_loaded);
3010 static const struct got_error *
3011 stop_log_thread(struct tog_log_view_state *s)
3013 const struct got_error *err = NULL, *thread_err = NULL;
3018 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3020 return got_error_set_errno(errcode,
3021 "pthread_cond_signal");
3022 errcode = pthread_mutex_unlock(&tog_mutex);
3024 return got_error_set_errno(errcode,
3025 "pthread_mutex_unlock");
3026 errcode = pthread_join(s->thread, (void **)&thread_err);
3028 return got_error_set_errno(errcode, "pthread_join");
3029 errcode = pthread_mutex_lock(&tog_mutex);
3031 return got_error_set_errno(errcode,
3032 "pthread_mutex_lock");
3036 if (s->thread_args.repo) {
3037 err = got_repo_close(s->thread_args.repo);
3038 s->thread_args.repo = NULL;
3041 if (s->thread_args.pack_fds) {
3042 const struct got_error *pack_err =
3043 got_repo_pack_fds_close(s->thread_args.pack_fds);
3046 s->thread_args.pack_fds = NULL;
3049 if (s->thread_args.graph) {
3050 got_commit_graph_close(s->thread_args.graph);
3051 s->thread_args.graph = NULL;
3054 return err ? err : thread_err;
3057 static const struct got_error *
3058 close_log_view(struct tog_view *view)
3060 const struct got_error *err = NULL;
3061 struct tog_log_view_state *s = &view->state.log;
3064 err = stop_log_thread(s);
3066 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3067 if (errcode && err == NULL)
3068 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3070 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3071 if (errcode && err == NULL)
3072 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3074 free_commits(&s->limit_commits);
3075 free_commits(&s->real_commits);
3076 free(s->in_repo_path);
3077 s->in_repo_path = NULL;
3080 free(s->head_ref_name);
3081 s->head_ref_name = NULL;
3086 * We use two queues to implement the limit feature: first consists of
3087 * commits matching the current limit_regex; second is the real queue
3088 * of all known commits (real_commits). When the user starts limiting,
3089 * we swap queues such that all movement and displaying functionality
3090 * works with very slight change.
3092 static const struct got_error *
3093 limit_log_view(struct tog_view *view)
3095 struct tog_log_view_state *s = &view->state.log;
3096 struct commit_queue_entry *entry;
3097 struct tog_view *v = view;
3098 const struct got_error *err = NULL;
3102 if (view_is_hsplit_top(view))
3104 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3107 /* Get the pattern */
3108 wmove(v->window, v->nlines - 1, 0);
3109 wclrtoeol(v->window);
3110 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3111 nodelay(v->window, FALSE);
3114 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3117 nodelay(v->window, TRUE);
3121 if (*pattern == '\0') {
3123 * Safety measure for the situation where the user
3124 * resets limit without previously limiting anything.
3130 * User could have pressed Ctrl+L, which refreshed the
3131 * commit queues, it means we can't save previously
3132 * (before limit took place) displayed entries,
3133 * because they would point to already free'ed memory,
3134 * so we are forced to always select first entry of
3137 s->commits = &s->real_commits;
3138 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3139 s->selected_entry = s->first_displayed_entry;
3146 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3151 /* Clear the screen while loading limit view */
3152 s->first_displayed_entry = NULL;
3153 s->last_displayed_entry = NULL;
3154 s->selected_entry = NULL;
3155 s->commits = &s->limit_commits;
3157 /* Prepare limit queue for new search */
3158 free_commits(&s->limit_commits);
3159 s->limit_commits.ncommits = 0;
3161 /* First process commits, which are in queue already */
3162 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3165 err = match_commit(&have_match, entry->id,
3166 entry->commit, &s->limit_regex);
3171 struct commit_queue_entry *matched;
3173 matched = alloc_commit_queue_entry(entry->commit,
3175 if (matched == NULL) {
3176 err = got_error_from_errno(
3177 "alloc_commit_queue_entry");
3181 err = got_object_commit_dup(&matched->commit,
3188 matched->idx = s->limit_commits.ncommits;
3189 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3191 s->limit_commits.ncommits++;
3195 /* Second process all the commits, until we fill the screen */
3196 if (s->limit_commits.ncommits < view->nlines - 1 &&
3197 !s->thread_args.log_complete) {
3198 s->thread_args.commits_needed +=
3199 view->nlines - s->limit_commits.ncommits - 1;
3200 err = trigger_log_thread(view, 1);
3205 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3206 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3212 static const struct got_error *
3213 search_start_log_view(struct tog_view *view)
3215 struct tog_log_view_state *s = &view->state.log;
3217 s->matched_entry = NULL;
3218 s->search_entry = NULL;
3222 static const struct got_error *
3223 search_next_log_view(struct tog_view *view)
3225 const struct got_error *err = NULL;
3226 struct tog_log_view_state *s = &view->state.log;
3227 struct commit_queue_entry *entry;
3229 /* Display progress update in log view. */
3230 show_log_view(view);
3234 if (s->search_entry) {
3236 errcode = pthread_mutex_unlock(&tog_mutex);
3238 return got_error_set_errno(errcode,
3239 "pthread_mutex_unlock");
3240 ch = wgetch(view->window);
3241 errcode = pthread_mutex_lock(&tog_mutex);
3243 return got_error_set_errno(errcode,
3244 "pthread_mutex_lock");
3245 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3246 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3249 if (view->searching == TOG_SEARCH_FORWARD)
3250 entry = TAILQ_NEXT(s->search_entry, entry);
3252 entry = TAILQ_PREV(s->search_entry,
3253 commit_queue_head, entry);
3254 } else if (s->matched_entry) {
3256 * If the user has moved the cursor after we hit a match,
3257 * the position from where we should continue searching
3258 * might have changed.
3260 if (view->searching == TOG_SEARCH_FORWARD)
3261 entry = TAILQ_NEXT(s->selected_entry, entry);
3263 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3266 entry = s->selected_entry;
3272 if (entry == NULL) {
3273 if (s->thread_args.log_complete ||
3274 view->searching == TOG_SEARCH_BACKWARD) {
3275 view->search_next_done =
3276 (s->matched_entry == NULL ?
3277 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3278 s->search_entry = NULL;
3282 * Poke the log thread for more commits and return,
3283 * allowing the main loop to make progress. Search
3284 * will resume at s->search_entry once we come back.
3286 s->thread_args.commits_needed++;
3287 return trigger_log_thread(view, 0);
3290 err = match_commit(&have_match, entry->id, entry->commit,
3295 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3296 s->matched_entry = entry;
3300 s->search_entry = entry;
3301 if (view->searching == TOG_SEARCH_FORWARD)
3302 entry = TAILQ_NEXT(entry, entry);
3304 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3307 if (s->matched_entry) {
3308 int cur = s->selected_entry->idx;
3309 while (cur < s->matched_entry->idx) {
3310 err = input_log_view(NULL, view, KEY_DOWN);
3315 while (cur > s->matched_entry->idx) {
3316 err = input_log_view(NULL, view, KEY_UP);
3323 s->search_entry = NULL;
3328 static const struct got_error *
3329 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3330 struct got_repository *repo, const char *head_ref_name,
3331 const char *in_repo_path, int log_branches)
3333 const struct got_error *err = NULL;
3334 struct tog_log_view_state *s = &view->state.log;
3335 struct got_repository *thread_repo = NULL;
3336 struct got_commit_graph *thread_graph = NULL;
3339 if (in_repo_path != s->in_repo_path) {
3340 free(s->in_repo_path);
3341 s->in_repo_path = strdup(in_repo_path);
3342 if (s->in_repo_path == NULL)
3343 return got_error_from_errno("strdup");
3346 /* The commit queue only contains commits being displayed. */
3347 TAILQ_INIT(&s->real_commits.head);
3348 s->real_commits.ncommits = 0;
3349 s->commits = &s->real_commits;
3351 TAILQ_INIT(&s->limit_commits.head);
3353 s->limit_commits.ncommits = 0;
3356 if (head_ref_name) {
3357 s->head_ref_name = strdup(head_ref_name);
3358 if (s->head_ref_name == NULL) {
3359 err = got_error_from_errno("strdup");
3363 s->start_id = got_object_id_dup(start_id);
3364 if (s->start_id == NULL) {
3365 err = got_error_from_errno("got_object_id_dup");
3368 s->log_branches = log_branches;
3370 STAILQ_INIT(&s->colors);
3371 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3372 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3373 get_color_value("TOG_COLOR_COMMIT"));
3376 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3377 get_color_value("TOG_COLOR_AUTHOR"));
3379 free_colors(&s->colors);
3382 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3383 get_color_value("TOG_COLOR_DATE"));
3385 free_colors(&s->colors);
3390 view->show = show_log_view;
3391 view->input = input_log_view;
3392 view->resize = resize_log_view;
3393 view->close = close_log_view;
3394 view->search_start = search_start_log_view;
3395 view->search_next = search_next_log_view;
3397 if (s->thread_args.pack_fds == NULL) {
3398 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3402 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3403 s->thread_args.pack_fds);
3406 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3410 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3411 s->repo, NULL, NULL);
3415 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3417 err = got_error_set_errno(errcode, "pthread_cond_init");
3420 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3422 err = got_error_set_errno(errcode, "pthread_cond_init");
3426 s->thread_args.commits_needed = view->nlines;
3427 s->thread_args.graph = thread_graph;
3428 s->thread_args.real_commits = &s->real_commits;
3429 s->thread_args.limit_commits = &s->limit_commits;
3430 s->thread_args.in_repo_path = s->in_repo_path;
3431 s->thread_args.start_id = s->start_id;
3432 s->thread_args.repo = thread_repo;
3433 s->thread_args.log_complete = 0;
3434 s->thread_args.quit = &s->quit;
3435 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3436 s->thread_args.selected_entry = &s->selected_entry;
3437 s->thread_args.searching = &view->searching;
3438 s->thread_args.search_next_done = &view->search_next_done;
3439 s->thread_args.regex = &view->regex;
3440 s->thread_args.limiting = &s->limit_view;
3441 s->thread_args.limit_regex = &s->limit_regex;
3442 s->thread_args.limit_commits = &s->limit_commits;
3445 close_log_view(view);
3449 static const struct got_error *
3450 show_log_view(struct tog_view *view)
3452 const struct got_error *err;
3453 struct tog_log_view_state *s = &view->state.log;
3455 if (s->thread == NULL) {
3456 int errcode = pthread_create(&s->thread, NULL, log_thread,
3459 return got_error_set_errno(errcode, "pthread_create");
3460 if (s->thread_args.commits_needed > 0) {
3461 err = trigger_log_thread(view, 1);
3467 return draw_commits(view);
3471 log_move_cursor_up(struct tog_view *view, int page, int home)
3473 struct tog_log_view_state *s = &view->state.log;
3475 if (s->first_displayed_entry == NULL)
3477 if (s->selected_entry->idx == 0)
3480 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3482 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3484 if (!page && !home && s->selected > 0)
3487 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3493 static const struct got_error *
3494 log_move_cursor_down(struct tog_view *view, int page)
3496 struct tog_log_view_state *s = &view->state.log;
3497 const struct got_error *err = NULL;
3498 int eos = view->nlines - 2;
3500 if (s->first_displayed_entry == NULL)
3503 if (s->thread_args.log_complete &&
3504 s->selected_entry->idx >= s->commits->ncommits - 1)
3507 if (view_is_hsplit_top(view))
3508 --eos; /* border consumes the last line */
3511 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3514 err = log_scroll_down(view, 1);
3515 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3516 struct commit_queue_entry *entry;
3520 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3521 s->last_displayed_entry = entry;
3522 for (n = 0; n <= eos; n++) {
3525 s->first_displayed_entry = entry;
3526 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3529 s->selected = n - 1;
3531 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3532 s->thread_args.log_complete)
3533 s->selected += MIN(page,
3534 s->commits->ncommits - s->selected_entry->idx - 1);
3536 err = log_scroll_down(view, page);
3542 * We might necessarily overshoot in horizontal
3543 * splits; if so, select the last displayed commit.
3545 if (s->first_displayed_entry && s->last_displayed_entry) {
3546 s->selected = MIN(s->selected,
3547 s->last_displayed_entry->idx -
3548 s->first_displayed_entry->idx);
3553 if (s->thread_args.log_complete &&
3554 s->selected_entry->idx == s->commits->ncommits - 1)
3561 view_get_split(struct tog_view *view, int *y, int *x)
3566 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3567 if (view->child && view->child->resized_y)
3568 *y = view->child->resized_y;
3569 else if (view->resized_y)
3570 *y = view->resized_y;
3572 *y = view_split_begin_y(view->lines);
3573 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3574 if (view->child && view->child->resized_x)
3575 *x = view->child->resized_x;
3576 else if (view->resized_x)
3577 *x = view->resized_x;
3579 *x = view_split_begin_x(view->begin_x);
3583 /* Split view horizontally at y and offset view->state->selected line. */
3584 static const struct got_error *
3585 view_init_hsplit(struct tog_view *view, int y)
3587 const struct got_error *err = NULL;
3591 err = view_resize(view);
3595 err = offset_selection_down(view);
3600 static const struct got_error *
3601 log_goto_line(struct tog_view *view, int nlines)
3603 const struct got_error *err = NULL;
3604 struct tog_log_view_state *s = &view->state.log;
3605 int g, idx = s->selected_entry->idx;
3607 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3613 if (g >= s->first_displayed_entry->idx + 1 &&
3614 g <= s->last_displayed_entry->idx + 1 &&
3615 g - s->first_displayed_entry->idx - 1 < nlines) {
3616 s->selected = g - s->first_displayed_entry->idx - 1;
3622 err = log_move_cursor_down(view, g - idx - 1);
3623 if (!err && g > s->selected_entry->idx + 1)
3624 err = log_move_cursor_down(view,
3625 g - s->first_displayed_entry->idx - 1);
3628 } else if (idx + 1 > g)
3629 log_move_cursor_up(view, idx - g + 1, 0);
3631 if (g < nlines && s->first_displayed_entry->idx == 0)
3632 s->selected = g - 1;
3639 static const struct got_error *
3640 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3642 const struct got_error *err = NULL;
3643 struct tog_log_view_state *s = &view->state.log;
3646 if (s->thread_args.load_all) {
3647 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3648 s->thread_args.load_all = 0;
3649 else if (s->thread_args.log_complete) {
3650 err = log_move_cursor_down(view, s->commits->ncommits);
3651 s->thread_args.load_all = 0;
3657 eos = nscroll = view->nlines - 1;
3658 if (view_is_hsplit_top(view))
3662 return log_goto_line(view, eos);
3666 err = limit_log_view(view);
3675 view->x = MAX(view->maxx - view->ncols / 2, 0);
3680 if (view->x + view->ncols / 2 < view->maxx)
3681 view->x += 2; /* move two columns right */
3687 view->x -= MIN(view->x, 2); /* move two columns back */
3696 log_move_cursor_up(view, 0, 0);
3700 log_move_cursor_up(view, 0, 1);
3710 log_move_cursor_up(view, nscroll, 0);
3717 err = log_move_cursor_down(view, 0);
3720 s->use_committer = !s->use_committer;
3724 /* We don't know yet how many commits, so we're forced to
3725 * traverse them all. */
3727 s->thread_args.load_all = 1;
3728 if (!s->thread_args.log_complete)
3729 return trigger_log_thread(view, 0);
3730 err = log_move_cursor_down(view, s->commits->ncommits);
3731 s->thread_args.load_all = 0;
3742 err = log_move_cursor_down(view, nscroll);
3745 if (s->selected > view->nlines - 2)
3746 s->selected = view->nlines - 2;
3747 if (s->selected > s->commits->ncommits - 1)
3748 s->selected = s->commits->ncommits - 1;
3750 if (s->commits->ncommits < view->nlines - 1 &&
3751 !s->thread_args.log_complete) {
3752 s->thread_args.commits_needed += (view->nlines - 1) -
3753 s->commits->ncommits;
3754 err = trigger_log_thread(view, 1);
3760 if (s->selected_entry == NULL)
3762 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3766 if (s->selected_entry == NULL)
3768 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3774 if (ch == KEY_BACKSPACE &&
3775 got_path_is_root_dir(s->in_repo_path))
3777 err = stop_log_thread(s);
3780 if (ch == KEY_BACKSPACE) {
3782 err = got_path_dirname(&parent_path, s->in_repo_path);
3785 free(s->in_repo_path);
3786 s->in_repo_path = parent_path;
3787 s->thread_args.in_repo_path = s->in_repo_path;
3788 } else if (ch == CTRL('l')) {
3789 struct got_object_id *start_id;
3790 err = got_repo_match_object_id(&start_id, NULL,
3791 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3792 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3796 s->start_id = start_id;
3797 s->thread_args.start_id = s->start_id;
3799 s->log_branches = !s->log_branches;
3801 if (s->thread_args.pack_fds == NULL) {
3802 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3806 err = got_repo_open(&s->thread_args.repo,
3807 got_repo_get_path(s->repo), NULL,
3808 s->thread_args.pack_fds);
3812 err = tog_load_refs(s->repo, 0);
3815 err = got_commit_graph_open(&s->thread_args.graph,
3816 s->in_repo_path, !s->log_branches);
3819 err = got_commit_graph_iter_start(s->thread_args.graph,
3820 s->start_id, s->repo, NULL, NULL);
3823 free_commits(&s->real_commits);
3824 free_commits(&s->limit_commits);
3825 s->first_displayed_entry = NULL;
3826 s->last_displayed_entry = NULL;
3827 s->selected_entry = NULL;
3829 s->thread_args.log_complete = 0;
3831 s->thread_args.commits_needed = view->lines;
3832 s->matched_entry = NULL;
3833 s->search_entry = NULL;
3838 err = view_request_new(new_view, view, TOG_VIEW_REF);
3848 static const struct got_error *
3849 apply_unveil(const char *repo_path, const char *worktree_path)
3851 const struct got_error *error;
3854 if (unveil("gmon.out", "rwc") != 0)
3855 return got_error_from_errno2("unveil", "gmon.out");
3857 if (repo_path && unveil(repo_path, "r") != 0)
3858 return got_error_from_errno2("unveil", repo_path);
3860 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3861 return got_error_from_errno2("unveil", worktree_path);
3863 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3864 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3866 error = got_privsep_unveil_exec_helpers();
3870 if (unveil(NULL, NULL) != 0)
3871 return got_error_from_errno("unveil");
3880 * Override default signal handlers before starting ncurses.
3881 * This should prevent ncurses from installing its own
3882 * broken cleanup() signal handler.
3884 signal(SIGWINCH, tog_sigwinch);
3885 signal(SIGPIPE, tog_sigpipe);
3886 signal(SIGCONT, tog_sigcont);
3887 signal(SIGINT, tog_sigint);
3888 signal(SIGTERM, tog_sigterm);
3892 halfdelay(1); /* Do fast refresh while initial view is loading. */
3895 intrflush(stdscr, FALSE);
3896 keypad(stdscr, TRUE);
3898 if (getenv("TOG_COLORS") != NULL) {
3900 use_default_colors();
3904 static const struct got_error *
3905 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3906 struct got_repository *repo, struct got_worktree *worktree)
3908 const struct got_error *err = NULL;
3911 *in_repo_path = strdup("/");
3912 if (*in_repo_path == NULL)
3913 return got_error_from_errno("strdup");
3918 const char *prefix = got_worktree_get_path_prefix(worktree);
3921 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3924 if (asprintf(in_repo_path, "%s%s%s", prefix,
3925 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3927 err = got_error_from_errno("asprintf");
3928 *in_repo_path = NULL;
3932 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3937 static const struct got_error *
3938 cmd_log(int argc, char *argv[])
3940 const struct got_error *error;
3941 struct got_repository *repo = NULL;
3942 struct got_worktree *worktree = NULL;
3943 struct got_object_id *start_id = NULL;
3944 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3945 char *start_commit = NULL, *label = NULL;
3946 struct got_reference *ref = NULL;
3947 const char *head_ref_name = NULL;
3948 int ch, log_branches = 0;
3949 struct tog_view *view;
3950 int *pack_fds = NULL;
3952 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3958 start_commit = optarg;
3961 repo_path = realpath(optarg, NULL);
3962 if (repo_path == NULL)
3963 return got_error_from_errno2("realpath",
3978 error = got_repo_pack_fds_open(&pack_fds);
3982 if (repo_path == NULL) {
3983 cwd = getcwd(NULL, 0);
3985 return got_error_from_errno("getcwd");
3986 error = got_worktree_open(&worktree, cwd);
3987 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3991 strdup(got_worktree_get_repo_path(worktree));
3993 repo_path = strdup(cwd);
3994 if (repo_path == NULL) {
3995 error = got_error_from_errno("strdup");
4000 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4004 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4011 error = apply_unveil(got_repo_get_path(repo),
4012 worktree ? got_worktree_get_root_path(worktree) : NULL);
4016 /* already loaded by tog_log_with_path()? */
4017 if (TAILQ_EMPTY(&tog_refs)) {
4018 error = tog_load_refs(repo, 0);
4023 if (start_commit == NULL) {
4024 error = got_repo_match_object_id(&start_id, &label,
4025 worktree ? got_worktree_get_head_ref_name(worktree) :
4026 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4029 head_ref_name = label;
4031 error = got_ref_open(&ref, repo, start_commit, 0);
4033 head_ref_name = got_ref_get_name(ref);
4034 else if (error->code != GOT_ERR_NOT_REF)
4036 error = got_repo_match_object_id(&start_id, NULL,
4037 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4042 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4044 error = got_error_from_errno("view_open");
4047 error = open_log_view(view, start_id, repo, head_ref_name,
4048 in_repo_path, log_branches);
4052 /* Release work tree lock. */
4053 got_worktree_close(worktree);
4056 error = view_loop(view);
4066 const struct got_error *close_err = got_repo_close(repo);
4071 got_worktree_close(worktree);
4073 const struct got_error *pack_err =
4074 got_repo_pack_fds_close(pack_fds);
4086 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4087 "object1 object2\n", getprogname());
4092 match_line(const char *line, regex_t *regex, size_t nmatch,
4093 regmatch_t *regmatch)
4095 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4098 static struct tog_color *
4099 match_color(struct tog_colors *colors, const char *line)
4101 struct tog_color *tc = NULL;
4103 STAILQ_FOREACH(tc, colors, entry) {
4104 if (match_line(line, &tc->regex, 0, NULL))
4111 static const struct got_error *
4112 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4113 WINDOW *window, int skipcol, regmatch_t *regmatch)
4115 const struct got_error *err = NULL;
4117 wchar_t *wline = NULL;
4118 int rme, rms, n, width, scrollx;
4119 int width0 = 0, width1 = 0, width2 = 0;
4120 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4124 rms = regmatch->rm_so;
4125 rme = regmatch->rm_eo;
4127 err = expand_tab(&exstr, line);
4131 /* Split the line into 3 segments, according to match offsets. */
4132 seg0 = strndup(exstr, rms);
4134 err = got_error_from_errno("strndup");
4137 seg1 = strndup(exstr + rms, rme - rms);
4139 err = got_error_from_errno("strndup");
4142 seg2 = strdup(exstr + rme);
4144 err = got_error_from_errno("strndup");
4148 /* draw up to matched token if we haven't scrolled past it */
4149 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4153 n = MAX(width0 - skipcol, 0);
4156 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4157 wlimit, col_tab_align, 1);
4160 waddwstr(window, &wline[scrollx]);
4170 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4174 wlen = wcslen(wline);
4176 width = wcwidth(wline[i]);
4178 /* should not happen, tabs are expanded */
4179 err = got_error(GOT_ERR_RANGE);
4182 if (width0 + w + width > skipcol)
4187 /* draw (visible part of) matched token (if scrolled into it) */
4188 if (width1 - w > 0) {
4189 wattron(window, A_STANDOUT);
4190 waddwstr(window, &wline[i]);
4191 wattroff(window, A_STANDOUT);
4192 wlimit -= (width1 - w);
4193 *wtotal += (width1 - w);
4197 if (wlimit > 0) { /* draw rest of line */
4199 if (skipcol > width0 + width1) {
4200 err = format_line(&wline, &width2, &scrollx, seg2,
4201 skipcol - (width0 + width1), wlimit,
4205 waddwstr(window, &wline[scrollx]);
4207 err = format_line(&wline, &width2, NULL, seg2, 0,
4208 wlimit, col_tab_align, 1);
4211 waddwstr(window, wline);
4225 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4228 int *eof, *first, *selected;
4230 if (view->type == TOG_VIEW_DIFF) {
4231 struct tog_diff_view_state *s = &view->state.diff;
4233 first = &s->first_displayed_line;
4237 } else if (view->type == TOG_VIEW_HELP) {
4238 struct tog_help_view_state *s = &view->state.help;
4240 first = &s->first_displayed_line;
4244 } else if (view->type == TOG_VIEW_BLAME) {
4245 struct tog_blame_view_state *s = &view->state.blame;
4247 first = &s->first_displayed_line;
4248 selected = &s->selected_line;
4254 /* Center gline in the middle of the page like vi(1). */
4255 if (*lineno < view->gline - (view->nlines - 3) / 2)
4257 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4266 *selected = view->gline <= (view->nlines - 3) / 2 ?
4267 view->gline : (view->nlines - 3) / 2 + 1;
4273 static const struct got_error *
4274 draw_file(struct tog_view *view, const char *header)
4276 struct tog_diff_view_state *s = &view->state.diff;
4277 regmatch_t *regmatch = &view->regmatch;
4278 const struct got_error *err;
4281 size_t linesize = 0;
4285 int max_lines = view->nlines;
4286 int nlines = s->nlines;
4289 s->lineno = s->first_displayed_line - 1;
4290 line_offset = s->lines[s->first_displayed_line - 1].offset;
4291 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4292 return got_error_from_errno("fseek");
4294 werase(view->window);
4296 if (view->gline > s->nlines - 1)
4297 view->gline = s->nlines - 1;
4300 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4301 1 : view->gline - (view->nlines - 3) / 2 :
4302 s->lineno + s->selected_line;
4304 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4305 return got_error_from_errno("asprintf");
4306 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4312 if (view_needs_focus_indication(view))
4313 wstandout(view->window);
4314 waddwstr(view->window, wline);
4317 while (width++ < view->ncols)
4318 waddch(view->window, ' ');
4319 if (view_needs_focus_indication(view))
4320 wstandend(view->window);
4330 while (max_lines > 0 && nprinted < max_lines) {
4331 enum got_diff_line_type linetype;
4334 linelen = getline(&line, &linesize, s->f);
4335 if (linelen == -1) {
4341 return got_ferror(s->f, GOT_ERR_IO);
4344 if (++s->lineno < s->first_displayed_line)
4346 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4348 if (s->lineno == view->hiline)
4351 /* Set view->maxx based on full line length. */
4352 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4358 view->maxx = MAX(view->maxx, width);
4362 linetype = s->lines[s->lineno].type;
4363 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4364 linetype < GOT_DIFF_LINE_CONTEXT)
4365 attr |= COLOR_PAIR(linetype);
4367 wattron(view->window, attr);
4368 if (s->first_displayed_line + nprinted == s->matched_line &&
4369 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4370 err = add_matched_line(&width, line, view->ncols, 0,
4371 view->window, view->x, regmatch);
4378 err = format_line(&wline, &width, &skip, line,
4379 view->x, view->ncols, 0, view->x ? 1 : 0);
4384 waddwstr(view->window, &wline[skip]);
4388 if (s->lineno == view->hiline) {
4389 /* highlight full gline length */
4390 while (width++ < view->ncols)
4391 waddch(view->window, ' ');
4393 if (width <= view->ncols - 1)
4394 waddch(view->window, '\n');
4397 wattroff(view->window, attr);
4398 if (++nprinted == 1)
4399 s->first_displayed_line = s->lineno;
4403 s->last_displayed_line = s->first_displayed_line +
4406 s->last_displayed_line = s->first_displayed_line;
4411 while (nprinted < view->nlines) {
4412 waddch(view->window, '\n');
4416 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4422 wstandout(view->window);
4423 waddwstr(view->window, wline);
4426 wstandend(view->window);
4433 get_datestr(time_t *time, char *datebuf)
4435 struct tm mytm, *tm;
4438 tm = gmtime_r(time, &mytm);
4441 s = asctime_r(tm, datebuf);
4444 p = strchr(s, '\n');
4450 static const struct got_error *
4451 get_changed_paths(struct got_pathlist_head *paths,
4452 struct got_commit_object *commit, struct got_repository *repo)
4454 const struct got_error *err = NULL;
4455 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4456 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4457 struct got_object_qid *qid;
4459 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4461 struct got_commit_object *pcommit;
4462 err = got_object_open_as_commit(&pcommit, repo,
4467 tree_id1 = got_object_id_dup(
4468 got_object_commit_get_tree_id(pcommit));
4469 if (tree_id1 == NULL) {
4470 got_object_commit_close(pcommit);
4471 return got_error_from_errno("got_object_id_dup");
4473 got_object_commit_close(pcommit);
4478 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4483 tree_id2 = got_object_commit_get_tree_id(commit);
4484 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4488 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4489 got_diff_tree_collect_changed_paths, paths, 0);
4492 got_object_tree_close(tree1);
4494 got_object_tree_close(tree2);
4499 static const struct got_error *
4500 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4501 off_t off, uint8_t type)
4503 struct got_diff_line *p;
4505 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4507 return got_error_from_errno("reallocarray");
4509 (*lines)[*nlines].offset = off;
4510 (*lines)[*nlines].type = type;
4516 static const struct got_error *
4517 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4518 struct got_object_id *commit_id, struct got_reflist_head *refs,
4519 struct got_repository *repo, FILE *outfile)
4521 const struct got_error *err = NULL;
4522 char datebuf[26], *datestr;
4523 struct got_commit_object *commit;
4524 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4525 time_t committer_time;
4526 const char *author, *committer;
4527 char *refs_str = NULL;
4528 struct got_pathlist_head changed_paths;
4529 struct got_pathlist_entry *pe;
4533 TAILQ_INIT(&changed_paths);
4536 err = build_refs_str(&refs_str, refs, commit_id, repo);
4541 err = got_object_open_as_commit(&commit, repo, commit_id);
4545 err = got_object_id_str(&id_str, commit_id);
4547 err = got_error_from_errno("got_object_id_str");
4551 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4555 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4556 refs_str ? refs_str : "", refs_str ? ")" : "");
4558 err = got_error_from_errno("fprintf");
4562 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4566 n = fprintf(outfile, "from: %s\n",
4567 got_object_commit_get_author(commit));
4569 err = got_error_from_errno("fprintf");
4573 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4577 committer_time = got_object_commit_get_committer_time(commit);
4578 datestr = get_datestr(&committer_time, datebuf);
4580 n = fprintf(outfile, "date: %s UTC\n", datestr);
4582 err = got_error_from_errno("fprintf");
4586 err = add_line_metadata(lines, nlines, outoff,
4587 GOT_DIFF_LINE_DATE);
4591 author = got_object_commit_get_author(commit);
4592 committer = got_object_commit_get_committer(commit);
4593 if (strcmp(author, committer) != 0) {
4594 n = fprintf(outfile, "via: %s\n", committer);
4596 err = got_error_from_errno("fprintf");
4600 err = add_line_metadata(lines, nlines, outoff,
4601 GOT_DIFF_LINE_AUTHOR);
4605 if (got_object_commit_get_nparents(commit) > 1) {
4606 const struct got_object_id_queue *parent_ids;
4607 struct got_object_qid *qid;
4609 parent_ids = got_object_commit_get_parent_ids(commit);
4610 STAILQ_FOREACH(qid, parent_ids, entry) {
4611 err = got_object_id_str(&id_str, &qid->id);
4614 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4616 err = got_error_from_errno("fprintf");
4620 err = add_line_metadata(lines, nlines, outoff,
4621 GOT_DIFF_LINE_META);
4629 err = got_object_commit_get_logmsg(&logmsg, commit);
4633 while ((line = strsep(&s, "\n")) != NULL) {
4634 n = fprintf(outfile, "%s\n", line);
4636 err = got_error_from_errno("fprintf");
4640 err = add_line_metadata(lines, nlines, outoff,
4641 GOT_DIFF_LINE_LOGMSG);
4646 err = get_changed_paths(&changed_paths, commit, repo);
4649 TAILQ_FOREACH(pe, &changed_paths, entry) {
4650 struct got_diff_changed_path *cp = pe->data;
4651 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4653 err = got_error_from_errno("fprintf");
4657 err = add_line_metadata(lines, nlines, outoff,
4658 GOT_DIFF_LINE_CHANGES);
4661 free((char *)pe->path);
4665 fputc('\n', outfile);
4667 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4669 got_pathlist_free(&changed_paths);
4673 got_object_commit_close(commit);
4682 static const struct got_error *
4683 create_diff(struct tog_diff_view_state *s)
4685 const struct got_error *err = NULL;
4690 s->lines = malloc(sizeof(*s->lines));
4691 if (s->lines == NULL)
4692 return got_error_from_errno("malloc");
4697 err = got_error_from_errno("got_opentemp");
4700 if (s->f && fclose(s->f) == EOF) {
4701 err = got_error_from_errno("fclose");
4707 err = got_object_get_type(&obj_type, s->repo, s->id1);
4709 err = got_object_get_type(&obj_type, s->repo, s->id2);
4714 case GOT_OBJ_TYPE_BLOB:
4715 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4716 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4717 s->label1, s->label2, tog_diff_algo, s->diff_context,
4718 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4720 case GOT_OBJ_TYPE_TREE:
4721 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4722 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4723 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4724 s->force_text_diff, s->repo, s->f);
4726 case GOT_OBJ_TYPE_COMMIT: {
4727 const struct got_object_id_queue *parent_ids;
4728 struct got_object_qid *pid;
4729 struct got_commit_object *commit2;
4730 struct got_reflist_head *refs;
4732 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4735 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4736 /* Show commit info if we're diffing to a parent/root commit. */
4737 if (s->id1 == NULL) {
4738 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4739 refs, s->repo, s->f);
4743 parent_ids = got_object_commit_get_parent_ids(commit2);
4744 STAILQ_FOREACH(pid, parent_ids, entry) {
4745 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4746 err = write_commit_info(&s->lines,
4747 &s->nlines, s->id2, refs, s->repo,
4755 got_object_commit_close(commit2);
4757 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4758 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4759 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4760 s->force_text_diff, s->repo, s->f);
4764 err = got_error(GOT_ERR_OBJ_TYPE);
4768 if (s->f && fflush(s->f) != 0 && err == NULL)
4769 err = got_error_from_errno("fflush");
4774 diff_view_indicate_progress(struct tog_view *view)
4776 mvwaddstr(view->window, 0, 0, "diffing...");
4781 static const struct got_error *
4782 search_start_diff_view(struct tog_view *view)
4784 struct tog_diff_view_state *s = &view->state.diff;
4786 s->matched_line = 0;
4791 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4792 size_t *nlines, int **first, int **last, int **match, int **selected)
4794 struct tog_diff_view_state *s = &view->state.diff;
4797 *nlines = s->nlines;
4798 *line_offsets = NULL;
4799 *match = &s->matched_line;
4800 *first = &s->first_displayed_line;
4801 *last = &s->last_displayed_line;
4802 *selected = &s->selected_line;
4805 static const struct got_error *
4806 search_next_view_match(struct tog_view *view)
4808 const struct got_error *err = NULL;
4812 size_t linesize = 0;
4814 off_t *line_offsets;
4816 int *first, *last, *match, *selected;
4818 if (!view->search_setup)
4819 return got_error_msg(GOT_ERR_NOT_IMPL,
4820 "view search not supported");
4821 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4824 if (!view->searching) {
4825 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4830 if (view->searching == TOG_SEARCH_FORWARD)
4831 lineno = *match + 1;
4833 lineno = *match - 1;
4835 lineno = *first - 1 + *selected;
4840 if (lineno <= 0 || lineno > nlines) {
4842 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4846 if (view->searching == TOG_SEARCH_FORWARD)
4852 offset = view->type == TOG_VIEW_DIFF ?
4853 view->state.diff.lines[lineno - 1].offset :
4854 line_offsets[lineno - 1];
4855 if (fseeko(f, offset, SEEK_SET) != 0) {
4857 return got_error_from_errno("fseeko");
4859 linelen = getline(&line, &linesize, f);
4860 if (linelen != -1) {
4862 err = expand_tab(&exstr, line);
4865 if (match_line(exstr, &view->regex, 1,
4867 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4874 if (view->searching == TOG_SEARCH_FORWARD)
4889 static const struct got_error *
4890 close_diff_view(struct tog_view *view)
4892 const struct got_error *err = NULL;
4893 struct tog_diff_view_state *s = &view->state.diff;
4899 if (s->f && fclose(s->f) == EOF)
4900 err = got_error_from_errno("fclose");
4902 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4903 err = got_error_from_errno("fclose");
4905 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4906 err = got_error_from_errno("fclose");
4908 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4909 err = got_error_from_errno("close");
4911 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4912 err = got_error_from_errno("close");
4920 static const struct got_error *
4921 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4922 struct got_object_id *id2, const char *label1, const char *label2,
4923 int diff_context, int ignore_whitespace, int force_text_diff,
4924 struct tog_view *parent_view, struct got_repository *repo)
4926 const struct got_error *err;
4927 struct tog_diff_view_state *s = &view->state.diff;
4929 memset(s, 0, sizeof(*s));
4933 if (id1 != NULL && id2 != NULL) {
4935 err = got_object_get_type(&type1, repo, id1);
4938 err = got_object_get_type(&type2, repo, id2);
4943 return got_error(GOT_ERR_OBJ_TYPE);
4945 s->first_displayed_line = 1;
4946 s->last_displayed_line = view->nlines;
4947 s->selected_line = 1;
4955 s->id1 = got_object_id_dup(id1);
4957 return got_error_from_errno("got_object_id_dup");
4961 s->id2 = got_object_id_dup(id2);
4962 if (s->id2 == NULL) {
4963 err = got_error_from_errno("got_object_id_dup");
4967 s->f1 = got_opentemp();
4968 if (s->f1 == NULL) {
4969 err = got_error_from_errno("got_opentemp");
4973 s->f2 = got_opentemp();
4974 if (s->f2 == NULL) {
4975 err = got_error_from_errno("got_opentemp");
4979 s->fd1 = got_opentempfd();
4981 err = got_error_from_errno("got_opentempfd");
4985 s->fd2 = got_opentempfd();
4987 err = got_error_from_errno("got_opentempfd");
4991 s->first_displayed_line = 1;
4992 s->last_displayed_line = view->nlines;
4993 s->diff_context = diff_context;
4994 s->ignore_whitespace = ignore_whitespace;
4995 s->force_text_diff = force_text_diff;
4996 s->parent_view = parent_view;
4999 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5002 rc = init_pair(GOT_DIFF_LINE_MINUS,
5003 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5005 rc = init_pair(GOT_DIFF_LINE_PLUS,
5006 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5008 rc = init_pair(GOT_DIFF_LINE_HUNK,
5009 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5011 rc = init_pair(GOT_DIFF_LINE_META,
5012 get_color_value("TOG_COLOR_DIFF_META"), -1);
5014 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5015 get_color_value("TOG_COLOR_DIFF_META"), -1);
5017 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5018 get_color_value("TOG_COLOR_DIFF_META"), -1);
5020 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5021 get_color_value("TOG_COLOR_DIFF_META"), -1);
5023 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5024 get_color_value("TOG_COLOR_AUTHOR"), -1);
5026 rc = init_pair(GOT_DIFF_LINE_DATE,
5027 get_color_value("TOG_COLOR_DATE"), -1);
5029 err = got_error(GOT_ERR_RANGE);
5034 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5035 view_is_splitscreen(view))
5036 show_log_view(parent_view); /* draw border */
5037 diff_view_indicate_progress(view);
5039 err = create_diff(s);
5041 view->show = show_diff_view;
5042 view->input = input_diff_view;
5043 view->reset = reset_diff_view;
5044 view->close = close_diff_view;
5045 view->search_start = search_start_diff_view;
5046 view->search_setup = search_setup_diff_view;
5047 view->search_next = search_next_view_match;
5050 close_diff_view(view);
5054 static const struct got_error *
5055 show_diff_view(struct tog_view *view)
5057 const struct got_error *err;
5058 struct tog_diff_view_state *s = &view->state.diff;
5059 char *id_str1 = NULL, *id_str2, *header;
5060 const char *label1, *label2;
5063 err = got_object_id_str(&id_str1, s->id1);
5066 label1 = s->label1 ? s->label1 : id_str1;
5068 label1 = "/dev/null";
5070 err = got_object_id_str(&id_str2, s->id2);
5073 label2 = s->label2 ? s->label2 : id_str2;
5075 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5076 err = got_error_from_errno("asprintf");
5084 err = draw_file(view, header);
5089 static const struct got_error *
5090 set_selected_commit(struct tog_diff_view_state *s,
5091 struct commit_queue_entry *entry)
5093 const struct got_error *err;
5094 const struct got_object_id_queue *parent_ids;
5095 struct got_commit_object *selected_commit;
5096 struct got_object_qid *pid;
5099 s->id2 = got_object_id_dup(entry->id);
5101 return got_error_from_errno("got_object_id_dup");
5103 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5106 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5108 pid = STAILQ_FIRST(parent_ids);
5109 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5110 got_object_commit_close(selected_commit);
5114 static const struct got_error *
5115 reset_diff_view(struct tog_view *view)
5117 struct tog_diff_view_state *s = &view->state.diff;
5120 wclear(view->window);
5121 s->first_displayed_line = 1;
5122 s->last_displayed_line = view->nlines;
5123 s->matched_line = 0;
5124 diff_view_indicate_progress(view);
5125 return create_diff(s);
5129 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5133 i = start = s->first_displayed_line - 1;
5135 while (s->lines[i].type != type) {
5139 return; /* do nothing, requested type not in file */
5142 s->selected_line = 1;
5143 s->first_displayed_line = i;
5147 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5151 i = start = s->first_displayed_line + 1;
5153 while (s->lines[i].type != type) {
5154 if (i == s->nlines - 1)
5157 return; /* do nothing, requested type not in file */
5160 s->selected_line = 1;
5161 s->first_displayed_line = i;
5164 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5166 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5169 static const struct got_error *
5170 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5172 const struct got_error *err = NULL;
5173 struct tog_diff_view_state *s = &view->state.diff;
5174 struct tog_log_view_state *ls;
5175 struct commit_queue_entry *old_selected_entry;
5177 size_t linesize = 0;
5179 int i, nscroll = view->nlines - 1, up = 0;
5181 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5188 view->x = MAX(view->maxx - view->ncols / 3, 0);
5193 if (view->x + view->ncols / 3 < view->maxx)
5194 view->x += 2; /* move two columns right */
5200 view->x -= MIN(view->x, 2); /* move two columns back */
5207 s->force_text_diff = !s->force_text_diff;
5209 s->ignore_whitespace = !s->ignore_whitespace;
5210 err = reset_diff_view(view);
5214 s->first_displayed_line = 1;
5223 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5229 if (s->first_displayed_line > 1)
5230 s->first_displayed_line--;
5241 if (s->first_displayed_line == 1) {
5246 while (i++ < nscroll && s->first_displayed_line > 1)
5247 s->first_displayed_line--;
5253 s->first_displayed_line++;
5270 while (!s->eof && i++ < nscroll) {
5271 linelen = getline(&line, &linesize, s->f);
5272 s->first_displayed_line++;
5273 if (linelen == -1) {
5277 err = got_ferror(s->f, GOT_ERR_IO);
5284 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5287 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5290 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5293 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5296 if (s->diff_context > 0) {
5298 s->matched_line = 0;
5299 diff_view_indicate_progress(view);
5300 err = create_diff(s);
5301 if (s->first_displayed_line + view->nlines - 1 >
5303 s->first_displayed_line = 1;
5304 s->last_displayed_line = view->nlines;
5310 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5312 s->matched_line = 0;
5313 diff_view_indicate_progress(view);
5314 err = create_diff(s);
5326 if (s->parent_view == NULL) {
5330 s->parent_view->count = view->count;
5332 if (s->parent_view->type == TOG_VIEW_LOG) {
5333 ls = &s->parent_view->state.log;
5334 old_selected_entry = ls->selected_entry;
5336 err = input_log_view(NULL, s->parent_view,
5337 up ? KEY_UP : KEY_DOWN);
5340 view->count = s->parent_view->count;
5342 if (old_selected_entry == ls->selected_entry)
5345 err = set_selected_commit(s, ls->selected_entry);
5348 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5349 struct tog_blame_view_state *bs;
5350 struct got_object_id *id, *prev_id;
5352 bs = &s->parent_view->state.blame;
5353 prev_id = get_annotation_for_line(bs->blame.lines,
5354 bs->blame.nlines, bs->last_diffed_line);
5356 err = input_blame_view(&view, s->parent_view,
5357 up ? KEY_UP : KEY_DOWN);
5360 view->count = s->parent_view->count;
5362 if (prev_id == NULL)
5364 id = get_selected_commit_id(bs->blame.lines,
5365 bs->blame.nlines, bs->first_displayed_line,
5370 if (!got_object_id_cmp(prev_id, id))
5373 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5377 s->first_displayed_line = 1;
5378 s->last_displayed_line = view->nlines;
5379 s->matched_line = 0;
5382 diff_view_indicate_progress(view);
5383 err = create_diff(s);
5393 static const struct got_error *
5394 cmd_diff(int argc, char *argv[])
5396 const struct got_error *error = NULL;
5397 struct got_repository *repo = NULL;
5398 struct got_worktree *worktree = NULL;
5399 struct got_object_id *id1 = NULL, *id2 = NULL;
5400 char *repo_path = NULL, *cwd = NULL;
5401 char *id_str1 = NULL, *id_str2 = NULL;
5402 char *label1 = NULL, *label2 = NULL;
5403 int diff_context = 3, ignore_whitespace = 0;
5404 int ch, force_text_diff = 0;
5406 struct tog_view *view;
5407 int *pack_fds = NULL;
5409 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5412 force_text_diff = 1;
5415 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5418 errx(1, "number of context lines is %s: %s",
5422 repo_path = realpath(optarg, NULL);
5423 if (repo_path == NULL)
5424 return got_error_from_errno2("realpath",
5426 got_path_strip_trailing_slashes(repo_path);
5429 ignore_whitespace = 1;
5441 usage_diff(); /* TODO show local worktree changes */
5442 } else if (argc == 2) {
5448 error = got_repo_pack_fds_open(&pack_fds);
5452 if (repo_path == NULL) {
5453 cwd = getcwd(NULL, 0);
5455 return got_error_from_errno("getcwd");
5456 error = got_worktree_open(&worktree, cwd);
5457 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5461 strdup(got_worktree_get_repo_path(worktree));
5463 repo_path = strdup(cwd);
5464 if (repo_path == NULL) {
5465 error = got_error_from_errno("strdup");
5470 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5476 error = apply_unveil(got_repo_get_path(repo), NULL);
5480 error = tog_load_refs(repo, 0);
5484 error = got_repo_match_object_id(&id1, &label1, id_str1,
5485 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5489 error = got_repo_match_object_id(&id2, &label2, id_str2,
5490 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5494 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5496 error = got_error_from_errno("view_open");
5499 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5500 ignore_whitespace, force_text_diff, NULL, repo);
5503 error = view_loop(view);
5510 const struct got_error *close_err = got_repo_close(repo);
5515 got_worktree_close(worktree);
5517 const struct got_error *pack_err =
5518 got_repo_pack_fds_close(pack_fds);
5531 "usage: %s blame [-c commit] [-r repository-path] path\n",
5536 struct tog_blame_line {
5538 struct got_object_id *id;
5541 static const struct got_error *
5542 draw_blame(struct tog_view *view)
5544 struct tog_blame_view_state *s = &view->state.blame;
5545 struct tog_blame *blame = &s->blame;
5546 regmatch_t *regmatch = &view->regmatch;
5547 const struct got_error *err;
5548 int lineno = 0, nprinted = 0;
5550 size_t linesize = 0;
5554 struct tog_blame_line *blame_line;
5555 struct got_object_id *prev_id = NULL;
5557 struct tog_color *tc;
5559 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5564 werase(view->window);
5566 if (asprintf(&line, "commit %s", id_str) == -1) {
5567 err = got_error_from_errno("asprintf");
5572 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5577 if (view_needs_focus_indication(view))
5578 wstandout(view->window);
5579 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5581 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5582 waddwstr(view->window, wline);
5583 while (width++ < view->ncols)
5584 waddch(view->window, ' ');
5586 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5587 if (view_needs_focus_indication(view))
5588 wstandend(view->window);
5592 if (view->gline > blame->nlines)
5593 view->gline = blame->nlines;
5595 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5596 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5597 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5599 return got_error_from_errno("asprintf");
5602 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5607 waddwstr(view->window, wline);
5610 if (width < view->ncols - 1)
5611 waddch(view->window, '\n');
5615 while (nprinted < view->nlines - 2) {
5616 linelen = getline(&line, &linesize, blame->f);
5617 if (linelen == -1) {
5618 if (feof(blame->f)) {
5623 return got_ferror(blame->f, GOT_ERR_IO);
5625 if (++lineno < s->first_displayed_line)
5627 if (view->gline && !gotoline(view, &lineno, &nprinted))
5630 /* Set view->maxx based on full line length. */
5631 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5638 view->maxx = MAX(view->maxx, width);
5640 if (nprinted == s->selected_line - 1)
5641 wstandout(view->window);
5643 if (blame->nlines > 0) {
5644 blame_line = &blame->lines[lineno - 1];
5645 if (blame_line->annotated && prev_id &&
5646 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5647 !(nprinted == s->selected_line - 1)) {
5648 waddstr(view->window, " ");
5649 } else if (blame_line->annotated) {
5651 err = got_object_id_str(&id_str,
5657 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5659 wattr_on(view->window,
5660 COLOR_PAIR(tc->colorpair), NULL);
5661 wprintw(view->window, "%.8s", id_str);
5663 wattr_off(view->window,
5664 COLOR_PAIR(tc->colorpair), NULL);
5666 prev_id = blame_line->id;
5668 waddstr(view->window, "........");
5672 waddstr(view->window, "........");
5676 if (nprinted == s->selected_line - 1)
5677 wstandend(view->window);
5678 waddstr(view->window, " ");
5680 if (view->ncols <= 9) {
5682 } else if (s->first_displayed_line + nprinted ==
5684 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5685 err = add_matched_line(&width, line, view->ncols - 9, 9,
5686 view->window, view->x, regmatch);
5694 err = format_line(&wline, &width, &skip, line,
5695 view->x, view->ncols - 9, 9, 1);
5700 waddwstr(view->window, &wline[skip]);
5706 if (width <= view->ncols - 1)
5707 waddch(view->window, '\n');
5708 if (++nprinted == 1)
5709 s->first_displayed_line = lineno;
5712 s->last_displayed_line = lineno;
5719 static const struct got_error *
5720 blame_cb(void *arg, int nlines, int lineno,
5721 struct got_commit_object *commit, struct got_object_id *id)
5723 const struct got_error *err = NULL;
5724 struct tog_blame_cb_args *a = arg;
5725 struct tog_blame_line *line;
5728 if (nlines != a->nlines ||
5729 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5730 return got_error(GOT_ERR_RANGE);
5732 errcode = pthread_mutex_lock(&tog_mutex);
5734 return got_error_set_errno(errcode, "pthread_mutex_lock");
5736 if (*a->quit) { /* user has quit the blame view */
5737 err = got_error(GOT_ERR_ITER_COMPLETED);
5742 goto done; /* no change in this commit */
5744 line = &a->lines[lineno - 1];
5745 if (line->annotated)
5748 line->id = got_object_id_dup(id);
5749 if (line->id == NULL) {
5750 err = got_error_from_errno("got_object_id_dup");
5753 line->annotated = 1;
5755 errcode = pthread_mutex_unlock(&tog_mutex);
5757 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5762 blame_thread(void *arg)
5764 const struct got_error *err, *close_err;
5765 struct tog_blame_thread_args *ta = arg;
5766 struct tog_blame_cb_args *a = ta->cb_args;
5767 int errcode, fd1 = -1, fd2 = -1;
5768 FILE *f1 = NULL, *f2 = NULL;
5770 fd1 = got_opentempfd();
5772 return (void *)got_error_from_errno("got_opentempfd");
5774 fd2 = got_opentempfd();
5776 err = got_error_from_errno("got_opentempfd");
5780 f1 = got_opentemp();
5782 err = (void *)got_error_from_errno("got_opentemp");
5785 f2 = got_opentemp();
5787 err = (void *)got_error_from_errno("got_opentemp");
5791 err = block_signals_used_by_main_thread();
5795 err = got_blame(ta->path, a->commit_id, ta->repo,
5796 tog_diff_algo, blame_cb, ta->cb_args,
5797 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5798 if (err && err->code == GOT_ERR_CANCELLED)
5801 errcode = pthread_mutex_lock(&tog_mutex);
5803 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5807 close_err = got_repo_close(ta->repo);
5813 errcode = pthread_mutex_unlock(&tog_mutex);
5814 if (errcode && err == NULL)
5815 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5818 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5819 err = got_error_from_errno("close");
5820 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5821 err = got_error_from_errno("close");
5822 if (f1 && fclose(f1) == EOF && err == NULL)
5823 err = got_error_from_errno("fclose");
5824 if (f2 && fclose(f2) == EOF && err == NULL)
5825 err = got_error_from_errno("fclose");
5830 static struct got_object_id *
5831 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5832 int first_displayed_line, int selected_line)
5834 struct tog_blame_line *line;
5839 line = &lines[first_displayed_line - 1 + selected_line - 1];
5840 if (!line->annotated)
5846 static struct got_object_id *
5847 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5850 struct tog_blame_line *line;
5852 if (nlines <= 0 || lineno >= nlines)
5855 line = &lines[lineno - 1];
5856 if (!line->annotated)
5862 static const struct got_error *
5863 stop_blame(struct tog_blame *blame)
5865 const struct got_error *err = NULL;
5868 if (blame->thread) {
5870 errcode = pthread_mutex_unlock(&tog_mutex);
5872 return got_error_set_errno(errcode,
5873 "pthread_mutex_unlock");
5874 errcode = pthread_join(blame->thread, (void **)&err);
5876 return got_error_set_errno(errcode, "pthread_join");
5877 errcode = pthread_mutex_lock(&tog_mutex);
5879 return got_error_set_errno(errcode,
5880 "pthread_mutex_lock");
5881 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5883 blame->thread = NULL;
5885 if (blame->thread_args.repo) {
5886 const struct got_error *close_err;
5887 close_err = got_repo_close(blame->thread_args.repo);
5890 blame->thread_args.repo = NULL;
5893 if (fclose(blame->f) == EOF && err == NULL)
5894 err = got_error_from_errno("fclose");
5898 for (i = 0; i < blame->nlines; i++)
5899 free(blame->lines[i].id);
5901 blame->lines = NULL;
5903 free(blame->cb_args.commit_id);
5904 blame->cb_args.commit_id = NULL;
5905 if (blame->pack_fds) {
5906 const struct got_error *pack_err =
5907 got_repo_pack_fds_close(blame->pack_fds);
5910 blame->pack_fds = NULL;
5915 static const struct got_error *
5916 cancel_blame_view(void *arg)
5918 const struct got_error *err = NULL;
5922 errcode = pthread_mutex_lock(&tog_mutex);
5924 return got_error_set_errno(errcode,
5925 "pthread_mutex_unlock");
5928 err = got_error(GOT_ERR_CANCELLED);
5930 errcode = pthread_mutex_unlock(&tog_mutex);
5932 return got_error_set_errno(errcode,
5933 "pthread_mutex_lock");
5938 static const struct got_error *
5939 run_blame(struct tog_view *view)
5941 struct tog_blame_view_state *s = &view->state.blame;
5942 struct tog_blame *blame = &s->blame;
5943 const struct got_error *err = NULL;
5944 struct got_commit_object *commit = NULL;
5945 struct got_blob_object *blob = NULL;
5946 struct got_repository *thread_repo = NULL;
5947 struct got_object_id *obj_id = NULL;
5948 int obj_type, fd = -1;
5949 int *pack_fds = NULL;
5951 err = got_object_open_as_commit(&commit, s->repo,
5952 &s->blamed_commit->id);
5956 fd = got_opentempfd();
5958 err = got_error_from_errno("got_opentempfd");
5962 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5966 err = got_object_get_type(&obj_type, s->repo, obj_id);
5970 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5971 err = got_error(GOT_ERR_OBJ_TYPE);
5975 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5978 blame->f = got_opentemp();
5979 if (blame->f == NULL) {
5980 err = got_error_from_errno("got_opentemp");
5983 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5984 &blame->line_offsets, blame->f, blob);
5987 if (blame->nlines == 0) {
5988 s->blame_complete = 1;
5992 /* Don't include \n at EOF in the blame line count. */
5993 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5996 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5997 if (blame->lines == NULL) {
5998 err = got_error_from_errno("calloc");
6002 err = got_repo_pack_fds_open(&pack_fds);
6005 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6010 blame->pack_fds = pack_fds;
6011 blame->cb_args.view = view;
6012 blame->cb_args.lines = blame->lines;
6013 blame->cb_args.nlines = blame->nlines;
6014 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6015 if (blame->cb_args.commit_id == NULL) {
6016 err = got_error_from_errno("got_object_id_dup");
6019 blame->cb_args.quit = &s->done;
6021 blame->thread_args.path = s->path;
6022 blame->thread_args.repo = thread_repo;
6023 blame->thread_args.cb_args = &blame->cb_args;
6024 blame->thread_args.complete = &s->blame_complete;
6025 blame->thread_args.cancel_cb = cancel_blame_view;
6026 blame->thread_args.cancel_arg = &s->done;
6027 s->blame_complete = 0;
6029 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6030 s->first_displayed_line = 1;
6031 s->last_displayed_line = view->nlines;
6032 s->selected_line = 1;
6034 s->matched_line = 0;
6038 got_object_commit_close(commit);
6039 if (fd != -1 && close(fd) == -1 && err == NULL)
6040 err = got_error_from_errno("close");
6042 got_object_blob_close(blob);
6049 static const struct got_error *
6050 open_blame_view(struct tog_view *view, char *path,
6051 struct got_object_id *commit_id, struct got_repository *repo)
6053 const struct got_error *err = NULL;
6054 struct tog_blame_view_state *s = &view->state.blame;
6056 STAILQ_INIT(&s->blamed_commits);
6058 s->path = strdup(path);
6059 if (s->path == NULL)
6060 return got_error_from_errno("strdup");
6062 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6068 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6069 s->first_displayed_line = 1;
6070 s->last_displayed_line = view->nlines;
6071 s->selected_line = 1;
6072 s->blame_complete = 0;
6074 s->commit_id = commit_id;
6075 memset(&s->blame, 0, sizeof(s->blame));
6077 STAILQ_INIT(&s->colors);
6078 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6079 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6080 get_color_value("TOG_COLOR_COMMIT"));
6085 view->show = show_blame_view;
6086 view->input = input_blame_view;
6087 view->reset = reset_blame_view;
6088 view->close = close_blame_view;
6089 view->search_start = search_start_blame_view;
6090 view->search_setup = search_setup_blame_view;
6091 view->search_next = search_next_view_match;
6093 return run_blame(view);
6096 static const struct got_error *
6097 close_blame_view(struct tog_view *view)
6099 const struct got_error *err = NULL;
6100 struct tog_blame_view_state *s = &view->state.blame;
6102 if (s->blame.thread)
6103 err = stop_blame(&s->blame);
6105 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6106 struct got_object_qid *blamed_commit;
6107 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6108 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6109 got_object_qid_free(blamed_commit);
6113 free_colors(&s->colors);
6117 static const struct got_error *
6118 search_start_blame_view(struct tog_view *view)
6120 struct tog_blame_view_state *s = &view->state.blame;
6122 s->matched_line = 0;
6127 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6128 size_t *nlines, int **first, int **last, int **match, int **selected)
6130 struct tog_blame_view_state *s = &view->state.blame;
6133 *nlines = s->blame.nlines;
6134 *line_offsets = s->blame.line_offsets;
6135 *match = &s->matched_line;
6136 *first = &s->first_displayed_line;
6137 *last = &s->last_displayed_line;
6138 *selected = &s->selected_line;
6141 static const struct got_error *
6142 show_blame_view(struct tog_view *view)
6144 const struct got_error *err = NULL;
6145 struct tog_blame_view_state *s = &view->state.blame;
6148 if (s->blame.thread == NULL && !s->blame_complete) {
6149 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6150 &s->blame.thread_args);
6152 return got_error_set_errno(errcode, "pthread_create");
6154 halfdelay(1); /* fast refresh while annotating */
6157 if (s->blame_complete)
6158 halfdelay(10); /* disable fast refresh */
6160 err = draw_blame(view);
6166 static const struct got_error *
6167 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6168 struct got_repository *repo, struct got_object_id *id)
6170 struct tog_view *log_view;
6171 const struct got_error *err = NULL;
6175 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6176 if (log_view == NULL)
6177 return got_error_from_errno("view_open");
6179 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6181 view_close(log_view);
6183 *new_view = log_view;
6188 static const struct got_error *
6189 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6191 const struct got_error *err = NULL, *thread_err = NULL;
6192 struct tog_view *diff_view;
6193 struct tog_blame_view_state *s = &view->state.blame;
6194 int eos, nscroll, begin_y = 0, begin_x = 0;
6196 eos = nscroll = view->nlines - 2;
6197 if (view_is_hsplit_top(view))
6205 view->x = MAX(view->maxx - view->ncols / 3, 0);
6210 if (view->x + view->ncols / 3 < view->maxx)
6211 view->x += 2; /* move two columns right */
6217 view->x -= MIN(view->x, 2); /* move two columns back */
6226 s->selected_line = 1;
6227 s->first_displayed_line = 1;
6232 if (s->blame.nlines < eos) {
6233 s->selected_line = s->blame.nlines;
6234 s->first_displayed_line = 1;
6236 s->selected_line = eos;
6237 s->first_displayed_line = s->blame.nlines - (eos - 1);
6244 if (s->selected_line > 1)
6246 else if (s->selected_line == 1 &&
6247 s->first_displayed_line > 1)
6248 s->first_displayed_line--;
6259 if (s->first_displayed_line == 1) {
6260 if (view->count > 1)
6262 s->selected_line = MAX(1, s->selected_line - nscroll);
6266 if (s->first_displayed_line > nscroll)
6267 s->first_displayed_line -= nscroll;
6269 s->first_displayed_line = 1;
6274 if (s->selected_line < eos && s->first_displayed_line +
6275 s->selected_line <= s->blame.nlines)
6277 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6278 s->first_displayed_line++;
6284 struct got_object_id *id = NULL;
6287 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6288 s->first_displayed_line, s->selected_line);
6292 struct got_commit_object *commit, *pcommit;
6293 struct got_object_qid *pid;
6294 struct got_object_id *blob_id = NULL;
6296 err = got_object_open_as_commit(&commit,
6301 got_object_commit_get_parent_ids(commit));
6303 got_object_commit_close(commit);
6306 /* Check if path history ends here. */
6307 err = got_object_open_as_commit(&pcommit,
6311 err = got_object_id_by_path(&blob_id, s->repo,
6313 got_object_commit_close(pcommit);
6315 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6317 got_object_commit_close(commit);
6320 err = got_object_get_type(&obj_type, s->repo,
6323 /* Can't blame non-blob type objects. */
6324 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6325 got_object_commit_close(commit);
6328 err = got_object_qid_alloc(&s->blamed_commit,
6330 got_object_commit_close(commit);
6332 if (got_object_id_cmp(id,
6333 &s->blamed_commit->id) == 0)
6335 err = got_object_qid_alloc(&s->blamed_commit,
6341 thread_err = stop_blame(&s->blame);
6345 STAILQ_INSERT_HEAD(&s->blamed_commits,
6346 s->blamed_commit, entry);
6347 err = run_blame(view);
6353 struct got_object_qid *first;
6356 first = STAILQ_FIRST(&s->blamed_commits);
6357 if (!got_object_id_cmp(&first->id, s->commit_id))
6360 thread_err = stop_blame(&s->blame);
6364 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6365 got_object_qid_free(s->blamed_commit);
6367 STAILQ_FIRST(&s->blamed_commits);
6368 err = run_blame(view);
6375 s->id_to_log = get_selected_commit_id(s->blame.lines,
6376 s->blame.nlines, s->first_displayed_line, s->selected_line);
6378 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6382 struct got_object_id *id = NULL;
6383 struct got_object_qid *pid;
6384 struct got_commit_object *commit = NULL;
6387 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6388 s->first_displayed_line, s->selected_line);
6391 err = got_object_open_as_commit(&commit, s->repo, id);
6394 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6396 /* traversed from diff view, release diff resources */
6397 err = close_diff_view(*new_view);
6400 diff_view = *new_view;
6402 if (view_is_parent_view(view))
6403 view_get_split(view, &begin_y, &begin_x);
6405 diff_view = view_open(0, 0, begin_y, begin_x,
6407 if (diff_view == NULL) {
6408 got_object_commit_close(commit);
6409 err = got_error_from_errno("view_open");
6413 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6414 id, NULL, NULL, 3, 0, 0, view, s->repo);
6415 got_object_commit_close(commit);
6417 view_close(diff_view);
6420 s->last_diffed_line = s->first_displayed_line - 1 +
6423 break; /* still open from active diff view */
6424 if (view_is_parent_view(view) &&
6425 view->mode == TOG_VIEW_SPLIT_HRZN) {
6426 err = view_init_hsplit(view, begin_y);
6432 diff_view->focussed = 1;
6433 diff_view->mode = view->mode;
6434 diff_view->nlines = view->lines - begin_y;
6435 if (view_is_parent_view(view)) {
6436 view_transfer_size(diff_view, view);
6437 err = view_close_child(view);
6440 err = view_set_child(view, diff_view);
6443 view->focus_child = 1;
6445 *new_view = diff_view;
6458 if (s->last_displayed_line >= s->blame.nlines &&
6459 s->selected_line >= MIN(s->blame.nlines,
6460 view->nlines - 2)) {
6464 if (s->last_displayed_line >= s->blame.nlines &&
6465 s->selected_line < view->nlines - 2) {
6467 MIN(nscroll, s->last_displayed_line -
6468 s->first_displayed_line - s->selected_line + 1);
6470 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6471 s->first_displayed_line += nscroll;
6473 s->first_displayed_line =
6474 s->blame.nlines - (view->nlines - 3);
6477 if (s->selected_line > view->nlines - 2) {
6478 s->selected_line = MIN(s->blame.nlines,
6486 return thread_err ? thread_err : err;
6489 static const struct got_error *
6490 reset_blame_view(struct tog_view *view)
6492 const struct got_error *err;
6493 struct tog_blame_view_state *s = &view->state.blame;
6497 err = stop_blame(&s->blame);
6501 return run_blame(view);
6504 static const struct got_error *
6505 cmd_blame(int argc, char *argv[])
6507 const struct got_error *error;
6508 struct got_repository *repo = NULL;
6509 struct got_worktree *worktree = NULL;
6510 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6511 char *link_target = NULL;
6512 struct got_object_id *commit_id = NULL;
6513 struct got_commit_object *commit = NULL;
6514 char *commit_id_str = NULL;
6516 struct tog_view *view;
6517 int *pack_fds = NULL;
6519 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6522 commit_id_str = optarg;
6525 repo_path = realpath(optarg, NULL);
6526 if (repo_path == NULL)
6527 return got_error_from_errno2("realpath",
6542 error = got_repo_pack_fds_open(&pack_fds);
6546 if (repo_path == NULL) {
6547 cwd = getcwd(NULL, 0);
6549 return got_error_from_errno("getcwd");
6550 error = got_worktree_open(&worktree, cwd);
6551 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6555 strdup(got_worktree_get_repo_path(worktree));
6557 repo_path = strdup(cwd);
6558 if (repo_path == NULL) {
6559 error = got_error_from_errno("strdup");
6564 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6568 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6575 error = apply_unveil(got_repo_get_path(repo), NULL);
6579 error = tog_load_refs(repo, 0);
6583 if (commit_id_str == NULL) {
6584 struct got_reference *head_ref;
6585 error = got_ref_open(&head_ref, repo, worktree ?
6586 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6589 error = got_ref_resolve(&commit_id, repo, head_ref);
6590 got_ref_close(head_ref);
6592 error = got_repo_match_object_id(&commit_id, NULL,
6593 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6598 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6600 error = got_error_from_errno("view_open");
6604 error = got_object_open_as_commit(&commit, repo, commit_id);
6608 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6613 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6618 /* Release work tree lock. */
6619 got_worktree_close(worktree);
6622 error = view_loop(view);
6630 got_object_commit_close(commit);
6632 got_worktree_close(worktree);
6634 const struct got_error *close_err = got_repo_close(repo);
6639 const struct got_error *pack_err =
6640 got_repo_pack_fds_close(pack_fds);
6648 static const struct got_error *
6649 draw_tree_entries(struct tog_view *view, const char *parent_path)
6651 struct tog_tree_view_state *s = &view->state.tree;
6652 const struct got_error *err = NULL;
6653 struct got_tree_entry *te;
6656 struct tog_color *tc;
6657 int width, n, nentries, i = 1;
6658 int limit = view->nlines;
6661 if (view_is_hsplit_top(view))
6662 --limit; /* border */
6664 werase(view->window);
6669 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6673 if (view_needs_focus_indication(view))
6674 wstandout(view->window);
6675 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6677 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6678 waddwstr(view->window, wline);
6681 while (width++ < view->ncols)
6682 waddch(view->window, ' ');
6684 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6685 if (view_needs_focus_indication(view))
6686 wstandend(view->window);
6691 if (s->first_displayed_entry) {
6692 i += got_tree_entry_get_index(s->first_displayed_entry);
6693 if (s->tree != s->root)
6694 ++i; /* account for ".." entry */
6696 nentries = got_object_tree_get_nentries(s->tree);
6697 if (asprintf(&index, "[%d/%d] %s",
6698 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6699 return got_error_from_errno("asprintf");
6700 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6704 waddwstr(view->window, wline);
6707 if (width < view->ncols - 1)
6708 waddch(view->window, '\n');
6711 waddch(view->window, '\n');
6715 if (s->first_displayed_entry == NULL) {
6716 te = got_object_tree_get_first_entry(s->tree);
6717 if (s->selected == 0) {
6719 wstandout(view->window);
6720 s->selected_entry = NULL;
6722 waddstr(view->window, " ..\n"); /* parent directory */
6723 if (s->selected == 0 && view->focussed)
6724 wstandend(view->window);
6731 te = s->first_displayed_entry;
6734 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6735 char *line = NULL, *id_str = NULL, *link_target = NULL;
6736 const char *modestr = "";
6739 te = got_object_tree_get_entry(s->tree, i);
6740 mode = got_tree_entry_get_mode(te);
6743 err = got_object_id_str(&id_str,
6744 got_tree_entry_get_id(te));
6746 return got_error_from_errno(
6747 "got_object_id_str");
6749 if (got_object_tree_entry_is_submodule(te))
6751 else if (S_ISLNK(mode)) {
6754 err = got_tree_entry_get_symlink_target(&link_target,
6760 for (i = 0; i < strlen(link_target); i++) {
6761 if (!isprint((unsigned char)link_target[i]))
6762 link_target[i] = '?';
6766 else if (S_ISDIR(mode))
6768 else if (mode & S_IXUSR)
6770 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6771 got_tree_entry_get_name(te), modestr,
6772 link_target ? " -> ": "",
6773 link_target ? link_target : "") == -1) {
6776 return got_error_from_errno("asprintf");
6780 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6786 if (n == s->selected) {
6788 wstandout(view->window);
6789 s->selected_entry = te;
6791 tc = match_color(&s->colors, line);
6793 wattr_on(view->window,
6794 COLOR_PAIR(tc->colorpair), NULL);
6795 waddwstr(view->window, wline);
6797 wattr_off(view->window,
6798 COLOR_PAIR(tc->colorpair), NULL);
6799 if (width < view->ncols - 1)
6800 waddch(view->window, '\n');
6801 if (n == s->selected && view->focussed)
6802 wstandend(view->window);
6808 s->last_displayed_entry = te;
6817 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6819 struct got_tree_entry *te;
6820 int isroot = s->tree == s->root;
6823 if (s->first_displayed_entry == NULL)
6826 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6827 while (i++ < maxscroll) {
6830 s->first_displayed_entry = NULL;
6833 s->first_displayed_entry = te;
6834 te = got_tree_entry_get_prev(s->tree, te);
6838 static const struct got_error *
6839 tree_scroll_down(struct tog_view *view, int maxscroll)
6841 struct tog_tree_view_state *s = &view->state.tree;
6842 struct got_tree_entry *next, *last;
6845 if (s->first_displayed_entry)
6846 next = got_tree_entry_get_next(s->tree,
6847 s->first_displayed_entry);
6849 next = got_object_tree_get_first_entry(s->tree);
6851 last = s->last_displayed_entry;
6852 while (next && n++ < maxscroll) {
6854 s->last_displayed_entry = last;
6855 last = got_tree_entry_get_next(s->tree, last);
6857 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6858 s->first_displayed_entry = next;
6859 next = got_tree_entry_get_next(s->tree, next);
6866 static const struct got_error *
6867 tree_entry_path(char **path, struct tog_parent_trees *parents,
6868 struct got_tree_entry *te)
6870 const struct got_error *err = NULL;
6871 struct tog_parent_tree *pt;
6872 size_t len = 2; /* for leading slash and NUL */
6874 TAILQ_FOREACH(pt, parents, entry)
6875 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6878 len += strlen(got_tree_entry_get_name(te));
6880 *path = calloc(1, len);
6882 return got_error_from_errno("calloc");
6885 pt = TAILQ_LAST(parents, tog_parent_trees);
6887 const char *name = got_tree_entry_get_name(pt->selected_entry);
6888 if (strlcat(*path, name, len) >= len) {
6889 err = got_error(GOT_ERR_NO_SPACE);
6892 if (strlcat(*path, "/", len) >= len) {
6893 err = got_error(GOT_ERR_NO_SPACE);
6896 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6899 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6900 err = got_error(GOT_ERR_NO_SPACE);
6912 static const struct got_error *
6913 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6914 struct got_tree_entry *te, struct tog_parent_trees *parents,
6915 struct got_object_id *commit_id, struct got_repository *repo)
6917 const struct got_error *err = NULL;
6919 struct tog_view *blame_view;
6923 err = tree_entry_path(&path, parents, te);
6927 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6928 if (blame_view == NULL) {
6929 err = got_error_from_errno("view_open");
6933 err = open_blame_view(blame_view, path, commit_id, repo);
6935 if (err->code == GOT_ERR_CANCELLED)
6937 view_close(blame_view);
6939 *new_view = blame_view;
6945 static const struct got_error *
6946 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6947 struct tog_tree_view_state *s)
6949 struct tog_view *log_view;
6950 const struct got_error *err = NULL;
6955 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6956 if (log_view == NULL)
6957 return got_error_from_errno("view_open");
6959 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6963 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6966 view_close(log_view);
6968 *new_view = log_view;
6973 static const struct got_error *
6974 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6975 const char *head_ref_name, struct got_repository *repo)
6977 const struct got_error *err = NULL;
6978 char *commit_id_str = NULL;
6979 struct tog_tree_view_state *s = &view->state.tree;
6980 struct got_commit_object *commit = NULL;
6982 TAILQ_INIT(&s->parents);
6983 STAILQ_INIT(&s->colors);
6985 s->commit_id = got_object_id_dup(commit_id);
6986 if (s->commit_id == NULL)
6987 return got_error_from_errno("got_object_id_dup");
6989 err = got_object_open_as_commit(&commit, repo, commit_id);
6994 * The root is opened here and will be closed when the view is closed.
6995 * Any visited subtrees and their path-wise parents are opened and
6998 err = got_object_open_as_tree(&s->root, repo,
6999 got_object_commit_get_tree_id(commit));
7004 err = got_object_id_str(&commit_id_str, commit_id);
7008 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7009 err = got_error_from_errno("asprintf");
7013 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7014 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7015 if (head_ref_name) {
7016 s->head_ref_name = strdup(head_ref_name);
7017 if (s->head_ref_name == NULL) {
7018 err = got_error_from_errno("strdup");
7024 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7025 err = add_color(&s->colors, "\\$$",
7026 TOG_COLOR_TREE_SUBMODULE,
7027 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7030 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7031 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7034 err = add_color(&s->colors, "/$",
7035 TOG_COLOR_TREE_DIRECTORY,
7036 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7040 err = add_color(&s->colors, "\\*$",
7041 TOG_COLOR_TREE_EXECUTABLE,
7042 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7046 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7047 get_color_value("TOG_COLOR_COMMIT"));
7052 view->show = show_tree_view;
7053 view->input = input_tree_view;
7054 view->close = close_tree_view;
7055 view->search_start = search_start_tree_view;
7056 view->search_next = search_next_tree_view;
7058 free(commit_id_str);
7060 got_object_commit_close(commit);
7062 close_tree_view(view);
7066 static const struct got_error *
7067 close_tree_view(struct tog_view *view)
7069 struct tog_tree_view_state *s = &view->state.tree;
7071 free_colors(&s->colors);
7072 free(s->tree_label);
7073 s->tree_label = NULL;
7075 s->commit_id = NULL;
7076 free(s->head_ref_name);
7077 s->head_ref_name = NULL;
7078 while (!TAILQ_EMPTY(&s->parents)) {
7079 struct tog_parent_tree *parent;
7080 parent = TAILQ_FIRST(&s->parents);
7081 TAILQ_REMOVE(&s->parents, parent, entry);
7082 if (parent->tree != s->root)
7083 got_object_tree_close(parent->tree);
7087 if (s->tree != NULL && s->tree != s->root)
7088 got_object_tree_close(s->tree);
7090 got_object_tree_close(s->root);
7094 static const struct got_error *
7095 search_start_tree_view(struct tog_view *view)
7097 struct tog_tree_view_state *s = &view->state.tree;
7099 s->matched_entry = NULL;
7104 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7106 regmatch_t regmatch;
7108 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7112 static const struct got_error *
7113 search_next_tree_view(struct tog_view *view)
7115 struct tog_tree_view_state *s = &view->state.tree;
7116 struct got_tree_entry *te = NULL;
7118 if (!view->searching) {
7119 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7123 if (s->matched_entry) {
7124 if (view->searching == TOG_SEARCH_FORWARD) {
7125 if (s->selected_entry)
7126 te = got_tree_entry_get_next(s->tree,
7129 te = got_object_tree_get_first_entry(s->tree);
7131 if (s->selected_entry == NULL)
7132 te = got_object_tree_get_last_entry(s->tree);
7134 te = got_tree_entry_get_prev(s->tree,
7138 if (s->selected_entry)
7139 te = s->selected_entry;
7140 else if (view->searching == TOG_SEARCH_FORWARD)
7141 te = got_object_tree_get_first_entry(s->tree);
7143 te = got_object_tree_get_last_entry(s->tree);
7148 if (s->matched_entry == NULL) {
7149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7152 if (view->searching == TOG_SEARCH_FORWARD)
7153 te = got_object_tree_get_first_entry(s->tree);
7155 te = got_object_tree_get_last_entry(s->tree);
7158 if (match_tree_entry(te, &view->regex)) {
7159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7160 s->matched_entry = te;
7164 if (view->searching == TOG_SEARCH_FORWARD)
7165 te = got_tree_entry_get_next(s->tree, te);
7167 te = got_tree_entry_get_prev(s->tree, te);
7170 if (s->matched_entry) {
7171 s->first_displayed_entry = s->matched_entry;
7178 static const struct got_error *
7179 show_tree_view(struct tog_view *view)
7181 const struct got_error *err = NULL;
7182 struct tog_tree_view_state *s = &view->state.tree;
7185 err = tree_entry_path(&parent_path, &s->parents, NULL);
7189 err = draw_tree_entries(view, parent_path);
7196 static const struct got_error *
7197 tree_goto_line(struct tog_view *view, int nlines)
7199 const struct got_error *err = NULL;
7200 struct tog_tree_view_state *s = &view->state.tree;
7201 struct got_tree_entry **fte, **lte, **ste;
7202 int g, last, first = 1, i = 1;
7203 int root = s->tree == s->root;
7204 int off = root ? 1 : 2;
7211 else if (g > got_object_tree_get_nentries(s->tree))
7212 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7214 fte = &s->first_displayed_entry;
7215 lte = &s->last_displayed_entry;
7216 ste = &s->selected_entry;
7219 first = got_tree_entry_get_index(*fte);
7220 first += off; /* account for ".." */
7222 last = got_tree_entry_get_index(*lte);
7225 if (g >= first && g <= last && g - first < nlines) {
7226 s->selected = g - first;
7227 return NULL; /* gline is on the current page */
7231 i = got_tree_entry_get_index(*ste);
7236 err = tree_scroll_down(view, g - i);
7239 if (got_tree_entry_get_index(*lte) >=
7240 got_object_tree_get_nentries(s->tree) - 1 &&
7241 first + s->selected < g &&
7242 s->selected < s->ndisplayed - 1) {
7243 first = got_tree_entry_get_index(*fte);
7245 s->selected = g - first;
7248 tree_scroll_up(s, i - g);
7251 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7252 s->selected = g - 1;
7257 static const struct got_error *
7258 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7260 const struct got_error *err = NULL;
7261 struct tog_tree_view_state *s = &view->state.tree;
7262 struct got_tree_entry *te;
7263 int n, nscroll = view->nlines - 3;
7266 return tree_goto_line(view, nscroll);
7270 s->show_ids = !s->show_ids;
7275 if (!s->selected_entry)
7277 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7281 err = view_request_new(new_view, view, TOG_VIEW_REF);
7287 if (s->tree == s->root)
7288 s->first_displayed_entry =
7289 got_object_tree_get_first_entry(s->tree);
7291 s->first_displayed_entry = NULL;
7295 int eos = view->nlines - 3;
7297 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7301 te = got_object_tree_get_last_entry(s->tree);
7302 for (n = 0; n < eos; n++) {
7304 if (s->tree != s->root) {
7305 s->first_displayed_entry = NULL;
7310 s->first_displayed_entry = te;
7311 te = got_tree_entry_get_prev(s->tree, te);
7314 s->selected = n - 1;
7320 if (s->selected > 0) {
7324 tree_scroll_up(s, 1);
7325 if (s->selected_entry == NULL ||
7326 (s->tree == s->root && s->selected_entry ==
7327 got_object_tree_get_first_entry(s->tree)))
7337 if (s->tree == s->root) {
7338 if (got_object_tree_get_first_entry(s->tree) ==
7339 s->first_displayed_entry)
7340 s->selected -= MIN(s->selected, nscroll);
7342 if (s->first_displayed_entry == NULL)
7343 s->selected -= MIN(s->selected, nscroll);
7345 tree_scroll_up(s, MAX(0, nscroll));
7346 if (s->selected_entry == NULL ||
7347 (s->tree == s->root && s->selected_entry ==
7348 got_object_tree_get_first_entry(s->tree)))
7354 if (s->selected < s->ndisplayed - 1) {
7358 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7360 /* can't scroll any further */
7364 tree_scroll_down(view, 1);
7374 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7376 /* can't scroll any further; move cursor down */
7377 if (s->selected < s->ndisplayed - 1)
7378 s->selected += MIN(nscroll,
7379 s->ndisplayed - s->selected - 1);
7384 tree_scroll_down(view, nscroll);
7389 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7390 struct tog_parent_tree *parent;
7391 /* user selected '..' */
7392 if (s->tree == s->root) {
7396 parent = TAILQ_FIRST(&s->parents);
7397 TAILQ_REMOVE(&s->parents, parent,
7399 got_object_tree_close(s->tree);
7400 s->tree = parent->tree;
7401 s->first_displayed_entry =
7402 parent->first_displayed_entry;
7404 parent->selected_entry;
7405 s->selected = parent->selected;
7406 if (s->selected > view->nlines - 3) {
7407 err = offset_selection_down(view);
7412 } else if (S_ISDIR(got_tree_entry_get_mode(
7413 s->selected_entry))) {
7414 struct got_tree_object *subtree;
7416 err = got_object_open_as_tree(&subtree, s->repo,
7417 got_tree_entry_get_id(s->selected_entry));
7420 err = tree_view_visit_subtree(s, subtree);
7422 got_object_tree_close(subtree);
7425 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7426 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7429 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7430 s->selected = view->nlines - 4;
7446 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7451 static const struct got_error *
7452 cmd_tree(int argc, char *argv[])
7454 const struct got_error *error;
7455 struct got_repository *repo = NULL;
7456 struct got_worktree *worktree = NULL;
7457 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7458 struct got_object_id *commit_id = NULL;
7459 struct got_commit_object *commit = NULL;
7460 const char *commit_id_arg = NULL;
7462 struct got_reference *ref = NULL;
7463 const char *head_ref_name = NULL;
7465 struct tog_view *view;
7466 int *pack_fds = NULL;
7468 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7471 commit_id_arg = optarg;
7474 repo_path = realpath(optarg, NULL);
7475 if (repo_path == NULL)
7476 return got_error_from_errno2("realpath",
7491 error = got_repo_pack_fds_open(&pack_fds);
7495 if (repo_path == NULL) {
7496 cwd = getcwd(NULL, 0);
7498 return got_error_from_errno("getcwd");
7499 error = got_worktree_open(&worktree, cwd);
7500 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7504 strdup(got_worktree_get_repo_path(worktree));
7506 repo_path = strdup(cwd);
7507 if (repo_path == NULL) {
7508 error = got_error_from_errno("strdup");
7513 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7517 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7524 error = apply_unveil(got_repo_get_path(repo), NULL);
7528 error = tog_load_refs(repo, 0);
7532 if (commit_id_arg == NULL) {
7533 error = got_repo_match_object_id(&commit_id, &label,
7534 worktree ? got_worktree_get_head_ref_name(worktree) :
7535 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7538 head_ref_name = label;
7540 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7542 head_ref_name = got_ref_get_name(ref);
7543 else if (error->code != GOT_ERR_NOT_REF)
7545 error = got_repo_match_object_id(&commit_id, NULL,
7546 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7551 error = got_object_open_as_commit(&commit, repo, commit_id);
7555 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7557 error = got_error_from_errno("view_open");
7560 error = open_tree_view(view, commit_id, head_ref_name, repo);
7563 if (!got_path_is_root_dir(in_repo_path)) {
7564 error = tree_view_walk_path(&view->state.tree, commit,
7571 /* Release work tree lock. */
7572 got_worktree_close(worktree);
7575 error = view_loop(view);
7584 const struct got_error *close_err = got_repo_close(repo);
7589 const struct got_error *pack_err =
7590 got_repo_pack_fds_close(pack_fds);
7598 static const struct got_error *
7599 ref_view_load_refs(struct tog_ref_view_state *s)
7601 struct got_reflist_entry *sre;
7602 struct tog_reflist_entry *re;
7605 TAILQ_FOREACH(sre, &tog_refs, entry) {
7606 if (strncmp(got_ref_get_name(sre->ref),
7607 "refs/got/", 9) == 0 &&
7608 strncmp(got_ref_get_name(sre->ref),
7609 "refs/got/backup/", 16) != 0)
7612 re = malloc(sizeof(*re));
7614 return got_error_from_errno("malloc");
7616 re->ref = got_ref_dup(sre->ref);
7617 if (re->ref == NULL)
7618 return got_error_from_errno("got_ref_dup");
7619 re->idx = s->nrefs++;
7620 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7623 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7628 ref_view_free_refs(struct tog_ref_view_state *s)
7630 struct tog_reflist_entry *re;
7632 while (!TAILQ_EMPTY(&s->refs)) {
7633 re = TAILQ_FIRST(&s->refs);
7634 TAILQ_REMOVE(&s->refs, re, entry);
7635 got_ref_close(re->ref);
7640 static const struct got_error *
7641 open_ref_view(struct tog_view *view, struct got_repository *repo)
7643 const struct got_error *err = NULL;
7644 struct tog_ref_view_state *s = &view->state.ref;
7646 s->selected_entry = 0;
7649 TAILQ_INIT(&s->refs);
7650 STAILQ_INIT(&s->colors);
7652 err = ref_view_load_refs(s);
7656 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7657 err = add_color(&s->colors, "^refs/heads/",
7658 TOG_COLOR_REFS_HEADS,
7659 get_color_value("TOG_COLOR_REFS_HEADS"));
7663 err = add_color(&s->colors, "^refs/tags/",
7664 TOG_COLOR_REFS_TAGS,
7665 get_color_value("TOG_COLOR_REFS_TAGS"));
7669 err = add_color(&s->colors, "^refs/remotes/",
7670 TOG_COLOR_REFS_REMOTES,
7671 get_color_value("TOG_COLOR_REFS_REMOTES"));
7675 err = add_color(&s->colors, "^refs/got/backup/",
7676 TOG_COLOR_REFS_BACKUP,
7677 get_color_value("TOG_COLOR_REFS_BACKUP"));
7682 view->show = show_ref_view;
7683 view->input = input_ref_view;
7684 view->close = close_ref_view;
7685 view->search_start = search_start_ref_view;
7686 view->search_next = search_next_ref_view;
7689 free_colors(&s->colors);
7693 static const struct got_error *
7694 close_ref_view(struct tog_view *view)
7696 struct tog_ref_view_state *s = &view->state.ref;
7698 ref_view_free_refs(s);
7699 free_colors(&s->colors);
7704 static const struct got_error *
7705 resolve_reflist_entry(struct got_object_id **commit_id,
7706 struct tog_reflist_entry *re, struct got_repository *repo)
7708 const struct got_error *err = NULL;
7709 struct got_object_id *obj_id;
7710 struct got_tag_object *tag = NULL;
7715 err = got_ref_resolve(&obj_id, repo, re->ref);
7719 err = got_object_get_type(&obj_type, repo, obj_id);
7724 case GOT_OBJ_TYPE_COMMIT:
7725 *commit_id = obj_id;
7727 case GOT_OBJ_TYPE_TAG:
7728 err = got_object_open_as_tag(&tag, repo, obj_id);
7732 err = got_object_get_type(&obj_type, repo,
7733 got_object_tag_get_object_id(tag));
7736 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7737 err = got_error(GOT_ERR_OBJ_TYPE);
7740 *commit_id = got_object_id_dup(
7741 got_object_tag_get_object_id(tag));
7742 if (*commit_id == NULL) {
7743 err = got_error_from_errno("got_object_id_dup");
7748 err = got_error(GOT_ERR_OBJ_TYPE);
7754 got_object_tag_close(tag);
7762 static const struct got_error *
7763 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7764 struct tog_reflist_entry *re, struct got_repository *repo)
7766 struct tog_view *log_view;
7767 const struct got_error *err = NULL;
7768 struct got_object_id *commit_id = NULL;
7772 err = resolve_reflist_entry(&commit_id, re, repo);
7774 if (err->code != GOT_ERR_OBJ_TYPE)
7780 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7781 if (log_view == NULL) {
7782 err = got_error_from_errno("view_open");
7786 err = open_log_view(log_view, commit_id, repo,
7787 got_ref_get_name(re->ref), "", 0);
7790 view_close(log_view);
7792 *new_view = log_view;
7798 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7800 struct tog_reflist_entry *re;
7803 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7806 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7807 while (i++ < maxscroll) {
7810 s->first_displayed_entry = re;
7811 re = TAILQ_PREV(re, tog_reflist_head, entry);
7815 static const struct got_error *
7816 ref_scroll_down(struct tog_view *view, int maxscroll)
7818 struct tog_ref_view_state *s = &view->state.ref;
7819 struct tog_reflist_entry *next, *last;
7822 if (s->first_displayed_entry)
7823 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7825 next = TAILQ_FIRST(&s->refs);
7827 last = s->last_displayed_entry;
7828 while (next && n++ < maxscroll) {
7830 s->last_displayed_entry = last;
7831 last = TAILQ_NEXT(last, entry);
7833 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7834 s->first_displayed_entry = next;
7835 next = TAILQ_NEXT(next, entry);
7842 static const struct got_error *
7843 search_start_ref_view(struct tog_view *view)
7845 struct tog_ref_view_state *s = &view->state.ref;
7847 s->matched_entry = NULL;
7852 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7854 regmatch_t regmatch;
7856 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7860 static const struct got_error *
7861 search_next_ref_view(struct tog_view *view)
7863 struct tog_ref_view_state *s = &view->state.ref;
7864 struct tog_reflist_entry *re = NULL;
7866 if (!view->searching) {
7867 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7871 if (s->matched_entry) {
7872 if (view->searching == TOG_SEARCH_FORWARD) {
7873 if (s->selected_entry)
7874 re = TAILQ_NEXT(s->selected_entry, entry);
7876 re = TAILQ_PREV(s->selected_entry,
7877 tog_reflist_head, entry);
7879 if (s->selected_entry == NULL)
7880 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7882 re = TAILQ_PREV(s->selected_entry,
7883 tog_reflist_head, entry);
7886 if (s->selected_entry)
7887 re = s->selected_entry;
7888 else if (view->searching == TOG_SEARCH_FORWARD)
7889 re = TAILQ_FIRST(&s->refs);
7891 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7896 if (s->matched_entry == NULL) {
7897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7900 if (view->searching == TOG_SEARCH_FORWARD)
7901 re = TAILQ_FIRST(&s->refs);
7903 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7906 if (match_reflist_entry(re, &view->regex)) {
7907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7908 s->matched_entry = re;
7912 if (view->searching == TOG_SEARCH_FORWARD)
7913 re = TAILQ_NEXT(re, entry);
7915 re = TAILQ_PREV(re, tog_reflist_head, entry);
7918 if (s->matched_entry) {
7919 s->first_displayed_entry = s->matched_entry;
7926 static const struct got_error *
7927 show_ref_view(struct tog_view *view)
7929 const struct got_error *err = NULL;
7930 struct tog_ref_view_state *s = &view->state.ref;
7931 struct tog_reflist_entry *re;
7934 struct tog_color *tc;
7936 int limit = view->nlines;
7938 werase(view->window);
7941 if (view_is_hsplit_top(view))
7942 --limit; /* border */
7947 re = s->first_displayed_entry;
7949 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7951 return got_error_from_errno("asprintf");
7953 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7958 if (view_needs_focus_indication(view))
7959 wstandout(view->window);
7960 waddwstr(view->window, wline);
7961 while (width++ < view->ncols)
7962 waddch(view->window, ' ');
7963 if (view_needs_focus_indication(view))
7964 wstandend(view->window);
7973 while (re && limit > 0) {
7975 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7978 struct got_commit_object *ci;
7979 struct got_tag_object *tag;
7980 struct got_object_id *id;
7984 err = got_ref_resolve(&id, s->repo, re->ref);
7987 err = got_object_open_as_tag(&tag, s->repo, id);
7989 if (err->code != GOT_ERR_OBJ_TYPE) {
7993 err = got_object_open_as_commit(&ci, s->repo,
7999 t = got_object_commit_get_committer_time(ci);
8000 got_object_commit_close(ci);
8002 t = got_object_tag_get_tagger_time(tag);
8003 got_object_tag_close(tag);
8006 if (gmtime_r(&t, &tm) == NULL)
8007 return got_error_from_errno("gmtime_r");
8008 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8009 return got_error(GOT_ERR_NO_SPACE);
8011 if (got_ref_is_symbolic(re->ref)) {
8012 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8013 ymd : "", got_ref_get_name(re->ref),
8014 got_ref_get_symref_target(re->ref)) == -1)
8015 return got_error_from_errno("asprintf");
8016 } else if (s->show_ids) {
8017 struct got_object_id *id;
8019 err = got_ref_resolve(&id, s->repo, re->ref);
8022 err = got_object_id_str(&id_str, id);
8027 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8028 got_ref_get_name(re->ref), id_str) == -1) {
8029 err = got_error_from_errno("asprintf");
8036 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8037 got_ref_get_name(re->ref)) == -1)
8038 return got_error_from_errno("asprintf");
8040 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8046 if (n == s->selected) {
8048 wstandout(view->window);
8049 s->selected_entry = re;
8051 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8053 wattr_on(view->window,
8054 COLOR_PAIR(tc->colorpair), NULL);
8055 waddwstr(view->window, wline);
8057 wattr_off(view->window,
8058 COLOR_PAIR(tc->colorpair), NULL);
8059 if (width < view->ncols - 1)
8060 waddch(view->window, '\n');
8061 if (n == s->selected && view->focussed)
8062 wstandend(view->window);
8068 s->last_displayed_entry = re;
8071 re = TAILQ_NEXT(re, entry);
8078 static const struct got_error *
8079 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8080 struct tog_reflist_entry *re, struct got_repository *repo)
8082 const struct got_error *err = NULL;
8083 struct got_object_id *commit_id = NULL;
8084 struct tog_view *tree_view;
8088 err = resolve_reflist_entry(&commit_id, re, repo);
8090 if (err->code != GOT_ERR_OBJ_TYPE)
8097 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8098 if (tree_view == NULL) {
8099 err = got_error_from_errno("view_open");
8103 err = open_tree_view(tree_view, commit_id,
8104 got_ref_get_name(re->ref), repo);
8108 *new_view = tree_view;
8114 static const struct got_error *
8115 ref_goto_line(struct tog_view *view, int nlines)
8117 const struct got_error *err = NULL;
8118 struct tog_ref_view_state *s = &view->state.ref;
8119 int g, idx = s->selected_entry->idx;
8126 else if (g > s->nrefs)
8129 if (g >= s->first_displayed_entry->idx + 1 &&
8130 g <= s->last_displayed_entry->idx + 1 &&
8131 g - s->first_displayed_entry->idx - 1 < nlines) {
8132 s->selected = g - s->first_displayed_entry->idx - 1;
8137 err = ref_scroll_down(view, g - idx - 1);
8140 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8141 s->first_displayed_entry->idx + s->selected < g &&
8142 s->selected < s->ndisplayed - 1)
8143 s->selected = g - s->first_displayed_entry->idx - 1;
8144 } else if (idx + 1 > g)
8145 ref_scroll_up(s, idx - g + 1);
8147 if (g < nlines && s->first_displayed_entry->idx == 0)
8148 s->selected = g - 1;
8154 static const struct got_error *
8155 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8157 const struct got_error *err = NULL;
8158 struct tog_ref_view_state *s = &view->state.ref;
8159 struct tog_reflist_entry *re;
8160 int n, nscroll = view->nlines - 1;
8163 return ref_goto_line(view, nscroll);
8167 s->show_ids = !s->show_ids;
8171 s->show_date = !s->show_date;
8175 s->sort_by_date = !s->sort_by_date;
8177 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8178 got_ref_cmp_by_commit_timestamp_descending :
8179 tog_ref_cmp_by_name, s->repo);
8182 got_reflist_object_id_map_free(tog_refs_idmap);
8183 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8184 &tog_refs, s->repo);
8187 ref_view_free_refs(s);
8188 err = ref_view_load_refs(s);
8193 if (!s->selected_entry)
8195 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8199 if (!s->selected_entry)
8201 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8207 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8211 int eos = view->nlines - 1;
8213 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8217 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8218 for (n = 0; n < eos; n++) {
8221 s->first_displayed_entry = re;
8222 re = TAILQ_PREV(re, tog_reflist_head, entry);
8225 s->selected = n - 1;
8231 if (s->selected > 0) {
8235 ref_scroll_up(s, 1);
8236 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8246 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8247 s->selected -= MIN(nscroll, s->selected);
8248 ref_scroll_up(s, MAX(0, nscroll));
8249 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8255 if (s->selected < s->ndisplayed - 1) {
8259 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8260 /* can't scroll any further */
8264 ref_scroll_down(view, 1);
8274 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8275 /* can't scroll any further; move cursor down */
8276 if (s->selected < s->ndisplayed - 1)
8277 s->selected += MIN(nscroll,
8278 s->ndisplayed - s->selected - 1);
8279 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8280 s->selected += s->ndisplayed - s->selected - 1;
8284 ref_scroll_down(view, nscroll);
8289 err = tog_load_refs(s->repo, s->sort_by_date);
8292 ref_view_free_refs(s);
8293 err = ref_view_load_refs(s);
8296 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8297 s->selected = view->nlines - 2;
8311 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8316 static const struct got_error *
8317 cmd_ref(int argc, char *argv[])
8319 const struct got_error *error;
8320 struct got_repository *repo = NULL;
8321 struct got_worktree *worktree = NULL;
8322 char *cwd = NULL, *repo_path = NULL;
8324 struct tog_view *view;
8325 int *pack_fds = NULL;
8327 while ((ch = getopt(argc, argv, "r:")) != -1) {
8330 repo_path = realpath(optarg, NULL);
8331 if (repo_path == NULL)
8332 return got_error_from_errno2("realpath",
8347 error = got_repo_pack_fds_open(&pack_fds);
8351 if (repo_path == NULL) {
8352 cwd = getcwd(NULL, 0);
8354 return got_error_from_errno("getcwd");
8355 error = got_worktree_open(&worktree, cwd);
8356 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8360 strdup(got_worktree_get_repo_path(worktree));
8362 repo_path = strdup(cwd);
8363 if (repo_path == NULL) {
8364 error = got_error_from_errno("strdup");
8369 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8375 error = apply_unveil(got_repo_get_path(repo), NULL);
8379 error = tog_load_refs(repo, 0);
8383 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8385 error = got_error_from_errno("view_open");
8389 error = open_ref_view(view, repo);
8394 /* Release work tree lock. */
8395 got_worktree_close(worktree);
8398 error = view_loop(view);
8403 const struct got_error *close_err = got_repo_close(repo);
8408 const struct got_error *pack_err =
8409 got_repo_pack_fds_close(pack_fds);
8417 static const struct got_error*
8418 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8427 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8431 if (mvwprintw(win, y, x, "%s", str) == ERR)
8432 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8439 static const struct got_error *
8440 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8444 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8446 free(*line_offsets);
8447 *line_offsets = NULL;
8448 return got_error_from_errno("reallocarray");
8452 (*line_offsets)[*nlines] = off;
8457 static const struct got_error *
8458 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8462 for (;n > 0; --n, ++km) {
8466 if (km->keys == NULL)
8469 t = t0 = strdup(km->keys);
8471 return got_error_from_errno("strdup");
8474 while ((k = strsep(&t, " ")) != NULL)
8475 len += strlen(k) > 1 ? 2 : 0;
8477 *ret = MAX(*ret, len);
8484 * Write keymap section headers, keys, and key info in km to f.
8485 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8486 * wrap control and symbolic keys in guillemets, else use <>.
8488 static const struct got_error *
8489 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8494 static const char *u8_glyph[] = {
8495 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8496 "\xe2\x80\xba" /* U+203A (utf8 >) */
8499 int cs, s, first = 1;
8501 cs = got_locale_is_utf8();
8503 t = t0 = strdup(km->keys);
8505 return got_error_from_errno("strdup");
8507 len = strlen(km->keys);
8508 while ((k = strsep(&t, " ")) != NULL) {
8509 s = strlen(k) > 1; /* control or symbolic key */
8510 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8511 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8512 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8515 return got_error_from_errno("fprintf");
8523 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8525 return got_error_from_errno("fprintf");
8531 static const struct got_error *
8532 format_help(struct tog_help_view_state *s)
8534 const struct got_error *err = NULL;
8536 int i, max, n, show = s->all;
8537 static const struct tog_key_map km[] = {
8538 #define KEYMAP_(info, type) { NULL, (info), type }
8539 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8545 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8550 err = max_key_str(&max, km, n);
8554 for (i = 0; i < n; ++i) {
8555 if (km[i].keys == NULL) {
8557 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8558 km[i].type == s->type || s->all)
8562 err = format_help_line(&off, s->f, &km[i], max);
8565 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8572 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8576 static const struct got_error *
8577 create_help(struct tog_help_view_state *s)
8580 const struct got_error *err;
8582 free(s->line_offsets);
8583 s->line_offsets = NULL;
8588 return got_error_from_errno("got_opentemp");
8591 err = format_help(s);
8595 if (s->f && fflush(s->f) != 0)
8596 return got_error_from_errno("fflush");
8601 static const struct got_error *
8602 search_start_help_view(struct tog_view *view)
8604 view->state.help.matched_line = 0;
8609 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8610 size_t *nlines, int **first, int **last, int **match, int **selected)
8612 struct tog_help_view_state *s = &view->state.help;
8615 *nlines = s->nlines;
8616 *line_offsets = s->line_offsets;
8617 *match = &s->matched_line;
8618 *first = &s->first_displayed_line;
8619 *last = &s->last_displayed_line;
8620 *selected = &s->selected_line;
8623 static const struct got_error *
8624 show_help_view(struct tog_view *view)
8626 struct tog_help_view_state *s = &view->state.help;
8627 const struct got_error *err;
8628 regmatch_t *regmatch = &view->regmatch;
8633 int width, nprinted = 0, rc = 0;
8634 int eos = view->nlines;
8636 if (view_is_hsplit_top(view))
8637 --eos; /* account for border */
8641 werase(view->window);
8643 if (view->gline > s->nlines - 1)
8644 view->gline = s->nlines - 1;
8646 err = win_draw_center(view->window, 0, 0, view->ncols,
8647 view_needs_focus_indication(view), "tog help");
8652 waddstr(view->window, "\n\n");
8658 while (eos > 0 && nprinted < eos) {
8661 linelen = getline(&line, &linesz, s->f);
8662 if (linelen == -1) {
8665 return got_ferror(s->f, GOT_ERR_IO);
8670 if (++s->lineno < s->first_displayed_line)
8672 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8674 if (s->lineno == view->hiline)
8677 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8683 view->maxx = MAX(view->maxx, width);
8688 wattron(view->window, attr);
8689 if (s->first_displayed_line + nprinted == s->matched_line &&
8690 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8691 err = add_matched_line(&width, line, view->ncols - 1, 0,
8692 view->window, view->x, regmatch);
8700 err = format_line(&wline, &width, &skip, line,
8701 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8706 rc = waddwstr(view->window, &wline[skip]);
8710 return got_error_msg(GOT_ERR_IO, "waddwstr");
8712 if (s->lineno == view->hiline) {
8713 while (width++ < view->ncols)
8714 waddch(view->window, ' ');
8716 if (width <= view->ncols)
8717 waddch(view->window, '\n');
8720 wattroff(view->window, attr);
8721 if (++nprinted == 1)
8722 s->first_displayed_line = s->lineno;
8726 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8728 s->last_displayed_line = s->first_displayed_line;
8733 rc = waddnstr(view->window,
8734 "See the tog(1) manual page for full documentation",
8737 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8739 wmove(view->window, view->nlines - 1, 0);
8740 wclrtoeol(view->window);
8741 wstandout(view->window);
8742 rc = waddnstr(view->window, "scroll down for more...",
8745 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8746 if (getcurx(view->window) < view->ncols - 6) {
8747 rc = wprintw(view->window, "[%.0f%%]",
8748 100.00 * s->last_displayed_line / s->nlines);
8750 return got_error_msg(GOT_ERR_IO, "wprintw");
8752 wstandend(view->window);
8758 static const struct got_error *
8759 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8761 struct tog_help_view_state *s = &view->state.help;
8762 const struct got_error *err = NULL;
8768 eos = nscroll = view->nlines;
8769 if (view_is_hsplit_top(view))
8772 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8779 view->x = MAX(view->maxx - view->ncols / 3, 0);
8784 if (view->x + view->ncols / 3 < view->maxx)
8791 view->x -= MIN(view->x, 2);
8797 s->first_displayed_line = 1;
8805 s->first_displayed_line = (s->nlines - eos) + 3;
8810 if (s->first_displayed_line > 1)
8811 --s->first_displayed_line;
8822 if (s->first_displayed_line == 1) {
8826 while (--nscroll > 0 && s->first_displayed_line > 1)
8827 s->first_displayed_line--;
8833 ++s->first_displayed_line;
8849 while (!s->eof && --nscroll > 0) {
8850 linelen = getline(&line, &linesz, s->f);
8851 s->first_displayed_line++;
8852 if (linelen == -1) {
8856 err = got_ferror(s->f, GOT_ERR_IO);
8870 static const struct got_error *
8871 close_help_view(struct tog_view *view)
8873 struct tog_help_view_state *s = &view->state.help;
8875 free(s->line_offsets);
8876 s->line_offsets = NULL;
8877 if (fclose(s->f) == EOF)
8878 return got_error_from_errno("fclose");
8883 static const struct got_error *
8884 reset_help_view(struct tog_view *view)
8886 struct tog_help_view_state *s = &view->state.help;
8889 if (s->f && fclose(s->f) == EOF)
8890 return got_error_from_errno("fclose");
8892 wclear(view->window);
8896 s->first_displayed_line = 1;
8897 s->last_displayed_line = view->nlines;
8898 s->matched_line = 0;
8900 return create_help(s);
8903 static const struct got_error *
8904 open_help_view(struct tog_view *view, struct tog_view *parent)
8906 const struct got_error *err = NULL;
8907 struct tog_help_view_state *s = &view->state.help;
8909 s->type = (enum tog_keymap_type)parent->type;
8910 s->first_displayed_line = 1;
8911 s->last_displayed_line = view->nlines;
8912 s->selected_line = 1;
8914 view->show = show_help_view;
8915 view->input = input_help_view;
8916 view->reset = reset_help_view;
8917 view->close = close_help_view;
8918 view->search_start = search_start_help_view;
8919 view->search_setup = search_setup_help_view;
8920 view->search_next = search_next_view_match;
8922 err = create_help(s);
8926 static const struct got_error *
8927 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8928 enum tog_view_type request, int y, int x)
8930 const struct got_error *err = NULL;
8936 if (view->type == TOG_VIEW_LOG) {
8937 struct tog_log_view_state *s = &view->state.log;
8939 err = open_diff_view_for_commit(new_view, y, x,
8940 s->selected_entry->commit, s->selected_entry->id,
8943 return got_error_msg(GOT_ERR_NOT_IMPL,
8944 "parent/child view pair not supported");
8946 case TOG_VIEW_BLAME:
8947 if (view->type == TOG_VIEW_TREE) {
8948 struct tog_tree_view_state *s = &view->state.tree;
8950 err = blame_tree_entry(new_view, y, x,
8951 s->selected_entry, &s->parents, s->commit_id,
8954 return got_error_msg(GOT_ERR_NOT_IMPL,
8955 "parent/child view pair not supported");
8958 if (view->type == TOG_VIEW_BLAME)
8959 err = log_annotated_line(new_view, y, x,
8960 view->state.blame.repo, view->state.blame.id_to_log);
8961 else if (view->type == TOG_VIEW_TREE)
8962 err = log_selected_tree_entry(new_view, y, x,
8964 else if (view->type == TOG_VIEW_REF)
8965 err = log_ref_entry(new_view, y, x,
8966 view->state.ref.selected_entry,
8967 view->state.ref.repo);
8969 return got_error_msg(GOT_ERR_NOT_IMPL,
8970 "parent/child view pair not supported");
8973 if (view->type == TOG_VIEW_LOG)
8974 err = browse_commit_tree(new_view, y, x,
8975 view->state.log.selected_entry,
8976 view->state.log.in_repo_path,
8977 view->state.log.head_ref_name,
8978 view->state.log.repo);
8979 else if (view->type == TOG_VIEW_REF)
8980 err = browse_ref_tree(new_view, y, x,
8981 view->state.ref.selected_entry,
8982 view->state.ref.repo);
8984 return got_error_msg(GOT_ERR_NOT_IMPL,
8985 "parent/child view pair not supported");
8988 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8989 if (*new_view == NULL)
8990 return got_error_from_errno("view_open");
8991 if (view->type == TOG_VIEW_LOG)
8992 err = open_ref_view(*new_view, view->state.log.repo);
8993 else if (view->type == TOG_VIEW_TREE)
8994 err = open_ref_view(*new_view, view->state.tree.repo);
8996 err = got_error_msg(GOT_ERR_NOT_IMPL,
8997 "parent/child view pair not supported");
8999 view_close(*new_view);
9002 *new_view = view_open(0, 0, y, x, TOG_VIEW_HELP);
9003 if (*new_view == NULL)
9004 return got_error_from_errno("view_open");
9005 err = open_help_view(*new_view, view);
9007 view_close(*new_view);
9010 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9017 * If view was scrolled down to move the selected line into view when opening a
9018 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9021 offset_selection_up(struct tog_view *view)
9023 switch (view->type) {
9024 case TOG_VIEW_BLAME: {
9025 struct tog_blame_view_state *s = &view->state.blame;
9026 if (s->first_displayed_line == 1) {
9027 s->selected_line = MAX(s->selected_line - view->offset,
9031 if (s->first_displayed_line > view->offset)
9032 s->first_displayed_line -= view->offset;
9034 s->first_displayed_line = 1;
9035 s->selected_line += view->offset;
9039 log_scroll_up(&view->state.log, view->offset);
9040 view->state.log.selected += view->offset;
9043 ref_scroll_up(&view->state.ref, view->offset);
9044 view->state.ref.selected += view->offset;
9047 tree_scroll_up(&view->state.tree, view->offset);
9048 view->state.tree.selected += view->offset;
9058 * If the selected line is in the section of screen covered by the bottom split,
9059 * scroll down offset lines to move it into view and index its new position.
9061 static const struct got_error *
9062 offset_selection_down(struct tog_view *view)
9064 const struct got_error *err = NULL;
9065 const struct got_error *(*scrolld)(struct tog_view *, int);
9066 int *selected = NULL;
9069 switch (view->type) {
9070 case TOG_VIEW_BLAME: {
9071 struct tog_blame_view_state *s = &view->state.blame;
9074 if (s->selected_line > view->nlines - header) {
9075 offset = abs(view->nlines - s->selected_line - header);
9076 s->first_displayed_line += offset;
9077 s->selected_line -= offset;
9078 view->offset = offset;
9082 case TOG_VIEW_LOG: {
9083 struct tog_log_view_state *s = &view->state.log;
9084 scrolld = &log_scroll_down;
9085 header = view_is_parent_view(view) ? 3 : 2;
9086 selected = &s->selected;
9089 case TOG_VIEW_REF: {
9090 struct tog_ref_view_state *s = &view->state.ref;
9091 scrolld = &ref_scroll_down;
9093 selected = &s->selected;
9096 case TOG_VIEW_TREE: {
9097 struct tog_tree_view_state *s = &view->state.tree;
9098 scrolld = &tree_scroll_down;
9100 selected = &s->selected;
9110 if (selected && *selected > view->nlines - header) {
9111 offset = abs(view->nlines - *selected - header);
9112 view->offset = offset;
9113 if (scrolld && offset) {
9114 err = scrolld(view, offset);
9115 *selected -= offset;
9123 list_commands(FILE *fp)
9127 fprintf(fp, "commands:");
9128 for (i = 0; i < nitems(tog_commands); i++) {
9129 const struct tog_cmd *cmd = &tog_commands[i];
9130 fprintf(fp, " %s", cmd->name);
9136 usage(int hflag, int status)
9138 FILE *fp = (status == 0) ? stdout : stderr;
9140 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
9143 fprintf(fp, "lazy usage: %s path\n", getprogname());
9150 make_argv(int argc, ...)
9158 argv = calloc(argc, sizeof(char *));
9161 for (i = 0; i < argc; i++) {
9162 argv[i] = strdup(va_arg(ap, char *));
9163 if (argv[i] == NULL)
9172 * Try to convert 'tog path' into a 'tog log path' command.
9173 * The user could simply have mistyped the command rather than knowingly
9174 * provided a path. So check whether argv[0] can in fact be resolved
9175 * to a path in the HEAD commit and print a special error if not.
9176 * This hack is for mpi@ <3
9178 static const struct got_error *
9179 tog_log_with_path(int argc, char *argv[])
9181 const struct got_error *error = NULL, *close_err;
9182 const struct tog_cmd *cmd = NULL;
9183 struct got_repository *repo = NULL;
9184 struct got_worktree *worktree = NULL;
9185 struct got_object_id *commit_id = NULL, *id = NULL;
9186 struct got_commit_object *commit = NULL;
9187 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9188 char *commit_id_str = NULL, **cmd_argv = NULL;
9189 int *pack_fds = NULL;
9191 cwd = getcwd(NULL, 0);
9193 return got_error_from_errno("getcwd");
9195 error = got_repo_pack_fds_open(&pack_fds);
9199 error = got_worktree_open(&worktree, cwd);
9200 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9204 repo_path = strdup(got_worktree_get_repo_path(worktree));
9206 repo_path = strdup(cwd);
9207 if (repo_path == NULL) {
9208 error = got_error_from_errno("strdup");
9212 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9216 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9221 error = tog_load_refs(repo, 0);
9224 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9225 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9226 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9231 got_worktree_close(worktree);
9235 error = got_object_open_as_commit(&commit, repo, commit_id);
9239 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9241 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9243 fprintf(stderr, "%s: '%s' is no known command or path\n",
9244 getprogname(), argv[0]);
9249 error = got_object_id_str(&commit_id_str, commit_id);
9253 cmd = &tog_commands[0]; /* log */
9255 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9256 error = cmd->cmd_main(argc, cmd_argv);
9259 close_err = got_repo_close(repo);
9264 got_object_commit_close(commit);
9266 got_worktree_close(worktree);
9268 const struct got_error *pack_err =
9269 got_repo_pack_fds_close(pack_fds);
9274 free(commit_id_str);
9281 for (i = 0; i < argc; i++)
9290 main(int argc, char *argv[])
9292 const struct got_error *error = NULL;
9293 const struct tog_cmd *cmd = NULL;
9294 int ch, hflag = 0, Vflag = 0;
9295 char **cmd_argv = NULL;
9296 static const struct option longopts[] = {
9297 { "version", no_argument, NULL, 'V' },
9300 char *diff_algo_str = NULL;
9302 if (!isatty(STDIN_FILENO))
9303 errx(1, "standard input is not a tty");
9305 setlocale(LC_CTYPE, "");
9307 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9327 got_version_print_str();
9332 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9340 /* Build an argument vector which runs a default command. */
9341 cmd = &tog_commands[0];
9343 cmd_argv = make_argv(argc, cmd->name);
9347 /* Did the user specify a command? */
9348 for (i = 0; i < nitems(tog_commands); i++) {
9349 if (strncmp(tog_commands[i].name, argv[0],
9350 strlen(argv[0])) == 0) {
9351 cmd = &tog_commands[i];
9357 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9358 if (diff_algo_str) {
9359 if (strcasecmp(diff_algo_str, "patience") == 0)
9360 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9361 if (strcasecmp(diff_algo_str, "myers") == 0)
9362 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9368 /* No command specified; try log with a path */
9369 error = tog_log_with_path(argc, argv);
9374 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9381 for (i = 0; i < argc; i++)
9386 if (error && error->code != GOT_ERR_CANCELLED)
9387 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);