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 "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/ioctl.h>
25 #if defined(__FreeBSD__) || defined(__APPLE__)
26 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
61 #include "got_keyword.h"
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 #define CTRL(x) ((x) & 0x1f)
76 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
85 __dead static void usage(int, int);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_ref(void);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct tog_cmd tog_commands[] = {
99 { "log", cmd_log, usage_log },
100 { "diff", cmd_diff, usage_diff },
101 { "blame", cmd_blame, usage_blame },
102 { "tree", cmd_tree, usage_tree },
103 { "ref", cmd_ref, usage_ref },
115 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
116 enum tog_keymap_type {
117 TOG_KEYMAP_KEYS = -2,
133 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
135 #define TOG_EOF_STRING "(END)"
137 struct commit_queue_entry {
138 TAILQ_ENTRY(commit_queue_entry) entry;
139 struct got_object_id *id;
140 struct got_commit_object *commit;
143 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
144 struct commit_queue {
146 struct commit_queue_head head;
150 STAILQ_ENTRY(tog_color) entry;
154 STAILQ_HEAD(tog_colors, tog_color);
156 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
157 static struct got_reflist_object_id_map *tog_refs_idmap;
158 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
160 static const struct got_error *
161 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
162 struct got_reference* re2)
164 const char *name1 = got_ref_get_name(re1);
165 const char *name2 = got_ref_get_name(re2);
166 int isbackup1, isbackup2;
168 /* Sort backup refs towards the bottom of the list. */
169 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
170 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
171 if (!isbackup1 && isbackup2) {
174 } else if (isbackup1 && !isbackup2) {
179 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
183 static const struct got_error *
184 tog_load_refs(struct got_repository *repo, int sort_by_date)
186 const struct got_error *err;
188 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
189 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
194 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
201 if (tog_refs_idmap) {
202 got_reflist_object_id_map_free(tog_refs_idmap);
203 tog_refs_idmap = NULL;
205 got_ref_list_free(&tog_refs);
208 static const struct got_error *
209 add_color(struct tog_colors *colors, const char *pattern,
210 int idx, short color)
212 const struct got_error *err = NULL;
213 struct tog_color *tc;
216 if (idx < 1 || idx > COLOR_PAIRS - 1)
219 init_pair(idx, color, -1);
221 tc = calloc(1, sizeof(*tc));
223 return got_error_from_errno("calloc");
224 regerr = regcomp(&tc->regex, pattern,
225 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
227 static char regerr_msg[512];
228 static char err_msg[512];
229 regerror(regerr, &tc->regex, regerr_msg,
231 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
233 err = got_error_msg(GOT_ERR_REGEX, err_msg);
238 STAILQ_INSERT_HEAD(colors, tc, entry);
243 free_colors(struct tog_colors *colors)
245 struct tog_color *tc;
247 while (!STAILQ_EMPTY(colors)) {
248 tc = STAILQ_FIRST(colors);
249 STAILQ_REMOVE_HEAD(colors, entry);
255 static struct tog_color *
256 get_color(struct tog_colors *colors, int colorpair)
258 struct tog_color *tc = NULL;
260 STAILQ_FOREACH(tc, colors, entry) {
261 if (tc->colorpair == colorpair)
269 default_color_value(const char *envvar)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
272 return COLOR_MAGENTA;
273 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
275 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
279 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
280 return COLOR_MAGENTA;
281 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
285 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
287 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
289 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
291 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
293 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
295 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
296 return COLOR_MAGENTA;
297 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
299 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
306 get_color_value(const char *envvar)
308 const char *val = getenv(envvar);
311 return default_color_value(envvar);
313 if (strcasecmp(val, "black") == 0)
315 if (strcasecmp(val, "red") == 0)
317 if (strcasecmp(val, "green") == 0)
319 if (strcasecmp(val, "yellow") == 0)
321 if (strcasecmp(val, "blue") == 0)
323 if (strcasecmp(val, "magenta") == 0)
324 return COLOR_MAGENTA;
325 if (strcasecmp(val, "cyan") == 0)
327 if (strcasecmp(val, "white") == 0)
329 if (strcasecmp(val, "default") == 0)
332 return default_color_value(envvar);
335 struct tog_diff_view_state {
336 struct got_object_id *id1, *id2;
337 const char *label1, *label2;
341 int first_displayed_line;
342 int last_displayed_line;
345 int ignore_whitespace;
347 struct got_repository *repo;
348 struct got_diff_line *lines;
353 /* passed from log or blame view; may be NULL */
354 struct tog_view *parent_view;
357 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
358 static volatile sig_atomic_t tog_thread_error;
360 struct tog_log_thread_args {
361 pthread_cond_t need_commits;
362 pthread_cond_t commit_loaded;
365 struct got_commit_graph *graph;
366 struct commit_queue *real_commits;
367 const char *in_repo_path;
368 struct got_object_id *start_id;
369 struct got_repository *repo;
373 struct commit_queue_entry **first_displayed_entry;
374 struct commit_queue_entry **selected_entry;
376 int *search_next_done;
380 regex_t *limit_regex;
381 struct commit_queue *limit_commits;
384 struct tog_log_view_state {
385 struct commit_queue *commits;
386 struct commit_queue_entry *first_displayed_entry;
387 struct commit_queue_entry *last_displayed_entry;
388 struct commit_queue_entry *selected_entry;
389 struct commit_queue real_commits;
394 struct got_repository *repo;
395 struct got_object_id *start_id;
398 struct tog_log_thread_args thread_args;
399 struct commit_queue_entry *matched_entry;
400 struct commit_queue_entry *search_entry;
401 struct tog_colors colors;
405 struct commit_queue limit_commits;
408 #define TOG_COLOR_DIFF_MINUS 1
409 #define TOG_COLOR_DIFF_PLUS 2
410 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
411 #define TOG_COLOR_DIFF_META 4
412 #define TOG_COLOR_TREE_SUBMODULE 5
413 #define TOG_COLOR_TREE_SYMLINK 6
414 #define TOG_COLOR_TREE_DIRECTORY 7
415 #define TOG_COLOR_TREE_EXECUTABLE 8
416 #define TOG_COLOR_COMMIT 9
417 #define TOG_COLOR_AUTHOR 10
418 #define TOG_COLOR_DATE 11
419 #define TOG_COLOR_REFS_HEADS 12
420 #define TOG_COLOR_REFS_TAGS 13
421 #define TOG_COLOR_REFS_REMOTES 14
422 #define TOG_COLOR_REFS_BACKUP 15
424 struct tog_blame_cb_args {
425 struct tog_blame_line *lines; /* one per line */
428 struct tog_view *view;
429 struct got_object_id *commit_id;
433 struct tog_blame_thread_args {
435 struct got_repository *repo;
436 struct tog_blame_cb_args *cb_args;
438 got_cancel_cb cancel_cb;
440 pthread_cond_t blame_complete;
446 struct tog_blame_line *lines;
450 struct tog_blame_thread_args thread_args;
451 struct tog_blame_cb_args cb_args;
456 struct tog_blame_view_state {
457 int first_displayed_line;
458 int last_displayed_line;
460 int last_diffed_line;
464 struct got_object_id_queue blamed_commits;
465 struct got_object_qid *blamed_commit;
467 struct got_repository *repo;
468 struct got_object_id *commit_id;
469 struct got_object_id *id_to_log;
470 struct tog_blame blame;
472 struct tog_colors colors;
475 struct tog_parent_tree {
476 TAILQ_ENTRY(tog_parent_tree) entry;
477 struct got_tree_object *tree;
478 struct got_tree_entry *first_displayed_entry;
479 struct got_tree_entry *selected_entry;
483 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
485 struct tog_tree_view_state {
487 struct got_object_id *commit_id;/* commit which this tree belongs to */
488 struct got_tree_object *root; /* the commit's root tree entry */
489 struct got_tree_object *tree; /* currently displayed (sub-)tree */
490 struct got_tree_entry *first_displayed_entry;
491 struct got_tree_entry *last_displayed_entry;
492 struct got_tree_entry *selected_entry;
493 int ndisplayed, selected, show_ids;
494 struct tog_parent_trees parents; /* parent trees of current sub-tree */
496 struct got_repository *repo;
497 struct got_tree_entry *matched_entry;
498 struct tog_colors colors;
501 struct tog_reflist_entry {
502 TAILQ_ENTRY(tog_reflist_entry) entry;
503 struct got_reference *ref;
507 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
509 struct tog_ref_view_state {
510 struct tog_reflist_head refs;
511 struct tog_reflist_entry *first_displayed_entry;
512 struct tog_reflist_entry *last_displayed_entry;
513 struct tog_reflist_entry *selected_entry;
514 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
515 struct got_repository *repo;
516 struct tog_reflist_entry *matched_entry;
517 struct tog_colors colors;
520 struct tog_help_view_state {
525 int first_displayed_line;
526 int last_displayed_line;
531 enum tog_keymap_type type;
534 #define GENERATE_HELP \
535 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
536 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
537 KEY_("k C-p Up", "Move cursor or page up one line"), \
538 KEY_("j C-n Down", "Move cursor or page down one line"), \
539 KEY_("C-b b PgUp", "Scroll the view up one page"), \
540 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
541 KEY_("C-u u", "Scroll the view up one half page"), \
542 KEY_("C-d d", "Scroll the view down one half page"), \
543 KEY_("g", "Go to line N (default: first line)"), \
544 KEY_("Home =", "Go to the first line"), \
545 KEY_("G", "Go to line N (default: last line)"), \
546 KEY_("End *", "Go to the last line"), \
547 KEY_("l Right", "Scroll the view right"), \
548 KEY_("h Left", "Scroll the view left"), \
549 KEY_("$", "Scroll view to the rightmost position"), \
550 KEY_("0", "Scroll view to the leftmost position"), \
551 KEY_("-", "Decrease size of the focussed split"), \
552 KEY_("+", "Increase size of the focussed split"), \
553 KEY_("Tab", "Switch focus between views"), \
554 KEY_("F", "Toggle fullscreen mode"), \
555 KEY_("S", "Switch split-screen layout"), \
556 KEY_("/", "Open prompt to enter search term"), \
557 KEY_("n", "Find next line/token matching the current search term"), \
558 KEY_("N", "Find previous line/token matching the current search term"),\
559 KEY_("q", "Quit the focussed view; Quit help screen"), \
560 KEY_("Q", "Quit tog"), \
562 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
563 KEY_("< ,", "Move cursor up one commit"), \
564 KEY_("> .", "Move cursor down one commit"), \
565 KEY_("Enter", "Open diff view of the selected commit"), \
566 KEY_("B", "Reload the log view and toggle display of merged commits"), \
567 KEY_("R", "Open ref view of all repository references"), \
568 KEY_("T", "Display tree view of the repository from the selected" \
570 KEY_("@", "Toggle between displaying author and committer name"), \
571 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
572 KEY_("C-g Backspace", "Cancel current search or log operation"), \
573 KEY_("C-l", "Reload the log view with new commits in the repository"), \
575 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
576 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
577 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
578 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
579 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
581 KEY_("(", "Go to the previous file in the diff"), \
582 KEY_(")", "Go to the next file in the diff"), \
583 KEY_("{", "Go to the previous hunk in the diff"), \
584 KEY_("}", "Go to the next hunk in the diff"), \
585 KEY_("[", "Decrease the number of context lines"), \
586 KEY_("]", "Increase the number of context lines"), \
587 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
589 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
590 KEY_("Enter", "Display diff view of the selected line's commit"), \
591 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
592 KEY_("L", "Open log view for the currently selected annotated line"), \
593 KEY_("C", "Reload view with the previously blamed commit"), \
594 KEY_("c", "Reload view with the version of the file found in the" \
595 " selected line's commit"), \
596 KEY_("p", "Reload view with the version of the file found in the" \
597 " selected line's parent commit"), \
599 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
600 KEY_("Enter", "Enter selected directory or open blame view of the" \
602 KEY_("L", "Open log view for the selected entry"), \
603 KEY_("R", "Open ref view of all repository references"), \
604 KEY_("i", "Show object IDs for all tree entries"), \
605 KEY_("Backspace", "Return to the parent directory"), \
607 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
608 KEY_("Enter", "Display log view of the selected reference"), \
609 KEY_("T", "Display tree view of the selected reference"), \
610 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
611 KEY_("m", "Toggle display of last modified date for each reference"), \
612 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
613 KEY_("C-l", "Reload view with all repository references")
618 enum tog_keymap_type type;
621 /* curses io for tog regress */
629 static int using_mock_io;
631 #define TOG_KEY_SCRDUMP SHRT_MIN
634 * We implement two types of views: parent views and child views.
636 * The 'Tab' key switches focus between a parent view and its child view.
637 * Child views are shown side-by-side to their parent view, provided
638 * there is enough screen estate.
640 * When a new view is opened from within a parent view, this new view
641 * becomes a child view of the parent view, replacing any existing child.
643 * When a new view is opened from within a child view, this new view
644 * becomes a parent view which will obscure the views below until the
645 * user quits the new parent view by typing 'q'.
647 * This list of views contains parent views only.
648 * Child views are only pointed to by their parent view.
650 TAILQ_HEAD(tog_view_list_head, tog_view);
653 TAILQ_ENTRY(tog_view) entry;
656 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
657 int resized_y, resized_x; /* begin_y/x based on user resizing */
658 int maxx, x; /* max column and current start column */
659 int lines, cols; /* copies of LINES and COLS */
660 int nscrolled, offset; /* lines scrolled and hsplit line offset */
661 int gline, hiline; /* navigate to and highlight this nG line */
662 int ch, count; /* current keymap and count prefix */
663 int resized; /* set when in a resize event */
664 int focussed; /* Only set on one parent or child view at a time. */
666 struct tog_view *parent;
667 struct tog_view *child;
670 * This flag is initially set on parent views when a new child view
671 * is created. It gets toggled when the 'Tab' key switches focus
672 * between parent and child.
673 * The flag indicates whether focus should be passed on to our child
674 * view if this parent view gets picked for focus after another parent
675 * view was closed. This prevents child views from losing focus in such
680 enum tog_view_mode mode;
681 /* type-specific state */
682 enum tog_view_type type;
684 struct tog_diff_view_state diff;
685 struct tog_log_view_state log;
686 struct tog_blame_view_state blame;
687 struct tog_tree_view_state tree;
688 struct tog_ref_view_state ref;
689 struct tog_help_view_state help;
692 const struct got_error *(*show)(struct tog_view *);
693 const struct got_error *(*input)(struct tog_view **,
694 struct tog_view *, int);
695 const struct got_error *(*reset)(struct tog_view *);
696 const struct got_error *(*resize)(struct tog_view *, int);
697 const struct got_error *(*close)(struct tog_view *);
699 const struct got_error *(*search_start)(struct tog_view *);
700 const struct got_error *(*search_next)(struct tog_view *);
701 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
702 int **, int **, int **, int **);
705 #define TOG_SEARCH_FORWARD 1
706 #define TOG_SEARCH_BACKWARD 2
707 int search_next_done;
708 #define TOG_SEARCH_HAVE_MORE 1
709 #define TOG_SEARCH_NO_MORE 2
710 #define TOG_SEARCH_HAVE_NONE 3
716 static const struct got_error *open_diff_view(struct tog_view *,
717 struct got_object_id *, struct got_object_id *,
718 const char *, const char *, int, int, int, struct tog_view *,
719 struct got_repository *);
720 static const struct got_error *show_diff_view(struct tog_view *);
721 static const struct got_error *input_diff_view(struct tog_view **,
722 struct tog_view *, int);
723 static const struct got_error *reset_diff_view(struct tog_view *);
724 static const struct got_error* close_diff_view(struct tog_view *);
725 static const struct got_error *search_start_diff_view(struct tog_view *);
726 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
727 size_t *, int **, int **, int **, int **);
728 static const struct got_error *search_next_view_match(struct tog_view *);
730 static const struct got_error *open_log_view(struct tog_view *,
731 struct got_object_id *, struct got_repository *,
732 const char *, const char *, int);
733 static const struct got_error * show_log_view(struct tog_view *);
734 static const struct got_error *input_log_view(struct tog_view **,
735 struct tog_view *, int);
736 static const struct got_error *resize_log_view(struct tog_view *, int);
737 static const struct got_error *close_log_view(struct tog_view *);
738 static const struct got_error *search_start_log_view(struct tog_view *);
739 static const struct got_error *search_next_log_view(struct tog_view *);
741 static const struct got_error *open_blame_view(struct tog_view *, char *,
742 struct got_object_id *, struct got_repository *);
743 static const struct got_error *show_blame_view(struct tog_view *);
744 static const struct got_error *input_blame_view(struct tog_view **,
745 struct tog_view *, int);
746 static const struct got_error *reset_blame_view(struct tog_view *);
747 static const struct got_error *close_blame_view(struct tog_view *);
748 static const struct got_error *search_start_blame_view(struct tog_view *);
749 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
750 size_t *, int **, int **, int **, int **);
752 static const struct got_error *open_tree_view(struct tog_view *,
753 struct got_object_id *, const char *, struct got_repository *);
754 static const struct got_error *show_tree_view(struct tog_view *);
755 static const struct got_error *input_tree_view(struct tog_view **,
756 struct tog_view *, int);
757 static const struct got_error *close_tree_view(struct tog_view *);
758 static const struct got_error *search_start_tree_view(struct tog_view *);
759 static const struct got_error *search_next_tree_view(struct tog_view *);
761 static const struct got_error *open_ref_view(struct tog_view *,
762 struct got_repository *);
763 static const struct got_error *show_ref_view(struct tog_view *);
764 static const struct got_error *input_ref_view(struct tog_view **,
765 struct tog_view *, int);
766 static const struct got_error *close_ref_view(struct tog_view *);
767 static const struct got_error *search_start_ref_view(struct tog_view *);
768 static const struct got_error *search_next_ref_view(struct tog_view *);
770 static const struct got_error *open_help_view(struct tog_view *,
772 static const struct got_error *show_help_view(struct tog_view *);
773 static const struct got_error *input_help_view(struct tog_view **,
774 struct tog_view *, int);
775 static const struct got_error *reset_help_view(struct tog_view *);
776 static const struct got_error* close_help_view(struct tog_view *);
777 static const struct got_error *search_start_help_view(struct tog_view *);
778 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
779 size_t *, int **, int **, int **, int **);
781 static volatile sig_atomic_t tog_sigwinch_received;
782 static volatile sig_atomic_t tog_sigpipe_received;
783 static volatile sig_atomic_t tog_sigcont_received;
784 static volatile sig_atomic_t tog_sigint_received;
785 static volatile sig_atomic_t tog_sigterm_received;
788 tog_sigwinch(int signo)
790 tog_sigwinch_received = 1;
794 tog_sigpipe(int signo)
796 tog_sigpipe_received = 1;
800 tog_sigcont(int signo)
802 tog_sigcont_received = 1;
806 tog_sigint(int signo)
808 tog_sigint_received = 1;
812 tog_sigterm(int signo)
814 tog_sigterm_received = 1;
818 tog_fatal_signal_received(void)
820 return (tog_sigpipe_received ||
821 tog_sigint_received || tog_sigterm_received);
824 static const struct got_error *
825 view_close(struct tog_view *view)
827 const struct got_error *err = NULL, *child_err = NULL;
830 child_err = view_close(view->child);
834 err = view->close(view);
836 del_panel(view->panel);
838 delwin(view->window);
840 return err ? err : child_err;
843 static struct tog_view *
844 view_open(int nlines, int ncols, int begin_y, int begin_x,
845 enum tog_view_type type)
847 struct tog_view *view = calloc(1, sizeof(*view));
855 view->nlines = nlines ? nlines : LINES - begin_y;
856 view->ncols = ncols ? ncols : COLS - begin_x;
857 view->begin_y = begin_y;
858 view->begin_x = begin_x;
859 view->window = newwin(nlines, ncols, begin_y, begin_x);
860 if (view->window == NULL) {
864 view->panel = new_panel(view->window);
865 if (view->panel == NULL ||
866 set_panel_userptr(view->panel, view) != OK) {
871 keypad(view->window, TRUE);
876 view_split_begin_x(int begin_x)
878 if (begin_x > 0 || COLS < 120)
880 return (COLS - MAX(COLS / 2, 80));
883 /* XXX Stub till we decide what to do. */
885 view_split_begin_y(int lines)
887 return lines * HSPLIT_SCALE;
890 static const struct got_error *view_resize(struct tog_view *);
892 static const struct got_error *
893 view_splitscreen(struct tog_view *view)
895 const struct got_error *err = NULL;
897 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
898 if (view->resized_y && view->resized_y < view->lines)
899 view->begin_y = view->resized_y;
901 view->begin_y = view_split_begin_y(view->nlines);
903 } else if (!view->resized) {
904 if (view->resized_x && view->resized_x < view->cols - 1 &&
906 view->begin_x = view->resized_x;
908 view->begin_x = view_split_begin_x(0);
911 view->nlines = LINES - view->begin_y;
912 view->ncols = COLS - view->begin_x;
915 err = view_resize(view);
919 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
920 view->parent->nlines = view->begin_y;
922 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
923 return got_error_from_errno("mvwin");
928 static const struct got_error *
929 view_fullscreen(struct tog_view *view)
931 const struct got_error *err = NULL;
934 view->begin_y = view->resized ? view->begin_y : 0;
935 view->nlines = view->resized ? view->nlines : LINES;
939 err = view_resize(view);
943 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
944 return got_error_from_errno("mvwin");
950 view_is_parent_view(struct tog_view *view)
952 return view->parent == NULL;
956 view_is_splitscreen(struct tog_view *view)
958 return view->begin_x > 0 || view->begin_y > 0;
962 view_is_fullscreen(struct tog_view *view)
964 return view->nlines == LINES && view->ncols == COLS;
968 view_is_hsplit_top(struct tog_view *view)
970 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
971 view_is_splitscreen(view->child);
975 view_border(struct tog_view *view)
978 const struct tog_view *view_above;
981 return view_border(view->parent);
983 panel = panel_above(view->panel);
987 view_above = panel_userptr(panel);
988 if (view->mode == TOG_VIEW_SPLIT_HRZN)
989 mvwhline(view->window, view_above->begin_y - 1,
990 view->begin_x, ACS_HLINE, view->ncols);
992 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
993 ACS_VLINE, view->nlines);
996 static const struct got_error *view_init_hsplit(struct tog_view *, int);
997 static const struct got_error *request_log_commits(struct tog_view *);
998 static const struct got_error *offset_selection_down(struct tog_view *);
999 static void offset_selection_up(struct tog_view *);
1000 static void view_get_split(struct tog_view *, int *, int *);
1002 static const struct got_error *
1003 view_resize(struct tog_view *view)
1005 const struct got_error *err = NULL;
1006 int dif, nlines, ncols;
1008 dif = LINES - view->lines; /* line difference */
1010 if (view->lines > LINES)
1011 nlines = view->nlines - (view->lines - LINES);
1013 nlines = view->nlines + (LINES - view->lines);
1014 if (view->cols > COLS)
1015 ncols = view->ncols - (view->cols - COLS);
1017 ncols = view->ncols + (COLS - view->cols);
1020 int hs = view->child->begin_y;
1022 if (!view_is_fullscreen(view))
1023 view->child->begin_x = view_split_begin_x(view->begin_x);
1024 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1025 view->child->begin_x == 0) {
1028 view_fullscreen(view->child);
1029 if (view->child->focussed)
1030 show_panel(view->child->panel);
1032 show_panel(view->panel);
1034 ncols = view->child->begin_x;
1036 view_splitscreen(view->child);
1037 show_panel(view->child->panel);
1040 * XXX This is ugly and needs to be moved into the above
1041 * logic but "works" for now and my attempts at moving it
1042 * break either 'tab' or 'F' key maps in horizontal splits.
1045 err = view_splitscreen(view->child);
1048 if (dif < 0) { /* top split decreased */
1049 err = offset_selection_down(view);
1056 show_panel(view->child->panel);
1057 nlines = view->nlines;
1059 } else if (view->parent == NULL)
1062 if (view->resize && dif > 0) {
1063 err = view->resize(view, dif);
1068 if (wresize(view->window, nlines, ncols) == ERR)
1069 return got_error_from_errno("wresize");
1070 if (replace_panel(view->panel, view->window) == ERR)
1071 return got_error_from_errno("replace_panel");
1072 wclear(view->window);
1074 view->nlines = nlines;
1075 view->ncols = ncols;
1076 view->lines = LINES;
1082 static const struct got_error *
1083 resize_log_view(struct tog_view *view, int increase)
1085 struct tog_log_view_state *s = &view->state.log;
1086 const struct got_error *err = NULL;
1089 if (s->selected_entry)
1090 n = s->selected_entry->idx + view->lines - s->selected;
1093 * Request commits to account for the increased
1094 * height so we have enough to populate the view.
1096 if (s->commits->ncommits < n) {
1097 view->nscrolled = n - s->commits->ncommits + increase + 1;
1098 err = request_log_commits(view);
1105 view_adjust_offset(struct tog_view *view, int n)
1110 if (view->parent && view->parent->offset) {
1111 if (view->parent->offset + n >= 0)
1112 view->parent->offset += n;
1114 view->parent->offset = 0;
1115 } else if (view->offset) {
1116 if (view->offset - n >= 0)
1123 static const struct got_error *
1124 view_resize_split(struct tog_view *view, int resize)
1126 const struct got_error *err = NULL;
1127 struct tog_view *v = NULL;
1134 if (!v->child || !view_is_splitscreen(v->child))
1137 v->resized = v->child->resized = resize; /* lock for resize event */
1139 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1140 if (v->child->resized_y)
1141 v->child->begin_y = v->child->resized_y;
1143 v->child->begin_y -= resize;
1145 v->child->begin_y += resize;
1146 if (v->child->begin_y < 3) {
1148 v->child->begin_y = 3;
1149 } else if (v->child->begin_y > LINES - 1) {
1151 v->child->begin_y = LINES - 1;
1154 v->child->ncols = COLS;
1155 view_adjust_offset(view, resize);
1156 err = view_init_hsplit(v, v->child->begin_y);
1159 v->child->resized_y = v->child->begin_y;
1161 if (v->child->resized_x)
1162 v->child->begin_x = v->child->resized_x;
1164 v->child->begin_x -= resize;
1166 v->child->begin_x += resize;
1167 if (v->child->begin_x < 11) {
1169 v->child->begin_x = 11;
1170 } else if (v->child->begin_x > COLS - 1) {
1172 v->child->begin_x = COLS - 1;
1174 v->child->resized_x = v->child->begin_x;
1177 v->child->mode = v->mode;
1178 v->child->nlines = v->lines - v->child->begin_y;
1179 v->child->ncols = v->cols - v->child->begin_x;
1182 err = view_fullscreen(v);
1185 err = view_splitscreen(v->child);
1189 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 err = offset_selection_down(v->child);
1196 err = v->resize(v, 0);
1197 else if (v->child->resize)
1198 err = v->child->resize(v->child, 0);
1200 v->resized = v->child->resized = 0;
1206 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1208 struct tog_view *v = src->child ? src->child : src;
1210 dst->resized_x = v->resized_x;
1211 dst->resized_y = v->resized_y;
1214 static const struct got_error *
1215 view_close_child(struct tog_view *view)
1217 const struct got_error *err = NULL;
1219 if (view->child == NULL)
1222 err = view_close(view->child);
1227 static const struct got_error *
1228 view_set_child(struct tog_view *view, struct tog_view *child)
1230 const struct got_error *err = NULL;
1232 view->child = child;
1233 child->parent = view;
1235 err = view_resize(view);
1239 if (view->child->resized_x || view->child->resized_y)
1240 err = view_resize_split(view, 0);
1245 static const struct got_error *view_dispatch_request(struct tog_view **,
1246 struct tog_view *, enum tog_view_type, int, int);
1248 static const struct got_error *
1249 view_request_new(struct tog_view **requested, struct tog_view *view,
1250 enum tog_view_type request)
1252 struct tog_view *new_view = NULL;
1253 const struct got_error *err;
1258 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1259 view_get_split(view, &y, &x);
1261 err = view_dispatch_request(&new_view, view, request, y, x);
1265 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1266 request != TOG_VIEW_HELP) {
1267 err = view_init_hsplit(view, y);
1273 new_view->focussed = 1;
1274 new_view->mode = view->mode;
1275 new_view->nlines = request == TOG_VIEW_HELP ?
1276 view->lines : view->lines - y;
1278 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1279 view_transfer_size(new_view, view);
1280 err = view_close_child(view);
1283 err = view_set_child(view, new_view);
1286 view->focus_child = 1;
1288 *requested = new_view;
1294 tog_resizeterm(void)
1297 struct winsize size;
1299 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1300 cols = 80; /* Default */
1304 lines = size.ws_row;
1306 resize_term(lines, cols);
1309 static const struct got_error *
1310 view_search_start(struct tog_view *view, int fast_refresh)
1312 const struct got_error *err = NULL;
1313 struct tog_view *v = view;
1317 if (view->search_started) {
1318 regfree(&view->regex);
1319 view->searching = 0;
1320 memset(&view->regmatch, 0, sizeof(view->regmatch));
1322 view->search_started = 0;
1324 if (view->nlines < 1)
1327 if (view_is_hsplit_top(view))
1329 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1332 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1333 wclrtoeol(v->window);
1335 nodelay(v->window, FALSE); /* block for search term input */
1338 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1339 wrefresh(v->window);
1342 nodelay(v->window, TRUE);
1343 if (!fast_refresh && !using_mock_io)
1348 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1349 err = view->search_start(view);
1351 regfree(&view->regex);
1354 view->search_started = 1;
1355 view->searching = TOG_SEARCH_FORWARD;
1356 view->search_next_done = 0;
1357 view->search_next(view);
1363 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1364 static const struct got_error *
1365 switch_split(struct tog_view *view)
1367 const struct got_error *err = NULL;
1368 struct tog_view *v = NULL;
1375 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1376 v->mode = TOG_VIEW_SPLIT_VERT;
1378 v->mode = TOG_VIEW_SPLIT_HRZN;
1382 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1383 v->mode = TOG_VIEW_SPLIT_NONE;
1385 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1386 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1387 v->child->begin_y = v->child->resized_y;
1388 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1389 v->child->begin_x = v->child->resized_x;
1392 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1394 v->child->ncols = COLS;
1395 v->child->nscrolled = LINES - v->child->nlines;
1397 err = view_init_hsplit(v, v->child->begin_y);
1401 v->child->mode = v->mode;
1402 v->child->nlines = v->lines - v->child->begin_y;
1405 err = view_fullscreen(v);
1408 err = view_splitscreen(v->child);
1412 if (v->mode == TOG_VIEW_SPLIT_NONE)
1413 v->mode = TOG_VIEW_SPLIT_VERT;
1414 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1415 err = offset_selection_down(v);
1418 err = offset_selection_down(v->child);
1422 offset_selection_up(v);
1423 offset_selection_up(v->child);
1426 err = v->resize(v, 0);
1427 else if (v->child->resize)
1428 err = v->child->resize(v->child, 0);
1434 * Strip trailing whitespace from str starting at byte *n;
1435 * if *n < 0, use strlen(str). Return new str length in *n.
1438 strip_trailing_ws(char *str, int *n)
1442 if (str == NULL || *str == '\0')
1448 while (x-- > 0 && isspace((unsigned char)str[x]))
1455 * Extract visible substring of line y from the curses screen
1456 * and strip trailing whitespace. If vline is set, overwrite
1457 * line[vline] with '|' because the ACS_VLINE character is
1458 * written out as 'x'. Write the line to file f.
1460 static const struct got_error *
1461 view_write_line(FILE *f, int y, int vline)
1463 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1466 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1468 return got_error_fmt(GOT_ERR_RANGE,
1469 "failed to extract line %d", y);
1472 * In some views, lines are padded with blanks to COLS width.
1473 * Strip them so we can diff without the -b flag when testing.
1475 strip_trailing_ws(line, &r);
1480 w = fprintf(f, "%s\n", line);
1481 if (w != r + 1) /* \n */
1482 return got_ferror(f, GOT_ERR_IO);
1488 * Capture the visible curses screen by writing each line to the
1489 * file at the path set via the TOG_SCR_DUMP environment variable.
1491 static const struct got_error *
1492 screendump(struct tog_view *view)
1494 const struct got_error *err;
1497 err = got_opentemp_truncate(tog_io.sdump);
1501 if ((view->child && view->child->begin_x) ||
1502 (view->parent && view->begin_x)) {
1503 int ncols = view->child ? view->ncols : view->parent->ncols;
1505 /* vertical splitscreen */
1506 for (i = 0; i < view->nlines; ++i) {
1507 err = view_write_line(tog_io.sdump, i, ncols - 1);
1514 /* fullscreen or horizontal splitscreen */
1515 if ((view->child && view->child->begin_y) ||
1516 (view->parent && view->begin_y)) /* hsplit */
1517 hline = view->child ?
1518 view->child->begin_y : view->begin_y;
1520 for (i = 0; i < view->lines; i++) {
1521 if (hline && i == hline - 1) {
1524 /* ACS_HLINE writes out as 'q', overwrite it */
1525 for (c = 0; c < view->cols; ++c)
1526 fputc('-', tog_io.sdump);
1527 fputc('\n', tog_io.sdump);
1531 err = view_write_line(tog_io.sdump, i, 0);
1542 * Compute view->count from numeric input. Assign total to view->count and
1543 * return first non-numeric key entered.
1546 get_compound_key(struct tog_view *view, int c)
1548 struct tog_view *v = view;
1551 if (view_is_hsplit_top(view))
1553 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1557 cbreak(); /* block for input */
1558 nodelay(view->window, FALSE);
1559 wmove(v->window, v->nlines - 1, 0);
1560 wclrtoeol(v->window);
1561 waddch(v->window, ':');
1564 x = getcurx(v->window);
1565 if (x != ERR && x < view->ncols) {
1566 waddch(v->window, c);
1567 wrefresh(v->window);
1571 * Don't overflow. Max valid request should be the greatest
1572 * between the longest and total lines; cap at 10 million.
1577 n = n * 10 + (c - '0');
1578 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1580 if (c == 'G' || c == 'g') { /* nG key map */
1581 view->gline = view->hiline = n;
1586 /* Massage excessive or inapplicable values at the input handler. */
1593 action_report(struct tog_view *view)
1595 struct tog_view *v = view;
1597 if (view_is_hsplit_top(view))
1599 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1602 wmove(v->window, v->nlines - 1, 0);
1603 wclrtoeol(v->window);
1604 wprintw(v->window, ":%s", view->action);
1605 wrefresh(v->window);
1608 * Clear action status report. Only clear in blame view
1609 * once annotating is complete, otherwise it's too fast.
1611 if (view->type == TOG_VIEW_BLAME) {
1612 if (view->state.blame.blame_complete)
1613 view->action = NULL;
1615 view->action = NULL;
1619 * Read the next line from the test script and assign
1620 * key instruction to *ch. If at EOF, set the *done flag.
1622 static const struct got_error *
1623 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1625 const struct got_error *err = NULL;
1629 if (view->count && --view->count) {
1635 if (getline(&line, &linesz, script) == -1) {
1640 err = got_ferror(script, GOT_ERR_IO);
1645 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1646 tog_io.wait_for_ui = 1;
1647 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1649 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1651 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1653 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1655 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1657 else if (strncasecmp(line, "TAB", 3) == 0)
1659 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1660 *ch = TOG_KEY_SCRDUMP;
1661 else if (isdigit((unsigned char)*line)) {
1664 while (isdigit((unsigned char)*t))
1666 view->ch = *ch = *t;
1668 /* ignore error, view->count is 0 if instruction is invalid */
1669 view->count = strtonum(line, 0, INT_MAX, NULL);
1678 static const struct got_error *
1679 view_input(struct tog_view **new, int *done, struct tog_view *view,
1680 struct tog_view_list_head *views, int fast_refresh)
1682 const struct got_error *err = NULL;
1689 action_report(view);
1691 /* Clear "no matches" indicator. */
1692 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1693 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1694 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1698 if (view->searching && !view->search_next_done) {
1699 errcode = pthread_mutex_unlock(&tog_mutex);
1701 return got_error_set_errno(errcode,
1702 "pthread_mutex_unlock");
1704 errcode = pthread_mutex_lock(&tog_mutex);
1706 return got_error_set_errno(errcode,
1707 "pthread_mutex_lock");
1708 view->search_next(view);
1712 /* Allow threads to make progress while we are waiting for input. */
1713 errcode = pthread_mutex_unlock(&tog_mutex);
1715 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1717 if (using_mock_io) {
1718 err = tog_read_script_key(tog_io.f, view, &ch, done);
1720 errcode = pthread_mutex_lock(&tog_mutex);
1723 } else if (view->count && --view->count) {
1725 nodelay(view->window, TRUE);
1726 ch = wgetch(view->window);
1727 /* let C-g or backspace abort unfinished count */
1728 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1733 ch = wgetch(view->window);
1734 if (ch >= '1' && ch <= '9')
1735 view->ch = ch = get_compound_key(view, ch);
1737 if (view->hiline && ch != ERR && ch != 0)
1738 view->hiline = 0; /* key pressed, clear line highlight */
1739 nodelay(view->window, TRUE);
1740 errcode = pthread_mutex_lock(&tog_mutex);
1742 return got_error_set_errno(errcode, "pthread_mutex_lock");
1744 if (tog_sigwinch_received || tog_sigcont_received) {
1746 tog_sigwinch_received = 0;
1747 tog_sigcont_received = 0;
1748 TAILQ_FOREACH(v, views, entry) {
1749 err = view_resize(v);
1752 err = v->input(new, v, KEY_RESIZE);
1756 err = view_resize(v->child);
1759 err = v->child->input(new, v->child,
1763 if (v->child->resized_x || v->child->resized_y) {
1764 err = view_resize_split(v, 0);
1776 if (view->type == TOG_VIEW_HELP)
1777 err = view->reset(view);
1779 err = view_request_new(new, view, TOG_VIEW_HELP);
1785 view->child->focussed = 1;
1786 view->focus_child = 1;
1787 } else if (view->parent) {
1789 view->parent->focussed = 1;
1790 view->parent->focus_child = 0;
1791 if (!view_is_splitscreen(view)) {
1792 if (view->parent->resize) {
1793 err = view->parent->resize(view->parent,
1798 offset_selection_up(view->parent);
1799 err = view_fullscreen(view->parent);
1806 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1807 if (view->parent->resize) {
1808 /* might need more commits to fill fullscreen */
1809 err = view->parent->resize(view->parent, 0);
1813 offset_selection_up(view->parent);
1815 err = view->input(new, view, ch);
1823 if (view_is_parent_view(view)) {
1824 if (view->child == NULL)
1826 if (view_is_splitscreen(view->child)) {
1828 view->child->focussed = 1;
1829 err = view_fullscreen(view->child);
1831 err = view_splitscreen(view->child);
1833 err = view_resize_split(view, 0);
1837 err = view->child->input(new, view->child,
1840 if (view_is_splitscreen(view)) {
1841 view->parent->focussed = 0;
1843 err = view_fullscreen(view);
1845 err = view_splitscreen(view);
1846 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1847 err = view_resize(view->parent);
1849 err = view_resize_split(view, 0);
1853 err = view->input(new, view, KEY_RESIZE);
1858 err = view->resize(view, 0);
1863 if (view->parent->resize) {
1864 err = view->parent->resize(view->parent, 0);
1868 err = offset_selection_down(view->parent);
1872 err = offset_selection_down(view);
1876 err = switch_split(view);
1879 err = view_resize_split(view, -1);
1882 err = view_resize_split(view, 1);
1888 if (view->search_start)
1889 view_search_start(view, fast_refresh);
1891 err = view->input(new, view, ch);
1895 if (view->search_started && view->search_next) {
1896 view->searching = (ch == 'n' ?
1897 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1898 view->search_next_done = 0;
1899 view->search_next(view);
1901 err = view->input(new, view, ch);
1904 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1905 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1906 view->action = "Patience diff algorithm";
1908 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1909 view->action = "Myers diff algorithm";
1911 TAILQ_FOREACH(v, views, entry) {
1917 if (v->child && v->child->reset) {
1918 err = v->child->reset(v->child);
1924 case TOG_KEY_SCRDUMP:
1925 err = screendump(view);
1928 err = view->input(new, view, ch);
1936 view_needs_focus_indication(struct tog_view *view)
1938 if (view_is_parent_view(view)) {
1939 if (view->child == NULL || view->child->focussed)
1941 if (!view_is_splitscreen(view->child))
1943 } else if (!view_is_splitscreen(view))
1946 return view->focussed;
1949 static const struct got_error *
1952 const struct got_error *err = NULL;
1954 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1955 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1956 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1957 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1958 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1959 err = got_ferror(tog_io.f, GOT_ERR_IO);
1960 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1961 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1966 static const struct got_error *
1967 view_loop(struct tog_view *view)
1969 const struct got_error *err = NULL;
1970 struct tog_view_list_head views;
1971 struct tog_view *new_view;
1973 int fast_refresh = 10;
1974 int done = 0, errcode;
1976 mode = getenv("TOG_VIEW_SPLIT_MODE");
1977 if (!mode || !(*mode == 'h' || *mode == 'H'))
1978 view->mode = TOG_VIEW_SPLIT_VERT;
1980 view->mode = TOG_VIEW_SPLIT_HRZN;
1982 errcode = pthread_mutex_lock(&tog_mutex);
1984 return got_error_set_errno(errcode, "pthread_mutex_lock");
1987 TAILQ_INSERT_HEAD(&views, view, entry);
1990 err = view->show(view);
1995 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1996 !tog_fatal_signal_received()) {
1997 /* Refresh fast during initialization, then become slower. */
1998 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1999 halfdelay(10); /* switch to once per second */
2001 err = view_input(&new_view, &done, view, &views, fast_refresh);
2005 if (view->dying && view == TAILQ_FIRST(&views) &&
2006 TAILQ_NEXT(view, entry) == NULL)
2012 * When we quit, scroll the screen up a single line
2013 * so we don't lose any information.
2015 TAILQ_FOREACH(v, &views, entry) {
2016 wmove(v->window, 0, 0);
2017 wdeleteln(v->window);
2018 wnoutrefresh(v->window);
2019 if (v->child && !view_is_fullscreen(v)) {
2020 wmove(v->child->window, 0, 0);
2021 wdeleteln(v->child->window);
2022 wnoutrefresh(v->child->window);
2029 struct tog_view *v, *prev = NULL;
2031 if (view_is_parent_view(view))
2032 prev = TAILQ_PREV(view, tog_view_list_head,
2034 else if (view->parent)
2035 prev = view->parent;
2038 view->parent->child = NULL;
2039 view->parent->focus_child = 0;
2040 /* Restore fullscreen line height. */
2041 view->parent->nlines = view->parent->lines;
2042 err = view_resize(view->parent);
2045 /* Make resized splits persist. */
2046 view_transfer_size(view->parent, view);
2048 TAILQ_REMOVE(&views, view, entry);
2050 err = view_close(view);
2055 TAILQ_FOREACH(v, &views, entry) {
2059 if (view == NULL && new_view == NULL) {
2060 /* No view has focus. Try to pick one. */
2063 else if (!TAILQ_EMPTY(&views)) {
2064 view = TAILQ_LAST(&views,
2065 tog_view_list_head);
2068 if (view->focus_child) {
2069 view->child->focussed = 1;
2077 struct tog_view *v, *t;
2078 /* Only allow one parent view per type. */
2079 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2080 if (v->type != new_view->type)
2082 TAILQ_REMOVE(&views, v, entry);
2083 err = view_close(v);
2088 TAILQ_INSERT_TAIL(&views, new_view, entry);
2091 if (view && !done) {
2092 if (view_is_parent_view(view)) {
2093 if (view->child && view->child->focussed)
2096 if (view->parent && view->parent->focussed)
2097 view = view->parent;
2099 show_panel(view->panel);
2100 if (view->child && view_is_splitscreen(view->child))
2101 show_panel(view->child->panel);
2102 if (view->parent && view_is_splitscreen(view)) {
2103 err = view->parent->show(view->parent);
2107 err = view->show(view);
2111 err = view->child->show(view->child);
2120 while (!TAILQ_EMPTY(&views)) {
2121 const struct got_error *close_err;
2122 view = TAILQ_FIRST(&views);
2123 TAILQ_REMOVE(&views, view, entry);
2124 close_err = view_close(view);
2125 if (close_err && err == NULL)
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2130 if (errcode && err == NULL)
2131 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2141 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2146 /* Create newly allocated wide-character string equivalent to a byte string. */
2147 static const struct got_error *
2148 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2151 const struct got_error *err = NULL;
2154 *wlen = mbstowcs(NULL, s, 0);
2155 if (*wlen == (size_t)-1) {
2157 if (errno != EILSEQ)
2158 return got_error_from_errno("mbstowcs");
2160 /* byte string invalid in current encoding; try to "fix" it */
2161 err = got_mbsavis(&vis, &vislen, s);
2164 *wlen = mbstowcs(NULL, vis, 0);
2165 if (*wlen == (size_t)-1) {
2166 err = got_error_from_errno("mbstowcs"); /* give up */
2171 *ws = calloc(*wlen + 1, sizeof(**ws));
2173 err = got_error_from_errno("calloc");
2177 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2178 err = got_error_from_errno("mbstowcs");
2189 static const struct got_error *
2190 expand_tab(char **ptr, const char *src)
2193 size_t len, n, idx = 0, sz = 0;
2196 n = len = strlen(src);
2197 dst = malloc(n + 1);
2199 return got_error_from_errno("malloc");
2201 while (idx < len && src[idx]) {
2202 const char c = src[idx];
2205 size_t nb = TABSIZE - sz % TABSIZE;
2208 p = realloc(dst, n + nb);
2211 return got_error_from_errno("realloc");
2216 memset(dst + sz, ' ', nb);
2219 dst[sz++] = src[idx];
2229 * Advance at most n columns from wline starting at offset off.
2230 * Return the index to the first character after the span operation.
2231 * Return the combined column width of all spanned wide characters in
2235 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2237 int width, i, cols = 0;
2244 for (i = off; wline[i] != L'\0'; ++i) {
2245 if (wline[i] == L'\t')
2246 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2248 width = wcwidth(wline[i]);
2255 if (cols + width > n)
2265 * Format a line for display, ensuring that it won't overflow a width limit.
2266 * With scrolling, the width returned refers to the scrolled version of the
2267 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2269 static const struct got_error *
2270 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2271 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2273 const struct got_error *err = NULL;
2275 wchar_t *wline = NULL;
2284 err = expand_tab(&exstr, line);
2289 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2294 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2296 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2297 wline[wlen - 1] = L'\0';
2300 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2301 wline[wlen - 1] = L'\0';
2305 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2311 *scrollxp = scrollx;
2319 static const struct got_error*
2320 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2321 struct got_object_id *id, struct got_repository *repo)
2323 static const struct got_error *err = NULL;
2324 struct got_reflist_entry *re;
2333 TAILQ_FOREACH(re, refs, entry) {
2334 struct got_tag_object *tag = NULL;
2335 struct got_object_id *ref_id;
2338 name = got_ref_get_name(re->ref);
2339 if (strcmp(name, GOT_REF_HEAD) == 0)
2341 if (strncmp(name, "refs/", 5) == 0)
2343 if (strncmp(name, "got/", 4) == 0)
2345 if (strncmp(name, "heads/", 6) == 0)
2347 if (strncmp(name, "remotes/", 8) == 0) {
2349 s = strstr(name, "/" GOT_REF_HEAD);
2350 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2353 err = got_ref_resolve(&ref_id, repo, re->ref);
2356 if (strncmp(name, "tags/", 5) == 0) {
2357 err = got_object_open_as_tag(&tag, repo, ref_id);
2359 if (err->code != GOT_ERR_OBJ_TYPE) {
2363 /* Ref points at something other than a tag. */
2368 cmp = got_object_id_cmp(tag ?
2369 got_object_tag_get_object_id(tag) : ref_id, id);
2372 got_object_tag_close(tag);
2376 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2377 s ? ", " : "", name) == -1) {
2378 err = got_error_from_errno("asprintf");
2389 static const struct got_error *
2390 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2395 smallerthan = strchr(author, '<');
2396 if (smallerthan && smallerthan[1] != '\0')
2397 author = smallerthan + 1;
2398 author[strcspn(author, "@>")] = '\0';
2399 return format_line(wauthor, author_width, NULL, author, 0, limit,
2403 static const struct got_error *
2404 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2405 struct got_object_id *id, const size_t date_display_cols,
2406 int author_display_cols)
2408 struct tog_log_view_state *s = &view->state.log;
2409 const struct got_error *err = NULL;
2410 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2411 char *refs_str = NULL;
2412 char *logmsg0 = NULL, *logmsg = NULL;
2413 char *author = NULL;
2414 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2415 int author_width, refstr_width, logmsg_width;
2416 char *newline, *line = NULL;
2417 int col, limit, scrollx, logmsg_x;
2418 const int avail = view->ncols;
2420 time_t committer_time;
2421 struct tog_color *tc;
2422 struct got_reflist_head *refs;
2424 committer_time = got_object_commit_get_committer_time(commit);
2425 if (gmtime_r(&committer_time, &tm) == NULL)
2426 return got_error_from_errno("gmtime_r");
2427 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2428 return got_error(GOT_ERR_NO_SPACE);
2430 if (avail <= date_display_cols)
2431 limit = MIN(sizeof(datebuf) - 1, avail);
2433 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2434 tc = get_color(&s->colors, TOG_COLOR_DATE);
2436 wattr_on(view->window,
2437 COLOR_PAIR(tc->colorpair), NULL);
2438 waddnstr(view->window, datebuf, limit);
2440 wattr_off(view->window,
2441 COLOR_PAIR(tc->colorpair), NULL);
2448 err = got_object_id_str(&id_str, id);
2451 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2453 wattr_on(view->window,
2454 COLOR_PAIR(tc->colorpair), NULL);
2455 wprintw(view->window, "%.8s ", id_str);
2457 wattr_off(view->window,
2458 COLOR_PAIR(tc->colorpair), NULL);
2465 if (s->use_committer)
2466 author = strdup(got_object_commit_get_committer(commit));
2468 author = strdup(got_object_commit_get_author(commit));
2469 if (author == NULL) {
2470 err = got_error_from_errno("strdup");
2473 err = format_author(&wauthor, &author_width, author, avail - col, col);
2476 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2478 wattr_on(view->window,
2479 COLOR_PAIR(tc->colorpair), NULL);
2480 waddwstr(view->window, wauthor);
2481 col += author_width;
2482 while (col < avail && author_width < author_display_cols + 2) {
2483 waddch(view->window, ' ');
2488 wattr_off(view->window,
2489 COLOR_PAIR(tc->colorpair), NULL);
2493 err = got_object_commit_get_logmsg(&logmsg0, commit);
2497 while (*logmsg == '\n')
2499 newline = strchr(logmsg, '\n');
2503 limit = avail - col;
2504 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2505 limit--; /* for the border */
2507 /* Prepend reference labels to log message if possible .*/
2508 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2509 err = build_refs_str(&refs_str, refs, id, s->repo);
2515 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2516 err = got_error_from_errno("asprintf");
2519 err = format_line(&wrefstr, &refstr_width,
2520 &scrollx, rs, view->x, limit, col, 1);
2524 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2526 wattr_on(view->window,
2527 COLOR_PAIR(tc->colorpair), NULL);
2528 waddwstr(view->window, &wrefstr[scrollx]);
2530 wattr_off(view->window,
2531 COLOR_PAIR(tc->colorpair), NULL);
2532 col += MAX(refstr_width, 0);
2537 waddch(view->window, ' ');
2541 if (refstr_width > 0)
2544 int unscrolled_refstr_width;
2545 size_t len = wcslen(wrefstr);
2548 * No need to check for -1 return value here since
2549 * unprintables have been replaced by span_wline().
2551 unscrolled_refstr_width = wcswidth(wrefstr, len);
2552 unscrolled_refstr_width += 1; /* trailing space */
2553 logmsg_x = view->x - unscrolled_refstr_width;
2556 limit = avail - col;
2557 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2558 limit--; /* for the border */
2562 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2566 waddwstr(view->window, &wlogmsg[scrollx]);
2567 col += MAX(logmsg_width, 0);
2568 while (col < avail) {
2569 waddch(view->window, ' ');
2583 static struct commit_queue_entry *
2584 alloc_commit_queue_entry(struct got_commit_object *commit,
2585 struct got_object_id *id)
2587 struct commit_queue_entry *entry;
2588 struct got_object_id *dup;
2590 entry = calloc(1, sizeof(*entry));
2594 dup = got_object_id_dup(id);
2601 entry->commit = commit;
2606 pop_commit(struct commit_queue *commits)
2608 struct commit_queue_entry *entry;
2610 entry = TAILQ_FIRST(&commits->head);
2611 TAILQ_REMOVE(&commits->head, entry, entry);
2612 got_object_commit_close(entry->commit);
2613 commits->ncommits--;
2619 free_commits(struct commit_queue *commits)
2621 while (!TAILQ_EMPTY(&commits->head))
2622 pop_commit(commits);
2625 static const struct got_error *
2626 match_commit(int *have_match, struct got_object_id *id,
2627 struct got_commit_object *commit, regex_t *regex)
2629 const struct got_error *err = NULL;
2630 regmatch_t regmatch;
2631 char *id_str = NULL, *logmsg = NULL;
2635 err = got_object_id_str(&id_str, id);
2639 err = got_object_commit_get_logmsg(&logmsg, commit);
2643 if (regexec(regex, got_object_commit_get_author(commit), 1,
2644 ®match, 0) == 0 ||
2645 regexec(regex, got_object_commit_get_committer(commit), 1,
2646 ®match, 0) == 0 ||
2647 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2648 regexec(regex, logmsg, 1, ®match, 0) == 0)
2656 static const struct got_error *
2657 queue_commits(struct tog_log_thread_args *a)
2659 const struct got_error *err = NULL;
2662 * We keep all commits open throughout the lifetime of the log
2663 * view in order to avoid having to re-fetch commits from disk
2664 * while updating the display.
2667 struct got_object_id id;
2668 struct got_commit_object *commit;
2669 struct commit_queue_entry *entry;
2670 int limit_match = 0;
2673 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2678 err = got_object_open_as_commit(&commit, a->repo, &id);
2681 entry = alloc_commit_queue_entry(commit, &id);
2682 if (entry == NULL) {
2683 err = got_error_from_errno("alloc_commit_queue_entry");
2687 errcode = pthread_mutex_lock(&tog_mutex);
2689 err = got_error_set_errno(errcode,
2690 "pthread_mutex_lock");
2694 entry->idx = a->real_commits->ncommits;
2695 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2696 a->real_commits->ncommits++;
2699 err = match_commit(&limit_match, &id, commit,
2705 struct commit_queue_entry *matched;
2707 matched = alloc_commit_queue_entry(
2708 entry->commit, entry->id);
2709 if (matched == NULL) {
2710 err = got_error_from_errno(
2711 "alloc_commit_queue_entry");
2714 matched->commit = entry->commit;
2715 got_object_commit_retain(entry->commit);
2717 matched->idx = a->limit_commits->ncommits;
2718 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2720 a->limit_commits->ncommits++;
2724 * This is how we signal log_thread() that we
2725 * have found a match, and that it should be
2726 * counted as a new entry for the view.
2728 a->limit_match = limit_match;
2731 if (*a->searching == TOG_SEARCH_FORWARD &&
2732 !*a->search_next_done) {
2734 err = match_commit(&have_match, &id, commit, a->regex);
2739 if (limit_match && have_match)
2740 *a->search_next_done =
2741 TOG_SEARCH_HAVE_MORE;
2742 } else if (have_match)
2743 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2746 errcode = pthread_mutex_unlock(&tog_mutex);
2747 if (errcode && err == NULL)
2748 err = got_error_set_errno(errcode,
2749 "pthread_mutex_unlock");
2752 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2758 select_commit(struct tog_log_view_state *s)
2760 struct commit_queue_entry *entry;
2763 entry = s->first_displayed_entry;
2765 if (ncommits == s->selected) {
2766 s->selected_entry = entry;
2769 entry = TAILQ_NEXT(entry, entry);
2774 static const struct got_error *
2775 draw_commits(struct tog_view *view)
2777 const struct got_error *err = NULL;
2778 struct tog_log_view_state *s = &view->state.log;
2779 struct commit_queue_entry *entry = s->selected_entry;
2780 int limit = view->nlines;
2782 int ncommits, author_cols = 4, refstr_cols;
2783 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2784 char *refs_str = NULL;
2786 struct tog_color *tc;
2787 static const size_t date_display_cols = 12;
2788 struct got_reflist_head *refs;
2790 if (view_is_hsplit_top(view))
2791 --limit; /* account for border */
2793 if (s->selected_entry &&
2794 !(view->searching && view->search_next_done == 0)) {
2795 err = got_object_id_str(&id_str, s->selected_entry->id);
2798 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2799 s->selected_entry->id);
2800 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2806 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2807 halfdelay(10); /* disable fast refresh */
2809 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2810 if (asprintf(&ncommits_str, " [%d/%d] %s",
2811 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2812 (view->searching && !view->search_next_done) ?
2813 "searching..." : "loading...") == -1) {
2814 err = got_error_from_errno("asprintf");
2818 const char *search_str = NULL;
2819 const char *limit_str = NULL;
2821 if (view->searching) {
2822 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2823 search_str = "no more matches";
2824 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2825 search_str = "no matches found";
2826 else if (!view->search_next_done)
2827 search_str = "searching...";
2830 if (s->limit_view && s->commits->ncommits == 0)
2831 limit_str = "no matches found";
2833 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2834 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2835 search_str ? search_str : (refs_str ? refs_str : ""),
2836 limit_str ? limit_str : "") == -1) {
2837 err = got_error_from_errno("asprintf");
2845 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2846 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2847 "........................................",
2848 s->in_repo_path, ncommits_str) == -1) {
2849 err = got_error_from_errno("asprintf");
2853 } else if (asprintf(&header, "commit %s%s",
2854 id_str ? id_str : "........................................",
2855 ncommits_str) == -1) {
2856 err = got_error_from_errno("asprintf");
2860 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2864 werase(view->window);
2866 if (view_needs_focus_indication(view))
2867 wstandout(view->window);
2868 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2870 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2871 waddwstr(view->window, wline);
2872 while (width < view->ncols) {
2873 waddch(view->window, ' ');
2877 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2878 if (view_needs_focus_indication(view))
2879 wstandend(view->window);
2884 /* Grow author column size if necessary, and set view->maxx. */
2885 entry = s->first_displayed_entry;
2889 struct got_commit_object *c = entry->commit;
2890 char *author, *eol, *msg, *msg0;
2891 wchar_t *wauthor, *wmsg;
2893 if (ncommits >= limit - 1)
2895 if (s->use_committer)
2896 author = strdup(got_object_commit_get_committer(c));
2898 author = strdup(got_object_commit_get_author(c));
2899 if (author == NULL) {
2900 err = got_error_from_errno("strdup");
2903 err = format_author(&wauthor, &width, author, COLS,
2905 if (author_cols < width)
2906 author_cols = width;
2911 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2913 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2918 err = format_line(&ws, &width, NULL, refs_str,
2919 0, INT_MAX, date_display_cols + author_cols, 0);
2925 refstr_cols = width + 3; /* account for [ ] + space */
2928 err = got_object_commit_get_logmsg(&msg0, c);
2932 while (*msg == '\n')
2934 if ((eol = strchr(msg, '\n')))
2936 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2937 date_display_cols + author_cols + refstr_cols, 0);
2940 view->maxx = MAX(view->maxx, width + refstr_cols);
2944 entry = TAILQ_NEXT(entry, entry);
2947 entry = s->first_displayed_entry;
2948 s->last_displayed_entry = s->first_displayed_entry;
2951 if (ncommits >= limit - 1)
2953 if (ncommits == s->selected)
2954 wstandout(view->window);
2955 err = draw_commit(view, entry->commit, entry->id,
2956 date_display_cols, author_cols);
2957 if (ncommits == s->selected)
2958 wstandend(view->window);
2962 s->last_displayed_entry = entry;
2963 entry = TAILQ_NEXT(entry, entry);
2976 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2978 struct commit_queue_entry *entry;
2981 entry = TAILQ_FIRST(&s->commits->head);
2982 if (s->first_displayed_entry == entry)
2985 entry = s->first_displayed_entry;
2986 while (entry && nscrolled < maxscroll) {
2987 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2989 s->first_displayed_entry = entry;
2995 static const struct got_error *
2996 trigger_log_thread(struct tog_view *view, int wait)
2998 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3002 halfdelay(1); /* fast refresh while loading commits */
3004 while (!ta->log_complete && !tog_thread_error &&
3005 (ta->commits_needed > 0 || ta->load_all)) {
3006 /* Wake the log thread. */
3007 errcode = pthread_cond_signal(&ta->need_commits);
3009 return got_error_set_errno(errcode,
3010 "pthread_cond_signal");
3013 * The mutex will be released while the view loop waits
3014 * in wgetch(), at which time the log thread will run.
3019 /* Display progress update in log view. */
3020 show_log_view(view);
3024 /* Wait right here while next commit is being loaded. */
3025 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3027 return got_error_set_errno(errcode,
3028 "pthread_cond_wait");
3030 /* Display progress update in log view. */
3031 show_log_view(view);
3039 static const struct got_error *
3040 request_log_commits(struct tog_view *view)
3042 struct tog_log_view_state *state = &view->state.log;
3043 const struct got_error *err = NULL;
3045 if (state->thread_args.log_complete)
3048 state->thread_args.commits_needed += view->nscrolled;
3049 err = trigger_log_thread(view, 1);
3050 view->nscrolled = 0;
3055 static const struct got_error *
3056 log_scroll_down(struct tog_view *view, int maxscroll)
3058 struct tog_log_view_state *s = &view->state.log;
3059 const struct got_error *err = NULL;
3060 struct commit_queue_entry *pentry;
3061 int nscrolled = 0, ncommits_needed;
3063 if (s->last_displayed_entry == NULL)
3066 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3067 if (s->commits->ncommits < ncommits_needed &&
3068 !s->thread_args.log_complete) {
3070 * Ask the log thread for required amount of commits.
3072 s->thread_args.commits_needed +=
3073 ncommits_needed - s->commits->ncommits;
3074 err = trigger_log_thread(view, 1);
3080 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3081 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3084 s->last_displayed_entry = pentry ?
3085 pentry : s->last_displayed_entry;
3087 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3090 s->first_displayed_entry = pentry;
3091 } while (++nscrolled < maxscroll);
3093 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3094 view->nscrolled += nscrolled;
3096 view->nscrolled = 0;
3101 static const struct got_error *
3102 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3103 struct got_commit_object *commit, struct got_object_id *commit_id,
3104 struct tog_view *log_view, struct got_repository *repo)
3106 const struct got_error *err;
3107 struct got_object_qid *parent_id;
3108 struct tog_view *diff_view;
3110 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3111 if (diff_view == NULL)
3112 return got_error_from_errno("view_open");
3114 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3115 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3116 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3118 *new_view = diff_view;
3122 static const struct got_error *
3123 tree_view_visit_subtree(struct tog_tree_view_state *s,
3124 struct got_tree_object *subtree)
3126 struct tog_parent_tree *parent;
3128 parent = calloc(1, sizeof(*parent));
3130 return got_error_from_errno("calloc");
3132 parent->tree = s->tree;
3133 parent->first_displayed_entry = s->first_displayed_entry;
3134 parent->selected_entry = s->selected_entry;
3135 parent->selected = s->selected;
3136 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3139 s->first_displayed_entry = NULL;
3143 static const struct got_error *
3144 tree_view_walk_path(struct tog_tree_view_state *s,
3145 struct got_commit_object *commit, const char *path)
3147 const struct got_error *err = NULL;
3148 struct got_tree_object *tree = NULL;
3150 char *slash, *subpath = NULL;
3152 /* Walk the path and open corresponding tree objects. */
3155 struct got_tree_entry *te;
3156 struct got_object_id *tree_id;
3162 /* Ensure the correct subtree entry is selected. */
3163 slash = strchr(p, '/');
3165 te_name = strdup(p);
3167 te_name = strndup(p, slash - p);
3168 if (te_name == NULL) {
3169 err = got_error_from_errno("strndup");
3172 te = got_object_tree_find_entry(s->tree, te_name);
3174 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3179 s->first_displayed_entry = s->selected_entry = te;
3181 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3182 break; /* jump to this file's entry */
3184 slash = strchr(p, '/');
3186 subpath = strndup(path, slash - path);
3188 subpath = strdup(path);
3189 if (subpath == NULL) {
3190 err = got_error_from_errno("strdup");
3194 err = got_object_id_by_path(&tree_id, s->repo, commit,
3199 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3204 err = tree_view_visit_subtree(s, tree);
3206 got_object_tree_close(tree);
3220 static const struct got_error *
3221 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3222 struct commit_queue_entry *entry, const char *path,
3223 const char *head_ref_name, struct got_repository *repo)
3225 const struct got_error *err = NULL;
3226 struct tog_tree_view_state *s;
3227 struct tog_view *tree_view;
3229 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3230 if (tree_view == NULL)
3231 return got_error_from_errno("view_open");
3233 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3236 s = &tree_view->state.tree;
3238 *new_view = tree_view;
3240 if (got_path_is_root_dir(path))
3243 return tree_view_walk_path(s, entry->commit, path);
3246 static const struct got_error *
3247 block_signals_used_by_main_thread(void)
3252 if (sigemptyset(&sigset) == -1)
3253 return got_error_from_errno("sigemptyset");
3255 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3256 if (sigaddset(&sigset, SIGWINCH) == -1)
3257 return got_error_from_errno("sigaddset");
3258 if (sigaddset(&sigset, SIGCONT) == -1)
3259 return got_error_from_errno("sigaddset");
3260 if (sigaddset(&sigset, SIGINT) == -1)
3261 return got_error_from_errno("sigaddset");
3262 if (sigaddset(&sigset, SIGTERM) == -1)
3263 return got_error_from_errno("sigaddset");
3265 /* ncurses handles SIGTSTP */
3266 if (sigaddset(&sigset, SIGTSTP) == -1)
3267 return got_error_from_errno("sigaddset");
3269 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3271 return got_error_set_errno(errcode, "pthread_sigmask");
3277 log_thread(void *arg)
3279 const struct got_error *err = NULL;
3281 struct tog_log_thread_args *a = arg;
3285 * Sync startup with main thread such that we begin our
3286 * work once view_input() has released the mutex.
3288 errcode = pthread_mutex_lock(&tog_mutex);
3290 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3294 err = block_signals_used_by_main_thread();
3296 pthread_mutex_unlock(&tog_mutex);
3300 while (!done && !err && !tog_fatal_signal_received()) {
3301 errcode = pthread_mutex_unlock(&tog_mutex);
3303 err = got_error_set_errno(errcode,
3304 "pthread_mutex_unlock");
3307 err = queue_commits(a);
3309 if (err->code != GOT_ERR_ITER_COMPLETED)
3313 } else if (a->commits_needed > 0 && !a->load_all) {
3316 a->commits_needed--;
3318 a->commits_needed--;
3321 errcode = pthread_mutex_lock(&tog_mutex);
3323 err = got_error_set_errno(errcode,
3324 "pthread_mutex_lock");
3326 } else if (*a->quit)
3328 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3329 *a->first_displayed_entry =
3330 TAILQ_FIRST(&a->limit_commits->head);
3331 *a->selected_entry = *a->first_displayed_entry;
3332 } else if (*a->first_displayed_entry == NULL) {
3333 *a->first_displayed_entry =
3334 TAILQ_FIRST(&a->real_commits->head);
3335 *a->selected_entry = *a->first_displayed_entry;
3338 errcode = pthread_cond_signal(&a->commit_loaded);
3340 err = got_error_set_errno(errcode,
3341 "pthread_cond_signal");
3342 pthread_mutex_unlock(&tog_mutex);
3347 a->commits_needed = 0;
3349 if (a->commits_needed == 0 && !a->load_all) {
3350 errcode = pthread_cond_wait(&a->need_commits,
3353 err = got_error_set_errno(errcode,
3354 "pthread_cond_wait");
3355 pthread_mutex_unlock(&tog_mutex);
3363 a->log_complete = 1;
3364 errcode = pthread_mutex_unlock(&tog_mutex);
3366 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3369 tog_thread_error = 1;
3370 pthread_cond_signal(&a->commit_loaded);
3375 static const struct got_error *
3376 stop_log_thread(struct tog_log_view_state *s)
3378 const struct got_error *err = NULL, *thread_err = NULL;
3383 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3385 return got_error_set_errno(errcode,
3386 "pthread_cond_signal");
3387 errcode = pthread_mutex_unlock(&tog_mutex);
3389 return got_error_set_errno(errcode,
3390 "pthread_mutex_unlock");
3391 errcode = pthread_join(s->thread, (void **)&thread_err);
3393 return got_error_set_errno(errcode, "pthread_join");
3394 errcode = pthread_mutex_lock(&tog_mutex);
3396 return got_error_set_errno(errcode,
3397 "pthread_mutex_lock");
3398 s->thread = 0; //NULL;
3401 if (s->thread_args.repo) {
3402 err = got_repo_close(s->thread_args.repo);
3403 s->thread_args.repo = NULL;
3406 if (s->thread_args.pack_fds) {
3407 const struct got_error *pack_err =
3408 got_repo_pack_fds_close(s->thread_args.pack_fds);
3411 s->thread_args.pack_fds = NULL;
3414 if (s->thread_args.graph) {
3415 got_commit_graph_close(s->thread_args.graph);
3416 s->thread_args.graph = NULL;
3419 return err ? err : thread_err;
3422 static const struct got_error *
3423 close_log_view(struct tog_view *view)
3425 const struct got_error *err = NULL;
3426 struct tog_log_view_state *s = &view->state.log;
3429 err = stop_log_thread(s);
3431 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3432 if (errcode && err == NULL)
3433 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3435 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3436 if (errcode && err == NULL)
3437 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3439 free_commits(&s->limit_commits);
3440 free_commits(&s->real_commits);
3441 free(s->in_repo_path);
3442 s->in_repo_path = NULL;
3445 free(s->head_ref_name);
3446 s->head_ref_name = NULL;
3451 * We use two queues to implement the limit feature: first consists of
3452 * commits matching the current limit_regex; second is the real queue
3453 * of all known commits (real_commits). When the user starts limiting,
3454 * we swap queues such that all movement and displaying functionality
3455 * works with very slight change.
3457 static const struct got_error *
3458 limit_log_view(struct tog_view *view)
3460 struct tog_log_view_state *s = &view->state.log;
3461 struct commit_queue_entry *entry;
3462 struct tog_view *v = view;
3463 const struct got_error *err = NULL;
3467 if (view_is_hsplit_top(view))
3469 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3472 /* Get the pattern */
3473 wmove(v->window, v->nlines - 1, 0);
3474 wclrtoeol(v->window);
3475 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3476 nodelay(v->window, FALSE);
3479 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3482 nodelay(v->window, TRUE);
3486 if (*pattern == '\0') {
3488 * Safety measure for the situation where the user
3489 * resets limit without previously limiting anything.
3495 * User could have pressed Ctrl+L, which refreshed the
3496 * commit queues, it means we can't save previously
3497 * (before limit took place) displayed entries,
3498 * because they would point to already free'ed memory,
3499 * so we are forced to always select first entry of
3502 s->commits = &s->real_commits;
3503 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3504 s->selected_entry = s->first_displayed_entry;
3511 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3516 /* Clear the screen while loading limit view */
3517 s->first_displayed_entry = NULL;
3518 s->last_displayed_entry = NULL;
3519 s->selected_entry = NULL;
3520 s->commits = &s->limit_commits;
3522 /* Prepare limit queue for new search */
3523 free_commits(&s->limit_commits);
3524 s->limit_commits.ncommits = 0;
3526 /* First process commits, which are in queue already */
3527 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3530 err = match_commit(&have_match, entry->id,
3531 entry->commit, &s->limit_regex);
3536 struct commit_queue_entry *matched;
3538 matched = alloc_commit_queue_entry(entry->commit,
3540 if (matched == NULL) {
3541 err = got_error_from_errno(
3542 "alloc_commit_queue_entry");
3545 matched->commit = entry->commit;
3546 got_object_commit_retain(entry->commit);
3548 matched->idx = s->limit_commits.ncommits;
3549 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3551 s->limit_commits.ncommits++;
3555 /* Second process all the commits, until we fill the screen */
3556 if (s->limit_commits.ncommits < view->nlines - 1 &&
3557 !s->thread_args.log_complete) {
3558 s->thread_args.commits_needed +=
3559 view->nlines - s->limit_commits.ncommits - 1;
3560 err = trigger_log_thread(view, 1);
3565 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3566 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3572 static const struct got_error *
3573 search_start_log_view(struct tog_view *view)
3575 struct tog_log_view_state *s = &view->state.log;
3577 s->matched_entry = NULL;
3578 s->search_entry = NULL;
3582 static const struct got_error *
3583 search_next_log_view(struct tog_view *view)
3585 const struct got_error *err = NULL;
3586 struct tog_log_view_state *s = &view->state.log;
3587 struct commit_queue_entry *entry;
3589 /* Display progress update in log view. */
3590 show_log_view(view);
3594 if (s->search_entry) {
3596 errcode = pthread_mutex_unlock(&tog_mutex);
3598 return got_error_set_errno(errcode,
3599 "pthread_mutex_unlock");
3600 ch = wgetch(view->window);
3601 errcode = pthread_mutex_lock(&tog_mutex);
3603 return got_error_set_errno(errcode,
3604 "pthread_mutex_lock");
3605 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3606 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3609 if (view->searching == TOG_SEARCH_FORWARD)
3610 entry = TAILQ_NEXT(s->search_entry, entry);
3612 entry = TAILQ_PREV(s->search_entry,
3613 commit_queue_head, entry);
3614 } else if (s->matched_entry) {
3616 * If the user has moved the cursor after we hit a match,
3617 * the position from where we should continue searching
3618 * might have changed.
3620 if (view->searching == TOG_SEARCH_FORWARD)
3621 entry = TAILQ_NEXT(s->selected_entry, entry);
3623 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3626 entry = s->selected_entry;
3632 if (entry == NULL) {
3633 if (s->thread_args.log_complete ||
3634 view->searching == TOG_SEARCH_BACKWARD) {
3635 view->search_next_done =
3636 (s->matched_entry == NULL ?
3637 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3638 s->search_entry = NULL;
3642 * Poke the log thread for more commits and return,
3643 * allowing the main loop to make progress. Search
3644 * will resume at s->search_entry once we come back.
3646 s->thread_args.commits_needed++;
3647 return trigger_log_thread(view, 0);
3650 err = match_commit(&have_match, entry->id, entry->commit,
3655 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3656 s->matched_entry = entry;
3660 s->search_entry = entry;
3661 if (view->searching == TOG_SEARCH_FORWARD)
3662 entry = TAILQ_NEXT(entry, entry);
3664 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3667 if (s->matched_entry) {
3668 int cur = s->selected_entry->idx;
3669 while (cur < s->matched_entry->idx) {
3670 err = input_log_view(NULL, view, KEY_DOWN);
3675 while (cur > s->matched_entry->idx) {
3676 err = input_log_view(NULL, view, KEY_UP);
3683 s->search_entry = NULL;
3688 static const struct got_error *
3689 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3690 struct got_repository *repo, const char *head_ref_name,
3691 const char *in_repo_path, int log_branches)
3693 const struct got_error *err = NULL;
3694 struct tog_log_view_state *s = &view->state.log;
3695 struct got_repository *thread_repo = NULL;
3696 struct got_commit_graph *thread_graph = NULL;
3699 if (in_repo_path != s->in_repo_path) {
3700 free(s->in_repo_path);
3701 s->in_repo_path = strdup(in_repo_path);
3702 if (s->in_repo_path == NULL) {
3703 err = got_error_from_errno("strdup");
3708 /* The commit queue only contains commits being displayed. */
3709 TAILQ_INIT(&s->real_commits.head);
3710 s->real_commits.ncommits = 0;
3711 s->commits = &s->real_commits;
3713 TAILQ_INIT(&s->limit_commits.head);
3715 s->limit_commits.ncommits = 0;
3718 if (head_ref_name) {
3719 s->head_ref_name = strdup(head_ref_name);
3720 if (s->head_ref_name == NULL) {
3721 err = got_error_from_errno("strdup");
3725 s->start_id = got_object_id_dup(start_id);
3726 if (s->start_id == NULL) {
3727 err = got_error_from_errno("got_object_id_dup");
3730 s->log_branches = log_branches;
3731 s->use_committer = 1;
3733 STAILQ_INIT(&s->colors);
3734 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3735 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3736 get_color_value("TOG_COLOR_COMMIT"));
3739 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3740 get_color_value("TOG_COLOR_AUTHOR"));
3742 free_colors(&s->colors);
3745 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3746 get_color_value("TOG_COLOR_DATE"));
3748 free_colors(&s->colors);
3753 view->show = show_log_view;
3754 view->input = input_log_view;
3755 view->resize = resize_log_view;
3756 view->close = close_log_view;
3757 view->search_start = search_start_log_view;
3758 view->search_next = search_next_log_view;
3760 if (s->thread_args.pack_fds == NULL) {
3761 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3765 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3766 s->thread_args.pack_fds);
3769 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3773 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3774 s->repo, NULL, NULL);
3778 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3780 err = got_error_set_errno(errcode, "pthread_cond_init");
3783 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3785 err = got_error_set_errno(errcode, "pthread_cond_init");
3789 s->thread_args.commits_needed = view->nlines;
3790 s->thread_args.graph = thread_graph;
3791 s->thread_args.real_commits = &s->real_commits;
3792 s->thread_args.limit_commits = &s->limit_commits;
3793 s->thread_args.in_repo_path = s->in_repo_path;
3794 s->thread_args.start_id = s->start_id;
3795 s->thread_args.repo = thread_repo;
3796 s->thread_args.log_complete = 0;
3797 s->thread_args.quit = &s->quit;
3798 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3799 s->thread_args.selected_entry = &s->selected_entry;
3800 s->thread_args.searching = &view->searching;
3801 s->thread_args.search_next_done = &view->search_next_done;
3802 s->thread_args.regex = &view->regex;
3803 s->thread_args.limiting = &s->limit_view;
3804 s->thread_args.limit_regex = &s->limit_regex;
3805 s->thread_args.limit_commits = &s->limit_commits;
3808 if (view->close == NULL)
3809 close_log_view(view);
3815 static const struct got_error *
3816 show_log_view(struct tog_view *view)
3818 const struct got_error *err;
3819 struct tog_log_view_state *s = &view->state.log;
3821 if (s->thread == 0) { //NULL) {
3822 int errcode = pthread_create(&s->thread, NULL, log_thread,
3825 return got_error_set_errno(errcode, "pthread_create");
3826 if (s->thread_args.commits_needed > 0) {
3827 err = trigger_log_thread(view, 1);
3833 return draw_commits(view);
3837 log_move_cursor_up(struct tog_view *view, int page, int home)
3839 struct tog_log_view_state *s = &view->state.log;
3841 if (s->first_displayed_entry == NULL)
3843 if (s->selected_entry->idx == 0)
3846 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3848 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3850 if (!page && !home && s->selected > 0)
3853 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3859 static const struct got_error *
3860 log_move_cursor_down(struct tog_view *view, int page)
3862 struct tog_log_view_state *s = &view->state.log;
3863 const struct got_error *err = NULL;
3864 int eos = view->nlines - 2;
3866 if (s->first_displayed_entry == NULL)
3869 if (s->thread_args.log_complete &&
3870 s->selected_entry->idx >= s->commits->ncommits - 1)
3873 if (view_is_hsplit_top(view))
3874 --eos; /* border consumes the last line */
3877 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3880 err = log_scroll_down(view, 1);
3881 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3882 struct commit_queue_entry *entry;
3886 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3887 s->last_displayed_entry = entry;
3888 for (n = 0; n <= eos; n++) {
3891 s->first_displayed_entry = entry;
3892 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3895 s->selected = n - 1;
3897 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3898 s->thread_args.log_complete)
3899 s->selected += MIN(page,
3900 s->commits->ncommits - s->selected_entry->idx - 1);
3902 err = log_scroll_down(view, page);
3908 * We might necessarily overshoot in horizontal
3909 * splits; if so, select the last displayed commit.
3911 if (s->first_displayed_entry && s->last_displayed_entry) {
3912 s->selected = MIN(s->selected,
3913 s->last_displayed_entry->idx -
3914 s->first_displayed_entry->idx);
3919 if (s->thread_args.log_complete &&
3920 s->selected_entry->idx == s->commits->ncommits - 1)
3927 view_get_split(struct tog_view *view, int *y, int *x)
3932 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3933 if (view->child && view->child->resized_y)
3934 *y = view->child->resized_y;
3935 else if (view->resized_y)
3936 *y = view->resized_y;
3938 *y = view_split_begin_y(view->lines);
3939 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3940 if (view->child && view->child->resized_x)
3941 *x = view->child->resized_x;
3942 else if (view->resized_x)
3943 *x = view->resized_x;
3945 *x = view_split_begin_x(view->begin_x);
3949 /* Split view horizontally at y and offset view->state->selected line. */
3950 static const struct got_error *
3951 view_init_hsplit(struct tog_view *view, int y)
3953 const struct got_error *err = NULL;
3957 err = view_resize(view);
3961 err = offset_selection_down(view);
3966 static const struct got_error *
3967 log_goto_line(struct tog_view *view, int nlines)
3969 const struct got_error *err = NULL;
3970 struct tog_log_view_state *s = &view->state.log;
3971 int g, idx = s->selected_entry->idx;
3973 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3979 if (g >= s->first_displayed_entry->idx + 1 &&
3980 g <= s->last_displayed_entry->idx + 1 &&
3981 g - s->first_displayed_entry->idx - 1 < nlines) {
3982 s->selected = g - s->first_displayed_entry->idx - 1;
3988 err = log_move_cursor_down(view, g - idx - 1);
3989 if (!err && g > s->selected_entry->idx + 1)
3990 err = log_move_cursor_down(view,
3991 g - s->first_displayed_entry->idx - 1);
3994 } else if (idx + 1 > g)
3995 log_move_cursor_up(view, idx - g + 1, 0);
3997 if (g < nlines && s->first_displayed_entry->idx == 0)
3998 s->selected = g - 1;
4006 horizontal_scroll_input(struct tog_view *view, int ch)
4012 view->x -= MIN(view->x, 2);
4018 if (view->x + view->ncols / 2 < view->maxx)
4027 view->x = MAX(view->maxx - view->ncols / 2, 0);
4035 static const struct got_error *
4036 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4038 const struct got_error *err = NULL;
4039 struct tog_log_view_state *s = &view->state.log;
4042 if (s->thread_args.load_all) {
4043 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4044 s->thread_args.load_all = 0;
4045 else if (s->thread_args.log_complete) {
4046 err = log_move_cursor_down(view, s->commits->ncommits);
4047 s->thread_args.load_all = 0;
4053 eos = nscroll = view->nlines - 1;
4054 if (view_is_hsplit_top(view))
4058 return log_goto_line(view, eos);
4062 err = limit_log_view(view);
4073 horizontal_scroll_input(view, ch);
4080 log_move_cursor_up(view, 0, 0);
4085 log_move_cursor_up(view, 0, 1);
4095 log_move_cursor_up(view, nscroll, 0);
4102 err = log_move_cursor_down(view, 0);
4105 s->use_committer = !s->use_committer;
4106 view->action = s->use_committer ?
4107 "show committer" : "show commit author";
4112 /* We don't know yet how many commits, so we're forced to
4113 * traverse them all. */
4115 s->thread_args.load_all = 1;
4116 if (!s->thread_args.log_complete)
4117 return trigger_log_thread(view, 0);
4118 err = log_move_cursor_down(view, s->commits->ncommits);
4119 s->thread_args.load_all = 0;
4130 err = log_move_cursor_down(view, nscroll);
4133 if (s->selected > view->nlines - 2)
4134 s->selected = view->nlines - 2;
4135 if (s->selected > s->commits->ncommits - 1)
4136 s->selected = s->commits->ncommits - 1;
4138 if (s->commits->ncommits < view->nlines - 1 &&
4139 !s->thread_args.log_complete) {
4140 s->thread_args.commits_needed += (view->nlines - 1) -
4141 s->commits->ncommits;
4142 err = trigger_log_thread(view, 1);
4148 if (s->selected_entry == NULL)
4150 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4154 if (s->selected_entry == NULL)
4156 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4162 if (ch == KEY_BACKSPACE &&
4163 got_path_is_root_dir(s->in_repo_path))
4165 err = stop_log_thread(s);
4168 if (ch == KEY_BACKSPACE) {
4170 err = got_path_dirname(&parent_path, s->in_repo_path);
4173 free(s->in_repo_path);
4174 s->in_repo_path = parent_path;
4175 s->thread_args.in_repo_path = s->in_repo_path;
4176 } else if (ch == CTRL('l')) {
4177 struct got_object_id *start_id;
4178 err = got_repo_match_object_id(&start_id, NULL,
4179 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4180 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4182 if (s->head_ref_name == NULL ||
4183 err->code != GOT_ERR_NOT_REF)
4185 /* Try to cope with deleted references. */
4186 free(s->head_ref_name);
4187 s->head_ref_name = NULL;
4188 err = got_repo_match_object_id(&start_id,
4189 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4190 &tog_refs, s->repo);
4195 s->start_id = start_id;
4196 s->thread_args.start_id = s->start_id;
4198 s->log_branches = !s->log_branches;
4200 if (s->thread_args.pack_fds == NULL) {
4201 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4205 err = got_repo_open(&s->thread_args.repo,
4206 got_repo_get_path(s->repo), NULL,
4207 s->thread_args.pack_fds);
4211 err = tog_load_refs(s->repo, 0);
4214 err = got_commit_graph_open(&s->thread_args.graph,
4215 s->in_repo_path, !s->log_branches);
4218 err = got_commit_graph_iter_start(s->thread_args.graph,
4219 s->start_id, s->repo, NULL, NULL);
4222 free_commits(&s->real_commits);
4223 free_commits(&s->limit_commits);
4224 s->first_displayed_entry = NULL;
4225 s->last_displayed_entry = NULL;
4226 s->selected_entry = NULL;
4228 s->thread_args.log_complete = 0;
4230 s->thread_args.commits_needed = view->lines;
4231 s->matched_entry = NULL;
4232 s->search_entry = NULL;
4237 err = view_request_new(new_view, view, TOG_VIEW_REF);
4247 static const struct got_error *
4248 apply_unveil(const char *repo_path, const char *worktree_path)
4250 const struct got_error *error;
4253 if (unveil("gmon.out", "rwc") != 0)
4254 return got_error_from_errno2("unveil", "gmon.out");
4256 if (repo_path && unveil(repo_path, "r") != 0)
4257 return got_error_from_errno2("unveil", repo_path);
4259 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4260 return got_error_from_errno2("unveil", worktree_path);
4262 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4263 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4265 error = got_privsep_unveil_exec_helpers();
4269 if (unveil(NULL, NULL) != 0)
4270 return got_error_from_errno("unveil");
4275 static const struct got_error *
4276 init_mock_term(const char *test_script_path)
4278 const struct got_error *err = NULL;
4279 const char *screen_dump_path;
4282 if (test_script_path == NULL || *test_script_path == '\0')
4283 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4285 tog_io.f = fopen(test_script_path, "re");
4286 if (tog_io.f == NULL) {
4287 err = got_error_from_errno_fmt("fopen: %s",
4292 /* test mode, we don't want any output */
4293 tog_io.cout = fopen("/dev/null", "w+");
4294 if (tog_io.cout == NULL) {
4295 err = got_error_from_errno2("fopen", "/dev/null");
4299 in = dup(fileno(tog_io.cout));
4301 err = got_error_from_errno("dup");
4304 tog_io.cin = fdopen(in, "r");
4305 if (tog_io.cin == NULL) {
4306 err = got_error_from_errno("fdopen");
4311 screen_dump_path = getenv("TOG_SCR_DUMP");
4312 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4313 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4314 tog_io.sdump = fopen(screen_dump_path, "we");
4315 if (tog_io.sdump == NULL) {
4316 err = got_error_from_errno2("fopen", screen_dump_path);
4320 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4321 err = got_error_from_errno("fseeko");
4325 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4326 err = got_error_msg(GOT_ERR_IO,
4327 "newterm: failed to initialise curses");
4341 * Override default signal handlers before starting ncurses.
4342 * This should prevent ncurses from installing its own
4343 * broken cleanup() signal handler.
4345 signal(SIGWINCH, tog_sigwinch);
4346 signal(SIGPIPE, tog_sigpipe);
4347 signal(SIGCONT, tog_sigcont);
4348 signal(SIGINT, tog_sigint);
4349 signal(SIGTERM, tog_sigterm);
4351 if (using_mock_io) /* In test mode we use a fake terminal */
4357 halfdelay(1); /* Fast refresh while initial view is loading. */
4360 intrflush(stdscr, FALSE);
4361 keypad(stdscr, TRUE);
4363 if (getenv("TOG_COLORS") != NULL) {
4365 use_default_colors();
4371 static const struct got_error *
4372 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4373 struct got_repository *repo, struct got_worktree *worktree)
4375 const struct got_error *err = NULL;
4378 *in_repo_path = strdup("/");
4379 if (*in_repo_path == NULL)
4380 return got_error_from_errno("strdup");
4385 const char *prefix = got_worktree_get_path_prefix(worktree);
4388 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4391 if (asprintf(in_repo_path, "%s%s%s", prefix,
4392 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4394 err = got_error_from_errno("asprintf");
4395 *in_repo_path = NULL;
4399 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4404 static const struct got_error *
4405 cmd_log(int argc, char *argv[])
4407 const struct got_error *error;
4408 struct got_repository *repo = NULL;
4409 struct got_worktree *worktree = NULL;
4410 struct got_object_id *start_id = NULL;
4411 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4412 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4413 struct got_reference *ref = NULL;
4414 const char *head_ref_name = NULL;
4415 int ch, log_branches = 0;
4416 struct tog_view *view;
4417 int *pack_fds = NULL;
4419 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4425 start_commit = optarg;
4428 repo_path = realpath(optarg, NULL);
4429 if (repo_path == NULL)
4430 return got_error_from_errno2("realpath",
4445 error = got_repo_pack_fds_open(&pack_fds);
4449 if (repo_path == NULL) {
4450 cwd = getcwd(NULL, 0);
4452 return got_error_from_errno("getcwd");
4453 error = got_worktree_open(&worktree, cwd, NULL);
4454 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4458 strdup(got_worktree_get_repo_path(worktree));
4460 repo_path = strdup(cwd);
4461 if (repo_path == NULL) {
4462 error = got_error_from_errno("strdup");
4467 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4471 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4478 error = apply_unveil(got_repo_get_path(repo),
4479 worktree ? got_worktree_get_root_path(worktree) : NULL);
4483 /* already loaded by tog_log_with_path()? */
4484 if (TAILQ_EMPTY(&tog_refs)) {
4485 error = tog_load_refs(repo, 0);
4490 if (start_commit == NULL) {
4491 error = got_repo_match_object_id(&start_id, &label,
4492 worktree ? got_worktree_get_head_ref_name(worktree) :
4493 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4496 head_ref_name = label;
4498 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4502 if (keyword_idstr != NULL)
4503 start_commit = keyword_idstr;
4505 error = got_ref_open(&ref, repo, start_commit, 0);
4507 head_ref_name = got_ref_get_name(ref);
4508 else if (error->code != GOT_ERR_NOT_REF)
4510 error = got_repo_match_object_id(&start_id, NULL,
4511 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4516 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4518 error = got_error_from_errno("view_open");
4521 error = open_log_view(view, start_id, repo, head_ref_name,
4522 in_repo_path, log_branches);
4526 /* Release work tree lock. */
4527 got_worktree_close(worktree);
4530 error = view_loop(view);
4532 free(keyword_idstr);
4541 const struct got_error *close_err = got_repo_close(repo);
4546 got_worktree_close(worktree);
4548 const struct got_error *pack_err =
4549 got_repo_pack_fds_close(pack_fds);
4561 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4562 "object1 object2\n", getprogname());
4567 match_line(const char *line, regex_t *regex, size_t nmatch,
4568 regmatch_t *regmatch)
4570 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4573 static struct tog_color *
4574 match_color(struct tog_colors *colors, const char *line)
4576 struct tog_color *tc = NULL;
4578 STAILQ_FOREACH(tc, colors, entry) {
4579 if (match_line(line, &tc->regex, 0, NULL))
4586 static const struct got_error *
4587 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4588 WINDOW *window, int skipcol, regmatch_t *regmatch)
4590 const struct got_error *err = NULL;
4592 wchar_t *wline = NULL;
4593 int rme, rms, n, width, scrollx;
4594 int width0 = 0, width1 = 0, width2 = 0;
4595 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4599 rms = regmatch->rm_so;
4600 rme = regmatch->rm_eo;
4602 err = expand_tab(&exstr, line);
4606 /* Split the line into 3 segments, according to match offsets. */
4607 seg0 = strndup(exstr, rms);
4609 err = got_error_from_errno("strndup");
4612 seg1 = strndup(exstr + rms, rme - rms);
4614 err = got_error_from_errno("strndup");
4617 seg2 = strdup(exstr + rme);
4619 err = got_error_from_errno("strndup");
4623 /* draw up to matched token if we haven't scrolled past it */
4624 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4628 n = MAX(width0 - skipcol, 0);
4631 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4632 wlimit, col_tab_align, 1);
4635 waddwstr(window, &wline[scrollx]);
4645 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4649 wlen = wcslen(wline);
4651 width = wcwidth(wline[i]);
4653 /* should not happen, tabs are expanded */
4654 err = got_error(GOT_ERR_RANGE);
4657 if (width0 + w + width > skipcol)
4662 /* draw (visible part of) matched token (if scrolled into it) */
4663 if (width1 - w > 0) {
4664 wattron(window, A_STANDOUT);
4665 waddwstr(window, &wline[i]);
4666 wattroff(window, A_STANDOUT);
4667 wlimit -= (width1 - w);
4668 *wtotal += (width1 - w);
4672 if (wlimit > 0) { /* draw rest of line */
4674 if (skipcol > width0 + width1) {
4675 err = format_line(&wline, &width2, &scrollx, seg2,
4676 skipcol - (width0 + width1), wlimit,
4680 waddwstr(window, &wline[scrollx]);
4682 err = format_line(&wline, &width2, NULL, seg2, 0,
4683 wlimit, col_tab_align, 1);
4686 waddwstr(window, wline);
4700 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4703 int *eof, *first, *selected;
4705 if (view->type == TOG_VIEW_DIFF) {
4706 struct tog_diff_view_state *s = &view->state.diff;
4708 first = &s->first_displayed_line;
4712 } else if (view->type == TOG_VIEW_HELP) {
4713 struct tog_help_view_state *s = &view->state.help;
4715 first = &s->first_displayed_line;
4719 } else if (view->type == TOG_VIEW_BLAME) {
4720 struct tog_blame_view_state *s = &view->state.blame;
4722 first = &s->first_displayed_line;
4723 selected = &s->selected_line;
4729 /* Center gline in the middle of the page like vi(1). */
4730 if (*lineno < view->gline - (view->nlines - 3) / 2)
4732 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4741 *selected = view->gline <= (view->nlines - 3) / 2 ?
4742 view->gline : (view->nlines - 3) / 2 + 1;
4748 static const struct got_error *
4749 draw_file(struct tog_view *view, const char *header)
4751 struct tog_diff_view_state *s = &view->state.diff;
4752 regmatch_t *regmatch = &view->regmatch;
4753 const struct got_error *err;
4756 size_t linesize = 0;
4760 int max_lines = view->nlines;
4761 int nlines = s->nlines;
4764 s->lineno = s->first_displayed_line - 1;
4765 line_offset = s->lines[s->first_displayed_line - 1].offset;
4766 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4767 return got_error_from_errno("fseek");
4769 werase(view->window);
4771 if (view->gline > s->nlines - 1)
4772 view->gline = s->nlines - 1;
4775 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4776 1 : view->gline - (view->nlines - 3) / 2 :
4777 s->lineno + s->selected_line;
4779 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4780 return got_error_from_errno("asprintf");
4781 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4787 if (view_needs_focus_indication(view))
4788 wstandout(view->window);
4789 waddwstr(view->window, wline);
4792 while (width++ < view->ncols)
4793 waddch(view->window, ' ');
4794 if (view_needs_focus_indication(view))
4795 wstandend(view->window);
4805 while (max_lines > 0 && nprinted < max_lines) {
4806 enum got_diff_line_type linetype;
4809 linelen = getline(&line, &linesize, s->f);
4810 if (linelen == -1) {
4816 return got_ferror(s->f, GOT_ERR_IO);
4819 if (++s->lineno < s->first_displayed_line)
4821 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4823 if (s->lineno == view->hiline)
4826 /* Set view->maxx based on full line length. */
4827 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4833 view->maxx = MAX(view->maxx, width);
4837 linetype = s->lines[s->lineno].type;
4838 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4839 linetype < GOT_DIFF_LINE_CONTEXT)
4840 attr |= COLOR_PAIR(linetype);
4842 wattron(view->window, attr);
4843 if (s->first_displayed_line + nprinted == s->matched_line &&
4844 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4845 err = add_matched_line(&width, line, view->ncols, 0,
4846 view->window, view->x, regmatch);
4853 err = format_line(&wline, &width, &skip, line,
4854 view->x, view->ncols, 0, view->x ? 1 : 0);
4859 waddwstr(view->window, &wline[skip]);
4863 if (s->lineno == view->hiline) {
4864 /* highlight full gline length */
4865 while (width++ < view->ncols)
4866 waddch(view->window, ' ');
4868 if (width <= view->ncols - 1)
4869 waddch(view->window, '\n');
4872 wattroff(view->window, attr);
4873 if (++nprinted == 1)
4874 s->first_displayed_line = s->lineno;
4878 s->last_displayed_line = s->first_displayed_line +
4881 s->last_displayed_line = s->first_displayed_line;
4886 while (nprinted < view->nlines) {
4887 waddch(view->window, '\n');
4891 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4897 wstandout(view->window);
4898 waddwstr(view->window, wline);
4901 wstandend(view->window);
4908 get_datestr(time_t *time, char *datebuf)
4910 struct tm mytm, *tm;
4913 tm = gmtime_r(time, &mytm);
4916 s = asctime_r(tm, datebuf);
4919 p = strchr(s, '\n');
4925 static const struct got_error *
4926 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4927 off_t off, uint8_t type)
4929 struct got_diff_line *p;
4931 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4933 return got_error_from_errno("reallocarray");
4935 (*lines)[*nlines].offset = off;
4936 (*lines)[*nlines].type = type;
4942 static const struct got_error *
4943 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4944 struct got_diff_line *s_lines, size_t s_nlines)
4946 struct got_diff_line *p;
4950 if (fseeko(src, 0L, SEEK_SET) == -1)
4951 return got_error_from_errno("fseeko");
4954 r = fread(buf, 1, sizeof(buf), src);
4957 return got_error_from_errno("fread");
4961 if (fwrite(buf, 1, r, dst) != r)
4962 return got_ferror(dst, GOT_ERR_IO);
4965 if (s_nlines == 0 && *d_nlines == 0)
4969 * If commit info was in dst, increment line offsets
4970 * of the appended diff content, but skip s_lines[0]
4971 * because offset zero is already in *d_lines.
4973 if (*d_nlines > 0) {
4974 for (i = 1; i < s_nlines; ++i)
4975 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4983 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4985 /* d_lines is freed in close_diff_view() */
4986 return got_error_from_errno("reallocarray");
4991 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4992 *d_nlines += s_nlines;
4997 static const struct got_error *
4998 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4999 struct got_object_id *commit_id, struct got_reflist_head *refs,
5000 struct got_repository *repo, int ignore_ws, int force_text_diff,
5001 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5003 const struct got_error *err = NULL;
5004 char datebuf[26], *datestr;
5005 struct got_commit_object *commit;
5006 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5007 time_t committer_time;
5008 const char *author, *committer;
5009 char *refs_str = NULL;
5010 struct got_pathlist_entry *pe;
5014 err = build_refs_str(&refs_str, refs, commit_id, repo);
5018 err = got_object_open_as_commit(&commit, repo, commit_id);
5022 err = got_object_id_str(&id_str, commit_id);
5024 err = got_error_from_errno("got_object_id_str");
5028 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5032 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5033 refs_str ? refs_str : "", refs_str ? ")" : "");
5035 err = got_error_from_errno("fprintf");
5039 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5043 n = fprintf(outfile, "from: %s\n",
5044 got_object_commit_get_author(commit));
5046 err = got_error_from_errno("fprintf");
5050 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5054 author = got_object_commit_get_author(commit);
5055 committer = got_object_commit_get_committer(commit);
5056 if (strcmp(author, committer) != 0) {
5057 n = fprintf(outfile, "via: %s\n", committer);
5059 err = got_error_from_errno("fprintf");
5063 err = add_line_metadata(lines, nlines, outoff,
5064 GOT_DIFF_LINE_AUTHOR);
5068 committer_time = got_object_commit_get_committer_time(commit);
5069 datestr = get_datestr(&committer_time, datebuf);
5071 n = fprintf(outfile, "date: %s UTC\n", datestr);
5073 err = got_error_from_errno("fprintf");
5077 err = add_line_metadata(lines, nlines, outoff,
5078 GOT_DIFF_LINE_DATE);
5082 if (got_object_commit_get_nparents(commit) > 1) {
5083 const struct got_object_id_queue *parent_ids;
5084 struct got_object_qid *qid;
5086 parent_ids = got_object_commit_get_parent_ids(commit);
5087 STAILQ_FOREACH(qid, parent_ids, entry) {
5088 err = got_object_id_str(&id_str, &qid->id);
5091 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5093 err = got_error_from_errno("fprintf");
5097 err = add_line_metadata(lines, nlines, outoff,
5098 GOT_DIFF_LINE_META);
5106 err = got_object_commit_get_logmsg(&logmsg, commit);
5110 while ((line = strsep(&s, "\n")) != NULL) {
5111 n = fprintf(outfile, "%s\n", line);
5113 err = got_error_from_errno("fprintf");
5117 err = add_line_metadata(lines, nlines, outoff,
5118 GOT_DIFF_LINE_LOGMSG);
5123 TAILQ_FOREACH(pe, dsa->paths, entry) {
5124 struct got_diff_changed_path *cp = pe->data;
5125 int pad = dsa->max_path_len - pe->path_len + 1;
5127 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5128 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5129 dsa->rm_cols + 1, cp->rm);
5131 err = got_error_from_errno("fprintf");
5135 err = add_line_metadata(lines, nlines, outoff,
5136 GOT_DIFF_LINE_CHANGES);
5141 fputc('\n', outfile);
5143 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5147 n = fprintf(outfile,
5148 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5149 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5150 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5152 err = got_error_from_errno("fprintf");
5156 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5160 fputc('\n', outfile);
5162 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5167 got_object_commit_close(commit);
5176 static const struct got_error *
5177 create_diff(struct tog_diff_view_state *s)
5179 const struct got_error *err = NULL;
5180 FILE *f = NULL, *tmp_diff_file = NULL;
5182 struct got_diff_line *lines = NULL;
5183 struct got_pathlist_head changed_paths;
5185 TAILQ_INIT(&changed_paths);
5188 s->lines = malloc(sizeof(*s->lines));
5189 if (s->lines == NULL)
5190 return got_error_from_errno("malloc");
5195 err = got_error_from_errno("got_opentemp");
5198 tmp_diff_file = got_opentemp();
5199 if (tmp_diff_file == NULL) {
5200 err = got_error_from_errno("got_opentemp");
5203 if (s->f && fclose(s->f) == EOF) {
5204 err = got_error_from_errno("fclose");
5210 err = got_object_get_type(&obj_type, s->repo, s->id1);
5212 err = got_object_get_type(&obj_type, s->repo, s->id2);
5217 case GOT_OBJ_TYPE_BLOB:
5218 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5219 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5220 s->label1, s->label2, tog_diff_algo, s->diff_context,
5221 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5224 case GOT_OBJ_TYPE_TREE:
5225 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5226 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5227 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5228 s->force_text_diff, NULL, s->repo, s->f);
5230 case GOT_OBJ_TYPE_COMMIT: {
5231 const struct got_object_id_queue *parent_ids;
5232 struct got_object_qid *pid;
5233 struct got_commit_object *commit2;
5234 struct got_reflist_head *refs;
5236 struct got_diffstat_cb_arg dsa = {
5239 s->ignore_whitespace,
5244 lines = malloc(sizeof(*lines));
5245 if (lines == NULL) {
5246 err = got_error_from_errno("malloc");
5250 /* build diff first in tmp file then append to commit info */
5251 err = got_diff_objects_as_commits(&lines, &nlines,
5252 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5253 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5254 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5258 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5261 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5262 /* Show commit info if we're diffing to a parent/root commit. */
5263 if (s->id1 == NULL) {
5264 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5265 refs, s->repo, s->ignore_whitespace,
5266 s->force_text_diff, &dsa, s->f);
5270 parent_ids = got_object_commit_get_parent_ids(commit2);
5271 STAILQ_FOREACH(pid, parent_ids, entry) {
5272 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5273 err = write_commit_info(&s->lines,
5274 &s->nlines, s->id2, refs, s->repo,
5275 s->ignore_whitespace,
5276 s->force_text_diff, &dsa, s->f);
5283 got_object_commit_close(commit2);
5285 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5290 err = got_error(GOT_ERR_OBJ_TYPE);
5295 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5296 if (s->f && fflush(s->f) != 0 && err == NULL)
5297 err = got_error_from_errno("fflush");
5298 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5299 err = got_error_from_errno("fclose");
5304 diff_view_indicate_progress(struct tog_view *view)
5306 mvwaddstr(view->window, 0, 0, "diffing...");
5311 static const struct got_error *
5312 search_start_diff_view(struct tog_view *view)
5314 struct tog_diff_view_state *s = &view->state.diff;
5316 s->matched_line = 0;
5321 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5322 size_t *nlines, int **first, int **last, int **match, int **selected)
5324 struct tog_diff_view_state *s = &view->state.diff;
5327 *nlines = s->nlines;
5328 *line_offsets = NULL;
5329 *match = &s->matched_line;
5330 *first = &s->first_displayed_line;
5331 *last = &s->last_displayed_line;
5332 *selected = &s->selected_line;
5335 static const struct got_error *
5336 search_next_view_match(struct tog_view *view)
5338 const struct got_error *err = NULL;
5342 size_t linesize = 0;
5344 off_t *line_offsets;
5346 int *first, *last, *match, *selected;
5348 if (!view->search_setup)
5349 return got_error_msg(GOT_ERR_NOT_IMPL,
5350 "view search not supported");
5351 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5354 if (!view->searching) {
5355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5360 if (view->searching == TOG_SEARCH_FORWARD)
5361 lineno = *first + 1;
5363 lineno = *first - 1;
5365 lineno = *first - 1 + *selected;
5370 if (lineno <= 0 || lineno > nlines) {
5372 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5376 if (view->searching == TOG_SEARCH_FORWARD)
5382 offset = view->type == TOG_VIEW_DIFF ?
5383 view->state.diff.lines[lineno - 1].offset :
5384 line_offsets[lineno - 1];
5385 if (fseeko(f, offset, SEEK_SET) != 0) {
5387 return got_error_from_errno("fseeko");
5389 linelen = getline(&line, &linesize, f);
5390 if (linelen != -1) {
5392 err = expand_tab(&exstr, line);
5395 if (match_line(exstr, &view->regex, 1,
5397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5404 if (view->searching == TOG_SEARCH_FORWARD)
5419 static const struct got_error *
5420 close_diff_view(struct tog_view *view)
5422 const struct got_error *err = NULL;
5423 struct tog_diff_view_state *s = &view->state.diff;
5429 if (s->f && fclose(s->f) == EOF)
5430 err = got_error_from_errno("fclose");
5432 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5433 err = got_error_from_errno("fclose");
5435 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5436 err = got_error_from_errno("fclose");
5438 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5439 err = got_error_from_errno("close");
5441 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5442 err = got_error_from_errno("close");
5450 static const struct got_error *
5451 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5452 struct got_object_id *id2, const char *label1, const char *label2,
5453 int diff_context, int ignore_whitespace, int force_text_diff,
5454 struct tog_view *parent_view, struct got_repository *repo)
5456 const struct got_error *err;
5457 struct tog_diff_view_state *s = &view->state.diff;
5459 memset(s, 0, sizeof(*s));
5463 if (id1 != NULL && id2 != NULL) {
5466 err = got_object_get_type(&type1, repo, id1);
5469 err = got_object_get_type(&type2, repo, id2);
5473 if (type1 != type2) {
5474 err = got_error(GOT_ERR_OBJ_TYPE);
5478 s->first_displayed_line = 1;
5479 s->last_displayed_line = view->nlines;
5480 s->selected_line = 1;
5488 s->id1 = got_object_id_dup(id1);
5489 if (s->id1 == NULL) {
5490 err = got_error_from_errno("got_object_id_dup");
5496 s->id2 = got_object_id_dup(id2);
5497 if (s->id2 == NULL) {
5498 err = got_error_from_errno("got_object_id_dup");
5502 s->f1 = got_opentemp();
5503 if (s->f1 == NULL) {
5504 err = got_error_from_errno("got_opentemp");
5508 s->f2 = got_opentemp();
5509 if (s->f2 == NULL) {
5510 err = got_error_from_errno("got_opentemp");
5514 s->fd1 = got_opentempfd();
5516 err = got_error_from_errno("got_opentempfd");
5520 s->fd2 = got_opentempfd();
5522 err = got_error_from_errno("got_opentempfd");
5526 s->diff_context = diff_context;
5527 s->ignore_whitespace = ignore_whitespace;
5528 s->force_text_diff = force_text_diff;
5529 s->parent_view = parent_view;
5532 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5535 rc = init_pair(GOT_DIFF_LINE_MINUS,
5536 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5538 rc = init_pair(GOT_DIFF_LINE_PLUS,
5539 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5541 rc = init_pair(GOT_DIFF_LINE_HUNK,
5542 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5544 rc = init_pair(GOT_DIFF_LINE_META,
5545 get_color_value("TOG_COLOR_DIFF_META"), -1);
5547 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5548 get_color_value("TOG_COLOR_DIFF_META"), -1);
5550 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5551 get_color_value("TOG_COLOR_DIFF_META"), -1);
5553 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5554 get_color_value("TOG_COLOR_DIFF_META"), -1);
5556 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5557 get_color_value("TOG_COLOR_AUTHOR"), -1);
5559 rc = init_pair(GOT_DIFF_LINE_DATE,
5560 get_color_value("TOG_COLOR_DATE"), -1);
5562 err = got_error(GOT_ERR_RANGE);
5567 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5568 view_is_splitscreen(view))
5569 show_log_view(parent_view); /* draw border */
5570 diff_view_indicate_progress(view);
5572 err = create_diff(s);
5574 view->show = show_diff_view;
5575 view->input = input_diff_view;
5576 view->reset = reset_diff_view;
5577 view->close = close_diff_view;
5578 view->search_start = search_start_diff_view;
5579 view->search_setup = search_setup_diff_view;
5580 view->search_next = search_next_view_match;
5583 if (view->close == NULL)
5584 close_diff_view(view);
5590 static const struct got_error *
5591 show_diff_view(struct tog_view *view)
5593 const struct got_error *err;
5594 struct tog_diff_view_state *s = &view->state.diff;
5595 char *id_str1 = NULL, *id_str2, *header;
5596 const char *label1, *label2;
5599 err = got_object_id_str(&id_str1, s->id1);
5602 label1 = s->label1 ? s->label1 : id_str1;
5604 label1 = "/dev/null";
5606 err = got_object_id_str(&id_str2, s->id2);
5609 label2 = s->label2 ? s->label2 : id_str2;
5611 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5612 err = got_error_from_errno("asprintf");
5620 err = draw_file(view, header);
5625 static const struct got_error *
5626 set_selected_commit(struct tog_diff_view_state *s,
5627 struct commit_queue_entry *entry)
5629 const struct got_error *err;
5630 const struct got_object_id_queue *parent_ids;
5631 struct got_commit_object *selected_commit;
5632 struct got_object_qid *pid;
5635 s->id2 = got_object_id_dup(entry->id);
5637 return got_error_from_errno("got_object_id_dup");
5639 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5642 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5644 pid = STAILQ_FIRST(parent_ids);
5645 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5646 got_object_commit_close(selected_commit);
5650 static const struct got_error *
5651 reset_diff_view(struct tog_view *view)
5653 struct tog_diff_view_state *s = &view->state.diff;
5656 wclear(view->window);
5657 s->first_displayed_line = 1;
5658 s->last_displayed_line = view->nlines;
5659 s->matched_line = 0;
5660 diff_view_indicate_progress(view);
5661 return create_diff(s);
5665 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5669 i = start = s->first_displayed_line - 1;
5671 while (s->lines[i].type != type) {
5675 return; /* do nothing, requested type not in file */
5678 s->selected_line = 1;
5679 s->first_displayed_line = i;
5683 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5687 i = start = s->first_displayed_line + 1;
5689 while (s->lines[i].type != type) {
5690 if (i == s->nlines - 1)
5693 return; /* do nothing, requested type not in file */
5696 s->selected_line = 1;
5697 s->first_displayed_line = i;
5700 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5702 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5705 static const struct got_error *
5706 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5708 const struct got_error *err = NULL;
5709 struct tog_diff_view_state *s = &view->state.diff;
5710 struct tog_log_view_state *ls;
5711 struct commit_queue_entry *old_selected_entry;
5713 size_t linesize = 0;
5715 int i, nscroll = view->nlines - 1, up = 0;
5717 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5726 horizontal_scroll_input(view, ch);
5731 s->force_text_diff = !s->force_text_diff;
5732 view->action = s->force_text_diff ?
5733 "force ASCII text enabled" :
5734 "force ASCII text disabled";
5736 else if (ch == 'w') {
5737 s->ignore_whitespace = !s->ignore_whitespace;
5738 view->action = s->ignore_whitespace ?
5739 "ignore whitespace enabled" :
5740 "ignore whitespace disabled";
5742 err = reset_diff_view(view);
5746 s->first_displayed_line = 1;
5755 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5761 if (s->first_displayed_line > 1)
5762 s->first_displayed_line--;
5773 if (s->first_displayed_line == 1) {
5778 while (i++ < nscroll && s->first_displayed_line > 1)
5779 s->first_displayed_line--;
5785 s->first_displayed_line++;
5802 while (!s->eof && i++ < nscroll) {
5803 linelen = getline(&line, &linesize, s->f);
5804 s->first_displayed_line++;
5805 if (linelen == -1) {
5809 err = got_ferror(s->f, GOT_ERR_IO);
5816 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5819 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5822 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5825 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5828 if (s->diff_context > 0) {
5830 s->matched_line = 0;
5831 diff_view_indicate_progress(view);
5832 err = create_diff(s);
5833 if (s->first_displayed_line + view->nlines - 1 >
5835 s->first_displayed_line = 1;
5836 s->last_displayed_line = view->nlines;
5842 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5844 s->matched_line = 0;
5845 diff_view_indicate_progress(view);
5846 err = create_diff(s);
5858 if (s->parent_view == NULL) {
5862 s->parent_view->count = view->count;
5864 if (s->parent_view->type == TOG_VIEW_LOG) {
5865 ls = &s->parent_view->state.log;
5866 old_selected_entry = ls->selected_entry;
5868 err = input_log_view(NULL, s->parent_view,
5869 up ? KEY_UP : KEY_DOWN);
5872 view->count = s->parent_view->count;
5874 if (old_selected_entry == ls->selected_entry)
5877 err = set_selected_commit(s, ls->selected_entry);
5880 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5881 struct tog_blame_view_state *bs;
5882 struct got_object_id *id, *prev_id;
5884 bs = &s->parent_view->state.blame;
5885 prev_id = get_annotation_for_line(bs->blame.lines,
5886 bs->blame.nlines, bs->last_diffed_line);
5888 err = input_blame_view(&view, s->parent_view,
5889 up ? KEY_UP : KEY_DOWN);
5892 view->count = s->parent_view->count;
5894 if (prev_id == NULL)
5896 id = get_selected_commit_id(bs->blame.lines,
5897 bs->blame.nlines, bs->first_displayed_line,
5902 if (!got_object_id_cmp(prev_id, id))
5905 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5909 s->first_displayed_line = 1;
5910 s->last_displayed_line = view->nlines;
5911 s->matched_line = 0;
5914 diff_view_indicate_progress(view);
5915 err = create_diff(s);
5925 static const struct got_error *
5926 cmd_diff(int argc, char *argv[])
5928 const struct got_error *error;
5929 struct got_repository *repo = NULL;
5930 struct got_worktree *worktree = NULL;
5931 struct got_object_id *id1 = NULL, *id2 = NULL;
5932 char *repo_path = NULL, *cwd = NULL;
5933 char *id_str1 = NULL, *id_str2 = NULL;
5934 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
5935 char *label1 = NULL, *label2 = NULL;
5936 int diff_context = 3, ignore_whitespace = 0;
5937 int ch, force_text_diff = 0;
5939 struct tog_view *view;
5940 int *pack_fds = NULL;
5942 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5945 force_text_diff = 1;
5948 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5951 errx(1, "number of context lines is %s: %s",
5955 repo_path = realpath(optarg, NULL);
5956 if (repo_path == NULL)
5957 return got_error_from_errno2("realpath",
5959 got_path_strip_trailing_slashes(repo_path);
5962 ignore_whitespace = 1;
5974 usage_diff(); /* TODO show local worktree changes */
5975 } else if (argc == 2) {
5981 error = got_repo_pack_fds_open(&pack_fds);
5985 if (repo_path == NULL) {
5986 cwd = getcwd(NULL, 0);
5988 return got_error_from_errno("getcwd");
5989 error = got_worktree_open(&worktree, cwd, NULL);
5990 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5994 strdup(got_worktree_get_repo_path(worktree));
5996 repo_path = strdup(cwd);
5997 if (repo_path == NULL) {
5998 error = got_error_from_errno("strdup");
6003 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6009 error = apply_unveil(got_repo_get_path(repo), NULL);
6013 error = tog_load_refs(repo, 0);
6017 if (id_str1 != NULL) {
6018 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6022 if (keyword_idstr1 != NULL)
6023 id_str1 = keyword_idstr1;
6025 if (id_str2 != NULL) {
6026 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6030 if (keyword_idstr2 != NULL)
6031 id_str2 = keyword_idstr2;
6034 error = got_repo_match_object_id(&id1, &label1, id_str1,
6035 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6039 error = got_repo_match_object_id(&id2, &label2, id_str2,
6040 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6044 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6046 error = got_error_from_errno("view_open");
6049 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6050 ignore_whitespace, force_text_diff, NULL, repo);
6053 error = view_loop(view);
6055 free(keyword_idstr1);
6056 free(keyword_idstr2);
6062 const struct got_error *close_err = got_repo_close(repo);
6067 got_worktree_close(worktree);
6069 const struct got_error *pack_err =
6070 got_repo_pack_fds_close(pack_fds);
6083 "usage: %s blame [-c commit] [-r repository-path] path\n",
6088 struct tog_blame_line {
6090 struct got_object_id *id;
6093 static const struct got_error *
6094 draw_blame(struct tog_view *view)
6096 struct tog_blame_view_state *s = &view->state.blame;
6097 struct tog_blame *blame = &s->blame;
6098 regmatch_t *regmatch = &view->regmatch;
6099 const struct got_error *err;
6100 int lineno = 0, nprinted = 0;
6102 size_t linesize = 0;
6106 struct tog_blame_line *blame_line;
6107 struct got_object_id *prev_id = NULL;
6109 struct tog_color *tc;
6111 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6116 werase(view->window);
6118 if (asprintf(&line, "commit %s", id_str) == -1) {
6119 err = got_error_from_errno("asprintf");
6124 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6129 if (view_needs_focus_indication(view))
6130 wstandout(view->window);
6131 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6133 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6134 waddwstr(view->window, wline);
6135 while (width++ < view->ncols)
6136 waddch(view->window, ' ');
6138 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6139 if (view_needs_focus_indication(view))
6140 wstandend(view->window);
6144 if (view->gline > blame->nlines)
6145 view->gline = blame->nlines;
6147 if (tog_io.wait_for_ui) {
6148 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6151 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6153 return got_error_set_errno(rc, "pthread_cond_wait");
6154 tog_io.wait_for_ui = 0;
6157 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6158 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6159 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6161 return got_error_from_errno("asprintf");
6164 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6169 waddwstr(view->window, wline);
6172 if (width < view->ncols - 1)
6173 waddch(view->window, '\n');
6177 while (nprinted < view->nlines - 2) {
6178 linelen = getline(&line, &linesize, blame->f);
6179 if (linelen == -1) {
6180 if (feof(blame->f)) {
6185 return got_ferror(blame->f, GOT_ERR_IO);
6187 if (++lineno < s->first_displayed_line)
6189 if (view->gline && !gotoline(view, &lineno, &nprinted))
6192 /* Set view->maxx based on full line length. */
6193 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6200 view->maxx = MAX(view->maxx, width);
6202 if (nprinted == s->selected_line - 1)
6203 wstandout(view->window);
6205 if (blame->nlines > 0) {
6206 blame_line = &blame->lines[lineno - 1];
6207 if (blame_line->annotated && prev_id &&
6208 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6209 !(nprinted == s->selected_line - 1)) {
6210 waddstr(view->window, " ");
6211 } else if (blame_line->annotated) {
6213 err = got_object_id_str(&id_str,
6219 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6221 wattr_on(view->window,
6222 COLOR_PAIR(tc->colorpair), NULL);
6223 wprintw(view->window, "%.8s", id_str);
6225 wattr_off(view->window,
6226 COLOR_PAIR(tc->colorpair), NULL);
6228 prev_id = blame_line->id;
6230 waddstr(view->window, "........");
6234 waddstr(view->window, "........");
6238 if (nprinted == s->selected_line - 1)
6239 wstandend(view->window);
6240 waddstr(view->window, " ");
6242 if (view->ncols <= 9) {
6244 } else if (s->first_displayed_line + nprinted ==
6246 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6247 err = add_matched_line(&width, line, view->ncols - 9, 9,
6248 view->window, view->x, regmatch);
6256 err = format_line(&wline, &width, &skip, line,
6257 view->x, view->ncols - 9, 9, 1);
6262 waddwstr(view->window, &wline[skip]);
6268 if (width <= view->ncols - 1)
6269 waddch(view->window, '\n');
6270 if (++nprinted == 1)
6271 s->first_displayed_line = lineno;
6274 s->last_displayed_line = lineno;
6281 static const struct got_error *
6282 blame_cb(void *arg, int nlines, int lineno,
6283 struct got_commit_object *commit, struct got_object_id *id)
6285 const struct got_error *err = NULL;
6286 struct tog_blame_cb_args *a = arg;
6287 struct tog_blame_line *line;
6290 if (nlines != a->nlines ||
6291 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6292 return got_error(GOT_ERR_RANGE);
6294 errcode = pthread_mutex_lock(&tog_mutex);
6296 return got_error_set_errno(errcode, "pthread_mutex_lock");
6298 if (*a->quit) { /* user has quit the blame view */
6299 err = got_error(GOT_ERR_ITER_COMPLETED);
6304 goto done; /* no change in this commit */
6306 line = &a->lines[lineno - 1];
6307 if (line->annotated)
6310 line->id = got_object_id_dup(id);
6311 if (line->id == NULL) {
6312 err = got_error_from_errno("got_object_id_dup");
6315 line->annotated = 1;
6317 errcode = pthread_mutex_unlock(&tog_mutex);
6319 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6324 blame_thread(void *arg)
6326 const struct got_error *err, *close_err;
6327 struct tog_blame_thread_args *ta = arg;
6328 struct tog_blame_cb_args *a = ta->cb_args;
6329 int errcode, fd1 = -1, fd2 = -1;
6330 FILE *f1 = NULL, *f2 = NULL;
6332 fd1 = got_opentempfd();
6334 return (void *)got_error_from_errno("got_opentempfd");
6336 fd2 = got_opentempfd();
6338 err = got_error_from_errno("got_opentempfd");
6342 f1 = got_opentemp();
6344 err = (void *)got_error_from_errno("got_opentemp");
6347 f2 = got_opentemp();
6349 err = (void *)got_error_from_errno("got_opentemp");
6353 err = block_signals_used_by_main_thread();
6357 err = got_blame(ta->path, a->commit_id, ta->repo,
6358 tog_diff_algo, blame_cb, ta->cb_args,
6359 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6360 if (err && err->code == GOT_ERR_CANCELLED)
6363 errcode = pthread_mutex_lock(&tog_mutex);
6365 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6369 close_err = got_repo_close(ta->repo);
6375 if (tog_io.wait_for_ui) {
6376 errcode = pthread_cond_signal(&ta->blame_complete);
6377 if (errcode && err == NULL)
6378 err = got_error_set_errno(errcode,
6379 "pthread_cond_signal");
6382 errcode = pthread_mutex_unlock(&tog_mutex);
6383 if (errcode && err == NULL)
6384 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6387 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6388 err = got_error_from_errno("close");
6389 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6390 err = got_error_from_errno("close");
6391 if (f1 && fclose(f1) == EOF && err == NULL)
6392 err = got_error_from_errno("fclose");
6393 if (f2 && fclose(f2) == EOF && err == NULL)
6394 err = got_error_from_errno("fclose");
6399 static struct got_object_id *
6400 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6401 int first_displayed_line, int selected_line)
6403 struct tog_blame_line *line;
6408 line = &lines[first_displayed_line - 1 + selected_line - 1];
6409 if (!line->annotated)
6415 static struct got_object_id *
6416 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6419 struct tog_blame_line *line;
6421 if (nlines <= 0 || lineno >= nlines)
6424 line = &lines[lineno - 1];
6425 if (!line->annotated)
6431 static const struct got_error *
6432 stop_blame(struct tog_blame *blame)
6434 const struct got_error *err = NULL;
6437 if (blame->thread) {
6439 errcode = pthread_mutex_unlock(&tog_mutex);
6441 return got_error_set_errno(errcode,
6442 "pthread_mutex_unlock");
6443 errcode = pthread_join(blame->thread, (void **)&err);
6445 return got_error_set_errno(errcode, "pthread_join");
6446 errcode = pthread_mutex_lock(&tog_mutex);
6448 return got_error_set_errno(errcode,
6449 "pthread_mutex_lock");
6450 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6452 blame->thread = 0; //NULL;
6454 if (blame->thread_args.repo) {
6455 const struct got_error *close_err;
6456 close_err = got_repo_close(blame->thread_args.repo);
6459 blame->thread_args.repo = NULL;
6462 if (fclose(blame->f) == EOF && err == NULL)
6463 err = got_error_from_errno("fclose");
6467 for (i = 0; i < blame->nlines; i++)
6468 free(blame->lines[i].id);
6470 blame->lines = NULL;
6472 free(blame->cb_args.commit_id);
6473 blame->cb_args.commit_id = NULL;
6474 if (blame->pack_fds) {
6475 const struct got_error *pack_err =
6476 got_repo_pack_fds_close(blame->pack_fds);
6479 blame->pack_fds = NULL;
6484 static const struct got_error *
6485 cancel_blame_view(void *arg)
6487 const struct got_error *err = NULL;
6491 errcode = pthread_mutex_lock(&tog_mutex);
6493 return got_error_set_errno(errcode,
6494 "pthread_mutex_unlock");
6497 err = got_error(GOT_ERR_CANCELLED);
6499 errcode = pthread_mutex_unlock(&tog_mutex);
6501 return got_error_set_errno(errcode,
6502 "pthread_mutex_lock");
6507 static const struct got_error *
6508 run_blame(struct tog_view *view)
6510 struct tog_blame_view_state *s = &view->state.blame;
6511 struct tog_blame *blame = &s->blame;
6512 const struct got_error *err = NULL;
6513 struct got_commit_object *commit = NULL;
6514 struct got_blob_object *blob = NULL;
6515 struct got_repository *thread_repo = NULL;
6516 struct got_object_id *obj_id = NULL;
6517 int obj_type, fd = -1;
6518 int *pack_fds = NULL;
6520 err = got_object_open_as_commit(&commit, s->repo,
6521 &s->blamed_commit->id);
6525 fd = got_opentempfd();
6527 err = got_error_from_errno("got_opentempfd");
6531 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6535 err = got_object_get_type(&obj_type, s->repo, obj_id);
6539 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6540 err = got_error(GOT_ERR_OBJ_TYPE);
6544 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6547 blame->f = got_opentemp();
6548 if (blame->f == NULL) {
6549 err = got_error_from_errno("got_opentemp");
6552 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6553 &blame->line_offsets, blame->f, blob);
6556 if (blame->nlines == 0) {
6557 s->blame_complete = 1;
6561 /* Don't include \n at EOF in the blame line count. */
6562 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6565 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6566 if (blame->lines == NULL) {
6567 err = got_error_from_errno("calloc");
6571 err = got_repo_pack_fds_open(&pack_fds);
6574 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6579 blame->pack_fds = pack_fds;
6580 blame->cb_args.view = view;
6581 blame->cb_args.lines = blame->lines;
6582 blame->cb_args.nlines = blame->nlines;
6583 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6584 if (blame->cb_args.commit_id == NULL) {
6585 err = got_error_from_errno("got_object_id_dup");
6588 blame->cb_args.quit = &s->done;
6590 blame->thread_args.path = s->path;
6591 blame->thread_args.repo = thread_repo;
6592 blame->thread_args.cb_args = &blame->cb_args;
6593 blame->thread_args.complete = &s->blame_complete;
6594 blame->thread_args.cancel_cb = cancel_blame_view;
6595 blame->thread_args.cancel_arg = &s->done;
6596 s->blame_complete = 0;
6598 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6599 s->first_displayed_line = 1;
6600 s->last_displayed_line = view->nlines;
6601 s->selected_line = 1;
6603 s->matched_line = 0;
6607 got_object_commit_close(commit);
6608 if (fd != -1 && close(fd) == -1 && err == NULL)
6609 err = got_error_from_errno("close");
6611 got_object_blob_close(blob);
6618 static const struct got_error *
6619 open_blame_view(struct tog_view *view, char *path,
6620 struct got_object_id *commit_id, struct got_repository *repo)
6622 const struct got_error *err = NULL;
6623 struct tog_blame_view_state *s = &view->state.blame;
6625 STAILQ_INIT(&s->blamed_commits);
6627 s->path = strdup(path);
6628 if (s->path == NULL)
6629 return got_error_from_errno("strdup");
6631 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6637 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6638 s->first_displayed_line = 1;
6639 s->last_displayed_line = view->nlines;
6640 s->selected_line = 1;
6641 s->blame_complete = 0;
6643 s->commit_id = commit_id;
6644 memset(&s->blame, 0, sizeof(s->blame));
6646 STAILQ_INIT(&s->colors);
6647 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6648 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6649 get_color_value("TOG_COLOR_COMMIT"));
6654 view->show = show_blame_view;
6655 view->input = input_blame_view;
6656 view->reset = reset_blame_view;
6657 view->close = close_blame_view;
6658 view->search_start = search_start_blame_view;
6659 view->search_setup = search_setup_blame_view;
6660 view->search_next = search_next_view_match;
6662 if (using_mock_io) {
6663 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6666 rc = pthread_cond_init(&bta->blame_complete, NULL);
6668 return got_error_set_errno(rc, "pthread_cond_init");
6671 return run_blame(view);
6674 static const struct got_error *
6675 close_blame_view(struct tog_view *view)
6677 const struct got_error *err = NULL;
6678 struct tog_blame_view_state *s = &view->state.blame;
6680 if (s->blame.thread)
6681 err = stop_blame(&s->blame);
6683 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6684 struct got_object_qid *blamed_commit;
6685 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6686 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6687 got_object_qid_free(blamed_commit);
6690 if (using_mock_io) {
6691 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6694 rc = pthread_cond_destroy(&bta->blame_complete);
6695 if (rc && err == NULL)
6696 err = got_error_set_errno(rc, "pthread_cond_destroy");
6700 free_colors(&s->colors);
6704 static const struct got_error *
6705 search_start_blame_view(struct tog_view *view)
6707 struct tog_blame_view_state *s = &view->state.blame;
6709 s->matched_line = 0;
6714 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6715 size_t *nlines, int **first, int **last, int **match, int **selected)
6717 struct tog_blame_view_state *s = &view->state.blame;
6720 *nlines = s->blame.nlines;
6721 *line_offsets = s->blame.line_offsets;
6722 *match = &s->matched_line;
6723 *first = &s->first_displayed_line;
6724 *last = &s->last_displayed_line;
6725 *selected = &s->selected_line;
6728 static const struct got_error *
6729 show_blame_view(struct tog_view *view)
6731 const struct got_error *err = NULL;
6732 struct tog_blame_view_state *s = &view->state.blame;
6735 if (s->blame.thread == 0 && !s->blame_complete) {
6736 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6737 &s->blame.thread_args);
6739 return got_error_set_errno(errcode, "pthread_create");
6742 halfdelay(1); /* fast refresh while annotating */
6745 if (s->blame_complete && !using_mock_io)
6746 halfdelay(10); /* disable fast refresh */
6748 err = draw_blame(view);
6754 static const struct got_error *
6755 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6756 struct got_repository *repo, struct got_object_id *id)
6758 struct tog_view *log_view;
6759 const struct got_error *err = NULL;
6763 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6764 if (log_view == NULL)
6765 return got_error_from_errno("view_open");
6767 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6769 view_close(log_view);
6771 *new_view = log_view;
6776 static const struct got_error *
6777 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6779 const struct got_error *err = NULL, *thread_err = NULL;
6780 struct tog_view *diff_view;
6781 struct tog_blame_view_state *s = &view->state.blame;
6782 int eos, nscroll, begin_y = 0, begin_x = 0;
6784 eos = nscroll = view->nlines - 2;
6785 if (view_is_hsplit_top(view))
6795 horizontal_scroll_input(view, ch);
6802 s->selected_line = 1;
6803 s->first_displayed_line = 1;
6808 if (s->blame.nlines < eos) {
6809 s->selected_line = s->blame.nlines;
6810 s->first_displayed_line = 1;
6812 s->selected_line = eos;
6813 s->first_displayed_line = s->blame.nlines - (eos - 1);
6820 if (s->selected_line > 1)
6822 else if (s->selected_line == 1 &&
6823 s->first_displayed_line > 1)
6824 s->first_displayed_line--;
6835 if (s->first_displayed_line == 1) {
6836 if (view->count > 1)
6838 s->selected_line = MAX(1, s->selected_line - nscroll);
6842 if (s->first_displayed_line > nscroll)
6843 s->first_displayed_line -= nscroll;
6845 s->first_displayed_line = 1;
6850 if (s->selected_line < eos && s->first_displayed_line +
6851 s->selected_line <= s->blame.nlines)
6853 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6854 s->first_displayed_line++;
6860 struct got_object_id *id = NULL;
6863 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6864 s->first_displayed_line, s->selected_line);
6868 struct got_commit_object *commit, *pcommit;
6869 struct got_object_qid *pid;
6870 struct got_object_id *blob_id = NULL;
6872 err = got_object_open_as_commit(&commit,
6877 got_object_commit_get_parent_ids(commit));
6879 got_object_commit_close(commit);
6882 /* Check if path history ends here. */
6883 err = got_object_open_as_commit(&pcommit,
6887 err = got_object_id_by_path(&blob_id, s->repo,
6889 got_object_commit_close(pcommit);
6891 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6893 got_object_commit_close(commit);
6896 err = got_object_get_type(&obj_type, s->repo,
6899 /* Can't blame non-blob type objects. */
6900 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6901 got_object_commit_close(commit);
6904 err = got_object_qid_alloc(&s->blamed_commit,
6906 got_object_commit_close(commit);
6908 if (got_object_id_cmp(id,
6909 &s->blamed_commit->id) == 0)
6911 err = got_object_qid_alloc(&s->blamed_commit,
6917 thread_err = stop_blame(&s->blame);
6921 STAILQ_INSERT_HEAD(&s->blamed_commits,
6922 s->blamed_commit, entry);
6923 err = run_blame(view);
6929 struct got_object_qid *first;
6932 first = STAILQ_FIRST(&s->blamed_commits);
6933 if (!got_object_id_cmp(&first->id, s->commit_id))
6936 thread_err = stop_blame(&s->blame);
6940 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6941 got_object_qid_free(s->blamed_commit);
6943 STAILQ_FIRST(&s->blamed_commits);
6944 err = run_blame(view);
6951 s->id_to_log = get_selected_commit_id(s->blame.lines,
6952 s->blame.nlines, s->first_displayed_line, s->selected_line);
6954 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6958 struct got_object_id *id = NULL;
6959 struct got_object_qid *pid;
6960 struct got_commit_object *commit = NULL;
6963 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6964 s->first_displayed_line, s->selected_line);
6967 err = got_object_open_as_commit(&commit, s->repo, id);
6970 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6972 /* traversed from diff view, release diff resources */
6973 err = close_diff_view(*new_view);
6976 diff_view = *new_view;
6978 if (view_is_parent_view(view))
6979 view_get_split(view, &begin_y, &begin_x);
6981 diff_view = view_open(0, 0, begin_y, begin_x,
6983 if (diff_view == NULL) {
6984 got_object_commit_close(commit);
6985 err = got_error_from_errno("view_open");
6989 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6990 id, NULL, NULL, 3, 0, 0, view, s->repo);
6991 got_object_commit_close(commit);
6994 s->last_diffed_line = s->first_displayed_line - 1 +
6997 break; /* still open from active diff view */
6998 if (view_is_parent_view(view) &&
6999 view->mode == TOG_VIEW_SPLIT_HRZN) {
7000 err = view_init_hsplit(view, begin_y);
7006 diff_view->focussed = 1;
7007 diff_view->mode = view->mode;
7008 diff_view->nlines = view->lines - begin_y;
7009 if (view_is_parent_view(view)) {
7010 view_transfer_size(diff_view, view);
7011 err = view_close_child(view);
7014 err = view_set_child(view, diff_view);
7017 view->focus_child = 1;
7019 *new_view = diff_view;
7032 if (s->last_displayed_line >= s->blame.nlines &&
7033 s->selected_line >= MIN(s->blame.nlines,
7034 view->nlines - 2)) {
7038 if (s->last_displayed_line >= s->blame.nlines &&
7039 s->selected_line < view->nlines - 2) {
7041 MIN(nscroll, s->last_displayed_line -
7042 s->first_displayed_line - s->selected_line + 1);
7044 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7045 s->first_displayed_line += nscroll;
7047 s->first_displayed_line =
7048 s->blame.nlines - (view->nlines - 3);
7051 if (s->selected_line > view->nlines - 2) {
7052 s->selected_line = MIN(s->blame.nlines,
7060 return thread_err ? thread_err : err;
7063 static const struct got_error *
7064 reset_blame_view(struct tog_view *view)
7066 const struct got_error *err;
7067 struct tog_blame_view_state *s = &view->state.blame;
7071 err = stop_blame(&s->blame);
7075 return run_blame(view);
7078 static const struct got_error *
7079 cmd_blame(int argc, char *argv[])
7081 const struct got_error *error;
7082 struct got_repository *repo = NULL;
7083 struct got_worktree *worktree = NULL;
7084 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7085 char *link_target = NULL;
7086 struct got_object_id *commit_id = NULL;
7087 struct got_commit_object *commit = NULL;
7088 char *keyword_idstr = NULL, *commit_id_str = NULL;
7090 struct tog_view *view = NULL;
7091 int *pack_fds = NULL;
7093 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7096 commit_id_str = optarg;
7099 repo_path = realpath(optarg, NULL);
7100 if (repo_path == NULL)
7101 return got_error_from_errno2("realpath",
7116 error = got_repo_pack_fds_open(&pack_fds);
7120 if (repo_path == NULL) {
7121 cwd = getcwd(NULL, 0);
7123 return got_error_from_errno("getcwd");
7124 error = got_worktree_open(&worktree, cwd, NULL);
7125 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7129 strdup(got_worktree_get_repo_path(worktree));
7131 repo_path = strdup(cwd);
7132 if (repo_path == NULL) {
7133 error = got_error_from_errno("strdup");
7138 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7142 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7149 error = apply_unveil(got_repo_get_path(repo), NULL);
7153 error = tog_load_refs(repo, 0);
7157 if (commit_id_str == NULL) {
7158 struct got_reference *head_ref;
7159 error = got_ref_open(&head_ref, repo, worktree ?
7160 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7163 error = got_ref_resolve(&commit_id, repo, head_ref);
7164 got_ref_close(head_ref);
7166 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7170 if (keyword_idstr != NULL)
7171 commit_id_str = keyword_idstr;
7173 error = got_repo_match_object_id(&commit_id, NULL,
7174 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7179 error = got_object_open_as_commit(&commit, repo, commit_id);
7183 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7188 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7190 error = got_error_from_errno("view_open");
7193 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7195 if (error != NULL) {
7196 if (view->close == NULL)
7197 close_blame_view(view);
7202 /* Release work tree lock. */
7203 got_worktree_close(worktree);
7206 error = view_loop(view);
7213 free(keyword_idstr);
7215 got_object_commit_close(commit);
7217 got_worktree_close(worktree);
7219 const struct got_error *close_err = got_repo_close(repo);
7224 const struct got_error *pack_err =
7225 got_repo_pack_fds_close(pack_fds);
7233 static const struct got_error *
7234 draw_tree_entries(struct tog_view *view, const char *parent_path)
7236 struct tog_tree_view_state *s = &view->state.tree;
7237 const struct got_error *err = NULL;
7238 struct got_tree_entry *te;
7241 struct tog_color *tc;
7242 int width, n, nentries, scrollx, i = 1;
7243 int limit = view->nlines;
7246 if (view_is_hsplit_top(view))
7247 --limit; /* border */
7249 werase(view->window);
7254 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7258 if (view_needs_focus_indication(view))
7259 wstandout(view->window);
7260 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7262 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7263 waddwstr(view->window, wline);
7266 while (width++ < view->ncols)
7267 waddch(view->window, ' ');
7269 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7270 if (view_needs_focus_indication(view))
7271 wstandend(view->window);
7276 if (s->first_displayed_entry) {
7277 i += got_tree_entry_get_index(s->first_displayed_entry);
7278 if (s->tree != s->root)
7279 ++i; /* account for ".." entry */
7281 nentries = got_object_tree_get_nentries(s->tree);
7282 if (asprintf(&index, "[%d/%d] %s",
7283 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7284 return got_error_from_errno("asprintf");
7285 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7289 waddwstr(view->window, wline);
7292 if (width < view->ncols - 1)
7293 waddch(view->window, '\n');
7296 waddch(view->window, '\n');
7300 if (s->first_displayed_entry == NULL) {
7301 te = got_object_tree_get_first_entry(s->tree);
7302 if (s->selected == 0) {
7304 wstandout(view->window);
7305 s->selected_entry = NULL;
7307 waddstr(view->window, " ..\n"); /* parent directory */
7308 if (s->selected == 0 && view->focussed)
7309 wstandend(view->window);
7316 te = s->first_displayed_entry;
7320 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7321 char *line = NULL, *id_str = NULL, *link_target = NULL;
7322 const char *modestr = "";
7325 te = got_object_tree_get_entry(s->tree, i);
7326 mode = got_tree_entry_get_mode(te);
7329 err = got_object_id_str(&id_str,
7330 got_tree_entry_get_id(te));
7332 return got_error_from_errno(
7333 "got_object_id_str");
7335 if (got_object_tree_entry_is_submodule(te))
7337 else if (S_ISLNK(mode)) {
7340 err = got_tree_entry_get_symlink_target(&link_target,
7346 for (i = 0; link_target[i] != '\0'; i++) {
7347 if (!isprint((unsigned char)link_target[i]))
7348 link_target[i] = '?';
7352 else if (S_ISDIR(mode))
7354 else if (mode & S_IXUSR)
7356 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7357 got_tree_entry_get_name(te), modestr,
7358 link_target ? " -> ": "",
7359 link_target ? link_target : "") == -1) {
7362 return got_error_from_errno("asprintf");
7367 /* use full line width to determine view->maxx */
7368 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7373 view->maxx = MAX(view->maxx, width);
7377 err = format_line(&wline, &width, &scrollx, line, view->x,
7383 if (n == s->selected) {
7385 wstandout(view->window);
7386 s->selected_entry = te;
7388 tc = match_color(&s->colors, line);
7390 wattr_on(view->window,
7391 COLOR_PAIR(tc->colorpair), NULL);
7392 waddwstr(view->window, &wline[scrollx]);
7394 wattr_off(view->window,
7395 COLOR_PAIR(tc->colorpair), NULL);
7396 if (width < view->ncols)
7397 waddch(view->window, '\n');
7398 if (n == s->selected && view->focussed)
7399 wstandend(view->window);
7405 s->last_displayed_entry = te;
7414 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7416 struct got_tree_entry *te;
7417 int isroot = s->tree == s->root;
7420 if (s->first_displayed_entry == NULL)
7423 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7424 while (i++ < maxscroll) {
7427 s->first_displayed_entry = NULL;
7430 s->first_displayed_entry = te;
7431 te = got_tree_entry_get_prev(s->tree, te);
7435 static const struct got_error *
7436 tree_scroll_down(struct tog_view *view, int maxscroll)
7438 struct tog_tree_view_state *s = &view->state.tree;
7439 struct got_tree_entry *next, *last;
7442 if (s->first_displayed_entry)
7443 next = got_tree_entry_get_next(s->tree,
7444 s->first_displayed_entry);
7446 next = got_object_tree_get_first_entry(s->tree);
7448 last = s->last_displayed_entry;
7449 while (next && n++ < maxscroll) {
7451 s->last_displayed_entry = last;
7452 last = got_tree_entry_get_next(s->tree, last);
7454 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7455 s->first_displayed_entry = next;
7456 next = got_tree_entry_get_next(s->tree, next);
7463 static const struct got_error *
7464 tree_entry_path(char **path, struct tog_parent_trees *parents,
7465 struct got_tree_entry *te)
7467 const struct got_error *err = NULL;
7468 struct tog_parent_tree *pt;
7469 size_t len = 2; /* for leading slash and NUL */
7471 TAILQ_FOREACH(pt, parents, entry)
7472 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7475 len += strlen(got_tree_entry_get_name(te));
7477 *path = calloc(1, len);
7479 return got_error_from_errno("calloc");
7482 pt = TAILQ_LAST(parents, tog_parent_trees);
7484 const char *name = got_tree_entry_get_name(pt->selected_entry);
7485 if (strlcat(*path, name, len) >= len) {
7486 err = got_error(GOT_ERR_NO_SPACE);
7489 if (strlcat(*path, "/", len) >= len) {
7490 err = got_error(GOT_ERR_NO_SPACE);
7493 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7496 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7497 err = got_error(GOT_ERR_NO_SPACE);
7509 static const struct got_error *
7510 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7511 struct got_tree_entry *te, struct tog_parent_trees *parents,
7512 struct got_object_id *commit_id, struct got_repository *repo)
7514 const struct got_error *err = NULL;
7516 struct tog_view *blame_view;
7520 err = tree_entry_path(&path, parents, te);
7524 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7525 if (blame_view == NULL) {
7526 err = got_error_from_errno("view_open");
7530 err = open_blame_view(blame_view, path, commit_id, repo);
7532 if (err->code == GOT_ERR_CANCELLED)
7534 view_close(blame_view);
7536 *new_view = blame_view;
7542 static const struct got_error *
7543 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7544 struct tog_tree_view_state *s)
7546 struct tog_view *log_view;
7547 const struct got_error *err = NULL;
7552 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7553 if (log_view == NULL)
7554 return got_error_from_errno("view_open");
7556 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7560 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7563 view_close(log_view);
7565 *new_view = log_view;
7570 static const struct got_error *
7571 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7572 const char *head_ref_name, struct got_repository *repo)
7574 const struct got_error *err = NULL;
7575 char *commit_id_str = NULL;
7576 struct tog_tree_view_state *s = &view->state.tree;
7577 struct got_commit_object *commit = NULL;
7579 TAILQ_INIT(&s->parents);
7580 STAILQ_INIT(&s->colors);
7582 s->commit_id = got_object_id_dup(commit_id);
7583 if (s->commit_id == NULL) {
7584 err = got_error_from_errno("got_object_id_dup");
7588 err = got_object_open_as_commit(&commit, repo, commit_id);
7593 * The root is opened here and will be closed when the view is closed.
7594 * Any visited subtrees and their path-wise parents are opened and
7597 err = got_object_open_as_tree(&s->root, repo,
7598 got_object_commit_get_tree_id(commit));
7603 err = got_object_id_str(&commit_id_str, commit_id);
7607 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7608 err = got_error_from_errno("asprintf");
7612 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7613 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7614 if (head_ref_name) {
7615 s->head_ref_name = strdup(head_ref_name);
7616 if (s->head_ref_name == NULL) {
7617 err = got_error_from_errno("strdup");
7623 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7624 err = add_color(&s->colors, "\\$$",
7625 TOG_COLOR_TREE_SUBMODULE,
7626 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7629 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7630 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7633 err = add_color(&s->colors, "/$",
7634 TOG_COLOR_TREE_DIRECTORY,
7635 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7639 err = add_color(&s->colors, "\\*$",
7640 TOG_COLOR_TREE_EXECUTABLE,
7641 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7645 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7646 get_color_value("TOG_COLOR_COMMIT"));
7651 view->show = show_tree_view;
7652 view->input = input_tree_view;
7653 view->close = close_tree_view;
7654 view->search_start = search_start_tree_view;
7655 view->search_next = search_next_tree_view;
7657 free(commit_id_str);
7659 got_object_commit_close(commit);
7661 if (view->close == NULL)
7662 close_tree_view(view);
7668 static const struct got_error *
7669 close_tree_view(struct tog_view *view)
7671 struct tog_tree_view_state *s = &view->state.tree;
7673 free_colors(&s->colors);
7674 free(s->tree_label);
7675 s->tree_label = NULL;
7677 s->commit_id = NULL;
7678 free(s->head_ref_name);
7679 s->head_ref_name = NULL;
7680 while (!TAILQ_EMPTY(&s->parents)) {
7681 struct tog_parent_tree *parent;
7682 parent = TAILQ_FIRST(&s->parents);
7683 TAILQ_REMOVE(&s->parents, parent, entry);
7684 if (parent->tree != s->root)
7685 got_object_tree_close(parent->tree);
7689 if (s->tree != NULL && s->tree != s->root)
7690 got_object_tree_close(s->tree);
7692 got_object_tree_close(s->root);
7696 static const struct got_error *
7697 search_start_tree_view(struct tog_view *view)
7699 struct tog_tree_view_state *s = &view->state.tree;
7701 s->matched_entry = NULL;
7706 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7708 regmatch_t regmatch;
7710 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7714 static const struct got_error *
7715 search_next_tree_view(struct tog_view *view)
7717 struct tog_tree_view_state *s = &view->state.tree;
7718 struct got_tree_entry *te = NULL;
7720 if (!view->searching) {
7721 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7725 if (s->matched_entry) {
7726 if (view->searching == TOG_SEARCH_FORWARD) {
7727 if (s->selected_entry)
7728 te = got_tree_entry_get_next(s->tree,
7731 te = got_object_tree_get_first_entry(s->tree);
7733 if (s->selected_entry == NULL)
7734 te = got_object_tree_get_last_entry(s->tree);
7736 te = got_tree_entry_get_prev(s->tree,
7740 if (s->selected_entry)
7741 te = s->selected_entry;
7742 else if (view->searching == TOG_SEARCH_FORWARD)
7743 te = got_object_tree_get_first_entry(s->tree);
7745 te = got_object_tree_get_last_entry(s->tree);
7750 if (s->matched_entry == NULL) {
7751 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7754 if (view->searching == TOG_SEARCH_FORWARD)
7755 te = got_object_tree_get_first_entry(s->tree);
7757 te = got_object_tree_get_last_entry(s->tree);
7760 if (match_tree_entry(te, &view->regex)) {
7761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7762 s->matched_entry = te;
7766 if (view->searching == TOG_SEARCH_FORWARD)
7767 te = got_tree_entry_get_next(s->tree, te);
7769 te = got_tree_entry_get_prev(s->tree, te);
7772 if (s->matched_entry) {
7773 s->first_displayed_entry = s->matched_entry;
7780 static const struct got_error *
7781 show_tree_view(struct tog_view *view)
7783 const struct got_error *err = NULL;
7784 struct tog_tree_view_state *s = &view->state.tree;
7787 err = tree_entry_path(&parent_path, &s->parents, NULL);
7791 err = draw_tree_entries(view, parent_path);
7798 static const struct got_error *
7799 tree_goto_line(struct tog_view *view, int nlines)
7801 const struct got_error *err = NULL;
7802 struct tog_tree_view_state *s = &view->state.tree;
7803 struct got_tree_entry **fte, **lte, **ste;
7804 int g, last, first = 1, i = 1;
7805 int root = s->tree == s->root;
7806 int off = root ? 1 : 2;
7813 else if (g > got_object_tree_get_nentries(s->tree))
7814 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7816 fte = &s->first_displayed_entry;
7817 lte = &s->last_displayed_entry;
7818 ste = &s->selected_entry;
7821 first = got_tree_entry_get_index(*fte);
7822 first += off; /* account for ".." */
7824 last = got_tree_entry_get_index(*lte);
7827 if (g >= first && g <= last && g - first < nlines) {
7828 s->selected = g - first;
7829 return NULL; /* gline is on the current page */
7833 i = got_tree_entry_get_index(*ste);
7838 err = tree_scroll_down(view, g - i);
7841 if (got_tree_entry_get_index(*lte) >=
7842 got_object_tree_get_nentries(s->tree) - 1 &&
7843 first + s->selected < g &&
7844 s->selected < s->ndisplayed - 1) {
7845 first = got_tree_entry_get_index(*fte);
7847 s->selected = g - first;
7850 tree_scroll_up(s, i - g);
7853 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7854 s->selected = g - 1;
7859 static const struct got_error *
7860 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7862 const struct got_error *err = NULL;
7863 struct tog_tree_view_state *s = &view->state.tree;
7864 struct got_tree_entry *te;
7865 int n, nscroll = view->nlines - 3;
7868 return tree_goto_line(view, nscroll);
7877 horizontal_scroll_input(view, ch);
7880 s->show_ids = !s->show_ids;
7885 if (!s->selected_entry)
7887 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7891 err = view_request_new(new_view, view, TOG_VIEW_REF);
7898 if (s->tree == s->root)
7899 s->first_displayed_entry =
7900 got_object_tree_get_first_entry(s->tree);
7902 s->first_displayed_entry = NULL;
7907 int eos = view->nlines - 3;
7909 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7913 te = got_object_tree_get_last_entry(s->tree);
7914 for (n = 0; n < eos; n++) {
7916 if (s->tree != s->root) {
7917 s->first_displayed_entry = NULL;
7922 s->first_displayed_entry = te;
7923 te = got_tree_entry_get_prev(s->tree, te);
7926 s->selected = n - 1;
7932 if (s->selected > 0) {
7936 tree_scroll_up(s, 1);
7937 if (s->selected_entry == NULL ||
7938 (s->tree == s->root && s->selected_entry ==
7939 got_object_tree_get_first_entry(s->tree)))
7949 if (s->tree == s->root) {
7950 if (got_object_tree_get_first_entry(s->tree) ==
7951 s->first_displayed_entry)
7952 s->selected -= MIN(s->selected, nscroll);
7954 if (s->first_displayed_entry == NULL)
7955 s->selected -= MIN(s->selected, nscroll);
7957 tree_scroll_up(s, MAX(0, nscroll));
7958 if (s->selected_entry == NULL ||
7959 (s->tree == s->root && s->selected_entry ==
7960 got_object_tree_get_first_entry(s->tree)))
7966 if (s->selected < s->ndisplayed - 1) {
7970 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7972 /* can't scroll any further */
7976 tree_scroll_down(view, 1);
7986 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7988 /* can't scroll any further; move cursor down */
7989 if (s->selected < s->ndisplayed - 1)
7990 s->selected += MIN(nscroll,
7991 s->ndisplayed - s->selected - 1);
7996 tree_scroll_down(view, nscroll);
8001 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8002 struct tog_parent_tree *parent;
8003 /* user selected '..' */
8004 if (s->tree == s->root) {
8008 parent = TAILQ_FIRST(&s->parents);
8009 TAILQ_REMOVE(&s->parents, parent,
8011 got_object_tree_close(s->tree);
8012 s->tree = parent->tree;
8013 s->first_displayed_entry =
8014 parent->first_displayed_entry;
8016 parent->selected_entry;
8017 s->selected = parent->selected;
8018 if (s->selected > view->nlines - 3) {
8019 err = offset_selection_down(view);
8024 } else if (S_ISDIR(got_tree_entry_get_mode(
8025 s->selected_entry))) {
8026 struct got_tree_object *subtree;
8028 err = got_object_open_as_tree(&subtree, s->repo,
8029 got_tree_entry_get_id(s->selected_entry));
8032 err = tree_view_visit_subtree(s, subtree);
8034 got_object_tree_close(subtree);
8037 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8038 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8041 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8042 s->selected = view->nlines - 4;
8058 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8063 static const struct got_error *
8064 cmd_tree(int argc, char *argv[])
8066 const struct got_error *error;
8067 struct got_repository *repo = NULL;
8068 struct got_worktree *worktree = NULL;
8069 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8070 struct got_object_id *commit_id = NULL;
8071 struct got_commit_object *commit = NULL;
8072 const char *commit_id_arg = NULL;
8073 char *keyword_idstr = NULL, *label = NULL;
8074 struct got_reference *ref = NULL;
8075 const char *head_ref_name = NULL;
8077 struct tog_view *view;
8078 int *pack_fds = NULL;
8080 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8083 commit_id_arg = optarg;
8086 repo_path = realpath(optarg, NULL);
8087 if (repo_path == NULL)
8088 return got_error_from_errno2("realpath",
8103 error = got_repo_pack_fds_open(&pack_fds);
8107 if (repo_path == NULL) {
8108 cwd = getcwd(NULL, 0);
8110 return got_error_from_errno("getcwd");
8111 error = got_worktree_open(&worktree, cwd, NULL);
8112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8116 strdup(got_worktree_get_repo_path(worktree));
8118 repo_path = strdup(cwd);
8119 if (repo_path == NULL) {
8120 error = got_error_from_errno("strdup");
8125 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8129 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8136 error = apply_unveil(got_repo_get_path(repo), NULL);
8140 error = tog_load_refs(repo, 0);
8144 if (commit_id_arg == NULL) {
8145 error = got_repo_match_object_id(&commit_id, &label,
8146 worktree ? got_worktree_get_head_ref_name(worktree) :
8147 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8150 head_ref_name = label;
8152 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8156 if (keyword_idstr != NULL)
8157 commit_id_arg = keyword_idstr;
8159 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8161 head_ref_name = got_ref_get_name(ref);
8162 else if (error->code != GOT_ERR_NOT_REF)
8164 error = got_repo_match_object_id(&commit_id, NULL,
8165 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8170 error = got_object_open_as_commit(&commit, repo, commit_id);
8174 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8176 error = got_error_from_errno("view_open");
8179 error = open_tree_view(view, commit_id, head_ref_name, repo);
8182 if (!got_path_is_root_dir(in_repo_path)) {
8183 error = tree_view_walk_path(&view->state.tree, commit,
8190 /* Release work tree lock. */
8191 got_worktree_close(worktree);
8194 error = view_loop(view);
8196 free(keyword_idstr);
8204 const struct got_error *close_err = got_repo_close(repo);
8209 const struct got_error *pack_err =
8210 got_repo_pack_fds_close(pack_fds);
8218 static const struct got_error *
8219 ref_view_load_refs(struct tog_ref_view_state *s)
8221 struct got_reflist_entry *sre;
8222 struct tog_reflist_entry *re;
8225 TAILQ_FOREACH(sre, &tog_refs, entry) {
8226 if (strncmp(got_ref_get_name(sre->ref),
8227 "refs/got/", 9) == 0 &&
8228 strncmp(got_ref_get_name(sre->ref),
8229 "refs/got/backup/", 16) != 0)
8232 re = malloc(sizeof(*re));
8234 return got_error_from_errno("malloc");
8236 re->ref = got_ref_dup(sre->ref);
8237 if (re->ref == NULL)
8238 return got_error_from_errno("got_ref_dup");
8239 re->idx = s->nrefs++;
8240 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8243 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8248 ref_view_free_refs(struct tog_ref_view_state *s)
8250 struct tog_reflist_entry *re;
8252 while (!TAILQ_EMPTY(&s->refs)) {
8253 re = TAILQ_FIRST(&s->refs);
8254 TAILQ_REMOVE(&s->refs, re, entry);
8255 got_ref_close(re->ref);
8260 static const struct got_error *
8261 open_ref_view(struct tog_view *view, struct got_repository *repo)
8263 const struct got_error *err = NULL;
8264 struct tog_ref_view_state *s = &view->state.ref;
8266 s->selected_entry = 0;
8269 TAILQ_INIT(&s->refs);
8270 STAILQ_INIT(&s->colors);
8272 err = ref_view_load_refs(s);
8276 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8277 err = add_color(&s->colors, "^refs/heads/",
8278 TOG_COLOR_REFS_HEADS,
8279 get_color_value("TOG_COLOR_REFS_HEADS"));
8283 err = add_color(&s->colors, "^refs/tags/",
8284 TOG_COLOR_REFS_TAGS,
8285 get_color_value("TOG_COLOR_REFS_TAGS"));
8289 err = add_color(&s->colors, "^refs/remotes/",
8290 TOG_COLOR_REFS_REMOTES,
8291 get_color_value("TOG_COLOR_REFS_REMOTES"));
8295 err = add_color(&s->colors, "^refs/got/backup/",
8296 TOG_COLOR_REFS_BACKUP,
8297 get_color_value("TOG_COLOR_REFS_BACKUP"));
8302 view->show = show_ref_view;
8303 view->input = input_ref_view;
8304 view->close = close_ref_view;
8305 view->search_start = search_start_ref_view;
8306 view->search_next = search_next_ref_view;
8309 if (view->close == NULL)
8310 close_ref_view(view);
8316 static const struct got_error *
8317 close_ref_view(struct tog_view *view)
8319 struct tog_ref_view_state *s = &view->state.ref;
8321 ref_view_free_refs(s);
8322 free_colors(&s->colors);
8327 static const struct got_error *
8328 resolve_reflist_entry(struct got_object_id **commit_id,
8329 struct tog_reflist_entry *re, struct got_repository *repo)
8331 const struct got_error *err = NULL;
8332 struct got_object_id *obj_id;
8333 struct got_tag_object *tag = NULL;
8338 err = got_ref_resolve(&obj_id, repo, re->ref);
8342 err = got_object_get_type(&obj_type, repo, obj_id);
8347 case GOT_OBJ_TYPE_COMMIT:
8348 *commit_id = obj_id;
8350 case GOT_OBJ_TYPE_TAG:
8351 err = got_object_open_as_tag(&tag, repo, obj_id);
8355 err = got_object_get_type(&obj_type, repo,
8356 got_object_tag_get_object_id(tag));
8359 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8360 err = got_error(GOT_ERR_OBJ_TYPE);
8363 *commit_id = got_object_id_dup(
8364 got_object_tag_get_object_id(tag));
8365 if (*commit_id == NULL) {
8366 err = got_error_from_errno("got_object_id_dup");
8371 err = got_error(GOT_ERR_OBJ_TYPE);
8377 got_object_tag_close(tag);
8385 static const struct got_error *
8386 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8387 struct tog_reflist_entry *re, struct got_repository *repo)
8389 struct tog_view *log_view;
8390 const struct got_error *err = NULL;
8391 struct got_object_id *commit_id = NULL;
8395 err = resolve_reflist_entry(&commit_id, re, repo);
8397 if (err->code != GOT_ERR_OBJ_TYPE)
8403 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8404 if (log_view == NULL) {
8405 err = got_error_from_errno("view_open");
8409 err = open_log_view(log_view, commit_id, repo,
8410 got_ref_get_name(re->ref), "", 0);
8413 view_close(log_view);
8415 *new_view = log_view;
8421 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8423 struct tog_reflist_entry *re;
8426 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8429 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8430 while (i++ < maxscroll) {
8433 s->first_displayed_entry = re;
8434 re = TAILQ_PREV(re, tog_reflist_head, entry);
8438 static const struct got_error *
8439 ref_scroll_down(struct tog_view *view, int maxscroll)
8441 struct tog_ref_view_state *s = &view->state.ref;
8442 struct tog_reflist_entry *next, *last;
8445 if (s->first_displayed_entry)
8446 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8448 next = TAILQ_FIRST(&s->refs);
8450 last = s->last_displayed_entry;
8451 while (next && n++ < maxscroll) {
8453 s->last_displayed_entry = last;
8454 last = TAILQ_NEXT(last, entry);
8456 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8457 s->first_displayed_entry = next;
8458 next = TAILQ_NEXT(next, entry);
8465 static const struct got_error *
8466 search_start_ref_view(struct tog_view *view)
8468 struct tog_ref_view_state *s = &view->state.ref;
8470 s->matched_entry = NULL;
8475 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8477 regmatch_t regmatch;
8479 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8483 static const struct got_error *
8484 search_next_ref_view(struct tog_view *view)
8486 struct tog_ref_view_state *s = &view->state.ref;
8487 struct tog_reflist_entry *re = NULL;
8489 if (!view->searching) {
8490 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8494 if (s->matched_entry) {
8495 if (view->searching == TOG_SEARCH_FORWARD) {
8496 if (s->selected_entry)
8497 re = TAILQ_NEXT(s->selected_entry, entry);
8499 re = TAILQ_PREV(s->selected_entry,
8500 tog_reflist_head, entry);
8502 if (s->selected_entry == NULL)
8503 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8505 re = TAILQ_PREV(s->selected_entry,
8506 tog_reflist_head, entry);
8509 if (s->selected_entry)
8510 re = s->selected_entry;
8511 else if (view->searching == TOG_SEARCH_FORWARD)
8512 re = TAILQ_FIRST(&s->refs);
8514 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8519 if (s->matched_entry == NULL) {
8520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8523 if (view->searching == TOG_SEARCH_FORWARD)
8524 re = TAILQ_FIRST(&s->refs);
8526 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8529 if (match_reflist_entry(re, &view->regex)) {
8530 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8531 s->matched_entry = re;
8535 if (view->searching == TOG_SEARCH_FORWARD)
8536 re = TAILQ_NEXT(re, entry);
8538 re = TAILQ_PREV(re, tog_reflist_head, entry);
8541 if (s->matched_entry) {
8542 s->first_displayed_entry = s->matched_entry;
8549 static const struct got_error *
8550 show_ref_view(struct tog_view *view)
8552 const struct got_error *err = NULL;
8553 struct tog_ref_view_state *s = &view->state.ref;
8554 struct tog_reflist_entry *re;
8557 struct tog_color *tc;
8558 int width, n, scrollx;
8559 int limit = view->nlines;
8561 werase(view->window);
8564 if (view_is_hsplit_top(view))
8565 --limit; /* border */
8570 re = s->first_displayed_entry;
8572 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8574 return got_error_from_errno("asprintf");
8576 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8581 if (view_needs_focus_indication(view))
8582 wstandout(view->window);
8583 waddwstr(view->window, wline);
8584 while (width++ < view->ncols)
8585 waddch(view->window, ' ');
8586 if (view_needs_focus_indication(view))
8587 wstandend(view->window);
8597 while (re && limit > 0) {
8599 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8602 struct got_commit_object *ci;
8603 struct got_tag_object *tag;
8604 struct got_object_id *id;
8608 err = got_ref_resolve(&id, s->repo, re->ref);
8611 err = got_object_open_as_tag(&tag, s->repo, id);
8613 if (err->code != GOT_ERR_OBJ_TYPE) {
8617 err = got_object_open_as_commit(&ci, s->repo,
8623 t = got_object_commit_get_committer_time(ci);
8624 got_object_commit_close(ci);
8626 t = got_object_tag_get_tagger_time(tag);
8627 got_object_tag_close(tag);
8630 if (gmtime_r(&t, &tm) == NULL)
8631 return got_error_from_errno("gmtime_r");
8632 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8633 return got_error(GOT_ERR_NO_SPACE);
8635 if (got_ref_is_symbolic(re->ref)) {
8636 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8637 ymd : "", got_ref_get_name(re->ref),
8638 got_ref_get_symref_target(re->ref)) == -1)
8639 return got_error_from_errno("asprintf");
8640 } else if (s->show_ids) {
8641 struct got_object_id *id;
8643 err = got_ref_resolve(&id, s->repo, re->ref);
8646 err = got_object_id_str(&id_str, id);
8651 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8652 got_ref_get_name(re->ref), id_str) == -1) {
8653 err = got_error_from_errno("asprintf");
8660 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8661 got_ref_get_name(re->ref)) == -1)
8662 return got_error_from_errno("asprintf");
8664 /* use full line width to determine view->maxx */
8665 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8670 view->maxx = MAX(view->maxx, width);
8674 err = format_line(&wline, &width, &scrollx, line, view->x,
8680 if (n == s->selected) {
8682 wstandout(view->window);
8683 s->selected_entry = re;
8685 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8687 wattr_on(view->window,
8688 COLOR_PAIR(tc->colorpair), NULL);
8689 waddwstr(view->window, &wline[scrollx]);
8691 wattr_off(view->window,
8692 COLOR_PAIR(tc->colorpair), NULL);
8693 if (width < view->ncols)
8694 waddch(view->window, '\n');
8695 if (n == s->selected && view->focussed)
8696 wstandend(view->window);
8702 s->last_displayed_entry = re;
8705 re = TAILQ_NEXT(re, entry);
8712 static const struct got_error *
8713 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8714 struct tog_reflist_entry *re, struct got_repository *repo)
8716 const struct got_error *err = NULL;
8717 struct got_object_id *commit_id = NULL;
8718 struct tog_view *tree_view;
8722 err = resolve_reflist_entry(&commit_id, re, repo);
8724 if (err->code != GOT_ERR_OBJ_TYPE)
8731 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8732 if (tree_view == NULL) {
8733 err = got_error_from_errno("view_open");
8737 err = open_tree_view(tree_view, commit_id,
8738 got_ref_get_name(re->ref), repo);
8742 *new_view = tree_view;
8748 static const struct got_error *
8749 ref_goto_line(struct tog_view *view, int nlines)
8751 const struct got_error *err = NULL;
8752 struct tog_ref_view_state *s = &view->state.ref;
8753 int g, idx = s->selected_entry->idx;
8760 else if (g > s->nrefs)
8763 if (g >= s->first_displayed_entry->idx + 1 &&
8764 g <= s->last_displayed_entry->idx + 1 &&
8765 g - s->first_displayed_entry->idx - 1 < nlines) {
8766 s->selected = g - s->first_displayed_entry->idx - 1;
8771 err = ref_scroll_down(view, g - idx - 1);
8774 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8775 s->first_displayed_entry->idx + s->selected < g &&
8776 s->selected < s->ndisplayed - 1)
8777 s->selected = g - s->first_displayed_entry->idx - 1;
8778 } else if (idx + 1 > g)
8779 ref_scroll_up(s, idx - g + 1);
8781 if (g < nlines && s->first_displayed_entry->idx == 0)
8782 s->selected = g - 1;
8788 static const struct got_error *
8789 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8791 const struct got_error *err = NULL;
8792 struct tog_ref_view_state *s = &view->state.ref;
8793 struct tog_reflist_entry *re;
8794 int n, nscroll = view->nlines - 1;
8797 return ref_goto_line(view, nscroll);
8806 horizontal_scroll_input(view, ch);
8809 s->show_ids = !s->show_ids;
8813 s->show_date = !s->show_date;
8817 s->sort_by_date = !s->sort_by_date;
8818 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8820 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8821 got_ref_cmp_by_commit_timestamp_descending :
8822 tog_ref_cmp_by_name, s->repo);
8825 got_reflist_object_id_map_free(tog_refs_idmap);
8826 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8827 &tog_refs, s->repo);
8830 ref_view_free_refs(s);
8831 err = ref_view_load_refs(s);
8836 if (!s->selected_entry)
8838 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8842 if (!s->selected_entry)
8844 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8851 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8856 int eos = view->nlines - 1;
8858 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8862 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8863 for (n = 0; n < eos; n++) {
8866 s->first_displayed_entry = re;
8867 re = TAILQ_PREV(re, tog_reflist_head, entry);
8870 s->selected = n - 1;
8876 if (s->selected > 0) {
8880 ref_scroll_up(s, 1);
8881 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8891 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8892 s->selected -= MIN(nscroll, s->selected);
8893 ref_scroll_up(s, MAX(0, nscroll));
8894 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8900 if (s->selected < s->ndisplayed - 1) {
8904 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8905 /* can't scroll any further */
8909 ref_scroll_down(view, 1);
8919 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8920 /* can't scroll any further; move cursor down */
8921 if (s->selected < s->ndisplayed - 1)
8922 s->selected += MIN(nscroll,
8923 s->ndisplayed - s->selected - 1);
8924 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8925 s->selected += s->ndisplayed - s->selected - 1;
8929 ref_scroll_down(view, nscroll);
8934 err = tog_load_refs(s->repo, s->sort_by_date);
8937 ref_view_free_refs(s);
8938 err = ref_view_load_refs(s);
8941 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8942 s->selected = view->nlines - 2;
8956 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8961 static const struct got_error *
8962 cmd_ref(int argc, char *argv[])
8964 const struct got_error *error;
8965 struct got_repository *repo = NULL;
8966 struct got_worktree *worktree = NULL;
8967 char *cwd = NULL, *repo_path = NULL;
8969 struct tog_view *view;
8970 int *pack_fds = NULL;
8972 while ((ch = getopt(argc, argv, "r:")) != -1) {
8975 repo_path = realpath(optarg, NULL);
8976 if (repo_path == NULL)
8977 return got_error_from_errno2("realpath",
8992 error = got_repo_pack_fds_open(&pack_fds);
8996 if (repo_path == NULL) {
8997 cwd = getcwd(NULL, 0);
8999 return got_error_from_errno("getcwd");
9000 error = got_worktree_open(&worktree, cwd, NULL);
9001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9005 strdup(got_worktree_get_repo_path(worktree));
9007 repo_path = strdup(cwd);
9008 if (repo_path == NULL) {
9009 error = got_error_from_errno("strdup");
9014 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9020 error = apply_unveil(got_repo_get_path(repo), NULL);
9024 error = tog_load_refs(repo, 0);
9028 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9030 error = got_error_from_errno("view_open");
9034 error = open_ref_view(view, repo);
9039 /* Release work tree lock. */
9040 got_worktree_close(worktree);
9043 error = view_loop(view);
9048 const struct got_error *close_err = got_repo_close(repo);
9053 const struct got_error *pack_err =
9054 got_repo_pack_fds_close(pack_fds);
9062 static const struct got_error*
9063 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9072 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9076 if (mvwprintw(win, y, x, "%s", str) == ERR)
9077 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9084 static const struct got_error *
9085 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9089 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9091 free(*line_offsets);
9092 *line_offsets = NULL;
9093 return got_error_from_errno("reallocarray");
9097 (*line_offsets)[*nlines] = off;
9102 static const struct got_error *
9103 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9107 for (;n > 0; --n, ++km) {
9111 if (km->keys == NULL)
9114 t = t0 = strdup(km->keys);
9116 return got_error_from_errno("strdup");
9119 while ((k = strsep(&t, " ")) != NULL)
9120 len += strlen(k) > 1 ? 2 : 0;
9122 *ret = MAX(*ret, len);
9129 * Write keymap section headers, keys, and key info in km to f.
9130 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9131 * wrap control and symbolic keys in guillemets, else use <>.
9133 static const struct got_error *
9134 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9139 static const char *u8_glyph[] = {
9140 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9141 "\xe2\x80\xba" /* U+203A (utf8 >) */
9144 int cs, s, first = 1;
9146 cs = got_locale_is_utf8();
9148 t = t0 = strdup(km->keys);
9150 return got_error_from_errno("strdup");
9152 len = strlen(km->keys);
9153 while ((k = strsep(&t, " ")) != NULL) {
9154 s = strlen(k) > 1; /* control or symbolic key */
9155 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9156 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9157 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9160 return got_error_from_errno("fprintf");
9168 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9170 return got_error_from_errno("fprintf");
9176 static const struct got_error *
9177 format_help(struct tog_help_view_state *s)
9179 const struct got_error *err = NULL;
9181 int i, max, n, show = s->all;
9182 static const struct tog_key_map km[] = {
9183 #define KEYMAP_(info, type) { NULL, (info), type }
9184 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9190 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9195 err = max_key_str(&max, km, n);
9199 for (i = 0; i < n; ++i) {
9200 if (km[i].keys == NULL) {
9202 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9203 km[i].type == s->type || s->all)
9207 err = format_help_line(&off, s->f, &km[i], max);
9210 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9217 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9221 static const struct got_error *
9222 create_help(struct tog_help_view_state *s)
9225 const struct got_error *err;
9227 free(s->line_offsets);
9228 s->line_offsets = NULL;
9233 return got_error_from_errno("got_opentemp");
9236 err = format_help(s);
9240 if (s->f && fflush(s->f) != 0)
9241 return got_error_from_errno("fflush");
9246 static const struct got_error *
9247 search_start_help_view(struct tog_view *view)
9249 view->state.help.matched_line = 0;
9254 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9255 size_t *nlines, int **first, int **last, int **match, int **selected)
9257 struct tog_help_view_state *s = &view->state.help;
9260 *nlines = s->nlines;
9261 *line_offsets = s->line_offsets;
9262 *match = &s->matched_line;
9263 *first = &s->first_displayed_line;
9264 *last = &s->last_displayed_line;
9265 *selected = &s->selected_line;
9268 static const struct got_error *
9269 show_help_view(struct tog_view *view)
9271 struct tog_help_view_state *s = &view->state.help;
9272 const struct got_error *err;
9273 regmatch_t *regmatch = &view->regmatch;
9278 int width, nprinted = 0, rc = 0;
9279 int eos = view->nlines;
9281 if (view_is_hsplit_top(view))
9282 --eos; /* account for border */
9286 werase(view->window);
9288 if (view->gline > s->nlines - 1)
9289 view->gline = s->nlines - 1;
9291 err = win_draw_center(view->window, 0, 0, view->ncols,
9292 view_needs_focus_indication(view),
9293 "tog help (press q to return to tog)");
9298 waddstr(view->window, "\n\n");
9304 while (eos > 0 && nprinted < eos) {
9307 linelen = getline(&line, &linesz, s->f);
9308 if (linelen == -1) {
9311 return got_ferror(s->f, GOT_ERR_IO);
9316 if (++s->lineno < s->first_displayed_line)
9318 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9320 if (s->lineno == view->hiline)
9323 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9329 view->maxx = MAX(view->maxx, width);
9334 wattron(view->window, attr);
9335 if (s->first_displayed_line + nprinted == s->matched_line &&
9336 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9337 err = add_matched_line(&width, line, view->ncols - 1, 0,
9338 view->window, view->x, regmatch);
9346 err = format_line(&wline, &width, &skip, line,
9347 view->x, view->ncols, 0, view->x ? 1 : 0);
9352 waddwstr(view->window, &wline[skip]);
9356 if (s->lineno == view->hiline) {
9357 while (width++ < view->ncols)
9358 waddch(view->window, ' ');
9360 if (width < view->ncols)
9361 waddch(view->window, '\n');
9364 wattroff(view->window, attr);
9365 if (++nprinted == 1)
9366 s->first_displayed_line = s->lineno;
9370 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9372 s->last_displayed_line = s->first_displayed_line;
9377 rc = waddnstr(view->window,
9378 "See the tog(1) manual page for full documentation",
9381 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9383 wmove(view->window, view->nlines - 1, 0);
9384 wclrtoeol(view->window);
9385 wstandout(view->window);
9386 rc = waddnstr(view->window, "scroll down for more...",
9389 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9390 if (getcurx(view->window) < view->ncols - 6) {
9391 rc = wprintw(view->window, "[%.0f%%]",
9392 100.00 * s->last_displayed_line / s->nlines);
9394 return got_error_msg(GOT_ERR_IO, "wprintw");
9396 wstandend(view->window);
9402 static const struct got_error *
9403 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9405 struct tog_help_view_state *s = &view->state.help;
9406 const struct got_error *err = NULL;
9412 eos = nscroll = view->nlines;
9413 if (view_is_hsplit_top(view))
9416 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9425 horizontal_scroll_input(view, ch);
9429 s->first_displayed_line = 1;
9437 s->first_displayed_line = (s->nlines - eos) + 3;
9442 if (s->first_displayed_line > 1)
9443 --s->first_displayed_line;
9454 if (s->first_displayed_line == 1) {
9458 while (--nscroll > 0 && s->first_displayed_line > 1)
9459 s->first_displayed_line--;
9465 ++s->first_displayed_line;
9481 while (!s->eof && --nscroll > 0) {
9482 linelen = getline(&line, &linesz, s->f);
9483 s->first_displayed_line++;
9484 if (linelen == -1) {
9488 err = got_ferror(s->f, GOT_ERR_IO);
9502 static const struct got_error *
9503 close_help_view(struct tog_view *view)
9505 struct tog_help_view_state *s = &view->state.help;
9507 free(s->line_offsets);
9508 s->line_offsets = NULL;
9509 if (fclose(s->f) == EOF)
9510 return got_error_from_errno("fclose");
9515 static const struct got_error *
9516 reset_help_view(struct tog_view *view)
9518 struct tog_help_view_state *s = &view->state.help;
9521 if (s->f && fclose(s->f) == EOF)
9522 return got_error_from_errno("fclose");
9524 wclear(view->window);
9528 s->first_displayed_line = 1;
9529 s->last_displayed_line = view->nlines;
9530 s->matched_line = 0;
9532 return create_help(s);
9535 static const struct got_error *
9536 open_help_view(struct tog_view *view, struct tog_view *parent)
9538 const struct got_error *err = NULL;
9539 struct tog_help_view_state *s = &view->state.help;
9541 s->type = (enum tog_keymap_type)parent->type;
9542 s->first_displayed_line = 1;
9543 s->last_displayed_line = view->nlines;
9544 s->selected_line = 1;
9546 view->show = show_help_view;
9547 view->input = input_help_view;
9548 view->reset = reset_help_view;
9549 view->close = close_help_view;
9550 view->search_start = search_start_help_view;
9551 view->search_setup = search_setup_help_view;
9552 view->search_next = search_next_view_match;
9554 err = create_help(s);
9558 static const struct got_error *
9559 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9560 enum tog_view_type request, int y, int x)
9562 const struct got_error *err = NULL;
9568 if (view->type == TOG_VIEW_LOG) {
9569 struct tog_log_view_state *s = &view->state.log;
9571 err = open_diff_view_for_commit(new_view, y, x,
9572 s->selected_entry->commit, s->selected_entry->id,
9575 return got_error_msg(GOT_ERR_NOT_IMPL,
9576 "parent/child view pair not supported");
9578 case TOG_VIEW_BLAME:
9579 if (view->type == TOG_VIEW_TREE) {
9580 struct tog_tree_view_state *s = &view->state.tree;
9582 err = blame_tree_entry(new_view, y, x,
9583 s->selected_entry, &s->parents, s->commit_id,
9586 return got_error_msg(GOT_ERR_NOT_IMPL,
9587 "parent/child view pair not supported");
9590 if (view->type == TOG_VIEW_BLAME)
9591 err = log_annotated_line(new_view, y, x,
9592 view->state.blame.repo, view->state.blame.id_to_log);
9593 else if (view->type == TOG_VIEW_TREE)
9594 err = log_selected_tree_entry(new_view, y, x,
9596 else if (view->type == TOG_VIEW_REF)
9597 err = log_ref_entry(new_view, y, x,
9598 view->state.ref.selected_entry,
9599 view->state.ref.repo);
9601 return got_error_msg(GOT_ERR_NOT_IMPL,
9602 "parent/child view pair not supported");
9605 if (view->type == TOG_VIEW_LOG)
9606 err = browse_commit_tree(new_view, y, x,
9607 view->state.log.selected_entry,
9608 view->state.log.in_repo_path,
9609 view->state.log.head_ref_name,
9610 view->state.log.repo);
9611 else if (view->type == TOG_VIEW_REF)
9612 err = browse_ref_tree(new_view, y, x,
9613 view->state.ref.selected_entry,
9614 view->state.ref.repo);
9616 return got_error_msg(GOT_ERR_NOT_IMPL,
9617 "parent/child view pair not supported");
9620 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9621 if (*new_view == NULL)
9622 return got_error_from_errno("view_open");
9623 if (view->type == TOG_VIEW_LOG)
9624 err = open_ref_view(*new_view, view->state.log.repo);
9625 else if (view->type == TOG_VIEW_TREE)
9626 err = open_ref_view(*new_view, view->state.tree.repo);
9628 err = got_error_msg(GOT_ERR_NOT_IMPL,
9629 "parent/child view pair not supported");
9631 view_close(*new_view);
9634 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9635 if (*new_view == NULL)
9636 return got_error_from_errno("view_open");
9637 err = open_help_view(*new_view, view);
9639 view_close(*new_view);
9642 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9649 * If view was scrolled down to move the selected line into view when opening a
9650 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9653 offset_selection_up(struct tog_view *view)
9655 switch (view->type) {
9656 case TOG_VIEW_BLAME: {
9657 struct tog_blame_view_state *s = &view->state.blame;
9658 if (s->first_displayed_line == 1) {
9659 s->selected_line = MAX(s->selected_line - view->offset,
9663 if (s->first_displayed_line > view->offset)
9664 s->first_displayed_line -= view->offset;
9666 s->first_displayed_line = 1;
9667 s->selected_line += view->offset;
9671 log_scroll_up(&view->state.log, view->offset);
9672 view->state.log.selected += view->offset;
9675 ref_scroll_up(&view->state.ref, view->offset);
9676 view->state.ref.selected += view->offset;
9679 tree_scroll_up(&view->state.tree, view->offset);
9680 view->state.tree.selected += view->offset;
9690 * If the selected line is in the section of screen covered by the bottom split,
9691 * scroll down offset lines to move it into view and index its new position.
9693 static const struct got_error *
9694 offset_selection_down(struct tog_view *view)
9696 const struct got_error *err = NULL;
9697 const struct got_error *(*scrolld)(struct tog_view *, int);
9698 int *selected = NULL;
9701 switch (view->type) {
9702 case TOG_VIEW_BLAME: {
9703 struct tog_blame_view_state *s = &view->state.blame;
9706 if (s->selected_line > view->nlines - header) {
9707 offset = abs(view->nlines - s->selected_line - header);
9708 s->first_displayed_line += offset;
9709 s->selected_line -= offset;
9710 view->offset = offset;
9714 case TOG_VIEW_LOG: {
9715 struct tog_log_view_state *s = &view->state.log;
9716 scrolld = &log_scroll_down;
9717 header = view_is_parent_view(view) ? 3 : 2;
9718 selected = &s->selected;
9721 case TOG_VIEW_REF: {
9722 struct tog_ref_view_state *s = &view->state.ref;
9723 scrolld = &ref_scroll_down;
9725 selected = &s->selected;
9728 case TOG_VIEW_TREE: {
9729 struct tog_tree_view_state *s = &view->state.tree;
9730 scrolld = &tree_scroll_down;
9732 selected = &s->selected;
9742 if (selected && *selected > view->nlines - header) {
9743 offset = abs(view->nlines - *selected - header);
9744 view->offset = offset;
9745 if (scrolld && offset) {
9746 err = scrolld(view, offset);
9747 *selected -= offset;
9755 list_commands(FILE *fp)
9759 fprintf(fp, "commands:");
9760 for (i = 0; i < nitems(tog_commands); i++) {
9761 const struct tog_cmd *cmd = &tog_commands[i];
9762 fprintf(fp, " %s", cmd->name);
9768 usage(int hflag, int status)
9770 FILE *fp = (status == 0) ? stdout : stderr;
9772 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9775 fprintf(fp, "lazy usage: %s path\n", getprogname());
9782 make_argv(int argc, ...)
9790 argv = calloc(argc, sizeof(char *));
9793 for (i = 0; i < argc; i++) {
9794 argv[i] = strdup(va_arg(ap, char *));
9795 if (argv[i] == NULL)
9804 * Try to convert 'tog path' into a 'tog log path' command.
9805 * The user could simply have mistyped the command rather than knowingly
9806 * provided a path. So check whether argv[0] can in fact be resolved
9807 * to a path in the HEAD commit and print a special error if not.
9808 * This hack is for mpi@ <3
9810 static const struct got_error *
9811 tog_log_with_path(int argc, char *argv[])
9813 const struct got_error *error = NULL, *close_err;
9814 const struct tog_cmd *cmd = NULL;
9815 struct got_repository *repo = NULL;
9816 struct got_worktree *worktree = NULL;
9817 struct got_object_id *commit_id = NULL, *id = NULL;
9818 struct got_commit_object *commit = NULL;
9819 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9820 char *commit_id_str = NULL, **cmd_argv = NULL;
9821 int *pack_fds = NULL;
9823 cwd = getcwd(NULL, 0);
9825 return got_error_from_errno("getcwd");
9827 error = got_repo_pack_fds_open(&pack_fds);
9831 error = got_worktree_open(&worktree, cwd, NULL);
9832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9836 repo_path = strdup(got_worktree_get_repo_path(worktree));
9838 repo_path = strdup(cwd);
9839 if (repo_path == NULL) {
9840 error = got_error_from_errno("strdup");
9844 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9848 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9853 error = tog_load_refs(repo, 0);
9856 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9857 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9858 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9863 got_worktree_close(worktree);
9867 error = got_object_open_as_commit(&commit, repo, commit_id);
9871 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9873 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9875 fprintf(stderr, "%s: '%s' is no known command or path\n",
9876 getprogname(), argv[0]);
9881 error = got_object_id_str(&commit_id_str, commit_id);
9885 cmd = &tog_commands[0]; /* log */
9887 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9888 error = cmd->cmd_main(argc, cmd_argv);
9891 close_err = got_repo_close(repo);
9896 got_object_commit_close(commit);
9898 got_worktree_close(worktree);
9900 const struct got_error *pack_err =
9901 got_repo_pack_fds_close(pack_fds);
9906 free(commit_id_str);
9913 for (i = 0; i < argc; i++)
9922 main(int argc, char *argv[])
9924 const struct got_error *io_err, *error = NULL;
9925 const struct tog_cmd *cmd = NULL;
9926 int ch, hflag = 0, Vflag = 0;
9927 char **cmd_argv = NULL;
9928 static const struct option longopts[] = {
9929 { "version", no_argument, NULL, 'V' },
9932 char *diff_algo_str = NULL;
9933 const char *test_script_path;
9935 setlocale(LC_CTYPE, "");
9938 * Test mode init must happen before pledge() because "tty" will
9939 * not allow TTY-related ioctls to occur via regular files.
9941 test_script_path = getenv("TOG_TEST_SCRIPT");
9942 if (test_script_path != NULL) {
9943 error = init_mock_term(test_script_path);
9945 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9948 } else if (!isatty(STDIN_FILENO))
9949 errx(1, "standard input is not a tty");
9951 #if !defined(PROFILE)
9952 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9957 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9977 got_version_print_str();
9984 /* Build an argument vector which runs a default command. */
9985 cmd = &tog_commands[0];
9987 cmd_argv = make_argv(argc, cmd->name);
9991 /* Did the user specify a command? */
9992 for (i = 0; i < nitems(tog_commands); i++) {
9993 if (strncmp(tog_commands[i].name, argv[0],
9994 strlen(argv[0])) == 0) {
9995 cmd = &tog_commands[i];
10001 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10002 if (diff_algo_str) {
10003 if (strcasecmp(diff_algo_str, "patience") == 0)
10004 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10005 if (strcasecmp(diff_algo_str, "myers") == 0)
10006 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10012 /* No command specified; try log with a path */
10013 error = tog_log_with_path(argc, argv);
10018 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10021 if (using_mock_io) {
10022 io_err = tog_io_close();
10029 for (i = 0; i < argc; i++)
10034 if (error && error->code != GOT_ERR_CANCELLED &&
10035 error->code != GOT_ERR_EOF &&
10036 error->code != GOT_ERR_PRIVSEP_EXIT &&
10037 error->code != GOT_ERR_PRIVSEP_PIPE &&
10038 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10039 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);