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 */
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_opentemp.h"
53 #include "got_cancel.h"
54 #include "got_commit_graph.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
58 #include "got_worktree.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define CTRL(x) ((x) & 0x1f)
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
80 __dead static void usage(int, int);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_ref(void);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct tog_cmd tog_commands[] = {
94 { "log", cmd_log, usage_log },
95 { "diff", cmd_diff, usage_diff },
96 { "blame", cmd_blame, usage_blame },
97 { "tree", cmd_tree, usage_tree },
98 { "ref", cmd_ref, usage_ref },
110 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 enum tog_keymap_type {
112 TOG_KEYMAP_KEYS = -2,
128 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 #define TOG_EOF_STRING "(END)"
132 struct commit_queue_entry {
133 TAILQ_ENTRY(commit_queue_entry) entry;
134 struct got_object_id *id;
135 struct got_commit_object *commit;
138 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 struct commit_queue {
141 struct commit_queue_head head;
145 STAILQ_ENTRY(tog_color) entry;
149 STAILQ_HEAD(tog_colors, tog_color);
151 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 static struct got_reflist_object_id_map *tog_refs_idmap;
153 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 static const struct got_error *
156 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 struct got_reference* re2)
159 const char *name1 = got_ref_get_name(re1);
160 const char *name2 = got_ref_get_name(re2);
161 int isbackup1, isbackup2;
163 /* Sort backup refs towards the bottom of the list. */
164 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 if (!isbackup1 && isbackup2) {
169 } else if (isbackup1 && !isbackup2) {
174 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
178 static const struct got_error *
179 tog_load_refs(struct got_repository *repo, int sort_by_date)
181 const struct got_error *err;
183 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
196 if (tog_refs_idmap) {
197 got_reflist_object_id_map_free(tog_refs_idmap);
198 tog_refs_idmap = NULL;
200 got_ref_list_free(&tog_refs);
203 static const struct got_error *
204 add_color(struct tog_colors *colors, const char *pattern,
205 int idx, short color)
207 const struct got_error *err = NULL;
208 struct tog_color *tc;
211 if (idx < 1 || idx > COLOR_PAIRS - 1)
214 init_pair(idx, color, -1);
216 tc = calloc(1, sizeof(*tc));
218 return got_error_from_errno("calloc");
219 regerr = regcomp(&tc->regex, pattern,
220 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
222 static char regerr_msg[512];
223 static char err_msg[512];
224 regerror(regerr, &tc->regex, regerr_msg,
226 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
228 err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 STAILQ_INSERT_HEAD(colors, tc, entry);
238 free_colors(struct tog_colors *colors)
240 struct tog_color *tc;
242 while (!STAILQ_EMPTY(colors)) {
243 tc = STAILQ_FIRST(colors);
244 STAILQ_REMOVE_HEAD(colors, entry);
250 static struct tog_color *
251 get_color(struct tog_colors *colors, int colorpair)
253 struct tog_color *tc = NULL;
255 STAILQ_FOREACH(tc, colors, entry) {
256 if (tc->colorpair == colorpair)
264 default_color_value(const char *envvar)
266 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
274 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
282 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
284 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
286 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
290 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 return COLOR_MAGENTA;
292 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
301 get_color_value(const char *envvar)
303 const char *val = getenv(envvar);
306 return default_color_value(envvar);
308 if (strcasecmp(val, "black") == 0)
310 if (strcasecmp(val, "red") == 0)
312 if (strcasecmp(val, "green") == 0)
314 if (strcasecmp(val, "yellow") == 0)
316 if (strcasecmp(val, "blue") == 0)
318 if (strcasecmp(val, "magenta") == 0)
319 return COLOR_MAGENTA;
320 if (strcasecmp(val, "cyan") == 0)
322 if (strcasecmp(val, "white") == 0)
324 if (strcasecmp(val, "default") == 0)
327 return default_color_value(envvar);
330 struct tog_diff_view_state {
331 struct got_object_id *id1, *id2;
332 const char *label1, *label2;
336 int first_displayed_line;
337 int last_displayed_line;
340 int ignore_whitespace;
342 struct got_repository *repo;
343 struct got_diff_line *lines;
348 /* passed from log or blame view; may be NULL */
349 struct tog_view *parent_view;
352 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 static volatile sig_atomic_t tog_thread_error;
355 struct tog_log_thread_args {
356 pthread_cond_t need_commits;
357 pthread_cond_t commit_loaded;
360 struct got_commit_graph *graph;
361 struct commit_queue *real_commits;
362 const char *in_repo_path;
363 struct got_object_id *start_id;
364 struct got_repository *repo;
368 struct commit_queue_entry **first_displayed_entry;
369 struct commit_queue_entry **selected_entry;
371 int *search_next_done;
375 regex_t *limit_regex;
376 struct commit_queue *limit_commits;
379 struct tog_log_view_state {
380 struct commit_queue *commits;
381 struct commit_queue_entry *first_displayed_entry;
382 struct commit_queue_entry *last_displayed_entry;
383 struct commit_queue_entry *selected_entry;
384 struct commit_queue real_commits;
389 struct got_repository *repo;
390 struct got_object_id *start_id;
393 struct tog_log_thread_args thread_args;
394 struct commit_queue_entry *matched_entry;
395 struct commit_queue_entry *search_entry;
396 struct tog_colors colors;
400 struct commit_queue limit_commits;
403 #define TOG_COLOR_DIFF_MINUS 1
404 #define TOG_COLOR_DIFF_PLUS 2
405 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 #define TOG_COLOR_DIFF_META 4
407 #define TOG_COLOR_TREE_SUBMODULE 5
408 #define TOG_COLOR_TREE_SYMLINK 6
409 #define TOG_COLOR_TREE_DIRECTORY 7
410 #define TOG_COLOR_TREE_EXECUTABLE 8
411 #define TOG_COLOR_COMMIT 9
412 #define TOG_COLOR_AUTHOR 10
413 #define TOG_COLOR_DATE 11
414 #define TOG_COLOR_REFS_HEADS 12
415 #define TOG_COLOR_REFS_TAGS 13
416 #define TOG_COLOR_REFS_REMOTES 14
417 #define TOG_COLOR_REFS_BACKUP 15
419 struct tog_blame_cb_args {
420 struct tog_blame_line *lines; /* one per line */
423 struct tog_view *view;
424 struct got_object_id *commit_id;
428 struct tog_blame_thread_args {
430 struct got_repository *repo;
431 struct tog_blame_cb_args *cb_args;
433 got_cancel_cb cancel_cb;
440 struct tog_blame_line *lines;
444 struct tog_blame_thread_args thread_args;
445 struct tog_blame_cb_args cb_args;
450 struct tog_blame_view_state {
451 int first_displayed_line;
452 int last_displayed_line;
454 int last_diffed_line;
458 struct got_object_id_queue blamed_commits;
459 struct got_object_qid *blamed_commit;
461 struct got_repository *repo;
462 struct got_object_id *commit_id;
463 struct got_object_id *id_to_log;
464 struct tog_blame blame;
466 struct tog_colors colors;
469 struct tog_parent_tree {
470 TAILQ_ENTRY(tog_parent_tree) entry;
471 struct got_tree_object *tree;
472 struct got_tree_entry *first_displayed_entry;
473 struct got_tree_entry *selected_entry;
477 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 struct tog_tree_view_state {
481 struct got_object_id *commit_id;/* commit which this tree belongs to */
482 struct got_tree_object *root; /* the commit's root tree entry */
483 struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 struct got_tree_entry *first_displayed_entry;
485 struct got_tree_entry *last_displayed_entry;
486 struct got_tree_entry *selected_entry;
487 int ndisplayed, selected, show_ids;
488 struct tog_parent_trees parents; /* parent trees of current sub-tree */
490 struct got_repository *repo;
491 struct got_tree_entry *matched_entry;
492 struct tog_colors colors;
495 struct tog_reflist_entry {
496 TAILQ_ENTRY(tog_reflist_entry) entry;
497 struct got_reference *ref;
501 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 struct tog_ref_view_state {
504 struct tog_reflist_head refs;
505 struct tog_reflist_entry *first_displayed_entry;
506 struct tog_reflist_entry *last_displayed_entry;
507 struct tog_reflist_entry *selected_entry;
508 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 struct got_repository *repo;
510 struct tog_reflist_entry *matched_entry;
511 struct tog_colors colors;
514 struct tog_help_view_state {
519 int first_displayed_line;
520 int last_displayed_line;
525 enum tog_keymap_type type;
528 #define GENERATE_HELP \
529 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 KEY_("k C-p Up", "Move cursor or page up one line"), \
532 KEY_("j C-n Down", "Move cursor or page down one line"), \
533 KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 KEY_("C-u u", "Scroll the view up one half page"), \
536 KEY_("C-d d", "Scroll the view down one half page"), \
537 KEY_("g", "Go to line N (default: first line)"), \
538 KEY_("Home =", "Go to the first line"), \
539 KEY_("G", "Go to line N (default: last line)"), \
540 KEY_("End *", "Go to the last line"), \
541 KEY_("l Right", "Scroll the view right"), \
542 KEY_("h Left", "Scroll the view left"), \
543 KEY_("$", "Scroll view to the rightmost position"), \
544 KEY_("0", "Scroll view to the leftmost position"), \
545 KEY_("-", "Decrease size of the focussed split"), \
546 KEY_("+", "Increase size of the focussed split"), \
547 KEY_("Tab", "Switch focus between views"), \
548 KEY_("F", "Toggle fullscreen mode"), \
549 KEY_("S", "Switch split-screen layout"), \
550 KEY_("/", "Open prompt to enter search term"), \
551 KEY_("n", "Find next line/token matching the current search term"), \
552 KEY_("N", "Find previous line/token matching the current search term"),\
553 KEY_("q", "Quit the focussed view; Quit help screen"), \
554 KEY_("Q", "Quit tog"), \
556 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
557 KEY_("< ,", "Move cursor up one commit"), \
558 KEY_("> .", "Move cursor down one commit"), \
559 KEY_("Enter", "Open diff view of the selected commit"), \
560 KEY_("B", "Reload the log view and toggle display of merged commits"), \
561 KEY_("R", "Open ref view of all repository references"), \
562 KEY_("T", "Display tree view of the repository from the selected" \
564 KEY_("@", "Toggle between displaying author and committer name"), \
565 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
566 KEY_("C-g Backspace", "Cancel current search or log operation"), \
567 KEY_("C-l", "Reload the log view with new commits in the repository"), \
569 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
570 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
571 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
572 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
573 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
575 KEY_("(", "Go to the previous file in the diff"), \
576 KEY_(")", "Go to the next file in the diff"), \
577 KEY_("{", "Go to the previous hunk in the diff"), \
578 KEY_("}", "Go to the next hunk in the diff"), \
579 KEY_("[", "Decrease the number of context lines"), \
580 KEY_("]", "Increase the number of context lines"), \
581 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
583 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
584 KEY_("Enter", "Display diff view of the selected line's commit"), \
585 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
586 KEY_("L", "Open log view for the currently selected annotated line"), \
587 KEY_("C", "Reload view with the previously blamed commit"), \
588 KEY_("c", "Reload view with the version of the file found in the" \
589 " selected line's commit"), \
590 KEY_("p", "Reload view with the version of the file found in the" \
591 " selected line's parent commit"), \
593 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
594 KEY_("Enter", "Enter selected directory or open blame view of the" \
596 KEY_("L", "Open log view for the selected entry"), \
597 KEY_("R", "Open ref view of all repository references"), \
598 KEY_("i", "Show object IDs for all tree entries"), \
599 KEY_("Backspace", "Return to the parent directory"), \
601 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
602 KEY_("Enter", "Display log view of the selected reference"), \
603 KEY_("T", "Display tree view of the selected reference"), \
604 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
605 KEY_("m", "Toggle display of last modified date for each reference"), \
606 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
607 KEY_("C-l", "Reload view with all repository references")
612 enum tog_keymap_type type;
615 /* curses io for tog regress */
622 static int using_mock_io;
624 #define TOG_SCREEN_DUMP "SCREENDUMP"
625 #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
626 #define TOG_KEY_SCRDUMP SHRT_MIN
629 * We implement two types of views: parent views and child views.
631 * The 'Tab' key switches focus between a parent view and its child view.
632 * Child views are shown side-by-side to their parent view, provided
633 * there is enough screen estate.
635 * When a new view is opened from within a parent view, this new view
636 * becomes a child view of the parent view, replacing any existing child.
638 * When a new view is opened from within a child view, this new view
639 * becomes a parent view which will obscure the views below until the
640 * user quits the new parent view by typing 'q'.
642 * This list of views contains parent views only.
643 * Child views are only pointed to by their parent view.
645 TAILQ_HEAD(tog_view_list_head, tog_view);
648 TAILQ_ENTRY(tog_view) entry;
651 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
652 int resized_y, resized_x; /* begin_y/x based on user resizing */
653 int maxx, x; /* max column and current start column */
654 int lines, cols; /* copies of LINES and COLS */
655 int nscrolled, offset; /* lines scrolled and hsplit line offset */
656 int gline, hiline; /* navigate to and highlight this nG line */
657 int ch, count; /* current keymap and count prefix */
658 int resized; /* set when in a resize event */
659 int focussed; /* Only set on one parent or child view at a time. */
661 struct tog_view *parent;
662 struct tog_view *child;
665 * This flag is initially set on parent views when a new child view
666 * is created. It gets toggled when the 'Tab' key switches focus
667 * between parent and child.
668 * The flag indicates whether focus should be passed on to our child
669 * view if this parent view gets picked for focus after another parent
670 * view was closed. This prevents child views from losing focus in such
675 enum tog_view_mode mode;
676 /* type-specific state */
677 enum tog_view_type type;
679 struct tog_diff_view_state diff;
680 struct tog_log_view_state log;
681 struct tog_blame_view_state blame;
682 struct tog_tree_view_state tree;
683 struct tog_ref_view_state ref;
684 struct tog_help_view_state help;
687 const struct got_error *(*show)(struct tog_view *);
688 const struct got_error *(*input)(struct tog_view **,
689 struct tog_view *, int);
690 const struct got_error *(*reset)(struct tog_view *);
691 const struct got_error *(*resize)(struct tog_view *, int);
692 const struct got_error *(*close)(struct tog_view *);
694 const struct got_error *(*search_start)(struct tog_view *);
695 const struct got_error *(*search_next)(struct tog_view *);
696 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
697 int **, int **, int **, int **);
700 #define TOG_SEARCH_FORWARD 1
701 #define TOG_SEARCH_BACKWARD 2
702 int search_next_done;
703 #define TOG_SEARCH_HAVE_MORE 1
704 #define TOG_SEARCH_NO_MORE 2
705 #define TOG_SEARCH_HAVE_NONE 3
711 static const struct got_error *open_diff_view(struct tog_view *,
712 struct got_object_id *, struct got_object_id *,
713 const char *, const char *, int, int, int, struct tog_view *,
714 struct got_repository *);
715 static const struct got_error *show_diff_view(struct tog_view *);
716 static const struct got_error *input_diff_view(struct tog_view **,
717 struct tog_view *, int);
718 static const struct got_error *reset_diff_view(struct tog_view *);
719 static const struct got_error* close_diff_view(struct tog_view *);
720 static const struct got_error *search_start_diff_view(struct tog_view *);
721 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
722 size_t *, int **, int **, int **, int **);
723 static const struct got_error *search_next_view_match(struct tog_view *);
725 static const struct got_error *open_log_view(struct tog_view *,
726 struct got_object_id *, struct got_repository *,
727 const char *, const char *, int);
728 static const struct got_error * show_log_view(struct tog_view *);
729 static const struct got_error *input_log_view(struct tog_view **,
730 struct tog_view *, int);
731 static const struct got_error *resize_log_view(struct tog_view *, int);
732 static const struct got_error *close_log_view(struct tog_view *);
733 static const struct got_error *search_start_log_view(struct tog_view *);
734 static const struct got_error *search_next_log_view(struct tog_view *);
736 static const struct got_error *open_blame_view(struct tog_view *, char *,
737 struct got_object_id *, struct got_repository *);
738 static const struct got_error *show_blame_view(struct tog_view *);
739 static const struct got_error *input_blame_view(struct tog_view **,
740 struct tog_view *, int);
741 static const struct got_error *reset_blame_view(struct tog_view *);
742 static const struct got_error *close_blame_view(struct tog_view *);
743 static const struct got_error *search_start_blame_view(struct tog_view *);
744 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
745 size_t *, int **, int **, int **, int **);
747 static const struct got_error *open_tree_view(struct tog_view *,
748 struct got_object_id *, const char *, struct got_repository *);
749 static const struct got_error *show_tree_view(struct tog_view *);
750 static const struct got_error *input_tree_view(struct tog_view **,
751 struct tog_view *, int);
752 static const struct got_error *close_tree_view(struct tog_view *);
753 static const struct got_error *search_start_tree_view(struct tog_view *);
754 static const struct got_error *search_next_tree_view(struct tog_view *);
756 static const struct got_error *open_ref_view(struct tog_view *,
757 struct got_repository *);
758 static const struct got_error *show_ref_view(struct tog_view *);
759 static const struct got_error *input_ref_view(struct tog_view **,
760 struct tog_view *, int);
761 static const struct got_error *close_ref_view(struct tog_view *);
762 static const struct got_error *search_start_ref_view(struct tog_view *);
763 static const struct got_error *search_next_ref_view(struct tog_view *);
765 static const struct got_error *open_help_view(struct tog_view *,
767 static const struct got_error *show_help_view(struct tog_view *);
768 static const struct got_error *input_help_view(struct tog_view **,
769 struct tog_view *, int);
770 static const struct got_error *reset_help_view(struct tog_view *);
771 static const struct got_error* close_help_view(struct tog_view *);
772 static const struct got_error *search_start_help_view(struct tog_view *);
773 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
774 size_t *, int **, int **, int **, int **);
776 static volatile sig_atomic_t tog_sigwinch_received;
777 static volatile sig_atomic_t tog_sigpipe_received;
778 static volatile sig_atomic_t tog_sigcont_received;
779 static volatile sig_atomic_t tog_sigint_received;
780 static volatile sig_atomic_t tog_sigterm_received;
783 tog_sigwinch(int signo)
785 tog_sigwinch_received = 1;
789 tog_sigpipe(int signo)
791 tog_sigpipe_received = 1;
795 tog_sigcont(int signo)
797 tog_sigcont_received = 1;
801 tog_sigint(int signo)
803 tog_sigint_received = 1;
807 tog_sigterm(int signo)
809 tog_sigterm_received = 1;
813 tog_fatal_signal_received(void)
815 return (tog_sigpipe_received ||
816 tog_sigint_received || tog_sigterm_received);
819 static const struct got_error *
820 view_close(struct tog_view *view)
822 const struct got_error *err = NULL, *child_err = NULL;
825 child_err = view_close(view->child);
829 err = view->close(view);
831 del_panel(view->panel);
833 delwin(view->window);
835 return err ? err : child_err;
838 static struct tog_view *
839 view_open(int nlines, int ncols, int begin_y, int begin_x,
840 enum tog_view_type type)
842 struct tog_view *view = calloc(1, sizeof(*view));
850 view->nlines = nlines ? nlines : LINES - begin_y;
851 view->ncols = ncols ? ncols : COLS - begin_x;
852 view->begin_y = begin_y;
853 view->begin_x = begin_x;
854 view->window = newwin(nlines, ncols, begin_y, begin_x);
855 if (view->window == NULL) {
859 view->panel = new_panel(view->window);
860 if (view->panel == NULL ||
861 set_panel_userptr(view->panel, view) != OK) {
866 keypad(view->window, TRUE);
871 view_split_begin_x(int begin_x)
873 if (begin_x > 0 || COLS < 120)
875 return (COLS - MAX(COLS / 2, 80));
878 /* XXX Stub till we decide what to do. */
880 view_split_begin_y(int lines)
882 return lines * HSPLIT_SCALE;
885 static const struct got_error *view_resize(struct tog_view *);
887 static const struct got_error *
888 view_splitscreen(struct tog_view *view)
890 const struct got_error *err = NULL;
892 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
893 if (view->resized_y && view->resized_y < view->lines)
894 view->begin_y = view->resized_y;
896 view->begin_y = view_split_begin_y(view->nlines);
898 } else if (!view->resized) {
899 if (view->resized_x && view->resized_x < view->cols - 1 &&
901 view->begin_x = view->resized_x;
903 view->begin_x = view_split_begin_x(0);
906 view->nlines = LINES - view->begin_y;
907 view->ncols = COLS - view->begin_x;
910 err = view_resize(view);
914 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
915 view->parent->nlines = view->begin_y;
917 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
918 return got_error_from_errno("mvwin");
923 static const struct got_error *
924 view_fullscreen(struct tog_view *view)
926 const struct got_error *err = NULL;
929 view->begin_y = view->resized ? view->begin_y : 0;
930 view->nlines = view->resized ? view->nlines : LINES;
934 err = view_resize(view);
938 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
939 return got_error_from_errno("mvwin");
945 view_is_parent_view(struct tog_view *view)
947 return view->parent == NULL;
951 view_is_splitscreen(struct tog_view *view)
953 return view->begin_x > 0 || view->begin_y > 0;
957 view_is_fullscreen(struct tog_view *view)
959 return view->nlines == LINES && view->ncols == COLS;
963 view_is_hsplit_top(struct tog_view *view)
965 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
966 view_is_splitscreen(view->child);
970 view_border(struct tog_view *view)
973 const struct tog_view *view_above;
976 return view_border(view->parent);
978 panel = panel_above(view->panel);
982 view_above = panel_userptr(panel);
983 if (view->mode == TOG_VIEW_SPLIT_HRZN)
984 mvwhline(view->window, view_above->begin_y - 1,
985 view->begin_x, got_locale_is_utf8() ?
986 ACS_HLINE : '-', view->ncols);
988 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
989 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
992 static const struct got_error *view_init_hsplit(struct tog_view *, int);
993 static const struct got_error *request_log_commits(struct tog_view *);
994 static const struct got_error *offset_selection_down(struct tog_view *);
995 static void offset_selection_up(struct tog_view *);
996 static void view_get_split(struct tog_view *, int *, int *);
998 static const struct got_error *
999 view_resize(struct tog_view *view)
1001 const struct got_error *err = NULL;
1002 int dif, nlines, ncols;
1004 dif = LINES - view->lines; /* line difference */
1006 if (view->lines > LINES)
1007 nlines = view->nlines - (view->lines - LINES);
1009 nlines = view->nlines + (LINES - view->lines);
1010 if (view->cols > COLS)
1011 ncols = view->ncols - (view->cols - COLS);
1013 ncols = view->ncols + (COLS - view->cols);
1016 int hs = view->child->begin_y;
1018 if (!view_is_fullscreen(view))
1019 view->child->begin_x = view_split_begin_x(view->begin_x);
1020 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1021 view->child->begin_x == 0) {
1024 view_fullscreen(view->child);
1025 if (view->child->focussed)
1026 show_panel(view->child->panel);
1028 show_panel(view->panel);
1030 ncols = view->child->begin_x;
1032 view_splitscreen(view->child);
1033 show_panel(view->child->panel);
1036 * XXX This is ugly and needs to be moved into the above
1037 * logic but "works" for now and my attempts at moving it
1038 * break either 'tab' or 'F' key maps in horizontal splits.
1041 err = view_splitscreen(view->child);
1044 if (dif < 0) { /* top split decreased */
1045 err = offset_selection_down(view);
1052 show_panel(view->child->panel);
1053 nlines = view->nlines;
1055 } else if (view->parent == NULL)
1058 if (view->resize && dif > 0) {
1059 err = view->resize(view, dif);
1064 if (wresize(view->window, nlines, ncols) == ERR)
1065 return got_error_from_errno("wresize");
1066 if (replace_panel(view->panel, view->window) == ERR)
1067 return got_error_from_errno("replace_panel");
1068 wclear(view->window);
1070 view->nlines = nlines;
1071 view->ncols = ncols;
1072 view->lines = LINES;
1078 static const struct got_error *
1079 resize_log_view(struct tog_view *view, int increase)
1081 struct tog_log_view_state *s = &view->state.log;
1082 const struct got_error *err = NULL;
1085 if (s->selected_entry)
1086 n = s->selected_entry->idx + view->lines - s->selected;
1089 * Request commits to account for the increased
1090 * height so we have enough to populate the view.
1092 if (s->commits->ncommits < n) {
1093 view->nscrolled = n - s->commits->ncommits + increase + 1;
1094 err = request_log_commits(view);
1101 view_adjust_offset(struct tog_view *view, int n)
1106 if (view->parent && view->parent->offset) {
1107 if (view->parent->offset + n >= 0)
1108 view->parent->offset += n;
1110 view->parent->offset = 0;
1111 } else if (view->offset) {
1112 if (view->offset - n >= 0)
1119 static const struct got_error *
1120 view_resize_split(struct tog_view *view, int resize)
1122 const struct got_error *err = NULL;
1123 struct tog_view *v = NULL;
1130 if (!v->child || !view_is_splitscreen(v->child))
1133 v->resized = v->child->resized = resize; /* lock for resize event */
1135 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1136 if (v->child->resized_y)
1137 v->child->begin_y = v->child->resized_y;
1139 v->child->begin_y -= resize;
1141 v->child->begin_y += resize;
1142 if (v->child->begin_y < 3) {
1144 v->child->begin_y = 3;
1145 } else if (v->child->begin_y > LINES - 1) {
1147 v->child->begin_y = LINES - 1;
1150 v->child->ncols = COLS;
1151 view_adjust_offset(view, resize);
1152 err = view_init_hsplit(v, v->child->begin_y);
1155 v->child->resized_y = v->child->begin_y;
1157 if (v->child->resized_x)
1158 v->child->begin_x = v->child->resized_x;
1160 v->child->begin_x -= resize;
1162 v->child->begin_x += resize;
1163 if (v->child->begin_x < 11) {
1165 v->child->begin_x = 11;
1166 } else if (v->child->begin_x > COLS - 1) {
1168 v->child->begin_x = COLS - 1;
1170 v->child->resized_x = v->child->begin_x;
1173 v->child->mode = v->mode;
1174 v->child->nlines = v->lines - v->child->begin_y;
1175 v->child->ncols = v->cols - v->child->begin_x;
1178 err = view_fullscreen(v);
1181 err = view_splitscreen(v->child);
1185 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1186 err = offset_selection_down(v->child);
1192 err = v->resize(v, 0);
1193 else if (v->child->resize)
1194 err = v->child->resize(v->child, 0);
1196 v->resized = v->child->resized = 0;
1202 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1204 struct tog_view *v = src->child ? src->child : src;
1206 dst->resized_x = v->resized_x;
1207 dst->resized_y = v->resized_y;
1210 static const struct got_error *
1211 view_close_child(struct tog_view *view)
1213 const struct got_error *err = NULL;
1215 if (view->child == NULL)
1218 err = view_close(view->child);
1223 static const struct got_error *
1224 view_set_child(struct tog_view *view, struct tog_view *child)
1226 const struct got_error *err = NULL;
1228 view->child = child;
1229 child->parent = view;
1231 err = view_resize(view);
1235 if (view->child->resized_x || view->child->resized_y)
1236 err = view_resize_split(view, 0);
1241 static const struct got_error *view_dispatch_request(struct tog_view **,
1242 struct tog_view *, enum tog_view_type, int, int);
1244 static const struct got_error *
1245 view_request_new(struct tog_view **requested, struct tog_view *view,
1246 enum tog_view_type request)
1248 struct tog_view *new_view = NULL;
1249 const struct got_error *err;
1254 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1255 view_get_split(view, &y, &x);
1257 err = view_dispatch_request(&new_view, view, request, y, x);
1261 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1262 request != TOG_VIEW_HELP) {
1263 err = view_init_hsplit(view, y);
1269 new_view->focussed = 1;
1270 new_view->mode = view->mode;
1271 new_view->nlines = request == TOG_VIEW_HELP ?
1272 view->lines : view->lines - y;
1274 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1275 view_transfer_size(new_view, view);
1276 err = view_close_child(view);
1279 err = view_set_child(view, new_view);
1282 view->focus_child = 1;
1284 *requested = new_view;
1290 tog_resizeterm(void)
1293 struct winsize size;
1295 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1296 cols = 80; /* Default */
1300 lines = size.ws_row;
1302 resize_term(lines, cols);
1305 static const struct got_error *
1306 view_search_start(struct tog_view *view, int fast_refresh)
1308 const struct got_error *err = NULL;
1309 struct tog_view *v = view;
1313 if (view->search_started) {
1314 regfree(&view->regex);
1315 view->searching = 0;
1316 memset(&view->regmatch, 0, sizeof(view->regmatch));
1318 view->search_started = 0;
1320 if (view->nlines < 1)
1323 if (view_is_hsplit_top(view))
1325 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1328 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1329 wclrtoeol(v->window);
1331 nodelay(v->window, FALSE); /* block for search term input */
1334 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1335 wrefresh(v->window);
1338 nodelay(v->window, TRUE);
1339 if (!fast_refresh && !using_mock_io)
1344 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1345 err = view->search_start(view);
1347 regfree(&view->regex);
1350 view->search_started = 1;
1351 view->searching = TOG_SEARCH_FORWARD;
1352 view->search_next_done = 0;
1353 view->search_next(view);
1359 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1360 static const struct got_error *
1361 switch_split(struct tog_view *view)
1363 const struct got_error *err = NULL;
1364 struct tog_view *v = NULL;
1371 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1372 v->mode = TOG_VIEW_SPLIT_VERT;
1374 v->mode = TOG_VIEW_SPLIT_HRZN;
1378 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1379 v->mode = TOG_VIEW_SPLIT_NONE;
1381 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1382 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1383 v->child->begin_y = v->child->resized_y;
1384 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1385 v->child->begin_x = v->child->resized_x;
1388 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1390 v->child->ncols = COLS;
1391 v->child->nscrolled = LINES - v->child->nlines;
1393 err = view_init_hsplit(v, v->child->begin_y);
1397 v->child->mode = v->mode;
1398 v->child->nlines = v->lines - v->child->begin_y;
1401 err = view_fullscreen(v);
1404 err = view_splitscreen(v->child);
1408 if (v->mode == TOG_VIEW_SPLIT_NONE)
1409 v->mode = TOG_VIEW_SPLIT_VERT;
1410 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1411 err = offset_selection_down(v);
1414 err = offset_selection_down(v->child);
1418 offset_selection_up(v);
1419 offset_selection_up(v->child);
1422 err = v->resize(v, 0);
1423 else if (v->child->resize)
1424 err = v->child->resize(v->child, 0);
1430 * Strip trailing whitespace from str starting at byte *n;
1431 * if *n < 0, use strlen(str). Return new str length in *n.
1434 strip_trailing_ws(char *str, int *n)
1438 if (str == NULL || *str == '\0')
1444 while (x-- > 0 && isspace((unsigned char)str[x]))
1451 * Extract visible substring of line y from the curses screen
1452 * and strip trailing whitespace. If vline is set and locale is
1453 * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1454 * character is written out as 'x'. Write the line to file f.
1456 static const struct got_error *
1457 view_write_line(FILE *f, int y, int vline)
1459 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1462 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1464 return got_error_fmt(GOT_ERR_RANGE,
1465 "failed to extract line %d", y);
1468 * In some views, lines are padded with blanks to COLS width.
1469 * Strip them so we can diff without the -b flag when testing.
1471 strip_trailing_ws(line, &r);
1473 if (vline > 0 && got_locale_is_utf8())
1476 w = fprintf(f, "%s\n", line);
1477 if (w != r + 1) /* \n */
1478 return got_ferror(f, GOT_ERR_IO);
1484 * Capture the visible curses screen by writing each line to the
1485 * file at the path set via the TOG_SCR_DUMP environment variable.
1487 static const struct got_error *
1488 screendump(struct tog_view *view)
1490 const struct got_error *err;
1495 path = getenv("TOG_SCR_DUMP");
1496 if (path == NULL || *path == '\0')
1497 return got_error_msg(GOT_ERR_BAD_PATH,
1498 "TOG_SCR_DUMP path not set to capture screen dump");
1499 f = fopen(path, "wex");
1501 return got_error_from_errno_fmt("fopen: %s", path);
1503 if ((view->child && view->child->begin_x) ||
1504 (view->parent && view->begin_x)) {
1505 int ncols = view->child ? view->ncols : view->parent->ncols;
1507 /* vertical splitscreen */
1508 for (i = 0; i < view->nlines; ++i) {
1509 err = view_write_line(f, i, ncols - 1);
1516 /* fullscreen or horizontal splitscreen */
1517 if ((view->child && view->child->begin_y) ||
1518 (view->parent && view->begin_y)) /* hsplit */
1519 hline = view->child ?
1520 view->child->begin_y : view->begin_y;
1522 for (i = 0; i < view->lines; i++) {
1523 if (hline && got_locale_is_utf8() && i == hline - 1) {
1526 /* ACS_HLINE writes out as 'q', overwrite it */
1527 for (c = 0; c < view->cols; ++c)
1533 err = view_write_line(f, i, 0);
1540 if (f && fclose(f) == EOF && err == NULL)
1541 err = got_ferror(f, GOT_ERR_IO);
1546 * Compute view->count from numeric input. Assign total to view->count and
1547 * return first non-numeric key entered.
1550 get_compound_key(struct tog_view *view, int c)
1552 struct tog_view *v = view;
1555 if (view_is_hsplit_top(view))
1557 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1561 cbreak(); /* block for input */
1562 nodelay(view->window, FALSE);
1563 wmove(v->window, v->nlines - 1, 0);
1564 wclrtoeol(v->window);
1565 waddch(v->window, ':');
1568 x = getcurx(v->window);
1569 if (x != ERR && x < view->ncols) {
1570 waddch(v->window, c);
1571 wrefresh(v->window);
1575 * Don't overflow. Max valid request should be the greatest
1576 * between the longest and total lines; cap at 10 million.
1581 n = n * 10 + (c - '0');
1582 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1584 if (c == 'G' || c == 'g') { /* nG key map */
1585 view->gline = view->hiline = n;
1590 /* Massage excessive or inapplicable values at the input handler. */
1597 action_report(struct tog_view *view)
1599 struct tog_view *v = view;
1601 if (view_is_hsplit_top(view))
1603 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1606 wmove(v->window, v->nlines - 1, 0);
1607 wclrtoeol(v->window);
1608 wprintw(v->window, ":%s", view->action);
1609 wrefresh(v->window);
1612 * Clear action status report. Only clear in blame view
1613 * once annotating is complete, otherwise it's too fast.
1615 if (view->type == TOG_VIEW_BLAME) {
1616 if (view->state.blame.blame_complete)
1617 view->action = NULL;
1619 view->action = NULL;
1623 * Read the next line from the test script and assign
1624 * key instruction to *ch. If at EOF, set the *done flag.
1626 static const struct got_error *
1627 tog_read_script_key(FILE *script, int *ch, int *done)
1629 const struct got_error *err = NULL;
1635 if (tog_io.wait_for_ui)
1638 if (getline(&line, &linesz, script) == -1) {
1643 err = got_ferror(script, GOT_ERR_IO);
1647 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1648 tog_io.wait_for_ui = 1;
1649 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1651 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1653 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1655 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1657 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1659 else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1660 *ch = TOG_KEY_SCRDUMP;
1669 static const struct got_error *
1670 view_input(struct tog_view **new, int *done, struct tog_view *view,
1671 struct tog_view_list_head *views, int fast_refresh)
1673 const struct got_error *err = NULL;
1680 action_report(view);
1682 /* Clear "no matches" indicator. */
1683 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1684 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1685 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1689 if (view->searching && !view->search_next_done) {
1690 errcode = pthread_mutex_unlock(&tog_mutex);
1692 return got_error_set_errno(errcode,
1693 "pthread_mutex_unlock");
1695 errcode = pthread_mutex_lock(&tog_mutex);
1697 return got_error_set_errno(errcode,
1698 "pthread_mutex_lock");
1699 view->search_next(view);
1703 /* Allow threads to make progress while we are waiting for input. */
1704 errcode = pthread_mutex_unlock(&tog_mutex);
1706 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1708 if (using_mock_io) {
1709 err = tog_read_script_key(tog_io.f, &ch, done);
1712 } else if (view->count && --view->count) {
1714 nodelay(view->window, TRUE);
1715 ch = wgetch(view->window);
1716 /* let C-g or backspace abort unfinished count */
1717 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1722 ch = wgetch(view->window);
1723 if (ch >= '1' && ch <= '9')
1724 view->ch = ch = get_compound_key(view, ch);
1726 if (view->hiline && ch != ERR && ch != 0)
1727 view->hiline = 0; /* key pressed, clear line highlight */
1728 nodelay(view->window, TRUE);
1729 errcode = pthread_mutex_lock(&tog_mutex);
1731 return got_error_set_errno(errcode, "pthread_mutex_lock");
1733 if (tog_sigwinch_received || tog_sigcont_received) {
1735 tog_sigwinch_received = 0;
1736 tog_sigcont_received = 0;
1737 TAILQ_FOREACH(v, views, entry) {
1738 err = view_resize(v);
1741 err = v->input(new, v, KEY_RESIZE);
1745 err = view_resize(v->child);
1748 err = v->child->input(new, v->child,
1752 if (v->child->resized_x || v->child->resized_y) {
1753 err = view_resize_split(v, 0);
1765 if (view->type == TOG_VIEW_HELP)
1766 err = view->reset(view);
1768 err = view_request_new(new, view, TOG_VIEW_HELP);
1774 view->child->focussed = 1;
1775 view->focus_child = 1;
1776 } else if (view->parent) {
1778 view->parent->focussed = 1;
1779 view->parent->focus_child = 0;
1780 if (!view_is_splitscreen(view)) {
1781 if (view->parent->resize) {
1782 err = view->parent->resize(view->parent,
1787 offset_selection_up(view->parent);
1788 err = view_fullscreen(view->parent);
1795 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1796 if (view->parent->resize) {
1797 /* might need more commits to fill fullscreen */
1798 err = view->parent->resize(view->parent, 0);
1802 offset_selection_up(view->parent);
1804 err = view->input(new, view, ch);
1812 if (view_is_parent_view(view)) {
1813 if (view->child == NULL)
1815 if (view_is_splitscreen(view->child)) {
1817 view->child->focussed = 1;
1818 err = view_fullscreen(view->child);
1820 err = view_splitscreen(view->child);
1822 err = view_resize_split(view, 0);
1826 err = view->child->input(new, view->child,
1829 if (view_is_splitscreen(view)) {
1830 view->parent->focussed = 0;
1832 err = view_fullscreen(view);
1834 err = view_splitscreen(view);
1835 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1836 err = view_resize(view->parent);
1838 err = view_resize_split(view, 0);
1842 err = view->input(new, view, KEY_RESIZE);
1847 err = view->resize(view, 0);
1852 err = offset_selection_down(view->parent);
1854 err = offset_selection_down(view);
1858 err = switch_split(view);
1861 err = view_resize_split(view, -1);
1864 err = view_resize_split(view, 1);
1870 if (view->search_start)
1871 view_search_start(view, fast_refresh);
1873 err = view->input(new, view, ch);
1877 if (view->search_started && view->search_next) {
1878 view->searching = (ch == 'n' ?
1879 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1880 view->search_next_done = 0;
1881 view->search_next(view);
1883 err = view->input(new, view, ch);
1886 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1887 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1888 view->action = "Patience diff algorithm";
1890 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1891 view->action = "Myers diff algorithm";
1893 TAILQ_FOREACH(v, views, entry) {
1899 if (v->child && v->child->reset) {
1900 err = v->child->reset(v->child);
1906 case TOG_KEY_SCRDUMP:
1907 err = screendump(view);
1910 err = view->input(new, view, ch);
1918 view_needs_focus_indication(struct tog_view *view)
1920 if (view_is_parent_view(view)) {
1921 if (view->child == NULL || view->child->focussed)
1923 if (!view_is_splitscreen(view->child))
1925 } else if (!view_is_splitscreen(view))
1928 return view->focussed;
1931 static const struct got_error *
1934 const struct got_error *err = NULL;
1936 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1937 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1938 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1939 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1940 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1941 err = got_ferror(tog_io.f, GOT_ERR_IO);
1946 static const struct got_error *
1947 view_loop(struct tog_view *view)
1949 const struct got_error *err = NULL;
1950 struct tog_view_list_head views;
1951 struct tog_view *new_view;
1953 int fast_refresh = 10;
1954 int done = 0, errcode;
1956 mode = getenv("TOG_VIEW_SPLIT_MODE");
1957 if (!mode || !(*mode == 'h' || *mode == 'H'))
1958 view->mode = TOG_VIEW_SPLIT_VERT;
1960 view->mode = TOG_VIEW_SPLIT_HRZN;
1962 errcode = pthread_mutex_lock(&tog_mutex);
1964 return got_error_set_errno(errcode, "pthread_mutex_lock");
1967 TAILQ_INSERT_HEAD(&views, view, entry);
1970 err = view->show(view);
1975 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1976 !tog_fatal_signal_received()) {
1977 /* Refresh fast during initialization, then become slower. */
1978 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1979 halfdelay(10); /* switch to once per second */
1981 err = view_input(&new_view, &done, view, &views, fast_refresh);
1985 if (view->dying && view == TAILQ_FIRST(&views) &&
1986 TAILQ_NEXT(view, entry) == NULL)
1992 * When we quit, scroll the screen up a single line
1993 * so we don't lose any information.
1995 TAILQ_FOREACH(v, &views, entry) {
1996 wmove(v->window, 0, 0);
1997 wdeleteln(v->window);
1998 wnoutrefresh(v->window);
1999 if (v->child && !view_is_fullscreen(v)) {
2000 wmove(v->child->window, 0, 0);
2001 wdeleteln(v->child->window);
2002 wnoutrefresh(v->child->window);
2009 struct tog_view *v, *prev = NULL;
2011 if (view_is_parent_view(view))
2012 prev = TAILQ_PREV(view, tog_view_list_head,
2014 else if (view->parent)
2015 prev = view->parent;
2018 view->parent->child = NULL;
2019 view->parent->focus_child = 0;
2020 /* Restore fullscreen line height. */
2021 view->parent->nlines = view->parent->lines;
2022 err = view_resize(view->parent);
2025 /* Make resized splits persist. */
2026 view_transfer_size(view->parent, view);
2028 TAILQ_REMOVE(&views, view, entry);
2030 err = view_close(view);
2035 TAILQ_FOREACH(v, &views, entry) {
2039 if (view == NULL && new_view == NULL) {
2040 /* No view has focus. Try to pick one. */
2043 else if (!TAILQ_EMPTY(&views)) {
2044 view = TAILQ_LAST(&views,
2045 tog_view_list_head);
2048 if (view->focus_child) {
2049 view->child->focussed = 1;
2057 struct tog_view *v, *t;
2058 /* Only allow one parent view per type. */
2059 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2060 if (v->type != new_view->type)
2062 TAILQ_REMOVE(&views, v, entry);
2063 err = view_close(v);
2068 TAILQ_INSERT_TAIL(&views, new_view, entry);
2071 if (view && !done) {
2072 if (view_is_parent_view(view)) {
2073 if (view->child && view->child->focussed)
2076 if (view->parent && view->parent->focussed)
2077 view = view->parent;
2079 show_panel(view->panel);
2080 if (view->child && view_is_splitscreen(view->child))
2081 show_panel(view->child->panel);
2082 if (view->parent && view_is_splitscreen(view)) {
2083 err = view->parent->show(view->parent);
2087 err = view->show(view);
2091 err = view->child->show(view->child);
2100 while (!TAILQ_EMPTY(&views)) {
2101 const struct got_error *close_err;
2102 view = TAILQ_FIRST(&views);
2103 TAILQ_REMOVE(&views, view, entry);
2104 close_err = view_close(view);
2105 if (close_err && err == NULL)
2109 errcode = pthread_mutex_unlock(&tog_mutex);
2110 if (errcode && err == NULL)
2111 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2121 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2126 /* Create newly allocated wide-character string equivalent to a byte string. */
2127 static const struct got_error *
2128 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2131 const struct got_error *err = NULL;
2134 *wlen = mbstowcs(NULL, s, 0);
2135 if (*wlen == (size_t)-1) {
2137 if (errno != EILSEQ)
2138 return got_error_from_errno("mbstowcs");
2140 /* byte string invalid in current encoding; try to "fix" it */
2141 err = got_mbsavis(&vis, &vislen, s);
2144 *wlen = mbstowcs(NULL, vis, 0);
2145 if (*wlen == (size_t)-1) {
2146 err = got_error_from_errno("mbstowcs"); /* give up */
2151 *ws = calloc(*wlen + 1, sizeof(**ws));
2153 err = got_error_from_errno("calloc");
2157 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2158 err = got_error_from_errno("mbstowcs");
2169 static const struct got_error *
2170 expand_tab(char **ptr, const char *src)
2173 size_t len, n, idx = 0, sz = 0;
2176 n = len = strlen(src);
2177 dst = malloc(n + 1);
2179 return got_error_from_errno("malloc");
2181 while (idx < len && src[idx]) {
2182 const char c = src[idx];
2185 size_t nb = TABSIZE - sz % TABSIZE;
2188 p = realloc(dst, n + nb);
2191 return got_error_from_errno("realloc");
2196 memset(dst + sz, ' ', nb);
2199 dst[sz++] = src[idx];
2209 * Advance at most n columns from wline starting at offset off.
2210 * Return the index to the first character after the span operation.
2211 * Return the combined column width of all spanned wide character in
2215 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2217 int width, i, cols = 0;
2224 for (i = off; wline[i] != L'\0'; ++i) {
2225 if (wline[i] == L'\t')
2226 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2228 width = wcwidth(wline[i]);
2235 if (cols + width > n)
2245 * Format a line for display, ensuring that it won't overflow a width limit.
2246 * With scrolling, the width returned refers to the scrolled version of the
2247 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2249 static const struct got_error *
2250 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2251 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2253 const struct got_error *err = NULL;
2255 wchar_t *wline = NULL;
2264 err = expand_tab(&exstr, line);
2269 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2274 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2276 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2277 wline[wlen - 1] = L'\0';
2280 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2281 wline[wlen - 1] = L'\0';
2285 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2291 *scrollxp = scrollx;
2299 static const struct got_error*
2300 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2301 struct got_object_id *id, struct got_repository *repo)
2303 static const struct got_error *err = NULL;
2304 struct got_reflist_entry *re;
2310 TAILQ_FOREACH(re, refs, entry) {
2311 struct got_tag_object *tag = NULL;
2312 struct got_object_id *ref_id;
2315 name = got_ref_get_name(re->ref);
2316 if (strcmp(name, GOT_REF_HEAD) == 0)
2318 if (strncmp(name, "refs/", 5) == 0)
2320 if (strncmp(name, "got/", 4) == 0 &&
2321 strncmp(name, "got/backup/", 11) != 0)
2323 if (strncmp(name, "heads/", 6) == 0)
2325 if (strncmp(name, "remotes/", 8) == 0) {
2327 s = strstr(name, "/" GOT_REF_HEAD);
2328 if (s != NULL && s[strlen(s)] == '\0')
2331 err = got_ref_resolve(&ref_id, repo, re->ref);
2334 if (strncmp(name, "tags/", 5) == 0) {
2335 err = got_object_open_as_tag(&tag, repo, ref_id);
2337 if (err->code != GOT_ERR_OBJ_TYPE) {
2341 /* Ref points at something other than a tag. */
2346 cmp = got_object_id_cmp(tag ?
2347 got_object_tag_get_object_id(tag) : ref_id, id);
2350 got_object_tag_close(tag);
2354 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2355 s ? ", " : "", name) == -1) {
2356 err = got_error_from_errno("asprintf");
2367 static const struct got_error *
2368 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2373 smallerthan = strchr(author, '<');
2374 if (smallerthan && smallerthan[1] != '\0')
2375 author = smallerthan + 1;
2376 author[strcspn(author, "@>")] = '\0';
2377 return format_line(wauthor, author_width, NULL, author, 0, limit,
2381 static const struct got_error *
2382 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2383 struct got_object_id *id, const size_t date_display_cols,
2384 int author_display_cols)
2386 struct tog_log_view_state *s = &view->state.log;
2387 const struct got_error *err = NULL;
2388 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2389 char *logmsg0 = NULL, *logmsg = NULL;
2390 char *author = NULL;
2391 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2392 int author_width, logmsg_width;
2393 char *newline, *line = NULL;
2394 int col, limit, scrollx;
2395 const int avail = view->ncols;
2397 time_t committer_time;
2398 struct tog_color *tc;
2400 committer_time = got_object_commit_get_committer_time(commit);
2401 if (gmtime_r(&committer_time, &tm) == NULL)
2402 return got_error_from_errno("gmtime_r");
2403 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2404 return got_error(GOT_ERR_NO_SPACE);
2406 if (avail <= date_display_cols)
2407 limit = MIN(sizeof(datebuf) - 1, avail);
2409 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2410 tc = get_color(&s->colors, TOG_COLOR_DATE);
2412 wattr_on(view->window,
2413 COLOR_PAIR(tc->colorpair), NULL);
2414 waddnstr(view->window, datebuf, limit);
2416 wattr_off(view->window,
2417 COLOR_PAIR(tc->colorpair), NULL);
2424 err = got_object_id_str(&id_str, id);
2427 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2429 wattr_on(view->window,
2430 COLOR_PAIR(tc->colorpair), NULL);
2431 wprintw(view->window, "%.8s ", id_str);
2433 wattr_off(view->window,
2434 COLOR_PAIR(tc->colorpair), NULL);
2441 if (s->use_committer)
2442 author = strdup(got_object_commit_get_committer(commit));
2444 author = strdup(got_object_commit_get_author(commit));
2445 if (author == NULL) {
2446 err = got_error_from_errno("strdup");
2449 err = format_author(&wauthor, &author_width, author, avail - col, col);
2452 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2454 wattr_on(view->window,
2455 COLOR_PAIR(tc->colorpair), NULL);
2456 waddwstr(view->window, wauthor);
2457 col += author_width;
2458 while (col < avail && author_width < author_display_cols + 2) {
2459 waddch(view->window, ' ');
2464 wattr_off(view->window,
2465 COLOR_PAIR(tc->colorpair), NULL);
2469 err = got_object_commit_get_logmsg(&logmsg0, commit);
2473 while (*logmsg == '\n')
2475 newline = strchr(logmsg, '\n');
2478 limit = avail - col;
2479 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2480 limit--; /* for the border */
2481 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2485 waddwstr(view->window, &wlogmsg[scrollx]);
2486 col += MAX(logmsg_width, 0);
2487 while (col < avail) {
2488 waddch(view->window, ' ');
2500 static struct commit_queue_entry *
2501 alloc_commit_queue_entry(struct got_commit_object *commit,
2502 struct got_object_id *id)
2504 struct commit_queue_entry *entry;
2505 struct got_object_id *dup;
2507 entry = calloc(1, sizeof(*entry));
2511 dup = got_object_id_dup(id);
2518 entry->commit = commit;
2523 pop_commit(struct commit_queue *commits)
2525 struct commit_queue_entry *entry;
2527 entry = TAILQ_FIRST(&commits->head);
2528 TAILQ_REMOVE(&commits->head, entry, entry);
2529 got_object_commit_close(entry->commit);
2530 commits->ncommits--;
2536 free_commits(struct commit_queue *commits)
2538 while (!TAILQ_EMPTY(&commits->head))
2539 pop_commit(commits);
2542 static const struct got_error *
2543 match_commit(int *have_match, struct got_object_id *id,
2544 struct got_commit_object *commit, regex_t *regex)
2546 const struct got_error *err = NULL;
2547 regmatch_t regmatch;
2548 char *id_str = NULL, *logmsg = NULL;
2552 err = got_object_id_str(&id_str, id);
2556 err = got_object_commit_get_logmsg(&logmsg, commit);
2560 if (regexec(regex, got_object_commit_get_author(commit), 1,
2561 ®match, 0) == 0 ||
2562 regexec(regex, got_object_commit_get_committer(commit), 1,
2563 ®match, 0) == 0 ||
2564 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2565 regexec(regex, logmsg, 1, ®match, 0) == 0)
2573 static const struct got_error *
2574 queue_commits(struct tog_log_thread_args *a)
2576 const struct got_error *err = NULL;
2579 * We keep all commits open throughout the lifetime of the log
2580 * view in order to avoid having to re-fetch commits from disk
2581 * while updating the display.
2584 struct got_object_id id;
2585 struct got_commit_object *commit;
2586 struct commit_queue_entry *entry;
2587 int limit_match = 0;
2590 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2595 err = got_object_open_as_commit(&commit, a->repo, &id);
2598 entry = alloc_commit_queue_entry(commit, &id);
2599 if (entry == NULL) {
2600 err = got_error_from_errno("alloc_commit_queue_entry");
2604 errcode = pthread_mutex_lock(&tog_mutex);
2606 err = got_error_set_errno(errcode,
2607 "pthread_mutex_lock");
2611 entry->idx = a->real_commits->ncommits;
2612 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2613 a->real_commits->ncommits++;
2616 err = match_commit(&limit_match, &id, commit,
2622 struct commit_queue_entry *matched;
2624 matched = alloc_commit_queue_entry(
2625 entry->commit, entry->id);
2626 if (matched == NULL) {
2627 err = got_error_from_errno(
2628 "alloc_commit_queue_entry");
2631 matched->commit = entry->commit;
2632 got_object_commit_retain(entry->commit);
2634 matched->idx = a->limit_commits->ncommits;
2635 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2637 a->limit_commits->ncommits++;
2641 * This is how we signal log_thread() that we
2642 * have found a match, and that it should be
2643 * counted as a new entry for the view.
2645 a->limit_match = limit_match;
2648 if (*a->searching == TOG_SEARCH_FORWARD &&
2649 !*a->search_next_done) {
2651 err = match_commit(&have_match, &id, commit, a->regex);
2656 if (limit_match && have_match)
2657 *a->search_next_done =
2658 TOG_SEARCH_HAVE_MORE;
2659 } else if (have_match)
2660 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2663 errcode = pthread_mutex_unlock(&tog_mutex);
2664 if (errcode && err == NULL)
2665 err = got_error_set_errno(errcode,
2666 "pthread_mutex_unlock");
2669 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2675 select_commit(struct tog_log_view_state *s)
2677 struct commit_queue_entry *entry;
2680 entry = s->first_displayed_entry;
2682 if (ncommits == s->selected) {
2683 s->selected_entry = entry;
2686 entry = TAILQ_NEXT(entry, entry);
2691 static const struct got_error *
2692 draw_commits(struct tog_view *view)
2694 const struct got_error *err = NULL;
2695 struct tog_log_view_state *s = &view->state.log;
2696 struct commit_queue_entry *entry = s->selected_entry;
2697 int limit = view->nlines;
2699 int ncommits, author_cols = 4;
2700 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2701 char *refs_str = NULL;
2703 struct tog_color *tc;
2704 static const size_t date_display_cols = 12;
2706 if (view_is_hsplit_top(view))
2707 --limit; /* account for border */
2709 if (s->selected_entry &&
2710 !(view->searching && view->search_next_done == 0)) {
2711 struct got_reflist_head *refs;
2712 err = got_object_id_str(&id_str, s->selected_entry->id);
2715 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2716 s->selected_entry->id);
2718 err = build_refs_str(&refs_str, refs,
2719 s->selected_entry->id, s->repo);
2725 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2726 halfdelay(10); /* disable fast refresh */
2728 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2729 if (asprintf(&ncommits_str, " [%d/%d] %s",
2730 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2731 (view->searching && !view->search_next_done) ?
2732 "searching..." : "loading...") == -1) {
2733 err = got_error_from_errno("asprintf");
2737 const char *search_str = NULL;
2738 const char *limit_str = NULL;
2740 if (view->searching) {
2741 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2742 search_str = "no more matches";
2743 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2744 search_str = "no matches found";
2745 else if (!view->search_next_done)
2746 search_str = "searching...";
2749 if (s->limit_view && s->commits->ncommits == 0)
2750 limit_str = "no matches found";
2752 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2753 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2754 search_str ? search_str : (refs_str ? refs_str : ""),
2755 limit_str ? limit_str : "") == -1) {
2756 err = got_error_from_errno("asprintf");
2761 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2762 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2763 "........................................",
2764 s->in_repo_path, ncommits_str) == -1) {
2765 err = got_error_from_errno("asprintf");
2769 } else if (asprintf(&header, "commit %s%s",
2770 id_str ? id_str : "........................................",
2771 ncommits_str) == -1) {
2772 err = got_error_from_errno("asprintf");
2776 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2780 werase(view->window);
2782 if (view_needs_focus_indication(view))
2783 wstandout(view->window);
2784 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2786 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2787 waddwstr(view->window, wline);
2788 while (width < view->ncols) {
2789 waddch(view->window, ' ');
2793 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2794 if (view_needs_focus_indication(view))
2795 wstandend(view->window);
2800 /* Grow author column size if necessary, and set view->maxx. */
2801 entry = s->first_displayed_entry;
2805 struct got_commit_object *c = entry->commit;
2806 char *author, *eol, *msg, *msg0;
2807 wchar_t *wauthor, *wmsg;
2809 if (ncommits >= limit - 1)
2811 if (s->use_committer)
2812 author = strdup(got_object_commit_get_committer(c));
2814 author = strdup(got_object_commit_get_author(c));
2815 if (author == NULL) {
2816 err = got_error_from_errno("strdup");
2819 err = format_author(&wauthor, &width, author, COLS,
2821 if (author_cols < width)
2822 author_cols = width;
2827 err = got_object_commit_get_logmsg(&msg0, c);
2831 while (*msg == '\n')
2833 if ((eol = strchr(msg, '\n')))
2835 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2836 date_display_cols + author_cols, 0);
2839 view->maxx = MAX(view->maxx, width);
2843 entry = TAILQ_NEXT(entry, entry);
2846 entry = s->first_displayed_entry;
2847 s->last_displayed_entry = s->first_displayed_entry;
2850 if (ncommits >= limit - 1)
2852 if (ncommits == s->selected)
2853 wstandout(view->window);
2854 err = draw_commit(view, entry->commit, entry->id,
2855 date_display_cols, author_cols);
2856 if (ncommits == s->selected)
2857 wstandend(view->window);
2861 s->last_displayed_entry = entry;
2862 entry = TAILQ_NEXT(entry, entry);
2875 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2877 struct commit_queue_entry *entry;
2880 entry = TAILQ_FIRST(&s->commits->head);
2881 if (s->first_displayed_entry == entry)
2884 entry = s->first_displayed_entry;
2885 while (entry && nscrolled < maxscroll) {
2886 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2888 s->first_displayed_entry = entry;
2894 static const struct got_error *
2895 trigger_log_thread(struct tog_view *view, int wait)
2897 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2901 halfdelay(1); /* fast refresh while loading commits */
2903 while (!ta->log_complete && !tog_thread_error &&
2904 (ta->commits_needed > 0 || ta->load_all)) {
2905 /* Wake the log thread. */
2906 errcode = pthread_cond_signal(&ta->need_commits);
2908 return got_error_set_errno(errcode,
2909 "pthread_cond_signal");
2912 * The mutex will be released while the view loop waits
2913 * in wgetch(), at which time the log thread will run.
2918 /* Display progress update in log view. */
2919 show_log_view(view);
2923 /* Wait right here while next commit is being loaded. */
2924 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2926 return got_error_set_errno(errcode,
2927 "pthread_cond_wait");
2929 /* Display progress update in log view. */
2930 show_log_view(view);
2938 static const struct got_error *
2939 request_log_commits(struct tog_view *view)
2941 struct tog_log_view_state *state = &view->state.log;
2942 const struct got_error *err = NULL;
2944 if (state->thread_args.log_complete)
2947 state->thread_args.commits_needed += view->nscrolled;
2948 err = trigger_log_thread(view, 1);
2949 view->nscrolled = 0;
2954 static const struct got_error *
2955 log_scroll_down(struct tog_view *view, int maxscroll)
2957 struct tog_log_view_state *s = &view->state.log;
2958 const struct got_error *err = NULL;
2959 struct commit_queue_entry *pentry;
2960 int nscrolled = 0, ncommits_needed;
2962 if (s->last_displayed_entry == NULL)
2965 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2966 if (s->commits->ncommits < ncommits_needed &&
2967 !s->thread_args.log_complete) {
2969 * Ask the log thread for required amount of commits.
2971 s->thread_args.commits_needed +=
2972 ncommits_needed - s->commits->ncommits;
2973 err = trigger_log_thread(view, 1);
2979 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2980 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2983 s->last_displayed_entry = pentry ?
2984 pentry : s->last_displayed_entry;
2986 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2989 s->first_displayed_entry = pentry;
2990 } while (++nscrolled < maxscroll);
2992 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2993 view->nscrolled += nscrolled;
2995 view->nscrolled = 0;
3000 static const struct got_error *
3001 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3002 struct got_commit_object *commit, struct got_object_id *commit_id,
3003 struct tog_view *log_view, struct got_repository *repo)
3005 const struct got_error *err;
3006 struct got_object_qid *parent_id;
3007 struct tog_view *diff_view;
3009 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3010 if (diff_view == NULL)
3011 return got_error_from_errno("view_open");
3013 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3014 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3015 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3017 *new_view = diff_view;
3021 static const struct got_error *
3022 tree_view_visit_subtree(struct tog_tree_view_state *s,
3023 struct got_tree_object *subtree)
3025 struct tog_parent_tree *parent;
3027 parent = calloc(1, sizeof(*parent));
3029 return got_error_from_errno("calloc");
3031 parent->tree = s->tree;
3032 parent->first_displayed_entry = s->first_displayed_entry;
3033 parent->selected_entry = s->selected_entry;
3034 parent->selected = s->selected;
3035 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3038 s->first_displayed_entry = NULL;
3042 static const struct got_error *
3043 tree_view_walk_path(struct tog_tree_view_state *s,
3044 struct got_commit_object *commit, const char *path)
3046 const struct got_error *err = NULL;
3047 struct got_tree_object *tree = NULL;
3049 char *slash, *subpath = NULL;
3051 /* Walk the path and open corresponding tree objects. */
3054 struct got_tree_entry *te;
3055 struct got_object_id *tree_id;
3061 /* Ensure the correct subtree entry is selected. */
3062 slash = strchr(p, '/');
3064 te_name = strdup(p);
3066 te_name = strndup(p, slash - p);
3067 if (te_name == NULL) {
3068 err = got_error_from_errno("strndup");
3071 te = got_object_tree_find_entry(s->tree, te_name);
3073 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3078 s->first_displayed_entry = s->selected_entry = te;
3080 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3081 break; /* jump to this file's entry */
3083 slash = strchr(p, '/');
3085 subpath = strndup(path, slash - path);
3087 subpath = strdup(path);
3088 if (subpath == NULL) {
3089 err = got_error_from_errno("strdup");
3093 err = got_object_id_by_path(&tree_id, s->repo, commit,
3098 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3103 err = tree_view_visit_subtree(s, tree);
3105 got_object_tree_close(tree);
3119 static const struct got_error *
3120 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3121 struct commit_queue_entry *entry, const char *path,
3122 const char *head_ref_name, struct got_repository *repo)
3124 const struct got_error *err = NULL;
3125 struct tog_tree_view_state *s;
3126 struct tog_view *tree_view;
3128 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3129 if (tree_view == NULL)
3130 return got_error_from_errno("view_open");
3132 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3135 s = &tree_view->state.tree;
3137 *new_view = tree_view;
3139 if (got_path_is_root_dir(path))
3142 return tree_view_walk_path(s, entry->commit, path);
3145 static const struct got_error *
3146 block_signals_used_by_main_thread(void)
3151 if (sigemptyset(&sigset) == -1)
3152 return got_error_from_errno("sigemptyset");
3154 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3155 if (sigaddset(&sigset, SIGWINCH) == -1)
3156 return got_error_from_errno("sigaddset");
3157 if (sigaddset(&sigset, SIGCONT) == -1)
3158 return got_error_from_errno("sigaddset");
3159 if (sigaddset(&sigset, SIGINT) == -1)
3160 return got_error_from_errno("sigaddset");
3161 if (sigaddset(&sigset, SIGTERM) == -1)
3162 return got_error_from_errno("sigaddset");
3164 /* ncurses handles SIGTSTP */
3165 if (sigaddset(&sigset, SIGTSTP) == -1)
3166 return got_error_from_errno("sigaddset");
3168 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3170 return got_error_set_errno(errcode, "pthread_sigmask");
3176 log_thread(void *arg)
3178 const struct got_error *err = NULL;
3180 struct tog_log_thread_args *a = arg;
3184 * Sync startup with main thread such that we begin our
3185 * work once view_input() has released the mutex.
3187 errcode = pthread_mutex_lock(&tog_mutex);
3189 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3193 err = block_signals_used_by_main_thread();
3195 pthread_mutex_unlock(&tog_mutex);
3199 while (!done && !err && !tog_fatal_signal_received()) {
3200 errcode = pthread_mutex_unlock(&tog_mutex);
3202 err = got_error_set_errno(errcode,
3203 "pthread_mutex_unlock");
3206 err = queue_commits(a);
3208 if (err->code != GOT_ERR_ITER_COMPLETED)
3212 } else if (a->commits_needed > 0 && !a->load_all) {
3215 a->commits_needed--;
3217 a->commits_needed--;
3220 errcode = pthread_mutex_lock(&tog_mutex);
3222 err = got_error_set_errno(errcode,
3223 "pthread_mutex_lock");
3225 } else if (*a->quit)
3227 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3228 *a->first_displayed_entry =
3229 TAILQ_FIRST(&a->limit_commits->head);
3230 *a->selected_entry = *a->first_displayed_entry;
3231 } else if (*a->first_displayed_entry == NULL) {
3232 *a->first_displayed_entry =
3233 TAILQ_FIRST(&a->real_commits->head);
3234 *a->selected_entry = *a->first_displayed_entry;
3237 errcode = pthread_cond_signal(&a->commit_loaded);
3239 err = got_error_set_errno(errcode,
3240 "pthread_cond_signal");
3241 pthread_mutex_unlock(&tog_mutex);
3246 a->commits_needed = 0;
3248 if (a->commits_needed == 0 && !a->load_all) {
3249 errcode = pthread_cond_wait(&a->need_commits,
3252 err = got_error_set_errno(errcode,
3253 "pthread_cond_wait");
3254 pthread_mutex_unlock(&tog_mutex);
3262 a->log_complete = 1;
3263 errcode = pthread_mutex_unlock(&tog_mutex);
3265 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3268 tog_thread_error = 1;
3269 pthread_cond_signal(&a->commit_loaded);
3274 static const struct got_error *
3275 stop_log_thread(struct tog_log_view_state *s)
3277 const struct got_error *err = NULL, *thread_err = NULL;
3282 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3284 return got_error_set_errno(errcode,
3285 "pthread_cond_signal");
3286 errcode = pthread_mutex_unlock(&tog_mutex);
3288 return got_error_set_errno(errcode,
3289 "pthread_mutex_unlock");
3290 errcode = pthread_join(s->thread, (void **)&thread_err);
3292 return got_error_set_errno(errcode, "pthread_join");
3293 errcode = pthread_mutex_lock(&tog_mutex);
3295 return got_error_set_errno(errcode,
3296 "pthread_mutex_lock");
3300 if (s->thread_args.repo) {
3301 err = got_repo_close(s->thread_args.repo);
3302 s->thread_args.repo = NULL;
3305 if (s->thread_args.pack_fds) {
3306 const struct got_error *pack_err =
3307 got_repo_pack_fds_close(s->thread_args.pack_fds);
3310 s->thread_args.pack_fds = NULL;
3313 if (s->thread_args.graph) {
3314 got_commit_graph_close(s->thread_args.graph);
3315 s->thread_args.graph = NULL;
3318 return err ? err : thread_err;
3321 static const struct got_error *
3322 close_log_view(struct tog_view *view)
3324 const struct got_error *err = NULL;
3325 struct tog_log_view_state *s = &view->state.log;
3328 err = stop_log_thread(s);
3330 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3331 if (errcode && err == NULL)
3332 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3334 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3335 if (errcode && err == NULL)
3336 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3338 free_commits(&s->limit_commits);
3339 free_commits(&s->real_commits);
3340 free(s->in_repo_path);
3341 s->in_repo_path = NULL;
3344 free(s->head_ref_name);
3345 s->head_ref_name = NULL;
3350 * We use two queues to implement the limit feature: first consists of
3351 * commits matching the current limit_regex; second is the real queue
3352 * of all known commits (real_commits). When the user starts limiting,
3353 * we swap queues such that all movement and displaying functionality
3354 * works with very slight change.
3356 static const struct got_error *
3357 limit_log_view(struct tog_view *view)
3359 struct tog_log_view_state *s = &view->state.log;
3360 struct commit_queue_entry *entry;
3361 struct tog_view *v = view;
3362 const struct got_error *err = NULL;
3366 if (view_is_hsplit_top(view))
3368 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3371 /* Get the pattern */
3372 wmove(v->window, v->nlines - 1, 0);
3373 wclrtoeol(v->window);
3374 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3375 nodelay(v->window, FALSE);
3378 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3381 nodelay(v->window, TRUE);
3385 if (*pattern == '\0') {
3387 * Safety measure for the situation where the user
3388 * resets limit without previously limiting anything.
3394 * User could have pressed Ctrl+L, which refreshed the
3395 * commit queues, it means we can't save previously
3396 * (before limit took place) displayed entries,
3397 * because they would point to already free'ed memory,
3398 * so we are forced to always select first entry of
3401 s->commits = &s->real_commits;
3402 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3403 s->selected_entry = s->first_displayed_entry;
3410 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3415 /* Clear the screen while loading limit view */
3416 s->first_displayed_entry = NULL;
3417 s->last_displayed_entry = NULL;
3418 s->selected_entry = NULL;
3419 s->commits = &s->limit_commits;
3421 /* Prepare limit queue for new search */
3422 free_commits(&s->limit_commits);
3423 s->limit_commits.ncommits = 0;
3425 /* First process commits, which are in queue already */
3426 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3429 err = match_commit(&have_match, entry->id,
3430 entry->commit, &s->limit_regex);
3435 struct commit_queue_entry *matched;
3437 matched = alloc_commit_queue_entry(entry->commit,
3439 if (matched == NULL) {
3440 err = got_error_from_errno(
3441 "alloc_commit_queue_entry");
3444 matched->commit = entry->commit;
3445 got_object_commit_retain(entry->commit);
3447 matched->idx = s->limit_commits.ncommits;
3448 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3450 s->limit_commits.ncommits++;
3454 /* Second process all the commits, until we fill the screen */
3455 if (s->limit_commits.ncommits < view->nlines - 1 &&
3456 !s->thread_args.log_complete) {
3457 s->thread_args.commits_needed +=
3458 view->nlines - s->limit_commits.ncommits - 1;
3459 err = trigger_log_thread(view, 1);
3464 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3465 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3471 static const struct got_error *
3472 search_start_log_view(struct tog_view *view)
3474 struct tog_log_view_state *s = &view->state.log;
3476 s->matched_entry = NULL;
3477 s->search_entry = NULL;
3481 static const struct got_error *
3482 search_next_log_view(struct tog_view *view)
3484 const struct got_error *err = NULL;
3485 struct tog_log_view_state *s = &view->state.log;
3486 struct commit_queue_entry *entry;
3488 /* Display progress update in log view. */
3489 show_log_view(view);
3493 if (s->search_entry) {
3495 errcode = pthread_mutex_unlock(&tog_mutex);
3497 return got_error_set_errno(errcode,
3498 "pthread_mutex_unlock");
3499 ch = wgetch(view->window);
3500 errcode = pthread_mutex_lock(&tog_mutex);
3502 return got_error_set_errno(errcode,
3503 "pthread_mutex_lock");
3504 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3505 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3508 if (view->searching == TOG_SEARCH_FORWARD)
3509 entry = TAILQ_NEXT(s->search_entry, entry);
3511 entry = TAILQ_PREV(s->search_entry,
3512 commit_queue_head, entry);
3513 } else if (s->matched_entry) {
3515 * If the user has moved the cursor after we hit a match,
3516 * the position from where we should continue searching
3517 * might have changed.
3519 if (view->searching == TOG_SEARCH_FORWARD)
3520 entry = TAILQ_NEXT(s->selected_entry, entry);
3522 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3525 entry = s->selected_entry;
3531 if (entry == NULL) {
3532 if (s->thread_args.log_complete ||
3533 view->searching == TOG_SEARCH_BACKWARD) {
3534 view->search_next_done =
3535 (s->matched_entry == NULL ?
3536 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3537 s->search_entry = NULL;
3541 * Poke the log thread for more commits and return,
3542 * allowing the main loop to make progress. Search
3543 * will resume at s->search_entry once we come back.
3545 s->thread_args.commits_needed++;
3546 return trigger_log_thread(view, 0);
3549 err = match_commit(&have_match, entry->id, entry->commit,
3554 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3555 s->matched_entry = entry;
3559 s->search_entry = entry;
3560 if (view->searching == TOG_SEARCH_FORWARD)
3561 entry = TAILQ_NEXT(entry, entry);
3563 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3566 if (s->matched_entry) {
3567 int cur = s->selected_entry->idx;
3568 while (cur < s->matched_entry->idx) {
3569 err = input_log_view(NULL, view, KEY_DOWN);
3574 while (cur > s->matched_entry->idx) {
3575 err = input_log_view(NULL, view, KEY_UP);
3582 s->search_entry = NULL;
3587 static const struct got_error *
3588 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3589 struct got_repository *repo, const char *head_ref_name,
3590 const char *in_repo_path, int log_branches)
3592 const struct got_error *err = NULL;
3593 struct tog_log_view_state *s = &view->state.log;
3594 struct got_repository *thread_repo = NULL;
3595 struct got_commit_graph *thread_graph = NULL;
3598 if (in_repo_path != s->in_repo_path) {
3599 free(s->in_repo_path);
3600 s->in_repo_path = strdup(in_repo_path);
3601 if (s->in_repo_path == NULL) {
3602 err = got_error_from_errno("strdup");
3607 /* The commit queue only contains commits being displayed. */
3608 TAILQ_INIT(&s->real_commits.head);
3609 s->real_commits.ncommits = 0;
3610 s->commits = &s->real_commits;
3612 TAILQ_INIT(&s->limit_commits.head);
3614 s->limit_commits.ncommits = 0;
3617 if (head_ref_name) {
3618 s->head_ref_name = strdup(head_ref_name);
3619 if (s->head_ref_name == NULL) {
3620 err = got_error_from_errno("strdup");
3624 s->start_id = got_object_id_dup(start_id);
3625 if (s->start_id == NULL) {
3626 err = got_error_from_errno("got_object_id_dup");
3629 s->log_branches = log_branches;
3630 s->use_committer = 1;
3632 STAILQ_INIT(&s->colors);
3633 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3634 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3635 get_color_value("TOG_COLOR_COMMIT"));
3638 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3639 get_color_value("TOG_COLOR_AUTHOR"));
3641 free_colors(&s->colors);
3644 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3645 get_color_value("TOG_COLOR_DATE"));
3647 free_colors(&s->colors);
3652 view->show = show_log_view;
3653 view->input = input_log_view;
3654 view->resize = resize_log_view;
3655 view->close = close_log_view;
3656 view->search_start = search_start_log_view;
3657 view->search_next = search_next_log_view;
3659 if (s->thread_args.pack_fds == NULL) {
3660 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3664 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3665 s->thread_args.pack_fds);
3668 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3672 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3673 s->repo, NULL, NULL);
3677 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3679 err = got_error_set_errno(errcode, "pthread_cond_init");
3682 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3684 err = got_error_set_errno(errcode, "pthread_cond_init");
3688 s->thread_args.commits_needed = view->nlines;
3689 s->thread_args.graph = thread_graph;
3690 s->thread_args.real_commits = &s->real_commits;
3691 s->thread_args.limit_commits = &s->limit_commits;
3692 s->thread_args.in_repo_path = s->in_repo_path;
3693 s->thread_args.start_id = s->start_id;
3694 s->thread_args.repo = thread_repo;
3695 s->thread_args.log_complete = 0;
3696 s->thread_args.quit = &s->quit;
3697 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3698 s->thread_args.selected_entry = &s->selected_entry;
3699 s->thread_args.searching = &view->searching;
3700 s->thread_args.search_next_done = &view->search_next_done;
3701 s->thread_args.regex = &view->regex;
3702 s->thread_args.limiting = &s->limit_view;
3703 s->thread_args.limit_regex = &s->limit_regex;
3704 s->thread_args.limit_commits = &s->limit_commits;
3707 if (view->close == NULL)
3708 close_log_view(view);
3714 static const struct got_error *
3715 show_log_view(struct tog_view *view)
3717 const struct got_error *err;
3718 struct tog_log_view_state *s = &view->state.log;
3720 if (s->thread == NULL) {
3721 int errcode = pthread_create(&s->thread, NULL, log_thread,
3724 return got_error_set_errno(errcode, "pthread_create");
3725 if (s->thread_args.commits_needed > 0) {
3726 err = trigger_log_thread(view, 1);
3732 return draw_commits(view);
3736 log_move_cursor_up(struct tog_view *view, int page, int home)
3738 struct tog_log_view_state *s = &view->state.log;
3740 if (s->first_displayed_entry == NULL)
3742 if (s->selected_entry->idx == 0)
3745 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3747 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3749 if (!page && !home && s->selected > 0)
3752 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3758 static const struct got_error *
3759 log_move_cursor_down(struct tog_view *view, int page)
3761 struct tog_log_view_state *s = &view->state.log;
3762 const struct got_error *err = NULL;
3763 int eos = view->nlines - 2;
3765 if (s->first_displayed_entry == NULL)
3768 if (s->thread_args.log_complete &&
3769 s->selected_entry->idx >= s->commits->ncommits - 1)
3772 if (view_is_hsplit_top(view))
3773 --eos; /* border consumes the last line */
3776 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3779 err = log_scroll_down(view, 1);
3780 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3781 struct commit_queue_entry *entry;
3785 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3786 s->last_displayed_entry = entry;
3787 for (n = 0; n <= eos; n++) {
3790 s->first_displayed_entry = entry;
3791 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3794 s->selected = n - 1;
3796 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3797 s->thread_args.log_complete)
3798 s->selected += MIN(page,
3799 s->commits->ncommits - s->selected_entry->idx - 1);
3801 err = log_scroll_down(view, page);
3807 * We might necessarily overshoot in horizontal
3808 * splits; if so, select the last displayed commit.
3810 if (s->first_displayed_entry && s->last_displayed_entry) {
3811 s->selected = MIN(s->selected,
3812 s->last_displayed_entry->idx -
3813 s->first_displayed_entry->idx);
3818 if (s->thread_args.log_complete &&
3819 s->selected_entry->idx == s->commits->ncommits - 1)
3826 view_get_split(struct tog_view *view, int *y, int *x)
3831 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3832 if (view->child && view->child->resized_y)
3833 *y = view->child->resized_y;
3834 else if (view->resized_y)
3835 *y = view->resized_y;
3837 *y = view_split_begin_y(view->lines);
3838 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3839 if (view->child && view->child->resized_x)
3840 *x = view->child->resized_x;
3841 else if (view->resized_x)
3842 *x = view->resized_x;
3844 *x = view_split_begin_x(view->begin_x);
3848 /* Split view horizontally at y and offset view->state->selected line. */
3849 static const struct got_error *
3850 view_init_hsplit(struct tog_view *view, int y)
3852 const struct got_error *err = NULL;
3856 err = view_resize(view);
3860 err = offset_selection_down(view);
3865 static const struct got_error *
3866 log_goto_line(struct tog_view *view, int nlines)
3868 const struct got_error *err = NULL;
3869 struct tog_log_view_state *s = &view->state.log;
3870 int g, idx = s->selected_entry->idx;
3872 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3878 if (g >= s->first_displayed_entry->idx + 1 &&
3879 g <= s->last_displayed_entry->idx + 1 &&
3880 g - s->first_displayed_entry->idx - 1 < nlines) {
3881 s->selected = g - s->first_displayed_entry->idx - 1;
3887 err = log_move_cursor_down(view, g - idx - 1);
3888 if (!err && g > s->selected_entry->idx + 1)
3889 err = log_move_cursor_down(view,
3890 g - s->first_displayed_entry->idx - 1);
3893 } else if (idx + 1 > g)
3894 log_move_cursor_up(view, idx - g + 1, 0);
3896 if (g < nlines && s->first_displayed_entry->idx == 0)
3897 s->selected = g - 1;
3905 horizontal_scroll_input(struct tog_view *view, int ch)
3911 view->x -= MIN(view->x, 2);
3917 if (view->x + view->ncols / 2 < view->maxx)
3926 view->x = MAX(view->maxx - view->ncols / 2, 0);
3934 static const struct got_error *
3935 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3937 const struct got_error *err = NULL;
3938 struct tog_log_view_state *s = &view->state.log;
3941 if (s->thread_args.load_all) {
3942 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3943 s->thread_args.load_all = 0;
3944 else if (s->thread_args.log_complete) {
3945 err = log_move_cursor_down(view, s->commits->ncommits);
3946 s->thread_args.load_all = 0;
3952 eos = nscroll = view->nlines - 1;
3953 if (view_is_hsplit_top(view))
3957 return log_goto_line(view, eos);
3961 err = limit_log_view(view);
3972 horizontal_scroll_input(view, ch);
3979 log_move_cursor_up(view, 0, 0);
3984 log_move_cursor_up(view, 0, 1);
3994 log_move_cursor_up(view, nscroll, 0);
4001 err = log_move_cursor_down(view, 0);
4004 s->use_committer = !s->use_committer;
4005 view->action = s->use_committer ?
4006 "show committer" : "show commit author";
4011 /* We don't know yet how many commits, so we're forced to
4012 * traverse them all. */
4014 s->thread_args.load_all = 1;
4015 if (!s->thread_args.log_complete)
4016 return trigger_log_thread(view, 0);
4017 err = log_move_cursor_down(view, s->commits->ncommits);
4018 s->thread_args.load_all = 0;
4029 err = log_move_cursor_down(view, nscroll);
4032 if (s->selected > view->nlines - 2)
4033 s->selected = view->nlines - 2;
4034 if (s->selected > s->commits->ncommits - 1)
4035 s->selected = s->commits->ncommits - 1;
4037 if (s->commits->ncommits < view->nlines - 1 &&
4038 !s->thread_args.log_complete) {
4039 s->thread_args.commits_needed += (view->nlines - 1) -
4040 s->commits->ncommits;
4041 err = trigger_log_thread(view, 1);
4047 if (s->selected_entry == NULL)
4049 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4053 if (s->selected_entry == NULL)
4055 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4061 if (ch == KEY_BACKSPACE &&
4062 got_path_is_root_dir(s->in_repo_path))
4064 err = stop_log_thread(s);
4067 if (ch == KEY_BACKSPACE) {
4069 err = got_path_dirname(&parent_path, s->in_repo_path);
4072 free(s->in_repo_path);
4073 s->in_repo_path = parent_path;
4074 s->thread_args.in_repo_path = s->in_repo_path;
4075 } else if (ch == CTRL('l')) {
4076 struct got_object_id *start_id;
4077 err = got_repo_match_object_id(&start_id, NULL,
4078 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4079 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4081 if (s->head_ref_name == NULL ||
4082 err->code != GOT_ERR_NOT_REF)
4084 /* Try to cope with deleted references. */
4085 free(s->head_ref_name);
4086 s->head_ref_name = NULL;
4087 err = got_repo_match_object_id(&start_id,
4088 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4089 &tog_refs, s->repo);
4094 s->start_id = start_id;
4095 s->thread_args.start_id = s->start_id;
4097 s->log_branches = !s->log_branches;
4099 if (s->thread_args.pack_fds == NULL) {
4100 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4104 err = got_repo_open(&s->thread_args.repo,
4105 got_repo_get_path(s->repo), NULL,
4106 s->thread_args.pack_fds);
4110 err = tog_load_refs(s->repo, 0);
4113 err = got_commit_graph_open(&s->thread_args.graph,
4114 s->in_repo_path, !s->log_branches);
4117 err = got_commit_graph_iter_start(s->thread_args.graph,
4118 s->start_id, s->repo, NULL, NULL);
4121 free_commits(&s->real_commits);
4122 free_commits(&s->limit_commits);
4123 s->first_displayed_entry = NULL;
4124 s->last_displayed_entry = NULL;
4125 s->selected_entry = NULL;
4127 s->thread_args.log_complete = 0;
4129 s->thread_args.commits_needed = view->lines;
4130 s->matched_entry = NULL;
4131 s->search_entry = NULL;
4136 err = view_request_new(new_view, view, TOG_VIEW_REF);
4146 static const struct got_error *
4147 apply_unveil(const char *repo_path, const char *worktree_path)
4149 const struct got_error *error;
4152 if (unveil("gmon.out", "rwc") != 0)
4153 return got_error_from_errno2("unveil", "gmon.out");
4155 if (repo_path && unveil(repo_path, "r") != 0)
4156 return got_error_from_errno2("unveil", repo_path);
4158 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4159 return got_error_from_errno2("unveil", worktree_path);
4161 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4162 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4164 error = got_privsep_unveil_exec_helpers();
4168 if (unveil(NULL, NULL) != 0)
4169 return got_error_from_errno("unveil");
4174 static const struct got_error *
4175 init_mock_term(const char *test_script_path)
4177 const struct got_error *err = NULL;
4179 if (test_script_path == NULL || *test_script_path == '\0')
4180 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4182 tog_io.f = fopen(test_script_path, "re");
4183 if (tog_io.f == NULL) {
4184 err = got_error_from_errno_fmt("fopen: %s",
4189 /* test mode, we don't want any output */
4190 tog_io.cout = fopen("/dev/null", "w+");
4191 if (tog_io.cout == NULL) {
4192 err = got_error_from_errno("fopen: /dev/null");
4196 tog_io.cin = fopen("/dev/tty", "r+");
4197 if (tog_io.cin == NULL) {
4198 err = got_error_from_errno("fopen: /dev/tty");
4202 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4203 err = got_error_from_errno("fseeko");
4207 /* use local TERM so we test in different environments */
4208 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4209 err = got_error_msg(GOT_ERR_IO,
4210 "newterm: failed to initialise curses");
4223 * Override default signal handlers before starting ncurses.
4224 * This should prevent ncurses from installing its own
4225 * broken cleanup() signal handler.
4227 signal(SIGWINCH, tog_sigwinch);
4228 signal(SIGPIPE, tog_sigpipe);
4229 signal(SIGCONT, tog_sigcont);
4230 signal(SIGINT, tog_sigint);
4231 signal(SIGTERM, tog_sigterm);
4233 if (using_mock_io) /* In test mode we use a fake terminal */
4239 halfdelay(1); /* Fast refresh while initial view is loading. */
4242 intrflush(stdscr, FALSE);
4243 keypad(stdscr, TRUE);
4245 if (getenv("TOG_COLORS") != NULL) {
4247 use_default_colors();
4253 static const struct got_error *
4254 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4255 struct got_repository *repo, struct got_worktree *worktree)
4257 const struct got_error *err = NULL;
4260 *in_repo_path = strdup("/");
4261 if (*in_repo_path == NULL)
4262 return got_error_from_errno("strdup");
4267 const char *prefix = got_worktree_get_path_prefix(worktree);
4270 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4273 if (asprintf(in_repo_path, "%s%s%s", prefix,
4274 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4276 err = got_error_from_errno("asprintf");
4277 *in_repo_path = NULL;
4281 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4286 static const struct got_error *
4287 cmd_log(int argc, char *argv[])
4289 const struct got_error *error;
4290 struct got_repository *repo = NULL;
4291 struct got_worktree *worktree = NULL;
4292 struct got_object_id *start_id = NULL;
4293 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4294 char *start_commit = NULL, *label = NULL;
4295 struct got_reference *ref = NULL;
4296 const char *head_ref_name = NULL;
4297 int ch, log_branches = 0;
4298 struct tog_view *view;
4299 int *pack_fds = NULL;
4301 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4307 start_commit = optarg;
4310 repo_path = realpath(optarg, NULL);
4311 if (repo_path == NULL)
4312 return got_error_from_errno2("realpath",
4327 error = got_repo_pack_fds_open(&pack_fds);
4331 if (repo_path == NULL) {
4332 cwd = getcwd(NULL, 0);
4334 return got_error_from_errno("getcwd");
4335 error = got_worktree_open(&worktree, cwd);
4336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4340 strdup(got_worktree_get_repo_path(worktree));
4342 repo_path = strdup(cwd);
4343 if (repo_path == NULL) {
4344 error = got_error_from_errno("strdup");
4349 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4353 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4360 error = apply_unveil(got_repo_get_path(repo),
4361 worktree ? got_worktree_get_root_path(worktree) : NULL);
4365 /* already loaded by tog_log_with_path()? */
4366 if (TAILQ_EMPTY(&tog_refs)) {
4367 error = tog_load_refs(repo, 0);
4372 if (start_commit == NULL) {
4373 error = got_repo_match_object_id(&start_id, &label,
4374 worktree ? got_worktree_get_head_ref_name(worktree) :
4375 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4378 head_ref_name = label;
4380 error = got_ref_open(&ref, repo, start_commit, 0);
4382 head_ref_name = got_ref_get_name(ref);
4383 else if (error->code != GOT_ERR_NOT_REF)
4385 error = got_repo_match_object_id(&start_id, NULL,
4386 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4391 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4393 error = got_error_from_errno("view_open");
4396 error = open_log_view(view, start_id, repo, head_ref_name,
4397 in_repo_path, log_branches);
4401 /* Release work tree lock. */
4402 got_worktree_close(worktree);
4405 error = view_loop(view);
4415 const struct got_error *close_err = got_repo_close(repo);
4420 got_worktree_close(worktree);
4422 const struct got_error *pack_err =
4423 got_repo_pack_fds_close(pack_fds);
4435 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4436 "object1 object2\n", getprogname());
4441 match_line(const char *line, regex_t *regex, size_t nmatch,
4442 regmatch_t *regmatch)
4444 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4447 static struct tog_color *
4448 match_color(struct tog_colors *colors, const char *line)
4450 struct tog_color *tc = NULL;
4452 STAILQ_FOREACH(tc, colors, entry) {
4453 if (match_line(line, &tc->regex, 0, NULL))
4460 static const struct got_error *
4461 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4462 WINDOW *window, int skipcol, regmatch_t *regmatch)
4464 const struct got_error *err = NULL;
4466 wchar_t *wline = NULL;
4467 int rme, rms, n, width, scrollx;
4468 int width0 = 0, width1 = 0, width2 = 0;
4469 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4473 rms = regmatch->rm_so;
4474 rme = regmatch->rm_eo;
4476 err = expand_tab(&exstr, line);
4480 /* Split the line into 3 segments, according to match offsets. */
4481 seg0 = strndup(exstr, rms);
4483 err = got_error_from_errno("strndup");
4486 seg1 = strndup(exstr + rms, rme - rms);
4488 err = got_error_from_errno("strndup");
4491 seg2 = strdup(exstr + rme);
4493 err = got_error_from_errno("strndup");
4497 /* draw up to matched token if we haven't scrolled past it */
4498 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4502 n = MAX(width0 - skipcol, 0);
4505 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4506 wlimit, col_tab_align, 1);
4509 waddwstr(window, &wline[scrollx]);
4519 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4523 wlen = wcslen(wline);
4525 width = wcwidth(wline[i]);
4527 /* should not happen, tabs are expanded */
4528 err = got_error(GOT_ERR_RANGE);
4531 if (width0 + w + width > skipcol)
4536 /* draw (visible part of) matched token (if scrolled into it) */
4537 if (width1 - w > 0) {
4538 wattron(window, A_STANDOUT);
4539 waddwstr(window, &wline[i]);
4540 wattroff(window, A_STANDOUT);
4541 wlimit -= (width1 - w);
4542 *wtotal += (width1 - w);
4546 if (wlimit > 0) { /* draw rest of line */
4548 if (skipcol > width0 + width1) {
4549 err = format_line(&wline, &width2, &scrollx, seg2,
4550 skipcol - (width0 + width1), wlimit,
4554 waddwstr(window, &wline[scrollx]);
4556 err = format_line(&wline, &width2, NULL, seg2, 0,
4557 wlimit, col_tab_align, 1);
4560 waddwstr(window, wline);
4574 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4577 int *eof, *first, *selected;
4579 if (view->type == TOG_VIEW_DIFF) {
4580 struct tog_diff_view_state *s = &view->state.diff;
4582 first = &s->first_displayed_line;
4586 } else if (view->type == TOG_VIEW_HELP) {
4587 struct tog_help_view_state *s = &view->state.help;
4589 first = &s->first_displayed_line;
4593 } else if (view->type == TOG_VIEW_BLAME) {
4594 struct tog_blame_view_state *s = &view->state.blame;
4596 first = &s->first_displayed_line;
4597 selected = &s->selected_line;
4603 /* Center gline in the middle of the page like vi(1). */
4604 if (*lineno < view->gline - (view->nlines - 3) / 2)
4606 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4615 *selected = view->gline <= (view->nlines - 3) / 2 ?
4616 view->gline : (view->nlines - 3) / 2 + 1;
4622 static const struct got_error *
4623 draw_file(struct tog_view *view, const char *header)
4625 struct tog_diff_view_state *s = &view->state.diff;
4626 regmatch_t *regmatch = &view->regmatch;
4627 const struct got_error *err;
4630 size_t linesize = 0;
4634 int max_lines = view->nlines;
4635 int nlines = s->nlines;
4638 s->lineno = s->first_displayed_line - 1;
4639 line_offset = s->lines[s->first_displayed_line - 1].offset;
4640 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4641 return got_error_from_errno("fseek");
4643 werase(view->window);
4645 if (view->gline > s->nlines - 1)
4646 view->gline = s->nlines - 1;
4649 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4650 1 : view->gline - (view->nlines - 3) / 2 :
4651 s->lineno + s->selected_line;
4653 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4654 return got_error_from_errno("asprintf");
4655 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4661 if (view_needs_focus_indication(view))
4662 wstandout(view->window);
4663 waddwstr(view->window, wline);
4666 while (width++ < view->ncols)
4667 waddch(view->window, ' ');
4668 if (view_needs_focus_indication(view))
4669 wstandend(view->window);
4679 while (max_lines > 0 && nprinted < max_lines) {
4680 enum got_diff_line_type linetype;
4683 linelen = getline(&line, &linesize, s->f);
4684 if (linelen == -1) {
4690 return got_ferror(s->f, GOT_ERR_IO);
4693 if (++s->lineno < s->first_displayed_line)
4695 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4697 if (s->lineno == view->hiline)
4700 /* Set view->maxx based on full line length. */
4701 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4707 view->maxx = MAX(view->maxx, width);
4711 linetype = s->lines[s->lineno].type;
4712 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4713 linetype < GOT_DIFF_LINE_CONTEXT)
4714 attr |= COLOR_PAIR(linetype);
4716 wattron(view->window, attr);
4717 if (s->first_displayed_line + nprinted == s->matched_line &&
4718 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4719 err = add_matched_line(&width, line, view->ncols, 0,
4720 view->window, view->x, regmatch);
4727 err = format_line(&wline, &width, &skip, line,
4728 view->x, view->ncols, 0, view->x ? 1 : 0);
4733 waddwstr(view->window, &wline[skip]);
4737 if (s->lineno == view->hiline) {
4738 /* highlight full gline length */
4739 while (width++ < view->ncols)
4740 waddch(view->window, ' ');
4742 if (width <= view->ncols - 1)
4743 waddch(view->window, '\n');
4746 wattroff(view->window, attr);
4747 if (++nprinted == 1)
4748 s->first_displayed_line = s->lineno;
4752 s->last_displayed_line = s->first_displayed_line +
4755 s->last_displayed_line = s->first_displayed_line;
4760 while (nprinted < view->nlines) {
4761 waddch(view->window, '\n');
4765 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4771 wstandout(view->window);
4772 waddwstr(view->window, wline);
4775 wstandend(view->window);
4782 get_datestr(time_t *time, char *datebuf)
4784 struct tm mytm, *tm;
4787 tm = gmtime_r(time, &mytm);
4790 s = asctime_r(tm, datebuf);
4793 p = strchr(s, '\n');
4799 static const struct got_error *
4800 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4801 off_t off, uint8_t type)
4803 struct got_diff_line *p;
4805 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4807 return got_error_from_errno("reallocarray");
4809 (*lines)[*nlines].offset = off;
4810 (*lines)[*nlines].type = type;
4816 static const struct got_error *
4817 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4818 struct got_diff_line *s_lines, size_t s_nlines)
4820 struct got_diff_line *p;
4824 if (fseeko(src, 0L, SEEK_SET) == -1)
4825 return got_error_from_errno("fseeko");
4828 r = fread(buf, 1, sizeof(buf), src);
4831 return got_error_from_errno("fread");
4835 if (fwrite(buf, 1, r, dst) != r)
4836 return got_ferror(dst, GOT_ERR_IO);
4839 if (s_nlines == 0 && *d_nlines == 0)
4843 * If commit info was in dst, increment line offsets
4844 * of the appended diff content, but skip s_lines[0]
4845 * because offset zero is already in *d_lines.
4847 if (*d_nlines > 0) {
4848 for (i = 1; i < s_nlines; ++i)
4849 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4857 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4859 /* d_lines is freed in close_diff_view() */
4860 return got_error_from_errno("reallocarray");
4865 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4866 *d_nlines += s_nlines;
4871 static const struct got_error *
4872 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4873 struct got_object_id *commit_id, struct got_reflist_head *refs,
4874 struct got_repository *repo, int ignore_ws, int force_text_diff,
4875 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4877 const struct got_error *err = NULL;
4878 char datebuf[26], *datestr;
4879 struct got_commit_object *commit;
4880 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4881 time_t committer_time;
4882 const char *author, *committer;
4883 char *refs_str = NULL;
4884 struct got_pathlist_entry *pe;
4889 err = build_refs_str(&refs_str, refs, commit_id, repo);
4894 err = got_object_open_as_commit(&commit, repo, commit_id);
4898 err = got_object_id_str(&id_str, commit_id);
4900 err = got_error_from_errno("got_object_id_str");
4904 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4908 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4909 refs_str ? refs_str : "", refs_str ? ")" : "");
4911 err = got_error_from_errno("fprintf");
4915 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4919 n = fprintf(outfile, "from: %s\n",
4920 got_object_commit_get_author(commit));
4922 err = got_error_from_errno("fprintf");
4926 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4930 author = got_object_commit_get_author(commit);
4931 committer = got_object_commit_get_committer(commit);
4932 if (strcmp(author, committer) != 0) {
4933 n = fprintf(outfile, "via: %s\n", committer);
4935 err = got_error_from_errno("fprintf");
4939 err = add_line_metadata(lines, nlines, outoff,
4940 GOT_DIFF_LINE_AUTHOR);
4944 committer_time = got_object_commit_get_committer_time(commit);
4945 datestr = get_datestr(&committer_time, datebuf);
4947 n = fprintf(outfile, "date: %s UTC\n", datestr);
4949 err = got_error_from_errno("fprintf");
4953 err = add_line_metadata(lines, nlines, outoff,
4954 GOT_DIFF_LINE_DATE);
4958 if (got_object_commit_get_nparents(commit) > 1) {
4959 const struct got_object_id_queue *parent_ids;
4960 struct got_object_qid *qid;
4962 parent_ids = got_object_commit_get_parent_ids(commit);
4963 STAILQ_FOREACH(qid, parent_ids, entry) {
4964 err = got_object_id_str(&id_str, &qid->id);
4967 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4969 err = got_error_from_errno("fprintf");
4973 err = add_line_metadata(lines, nlines, outoff,
4974 GOT_DIFF_LINE_META);
4982 err = got_object_commit_get_logmsg(&logmsg, commit);
4986 while ((line = strsep(&s, "\n")) != NULL) {
4987 n = fprintf(outfile, "%s\n", line);
4989 err = got_error_from_errno("fprintf");
4993 err = add_line_metadata(lines, nlines, outoff,
4994 GOT_DIFF_LINE_LOGMSG);
4999 TAILQ_FOREACH(pe, dsa->paths, entry) {
5000 struct got_diff_changed_path *cp = pe->data;
5001 int pad = dsa->max_path_len - pe->path_len + 1;
5003 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5004 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5005 dsa->rm_cols + 1, cp->rm);
5007 err = got_error_from_errno("fprintf");
5011 err = add_line_metadata(lines, nlines, outoff,
5012 GOT_DIFF_LINE_CHANGES);
5017 fputc('\n', outfile);
5019 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5023 n = fprintf(outfile,
5024 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5025 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5026 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5028 err = got_error_from_errno("fprintf");
5032 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5036 fputc('\n', outfile);
5038 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5043 got_object_commit_close(commit);
5052 static const struct got_error *
5053 create_diff(struct tog_diff_view_state *s)
5055 const struct got_error *err = NULL;
5056 FILE *f = NULL, *tmp_diff_file = NULL;
5058 struct got_diff_line *lines = NULL;
5059 struct got_pathlist_head changed_paths;
5061 TAILQ_INIT(&changed_paths);
5064 s->lines = malloc(sizeof(*s->lines));
5065 if (s->lines == NULL)
5066 return got_error_from_errno("malloc");
5071 err = got_error_from_errno("got_opentemp");
5074 tmp_diff_file = got_opentemp();
5075 if (tmp_diff_file == NULL) {
5076 err = got_error_from_errno("got_opentemp");
5079 if (s->f && fclose(s->f) == EOF) {
5080 err = got_error_from_errno("fclose");
5086 err = got_object_get_type(&obj_type, s->repo, s->id1);
5088 err = got_object_get_type(&obj_type, s->repo, s->id2);
5093 case GOT_OBJ_TYPE_BLOB:
5094 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5095 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5096 s->label1, s->label2, tog_diff_algo, s->diff_context,
5097 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5100 case GOT_OBJ_TYPE_TREE:
5101 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5102 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5103 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5104 s->force_text_diff, NULL, s->repo, s->f);
5106 case GOT_OBJ_TYPE_COMMIT: {
5107 const struct got_object_id_queue *parent_ids;
5108 struct got_object_qid *pid;
5109 struct got_commit_object *commit2;
5110 struct got_reflist_head *refs;
5112 struct got_diffstat_cb_arg dsa = {
5115 s->ignore_whitespace,
5120 lines = malloc(sizeof(*lines));
5121 if (lines == NULL) {
5122 err = got_error_from_errno("malloc");
5126 /* build diff first in tmp file then append to commit info */
5127 err = got_diff_objects_as_commits(&lines, &nlines,
5128 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5129 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5130 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5134 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5137 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5138 /* Show commit info if we're diffing to a parent/root commit. */
5139 if (s->id1 == NULL) {
5140 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5141 refs, s->repo, s->ignore_whitespace,
5142 s->force_text_diff, &dsa, s->f);
5146 parent_ids = got_object_commit_get_parent_ids(commit2);
5147 STAILQ_FOREACH(pid, parent_ids, entry) {
5148 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5149 err = write_commit_info(&s->lines,
5150 &s->nlines, s->id2, refs, s->repo,
5151 s->ignore_whitespace,
5152 s->force_text_diff, &dsa, s->f);
5159 got_object_commit_close(commit2);
5161 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5166 err = got_error(GOT_ERR_OBJ_TYPE);
5171 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5172 if (s->f && fflush(s->f) != 0 && err == NULL)
5173 err = got_error_from_errno("fflush");
5174 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5175 err = got_error_from_errno("fclose");
5180 diff_view_indicate_progress(struct tog_view *view)
5182 mvwaddstr(view->window, 0, 0, "diffing...");
5187 static const struct got_error *
5188 search_start_diff_view(struct tog_view *view)
5190 struct tog_diff_view_state *s = &view->state.diff;
5192 s->matched_line = 0;
5197 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5198 size_t *nlines, int **first, int **last, int **match, int **selected)
5200 struct tog_diff_view_state *s = &view->state.diff;
5203 *nlines = s->nlines;
5204 *line_offsets = NULL;
5205 *match = &s->matched_line;
5206 *first = &s->first_displayed_line;
5207 *last = &s->last_displayed_line;
5208 *selected = &s->selected_line;
5211 static const struct got_error *
5212 search_next_view_match(struct tog_view *view)
5214 const struct got_error *err = NULL;
5218 size_t linesize = 0;
5220 off_t *line_offsets;
5222 int *first, *last, *match, *selected;
5224 if (!view->search_setup)
5225 return got_error_msg(GOT_ERR_NOT_IMPL,
5226 "view search not supported");
5227 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5230 if (!view->searching) {
5231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 if (view->searching == TOG_SEARCH_FORWARD)
5237 lineno = *first + 1;
5239 lineno = *first - 1;
5241 lineno = *first - 1 + *selected;
5246 if (lineno <= 0 || lineno > nlines) {
5248 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5252 if (view->searching == TOG_SEARCH_FORWARD)
5258 offset = view->type == TOG_VIEW_DIFF ?
5259 view->state.diff.lines[lineno - 1].offset :
5260 line_offsets[lineno - 1];
5261 if (fseeko(f, offset, SEEK_SET) != 0) {
5263 return got_error_from_errno("fseeko");
5265 linelen = getline(&line, &linesize, f);
5266 if (linelen != -1) {
5268 err = expand_tab(&exstr, line);
5271 if (match_line(exstr, &view->regex, 1,
5273 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5280 if (view->searching == TOG_SEARCH_FORWARD)
5295 static const struct got_error *
5296 close_diff_view(struct tog_view *view)
5298 const struct got_error *err = NULL;
5299 struct tog_diff_view_state *s = &view->state.diff;
5305 if (s->f && fclose(s->f) == EOF)
5306 err = got_error_from_errno("fclose");
5308 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5309 err = got_error_from_errno("fclose");
5311 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5312 err = got_error_from_errno("fclose");
5314 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5315 err = got_error_from_errno("close");
5317 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5318 err = got_error_from_errno("close");
5326 static const struct got_error *
5327 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5328 struct got_object_id *id2, const char *label1, const char *label2,
5329 int diff_context, int ignore_whitespace, int force_text_diff,
5330 struct tog_view *parent_view, struct got_repository *repo)
5332 const struct got_error *err;
5333 struct tog_diff_view_state *s = &view->state.diff;
5335 memset(s, 0, sizeof(*s));
5339 if (id1 != NULL && id2 != NULL) {
5342 err = got_object_get_type(&type1, repo, id1);
5345 err = got_object_get_type(&type2, repo, id2);
5349 if (type1 != type2) {
5350 err = got_error(GOT_ERR_OBJ_TYPE);
5354 s->first_displayed_line = 1;
5355 s->last_displayed_line = view->nlines;
5356 s->selected_line = 1;
5364 s->id1 = got_object_id_dup(id1);
5365 if (s->id1 == NULL) {
5366 err = got_error_from_errno("got_object_id_dup");
5372 s->id2 = got_object_id_dup(id2);
5373 if (s->id2 == NULL) {
5374 err = got_error_from_errno("got_object_id_dup");
5378 s->f1 = got_opentemp();
5379 if (s->f1 == NULL) {
5380 err = got_error_from_errno("got_opentemp");
5384 s->f2 = got_opentemp();
5385 if (s->f2 == NULL) {
5386 err = got_error_from_errno("got_opentemp");
5390 s->fd1 = got_opentempfd();
5392 err = got_error_from_errno("got_opentempfd");
5396 s->fd2 = got_opentempfd();
5398 err = got_error_from_errno("got_opentempfd");
5402 s->diff_context = diff_context;
5403 s->ignore_whitespace = ignore_whitespace;
5404 s->force_text_diff = force_text_diff;
5405 s->parent_view = parent_view;
5408 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5411 rc = init_pair(GOT_DIFF_LINE_MINUS,
5412 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5414 rc = init_pair(GOT_DIFF_LINE_PLUS,
5415 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5417 rc = init_pair(GOT_DIFF_LINE_HUNK,
5418 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5420 rc = init_pair(GOT_DIFF_LINE_META,
5421 get_color_value("TOG_COLOR_DIFF_META"), -1);
5423 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5424 get_color_value("TOG_COLOR_DIFF_META"), -1);
5426 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5427 get_color_value("TOG_COLOR_DIFF_META"), -1);
5429 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5430 get_color_value("TOG_COLOR_DIFF_META"), -1);
5432 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5433 get_color_value("TOG_COLOR_AUTHOR"), -1);
5435 rc = init_pair(GOT_DIFF_LINE_DATE,
5436 get_color_value("TOG_COLOR_DATE"), -1);
5438 err = got_error(GOT_ERR_RANGE);
5443 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5444 view_is_splitscreen(view))
5445 show_log_view(parent_view); /* draw border */
5446 diff_view_indicate_progress(view);
5448 err = create_diff(s);
5450 view->show = show_diff_view;
5451 view->input = input_diff_view;
5452 view->reset = reset_diff_view;
5453 view->close = close_diff_view;
5454 view->search_start = search_start_diff_view;
5455 view->search_setup = search_setup_diff_view;
5456 view->search_next = search_next_view_match;
5459 if (view->close == NULL)
5460 close_diff_view(view);
5466 static const struct got_error *
5467 show_diff_view(struct tog_view *view)
5469 const struct got_error *err;
5470 struct tog_diff_view_state *s = &view->state.diff;
5471 char *id_str1 = NULL, *id_str2, *header;
5472 const char *label1, *label2;
5475 err = got_object_id_str(&id_str1, s->id1);
5478 label1 = s->label1 ? s->label1 : id_str1;
5480 label1 = "/dev/null";
5482 err = got_object_id_str(&id_str2, s->id2);
5485 label2 = s->label2 ? s->label2 : id_str2;
5487 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5488 err = got_error_from_errno("asprintf");
5496 err = draw_file(view, header);
5501 static const struct got_error *
5502 set_selected_commit(struct tog_diff_view_state *s,
5503 struct commit_queue_entry *entry)
5505 const struct got_error *err;
5506 const struct got_object_id_queue *parent_ids;
5507 struct got_commit_object *selected_commit;
5508 struct got_object_qid *pid;
5511 s->id2 = got_object_id_dup(entry->id);
5513 return got_error_from_errno("got_object_id_dup");
5515 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5518 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5520 pid = STAILQ_FIRST(parent_ids);
5521 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5522 got_object_commit_close(selected_commit);
5526 static const struct got_error *
5527 reset_diff_view(struct tog_view *view)
5529 struct tog_diff_view_state *s = &view->state.diff;
5532 wclear(view->window);
5533 s->first_displayed_line = 1;
5534 s->last_displayed_line = view->nlines;
5535 s->matched_line = 0;
5536 diff_view_indicate_progress(view);
5537 return create_diff(s);
5541 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5545 i = start = s->first_displayed_line - 1;
5547 while (s->lines[i].type != type) {
5551 return; /* do nothing, requested type not in file */
5554 s->selected_line = 1;
5555 s->first_displayed_line = i;
5559 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5563 i = start = s->first_displayed_line + 1;
5565 while (s->lines[i].type != type) {
5566 if (i == s->nlines - 1)
5569 return; /* do nothing, requested type not in file */
5572 s->selected_line = 1;
5573 s->first_displayed_line = i;
5576 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5578 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5581 static const struct got_error *
5582 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5584 const struct got_error *err = NULL;
5585 struct tog_diff_view_state *s = &view->state.diff;
5586 struct tog_log_view_state *ls;
5587 struct commit_queue_entry *old_selected_entry;
5589 size_t linesize = 0;
5591 int i, nscroll = view->nlines - 1, up = 0;
5593 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5602 horizontal_scroll_input(view, ch);
5607 s->force_text_diff = !s->force_text_diff;
5608 view->action = s->force_text_diff ?
5609 "force ASCII text enabled" :
5610 "force ASCII text disabled";
5612 else if (ch == 'w') {
5613 s->ignore_whitespace = !s->ignore_whitespace;
5614 view->action = s->ignore_whitespace ?
5615 "ignore whitespace enabled" :
5616 "ignore whitespace disabled";
5618 err = reset_diff_view(view);
5622 s->first_displayed_line = 1;
5631 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5637 if (s->first_displayed_line > 1)
5638 s->first_displayed_line--;
5649 if (s->first_displayed_line == 1) {
5654 while (i++ < nscroll && s->first_displayed_line > 1)
5655 s->first_displayed_line--;
5661 s->first_displayed_line++;
5678 while (!s->eof && i++ < nscroll) {
5679 linelen = getline(&line, &linesize, s->f);
5680 s->first_displayed_line++;
5681 if (linelen == -1) {
5685 err = got_ferror(s->f, GOT_ERR_IO);
5692 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5695 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5698 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5701 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5704 if (s->diff_context > 0) {
5706 s->matched_line = 0;
5707 diff_view_indicate_progress(view);
5708 err = create_diff(s);
5709 if (s->first_displayed_line + view->nlines - 1 >
5711 s->first_displayed_line = 1;
5712 s->last_displayed_line = view->nlines;
5718 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5720 s->matched_line = 0;
5721 diff_view_indicate_progress(view);
5722 err = create_diff(s);
5734 if (s->parent_view == NULL) {
5738 s->parent_view->count = view->count;
5740 if (s->parent_view->type == TOG_VIEW_LOG) {
5741 ls = &s->parent_view->state.log;
5742 old_selected_entry = ls->selected_entry;
5744 err = input_log_view(NULL, s->parent_view,
5745 up ? KEY_UP : KEY_DOWN);
5748 view->count = s->parent_view->count;
5750 if (old_selected_entry == ls->selected_entry)
5753 err = set_selected_commit(s, ls->selected_entry);
5756 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5757 struct tog_blame_view_state *bs;
5758 struct got_object_id *id, *prev_id;
5760 bs = &s->parent_view->state.blame;
5761 prev_id = get_annotation_for_line(bs->blame.lines,
5762 bs->blame.nlines, bs->last_diffed_line);
5764 err = input_blame_view(&view, s->parent_view,
5765 up ? KEY_UP : KEY_DOWN);
5768 view->count = s->parent_view->count;
5770 if (prev_id == NULL)
5772 id = get_selected_commit_id(bs->blame.lines,
5773 bs->blame.nlines, bs->first_displayed_line,
5778 if (!got_object_id_cmp(prev_id, id))
5781 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5785 s->first_displayed_line = 1;
5786 s->last_displayed_line = view->nlines;
5787 s->matched_line = 0;
5790 diff_view_indicate_progress(view);
5791 err = create_diff(s);
5801 static const struct got_error *
5802 cmd_diff(int argc, char *argv[])
5804 const struct got_error *error;
5805 struct got_repository *repo = NULL;
5806 struct got_worktree *worktree = NULL;
5807 struct got_object_id *id1 = NULL, *id2 = NULL;
5808 char *repo_path = NULL, *cwd = NULL;
5809 char *id_str1 = NULL, *id_str2 = NULL;
5810 char *label1 = NULL, *label2 = NULL;
5811 int diff_context = 3, ignore_whitespace = 0;
5812 int ch, force_text_diff = 0;
5814 struct tog_view *view;
5815 int *pack_fds = NULL;
5817 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5820 force_text_diff = 1;
5823 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5826 errx(1, "number of context lines is %s: %s",
5830 repo_path = realpath(optarg, NULL);
5831 if (repo_path == NULL)
5832 return got_error_from_errno2("realpath",
5834 got_path_strip_trailing_slashes(repo_path);
5837 ignore_whitespace = 1;
5849 usage_diff(); /* TODO show local worktree changes */
5850 } else if (argc == 2) {
5856 error = got_repo_pack_fds_open(&pack_fds);
5860 if (repo_path == NULL) {
5861 cwd = getcwd(NULL, 0);
5863 return got_error_from_errno("getcwd");
5864 error = got_worktree_open(&worktree, cwd);
5865 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5869 strdup(got_worktree_get_repo_path(worktree));
5871 repo_path = strdup(cwd);
5872 if (repo_path == NULL) {
5873 error = got_error_from_errno("strdup");
5878 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5884 error = apply_unveil(got_repo_get_path(repo), NULL);
5888 error = tog_load_refs(repo, 0);
5892 error = got_repo_match_object_id(&id1, &label1, id_str1,
5893 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5897 error = got_repo_match_object_id(&id2, &label2, id_str2,
5898 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5902 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5904 error = got_error_from_errno("view_open");
5907 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5908 ignore_whitespace, force_text_diff, NULL, repo);
5911 error = view_loop(view);
5918 const struct got_error *close_err = got_repo_close(repo);
5923 got_worktree_close(worktree);
5925 const struct got_error *pack_err =
5926 got_repo_pack_fds_close(pack_fds);
5939 "usage: %s blame [-c commit] [-r repository-path] path\n",
5944 struct tog_blame_line {
5946 struct got_object_id *id;
5949 static const struct got_error *
5950 draw_blame(struct tog_view *view)
5952 struct tog_blame_view_state *s = &view->state.blame;
5953 struct tog_blame *blame = &s->blame;
5954 regmatch_t *regmatch = &view->regmatch;
5955 const struct got_error *err;
5956 int lineno = 0, nprinted = 0;
5958 size_t linesize = 0;
5962 struct tog_blame_line *blame_line;
5963 struct got_object_id *prev_id = NULL;
5965 struct tog_color *tc;
5967 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5972 werase(view->window);
5974 if (asprintf(&line, "commit %s", id_str) == -1) {
5975 err = got_error_from_errno("asprintf");
5980 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5985 if (view_needs_focus_indication(view))
5986 wstandout(view->window);
5987 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5989 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5990 waddwstr(view->window, wline);
5991 while (width++ < view->ncols)
5992 waddch(view->window, ' ');
5994 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5995 if (view_needs_focus_indication(view))
5996 wstandend(view->window);
6000 if (view->gline > blame->nlines)
6001 view->gline = blame->nlines;
6003 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6004 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6005 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6007 return got_error_from_errno("asprintf");
6010 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6015 waddwstr(view->window, wline);
6018 if (width < view->ncols - 1)
6019 waddch(view->window, '\n');
6023 while (nprinted < view->nlines - 2) {
6024 linelen = getline(&line, &linesize, blame->f);
6025 if (linelen == -1) {
6026 if (feof(blame->f)) {
6031 return got_ferror(blame->f, GOT_ERR_IO);
6033 if (++lineno < s->first_displayed_line)
6035 if (view->gline && !gotoline(view, &lineno, &nprinted))
6038 /* Set view->maxx based on full line length. */
6039 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6046 view->maxx = MAX(view->maxx, width);
6048 if (nprinted == s->selected_line - 1)
6049 wstandout(view->window);
6051 if (blame->nlines > 0) {
6052 blame_line = &blame->lines[lineno - 1];
6053 if (blame_line->annotated && prev_id &&
6054 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6055 !(nprinted == s->selected_line - 1)) {
6056 waddstr(view->window, " ");
6057 } else if (blame_line->annotated) {
6059 err = got_object_id_str(&id_str,
6065 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6067 wattr_on(view->window,
6068 COLOR_PAIR(tc->colorpair), NULL);
6069 wprintw(view->window, "%.8s", id_str);
6071 wattr_off(view->window,
6072 COLOR_PAIR(tc->colorpair), NULL);
6074 prev_id = blame_line->id;
6076 waddstr(view->window, "........");
6080 waddstr(view->window, "........");
6084 if (nprinted == s->selected_line - 1)
6085 wstandend(view->window);
6086 waddstr(view->window, " ");
6088 if (view->ncols <= 9) {
6090 } else if (s->first_displayed_line + nprinted ==
6092 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6093 err = add_matched_line(&width, line, view->ncols - 9, 9,
6094 view->window, view->x, regmatch);
6102 err = format_line(&wline, &width, &skip, line,
6103 view->x, view->ncols - 9, 9, 1);
6108 waddwstr(view->window, &wline[skip]);
6114 if (width <= view->ncols - 1)
6115 waddch(view->window, '\n');
6116 if (++nprinted == 1)
6117 s->first_displayed_line = lineno;
6120 s->last_displayed_line = lineno;
6124 if (tog_io.wait_for_ui) {
6125 if (s->blame_complete)
6126 tog_io.wait_for_ui = 0;
6132 static const struct got_error *
6133 blame_cb(void *arg, int nlines, int lineno,
6134 struct got_commit_object *commit, struct got_object_id *id)
6136 const struct got_error *err = NULL;
6137 struct tog_blame_cb_args *a = arg;
6138 struct tog_blame_line *line;
6141 if (nlines != a->nlines ||
6142 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6143 return got_error(GOT_ERR_RANGE);
6145 errcode = pthread_mutex_lock(&tog_mutex);
6147 return got_error_set_errno(errcode, "pthread_mutex_lock");
6149 if (*a->quit) { /* user has quit the blame view */
6150 err = got_error(GOT_ERR_ITER_COMPLETED);
6155 goto done; /* no change in this commit */
6157 line = &a->lines[lineno - 1];
6158 if (line->annotated)
6161 line->id = got_object_id_dup(id);
6162 if (line->id == NULL) {
6163 err = got_error_from_errno("got_object_id_dup");
6166 line->annotated = 1;
6168 errcode = pthread_mutex_unlock(&tog_mutex);
6170 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6175 blame_thread(void *arg)
6177 const struct got_error *err, *close_err;
6178 struct tog_blame_thread_args *ta = arg;
6179 struct tog_blame_cb_args *a = ta->cb_args;
6180 int errcode, fd1 = -1, fd2 = -1;
6181 FILE *f1 = NULL, *f2 = NULL;
6183 fd1 = got_opentempfd();
6185 return (void *)got_error_from_errno("got_opentempfd");
6187 fd2 = got_opentempfd();
6189 err = got_error_from_errno("got_opentempfd");
6193 f1 = got_opentemp();
6195 err = (void *)got_error_from_errno("got_opentemp");
6198 f2 = got_opentemp();
6200 err = (void *)got_error_from_errno("got_opentemp");
6204 err = block_signals_used_by_main_thread();
6208 err = got_blame(ta->path, a->commit_id, ta->repo,
6209 tog_diff_algo, blame_cb, ta->cb_args,
6210 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6211 if (err && err->code == GOT_ERR_CANCELLED)
6214 errcode = pthread_mutex_lock(&tog_mutex);
6216 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6220 close_err = got_repo_close(ta->repo);
6226 errcode = pthread_mutex_unlock(&tog_mutex);
6227 if (errcode && err == NULL)
6228 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6231 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6232 err = got_error_from_errno("close");
6233 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6234 err = got_error_from_errno("close");
6235 if (f1 && fclose(f1) == EOF && err == NULL)
6236 err = got_error_from_errno("fclose");
6237 if (f2 && fclose(f2) == EOF && err == NULL)
6238 err = got_error_from_errno("fclose");
6243 static struct got_object_id *
6244 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6245 int first_displayed_line, int selected_line)
6247 struct tog_blame_line *line;
6252 line = &lines[first_displayed_line - 1 + selected_line - 1];
6253 if (!line->annotated)
6259 static struct got_object_id *
6260 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6263 struct tog_blame_line *line;
6265 if (nlines <= 0 || lineno >= nlines)
6268 line = &lines[lineno - 1];
6269 if (!line->annotated)
6275 static const struct got_error *
6276 stop_blame(struct tog_blame *blame)
6278 const struct got_error *err = NULL;
6281 if (blame->thread) {
6283 errcode = pthread_mutex_unlock(&tog_mutex);
6285 return got_error_set_errno(errcode,
6286 "pthread_mutex_unlock");
6287 errcode = pthread_join(blame->thread, (void **)&err);
6289 return got_error_set_errno(errcode, "pthread_join");
6290 errcode = pthread_mutex_lock(&tog_mutex);
6292 return got_error_set_errno(errcode,
6293 "pthread_mutex_lock");
6294 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6296 blame->thread = NULL;
6298 if (blame->thread_args.repo) {
6299 const struct got_error *close_err;
6300 close_err = got_repo_close(blame->thread_args.repo);
6303 blame->thread_args.repo = NULL;
6306 if (fclose(blame->f) == EOF && err == NULL)
6307 err = got_error_from_errno("fclose");
6311 for (i = 0; i < blame->nlines; i++)
6312 free(blame->lines[i].id);
6314 blame->lines = NULL;
6316 free(blame->cb_args.commit_id);
6317 blame->cb_args.commit_id = NULL;
6318 if (blame->pack_fds) {
6319 const struct got_error *pack_err =
6320 got_repo_pack_fds_close(blame->pack_fds);
6323 blame->pack_fds = NULL;
6328 static const struct got_error *
6329 cancel_blame_view(void *arg)
6331 const struct got_error *err = NULL;
6335 errcode = pthread_mutex_lock(&tog_mutex);
6337 return got_error_set_errno(errcode,
6338 "pthread_mutex_unlock");
6341 err = got_error(GOT_ERR_CANCELLED);
6343 errcode = pthread_mutex_unlock(&tog_mutex);
6345 return got_error_set_errno(errcode,
6346 "pthread_mutex_lock");
6351 static const struct got_error *
6352 run_blame(struct tog_view *view)
6354 struct tog_blame_view_state *s = &view->state.blame;
6355 struct tog_blame *blame = &s->blame;
6356 const struct got_error *err = NULL;
6357 struct got_commit_object *commit = NULL;
6358 struct got_blob_object *blob = NULL;
6359 struct got_repository *thread_repo = NULL;
6360 struct got_object_id *obj_id = NULL;
6361 int obj_type, fd = -1;
6362 int *pack_fds = NULL;
6364 err = got_object_open_as_commit(&commit, s->repo,
6365 &s->blamed_commit->id);
6369 fd = got_opentempfd();
6371 err = got_error_from_errno("got_opentempfd");
6375 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6379 err = got_object_get_type(&obj_type, s->repo, obj_id);
6383 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6384 err = got_error(GOT_ERR_OBJ_TYPE);
6388 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6391 blame->f = got_opentemp();
6392 if (blame->f == NULL) {
6393 err = got_error_from_errno("got_opentemp");
6396 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6397 &blame->line_offsets, blame->f, blob);
6400 if (blame->nlines == 0) {
6401 s->blame_complete = 1;
6405 /* Don't include \n at EOF in the blame line count. */
6406 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6409 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6410 if (blame->lines == NULL) {
6411 err = got_error_from_errno("calloc");
6415 err = got_repo_pack_fds_open(&pack_fds);
6418 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6423 blame->pack_fds = pack_fds;
6424 blame->cb_args.view = view;
6425 blame->cb_args.lines = blame->lines;
6426 blame->cb_args.nlines = blame->nlines;
6427 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6428 if (blame->cb_args.commit_id == NULL) {
6429 err = got_error_from_errno("got_object_id_dup");
6432 blame->cb_args.quit = &s->done;
6434 blame->thread_args.path = s->path;
6435 blame->thread_args.repo = thread_repo;
6436 blame->thread_args.cb_args = &blame->cb_args;
6437 blame->thread_args.complete = &s->blame_complete;
6438 blame->thread_args.cancel_cb = cancel_blame_view;
6439 blame->thread_args.cancel_arg = &s->done;
6440 s->blame_complete = 0;
6442 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6443 s->first_displayed_line = 1;
6444 s->last_displayed_line = view->nlines;
6445 s->selected_line = 1;
6447 s->matched_line = 0;
6451 got_object_commit_close(commit);
6452 if (fd != -1 && close(fd) == -1 && err == NULL)
6453 err = got_error_from_errno("close");
6455 got_object_blob_close(blob);
6462 static const struct got_error *
6463 open_blame_view(struct tog_view *view, char *path,
6464 struct got_object_id *commit_id, struct got_repository *repo)
6466 const struct got_error *err = NULL;
6467 struct tog_blame_view_state *s = &view->state.blame;
6469 STAILQ_INIT(&s->blamed_commits);
6471 s->path = strdup(path);
6472 if (s->path == NULL)
6473 return got_error_from_errno("strdup");
6475 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6481 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6482 s->first_displayed_line = 1;
6483 s->last_displayed_line = view->nlines;
6484 s->selected_line = 1;
6485 s->blame_complete = 0;
6487 s->commit_id = commit_id;
6488 memset(&s->blame, 0, sizeof(s->blame));
6490 STAILQ_INIT(&s->colors);
6491 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6492 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6493 get_color_value("TOG_COLOR_COMMIT"));
6498 view->show = show_blame_view;
6499 view->input = input_blame_view;
6500 view->reset = reset_blame_view;
6501 view->close = close_blame_view;
6502 view->search_start = search_start_blame_view;
6503 view->search_setup = search_setup_blame_view;
6504 view->search_next = search_next_view_match;
6506 return run_blame(view);
6509 static const struct got_error *
6510 close_blame_view(struct tog_view *view)
6512 const struct got_error *err = NULL;
6513 struct tog_blame_view_state *s = &view->state.blame;
6515 if (s->blame.thread)
6516 err = stop_blame(&s->blame);
6518 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6519 struct got_object_qid *blamed_commit;
6520 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6521 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6522 got_object_qid_free(blamed_commit);
6526 free_colors(&s->colors);
6530 static const struct got_error *
6531 search_start_blame_view(struct tog_view *view)
6533 struct tog_blame_view_state *s = &view->state.blame;
6535 s->matched_line = 0;
6540 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6541 size_t *nlines, int **first, int **last, int **match, int **selected)
6543 struct tog_blame_view_state *s = &view->state.blame;
6546 *nlines = s->blame.nlines;
6547 *line_offsets = s->blame.line_offsets;
6548 *match = &s->matched_line;
6549 *first = &s->first_displayed_line;
6550 *last = &s->last_displayed_line;
6551 *selected = &s->selected_line;
6554 static const struct got_error *
6555 show_blame_view(struct tog_view *view)
6557 const struct got_error *err = NULL;
6558 struct tog_blame_view_state *s = &view->state.blame;
6561 if (s->blame.thread == NULL && !s->blame_complete) {
6562 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6563 &s->blame.thread_args);
6565 return got_error_set_errno(errcode, "pthread_create");
6568 halfdelay(1); /* fast refresh while annotating */
6571 if (s->blame_complete && !using_mock_io)
6572 halfdelay(10); /* disable fast refresh */
6574 err = draw_blame(view);
6580 static const struct got_error *
6581 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6582 struct got_repository *repo, struct got_object_id *id)
6584 struct tog_view *log_view;
6585 const struct got_error *err = NULL;
6589 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6590 if (log_view == NULL)
6591 return got_error_from_errno("view_open");
6593 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6595 view_close(log_view);
6597 *new_view = log_view;
6602 static const struct got_error *
6603 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6605 const struct got_error *err = NULL, *thread_err = NULL;
6606 struct tog_view *diff_view;
6607 struct tog_blame_view_state *s = &view->state.blame;
6608 int eos, nscroll, begin_y = 0, begin_x = 0;
6610 eos = nscroll = view->nlines - 2;
6611 if (view_is_hsplit_top(view))
6621 horizontal_scroll_input(view, ch);
6628 s->selected_line = 1;
6629 s->first_displayed_line = 1;
6634 if (s->blame.nlines < eos) {
6635 s->selected_line = s->blame.nlines;
6636 s->first_displayed_line = 1;
6638 s->selected_line = eos;
6639 s->first_displayed_line = s->blame.nlines - (eos - 1);
6646 if (s->selected_line > 1)
6648 else if (s->selected_line == 1 &&
6649 s->first_displayed_line > 1)
6650 s->first_displayed_line--;
6661 if (s->first_displayed_line == 1) {
6662 if (view->count > 1)
6664 s->selected_line = MAX(1, s->selected_line - nscroll);
6668 if (s->first_displayed_line > nscroll)
6669 s->first_displayed_line -= nscroll;
6671 s->first_displayed_line = 1;
6676 if (s->selected_line < eos && s->first_displayed_line +
6677 s->selected_line <= s->blame.nlines)
6679 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6680 s->first_displayed_line++;
6686 struct got_object_id *id = NULL;
6689 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6690 s->first_displayed_line, s->selected_line);
6694 struct got_commit_object *commit, *pcommit;
6695 struct got_object_qid *pid;
6696 struct got_object_id *blob_id = NULL;
6698 err = got_object_open_as_commit(&commit,
6703 got_object_commit_get_parent_ids(commit));
6705 got_object_commit_close(commit);
6708 /* Check if path history ends here. */
6709 err = got_object_open_as_commit(&pcommit,
6713 err = got_object_id_by_path(&blob_id, s->repo,
6715 got_object_commit_close(pcommit);
6717 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6719 got_object_commit_close(commit);
6722 err = got_object_get_type(&obj_type, s->repo,
6725 /* Can't blame non-blob type objects. */
6726 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6727 got_object_commit_close(commit);
6730 err = got_object_qid_alloc(&s->blamed_commit,
6732 got_object_commit_close(commit);
6734 if (got_object_id_cmp(id,
6735 &s->blamed_commit->id) == 0)
6737 err = got_object_qid_alloc(&s->blamed_commit,
6743 thread_err = stop_blame(&s->blame);
6747 STAILQ_INSERT_HEAD(&s->blamed_commits,
6748 s->blamed_commit, entry);
6749 err = run_blame(view);
6755 struct got_object_qid *first;
6758 first = STAILQ_FIRST(&s->blamed_commits);
6759 if (!got_object_id_cmp(&first->id, s->commit_id))
6762 thread_err = stop_blame(&s->blame);
6766 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6767 got_object_qid_free(s->blamed_commit);
6769 STAILQ_FIRST(&s->blamed_commits);
6770 err = run_blame(view);
6777 s->id_to_log = get_selected_commit_id(s->blame.lines,
6778 s->blame.nlines, s->first_displayed_line, s->selected_line);
6780 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6784 struct got_object_id *id = NULL;
6785 struct got_object_qid *pid;
6786 struct got_commit_object *commit = NULL;
6789 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6790 s->first_displayed_line, s->selected_line);
6793 err = got_object_open_as_commit(&commit, s->repo, id);
6796 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6798 /* traversed from diff view, release diff resources */
6799 err = close_diff_view(*new_view);
6802 diff_view = *new_view;
6804 if (view_is_parent_view(view))
6805 view_get_split(view, &begin_y, &begin_x);
6807 diff_view = view_open(0, 0, begin_y, begin_x,
6809 if (diff_view == NULL) {
6810 got_object_commit_close(commit);
6811 err = got_error_from_errno("view_open");
6815 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6816 id, NULL, NULL, 3, 0, 0, view, s->repo);
6817 got_object_commit_close(commit);
6819 view_close(diff_view);
6822 s->last_diffed_line = s->first_displayed_line - 1 +
6825 break; /* still open from active diff view */
6826 if (view_is_parent_view(view) &&
6827 view->mode == TOG_VIEW_SPLIT_HRZN) {
6828 err = view_init_hsplit(view, begin_y);
6834 diff_view->focussed = 1;
6835 diff_view->mode = view->mode;
6836 diff_view->nlines = view->lines - begin_y;
6837 if (view_is_parent_view(view)) {
6838 view_transfer_size(diff_view, view);
6839 err = view_close_child(view);
6842 err = view_set_child(view, diff_view);
6845 view->focus_child = 1;
6847 *new_view = diff_view;
6860 if (s->last_displayed_line >= s->blame.nlines &&
6861 s->selected_line >= MIN(s->blame.nlines,
6862 view->nlines - 2)) {
6866 if (s->last_displayed_line >= s->blame.nlines &&
6867 s->selected_line < view->nlines - 2) {
6869 MIN(nscroll, s->last_displayed_line -
6870 s->first_displayed_line - s->selected_line + 1);
6872 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6873 s->first_displayed_line += nscroll;
6875 s->first_displayed_line =
6876 s->blame.nlines - (view->nlines - 3);
6879 if (s->selected_line > view->nlines - 2) {
6880 s->selected_line = MIN(s->blame.nlines,
6888 return thread_err ? thread_err : err;
6891 static const struct got_error *
6892 reset_blame_view(struct tog_view *view)
6894 const struct got_error *err;
6895 struct tog_blame_view_state *s = &view->state.blame;
6899 err = stop_blame(&s->blame);
6903 return run_blame(view);
6906 static const struct got_error *
6907 cmd_blame(int argc, char *argv[])
6909 const struct got_error *error;
6910 struct got_repository *repo = NULL;
6911 struct got_worktree *worktree = NULL;
6912 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6913 char *link_target = NULL;
6914 struct got_object_id *commit_id = NULL;
6915 struct got_commit_object *commit = NULL;
6916 char *commit_id_str = NULL;
6918 struct tog_view *view = NULL;
6919 int *pack_fds = NULL;
6921 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6924 commit_id_str = optarg;
6927 repo_path = realpath(optarg, NULL);
6928 if (repo_path == NULL)
6929 return got_error_from_errno2("realpath",
6944 error = got_repo_pack_fds_open(&pack_fds);
6948 if (repo_path == NULL) {
6949 cwd = getcwd(NULL, 0);
6951 return got_error_from_errno("getcwd");
6952 error = got_worktree_open(&worktree, cwd);
6953 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6957 strdup(got_worktree_get_repo_path(worktree));
6959 repo_path = strdup(cwd);
6960 if (repo_path == NULL) {
6961 error = got_error_from_errno("strdup");
6966 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6970 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6977 error = apply_unveil(got_repo_get_path(repo), NULL);
6981 error = tog_load_refs(repo, 0);
6985 if (commit_id_str == NULL) {
6986 struct got_reference *head_ref;
6987 error = got_ref_open(&head_ref, repo, worktree ?
6988 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6991 error = got_ref_resolve(&commit_id, repo, head_ref);
6992 got_ref_close(head_ref);
6994 error = got_repo_match_object_id(&commit_id, NULL,
6995 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7000 error = got_object_open_as_commit(&commit, repo, commit_id);
7004 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7009 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7011 error = got_error_from_errno("view_open");
7014 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7016 if (error != NULL) {
7017 if (view->close == NULL)
7018 close_blame_view(view);
7023 /* Release work tree lock. */
7024 got_worktree_close(worktree);
7027 error = view_loop(view);
7035 got_object_commit_close(commit);
7037 got_worktree_close(worktree);
7039 const struct got_error *close_err = got_repo_close(repo);
7044 const struct got_error *pack_err =
7045 got_repo_pack_fds_close(pack_fds);
7053 static const struct got_error *
7054 draw_tree_entries(struct tog_view *view, const char *parent_path)
7056 struct tog_tree_view_state *s = &view->state.tree;
7057 const struct got_error *err = NULL;
7058 struct got_tree_entry *te;
7061 struct tog_color *tc;
7062 int width, n, nentries, scrollx, i = 1;
7063 int limit = view->nlines;
7066 if (view_is_hsplit_top(view))
7067 --limit; /* border */
7069 werase(view->window);
7074 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7078 if (view_needs_focus_indication(view))
7079 wstandout(view->window);
7080 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7082 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7083 waddwstr(view->window, wline);
7086 while (width++ < view->ncols)
7087 waddch(view->window, ' ');
7089 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7090 if (view_needs_focus_indication(view))
7091 wstandend(view->window);
7096 if (s->first_displayed_entry) {
7097 i += got_tree_entry_get_index(s->first_displayed_entry);
7098 if (s->tree != s->root)
7099 ++i; /* account for ".." entry */
7101 nentries = got_object_tree_get_nentries(s->tree);
7102 if (asprintf(&index, "[%d/%d] %s",
7103 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7104 return got_error_from_errno("asprintf");
7105 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7109 waddwstr(view->window, wline);
7112 if (width < view->ncols - 1)
7113 waddch(view->window, '\n');
7116 waddch(view->window, '\n');
7120 if (s->first_displayed_entry == NULL) {
7121 te = got_object_tree_get_first_entry(s->tree);
7122 if (s->selected == 0) {
7124 wstandout(view->window);
7125 s->selected_entry = NULL;
7127 waddstr(view->window, " ..\n"); /* parent directory */
7128 if (s->selected == 0 && view->focussed)
7129 wstandend(view->window);
7136 te = s->first_displayed_entry;
7140 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7141 char *line = NULL, *id_str = NULL, *link_target = NULL;
7142 const char *modestr = "";
7145 te = got_object_tree_get_entry(s->tree, i);
7146 mode = got_tree_entry_get_mode(te);
7149 err = got_object_id_str(&id_str,
7150 got_tree_entry_get_id(te));
7152 return got_error_from_errno(
7153 "got_object_id_str");
7155 if (got_object_tree_entry_is_submodule(te))
7157 else if (S_ISLNK(mode)) {
7160 err = got_tree_entry_get_symlink_target(&link_target,
7166 for (i = 0; i < strlen(link_target); i++) {
7167 if (!isprint((unsigned char)link_target[i]))
7168 link_target[i] = '?';
7172 else if (S_ISDIR(mode))
7174 else if (mode & S_IXUSR)
7176 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7177 got_tree_entry_get_name(te), modestr,
7178 link_target ? " -> ": "",
7179 link_target ? link_target : "") == -1) {
7182 return got_error_from_errno("asprintf");
7187 /* use full line width to determine view->maxx */
7188 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7193 view->maxx = MAX(view->maxx, width);
7197 err = format_line(&wline, &width, &scrollx, line, view->x,
7203 if (n == s->selected) {
7205 wstandout(view->window);
7206 s->selected_entry = te;
7208 tc = match_color(&s->colors, line);
7210 wattr_on(view->window,
7211 COLOR_PAIR(tc->colorpair), NULL);
7212 waddwstr(view->window, &wline[scrollx]);
7214 wattr_off(view->window,
7215 COLOR_PAIR(tc->colorpair), NULL);
7216 if (width < view->ncols)
7217 waddch(view->window, '\n');
7218 if (n == s->selected && view->focussed)
7219 wstandend(view->window);
7225 s->last_displayed_entry = te;
7234 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7236 struct got_tree_entry *te;
7237 int isroot = s->tree == s->root;
7240 if (s->first_displayed_entry == NULL)
7243 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7244 while (i++ < maxscroll) {
7247 s->first_displayed_entry = NULL;
7250 s->first_displayed_entry = te;
7251 te = got_tree_entry_get_prev(s->tree, te);
7255 static const struct got_error *
7256 tree_scroll_down(struct tog_view *view, int maxscroll)
7258 struct tog_tree_view_state *s = &view->state.tree;
7259 struct got_tree_entry *next, *last;
7262 if (s->first_displayed_entry)
7263 next = got_tree_entry_get_next(s->tree,
7264 s->first_displayed_entry);
7266 next = got_object_tree_get_first_entry(s->tree);
7268 last = s->last_displayed_entry;
7269 while (next && n++ < maxscroll) {
7271 s->last_displayed_entry = last;
7272 last = got_tree_entry_get_next(s->tree, last);
7274 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7275 s->first_displayed_entry = next;
7276 next = got_tree_entry_get_next(s->tree, next);
7283 static const struct got_error *
7284 tree_entry_path(char **path, struct tog_parent_trees *parents,
7285 struct got_tree_entry *te)
7287 const struct got_error *err = NULL;
7288 struct tog_parent_tree *pt;
7289 size_t len = 2; /* for leading slash and NUL */
7291 TAILQ_FOREACH(pt, parents, entry)
7292 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7295 len += strlen(got_tree_entry_get_name(te));
7297 *path = calloc(1, len);
7299 return got_error_from_errno("calloc");
7302 pt = TAILQ_LAST(parents, tog_parent_trees);
7304 const char *name = got_tree_entry_get_name(pt->selected_entry);
7305 if (strlcat(*path, name, len) >= len) {
7306 err = got_error(GOT_ERR_NO_SPACE);
7309 if (strlcat(*path, "/", len) >= len) {
7310 err = got_error(GOT_ERR_NO_SPACE);
7313 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7316 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7317 err = got_error(GOT_ERR_NO_SPACE);
7329 static const struct got_error *
7330 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7331 struct got_tree_entry *te, struct tog_parent_trees *parents,
7332 struct got_object_id *commit_id, struct got_repository *repo)
7334 const struct got_error *err = NULL;
7336 struct tog_view *blame_view;
7340 err = tree_entry_path(&path, parents, te);
7344 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7345 if (blame_view == NULL) {
7346 err = got_error_from_errno("view_open");
7350 err = open_blame_view(blame_view, path, commit_id, repo);
7352 if (err->code == GOT_ERR_CANCELLED)
7354 view_close(blame_view);
7356 *new_view = blame_view;
7362 static const struct got_error *
7363 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7364 struct tog_tree_view_state *s)
7366 struct tog_view *log_view;
7367 const struct got_error *err = NULL;
7372 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7373 if (log_view == NULL)
7374 return got_error_from_errno("view_open");
7376 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7380 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7383 view_close(log_view);
7385 *new_view = log_view;
7390 static const struct got_error *
7391 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7392 const char *head_ref_name, struct got_repository *repo)
7394 const struct got_error *err = NULL;
7395 char *commit_id_str = NULL;
7396 struct tog_tree_view_state *s = &view->state.tree;
7397 struct got_commit_object *commit = NULL;
7399 TAILQ_INIT(&s->parents);
7400 STAILQ_INIT(&s->colors);
7402 s->commit_id = got_object_id_dup(commit_id);
7403 if (s->commit_id == NULL) {
7404 err = got_error_from_errno("got_object_id_dup");
7408 err = got_object_open_as_commit(&commit, repo, commit_id);
7413 * The root is opened here and will be closed when the view is closed.
7414 * Any visited subtrees and their path-wise parents are opened and
7417 err = got_object_open_as_tree(&s->root, repo,
7418 got_object_commit_get_tree_id(commit));
7423 err = got_object_id_str(&commit_id_str, commit_id);
7427 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7428 err = got_error_from_errno("asprintf");
7432 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7433 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7434 if (head_ref_name) {
7435 s->head_ref_name = strdup(head_ref_name);
7436 if (s->head_ref_name == NULL) {
7437 err = got_error_from_errno("strdup");
7443 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7444 err = add_color(&s->colors, "\\$$",
7445 TOG_COLOR_TREE_SUBMODULE,
7446 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7449 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7450 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7453 err = add_color(&s->colors, "/$",
7454 TOG_COLOR_TREE_DIRECTORY,
7455 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7459 err = add_color(&s->colors, "\\*$",
7460 TOG_COLOR_TREE_EXECUTABLE,
7461 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7465 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7466 get_color_value("TOG_COLOR_COMMIT"));
7471 view->show = show_tree_view;
7472 view->input = input_tree_view;
7473 view->close = close_tree_view;
7474 view->search_start = search_start_tree_view;
7475 view->search_next = search_next_tree_view;
7477 free(commit_id_str);
7479 got_object_commit_close(commit);
7481 if (view->close == NULL)
7482 close_tree_view(view);
7488 static const struct got_error *
7489 close_tree_view(struct tog_view *view)
7491 struct tog_tree_view_state *s = &view->state.tree;
7493 free_colors(&s->colors);
7494 free(s->tree_label);
7495 s->tree_label = NULL;
7497 s->commit_id = NULL;
7498 free(s->head_ref_name);
7499 s->head_ref_name = NULL;
7500 while (!TAILQ_EMPTY(&s->parents)) {
7501 struct tog_parent_tree *parent;
7502 parent = TAILQ_FIRST(&s->parents);
7503 TAILQ_REMOVE(&s->parents, parent, entry);
7504 if (parent->tree != s->root)
7505 got_object_tree_close(parent->tree);
7509 if (s->tree != NULL && s->tree != s->root)
7510 got_object_tree_close(s->tree);
7512 got_object_tree_close(s->root);
7516 static const struct got_error *
7517 search_start_tree_view(struct tog_view *view)
7519 struct tog_tree_view_state *s = &view->state.tree;
7521 s->matched_entry = NULL;
7526 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7528 regmatch_t regmatch;
7530 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7534 static const struct got_error *
7535 search_next_tree_view(struct tog_view *view)
7537 struct tog_tree_view_state *s = &view->state.tree;
7538 struct got_tree_entry *te = NULL;
7540 if (!view->searching) {
7541 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7545 if (s->matched_entry) {
7546 if (view->searching == TOG_SEARCH_FORWARD) {
7547 if (s->selected_entry)
7548 te = got_tree_entry_get_next(s->tree,
7551 te = got_object_tree_get_first_entry(s->tree);
7553 if (s->selected_entry == NULL)
7554 te = got_object_tree_get_last_entry(s->tree);
7556 te = got_tree_entry_get_prev(s->tree,
7560 if (s->selected_entry)
7561 te = s->selected_entry;
7562 else if (view->searching == TOG_SEARCH_FORWARD)
7563 te = got_object_tree_get_first_entry(s->tree);
7565 te = got_object_tree_get_last_entry(s->tree);
7570 if (s->matched_entry == NULL) {
7571 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7574 if (view->searching == TOG_SEARCH_FORWARD)
7575 te = got_object_tree_get_first_entry(s->tree);
7577 te = got_object_tree_get_last_entry(s->tree);
7580 if (match_tree_entry(te, &view->regex)) {
7581 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7582 s->matched_entry = te;
7586 if (view->searching == TOG_SEARCH_FORWARD)
7587 te = got_tree_entry_get_next(s->tree, te);
7589 te = got_tree_entry_get_prev(s->tree, te);
7592 if (s->matched_entry) {
7593 s->first_displayed_entry = s->matched_entry;
7600 static const struct got_error *
7601 show_tree_view(struct tog_view *view)
7603 const struct got_error *err = NULL;
7604 struct tog_tree_view_state *s = &view->state.tree;
7607 err = tree_entry_path(&parent_path, &s->parents, NULL);
7611 err = draw_tree_entries(view, parent_path);
7618 static const struct got_error *
7619 tree_goto_line(struct tog_view *view, int nlines)
7621 const struct got_error *err = NULL;
7622 struct tog_tree_view_state *s = &view->state.tree;
7623 struct got_tree_entry **fte, **lte, **ste;
7624 int g, last, first = 1, i = 1;
7625 int root = s->tree == s->root;
7626 int off = root ? 1 : 2;
7633 else if (g > got_object_tree_get_nentries(s->tree))
7634 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7636 fte = &s->first_displayed_entry;
7637 lte = &s->last_displayed_entry;
7638 ste = &s->selected_entry;
7641 first = got_tree_entry_get_index(*fte);
7642 first += off; /* account for ".." */
7644 last = got_tree_entry_get_index(*lte);
7647 if (g >= first && g <= last && g - first < nlines) {
7648 s->selected = g - first;
7649 return NULL; /* gline is on the current page */
7653 i = got_tree_entry_get_index(*ste);
7658 err = tree_scroll_down(view, g - i);
7661 if (got_tree_entry_get_index(*lte) >=
7662 got_object_tree_get_nentries(s->tree) - 1 &&
7663 first + s->selected < g &&
7664 s->selected < s->ndisplayed - 1) {
7665 first = got_tree_entry_get_index(*fte);
7667 s->selected = g - first;
7670 tree_scroll_up(s, i - g);
7673 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7674 s->selected = g - 1;
7679 static const struct got_error *
7680 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7682 const struct got_error *err = NULL;
7683 struct tog_tree_view_state *s = &view->state.tree;
7684 struct got_tree_entry *te;
7685 int n, nscroll = view->nlines - 3;
7688 return tree_goto_line(view, nscroll);
7697 horizontal_scroll_input(view, ch);
7700 s->show_ids = !s->show_ids;
7705 if (!s->selected_entry)
7707 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7711 err = view_request_new(new_view, view, TOG_VIEW_REF);
7718 if (s->tree == s->root)
7719 s->first_displayed_entry =
7720 got_object_tree_get_first_entry(s->tree);
7722 s->first_displayed_entry = NULL;
7727 int eos = view->nlines - 3;
7729 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7733 te = got_object_tree_get_last_entry(s->tree);
7734 for (n = 0; n < eos; n++) {
7736 if (s->tree != s->root) {
7737 s->first_displayed_entry = NULL;
7742 s->first_displayed_entry = te;
7743 te = got_tree_entry_get_prev(s->tree, te);
7746 s->selected = n - 1;
7752 if (s->selected > 0) {
7756 tree_scroll_up(s, 1);
7757 if (s->selected_entry == NULL ||
7758 (s->tree == s->root && s->selected_entry ==
7759 got_object_tree_get_first_entry(s->tree)))
7769 if (s->tree == s->root) {
7770 if (got_object_tree_get_first_entry(s->tree) ==
7771 s->first_displayed_entry)
7772 s->selected -= MIN(s->selected, nscroll);
7774 if (s->first_displayed_entry == NULL)
7775 s->selected -= MIN(s->selected, nscroll);
7777 tree_scroll_up(s, MAX(0, nscroll));
7778 if (s->selected_entry == NULL ||
7779 (s->tree == s->root && s->selected_entry ==
7780 got_object_tree_get_first_entry(s->tree)))
7786 if (s->selected < s->ndisplayed - 1) {
7790 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7792 /* can't scroll any further */
7796 tree_scroll_down(view, 1);
7806 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7808 /* can't scroll any further; move cursor down */
7809 if (s->selected < s->ndisplayed - 1)
7810 s->selected += MIN(nscroll,
7811 s->ndisplayed - s->selected - 1);
7816 tree_scroll_down(view, nscroll);
7821 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7822 struct tog_parent_tree *parent;
7823 /* user selected '..' */
7824 if (s->tree == s->root) {
7828 parent = TAILQ_FIRST(&s->parents);
7829 TAILQ_REMOVE(&s->parents, parent,
7831 got_object_tree_close(s->tree);
7832 s->tree = parent->tree;
7833 s->first_displayed_entry =
7834 parent->first_displayed_entry;
7836 parent->selected_entry;
7837 s->selected = parent->selected;
7838 if (s->selected > view->nlines - 3) {
7839 err = offset_selection_down(view);
7844 } else if (S_ISDIR(got_tree_entry_get_mode(
7845 s->selected_entry))) {
7846 struct got_tree_object *subtree;
7848 err = got_object_open_as_tree(&subtree, s->repo,
7849 got_tree_entry_get_id(s->selected_entry));
7852 err = tree_view_visit_subtree(s, subtree);
7854 got_object_tree_close(subtree);
7857 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7858 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7861 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7862 s->selected = view->nlines - 4;
7878 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7883 static const struct got_error *
7884 cmd_tree(int argc, char *argv[])
7886 const struct got_error *error;
7887 struct got_repository *repo = NULL;
7888 struct got_worktree *worktree = NULL;
7889 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7890 struct got_object_id *commit_id = NULL;
7891 struct got_commit_object *commit = NULL;
7892 const char *commit_id_arg = NULL;
7894 struct got_reference *ref = NULL;
7895 const char *head_ref_name = NULL;
7897 struct tog_view *view;
7898 int *pack_fds = NULL;
7900 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7903 commit_id_arg = optarg;
7906 repo_path = realpath(optarg, NULL);
7907 if (repo_path == NULL)
7908 return got_error_from_errno2("realpath",
7923 error = got_repo_pack_fds_open(&pack_fds);
7927 if (repo_path == NULL) {
7928 cwd = getcwd(NULL, 0);
7930 return got_error_from_errno("getcwd");
7931 error = got_worktree_open(&worktree, cwd);
7932 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7936 strdup(got_worktree_get_repo_path(worktree));
7938 repo_path = strdup(cwd);
7939 if (repo_path == NULL) {
7940 error = got_error_from_errno("strdup");
7945 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7949 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7956 error = apply_unveil(got_repo_get_path(repo), NULL);
7960 error = tog_load_refs(repo, 0);
7964 if (commit_id_arg == NULL) {
7965 error = got_repo_match_object_id(&commit_id, &label,
7966 worktree ? got_worktree_get_head_ref_name(worktree) :
7967 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7970 head_ref_name = label;
7972 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7974 head_ref_name = got_ref_get_name(ref);
7975 else if (error->code != GOT_ERR_NOT_REF)
7977 error = got_repo_match_object_id(&commit_id, NULL,
7978 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7983 error = got_object_open_as_commit(&commit, repo, commit_id);
7987 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7989 error = got_error_from_errno("view_open");
7992 error = open_tree_view(view, commit_id, head_ref_name, repo);
7995 if (!got_path_is_root_dir(in_repo_path)) {
7996 error = tree_view_walk_path(&view->state.tree, commit,
8003 /* Release work tree lock. */
8004 got_worktree_close(worktree);
8007 error = view_loop(view);
8016 const struct got_error *close_err = got_repo_close(repo);
8021 const struct got_error *pack_err =
8022 got_repo_pack_fds_close(pack_fds);
8030 static const struct got_error *
8031 ref_view_load_refs(struct tog_ref_view_state *s)
8033 struct got_reflist_entry *sre;
8034 struct tog_reflist_entry *re;
8037 TAILQ_FOREACH(sre, &tog_refs, entry) {
8038 if (strncmp(got_ref_get_name(sre->ref),
8039 "refs/got/", 9) == 0 &&
8040 strncmp(got_ref_get_name(sre->ref),
8041 "refs/got/backup/", 16) != 0)
8044 re = malloc(sizeof(*re));
8046 return got_error_from_errno("malloc");
8048 re->ref = got_ref_dup(sre->ref);
8049 if (re->ref == NULL)
8050 return got_error_from_errno("got_ref_dup");
8051 re->idx = s->nrefs++;
8052 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8055 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8060 ref_view_free_refs(struct tog_ref_view_state *s)
8062 struct tog_reflist_entry *re;
8064 while (!TAILQ_EMPTY(&s->refs)) {
8065 re = TAILQ_FIRST(&s->refs);
8066 TAILQ_REMOVE(&s->refs, re, entry);
8067 got_ref_close(re->ref);
8072 static const struct got_error *
8073 open_ref_view(struct tog_view *view, struct got_repository *repo)
8075 const struct got_error *err = NULL;
8076 struct tog_ref_view_state *s = &view->state.ref;
8078 s->selected_entry = 0;
8081 TAILQ_INIT(&s->refs);
8082 STAILQ_INIT(&s->colors);
8084 err = ref_view_load_refs(s);
8088 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8089 err = add_color(&s->colors, "^refs/heads/",
8090 TOG_COLOR_REFS_HEADS,
8091 get_color_value("TOG_COLOR_REFS_HEADS"));
8095 err = add_color(&s->colors, "^refs/tags/",
8096 TOG_COLOR_REFS_TAGS,
8097 get_color_value("TOG_COLOR_REFS_TAGS"));
8101 err = add_color(&s->colors, "^refs/remotes/",
8102 TOG_COLOR_REFS_REMOTES,
8103 get_color_value("TOG_COLOR_REFS_REMOTES"));
8107 err = add_color(&s->colors, "^refs/got/backup/",
8108 TOG_COLOR_REFS_BACKUP,
8109 get_color_value("TOG_COLOR_REFS_BACKUP"));
8114 view->show = show_ref_view;
8115 view->input = input_ref_view;
8116 view->close = close_ref_view;
8117 view->search_start = search_start_ref_view;
8118 view->search_next = search_next_ref_view;
8121 if (view->close == NULL)
8122 close_ref_view(view);
8128 static const struct got_error *
8129 close_ref_view(struct tog_view *view)
8131 struct tog_ref_view_state *s = &view->state.ref;
8133 ref_view_free_refs(s);
8134 free_colors(&s->colors);
8139 static const struct got_error *
8140 resolve_reflist_entry(struct got_object_id **commit_id,
8141 struct tog_reflist_entry *re, struct got_repository *repo)
8143 const struct got_error *err = NULL;
8144 struct got_object_id *obj_id;
8145 struct got_tag_object *tag = NULL;
8150 err = got_ref_resolve(&obj_id, repo, re->ref);
8154 err = got_object_get_type(&obj_type, repo, obj_id);
8159 case GOT_OBJ_TYPE_COMMIT:
8160 *commit_id = obj_id;
8162 case GOT_OBJ_TYPE_TAG:
8163 err = got_object_open_as_tag(&tag, repo, obj_id);
8167 err = got_object_get_type(&obj_type, repo,
8168 got_object_tag_get_object_id(tag));
8171 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8172 err = got_error(GOT_ERR_OBJ_TYPE);
8175 *commit_id = got_object_id_dup(
8176 got_object_tag_get_object_id(tag));
8177 if (*commit_id == NULL) {
8178 err = got_error_from_errno("got_object_id_dup");
8183 err = got_error(GOT_ERR_OBJ_TYPE);
8189 got_object_tag_close(tag);
8197 static const struct got_error *
8198 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8199 struct tog_reflist_entry *re, struct got_repository *repo)
8201 struct tog_view *log_view;
8202 const struct got_error *err = NULL;
8203 struct got_object_id *commit_id = NULL;
8207 err = resolve_reflist_entry(&commit_id, re, repo);
8209 if (err->code != GOT_ERR_OBJ_TYPE)
8215 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8216 if (log_view == NULL) {
8217 err = got_error_from_errno("view_open");
8221 err = open_log_view(log_view, commit_id, repo,
8222 got_ref_get_name(re->ref), "", 0);
8225 view_close(log_view);
8227 *new_view = log_view;
8233 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8235 struct tog_reflist_entry *re;
8238 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8241 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8242 while (i++ < maxscroll) {
8245 s->first_displayed_entry = re;
8246 re = TAILQ_PREV(re, tog_reflist_head, entry);
8250 static const struct got_error *
8251 ref_scroll_down(struct tog_view *view, int maxscroll)
8253 struct tog_ref_view_state *s = &view->state.ref;
8254 struct tog_reflist_entry *next, *last;
8257 if (s->first_displayed_entry)
8258 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8260 next = TAILQ_FIRST(&s->refs);
8262 last = s->last_displayed_entry;
8263 while (next && n++ < maxscroll) {
8265 s->last_displayed_entry = last;
8266 last = TAILQ_NEXT(last, entry);
8268 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8269 s->first_displayed_entry = next;
8270 next = TAILQ_NEXT(next, entry);
8277 static const struct got_error *
8278 search_start_ref_view(struct tog_view *view)
8280 struct tog_ref_view_state *s = &view->state.ref;
8282 s->matched_entry = NULL;
8287 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8289 regmatch_t regmatch;
8291 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8295 static const struct got_error *
8296 search_next_ref_view(struct tog_view *view)
8298 struct tog_ref_view_state *s = &view->state.ref;
8299 struct tog_reflist_entry *re = NULL;
8301 if (!view->searching) {
8302 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8306 if (s->matched_entry) {
8307 if (view->searching == TOG_SEARCH_FORWARD) {
8308 if (s->selected_entry)
8309 re = TAILQ_NEXT(s->selected_entry, entry);
8311 re = TAILQ_PREV(s->selected_entry,
8312 tog_reflist_head, entry);
8314 if (s->selected_entry == NULL)
8315 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8317 re = TAILQ_PREV(s->selected_entry,
8318 tog_reflist_head, entry);
8321 if (s->selected_entry)
8322 re = s->selected_entry;
8323 else if (view->searching == TOG_SEARCH_FORWARD)
8324 re = TAILQ_FIRST(&s->refs);
8326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8331 if (s->matched_entry == NULL) {
8332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8335 if (view->searching == TOG_SEARCH_FORWARD)
8336 re = TAILQ_FIRST(&s->refs);
8338 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8341 if (match_reflist_entry(re, &view->regex)) {
8342 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8343 s->matched_entry = re;
8347 if (view->searching == TOG_SEARCH_FORWARD)
8348 re = TAILQ_NEXT(re, entry);
8350 re = TAILQ_PREV(re, tog_reflist_head, entry);
8353 if (s->matched_entry) {
8354 s->first_displayed_entry = s->matched_entry;
8361 static const struct got_error *
8362 show_ref_view(struct tog_view *view)
8364 const struct got_error *err = NULL;
8365 struct tog_ref_view_state *s = &view->state.ref;
8366 struct tog_reflist_entry *re;
8369 struct tog_color *tc;
8370 int width, n, scrollx;
8371 int limit = view->nlines;
8373 werase(view->window);
8376 if (view_is_hsplit_top(view))
8377 --limit; /* border */
8382 re = s->first_displayed_entry;
8384 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8386 return got_error_from_errno("asprintf");
8388 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8393 if (view_needs_focus_indication(view))
8394 wstandout(view->window);
8395 waddwstr(view->window, wline);
8396 while (width++ < view->ncols)
8397 waddch(view->window, ' ');
8398 if (view_needs_focus_indication(view))
8399 wstandend(view->window);
8409 while (re && limit > 0) {
8411 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8414 struct got_commit_object *ci;
8415 struct got_tag_object *tag;
8416 struct got_object_id *id;
8420 err = got_ref_resolve(&id, s->repo, re->ref);
8423 err = got_object_open_as_tag(&tag, s->repo, id);
8425 if (err->code != GOT_ERR_OBJ_TYPE) {
8429 err = got_object_open_as_commit(&ci, s->repo,
8435 t = got_object_commit_get_committer_time(ci);
8436 got_object_commit_close(ci);
8438 t = got_object_tag_get_tagger_time(tag);
8439 got_object_tag_close(tag);
8442 if (gmtime_r(&t, &tm) == NULL)
8443 return got_error_from_errno("gmtime_r");
8444 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8445 return got_error(GOT_ERR_NO_SPACE);
8447 if (got_ref_is_symbolic(re->ref)) {
8448 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8449 ymd : "", got_ref_get_name(re->ref),
8450 got_ref_get_symref_target(re->ref)) == -1)
8451 return got_error_from_errno("asprintf");
8452 } else if (s->show_ids) {
8453 struct got_object_id *id;
8455 err = got_ref_resolve(&id, s->repo, re->ref);
8458 err = got_object_id_str(&id_str, id);
8463 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8464 got_ref_get_name(re->ref), id_str) == -1) {
8465 err = got_error_from_errno("asprintf");
8472 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8473 got_ref_get_name(re->ref)) == -1)
8474 return got_error_from_errno("asprintf");
8476 /* use full line width to determine view->maxx */
8477 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8482 view->maxx = MAX(view->maxx, width);
8486 err = format_line(&wline, &width, &scrollx, line, view->x,
8492 if (n == s->selected) {
8494 wstandout(view->window);
8495 s->selected_entry = re;
8497 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8499 wattr_on(view->window,
8500 COLOR_PAIR(tc->colorpair), NULL);
8501 waddwstr(view->window, &wline[scrollx]);
8503 wattr_off(view->window,
8504 COLOR_PAIR(tc->colorpair), NULL);
8505 if (width < view->ncols)
8506 waddch(view->window, '\n');
8507 if (n == s->selected && view->focussed)
8508 wstandend(view->window);
8514 s->last_displayed_entry = re;
8517 re = TAILQ_NEXT(re, entry);
8524 static const struct got_error *
8525 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8526 struct tog_reflist_entry *re, struct got_repository *repo)
8528 const struct got_error *err = NULL;
8529 struct got_object_id *commit_id = NULL;
8530 struct tog_view *tree_view;
8534 err = resolve_reflist_entry(&commit_id, re, repo);
8536 if (err->code != GOT_ERR_OBJ_TYPE)
8543 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8544 if (tree_view == NULL) {
8545 err = got_error_from_errno("view_open");
8549 err = open_tree_view(tree_view, commit_id,
8550 got_ref_get_name(re->ref), repo);
8554 *new_view = tree_view;
8560 static const struct got_error *
8561 ref_goto_line(struct tog_view *view, int nlines)
8563 const struct got_error *err = NULL;
8564 struct tog_ref_view_state *s = &view->state.ref;
8565 int g, idx = s->selected_entry->idx;
8572 else if (g > s->nrefs)
8575 if (g >= s->first_displayed_entry->idx + 1 &&
8576 g <= s->last_displayed_entry->idx + 1 &&
8577 g - s->first_displayed_entry->idx - 1 < nlines) {
8578 s->selected = g - s->first_displayed_entry->idx - 1;
8583 err = ref_scroll_down(view, g - idx - 1);
8586 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8587 s->first_displayed_entry->idx + s->selected < g &&
8588 s->selected < s->ndisplayed - 1)
8589 s->selected = g - s->first_displayed_entry->idx - 1;
8590 } else if (idx + 1 > g)
8591 ref_scroll_up(s, idx - g + 1);
8593 if (g < nlines && s->first_displayed_entry->idx == 0)
8594 s->selected = g - 1;
8600 static const struct got_error *
8601 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8603 const struct got_error *err = NULL;
8604 struct tog_ref_view_state *s = &view->state.ref;
8605 struct tog_reflist_entry *re;
8606 int n, nscroll = view->nlines - 1;
8609 return ref_goto_line(view, nscroll);
8618 horizontal_scroll_input(view, ch);
8621 s->show_ids = !s->show_ids;
8625 s->show_date = !s->show_date;
8629 s->sort_by_date = !s->sort_by_date;
8630 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8632 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8633 got_ref_cmp_by_commit_timestamp_descending :
8634 tog_ref_cmp_by_name, s->repo);
8637 got_reflist_object_id_map_free(tog_refs_idmap);
8638 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8639 &tog_refs, s->repo);
8642 ref_view_free_refs(s);
8643 err = ref_view_load_refs(s);
8648 if (!s->selected_entry)
8650 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8654 if (!s->selected_entry)
8656 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8663 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8668 int eos = view->nlines - 1;
8670 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8674 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8675 for (n = 0; n < eos; n++) {
8678 s->first_displayed_entry = re;
8679 re = TAILQ_PREV(re, tog_reflist_head, entry);
8682 s->selected = n - 1;
8688 if (s->selected > 0) {
8692 ref_scroll_up(s, 1);
8693 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8703 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8704 s->selected -= MIN(nscroll, s->selected);
8705 ref_scroll_up(s, MAX(0, nscroll));
8706 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8712 if (s->selected < s->ndisplayed - 1) {
8716 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8717 /* can't scroll any further */
8721 ref_scroll_down(view, 1);
8731 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8732 /* can't scroll any further; move cursor down */
8733 if (s->selected < s->ndisplayed - 1)
8734 s->selected += MIN(nscroll,
8735 s->ndisplayed - s->selected - 1);
8736 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8737 s->selected += s->ndisplayed - s->selected - 1;
8741 ref_scroll_down(view, nscroll);
8746 err = tog_load_refs(s->repo, s->sort_by_date);
8749 ref_view_free_refs(s);
8750 err = ref_view_load_refs(s);
8753 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8754 s->selected = view->nlines - 2;
8768 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8773 static const struct got_error *
8774 cmd_ref(int argc, char *argv[])
8776 const struct got_error *error;
8777 struct got_repository *repo = NULL;
8778 struct got_worktree *worktree = NULL;
8779 char *cwd = NULL, *repo_path = NULL;
8781 struct tog_view *view;
8782 int *pack_fds = NULL;
8784 while ((ch = getopt(argc, argv, "r:")) != -1) {
8787 repo_path = realpath(optarg, NULL);
8788 if (repo_path == NULL)
8789 return got_error_from_errno2("realpath",
8804 error = got_repo_pack_fds_open(&pack_fds);
8808 if (repo_path == NULL) {
8809 cwd = getcwd(NULL, 0);
8811 return got_error_from_errno("getcwd");
8812 error = got_worktree_open(&worktree, cwd);
8813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8817 strdup(got_worktree_get_repo_path(worktree));
8819 repo_path = strdup(cwd);
8820 if (repo_path == NULL) {
8821 error = got_error_from_errno("strdup");
8826 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8832 error = apply_unveil(got_repo_get_path(repo), NULL);
8836 error = tog_load_refs(repo, 0);
8840 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8842 error = got_error_from_errno("view_open");
8846 error = open_ref_view(view, repo);
8851 /* Release work tree lock. */
8852 got_worktree_close(worktree);
8855 error = view_loop(view);
8860 const struct got_error *close_err = got_repo_close(repo);
8865 const struct got_error *pack_err =
8866 got_repo_pack_fds_close(pack_fds);
8874 static const struct got_error*
8875 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8884 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8888 if (mvwprintw(win, y, x, "%s", str) == ERR)
8889 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8896 static const struct got_error *
8897 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8901 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8903 free(*line_offsets);
8904 *line_offsets = NULL;
8905 return got_error_from_errno("reallocarray");
8909 (*line_offsets)[*nlines] = off;
8914 static const struct got_error *
8915 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8919 for (;n > 0; --n, ++km) {
8923 if (km->keys == NULL)
8926 t = t0 = strdup(km->keys);
8928 return got_error_from_errno("strdup");
8931 while ((k = strsep(&t, " ")) != NULL)
8932 len += strlen(k) > 1 ? 2 : 0;
8934 *ret = MAX(*ret, len);
8941 * Write keymap section headers, keys, and key info in km to f.
8942 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8943 * wrap control and symbolic keys in guillemets, else use <>.
8945 static const struct got_error *
8946 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8951 static const char *u8_glyph[] = {
8952 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8953 "\xe2\x80\xba" /* U+203A (utf8 >) */
8956 int cs, s, first = 1;
8958 cs = got_locale_is_utf8();
8960 t = t0 = strdup(km->keys);
8962 return got_error_from_errno("strdup");
8964 len = strlen(km->keys);
8965 while ((k = strsep(&t, " ")) != NULL) {
8966 s = strlen(k) > 1; /* control or symbolic key */
8967 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8968 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8969 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8972 return got_error_from_errno("fprintf");
8980 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8982 return got_error_from_errno("fprintf");
8988 static const struct got_error *
8989 format_help(struct tog_help_view_state *s)
8991 const struct got_error *err = NULL;
8993 int i, max, n, show = s->all;
8994 static const struct tog_key_map km[] = {
8995 #define KEYMAP_(info, type) { NULL, (info), type }
8996 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9002 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9007 err = max_key_str(&max, km, n);
9011 for (i = 0; i < n; ++i) {
9012 if (km[i].keys == NULL) {
9014 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9015 km[i].type == s->type || s->all)
9019 err = format_help_line(&off, s->f, &km[i], max);
9022 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9029 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9033 static const struct got_error *
9034 create_help(struct tog_help_view_state *s)
9037 const struct got_error *err;
9039 free(s->line_offsets);
9040 s->line_offsets = NULL;
9045 return got_error_from_errno("got_opentemp");
9048 err = format_help(s);
9052 if (s->f && fflush(s->f) != 0)
9053 return got_error_from_errno("fflush");
9058 static const struct got_error *
9059 search_start_help_view(struct tog_view *view)
9061 view->state.help.matched_line = 0;
9066 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9067 size_t *nlines, int **first, int **last, int **match, int **selected)
9069 struct tog_help_view_state *s = &view->state.help;
9072 *nlines = s->nlines;
9073 *line_offsets = s->line_offsets;
9074 *match = &s->matched_line;
9075 *first = &s->first_displayed_line;
9076 *last = &s->last_displayed_line;
9077 *selected = &s->selected_line;
9080 static const struct got_error *
9081 show_help_view(struct tog_view *view)
9083 struct tog_help_view_state *s = &view->state.help;
9084 const struct got_error *err;
9085 regmatch_t *regmatch = &view->regmatch;
9090 int width, nprinted = 0, rc = 0;
9091 int eos = view->nlines;
9093 if (view_is_hsplit_top(view))
9094 --eos; /* account for border */
9098 werase(view->window);
9100 if (view->gline > s->nlines - 1)
9101 view->gline = s->nlines - 1;
9103 err = win_draw_center(view->window, 0, 0, view->ncols,
9104 view_needs_focus_indication(view),
9105 "tog help (press q to return to tog)");
9110 waddstr(view->window, "\n\n");
9116 while (eos > 0 && nprinted < eos) {
9119 linelen = getline(&line, &linesz, s->f);
9120 if (linelen == -1) {
9123 return got_ferror(s->f, GOT_ERR_IO);
9128 if (++s->lineno < s->first_displayed_line)
9130 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9132 if (s->lineno == view->hiline)
9135 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9141 view->maxx = MAX(view->maxx, width);
9146 wattron(view->window, attr);
9147 if (s->first_displayed_line + nprinted == s->matched_line &&
9148 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9149 err = add_matched_line(&width, line, view->ncols - 1, 0,
9150 view->window, view->x, regmatch);
9158 err = format_line(&wline, &width, &skip, line,
9159 view->x, view->ncols, 0, view->x ? 1 : 0);
9164 waddwstr(view->window, &wline[skip]);
9168 if (s->lineno == view->hiline) {
9169 while (width++ < view->ncols)
9170 waddch(view->window, ' ');
9172 if (width < view->ncols)
9173 waddch(view->window, '\n');
9176 wattroff(view->window, attr);
9177 if (++nprinted == 1)
9178 s->first_displayed_line = s->lineno;
9182 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9184 s->last_displayed_line = s->first_displayed_line;
9189 rc = waddnstr(view->window,
9190 "See the tog(1) manual page for full documentation",
9193 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9195 wmove(view->window, view->nlines - 1, 0);
9196 wclrtoeol(view->window);
9197 wstandout(view->window);
9198 rc = waddnstr(view->window, "scroll down for more...",
9201 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9202 if (getcurx(view->window) < view->ncols - 6) {
9203 rc = wprintw(view->window, "[%.0f%%]",
9204 100.00 * s->last_displayed_line / s->nlines);
9206 return got_error_msg(GOT_ERR_IO, "wprintw");
9208 wstandend(view->window);
9214 static const struct got_error *
9215 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9217 struct tog_help_view_state *s = &view->state.help;
9218 const struct got_error *err = NULL;
9224 eos = nscroll = view->nlines;
9225 if (view_is_hsplit_top(view))
9228 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9237 horizontal_scroll_input(view, ch);
9241 s->first_displayed_line = 1;
9249 s->first_displayed_line = (s->nlines - eos) + 3;
9254 if (s->first_displayed_line > 1)
9255 --s->first_displayed_line;
9266 if (s->first_displayed_line == 1) {
9270 while (--nscroll > 0 && s->first_displayed_line > 1)
9271 s->first_displayed_line--;
9277 ++s->first_displayed_line;
9293 while (!s->eof && --nscroll > 0) {
9294 linelen = getline(&line, &linesz, s->f);
9295 s->first_displayed_line++;
9296 if (linelen == -1) {
9300 err = got_ferror(s->f, GOT_ERR_IO);
9314 static const struct got_error *
9315 close_help_view(struct tog_view *view)
9317 struct tog_help_view_state *s = &view->state.help;
9319 free(s->line_offsets);
9320 s->line_offsets = NULL;
9321 if (fclose(s->f) == EOF)
9322 return got_error_from_errno("fclose");
9327 static const struct got_error *
9328 reset_help_view(struct tog_view *view)
9330 struct tog_help_view_state *s = &view->state.help;
9333 if (s->f && fclose(s->f) == EOF)
9334 return got_error_from_errno("fclose");
9336 wclear(view->window);
9340 s->first_displayed_line = 1;
9341 s->last_displayed_line = view->nlines;
9342 s->matched_line = 0;
9344 return create_help(s);
9347 static const struct got_error *
9348 open_help_view(struct tog_view *view, struct tog_view *parent)
9350 const struct got_error *err = NULL;
9351 struct tog_help_view_state *s = &view->state.help;
9353 s->type = (enum tog_keymap_type)parent->type;
9354 s->first_displayed_line = 1;
9355 s->last_displayed_line = view->nlines;
9356 s->selected_line = 1;
9358 view->show = show_help_view;
9359 view->input = input_help_view;
9360 view->reset = reset_help_view;
9361 view->close = close_help_view;
9362 view->search_start = search_start_help_view;
9363 view->search_setup = search_setup_help_view;
9364 view->search_next = search_next_view_match;
9366 err = create_help(s);
9370 static const struct got_error *
9371 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9372 enum tog_view_type request, int y, int x)
9374 const struct got_error *err = NULL;
9380 if (view->type == TOG_VIEW_LOG) {
9381 struct tog_log_view_state *s = &view->state.log;
9383 err = open_diff_view_for_commit(new_view, y, x,
9384 s->selected_entry->commit, s->selected_entry->id,
9387 return got_error_msg(GOT_ERR_NOT_IMPL,
9388 "parent/child view pair not supported");
9390 case TOG_VIEW_BLAME:
9391 if (view->type == TOG_VIEW_TREE) {
9392 struct tog_tree_view_state *s = &view->state.tree;
9394 err = blame_tree_entry(new_view, y, x,
9395 s->selected_entry, &s->parents, s->commit_id,
9398 return got_error_msg(GOT_ERR_NOT_IMPL,
9399 "parent/child view pair not supported");
9402 if (view->type == TOG_VIEW_BLAME)
9403 err = log_annotated_line(new_view, y, x,
9404 view->state.blame.repo, view->state.blame.id_to_log);
9405 else if (view->type == TOG_VIEW_TREE)
9406 err = log_selected_tree_entry(new_view, y, x,
9408 else if (view->type == TOG_VIEW_REF)
9409 err = log_ref_entry(new_view, y, x,
9410 view->state.ref.selected_entry,
9411 view->state.ref.repo);
9413 return got_error_msg(GOT_ERR_NOT_IMPL,
9414 "parent/child view pair not supported");
9417 if (view->type == TOG_VIEW_LOG)
9418 err = browse_commit_tree(new_view, y, x,
9419 view->state.log.selected_entry,
9420 view->state.log.in_repo_path,
9421 view->state.log.head_ref_name,
9422 view->state.log.repo);
9423 else if (view->type == TOG_VIEW_REF)
9424 err = browse_ref_tree(new_view, y, x,
9425 view->state.ref.selected_entry,
9426 view->state.ref.repo);
9428 return got_error_msg(GOT_ERR_NOT_IMPL,
9429 "parent/child view pair not supported");
9432 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9433 if (*new_view == NULL)
9434 return got_error_from_errno("view_open");
9435 if (view->type == TOG_VIEW_LOG)
9436 err = open_ref_view(*new_view, view->state.log.repo);
9437 else if (view->type == TOG_VIEW_TREE)
9438 err = open_ref_view(*new_view, view->state.tree.repo);
9440 err = got_error_msg(GOT_ERR_NOT_IMPL,
9441 "parent/child view pair not supported");
9443 view_close(*new_view);
9446 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9447 if (*new_view == NULL)
9448 return got_error_from_errno("view_open");
9449 err = open_help_view(*new_view, view);
9451 view_close(*new_view);
9454 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9461 * If view was scrolled down to move the selected line into view when opening a
9462 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9465 offset_selection_up(struct tog_view *view)
9467 switch (view->type) {
9468 case TOG_VIEW_BLAME: {
9469 struct tog_blame_view_state *s = &view->state.blame;
9470 if (s->first_displayed_line == 1) {
9471 s->selected_line = MAX(s->selected_line - view->offset,
9475 if (s->first_displayed_line > view->offset)
9476 s->first_displayed_line -= view->offset;
9478 s->first_displayed_line = 1;
9479 s->selected_line += view->offset;
9483 log_scroll_up(&view->state.log, view->offset);
9484 view->state.log.selected += view->offset;
9487 ref_scroll_up(&view->state.ref, view->offset);
9488 view->state.ref.selected += view->offset;
9491 tree_scroll_up(&view->state.tree, view->offset);
9492 view->state.tree.selected += view->offset;
9502 * If the selected line is in the section of screen covered by the bottom split,
9503 * scroll down offset lines to move it into view and index its new position.
9505 static const struct got_error *
9506 offset_selection_down(struct tog_view *view)
9508 const struct got_error *err = NULL;
9509 const struct got_error *(*scrolld)(struct tog_view *, int);
9510 int *selected = NULL;
9513 switch (view->type) {
9514 case TOG_VIEW_BLAME: {
9515 struct tog_blame_view_state *s = &view->state.blame;
9518 if (s->selected_line > view->nlines - header) {
9519 offset = abs(view->nlines - s->selected_line - header);
9520 s->first_displayed_line += offset;
9521 s->selected_line -= offset;
9522 view->offset = offset;
9526 case TOG_VIEW_LOG: {
9527 struct tog_log_view_state *s = &view->state.log;
9528 scrolld = &log_scroll_down;
9529 header = view_is_parent_view(view) ? 3 : 2;
9530 selected = &s->selected;
9533 case TOG_VIEW_REF: {
9534 struct tog_ref_view_state *s = &view->state.ref;
9535 scrolld = &ref_scroll_down;
9537 selected = &s->selected;
9540 case TOG_VIEW_TREE: {
9541 struct tog_tree_view_state *s = &view->state.tree;
9542 scrolld = &tree_scroll_down;
9544 selected = &s->selected;
9554 if (selected && *selected > view->nlines - header) {
9555 offset = abs(view->nlines - *selected - header);
9556 view->offset = offset;
9557 if (scrolld && offset) {
9558 err = scrolld(view, offset);
9559 *selected -= offset;
9567 list_commands(FILE *fp)
9571 fprintf(fp, "commands:");
9572 for (i = 0; i < nitems(tog_commands); i++) {
9573 const struct tog_cmd *cmd = &tog_commands[i];
9574 fprintf(fp, " %s", cmd->name);
9580 usage(int hflag, int status)
9582 FILE *fp = (status == 0) ? stdout : stderr;
9584 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9587 fprintf(fp, "lazy usage: %s path\n", getprogname());
9594 make_argv(int argc, ...)
9602 argv = calloc(argc, sizeof(char *));
9605 for (i = 0; i < argc; i++) {
9606 argv[i] = strdup(va_arg(ap, char *));
9607 if (argv[i] == NULL)
9616 * Try to convert 'tog path' into a 'tog log path' command.
9617 * The user could simply have mistyped the command rather than knowingly
9618 * provided a path. So check whether argv[0] can in fact be resolved
9619 * to a path in the HEAD commit and print a special error if not.
9620 * This hack is for mpi@ <3
9622 static const struct got_error *
9623 tog_log_with_path(int argc, char *argv[])
9625 const struct got_error *error = NULL, *close_err;
9626 const struct tog_cmd *cmd = NULL;
9627 struct got_repository *repo = NULL;
9628 struct got_worktree *worktree = NULL;
9629 struct got_object_id *commit_id = NULL, *id = NULL;
9630 struct got_commit_object *commit = NULL;
9631 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9632 char *commit_id_str = NULL, **cmd_argv = NULL;
9633 int *pack_fds = NULL;
9635 cwd = getcwd(NULL, 0);
9637 return got_error_from_errno("getcwd");
9639 error = got_repo_pack_fds_open(&pack_fds);
9643 error = got_worktree_open(&worktree, cwd);
9644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9648 repo_path = strdup(got_worktree_get_repo_path(worktree));
9650 repo_path = strdup(cwd);
9651 if (repo_path == NULL) {
9652 error = got_error_from_errno("strdup");
9656 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9660 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9665 error = tog_load_refs(repo, 0);
9668 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9669 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9670 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9675 got_worktree_close(worktree);
9679 error = got_object_open_as_commit(&commit, repo, commit_id);
9683 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9685 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9687 fprintf(stderr, "%s: '%s' is no known command or path\n",
9688 getprogname(), argv[0]);
9693 error = got_object_id_str(&commit_id_str, commit_id);
9697 cmd = &tog_commands[0]; /* log */
9699 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9700 error = cmd->cmd_main(argc, cmd_argv);
9703 close_err = got_repo_close(repo);
9708 got_object_commit_close(commit);
9710 got_worktree_close(worktree);
9712 const struct got_error *pack_err =
9713 got_repo_pack_fds_close(pack_fds);
9718 free(commit_id_str);
9725 for (i = 0; i < argc; i++)
9734 main(int argc, char *argv[])
9736 const struct got_error *io_err, *error = NULL;
9737 const struct tog_cmd *cmd = NULL;
9738 int ch, hflag = 0, Vflag = 0;
9739 char **cmd_argv = NULL;
9740 static const struct option longopts[] = {
9741 { "version", no_argument, NULL, 'V' },
9744 char *diff_algo_str = NULL;
9745 const char *test_script_path;
9747 setlocale(LC_CTYPE, "");
9750 * Test mode init must happen before pledge() because "tty" will
9751 * not allow TTY-related ioctls to occur via regular files.
9753 test_script_path = getenv("TOG_TEST_SCRIPT");
9754 if (test_script_path != NULL) {
9755 error = init_mock_term(test_script_path);
9757 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9762 #if !defined(PROFILE)
9763 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9768 if (!isatty(STDIN_FILENO))
9769 errx(1, "standard input is not a tty");
9771 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9791 got_version_print_str();
9798 /* Build an argument vector which runs a default command. */
9799 cmd = &tog_commands[0];
9801 cmd_argv = make_argv(argc, cmd->name);
9805 /* Did the user specify a command? */
9806 for (i = 0; i < nitems(tog_commands); i++) {
9807 if (strncmp(tog_commands[i].name, argv[0],
9808 strlen(argv[0])) == 0) {
9809 cmd = &tog_commands[i];
9815 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9816 if (diff_algo_str) {
9817 if (strcasecmp(diff_algo_str, "patience") == 0)
9818 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9819 if (strcasecmp(diff_algo_str, "myers") == 0)
9820 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9826 /* No command specified; try log with a path */
9827 error = tog_log_with_path(argc, argv);
9832 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9835 if (using_mock_io) {
9836 io_err = tog_io_close();
9843 for (i = 0; i < argc; i++)
9848 if (error && error->code != GOT_ERR_CANCELLED &&
9849 error->code != GOT_ERR_EOF &&
9850 error->code != GOT_ERR_PRIVSEP_EXIT &&
9851 error->code != GOT_ERR_PRIVSEP_PIPE &&
9852 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9853 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);