2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_opentemp.h"
53 #include "got_cancel.h"
54 #include "got_commit_graph.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
58 #include "got_worktree.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define CTRL(x) ((x) & 0x1f)
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
80 __dead static void usage(int, int);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_ref(void);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct tog_cmd tog_commands[] = {
94 { "log", cmd_log, usage_log },
95 { "diff", cmd_diff, usage_diff },
96 { "blame", cmd_blame, usage_blame },
97 { "tree", cmd_tree, usage_tree },
98 { "ref", cmd_ref, usage_ref },
110 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 enum tog_keymap_type {
112 TOG_KEYMAP_KEYS = -2,
128 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 #define TOG_EOF_STRING "(END)"
132 struct commit_queue_entry {
133 TAILQ_ENTRY(commit_queue_entry) entry;
134 struct got_object_id *id;
135 struct got_commit_object *commit;
138 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 struct commit_queue {
141 struct commit_queue_head head;
145 STAILQ_ENTRY(tog_color) entry;
149 STAILQ_HEAD(tog_colors, tog_color);
151 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 static struct got_reflist_object_id_map *tog_refs_idmap;
153 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 static const struct got_error *
156 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 struct got_reference* re2)
159 const char *name1 = got_ref_get_name(re1);
160 const char *name2 = got_ref_get_name(re2);
161 int isbackup1, isbackup2;
163 /* Sort backup refs towards the bottom of the list. */
164 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 if (!isbackup1 && isbackup2) {
169 } else if (isbackup1 && !isbackup2) {
174 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
178 static const struct got_error *
179 tog_load_refs(struct got_repository *repo, int sort_by_date)
181 const struct got_error *err;
183 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
196 if (tog_refs_idmap) {
197 got_reflist_object_id_map_free(tog_refs_idmap);
198 tog_refs_idmap = NULL;
200 got_ref_list_free(&tog_refs);
203 static const struct got_error *
204 add_color(struct tog_colors *colors, const char *pattern,
205 int idx, short color)
207 const struct got_error *err = NULL;
208 struct tog_color *tc;
211 if (idx < 1 || idx > COLOR_PAIRS - 1)
214 init_pair(idx, color, -1);
216 tc = calloc(1, sizeof(*tc));
218 return got_error_from_errno("calloc");
219 regerr = regcomp(&tc->regex, pattern,
220 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
222 static char regerr_msg[512];
223 static char err_msg[512];
224 regerror(regerr, &tc->regex, regerr_msg,
226 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
228 err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 STAILQ_INSERT_HEAD(colors, tc, entry);
238 free_colors(struct tog_colors *colors)
240 struct tog_color *tc;
242 while (!STAILQ_EMPTY(colors)) {
243 tc = STAILQ_FIRST(colors);
244 STAILQ_REMOVE_HEAD(colors, entry);
250 static struct tog_color *
251 get_color(struct tog_colors *colors, int colorpair)
253 struct tog_color *tc = NULL;
255 STAILQ_FOREACH(tc, colors, entry) {
256 if (tc->colorpair == colorpair)
264 default_color_value(const char *envvar)
266 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
274 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
282 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
284 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
286 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
290 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 return COLOR_MAGENTA;
292 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
301 get_color_value(const char *envvar)
303 const char *val = getenv(envvar);
306 return default_color_value(envvar);
308 if (strcasecmp(val, "black") == 0)
310 if (strcasecmp(val, "red") == 0)
312 if (strcasecmp(val, "green") == 0)
314 if (strcasecmp(val, "yellow") == 0)
316 if (strcasecmp(val, "blue") == 0)
318 if (strcasecmp(val, "magenta") == 0)
319 return COLOR_MAGENTA;
320 if (strcasecmp(val, "cyan") == 0)
322 if (strcasecmp(val, "white") == 0)
324 if (strcasecmp(val, "default") == 0)
327 return default_color_value(envvar);
330 struct tog_diff_view_state {
331 struct got_object_id *id1, *id2;
332 const char *label1, *label2;
336 int first_displayed_line;
337 int last_displayed_line;
340 int ignore_whitespace;
342 struct got_repository *repo;
343 struct got_diff_line *lines;
348 /* passed from log or blame view; may be NULL */
349 struct tog_view *parent_view;
352 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 static volatile sig_atomic_t tog_thread_error;
355 struct tog_log_thread_args {
356 pthread_cond_t need_commits;
357 pthread_cond_t commit_loaded;
360 struct got_commit_graph *graph;
361 struct commit_queue *real_commits;
362 const char *in_repo_path;
363 struct got_object_id *start_id;
364 struct got_repository *repo;
368 struct commit_queue_entry **first_displayed_entry;
369 struct commit_queue_entry **selected_entry;
371 int *search_next_done;
375 regex_t *limit_regex;
376 struct commit_queue *limit_commits;
379 struct tog_log_view_state {
380 struct commit_queue *commits;
381 struct commit_queue_entry *first_displayed_entry;
382 struct commit_queue_entry *last_displayed_entry;
383 struct commit_queue_entry *selected_entry;
384 struct commit_queue real_commits;
389 struct got_repository *repo;
390 struct got_object_id *start_id;
393 struct tog_log_thread_args thread_args;
394 struct commit_queue_entry *matched_entry;
395 struct commit_queue_entry *search_entry;
396 struct tog_colors colors;
400 struct commit_queue limit_commits;
403 #define TOG_COLOR_DIFF_MINUS 1
404 #define TOG_COLOR_DIFF_PLUS 2
405 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 #define TOG_COLOR_DIFF_META 4
407 #define TOG_COLOR_TREE_SUBMODULE 5
408 #define TOG_COLOR_TREE_SYMLINK 6
409 #define TOG_COLOR_TREE_DIRECTORY 7
410 #define TOG_COLOR_TREE_EXECUTABLE 8
411 #define TOG_COLOR_COMMIT 9
412 #define TOG_COLOR_AUTHOR 10
413 #define TOG_COLOR_DATE 11
414 #define TOG_COLOR_REFS_HEADS 12
415 #define TOG_COLOR_REFS_TAGS 13
416 #define TOG_COLOR_REFS_REMOTES 14
417 #define TOG_COLOR_REFS_BACKUP 15
419 struct tog_blame_cb_args {
420 struct tog_blame_line *lines; /* one per line */
423 struct tog_view *view;
424 struct got_object_id *commit_id;
428 struct tog_blame_thread_args {
430 struct got_repository *repo;
431 struct tog_blame_cb_args *cb_args;
433 got_cancel_cb cancel_cb;
435 pthread_cond_t blame_complete;
441 struct tog_blame_line *lines;
445 struct tog_blame_thread_args thread_args;
446 struct tog_blame_cb_args cb_args;
451 struct tog_blame_view_state {
452 int first_displayed_line;
453 int last_displayed_line;
455 int last_diffed_line;
459 struct got_object_id_queue blamed_commits;
460 struct got_object_qid *blamed_commit;
462 struct got_repository *repo;
463 struct got_object_id *commit_id;
464 struct got_object_id *id_to_log;
465 struct tog_blame blame;
467 struct tog_colors colors;
470 struct tog_parent_tree {
471 TAILQ_ENTRY(tog_parent_tree) entry;
472 struct got_tree_object *tree;
473 struct got_tree_entry *first_displayed_entry;
474 struct got_tree_entry *selected_entry;
478 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
480 struct tog_tree_view_state {
482 struct got_object_id *commit_id;/* commit which this tree belongs to */
483 struct got_tree_object *root; /* the commit's root tree entry */
484 struct got_tree_object *tree; /* currently displayed (sub-)tree */
485 struct got_tree_entry *first_displayed_entry;
486 struct got_tree_entry *last_displayed_entry;
487 struct got_tree_entry *selected_entry;
488 int ndisplayed, selected, show_ids;
489 struct tog_parent_trees parents; /* parent trees of current sub-tree */
491 struct got_repository *repo;
492 struct got_tree_entry *matched_entry;
493 struct tog_colors colors;
496 struct tog_reflist_entry {
497 TAILQ_ENTRY(tog_reflist_entry) entry;
498 struct got_reference *ref;
502 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
504 struct tog_ref_view_state {
505 struct tog_reflist_head refs;
506 struct tog_reflist_entry *first_displayed_entry;
507 struct tog_reflist_entry *last_displayed_entry;
508 struct tog_reflist_entry *selected_entry;
509 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
510 struct got_repository *repo;
511 struct tog_reflist_entry *matched_entry;
512 struct tog_colors colors;
515 struct tog_help_view_state {
520 int first_displayed_line;
521 int last_displayed_line;
526 enum tog_keymap_type type;
529 #define GENERATE_HELP \
530 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
531 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
532 KEY_("k C-p Up", "Move cursor or page up one line"), \
533 KEY_("j C-n Down", "Move cursor or page down one line"), \
534 KEY_("C-b b PgUp", "Scroll the view up one page"), \
535 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
536 KEY_("C-u u", "Scroll the view up one half page"), \
537 KEY_("C-d d", "Scroll the view down one half page"), \
538 KEY_("g", "Go to line N (default: first line)"), \
539 KEY_("Home =", "Go to the first line"), \
540 KEY_("G", "Go to line N (default: last line)"), \
541 KEY_("End *", "Go to the last line"), \
542 KEY_("l Right", "Scroll the view right"), \
543 KEY_("h Left", "Scroll the view left"), \
544 KEY_("$", "Scroll view to the rightmost position"), \
545 KEY_("0", "Scroll view to the leftmost position"), \
546 KEY_("-", "Decrease size of the focussed split"), \
547 KEY_("+", "Increase size of the focussed split"), \
548 KEY_("Tab", "Switch focus between views"), \
549 KEY_("F", "Toggle fullscreen mode"), \
550 KEY_("S", "Switch split-screen layout"), \
551 KEY_("/", "Open prompt to enter search term"), \
552 KEY_("n", "Find next line/token matching the current search term"), \
553 KEY_("N", "Find previous line/token matching the current search term"),\
554 KEY_("q", "Quit the focussed view; Quit help screen"), \
555 KEY_("Q", "Quit tog"), \
557 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 KEY_("< ,", "Move cursor up one commit"), \
559 KEY_("> .", "Move cursor down one commit"), \
560 KEY_("Enter", "Open diff view of the selected commit"), \
561 KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 KEY_("R", "Open ref view of all repository references"), \
563 KEY_("T", "Display tree view of the repository from the selected" \
565 KEY_("@", "Toggle between displaying author and committer name"), \
566 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 KEY_("C-l", "Reload the log view with new commits in the repository"), \
570 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
576 KEY_("(", "Go to the previous file in the diff"), \
577 KEY_(")", "Go to the next file in the diff"), \
578 KEY_("{", "Go to the previous hunk in the diff"), \
579 KEY_("}", "Go to the next hunk in the diff"), \
580 KEY_("[", "Decrease the number of context lines"), \
581 KEY_("]", "Increase the number of context lines"), \
582 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
584 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 KEY_("Enter", "Display diff view of the selected line's commit"), \
586 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 KEY_("L", "Open log view for the currently selected annotated line"), \
588 KEY_("C", "Reload view with the previously blamed commit"), \
589 KEY_("c", "Reload view with the version of the file found in the" \
590 " selected line's commit"), \
591 KEY_("p", "Reload view with the version of the file found in the" \
592 " selected line's parent commit"), \
594 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 KEY_("Enter", "Enter selected directory or open blame view of the" \
597 KEY_("L", "Open log view for the selected entry"), \
598 KEY_("R", "Open ref view of all repository references"), \
599 KEY_("i", "Show object IDs for all tree entries"), \
600 KEY_("Backspace", "Return to the parent directory"), \
602 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 KEY_("Enter", "Display log view of the selected reference"), \
604 KEY_("T", "Display tree view of the selected reference"), \
605 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 KEY_("m", "Toggle display of last modified date for each reference"), \
607 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 KEY_("C-l", "Reload view with all repository references")
613 enum tog_keymap_type type;
616 /* curses io for tog regress */
623 static int using_mock_io;
625 #define TOG_KEY_SCRDUMP SHRT_MIN
628 * We implement two types of views: parent views and child views.
630 * The 'Tab' key switches focus between a parent view and its child view.
631 * Child views are shown side-by-side to their parent view, provided
632 * there is enough screen estate.
634 * When a new view is opened from within a parent view, this new view
635 * becomes a child view of the parent view, replacing any existing child.
637 * When a new view is opened from within a child view, this new view
638 * becomes a parent view which will obscure the views below until the
639 * user quits the new parent view by typing 'q'.
641 * This list of views contains parent views only.
642 * Child views are only pointed to by their parent view.
644 TAILQ_HEAD(tog_view_list_head, tog_view);
647 TAILQ_ENTRY(tog_view) entry;
650 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
651 int resized_y, resized_x; /* begin_y/x based on user resizing */
652 int maxx, x; /* max column and current start column */
653 int lines, cols; /* copies of LINES and COLS */
654 int nscrolled, offset; /* lines scrolled and hsplit line offset */
655 int gline, hiline; /* navigate to and highlight this nG line */
656 int ch, count; /* current keymap and count prefix */
657 int resized; /* set when in a resize event */
658 int focussed; /* Only set on one parent or child view at a time. */
660 struct tog_view *parent;
661 struct tog_view *child;
664 * This flag is initially set on parent views when a new child view
665 * is created. It gets toggled when the 'Tab' key switches focus
666 * between parent and child.
667 * The flag indicates whether focus should be passed on to our child
668 * view if this parent view gets picked for focus after another parent
669 * view was closed. This prevents child views from losing focus in such
674 enum tog_view_mode mode;
675 /* type-specific state */
676 enum tog_view_type type;
678 struct tog_diff_view_state diff;
679 struct tog_log_view_state log;
680 struct tog_blame_view_state blame;
681 struct tog_tree_view_state tree;
682 struct tog_ref_view_state ref;
683 struct tog_help_view_state help;
686 const struct got_error *(*show)(struct tog_view *);
687 const struct got_error *(*input)(struct tog_view **,
688 struct tog_view *, int);
689 const struct got_error *(*reset)(struct tog_view *);
690 const struct got_error *(*resize)(struct tog_view *, int);
691 const struct got_error *(*close)(struct tog_view *);
693 const struct got_error *(*search_start)(struct tog_view *);
694 const struct got_error *(*search_next)(struct tog_view *);
695 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
696 int **, int **, int **, int **);
699 #define TOG_SEARCH_FORWARD 1
700 #define TOG_SEARCH_BACKWARD 2
701 int search_next_done;
702 #define TOG_SEARCH_HAVE_MORE 1
703 #define TOG_SEARCH_NO_MORE 2
704 #define TOG_SEARCH_HAVE_NONE 3
710 static const struct got_error *open_diff_view(struct tog_view *,
711 struct got_object_id *, struct got_object_id *,
712 const char *, const char *, int, int, int, struct tog_view *,
713 struct got_repository *);
714 static const struct got_error *show_diff_view(struct tog_view *);
715 static const struct got_error *input_diff_view(struct tog_view **,
716 struct tog_view *, int);
717 static const struct got_error *reset_diff_view(struct tog_view *);
718 static const struct got_error* close_diff_view(struct tog_view *);
719 static const struct got_error *search_start_diff_view(struct tog_view *);
720 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
721 size_t *, int **, int **, int **, int **);
722 static const struct got_error *search_next_view_match(struct tog_view *);
724 static const struct got_error *open_log_view(struct tog_view *,
725 struct got_object_id *, struct got_repository *,
726 const char *, const char *, int);
727 static const struct got_error * show_log_view(struct tog_view *);
728 static const struct got_error *input_log_view(struct tog_view **,
729 struct tog_view *, int);
730 static const struct got_error *resize_log_view(struct tog_view *, int);
731 static const struct got_error *close_log_view(struct tog_view *);
732 static const struct got_error *search_start_log_view(struct tog_view *);
733 static const struct got_error *search_next_log_view(struct tog_view *);
735 static const struct got_error *open_blame_view(struct tog_view *, char *,
736 struct got_object_id *, struct got_repository *);
737 static const struct got_error *show_blame_view(struct tog_view *);
738 static const struct got_error *input_blame_view(struct tog_view **,
739 struct tog_view *, int);
740 static const struct got_error *reset_blame_view(struct tog_view *);
741 static const struct got_error *close_blame_view(struct tog_view *);
742 static const struct got_error *search_start_blame_view(struct tog_view *);
743 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
744 size_t *, int **, int **, int **, int **);
746 static const struct got_error *open_tree_view(struct tog_view *,
747 struct got_object_id *, const char *, struct got_repository *);
748 static const struct got_error *show_tree_view(struct tog_view *);
749 static const struct got_error *input_tree_view(struct tog_view **,
750 struct tog_view *, int);
751 static const struct got_error *close_tree_view(struct tog_view *);
752 static const struct got_error *search_start_tree_view(struct tog_view *);
753 static const struct got_error *search_next_tree_view(struct tog_view *);
755 static const struct got_error *open_ref_view(struct tog_view *,
756 struct got_repository *);
757 static const struct got_error *show_ref_view(struct tog_view *);
758 static const struct got_error *input_ref_view(struct tog_view **,
759 struct tog_view *, int);
760 static const struct got_error *close_ref_view(struct tog_view *);
761 static const struct got_error *search_start_ref_view(struct tog_view *);
762 static const struct got_error *search_next_ref_view(struct tog_view *);
764 static const struct got_error *open_help_view(struct tog_view *,
766 static const struct got_error *show_help_view(struct tog_view *);
767 static const struct got_error *input_help_view(struct tog_view **,
768 struct tog_view *, int);
769 static const struct got_error *reset_help_view(struct tog_view *);
770 static const struct got_error* close_help_view(struct tog_view *);
771 static const struct got_error *search_start_help_view(struct tog_view *);
772 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
773 size_t *, int **, int **, int **, int **);
775 static volatile sig_atomic_t tog_sigwinch_received;
776 static volatile sig_atomic_t tog_sigpipe_received;
777 static volatile sig_atomic_t tog_sigcont_received;
778 static volatile sig_atomic_t tog_sigint_received;
779 static volatile sig_atomic_t tog_sigterm_received;
782 tog_sigwinch(int signo)
784 tog_sigwinch_received = 1;
788 tog_sigpipe(int signo)
790 tog_sigpipe_received = 1;
794 tog_sigcont(int signo)
796 tog_sigcont_received = 1;
800 tog_sigint(int signo)
802 tog_sigint_received = 1;
806 tog_sigterm(int signo)
808 tog_sigterm_received = 1;
812 tog_fatal_signal_received(void)
814 return (tog_sigpipe_received ||
815 tog_sigint_received || tog_sigterm_received);
818 static const struct got_error *
819 view_close(struct tog_view *view)
821 const struct got_error *err = NULL, *child_err = NULL;
824 child_err = view_close(view->child);
828 err = view->close(view);
830 del_panel(view->panel);
832 delwin(view->window);
834 return err ? err : child_err;
837 static struct tog_view *
838 view_open(int nlines, int ncols, int begin_y, int begin_x,
839 enum tog_view_type type)
841 struct tog_view *view = calloc(1, sizeof(*view));
849 view->nlines = nlines ? nlines : LINES - begin_y;
850 view->ncols = ncols ? ncols : COLS - begin_x;
851 view->begin_y = begin_y;
852 view->begin_x = begin_x;
853 view->window = newwin(nlines, ncols, begin_y, begin_x);
854 if (view->window == NULL) {
858 view->panel = new_panel(view->window);
859 if (view->panel == NULL ||
860 set_panel_userptr(view->panel, view) != OK) {
865 keypad(view->window, TRUE);
870 view_split_begin_x(int begin_x)
872 if (begin_x > 0 || COLS < 120)
874 return (COLS - MAX(COLS / 2, 80));
877 /* XXX Stub till we decide what to do. */
879 view_split_begin_y(int lines)
881 return lines * HSPLIT_SCALE;
884 static const struct got_error *view_resize(struct tog_view *);
886 static const struct got_error *
887 view_splitscreen(struct tog_view *view)
889 const struct got_error *err = NULL;
891 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
892 if (view->resized_y && view->resized_y < view->lines)
893 view->begin_y = view->resized_y;
895 view->begin_y = view_split_begin_y(view->nlines);
897 } else if (!view->resized) {
898 if (view->resized_x && view->resized_x < view->cols - 1 &&
900 view->begin_x = view->resized_x;
902 view->begin_x = view_split_begin_x(0);
905 view->nlines = LINES - view->begin_y;
906 view->ncols = COLS - view->begin_x;
909 err = view_resize(view);
913 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
914 view->parent->nlines = view->begin_y;
916 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
917 return got_error_from_errno("mvwin");
922 static const struct got_error *
923 view_fullscreen(struct tog_view *view)
925 const struct got_error *err = NULL;
928 view->begin_y = view->resized ? view->begin_y : 0;
929 view->nlines = view->resized ? view->nlines : LINES;
933 err = view_resize(view);
937 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
938 return got_error_from_errno("mvwin");
944 view_is_parent_view(struct tog_view *view)
946 return view->parent == NULL;
950 view_is_splitscreen(struct tog_view *view)
952 return view->begin_x > 0 || view->begin_y > 0;
956 view_is_fullscreen(struct tog_view *view)
958 return view->nlines == LINES && view->ncols == COLS;
962 view_is_hsplit_top(struct tog_view *view)
964 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
965 view_is_splitscreen(view->child);
969 view_border(struct tog_view *view)
972 const struct tog_view *view_above;
975 return view_border(view->parent);
977 panel = panel_above(view->panel);
981 view_above = panel_userptr(panel);
982 if (view->mode == TOG_VIEW_SPLIT_HRZN)
983 mvwhline(view->window, view_above->begin_y - 1,
984 view->begin_x, ACS_HLINE, view->ncols);
986 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
987 ACS_VLINE, view->nlines);
990 static const struct got_error *view_init_hsplit(struct tog_view *, int);
991 static const struct got_error *request_log_commits(struct tog_view *);
992 static const struct got_error *offset_selection_down(struct tog_view *);
993 static void offset_selection_up(struct tog_view *);
994 static void view_get_split(struct tog_view *, int *, int *);
996 static const struct got_error *
997 view_resize(struct tog_view *view)
999 const struct got_error *err = NULL;
1000 int dif, nlines, ncols;
1002 dif = LINES - view->lines; /* line difference */
1004 if (view->lines > LINES)
1005 nlines = view->nlines - (view->lines - LINES);
1007 nlines = view->nlines + (LINES - view->lines);
1008 if (view->cols > COLS)
1009 ncols = view->ncols - (view->cols - COLS);
1011 ncols = view->ncols + (COLS - view->cols);
1014 int hs = view->child->begin_y;
1016 if (!view_is_fullscreen(view))
1017 view->child->begin_x = view_split_begin_x(view->begin_x);
1018 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1019 view->child->begin_x == 0) {
1022 view_fullscreen(view->child);
1023 if (view->child->focussed)
1024 show_panel(view->child->panel);
1026 show_panel(view->panel);
1028 ncols = view->child->begin_x;
1030 view_splitscreen(view->child);
1031 show_panel(view->child->panel);
1034 * XXX This is ugly and needs to be moved into the above
1035 * logic but "works" for now and my attempts at moving it
1036 * break either 'tab' or 'F' key maps in horizontal splits.
1039 err = view_splitscreen(view->child);
1042 if (dif < 0) { /* top split decreased */
1043 err = offset_selection_down(view);
1050 show_panel(view->child->panel);
1051 nlines = view->nlines;
1053 } else if (view->parent == NULL)
1056 if (view->resize && dif > 0) {
1057 err = view->resize(view, dif);
1062 if (wresize(view->window, nlines, ncols) == ERR)
1063 return got_error_from_errno("wresize");
1064 if (replace_panel(view->panel, view->window) == ERR)
1065 return got_error_from_errno("replace_panel");
1066 wclear(view->window);
1068 view->nlines = nlines;
1069 view->ncols = ncols;
1070 view->lines = LINES;
1076 static const struct got_error *
1077 resize_log_view(struct tog_view *view, int increase)
1079 struct tog_log_view_state *s = &view->state.log;
1080 const struct got_error *err = NULL;
1083 if (s->selected_entry)
1084 n = s->selected_entry->idx + view->lines - s->selected;
1087 * Request commits to account for the increased
1088 * height so we have enough to populate the view.
1090 if (s->commits->ncommits < n) {
1091 view->nscrolled = n - s->commits->ncommits + increase + 1;
1092 err = request_log_commits(view);
1099 view_adjust_offset(struct tog_view *view, int n)
1104 if (view->parent && view->parent->offset) {
1105 if (view->parent->offset + n >= 0)
1106 view->parent->offset += n;
1108 view->parent->offset = 0;
1109 } else if (view->offset) {
1110 if (view->offset - n >= 0)
1117 static const struct got_error *
1118 view_resize_split(struct tog_view *view, int resize)
1120 const struct got_error *err = NULL;
1121 struct tog_view *v = NULL;
1128 if (!v->child || !view_is_splitscreen(v->child))
1131 v->resized = v->child->resized = resize; /* lock for resize event */
1133 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1134 if (v->child->resized_y)
1135 v->child->begin_y = v->child->resized_y;
1137 v->child->begin_y -= resize;
1139 v->child->begin_y += resize;
1140 if (v->child->begin_y < 3) {
1142 v->child->begin_y = 3;
1143 } else if (v->child->begin_y > LINES - 1) {
1145 v->child->begin_y = LINES - 1;
1148 v->child->ncols = COLS;
1149 view_adjust_offset(view, resize);
1150 err = view_init_hsplit(v, v->child->begin_y);
1153 v->child->resized_y = v->child->begin_y;
1155 if (v->child->resized_x)
1156 v->child->begin_x = v->child->resized_x;
1158 v->child->begin_x -= resize;
1160 v->child->begin_x += resize;
1161 if (v->child->begin_x < 11) {
1163 v->child->begin_x = 11;
1164 } else if (v->child->begin_x > COLS - 1) {
1166 v->child->begin_x = COLS - 1;
1168 v->child->resized_x = v->child->begin_x;
1171 v->child->mode = v->mode;
1172 v->child->nlines = v->lines - v->child->begin_y;
1173 v->child->ncols = v->cols - v->child->begin_x;
1176 err = view_fullscreen(v);
1179 err = view_splitscreen(v->child);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1184 err = offset_selection_down(v->child);
1190 err = v->resize(v, 0);
1191 else if (v->child->resize)
1192 err = v->child->resize(v->child, 0);
1194 v->resized = v->child->resized = 0;
1200 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1202 struct tog_view *v = src->child ? src->child : src;
1204 dst->resized_x = v->resized_x;
1205 dst->resized_y = v->resized_y;
1208 static const struct got_error *
1209 view_close_child(struct tog_view *view)
1211 const struct got_error *err = NULL;
1213 if (view->child == NULL)
1216 err = view_close(view->child);
1221 static const struct got_error *
1222 view_set_child(struct tog_view *view, struct tog_view *child)
1224 const struct got_error *err = NULL;
1226 view->child = child;
1227 child->parent = view;
1229 err = view_resize(view);
1233 if (view->child->resized_x || view->child->resized_y)
1234 err = view_resize_split(view, 0);
1239 static const struct got_error *view_dispatch_request(struct tog_view **,
1240 struct tog_view *, enum tog_view_type, int, int);
1242 static const struct got_error *
1243 view_request_new(struct tog_view **requested, struct tog_view *view,
1244 enum tog_view_type request)
1246 struct tog_view *new_view = NULL;
1247 const struct got_error *err;
1252 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1253 view_get_split(view, &y, &x);
1255 err = view_dispatch_request(&new_view, view, request, y, x);
1259 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1260 request != TOG_VIEW_HELP) {
1261 err = view_init_hsplit(view, y);
1267 new_view->focussed = 1;
1268 new_view->mode = view->mode;
1269 new_view->nlines = request == TOG_VIEW_HELP ?
1270 view->lines : view->lines - y;
1272 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1273 view_transfer_size(new_view, view);
1274 err = view_close_child(view);
1277 err = view_set_child(view, new_view);
1280 view->focus_child = 1;
1282 *requested = new_view;
1288 tog_resizeterm(void)
1291 struct winsize size;
1293 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1294 cols = 80; /* Default */
1298 lines = size.ws_row;
1300 resize_term(lines, cols);
1303 static const struct got_error *
1304 view_search_start(struct tog_view *view, int fast_refresh)
1306 const struct got_error *err = NULL;
1307 struct tog_view *v = view;
1311 if (view->search_started) {
1312 regfree(&view->regex);
1313 view->searching = 0;
1314 memset(&view->regmatch, 0, sizeof(view->regmatch));
1316 view->search_started = 0;
1318 if (view->nlines < 1)
1321 if (view_is_hsplit_top(view))
1323 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1326 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1327 wclrtoeol(v->window);
1329 nodelay(v->window, FALSE); /* block for search term input */
1332 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1333 wrefresh(v->window);
1336 nodelay(v->window, TRUE);
1337 if (!fast_refresh && !using_mock_io)
1342 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1343 err = view->search_start(view);
1345 regfree(&view->regex);
1348 view->search_started = 1;
1349 view->searching = TOG_SEARCH_FORWARD;
1350 view->search_next_done = 0;
1351 view->search_next(view);
1357 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1358 static const struct got_error *
1359 switch_split(struct tog_view *view)
1361 const struct got_error *err = NULL;
1362 struct tog_view *v = NULL;
1369 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1370 v->mode = TOG_VIEW_SPLIT_VERT;
1372 v->mode = TOG_VIEW_SPLIT_HRZN;
1376 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1377 v->mode = TOG_VIEW_SPLIT_NONE;
1379 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1380 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1381 v->child->begin_y = v->child->resized_y;
1382 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1383 v->child->begin_x = v->child->resized_x;
1386 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1388 v->child->ncols = COLS;
1389 v->child->nscrolled = LINES - v->child->nlines;
1391 err = view_init_hsplit(v, v->child->begin_y);
1395 v->child->mode = v->mode;
1396 v->child->nlines = v->lines - v->child->begin_y;
1399 err = view_fullscreen(v);
1402 err = view_splitscreen(v->child);
1406 if (v->mode == TOG_VIEW_SPLIT_NONE)
1407 v->mode = TOG_VIEW_SPLIT_VERT;
1408 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1409 err = offset_selection_down(v);
1412 err = offset_selection_down(v->child);
1416 offset_selection_up(v);
1417 offset_selection_up(v->child);
1420 err = v->resize(v, 0);
1421 else if (v->child->resize)
1422 err = v->child->resize(v->child, 0);
1428 * Strip trailing whitespace from str starting at byte *n;
1429 * if *n < 0, use strlen(str). Return new str length in *n.
1432 strip_trailing_ws(char *str, int *n)
1436 if (str == NULL || *str == '\0')
1442 while (x-- > 0 && isspace((unsigned char)str[x]))
1449 * Extract visible substring of line y from the curses screen
1450 * and strip trailing whitespace. If vline is set, overwrite
1451 * line[vline] with '|' because the ACS_VLINE character is
1452 * written out as 'x'. Write the line to file f.
1454 static const struct got_error *
1455 view_write_line(FILE *f, int y, int vline)
1457 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1460 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1462 return got_error_fmt(GOT_ERR_RANGE,
1463 "failed to extract line %d", y);
1466 * In some views, lines are padded with blanks to COLS width.
1467 * Strip them so we can diff without the -b flag when testing.
1469 strip_trailing_ws(line, &r);
1474 w = fprintf(f, "%s\n", line);
1475 if (w != r + 1) /* \n */
1476 return got_ferror(f, GOT_ERR_IO);
1482 * Capture the visible curses screen by writing each line to the
1483 * file at the path set via the TOG_SCR_DUMP environment variable.
1485 static const struct got_error *
1486 screendump(struct tog_view *view)
1488 const struct got_error *err;
1493 path = getenv("TOG_SCR_DUMP");
1494 if (path == NULL || *path == '\0')
1495 return got_error_msg(GOT_ERR_BAD_PATH,
1496 "TOG_SCR_DUMP path not set to capture screen dump");
1497 f = fopen(path, "wex");
1499 return got_error_from_errno_fmt("fopen: %s", path);
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(f, 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)
1531 err = view_write_line(f, i, 0);
1538 if (f && fclose(f) == EOF && err == NULL)
1539 err = got_ferror(f, GOT_ERR_IO);
1544 * Compute view->count from numeric input. Assign total to view->count and
1545 * return first non-numeric key entered.
1548 get_compound_key(struct tog_view *view, int c)
1550 struct tog_view *v = view;
1553 if (view_is_hsplit_top(view))
1555 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1559 cbreak(); /* block for input */
1560 nodelay(view->window, FALSE);
1561 wmove(v->window, v->nlines - 1, 0);
1562 wclrtoeol(v->window);
1563 waddch(v->window, ':');
1566 x = getcurx(v->window);
1567 if (x != ERR && x < view->ncols) {
1568 waddch(v->window, c);
1569 wrefresh(v->window);
1573 * Don't overflow. Max valid request should be the greatest
1574 * between the longest and total lines; cap at 10 million.
1579 n = n * 10 + (c - '0');
1580 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1582 if (c == 'G' || c == 'g') { /* nG key map */
1583 view->gline = view->hiline = n;
1588 /* Massage excessive or inapplicable values at the input handler. */
1595 action_report(struct tog_view *view)
1597 struct tog_view *v = view;
1599 if (view_is_hsplit_top(view))
1601 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1604 wmove(v->window, v->nlines - 1, 0);
1605 wclrtoeol(v->window);
1606 wprintw(v->window, ":%s", view->action);
1607 wrefresh(v->window);
1610 * Clear action status report. Only clear in blame view
1611 * once annotating is complete, otherwise it's too fast.
1613 if (view->type == TOG_VIEW_BLAME) {
1614 if (view->state.blame.blame_complete)
1615 view->action = NULL;
1617 view->action = NULL;
1621 * Read the next line from the test script and assign
1622 * key instruction to *ch. If at EOF, set the *done flag.
1624 static const struct got_error *
1625 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1627 const struct got_error *err = NULL;
1631 if (view->count && --view->count) {
1637 if (getline(&line, &linesz, script) == -1) {
1642 err = got_ferror(script, GOT_ERR_IO);
1647 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1648 tog_io.wait_for_ui = 1;
1649 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1651 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1653 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1655 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1657 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1659 else if (strncasecmp(line, "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 err = offset_selection_down(view->parent);
1865 err = offset_selection_down(view);
1869 err = switch_split(view);
1872 err = view_resize_split(view, -1);
1875 err = view_resize_split(view, 1);
1881 if (view->search_start)
1882 view_search_start(view, fast_refresh);
1884 err = view->input(new, view, ch);
1888 if (view->search_started && view->search_next) {
1889 view->searching = (ch == 'n' ?
1890 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1891 view->search_next_done = 0;
1892 view->search_next(view);
1894 err = view->input(new, view, ch);
1897 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1898 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1899 view->action = "Patience diff algorithm";
1901 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1902 view->action = "Myers diff algorithm";
1904 TAILQ_FOREACH(v, views, entry) {
1910 if (v->child && v->child->reset) {
1911 err = v->child->reset(v->child);
1917 case TOG_KEY_SCRDUMP:
1918 err = screendump(view);
1921 err = view->input(new, view, ch);
1929 view_needs_focus_indication(struct tog_view *view)
1931 if (view_is_parent_view(view)) {
1932 if (view->child == NULL || view->child->focussed)
1934 if (!view_is_splitscreen(view->child))
1936 } else if (!view_is_splitscreen(view))
1939 return view->focussed;
1942 static const struct got_error *
1945 const struct got_error *err = NULL;
1947 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1948 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1949 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1950 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1951 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1952 err = got_ferror(tog_io.f, GOT_ERR_IO);
1957 static const struct got_error *
1958 view_loop(struct tog_view *view)
1960 const struct got_error *err = NULL;
1961 struct tog_view_list_head views;
1962 struct tog_view *new_view;
1964 int fast_refresh = 10;
1965 int done = 0, errcode;
1967 mode = getenv("TOG_VIEW_SPLIT_MODE");
1968 if (!mode || !(*mode == 'h' || *mode == 'H'))
1969 view->mode = TOG_VIEW_SPLIT_VERT;
1971 view->mode = TOG_VIEW_SPLIT_HRZN;
1973 errcode = pthread_mutex_lock(&tog_mutex);
1975 return got_error_set_errno(errcode, "pthread_mutex_lock");
1978 TAILQ_INSERT_HEAD(&views, view, entry);
1981 err = view->show(view);
1986 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1987 !tog_fatal_signal_received()) {
1988 /* Refresh fast during initialization, then become slower. */
1989 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1990 halfdelay(10); /* switch to once per second */
1992 err = view_input(&new_view, &done, view, &views, fast_refresh);
1996 if (view->dying && view == TAILQ_FIRST(&views) &&
1997 TAILQ_NEXT(view, entry) == NULL)
2003 * When we quit, scroll the screen up a single line
2004 * so we don't lose any information.
2006 TAILQ_FOREACH(v, &views, entry) {
2007 wmove(v->window, 0, 0);
2008 wdeleteln(v->window);
2009 wnoutrefresh(v->window);
2010 if (v->child && !view_is_fullscreen(v)) {
2011 wmove(v->child->window, 0, 0);
2012 wdeleteln(v->child->window);
2013 wnoutrefresh(v->child->window);
2020 struct tog_view *v, *prev = NULL;
2022 if (view_is_parent_view(view))
2023 prev = TAILQ_PREV(view, tog_view_list_head,
2025 else if (view->parent)
2026 prev = view->parent;
2029 view->parent->child = NULL;
2030 view->parent->focus_child = 0;
2031 /* Restore fullscreen line height. */
2032 view->parent->nlines = view->parent->lines;
2033 err = view_resize(view->parent);
2036 /* Make resized splits persist. */
2037 view_transfer_size(view->parent, view);
2039 TAILQ_REMOVE(&views, view, entry);
2041 err = view_close(view);
2046 TAILQ_FOREACH(v, &views, entry) {
2050 if (view == NULL && new_view == NULL) {
2051 /* No view has focus. Try to pick one. */
2054 else if (!TAILQ_EMPTY(&views)) {
2055 view = TAILQ_LAST(&views,
2056 tog_view_list_head);
2059 if (view->focus_child) {
2060 view->child->focussed = 1;
2068 struct tog_view *v, *t;
2069 /* Only allow one parent view per type. */
2070 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2071 if (v->type != new_view->type)
2073 TAILQ_REMOVE(&views, v, entry);
2074 err = view_close(v);
2079 TAILQ_INSERT_TAIL(&views, new_view, entry);
2082 if (view && !done) {
2083 if (view_is_parent_view(view)) {
2084 if (view->child && view->child->focussed)
2087 if (view->parent && view->parent->focussed)
2088 view = view->parent;
2090 show_panel(view->panel);
2091 if (view->child && view_is_splitscreen(view->child))
2092 show_panel(view->child->panel);
2093 if (view->parent && view_is_splitscreen(view)) {
2094 err = view->parent->show(view->parent);
2098 err = view->show(view);
2102 err = view->child->show(view->child);
2111 while (!TAILQ_EMPTY(&views)) {
2112 const struct got_error *close_err;
2113 view = TAILQ_FIRST(&views);
2114 TAILQ_REMOVE(&views, view, entry);
2115 close_err = view_close(view);
2116 if (close_err && err == NULL)
2120 errcode = pthread_mutex_unlock(&tog_mutex);
2121 if (errcode && err == NULL)
2122 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2132 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2137 /* Create newly allocated wide-character string equivalent to a byte string. */
2138 static const struct got_error *
2139 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2142 const struct got_error *err = NULL;
2145 *wlen = mbstowcs(NULL, s, 0);
2146 if (*wlen == (size_t)-1) {
2148 if (errno != EILSEQ)
2149 return got_error_from_errno("mbstowcs");
2151 /* byte string invalid in current encoding; try to "fix" it */
2152 err = got_mbsavis(&vis, &vislen, s);
2155 *wlen = mbstowcs(NULL, vis, 0);
2156 if (*wlen == (size_t)-1) {
2157 err = got_error_from_errno("mbstowcs"); /* give up */
2162 *ws = calloc(*wlen + 1, sizeof(**ws));
2164 err = got_error_from_errno("calloc");
2168 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2169 err = got_error_from_errno("mbstowcs");
2180 static const struct got_error *
2181 expand_tab(char **ptr, const char *src)
2184 size_t len, n, idx = 0, sz = 0;
2187 n = len = strlen(src);
2188 dst = malloc(n + 1);
2190 return got_error_from_errno("malloc");
2192 while (idx < len && src[idx]) {
2193 const char c = src[idx];
2196 size_t nb = TABSIZE - sz % TABSIZE;
2199 p = realloc(dst, n + nb);
2202 return got_error_from_errno("realloc");
2207 memset(dst + sz, ' ', nb);
2210 dst[sz++] = src[idx];
2220 * Advance at most n columns from wline starting at offset off.
2221 * Return the index to the first character after the span operation.
2222 * Return the combined column width of all spanned wide character in
2226 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2228 int width, i, cols = 0;
2235 for (i = off; wline[i] != L'\0'; ++i) {
2236 if (wline[i] == L'\t')
2237 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2239 width = wcwidth(wline[i]);
2246 if (cols + width > n)
2256 * Format a line for display, ensuring that it won't overflow a width limit.
2257 * With scrolling, the width returned refers to the scrolled version of the
2258 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2260 static const struct got_error *
2261 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2262 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2264 const struct got_error *err = NULL;
2266 wchar_t *wline = NULL;
2275 err = expand_tab(&exstr, line);
2280 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2285 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2287 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2288 wline[wlen - 1] = L'\0';
2291 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2292 wline[wlen - 1] = L'\0';
2296 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2302 *scrollxp = scrollx;
2310 static const struct got_error*
2311 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2312 struct got_object_id *id, struct got_repository *repo)
2314 static const struct got_error *err = NULL;
2315 struct got_reflist_entry *re;
2321 TAILQ_FOREACH(re, refs, entry) {
2322 struct got_tag_object *tag = NULL;
2323 struct got_object_id *ref_id;
2326 name = got_ref_get_name(re->ref);
2327 if (strcmp(name, GOT_REF_HEAD) == 0)
2329 if (strncmp(name, "refs/", 5) == 0)
2331 if (strncmp(name, "got/", 4) == 0 &&
2332 strncmp(name, "got/backup/", 11) != 0)
2334 if (strncmp(name, "heads/", 6) == 0)
2336 if (strncmp(name, "remotes/", 8) == 0) {
2338 s = strstr(name, "/" GOT_REF_HEAD);
2339 if (s != NULL && s[strlen(s)] == '\0')
2342 err = got_ref_resolve(&ref_id, repo, re->ref);
2345 if (strncmp(name, "tags/", 5) == 0) {
2346 err = got_object_open_as_tag(&tag, repo, ref_id);
2348 if (err->code != GOT_ERR_OBJ_TYPE) {
2352 /* Ref points at something other than a tag. */
2357 cmp = got_object_id_cmp(tag ?
2358 got_object_tag_get_object_id(tag) : ref_id, id);
2361 got_object_tag_close(tag);
2365 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2366 s ? ", " : "", name) == -1) {
2367 err = got_error_from_errno("asprintf");
2378 static const struct got_error *
2379 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2384 smallerthan = strchr(author, '<');
2385 if (smallerthan && smallerthan[1] != '\0')
2386 author = smallerthan + 1;
2387 author[strcspn(author, "@>")] = '\0';
2388 return format_line(wauthor, author_width, NULL, author, 0, limit,
2392 static const struct got_error *
2393 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2394 struct got_object_id *id, const size_t date_display_cols,
2395 int author_display_cols)
2397 struct tog_log_view_state *s = &view->state.log;
2398 const struct got_error *err = NULL;
2399 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2400 char *logmsg0 = NULL, *logmsg = NULL;
2401 char *author = NULL;
2402 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2403 int author_width, logmsg_width;
2404 char *newline, *line = NULL;
2405 int col, limit, scrollx;
2406 const int avail = view->ncols;
2408 time_t committer_time;
2409 struct tog_color *tc;
2411 committer_time = got_object_commit_get_committer_time(commit);
2412 if (gmtime_r(&committer_time, &tm) == NULL)
2413 return got_error_from_errno("gmtime_r");
2414 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2415 return got_error(GOT_ERR_NO_SPACE);
2417 if (avail <= date_display_cols)
2418 limit = MIN(sizeof(datebuf) - 1, avail);
2420 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2421 tc = get_color(&s->colors, TOG_COLOR_DATE);
2423 wattr_on(view->window,
2424 COLOR_PAIR(tc->colorpair), NULL);
2425 waddnstr(view->window, datebuf, limit);
2427 wattr_off(view->window,
2428 COLOR_PAIR(tc->colorpair), NULL);
2435 err = got_object_id_str(&id_str, id);
2438 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2440 wattr_on(view->window,
2441 COLOR_PAIR(tc->colorpair), NULL);
2442 wprintw(view->window, "%.8s ", id_str);
2444 wattr_off(view->window,
2445 COLOR_PAIR(tc->colorpair), NULL);
2452 if (s->use_committer)
2453 author = strdup(got_object_commit_get_committer(commit));
2455 author = strdup(got_object_commit_get_author(commit));
2456 if (author == NULL) {
2457 err = got_error_from_errno("strdup");
2460 err = format_author(&wauthor, &author_width, author, avail - col, col);
2463 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2465 wattr_on(view->window,
2466 COLOR_PAIR(tc->colorpair), NULL);
2467 waddwstr(view->window, wauthor);
2468 col += author_width;
2469 while (col < avail && author_width < author_display_cols + 2) {
2470 waddch(view->window, ' ');
2475 wattr_off(view->window,
2476 COLOR_PAIR(tc->colorpair), NULL);
2480 err = got_object_commit_get_logmsg(&logmsg0, commit);
2484 while (*logmsg == '\n')
2486 newline = strchr(logmsg, '\n');
2489 limit = avail - col;
2490 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2491 limit--; /* for the border */
2492 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2496 waddwstr(view->window, &wlogmsg[scrollx]);
2497 col += MAX(logmsg_width, 0);
2498 while (col < avail) {
2499 waddch(view->window, ' ');
2511 static struct commit_queue_entry *
2512 alloc_commit_queue_entry(struct got_commit_object *commit,
2513 struct got_object_id *id)
2515 struct commit_queue_entry *entry;
2516 struct got_object_id *dup;
2518 entry = calloc(1, sizeof(*entry));
2522 dup = got_object_id_dup(id);
2529 entry->commit = commit;
2534 pop_commit(struct commit_queue *commits)
2536 struct commit_queue_entry *entry;
2538 entry = TAILQ_FIRST(&commits->head);
2539 TAILQ_REMOVE(&commits->head, entry, entry);
2540 got_object_commit_close(entry->commit);
2541 commits->ncommits--;
2547 free_commits(struct commit_queue *commits)
2549 while (!TAILQ_EMPTY(&commits->head))
2550 pop_commit(commits);
2553 static const struct got_error *
2554 match_commit(int *have_match, struct got_object_id *id,
2555 struct got_commit_object *commit, regex_t *regex)
2557 const struct got_error *err = NULL;
2558 regmatch_t regmatch;
2559 char *id_str = NULL, *logmsg = NULL;
2563 err = got_object_id_str(&id_str, id);
2567 err = got_object_commit_get_logmsg(&logmsg, commit);
2571 if (regexec(regex, got_object_commit_get_author(commit), 1,
2572 ®match, 0) == 0 ||
2573 regexec(regex, got_object_commit_get_committer(commit), 1,
2574 ®match, 0) == 0 ||
2575 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2576 regexec(regex, logmsg, 1, ®match, 0) == 0)
2584 static const struct got_error *
2585 queue_commits(struct tog_log_thread_args *a)
2587 const struct got_error *err = NULL;
2590 * We keep all commits open throughout the lifetime of the log
2591 * view in order to avoid having to re-fetch commits from disk
2592 * while updating the display.
2595 struct got_object_id id;
2596 struct got_commit_object *commit;
2597 struct commit_queue_entry *entry;
2598 int limit_match = 0;
2601 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2606 err = got_object_open_as_commit(&commit, a->repo, &id);
2609 entry = alloc_commit_queue_entry(commit, &id);
2610 if (entry == NULL) {
2611 err = got_error_from_errno("alloc_commit_queue_entry");
2615 errcode = pthread_mutex_lock(&tog_mutex);
2617 err = got_error_set_errno(errcode,
2618 "pthread_mutex_lock");
2622 entry->idx = a->real_commits->ncommits;
2623 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2624 a->real_commits->ncommits++;
2627 err = match_commit(&limit_match, &id, commit,
2633 struct commit_queue_entry *matched;
2635 matched = alloc_commit_queue_entry(
2636 entry->commit, entry->id);
2637 if (matched == NULL) {
2638 err = got_error_from_errno(
2639 "alloc_commit_queue_entry");
2642 matched->commit = entry->commit;
2643 got_object_commit_retain(entry->commit);
2645 matched->idx = a->limit_commits->ncommits;
2646 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2648 a->limit_commits->ncommits++;
2652 * This is how we signal log_thread() that we
2653 * have found a match, and that it should be
2654 * counted as a new entry for the view.
2656 a->limit_match = limit_match;
2659 if (*a->searching == TOG_SEARCH_FORWARD &&
2660 !*a->search_next_done) {
2662 err = match_commit(&have_match, &id, commit, a->regex);
2667 if (limit_match && have_match)
2668 *a->search_next_done =
2669 TOG_SEARCH_HAVE_MORE;
2670 } else if (have_match)
2671 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2674 errcode = pthread_mutex_unlock(&tog_mutex);
2675 if (errcode && err == NULL)
2676 err = got_error_set_errno(errcode,
2677 "pthread_mutex_unlock");
2680 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2686 select_commit(struct tog_log_view_state *s)
2688 struct commit_queue_entry *entry;
2691 entry = s->first_displayed_entry;
2693 if (ncommits == s->selected) {
2694 s->selected_entry = entry;
2697 entry = TAILQ_NEXT(entry, entry);
2702 static const struct got_error *
2703 draw_commits(struct tog_view *view)
2705 const struct got_error *err = NULL;
2706 struct tog_log_view_state *s = &view->state.log;
2707 struct commit_queue_entry *entry = s->selected_entry;
2708 int limit = view->nlines;
2710 int ncommits, author_cols = 4;
2711 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2712 char *refs_str = NULL;
2714 struct tog_color *tc;
2715 static const size_t date_display_cols = 12;
2717 if (view_is_hsplit_top(view))
2718 --limit; /* account for border */
2720 if (s->selected_entry &&
2721 !(view->searching && view->search_next_done == 0)) {
2722 struct got_reflist_head *refs;
2723 err = got_object_id_str(&id_str, s->selected_entry->id);
2726 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2727 s->selected_entry->id);
2729 err = build_refs_str(&refs_str, refs,
2730 s->selected_entry->id, s->repo);
2736 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2737 halfdelay(10); /* disable fast refresh */
2739 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2740 if (asprintf(&ncommits_str, " [%d/%d] %s",
2741 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2742 (view->searching && !view->search_next_done) ?
2743 "searching..." : "loading...") == -1) {
2744 err = got_error_from_errno("asprintf");
2748 const char *search_str = NULL;
2749 const char *limit_str = NULL;
2751 if (view->searching) {
2752 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2753 search_str = "no more matches";
2754 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2755 search_str = "no matches found";
2756 else if (!view->search_next_done)
2757 search_str = "searching...";
2760 if (s->limit_view && s->commits->ncommits == 0)
2761 limit_str = "no matches found";
2763 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2764 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2765 search_str ? search_str : (refs_str ? refs_str : ""),
2766 limit_str ? limit_str : "") == -1) {
2767 err = got_error_from_errno("asprintf");
2772 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2773 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2774 "........................................",
2775 s->in_repo_path, ncommits_str) == -1) {
2776 err = got_error_from_errno("asprintf");
2780 } else if (asprintf(&header, "commit %s%s",
2781 id_str ? id_str : "........................................",
2782 ncommits_str) == -1) {
2783 err = got_error_from_errno("asprintf");
2787 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2791 werase(view->window);
2793 if (view_needs_focus_indication(view))
2794 wstandout(view->window);
2795 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2797 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2798 waddwstr(view->window, wline);
2799 while (width < view->ncols) {
2800 waddch(view->window, ' ');
2804 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2805 if (view_needs_focus_indication(view))
2806 wstandend(view->window);
2811 /* Grow author column size if necessary, and set view->maxx. */
2812 entry = s->first_displayed_entry;
2816 struct got_commit_object *c = entry->commit;
2817 char *author, *eol, *msg, *msg0;
2818 wchar_t *wauthor, *wmsg;
2820 if (ncommits >= limit - 1)
2822 if (s->use_committer)
2823 author = strdup(got_object_commit_get_committer(c));
2825 author = strdup(got_object_commit_get_author(c));
2826 if (author == NULL) {
2827 err = got_error_from_errno("strdup");
2830 err = format_author(&wauthor, &width, author, COLS,
2832 if (author_cols < width)
2833 author_cols = width;
2838 err = got_object_commit_get_logmsg(&msg0, c);
2842 while (*msg == '\n')
2844 if ((eol = strchr(msg, '\n')))
2846 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2847 date_display_cols + author_cols, 0);
2850 view->maxx = MAX(view->maxx, width);
2854 entry = TAILQ_NEXT(entry, entry);
2857 entry = s->first_displayed_entry;
2858 s->last_displayed_entry = s->first_displayed_entry;
2861 if (ncommits >= limit - 1)
2863 if (ncommits == s->selected)
2864 wstandout(view->window);
2865 err = draw_commit(view, entry->commit, entry->id,
2866 date_display_cols, author_cols);
2867 if (ncommits == s->selected)
2868 wstandend(view->window);
2872 s->last_displayed_entry = entry;
2873 entry = TAILQ_NEXT(entry, entry);
2886 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2888 struct commit_queue_entry *entry;
2891 entry = TAILQ_FIRST(&s->commits->head);
2892 if (s->first_displayed_entry == entry)
2895 entry = s->first_displayed_entry;
2896 while (entry && nscrolled < maxscroll) {
2897 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2899 s->first_displayed_entry = entry;
2905 static const struct got_error *
2906 trigger_log_thread(struct tog_view *view, int wait)
2908 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2912 halfdelay(1); /* fast refresh while loading commits */
2914 while (!ta->log_complete && !tog_thread_error &&
2915 (ta->commits_needed > 0 || ta->load_all)) {
2916 /* Wake the log thread. */
2917 errcode = pthread_cond_signal(&ta->need_commits);
2919 return got_error_set_errno(errcode,
2920 "pthread_cond_signal");
2923 * The mutex will be released while the view loop waits
2924 * in wgetch(), at which time the log thread will run.
2929 /* Display progress update in log view. */
2930 show_log_view(view);
2934 /* Wait right here while next commit is being loaded. */
2935 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2937 return got_error_set_errno(errcode,
2938 "pthread_cond_wait");
2940 /* Display progress update in log view. */
2941 show_log_view(view);
2949 static const struct got_error *
2950 request_log_commits(struct tog_view *view)
2952 struct tog_log_view_state *state = &view->state.log;
2953 const struct got_error *err = NULL;
2955 if (state->thread_args.log_complete)
2958 state->thread_args.commits_needed += view->nscrolled;
2959 err = trigger_log_thread(view, 1);
2960 view->nscrolled = 0;
2965 static const struct got_error *
2966 log_scroll_down(struct tog_view *view, int maxscroll)
2968 struct tog_log_view_state *s = &view->state.log;
2969 const struct got_error *err = NULL;
2970 struct commit_queue_entry *pentry;
2971 int nscrolled = 0, ncommits_needed;
2973 if (s->last_displayed_entry == NULL)
2976 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2977 if (s->commits->ncommits < ncommits_needed &&
2978 !s->thread_args.log_complete) {
2980 * Ask the log thread for required amount of commits.
2982 s->thread_args.commits_needed +=
2983 ncommits_needed - s->commits->ncommits;
2984 err = trigger_log_thread(view, 1);
2990 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2991 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2994 s->last_displayed_entry = pentry ?
2995 pentry : s->last_displayed_entry;
2997 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3000 s->first_displayed_entry = pentry;
3001 } while (++nscrolled < maxscroll);
3003 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3004 view->nscrolled += nscrolled;
3006 view->nscrolled = 0;
3011 static const struct got_error *
3012 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3013 struct got_commit_object *commit, struct got_object_id *commit_id,
3014 struct tog_view *log_view, struct got_repository *repo)
3016 const struct got_error *err;
3017 struct got_object_qid *parent_id;
3018 struct tog_view *diff_view;
3020 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3021 if (diff_view == NULL)
3022 return got_error_from_errno("view_open");
3024 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3025 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3026 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3028 *new_view = diff_view;
3032 static const struct got_error *
3033 tree_view_visit_subtree(struct tog_tree_view_state *s,
3034 struct got_tree_object *subtree)
3036 struct tog_parent_tree *parent;
3038 parent = calloc(1, sizeof(*parent));
3040 return got_error_from_errno("calloc");
3042 parent->tree = s->tree;
3043 parent->first_displayed_entry = s->first_displayed_entry;
3044 parent->selected_entry = s->selected_entry;
3045 parent->selected = s->selected;
3046 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3049 s->first_displayed_entry = NULL;
3053 static const struct got_error *
3054 tree_view_walk_path(struct tog_tree_view_state *s,
3055 struct got_commit_object *commit, const char *path)
3057 const struct got_error *err = NULL;
3058 struct got_tree_object *tree = NULL;
3060 char *slash, *subpath = NULL;
3062 /* Walk the path and open corresponding tree objects. */
3065 struct got_tree_entry *te;
3066 struct got_object_id *tree_id;
3072 /* Ensure the correct subtree entry is selected. */
3073 slash = strchr(p, '/');
3075 te_name = strdup(p);
3077 te_name = strndup(p, slash - p);
3078 if (te_name == NULL) {
3079 err = got_error_from_errno("strndup");
3082 te = got_object_tree_find_entry(s->tree, te_name);
3084 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3089 s->first_displayed_entry = s->selected_entry = te;
3091 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3092 break; /* jump to this file's entry */
3094 slash = strchr(p, '/');
3096 subpath = strndup(path, slash - path);
3098 subpath = strdup(path);
3099 if (subpath == NULL) {
3100 err = got_error_from_errno("strdup");
3104 err = got_object_id_by_path(&tree_id, s->repo, commit,
3109 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3114 err = tree_view_visit_subtree(s, tree);
3116 got_object_tree_close(tree);
3130 static const struct got_error *
3131 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3132 struct commit_queue_entry *entry, const char *path,
3133 const char *head_ref_name, struct got_repository *repo)
3135 const struct got_error *err = NULL;
3136 struct tog_tree_view_state *s;
3137 struct tog_view *tree_view;
3139 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3140 if (tree_view == NULL)
3141 return got_error_from_errno("view_open");
3143 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3146 s = &tree_view->state.tree;
3148 *new_view = tree_view;
3150 if (got_path_is_root_dir(path))
3153 return tree_view_walk_path(s, entry->commit, path);
3156 static const struct got_error *
3157 block_signals_used_by_main_thread(void)
3162 if (sigemptyset(&sigset) == -1)
3163 return got_error_from_errno("sigemptyset");
3165 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3166 if (sigaddset(&sigset, SIGWINCH) == -1)
3167 return got_error_from_errno("sigaddset");
3168 if (sigaddset(&sigset, SIGCONT) == -1)
3169 return got_error_from_errno("sigaddset");
3170 if (sigaddset(&sigset, SIGINT) == -1)
3171 return got_error_from_errno("sigaddset");
3172 if (sigaddset(&sigset, SIGTERM) == -1)
3173 return got_error_from_errno("sigaddset");
3175 /* ncurses handles SIGTSTP */
3176 if (sigaddset(&sigset, SIGTSTP) == -1)
3177 return got_error_from_errno("sigaddset");
3179 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3181 return got_error_set_errno(errcode, "pthread_sigmask");
3187 log_thread(void *arg)
3189 const struct got_error *err = NULL;
3191 struct tog_log_thread_args *a = arg;
3195 * Sync startup with main thread such that we begin our
3196 * work once view_input() has released the mutex.
3198 errcode = pthread_mutex_lock(&tog_mutex);
3200 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3204 err = block_signals_used_by_main_thread();
3206 pthread_mutex_unlock(&tog_mutex);
3210 while (!done && !err && !tog_fatal_signal_received()) {
3211 errcode = pthread_mutex_unlock(&tog_mutex);
3213 err = got_error_set_errno(errcode,
3214 "pthread_mutex_unlock");
3217 err = queue_commits(a);
3219 if (err->code != GOT_ERR_ITER_COMPLETED)
3223 } else if (a->commits_needed > 0 && !a->load_all) {
3226 a->commits_needed--;
3228 a->commits_needed--;
3231 errcode = pthread_mutex_lock(&tog_mutex);
3233 err = got_error_set_errno(errcode,
3234 "pthread_mutex_lock");
3236 } else if (*a->quit)
3238 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3239 *a->first_displayed_entry =
3240 TAILQ_FIRST(&a->limit_commits->head);
3241 *a->selected_entry = *a->first_displayed_entry;
3242 } else if (*a->first_displayed_entry == NULL) {
3243 *a->first_displayed_entry =
3244 TAILQ_FIRST(&a->real_commits->head);
3245 *a->selected_entry = *a->first_displayed_entry;
3248 errcode = pthread_cond_signal(&a->commit_loaded);
3250 err = got_error_set_errno(errcode,
3251 "pthread_cond_signal");
3252 pthread_mutex_unlock(&tog_mutex);
3257 a->commits_needed = 0;
3259 if (a->commits_needed == 0 && !a->load_all) {
3260 errcode = pthread_cond_wait(&a->need_commits,
3263 err = got_error_set_errno(errcode,
3264 "pthread_cond_wait");
3265 pthread_mutex_unlock(&tog_mutex);
3273 a->log_complete = 1;
3274 errcode = pthread_mutex_unlock(&tog_mutex);
3276 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3279 tog_thread_error = 1;
3280 pthread_cond_signal(&a->commit_loaded);
3285 static const struct got_error *
3286 stop_log_thread(struct tog_log_view_state *s)
3288 const struct got_error *err = NULL, *thread_err = NULL;
3293 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3295 return got_error_set_errno(errcode,
3296 "pthread_cond_signal");
3297 errcode = pthread_mutex_unlock(&tog_mutex);
3299 return got_error_set_errno(errcode,
3300 "pthread_mutex_unlock");
3301 errcode = pthread_join(s->thread, (void **)&thread_err);
3303 return got_error_set_errno(errcode, "pthread_join");
3304 errcode = pthread_mutex_lock(&tog_mutex);
3306 return got_error_set_errno(errcode,
3307 "pthread_mutex_lock");
3311 if (s->thread_args.repo) {
3312 err = got_repo_close(s->thread_args.repo);
3313 s->thread_args.repo = NULL;
3316 if (s->thread_args.pack_fds) {
3317 const struct got_error *pack_err =
3318 got_repo_pack_fds_close(s->thread_args.pack_fds);
3321 s->thread_args.pack_fds = NULL;
3324 if (s->thread_args.graph) {
3325 got_commit_graph_close(s->thread_args.graph);
3326 s->thread_args.graph = NULL;
3329 return err ? err : thread_err;
3332 static const struct got_error *
3333 close_log_view(struct tog_view *view)
3335 const struct got_error *err = NULL;
3336 struct tog_log_view_state *s = &view->state.log;
3339 err = stop_log_thread(s);
3341 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3342 if (errcode && err == NULL)
3343 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3345 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3346 if (errcode && err == NULL)
3347 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3349 free_commits(&s->limit_commits);
3350 free_commits(&s->real_commits);
3351 free(s->in_repo_path);
3352 s->in_repo_path = NULL;
3355 free(s->head_ref_name);
3356 s->head_ref_name = NULL;
3361 * We use two queues to implement the limit feature: first consists of
3362 * commits matching the current limit_regex; second is the real queue
3363 * of all known commits (real_commits). When the user starts limiting,
3364 * we swap queues such that all movement and displaying functionality
3365 * works with very slight change.
3367 static const struct got_error *
3368 limit_log_view(struct tog_view *view)
3370 struct tog_log_view_state *s = &view->state.log;
3371 struct commit_queue_entry *entry;
3372 struct tog_view *v = view;
3373 const struct got_error *err = NULL;
3377 if (view_is_hsplit_top(view))
3379 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3382 /* Get the pattern */
3383 wmove(v->window, v->nlines - 1, 0);
3384 wclrtoeol(v->window);
3385 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3386 nodelay(v->window, FALSE);
3389 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3392 nodelay(v->window, TRUE);
3396 if (*pattern == '\0') {
3398 * Safety measure for the situation where the user
3399 * resets limit without previously limiting anything.
3405 * User could have pressed Ctrl+L, which refreshed the
3406 * commit queues, it means we can't save previously
3407 * (before limit took place) displayed entries,
3408 * because they would point to already free'ed memory,
3409 * so we are forced to always select first entry of
3412 s->commits = &s->real_commits;
3413 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3414 s->selected_entry = s->first_displayed_entry;
3421 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3426 /* Clear the screen while loading limit view */
3427 s->first_displayed_entry = NULL;
3428 s->last_displayed_entry = NULL;
3429 s->selected_entry = NULL;
3430 s->commits = &s->limit_commits;
3432 /* Prepare limit queue for new search */
3433 free_commits(&s->limit_commits);
3434 s->limit_commits.ncommits = 0;
3436 /* First process commits, which are in queue already */
3437 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3440 err = match_commit(&have_match, entry->id,
3441 entry->commit, &s->limit_regex);
3446 struct commit_queue_entry *matched;
3448 matched = alloc_commit_queue_entry(entry->commit,
3450 if (matched == NULL) {
3451 err = got_error_from_errno(
3452 "alloc_commit_queue_entry");
3455 matched->commit = entry->commit;
3456 got_object_commit_retain(entry->commit);
3458 matched->idx = s->limit_commits.ncommits;
3459 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3461 s->limit_commits.ncommits++;
3465 /* Second process all the commits, until we fill the screen */
3466 if (s->limit_commits.ncommits < view->nlines - 1 &&
3467 !s->thread_args.log_complete) {
3468 s->thread_args.commits_needed +=
3469 view->nlines - s->limit_commits.ncommits - 1;
3470 err = trigger_log_thread(view, 1);
3475 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3476 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3482 static const struct got_error *
3483 search_start_log_view(struct tog_view *view)
3485 struct tog_log_view_state *s = &view->state.log;
3487 s->matched_entry = NULL;
3488 s->search_entry = NULL;
3492 static const struct got_error *
3493 search_next_log_view(struct tog_view *view)
3495 const struct got_error *err = NULL;
3496 struct tog_log_view_state *s = &view->state.log;
3497 struct commit_queue_entry *entry;
3499 /* Display progress update in log view. */
3500 show_log_view(view);
3504 if (s->search_entry) {
3506 errcode = pthread_mutex_unlock(&tog_mutex);
3508 return got_error_set_errno(errcode,
3509 "pthread_mutex_unlock");
3510 ch = wgetch(view->window);
3511 errcode = pthread_mutex_lock(&tog_mutex);
3513 return got_error_set_errno(errcode,
3514 "pthread_mutex_lock");
3515 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3516 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3519 if (view->searching == TOG_SEARCH_FORWARD)
3520 entry = TAILQ_NEXT(s->search_entry, entry);
3522 entry = TAILQ_PREV(s->search_entry,
3523 commit_queue_head, entry);
3524 } else if (s->matched_entry) {
3526 * If the user has moved the cursor after we hit a match,
3527 * the position from where we should continue searching
3528 * might have changed.
3530 if (view->searching == TOG_SEARCH_FORWARD)
3531 entry = TAILQ_NEXT(s->selected_entry, entry);
3533 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3536 entry = s->selected_entry;
3542 if (entry == NULL) {
3543 if (s->thread_args.log_complete ||
3544 view->searching == TOG_SEARCH_BACKWARD) {
3545 view->search_next_done =
3546 (s->matched_entry == NULL ?
3547 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3548 s->search_entry = NULL;
3552 * Poke the log thread for more commits and return,
3553 * allowing the main loop to make progress. Search
3554 * will resume at s->search_entry once we come back.
3556 s->thread_args.commits_needed++;
3557 return trigger_log_thread(view, 0);
3560 err = match_commit(&have_match, entry->id, entry->commit,
3565 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3566 s->matched_entry = entry;
3570 s->search_entry = entry;
3571 if (view->searching == TOG_SEARCH_FORWARD)
3572 entry = TAILQ_NEXT(entry, entry);
3574 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3577 if (s->matched_entry) {
3578 int cur = s->selected_entry->idx;
3579 while (cur < s->matched_entry->idx) {
3580 err = input_log_view(NULL, view, KEY_DOWN);
3585 while (cur > s->matched_entry->idx) {
3586 err = input_log_view(NULL, view, KEY_UP);
3593 s->search_entry = NULL;
3598 static const struct got_error *
3599 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3600 struct got_repository *repo, const char *head_ref_name,
3601 const char *in_repo_path, int log_branches)
3603 const struct got_error *err = NULL;
3604 struct tog_log_view_state *s = &view->state.log;
3605 struct got_repository *thread_repo = NULL;
3606 struct got_commit_graph *thread_graph = NULL;
3609 if (in_repo_path != s->in_repo_path) {
3610 free(s->in_repo_path);
3611 s->in_repo_path = strdup(in_repo_path);
3612 if (s->in_repo_path == NULL) {
3613 err = got_error_from_errno("strdup");
3618 /* The commit queue only contains commits being displayed. */
3619 TAILQ_INIT(&s->real_commits.head);
3620 s->real_commits.ncommits = 0;
3621 s->commits = &s->real_commits;
3623 TAILQ_INIT(&s->limit_commits.head);
3625 s->limit_commits.ncommits = 0;
3628 if (head_ref_name) {
3629 s->head_ref_name = strdup(head_ref_name);
3630 if (s->head_ref_name == NULL) {
3631 err = got_error_from_errno("strdup");
3635 s->start_id = got_object_id_dup(start_id);
3636 if (s->start_id == NULL) {
3637 err = got_error_from_errno("got_object_id_dup");
3640 s->log_branches = log_branches;
3641 s->use_committer = 1;
3643 STAILQ_INIT(&s->colors);
3644 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3645 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3646 get_color_value("TOG_COLOR_COMMIT"));
3649 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3650 get_color_value("TOG_COLOR_AUTHOR"));
3652 free_colors(&s->colors);
3655 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3656 get_color_value("TOG_COLOR_DATE"));
3658 free_colors(&s->colors);
3663 view->show = show_log_view;
3664 view->input = input_log_view;
3665 view->resize = resize_log_view;
3666 view->close = close_log_view;
3667 view->search_start = search_start_log_view;
3668 view->search_next = search_next_log_view;
3670 if (s->thread_args.pack_fds == NULL) {
3671 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3675 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3676 s->thread_args.pack_fds);
3679 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3683 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3684 s->repo, NULL, NULL);
3688 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3690 err = got_error_set_errno(errcode, "pthread_cond_init");
3693 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3695 err = got_error_set_errno(errcode, "pthread_cond_init");
3699 s->thread_args.commits_needed = view->nlines;
3700 s->thread_args.graph = thread_graph;
3701 s->thread_args.real_commits = &s->real_commits;
3702 s->thread_args.limit_commits = &s->limit_commits;
3703 s->thread_args.in_repo_path = s->in_repo_path;
3704 s->thread_args.start_id = s->start_id;
3705 s->thread_args.repo = thread_repo;
3706 s->thread_args.log_complete = 0;
3707 s->thread_args.quit = &s->quit;
3708 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3709 s->thread_args.selected_entry = &s->selected_entry;
3710 s->thread_args.searching = &view->searching;
3711 s->thread_args.search_next_done = &view->search_next_done;
3712 s->thread_args.regex = &view->regex;
3713 s->thread_args.limiting = &s->limit_view;
3714 s->thread_args.limit_regex = &s->limit_regex;
3715 s->thread_args.limit_commits = &s->limit_commits;
3718 if (view->close == NULL)
3719 close_log_view(view);
3725 static const struct got_error *
3726 show_log_view(struct tog_view *view)
3728 const struct got_error *err;
3729 struct tog_log_view_state *s = &view->state.log;
3731 if (s->thread == NULL) {
3732 int errcode = pthread_create(&s->thread, NULL, log_thread,
3735 return got_error_set_errno(errcode, "pthread_create");
3736 if (s->thread_args.commits_needed > 0) {
3737 err = trigger_log_thread(view, 1);
3743 return draw_commits(view);
3747 log_move_cursor_up(struct tog_view *view, int page, int home)
3749 struct tog_log_view_state *s = &view->state.log;
3751 if (s->first_displayed_entry == NULL)
3753 if (s->selected_entry->idx == 0)
3756 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3758 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3760 if (!page && !home && s->selected > 0)
3763 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3769 static const struct got_error *
3770 log_move_cursor_down(struct tog_view *view, int page)
3772 struct tog_log_view_state *s = &view->state.log;
3773 const struct got_error *err = NULL;
3774 int eos = view->nlines - 2;
3776 if (s->first_displayed_entry == NULL)
3779 if (s->thread_args.log_complete &&
3780 s->selected_entry->idx >= s->commits->ncommits - 1)
3783 if (view_is_hsplit_top(view))
3784 --eos; /* border consumes the last line */
3787 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3790 err = log_scroll_down(view, 1);
3791 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3792 struct commit_queue_entry *entry;
3796 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3797 s->last_displayed_entry = entry;
3798 for (n = 0; n <= eos; n++) {
3801 s->first_displayed_entry = entry;
3802 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3805 s->selected = n - 1;
3807 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3808 s->thread_args.log_complete)
3809 s->selected += MIN(page,
3810 s->commits->ncommits - s->selected_entry->idx - 1);
3812 err = log_scroll_down(view, page);
3818 * We might necessarily overshoot in horizontal
3819 * splits; if so, select the last displayed commit.
3821 if (s->first_displayed_entry && s->last_displayed_entry) {
3822 s->selected = MIN(s->selected,
3823 s->last_displayed_entry->idx -
3824 s->first_displayed_entry->idx);
3829 if (s->thread_args.log_complete &&
3830 s->selected_entry->idx == s->commits->ncommits - 1)
3837 view_get_split(struct tog_view *view, int *y, int *x)
3842 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3843 if (view->child && view->child->resized_y)
3844 *y = view->child->resized_y;
3845 else if (view->resized_y)
3846 *y = view->resized_y;
3848 *y = view_split_begin_y(view->lines);
3849 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3850 if (view->child && view->child->resized_x)
3851 *x = view->child->resized_x;
3852 else if (view->resized_x)
3853 *x = view->resized_x;
3855 *x = view_split_begin_x(view->begin_x);
3859 /* Split view horizontally at y and offset view->state->selected line. */
3860 static const struct got_error *
3861 view_init_hsplit(struct tog_view *view, int y)
3863 const struct got_error *err = NULL;
3867 err = view_resize(view);
3871 err = offset_selection_down(view);
3876 static const struct got_error *
3877 log_goto_line(struct tog_view *view, int nlines)
3879 const struct got_error *err = NULL;
3880 struct tog_log_view_state *s = &view->state.log;
3881 int g, idx = s->selected_entry->idx;
3883 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3889 if (g >= s->first_displayed_entry->idx + 1 &&
3890 g <= s->last_displayed_entry->idx + 1 &&
3891 g - s->first_displayed_entry->idx - 1 < nlines) {
3892 s->selected = g - s->first_displayed_entry->idx - 1;
3898 err = log_move_cursor_down(view, g - idx - 1);
3899 if (!err && g > s->selected_entry->idx + 1)
3900 err = log_move_cursor_down(view,
3901 g - s->first_displayed_entry->idx - 1);
3904 } else if (idx + 1 > g)
3905 log_move_cursor_up(view, idx - g + 1, 0);
3907 if (g < nlines && s->first_displayed_entry->idx == 0)
3908 s->selected = g - 1;
3916 horizontal_scroll_input(struct tog_view *view, int ch)
3922 view->x -= MIN(view->x, 2);
3928 if (view->x + view->ncols / 2 < view->maxx)
3937 view->x = MAX(view->maxx - view->ncols / 2, 0);
3945 static const struct got_error *
3946 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3948 const struct got_error *err = NULL;
3949 struct tog_log_view_state *s = &view->state.log;
3952 if (s->thread_args.load_all) {
3953 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3954 s->thread_args.load_all = 0;
3955 else if (s->thread_args.log_complete) {
3956 err = log_move_cursor_down(view, s->commits->ncommits);
3957 s->thread_args.load_all = 0;
3963 eos = nscroll = view->nlines - 1;
3964 if (view_is_hsplit_top(view))
3968 return log_goto_line(view, eos);
3972 err = limit_log_view(view);
3983 horizontal_scroll_input(view, ch);
3990 log_move_cursor_up(view, 0, 0);
3995 log_move_cursor_up(view, 0, 1);
4005 log_move_cursor_up(view, nscroll, 0);
4012 err = log_move_cursor_down(view, 0);
4015 s->use_committer = !s->use_committer;
4016 view->action = s->use_committer ?
4017 "show committer" : "show commit author";
4022 /* We don't know yet how many commits, so we're forced to
4023 * traverse them all. */
4025 s->thread_args.load_all = 1;
4026 if (!s->thread_args.log_complete)
4027 return trigger_log_thread(view, 0);
4028 err = log_move_cursor_down(view, s->commits->ncommits);
4029 s->thread_args.load_all = 0;
4040 err = log_move_cursor_down(view, nscroll);
4043 if (s->selected > view->nlines - 2)
4044 s->selected = view->nlines - 2;
4045 if (s->selected > s->commits->ncommits - 1)
4046 s->selected = s->commits->ncommits - 1;
4048 if (s->commits->ncommits < view->nlines - 1 &&
4049 !s->thread_args.log_complete) {
4050 s->thread_args.commits_needed += (view->nlines - 1) -
4051 s->commits->ncommits;
4052 err = trigger_log_thread(view, 1);
4058 if (s->selected_entry == NULL)
4060 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4064 if (s->selected_entry == NULL)
4066 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4072 if (ch == KEY_BACKSPACE &&
4073 got_path_is_root_dir(s->in_repo_path))
4075 err = stop_log_thread(s);
4078 if (ch == KEY_BACKSPACE) {
4080 err = got_path_dirname(&parent_path, s->in_repo_path);
4083 free(s->in_repo_path);
4084 s->in_repo_path = parent_path;
4085 s->thread_args.in_repo_path = s->in_repo_path;
4086 } else if (ch == CTRL('l')) {
4087 struct got_object_id *start_id;
4088 err = got_repo_match_object_id(&start_id, NULL,
4089 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4090 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4092 if (s->head_ref_name == NULL ||
4093 err->code != GOT_ERR_NOT_REF)
4095 /* Try to cope with deleted references. */
4096 free(s->head_ref_name);
4097 s->head_ref_name = NULL;
4098 err = got_repo_match_object_id(&start_id,
4099 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4100 &tog_refs, s->repo);
4105 s->start_id = start_id;
4106 s->thread_args.start_id = s->start_id;
4108 s->log_branches = !s->log_branches;
4110 if (s->thread_args.pack_fds == NULL) {
4111 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4115 err = got_repo_open(&s->thread_args.repo,
4116 got_repo_get_path(s->repo), NULL,
4117 s->thread_args.pack_fds);
4121 err = tog_load_refs(s->repo, 0);
4124 err = got_commit_graph_open(&s->thread_args.graph,
4125 s->in_repo_path, !s->log_branches);
4128 err = got_commit_graph_iter_start(s->thread_args.graph,
4129 s->start_id, s->repo, NULL, NULL);
4132 free_commits(&s->real_commits);
4133 free_commits(&s->limit_commits);
4134 s->first_displayed_entry = NULL;
4135 s->last_displayed_entry = NULL;
4136 s->selected_entry = NULL;
4138 s->thread_args.log_complete = 0;
4140 s->thread_args.commits_needed = view->lines;
4141 s->matched_entry = NULL;
4142 s->search_entry = NULL;
4147 err = view_request_new(new_view, view, TOG_VIEW_REF);
4157 static const struct got_error *
4158 apply_unveil(const char *repo_path, const char *worktree_path)
4160 const struct got_error *error;
4163 if (unveil("gmon.out", "rwc") != 0)
4164 return got_error_from_errno2("unveil", "gmon.out");
4166 if (repo_path && unveil(repo_path, "r") != 0)
4167 return got_error_from_errno2("unveil", repo_path);
4169 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4170 return got_error_from_errno2("unveil", worktree_path);
4172 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4173 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4175 error = got_privsep_unveil_exec_helpers();
4179 if (unveil(NULL, NULL) != 0)
4180 return got_error_from_errno("unveil");
4185 static const struct got_error *
4186 init_mock_term(const char *test_script_path)
4188 const struct got_error *err = NULL;
4190 if (test_script_path == NULL || *test_script_path == '\0')
4191 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4193 tog_io.f = fopen(test_script_path, "re");
4194 if (tog_io.f == NULL) {
4195 err = got_error_from_errno_fmt("fopen: %s",
4200 /* test mode, we don't want any output */
4201 tog_io.cout = fopen("/dev/null", "w+");
4202 if (tog_io.cout == NULL) {
4203 err = got_error_from_errno("fopen: /dev/null");
4207 tog_io.cin = fopen("/dev/tty", "r+");
4208 if (tog_io.cin == NULL) {
4209 err = got_error_from_errno("fopen: /dev/tty");
4213 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4214 err = got_error_from_errno("fseeko");
4218 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4219 err = got_error_msg(GOT_ERR_IO,
4220 "newterm: failed to initialise curses");
4234 * Override default signal handlers before starting ncurses.
4235 * This should prevent ncurses from installing its own
4236 * broken cleanup() signal handler.
4238 signal(SIGWINCH, tog_sigwinch);
4239 signal(SIGPIPE, tog_sigpipe);
4240 signal(SIGCONT, tog_sigcont);
4241 signal(SIGINT, tog_sigint);
4242 signal(SIGTERM, tog_sigterm);
4244 if (using_mock_io) /* In test mode we use a fake terminal */
4250 halfdelay(1); /* Fast refresh while initial view is loading. */
4253 intrflush(stdscr, FALSE);
4254 keypad(stdscr, TRUE);
4256 if (getenv("TOG_COLORS") != NULL) {
4258 use_default_colors();
4264 static const struct got_error *
4265 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4266 struct got_repository *repo, struct got_worktree *worktree)
4268 const struct got_error *err = NULL;
4271 *in_repo_path = strdup("/");
4272 if (*in_repo_path == NULL)
4273 return got_error_from_errno("strdup");
4278 const char *prefix = got_worktree_get_path_prefix(worktree);
4281 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4284 if (asprintf(in_repo_path, "%s%s%s", prefix,
4285 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4287 err = got_error_from_errno("asprintf");
4288 *in_repo_path = NULL;
4292 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4297 static const struct got_error *
4298 cmd_log(int argc, char *argv[])
4300 const struct got_error *error;
4301 struct got_repository *repo = NULL;
4302 struct got_worktree *worktree = NULL;
4303 struct got_object_id *start_id = NULL;
4304 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4305 char *start_commit = NULL, *label = NULL;
4306 struct got_reference *ref = NULL;
4307 const char *head_ref_name = NULL;
4308 int ch, log_branches = 0;
4309 struct tog_view *view;
4310 int *pack_fds = NULL;
4312 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4318 start_commit = optarg;
4321 repo_path = realpath(optarg, NULL);
4322 if (repo_path == NULL)
4323 return got_error_from_errno2("realpath",
4338 error = got_repo_pack_fds_open(&pack_fds);
4342 if (repo_path == NULL) {
4343 cwd = getcwd(NULL, 0);
4345 return got_error_from_errno("getcwd");
4346 error = got_worktree_open(&worktree, cwd);
4347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4351 strdup(got_worktree_get_repo_path(worktree));
4353 repo_path = strdup(cwd);
4354 if (repo_path == NULL) {
4355 error = got_error_from_errno("strdup");
4360 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4364 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4371 error = apply_unveil(got_repo_get_path(repo),
4372 worktree ? got_worktree_get_root_path(worktree) : NULL);
4376 /* already loaded by tog_log_with_path()? */
4377 if (TAILQ_EMPTY(&tog_refs)) {
4378 error = tog_load_refs(repo, 0);
4383 if (start_commit == NULL) {
4384 error = got_repo_match_object_id(&start_id, &label,
4385 worktree ? got_worktree_get_head_ref_name(worktree) :
4386 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4389 head_ref_name = label;
4391 error = got_ref_open(&ref, repo, start_commit, 0);
4393 head_ref_name = got_ref_get_name(ref);
4394 else if (error->code != GOT_ERR_NOT_REF)
4396 error = got_repo_match_object_id(&start_id, NULL,
4397 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4402 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4404 error = got_error_from_errno("view_open");
4407 error = open_log_view(view, start_id, repo, head_ref_name,
4408 in_repo_path, log_branches);
4412 /* Release work tree lock. */
4413 got_worktree_close(worktree);
4416 error = view_loop(view);
4426 const struct got_error *close_err = got_repo_close(repo);
4431 got_worktree_close(worktree);
4433 const struct got_error *pack_err =
4434 got_repo_pack_fds_close(pack_fds);
4446 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4447 "object1 object2\n", getprogname());
4452 match_line(const char *line, regex_t *regex, size_t nmatch,
4453 regmatch_t *regmatch)
4455 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4458 static struct tog_color *
4459 match_color(struct tog_colors *colors, const char *line)
4461 struct tog_color *tc = NULL;
4463 STAILQ_FOREACH(tc, colors, entry) {
4464 if (match_line(line, &tc->regex, 0, NULL))
4471 static const struct got_error *
4472 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4473 WINDOW *window, int skipcol, regmatch_t *regmatch)
4475 const struct got_error *err = NULL;
4477 wchar_t *wline = NULL;
4478 int rme, rms, n, width, scrollx;
4479 int width0 = 0, width1 = 0, width2 = 0;
4480 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4484 rms = regmatch->rm_so;
4485 rme = regmatch->rm_eo;
4487 err = expand_tab(&exstr, line);
4491 /* Split the line into 3 segments, according to match offsets. */
4492 seg0 = strndup(exstr, rms);
4494 err = got_error_from_errno("strndup");
4497 seg1 = strndup(exstr + rms, rme - rms);
4499 err = got_error_from_errno("strndup");
4502 seg2 = strdup(exstr + rme);
4504 err = got_error_from_errno("strndup");
4508 /* draw up to matched token if we haven't scrolled past it */
4509 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4513 n = MAX(width0 - skipcol, 0);
4516 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4517 wlimit, col_tab_align, 1);
4520 waddwstr(window, &wline[scrollx]);
4530 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4534 wlen = wcslen(wline);
4536 width = wcwidth(wline[i]);
4538 /* should not happen, tabs are expanded */
4539 err = got_error(GOT_ERR_RANGE);
4542 if (width0 + w + width > skipcol)
4547 /* draw (visible part of) matched token (if scrolled into it) */
4548 if (width1 - w > 0) {
4549 wattron(window, A_STANDOUT);
4550 waddwstr(window, &wline[i]);
4551 wattroff(window, A_STANDOUT);
4552 wlimit -= (width1 - w);
4553 *wtotal += (width1 - w);
4557 if (wlimit > 0) { /* draw rest of line */
4559 if (skipcol > width0 + width1) {
4560 err = format_line(&wline, &width2, &scrollx, seg2,
4561 skipcol - (width0 + width1), wlimit,
4565 waddwstr(window, &wline[scrollx]);
4567 err = format_line(&wline, &width2, NULL, seg2, 0,
4568 wlimit, col_tab_align, 1);
4571 waddwstr(window, wline);
4585 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4588 int *eof, *first, *selected;
4590 if (view->type == TOG_VIEW_DIFF) {
4591 struct tog_diff_view_state *s = &view->state.diff;
4593 first = &s->first_displayed_line;
4597 } else if (view->type == TOG_VIEW_HELP) {
4598 struct tog_help_view_state *s = &view->state.help;
4600 first = &s->first_displayed_line;
4604 } else if (view->type == TOG_VIEW_BLAME) {
4605 struct tog_blame_view_state *s = &view->state.blame;
4607 first = &s->first_displayed_line;
4608 selected = &s->selected_line;
4614 /* Center gline in the middle of the page like vi(1). */
4615 if (*lineno < view->gline - (view->nlines - 3) / 2)
4617 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4626 *selected = view->gline <= (view->nlines - 3) / 2 ?
4627 view->gline : (view->nlines - 3) / 2 + 1;
4633 static const struct got_error *
4634 draw_file(struct tog_view *view, const char *header)
4636 struct tog_diff_view_state *s = &view->state.diff;
4637 regmatch_t *regmatch = &view->regmatch;
4638 const struct got_error *err;
4641 size_t linesize = 0;
4645 int max_lines = view->nlines;
4646 int nlines = s->nlines;
4649 s->lineno = s->first_displayed_line - 1;
4650 line_offset = s->lines[s->first_displayed_line - 1].offset;
4651 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4652 return got_error_from_errno("fseek");
4654 werase(view->window);
4656 if (view->gline > s->nlines - 1)
4657 view->gline = s->nlines - 1;
4660 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4661 1 : view->gline - (view->nlines - 3) / 2 :
4662 s->lineno + s->selected_line;
4664 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4665 return got_error_from_errno("asprintf");
4666 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4672 if (view_needs_focus_indication(view))
4673 wstandout(view->window);
4674 waddwstr(view->window, wline);
4677 while (width++ < view->ncols)
4678 waddch(view->window, ' ');
4679 if (view_needs_focus_indication(view))
4680 wstandend(view->window);
4690 while (max_lines > 0 && nprinted < max_lines) {
4691 enum got_diff_line_type linetype;
4694 linelen = getline(&line, &linesize, s->f);
4695 if (linelen == -1) {
4701 return got_ferror(s->f, GOT_ERR_IO);
4704 if (++s->lineno < s->first_displayed_line)
4706 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4708 if (s->lineno == view->hiline)
4711 /* Set view->maxx based on full line length. */
4712 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4718 view->maxx = MAX(view->maxx, width);
4722 linetype = s->lines[s->lineno].type;
4723 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4724 linetype < GOT_DIFF_LINE_CONTEXT)
4725 attr |= COLOR_PAIR(linetype);
4727 wattron(view->window, attr);
4728 if (s->first_displayed_line + nprinted == s->matched_line &&
4729 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4730 err = add_matched_line(&width, line, view->ncols, 0,
4731 view->window, view->x, regmatch);
4738 err = format_line(&wline, &width, &skip, line,
4739 view->x, view->ncols, 0, view->x ? 1 : 0);
4744 waddwstr(view->window, &wline[skip]);
4748 if (s->lineno == view->hiline) {
4749 /* highlight full gline length */
4750 while (width++ < view->ncols)
4751 waddch(view->window, ' ');
4753 if (width <= view->ncols - 1)
4754 waddch(view->window, '\n');
4757 wattroff(view->window, attr);
4758 if (++nprinted == 1)
4759 s->first_displayed_line = s->lineno;
4763 s->last_displayed_line = s->first_displayed_line +
4766 s->last_displayed_line = s->first_displayed_line;
4771 while (nprinted < view->nlines) {
4772 waddch(view->window, '\n');
4776 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4782 wstandout(view->window);
4783 waddwstr(view->window, wline);
4786 wstandend(view->window);
4793 get_datestr(time_t *time, char *datebuf)
4795 struct tm mytm, *tm;
4798 tm = gmtime_r(time, &mytm);
4801 s = asctime_r(tm, datebuf);
4804 p = strchr(s, '\n');
4810 static const struct got_error *
4811 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4812 off_t off, uint8_t type)
4814 struct got_diff_line *p;
4816 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4818 return got_error_from_errno("reallocarray");
4820 (*lines)[*nlines].offset = off;
4821 (*lines)[*nlines].type = type;
4827 static const struct got_error *
4828 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4829 struct got_diff_line *s_lines, size_t s_nlines)
4831 struct got_diff_line *p;
4835 if (fseeko(src, 0L, SEEK_SET) == -1)
4836 return got_error_from_errno("fseeko");
4839 r = fread(buf, 1, sizeof(buf), src);
4842 return got_error_from_errno("fread");
4846 if (fwrite(buf, 1, r, dst) != r)
4847 return got_ferror(dst, GOT_ERR_IO);
4850 if (s_nlines == 0 && *d_nlines == 0)
4854 * If commit info was in dst, increment line offsets
4855 * of the appended diff content, but skip s_lines[0]
4856 * because offset zero is already in *d_lines.
4858 if (*d_nlines > 0) {
4859 for (i = 1; i < s_nlines; ++i)
4860 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4868 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4870 /* d_lines is freed in close_diff_view() */
4871 return got_error_from_errno("reallocarray");
4876 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4877 *d_nlines += s_nlines;
4882 static const struct got_error *
4883 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4884 struct got_object_id *commit_id, struct got_reflist_head *refs,
4885 struct got_repository *repo, int ignore_ws, int force_text_diff,
4886 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4888 const struct got_error *err = NULL;
4889 char datebuf[26], *datestr;
4890 struct got_commit_object *commit;
4891 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4892 time_t committer_time;
4893 const char *author, *committer;
4894 char *refs_str = NULL;
4895 struct got_pathlist_entry *pe;
4900 err = build_refs_str(&refs_str, refs, commit_id, repo);
4905 err = got_object_open_as_commit(&commit, repo, commit_id);
4909 err = got_object_id_str(&id_str, commit_id);
4911 err = got_error_from_errno("got_object_id_str");
4915 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4919 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4920 refs_str ? refs_str : "", refs_str ? ")" : "");
4922 err = got_error_from_errno("fprintf");
4926 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4930 n = fprintf(outfile, "from: %s\n",
4931 got_object_commit_get_author(commit));
4933 err = got_error_from_errno("fprintf");
4937 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4941 author = got_object_commit_get_author(commit);
4942 committer = got_object_commit_get_committer(commit);
4943 if (strcmp(author, committer) != 0) {
4944 n = fprintf(outfile, "via: %s\n", committer);
4946 err = got_error_from_errno("fprintf");
4950 err = add_line_metadata(lines, nlines, outoff,
4951 GOT_DIFF_LINE_AUTHOR);
4955 committer_time = got_object_commit_get_committer_time(commit);
4956 datestr = get_datestr(&committer_time, datebuf);
4958 n = fprintf(outfile, "date: %s UTC\n", datestr);
4960 err = got_error_from_errno("fprintf");
4964 err = add_line_metadata(lines, nlines, outoff,
4965 GOT_DIFF_LINE_DATE);
4969 if (got_object_commit_get_nparents(commit) > 1) {
4970 const struct got_object_id_queue *parent_ids;
4971 struct got_object_qid *qid;
4973 parent_ids = got_object_commit_get_parent_ids(commit);
4974 STAILQ_FOREACH(qid, parent_ids, entry) {
4975 err = got_object_id_str(&id_str, &qid->id);
4978 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4980 err = got_error_from_errno("fprintf");
4984 err = add_line_metadata(lines, nlines, outoff,
4985 GOT_DIFF_LINE_META);
4993 err = got_object_commit_get_logmsg(&logmsg, commit);
4997 while ((line = strsep(&s, "\n")) != NULL) {
4998 n = fprintf(outfile, "%s\n", line);
5000 err = got_error_from_errno("fprintf");
5004 err = add_line_metadata(lines, nlines, outoff,
5005 GOT_DIFF_LINE_LOGMSG);
5010 TAILQ_FOREACH(pe, dsa->paths, entry) {
5011 struct got_diff_changed_path *cp = pe->data;
5012 int pad = dsa->max_path_len - pe->path_len + 1;
5014 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5015 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5016 dsa->rm_cols + 1, cp->rm);
5018 err = got_error_from_errno("fprintf");
5022 err = add_line_metadata(lines, nlines, outoff,
5023 GOT_DIFF_LINE_CHANGES);
5028 fputc('\n', outfile);
5030 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5034 n = fprintf(outfile,
5035 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5036 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5037 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5039 err = got_error_from_errno("fprintf");
5043 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5047 fputc('\n', outfile);
5049 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5054 got_object_commit_close(commit);
5063 static const struct got_error *
5064 create_diff(struct tog_diff_view_state *s)
5066 const struct got_error *err = NULL;
5067 FILE *f = NULL, *tmp_diff_file = NULL;
5069 struct got_diff_line *lines = NULL;
5070 struct got_pathlist_head changed_paths;
5072 TAILQ_INIT(&changed_paths);
5075 s->lines = malloc(sizeof(*s->lines));
5076 if (s->lines == NULL)
5077 return got_error_from_errno("malloc");
5082 err = got_error_from_errno("got_opentemp");
5085 tmp_diff_file = got_opentemp();
5086 if (tmp_diff_file == NULL) {
5087 err = got_error_from_errno("got_opentemp");
5090 if (s->f && fclose(s->f) == EOF) {
5091 err = got_error_from_errno("fclose");
5097 err = got_object_get_type(&obj_type, s->repo, s->id1);
5099 err = got_object_get_type(&obj_type, s->repo, s->id2);
5104 case GOT_OBJ_TYPE_BLOB:
5105 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5106 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5107 s->label1, s->label2, tog_diff_algo, s->diff_context,
5108 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5111 case GOT_OBJ_TYPE_TREE:
5112 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5113 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5114 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5115 s->force_text_diff, NULL, s->repo, s->f);
5117 case GOT_OBJ_TYPE_COMMIT: {
5118 const struct got_object_id_queue *parent_ids;
5119 struct got_object_qid *pid;
5120 struct got_commit_object *commit2;
5121 struct got_reflist_head *refs;
5123 struct got_diffstat_cb_arg dsa = {
5126 s->ignore_whitespace,
5131 lines = malloc(sizeof(*lines));
5132 if (lines == NULL) {
5133 err = got_error_from_errno("malloc");
5137 /* build diff first in tmp file then append to commit info */
5138 err = got_diff_objects_as_commits(&lines, &nlines,
5139 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5140 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5141 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5145 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5148 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5149 /* Show commit info if we're diffing to a parent/root commit. */
5150 if (s->id1 == NULL) {
5151 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5152 refs, s->repo, s->ignore_whitespace,
5153 s->force_text_diff, &dsa, s->f);
5157 parent_ids = got_object_commit_get_parent_ids(commit2);
5158 STAILQ_FOREACH(pid, parent_ids, entry) {
5159 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5160 err = write_commit_info(&s->lines,
5161 &s->nlines, s->id2, refs, s->repo,
5162 s->ignore_whitespace,
5163 s->force_text_diff, &dsa, s->f);
5170 got_object_commit_close(commit2);
5172 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5177 err = got_error(GOT_ERR_OBJ_TYPE);
5182 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5183 if (s->f && fflush(s->f) != 0 && err == NULL)
5184 err = got_error_from_errno("fflush");
5185 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5186 err = got_error_from_errno("fclose");
5191 diff_view_indicate_progress(struct tog_view *view)
5193 mvwaddstr(view->window, 0, 0, "diffing...");
5198 static const struct got_error *
5199 search_start_diff_view(struct tog_view *view)
5201 struct tog_diff_view_state *s = &view->state.diff;
5203 s->matched_line = 0;
5208 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5209 size_t *nlines, int **first, int **last, int **match, int **selected)
5211 struct tog_diff_view_state *s = &view->state.diff;
5214 *nlines = s->nlines;
5215 *line_offsets = NULL;
5216 *match = &s->matched_line;
5217 *first = &s->first_displayed_line;
5218 *last = &s->last_displayed_line;
5219 *selected = &s->selected_line;
5222 static const struct got_error *
5223 search_next_view_match(struct tog_view *view)
5225 const struct got_error *err = NULL;
5229 size_t linesize = 0;
5231 off_t *line_offsets;
5233 int *first, *last, *match, *selected;
5235 if (!view->search_setup)
5236 return got_error_msg(GOT_ERR_NOT_IMPL,
5237 "view search not supported");
5238 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5241 if (!view->searching) {
5242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 lineno = *first + 1;
5250 lineno = *first - 1;
5252 lineno = *first - 1 + *selected;
5257 if (lineno <= 0 || lineno > nlines) {
5259 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5263 if (view->searching == TOG_SEARCH_FORWARD)
5269 offset = view->type == TOG_VIEW_DIFF ?
5270 view->state.diff.lines[lineno - 1].offset :
5271 line_offsets[lineno - 1];
5272 if (fseeko(f, offset, SEEK_SET) != 0) {
5274 return got_error_from_errno("fseeko");
5276 linelen = getline(&line, &linesize, f);
5277 if (linelen != -1) {
5279 err = expand_tab(&exstr, line);
5282 if (match_line(exstr, &view->regex, 1,
5284 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5291 if (view->searching == TOG_SEARCH_FORWARD)
5306 static const struct got_error *
5307 close_diff_view(struct tog_view *view)
5309 const struct got_error *err = NULL;
5310 struct tog_diff_view_state *s = &view->state.diff;
5316 if (s->f && fclose(s->f) == EOF)
5317 err = got_error_from_errno("fclose");
5319 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5320 err = got_error_from_errno("fclose");
5322 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5323 err = got_error_from_errno("fclose");
5325 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5326 err = got_error_from_errno("close");
5328 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5329 err = got_error_from_errno("close");
5337 static const struct got_error *
5338 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5339 struct got_object_id *id2, const char *label1, const char *label2,
5340 int diff_context, int ignore_whitespace, int force_text_diff,
5341 struct tog_view *parent_view, struct got_repository *repo)
5343 const struct got_error *err;
5344 struct tog_diff_view_state *s = &view->state.diff;
5346 memset(s, 0, sizeof(*s));
5350 if (id1 != NULL && id2 != NULL) {
5353 err = got_object_get_type(&type1, repo, id1);
5356 err = got_object_get_type(&type2, repo, id2);
5360 if (type1 != type2) {
5361 err = got_error(GOT_ERR_OBJ_TYPE);
5365 s->first_displayed_line = 1;
5366 s->last_displayed_line = view->nlines;
5367 s->selected_line = 1;
5375 s->id1 = got_object_id_dup(id1);
5376 if (s->id1 == NULL) {
5377 err = got_error_from_errno("got_object_id_dup");
5383 s->id2 = got_object_id_dup(id2);
5384 if (s->id2 == NULL) {
5385 err = got_error_from_errno("got_object_id_dup");
5389 s->f1 = got_opentemp();
5390 if (s->f1 == NULL) {
5391 err = got_error_from_errno("got_opentemp");
5395 s->f2 = got_opentemp();
5396 if (s->f2 == NULL) {
5397 err = got_error_from_errno("got_opentemp");
5401 s->fd1 = got_opentempfd();
5403 err = got_error_from_errno("got_opentempfd");
5407 s->fd2 = got_opentempfd();
5409 err = got_error_from_errno("got_opentempfd");
5413 s->diff_context = diff_context;
5414 s->ignore_whitespace = ignore_whitespace;
5415 s->force_text_diff = force_text_diff;
5416 s->parent_view = parent_view;
5419 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5422 rc = init_pair(GOT_DIFF_LINE_MINUS,
5423 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5425 rc = init_pair(GOT_DIFF_LINE_PLUS,
5426 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5428 rc = init_pair(GOT_DIFF_LINE_HUNK,
5429 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5431 rc = init_pair(GOT_DIFF_LINE_META,
5432 get_color_value("TOG_COLOR_DIFF_META"), -1);
5434 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5435 get_color_value("TOG_COLOR_DIFF_META"), -1);
5437 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5438 get_color_value("TOG_COLOR_DIFF_META"), -1);
5440 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5441 get_color_value("TOG_COLOR_DIFF_META"), -1);
5443 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5444 get_color_value("TOG_COLOR_AUTHOR"), -1);
5446 rc = init_pair(GOT_DIFF_LINE_DATE,
5447 get_color_value("TOG_COLOR_DATE"), -1);
5449 err = got_error(GOT_ERR_RANGE);
5454 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5455 view_is_splitscreen(view))
5456 show_log_view(parent_view); /* draw border */
5457 diff_view_indicate_progress(view);
5459 err = create_diff(s);
5461 view->show = show_diff_view;
5462 view->input = input_diff_view;
5463 view->reset = reset_diff_view;
5464 view->close = close_diff_view;
5465 view->search_start = search_start_diff_view;
5466 view->search_setup = search_setup_diff_view;
5467 view->search_next = search_next_view_match;
5470 if (view->close == NULL)
5471 close_diff_view(view);
5477 static const struct got_error *
5478 show_diff_view(struct tog_view *view)
5480 const struct got_error *err;
5481 struct tog_diff_view_state *s = &view->state.diff;
5482 char *id_str1 = NULL, *id_str2, *header;
5483 const char *label1, *label2;
5486 err = got_object_id_str(&id_str1, s->id1);
5489 label1 = s->label1 ? s->label1 : id_str1;
5491 label1 = "/dev/null";
5493 err = got_object_id_str(&id_str2, s->id2);
5496 label2 = s->label2 ? s->label2 : id_str2;
5498 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5499 err = got_error_from_errno("asprintf");
5507 err = draw_file(view, header);
5512 static const struct got_error *
5513 set_selected_commit(struct tog_diff_view_state *s,
5514 struct commit_queue_entry *entry)
5516 const struct got_error *err;
5517 const struct got_object_id_queue *parent_ids;
5518 struct got_commit_object *selected_commit;
5519 struct got_object_qid *pid;
5522 s->id2 = got_object_id_dup(entry->id);
5524 return got_error_from_errno("got_object_id_dup");
5526 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5529 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5531 pid = STAILQ_FIRST(parent_ids);
5532 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5533 got_object_commit_close(selected_commit);
5537 static const struct got_error *
5538 reset_diff_view(struct tog_view *view)
5540 struct tog_diff_view_state *s = &view->state.diff;
5543 wclear(view->window);
5544 s->first_displayed_line = 1;
5545 s->last_displayed_line = view->nlines;
5546 s->matched_line = 0;
5547 diff_view_indicate_progress(view);
5548 return create_diff(s);
5552 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5556 i = start = s->first_displayed_line - 1;
5558 while (s->lines[i].type != type) {
5562 return; /* do nothing, requested type not in file */
5565 s->selected_line = 1;
5566 s->first_displayed_line = i;
5570 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5574 i = start = s->first_displayed_line + 1;
5576 while (s->lines[i].type != type) {
5577 if (i == s->nlines - 1)
5580 return; /* do nothing, requested type not in file */
5583 s->selected_line = 1;
5584 s->first_displayed_line = i;
5587 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5589 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5592 static const struct got_error *
5593 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5595 const struct got_error *err = NULL;
5596 struct tog_diff_view_state *s = &view->state.diff;
5597 struct tog_log_view_state *ls;
5598 struct commit_queue_entry *old_selected_entry;
5600 size_t linesize = 0;
5602 int i, nscroll = view->nlines - 1, up = 0;
5604 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5613 horizontal_scroll_input(view, ch);
5618 s->force_text_diff = !s->force_text_diff;
5619 view->action = s->force_text_diff ?
5620 "force ASCII text enabled" :
5621 "force ASCII text disabled";
5623 else if (ch == 'w') {
5624 s->ignore_whitespace = !s->ignore_whitespace;
5625 view->action = s->ignore_whitespace ?
5626 "ignore whitespace enabled" :
5627 "ignore whitespace disabled";
5629 err = reset_diff_view(view);
5633 s->first_displayed_line = 1;
5642 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5648 if (s->first_displayed_line > 1)
5649 s->first_displayed_line--;
5660 if (s->first_displayed_line == 1) {
5665 while (i++ < nscroll && s->first_displayed_line > 1)
5666 s->first_displayed_line--;
5672 s->first_displayed_line++;
5689 while (!s->eof && i++ < nscroll) {
5690 linelen = getline(&line, &linesize, s->f);
5691 s->first_displayed_line++;
5692 if (linelen == -1) {
5696 err = got_ferror(s->f, GOT_ERR_IO);
5703 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5706 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5709 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5712 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5715 if (s->diff_context > 0) {
5717 s->matched_line = 0;
5718 diff_view_indicate_progress(view);
5719 err = create_diff(s);
5720 if (s->first_displayed_line + view->nlines - 1 >
5722 s->first_displayed_line = 1;
5723 s->last_displayed_line = view->nlines;
5729 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5731 s->matched_line = 0;
5732 diff_view_indicate_progress(view);
5733 err = create_diff(s);
5745 if (s->parent_view == NULL) {
5749 s->parent_view->count = view->count;
5751 if (s->parent_view->type == TOG_VIEW_LOG) {
5752 ls = &s->parent_view->state.log;
5753 old_selected_entry = ls->selected_entry;
5755 err = input_log_view(NULL, s->parent_view,
5756 up ? KEY_UP : KEY_DOWN);
5759 view->count = s->parent_view->count;
5761 if (old_selected_entry == ls->selected_entry)
5764 err = set_selected_commit(s, ls->selected_entry);
5767 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5768 struct tog_blame_view_state *bs;
5769 struct got_object_id *id, *prev_id;
5771 bs = &s->parent_view->state.blame;
5772 prev_id = get_annotation_for_line(bs->blame.lines,
5773 bs->blame.nlines, bs->last_diffed_line);
5775 err = input_blame_view(&view, s->parent_view,
5776 up ? KEY_UP : KEY_DOWN);
5779 view->count = s->parent_view->count;
5781 if (prev_id == NULL)
5783 id = get_selected_commit_id(bs->blame.lines,
5784 bs->blame.nlines, bs->first_displayed_line,
5789 if (!got_object_id_cmp(prev_id, id))
5792 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5796 s->first_displayed_line = 1;
5797 s->last_displayed_line = view->nlines;
5798 s->matched_line = 0;
5801 diff_view_indicate_progress(view);
5802 err = create_diff(s);
5812 static const struct got_error *
5813 cmd_diff(int argc, char *argv[])
5815 const struct got_error *error;
5816 struct got_repository *repo = NULL;
5817 struct got_worktree *worktree = NULL;
5818 struct got_object_id *id1 = NULL, *id2 = NULL;
5819 char *repo_path = NULL, *cwd = NULL;
5820 char *id_str1 = NULL, *id_str2 = NULL;
5821 char *label1 = NULL, *label2 = NULL;
5822 int diff_context = 3, ignore_whitespace = 0;
5823 int ch, force_text_diff = 0;
5825 struct tog_view *view;
5826 int *pack_fds = NULL;
5828 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5831 force_text_diff = 1;
5834 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5837 errx(1, "number of context lines is %s: %s",
5841 repo_path = realpath(optarg, NULL);
5842 if (repo_path == NULL)
5843 return got_error_from_errno2("realpath",
5845 got_path_strip_trailing_slashes(repo_path);
5848 ignore_whitespace = 1;
5860 usage_diff(); /* TODO show local worktree changes */
5861 } else if (argc == 2) {
5867 error = got_repo_pack_fds_open(&pack_fds);
5871 if (repo_path == NULL) {
5872 cwd = getcwd(NULL, 0);
5874 return got_error_from_errno("getcwd");
5875 error = got_worktree_open(&worktree, cwd);
5876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5880 strdup(got_worktree_get_repo_path(worktree));
5882 repo_path = strdup(cwd);
5883 if (repo_path == NULL) {
5884 error = got_error_from_errno("strdup");
5889 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5895 error = apply_unveil(got_repo_get_path(repo), NULL);
5899 error = tog_load_refs(repo, 0);
5903 error = got_repo_match_object_id(&id1, &label1, id_str1,
5904 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5908 error = got_repo_match_object_id(&id2, &label2, id_str2,
5909 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5913 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5915 error = got_error_from_errno("view_open");
5918 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5919 ignore_whitespace, force_text_diff, NULL, repo);
5922 error = view_loop(view);
5929 const struct got_error *close_err = got_repo_close(repo);
5934 got_worktree_close(worktree);
5936 const struct got_error *pack_err =
5937 got_repo_pack_fds_close(pack_fds);
5950 "usage: %s blame [-c commit] [-r repository-path] path\n",
5955 struct tog_blame_line {
5957 struct got_object_id *id;
5960 static const struct got_error *
5961 draw_blame(struct tog_view *view)
5963 struct tog_blame_view_state *s = &view->state.blame;
5964 struct tog_blame *blame = &s->blame;
5965 regmatch_t *regmatch = &view->regmatch;
5966 const struct got_error *err;
5967 int lineno = 0, nprinted = 0;
5969 size_t linesize = 0;
5973 struct tog_blame_line *blame_line;
5974 struct got_object_id *prev_id = NULL;
5976 struct tog_color *tc;
5978 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5983 werase(view->window);
5985 if (asprintf(&line, "commit %s", id_str) == -1) {
5986 err = got_error_from_errno("asprintf");
5991 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5996 if (view_needs_focus_indication(view))
5997 wstandout(view->window);
5998 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6000 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6001 waddwstr(view->window, wline);
6002 while (width++ < view->ncols)
6003 waddch(view->window, ' ');
6005 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6006 if (view_needs_focus_indication(view))
6007 wstandend(view->window);
6011 if (view->gline > blame->nlines)
6012 view->gline = blame->nlines;
6014 if (tog_io.wait_for_ui) {
6015 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6018 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6020 return got_error_set_errno(rc, "pthread_cond_wait");
6021 tog_io.wait_for_ui = 0;
6024 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6025 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6026 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6028 return got_error_from_errno("asprintf");
6031 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6036 waddwstr(view->window, wline);
6039 if (width < view->ncols - 1)
6040 waddch(view->window, '\n');
6044 while (nprinted < view->nlines - 2) {
6045 linelen = getline(&line, &linesize, blame->f);
6046 if (linelen == -1) {
6047 if (feof(blame->f)) {
6052 return got_ferror(blame->f, GOT_ERR_IO);
6054 if (++lineno < s->first_displayed_line)
6056 if (view->gline && !gotoline(view, &lineno, &nprinted))
6059 /* Set view->maxx based on full line length. */
6060 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6067 view->maxx = MAX(view->maxx, width);
6069 if (nprinted == s->selected_line - 1)
6070 wstandout(view->window);
6072 if (blame->nlines > 0) {
6073 blame_line = &blame->lines[lineno - 1];
6074 if (blame_line->annotated && prev_id &&
6075 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6076 !(nprinted == s->selected_line - 1)) {
6077 waddstr(view->window, " ");
6078 } else if (blame_line->annotated) {
6080 err = got_object_id_str(&id_str,
6086 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6088 wattr_on(view->window,
6089 COLOR_PAIR(tc->colorpair), NULL);
6090 wprintw(view->window, "%.8s", id_str);
6092 wattr_off(view->window,
6093 COLOR_PAIR(tc->colorpair), NULL);
6095 prev_id = blame_line->id;
6097 waddstr(view->window, "........");
6101 waddstr(view->window, "........");
6105 if (nprinted == s->selected_line - 1)
6106 wstandend(view->window);
6107 waddstr(view->window, " ");
6109 if (view->ncols <= 9) {
6111 } else if (s->first_displayed_line + nprinted ==
6113 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6114 err = add_matched_line(&width, line, view->ncols - 9, 9,
6115 view->window, view->x, regmatch);
6123 err = format_line(&wline, &width, &skip, line,
6124 view->x, view->ncols - 9, 9, 1);
6129 waddwstr(view->window, &wline[skip]);
6135 if (width <= view->ncols - 1)
6136 waddch(view->window, '\n');
6137 if (++nprinted == 1)
6138 s->first_displayed_line = lineno;
6141 s->last_displayed_line = lineno;
6148 static const struct got_error *
6149 blame_cb(void *arg, int nlines, int lineno,
6150 struct got_commit_object *commit, struct got_object_id *id)
6152 const struct got_error *err = NULL;
6153 struct tog_blame_cb_args *a = arg;
6154 struct tog_blame_line *line;
6157 if (nlines != a->nlines ||
6158 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6159 return got_error(GOT_ERR_RANGE);
6161 errcode = pthread_mutex_lock(&tog_mutex);
6163 return got_error_set_errno(errcode, "pthread_mutex_lock");
6165 if (*a->quit) { /* user has quit the blame view */
6166 err = got_error(GOT_ERR_ITER_COMPLETED);
6171 goto done; /* no change in this commit */
6173 line = &a->lines[lineno - 1];
6174 if (line->annotated)
6177 line->id = got_object_id_dup(id);
6178 if (line->id == NULL) {
6179 err = got_error_from_errno("got_object_id_dup");
6182 line->annotated = 1;
6184 errcode = pthread_mutex_unlock(&tog_mutex);
6186 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6191 blame_thread(void *arg)
6193 const struct got_error *err, *close_err;
6194 struct tog_blame_thread_args *ta = arg;
6195 struct tog_blame_cb_args *a = ta->cb_args;
6196 int errcode, fd1 = -1, fd2 = -1;
6197 FILE *f1 = NULL, *f2 = NULL;
6199 fd1 = got_opentempfd();
6201 return (void *)got_error_from_errno("got_opentempfd");
6203 fd2 = got_opentempfd();
6205 err = got_error_from_errno("got_opentempfd");
6209 f1 = got_opentemp();
6211 err = (void *)got_error_from_errno("got_opentemp");
6214 f2 = got_opentemp();
6216 err = (void *)got_error_from_errno("got_opentemp");
6220 err = block_signals_used_by_main_thread();
6224 err = got_blame(ta->path, a->commit_id, ta->repo,
6225 tog_diff_algo, blame_cb, ta->cb_args,
6226 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6227 if (err && err->code == GOT_ERR_CANCELLED)
6230 errcode = pthread_mutex_lock(&tog_mutex);
6232 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6236 close_err = got_repo_close(ta->repo);
6242 if (tog_io.wait_for_ui) {
6243 errcode = pthread_cond_signal(&ta->blame_complete);
6244 if (errcode && err == NULL)
6245 err = got_error_set_errno(errcode,
6246 "pthread_cond_signal");
6249 errcode = pthread_mutex_unlock(&tog_mutex);
6250 if (errcode && err == NULL)
6251 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6254 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6255 err = got_error_from_errno("close");
6256 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6257 err = got_error_from_errno("close");
6258 if (f1 && fclose(f1) == EOF && err == NULL)
6259 err = got_error_from_errno("fclose");
6260 if (f2 && fclose(f2) == EOF && err == NULL)
6261 err = got_error_from_errno("fclose");
6266 static struct got_object_id *
6267 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6268 int first_displayed_line, int selected_line)
6270 struct tog_blame_line *line;
6275 line = &lines[first_displayed_line - 1 + selected_line - 1];
6276 if (!line->annotated)
6282 static struct got_object_id *
6283 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6286 struct tog_blame_line *line;
6288 if (nlines <= 0 || lineno >= nlines)
6291 line = &lines[lineno - 1];
6292 if (!line->annotated)
6298 static const struct got_error *
6299 stop_blame(struct tog_blame *blame)
6301 const struct got_error *err = NULL;
6304 if (blame->thread) {
6306 errcode = pthread_mutex_unlock(&tog_mutex);
6308 return got_error_set_errno(errcode,
6309 "pthread_mutex_unlock");
6310 errcode = pthread_join(blame->thread, (void **)&err);
6312 return got_error_set_errno(errcode, "pthread_join");
6313 errcode = pthread_mutex_lock(&tog_mutex);
6315 return got_error_set_errno(errcode,
6316 "pthread_mutex_lock");
6317 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6319 blame->thread = NULL;
6321 if (blame->thread_args.repo) {
6322 const struct got_error *close_err;
6323 close_err = got_repo_close(blame->thread_args.repo);
6326 blame->thread_args.repo = NULL;
6329 if (fclose(blame->f) == EOF && err == NULL)
6330 err = got_error_from_errno("fclose");
6334 for (i = 0; i < blame->nlines; i++)
6335 free(blame->lines[i].id);
6337 blame->lines = NULL;
6339 free(blame->cb_args.commit_id);
6340 blame->cb_args.commit_id = NULL;
6341 if (blame->pack_fds) {
6342 const struct got_error *pack_err =
6343 got_repo_pack_fds_close(blame->pack_fds);
6346 blame->pack_fds = NULL;
6351 static const struct got_error *
6352 cancel_blame_view(void *arg)
6354 const struct got_error *err = NULL;
6358 errcode = pthread_mutex_lock(&tog_mutex);
6360 return got_error_set_errno(errcode,
6361 "pthread_mutex_unlock");
6364 err = got_error(GOT_ERR_CANCELLED);
6366 errcode = pthread_mutex_unlock(&tog_mutex);
6368 return got_error_set_errno(errcode,
6369 "pthread_mutex_lock");
6374 static const struct got_error *
6375 run_blame(struct tog_view *view)
6377 struct tog_blame_view_state *s = &view->state.blame;
6378 struct tog_blame *blame = &s->blame;
6379 const struct got_error *err = NULL;
6380 struct got_commit_object *commit = NULL;
6381 struct got_blob_object *blob = NULL;
6382 struct got_repository *thread_repo = NULL;
6383 struct got_object_id *obj_id = NULL;
6384 int obj_type, fd = -1;
6385 int *pack_fds = NULL;
6387 err = got_object_open_as_commit(&commit, s->repo,
6388 &s->blamed_commit->id);
6392 fd = got_opentempfd();
6394 err = got_error_from_errno("got_opentempfd");
6398 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6402 err = got_object_get_type(&obj_type, s->repo, obj_id);
6406 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6407 err = got_error(GOT_ERR_OBJ_TYPE);
6411 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6414 blame->f = got_opentemp();
6415 if (blame->f == NULL) {
6416 err = got_error_from_errno("got_opentemp");
6419 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6420 &blame->line_offsets, blame->f, blob);
6423 if (blame->nlines == 0) {
6424 s->blame_complete = 1;
6428 /* Don't include \n at EOF in the blame line count. */
6429 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6432 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6433 if (blame->lines == NULL) {
6434 err = got_error_from_errno("calloc");
6438 err = got_repo_pack_fds_open(&pack_fds);
6441 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6446 blame->pack_fds = pack_fds;
6447 blame->cb_args.view = view;
6448 blame->cb_args.lines = blame->lines;
6449 blame->cb_args.nlines = blame->nlines;
6450 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6451 if (blame->cb_args.commit_id == NULL) {
6452 err = got_error_from_errno("got_object_id_dup");
6455 blame->cb_args.quit = &s->done;
6457 blame->thread_args.path = s->path;
6458 blame->thread_args.repo = thread_repo;
6459 blame->thread_args.cb_args = &blame->cb_args;
6460 blame->thread_args.complete = &s->blame_complete;
6461 blame->thread_args.cancel_cb = cancel_blame_view;
6462 blame->thread_args.cancel_arg = &s->done;
6463 s->blame_complete = 0;
6465 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6466 s->first_displayed_line = 1;
6467 s->last_displayed_line = view->nlines;
6468 s->selected_line = 1;
6470 s->matched_line = 0;
6474 got_object_commit_close(commit);
6475 if (fd != -1 && close(fd) == -1 && err == NULL)
6476 err = got_error_from_errno("close");
6478 got_object_blob_close(blob);
6485 static const struct got_error *
6486 open_blame_view(struct tog_view *view, char *path,
6487 struct got_object_id *commit_id, struct got_repository *repo)
6489 const struct got_error *err = NULL;
6490 struct tog_blame_view_state *s = &view->state.blame;
6492 STAILQ_INIT(&s->blamed_commits);
6494 s->path = strdup(path);
6495 if (s->path == NULL)
6496 return got_error_from_errno("strdup");
6498 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6504 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6505 s->first_displayed_line = 1;
6506 s->last_displayed_line = view->nlines;
6507 s->selected_line = 1;
6508 s->blame_complete = 0;
6510 s->commit_id = commit_id;
6511 memset(&s->blame, 0, sizeof(s->blame));
6513 STAILQ_INIT(&s->colors);
6514 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6515 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6516 get_color_value("TOG_COLOR_COMMIT"));
6521 view->show = show_blame_view;
6522 view->input = input_blame_view;
6523 view->reset = reset_blame_view;
6524 view->close = close_blame_view;
6525 view->search_start = search_start_blame_view;
6526 view->search_setup = search_setup_blame_view;
6527 view->search_next = search_next_view_match;
6529 if (using_mock_io) {
6530 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6533 rc = pthread_cond_init(&bta->blame_complete, NULL);
6535 return got_error_set_errno(rc, "pthread_cond_init");
6538 return run_blame(view);
6541 static const struct got_error *
6542 close_blame_view(struct tog_view *view)
6544 const struct got_error *err = NULL;
6545 struct tog_blame_view_state *s = &view->state.blame;
6547 if (s->blame.thread)
6548 err = stop_blame(&s->blame);
6550 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6551 struct got_object_qid *blamed_commit;
6552 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6553 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6554 got_object_qid_free(blamed_commit);
6557 if (using_mock_io) {
6558 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6561 rc = pthread_cond_destroy(&bta->blame_complete);
6562 if (rc && err == NULL)
6563 err = got_error_set_errno(rc, "pthread_cond_destroy");
6567 free_colors(&s->colors);
6571 static const struct got_error *
6572 search_start_blame_view(struct tog_view *view)
6574 struct tog_blame_view_state *s = &view->state.blame;
6576 s->matched_line = 0;
6581 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6582 size_t *nlines, int **first, int **last, int **match, int **selected)
6584 struct tog_blame_view_state *s = &view->state.blame;
6587 *nlines = s->blame.nlines;
6588 *line_offsets = s->blame.line_offsets;
6589 *match = &s->matched_line;
6590 *first = &s->first_displayed_line;
6591 *last = &s->last_displayed_line;
6592 *selected = &s->selected_line;
6595 static const struct got_error *
6596 show_blame_view(struct tog_view *view)
6598 const struct got_error *err = NULL;
6599 struct tog_blame_view_state *s = &view->state.blame;
6602 if (s->blame.thread == NULL && !s->blame_complete) {
6603 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6604 &s->blame.thread_args);
6606 return got_error_set_errno(errcode, "pthread_create");
6609 halfdelay(1); /* fast refresh while annotating */
6612 if (s->blame_complete && !using_mock_io)
6613 halfdelay(10); /* disable fast refresh */
6615 err = draw_blame(view);
6621 static const struct got_error *
6622 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6623 struct got_repository *repo, struct got_object_id *id)
6625 struct tog_view *log_view;
6626 const struct got_error *err = NULL;
6630 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6631 if (log_view == NULL)
6632 return got_error_from_errno("view_open");
6634 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6636 view_close(log_view);
6638 *new_view = log_view;
6643 static const struct got_error *
6644 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6646 const struct got_error *err = NULL, *thread_err = NULL;
6647 struct tog_view *diff_view;
6648 struct tog_blame_view_state *s = &view->state.blame;
6649 int eos, nscroll, begin_y = 0, begin_x = 0;
6651 eos = nscroll = view->nlines - 2;
6652 if (view_is_hsplit_top(view))
6662 horizontal_scroll_input(view, ch);
6669 s->selected_line = 1;
6670 s->first_displayed_line = 1;
6675 if (s->blame.nlines < eos) {
6676 s->selected_line = s->blame.nlines;
6677 s->first_displayed_line = 1;
6679 s->selected_line = eos;
6680 s->first_displayed_line = s->blame.nlines - (eos - 1);
6687 if (s->selected_line > 1)
6689 else if (s->selected_line == 1 &&
6690 s->first_displayed_line > 1)
6691 s->first_displayed_line--;
6702 if (s->first_displayed_line == 1) {
6703 if (view->count > 1)
6705 s->selected_line = MAX(1, s->selected_line - nscroll);
6709 if (s->first_displayed_line > nscroll)
6710 s->first_displayed_line -= nscroll;
6712 s->first_displayed_line = 1;
6717 if (s->selected_line < eos && s->first_displayed_line +
6718 s->selected_line <= s->blame.nlines)
6720 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6721 s->first_displayed_line++;
6727 struct got_object_id *id = NULL;
6730 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6731 s->first_displayed_line, s->selected_line);
6735 struct got_commit_object *commit, *pcommit;
6736 struct got_object_qid *pid;
6737 struct got_object_id *blob_id = NULL;
6739 err = got_object_open_as_commit(&commit,
6744 got_object_commit_get_parent_ids(commit));
6746 got_object_commit_close(commit);
6749 /* Check if path history ends here. */
6750 err = got_object_open_as_commit(&pcommit,
6754 err = got_object_id_by_path(&blob_id, s->repo,
6756 got_object_commit_close(pcommit);
6758 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6760 got_object_commit_close(commit);
6763 err = got_object_get_type(&obj_type, s->repo,
6766 /* Can't blame non-blob type objects. */
6767 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6768 got_object_commit_close(commit);
6771 err = got_object_qid_alloc(&s->blamed_commit,
6773 got_object_commit_close(commit);
6775 if (got_object_id_cmp(id,
6776 &s->blamed_commit->id) == 0)
6778 err = got_object_qid_alloc(&s->blamed_commit,
6784 thread_err = stop_blame(&s->blame);
6788 STAILQ_INSERT_HEAD(&s->blamed_commits,
6789 s->blamed_commit, entry);
6790 err = run_blame(view);
6796 struct got_object_qid *first;
6799 first = STAILQ_FIRST(&s->blamed_commits);
6800 if (!got_object_id_cmp(&first->id, s->commit_id))
6803 thread_err = stop_blame(&s->blame);
6807 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6808 got_object_qid_free(s->blamed_commit);
6810 STAILQ_FIRST(&s->blamed_commits);
6811 err = run_blame(view);
6818 s->id_to_log = get_selected_commit_id(s->blame.lines,
6819 s->blame.nlines, s->first_displayed_line, s->selected_line);
6821 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6825 struct got_object_id *id = NULL;
6826 struct got_object_qid *pid;
6827 struct got_commit_object *commit = NULL;
6830 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6831 s->first_displayed_line, s->selected_line);
6834 err = got_object_open_as_commit(&commit, s->repo, id);
6837 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6839 /* traversed from diff view, release diff resources */
6840 err = close_diff_view(*new_view);
6843 diff_view = *new_view;
6845 if (view_is_parent_view(view))
6846 view_get_split(view, &begin_y, &begin_x);
6848 diff_view = view_open(0, 0, begin_y, begin_x,
6850 if (diff_view == NULL) {
6851 got_object_commit_close(commit);
6852 err = got_error_from_errno("view_open");
6856 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6857 id, NULL, NULL, 3, 0, 0, view, s->repo);
6858 got_object_commit_close(commit);
6860 view_close(diff_view);
6863 s->last_diffed_line = s->first_displayed_line - 1 +
6866 break; /* still open from active diff view */
6867 if (view_is_parent_view(view) &&
6868 view->mode == TOG_VIEW_SPLIT_HRZN) {
6869 err = view_init_hsplit(view, begin_y);
6875 diff_view->focussed = 1;
6876 diff_view->mode = view->mode;
6877 diff_view->nlines = view->lines - begin_y;
6878 if (view_is_parent_view(view)) {
6879 view_transfer_size(diff_view, view);
6880 err = view_close_child(view);
6883 err = view_set_child(view, diff_view);
6886 view->focus_child = 1;
6888 *new_view = diff_view;
6901 if (s->last_displayed_line >= s->blame.nlines &&
6902 s->selected_line >= MIN(s->blame.nlines,
6903 view->nlines - 2)) {
6907 if (s->last_displayed_line >= s->blame.nlines &&
6908 s->selected_line < view->nlines - 2) {
6910 MIN(nscroll, s->last_displayed_line -
6911 s->first_displayed_line - s->selected_line + 1);
6913 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6914 s->first_displayed_line += nscroll;
6916 s->first_displayed_line =
6917 s->blame.nlines - (view->nlines - 3);
6920 if (s->selected_line > view->nlines - 2) {
6921 s->selected_line = MIN(s->blame.nlines,
6929 return thread_err ? thread_err : err;
6932 static const struct got_error *
6933 reset_blame_view(struct tog_view *view)
6935 const struct got_error *err;
6936 struct tog_blame_view_state *s = &view->state.blame;
6940 err = stop_blame(&s->blame);
6944 return run_blame(view);
6947 static const struct got_error *
6948 cmd_blame(int argc, char *argv[])
6950 const struct got_error *error;
6951 struct got_repository *repo = NULL;
6952 struct got_worktree *worktree = NULL;
6953 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6954 char *link_target = NULL;
6955 struct got_object_id *commit_id = NULL;
6956 struct got_commit_object *commit = NULL;
6957 char *commit_id_str = NULL;
6959 struct tog_view *view = NULL;
6960 int *pack_fds = NULL;
6962 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6965 commit_id_str = optarg;
6968 repo_path = realpath(optarg, NULL);
6969 if (repo_path == NULL)
6970 return got_error_from_errno2("realpath",
6985 error = got_repo_pack_fds_open(&pack_fds);
6989 if (repo_path == NULL) {
6990 cwd = getcwd(NULL, 0);
6992 return got_error_from_errno("getcwd");
6993 error = got_worktree_open(&worktree, cwd);
6994 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6998 strdup(got_worktree_get_repo_path(worktree));
7000 repo_path = strdup(cwd);
7001 if (repo_path == NULL) {
7002 error = got_error_from_errno("strdup");
7007 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7011 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7018 error = apply_unveil(got_repo_get_path(repo), NULL);
7022 error = tog_load_refs(repo, 0);
7026 if (commit_id_str == NULL) {
7027 struct got_reference *head_ref;
7028 error = got_ref_open(&head_ref, repo, worktree ?
7029 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7032 error = got_ref_resolve(&commit_id, repo, head_ref);
7033 got_ref_close(head_ref);
7035 error = got_repo_match_object_id(&commit_id, NULL,
7036 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7041 error = got_object_open_as_commit(&commit, repo, commit_id);
7045 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7050 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7052 error = got_error_from_errno("view_open");
7055 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7057 if (error != NULL) {
7058 if (view->close == NULL)
7059 close_blame_view(view);
7064 /* Release work tree lock. */
7065 got_worktree_close(worktree);
7068 error = view_loop(view);
7076 got_object_commit_close(commit);
7078 got_worktree_close(worktree);
7080 const struct got_error *close_err = got_repo_close(repo);
7085 const struct got_error *pack_err =
7086 got_repo_pack_fds_close(pack_fds);
7094 static const struct got_error *
7095 draw_tree_entries(struct tog_view *view, const char *parent_path)
7097 struct tog_tree_view_state *s = &view->state.tree;
7098 const struct got_error *err = NULL;
7099 struct got_tree_entry *te;
7102 struct tog_color *tc;
7103 int width, n, nentries, scrollx, i = 1;
7104 int limit = view->nlines;
7107 if (view_is_hsplit_top(view))
7108 --limit; /* border */
7110 werase(view->window);
7115 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7119 if (view_needs_focus_indication(view))
7120 wstandout(view->window);
7121 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7123 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7124 waddwstr(view->window, wline);
7127 while (width++ < view->ncols)
7128 waddch(view->window, ' ');
7130 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7131 if (view_needs_focus_indication(view))
7132 wstandend(view->window);
7137 if (s->first_displayed_entry) {
7138 i += got_tree_entry_get_index(s->first_displayed_entry);
7139 if (s->tree != s->root)
7140 ++i; /* account for ".." entry */
7142 nentries = got_object_tree_get_nentries(s->tree);
7143 if (asprintf(&index, "[%d/%d] %s",
7144 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7145 return got_error_from_errno("asprintf");
7146 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7150 waddwstr(view->window, wline);
7153 if (width < view->ncols - 1)
7154 waddch(view->window, '\n');
7157 waddch(view->window, '\n');
7161 if (s->first_displayed_entry == NULL) {
7162 te = got_object_tree_get_first_entry(s->tree);
7163 if (s->selected == 0) {
7165 wstandout(view->window);
7166 s->selected_entry = NULL;
7168 waddstr(view->window, " ..\n"); /* parent directory */
7169 if (s->selected == 0 && view->focussed)
7170 wstandend(view->window);
7177 te = s->first_displayed_entry;
7181 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7182 char *line = NULL, *id_str = NULL, *link_target = NULL;
7183 const char *modestr = "";
7186 te = got_object_tree_get_entry(s->tree, i);
7187 mode = got_tree_entry_get_mode(te);
7190 err = got_object_id_str(&id_str,
7191 got_tree_entry_get_id(te));
7193 return got_error_from_errno(
7194 "got_object_id_str");
7196 if (got_object_tree_entry_is_submodule(te))
7198 else if (S_ISLNK(mode)) {
7201 err = got_tree_entry_get_symlink_target(&link_target,
7207 for (i = 0; i < strlen(link_target); i++) {
7208 if (!isprint((unsigned char)link_target[i]))
7209 link_target[i] = '?';
7213 else if (S_ISDIR(mode))
7215 else if (mode & S_IXUSR)
7217 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7218 got_tree_entry_get_name(te), modestr,
7219 link_target ? " -> ": "",
7220 link_target ? link_target : "") == -1) {
7223 return got_error_from_errno("asprintf");
7228 /* use full line width to determine view->maxx */
7229 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7234 view->maxx = MAX(view->maxx, width);
7238 err = format_line(&wline, &width, &scrollx, line, view->x,
7244 if (n == s->selected) {
7246 wstandout(view->window);
7247 s->selected_entry = te;
7249 tc = match_color(&s->colors, line);
7251 wattr_on(view->window,
7252 COLOR_PAIR(tc->colorpair), NULL);
7253 waddwstr(view->window, &wline[scrollx]);
7255 wattr_off(view->window,
7256 COLOR_PAIR(tc->colorpair), NULL);
7257 if (width < view->ncols)
7258 waddch(view->window, '\n');
7259 if (n == s->selected && view->focussed)
7260 wstandend(view->window);
7266 s->last_displayed_entry = te;
7275 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7277 struct got_tree_entry *te;
7278 int isroot = s->tree == s->root;
7281 if (s->first_displayed_entry == NULL)
7284 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7285 while (i++ < maxscroll) {
7288 s->first_displayed_entry = NULL;
7291 s->first_displayed_entry = te;
7292 te = got_tree_entry_get_prev(s->tree, te);
7296 static const struct got_error *
7297 tree_scroll_down(struct tog_view *view, int maxscroll)
7299 struct tog_tree_view_state *s = &view->state.tree;
7300 struct got_tree_entry *next, *last;
7303 if (s->first_displayed_entry)
7304 next = got_tree_entry_get_next(s->tree,
7305 s->first_displayed_entry);
7307 next = got_object_tree_get_first_entry(s->tree);
7309 last = s->last_displayed_entry;
7310 while (next && n++ < maxscroll) {
7312 s->last_displayed_entry = last;
7313 last = got_tree_entry_get_next(s->tree, last);
7315 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7316 s->first_displayed_entry = next;
7317 next = got_tree_entry_get_next(s->tree, next);
7324 static const struct got_error *
7325 tree_entry_path(char **path, struct tog_parent_trees *parents,
7326 struct got_tree_entry *te)
7328 const struct got_error *err = NULL;
7329 struct tog_parent_tree *pt;
7330 size_t len = 2; /* for leading slash and NUL */
7332 TAILQ_FOREACH(pt, parents, entry)
7333 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7336 len += strlen(got_tree_entry_get_name(te));
7338 *path = calloc(1, len);
7340 return got_error_from_errno("calloc");
7343 pt = TAILQ_LAST(parents, tog_parent_trees);
7345 const char *name = got_tree_entry_get_name(pt->selected_entry);
7346 if (strlcat(*path, name, len) >= len) {
7347 err = got_error(GOT_ERR_NO_SPACE);
7350 if (strlcat(*path, "/", len) >= len) {
7351 err = got_error(GOT_ERR_NO_SPACE);
7354 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7357 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7358 err = got_error(GOT_ERR_NO_SPACE);
7370 static const struct got_error *
7371 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7372 struct got_tree_entry *te, struct tog_parent_trees *parents,
7373 struct got_object_id *commit_id, struct got_repository *repo)
7375 const struct got_error *err = NULL;
7377 struct tog_view *blame_view;
7381 err = tree_entry_path(&path, parents, te);
7385 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7386 if (blame_view == NULL) {
7387 err = got_error_from_errno("view_open");
7391 err = open_blame_view(blame_view, path, commit_id, repo);
7393 if (err->code == GOT_ERR_CANCELLED)
7395 view_close(blame_view);
7397 *new_view = blame_view;
7403 static const struct got_error *
7404 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7405 struct tog_tree_view_state *s)
7407 struct tog_view *log_view;
7408 const struct got_error *err = NULL;
7413 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7414 if (log_view == NULL)
7415 return got_error_from_errno("view_open");
7417 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7421 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7424 view_close(log_view);
7426 *new_view = log_view;
7431 static const struct got_error *
7432 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7433 const char *head_ref_name, struct got_repository *repo)
7435 const struct got_error *err = NULL;
7436 char *commit_id_str = NULL;
7437 struct tog_tree_view_state *s = &view->state.tree;
7438 struct got_commit_object *commit = NULL;
7440 TAILQ_INIT(&s->parents);
7441 STAILQ_INIT(&s->colors);
7443 s->commit_id = got_object_id_dup(commit_id);
7444 if (s->commit_id == NULL) {
7445 err = got_error_from_errno("got_object_id_dup");
7449 err = got_object_open_as_commit(&commit, repo, commit_id);
7454 * The root is opened here and will be closed when the view is closed.
7455 * Any visited subtrees and their path-wise parents are opened and
7458 err = got_object_open_as_tree(&s->root, repo,
7459 got_object_commit_get_tree_id(commit));
7464 err = got_object_id_str(&commit_id_str, commit_id);
7468 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7469 err = got_error_from_errno("asprintf");
7473 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7474 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7475 if (head_ref_name) {
7476 s->head_ref_name = strdup(head_ref_name);
7477 if (s->head_ref_name == NULL) {
7478 err = got_error_from_errno("strdup");
7484 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7485 err = add_color(&s->colors, "\\$$",
7486 TOG_COLOR_TREE_SUBMODULE,
7487 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7490 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7491 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7494 err = add_color(&s->colors, "/$",
7495 TOG_COLOR_TREE_DIRECTORY,
7496 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7500 err = add_color(&s->colors, "\\*$",
7501 TOG_COLOR_TREE_EXECUTABLE,
7502 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7506 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7507 get_color_value("TOG_COLOR_COMMIT"));
7512 view->show = show_tree_view;
7513 view->input = input_tree_view;
7514 view->close = close_tree_view;
7515 view->search_start = search_start_tree_view;
7516 view->search_next = search_next_tree_view;
7518 free(commit_id_str);
7520 got_object_commit_close(commit);
7522 if (view->close == NULL)
7523 close_tree_view(view);
7529 static const struct got_error *
7530 close_tree_view(struct tog_view *view)
7532 struct tog_tree_view_state *s = &view->state.tree;
7534 free_colors(&s->colors);
7535 free(s->tree_label);
7536 s->tree_label = NULL;
7538 s->commit_id = NULL;
7539 free(s->head_ref_name);
7540 s->head_ref_name = NULL;
7541 while (!TAILQ_EMPTY(&s->parents)) {
7542 struct tog_parent_tree *parent;
7543 parent = TAILQ_FIRST(&s->parents);
7544 TAILQ_REMOVE(&s->parents, parent, entry);
7545 if (parent->tree != s->root)
7546 got_object_tree_close(parent->tree);
7550 if (s->tree != NULL && s->tree != s->root)
7551 got_object_tree_close(s->tree);
7553 got_object_tree_close(s->root);
7557 static const struct got_error *
7558 search_start_tree_view(struct tog_view *view)
7560 struct tog_tree_view_state *s = &view->state.tree;
7562 s->matched_entry = NULL;
7567 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7569 regmatch_t regmatch;
7571 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7575 static const struct got_error *
7576 search_next_tree_view(struct tog_view *view)
7578 struct tog_tree_view_state *s = &view->state.tree;
7579 struct got_tree_entry *te = NULL;
7581 if (!view->searching) {
7582 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7586 if (s->matched_entry) {
7587 if (view->searching == TOG_SEARCH_FORWARD) {
7588 if (s->selected_entry)
7589 te = got_tree_entry_get_next(s->tree,
7592 te = got_object_tree_get_first_entry(s->tree);
7594 if (s->selected_entry == NULL)
7595 te = got_object_tree_get_last_entry(s->tree);
7597 te = got_tree_entry_get_prev(s->tree,
7601 if (s->selected_entry)
7602 te = s->selected_entry;
7603 else if (view->searching == TOG_SEARCH_FORWARD)
7604 te = got_object_tree_get_first_entry(s->tree);
7606 te = got_object_tree_get_last_entry(s->tree);
7611 if (s->matched_entry == NULL) {
7612 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7615 if (view->searching == TOG_SEARCH_FORWARD)
7616 te = got_object_tree_get_first_entry(s->tree);
7618 te = got_object_tree_get_last_entry(s->tree);
7621 if (match_tree_entry(te, &view->regex)) {
7622 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7623 s->matched_entry = te;
7627 if (view->searching == TOG_SEARCH_FORWARD)
7628 te = got_tree_entry_get_next(s->tree, te);
7630 te = got_tree_entry_get_prev(s->tree, te);
7633 if (s->matched_entry) {
7634 s->first_displayed_entry = s->matched_entry;
7641 static const struct got_error *
7642 show_tree_view(struct tog_view *view)
7644 const struct got_error *err = NULL;
7645 struct tog_tree_view_state *s = &view->state.tree;
7648 err = tree_entry_path(&parent_path, &s->parents, NULL);
7652 err = draw_tree_entries(view, parent_path);
7659 static const struct got_error *
7660 tree_goto_line(struct tog_view *view, int nlines)
7662 const struct got_error *err = NULL;
7663 struct tog_tree_view_state *s = &view->state.tree;
7664 struct got_tree_entry **fte, **lte, **ste;
7665 int g, last, first = 1, i = 1;
7666 int root = s->tree == s->root;
7667 int off = root ? 1 : 2;
7674 else if (g > got_object_tree_get_nentries(s->tree))
7675 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7677 fte = &s->first_displayed_entry;
7678 lte = &s->last_displayed_entry;
7679 ste = &s->selected_entry;
7682 first = got_tree_entry_get_index(*fte);
7683 first += off; /* account for ".." */
7685 last = got_tree_entry_get_index(*lte);
7688 if (g >= first && g <= last && g - first < nlines) {
7689 s->selected = g - first;
7690 return NULL; /* gline is on the current page */
7694 i = got_tree_entry_get_index(*ste);
7699 err = tree_scroll_down(view, g - i);
7702 if (got_tree_entry_get_index(*lte) >=
7703 got_object_tree_get_nentries(s->tree) - 1 &&
7704 first + s->selected < g &&
7705 s->selected < s->ndisplayed - 1) {
7706 first = got_tree_entry_get_index(*fte);
7708 s->selected = g - first;
7711 tree_scroll_up(s, i - g);
7714 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7715 s->selected = g - 1;
7720 static const struct got_error *
7721 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7723 const struct got_error *err = NULL;
7724 struct tog_tree_view_state *s = &view->state.tree;
7725 struct got_tree_entry *te;
7726 int n, nscroll = view->nlines - 3;
7729 return tree_goto_line(view, nscroll);
7738 horizontal_scroll_input(view, ch);
7741 s->show_ids = !s->show_ids;
7746 if (!s->selected_entry)
7748 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7752 err = view_request_new(new_view, view, TOG_VIEW_REF);
7759 if (s->tree == s->root)
7760 s->first_displayed_entry =
7761 got_object_tree_get_first_entry(s->tree);
7763 s->first_displayed_entry = NULL;
7768 int eos = view->nlines - 3;
7770 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7774 te = got_object_tree_get_last_entry(s->tree);
7775 for (n = 0; n < eos; n++) {
7777 if (s->tree != s->root) {
7778 s->first_displayed_entry = NULL;
7783 s->first_displayed_entry = te;
7784 te = got_tree_entry_get_prev(s->tree, te);
7787 s->selected = n - 1;
7793 if (s->selected > 0) {
7797 tree_scroll_up(s, 1);
7798 if (s->selected_entry == NULL ||
7799 (s->tree == s->root && s->selected_entry ==
7800 got_object_tree_get_first_entry(s->tree)))
7810 if (s->tree == s->root) {
7811 if (got_object_tree_get_first_entry(s->tree) ==
7812 s->first_displayed_entry)
7813 s->selected -= MIN(s->selected, nscroll);
7815 if (s->first_displayed_entry == NULL)
7816 s->selected -= MIN(s->selected, nscroll);
7818 tree_scroll_up(s, MAX(0, nscroll));
7819 if (s->selected_entry == NULL ||
7820 (s->tree == s->root && s->selected_entry ==
7821 got_object_tree_get_first_entry(s->tree)))
7827 if (s->selected < s->ndisplayed - 1) {
7831 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7833 /* can't scroll any further */
7837 tree_scroll_down(view, 1);
7847 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7849 /* can't scroll any further; move cursor down */
7850 if (s->selected < s->ndisplayed - 1)
7851 s->selected += MIN(nscroll,
7852 s->ndisplayed - s->selected - 1);
7857 tree_scroll_down(view, nscroll);
7862 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7863 struct tog_parent_tree *parent;
7864 /* user selected '..' */
7865 if (s->tree == s->root) {
7869 parent = TAILQ_FIRST(&s->parents);
7870 TAILQ_REMOVE(&s->parents, parent,
7872 got_object_tree_close(s->tree);
7873 s->tree = parent->tree;
7874 s->first_displayed_entry =
7875 parent->first_displayed_entry;
7877 parent->selected_entry;
7878 s->selected = parent->selected;
7879 if (s->selected > view->nlines - 3) {
7880 err = offset_selection_down(view);
7885 } else if (S_ISDIR(got_tree_entry_get_mode(
7886 s->selected_entry))) {
7887 struct got_tree_object *subtree;
7889 err = got_object_open_as_tree(&subtree, s->repo,
7890 got_tree_entry_get_id(s->selected_entry));
7893 err = tree_view_visit_subtree(s, subtree);
7895 got_object_tree_close(subtree);
7898 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7899 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7902 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7903 s->selected = view->nlines - 4;
7919 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7924 static const struct got_error *
7925 cmd_tree(int argc, char *argv[])
7927 const struct got_error *error;
7928 struct got_repository *repo = NULL;
7929 struct got_worktree *worktree = NULL;
7930 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7931 struct got_object_id *commit_id = NULL;
7932 struct got_commit_object *commit = NULL;
7933 const char *commit_id_arg = NULL;
7935 struct got_reference *ref = NULL;
7936 const char *head_ref_name = NULL;
7938 struct tog_view *view;
7939 int *pack_fds = NULL;
7941 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7944 commit_id_arg = optarg;
7947 repo_path = realpath(optarg, NULL);
7948 if (repo_path == NULL)
7949 return got_error_from_errno2("realpath",
7964 error = got_repo_pack_fds_open(&pack_fds);
7968 if (repo_path == NULL) {
7969 cwd = getcwd(NULL, 0);
7971 return got_error_from_errno("getcwd");
7972 error = got_worktree_open(&worktree, cwd);
7973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7977 strdup(got_worktree_get_repo_path(worktree));
7979 repo_path = strdup(cwd);
7980 if (repo_path == NULL) {
7981 error = got_error_from_errno("strdup");
7986 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7990 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7997 error = apply_unveil(got_repo_get_path(repo), NULL);
8001 error = tog_load_refs(repo, 0);
8005 if (commit_id_arg == NULL) {
8006 error = got_repo_match_object_id(&commit_id, &label,
8007 worktree ? got_worktree_get_head_ref_name(worktree) :
8008 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8011 head_ref_name = label;
8013 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8015 head_ref_name = got_ref_get_name(ref);
8016 else if (error->code != GOT_ERR_NOT_REF)
8018 error = got_repo_match_object_id(&commit_id, NULL,
8019 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8024 error = got_object_open_as_commit(&commit, repo, commit_id);
8028 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8030 error = got_error_from_errno("view_open");
8033 error = open_tree_view(view, commit_id, head_ref_name, repo);
8036 if (!got_path_is_root_dir(in_repo_path)) {
8037 error = tree_view_walk_path(&view->state.tree, commit,
8044 /* Release work tree lock. */
8045 got_worktree_close(worktree);
8048 error = view_loop(view);
8057 const struct got_error *close_err = got_repo_close(repo);
8062 const struct got_error *pack_err =
8063 got_repo_pack_fds_close(pack_fds);
8071 static const struct got_error *
8072 ref_view_load_refs(struct tog_ref_view_state *s)
8074 struct got_reflist_entry *sre;
8075 struct tog_reflist_entry *re;
8078 TAILQ_FOREACH(sre, &tog_refs, entry) {
8079 if (strncmp(got_ref_get_name(sre->ref),
8080 "refs/got/", 9) == 0 &&
8081 strncmp(got_ref_get_name(sre->ref),
8082 "refs/got/backup/", 16) != 0)
8085 re = malloc(sizeof(*re));
8087 return got_error_from_errno("malloc");
8089 re->ref = got_ref_dup(sre->ref);
8090 if (re->ref == NULL)
8091 return got_error_from_errno("got_ref_dup");
8092 re->idx = s->nrefs++;
8093 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8096 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8101 ref_view_free_refs(struct tog_ref_view_state *s)
8103 struct tog_reflist_entry *re;
8105 while (!TAILQ_EMPTY(&s->refs)) {
8106 re = TAILQ_FIRST(&s->refs);
8107 TAILQ_REMOVE(&s->refs, re, entry);
8108 got_ref_close(re->ref);
8113 static const struct got_error *
8114 open_ref_view(struct tog_view *view, struct got_repository *repo)
8116 const struct got_error *err = NULL;
8117 struct tog_ref_view_state *s = &view->state.ref;
8119 s->selected_entry = 0;
8122 TAILQ_INIT(&s->refs);
8123 STAILQ_INIT(&s->colors);
8125 err = ref_view_load_refs(s);
8129 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8130 err = add_color(&s->colors, "^refs/heads/",
8131 TOG_COLOR_REFS_HEADS,
8132 get_color_value("TOG_COLOR_REFS_HEADS"));
8136 err = add_color(&s->colors, "^refs/tags/",
8137 TOG_COLOR_REFS_TAGS,
8138 get_color_value("TOG_COLOR_REFS_TAGS"));
8142 err = add_color(&s->colors, "^refs/remotes/",
8143 TOG_COLOR_REFS_REMOTES,
8144 get_color_value("TOG_COLOR_REFS_REMOTES"));
8148 err = add_color(&s->colors, "^refs/got/backup/",
8149 TOG_COLOR_REFS_BACKUP,
8150 get_color_value("TOG_COLOR_REFS_BACKUP"));
8155 view->show = show_ref_view;
8156 view->input = input_ref_view;
8157 view->close = close_ref_view;
8158 view->search_start = search_start_ref_view;
8159 view->search_next = search_next_ref_view;
8162 if (view->close == NULL)
8163 close_ref_view(view);
8169 static const struct got_error *
8170 close_ref_view(struct tog_view *view)
8172 struct tog_ref_view_state *s = &view->state.ref;
8174 ref_view_free_refs(s);
8175 free_colors(&s->colors);
8180 static const struct got_error *
8181 resolve_reflist_entry(struct got_object_id **commit_id,
8182 struct tog_reflist_entry *re, struct got_repository *repo)
8184 const struct got_error *err = NULL;
8185 struct got_object_id *obj_id;
8186 struct got_tag_object *tag = NULL;
8191 err = got_ref_resolve(&obj_id, repo, re->ref);
8195 err = got_object_get_type(&obj_type, repo, obj_id);
8200 case GOT_OBJ_TYPE_COMMIT:
8201 *commit_id = obj_id;
8203 case GOT_OBJ_TYPE_TAG:
8204 err = got_object_open_as_tag(&tag, repo, obj_id);
8208 err = got_object_get_type(&obj_type, repo,
8209 got_object_tag_get_object_id(tag));
8212 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8213 err = got_error(GOT_ERR_OBJ_TYPE);
8216 *commit_id = got_object_id_dup(
8217 got_object_tag_get_object_id(tag));
8218 if (*commit_id == NULL) {
8219 err = got_error_from_errno("got_object_id_dup");
8224 err = got_error(GOT_ERR_OBJ_TYPE);
8230 got_object_tag_close(tag);
8238 static const struct got_error *
8239 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8240 struct tog_reflist_entry *re, struct got_repository *repo)
8242 struct tog_view *log_view;
8243 const struct got_error *err = NULL;
8244 struct got_object_id *commit_id = NULL;
8248 err = resolve_reflist_entry(&commit_id, re, repo);
8250 if (err->code != GOT_ERR_OBJ_TYPE)
8256 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8257 if (log_view == NULL) {
8258 err = got_error_from_errno("view_open");
8262 err = open_log_view(log_view, commit_id, repo,
8263 got_ref_get_name(re->ref), "", 0);
8266 view_close(log_view);
8268 *new_view = log_view;
8274 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8276 struct tog_reflist_entry *re;
8279 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8282 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8283 while (i++ < maxscroll) {
8286 s->first_displayed_entry = re;
8287 re = TAILQ_PREV(re, tog_reflist_head, entry);
8291 static const struct got_error *
8292 ref_scroll_down(struct tog_view *view, int maxscroll)
8294 struct tog_ref_view_state *s = &view->state.ref;
8295 struct tog_reflist_entry *next, *last;
8298 if (s->first_displayed_entry)
8299 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8301 next = TAILQ_FIRST(&s->refs);
8303 last = s->last_displayed_entry;
8304 while (next && n++ < maxscroll) {
8306 s->last_displayed_entry = last;
8307 last = TAILQ_NEXT(last, entry);
8309 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8310 s->first_displayed_entry = next;
8311 next = TAILQ_NEXT(next, entry);
8318 static const struct got_error *
8319 search_start_ref_view(struct tog_view *view)
8321 struct tog_ref_view_state *s = &view->state.ref;
8323 s->matched_entry = NULL;
8328 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8330 regmatch_t regmatch;
8332 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8336 static const struct got_error *
8337 search_next_ref_view(struct tog_view *view)
8339 struct tog_ref_view_state *s = &view->state.ref;
8340 struct tog_reflist_entry *re = NULL;
8342 if (!view->searching) {
8343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8347 if (s->matched_entry) {
8348 if (view->searching == TOG_SEARCH_FORWARD) {
8349 if (s->selected_entry)
8350 re = TAILQ_NEXT(s->selected_entry, entry);
8352 re = TAILQ_PREV(s->selected_entry,
8353 tog_reflist_head, entry);
8355 if (s->selected_entry == NULL)
8356 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8358 re = TAILQ_PREV(s->selected_entry,
8359 tog_reflist_head, entry);
8362 if (s->selected_entry)
8363 re = s->selected_entry;
8364 else if (view->searching == TOG_SEARCH_FORWARD)
8365 re = TAILQ_FIRST(&s->refs);
8367 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8372 if (s->matched_entry == NULL) {
8373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8376 if (view->searching == TOG_SEARCH_FORWARD)
8377 re = TAILQ_FIRST(&s->refs);
8379 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8382 if (match_reflist_entry(re, &view->regex)) {
8383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8384 s->matched_entry = re;
8388 if (view->searching == TOG_SEARCH_FORWARD)
8389 re = TAILQ_NEXT(re, entry);
8391 re = TAILQ_PREV(re, tog_reflist_head, entry);
8394 if (s->matched_entry) {
8395 s->first_displayed_entry = s->matched_entry;
8402 static const struct got_error *
8403 show_ref_view(struct tog_view *view)
8405 const struct got_error *err = NULL;
8406 struct tog_ref_view_state *s = &view->state.ref;
8407 struct tog_reflist_entry *re;
8410 struct tog_color *tc;
8411 int width, n, scrollx;
8412 int limit = view->nlines;
8414 werase(view->window);
8417 if (view_is_hsplit_top(view))
8418 --limit; /* border */
8423 re = s->first_displayed_entry;
8425 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8427 return got_error_from_errno("asprintf");
8429 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8434 if (view_needs_focus_indication(view))
8435 wstandout(view->window);
8436 waddwstr(view->window, wline);
8437 while (width++ < view->ncols)
8438 waddch(view->window, ' ');
8439 if (view_needs_focus_indication(view))
8440 wstandend(view->window);
8450 while (re && limit > 0) {
8452 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8455 struct got_commit_object *ci;
8456 struct got_tag_object *tag;
8457 struct got_object_id *id;
8461 err = got_ref_resolve(&id, s->repo, re->ref);
8464 err = got_object_open_as_tag(&tag, s->repo, id);
8466 if (err->code != GOT_ERR_OBJ_TYPE) {
8470 err = got_object_open_as_commit(&ci, s->repo,
8476 t = got_object_commit_get_committer_time(ci);
8477 got_object_commit_close(ci);
8479 t = got_object_tag_get_tagger_time(tag);
8480 got_object_tag_close(tag);
8483 if (gmtime_r(&t, &tm) == NULL)
8484 return got_error_from_errno("gmtime_r");
8485 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8486 return got_error(GOT_ERR_NO_SPACE);
8488 if (got_ref_is_symbolic(re->ref)) {
8489 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8490 ymd : "", got_ref_get_name(re->ref),
8491 got_ref_get_symref_target(re->ref)) == -1)
8492 return got_error_from_errno("asprintf");
8493 } else if (s->show_ids) {
8494 struct got_object_id *id;
8496 err = got_ref_resolve(&id, s->repo, re->ref);
8499 err = got_object_id_str(&id_str, id);
8504 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8505 got_ref_get_name(re->ref), id_str) == -1) {
8506 err = got_error_from_errno("asprintf");
8513 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8514 got_ref_get_name(re->ref)) == -1)
8515 return got_error_from_errno("asprintf");
8517 /* use full line width to determine view->maxx */
8518 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8523 view->maxx = MAX(view->maxx, width);
8527 err = format_line(&wline, &width, &scrollx, line, view->x,
8533 if (n == s->selected) {
8535 wstandout(view->window);
8536 s->selected_entry = re;
8538 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8540 wattr_on(view->window,
8541 COLOR_PAIR(tc->colorpair), NULL);
8542 waddwstr(view->window, &wline[scrollx]);
8544 wattr_off(view->window,
8545 COLOR_PAIR(tc->colorpair), NULL);
8546 if (width < view->ncols)
8547 waddch(view->window, '\n');
8548 if (n == s->selected && view->focussed)
8549 wstandend(view->window);
8555 s->last_displayed_entry = re;
8558 re = TAILQ_NEXT(re, entry);
8565 static const struct got_error *
8566 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8567 struct tog_reflist_entry *re, struct got_repository *repo)
8569 const struct got_error *err = NULL;
8570 struct got_object_id *commit_id = NULL;
8571 struct tog_view *tree_view;
8575 err = resolve_reflist_entry(&commit_id, re, repo);
8577 if (err->code != GOT_ERR_OBJ_TYPE)
8584 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8585 if (tree_view == NULL) {
8586 err = got_error_from_errno("view_open");
8590 err = open_tree_view(tree_view, commit_id,
8591 got_ref_get_name(re->ref), repo);
8595 *new_view = tree_view;
8601 static const struct got_error *
8602 ref_goto_line(struct tog_view *view, int nlines)
8604 const struct got_error *err = NULL;
8605 struct tog_ref_view_state *s = &view->state.ref;
8606 int g, idx = s->selected_entry->idx;
8613 else if (g > s->nrefs)
8616 if (g >= s->first_displayed_entry->idx + 1 &&
8617 g <= s->last_displayed_entry->idx + 1 &&
8618 g - s->first_displayed_entry->idx - 1 < nlines) {
8619 s->selected = g - s->first_displayed_entry->idx - 1;
8624 err = ref_scroll_down(view, g - idx - 1);
8627 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8628 s->first_displayed_entry->idx + s->selected < g &&
8629 s->selected < s->ndisplayed - 1)
8630 s->selected = g - s->first_displayed_entry->idx - 1;
8631 } else if (idx + 1 > g)
8632 ref_scroll_up(s, idx - g + 1);
8634 if (g < nlines && s->first_displayed_entry->idx == 0)
8635 s->selected = g - 1;
8641 static const struct got_error *
8642 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8644 const struct got_error *err = NULL;
8645 struct tog_ref_view_state *s = &view->state.ref;
8646 struct tog_reflist_entry *re;
8647 int n, nscroll = view->nlines - 1;
8650 return ref_goto_line(view, nscroll);
8659 horizontal_scroll_input(view, ch);
8662 s->show_ids = !s->show_ids;
8666 s->show_date = !s->show_date;
8670 s->sort_by_date = !s->sort_by_date;
8671 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8673 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8674 got_ref_cmp_by_commit_timestamp_descending :
8675 tog_ref_cmp_by_name, s->repo);
8678 got_reflist_object_id_map_free(tog_refs_idmap);
8679 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8680 &tog_refs, s->repo);
8683 ref_view_free_refs(s);
8684 err = ref_view_load_refs(s);
8689 if (!s->selected_entry)
8691 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8695 if (!s->selected_entry)
8697 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8704 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8709 int eos = view->nlines - 1;
8711 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8715 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8716 for (n = 0; n < eos; n++) {
8719 s->first_displayed_entry = re;
8720 re = TAILQ_PREV(re, tog_reflist_head, entry);
8723 s->selected = n - 1;
8729 if (s->selected > 0) {
8733 ref_scroll_up(s, 1);
8734 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8744 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8745 s->selected -= MIN(nscroll, s->selected);
8746 ref_scroll_up(s, MAX(0, nscroll));
8747 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8753 if (s->selected < s->ndisplayed - 1) {
8757 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8758 /* can't scroll any further */
8762 ref_scroll_down(view, 1);
8772 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8773 /* can't scroll any further; move cursor down */
8774 if (s->selected < s->ndisplayed - 1)
8775 s->selected += MIN(nscroll,
8776 s->ndisplayed - s->selected - 1);
8777 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8778 s->selected += s->ndisplayed - s->selected - 1;
8782 ref_scroll_down(view, nscroll);
8787 err = tog_load_refs(s->repo, s->sort_by_date);
8790 ref_view_free_refs(s);
8791 err = ref_view_load_refs(s);
8794 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8795 s->selected = view->nlines - 2;
8809 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8814 static const struct got_error *
8815 cmd_ref(int argc, char *argv[])
8817 const struct got_error *error;
8818 struct got_repository *repo = NULL;
8819 struct got_worktree *worktree = NULL;
8820 char *cwd = NULL, *repo_path = NULL;
8822 struct tog_view *view;
8823 int *pack_fds = NULL;
8825 while ((ch = getopt(argc, argv, "r:")) != -1) {
8828 repo_path = realpath(optarg, NULL);
8829 if (repo_path == NULL)
8830 return got_error_from_errno2("realpath",
8845 error = got_repo_pack_fds_open(&pack_fds);
8849 if (repo_path == NULL) {
8850 cwd = getcwd(NULL, 0);
8852 return got_error_from_errno("getcwd");
8853 error = got_worktree_open(&worktree, cwd);
8854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8858 strdup(got_worktree_get_repo_path(worktree));
8860 repo_path = strdup(cwd);
8861 if (repo_path == NULL) {
8862 error = got_error_from_errno("strdup");
8867 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8873 error = apply_unveil(got_repo_get_path(repo), NULL);
8877 error = tog_load_refs(repo, 0);
8881 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8883 error = got_error_from_errno("view_open");
8887 error = open_ref_view(view, repo);
8892 /* Release work tree lock. */
8893 got_worktree_close(worktree);
8896 error = view_loop(view);
8901 const struct got_error *close_err = got_repo_close(repo);
8906 const struct got_error *pack_err =
8907 got_repo_pack_fds_close(pack_fds);
8915 static const struct got_error*
8916 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8925 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8929 if (mvwprintw(win, y, x, "%s", str) == ERR)
8930 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8937 static const struct got_error *
8938 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8942 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8944 free(*line_offsets);
8945 *line_offsets = NULL;
8946 return got_error_from_errno("reallocarray");
8950 (*line_offsets)[*nlines] = off;
8955 static const struct got_error *
8956 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8960 for (;n > 0; --n, ++km) {
8964 if (km->keys == NULL)
8967 t = t0 = strdup(km->keys);
8969 return got_error_from_errno("strdup");
8972 while ((k = strsep(&t, " ")) != NULL)
8973 len += strlen(k) > 1 ? 2 : 0;
8975 *ret = MAX(*ret, len);
8982 * Write keymap section headers, keys, and key info in km to f.
8983 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8984 * wrap control and symbolic keys in guillemets, else use <>.
8986 static const struct got_error *
8987 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8992 static const char *u8_glyph[] = {
8993 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8994 "\xe2\x80\xba" /* U+203A (utf8 >) */
8997 int cs, s, first = 1;
8999 cs = got_locale_is_utf8();
9001 t = t0 = strdup(km->keys);
9003 return got_error_from_errno("strdup");
9005 len = strlen(km->keys);
9006 while ((k = strsep(&t, " ")) != NULL) {
9007 s = strlen(k) > 1; /* control or symbolic key */
9008 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9009 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9010 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9013 return got_error_from_errno("fprintf");
9021 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9023 return got_error_from_errno("fprintf");
9029 static const struct got_error *
9030 format_help(struct tog_help_view_state *s)
9032 const struct got_error *err = NULL;
9034 int i, max, n, show = s->all;
9035 static const struct tog_key_map km[] = {
9036 #define KEYMAP_(info, type) { NULL, (info), type }
9037 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9043 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9048 err = max_key_str(&max, km, n);
9052 for (i = 0; i < n; ++i) {
9053 if (km[i].keys == NULL) {
9055 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9056 km[i].type == s->type || s->all)
9060 err = format_help_line(&off, s->f, &km[i], max);
9063 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9070 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9074 static const struct got_error *
9075 create_help(struct tog_help_view_state *s)
9078 const struct got_error *err;
9080 free(s->line_offsets);
9081 s->line_offsets = NULL;
9086 return got_error_from_errno("got_opentemp");
9089 err = format_help(s);
9093 if (s->f && fflush(s->f) != 0)
9094 return got_error_from_errno("fflush");
9099 static const struct got_error *
9100 search_start_help_view(struct tog_view *view)
9102 view->state.help.matched_line = 0;
9107 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9108 size_t *nlines, int **first, int **last, int **match, int **selected)
9110 struct tog_help_view_state *s = &view->state.help;
9113 *nlines = s->nlines;
9114 *line_offsets = s->line_offsets;
9115 *match = &s->matched_line;
9116 *first = &s->first_displayed_line;
9117 *last = &s->last_displayed_line;
9118 *selected = &s->selected_line;
9121 static const struct got_error *
9122 show_help_view(struct tog_view *view)
9124 struct tog_help_view_state *s = &view->state.help;
9125 const struct got_error *err;
9126 regmatch_t *regmatch = &view->regmatch;
9131 int width, nprinted = 0, rc = 0;
9132 int eos = view->nlines;
9134 if (view_is_hsplit_top(view))
9135 --eos; /* account for border */
9139 werase(view->window);
9141 if (view->gline > s->nlines - 1)
9142 view->gline = s->nlines - 1;
9144 err = win_draw_center(view->window, 0, 0, view->ncols,
9145 view_needs_focus_indication(view),
9146 "tog help (press q to return to tog)");
9151 waddstr(view->window, "\n\n");
9157 while (eos > 0 && nprinted < eos) {
9160 linelen = getline(&line, &linesz, s->f);
9161 if (linelen == -1) {
9164 return got_ferror(s->f, GOT_ERR_IO);
9169 if (++s->lineno < s->first_displayed_line)
9171 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9173 if (s->lineno == view->hiline)
9176 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9182 view->maxx = MAX(view->maxx, width);
9187 wattron(view->window, attr);
9188 if (s->first_displayed_line + nprinted == s->matched_line &&
9189 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9190 err = add_matched_line(&width, line, view->ncols - 1, 0,
9191 view->window, view->x, regmatch);
9199 err = format_line(&wline, &width, &skip, line,
9200 view->x, view->ncols, 0, view->x ? 1 : 0);
9205 waddwstr(view->window, &wline[skip]);
9209 if (s->lineno == view->hiline) {
9210 while (width++ < view->ncols)
9211 waddch(view->window, ' ');
9213 if (width < view->ncols)
9214 waddch(view->window, '\n');
9217 wattroff(view->window, attr);
9218 if (++nprinted == 1)
9219 s->first_displayed_line = s->lineno;
9223 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9225 s->last_displayed_line = s->first_displayed_line;
9230 rc = waddnstr(view->window,
9231 "See the tog(1) manual page for full documentation",
9234 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9236 wmove(view->window, view->nlines - 1, 0);
9237 wclrtoeol(view->window);
9238 wstandout(view->window);
9239 rc = waddnstr(view->window, "scroll down for more...",
9242 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9243 if (getcurx(view->window) < view->ncols - 6) {
9244 rc = wprintw(view->window, "[%.0f%%]",
9245 100.00 * s->last_displayed_line / s->nlines);
9247 return got_error_msg(GOT_ERR_IO, "wprintw");
9249 wstandend(view->window);
9255 static const struct got_error *
9256 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9258 struct tog_help_view_state *s = &view->state.help;
9259 const struct got_error *err = NULL;
9265 eos = nscroll = view->nlines;
9266 if (view_is_hsplit_top(view))
9269 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9278 horizontal_scroll_input(view, ch);
9282 s->first_displayed_line = 1;
9290 s->first_displayed_line = (s->nlines - eos) + 3;
9295 if (s->first_displayed_line > 1)
9296 --s->first_displayed_line;
9307 if (s->first_displayed_line == 1) {
9311 while (--nscroll > 0 && s->first_displayed_line > 1)
9312 s->first_displayed_line--;
9318 ++s->first_displayed_line;
9334 while (!s->eof && --nscroll > 0) {
9335 linelen = getline(&line, &linesz, s->f);
9336 s->first_displayed_line++;
9337 if (linelen == -1) {
9341 err = got_ferror(s->f, GOT_ERR_IO);
9355 static const struct got_error *
9356 close_help_view(struct tog_view *view)
9358 struct tog_help_view_state *s = &view->state.help;
9360 free(s->line_offsets);
9361 s->line_offsets = NULL;
9362 if (fclose(s->f) == EOF)
9363 return got_error_from_errno("fclose");
9368 static const struct got_error *
9369 reset_help_view(struct tog_view *view)
9371 struct tog_help_view_state *s = &view->state.help;
9374 if (s->f && fclose(s->f) == EOF)
9375 return got_error_from_errno("fclose");
9377 wclear(view->window);
9381 s->first_displayed_line = 1;
9382 s->last_displayed_line = view->nlines;
9383 s->matched_line = 0;
9385 return create_help(s);
9388 static const struct got_error *
9389 open_help_view(struct tog_view *view, struct tog_view *parent)
9391 const struct got_error *err = NULL;
9392 struct tog_help_view_state *s = &view->state.help;
9394 s->type = (enum tog_keymap_type)parent->type;
9395 s->first_displayed_line = 1;
9396 s->last_displayed_line = view->nlines;
9397 s->selected_line = 1;
9399 view->show = show_help_view;
9400 view->input = input_help_view;
9401 view->reset = reset_help_view;
9402 view->close = close_help_view;
9403 view->search_start = search_start_help_view;
9404 view->search_setup = search_setup_help_view;
9405 view->search_next = search_next_view_match;
9407 err = create_help(s);
9411 static const struct got_error *
9412 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9413 enum tog_view_type request, int y, int x)
9415 const struct got_error *err = NULL;
9421 if (view->type == TOG_VIEW_LOG) {
9422 struct tog_log_view_state *s = &view->state.log;
9424 err = open_diff_view_for_commit(new_view, y, x,
9425 s->selected_entry->commit, s->selected_entry->id,
9428 return got_error_msg(GOT_ERR_NOT_IMPL,
9429 "parent/child view pair not supported");
9431 case TOG_VIEW_BLAME:
9432 if (view->type == TOG_VIEW_TREE) {
9433 struct tog_tree_view_state *s = &view->state.tree;
9435 err = blame_tree_entry(new_view, y, x,
9436 s->selected_entry, &s->parents, s->commit_id,
9439 return got_error_msg(GOT_ERR_NOT_IMPL,
9440 "parent/child view pair not supported");
9443 if (view->type == TOG_VIEW_BLAME)
9444 err = log_annotated_line(new_view, y, x,
9445 view->state.blame.repo, view->state.blame.id_to_log);
9446 else if (view->type == TOG_VIEW_TREE)
9447 err = log_selected_tree_entry(new_view, y, x,
9449 else if (view->type == TOG_VIEW_REF)
9450 err = log_ref_entry(new_view, y, x,
9451 view->state.ref.selected_entry,
9452 view->state.ref.repo);
9454 return got_error_msg(GOT_ERR_NOT_IMPL,
9455 "parent/child view pair not supported");
9458 if (view->type == TOG_VIEW_LOG)
9459 err = browse_commit_tree(new_view, y, x,
9460 view->state.log.selected_entry,
9461 view->state.log.in_repo_path,
9462 view->state.log.head_ref_name,
9463 view->state.log.repo);
9464 else if (view->type == TOG_VIEW_REF)
9465 err = browse_ref_tree(new_view, y, x,
9466 view->state.ref.selected_entry,
9467 view->state.ref.repo);
9469 return got_error_msg(GOT_ERR_NOT_IMPL,
9470 "parent/child view pair not supported");
9473 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9474 if (*new_view == NULL)
9475 return got_error_from_errno("view_open");
9476 if (view->type == TOG_VIEW_LOG)
9477 err = open_ref_view(*new_view, view->state.log.repo);
9478 else if (view->type == TOG_VIEW_TREE)
9479 err = open_ref_view(*new_view, view->state.tree.repo);
9481 err = got_error_msg(GOT_ERR_NOT_IMPL,
9482 "parent/child view pair not supported");
9484 view_close(*new_view);
9487 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9488 if (*new_view == NULL)
9489 return got_error_from_errno("view_open");
9490 err = open_help_view(*new_view, view);
9492 view_close(*new_view);
9495 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9502 * If view was scrolled down to move the selected line into view when opening a
9503 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9506 offset_selection_up(struct tog_view *view)
9508 switch (view->type) {
9509 case TOG_VIEW_BLAME: {
9510 struct tog_blame_view_state *s = &view->state.blame;
9511 if (s->first_displayed_line == 1) {
9512 s->selected_line = MAX(s->selected_line - view->offset,
9516 if (s->first_displayed_line > view->offset)
9517 s->first_displayed_line -= view->offset;
9519 s->first_displayed_line = 1;
9520 s->selected_line += view->offset;
9524 log_scroll_up(&view->state.log, view->offset);
9525 view->state.log.selected += view->offset;
9528 ref_scroll_up(&view->state.ref, view->offset);
9529 view->state.ref.selected += view->offset;
9532 tree_scroll_up(&view->state.tree, view->offset);
9533 view->state.tree.selected += view->offset;
9543 * If the selected line is in the section of screen covered by the bottom split,
9544 * scroll down offset lines to move it into view and index its new position.
9546 static const struct got_error *
9547 offset_selection_down(struct tog_view *view)
9549 const struct got_error *err = NULL;
9550 const struct got_error *(*scrolld)(struct tog_view *, int);
9551 int *selected = NULL;
9554 switch (view->type) {
9555 case TOG_VIEW_BLAME: {
9556 struct tog_blame_view_state *s = &view->state.blame;
9559 if (s->selected_line > view->nlines - header) {
9560 offset = abs(view->nlines - s->selected_line - header);
9561 s->first_displayed_line += offset;
9562 s->selected_line -= offset;
9563 view->offset = offset;
9567 case TOG_VIEW_LOG: {
9568 struct tog_log_view_state *s = &view->state.log;
9569 scrolld = &log_scroll_down;
9570 header = view_is_parent_view(view) ? 3 : 2;
9571 selected = &s->selected;
9574 case TOG_VIEW_REF: {
9575 struct tog_ref_view_state *s = &view->state.ref;
9576 scrolld = &ref_scroll_down;
9578 selected = &s->selected;
9581 case TOG_VIEW_TREE: {
9582 struct tog_tree_view_state *s = &view->state.tree;
9583 scrolld = &tree_scroll_down;
9585 selected = &s->selected;
9595 if (selected && *selected > view->nlines - header) {
9596 offset = abs(view->nlines - *selected - header);
9597 view->offset = offset;
9598 if (scrolld && offset) {
9599 err = scrolld(view, offset);
9600 *selected -= offset;
9608 list_commands(FILE *fp)
9612 fprintf(fp, "commands:");
9613 for (i = 0; i < nitems(tog_commands); i++) {
9614 const struct tog_cmd *cmd = &tog_commands[i];
9615 fprintf(fp, " %s", cmd->name);
9621 usage(int hflag, int status)
9623 FILE *fp = (status == 0) ? stdout : stderr;
9625 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9628 fprintf(fp, "lazy usage: %s path\n", getprogname());
9635 make_argv(int argc, ...)
9643 argv = calloc(argc, sizeof(char *));
9646 for (i = 0; i < argc; i++) {
9647 argv[i] = strdup(va_arg(ap, char *));
9648 if (argv[i] == NULL)
9657 * Try to convert 'tog path' into a 'tog log path' command.
9658 * The user could simply have mistyped the command rather than knowingly
9659 * provided a path. So check whether argv[0] can in fact be resolved
9660 * to a path in the HEAD commit and print a special error if not.
9661 * This hack is for mpi@ <3
9663 static const struct got_error *
9664 tog_log_with_path(int argc, char *argv[])
9666 const struct got_error *error = NULL, *close_err;
9667 const struct tog_cmd *cmd = NULL;
9668 struct got_repository *repo = NULL;
9669 struct got_worktree *worktree = NULL;
9670 struct got_object_id *commit_id = NULL, *id = NULL;
9671 struct got_commit_object *commit = NULL;
9672 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9673 char *commit_id_str = NULL, **cmd_argv = NULL;
9674 int *pack_fds = NULL;
9676 cwd = getcwd(NULL, 0);
9678 return got_error_from_errno("getcwd");
9680 error = got_repo_pack_fds_open(&pack_fds);
9684 error = got_worktree_open(&worktree, cwd);
9685 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9689 repo_path = strdup(got_worktree_get_repo_path(worktree));
9691 repo_path = strdup(cwd);
9692 if (repo_path == NULL) {
9693 error = got_error_from_errno("strdup");
9697 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9701 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9706 error = tog_load_refs(repo, 0);
9709 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9710 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9711 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9716 got_worktree_close(worktree);
9720 error = got_object_open_as_commit(&commit, repo, commit_id);
9724 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9726 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9728 fprintf(stderr, "%s: '%s' is no known command or path\n",
9729 getprogname(), argv[0]);
9734 error = got_object_id_str(&commit_id_str, commit_id);
9738 cmd = &tog_commands[0]; /* log */
9740 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9741 error = cmd->cmd_main(argc, cmd_argv);
9744 close_err = got_repo_close(repo);
9749 got_object_commit_close(commit);
9751 got_worktree_close(worktree);
9753 const struct got_error *pack_err =
9754 got_repo_pack_fds_close(pack_fds);
9759 free(commit_id_str);
9766 for (i = 0; i < argc; i++)
9775 main(int argc, char *argv[])
9777 const struct got_error *io_err, *error = NULL;
9778 const struct tog_cmd *cmd = NULL;
9779 int ch, hflag = 0, Vflag = 0;
9780 char **cmd_argv = NULL;
9781 static const struct option longopts[] = {
9782 { "version", no_argument, NULL, 'V' },
9785 char *diff_algo_str = NULL;
9786 const char *test_script_path;
9788 setlocale(LC_CTYPE, "");
9791 * Test mode init must happen before pledge() because "tty" will
9792 * not allow TTY-related ioctls to occur via regular files.
9794 test_script_path = getenv("TOG_TEST_SCRIPT");
9795 if (test_script_path != NULL) {
9796 error = init_mock_term(test_script_path);
9798 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9803 #if !defined(PROFILE)
9804 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9809 if (!isatty(STDIN_FILENO))
9810 errx(1, "standard input is not a tty");
9812 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9832 got_version_print_str();
9839 /* Build an argument vector which runs a default command. */
9840 cmd = &tog_commands[0];
9842 cmd_argv = make_argv(argc, cmd->name);
9846 /* Did the user specify a command? */
9847 for (i = 0; i < nitems(tog_commands); i++) {
9848 if (strncmp(tog_commands[i].name, argv[0],
9849 strlen(argv[0])) == 0) {
9850 cmd = &tog_commands[i];
9856 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9857 if (diff_algo_str) {
9858 if (strcasecmp(diff_algo_str, "patience") == 0)
9859 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9860 if (strcasecmp(diff_algo_str, "myers") == 0)
9861 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9867 /* No command specified; try log with a path */
9868 error = tog_log_with_path(argc, argv);
9873 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9876 if (using_mock_io) {
9877 io_err = tog_io_close();
9884 for (i = 0; i < argc; i++)
9889 if (error && error->code != GOT_ERR_CANCELLED &&
9890 error->code != GOT_ERR_EOF &&
9891 error->code != GOT_ERR_PRIVSEP_EXIT &&
9892 error->code != GOT_ERR_PRIVSEP_PIPE &&
9893 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9894 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);