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"
59 #include "got_keyword.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 #define CTRL(x) ((x) & 0x1f)
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
81 __dead static void usage(int, int);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_ref(void);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct tog_cmd tog_commands[] = {
95 { "log", cmd_log, usage_log },
96 { "diff", cmd_diff, usage_diff },
97 { "blame", cmd_blame, usage_blame },
98 { "tree", cmd_tree, usage_tree },
99 { "ref", cmd_ref, usage_ref },
111 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
112 enum tog_keymap_type {
113 TOG_KEYMAP_KEYS = -2,
129 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
131 #define TOG_EOF_STRING "(END)"
133 struct commit_queue_entry {
134 TAILQ_ENTRY(commit_queue_entry) entry;
135 struct got_object_id *id;
136 struct got_commit_object *commit;
139 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
140 struct commit_queue {
142 struct commit_queue_head head;
146 STAILQ_ENTRY(tog_color) entry;
150 STAILQ_HEAD(tog_colors, tog_color);
152 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
153 static struct got_reflist_object_id_map *tog_refs_idmap;
155 struct got_object_id *id;
159 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
161 static const struct got_error *
162 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
163 struct got_reference* re2)
165 const char *name1 = got_ref_get_name(re1);
166 const char *name2 = got_ref_get_name(re2);
167 int isbackup1, isbackup2;
169 /* Sort backup refs towards the bottom of the list. */
170 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
171 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
172 if (!isbackup1 && isbackup2) {
175 } else if (isbackup1 && !isbackup2) {
180 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
184 static const struct got_error *
185 tog_load_refs(struct got_repository *repo, int sort_by_date)
187 const struct got_error *err;
189 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
190 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
195 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
202 if (tog_refs_idmap) {
203 got_reflist_object_id_map_free(tog_refs_idmap);
204 tog_refs_idmap = NULL;
206 got_ref_list_free(&tog_refs);
209 static const struct got_error *
210 add_color(struct tog_colors *colors, const char *pattern,
211 int idx, short color)
213 const struct got_error *err = NULL;
214 struct tog_color *tc;
217 if (idx < 1 || idx > COLOR_PAIRS - 1)
220 init_pair(idx, color, -1);
222 tc = calloc(1, sizeof(*tc));
224 return got_error_from_errno("calloc");
225 regerr = regcomp(&tc->regex, pattern,
226 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
228 static char regerr_msg[512];
229 static char err_msg[512];
230 regerror(regerr, &tc->regex, regerr_msg,
232 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
234 err = got_error_msg(GOT_ERR_REGEX, err_msg);
239 STAILQ_INSERT_HEAD(colors, tc, entry);
244 free_colors(struct tog_colors *colors)
246 struct tog_color *tc;
248 while (!STAILQ_EMPTY(colors)) {
249 tc = STAILQ_FIRST(colors);
250 STAILQ_REMOVE_HEAD(colors, entry);
256 static struct tog_color *
257 get_color(struct tog_colors *colors, int colorpair)
259 struct tog_color *tc = NULL;
261 STAILQ_FOREACH(tc, colors, entry) {
262 if (tc->colorpair == colorpair)
270 default_color_value(const char *envvar)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
273 return COLOR_MAGENTA;
274 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
278 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
283 return COLOR_MAGENTA;
284 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
286 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
290 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
292 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
296 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
297 return COLOR_MAGENTA;
298 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
300 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
307 get_color_value(const char *envvar)
309 const char *val = getenv(envvar);
312 return default_color_value(envvar);
314 if (strcasecmp(val, "black") == 0)
316 if (strcasecmp(val, "red") == 0)
318 if (strcasecmp(val, "green") == 0)
320 if (strcasecmp(val, "yellow") == 0)
322 if (strcasecmp(val, "blue") == 0)
324 if (strcasecmp(val, "magenta") == 0)
325 return COLOR_MAGENTA;
326 if (strcasecmp(val, "cyan") == 0)
328 if (strcasecmp(val, "white") == 0)
330 if (strcasecmp(val, "default") == 0)
333 return default_color_value(envvar);
336 struct tog_diff_view_state {
337 struct got_object_id *id1, *id2;
338 const char *label1, *label2;
342 int first_displayed_line;
343 int last_displayed_line;
346 int ignore_whitespace;
348 struct got_repository *repo;
349 struct got_diff_line *lines;
354 /* passed from log or blame view; may be NULL */
355 struct tog_view *parent_view;
358 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
359 static volatile sig_atomic_t tog_thread_error;
361 struct tog_log_thread_args {
362 pthread_cond_t need_commits;
363 pthread_cond_t commit_loaded;
366 struct got_commit_graph *graph;
367 struct commit_queue *real_commits;
368 const char *in_repo_path;
369 struct got_object_id *start_id;
370 struct got_repository *repo;
374 struct commit_queue_entry **first_displayed_entry;
375 struct commit_queue_entry **selected_entry;
377 int *search_next_done;
381 regex_t *limit_regex;
382 struct commit_queue *limit_commits;
385 struct tog_log_view_state {
386 struct commit_queue *commits;
387 struct commit_queue_entry *first_displayed_entry;
388 struct commit_queue_entry *last_displayed_entry;
389 struct commit_queue_entry *selected_entry;
390 struct commit_queue real_commits;
395 struct got_repository *repo;
396 struct got_object_id *start_id;
399 struct tog_log_thread_args thread_args;
400 struct commit_queue_entry *matched_entry;
401 struct commit_queue_entry *search_entry;
402 struct tog_colors colors;
406 struct commit_queue limit_commits;
409 #define TOG_COLOR_DIFF_MINUS 1
410 #define TOG_COLOR_DIFF_PLUS 2
411 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
412 #define TOG_COLOR_DIFF_META 4
413 #define TOG_COLOR_TREE_SUBMODULE 5
414 #define TOG_COLOR_TREE_SYMLINK 6
415 #define TOG_COLOR_TREE_DIRECTORY 7
416 #define TOG_COLOR_TREE_EXECUTABLE 8
417 #define TOG_COLOR_COMMIT 9
418 #define TOG_COLOR_AUTHOR 10
419 #define TOG_COLOR_DATE 11
420 #define TOG_COLOR_REFS_HEADS 12
421 #define TOG_COLOR_REFS_TAGS 13
422 #define TOG_COLOR_REFS_REMOTES 14
423 #define TOG_COLOR_REFS_BACKUP 15
425 struct tog_blame_cb_args {
426 struct tog_blame_line *lines; /* one per line */
429 struct tog_view *view;
430 struct got_object_id *commit_id;
434 struct tog_blame_thread_args {
436 struct got_repository *repo;
437 struct tog_blame_cb_args *cb_args;
439 got_cancel_cb cancel_cb;
441 pthread_cond_t blame_complete;
447 struct tog_blame_line *lines;
451 struct tog_blame_thread_args thread_args;
452 struct tog_blame_cb_args cb_args;
457 struct tog_blame_view_state {
458 int first_displayed_line;
459 int last_displayed_line;
461 int last_diffed_line;
465 struct got_object_id_queue blamed_commits;
466 struct got_object_qid *blamed_commit;
468 struct got_repository *repo;
469 struct got_object_id *commit_id;
470 struct got_object_id *id_to_log;
471 struct tog_blame blame;
473 struct tog_colors colors;
476 struct tog_parent_tree {
477 TAILQ_ENTRY(tog_parent_tree) entry;
478 struct got_tree_object *tree;
479 struct got_tree_entry *first_displayed_entry;
480 struct got_tree_entry *selected_entry;
484 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
486 struct tog_tree_view_state {
488 struct got_object_id *commit_id;/* commit which this tree belongs to */
489 struct got_tree_object *root; /* the commit's root tree entry */
490 struct got_tree_object *tree; /* currently displayed (sub-)tree */
491 struct got_tree_entry *first_displayed_entry;
492 struct got_tree_entry *last_displayed_entry;
493 struct got_tree_entry *selected_entry;
494 int ndisplayed, selected, show_ids;
495 struct tog_parent_trees parents; /* parent trees of current sub-tree */
497 struct got_repository *repo;
498 struct got_tree_entry *matched_entry;
499 struct tog_colors colors;
502 struct tog_reflist_entry {
503 TAILQ_ENTRY(tog_reflist_entry) entry;
504 struct got_reference *ref;
508 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
510 struct tog_ref_view_state {
511 struct tog_reflist_head refs;
512 struct tog_reflist_entry *first_displayed_entry;
513 struct tog_reflist_entry *last_displayed_entry;
514 struct tog_reflist_entry *selected_entry;
515 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
516 struct got_repository *repo;
517 struct tog_reflist_entry *matched_entry;
518 struct tog_colors colors;
521 struct tog_help_view_state {
526 int first_displayed_line;
527 int last_displayed_line;
532 enum tog_keymap_type type;
535 #define GENERATE_HELP \
536 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
537 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
538 KEY_("k C-p Up", "Move cursor or page up one line"), \
539 KEY_("j C-n Down", "Move cursor or page down one line"), \
540 KEY_("C-b b PgUp", "Scroll the view up one page"), \
541 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
542 KEY_("C-u u", "Scroll the view up one half page"), \
543 KEY_("C-d d", "Scroll the view down one half page"), \
544 KEY_("g", "Go to line N (default: first line)"), \
545 KEY_("Home =", "Go to the first line"), \
546 KEY_("G", "Go to line N (default: last line)"), \
547 KEY_("End *", "Go to the last line"), \
548 KEY_("l Right", "Scroll the view right"), \
549 KEY_("h Left", "Scroll the view left"), \
550 KEY_("$", "Scroll view to the rightmost position"), \
551 KEY_("0", "Scroll view to the leftmost position"), \
552 KEY_("-", "Decrease size of the focussed split"), \
553 KEY_("+", "Increase size of the focussed split"), \
554 KEY_("Tab", "Switch focus between views"), \
555 KEY_("F", "Toggle fullscreen mode"), \
556 KEY_("S", "Switch split-screen layout"), \
557 KEY_("/", "Open prompt to enter search term"), \
558 KEY_("n", "Find next line/token matching the current search term"), \
559 KEY_("N", "Find previous line/token matching the current search term"),\
560 KEY_("q", "Quit the focussed view; Quit help screen"), \
561 KEY_("Q", "Quit tog"), \
563 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
564 KEY_("< ,", "Move cursor up one commit"), \
565 KEY_("> .", "Move cursor down one commit"), \
566 KEY_("Enter", "Open diff view of the selected commit"), \
567 KEY_("B", "Reload the log view and toggle display of merged commits"), \
568 KEY_("R", "Open ref view of all repository references"), \
569 KEY_("T", "Display tree view of the repository from the selected" \
571 KEY_("@", "Toggle between displaying author and committer name"), \
572 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
573 KEY_("C-g Backspace", "Cancel current search or log operation"), \
574 KEY_("C-l", "Reload the log view with new commits in the repository"), \
576 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
577 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
578 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
579 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
580 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
582 KEY_("(", "Go to the previous file in the diff"), \
583 KEY_(")", "Go to the next file in the diff"), \
584 KEY_("{", "Go to the previous hunk in the diff"), \
585 KEY_("}", "Go to the next hunk in the diff"), \
586 KEY_("[", "Decrease the number of context lines"), \
587 KEY_("]", "Increase the number of context lines"), \
588 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
590 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
591 KEY_("Enter", "Display diff view of the selected line's commit"), \
592 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
593 KEY_("L", "Open log view for the currently selected annotated line"), \
594 KEY_("C", "Reload view with the previously blamed commit"), \
595 KEY_("c", "Reload view with the version of the file found in the" \
596 " selected line's commit"), \
597 KEY_("p", "Reload view with the version of the file found in the" \
598 " selected line's parent commit"), \
600 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
601 KEY_("Enter", "Enter selected directory or open blame view of the" \
603 KEY_("L", "Open log view for the selected entry"), \
604 KEY_("R", "Open ref view of all repository references"), \
605 KEY_("i", "Show object IDs for all tree entries"), \
606 KEY_("Backspace", "Return to the parent directory"), \
608 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
609 KEY_("Enter", "Display log view of the selected reference"), \
610 KEY_("T", "Display tree view of the selected reference"), \
611 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
612 KEY_("m", "Toggle display of last modified date for each reference"), \
613 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
614 KEY_("C-l", "Reload view with all repository references")
619 enum tog_keymap_type type;
622 /* curses io for tog regress */
630 static int using_mock_io;
632 #define TOG_KEY_SCRDUMP SHRT_MIN
635 * We implement two types of views: parent views and child views.
637 * The 'Tab' key switches focus between a parent view and its child view.
638 * Child views are shown side-by-side to their parent view, provided
639 * there is enough screen estate.
641 * When a new view is opened from within a parent view, this new view
642 * becomes a child view of the parent view, replacing any existing child.
644 * When a new view is opened from within a child view, this new view
645 * becomes a parent view which will obscure the views below until the
646 * user quits the new parent view by typing 'q'.
648 * This list of views contains parent views only.
649 * Child views are only pointed to by their parent view.
651 TAILQ_HEAD(tog_view_list_head, tog_view);
654 TAILQ_ENTRY(tog_view) entry;
657 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
658 int resized_y, resized_x; /* begin_y/x based on user resizing */
659 int maxx, x; /* max column and current start column */
660 int lines, cols; /* copies of LINES and COLS */
661 int nscrolled, offset; /* lines scrolled and hsplit line offset */
662 int gline, hiline; /* navigate to and highlight this nG line */
663 int ch, count; /* current keymap and count prefix */
664 int resized; /* set when in a resize event */
665 int focussed; /* Only set on one parent or child view at a time. */
667 struct tog_view *parent;
668 struct tog_view *child;
671 * This flag is initially set on parent views when a new child view
672 * is created. It gets toggled when the 'Tab' key switches focus
673 * between parent and child.
674 * The flag indicates whether focus should be passed on to our child
675 * view if this parent view gets picked for focus after another parent
676 * view was closed. This prevents child views from losing focus in such
681 enum tog_view_mode mode;
682 /* type-specific state */
683 enum tog_view_type type;
685 struct tog_diff_view_state diff;
686 struct tog_log_view_state log;
687 struct tog_blame_view_state blame;
688 struct tog_tree_view_state tree;
689 struct tog_ref_view_state ref;
690 struct tog_help_view_state help;
693 const struct got_error *(*show)(struct tog_view *);
694 const struct got_error *(*input)(struct tog_view **,
695 struct tog_view *, int);
696 const struct got_error *(*reset)(struct tog_view *);
697 const struct got_error *(*resize)(struct tog_view *, int);
698 const struct got_error *(*close)(struct tog_view *);
700 const struct got_error *(*search_start)(struct tog_view *);
701 const struct got_error *(*search_next)(struct tog_view *);
702 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
703 int **, int **, int **, int **);
706 #define TOG_SEARCH_FORWARD 1
707 #define TOG_SEARCH_BACKWARD 2
708 int search_next_done;
709 #define TOG_SEARCH_HAVE_MORE 1
710 #define TOG_SEARCH_NO_MORE 2
711 #define TOG_SEARCH_HAVE_NONE 3
717 static const struct got_error *open_diff_view(struct tog_view *,
718 struct got_object_id *, struct got_object_id *,
719 const char *, const char *, int, int, int, struct tog_view *,
720 struct got_repository *);
721 static const struct got_error *show_diff_view(struct tog_view *);
722 static const struct got_error *input_diff_view(struct tog_view **,
723 struct tog_view *, int);
724 static const struct got_error *reset_diff_view(struct tog_view *);
725 static const struct got_error* close_diff_view(struct tog_view *);
726 static const struct got_error *search_start_diff_view(struct tog_view *);
727 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
728 size_t *, int **, int **, int **, int **);
729 static const struct got_error *search_next_view_match(struct tog_view *);
731 static const struct got_error *open_log_view(struct tog_view *,
732 struct got_object_id *, struct got_repository *,
733 const char *, const char *, int);
734 static const struct got_error * show_log_view(struct tog_view *);
735 static const struct got_error *input_log_view(struct tog_view **,
736 struct tog_view *, int);
737 static const struct got_error *resize_log_view(struct tog_view *, int);
738 static const struct got_error *close_log_view(struct tog_view *);
739 static const struct got_error *search_start_log_view(struct tog_view *);
740 static const struct got_error *search_next_log_view(struct tog_view *);
742 static const struct got_error *open_blame_view(struct tog_view *, char *,
743 struct got_object_id *, struct got_repository *);
744 static const struct got_error *show_blame_view(struct tog_view *);
745 static const struct got_error *input_blame_view(struct tog_view **,
746 struct tog_view *, int);
747 static const struct got_error *reset_blame_view(struct tog_view *);
748 static const struct got_error *close_blame_view(struct tog_view *);
749 static const struct got_error *search_start_blame_view(struct tog_view *);
750 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
751 size_t *, int **, int **, int **, int **);
753 static const struct got_error *open_tree_view(struct tog_view *,
754 struct got_object_id *, const char *, struct got_repository *);
755 static const struct got_error *show_tree_view(struct tog_view *);
756 static const struct got_error *input_tree_view(struct tog_view **,
757 struct tog_view *, int);
758 static const struct got_error *close_tree_view(struct tog_view *);
759 static const struct got_error *search_start_tree_view(struct tog_view *);
760 static const struct got_error *search_next_tree_view(struct tog_view *);
762 static const struct got_error *open_ref_view(struct tog_view *,
763 struct got_repository *);
764 static const struct got_error *show_ref_view(struct tog_view *);
765 static const struct got_error *input_ref_view(struct tog_view **,
766 struct tog_view *, int);
767 static const struct got_error *close_ref_view(struct tog_view *);
768 static const struct got_error *search_start_ref_view(struct tog_view *);
769 static const struct got_error *search_next_ref_view(struct tog_view *);
771 static const struct got_error *open_help_view(struct tog_view *,
773 static const struct got_error *show_help_view(struct tog_view *);
774 static const struct got_error *input_help_view(struct tog_view **,
775 struct tog_view *, int);
776 static const struct got_error *reset_help_view(struct tog_view *);
777 static const struct got_error* close_help_view(struct tog_view *);
778 static const struct got_error *search_start_help_view(struct tog_view *);
779 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
780 size_t *, int **, int **, int **, int **);
782 static volatile sig_atomic_t tog_sigwinch_received;
783 static volatile sig_atomic_t tog_sigpipe_received;
784 static volatile sig_atomic_t tog_sigcont_received;
785 static volatile sig_atomic_t tog_sigint_received;
786 static volatile sig_atomic_t tog_sigterm_received;
789 tog_sigwinch(int signo)
791 tog_sigwinch_received = 1;
795 tog_sigpipe(int signo)
797 tog_sigpipe_received = 1;
801 tog_sigcont(int signo)
803 tog_sigcont_received = 1;
807 tog_sigint(int signo)
809 tog_sigint_received = 1;
813 tog_sigterm(int signo)
815 tog_sigterm_received = 1;
819 tog_fatal_signal_received(void)
821 return (tog_sigpipe_received ||
822 tog_sigint_received || tog_sigterm_received);
825 static const struct got_error *
826 view_close(struct tog_view *view)
828 const struct got_error *err = NULL, *child_err = NULL;
831 child_err = view_close(view->child);
835 err = view->close(view);
837 del_panel(view->panel);
839 delwin(view->window);
841 return err ? err : child_err;
844 static struct tog_view *
845 view_open(int nlines, int ncols, int begin_y, int begin_x,
846 enum tog_view_type type)
848 struct tog_view *view = calloc(1, sizeof(*view));
856 view->nlines = nlines ? nlines : LINES - begin_y;
857 view->ncols = ncols ? ncols : COLS - begin_x;
858 view->begin_y = begin_y;
859 view->begin_x = begin_x;
860 view->window = newwin(nlines, ncols, begin_y, begin_x);
861 if (view->window == NULL) {
865 view->panel = new_panel(view->window);
866 if (view->panel == NULL ||
867 set_panel_userptr(view->panel, view) != OK) {
872 keypad(view->window, TRUE);
877 view_split_begin_x(int begin_x)
879 if (begin_x > 0 || COLS < 120)
881 return (COLS - MAX(COLS / 2, 80));
884 /* XXX Stub till we decide what to do. */
886 view_split_begin_y(int lines)
888 return lines * HSPLIT_SCALE;
891 static const struct got_error *view_resize(struct tog_view *);
893 static const struct got_error *
894 view_splitscreen(struct tog_view *view)
896 const struct got_error *err = NULL;
898 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
899 if (view->resized_y && view->resized_y < view->lines)
900 view->begin_y = view->resized_y;
902 view->begin_y = view_split_begin_y(view->nlines);
904 } else if (!view->resized) {
905 if (view->resized_x && view->resized_x < view->cols - 1 &&
907 view->begin_x = view->resized_x;
909 view->begin_x = view_split_begin_x(0);
912 view->nlines = LINES - view->begin_y;
913 view->ncols = COLS - view->begin_x;
916 err = view_resize(view);
920 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
921 view->parent->nlines = view->begin_y;
923 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
924 return got_error_from_errno("mvwin");
929 static const struct got_error *
930 view_fullscreen(struct tog_view *view)
932 const struct got_error *err = NULL;
935 view->begin_y = view->resized ? view->begin_y : 0;
936 view->nlines = view->resized ? view->nlines : LINES;
940 err = view_resize(view);
944 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
945 return got_error_from_errno("mvwin");
951 view_is_parent_view(struct tog_view *view)
953 return view->parent == NULL;
957 view_is_splitscreen(struct tog_view *view)
959 return view->begin_x > 0 || view->begin_y > 0;
963 view_is_fullscreen(struct tog_view *view)
965 return view->nlines == LINES && view->ncols == COLS;
969 view_is_hsplit_top(struct tog_view *view)
971 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
972 view_is_splitscreen(view->child);
976 view_border(struct tog_view *view)
979 const struct tog_view *view_above;
982 return view_border(view->parent);
984 panel = panel_above(view->panel);
988 view_above = panel_userptr(panel);
989 if (view->mode == TOG_VIEW_SPLIT_HRZN)
990 mvwhline(view->window, view_above->begin_y - 1,
991 view->begin_x, ACS_HLINE, view->ncols);
993 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
994 ACS_VLINE, view->nlines);
997 static const struct got_error *view_init_hsplit(struct tog_view *, int);
998 static const struct got_error *request_log_commits(struct tog_view *);
999 static const struct got_error *offset_selection_down(struct tog_view *);
1000 static void offset_selection_up(struct tog_view *);
1001 static void view_get_split(struct tog_view *, int *, int *);
1003 static const struct got_error *
1004 view_resize(struct tog_view *view)
1006 const struct got_error *err = NULL;
1007 int dif, nlines, ncols;
1009 dif = LINES - view->lines; /* line difference */
1011 if (view->lines > LINES)
1012 nlines = view->nlines - (view->lines - LINES);
1014 nlines = view->nlines + (LINES - view->lines);
1015 if (view->cols > COLS)
1016 ncols = view->ncols - (view->cols - COLS);
1018 ncols = view->ncols + (COLS - view->cols);
1021 int hs = view->child->begin_y;
1023 if (!view_is_fullscreen(view))
1024 view->child->begin_x = view_split_begin_x(view->begin_x);
1025 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1026 view->child->begin_x == 0) {
1029 view_fullscreen(view->child);
1030 if (view->child->focussed)
1031 show_panel(view->child->panel);
1033 show_panel(view->panel);
1035 ncols = view->child->begin_x;
1037 view_splitscreen(view->child);
1038 show_panel(view->child->panel);
1041 * XXX This is ugly and needs to be moved into the above
1042 * logic but "works" for now and my attempts at moving it
1043 * break either 'tab' or 'F' key maps in horizontal splits.
1046 err = view_splitscreen(view->child);
1049 if (dif < 0) { /* top split decreased */
1050 err = offset_selection_down(view);
1057 show_panel(view->child->panel);
1058 nlines = view->nlines;
1060 } else if (view->parent == NULL)
1063 if (view->resize && dif > 0) {
1064 err = view->resize(view, dif);
1069 if (wresize(view->window, nlines, ncols) == ERR)
1070 return got_error_from_errno("wresize");
1071 if (replace_panel(view->panel, view->window) == ERR)
1072 return got_error_from_errno("replace_panel");
1073 wclear(view->window);
1075 view->nlines = nlines;
1076 view->ncols = ncols;
1077 view->lines = LINES;
1083 static const struct got_error *
1084 resize_log_view(struct tog_view *view, int increase)
1086 struct tog_log_view_state *s = &view->state.log;
1087 const struct got_error *err = NULL;
1090 if (s->selected_entry)
1091 n = s->selected_entry->idx + view->lines - s->selected;
1094 * Request commits to account for the increased
1095 * height so we have enough to populate the view.
1097 if (s->commits->ncommits < n) {
1098 view->nscrolled = n - s->commits->ncommits + increase + 1;
1099 err = request_log_commits(view);
1106 view_adjust_offset(struct tog_view *view, int n)
1111 if (view->parent && view->parent->offset) {
1112 if (view->parent->offset + n >= 0)
1113 view->parent->offset += n;
1115 view->parent->offset = 0;
1116 } else if (view->offset) {
1117 if (view->offset - n >= 0)
1124 static const struct got_error *
1125 view_resize_split(struct tog_view *view, int resize)
1127 const struct got_error *err = NULL;
1128 struct tog_view *v = NULL;
1135 if (!v->child || !view_is_splitscreen(v->child))
1138 v->resized = v->child->resized = resize; /* lock for resize event */
1140 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1141 if (v->child->resized_y)
1142 v->child->begin_y = v->child->resized_y;
1144 v->child->begin_y -= resize;
1146 v->child->begin_y += resize;
1147 if (v->child->begin_y < 3) {
1149 v->child->begin_y = 3;
1150 } else if (v->child->begin_y > LINES - 1) {
1152 v->child->begin_y = LINES - 1;
1155 v->child->ncols = COLS;
1156 view_adjust_offset(view, resize);
1157 err = view_init_hsplit(v, v->child->begin_y);
1160 v->child->resized_y = v->child->begin_y;
1162 if (v->child->resized_x)
1163 v->child->begin_x = v->child->resized_x;
1165 v->child->begin_x -= resize;
1167 v->child->begin_x += resize;
1168 if (v->child->begin_x < 11) {
1170 v->child->begin_x = 11;
1171 } else if (v->child->begin_x > COLS - 1) {
1173 v->child->begin_x = COLS - 1;
1175 v->child->resized_x = v->child->begin_x;
1178 v->child->mode = v->mode;
1179 v->child->nlines = v->lines - v->child->begin_y;
1180 v->child->ncols = v->cols - v->child->begin_x;
1183 err = view_fullscreen(v);
1186 err = view_splitscreen(v->child);
1190 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 err = offset_selection_down(v->child);
1197 err = v->resize(v, 0);
1198 else if (v->child->resize)
1199 err = v->child->resize(v->child, 0);
1201 v->resized = v->child->resized = 0;
1207 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1209 struct tog_view *v = src->child ? src->child : src;
1211 dst->resized_x = v->resized_x;
1212 dst->resized_y = v->resized_y;
1215 static const struct got_error *
1216 view_close_child(struct tog_view *view)
1218 const struct got_error *err = NULL;
1220 if (view->child == NULL)
1223 err = view_close(view->child);
1228 static const struct got_error *
1229 view_set_child(struct tog_view *view, struct tog_view *child)
1231 const struct got_error *err = NULL;
1233 view->child = child;
1234 child->parent = view;
1236 err = view_resize(view);
1240 if (view->child->resized_x || view->child->resized_y)
1241 err = view_resize_split(view, 0);
1246 static const struct got_error *view_dispatch_request(struct tog_view **,
1247 struct tog_view *, enum tog_view_type, int, int);
1249 static const struct got_error *
1250 view_request_new(struct tog_view **requested, struct tog_view *view,
1251 enum tog_view_type request)
1253 struct tog_view *new_view = NULL;
1254 const struct got_error *err;
1259 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1260 view_get_split(view, &y, &x);
1262 err = view_dispatch_request(&new_view, view, request, y, x);
1266 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1267 request != TOG_VIEW_HELP) {
1268 err = view_init_hsplit(view, y);
1274 new_view->focussed = 1;
1275 new_view->mode = view->mode;
1276 new_view->nlines = request == TOG_VIEW_HELP ?
1277 view->lines : view->lines - y;
1279 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1280 view_transfer_size(new_view, view);
1281 err = view_close_child(view);
1284 err = view_set_child(view, new_view);
1287 view->focus_child = 1;
1289 *requested = new_view;
1295 tog_resizeterm(void)
1298 struct winsize size;
1300 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1301 cols = 80; /* Default */
1305 lines = size.ws_row;
1307 resize_term(lines, cols);
1310 static const struct got_error *
1311 view_search_start(struct tog_view *view, int fast_refresh)
1313 const struct got_error *err = NULL;
1314 struct tog_view *v = view;
1318 if (view->search_started) {
1319 regfree(&view->regex);
1320 view->searching = 0;
1321 memset(&view->regmatch, 0, sizeof(view->regmatch));
1323 view->search_started = 0;
1325 if (view->nlines < 1)
1328 if (view_is_hsplit_top(view))
1330 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1333 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1334 wclrtoeol(v->window);
1336 nodelay(v->window, FALSE); /* block for search term input */
1339 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1340 wrefresh(v->window);
1343 nodelay(v->window, TRUE);
1344 if (!fast_refresh && !using_mock_io)
1349 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1350 err = view->search_start(view);
1352 regfree(&view->regex);
1355 view->search_started = 1;
1356 view->searching = TOG_SEARCH_FORWARD;
1357 view->search_next_done = 0;
1358 view->search_next(view);
1364 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1365 static const struct got_error *
1366 switch_split(struct tog_view *view)
1368 const struct got_error *err = NULL;
1369 struct tog_view *v = NULL;
1376 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1377 v->mode = TOG_VIEW_SPLIT_VERT;
1379 v->mode = TOG_VIEW_SPLIT_HRZN;
1383 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1384 v->mode = TOG_VIEW_SPLIT_NONE;
1386 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1387 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1388 v->child->begin_y = v->child->resized_y;
1389 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1390 v->child->begin_x = v->child->resized_x;
1393 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1395 v->child->ncols = COLS;
1396 v->child->nscrolled = LINES - v->child->nlines;
1398 err = view_init_hsplit(v, v->child->begin_y);
1402 v->child->mode = v->mode;
1403 v->child->nlines = v->lines - v->child->begin_y;
1406 err = view_fullscreen(v);
1409 err = view_splitscreen(v->child);
1413 if (v->mode == TOG_VIEW_SPLIT_NONE)
1414 v->mode = TOG_VIEW_SPLIT_VERT;
1415 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1416 err = offset_selection_down(v);
1419 err = offset_selection_down(v->child);
1423 offset_selection_up(v);
1424 offset_selection_up(v->child);
1427 err = v->resize(v, 0);
1428 else if (v->child->resize)
1429 err = v->child->resize(v->child, 0);
1435 * Strip trailing whitespace from str starting at byte *n;
1436 * if *n < 0, use strlen(str). Return new str length in *n.
1439 strip_trailing_ws(char *str, int *n)
1443 if (str == NULL || *str == '\0')
1449 while (x-- > 0 && isspace((unsigned char)str[x]))
1456 * Extract visible substring of line y from the curses screen
1457 * and strip trailing whitespace. If vline is set, overwrite
1458 * line[vline] with '|' because the ACS_VLINE character is
1459 * written out as 'x'. Write the line to file f.
1461 static const struct got_error *
1462 view_write_line(FILE *f, int y, int vline)
1464 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1467 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1469 return got_error_fmt(GOT_ERR_RANGE,
1470 "failed to extract line %d", y);
1473 * In some views, lines are padded with blanks to COLS width.
1474 * Strip them so we can diff without the -b flag when testing.
1476 strip_trailing_ws(line, &r);
1481 w = fprintf(f, "%s\n", line);
1482 if (w != r + 1) /* \n */
1483 return got_ferror(f, GOT_ERR_IO);
1489 * Capture the visible curses screen by writing each line to the
1490 * file at the path set via the TOG_SCR_DUMP environment variable.
1492 static const struct got_error *
1493 screendump(struct tog_view *view)
1495 const struct got_error *err;
1498 err = got_opentemp_truncate(tog_io.sdump);
1502 if ((view->child && view->child->begin_x) ||
1503 (view->parent && view->begin_x)) {
1504 int ncols = view->child ? view->ncols : view->parent->ncols;
1506 /* vertical splitscreen */
1507 for (i = 0; i < view->nlines; ++i) {
1508 err = view_write_line(tog_io.sdump, i, ncols - 1);
1515 /* fullscreen or horizontal splitscreen */
1516 if ((view->child && view->child->begin_y) ||
1517 (view->parent && view->begin_y)) /* hsplit */
1518 hline = view->child ?
1519 view->child->begin_y : view->begin_y;
1521 for (i = 0; i < view->lines; i++) {
1522 if (hline && i == hline - 1) {
1525 /* ACS_HLINE writes out as 'q', overwrite it */
1526 for (c = 0; c < view->cols; ++c)
1527 fputc('-', tog_io.sdump);
1528 fputc('\n', tog_io.sdump);
1532 err = view_write_line(tog_io.sdump, i, 0);
1543 * Compute view->count from numeric input. Assign total to view->count and
1544 * return first non-numeric key entered.
1547 get_compound_key(struct tog_view *view, int c)
1549 struct tog_view *v = view;
1552 if (view_is_hsplit_top(view))
1554 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1558 cbreak(); /* block for input */
1559 nodelay(view->window, FALSE);
1560 wmove(v->window, v->nlines - 1, 0);
1561 wclrtoeol(v->window);
1562 waddch(v->window, ':');
1565 x = getcurx(v->window);
1566 if (x != ERR && x < view->ncols) {
1567 waddch(v->window, c);
1568 wrefresh(v->window);
1572 * Don't overflow. Max valid request should be the greatest
1573 * between the longest and total lines; cap at 10 million.
1578 n = n * 10 + (c - '0');
1579 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1581 if (c == 'G' || c == 'g') { /* nG key map */
1582 view->gline = view->hiline = n;
1587 /* Massage excessive or inapplicable values at the input handler. */
1594 action_report(struct tog_view *view)
1596 struct tog_view *v = view;
1598 if (view_is_hsplit_top(view))
1600 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1603 wmove(v->window, v->nlines - 1, 0);
1604 wclrtoeol(v->window);
1605 wprintw(v->window, ":%s", view->action);
1606 wrefresh(v->window);
1609 * Clear action status report. Only clear in blame view
1610 * once annotating is complete, otherwise it's too fast.
1612 if (view->type == TOG_VIEW_BLAME) {
1613 if (view->state.blame.blame_complete)
1614 view->action = NULL;
1616 view->action = NULL;
1620 * Read the next line from the test script and assign
1621 * key instruction to *ch. If at EOF, set the *done flag.
1623 static const struct got_error *
1624 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1626 const struct got_error *err = NULL;
1630 if (view->count && --view->count) {
1636 if (getline(&line, &linesz, script) == -1) {
1641 err = got_ferror(script, GOT_ERR_IO);
1646 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1647 tog_io.wait_for_ui = 1;
1648 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1650 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1652 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1654 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1656 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1658 else if (strncasecmp(line, "TAB", 3) == 0)
1660 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1661 *ch = TOG_KEY_SCRDUMP;
1662 else if (isdigit((unsigned char)*line)) {
1665 while (isdigit((unsigned char)*t))
1667 view->ch = *ch = *t;
1669 /* ignore error, view->count is 0 if instruction is invalid */
1670 view->count = strtonum(line, 0, INT_MAX, NULL);
1679 static const struct got_error *
1680 view_input(struct tog_view **new, int *done, struct tog_view *view,
1681 struct tog_view_list_head *views, int fast_refresh)
1683 const struct got_error *err = NULL;
1690 action_report(view);
1692 /* Clear "no matches" indicator. */
1693 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1694 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1695 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1699 if (view->searching && !view->search_next_done) {
1700 errcode = pthread_mutex_unlock(&tog_mutex);
1702 return got_error_set_errno(errcode,
1703 "pthread_mutex_unlock");
1705 errcode = pthread_mutex_lock(&tog_mutex);
1707 return got_error_set_errno(errcode,
1708 "pthread_mutex_lock");
1709 view->search_next(view);
1713 /* Allow threads to make progress while we are waiting for input. */
1714 errcode = pthread_mutex_unlock(&tog_mutex);
1716 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1718 if (using_mock_io) {
1719 err = tog_read_script_key(tog_io.f, view, &ch, done);
1721 errcode = pthread_mutex_lock(&tog_mutex);
1724 } else if (view->count && --view->count) {
1726 nodelay(view->window, TRUE);
1727 ch = wgetch(view->window);
1728 /* let C-g or backspace abort unfinished count */
1729 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1734 ch = wgetch(view->window);
1735 if (ch >= '1' && ch <= '9')
1736 view->ch = ch = get_compound_key(view, ch);
1738 if (view->hiline && ch != ERR && ch != 0)
1739 view->hiline = 0; /* key pressed, clear line highlight */
1740 nodelay(view->window, TRUE);
1741 errcode = pthread_mutex_lock(&tog_mutex);
1743 return got_error_set_errno(errcode, "pthread_mutex_lock");
1745 if (tog_sigwinch_received || tog_sigcont_received) {
1747 tog_sigwinch_received = 0;
1748 tog_sigcont_received = 0;
1749 TAILQ_FOREACH(v, views, entry) {
1750 err = view_resize(v);
1753 err = v->input(new, v, KEY_RESIZE);
1757 err = view_resize(v->child);
1760 err = v->child->input(new, v->child,
1764 if (v->child->resized_x || v->child->resized_y) {
1765 err = view_resize_split(v, 0);
1777 if (view->type == TOG_VIEW_HELP)
1778 err = view->reset(view);
1780 err = view_request_new(new, view, TOG_VIEW_HELP);
1786 view->child->focussed = 1;
1787 view->focus_child = 1;
1788 } else if (view->parent) {
1790 view->parent->focussed = 1;
1791 view->parent->focus_child = 0;
1792 if (!view_is_splitscreen(view)) {
1793 if (view->parent->resize) {
1794 err = view->parent->resize(view->parent,
1799 offset_selection_up(view->parent);
1800 err = view_fullscreen(view->parent);
1807 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1808 if (view->parent->resize) {
1809 /* might need more commits to fill fullscreen */
1810 err = view->parent->resize(view->parent, 0);
1814 offset_selection_up(view->parent);
1816 err = view->input(new, view, ch);
1824 if (view_is_parent_view(view)) {
1825 if (view->child == NULL)
1827 if (view_is_splitscreen(view->child)) {
1829 view->child->focussed = 1;
1830 err = view_fullscreen(view->child);
1832 err = view_splitscreen(view->child);
1834 err = view_resize_split(view, 0);
1838 err = view->child->input(new, view->child,
1841 if (view_is_splitscreen(view)) {
1842 view->parent->focussed = 0;
1844 err = view_fullscreen(view);
1846 err = view_splitscreen(view);
1847 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1848 err = view_resize(view->parent);
1850 err = view_resize_split(view, 0);
1854 err = view->input(new, view, KEY_RESIZE);
1859 err = view->resize(view, 0);
1864 if (view->parent->resize) {
1865 err = view->parent->resize(view->parent, 0);
1869 err = offset_selection_down(view->parent);
1873 err = offset_selection_down(view);
1877 err = switch_split(view);
1880 err = view_resize_split(view, -1);
1883 err = view_resize_split(view, 1);
1889 if (view->search_start)
1890 view_search_start(view, fast_refresh);
1892 err = view->input(new, view, ch);
1896 if (view->search_started && view->search_next) {
1897 view->searching = (ch == 'n' ?
1898 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1899 view->search_next_done = 0;
1900 view->search_next(view);
1902 err = view->input(new, view, ch);
1905 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1906 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1907 view->action = "Patience diff algorithm";
1909 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1910 view->action = "Myers diff algorithm";
1912 TAILQ_FOREACH(v, views, entry) {
1918 if (v->child && v->child->reset) {
1919 err = v->child->reset(v->child);
1925 case TOG_KEY_SCRDUMP:
1926 err = screendump(view);
1929 err = view->input(new, view, ch);
1937 view_needs_focus_indication(struct tog_view *view)
1939 if (view_is_parent_view(view)) {
1940 if (view->child == NULL || view->child->focussed)
1942 if (!view_is_splitscreen(view->child))
1944 } else if (!view_is_splitscreen(view))
1947 return view->focussed;
1950 static const struct got_error *
1953 const struct got_error *err = NULL;
1955 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1956 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1957 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1958 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1959 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1960 err = got_ferror(tog_io.f, GOT_ERR_IO);
1961 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1962 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1967 static const struct got_error *
1968 view_loop(struct tog_view *view)
1970 const struct got_error *err = NULL;
1971 struct tog_view_list_head views;
1972 struct tog_view *new_view;
1974 int fast_refresh = 10;
1975 int done = 0, errcode;
1977 mode = getenv("TOG_VIEW_SPLIT_MODE");
1978 if (!mode || !(*mode == 'h' || *mode == 'H'))
1979 view->mode = TOG_VIEW_SPLIT_VERT;
1981 view->mode = TOG_VIEW_SPLIT_HRZN;
1983 errcode = pthread_mutex_lock(&tog_mutex);
1985 return got_error_set_errno(errcode, "pthread_mutex_lock");
1988 TAILQ_INSERT_HEAD(&views, view, entry);
1991 err = view->show(view);
1996 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1997 !tog_fatal_signal_received()) {
1998 /* Refresh fast during initialization, then become slower. */
1999 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2000 halfdelay(10); /* switch to once per second */
2002 err = view_input(&new_view, &done, view, &views, fast_refresh);
2006 if (view->dying && view == TAILQ_FIRST(&views) &&
2007 TAILQ_NEXT(view, entry) == NULL)
2013 * When we quit, scroll the screen up a single line
2014 * so we don't lose any information.
2016 TAILQ_FOREACH(v, &views, entry) {
2017 wmove(v->window, 0, 0);
2018 wdeleteln(v->window);
2019 wnoutrefresh(v->window);
2020 if (v->child && !view_is_fullscreen(v)) {
2021 wmove(v->child->window, 0, 0);
2022 wdeleteln(v->child->window);
2023 wnoutrefresh(v->child->window);
2030 struct tog_view *v, *prev = NULL;
2032 if (view_is_parent_view(view))
2033 prev = TAILQ_PREV(view, tog_view_list_head,
2035 else if (view->parent)
2036 prev = view->parent;
2039 view->parent->child = NULL;
2040 view->parent->focus_child = 0;
2041 /* Restore fullscreen line height. */
2042 view->parent->nlines = view->parent->lines;
2043 err = view_resize(view->parent);
2046 /* Make resized splits persist. */
2047 view_transfer_size(view->parent, view);
2049 TAILQ_REMOVE(&views, view, entry);
2051 err = view_close(view);
2056 TAILQ_FOREACH(v, &views, entry) {
2060 if (view == NULL && new_view == NULL) {
2061 /* No view has focus. Try to pick one. */
2064 else if (!TAILQ_EMPTY(&views)) {
2065 view = TAILQ_LAST(&views,
2066 tog_view_list_head);
2069 if (view->focus_child) {
2070 view->child->focussed = 1;
2078 struct tog_view *v, *t;
2079 /* Only allow one parent view per type. */
2080 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2081 if (v->type != new_view->type)
2083 TAILQ_REMOVE(&views, v, entry);
2084 err = view_close(v);
2089 TAILQ_INSERT_TAIL(&views, new_view, entry);
2092 if (view && !done) {
2093 if (view_is_parent_view(view)) {
2094 if (view->child && view->child->focussed)
2097 if (view->parent && view->parent->focussed)
2098 view = view->parent;
2100 show_panel(view->panel);
2101 if (view->child && view_is_splitscreen(view->child))
2102 show_panel(view->child->panel);
2103 if (view->parent && view_is_splitscreen(view)) {
2104 err = view->parent->show(view->parent);
2108 err = view->show(view);
2112 err = view->child->show(view->child);
2121 while (!TAILQ_EMPTY(&views)) {
2122 const struct got_error *close_err;
2123 view = TAILQ_FIRST(&views);
2124 TAILQ_REMOVE(&views, view, entry);
2125 close_err = view_close(view);
2126 if (close_err && err == NULL)
2130 errcode = pthread_mutex_unlock(&tog_mutex);
2131 if (errcode && err == NULL)
2132 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2142 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2147 /* Create newly allocated wide-character string equivalent to a byte string. */
2148 static const struct got_error *
2149 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2152 const struct got_error *err = NULL;
2155 *wlen = mbstowcs(NULL, s, 0);
2156 if (*wlen == (size_t)-1) {
2158 if (errno != EILSEQ)
2159 return got_error_from_errno("mbstowcs");
2161 /* byte string invalid in current encoding; try to "fix" it */
2162 err = got_mbsavis(&vis, &vislen, s);
2165 *wlen = mbstowcs(NULL, vis, 0);
2166 if (*wlen == (size_t)-1) {
2167 err = got_error_from_errno("mbstowcs"); /* give up */
2172 *ws = calloc(*wlen + 1, sizeof(**ws));
2174 err = got_error_from_errno("calloc");
2178 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2179 err = got_error_from_errno("mbstowcs");
2190 static const struct got_error *
2191 expand_tab(char **ptr, const char *src)
2194 size_t len, n, idx = 0, sz = 0;
2197 n = len = strlen(src);
2198 dst = malloc(n + 1);
2200 return got_error_from_errno("malloc");
2202 while (idx < len && src[idx]) {
2203 const char c = src[idx];
2206 size_t nb = TABSIZE - sz % TABSIZE;
2209 p = realloc(dst, n + nb);
2212 return got_error_from_errno("realloc");
2217 memset(dst + sz, ' ', nb);
2220 dst[sz++] = src[idx];
2230 * Advance at most n columns from wline starting at offset off.
2231 * Return the index to the first character after the span operation.
2232 * Return the combined column width of all spanned wide characters in
2236 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2238 int width, i, cols = 0;
2245 for (i = off; wline[i] != L'\0'; ++i) {
2246 if (wline[i] == L'\t')
2247 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2249 width = wcwidth(wline[i]);
2256 if (cols + width > n)
2266 * Format a line for display, ensuring that it won't overflow a width limit.
2267 * With scrolling, the width returned refers to the scrolled version of the
2268 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2270 static const struct got_error *
2271 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2272 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2274 const struct got_error *err = NULL;
2276 wchar_t *wline = NULL;
2285 err = expand_tab(&exstr, line);
2290 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2295 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2297 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2298 wline[wlen - 1] = L'\0';
2301 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2302 wline[wlen - 1] = L'\0';
2306 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2312 *scrollxp = scrollx;
2320 static const struct got_error*
2321 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2322 struct got_object_id *id, struct got_repository *repo)
2324 static const struct got_error *err = NULL;
2325 struct got_reflist_entry *re;
2334 TAILQ_FOREACH(re, refs, entry) {
2335 struct got_tag_object *tag = NULL;
2336 struct got_object_id *ref_id;
2339 name = got_ref_get_name(re->ref);
2340 if (strcmp(name, GOT_REF_HEAD) == 0)
2342 if (strncmp(name, "refs/", 5) == 0)
2344 if (strncmp(name, "got/", 4) == 0)
2346 if (strncmp(name, "heads/", 6) == 0)
2348 if (strncmp(name, "remotes/", 8) == 0) {
2350 s = strstr(name, "/" GOT_REF_HEAD);
2351 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2354 err = got_ref_resolve(&ref_id, repo, re->ref);
2357 if (strncmp(name, "tags/", 5) == 0) {
2358 err = got_object_open_as_tag(&tag, repo, ref_id);
2360 if (err->code != GOT_ERR_OBJ_TYPE) {
2364 /* Ref points at something other than a tag. */
2369 cmp = got_object_id_cmp(tag ?
2370 got_object_tag_get_object_id(tag) : ref_id, id);
2373 got_object_tag_close(tag);
2377 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2378 s ? ", " : "", name) == -1) {
2379 err = got_error_from_errno("asprintf");
2390 static const struct got_error *
2391 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2396 smallerthan = strchr(author, '<');
2397 if (smallerthan && smallerthan[1] != '\0')
2398 author = smallerthan + 1;
2399 author[strcspn(author, "@>")] = '\0';
2400 return format_line(wauthor, author_width, NULL, author, 0, limit,
2404 static const struct got_error *
2405 draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2406 const size_t date_display_cols, int author_display_cols)
2408 struct tog_log_view_state *s = &view->state.log;
2409 const struct got_error *err = NULL;
2410 struct got_commit_object *commit = entry->commit;
2411 struct got_object_id *id = entry->id;
2412 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2413 char *refs_str = NULL;
2414 char *logmsg0 = NULL, *logmsg = NULL;
2415 char *author = NULL;
2416 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2417 int author_width, refstr_width, logmsg_width;
2418 char *newline, *line = NULL;
2419 int col, limit, scrollx, logmsg_x;
2420 const int avail = view->ncols, marker_column = author_display_cols + 1;
2422 time_t committer_time;
2423 struct tog_color *tc;
2424 struct got_reflist_head *refs;
2426 committer_time = got_object_commit_get_committer_time(commit);
2427 if (gmtime_r(&committer_time, &tm) == NULL)
2428 return got_error_from_errno("gmtime_r");
2429 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2430 return got_error(GOT_ERR_NO_SPACE);
2432 if (avail <= date_display_cols)
2433 limit = MIN(sizeof(datebuf) - 1, avail);
2435 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2436 tc = get_color(&s->colors, TOG_COLOR_DATE);
2438 wattr_on(view->window,
2439 COLOR_PAIR(tc->colorpair), NULL);
2440 waddnstr(view->window, datebuf, limit);
2442 wattr_off(view->window,
2443 COLOR_PAIR(tc->colorpair), NULL);
2450 err = got_object_id_str(&id_str, id);
2453 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2455 wattr_on(view->window,
2456 COLOR_PAIR(tc->colorpair), NULL);
2457 wprintw(view->window, "%.8s ", id_str);
2459 wattr_off(view->window,
2460 COLOR_PAIR(tc->colorpair), NULL);
2467 if (s->use_committer)
2468 author = strdup(got_object_commit_get_committer(commit));
2470 author = strdup(got_object_commit_get_author(commit));
2471 if (author == NULL) {
2472 err = got_error_from_errno("strdup");
2475 err = format_author(&wauthor, &author_width, author, avail - col, col);
2478 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2480 wattr_on(view->window,
2481 COLOR_PAIR(tc->colorpair), NULL);
2482 waddwstr(view->window, wauthor);
2483 col += author_width;
2484 while (col < avail && author_width < author_display_cols + 2) {
2485 if (tog_base_commit.id != NULL &&
2486 author_width == marker_column &&
2487 entry->idx == tog_base_commit.idx)
2488 waddch(view->window, tog_base_commit.marker);
2490 waddch(view->window, ' ');
2495 wattr_off(view->window,
2496 COLOR_PAIR(tc->colorpair), NULL);
2500 err = got_object_commit_get_logmsg(&logmsg0, commit);
2504 while (*logmsg == '\n')
2506 newline = strchr(logmsg, '\n');
2510 limit = avail - col;
2511 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2512 limit--; /* for the border */
2514 /* Prepend reference labels to log message if possible .*/
2515 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2516 err = build_refs_str(&refs_str, refs, id, s->repo);
2522 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2523 err = got_error_from_errno("asprintf");
2526 err = format_line(&wrefstr, &refstr_width,
2527 &scrollx, rs, view->x, limit, col, 1);
2531 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2533 wattr_on(view->window,
2534 COLOR_PAIR(tc->colorpair), NULL);
2535 waddwstr(view->window, &wrefstr[scrollx]);
2537 wattr_off(view->window,
2538 COLOR_PAIR(tc->colorpair), NULL);
2539 col += MAX(refstr_width, 0);
2544 waddch(view->window, ' ');
2548 if (refstr_width > 0)
2551 int unscrolled_refstr_width;
2552 size_t len = wcslen(wrefstr);
2555 * No need to check for -1 return value here since
2556 * unprintables have been replaced by span_wline().
2558 unscrolled_refstr_width = wcswidth(wrefstr, len);
2559 unscrolled_refstr_width += 1; /* trailing space */
2560 logmsg_x = view->x - unscrolled_refstr_width;
2563 limit = avail - col;
2564 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2565 limit--; /* for the border */
2569 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2573 waddwstr(view->window, &wlogmsg[scrollx]);
2574 col += MAX(logmsg_width, 0);
2575 while (col < avail) {
2576 waddch(view->window, ' ');
2590 static struct commit_queue_entry *
2591 alloc_commit_queue_entry(struct got_commit_object *commit,
2592 struct got_object_id *id)
2594 struct commit_queue_entry *entry;
2595 struct got_object_id *dup;
2597 entry = calloc(1, sizeof(*entry));
2601 dup = got_object_id_dup(id);
2608 entry->commit = commit;
2613 pop_commit(struct commit_queue *commits)
2615 struct commit_queue_entry *entry;
2617 entry = TAILQ_FIRST(&commits->head);
2618 TAILQ_REMOVE(&commits->head, entry, entry);
2619 got_object_commit_close(entry->commit);
2620 commits->ncommits--;
2626 free_commits(struct commit_queue *commits)
2628 while (!TAILQ_EMPTY(&commits->head))
2629 pop_commit(commits);
2632 static const struct got_error *
2633 match_commit(int *have_match, struct got_object_id *id,
2634 struct got_commit_object *commit, regex_t *regex)
2636 const struct got_error *err = NULL;
2637 regmatch_t regmatch;
2638 char *id_str = NULL, *logmsg = NULL;
2642 err = got_object_id_str(&id_str, id);
2646 err = got_object_commit_get_logmsg(&logmsg, commit);
2650 if (regexec(regex, got_object_commit_get_author(commit), 1,
2651 ®match, 0) == 0 ||
2652 regexec(regex, got_object_commit_get_committer(commit), 1,
2653 ®match, 0) == 0 ||
2654 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2655 regexec(regex, logmsg, 1, ®match, 0) == 0)
2663 static const struct got_error *
2664 queue_commits(struct tog_log_thread_args *a)
2666 const struct got_error *err = NULL;
2669 * We keep all commits open throughout the lifetime of the log
2670 * view in order to avoid having to re-fetch commits from disk
2671 * while updating the display.
2674 struct got_object_id id;
2675 struct got_commit_object *commit;
2676 struct commit_queue_entry *entry;
2677 int limit_match = 0;
2680 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2685 err = got_object_open_as_commit(&commit, a->repo, &id);
2688 entry = alloc_commit_queue_entry(commit, &id);
2689 if (entry == NULL) {
2690 err = got_error_from_errno("alloc_commit_queue_entry");
2694 errcode = pthread_mutex_lock(&tog_mutex);
2696 err = got_error_set_errno(errcode,
2697 "pthread_mutex_lock");
2701 entry->idx = a->real_commits->ncommits;
2702 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2703 a->real_commits->ncommits++;
2705 if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2706 got_object_id_cmp(&id, tog_base_commit.id) == 0)
2707 tog_base_commit.idx = entry->idx;
2710 err = match_commit(&limit_match, &id, commit,
2716 struct commit_queue_entry *matched;
2718 matched = alloc_commit_queue_entry(
2719 entry->commit, entry->id);
2720 if (matched == NULL) {
2721 err = got_error_from_errno(
2722 "alloc_commit_queue_entry");
2725 matched->commit = entry->commit;
2726 got_object_commit_retain(entry->commit);
2728 matched->idx = a->limit_commits->ncommits;
2729 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2731 a->limit_commits->ncommits++;
2735 * This is how we signal log_thread() that we
2736 * have found a match, and that it should be
2737 * counted as a new entry for the view.
2739 a->limit_match = limit_match;
2742 if (*a->searching == TOG_SEARCH_FORWARD &&
2743 !*a->search_next_done) {
2745 err = match_commit(&have_match, &id, commit, a->regex);
2750 if (limit_match && have_match)
2751 *a->search_next_done =
2752 TOG_SEARCH_HAVE_MORE;
2753 } else if (have_match)
2754 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2757 errcode = pthread_mutex_unlock(&tog_mutex);
2758 if (errcode && err == NULL)
2759 err = got_error_set_errno(errcode,
2760 "pthread_mutex_unlock");
2763 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2769 select_commit(struct tog_log_view_state *s)
2771 struct commit_queue_entry *entry;
2774 entry = s->first_displayed_entry;
2776 if (ncommits == s->selected) {
2777 s->selected_entry = entry;
2780 entry = TAILQ_NEXT(entry, entry);
2785 static const struct got_error *
2786 draw_commits(struct tog_view *view)
2788 const struct got_error *err = NULL;
2789 struct tog_log_view_state *s = &view->state.log;
2790 struct commit_queue_entry *entry = s->selected_entry;
2791 int limit = view->nlines;
2793 int ncommits, author_cols = 4, refstr_cols;
2794 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2795 char *refs_str = NULL;
2797 struct tog_color *tc;
2798 static const size_t date_display_cols = 12;
2799 struct got_reflist_head *refs;
2801 if (view_is_hsplit_top(view))
2802 --limit; /* account for border */
2804 if (s->selected_entry &&
2805 !(view->searching && view->search_next_done == 0)) {
2806 err = got_object_id_str(&id_str, s->selected_entry->id);
2809 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2810 s->selected_entry->id);
2811 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2817 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2818 halfdelay(10); /* disable fast refresh */
2820 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2821 if (asprintf(&ncommits_str, " [%d/%d] %s",
2822 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2823 (view->searching && !view->search_next_done) ?
2824 "searching..." : "loading...") == -1) {
2825 err = got_error_from_errno("asprintf");
2829 const char *search_str = NULL;
2830 const char *limit_str = NULL;
2832 if (view->searching) {
2833 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2834 search_str = "no more matches";
2835 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2836 search_str = "no matches found";
2837 else if (!view->search_next_done)
2838 search_str = "searching...";
2841 if (s->limit_view && s->commits->ncommits == 0)
2842 limit_str = "no matches found";
2844 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2845 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2846 search_str ? search_str : (refs_str ? refs_str : ""),
2847 limit_str ? limit_str : "") == -1) {
2848 err = got_error_from_errno("asprintf");
2856 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2857 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2858 "........................................",
2859 s->in_repo_path, ncommits_str) == -1) {
2860 err = got_error_from_errno("asprintf");
2864 } else if (asprintf(&header, "commit %s%s",
2865 id_str ? id_str : "........................................",
2866 ncommits_str) == -1) {
2867 err = got_error_from_errno("asprintf");
2871 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2875 werase(view->window);
2877 if (view_needs_focus_indication(view))
2878 wstandout(view->window);
2879 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2881 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2882 waddwstr(view->window, wline);
2883 while (width < view->ncols) {
2884 waddch(view->window, ' ');
2888 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2889 if (view_needs_focus_indication(view))
2890 wstandend(view->window);
2895 /* Grow author column size if necessary, and set view->maxx. */
2896 entry = s->first_displayed_entry;
2900 struct got_commit_object *c = entry->commit;
2901 char *author, *eol, *msg, *msg0;
2902 wchar_t *wauthor, *wmsg;
2904 if (ncommits >= limit - 1)
2906 if (s->use_committer)
2907 author = strdup(got_object_commit_get_committer(c));
2909 author = strdup(got_object_commit_get_author(c));
2910 if (author == NULL) {
2911 err = got_error_from_errno("strdup");
2914 err = format_author(&wauthor, &width, author, COLS,
2916 if (author_cols < width)
2917 author_cols = width;
2922 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2924 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2929 err = format_line(&ws, &width, NULL, refs_str,
2930 0, INT_MAX, date_display_cols + author_cols, 0);
2936 refstr_cols = width + 3; /* account for [ ] + space */
2939 err = got_object_commit_get_logmsg(&msg0, c);
2943 while (*msg == '\n')
2945 if ((eol = strchr(msg, '\n')))
2947 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2948 date_display_cols + author_cols + refstr_cols, 0);
2951 view->maxx = MAX(view->maxx, width + refstr_cols);
2955 entry = TAILQ_NEXT(entry, entry);
2958 entry = s->first_displayed_entry;
2959 s->last_displayed_entry = s->first_displayed_entry;
2962 if (ncommits >= limit - 1)
2964 if (ncommits == s->selected)
2965 wstandout(view->window);
2966 err = draw_commit(view, entry, date_display_cols, author_cols);
2967 if (ncommits == s->selected)
2968 wstandend(view->window);
2972 s->last_displayed_entry = entry;
2973 entry = TAILQ_NEXT(entry, entry);
2986 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2988 struct commit_queue_entry *entry;
2991 entry = TAILQ_FIRST(&s->commits->head);
2992 if (s->first_displayed_entry == entry)
2995 entry = s->first_displayed_entry;
2996 while (entry && nscrolled < maxscroll) {
2997 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2999 s->first_displayed_entry = entry;
3005 static const struct got_error *
3006 trigger_log_thread(struct tog_view *view, int wait)
3008 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3012 halfdelay(1); /* fast refresh while loading commits */
3014 while (!ta->log_complete && !tog_thread_error &&
3015 (ta->commits_needed > 0 || ta->load_all)) {
3016 /* Wake the log thread. */
3017 errcode = pthread_cond_signal(&ta->need_commits);
3019 return got_error_set_errno(errcode,
3020 "pthread_cond_signal");
3023 * The mutex will be released while the view loop waits
3024 * in wgetch(), at which time the log thread will run.
3029 /* Display progress update in log view. */
3030 show_log_view(view);
3034 /* Wait right here while next commit is being loaded. */
3035 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3037 return got_error_set_errno(errcode,
3038 "pthread_cond_wait");
3040 /* Display progress update in log view. */
3041 show_log_view(view);
3049 static const struct got_error *
3050 request_log_commits(struct tog_view *view)
3052 struct tog_log_view_state *state = &view->state.log;
3053 const struct got_error *err = NULL;
3055 if (state->thread_args.log_complete)
3058 state->thread_args.commits_needed += view->nscrolled;
3059 err = trigger_log_thread(view, 1);
3060 view->nscrolled = 0;
3065 static const struct got_error *
3066 log_scroll_down(struct tog_view *view, int maxscroll)
3068 struct tog_log_view_state *s = &view->state.log;
3069 const struct got_error *err = NULL;
3070 struct commit_queue_entry *pentry;
3071 int nscrolled = 0, ncommits_needed;
3073 if (s->last_displayed_entry == NULL)
3076 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3077 if (s->commits->ncommits < ncommits_needed &&
3078 !s->thread_args.log_complete) {
3080 * Ask the log thread for required amount of commits.
3082 s->thread_args.commits_needed +=
3083 ncommits_needed - s->commits->ncommits;
3084 err = trigger_log_thread(view, 1);
3090 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3091 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3094 s->last_displayed_entry = pentry ?
3095 pentry : s->last_displayed_entry;
3097 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3100 s->first_displayed_entry = pentry;
3101 } while (++nscrolled < maxscroll);
3103 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3104 view->nscrolled += nscrolled;
3106 view->nscrolled = 0;
3111 static const struct got_error *
3112 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3113 struct got_commit_object *commit, struct got_object_id *commit_id,
3114 struct tog_view *log_view, struct got_repository *repo)
3116 const struct got_error *err;
3117 struct got_object_qid *parent_id;
3118 struct tog_view *diff_view;
3120 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3121 if (diff_view == NULL)
3122 return got_error_from_errno("view_open");
3124 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3125 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3126 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3128 *new_view = diff_view;
3132 static const struct got_error *
3133 tree_view_visit_subtree(struct tog_tree_view_state *s,
3134 struct got_tree_object *subtree)
3136 struct tog_parent_tree *parent;
3138 parent = calloc(1, sizeof(*parent));
3140 return got_error_from_errno("calloc");
3142 parent->tree = s->tree;
3143 parent->first_displayed_entry = s->first_displayed_entry;
3144 parent->selected_entry = s->selected_entry;
3145 parent->selected = s->selected;
3146 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3149 s->first_displayed_entry = NULL;
3153 static const struct got_error *
3154 tree_view_walk_path(struct tog_tree_view_state *s,
3155 struct got_commit_object *commit, const char *path)
3157 const struct got_error *err = NULL;
3158 struct got_tree_object *tree = NULL;
3160 char *slash, *subpath = NULL;
3162 /* Walk the path and open corresponding tree objects. */
3165 struct got_tree_entry *te;
3166 struct got_object_id *tree_id;
3172 /* Ensure the correct subtree entry is selected. */
3173 slash = strchr(p, '/');
3175 te_name = strdup(p);
3177 te_name = strndup(p, slash - p);
3178 if (te_name == NULL) {
3179 err = got_error_from_errno("strndup");
3182 te = got_object_tree_find_entry(s->tree, te_name);
3184 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3189 s->first_displayed_entry = s->selected_entry = te;
3191 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3192 break; /* jump to this file's entry */
3194 slash = strchr(p, '/');
3196 subpath = strndup(path, slash - path);
3198 subpath = strdup(path);
3199 if (subpath == NULL) {
3200 err = got_error_from_errno("strdup");
3204 err = got_object_id_by_path(&tree_id, s->repo, commit,
3209 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3214 err = tree_view_visit_subtree(s, tree);
3216 got_object_tree_close(tree);
3230 static const struct got_error *
3231 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3232 struct commit_queue_entry *entry, const char *path,
3233 const char *head_ref_name, struct got_repository *repo)
3235 const struct got_error *err = NULL;
3236 struct tog_tree_view_state *s;
3237 struct tog_view *tree_view;
3239 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3240 if (tree_view == NULL)
3241 return got_error_from_errno("view_open");
3243 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3246 s = &tree_view->state.tree;
3248 *new_view = tree_view;
3250 if (got_path_is_root_dir(path))
3253 return tree_view_walk_path(s, entry->commit, path);
3256 static const struct got_error *
3257 block_signals_used_by_main_thread(void)
3262 if (sigemptyset(&sigset) == -1)
3263 return got_error_from_errno("sigemptyset");
3265 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3266 if (sigaddset(&sigset, SIGWINCH) == -1)
3267 return got_error_from_errno("sigaddset");
3268 if (sigaddset(&sigset, SIGCONT) == -1)
3269 return got_error_from_errno("sigaddset");
3270 if (sigaddset(&sigset, SIGINT) == -1)
3271 return got_error_from_errno("sigaddset");
3272 if (sigaddset(&sigset, SIGTERM) == -1)
3273 return got_error_from_errno("sigaddset");
3275 /* ncurses handles SIGTSTP */
3276 if (sigaddset(&sigset, SIGTSTP) == -1)
3277 return got_error_from_errno("sigaddset");
3279 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3281 return got_error_set_errno(errcode, "pthread_sigmask");
3287 log_thread(void *arg)
3289 const struct got_error *err = NULL;
3291 struct tog_log_thread_args *a = arg;
3295 * Sync startup with main thread such that we begin our
3296 * work once view_input() has released the mutex.
3298 errcode = pthread_mutex_lock(&tog_mutex);
3300 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3304 err = block_signals_used_by_main_thread();
3306 pthread_mutex_unlock(&tog_mutex);
3310 while (!done && !err && !tog_fatal_signal_received()) {
3311 errcode = pthread_mutex_unlock(&tog_mutex);
3313 err = got_error_set_errno(errcode,
3314 "pthread_mutex_unlock");
3317 err = queue_commits(a);
3319 if (err->code != GOT_ERR_ITER_COMPLETED)
3323 } else if (a->commits_needed > 0 && !a->load_all) {
3326 a->commits_needed--;
3328 a->commits_needed--;
3331 errcode = pthread_mutex_lock(&tog_mutex);
3333 err = got_error_set_errno(errcode,
3334 "pthread_mutex_lock");
3336 } else if (*a->quit)
3338 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3339 *a->first_displayed_entry =
3340 TAILQ_FIRST(&a->limit_commits->head);
3341 *a->selected_entry = *a->first_displayed_entry;
3342 } else if (*a->first_displayed_entry == NULL) {
3343 *a->first_displayed_entry =
3344 TAILQ_FIRST(&a->real_commits->head);
3345 *a->selected_entry = *a->first_displayed_entry;
3348 errcode = pthread_cond_signal(&a->commit_loaded);
3350 err = got_error_set_errno(errcode,
3351 "pthread_cond_signal");
3352 pthread_mutex_unlock(&tog_mutex);
3357 a->commits_needed = 0;
3359 if (a->commits_needed == 0 && !a->load_all) {
3360 errcode = pthread_cond_wait(&a->need_commits,
3363 err = got_error_set_errno(errcode,
3364 "pthread_cond_wait");
3365 pthread_mutex_unlock(&tog_mutex);
3373 a->log_complete = 1;
3374 errcode = pthread_mutex_unlock(&tog_mutex);
3376 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3379 tog_thread_error = 1;
3380 pthread_cond_signal(&a->commit_loaded);
3385 static const struct got_error *
3386 stop_log_thread(struct tog_log_view_state *s)
3388 const struct got_error *err = NULL, *thread_err = NULL;
3393 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3395 return got_error_set_errno(errcode,
3396 "pthread_cond_signal");
3397 errcode = pthread_mutex_unlock(&tog_mutex);
3399 return got_error_set_errno(errcode,
3400 "pthread_mutex_unlock");
3401 errcode = pthread_join(s->thread, (void **)&thread_err);
3403 return got_error_set_errno(errcode, "pthread_join");
3404 errcode = pthread_mutex_lock(&tog_mutex);
3406 return got_error_set_errno(errcode,
3407 "pthread_mutex_lock");
3411 if (s->thread_args.repo) {
3412 err = got_repo_close(s->thread_args.repo);
3413 s->thread_args.repo = NULL;
3416 if (s->thread_args.pack_fds) {
3417 const struct got_error *pack_err =
3418 got_repo_pack_fds_close(s->thread_args.pack_fds);
3421 s->thread_args.pack_fds = NULL;
3424 if (s->thread_args.graph) {
3425 got_commit_graph_close(s->thread_args.graph);
3426 s->thread_args.graph = NULL;
3429 return err ? err : thread_err;
3432 static const struct got_error *
3433 close_log_view(struct tog_view *view)
3435 const struct got_error *err = NULL;
3436 struct tog_log_view_state *s = &view->state.log;
3439 err = stop_log_thread(s);
3441 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3442 if (errcode && err == NULL)
3443 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3445 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3446 if (errcode && err == NULL)
3447 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3449 free_commits(&s->limit_commits);
3450 free_commits(&s->real_commits);
3451 free(s->in_repo_path);
3452 s->in_repo_path = NULL;
3455 free(s->head_ref_name);
3456 s->head_ref_name = NULL;
3461 * We use two queues to implement the limit feature: first consists of
3462 * commits matching the current limit_regex; second is the real queue
3463 * of all known commits (real_commits). When the user starts limiting,
3464 * we swap queues such that all movement and displaying functionality
3465 * works with very slight change.
3467 static const struct got_error *
3468 limit_log_view(struct tog_view *view)
3470 struct tog_log_view_state *s = &view->state.log;
3471 struct commit_queue_entry *entry;
3472 struct tog_view *v = view;
3473 const struct got_error *err = NULL;
3477 if (view_is_hsplit_top(view))
3479 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3482 /* Get the pattern */
3483 wmove(v->window, v->nlines - 1, 0);
3484 wclrtoeol(v->window);
3485 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3486 nodelay(v->window, FALSE);
3489 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3492 nodelay(v->window, TRUE);
3496 if (*pattern == '\0') {
3498 * Safety measure for the situation where the user
3499 * resets limit without previously limiting anything.
3505 * User could have pressed Ctrl+L, which refreshed the
3506 * commit queues, it means we can't save previously
3507 * (before limit took place) displayed entries,
3508 * because they would point to already free'ed memory,
3509 * so we are forced to always select first entry of
3512 s->commits = &s->real_commits;
3513 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3514 s->selected_entry = s->first_displayed_entry;
3521 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3526 /* Clear the screen while loading limit view */
3527 s->first_displayed_entry = NULL;
3528 s->last_displayed_entry = NULL;
3529 s->selected_entry = NULL;
3530 s->commits = &s->limit_commits;
3532 /* Prepare limit queue for new search */
3533 free_commits(&s->limit_commits);
3534 s->limit_commits.ncommits = 0;
3536 /* First process commits, which are in queue already */
3537 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3540 err = match_commit(&have_match, entry->id,
3541 entry->commit, &s->limit_regex);
3546 struct commit_queue_entry *matched;
3548 matched = alloc_commit_queue_entry(entry->commit,
3550 if (matched == NULL) {
3551 err = got_error_from_errno(
3552 "alloc_commit_queue_entry");
3555 matched->commit = entry->commit;
3556 got_object_commit_retain(entry->commit);
3558 matched->idx = s->limit_commits.ncommits;
3559 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3561 s->limit_commits.ncommits++;
3565 /* Second process all the commits, until we fill the screen */
3566 if (s->limit_commits.ncommits < view->nlines - 1 &&
3567 !s->thread_args.log_complete) {
3568 s->thread_args.commits_needed +=
3569 view->nlines - s->limit_commits.ncommits - 1;
3570 err = trigger_log_thread(view, 1);
3575 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3576 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3582 static const struct got_error *
3583 search_start_log_view(struct tog_view *view)
3585 struct tog_log_view_state *s = &view->state.log;
3587 s->matched_entry = NULL;
3588 s->search_entry = NULL;
3592 static const struct got_error *
3593 search_next_log_view(struct tog_view *view)
3595 const struct got_error *err = NULL;
3596 struct tog_log_view_state *s = &view->state.log;
3597 struct commit_queue_entry *entry;
3599 /* Display progress update in log view. */
3600 show_log_view(view);
3604 if (s->search_entry) {
3606 errcode = pthread_mutex_unlock(&tog_mutex);
3608 return got_error_set_errno(errcode,
3609 "pthread_mutex_unlock");
3610 ch = wgetch(view->window);
3611 errcode = pthread_mutex_lock(&tog_mutex);
3613 return got_error_set_errno(errcode,
3614 "pthread_mutex_lock");
3615 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3616 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3619 if (view->searching == TOG_SEARCH_FORWARD)
3620 entry = TAILQ_NEXT(s->search_entry, entry);
3622 entry = TAILQ_PREV(s->search_entry,
3623 commit_queue_head, entry);
3624 } else if (s->matched_entry) {
3626 * If the user has moved the cursor after we hit a match,
3627 * the position from where we should continue searching
3628 * might have changed.
3630 if (view->searching == TOG_SEARCH_FORWARD)
3631 entry = TAILQ_NEXT(s->selected_entry, entry);
3633 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3636 entry = s->selected_entry;
3642 if (entry == NULL) {
3643 if (s->thread_args.log_complete ||
3644 view->searching == TOG_SEARCH_BACKWARD) {
3645 view->search_next_done =
3646 (s->matched_entry == NULL ?
3647 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3648 s->search_entry = NULL;
3652 * Poke the log thread for more commits and return,
3653 * allowing the main loop to make progress. Search
3654 * will resume at s->search_entry once we come back.
3656 s->thread_args.commits_needed++;
3657 return trigger_log_thread(view, 0);
3660 err = match_commit(&have_match, entry->id, entry->commit,
3665 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3666 s->matched_entry = entry;
3670 s->search_entry = entry;
3671 if (view->searching == TOG_SEARCH_FORWARD)
3672 entry = TAILQ_NEXT(entry, entry);
3674 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3677 if (s->matched_entry) {
3678 int cur = s->selected_entry->idx;
3679 while (cur < s->matched_entry->idx) {
3680 err = input_log_view(NULL, view, KEY_DOWN);
3685 while (cur > s->matched_entry->idx) {
3686 err = input_log_view(NULL, view, KEY_UP);
3693 s->search_entry = NULL;
3698 static const struct got_error *
3699 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3700 struct got_repository *repo, const char *head_ref_name,
3701 const char *in_repo_path, int log_branches)
3703 const struct got_error *err = NULL;
3704 struct tog_log_view_state *s = &view->state.log;
3705 struct got_repository *thread_repo = NULL;
3706 struct got_commit_graph *thread_graph = NULL;
3709 if (in_repo_path != s->in_repo_path) {
3710 free(s->in_repo_path);
3711 s->in_repo_path = strdup(in_repo_path);
3712 if (s->in_repo_path == NULL) {
3713 err = got_error_from_errno("strdup");
3718 /* The commit queue only contains commits being displayed. */
3719 TAILQ_INIT(&s->real_commits.head);
3720 s->real_commits.ncommits = 0;
3721 s->commits = &s->real_commits;
3723 TAILQ_INIT(&s->limit_commits.head);
3725 s->limit_commits.ncommits = 0;
3728 if (head_ref_name) {
3729 s->head_ref_name = strdup(head_ref_name);
3730 if (s->head_ref_name == NULL) {
3731 err = got_error_from_errno("strdup");
3735 s->start_id = got_object_id_dup(start_id);
3736 if (s->start_id == NULL) {
3737 err = got_error_from_errno("got_object_id_dup");
3740 s->log_branches = log_branches;
3741 s->use_committer = 1;
3743 STAILQ_INIT(&s->colors);
3744 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3745 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3746 get_color_value("TOG_COLOR_COMMIT"));
3749 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3750 get_color_value("TOG_COLOR_AUTHOR"));
3752 free_colors(&s->colors);
3755 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3756 get_color_value("TOG_COLOR_DATE"));
3758 free_colors(&s->colors);
3763 view->show = show_log_view;
3764 view->input = input_log_view;
3765 view->resize = resize_log_view;
3766 view->close = close_log_view;
3767 view->search_start = search_start_log_view;
3768 view->search_next = search_next_log_view;
3770 if (s->thread_args.pack_fds == NULL) {
3771 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3775 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3776 s->thread_args.pack_fds);
3779 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3783 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3784 s->repo, NULL, NULL);
3788 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3790 err = got_error_set_errno(errcode, "pthread_cond_init");
3793 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3795 err = got_error_set_errno(errcode, "pthread_cond_init");
3799 s->thread_args.commits_needed = view->nlines;
3800 s->thread_args.graph = thread_graph;
3801 s->thread_args.real_commits = &s->real_commits;
3802 s->thread_args.limit_commits = &s->limit_commits;
3803 s->thread_args.in_repo_path = s->in_repo_path;
3804 s->thread_args.start_id = s->start_id;
3805 s->thread_args.repo = thread_repo;
3806 s->thread_args.log_complete = 0;
3807 s->thread_args.quit = &s->quit;
3808 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3809 s->thread_args.selected_entry = &s->selected_entry;
3810 s->thread_args.searching = &view->searching;
3811 s->thread_args.search_next_done = &view->search_next_done;
3812 s->thread_args.regex = &view->regex;
3813 s->thread_args.limiting = &s->limit_view;
3814 s->thread_args.limit_regex = &s->limit_regex;
3815 s->thread_args.limit_commits = &s->limit_commits;
3818 if (view->close == NULL)
3819 close_log_view(view);
3825 static const struct got_error *
3826 show_log_view(struct tog_view *view)
3828 const struct got_error *err;
3829 struct tog_log_view_state *s = &view->state.log;
3831 if (s->thread == NULL) {
3832 int errcode = pthread_create(&s->thread, NULL, log_thread,
3835 return got_error_set_errno(errcode, "pthread_create");
3836 if (s->thread_args.commits_needed > 0) {
3837 err = trigger_log_thread(view, 1);
3843 return draw_commits(view);
3847 log_move_cursor_up(struct tog_view *view, int page, int home)
3849 struct tog_log_view_state *s = &view->state.log;
3851 if (s->first_displayed_entry == NULL)
3853 if (s->selected_entry->idx == 0)
3856 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3858 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3860 if (!page && !home && s->selected > 0)
3863 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3869 static const struct got_error *
3870 log_move_cursor_down(struct tog_view *view, int page)
3872 struct tog_log_view_state *s = &view->state.log;
3873 const struct got_error *err = NULL;
3874 int eos = view->nlines - 2;
3876 if (s->first_displayed_entry == NULL)
3879 if (s->thread_args.log_complete &&
3880 s->selected_entry->idx >= s->commits->ncommits - 1)
3883 if (view_is_hsplit_top(view))
3884 --eos; /* border consumes the last line */
3887 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3890 err = log_scroll_down(view, 1);
3891 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3892 struct commit_queue_entry *entry;
3896 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3897 s->last_displayed_entry = entry;
3898 for (n = 0; n <= eos; n++) {
3901 s->first_displayed_entry = entry;
3902 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3905 s->selected = n - 1;
3907 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3908 s->thread_args.log_complete)
3909 s->selected += MIN(page,
3910 s->commits->ncommits - s->selected_entry->idx - 1);
3912 err = log_scroll_down(view, page);
3918 * We might necessarily overshoot in horizontal
3919 * splits; if so, select the last displayed commit.
3921 if (s->first_displayed_entry && s->last_displayed_entry) {
3922 s->selected = MIN(s->selected,
3923 s->last_displayed_entry->idx -
3924 s->first_displayed_entry->idx);
3929 if (s->thread_args.log_complete &&
3930 s->selected_entry->idx == s->commits->ncommits - 1)
3937 view_get_split(struct tog_view *view, int *y, int *x)
3942 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3943 if (view->child && view->child->resized_y)
3944 *y = view->child->resized_y;
3945 else if (view->resized_y)
3946 *y = view->resized_y;
3948 *y = view_split_begin_y(view->lines);
3949 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3950 if (view->child && view->child->resized_x)
3951 *x = view->child->resized_x;
3952 else if (view->resized_x)
3953 *x = view->resized_x;
3955 *x = view_split_begin_x(view->begin_x);
3959 /* Split view horizontally at y and offset view->state->selected line. */
3960 static const struct got_error *
3961 view_init_hsplit(struct tog_view *view, int y)
3963 const struct got_error *err = NULL;
3967 err = view_resize(view);
3971 err = offset_selection_down(view);
3976 static const struct got_error *
3977 log_goto_line(struct tog_view *view, int nlines)
3979 const struct got_error *err = NULL;
3980 struct tog_log_view_state *s = &view->state.log;
3981 int g, idx = s->selected_entry->idx;
3983 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3989 if (g >= s->first_displayed_entry->idx + 1 &&
3990 g <= s->last_displayed_entry->idx + 1 &&
3991 g - s->first_displayed_entry->idx - 1 < nlines) {
3992 s->selected = g - s->first_displayed_entry->idx - 1;
3998 err = log_move_cursor_down(view, g - idx - 1);
3999 if (!err && g > s->selected_entry->idx + 1)
4000 err = log_move_cursor_down(view,
4001 g - s->first_displayed_entry->idx - 1);
4004 } else if (idx + 1 > g)
4005 log_move_cursor_up(view, idx - g + 1, 0);
4007 if (g < nlines && s->first_displayed_entry->idx == 0)
4008 s->selected = g - 1;
4016 horizontal_scroll_input(struct tog_view *view, int ch)
4022 view->x -= MIN(view->x, 2);
4028 if (view->x + view->ncols / 2 < view->maxx)
4037 view->x = MAX(view->maxx - view->ncols / 2, 0);
4045 static const struct got_error *
4046 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4048 const struct got_error *err = NULL;
4049 struct tog_log_view_state *s = &view->state.log;
4052 if (s->thread_args.load_all) {
4053 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4054 s->thread_args.load_all = 0;
4055 else if (s->thread_args.log_complete) {
4056 err = log_move_cursor_down(view, s->commits->ncommits);
4057 s->thread_args.load_all = 0;
4063 eos = nscroll = view->nlines - 1;
4064 if (view_is_hsplit_top(view))
4068 return log_goto_line(view, eos);
4072 err = limit_log_view(view);
4083 horizontal_scroll_input(view, ch);
4090 log_move_cursor_up(view, 0, 0);
4095 log_move_cursor_up(view, 0, 1);
4105 log_move_cursor_up(view, nscroll, 0);
4112 err = log_move_cursor_down(view, 0);
4115 s->use_committer = !s->use_committer;
4116 view->action = s->use_committer ?
4117 "show committer" : "show commit author";
4122 /* We don't know yet how many commits, so we're forced to
4123 * traverse them all. */
4125 s->thread_args.load_all = 1;
4126 if (!s->thread_args.log_complete)
4127 return trigger_log_thread(view, 0);
4128 err = log_move_cursor_down(view, s->commits->ncommits);
4129 s->thread_args.load_all = 0;
4140 err = log_move_cursor_down(view, nscroll);
4143 if (s->selected > view->nlines - 2)
4144 s->selected = view->nlines - 2;
4145 if (s->selected > s->commits->ncommits - 1)
4146 s->selected = s->commits->ncommits - 1;
4148 if (s->commits->ncommits < view->nlines - 1 &&
4149 !s->thread_args.log_complete) {
4150 s->thread_args.commits_needed += (view->nlines - 1) -
4151 s->commits->ncommits;
4152 err = trigger_log_thread(view, 1);
4158 if (s->selected_entry == NULL)
4160 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4164 if (s->selected_entry == NULL)
4166 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4172 if (ch == KEY_BACKSPACE &&
4173 got_path_is_root_dir(s->in_repo_path))
4175 err = stop_log_thread(s);
4178 if (ch == KEY_BACKSPACE) {
4180 err = got_path_dirname(&parent_path, s->in_repo_path);
4183 free(s->in_repo_path);
4184 s->in_repo_path = parent_path;
4185 s->thread_args.in_repo_path = s->in_repo_path;
4186 } else if (ch == CTRL('l')) {
4187 struct got_object_id *start_id;
4188 err = got_repo_match_object_id(&start_id, NULL,
4189 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4190 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4192 if (s->head_ref_name == NULL ||
4193 err->code != GOT_ERR_NOT_REF)
4195 /* Try to cope with deleted references. */
4196 free(s->head_ref_name);
4197 s->head_ref_name = NULL;
4198 err = got_repo_match_object_id(&start_id,
4199 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4200 &tog_refs, s->repo);
4205 s->start_id = start_id;
4206 s->thread_args.start_id = s->start_id;
4208 s->log_branches = !s->log_branches;
4210 if (s->thread_args.pack_fds == NULL) {
4211 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4215 err = got_repo_open(&s->thread_args.repo,
4216 got_repo_get_path(s->repo), NULL,
4217 s->thread_args.pack_fds);
4221 err = tog_load_refs(s->repo, 0);
4224 err = got_commit_graph_open(&s->thread_args.graph,
4225 s->in_repo_path, !s->log_branches);
4228 err = got_commit_graph_iter_start(s->thread_args.graph,
4229 s->start_id, s->repo, NULL, NULL);
4232 free_commits(&s->real_commits);
4233 free_commits(&s->limit_commits);
4234 s->first_displayed_entry = NULL;
4235 s->last_displayed_entry = NULL;
4236 s->selected_entry = NULL;
4238 s->thread_args.log_complete = 0;
4240 s->thread_args.commits_needed = view->lines;
4241 s->matched_entry = NULL;
4242 s->search_entry = NULL;
4247 err = view_request_new(new_view, view, TOG_VIEW_REF);
4257 static const struct got_error *
4258 apply_unveil(const char *repo_path, const char *worktree_path)
4260 const struct got_error *error;
4263 if (unveil("gmon.out", "rwc") != 0)
4264 return got_error_from_errno2("unveil", "gmon.out");
4266 if (repo_path && unveil(repo_path, "r") != 0)
4267 return got_error_from_errno2("unveil", repo_path);
4269 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4270 return got_error_from_errno2("unveil", worktree_path);
4272 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4273 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4275 error = got_privsep_unveil_exec_helpers();
4279 if (unveil(NULL, NULL) != 0)
4280 return got_error_from_errno("unveil");
4285 static const struct got_error *
4286 init_mock_term(const char *test_script_path)
4288 const struct got_error *err = NULL;
4289 const char *screen_dump_path;
4292 if (test_script_path == NULL || *test_script_path == '\0')
4293 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4295 tog_io.f = fopen(test_script_path, "re");
4296 if (tog_io.f == NULL) {
4297 err = got_error_from_errno_fmt("fopen: %s",
4302 /* test mode, we don't want any output */
4303 tog_io.cout = fopen("/dev/null", "w+");
4304 if (tog_io.cout == NULL) {
4305 err = got_error_from_errno2("fopen", "/dev/null");
4309 in = dup(fileno(tog_io.cout));
4311 err = got_error_from_errno("dup");
4314 tog_io.cin = fdopen(in, "r");
4315 if (tog_io.cin == NULL) {
4316 err = got_error_from_errno("fdopen");
4321 screen_dump_path = getenv("TOG_SCR_DUMP");
4322 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4323 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4324 tog_io.sdump = fopen(screen_dump_path, "we");
4325 if (tog_io.sdump == NULL) {
4326 err = got_error_from_errno2("fopen", screen_dump_path);
4330 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4331 err = got_error_from_errno("fseeko");
4335 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4336 err = got_error_msg(GOT_ERR_IO,
4337 "newterm: failed to initialise curses");
4351 * Override default signal handlers before starting ncurses.
4352 * This should prevent ncurses from installing its own
4353 * broken cleanup() signal handler.
4355 signal(SIGWINCH, tog_sigwinch);
4356 signal(SIGPIPE, tog_sigpipe);
4357 signal(SIGCONT, tog_sigcont);
4358 signal(SIGINT, tog_sigint);
4359 signal(SIGTERM, tog_sigterm);
4361 if (using_mock_io) /* In test mode we use a fake terminal */
4367 halfdelay(1); /* Fast refresh while initial view is loading. */
4370 intrflush(stdscr, FALSE);
4371 keypad(stdscr, TRUE);
4373 if (getenv("TOG_COLORS") != NULL) {
4375 use_default_colors();
4381 static const struct got_error *
4382 set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4384 tog_base_commit.id = got_object_id_dup(
4385 got_worktree_get_base_commit_id(worktree));
4386 if (tog_base_commit.id == NULL)
4387 return got_error_from_errno( "got_object_id_dup");
4389 tog_base_commit.idx = -1;
4391 return got_worktree_get_state(&tog_base_commit.marker, repo,
4395 static const struct got_error *
4396 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4397 struct got_repository *repo, struct got_worktree *worktree)
4399 const struct got_error *err = NULL;
4402 *in_repo_path = strdup("/");
4403 if (*in_repo_path == NULL)
4404 return got_error_from_errno("strdup");
4409 const char *prefix = got_worktree_get_path_prefix(worktree);
4412 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4415 if (asprintf(in_repo_path, "%s%s%s", prefix,
4416 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4418 err = got_error_from_errno("asprintf");
4419 *in_repo_path = NULL;
4423 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4428 static const struct got_error *
4429 cmd_log(int argc, char *argv[])
4431 const struct got_error *error;
4432 struct got_repository *repo = NULL;
4433 struct got_worktree *worktree = NULL;
4434 struct got_object_id *start_id = NULL;
4435 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4436 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4437 struct got_reference *ref = NULL;
4438 const char *head_ref_name = NULL;
4439 int ch, log_branches = 0;
4440 struct tog_view *view;
4441 int *pack_fds = NULL;
4443 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4449 start_commit = optarg;
4452 repo_path = realpath(optarg, NULL);
4453 if (repo_path == NULL)
4454 return got_error_from_errno2("realpath",
4469 error = got_repo_pack_fds_open(&pack_fds);
4473 if (repo_path == NULL) {
4474 cwd = getcwd(NULL, 0);
4476 return got_error_from_errno("getcwd");
4477 error = got_worktree_open(&worktree, cwd, NULL);
4478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4482 strdup(got_worktree_get_repo_path(worktree));
4484 repo_path = strdup(cwd);
4485 if (repo_path == NULL) {
4486 error = got_error_from_errno("strdup");
4491 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4495 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4502 error = apply_unveil(got_repo_get_path(repo),
4503 worktree ? got_worktree_get_root_path(worktree) : NULL);
4507 /* already loaded by tog_log_with_path()? */
4508 if (TAILQ_EMPTY(&tog_refs)) {
4509 error = tog_load_refs(repo, 0);
4514 if (start_commit == NULL) {
4515 error = got_repo_match_object_id(&start_id, &label,
4516 worktree ? got_worktree_get_head_ref_name(worktree) :
4517 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4520 head_ref_name = label;
4522 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4526 if (keyword_idstr != NULL)
4527 start_commit = keyword_idstr;
4529 error = got_ref_open(&ref, repo, start_commit, 0);
4531 head_ref_name = got_ref_get_name(ref);
4532 else if (error->code != GOT_ERR_NOT_REF)
4534 error = got_repo_match_object_id(&start_id, NULL,
4535 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4540 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4542 error = got_error_from_errno("view_open");
4545 error = open_log_view(view, start_id, repo, head_ref_name,
4546 in_repo_path, log_branches);
4551 error = set_tog_base_commit(repo, worktree);
4555 /* Release work tree lock. */
4556 got_worktree_close(worktree);
4560 error = view_loop(view);
4563 free(tog_base_commit.id);
4564 free(keyword_idstr);
4573 const struct got_error *close_err = got_repo_close(repo);
4578 got_worktree_close(worktree);
4580 const struct got_error *pack_err =
4581 got_repo_pack_fds_close(pack_fds);
4593 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4594 "object1 object2\n", getprogname());
4599 match_line(const char *line, regex_t *regex, size_t nmatch,
4600 regmatch_t *regmatch)
4602 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4605 static struct tog_color *
4606 match_color(struct tog_colors *colors, const char *line)
4608 struct tog_color *tc = NULL;
4610 STAILQ_FOREACH(tc, colors, entry) {
4611 if (match_line(line, &tc->regex, 0, NULL))
4618 static const struct got_error *
4619 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4620 WINDOW *window, int skipcol, regmatch_t *regmatch)
4622 const struct got_error *err = NULL;
4624 wchar_t *wline = NULL;
4625 int rme, rms, n, width, scrollx;
4626 int width0 = 0, width1 = 0, width2 = 0;
4627 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4631 rms = regmatch->rm_so;
4632 rme = regmatch->rm_eo;
4634 err = expand_tab(&exstr, line);
4638 /* Split the line into 3 segments, according to match offsets. */
4639 seg0 = strndup(exstr, rms);
4641 err = got_error_from_errno("strndup");
4644 seg1 = strndup(exstr + rms, rme - rms);
4646 err = got_error_from_errno("strndup");
4649 seg2 = strdup(exstr + rme);
4651 err = got_error_from_errno("strndup");
4655 /* draw up to matched token if we haven't scrolled past it */
4656 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4660 n = MAX(width0 - skipcol, 0);
4663 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4664 wlimit, col_tab_align, 1);
4667 waddwstr(window, &wline[scrollx]);
4677 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4681 wlen = wcslen(wline);
4683 width = wcwidth(wline[i]);
4685 /* should not happen, tabs are expanded */
4686 err = got_error(GOT_ERR_RANGE);
4689 if (width0 + w + width > skipcol)
4694 /* draw (visible part of) matched token (if scrolled into it) */
4695 if (width1 - w > 0) {
4696 wattron(window, A_STANDOUT);
4697 waddwstr(window, &wline[i]);
4698 wattroff(window, A_STANDOUT);
4699 wlimit -= (width1 - w);
4700 *wtotal += (width1 - w);
4704 if (wlimit > 0) { /* draw rest of line */
4706 if (skipcol > width0 + width1) {
4707 err = format_line(&wline, &width2, &scrollx, seg2,
4708 skipcol - (width0 + width1), wlimit,
4712 waddwstr(window, &wline[scrollx]);
4714 err = format_line(&wline, &width2, NULL, seg2, 0,
4715 wlimit, col_tab_align, 1);
4718 waddwstr(window, wline);
4732 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4735 int *eof, *first, *selected;
4737 if (view->type == TOG_VIEW_DIFF) {
4738 struct tog_diff_view_state *s = &view->state.diff;
4740 first = &s->first_displayed_line;
4744 } else if (view->type == TOG_VIEW_HELP) {
4745 struct tog_help_view_state *s = &view->state.help;
4747 first = &s->first_displayed_line;
4751 } else if (view->type == TOG_VIEW_BLAME) {
4752 struct tog_blame_view_state *s = &view->state.blame;
4754 first = &s->first_displayed_line;
4755 selected = &s->selected_line;
4761 /* Center gline in the middle of the page like vi(1). */
4762 if (*lineno < view->gline - (view->nlines - 3) / 2)
4764 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4773 *selected = view->gline <= (view->nlines - 3) / 2 ?
4774 view->gline : (view->nlines - 3) / 2 + 1;
4780 static const struct got_error *
4781 draw_file(struct tog_view *view, const char *header)
4783 struct tog_diff_view_state *s = &view->state.diff;
4784 regmatch_t *regmatch = &view->regmatch;
4785 const struct got_error *err;
4788 size_t linesize = 0;
4792 int max_lines = view->nlines;
4793 int nlines = s->nlines;
4796 s->lineno = s->first_displayed_line - 1;
4797 line_offset = s->lines[s->first_displayed_line - 1].offset;
4798 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4799 return got_error_from_errno("fseek");
4801 werase(view->window);
4803 if (view->gline > s->nlines - 1)
4804 view->gline = s->nlines - 1;
4807 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4808 1 : view->gline - (view->nlines - 3) / 2 :
4809 s->lineno + s->selected_line;
4811 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4812 return got_error_from_errno("asprintf");
4813 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4819 if (view_needs_focus_indication(view))
4820 wstandout(view->window);
4821 waddwstr(view->window, wline);
4824 while (width++ < view->ncols)
4825 waddch(view->window, ' ');
4826 if (view_needs_focus_indication(view))
4827 wstandend(view->window);
4837 while (max_lines > 0 && nprinted < max_lines) {
4838 enum got_diff_line_type linetype;
4841 linelen = getline(&line, &linesize, s->f);
4842 if (linelen == -1) {
4848 return got_ferror(s->f, GOT_ERR_IO);
4851 if (++s->lineno < s->first_displayed_line)
4853 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4855 if (s->lineno == view->hiline)
4858 /* Set view->maxx based on full line length. */
4859 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4865 view->maxx = MAX(view->maxx, width);
4869 linetype = s->lines[s->lineno].type;
4870 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4871 linetype < GOT_DIFF_LINE_CONTEXT)
4872 attr |= COLOR_PAIR(linetype);
4874 wattron(view->window, attr);
4875 if (s->first_displayed_line + nprinted == s->matched_line &&
4876 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4877 err = add_matched_line(&width, line, view->ncols, 0,
4878 view->window, view->x, regmatch);
4885 err = format_line(&wline, &width, &skip, line,
4886 view->x, view->ncols, 0, view->x ? 1 : 0);
4891 waddwstr(view->window, &wline[skip]);
4895 if (s->lineno == view->hiline) {
4896 /* highlight full gline length */
4897 while (width++ < view->ncols)
4898 waddch(view->window, ' ');
4900 if (width <= view->ncols - 1)
4901 waddch(view->window, '\n');
4904 wattroff(view->window, attr);
4905 if (++nprinted == 1)
4906 s->first_displayed_line = s->lineno;
4910 s->last_displayed_line = s->first_displayed_line +
4913 s->last_displayed_line = s->first_displayed_line;
4918 while (nprinted < view->nlines) {
4919 waddch(view->window, '\n');
4923 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4929 wstandout(view->window);
4930 waddwstr(view->window, wline);
4933 wstandend(view->window);
4940 get_datestr(time_t *time, char *datebuf)
4942 struct tm mytm, *tm;
4945 tm = gmtime_r(time, &mytm);
4948 s = asctime_r(tm, datebuf);
4951 p = strchr(s, '\n');
4957 static const struct got_error *
4958 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4959 off_t off, uint8_t type)
4961 struct got_diff_line *p;
4963 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4965 return got_error_from_errno("reallocarray");
4967 (*lines)[*nlines].offset = off;
4968 (*lines)[*nlines].type = type;
4974 static const struct got_error *
4975 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4976 struct got_diff_line *s_lines, size_t s_nlines)
4978 struct got_diff_line *p;
4982 if (fseeko(src, 0L, SEEK_SET) == -1)
4983 return got_error_from_errno("fseeko");
4986 r = fread(buf, 1, sizeof(buf), src);
4989 return got_error_from_errno("fread");
4993 if (fwrite(buf, 1, r, dst) != r)
4994 return got_ferror(dst, GOT_ERR_IO);
4997 if (s_nlines == 0 && *d_nlines == 0)
5001 * If commit info was in dst, increment line offsets
5002 * of the appended diff content, but skip s_lines[0]
5003 * because offset zero is already in *d_lines.
5005 if (*d_nlines > 0) {
5006 for (i = 1; i < s_nlines; ++i)
5007 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5015 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5017 /* d_lines is freed in close_diff_view() */
5018 return got_error_from_errno("reallocarray");
5023 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5024 *d_nlines += s_nlines;
5029 static const struct got_error *
5030 write_commit_info(struct got_diff_line **lines, size_t *nlines,
5031 struct got_object_id *commit_id, struct got_reflist_head *refs,
5032 struct got_repository *repo, int ignore_ws, int force_text_diff,
5033 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5035 const struct got_error *err = NULL;
5036 char datebuf[26], *datestr;
5037 struct got_commit_object *commit;
5038 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5039 time_t committer_time;
5040 const char *author, *committer;
5041 char *refs_str = NULL;
5042 struct got_pathlist_entry *pe;
5046 err = build_refs_str(&refs_str, refs, commit_id, repo);
5050 err = got_object_open_as_commit(&commit, repo, commit_id);
5054 err = got_object_id_str(&id_str, commit_id);
5056 err = got_error_from_errno("got_object_id_str");
5060 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5064 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5065 refs_str ? refs_str : "", refs_str ? ")" : "");
5067 err = got_error_from_errno("fprintf");
5071 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5075 n = fprintf(outfile, "from: %s\n",
5076 got_object_commit_get_author(commit));
5078 err = got_error_from_errno("fprintf");
5082 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5086 author = got_object_commit_get_author(commit);
5087 committer = got_object_commit_get_committer(commit);
5088 if (strcmp(author, committer) != 0) {
5089 n = fprintf(outfile, "via: %s\n", committer);
5091 err = got_error_from_errno("fprintf");
5095 err = add_line_metadata(lines, nlines, outoff,
5096 GOT_DIFF_LINE_AUTHOR);
5100 committer_time = got_object_commit_get_committer_time(commit);
5101 datestr = get_datestr(&committer_time, datebuf);
5103 n = fprintf(outfile, "date: %s UTC\n", datestr);
5105 err = got_error_from_errno("fprintf");
5109 err = add_line_metadata(lines, nlines, outoff,
5110 GOT_DIFF_LINE_DATE);
5114 if (got_object_commit_get_nparents(commit) > 1) {
5115 const struct got_object_id_queue *parent_ids;
5116 struct got_object_qid *qid;
5118 parent_ids = got_object_commit_get_parent_ids(commit);
5119 STAILQ_FOREACH(qid, parent_ids, entry) {
5120 err = got_object_id_str(&id_str, &qid->id);
5123 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5125 err = got_error_from_errno("fprintf");
5129 err = add_line_metadata(lines, nlines, outoff,
5130 GOT_DIFF_LINE_META);
5138 err = got_object_commit_get_logmsg(&logmsg, commit);
5142 while ((line = strsep(&s, "\n")) != NULL) {
5143 n = fprintf(outfile, "%s\n", line);
5145 err = got_error_from_errno("fprintf");
5149 err = add_line_metadata(lines, nlines, outoff,
5150 GOT_DIFF_LINE_LOGMSG);
5155 TAILQ_FOREACH(pe, dsa->paths, entry) {
5156 struct got_diff_changed_path *cp = pe->data;
5157 int pad = dsa->max_path_len - pe->path_len + 1;
5159 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5160 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5161 dsa->rm_cols + 1, cp->rm);
5163 err = got_error_from_errno("fprintf");
5167 err = add_line_metadata(lines, nlines, outoff,
5168 GOT_DIFF_LINE_CHANGES);
5173 fputc('\n', outfile);
5175 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5179 n = fprintf(outfile,
5180 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5181 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5182 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5184 err = got_error_from_errno("fprintf");
5188 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5192 fputc('\n', outfile);
5194 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5199 got_object_commit_close(commit);
5208 static const struct got_error *
5209 create_diff(struct tog_diff_view_state *s)
5211 const struct got_error *err = NULL;
5212 FILE *f = NULL, *tmp_diff_file = NULL;
5214 struct got_diff_line *lines = NULL;
5215 struct got_pathlist_head changed_paths;
5217 TAILQ_INIT(&changed_paths);
5220 s->lines = malloc(sizeof(*s->lines));
5221 if (s->lines == NULL)
5222 return got_error_from_errno("malloc");
5227 err = got_error_from_errno("got_opentemp");
5230 tmp_diff_file = got_opentemp();
5231 if (tmp_diff_file == NULL) {
5232 err = got_error_from_errno("got_opentemp");
5235 if (s->f && fclose(s->f) == EOF) {
5236 err = got_error_from_errno("fclose");
5242 err = got_object_get_type(&obj_type, s->repo, s->id1);
5244 err = got_object_get_type(&obj_type, s->repo, s->id2);
5249 case GOT_OBJ_TYPE_BLOB:
5250 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5251 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5252 s->label1, s->label2, tog_diff_algo, s->diff_context,
5253 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5256 case GOT_OBJ_TYPE_TREE:
5257 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5258 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5259 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5260 s->force_text_diff, NULL, s->repo, s->f);
5262 case GOT_OBJ_TYPE_COMMIT: {
5263 const struct got_object_id_queue *parent_ids;
5264 struct got_object_qid *pid;
5265 struct got_commit_object *commit2;
5266 struct got_reflist_head *refs;
5268 struct got_diffstat_cb_arg dsa = {
5271 s->ignore_whitespace,
5276 lines = malloc(sizeof(*lines));
5277 if (lines == NULL) {
5278 err = got_error_from_errno("malloc");
5282 /* build diff first in tmp file then append to commit info */
5283 err = got_diff_objects_as_commits(&lines, &nlines,
5284 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5285 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5286 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5290 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5293 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5294 /* Show commit info if we're diffing to a parent/root commit. */
5295 if (s->id1 == NULL) {
5296 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5297 refs, s->repo, s->ignore_whitespace,
5298 s->force_text_diff, &dsa, s->f);
5302 parent_ids = got_object_commit_get_parent_ids(commit2);
5303 STAILQ_FOREACH(pid, parent_ids, entry) {
5304 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5305 err = write_commit_info(&s->lines,
5306 &s->nlines, s->id2, refs, s->repo,
5307 s->ignore_whitespace,
5308 s->force_text_diff, &dsa, s->f);
5315 got_object_commit_close(commit2);
5317 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5322 err = got_error(GOT_ERR_OBJ_TYPE);
5327 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5328 if (s->f && fflush(s->f) != 0 && err == NULL)
5329 err = got_error_from_errno("fflush");
5330 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5331 err = got_error_from_errno("fclose");
5336 diff_view_indicate_progress(struct tog_view *view)
5338 mvwaddstr(view->window, 0, 0, "diffing...");
5343 static const struct got_error *
5344 search_start_diff_view(struct tog_view *view)
5346 struct tog_diff_view_state *s = &view->state.diff;
5348 s->matched_line = 0;
5353 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5354 size_t *nlines, int **first, int **last, int **match, int **selected)
5356 struct tog_diff_view_state *s = &view->state.diff;
5359 *nlines = s->nlines;
5360 *line_offsets = NULL;
5361 *match = &s->matched_line;
5362 *first = &s->first_displayed_line;
5363 *last = &s->last_displayed_line;
5364 *selected = &s->selected_line;
5367 static const struct got_error *
5368 search_next_view_match(struct tog_view *view)
5370 const struct got_error *err = NULL;
5374 size_t linesize = 0;
5376 off_t *line_offsets;
5378 int *first, *last, *match, *selected;
5380 if (!view->search_setup)
5381 return got_error_msg(GOT_ERR_NOT_IMPL,
5382 "view search not supported");
5383 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5386 if (!view->searching) {
5387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5392 if (view->searching == TOG_SEARCH_FORWARD)
5393 lineno = *first + 1;
5395 lineno = *first - 1;
5397 lineno = *first - 1 + *selected;
5402 if (lineno <= 0 || lineno > nlines) {
5404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5408 if (view->searching == TOG_SEARCH_FORWARD)
5414 offset = view->type == TOG_VIEW_DIFF ?
5415 view->state.diff.lines[lineno - 1].offset :
5416 line_offsets[lineno - 1];
5417 if (fseeko(f, offset, SEEK_SET) != 0) {
5419 return got_error_from_errno("fseeko");
5421 linelen = getline(&line, &linesize, f);
5422 if (linelen != -1) {
5424 err = expand_tab(&exstr, line);
5427 if (match_line(exstr, &view->regex, 1,
5429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5436 if (view->searching == TOG_SEARCH_FORWARD)
5451 static const struct got_error *
5452 close_diff_view(struct tog_view *view)
5454 const struct got_error *err = NULL;
5455 struct tog_diff_view_state *s = &view->state.diff;
5461 if (s->f && fclose(s->f) == EOF)
5462 err = got_error_from_errno("fclose");
5464 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5465 err = got_error_from_errno("fclose");
5467 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5468 err = got_error_from_errno("fclose");
5470 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5471 err = got_error_from_errno("close");
5473 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5474 err = got_error_from_errno("close");
5482 static const struct got_error *
5483 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5484 struct got_object_id *id2, const char *label1, const char *label2,
5485 int diff_context, int ignore_whitespace, int force_text_diff,
5486 struct tog_view *parent_view, struct got_repository *repo)
5488 const struct got_error *err;
5489 struct tog_diff_view_state *s = &view->state.diff;
5491 memset(s, 0, sizeof(*s));
5495 if (id1 != NULL && id2 != NULL) {
5498 err = got_object_get_type(&type1, repo, id1);
5501 err = got_object_get_type(&type2, repo, id2);
5505 if (type1 != type2) {
5506 err = got_error(GOT_ERR_OBJ_TYPE);
5510 s->first_displayed_line = 1;
5511 s->last_displayed_line = view->nlines;
5512 s->selected_line = 1;
5520 s->id1 = got_object_id_dup(id1);
5521 if (s->id1 == NULL) {
5522 err = got_error_from_errno("got_object_id_dup");
5528 s->id2 = got_object_id_dup(id2);
5529 if (s->id2 == NULL) {
5530 err = got_error_from_errno("got_object_id_dup");
5534 s->f1 = got_opentemp();
5535 if (s->f1 == NULL) {
5536 err = got_error_from_errno("got_opentemp");
5540 s->f2 = got_opentemp();
5541 if (s->f2 == NULL) {
5542 err = got_error_from_errno("got_opentemp");
5546 s->fd1 = got_opentempfd();
5548 err = got_error_from_errno("got_opentempfd");
5552 s->fd2 = got_opentempfd();
5554 err = got_error_from_errno("got_opentempfd");
5558 s->diff_context = diff_context;
5559 s->ignore_whitespace = ignore_whitespace;
5560 s->force_text_diff = force_text_diff;
5561 s->parent_view = parent_view;
5564 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5567 rc = init_pair(GOT_DIFF_LINE_MINUS,
5568 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5570 rc = init_pair(GOT_DIFF_LINE_PLUS,
5571 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5573 rc = init_pair(GOT_DIFF_LINE_HUNK,
5574 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5576 rc = init_pair(GOT_DIFF_LINE_META,
5577 get_color_value("TOG_COLOR_DIFF_META"), -1);
5579 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5580 get_color_value("TOG_COLOR_DIFF_META"), -1);
5582 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5583 get_color_value("TOG_COLOR_DIFF_META"), -1);
5585 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5586 get_color_value("TOG_COLOR_DIFF_META"), -1);
5588 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5589 get_color_value("TOG_COLOR_AUTHOR"), -1);
5591 rc = init_pair(GOT_DIFF_LINE_DATE,
5592 get_color_value("TOG_COLOR_DATE"), -1);
5594 err = got_error(GOT_ERR_RANGE);
5599 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5600 view_is_splitscreen(view))
5601 show_log_view(parent_view); /* draw border */
5602 diff_view_indicate_progress(view);
5604 err = create_diff(s);
5606 view->show = show_diff_view;
5607 view->input = input_diff_view;
5608 view->reset = reset_diff_view;
5609 view->close = close_diff_view;
5610 view->search_start = search_start_diff_view;
5611 view->search_setup = search_setup_diff_view;
5612 view->search_next = search_next_view_match;
5615 if (view->close == NULL)
5616 close_diff_view(view);
5622 static const struct got_error *
5623 show_diff_view(struct tog_view *view)
5625 const struct got_error *err;
5626 struct tog_diff_view_state *s = &view->state.diff;
5627 char *id_str1 = NULL, *id_str2, *header;
5628 const char *label1, *label2;
5631 err = got_object_id_str(&id_str1, s->id1);
5634 label1 = s->label1 ? s->label1 : id_str1;
5636 label1 = "/dev/null";
5638 err = got_object_id_str(&id_str2, s->id2);
5641 label2 = s->label2 ? s->label2 : id_str2;
5643 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5644 err = got_error_from_errno("asprintf");
5652 err = draw_file(view, header);
5657 static const struct got_error *
5658 set_selected_commit(struct tog_diff_view_state *s,
5659 struct commit_queue_entry *entry)
5661 const struct got_error *err;
5662 const struct got_object_id_queue *parent_ids;
5663 struct got_commit_object *selected_commit;
5664 struct got_object_qid *pid;
5667 s->id2 = got_object_id_dup(entry->id);
5669 return got_error_from_errno("got_object_id_dup");
5671 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5674 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5676 pid = STAILQ_FIRST(parent_ids);
5677 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5678 got_object_commit_close(selected_commit);
5682 static const struct got_error *
5683 reset_diff_view(struct tog_view *view)
5685 struct tog_diff_view_state *s = &view->state.diff;
5688 wclear(view->window);
5689 s->first_displayed_line = 1;
5690 s->last_displayed_line = view->nlines;
5691 s->matched_line = 0;
5692 diff_view_indicate_progress(view);
5693 return create_diff(s);
5697 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5701 i = start = s->first_displayed_line - 1;
5703 while (s->lines[i].type != type) {
5707 return; /* do nothing, requested type not in file */
5710 s->selected_line = 1;
5711 s->first_displayed_line = i;
5715 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5719 i = start = s->first_displayed_line + 1;
5721 while (s->lines[i].type != type) {
5722 if (i == s->nlines - 1)
5725 return; /* do nothing, requested type not in file */
5728 s->selected_line = 1;
5729 s->first_displayed_line = i;
5732 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5734 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5737 static const struct got_error *
5738 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5740 const struct got_error *err = NULL;
5741 struct tog_diff_view_state *s = &view->state.diff;
5742 struct tog_log_view_state *ls;
5743 struct commit_queue_entry *old_selected_entry;
5745 size_t linesize = 0;
5747 int i, nscroll = view->nlines - 1, up = 0;
5749 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5758 horizontal_scroll_input(view, ch);
5763 s->force_text_diff = !s->force_text_diff;
5764 view->action = s->force_text_diff ?
5765 "force ASCII text enabled" :
5766 "force ASCII text disabled";
5768 else if (ch == 'w') {
5769 s->ignore_whitespace = !s->ignore_whitespace;
5770 view->action = s->ignore_whitespace ?
5771 "ignore whitespace enabled" :
5772 "ignore whitespace disabled";
5774 err = reset_diff_view(view);
5778 s->first_displayed_line = 1;
5787 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5793 if (s->first_displayed_line > 1)
5794 s->first_displayed_line--;
5805 if (s->first_displayed_line == 1) {
5810 while (i++ < nscroll && s->first_displayed_line > 1)
5811 s->first_displayed_line--;
5817 s->first_displayed_line++;
5834 while (!s->eof && i++ < nscroll) {
5835 linelen = getline(&line, &linesize, s->f);
5836 s->first_displayed_line++;
5837 if (linelen == -1) {
5841 err = got_ferror(s->f, GOT_ERR_IO);
5848 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5851 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5854 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5857 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5860 if (s->diff_context > 0) {
5862 s->matched_line = 0;
5863 diff_view_indicate_progress(view);
5864 err = create_diff(s);
5865 if (s->first_displayed_line + view->nlines - 1 >
5867 s->first_displayed_line = 1;
5868 s->last_displayed_line = view->nlines;
5874 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5876 s->matched_line = 0;
5877 diff_view_indicate_progress(view);
5878 err = create_diff(s);
5890 if (s->parent_view == NULL) {
5894 s->parent_view->count = view->count;
5896 if (s->parent_view->type == TOG_VIEW_LOG) {
5897 ls = &s->parent_view->state.log;
5898 old_selected_entry = ls->selected_entry;
5900 err = input_log_view(NULL, s->parent_view,
5901 up ? KEY_UP : KEY_DOWN);
5904 view->count = s->parent_view->count;
5906 if (old_selected_entry == ls->selected_entry)
5909 err = set_selected_commit(s, ls->selected_entry);
5912 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5913 struct tog_blame_view_state *bs;
5914 struct got_object_id *id, *prev_id;
5916 bs = &s->parent_view->state.blame;
5917 prev_id = get_annotation_for_line(bs->blame.lines,
5918 bs->blame.nlines, bs->last_diffed_line);
5920 err = input_blame_view(&view, s->parent_view,
5921 up ? KEY_UP : KEY_DOWN);
5924 view->count = s->parent_view->count;
5926 if (prev_id == NULL)
5928 id = get_selected_commit_id(bs->blame.lines,
5929 bs->blame.nlines, bs->first_displayed_line,
5934 if (!got_object_id_cmp(prev_id, id))
5937 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5941 s->first_displayed_line = 1;
5942 s->last_displayed_line = view->nlines;
5943 s->matched_line = 0;
5946 diff_view_indicate_progress(view);
5947 err = create_diff(s);
5957 static const struct got_error *
5958 cmd_diff(int argc, char *argv[])
5960 const struct got_error *error;
5961 struct got_repository *repo = NULL;
5962 struct got_worktree *worktree = NULL;
5963 struct got_object_id *id1 = NULL, *id2 = NULL;
5964 char *repo_path = NULL, *cwd = NULL;
5965 char *id_str1 = NULL, *id_str2 = NULL;
5966 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
5967 char *label1 = NULL, *label2 = NULL;
5968 int diff_context = 3, ignore_whitespace = 0;
5969 int ch, force_text_diff = 0;
5971 struct tog_view *view;
5972 int *pack_fds = NULL;
5974 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5977 force_text_diff = 1;
5980 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5983 errx(1, "number of context lines is %s: %s",
5987 repo_path = realpath(optarg, NULL);
5988 if (repo_path == NULL)
5989 return got_error_from_errno2("realpath",
5991 got_path_strip_trailing_slashes(repo_path);
5994 ignore_whitespace = 1;
6006 usage_diff(); /* TODO show local worktree changes */
6007 } else if (argc == 2) {
6013 error = got_repo_pack_fds_open(&pack_fds);
6017 if (repo_path == NULL) {
6018 cwd = getcwd(NULL, 0);
6020 return got_error_from_errno("getcwd");
6021 error = got_worktree_open(&worktree, cwd, NULL);
6022 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6026 strdup(got_worktree_get_repo_path(worktree));
6028 repo_path = strdup(cwd);
6029 if (repo_path == NULL) {
6030 error = got_error_from_errno("strdup");
6035 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6041 error = apply_unveil(got_repo_get_path(repo), NULL);
6045 error = tog_load_refs(repo, 0);
6049 if (id_str1 != NULL) {
6050 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6054 if (keyword_idstr1 != NULL)
6055 id_str1 = keyword_idstr1;
6057 if (id_str2 != NULL) {
6058 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6062 if (keyword_idstr2 != NULL)
6063 id_str2 = keyword_idstr2;
6066 error = got_repo_match_object_id(&id1, &label1, id_str1,
6067 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6071 error = got_repo_match_object_id(&id2, &label2, id_str2,
6072 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6076 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6078 error = got_error_from_errno("view_open");
6081 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6082 ignore_whitespace, force_text_diff, NULL, repo);
6087 error = set_tog_base_commit(repo, worktree);
6092 error = view_loop(view);
6095 free(tog_base_commit.id);
6096 free(keyword_idstr1);
6097 free(keyword_idstr2);
6103 const struct got_error *close_err = got_repo_close(repo);
6108 got_worktree_close(worktree);
6110 const struct got_error *pack_err =
6111 got_repo_pack_fds_close(pack_fds);
6124 "usage: %s blame [-c commit] [-r repository-path] path\n",
6129 struct tog_blame_line {
6131 struct got_object_id *id;
6134 static const struct got_error *
6135 draw_blame(struct tog_view *view)
6137 struct tog_blame_view_state *s = &view->state.blame;
6138 struct tog_blame *blame = &s->blame;
6139 regmatch_t *regmatch = &view->regmatch;
6140 const struct got_error *err;
6141 int lineno = 0, nprinted = 0;
6143 size_t linesize = 0;
6147 struct tog_blame_line *blame_line;
6148 struct got_object_id *prev_id = NULL;
6150 struct tog_color *tc;
6152 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6157 werase(view->window);
6159 if (asprintf(&line, "commit %s", id_str) == -1) {
6160 err = got_error_from_errno("asprintf");
6165 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6170 if (view_needs_focus_indication(view))
6171 wstandout(view->window);
6172 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6174 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6175 waddwstr(view->window, wline);
6176 while (width++ < view->ncols)
6177 waddch(view->window, ' ');
6179 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6180 if (view_needs_focus_indication(view))
6181 wstandend(view->window);
6185 if (view->gline > blame->nlines)
6186 view->gline = blame->nlines;
6188 if (tog_io.wait_for_ui) {
6189 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6192 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6194 return got_error_set_errno(rc, "pthread_cond_wait");
6195 tog_io.wait_for_ui = 0;
6198 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6199 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6200 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6202 return got_error_from_errno("asprintf");
6205 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6210 waddwstr(view->window, wline);
6213 if (width < view->ncols - 1)
6214 waddch(view->window, '\n');
6218 while (nprinted < view->nlines - 2) {
6219 linelen = getline(&line, &linesize, blame->f);
6220 if (linelen == -1) {
6221 if (feof(blame->f)) {
6226 return got_ferror(blame->f, GOT_ERR_IO);
6228 if (++lineno < s->first_displayed_line)
6230 if (view->gline && !gotoline(view, &lineno, &nprinted))
6233 /* Set view->maxx based on full line length. */
6234 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6241 view->maxx = MAX(view->maxx, width);
6243 if (nprinted == s->selected_line - 1)
6244 wstandout(view->window);
6246 if (blame->nlines > 0) {
6247 blame_line = &blame->lines[lineno - 1];
6248 if (blame_line->annotated && prev_id &&
6249 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6250 !(nprinted == s->selected_line - 1)) {
6251 waddstr(view->window, " ");
6252 } else if (blame_line->annotated) {
6254 err = got_object_id_str(&id_str,
6260 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6262 wattr_on(view->window,
6263 COLOR_PAIR(tc->colorpair), NULL);
6264 wprintw(view->window, "%.8s", id_str);
6266 wattr_off(view->window,
6267 COLOR_PAIR(tc->colorpair), NULL);
6269 prev_id = blame_line->id;
6271 waddstr(view->window, "........");
6275 waddstr(view->window, "........");
6279 if (nprinted == s->selected_line - 1)
6280 wstandend(view->window);
6281 waddstr(view->window, " ");
6283 if (view->ncols <= 9) {
6285 } else if (s->first_displayed_line + nprinted ==
6287 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6288 err = add_matched_line(&width, line, view->ncols - 9, 9,
6289 view->window, view->x, regmatch);
6297 err = format_line(&wline, &width, &skip, line,
6298 view->x, view->ncols - 9, 9, 1);
6303 waddwstr(view->window, &wline[skip]);
6309 if (width <= view->ncols - 1)
6310 waddch(view->window, '\n');
6311 if (++nprinted == 1)
6312 s->first_displayed_line = lineno;
6315 s->last_displayed_line = lineno;
6322 static const struct got_error *
6323 blame_cb(void *arg, int nlines, int lineno,
6324 struct got_commit_object *commit, struct got_object_id *id)
6326 const struct got_error *err = NULL;
6327 struct tog_blame_cb_args *a = arg;
6328 struct tog_blame_line *line;
6331 if (nlines != a->nlines ||
6332 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6333 return got_error(GOT_ERR_RANGE);
6335 errcode = pthread_mutex_lock(&tog_mutex);
6337 return got_error_set_errno(errcode, "pthread_mutex_lock");
6339 if (*a->quit) { /* user has quit the blame view */
6340 err = got_error(GOT_ERR_ITER_COMPLETED);
6345 goto done; /* no change in this commit */
6347 line = &a->lines[lineno - 1];
6348 if (line->annotated)
6351 line->id = got_object_id_dup(id);
6352 if (line->id == NULL) {
6353 err = got_error_from_errno("got_object_id_dup");
6356 line->annotated = 1;
6358 errcode = pthread_mutex_unlock(&tog_mutex);
6360 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6365 blame_thread(void *arg)
6367 const struct got_error *err, *close_err;
6368 struct tog_blame_thread_args *ta = arg;
6369 struct tog_blame_cb_args *a = ta->cb_args;
6370 int errcode, fd1 = -1, fd2 = -1;
6371 FILE *f1 = NULL, *f2 = NULL;
6373 fd1 = got_opentempfd();
6375 return (void *)got_error_from_errno("got_opentempfd");
6377 fd2 = got_opentempfd();
6379 err = got_error_from_errno("got_opentempfd");
6383 f1 = got_opentemp();
6385 err = (void *)got_error_from_errno("got_opentemp");
6388 f2 = got_opentemp();
6390 err = (void *)got_error_from_errno("got_opentemp");
6394 err = block_signals_used_by_main_thread();
6398 err = got_blame(ta->path, a->commit_id, ta->repo,
6399 tog_diff_algo, blame_cb, ta->cb_args,
6400 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6401 if (err && err->code == GOT_ERR_CANCELLED)
6404 errcode = pthread_mutex_lock(&tog_mutex);
6406 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6410 close_err = got_repo_close(ta->repo);
6416 if (tog_io.wait_for_ui) {
6417 errcode = pthread_cond_signal(&ta->blame_complete);
6418 if (errcode && err == NULL)
6419 err = got_error_set_errno(errcode,
6420 "pthread_cond_signal");
6423 errcode = pthread_mutex_unlock(&tog_mutex);
6424 if (errcode && err == NULL)
6425 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6428 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6429 err = got_error_from_errno("close");
6430 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6431 err = got_error_from_errno("close");
6432 if (f1 && fclose(f1) == EOF && err == NULL)
6433 err = got_error_from_errno("fclose");
6434 if (f2 && fclose(f2) == EOF && err == NULL)
6435 err = got_error_from_errno("fclose");
6440 static struct got_object_id *
6441 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6442 int first_displayed_line, int selected_line)
6444 struct tog_blame_line *line;
6449 line = &lines[first_displayed_line - 1 + selected_line - 1];
6450 if (!line->annotated)
6456 static struct got_object_id *
6457 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6460 struct tog_blame_line *line;
6462 if (nlines <= 0 || lineno >= nlines)
6465 line = &lines[lineno - 1];
6466 if (!line->annotated)
6472 static const struct got_error *
6473 stop_blame(struct tog_blame *blame)
6475 const struct got_error *err = NULL;
6478 if (blame->thread) {
6480 errcode = pthread_mutex_unlock(&tog_mutex);
6482 return got_error_set_errno(errcode,
6483 "pthread_mutex_unlock");
6484 errcode = pthread_join(blame->thread, (void **)&err);
6486 return got_error_set_errno(errcode, "pthread_join");
6487 errcode = pthread_mutex_lock(&tog_mutex);
6489 return got_error_set_errno(errcode,
6490 "pthread_mutex_lock");
6491 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6493 blame->thread = NULL;
6495 if (blame->thread_args.repo) {
6496 const struct got_error *close_err;
6497 close_err = got_repo_close(blame->thread_args.repo);
6500 blame->thread_args.repo = NULL;
6503 if (fclose(blame->f) == EOF && err == NULL)
6504 err = got_error_from_errno("fclose");
6508 for (i = 0; i < blame->nlines; i++)
6509 free(blame->lines[i].id);
6511 blame->lines = NULL;
6513 free(blame->cb_args.commit_id);
6514 blame->cb_args.commit_id = NULL;
6515 if (blame->pack_fds) {
6516 const struct got_error *pack_err =
6517 got_repo_pack_fds_close(blame->pack_fds);
6520 blame->pack_fds = NULL;
6525 static const struct got_error *
6526 cancel_blame_view(void *arg)
6528 const struct got_error *err = NULL;
6532 errcode = pthread_mutex_lock(&tog_mutex);
6534 return got_error_set_errno(errcode,
6535 "pthread_mutex_unlock");
6538 err = got_error(GOT_ERR_CANCELLED);
6540 errcode = pthread_mutex_unlock(&tog_mutex);
6542 return got_error_set_errno(errcode,
6543 "pthread_mutex_lock");
6548 static const struct got_error *
6549 run_blame(struct tog_view *view)
6551 struct tog_blame_view_state *s = &view->state.blame;
6552 struct tog_blame *blame = &s->blame;
6553 const struct got_error *err = NULL;
6554 struct got_commit_object *commit = NULL;
6555 struct got_blob_object *blob = NULL;
6556 struct got_repository *thread_repo = NULL;
6557 struct got_object_id *obj_id = NULL;
6558 int obj_type, fd = -1;
6559 int *pack_fds = NULL;
6561 err = got_object_open_as_commit(&commit, s->repo,
6562 &s->blamed_commit->id);
6566 fd = got_opentempfd();
6568 err = got_error_from_errno("got_opentempfd");
6572 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6576 err = got_object_get_type(&obj_type, s->repo, obj_id);
6580 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6581 err = got_error(GOT_ERR_OBJ_TYPE);
6585 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6588 blame->f = got_opentemp();
6589 if (blame->f == NULL) {
6590 err = got_error_from_errno("got_opentemp");
6593 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6594 &blame->line_offsets, blame->f, blob);
6597 if (blame->nlines == 0) {
6598 s->blame_complete = 1;
6602 /* Don't include \n at EOF in the blame line count. */
6603 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6606 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6607 if (blame->lines == NULL) {
6608 err = got_error_from_errno("calloc");
6612 err = got_repo_pack_fds_open(&pack_fds);
6615 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6620 blame->pack_fds = pack_fds;
6621 blame->cb_args.view = view;
6622 blame->cb_args.lines = blame->lines;
6623 blame->cb_args.nlines = blame->nlines;
6624 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6625 if (blame->cb_args.commit_id == NULL) {
6626 err = got_error_from_errno("got_object_id_dup");
6629 blame->cb_args.quit = &s->done;
6631 blame->thread_args.path = s->path;
6632 blame->thread_args.repo = thread_repo;
6633 blame->thread_args.cb_args = &blame->cb_args;
6634 blame->thread_args.complete = &s->blame_complete;
6635 blame->thread_args.cancel_cb = cancel_blame_view;
6636 blame->thread_args.cancel_arg = &s->done;
6637 s->blame_complete = 0;
6639 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6640 s->first_displayed_line = 1;
6641 s->last_displayed_line = view->nlines;
6642 s->selected_line = 1;
6644 s->matched_line = 0;
6648 got_object_commit_close(commit);
6649 if (fd != -1 && close(fd) == -1 && err == NULL)
6650 err = got_error_from_errno("close");
6652 got_object_blob_close(blob);
6659 static const struct got_error *
6660 open_blame_view(struct tog_view *view, char *path,
6661 struct got_object_id *commit_id, struct got_repository *repo)
6663 const struct got_error *err = NULL;
6664 struct tog_blame_view_state *s = &view->state.blame;
6666 STAILQ_INIT(&s->blamed_commits);
6668 s->path = strdup(path);
6669 if (s->path == NULL)
6670 return got_error_from_errno("strdup");
6672 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6678 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6679 s->first_displayed_line = 1;
6680 s->last_displayed_line = view->nlines;
6681 s->selected_line = 1;
6682 s->blame_complete = 0;
6684 s->commit_id = commit_id;
6685 memset(&s->blame, 0, sizeof(s->blame));
6687 STAILQ_INIT(&s->colors);
6688 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6689 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6690 get_color_value("TOG_COLOR_COMMIT"));
6695 view->show = show_blame_view;
6696 view->input = input_blame_view;
6697 view->reset = reset_blame_view;
6698 view->close = close_blame_view;
6699 view->search_start = search_start_blame_view;
6700 view->search_setup = search_setup_blame_view;
6701 view->search_next = search_next_view_match;
6703 if (using_mock_io) {
6704 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6707 rc = pthread_cond_init(&bta->blame_complete, NULL);
6709 return got_error_set_errno(rc, "pthread_cond_init");
6712 return run_blame(view);
6715 static const struct got_error *
6716 close_blame_view(struct tog_view *view)
6718 const struct got_error *err = NULL;
6719 struct tog_blame_view_state *s = &view->state.blame;
6721 if (s->blame.thread)
6722 err = stop_blame(&s->blame);
6724 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6725 struct got_object_qid *blamed_commit;
6726 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6727 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6728 got_object_qid_free(blamed_commit);
6731 if (using_mock_io) {
6732 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6735 rc = pthread_cond_destroy(&bta->blame_complete);
6736 if (rc && err == NULL)
6737 err = got_error_set_errno(rc, "pthread_cond_destroy");
6741 free_colors(&s->colors);
6745 static const struct got_error *
6746 search_start_blame_view(struct tog_view *view)
6748 struct tog_blame_view_state *s = &view->state.blame;
6750 s->matched_line = 0;
6755 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6756 size_t *nlines, int **first, int **last, int **match, int **selected)
6758 struct tog_blame_view_state *s = &view->state.blame;
6761 *nlines = s->blame.nlines;
6762 *line_offsets = s->blame.line_offsets;
6763 *match = &s->matched_line;
6764 *first = &s->first_displayed_line;
6765 *last = &s->last_displayed_line;
6766 *selected = &s->selected_line;
6769 static const struct got_error *
6770 show_blame_view(struct tog_view *view)
6772 const struct got_error *err = NULL;
6773 struct tog_blame_view_state *s = &view->state.blame;
6776 if (s->blame.thread == NULL && !s->blame_complete) {
6777 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6778 &s->blame.thread_args);
6780 return got_error_set_errno(errcode, "pthread_create");
6783 halfdelay(1); /* fast refresh while annotating */
6786 if (s->blame_complete && !using_mock_io)
6787 halfdelay(10); /* disable fast refresh */
6789 err = draw_blame(view);
6795 static const struct got_error *
6796 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6797 struct got_repository *repo, struct got_object_id *id)
6799 struct tog_view *log_view;
6800 const struct got_error *err = NULL;
6804 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6805 if (log_view == NULL)
6806 return got_error_from_errno("view_open");
6808 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6810 view_close(log_view);
6812 *new_view = log_view;
6817 static const struct got_error *
6818 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6820 const struct got_error *err = NULL, *thread_err = NULL;
6821 struct tog_view *diff_view;
6822 struct tog_blame_view_state *s = &view->state.blame;
6823 int eos, nscroll, begin_y = 0, begin_x = 0;
6825 eos = nscroll = view->nlines - 2;
6826 if (view_is_hsplit_top(view))
6836 horizontal_scroll_input(view, ch);
6843 s->selected_line = 1;
6844 s->first_displayed_line = 1;
6849 if (s->blame.nlines < eos) {
6850 s->selected_line = s->blame.nlines;
6851 s->first_displayed_line = 1;
6853 s->selected_line = eos;
6854 s->first_displayed_line = s->blame.nlines - (eos - 1);
6861 if (s->selected_line > 1)
6863 else if (s->selected_line == 1 &&
6864 s->first_displayed_line > 1)
6865 s->first_displayed_line--;
6876 if (s->first_displayed_line == 1) {
6877 if (view->count > 1)
6879 s->selected_line = MAX(1, s->selected_line - nscroll);
6883 if (s->first_displayed_line > nscroll)
6884 s->first_displayed_line -= nscroll;
6886 s->first_displayed_line = 1;
6891 if (s->selected_line < eos && s->first_displayed_line +
6892 s->selected_line <= s->blame.nlines)
6894 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6895 s->first_displayed_line++;
6901 struct got_object_id *id = NULL;
6904 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6905 s->first_displayed_line, s->selected_line);
6909 struct got_commit_object *commit, *pcommit;
6910 struct got_object_qid *pid;
6911 struct got_object_id *blob_id = NULL;
6913 err = got_object_open_as_commit(&commit,
6918 got_object_commit_get_parent_ids(commit));
6920 got_object_commit_close(commit);
6923 /* Check if path history ends here. */
6924 err = got_object_open_as_commit(&pcommit,
6928 err = got_object_id_by_path(&blob_id, s->repo,
6930 got_object_commit_close(pcommit);
6932 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6934 got_object_commit_close(commit);
6937 err = got_object_get_type(&obj_type, s->repo,
6940 /* Can't blame non-blob type objects. */
6941 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6942 got_object_commit_close(commit);
6945 err = got_object_qid_alloc(&s->blamed_commit,
6947 got_object_commit_close(commit);
6949 if (got_object_id_cmp(id,
6950 &s->blamed_commit->id) == 0)
6952 err = got_object_qid_alloc(&s->blamed_commit,
6958 thread_err = stop_blame(&s->blame);
6962 STAILQ_INSERT_HEAD(&s->blamed_commits,
6963 s->blamed_commit, entry);
6964 err = run_blame(view);
6970 struct got_object_qid *first;
6973 first = STAILQ_FIRST(&s->blamed_commits);
6974 if (!got_object_id_cmp(&first->id, s->commit_id))
6977 thread_err = stop_blame(&s->blame);
6981 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6982 got_object_qid_free(s->blamed_commit);
6984 STAILQ_FIRST(&s->blamed_commits);
6985 err = run_blame(view);
6992 s->id_to_log = get_selected_commit_id(s->blame.lines,
6993 s->blame.nlines, s->first_displayed_line, s->selected_line);
6995 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6999 struct got_object_id *id = NULL;
7000 struct got_object_qid *pid;
7001 struct got_commit_object *commit = NULL;
7004 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7005 s->first_displayed_line, s->selected_line);
7008 err = got_object_open_as_commit(&commit, s->repo, id);
7011 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7013 /* traversed from diff view, release diff resources */
7014 err = close_diff_view(*new_view);
7017 diff_view = *new_view;
7019 if (view_is_parent_view(view))
7020 view_get_split(view, &begin_y, &begin_x);
7022 diff_view = view_open(0, 0, begin_y, begin_x,
7024 if (diff_view == NULL) {
7025 got_object_commit_close(commit);
7026 err = got_error_from_errno("view_open");
7030 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7031 id, NULL, NULL, 3, 0, 0, view, s->repo);
7032 got_object_commit_close(commit);
7035 s->last_diffed_line = s->first_displayed_line - 1 +
7038 break; /* still open from active diff view */
7039 if (view_is_parent_view(view) &&
7040 view->mode == TOG_VIEW_SPLIT_HRZN) {
7041 err = view_init_hsplit(view, begin_y);
7047 diff_view->focussed = 1;
7048 diff_view->mode = view->mode;
7049 diff_view->nlines = view->lines - begin_y;
7050 if (view_is_parent_view(view)) {
7051 view_transfer_size(diff_view, view);
7052 err = view_close_child(view);
7055 err = view_set_child(view, diff_view);
7058 view->focus_child = 1;
7060 *new_view = diff_view;
7073 if (s->last_displayed_line >= s->blame.nlines &&
7074 s->selected_line >= MIN(s->blame.nlines,
7075 view->nlines - 2)) {
7079 if (s->last_displayed_line >= s->blame.nlines &&
7080 s->selected_line < view->nlines - 2) {
7082 MIN(nscroll, s->last_displayed_line -
7083 s->first_displayed_line - s->selected_line + 1);
7085 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7086 s->first_displayed_line += nscroll;
7088 s->first_displayed_line =
7089 s->blame.nlines - (view->nlines - 3);
7092 if (s->selected_line > view->nlines - 2) {
7093 s->selected_line = MIN(s->blame.nlines,
7101 return thread_err ? thread_err : err;
7104 static const struct got_error *
7105 reset_blame_view(struct tog_view *view)
7107 const struct got_error *err;
7108 struct tog_blame_view_state *s = &view->state.blame;
7112 err = stop_blame(&s->blame);
7116 return run_blame(view);
7119 static const struct got_error *
7120 cmd_blame(int argc, char *argv[])
7122 const struct got_error *error;
7123 struct got_repository *repo = NULL;
7124 struct got_worktree *worktree = NULL;
7125 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7126 char *link_target = NULL;
7127 struct got_object_id *commit_id = NULL;
7128 struct got_commit_object *commit = NULL;
7129 char *keyword_idstr = NULL, *commit_id_str = NULL;
7131 struct tog_view *view = NULL;
7132 int *pack_fds = NULL;
7134 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7137 commit_id_str = optarg;
7140 repo_path = realpath(optarg, NULL);
7141 if (repo_path == NULL)
7142 return got_error_from_errno2("realpath",
7157 error = got_repo_pack_fds_open(&pack_fds);
7161 if (repo_path == NULL) {
7162 cwd = getcwd(NULL, 0);
7164 return got_error_from_errno("getcwd");
7165 error = got_worktree_open(&worktree, cwd, NULL);
7166 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7170 strdup(got_worktree_get_repo_path(worktree));
7172 repo_path = strdup(cwd);
7173 if (repo_path == NULL) {
7174 error = got_error_from_errno("strdup");
7179 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7183 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7190 error = apply_unveil(got_repo_get_path(repo), NULL);
7194 error = tog_load_refs(repo, 0);
7198 if (commit_id_str == NULL) {
7199 struct got_reference *head_ref;
7200 error = got_ref_open(&head_ref, repo, worktree ?
7201 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7204 error = got_ref_resolve(&commit_id, repo, head_ref);
7205 got_ref_close(head_ref);
7207 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7211 if (keyword_idstr != NULL)
7212 commit_id_str = keyword_idstr;
7214 error = got_repo_match_object_id(&commit_id, NULL,
7215 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7220 error = got_object_open_as_commit(&commit, repo, commit_id);
7224 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7229 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7231 error = got_error_from_errno("view_open");
7234 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7236 if (error != NULL) {
7237 if (view->close == NULL)
7238 close_blame_view(view);
7244 error = set_tog_base_commit(repo, worktree);
7248 /* Release work tree lock. */
7249 got_worktree_close(worktree);
7253 error = view_loop(view);
7256 free(tog_base_commit.id);
7262 free(keyword_idstr);
7264 got_object_commit_close(commit);
7266 got_worktree_close(worktree);
7268 const struct got_error *close_err = got_repo_close(repo);
7273 const struct got_error *pack_err =
7274 got_repo_pack_fds_close(pack_fds);
7282 static const struct got_error *
7283 draw_tree_entries(struct tog_view *view, const char *parent_path)
7285 struct tog_tree_view_state *s = &view->state.tree;
7286 const struct got_error *err = NULL;
7287 struct got_tree_entry *te;
7290 struct tog_color *tc;
7291 int width, n, nentries, scrollx, i = 1;
7292 int limit = view->nlines;
7295 if (view_is_hsplit_top(view))
7296 --limit; /* border */
7298 werase(view->window);
7303 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7307 if (view_needs_focus_indication(view))
7308 wstandout(view->window);
7309 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7311 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7312 waddwstr(view->window, wline);
7315 while (width++ < view->ncols)
7316 waddch(view->window, ' ');
7318 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7319 if (view_needs_focus_indication(view))
7320 wstandend(view->window);
7325 if (s->first_displayed_entry) {
7326 i += got_tree_entry_get_index(s->first_displayed_entry);
7327 if (s->tree != s->root)
7328 ++i; /* account for ".." entry */
7330 nentries = got_object_tree_get_nentries(s->tree);
7331 if (asprintf(&index, "[%d/%d] %s",
7332 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7333 return got_error_from_errno("asprintf");
7334 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7338 waddwstr(view->window, wline);
7341 if (width < view->ncols - 1)
7342 waddch(view->window, '\n');
7345 waddch(view->window, '\n');
7349 if (s->first_displayed_entry == NULL) {
7350 te = got_object_tree_get_first_entry(s->tree);
7351 if (s->selected == 0) {
7353 wstandout(view->window);
7354 s->selected_entry = NULL;
7356 waddstr(view->window, " ..\n"); /* parent directory */
7357 if (s->selected == 0 && view->focussed)
7358 wstandend(view->window);
7365 te = s->first_displayed_entry;
7369 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7370 char *line = NULL, *id_str = NULL, *link_target = NULL;
7371 const char *modestr = "";
7374 te = got_object_tree_get_entry(s->tree, i);
7375 mode = got_tree_entry_get_mode(te);
7378 err = got_object_id_str(&id_str,
7379 got_tree_entry_get_id(te));
7381 return got_error_from_errno(
7382 "got_object_id_str");
7384 if (got_object_tree_entry_is_submodule(te))
7386 else if (S_ISLNK(mode)) {
7389 err = got_tree_entry_get_symlink_target(&link_target,
7395 for (i = 0; link_target[i] != '\0'; i++) {
7396 if (!isprint((unsigned char)link_target[i]))
7397 link_target[i] = '?';
7401 else if (S_ISDIR(mode))
7403 else if (mode & S_IXUSR)
7405 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7406 got_tree_entry_get_name(te), modestr,
7407 link_target ? " -> ": "",
7408 link_target ? link_target : "") == -1) {
7411 return got_error_from_errno("asprintf");
7416 /* use full line width to determine view->maxx */
7417 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7422 view->maxx = MAX(view->maxx, width);
7426 err = format_line(&wline, &width, &scrollx, line, view->x,
7432 if (n == s->selected) {
7434 wstandout(view->window);
7435 s->selected_entry = te;
7437 tc = match_color(&s->colors, line);
7439 wattr_on(view->window,
7440 COLOR_PAIR(tc->colorpair), NULL);
7441 waddwstr(view->window, &wline[scrollx]);
7443 wattr_off(view->window,
7444 COLOR_PAIR(tc->colorpair), NULL);
7445 if (width < view->ncols)
7446 waddch(view->window, '\n');
7447 if (n == s->selected && view->focussed)
7448 wstandend(view->window);
7454 s->last_displayed_entry = te;
7463 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7465 struct got_tree_entry *te;
7466 int isroot = s->tree == s->root;
7469 if (s->first_displayed_entry == NULL)
7472 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7473 while (i++ < maxscroll) {
7476 s->first_displayed_entry = NULL;
7479 s->first_displayed_entry = te;
7480 te = got_tree_entry_get_prev(s->tree, te);
7484 static const struct got_error *
7485 tree_scroll_down(struct tog_view *view, int maxscroll)
7487 struct tog_tree_view_state *s = &view->state.tree;
7488 struct got_tree_entry *next, *last;
7491 if (s->first_displayed_entry)
7492 next = got_tree_entry_get_next(s->tree,
7493 s->first_displayed_entry);
7495 next = got_object_tree_get_first_entry(s->tree);
7497 last = s->last_displayed_entry;
7498 while (next && n++ < maxscroll) {
7500 s->last_displayed_entry = last;
7501 last = got_tree_entry_get_next(s->tree, last);
7503 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7504 s->first_displayed_entry = next;
7505 next = got_tree_entry_get_next(s->tree, next);
7512 static const struct got_error *
7513 tree_entry_path(char **path, struct tog_parent_trees *parents,
7514 struct got_tree_entry *te)
7516 const struct got_error *err = NULL;
7517 struct tog_parent_tree *pt;
7518 size_t len = 2; /* for leading slash and NUL */
7520 TAILQ_FOREACH(pt, parents, entry)
7521 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7524 len += strlen(got_tree_entry_get_name(te));
7526 *path = calloc(1, len);
7528 return got_error_from_errno("calloc");
7531 pt = TAILQ_LAST(parents, tog_parent_trees);
7533 const char *name = got_tree_entry_get_name(pt->selected_entry);
7534 if (strlcat(*path, name, len) >= len) {
7535 err = got_error(GOT_ERR_NO_SPACE);
7538 if (strlcat(*path, "/", len) >= len) {
7539 err = got_error(GOT_ERR_NO_SPACE);
7542 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7545 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7546 err = got_error(GOT_ERR_NO_SPACE);
7558 static const struct got_error *
7559 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7560 struct got_tree_entry *te, struct tog_parent_trees *parents,
7561 struct got_object_id *commit_id, struct got_repository *repo)
7563 const struct got_error *err = NULL;
7565 struct tog_view *blame_view;
7569 err = tree_entry_path(&path, parents, te);
7573 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7574 if (blame_view == NULL) {
7575 err = got_error_from_errno("view_open");
7579 err = open_blame_view(blame_view, path, commit_id, repo);
7581 if (err->code == GOT_ERR_CANCELLED)
7583 view_close(blame_view);
7585 *new_view = blame_view;
7591 static const struct got_error *
7592 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7593 struct tog_tree_view_state *s)
7595 struct tog_view *log_view;
7596 const struct got_error *err = NULL;
7601 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7602 if (log_view == NULL)
7603 return got_error_from_errno("view_open");
7605 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7609 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7612 view_close(log_view);
7614 *new_view = log_view;
7619 static const struct got_error *
7620 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7621 const char *head_ref_name, struct got_repository *repo)
7623 const struct got_error *err = NULL;
7624 char *commit_id_str = NULL;
7625 struct tog_tree_view_state *s = &view->state.tree;
7626 struct got_commit_object *commit = NULL;
7628 TAILQ_INIT(&s->parents);
7629 STAILQ_INIT(&s->colors);
7631 s->commit_id = got_object_id_dup(commit_id);
7632 if (s->commit_id == NULL) {
7633 err = got_error_from_errno("got_object_id_dup");
7637 err = got_object_open_as_commit(&commit, repo, commit_id);
7642 * The root is opened here and will be closed when the view is closed.
7643 * Any visited subtrees and their path-wise parents are opened and
7646 err = got_object_open_as_tree(&s->root, repo,
7647 got_object_commit_get_tree_id(commit));
7652 err = got_object_id_str(&commit_id_str, commit_id);
7656 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7657 err = got_error_from_errno("asprintf");
7661 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7662 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7663 if (head_ref_name) {
7664 s->head_ref_name = strdup(head_ref_name);
7665 if (s->head_ref_name == NULL) {
7666 err = got_error_from_errno("strdup");
7672 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7673 err = add_color(&s->colors, "\\$$",
7674 TOG_COLOR_TREE_SUBMODULE,
7675 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7678 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7679 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7682 err = add_color(&s->colors, "/$",
7683 TOG_COLOR_TREE_DIRECTORY,
7684 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7688 err = add_color(&s->colors, "\\*$",
7689 TOG_COLOR_TREE_EXECUTABLE,
7690 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7694 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7695 get_color_value("TOG_COLOR_COMMIT"));
7700 view->show = show_tree_view;
7701 view->input = input_tree_view;
7702 view->close = close_tree_view;
7703 view->search_start = search_start_tree_view;
7704 view->search_next = search_next_tree_view;
7706 free(commit_id_str);
7708 got_object_commit_close(commit);
7710 if (view->close == NULL)
7711 close_tree_view(view);
7717 static const struct got_error *
7718 close_tree_view(struct tog_view *view)
7720 struct tog_tree_view_state *s = &view->state.tree;
7722 free_colors(&s->colors);
7723 free(s->tree_label);
7724 s->tree_label = NULL;
7726 s->commit_id = NULL;
7727 free(s->head_ref_name);
7728 s->head_ref_name = NULL;
7729 while (!TAILQ_EMPTY(&s->parents)) {
7730 struct tog_parent_tree *parent;
7731 parent = TAILQ_FIRST(&s->parents);
7732 TAILQ_REMOVE(&s->parents, parent, entry);
7733 if (parent->tree != s->root)
7734 got_object_tree_close(parent->tree);
7738 if (s->tree != NULL && s->tree != s->root)
7739 got_object_tree_close(s->tree);
7741 got_object_tree_close(s->root);
7745 static const struct got_error *
7746 search_start_tree_view(struct tog_view *view)
7748 struct tog_tree_view_state *s = &view->state.tree;
7750 s->matched_entry = NULL;
7755 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7757 regmatch_t regmatch;
7759 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7763 static const struct got_error *
7764 search_next_tree_view(struct tog_view *view)
7766 struct tog_tree_view_state *s = &view->state.tree;
7767 struct got_tree_entry *te = NULL;
7769 if (!view->searching) {
7770 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7774 if (s->matched_entry) {
7775 if (view->searching == TOG_SEARCH_FORWARD) {
7776 if (s->selected_entry)
7777 te = got_tree_entry_get_next(s->tree,
7780 te = got_object_tree_get_first_entry(s->tree);
7782 if (s->selected_entry == NULL)
7783 te = got_object_tree_get_last_entry(s->tree);
7785 te = got_tree_entry_get_prev(s->tree,
7789 if (s->selected_entry)
7790 te = s->selected_entry;
7791 else if (view->searching == TOG_SEARCH_FORWARD)
7792 te = got_object_tree_get_first_entry(s->tree);
7794 te = got_object_tree_get_last_entry(s->tree);
7799 if (s->matched_entry == NULL) {
7800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7803 if (view->searching == TOG_SEARCH_FORWARD)
7804 te = got_object_tree_get_first_entry(s->tree);
7806 te = got_object_tree_get_last_entry(s->tree);
7809 if (match_tree_entry(te, &view->regex)) {
7810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7811 s->matched_entry = te;
7815 if (view->searching == TOG_SEARCH_FORWARD)
7816 te = got_tree_entry_get_next(s->tree, te);
7818 te = got_tree_entry_get_prev(s->tree, te);
7821 if (s->matched_entry) {
7822 s->first_displayed_entry = s->matched_entry;
7829 static const struct got_error *
7830 show_tree_view(struct tog_view *view)
7832 const struct got_error *err = NULL;
7833 struct tog_tree_view_state *s = &view->state.tree;
7836 err = tree_entry_path(&parent_path, &s->parents, NULL);
7840 err = draw_tree_entries(view, parent_path);
7847 static const struct got_error *
7848 tree_goto_line(struct tog_view *view, int nlines)
7850 const struct got_error *err = NULL;
7851 struct tog_tree_view_state *s = &view->state.tree;
7852 struct got_tree_entry **fte, **lte, **ste;
7853 int g, last, first = 1, i = 1;
7854 int root = s->tree == s->root;
7855 int off = root ? 1 : 2;
7862 else if (g > got_object_tree_get_nentries(s->tree))
7863 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7865 fte = &s->first_displayed_entry;
7866 lte = &s->last_displayed_entry;
7867 ste = &s->selected_entry;
7870 first = got_tree_entry_get_index(*fte);
7871 first += off; /* account for ".." */
7873 last = got_tree_entry_get_index(*lte);
7876 if (g >= first && g <= last && g - first < nlines) {
7877 s->selected = g - first;
7878 return NULL; /* gline is on the current page */
7882 i = got_tree_entry_get_index(*ste);
7887 err = tree_scroll_down(view, g - i);
7890 if (got_tree_entry_get_index(*lte) >=
7891 got_object_tree_get_nentries(s->tree) - 1 &&
7892 first + s->selected < g &&
7893 s->selected < s->ndisplayed - 1) {
7894 first = got_tree_entry_get_index(*fte);
7896 s->selected = g - first;
7899 tree_scroll_up(s, i - g);
7902 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7903 s->selected = g - 1;
7908 static const struct got_error *
7909 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7911 const struct got_error *err = NULL;
7912 struct tog_tree_view_state *s = &view->state.tree;
7913 struct got_tree_entry *te;
7914 int n, nscroll = view->nlines - 3;
7917 return tree_goto_line(view, nscroll);
7926 horizontal_scroll_input(view, ch);
7929 s->show_ids = !s->show_ids;
7934 if (!s->selected_entry)
7936 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7940 err = view_request_new(new_view, view, TOG_VIEW_REF);
7947 if (s->tree == s->root)
7948 s->first_displayed_entry =
7949 got_object_tree_get_first_entry(s->tree);
7951 s->first_displayed_entry = NULL;
7956 int eos = view->nlines - 3;
7958 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7962 te = got_object_tree_get_last_entry(s->tree);
7963 for (n = 0; n < eos; n++) {
7965 if (s->tree != s->root) {
7966 s->first_displayed_entry = NULL;
7971 s->first_displayed_entry = te;
7972 te = got_tree_entry_get_prev(s->tree, te);
7975 s->selected = n - 1;
7981 if (s->selected > 0) {
7985 tree_scroll_up(s, 1);
7986 if (s->selected_entry == NULL ||
7987 (s->tree == s->root && s->selected_entry ==
7988 got_object_tree_get_first_entry(s->tree)))
7998 if (s->tree == s->root) {
7999 if (got_object_tree_get_first_entry(s->tree) ==
8000 s->first_displayed_entry)
8001 s->selected -= MIN(s->selected, nscroll);
8003 if (s->first_displayed_entry == NULL)
8004 s->selected -= MIN(s->selected, nscroll);
8006 tree_scroll_up(s, MAX(0, nscroll));
8007 if (s->selected_entry == NULL ||
8008 (s->tree == s->root && s->selected_entry ==
8009 got_object_tree_get_first_entry(s->tree)))
8015 if (s->selected < s->ndisplayed - 1) {
8019 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8021 /* can't scroll any further */
8025 tree_scroll_down(view, 1);
8035 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8037 /* can't scroll any further; move cursor down */
8038 if (s->selected < s->ndisplayed - 1)
8039 s->selected += MIN(nscroll,
8040 s->ndisplayed - s->selected - 1);
8045 tree_scroll_down(view, nscroll);
8050 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8051 struct tog_parent_tree *parent;
8052 /* user selected '..' */
8053 if (s->tree == s->root) {
8057 parent = TAILQ_FIRST(&s->parents);
8058 TAILQ_REMOVE(&s->parents, parent,
8060 got_object_tree_close(s->tree);
8061 s->tree = parent->tree;
8062 s->first_displayed_entry =
8063 parent->first_displayed_entry;
8065 parent->selected_entry;
8066 s->selected = parent->selected;
8067 if (s->selected > view->nlines - 3) {
8068 err = offset_selection_down(view);
8073 } else if (S_ISDIR(got_tree_entry_get_mode(
8074 s->selected_entry))) {
8075 struct got_tree_object *subtree;
8077 err = got_object_open_as_tree(&subtree, s->repo,
8078 got_tree_entry_get_id(s->selected_entry));
8081 err = tree_view_visit_subtree(s, subtree);
8083 got_object_tree_close(subtree);
8086 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8087 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8090 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8091 s->selected = view->nlines - 4;
8107 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8112 static const struct got_error *
8113 cmd_tree(int argc, char *argv[])
8115 const struct got_error *error;
8116 struct got_repository *repo = NULL;
8117 struct got_worktree *worktree = NULL;
8118 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8119 struct got_object_id *commit_id = NULL;
8120 struct got_commit_object *commit = NULL;
8121 const char *commit_id_arg = NULL;
8122 char *keyword_idstr = NULL, *label = NULL;
8123 struct got_reference *ref = NULL;
8124 const char *head_ref_name = NULL;
8126 struct tog_view *view;
8127 int *pack_fds = NULL;
8129 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8132 commit_id_arg = optarg;
8135 repo_path = realpath(optarg, NULL);
8136 if (repo_path == NULL)
8137 return got_error_from_errno2("realpath",
8152 error = got_repo_pack_fds_open(&pack_fds);
8156 if (repo_path == NULL) {
8157 cwd = getcwd(NULL, 0);
8159 return got_error_from_errno("getcwd");
8160 error = got_worktree_open(&worktree, cwd, NULL);
8161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8165 strdup(got_worktree_get_repo_path(worktree));
8167 repo_path = strdup(cwd);
8168 if (repo_path == NULL) {
8169 error = got_error_from_errno("strdup");
8174 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8178 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8185 error = apply_unveil(got_repo_get_path(repo), NULL);
8189 error = tog_load_refs(repo, 0);
8193 if (commit_id_arg == NULL) {
8194 error = got_repo_match_object_id(&commit_id, &label,
8195 worktree ? got_worktree_get_head_ref_name(worktree) :
8196 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8199 head_ref_name = label;
8201 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8205 if (keyword_idstr != NULL)
8206 commit_id_arg = keyword_idstr;
8208 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8210 head_ref_name = got_ref_get_name(ref);
8211 else if (error->code != GOT_ERR_NOT_REF)
8213 error = got_repo_match_object_id(&commit_id, NULL,
8214 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8219 error = got_object_open_as_commit(&commit, repo, commit_id);
8223 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8225 error = got_error_from_errno("view_open");
8228 error = open_tree_view(view, commit_id, head_ref_name, repo);
8231 if (!got_path_is_root_dir(in_repo_path)) {
8232 error = tree_view_walk_path(&view->state.tree, commit,
8239 error = set_tog_base_commit(repo, worktree);
8243 /* Release work tree lock. */
8244 got_worktree_close(worktree);
8248 error = view_loop(view);
8251 free(tog_base_commit.id);
8252 free(keyword_idstr);
8259 if (worktree != NULL)
8260 got_worktree_close(worktree);
8262 const struct got_error *close_err = got_repo_close(repo);
8267 const struct got_error *pack_err =
8268 got_repo_pack_fds_close(pack_fds);
8276 static const struct got_error *
8277 ref_view_load_refs(struct tog_ref_view_state *s)
8279 struct got_reflist_entry *sre;
8280 struct tog_reflist_entry *re;
8283 TAILQ_FOREACH(sre, &tog_refs, entry) {
8284 if (strncmp(got_ref_get_name(sre->ref),
8285 "refs/got/", 9) == 0 &&
8286 strncmp(got_ref_get_name(sre->ref),
8287 "refs/got/backup/", 16) != 0)
8290 re = malloc(sizeof(*re));
8292 return got_error_from_errno("malloc");
8294 re->ref = got_ref_dup(sre->ref);
8295 if (re->ref == NULL)
8296 return got_error_from_errno("got_ref_dup");
8297 re->idx = s->nrefs++;
8298 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8301 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8306 ref_view_free_refs(struct tog_ref_view_state *s)
8308 struct tog_reflist_entry *re;
8310 while (!TAILQ_EMPTY(&s->refs)) {
8311 re = TAILQ_FIRST(&s->refs);
8312 TAILQ_REMOVE(&s->refs, re, entry);
8313 got_ref_close(re->ref);
8318 static const struct got_error *
8319 open_ref_view(struct tog_view *view, struct got_repository *repo)
8321 const struct got_error *err = NULL;
8322 struct tog_ref_view_state *s = &view->state.ref;
8324 s->selected_entry = 0;
8327 TAILQ_INIT(&s->refs);
8328 STAILQ_INIT(&s->colors);
8330 err = ref_view_load_refs(s);
8334 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8335 err = add_color(&s->colors, "^refs/heads/",
8336 TOG_COLOR_REFS_HEADS,
8337 get_color_value("TOG_COLOR_REFS_HEADS"));
8341 err = add_color(&s->colors, "^refs/tags/",
8342 TOG_COLOR_REFS_TAGS,
8343 get_color_value("TOG_COLOR_REFS_TAGS"));
8347 err = add_color(&s->colors, "^refs/remotes/",
8348 TOG_COLOR_REFS_REMOTES,
8349 get_color_value("TOG_COLOR_REFS_REMOTES"));
8353 err = add_color(&s->colors, "^refs/got/backup/",
8354 TOG_COLOR_REFS_BACKUP,
8355 get_color_value("TOG_COLOR_REFS_BACKUP"));
8360 view->show = show_ref_view;
8361 view->input = input_ref_view;
8362 view->close = close_ref_view;
8363 view->search_start = search_start_ref_view;
8364 view->search_next = search_next_ref_view;
8367 if (view->close == NULL)
8368 close_ref_view(view);
8374 static const struct got_error *
8375 close_ref_view(struct tog_view *view)
8377 struct tog_ref_view_state *s = &view->state.ref;
8379 ref_view_free_refs(s);
8380 free_colors(&s->colors);
8385 static const struct got_error *
8386 resolve_reflist_entry(struct got_object_id **commit_id,
8387 struct tog_reflist_entry *re, struct got_repository *repo)
8389 const struct got_error *err = NULL;
8390 struct got_object_id *obj_id;
8391 struct got_tag_object *tag = NULL;
8396 err = got_ref_resolve(&obj_id, repo, re->ref);
8400 err = got_object_get_type(&obj_type, repo, obj_id);
8405 case GOT_OBJ_TYPE_COMMIT:
8406 *commit_id = obj_id;
8408 case GOT_OBJ_TYPE_TAG:
8409 err = got_object_open_as_tag(&tag, repo, obj_id);
8413 err = got_object_get_type(&obj_type, repo,
8414 got_object_tag_get_object_id(tag));
8417 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8418 err = got_error(GOT_ERR_OBJ_TYPE);
8421 *commit_id = got_object_id_dup(
8422 got_object_tag_get_object_id(tag));
8423 if (*commit_id == NULL) {
8424 err = got_error_from_errno("got_object_id_dup");
8429 err = got_error(GOT_ERR_OBJ_TYPE);
8435 got_object_tag_close(tag);
8443 static const struct got_error *
8444 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8445 struct tog_reflist_entry *re, struct got_repository *repo)
8447 struct tog_view *log_view;
8448 const struct got_error *err = NULL;
8449 struct got_object_id *commit_id = NULL;
8453 err = resolve_reflist_entry(&commit_id, re, repo);
8455 if (err->code != GOT_ERR_OBJ_TYPE)
8461 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8462 if (log_view == NULL) {
8463 err = got_error_from_errno("view_open");
8467 err = open_log_view(log_view, commit_id, repo,
8468 got_ref_get_name(re->ref), "", 0);
8471 view_close(log_view);
8473 *new_view = log_view;
8479 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8481 struct tog_reflist_entry *re;
8484 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8487 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8488 while (i++ < maxscroll) {
8491 s->first_displayed_entry = re;
8492 re = TAILQ_PREV(re, tog_reflist_head, entry);
8496 static const struct got_error *
8497 ref_scroll_down(struct tog_view *view, int maxscroll)
8499 struct tog_ref_view_state *s = &view->state.ref;
8500 struct tog_reflist_entry *next, *last;
8503 if (s->first_displayed_entry)
8504 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8506 next = TAILQ_FIRST(&s->refs);
8508 last = s->last_displayed_entry;
8509 while (next && n++ < maxscroll) {
8511 s->last_displayed_entry = last;
8512 last = TAILQ_NEXT(last, entry);
8514 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8515 s->first_displayed_entry = next;
8516 next = TAILQ_NEXT(next, entry);
8523 static const struct got_error *
8524 search_start_ref_view(struct tog_view *view)
8526 struct tog_ref_view_state *s = &view->state.ref;
8528 s->matched_entry = NULL;
8533 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8535 regmatch_t regmatch;
8537 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8541 static const struct got_error *
8542 search_next_ref_view(struct tog_view *view)
8544 struct tog_ref_view_state *s = &view->state.ref;
8545 struct tog_reflist_entry *re = NULL;
8547 if (!view->searching) {
8548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8552 if (s->matched_entry) {
8553 if (view->searching == TOG_SEARCH_FORWARD) {
8554 if (s->selected_entry)
8555 re = TAILQ_NEXT(s->selected_entry, entry);
8557 re = TAILQ_PREV(s->selected_entry,
8558 tog_reflist_head, entry);
8560 if (s->selected_entry == NULL)
8561 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8563 re = TAILQ_PREV(s->selected_entry,
8564 tog_reflist_head, entry);
8567 if (s->selected_entry)
8568 re = s->selected_entry;
8569 else if (view->searching == TOG_SEARCH_FORWARD)
8570 re = TAILQ_FIRST(&s->refs);
8572 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8577 if (s->matched_entry == NULL) {
8578 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8581 if (view->searching == TOG_SEARCH_FORWARD)
8582 re = TAILQ_FIRST(&s->refs);
8584 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8587 if (match_reflist_entry(re, &view->regex)) {
8588 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8589 s->matched_entry = re;
8593 if (view->searching == TOG_SEARCH_FORWARD)
8594 re = TAILQ_NEXT(re, entry);
8596 re = TAILQ_PREV(re, tog_reflist_head, entry);
8599 if (s->matched_entry) {
8600 s->first_displayed_entry = s->matched_entry;
8607 static const struct got_error *
8608 show_ref_view(struct tog_view *view)
8610 const struct got_error *err = NULL;
8611 struct tog_ref_view_state *s = &view->state.ref;
8612 struct tog_reflist_entry *re;
8615 struct tog_color *tc;
8616 int width, n, scrollx;
8617 int limit = view->nlines;
8619 werase(view->window);
8622 if (view_is_hsplit_top(view))
8623 --limit; /* border */
8628 re = s->first_displayed_entry;
8630 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8632 return got_error_from_errno("asprintf");
8634 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8639 if (view_needs_focus_indication(view))
8640 wstandout(view->window);
8641 waddwstr(view->window, wline);
8642 while (width++ < view->ncols)
8643 waddch(view->window, ' ');
8644 if (view_needs_focus_indication(view))
8645 wstandend(view->window);
8655 while (re && limit > 0) {
8657 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8660 struct got_commit_object *ci;
8661 struct got_tag_object *tag;
8662 struct got_object_id *id;
8666 err = got_ref_resolve(&id, s->repo, re->ref);
8669 err = got_object_open_as_tag(&tag, s->repo, id);
8671 if (err->code != GOT_ERR_OBJ_TYPE) {
8675 err = got_object_open_as_commit(&ci, s->repo,
8681 t = got_object_commit_get_committer_time(ci);
8682 got_object_commit_close(ci);
8684 t = got_object_tag_get_tagger_time(tag);
8685 got_object_tag_close(tag);
8688 if (gmtime_r(&t, &tm) == NULL)
8689 return got_error_from_errno("gmtime_r");
8690 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8691 return got_error(GOT_ERR_NO_SPACE);
8693 if (got_ref_is_symbolic(re->ref)) {
8694 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8695 ymd : "", got_ref_get_name(re->ref),
8696 got_ref_get_symref_target(re->ref)) == -1)
8697 return got_error_from_errno("asprintf");
8698 } else if (s->show_ids) {
8699 struct got_object_id *id;
8701 err = got_ref_resolve(&id, s->repo, re->ref);
8704 err = got_object_id_str(&id_str, id);
8709 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8710 got_ref_get_name(re->ref), id_str) == -1) {
8711 err = got_error_from_errno("asprintf");
8718 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8719 got_ref_get_name(re->ref)) == -1)
8720 return got_error_from_errno("asprintf");
8722 /* use full line width to determine view->maxx */
8723 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8728 view->maxx = MAX(view->maxx, width);
8732 err = format_line(&wline, &width, &scrollx, line, view->x,
8738 if (n == s->selected) {
8740 wstandout(view->window);
8741 s->selected_entry = re;
8743 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8745 wattr_on(view->window,
8746 COLOR_PAIR(tc->colorpair), NULL);
8747 waddwstr(view->window, &wline[scrollx]);
8749 wattr_off(view->window,
8750 COLOR_PAIR(tc->colorpair), NULL);
8751 if (width < view->ncols)
8752 waddch(view->window, '\n');
8753 if (n == s->selected && view->focussed)
8754 wstandend(view->window);
8760 s->last_displayed_entry = re;
8763 re = TAILQ_NEXT(re, entry);
8770 static const struct got_error *
8771 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8772 struct tog_reflist_entry *re, struct got_repository *repo)
8774 const struct got_error *err = NULL;
8775 struct got_object_id *commit_id = NULL;
8776 struct tog_view *tree_view;
8780 err = resolve_reflist_entry(&commit_id, re, repo);
8782 if (err->code != GOT_ERR_OBJ_TYPE)
8789 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8790 if (tree_view == NULL) {
8791 err = got_error_from_errno("view_open");
8795 err = open_tree_view(tree_view, commit_id,
8796 got_ref_get_name(re->ref), repo);
8800 *new_view = tree_view;
8806 static const struct got_error *
8807 ref_goto_line(struct tog_view *view, int nlines)
8809 const struct got_error *err = NULL;
8810 struct tog_ref_view_state *s = &view->state.ref;
8811 int g, idx = s->selected_entry->idx;
8818 else if (g > s->nrefs)
8821 if (g >= s->first_displayed_entry->idx + 1 &&
8822 g <= s->last_displayed_entry->idx + 1 &&
8823 g - s->first_displayed_entry->idx - 1 < nlines) {
8824 s->selected = g - s->first_displayed_entry->idx - 1;
8829 err = ref_scroll_down(view, g - idx - 1);
8832 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8833 s->first_displayed_entry->idx + s->selected < g &&
8834 s->selected < s->ndisplayed - 1)
8835 s->selected = g - s->first_displayed_entry->idx - 1;
8836 } else if (idx + 1 > g)
8837 ref_scroll_up(s, idx - g + 1);
8839 if (g < nlines && s->first_displayed_entry->idx == 0)
8840 s->selected = g - 1;
8846 static const struct got_error *
8847 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8849 const struct got_error *err = NULL;
8850 struct tog_ref_view_state *s = &view->state.ref;
8851 struct tog_reflist_entry *re;
8852 int n, nscroll = view->nlines - 1;
8855 return ref_goto_line(view, nscroll);
8864 horizontal_scroll_input(view, ch);
8867 s->show_ids = !s->show_ids;
8871 s->show_date = !s->show_date;
8875 s->sort_by_date = !s->sort_by_date;
8876 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8878 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8879 got_ref_cmp_by_commit_timestamp_descending :
8880 tog_ref_cmp_by_name, s->repo);
8883 got_reflist_object_id_map_free(tog_refs_idmap);
8884 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8885 &tog_refs, s->repo);
8888 ref_view_free_refs(s);
8889 err = ref_view_load_refs(s);
8894 if (!s->selected_entry)
8896 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8900 if (!s->selected_entry)
8902 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8909 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8914 int eos = view->nlines - 1;
8916 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8920 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8921 for (n = 0; n < eos; n++) {
8924 s->first_displayed_entry = re;
8925 re = TAILQ_PREV(re, tog_reflist_head, entry);
8928 s->selected = n - 1;
8934 if (s->selected > 0) {
8938 ref_scroll_up(s, 1);
8939 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8949 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8950 s->selected -= MIN(nscroll, s->selected);
8951 ref_scroll_up(s, MAX(0, nscroll));
8952 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8958 if (s->selected < s->ndisplayed - 1) {
8962 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8963 /* can't scroll any further */
8967 ref_scroll_down(view, 1);
8977 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8978 /* can't scroll any further; move cursor down */
8979 if (s->selected < s->ndisplayed - 1)
8980 s->selected += MIN(nscroll,
8981 s->ndisplayed - s->selected - 1);
8982 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8983 s->selected += s->ndisplayed - s->selected - 1;
8987 ref_scroll_down(view, nscroll);
8992 err = tog_load_refs(s->repo, s->sort_by_date);
8995 ref_view_free_refs(s);
8996 err = ref_view_load_refs(s);
8999 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9000 s->selected = view->nlines - 2;
9014 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9019 static const struct got_error *
9020 cmd_ref(int argc, char *argv[])
9022 const struct got_error *error;
9023 struct got_repository *repo = NULL;
9024 struct got_worktree *worktree = NULL;
9025 char *cwd = NULL, *repo_path = NULL;
9027 struct tog_view *view;
9028 int *pack_fds = NULL;
9030 while ((ch = getopt(argc, argv, "r:")) != -1) {
9033 repo_path = realpath(optarg, NULL);
9034 if (repo_path == NULL)
9035 return got_error_from_errno2("realpath",
9050 error = got_repo_pack_fds_open(&pack_fds);
9054 if (repo_path == NULL) {
9055 cwd = getcwd(NULL, 0);
9057 return got_error_from_errno("getcwd");
9058 error = got_worktree_open(&worktree, cwd, NULL);
9059 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9063 strdup(got_worktree_get_repo_path(worktree));
9065 repo_path = strdup(cwd);
9066 if (repo_path == NULL) {
9067 error = got_error_from_errno("strdup");
9072 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9078 error = apply_unveil(got_repo_get_path(repo), NULL);
9082 error = tog_load_refs(repo, 0);
9086 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9088 error = got_error_from_errno("view_open");
9092 error = open_ref_view(view, repo);
9097 error = set_tog_base_commit(repo, worktree);
9101 /* Release work tree lock. */
9102 got_worktree_close(worktree);
9106 error = view_loop(view);
9109 free(tog_base_commit.id);
9112 if (worktree != NULL)
9113 got_worktree_close(worktree);
9115 const struct got_error *close_err = got_repo_close(repo);
9120 const struct got_error *pack_err =
9121 got_repo_pack_fds_close(pack_fds);
9129 static const struct got_error*
9130 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9139 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9143 if (mvwprintw(win, y, x, "%s", str) == ERR)
9144 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9151 static const struct got_error *
9152 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9156 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9158 free(*line_offsets);
9159 *line_offsets = NULL;
9160 return got_error_from_errno("reallocarray");
9164 (*line_offsets)[*nlines] = off;
9169 static const struct got_error *
9170 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9174 for (;n > 0; --n, ++km) {
9178 if (km->keys == NULL)
9181 t = t0 = strdup(km->keys);
9183 return got_error_from_errno("strdup");
9186 while ((k = strsep(&t, " ")) != NULL)
9187 len += strlen(k) > 1 ? 2 : 0;
9189 *ret = MAX(*ret, len);
9196 * Write keymap section headers, keys, and key info in km to f.
9197 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9198 * wrap control and symbolic keys in guillemets, else use <>.
9200 static const struct got_error *
9201 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9206 static const char *u8_glyph[] = {
9207 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9208 "\xe2\x80\xba" /* U+203A (utf8 >) */
9211 int cs, s, first = 1;
9213 cs = got_locale_is_utf8();
9215 t = t0 = strdup(km->keys);
9217 return got_error_from_errno("strdup");
9219 len = strlen(km->keys);
9220 while ((k = strsep(&t, " ")) != NULL) {
9221 s = strlen(k) > 1; /* control or symbolic key */
9222 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9223 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9224 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9227 return got_error_from_errno("fprintf");
9235 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9237 return got_error_from_errno("fprintf");
9243 static const struct got_error *
9244 format_help(struct tog_help_view_state *s)
9246 const struct got_error *err = NULL;
9248 int i, max, n, show = s->all;
9249 static const struct tog_key_map km[] = {
9250 #define KEYMAP_(info, type) { NULL, (info), type }
9251 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9257 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9262 err = max_key_str(&max, km, n);
9266 for (i = 0; i < n; ++i) {
9267 if (km[i].keys == NULL) {
9269 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9270 km[i].type == s->type || s->all)
9274 err = format_help_line(&off, s->f, &km[i], max);
9277 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9284 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9288 static const struct got_error *
9289 create_help(struct tog_help_view_state *s)
9292 const struct got_error *err;
9294 free(s->line_offsets);
9295 s->line_offsets = NULL;
9300 return got_error_from_errno("got_opentemp");
9303 err = format_help(s);
9307 if (s->f && fflush(s->f) != 0)
9308 return got_error_from_errno("fflush");
9313 static const struct got_error *
9314 search_start_help_view(struct tog_view *view)
9316 view->state.help.matched_line = 0;
9321 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9322 size_t *nlines, int **first, int **last, int **match, int **selected)
9324 struct tog_help_view_state *s = &view->state.help;
9327 *nlines = s->nlines;
9328 *line_offsets = s->line_offsets;
9329 *match = &s->matched_line;
9330 *first = &s->first_displayed_line;
9331 *last = &s->last_displayed_line;
9332 *selected = &s->selected_line;
9335 static const struct got_error *
9336 show_help_view(struct tog_view *view)
9338 struct tog_help_view_state *s = &view->state.help;
9339 const struct got_error *err;
9340 regmatch_t *regmatch = &view->regmatch;
9345 int width, nprinted = 0, rc = 0;
9346 int eos = view->nlines;
9348 if (view_is_hsplit_top(view))
9349 --eos; /* account for border */
9353 werase(view->window);
9355 if (view->gline > s->nlines - 1)
9356 view->gline = s->nlines - 1;
9358 err = win_draw_center(view->window, 0, 0, view->ncols,
9359 view_needs_focus_indication(view),
9360 "tog help (press q to return to tog)");
9365 waddstr(view->window, "\n\n");
9371 while (eos > 0 && nprinted < eos) {
9374 linelen = getline(&line, &linesz, s->f);
9375 if (linelen == -1) {
9378 return got_ferror(s->f, GOT_ERR_IO);
9383 if (++s->lineno < s->first_displayed_line)
9385 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9387 if (s->lineno == view->hiline)
9390 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9396 view->maxx = MAX(view->maxx, width);
9401 wattron(view->window, attr);
9402 if (s->first_displayed_line + nprinted == s->matched_line &&
9403 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9404 err = add_matched_line(&width, line, view->ncols - 1, 0,
9405 view->window, view->x, regmatch);
9413 err = format_line(&wline, &width, &skip, line,
9414 view->x, view->ncols, 0, view->x ? 1 : 0);
9419 waddwstr(view->window, &wline[skip]);
9423 if (s->lineno == view->hiline) {
9424 while (width++ < view->ncols)
9425 waddch(view->window, ' ');
9427 if (width < view->ncols)
9428 waddch(view->window, '\n');
9431 wattroff(view->window, attr);
9432 if (++nprinted == 1)
9433 s->first_displayed_line = s->lineno;
9437 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9439 s->last_displayed_line = s->first_displayed_line;
9444 rc = waddnstr(view->window,
9445 "See the tog(1) manual page for full documentation",
9448 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9450 wmove(view->window, view->nlines - 1, 0);
9451 wclrtoeol(view->window);
9452 wstandout(view->window);
9453 rc = waddnstr(view->window, "scroll down for more...",
9456 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9457 if (getcurx(view->window) < view->ncols - 6) {
9458 rc = wprintw(view->window, "[%.0f%%]",
9459 100.00 * s->last_displayed_line / s->nlines);
9461 return got_error_msg(GOT_ERR_IO, "wprintw");
9463 wstandend(view->window);
9469 static const struct got_error *
9470 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9472 struct tog_help_view_state *s = &view->state.help;
9473 const struct got_error *err = NULL;
9479 eos = nscroll = view->nlines;
9480 if (view_is_hsplit_top(view))
9483 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9492 horizontal_scroll_input(view, ch);
9496 s->first_displayed_line = 1;
9504 s->first_displayed_line = (s->nlines - eos) + 3;
9509 if (s->first_displayed_line > 1)
9510 --s->first_displayed_line;
9521 if (s->first_displayed_line == 1) {
9525 while (--nscroll > 0 && s->first_displayed_line > 1)
9526 s->first_displayed_line--;
9532 ++s->first_displayed_line;
9548 while (!s->eof && --nscroll > 0) {
9549 linelen = getline(&line, &linesz, s->f);
9550 s->first_displayed_line++;
9551 if (linelen == -1) {
9555 err = got_ferror(s->f, GOT_ERR_IO);
9569 static const struct got_error *
9570 close_help_view(struct tog_view *view)
9572 struct tog_help_view_state *s = &view->state.help;
9574 free(s->line_offsets);
9575 s->line_offsets = NULL;
9576 if (fclose(s->f) == EOF)
9577 return got_error_from_errno("fclose");
9582 static const struct got_error *
9583 reset_help_view(struct tog_view *view)
9585 struct tog_help_view_state *s = &view->state.help;
9588 if (s->f && fclose(s->f) == EOF)
9589 return got_error_from_errno("fclose");
9591 wclear(view->window);
9595 s->first_displayed_line = 1;
9596 s->last_displayed_line = view->nlines;
9597 s->matched_line = 0;
9599 return create_help(s);
9602 static const struct got_error *
9603 open_help_view(struct tog_view *view, struct tog_view *parent)
9605 const struct got_error *err = NULL;
9606 struct tog_help_view_state *s = &view->state.help;
9608 s->type = (enum tog_keymap_type)parent->type;
9609 s->first_displayed_line = 1;
9610 s->last_displayed_line = view->nlines;
9611 s->selected_line = 1;
9613 view->show = show_help_view;
9614 view->input = input_help_view;
9615 view->reset = reset_help_view;
9616 view->close = close_help_view;
9617 view->search_start = search_start_help_view;
9618 view->search_setup = search_setup_help_view;
9619 view->search_next = search_next_view_match;
9621 err = create_help(s);
9625 static const struct got_error *
9626 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9627 enum tog_view_type request, int y, int x)
9629 const struct got_error *err = NULL;
9635 if (view->type == TOG_VIEW_LOG) {
9636 struct tog_log_view_state *s = &view->state.log;
9638 err = open_diff_view_for_commit(new_view, y, x,
9639 s->selected_entry->commit, s->selected_entry->id,
9642 return got_error_msg(GOT_ERR_NOT_IMPL,
9643 "parent/child view pair not supported");
9645 case TOG_VIEW_BLAME:
9646 if (view->type == TOG_VIEW_TREE) {
9647 struct tog_tree_view_state *s = &view->state.tree;
9649 err = blame_tree_entry(new_view, y, x,
9650 s->selected_entry, &s->parents, s->commit_id,
9653 return got_error_msg(GOT_ERR_NOT_IMPL,
9654 "parent/child view pair not supported");
9657 if (view->type == TOG_VIEW_BLAME)
9658 err = log_annotated_line(new_view, y, x,
9659 view->state.blame.repo, view->state.blame.id_to_log);
9660 else if (view->type == TOG_VIEW_TREE)
9661 err = log_selected_tree_entry(new_view, y, x,
9663 else if (view->type == TOG_VIEW_REF)
9664 err = log_ref_entry(new_view, y, x,
9665 view->state.ref.selected_entry,
9666 view->state.ref.repo);
9668 return got_error_msg(GOT_ERR_NOT_IMPL,
9669 "parent/child view pair not supported");
9672 if (view->type == TOG_VIEW_LOG)
9673 err = browse_commit_tree(new_view, y, x,
9674 view->state.log.selected_entry,
9675 view->state.log.in_repo_path,
9676 view->state.log.head_ref_name,
9677 view->state.log.repo);
9678 else if (view->type == TOG_VIEW_REF)
9679 err = browse_ref_tree(new_view, y, x,
9680 view->state.ref.selected_entry,
9681 view->state.ref.repo);
9683 return got_error_msg(GOT_ERR_NOT_IMPL,
9684 "parent/child view pair not supported");
9687 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9688 if (*new_view == NULL)
9689 return got_error_from_errno("view_open");
9690 if (view->type == TOG_VIEW_LOG)
9691 err = open_ref_view(*new_view, view->state.log.repo);
9692 else if (view->type == TOG_VIEW_TREE)
9693 err = open_ref_view(*new_view, view->state.tree.repo);
9695 err = got_error_msg(GOT_ERR_NOT_IMPL,
9696 "parent/child view pair not supported");
9698 view_close(*new_view);
9701 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9702 if (*new_view == NULL)
9703 return got_error_from_errno("view_open");
9704 err = open_help_view(*new_view, view);
9706 view_close(*new_view);
9709 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9716 * If view was scrolled down to move the selected line into view when opening a
9717 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9720 offset_selection_up(struct tog_view *view)
9722 switch (view->type) {
9723 case TOG_VIEW_BLAME: {
9724 struct tog_blame_view_state *s = &view->state.blame;
9725 if (s->first_displayed_line == 1) {
9726 s->selected_line = MAX(s->selected_line - view->offset,
9730 if (s->first_displayed_line > view->offset)
9731 s->first_displayed_line -= view->offset;
9733 s->first_displayed_line = 1;
9734 s->selected_line += view->offset;
9738 log_scroll_up(&view->state.log, view->offset);
9739 view->state.log.selected += view->offset;
9742 ref_scroll_up(&view->state.ref, view->offset);
9743 view->state.ref.selected += view->offset;
9746 tree_scroll_up(&view->state.tree, view->offset);
9747 view->state.tree.selected += view->offset;
9757 * If the selected line is in the section of screen covered by the bottom split,
9758 * scroll down offset lines to move it into view and index its new position.
9760 static const struct got_error *
9761 offset_selection_down(struct tog_view *view)
9763 const struct got_error *err = NULL;
9764 const struct got_error *(*scrolld)(struct tog_view *, int);
9765 int *selected = NULL;
9768 switch (view->type) {
9769 case TOG_VIEW_BLAME: {
9770 struct tog_blame_view_state *s = &view->state.blame;
9773 if (s->selected_line > view->nlines - header) {
9774 offset = abs(view->nlines - s->selected_line - header);
9775 s->first_displayed_line += offset;
9776 s->selected_line -= offset;
9777 view->offset = offset;
9781 case TOG_VIEW_LOG: {
9782 struct tog_log_view_state *s = &view->state.log;
9783 scrolld = &log_scroll_down;
9784 header = view_is_parent_view(view) ? 3 : 2;
9785 selected = &s->selected;
9788 case TOG_VIEW_REF: {
9789 struct tog_ref_view_state *s = &view->state.ref;
9790 scrolld = &ref_scroll_down;
9792 selected = &s->selected;
9795 case TOG_VIEW_TREE: {
9796 struct tog_tree_view_state *s = &view->state.tree;
9797 scrolld = &tree_scroll_down;
9799 selected = &s->selected;
9809 if (selected && *selected > view->nlines - header) {
9810 offset = abs(view->nlines - *selected - header);
9811 view->offset = offset;
9812 if (scrolld && offset) {
9813 err = scrolld(view, offset);
9814 *selected -= offset;
9822 list_commands(FILE *fp)
9826 fprintf(fp, "commands:");
9827 for (i = 0; i < nitems(tog_commands); i++) {
9828 const struct tog_cmd *cmd = &tog_commands[i];
9829 fprintf(fp, " %s", cmd->name);
9835 usage(int hflag, int status)
9837 FILE *fp = (status == 0) ? stdout : stderr;
9839 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9842 fprintf(fp, "lazy usage: %s path\n", getprogname());
9849 make_argv(int argc, ...)
9857 argv = calloc(argc, sizeof(char *));
9860 for (i = 0; i < argc; i++) {
9861 argv[i] = strdup(va_arg(ap, char *));
9862 if (argv[i] == NULL)
9871 * Try to convert 'tog path' into a 'tog log path' command.
9872 * The user could simply have mistyped the command rather than knowingly
9873 * provided a path. So check whether argv[0] can in fact be resolved
9874 * to a path in the HEAD commit and print a special error if not.
9875 * This hack is for mpi@ <3
9877 static const struct got_error *
9878 tog_log_with_path(int argc, char *argv[])
9880 const struct got_error *error = NULL, *close_err;
9881 const struct tog_cmd *cmd = NULL;
9882 struct got_repository *repo = NULL;
9883 struct got_worktree *worktree = NULL;
9884 struct got_object_id *commit_id = NULL, *id = NULL;
9885 struct got_commit_object *commit = NULL;
9886 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9887 char *commit_id_str = NULL, **cmd_argv = NULL;
9888 int *pack_fds = NULL;
9890 cwd = getcwd(NULL, 0);
9892 return got_error_from_errno("getcwd");
9894 error = got_repo_pack_fds_open(&pack_fds);
9898 error = got_worktree_open(&worktree, cwd, NULL);
9899 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9903 repo_path = strdup(got_worktree_get_repo_path(worktree));
9905 repo_path = strdup(cwd);
9906 if (repo_path == NULL) {
9907 error = got_error_from_errno("strdup");
9911 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9915 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9920 error = tog_load_refs(repo, 0);
9923 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9924 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9925 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9930 got_worktree_close(worktree);
9934 error = got_object_open_as_commit(&commit, repo, commit_id);
9938 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9940 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9942 fprintf(stderr, "%s: '%s' is no known command or path\n",
9943 getprogname(), argv[0]);
9948 error = got_object_id_str(&commit_id_str, commit_id);
9952 cmd = &tog_commands[0]; /* log */
9954 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9955 error = cmd->cmd_main(argc, cmd_argv);
9958 close_err = got_repo_close(repo);
9963 got_object_commit_close(commit);
9965 got_worktree_close(worktree);
9967 const struct got_error *pack_err =
9968 got_repo_pack_fds_close(pack_fds);
9973 free(commit_id_str);
9980 for (i = 0; i < argc; i++)
9989 main(int argc, char *argv[])
9991 const struct got_error *io_err, *error = NULL;
9992 const struct tog_cmd *cmd = NULL;
9993 int ch, hflag = 0, Vflag = 0;
9994 char **cmd_argv = NULL;
9995 static const struct option longopts[] = {
9996 { "version", no_argument, NULL, 'V' },
9999 char *diff_algo_str = NULL;
10000 const char *test_script_path;
10002 setlocale(LC_CTYPE, "");
10005 * Test mode init must happen before pledge() because "tty" will
10006 * not allow TTY-related ioctls to occur via regular files.
10008 test_script_path = getenv("TOG_TEST_SCRIPT");
10009 if (test_script_path != NULL) {
10010 error = init_mock_term(test_script_path);
10012 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10015 } else if (!isatty(STDIN_FILENO))
10016 errx(1, "standard input is not a tty");
10018 #if !defined(PROFILE)
10019 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10024 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10044 got_version_print_str();
10051 /* Build an argument vector which runs a default command. */
10052 cmd = &tog_commands[0];
10054 cmd_argv = make_argv(argc, cmd->name);
10058 /* Did the user specify a command? */
10059 for (i = 0; i < nitems(tog_commands); i++) {
10060 if (strncmp(tog_commands[i].name, argv[0],
10061 strlen(argv[0])) == 0) {
10062 cmd = &tog_commands[i];
10068 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10069 if (diff_algo_str) {
10070 if (strcasecmp(diff_algo_str, "patience") == 0)
10071 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10072 if (strcasecmp(diff_algo_str, "myers") == 0)
10073 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10079 /* No command specified; try log with a path */
10080 error = tog_log_with_path(argc, argv);
10085 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10088 if (using_mock_io) {
10089 io_err = tog_io_close();
10096 for (i = 0; i < argc; i++)
10101 if (error && error->code != GOT_ERR_CANCELLED &&
10102 error->code != GOT_ERR_EOF &&
10103 error->code != GOT_ERR_PRIVSEP_EXIT &&
10104 error->code != GOT_ERR_PRIVSEP_PIPE &&
10105 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10106 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);