2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/ioctl.h>
25 #if defined(__FreeBSD__) || defined(__APPLE__)
26 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
145 struct commit_queue_head head;
149 STAILQ_ENTRY(tog_color) entry;
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
173 } else if (isbackup1 && !isbackup2) {
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
237 STAILQ_INSERT_HEAD(colors, tc, entry);
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
314 if (strcasecmp(val, "red") == 0)
316 if (strcasecmp(val, "green") == 0)
318 if (strcasecmp(val, "yellow") == 0)
320 if (strcasecmp(val, "blue") == 0)
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
326 if (strcasecmp(val, "white") == 0)
328 if (strcasecmp(val, "default") == 0)
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
340 int first_displayed_line;
341 int last_displayed_line;
344 int ignore_whitespace;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
375 int *search_next_done;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
404 struct commit_queue limit_commits;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
427 struct tog_view *view;
428 struct got_object_id *commit_id;
432 struct tog_blame_thread_args {
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
437 got_cancel_cb cancel_cb;
444 struct tog_blame_line *lines;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
458 int last_diffed_line;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
470 struct tog_colors colors;
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
518 struct tog_help_view_state {
523 int first_displayed_line;
524 int last_displayed_line;
529 enum tog_keymap_type type;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g", "Go to line N (default: first line)"), \
542 KEY_("Home =", "Go to the first line"), \
543 KEY_("G", "Go to line N (default: last line)"), \
544 KEY_("End *", "Go to the last line"), \
545 KEY_("l Right", "Scroll the view right"), \
546 KEY_("h Left", "Scroll the view left"), \
547 KEY_("$", "Scroll view to the rightmost position"), \
548 KEY_("0", "Scroll view to the leftmost position"), \
549 KEY_("-", "Decrease size of the focussed split"), \
550 KEY_("+", "Increase size of the focussed split"), \
551 KEY_("Tab", "Switch focus between views"), \
552 KEY_("F", "Toggle fullscreen mode"), \
553 KEY_("/", "Open prompt to enter search term"), \
554 KEY_("n", "Find next line/token matching the current search term"), \
555 KEY_("N", "Find previous line/token matching the current search term"),\
556 KEY_("q", "Quit the focussed view; Quit help screen"), \
557 KEY_("Q", "Quit tog"), \
559 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 KEY_("< ,", "Move cursor up one commit"), \
561 KEY_("> .", "Move cursor down one commit"), \
562 KEY_("Enter", "Open diff view of the selected commit"), \
563 KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 KEY_("R", "Open ref view of all repository references"), \
565 KEY_("T", "Display tree view of the repository from the selected" \
567 KEY_("@", "Toggle between displaying author and committer name"), \
568 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
578 KEY_("(", "Go to the previous file in the diff"), \
579 KEY_(")", "Go to the next file in the diff"), \
580 KEY_("{", "Go to the previous hunk in the diff"), \
581 KEY_("}", "Go to the next hunk in the diff"), \
582 KEY_("[", "Decrease the number of context lines"), \
583 KEY_("]", "Increase the number of context lines"), \
584 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 KEY_("Enter", "Display diff view of the selected line's commit"), \
588 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 KEY_("L", "Open log view for the currently selected annotated line"), \
590 KEY_("C", "Reload view with the previously blamed commit"), \
591 KEY_("c", "Reload view with the version of the file found in the" \
592 " selected line's commit"), \
593 KEY_("p", "Reload view with the version of the file found in the" \
594 " selected line's parent commit"), \
596 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 KEY_("Enter", "Enter selected directory or open blame view of the" \
599 KEY_("L", "Open log view for the selected entry"), \
600 KEY_("R", "Open ref view of all repository references"), \
601 KEY_("i", "Show object IDs for all tree entries"), \
602 KEY_("Backspace", "Return to the parent directory"), \
604 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 KEY_("Enter", "Display log view of the selected reference"), \
606 KEY_("T", "Display tree view of the selected reference"), \
607 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 KEY_("m", "Toggle display of last modified date for each reference"), \
609 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 KEY_("C-l", "Reload view with all repository references")
615 enum tog_keymap_type type;
618 /* curses io for tog regress */
625 #define TOG_SCREEN_DUMP "SCREENDUMP"
626 #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
627 #define TOG_KEY_SCRDUMP SHRT_MIN
630 * We implement two types of views: parent views and child views.
632 * The 'Tab' key switches focus between a parent view and its child view.
633 * Child views are shown side-by-side to their parent view, provided
634 * there is enough screen estate.
636 * When a new view is opened from within a parent view, this new view
637 * becomes a child view of the parent view, replacing any existing child.
639 * When a new view is opened from within a child view, this new view
640 * becomes a parent view which will obscure the views below until the
641 * user quits the new parent view by typing 'q'.
643 * This list of views contains parent views only.
644 * Child views are only pointed to by their parent view.
646 TAILQ_HEAD(tog_view_list_head, tog_view);
649 TAILQ_ENTRY(tog_view) entry;
652 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
653 int resized_y, resized_x; /* begin_y/x based on user resizing */
654 int maxx, x; /* max column and current start column */
655 int lines, cols; /* copies of LINES and COLS */
656 int nscrolled, offset; /* lines scrolled and hsplit line offset */
657 int gline, hiline; /* navigate to and highlight this nG line */
658 int ch, count; /* current keymap and count prefix */
659 int resized; /* set when in a resize event */
660 int focussed; /* Only set on one parent or child view at a time. */
662 struct tog_view *parent;
663 struct tog_view *child;
666 * This flag is initially set on parent views when a new child view
667 * is created. It gets toggled when the 'Tab' key switches focus
668 * between parent and child.
669 * The flag indicates whether focus should be passed on to our child
670 * view if this parent view gets picked for focus after another parent
671 * view was closed. This prevents child views from losing focus in such
676 enum tog_view_mode mode;
677 /* type-specific state */
678 enum tog_view_type type;
680 struct tog_diff_view_state diff;
681 struct tog_log_view_state log;
682 struct tog_blame_view_state blame;
683 struct tog_tree_view_state tree;
684 struct tog_ref_view_state ref;
685 struct tog_help_view_state help;
688 const struct got_error *(*show)(struct tog_view *);
689 const struct got_error *(*input)(struct tog_view **,
690 struct tog_view *, int);
691 const struct got_error *(*reset)(struct tog_view *);
692 const struct got_error *(*resize)(struct tog_view *, int);
693 const struct got_error *(*close)(struct tog_view *);
695 const struct got_error *(*search_start)(struct tog_view *);
696 const struct got_error *(*search_next)(struct tog_view *);
697 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
698 int **, int **, int **, int **);
701 #define TOG_SEARCH_FORWARD 1
702 #define TOG_SEARCH_BACKWARD 2
703 int search_next_done;
704 #define TOG_SEARCH_HAVE_MORE 1
705 #define TOG_SEARCH_NO_MORE 2
706 #define TOG_SEARCH_HAVE_NONE 3
712 static const struct got_error *open_diff_view(struct tog_view *,
713 struct got_object_id *, struct got_object_id *,
714 const char *, const char *, int, int, int, struct tog_view *,
715 struct got_repository *);
716 static const struct got_error *show_diff_view(struct tog_view *);
717 static const struct got_error *input_diff_view(struct tog_view **,
718 struct tog_view *, int);
719 static const struct got_error *reset_diff_view(struct tog_view *);
720 static const struct got_error* close_diff_view(struct tog_view *);
721 static const struct got_error *search_start_diff_view(struct tog_view *);
722 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
723 size_t *, int **, int **, int **, int **);
724 static const struct got_error *search_next_view_match(struct tog_view *);
726 static const struct got_error *open_log_view(struct tog_view *,
727 struct got_object_id *, struct got_repository *,
728 const char *, const char *, int);
729 static const struct got_error * show_log_view(struct tog_view *);
730 static const struct got_error *input_log_view(struct tog_view **,
731 struct tog_view *, int);
732 static const struct got_error *resize_log_view(struct tog_view *, int);
733 static const struct got_error *close_log_view(struct tog_view *);
734 static const struct got_error *search_start_log_view(struct tog_view *);
735 static const struct got_error *search_next_log_view(struct tog_view *);
737 static const struct got_error *open_blame_view(struct tog_view *, char *,
738 struct got_object_id *, struct got_repository *);
739 static const struct got_error *show_blame_view(struct tog_view *);
740 static const struct got_error *input_blame_view(struct tog_view **,
741 struct tog_view *, int);
742 static const struct got_error *reset_blame_view(struct tog_view *);
743 static const struct got_error *close_blame_view(struct tog_view *);
744 static const struct got_error *search_start_blame_view(struct tog_view *);
745 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
746 size_t *, int **, int **, int **, int **);
748 static const struct got_error *open_tree_view(struct tog_view *,
749 struct got_object_id *, const char *, struct got_repository *);
750 static const struct got_error *show_tree_view(struct tog_view *);
751 static const struct got_error *input_tree_view(struct tog_view **,
752 struct tog_view *, int);
753 static const struct got_error *close_tree_view(struct tog_view *);
754 static const struct got_error *search_start_tree_view(struct tog_view *);
755 static const struct got_error *search_next_tree_view(struct tog_view *);
757 static const struct got_error *open_ref_view(struct tog_view *,
758 struct got_repository *);
759 static const struct got_error *show_ref_view(struct tog_view *);
760 static const struct got_error *input_ref_view(struct tog_view **,
761 struct tog_view *, int);
762 static const struct got_error *close_ref_view(struct tog_view *);
763 static const struct got_error *search_start_ref_view(struct tog_view *);
764 static const struct got_error *search_next_ref_view(struct tog_view *);
766 static const struct got_error *open_help_view(struct tog_view *,
768 static const struct got_error *show_help_view(struct tog_view *);
769 static const struct got_error *input_help_view(struct tog_view **,
770 struct tog_view *, int);
771 static const struct got_error *reset_help_view(struct tog_view *);
772 static const struct got_error* close_help_view(struct tog_view *);
773 static const struct got_error *search_start_help_view(struct tog_view *);
774 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
775 size_t *, int **, int **, int **, int **);
777 static volatile sig_atomic_t tog_sigwinch_received;
778 static volatile sig_atomic_t tog_sigpipe_received;
779 static volatile sig_atomic_t tog_sigcont_received;
780 static volatile sig_atomic_t tog_sigint_received;
781 static volatile sig_atomic_t tog_sigterm_received;
784 tog_sigwinch(int signo)
786 tog_sigwinch_received = 1;
790 tog_sigpipe(int signo)
792 tog_sigpipe_received = 1;
796 tog_sigcont(int signo)
798 tog_sigcont_received = 1;
802 tog_sigint(int signo)
804 tog_sigint_received = 1;
808 tog_sigterm(int signo)
810 tog_sigterm_received = 1;
814 tog_fatal_signal_received(void)
816 return (tog_sigpipe_received ||
817 tog_sigint_received || tog_sigterm_received);
820 static const struct got_error *
821 view_close(struct tog_view *view)
823 const struct got_error *err = NULL, *child_err = NULL;
826 child_err = view_close(view->child);
830 err = view->close(view);
832 del_panel(view->panel);
834 delwin(view->window);
836 return err ? err : child_err;
839 static struct tog_view *
840 view_open(int nlines, int ncols, int begin_y, int begin_x,
841 enum tog_view_type type)
843 struct tog_view *view = calloc(1, sizeof(*view));
851 view->nlines = nlines ? nlines : LINES - begin_y;
852 view->ncols = ncols ? ncols : COLS - begin_x;
853 view->begin_y = begin_y;
854 view->begin_x = begin_x;
855 view->window = newwin(nlines, ncols, begin_y, begin_x);
856 if (view->window == NULL) {
860 view->panel = new_panel(view->window);
861 if (view->panel == NULL ||
862 set_panel_userptr(view->panel, view) != OK) {
867 keypad(view->window, TRUE);
872 view_split_begin_x(int begin_x)
874 if (begin_x > 0 || COLS < 120)
876 return (COLS - MAX(COLS / 2, 80));
879 /* XXX Stub till we decide what to do. */
881 view_split_begin_y(int lines)
883 return lines * HSPLIT_SCALE;
886 static const struct got_error *view_resize(struct tog_view *);
888 static const struct got_error *
889 view_splitscreen(struct tog_view *view)
891 const struct got_error *err = NULL;
893 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
894 if (view->resized_y && view->resized_y < view->lines)
895 view->begin_y = view->resized_y;
897 view->begin_y = view_split_begin_y(view->nlines);
899 } else if (!view->resized) {
900 if (view->resized_x && view->resized_x < view->cols - 1 &&
902 view->begin_x = view->resized_x;
904 view->begin_x = view_split_begin_x(0);
907 view->nlines = LINES - view->begin_y;
908 view->ncols = COLS - view->begin_x;
911 err = view_resize(view);
915 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
916 view->parent->nlines = view->begin_y;
918 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
919 return got_error_from_errno("mvwin");
924 static const struct got_error *
925 view_fullscreen(struct tog_view *view)
927 const struct got_error *err = NULL;
930 view->begin_y = view->resized ? view->begin_y : 0;
931 view->nlines = view->resized ? view->nlines : LINES;
935 err = view_resize(view);
939 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
940 return got_error_from_errno("mvwin");
946 view_is_parent_view(struct tog_view *view)
948 return view->parent == NULL;
952 view_is_splitscreen(struct tog_view *view)
954 return view->begin_x > 0 || view->begin_y > 0;
958 view_is_fullscreen(struct tog_view *view)
960 return view->nlines == LINES && view->ncols == COLS;
964 view_is_hsplit_top(struct tog_view *view)
966 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
967 view_is_splitscreen(view->child);
971 view_border(struct tog_view *view)
974 const struct tog_view *view_above;
977 return view_border(view->parent);
979 panel = panel_above(view->panel);
983 view_above = panel_userptr(panel);
984 if (view->mode == TOG_VIEW_SPLIT_HRZN)
985 mvwhline(view->window, view_above->begin_y - 1,
986 view->begin_x, got_locale_is_utf8() ?
987 ACS_HLINE : '-', view->ncols);
989 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
990 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
993 static const struct got_error *view_init_hsplit(struct tog_view *, int);
994 static const struct got_error *request_log_commits(struct tog_view *);
995 static const struct got_error *offset_selection_down(struct tog_view *);
996 static void offset_selection_up(struct tog_view *);
997 static void view_get_split(struct tog_view *, int *, int *);
999 static const struct got_error *
1000 view_resize(struct tog_view *view)
1002 const struct got_error *err = NULL;
1003 int dif, nlines, ncols;
1005 dif = LINES - view->lines; /* line difference */
1007 if (view->lines > LINES)
1008 nlines = view->nlines - (view->lines - LINES);
1010 nlines = view->nlines + (LINES - view->lines);
1011 if (view->cols > COLS)
1012 ncols = view->ncols - (view->cols - COLS);
1014 ncols = view->ncols + (COLS - view->cols);
1017 int hs = view->child->begin_y;
1019 if (!view_is_fullscreen(view))
1020 view->child->begin_x = view_split_begin_x(view->begin_x);
1021 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1022 view->child->begin_x == 0) {
1025 view_fullscreen(view->child);
1026 if (view->child->focussed)
1027 show_panel(view->child->panel);
1029 show_panel(view->panel);
1031 ncols = view->child->begin_x;
1033 view_splitscreen(view->child);
1034 show_panel(view->child->panel);
1037 * XXX This is ugly and needs to be moved into the above
1038 * logic but "works" for now and my attempts at moving it
1039 * break either 'tab' or 'F' key maps in horizontal splits.
1042 err = view_splitscreen(view->child);
1045 if (dif < 0) { /* top split decreased */
1046 err = offset_selection_down(view);
1053 show_panel(view->child->panel);
1054 nlines = view->nlines;
1056 } else if (view->parent == NULL)
1059 if (view->resize && dif > 0) {
1060 err = view->resize(view, dif);
1065 if (wresize(view->window, nlines, ncols) == ERR)
1066 return got_error_from_errno("wresize");
1067 if (replace_panel(view->panel, view->window) == ERR)
1068 return got_error_from_errno("replace_panel");
1069 wclear(view->window);
1071 view->nlines = nlines;
1072 view->ncols = ncols;
1073 view->lines = LINES;
1079 static const struct got_error *
1080 resize_log_view(struct tog_view *view, int increase)
1082 struct tog_log_view_state *s = &view->state.log;
1083 const struct got_error *err = NULL;
1086 if (s->selected_entry)
1087 n = s->selected_entry->idx + view->lines - s->selected;
1090 * Request commits to account for the increased
1091 * height so we have enough to populate the view.
1093 if (s->commits->ncommits < n) {
1094 view->nscrolled = n - s->commits->ncommits + increase + 1;
1095 err = request_log_commits(view);
1102 view_adjust_offset(struct tog_view *view, int n)
1107 if (view->parent && view->parent->offset) {
1108 if (view->parent->offset + n >= 0)
1109 view->parent->offset += n;
1111 view->parent->offset = 0;
1112 } else if (view->offset) {
1113 if (view->offset - n >= 0)
1120 static const struct got_error *
1121 view_resize_split(struct tog_view *view, int resize)
1123 const struct got_error *err = NULL;
1124 struct tog_view *v = NULL;
1131 if (!v->child || !view_is_splitscreen(v->child))
1134 v->resized = v->child->resized = resize; /* lock for resize event */
1136 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1137 if (v->child->resized_y)
1138 v->child->begin_y = v->child->resized_y;
1140 v->child->begin_y -= resize;
1142 v->child->begin_y += resize;
1143 if (v->child->begin_y < 3) {
1145 v->child->begin_y = 3;
1146 } else if (v->child->begin_y > LINES - 1) {
1148 v->child->begin_y = LINES - 1;
1151 v->child->ncols = COLS;
1152 view_adjust_offset(view, resize);
1153 err = view_init_hsplit(v, v->child->begin_y);
1156 v->child->resized_y = v->child->begin_y;
1158 if (v->child->resized_x)
1159 v->child->begin_x = v->child->resized_x;
1161 v->child->begin_x -= resize;
1163 v->child->begin_x += resize;
1164 if (v->child->begin_x < 11) {
1166 v->child->begin_x = 11;
1167 } else if (v->child->begin_x > COLS - 1) {
1169 v->child->begin_x = COLS - 1;
1171 v->child->resized_x = v->child->begin_x;
1174 v->child->mode = v->mode;
1175 v->child->nlines = v->lines - v->child->begin_y;
1176 v->child->ncols = v->cols - v->child->begin_x;
1179 err = view_fullscreen(v);
1182 err = view_splitscreen(v->child);
1186 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1187 err = offset_selection_down(v->child);
1193 err = v->resize(v, 0);
1194 else if (v->child->resize)
1195 err = v->child->resize(v->child, 0);
1197 v->resized = v->child->resized = 0;
1203 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1205 struct tog_view *v = src->child ? src->child : src;
1207 dst->resized_x = v->resized_x;
1208 dst->resized_y = v->resized_y;
1211 static const struct got_error *
1212 view_close_child(struct tog_view *view)
1214 const struct got_error *err = NULL;
1216 if (view->child == NULL)
1219 err = view_close(view->child);
1224 static const struct got_error *
1225 view_set_child(struct tog_view *view, struct tog_view *child)
1227 const struct got_error *err = NULL;
1229 view->child = child;
1230 child->parent = view;
1232 err = view_resize(view);
1236 if (view->child->resized_x || view->child->resized_y)
1237 err = view_resize_split(view, 0);
1242 static const struct got_error *view_dispatch_request(struct tog_view **,
1243 struct tog_view *, enum tog_view_type, int, int);
1245 static const struct got_error *
1246 view_request_new(struct tog_view **requested, struct tog_view *view,
1247 enum tog_view_type request)
1249 struct tog_view *new_view = NULL;
1250 const struct got_error *err;
1255 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1256 view_get_split(view, &y, &x);
1258 err = view_dispatch_request(&new_view, view, request, y, x);
1262 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1263 request != TOG_VIEW_HELP) {
1264 err = view_init_hsplit(view, y);
1270 new_view->focussed = 1;
1271 new_view->mode = view->mode;
1272 new_view->nlines = request == TOG_VIEW_HELP ?
1273 view->lines : view->lines - y;
1275 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1276 view_transfer_size(new_view, view);
1277 err = view_close_child(view);
1280 err = view_set_child(view, new_view);
1283 view->focus_child = 1;
1285 *requested = new_view;
1291 tog_resizeterm(void)
1294 struct winsize size;
1296 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1297 cols = 80; /* Default */
1301 lines = size.ws_row;
1303 resize_term(lines, cols);
1306 static const struct got_error *
1307 view_search_start(struct tog_view *view, int fast_refresh)
1309 const struct got_error *err = NULL;
1310 struct tog_view *v = view;
1314 if (view->search_started) {
1315 regfree(&view->regex);
1316 view->searching = 0;
1317 memset(&view->regmatch, 0, sizeof(view->regmatch));
1319 view->search_started = 0;
1321 if (view->nlines < 1)
1324 if (view_is_hsplit_top(view))
1326 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1329 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1330 wclrtoeol(v->window);
1332 nodelay(v->window, FALSE); /* block for search term input */
1335 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1336 wrefresh(v->window);
1339 nodelay(v->window, TRUE);
1345 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1346 err = view->search_start(view);
1348 regfree(&view->regex);
1351 view->search_started = 1;
1352 view->searching = TOG_SEARCH_FORWARD;
1353 view->search_next_done = 0;
1354 view->search_next(view);
1360 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1361 static const struct got_error *
1362 switch_split(struct tog_view *view)
1364 const struct got_error *err = NULL;
1365 struct tog_view *v = NULL;
1372 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1373 v->mode = TOG_VIEW_SPLIT_VERT;
1375 v->mode = TOG_VIEW_SPLIT_HRZN;
1379 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1380 v->mode = TOG_VIEW_SPLIT_NONE;
1382 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1383 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1384 v->child->begin_y = v->child->resized_y;
1385 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1386 v->child->begin_x = v->child->resized_x;
1389 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1391 v->child->ncols = COLS;
1392 v->child->nscrolled = LINES - v->child->nlines;
1394 err = view_init_hsplit(v, v->child->begin_y);
1398 v->child->mode = v->mode;
1399 v->child->nlines = v->lines - v->child->begin_y;
1402 err = view_fullscreen(v);
1405 err = view_splitscreen(v->child);
1409 if (v->mode == TOG_VIEW_SPLIT_NONE)
1410 v->mode = TOG_VIEW_SPLIT_VERT;
1411 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1412 err = offset_selection_down(v);
1415 err = offset_selection_down(v->child);
1419 offset_selection_up(v);
1420 offset_selection_up(v->child);
1423 err = v->resize(v, 0);
1424 else if (v->child->resize)
1425 err = v->child->resize(v->child, 0);
1431 * Strip trailing whitespace from str starting at byte *n;
1432 * if *n < 0, use strlen(str). Return new str length in *n.
1435 strip_trailing_ws(char *str, int *n)
1439 if (str == NULL || *str == '\0')
1445 while (x-- > 0 && isspace((unsigned char)str[x]))
1452 * Extract visible substring of line y from the curses screen
1453 * and strip trailing whitespace. If vline is set and locale is
1454 * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1455 * character is written out as 'x'. Write the line to file f.
1457 static const struct got_error *
1458 view_write_line(FILE *f, int y, int vline)
1460 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1463 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1465 return got_error_fmt(GOT_ERR_RANGE,
1466 "failed to extract line %d", y);
1469 * In some views, lines are padded with blanks to COLS width.
1470 * Strip them so we can diff without the -b flag when testing.
1472 strip_trailing_ws(line, &r);
1474 if (vline > 0 && got_locale_is_utf8())
1477 w = fprintf(f, "%s\n", line);
1478 if (w != r + 1) /* \n */
1479 return got_ferror(f, GOT_ERR_IO);
1485 * Capture the visible curses screen by writing each line to the
1486 * file at the path set via the TOG_SCR_DUMP environment variable.
1488 static const struct got_error *
1489 screendump(struct tog_view *view)
1491 const struct got_error *err;
1496 path = getenv("TOG_SCR_DUMP");
1497 if (path == NULL || *path == '\0')
1498 return got_error_msg(GOT_ERR_BAD_PATH,
1499 "TOG_SCR_DUMP path not set to capture screen dump");
1500 f = fopen(path, "wex");
1502 return got_error_from_errno_fmt("fopen: %s", path);
1504 if ((view->child && view->child->begin_x) ||
1505 (view->parent && view->begin_x)) {
1506 int ncols = view->child ? view->ncols : view->parent->ncols;
1508 /* vertical splitscreen */
1509 for (i = 0; i < view->nlines; ++i) {
1510 err = view_write_line(f, i, ncols - 1);
1517 /* fullscreen or horizontal splitscreen */
1518 if ((view->child && view->child->begin_y) ||
1519 (view->parent && view->begin_y)) /* hsplit */
1520 hline = view->child ?
1521 view->child->begin_y : view->begin_y;
1523 for (i = 0; i < view->lines; i++) {
1524 if (hline && got_locale_is_utf8() && i == hline - 1) {
1527 /* ACS_HLINE writes out as 'q', overwrite it */
1528 for (c = 0; c < view->cols; ++c)
1534 err = view_write_line(f, i, 0);
1541 if (f && fclose(f) == EOF && err == NULL)
1542 err = got_ferror(f, GOT_ERR_IO);
1547 * Compute view->count from numeric input. Assign total to view->count and
1548 * return first non-numeric key entered.
1551 get_compound_key(struct tog_view *view, int c)
1553 struct tog_view *v = view;
1556 if (view_is_hsplit_top(view))
1558 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1562 cbreak(); /* block for input */
1563 nodelay(view->window, FALSE);
1564 wmove(v->window, v->nlines - 1, 0);
1565 wclrtoeol(v->window);
1566 waddch(v->window, ':');
1569 x = getcurx(v->window);
1570 if (x != ERR && x < view->ncols) {
1571 waddch(v->window, c);
1572 wrefresh(v->window);
1576 * Don't overflow. Max valid request should be the greatest
1577 * between the longest and total lines; cap at 10 million.
1582 n = n * 10 + (c - '0');
1583 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1585 if (c == 'G' || c == 'g') { /* nG key map */
1586 view->gline = view->hiline = n;
1591 /* Massage excessive or inapplicable values at the input handler. */
1598 action_report(struct tog_view *view)
1600 struct tog_view *v = view;
1602 if (view_is_hsplit_top(view))
1604 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1607 wmove(v->window, v->nlines - 1, 0);
1608 wclrtoeol(v->window);
1609 wprintw(v->window, ":%s", view->action);
1610 wrefresh(v->window);
1613 * Clear action status report. Only clear in blame view
1614 * once annotating is complete, otherwise it's too fast.
1616 if (view->type == TOG_VIEW_BLAME) {
1617 if (view->state.blame.blame_complete)
1618 view->action = NULL;
1620 view->action = NULL;
1624 * Read the next line from the test script and assign
1625 * key instruction to *ch. If at EOF, set the *done flag.
1627 static const struct got_error *
1628 tog_read_script_key(FILE *script, int *ch, int *done)
1630 const struct got_error *err = NULL;
1634 if (getline(&line, &linesz, script) == -1) {
1639 err = got_ferror(script, GOT_ERR_IO);
1642 } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1644 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1646 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1648 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1650 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1652 else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1653 *ch = TOG_KEY_SCRDUMP;
1662 static const struct got_error *
1663 view_input(struct tog_view **new, int *done, struct tog_view *view,
1664 struct tog_view_list_head *views, struct tog_io *tog_io, int fast_refresh)
1666 const struct got_error *err = NULL;
1673 action_report(view);
1675 /* Clear "no matches" indicator. */
1676 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1677 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1678 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1682 if (view->searching && !view->search_next_done) {
1683 errcode = pthread_mutex_unlock(&tog_mutex);
1685 return got_error_set_errno(errcode,
1686 "pthread_mutex_unlock");
1688 errcode = pthread_mutex_lock(&tog_mutex);
1690 return got_error_set_errno(errcode,
1691 "pthread_mutex_lock");
1692 view->search_next(view);
1696 /* Allow threads to make progress while we are waiting for input. */
1697 errcode = pthread_mutex_unlock(&tog_mutex);
1699 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1701 if (tog_io && tog_io->f) {
1702 err = tog_read_script_key(tog_io->f, &ch, done);
1705 } else if (view->count && --view->count) {
1707 nodelay(view->window, TRUE);
1708 ch = wgetch(view->window);
1709 /* let C-g or backspace abort unfinished count */
1710 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1715 ch = wgetch(view->window);
1716 if (ch >= '1' && ch <= '9')
1717 view->ch = ch = get_compound_key(view, ch);
1719 if (view->hiline && ch != ERR && ch != 0)
1720 view->hiline = 0; /* key pressed, clear line highlight */
1721 nodelay(view->window, TRUE);
1722 errcode = pthread_mutex_lock(&tog_mutex);
1724 return got_error_set_errno(errcode, "pthread_mutex_lock");
1726 if (tog_sigwinch_received || tog_sigcont_received) {
1728 tog_sigwinch_received = 0;
1729 tog_sigcont_received = 0;
1730 TAILQ_FOREACH(v, views, entry) {
1731 err = view_resize(v);
1734 err = v->input(new, v, KEY_RESIZE);
1738 err = view_resize(v->child);
1741 err = v->child->input(new, v->child,
1745 if (v->child->resized_x || v->child->resized_y) {
1746 err = view_resize_split(v, 0);
1758 if (view->type == TOG_VIEW_HELP)
1759 err = view->reset(view);
1761 err = view_request_new(new, view, TOG_VIEW_HELP);
1767 view->child->focussed = 1;
1768 view->focus_child = 1;
1769 } else if (view->parent) {
1771 view->parent->focussed = 1;
1772 view->parent->focus_child = 0;
1773 if (!view_is_splitscreen(view)) {
1774 if (view->parent->resize) {
1775 err = view->parent->resize(view->parent,
1780 offset_selection_up(view->parent);
1781 err = view_fullscreen(view->parent);
1788 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1789 if (view->parent->resize) {
1790 /* might need more commits to fill fullscreen */
1791 err = view->parent->resize(view->parent, 0);
1795 offset_selection_up(view->parent);
1797 err = view->input(new, view, ch);
1805 if (view_is_parent_view(view)) {
1806 if (view->child == NULL)
1808 if (view_is_splitscreen(view->child)) {
1810 view->child->focussed = 1;
1811 err = view_fullscreen(view->child);
1813 err = view_splitscreen(view->child);
1815 err = view_resize_split(view, 0);
1819 err = view->child->input(new, view->child,
1822 if (view_is_splitscreen(view)) {
1823 view->parent->focussed = 0;
1825 err = view_fullscreen(view);
1827 err = view_splitscreen(view);
1828 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1829 err = view_resize(view->parent);
1831 err = view_resize_split(view, 0);
1835 err = view->input(new, view, KEY_RESIZE);
1840 err = view->resize(view, 0);
1845 err = offset_selection_down(view->parent);
1847 err = offset_selection_down(view);
1851 err = switch_split(view);
1854 err = view_resize_split(view, -1);
1857 err = view_resize_split(view, 1);
1863 if (view->search_start)
1864 view_search_start(view, fast_refresh);
1866 err = view->input(new, view, ch);
1870 if (view->search_started && view->search_next) {
1871 view->searching = (ch == 'n' ?
1872 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1873 view->search_next_done = 0;
1874 view->search_next(view);
1876 err = view->input(new, view, ch);
1879 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1880 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1881 view->action = "Patience diff algorithm";
1883 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1884 view->action = "Myers diff algorithm";
1886 TAILQ_FOREACH(v, views, entry) {
1892 if (v->child && v->child->reset) {
1893 err = v->child->reset(v->child);
1899 case TOG_KEY_SCRDUMP:
1900 err = screendump(view);
1903 err = view->input(new, view, ch);
1911 view_needs_focus_indication(struct tog_view *view)
1913 if (view_is_parent_view(view)) {
1914 if (view->child == NULL || view->child->focussed)
1916 if (!view_is_splitscreen(view->child))
1918 } else if (!view_is_splitscreen(view))
1921 return view->focussed;
1924 static const struct got_error *
1925 tog_io_close(struct tog_io *tog_io)
1927 const struct got_error *err = NULL;
1929 if (tog_io->cin && fclose(tog_io->cin) == EOF)
1930 err = got_ferror(tog_io->cin, GOT_ERR_IO);
1931 if (tog_io->cout && fclose(tog_io->cout) == EOF && err == NULL)
1932 err = got_ferror(tog_io->cout, GOT_ERR_IO);
1933 if (tog_io->f && fclose(tog_io->f) == EOF && err == NULL)
1934 err = got_ferror(tog_io->f, GOT_ERR_IO);
1941 static const struct got_error *
1942 view_loop(struct tog_view *view, struct tog_io *tog_io)
1944 const struct got_error *err = NULL;
1945 struct tog_view_list_head views;
1946 struct tog_view *new_view;
1948 int fast_refresh = 10;
1949 int done = 0, errcode;
1951 mode = getenv("TOG_VIEW_SPLIT_MODE");
1952 if (!mode || !(*mode == 'h' || *mode == 'H'))
1953 view->mode = TOG_VIEW_SPLIT_VERT;
1955 view->mode = TOG_VIEW_SPLIT_HRZN;
1957 errcode = pthread_mutex_lock(&tog_mutex);
1959 return got_error_set_errno(errcode, "pthread_mutex_lock");
1962 TAILQ_INSERT_HEAD(&views, view, entry);
1965 err = view->show(view);
1970 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1971 !tog_fatal_signal_received()) {
1972 /* Refresh fast during initialization, then become slower. */
1973 if (fast_refresh && --fast_refresh == 0)
1974 halfdelay(10); /* switch to once per second */
1976 err = view_input(&new_view, &done, view, &views, tog_io,
1981 if (view->dying && view == TAILQ_FIRST(&views) &&
1982 TAILQ_NEXT(view, entry) == NULL)
1988 * When we quit, scroll the screen up a single line
1989 * so we don't lose any information.
1991 TAILQ_FOREACH(v, &views, entry) {
1992 wmove(v->window, 0, 0);
1993 wdeleteln(v->window);
1994 wnoutrefresh(v->window);
1995 if (v->child && !view_is_fullscreen(v)) {
1996 wmove(v->child->window, 0, 0);
1997 wdeleteln(v->child->window);
1998 wnoutrefresh(v->child->window);
2005 struct tog_view *v, *prev = NULL;
2007 if (view_is_parent_view(view))
2008 prev = TAILQ_PREV(view, tog_view_list_head,
2010 else if (view->parent)
2011 prev = view->parent;
2014 view->parent->child = NULL;
2015 view->parent->focus_child = 0;
2016 /* Restore fullscreen line height. */
2017 view->parent->nlines = view->parent->lines;
2018 err = view_resize(view->parent);
2021 /* Make resized splits persist. */
2022 view_transfer_size(view->parent, view);
2024 TAILQ_REMOVE(&views, view, entry);
2026 err = view_close(view);
2031 TAILQ_FOREACH(v, &views, entry) {
2035 if (view == NULL && new_view == NULL) {
2036 /* No view has focus. Try to pick one. */
2039 else if (!TAILQ_EMPTY(&views)) {
2040 view = TAILQ_LAST(&views,
2041 tog_view_list_head);
2044 if (view->focus_child) {
2045 view->child->focussed = 1;
2053 struct tog_view *v, *t;
2054 /* Only allow one parent view per type. */
2055 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2056 if (v->type != new_view->type)
2058 TAILQ_REMOVE(&views, v, entry);
2059 err = view_close(v);
2064 TAILQ_INSERT_TAIL(&views, new_view, entry);
2067 if (view && !done) {
2068 if (view_is_parent_view(view)) {
2069 if (view->child && view->child->focussed)
2072 if (view->parent && view->parent->focussed)
2073 view = view->parent;
2075 show_panel(view->panel);
2076 if (view->child && view_is_splitscreen(view->child))
2077 show_panel(view->child->panel);
2078 if (view->parent && view_is_splitscreen(view)) {
2079 err = view->parent->show(view->parent);
2083 err = view->show(view);
2087 err = view->child->show(view->child);
2096 while (!TAILQ_EMPTY(&views)) {
2097 const struct got_error *close_err;
2098 view = TAILQ_FIRST(&views);
2099 TAILQ_REMOVE(&views, view, entry);
2100 close_err = view_close(view);
2101 if (close_err && err == NULL)
2105 errcode = pthread_mutex_unlock(&tog_mutex);
2106 if (errcode && err == NULL)
2107 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2117 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2122 /* Create newly allocated wide-character string equivalent to a byte string. */
2123 static const struct got_error *
2124 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2127 const struct got_error *err = NULL;
2130 *wlen = mbstowcs(NULL, s, 0);
2131 if (*wlen == (size_t)-1) {
2133 if (errno != EILSEQ)
2134 return got_error_from_errno("mbstowcs");
2136 /* byte string invalid in current encoding; try to "fix" it */
2137 err = got_mbsavis(&vis, &vislen, s);
2140 *wlen = mbstowcs(NULL, vis, 0);
2141 if (*wlen == (size_t)-1) {
2142 err = got_error_from_errno("mbstowcs"); /* give up */
2147 *ws = calloc(*wlen + 1, sizeof(**ws));
2149 err = got_error_from_errno("calloc");
2153 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2154 err = got_error_from_errno("mbstowcs");
2165 static const struct got_error *
2166 expand_tab(char **ptr, const char *src)
2169 size_t len, n, idx = 0, sz = 0;
2172 n = len = strlen(src);
2173 dst = malloc(n + 1);
2175 return got_error_from_errno("malloc");
2177 while (idx < len && src[idx]) {
2178 const char c = src[idx];
2181 size_t nb = TABSIZE - sz % TABSIZE;
2184 p = realloc(dst, n + nb);
2187 return got_error_from_errno("realloc");
2192 memset(dst + sz, ' ', nb);
2195 dst[sz++] = src[idx];
2205 * Advance at most n columns from wline starting at offset off.
2206 * Return the index to the first character after the span operation.
2207 * Return the combined column width of all spanned wide character in
2211 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2213 int width, i, cols = 0;
2220 for (i = off; wline[i] != L'\0'; ++i) {
2221 if (wline[i] == L'\t')
2222 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2224 width = wcwidth(wline[i]);
2231 if (cols + width > n)
2241 * Format a line for display, ensuring that it won't overflow a width limit.
2242 * With scrolling, the width returned refers to the scrolled version of the
2243 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2245 static const struct got_error *
2246 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2247 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2249 const struct got_error *err = NULL;
2251 wchar_t *wline = NULL;
2260 err = expand_tab(&exstr, line);
2265 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2270 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2272 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2273 wline[wlen - 1] = L'\0';
2276 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2277 wline[wlen - 1] = L'\0';
2281 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2287 *scrollxp = scrollx;
2295 static const struct got_error*
2296 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2297 struct got_object_id *id, struct got_repository *repo)
2299 static const struct got_error *err = NULL;
2300 struct got_reflist_entry *re;
2306 TAILQ_FOREACH(re, refs, entry) {
2307 struct got_tag_object *tag = NULL;
2308 struct got_object_id *ref_id;
2311 name = got_ref_get_name(re->ref);
2312 if (strcmp(name, GOT_REF_HEAD) == 0)
2314 if (strncmp(name, "refs/", 5) == 0)
2316 if (strncmp(name, "got/", 4) == 0 &&
2317 strncmp(name, "got/backup/", 11) != 0)
2319 if (strncmp(name, "heads/", 6) == 0)
2321 if (strncmp(name, "remotes/", 8) == 0) {
2323 s = strstr(name, "/" GOT_REF_HEAD);
2324 if (s != NULL && s[strlen(s)] == '\0')
2327 err = got_ref_resolve(&ref_id, repo, re->ref);
2330 if (strncmp(name, "tags/", 5) == 0) {
2331 err = got_object_open_as_tag(&tag, repo, ref_id);
2333 if (err->code != GOT_ERR_OBJ_TYPE) {
2337 /* Ref points at something other than a tag. */
2342 cmp = got_object_id_cmp(tag ?
2343 got_object_tag_get_object_id(tag) : ref_id, id);
2346 got_object_tag_close(tag);
2350 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2351 s ? ", " : "", name) == -1) {
2352 err = got_error_from_errno("asprintf");
2363 static const struct got_error *
2364 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2369 smallerthan = strchr(author, '<');
2370 if (smallerthan && smallerthan[1] != '\0')
2371 author = smallerthan + 1;
2372 author[strcspn(author, "@>")] = '\0';
2373 return format_line(wauthor, author_width, NULL, author, 0, limit,
2377 static const struct got_error *
2378 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2379 struct got_object_id *id, const size_t date_display_cols,
2380 int author_display_cols)
2382 struct tog_log_view_state *s = &view->state.log;
2383 const struct got_error *err = NULL;
2384 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2385 char *logmsg0 = NULL, *logmsg = NULL;
2386 char *author = NULL;
2387 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2388 int author_width, logmsg_width;
2389 char *newline, *line = NULL;
2390 int col, limit, scrollx;
2391 const int avail = view->ncols;
2393 time_t committer_time;
2394 struct tog_color *tc;
2396 committer_time = got_object_commit_get_committer_time(commit);
2397 if (gmtime_r(&committer_time, &tm) == NULL)
2398 return got_error_from_errno("gmtime_r");
2399 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2400 return got_error(GOT_ERR_NO_SPACE);
2402 if (avail <= date_display_cols)
2403 limit = MIN(sizeof(datebuf) - 1, avail);
2405 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2406 tc = get_color(&s->colors, TOG_COLOR_DATE);
2408 wattr_on(view->window,
2409 COLOR_PAIR(tc->colorpair), NULL);
2410 waddnstr(view->window, datebuf, limit);
2412 wattr_off(view->window,
2413 COLOR_PAIR(tc->colorpair), NULL);
2420 err = got_object_id_str(&id_str, id);
2423 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2425 wattr_on(view->window,
2426 COLOR_PAIR(tc->colorpair), NULL);
2427 wprintw(view->window, "%.8s ", id_str);
2429 wattr_off(view->window,
2430 COLOR_PAIR(tc->colorpair), NULL);
2437 if (s->use_committer)
2438 author = strdup(got_object_commit_get_committer(commit));
2440 author = strdup(got_object_commit_get_author(commit));
2441 if (author == NULL) {
2442 err = got_error_from_errno("strdup");
2445 err = format_author(&wauthor, &author_width, author, avail - col, col);
2448 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2450 wattr_on(view->window,
2451 COLOR_PAIR(tc->colorpair), NULL);
2452 waddwstr(view->window, wauthor);
2453 col += author_width;
2454 while (col < avail && author_width < author_display_cols + 2) {
2455 waddch(view->window, ' ');
2460 wattr_off(view->window,
2461 COLOR_PAIR(tc->colorpair), NULL);
2465 err = got_object_commit_get_logmsg(&logmsg0, commit);
2469 while (*logmsg == '\n')
2471 newline = strchr(logmsg, '\n');
2474 limit = avail - col;
2475 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2476 limit--; /* for the border */
2477 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2481 waddwstr(view->window, &wlogmsg[scrollx]);
2482 col += MAX(logmsg_width, 0);
2483 while (col < avail) {
2484 waddch(view->window, ' ');
2496 static struct commit_queue_entry *
2497 alloc_commit_queue_entry(struct got_commit_object *commit,
2498 struct got_object_id *id)
2500 struct commit_queue_entry *entry;
2501 struct got_object_id *dup;
2503 entry = calloc(1, sizeof(*entry));
2507 dup = got_object_id_dup(id);
2514 entry->commit = commit;
2519 pop_commit(struct commit_queue *commits)
2521 struct commit_queue_entry *entry;
2523 entry = TAILQ_FIRST(&commits->head);
2524 TAILQ_REMOVE(&commits->head, entry, entry);
2525 got_object_commit_close(entry->commit);
2526 commits->ncommits--;
2532 free_commits(struct commit_queue *commits)
2534 while (!TAILQ_EMPTY(&commits->head))
2535 pop_commit(commits);
2538 static const struct got_error *
2539 match_commit(int *have_match, struct got_object_id *id,
2540 struct got_commit_object *commit, regex_t *regex)
2542 const struct got_error *err = NULL;
2543 regmatch_t regmatch;
2544 char *id_str = NULL, *logmsg = NULL;
2548 err = got_object_id_str(&id_str, id);
2552 err = got_object_commit_get_logmsg(&logmsg, commit);
2556 if (regexec(regex, got_object_commit_get_author(commit), 1,
2557 ®match, 0) == 0 ||
2558 regexec(regex, got_object_commit_get_committer(commit), 1,
2559 ®match, 0) == 0 ||
2560 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2561 regexec(regex, logmsg, 1, ®match, 0) == 0)
2569 static const struct got_error *
2570 queue_commits(struct tog_log_thread_args *a)
2572 const struct got_error *err = NULL;
2575 * We keep all commits open throughout the lifetime of the log
2576 * view in order to avoid having to re-fetch commits from disk
2577 * while updating the display.
2580 struct got_object_id id;
2581 struct got_commit_object *commit;
2582 struct commit_queue_entry *entry;
2583 int limit_match = 0;
2586 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2591 err = got_object_open_as_commit(&commit, a->repo, &id);
2594 entry = alloc_commit_queue_entry(commit, &id);
2595 if (entry == NULL) {
2596 err = got_error_from_errno("alloc_commit_queue_entry");
2600 errcode = pthread_mutex_lock(&tog_mutex);
2602 err = got_error_set_errno(errcode,
2603 "pthread_mutex_lock");
2607 entry->idx = a->real_commits->ncommits;
2608 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2609 a->real_commits->ncommits++;
2612 err = match_commit(&limit_match, &id, commit,
2618 struct commit_queue_entry *matched;
2620 matched = alloc_commit_queue_entry(
2621 entry->commit, entry->id);
2622 if (matched == NULL) {
2623 err = got_error_from_errno(
2624 "alloc_commit_queue_entry");
2627 matched->commit = entry->commit;
2628 got_object_commit_retain(entry->commit);
2630 matched->idx = a->limit_commits->ncommits;
2631 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2633 a->limit_commits->ncommits++;
2637 * This is how we signal log_thread() that we
2638 * have found a match, and that it should be
2639 * counted as a new entry for the view.
2641 a->limit_match = limit_match;
2644 if (*a->searching == TOG_SEARCH_FORWARD &&
2645 !*a->search_next_done) {
2647 err = match_commit(&have_match, &id, commit, a->regex);
2652 if (limit_match && have_match)
2653 *a->search_next_done =
2654 TOG_SEARCH_HAVE_MORE;
2655 } else if (have_match)
2656 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2659 errcode = pthread_mutex_unlock(&tog_mutex);
2660 if (errcode && err == NULL)
2661 err = got_error_set_errno(errcode,
2662 "pthread_mutex_unlock");
2665 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2671 select_commit(struct tog_log_view_state *s)
2673 struct commit_queue_entry *entry;
2676 entry = s->first_displayed_entry;
2678 if (ncommits == s->selected) {
2679 s->selected_entry = entry;
2682 entry = TAILQ_NEXT(entry, entry);
2687 static const struct got_error *
2688 draw_commits(struct tog_view *view)
2690 const struct got_error *err = NULL;
2691 struct tog_log_view_state *s = &view->state.log;
2692 struct commit_queue_entry *entry = s->selected_entry;
2693 int limit = view->nlines;
2695 int ncommits, author_cols = 4;
2696 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2697 char *refs_str = NULL;
2699 struct tog_color *tc;
2700 static const size_t date_display_cols = 12;
2702 if (view_is_hsplit_top(view))
2703 --limit; /* account for border */
2705 if (s->selected_entry &&
2706 !(view->searching && view->search_next_done == 0)) {
2707 struct got_reflist_head *refs;
2708 err = got_object_id_str(&id_str, s->selected_entry->id);
2711 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2712 s->selected_entry->id);
2714 err = build_refs_str(&refs_str, refs,
2715 s->selected_entry->id, s->repo);
2721 if (s->thread_args.commits_needed == 0)
2722 halfdelay(10); /* disable fast refresh */
2724 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2725 if (asprintf(&ncommits_str, " [%d/%d] %s",
2726 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2727 (view->searching && !view->search_next_done) ?
2728 "searching..." : "loading...") == -1) {
2729 err = got_error_from_errno("asprintf");
2733 const char *search_str = NULL;
2734 const char *limit_str = NULL;
2736 if (view->searching) {
2737 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2738 search_str = "no more matches";
2739 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2740 search_str = "no matches found";
2741 else if (!view->search_next_done)
2742 search_str = "searching...";
2745 if (s->limit_view && s->commits->ncommits == 0)
2746 limit_str = "no matches found";
2748 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2749 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2750 search_str ? search_str : (refs_str ? refs_str : ""),
2751 limit_str ? limit_str : "") == -1) {
2752 err = got_error_from_errno("asprintf");
2757 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2758 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2759 "........................................",
2760 s->in_repo_path, ncommits_str) == -1) {
2761 err = got_error_from_errno("asprintf");
2765 } else if (asprintf(&header, "commit %s%s",
2766 id_str ? id_str : "........................................",
2767 ncommits_str) == -1) {
2768 err = got_error_from_errno("asprintf");
2772 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2776 werase(view->window);
2778 if (view_needs_focus_indication(view))
2779 wstandout(view->window);
2780 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2782 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2783 waddwstr(view->window, wline);
2784 while (width < view->ncols) {
2785 waddch(view->window, ' ');
2789 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2790 if (view_needs_focus_indication(view))
2791 wstandend(view->window);
2796 /* Grow author column size if necessary, and set view->maxx. */
2797 entry = s->first_displayed_entry;
2801 struct got_commit_object *c = entry->commit;
2802 char *author, *eol, *msg, *msg0;
2803 wchar_t *wauthor, *wmsg;
2805 if (ncommits >= limit - 1)
2807 if (s->use_committer)
2808 author = strdup(got_object_commit_get_committer(c));
2810 author = strdup(got_object_commit_get_author(c));
2811 if (author == NULL) {
2812 err = got_error_from_errno("strdup");
2815 err = format_author(&wauthor, &width, author, COLS,
2817 if (author_cols < width)
2818 author_cols = width;
2823 err = got_object_commit_get_logmsg(&msg0, c);
2827 while (*msg == '\n')
2829 if ((eol = strchr(msg, '\n')))
2831 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2832 date_display_cols + author_cols, 0);
2835 view->maxx = MAX(view->maxx, width);
2839 entry = TAILQ_NEXT(entry, entry);
2842 entry = s->first_displayed_entry;
2843 s->last_displayed_entry = s->first_displayed_entry;
2846 if (ncommits >= limit - 1)
2848 if (ncommits == s->selected)
2849 wstandout(view->window);
2850 err = draw_commit(view, entry->commit, entry->id,
2851 date_display_cols, author_cols);
2852 if (ncommits == s->selected)
2853 wstandend(view->window);
2857 s->last_displayed_entry = entry;
2858 entry = TAILQ_NEXT(entry, entry);
2871 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2873 struct commit_queue_entry *entry;
2876 entry = TAILQ_FIRST(&s->commits->head);
2877 if (s->first_displayed_entry == entry)
2880 entry = s->first_displayed_entry;
2881 while (entry && nscrolled < maxscroll) {
2882 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2884 s->first_displayed_entry = entry;
2890 static const struct got_error *
2891 trigger_log_thread(struct tog_view *view, int wait)
2893 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2896 halfdelay(1); /* fast refresh while loading commits */
2898 while (!ta->log_complete && !tog_thread_error &&
2899 (ta->commits_needed > 0 || ta->load_all)) {
2900 /* Wake the log thread. */
2901 errcode = pthread_cond_signal(&ta->need_commits);
2903 return got_error_set_errno(errcode,
2904 "pthread_cond_signal");
2907 * The mutex will be released while the view loop waits
2908 * in wgetch(), at which time the log thread will run.
2913 /* Display progress update in log view. */
2914 show_log_view(view);
2918 /* Wait right here while next commit is being loaded. */
2919 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2921 return got_error_set_errno(errcode,
2922 "pthread_cond_wait");
2924 /* Display progress update in log view. */
2925 show_log_view(view);
2933 static const struct got_error *
2934 request_log_commits(struct tog_view *view)
2936 struct tog_log_view_state *state = &view->state.log;
2937 const struct got_error *err = NULL;
2939 if (state->thread_args.log_complete)
2942 state->thread_args.commits_needed += view->nscrolled;
2943 err = trigger_log_thread(view, 1);
2944 view->nscrolled = 0;
2949 static const struct got_error *
2950 log_scroll_down(struct tog_view *view, int maxscroll)
2952 struct tog_log_view_state *s = &view->state.log;
2953 const struct got_error *err = NULL;
2954 struct commit_queue_entry *pentry;
2955 int nscrolled = 0, ncommits_needed;
2957 if (s->last_displayed_entry == NULL)
2960 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2961 if (s->commits->ncommits < ncommits_needed &&
2962 !s->thread_args.log_complete) {
2964 * Ask the log thread for required amount of commits.
2966 s->thread_args.commits_needed +=
2967 ncommits_needed - s->commits->ncommits;
2968 err = trigger_log_thread(view, 1);
2974 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2975 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2978 s->last_displayed_entry = pentry ?
2979 pentry : s->last_displayed_entry;
2981 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2984 s->first_displayed_entry = pentry;
2985 } while (++nscrolled < maxscroll);
2987 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2988 view->nscrolled += nscrolled;
2990 view->nscrolled = 0;
2995 static const struct got_error *
2996 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2997 struct got_commit_object *commit, struct got_object_id *commit_id,
2998 struct tog_view *log_view, struct got_repository *repo)
3000 const struct got_error *err;
3001 struct got_object_qid *parent_id;
3002 struct tog_view *diff_view;
3004 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3005 if (diff_view == NULL)
3006 return got_error_from_errno("view_open");
3008 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3009 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3010 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3012 *new_view = diff_view;
3016 static const struct got_error *
3017 tree_view_visit_subtree(struct tog_tree_view_state *s,
3018 struct got_tree_object *subtree)
3020 struct tog_parent_tree *parent;
3022 parent = calloc(1, sizeof(*parent));
3024 return got_error_from_errno("calloc");
3026 parent->tree = s->tree;
3027 parent->first_displayed_entry = s->first_displayed_entry;
3028 parent->selected_entry = s->selected_entry;
3029 parent->selected = s->selected;
3030 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3033 s->first_displayed_entry = NULL;
3037 static const struct got_error *
3038 tree_view_walk_path(struct tog_tree_view_state *s,
3039 struct got_commit_object *commit, const char *path)
3041 const struct got_error *err = NULL;
3042 struct got_tree_object *tree = NULL;
3044 char *slash, *subpath = NULL;
3046 /* Walk the path and open corresponding tree objects. */
3049 struct got_tree_entry *te;
3050 struct got_object_id *tree_id;
3056 /* Ensure the correct subtree entry is selected. */
3057 slash = strchr(p, '/');
3059 te_name = strdup(p);
3061 te_name = strndup(p, slash - p);
3062 if (te_name == NULL) {
3063 err = got_error_from_errno("strndup");
3066 te = got_object_tree_find_entry(s->tree, te_name);
3068 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3073 s->first_displayed_entry = s->selected_entry = te;
3075 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3076 break; /* jump to this file's entry */
3078 slash = strchr(p, '/');
3080 subpath = strndup(path, slash - path);
3082 subpath = strdup(path);
3083 if (subpath == NULL) {
3084 err = got_error_from_errno("strdup");
3088 err = got_object_id_by_path(&tree_id, s->repo, commit,
3093 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3098 err = tree_view_visit_subtree(s, tree);
3100 got_object_tree_close(tree);
3114 static const struct got_error *
3115 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3116 struct commit_queue_entry *entry, const char *path,
3117 const char *head_ref_name, struct got_repository *repo)
3119 const struct got_error *err = NULL;
3120 struct tog_tree_view_state *s;
3121 struct tog_view *tree_view;
3123 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3124 if (tree_view == NULL)
3125 return got_error_from_errno("view_open");
3127 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3130 s = &tree_view->state.tree;
3132 *new_view = tree_view;
3134 if (got_path_is_root_dir(path))
3137 return tree_view_walk_path(s, entry->commit, path);
3140 static const struct got_error *
3141 block_signals_used_by_main_thread(void)
3146 if (sigemptyset(&sigset) == -1)
3147 return got_error_from_errno("sigemptyset");
3149 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3150 if (sigaddset(&sigset, SIGWINCH) == -1)
3151 return got_error_from_errno("sigaddset");
3152 if (sigaddset(&sigset, SIGCONT) == -1)
3153 return got_error_from_errno("sigaddset");
3154 if (sigaddset(&sigset, SIGINT) == -1)
3155 return got_error_from_errno("sigaddset");
3156 if (sigaddset(&sigset, SIGTERM) == -1)
3157 return got_error_from_errno("sigaddset");
3159 /* ncurses handles SIGTSTP */
3160 if (sigaddset(&sigset, SIGTSTP) == -1)
3161 return got_error_from_errno("sigaddset");
3163 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3165 return got_error_set_errno(errcode, "pthread_sigmask");
3171 log_thread(void *arg)
3173 const struct got_error *err = NULL;
3175 struct tog_log_thread_args *a = arg;
3179 * Sync startup with main thread such that we begin our
3180 * work once view_input() has released the mutex.
3182 errcode = pthread_mutex_lock(&tog_mutex);
3184 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3188 err = block_signals_used_by_main_thread();
3190 pthread_mutex_unlock(&tog_mutex);
3194 while (!done && !err && !tog_fatal_signal_received()) {
3195 errcode = pthread_mutex_unlock(&tog_mutex);
3197 err = got_error_set_errno(errcode,
3198 "pthread_mutex_unlock");
3201 err = queue_commits(a);
3203 if (err->code != GOT_ERR_ITER_COMPLETED)
3207 } else if (a->commits_needed > 0 && !a->load_all) {
3210 a->commits_needed--;
3212 a->commits_needed--;
3215 errcode = pthread_mutex_lock(&tog_mutex);
3217 err = got_error_set_errno(errcode,
3218 "pthread_mutex_lock");
3220 } else if (*a->quit)
3222 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3223 *a->first_displayed_entry =
3224 TAILQ_FIRST(&a->limit_commits->head);
3225 *a->selected_entry = *a->first_displayed_entry;
3226 } else if (*a->first_displayed_entry == NULL) {
3227 *a->first_displayed_entry =
3228 TAILQ_FIRST(&a->real_commits->head);
3229 *a->selected_entry = *a->first_displayed_entry;
3232 errcode = pthread_cond_signal(&a->commit_loaded);
3234 err = got_error_set_errno(errcode,
3235 "pthread_cond_signal");
3236 pthread_mutex_unlock(&tog_mutex);
3241 a->commits_needed = 0;
3243 if (a->commits_needed == 0 && !a->load_all) {
3244 errcode = pthread_cond_wait(&a->need_commits,
3247 err = got_error_set_errno(errcode,
3248 "pthread_cond_wait");
3249 pthread_mutex_unlock(&tog_mutex);
3257 a->log_complete = 1;
3258 errcode = pthread_mutex_unlock(&tog_mutex);
3260 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3263 tog_thread_error = 1;
3264 pthread_cond_signal(&a->commit_loaded);
3269 static const struct got_error *
3270 stop_log_thread(struct tog_log_view_state *s)
3272 const struct got_error *err = NULL, *thread_err = NULL;
3277 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3279 return got_error_set_errno(errcode,
3280 "pthread_cond_signal");
3281 errcode = pthread_mutex_unlock(&tog_mutex);
3283 return got_error_set_errno(errcode,
3284 "pthread_mutex_unlock");
3285 errcode = pthread_join(s->thread, (void **)&thread_err);
3287 return got_error_set_errno(errcode, "pthread_join");
3288 errcode = pthread_mutex_lock(&tog_mutex);
3290 return got_error_set_errno(errcode,
3291 "pthread_mutex_lock");
3292 s->thread = 0; //NULL;
3295 if (s->thread_args.repo) {
3296 err = got_repo_close(s->thread_args.repo);
3297 s->thread_args.repo = NULL;
3300 if (s->thread_args.pack_fds) {
3301 const struct got_error *pack_err =
3302 got_repo_pack_fds_close(s->thread_args.pack_fds);
3305 s->thread_args.pack_fds = NULL;
3308 if (s->thread_args.graph) {
3309 got_commit_graph_close(s->thread_args.graph);
3310 s->thread_args.graph = NULL;
3313 return err ? err : thread_err;
3316 static const struct got_error *
3317 close_log_view(struct tog_view *view)
3319 const struct got_error *err = NULL;
3320 struct tog_log_view_state *s = &view->state.log;
3323 err = stop_log_thread(s);
3325 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3326 if (errcode && err == NULL)
3327 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3329 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3330 if (errcode && err == NULL)
3331 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3333 free_commits(&s->limit_commits);
3334 free_commits(&s->real_commits);
3335 free(s->in_repo_path);
3336 s->in_repo_path = NULL;
3339 free(s->head_ref_name);
3340 s->head_ref_name = NULL;
3345 * We use two queues to implement the limit feature: first consists of
3346 * commits matching the current limit_regex; second is the real queue
3347 * of all known commits (real_commits). When the user starts limiting,
3348 * we swap queues such that all movement and displaying functionality
3349 * works with very slight change.
3351 static const struct got_error *
3352 limit_log_view(struct tog_view *view)
3354 struct tog_log_view_state *s = &view->state.log;
3355 struct commit_queue_entry *entry;
3356 struct tog_view *v = view;
3357 const struct got_error *err = NULL;
3361 if (view_is_hsplit_top(view))
3363 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3366 /* Get the pattern */
3367 wmove(v->window, v->nlines - 1, 0);
3368 wclrtoeol(v->window);
3369 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3370 nodelay(v->window, FALSE);
3373 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3376 nodelay(v->window, TRUE);
3380 if (*pattern == '\0') {
3382 * Safety measure for the situation where the user
3383 * resets limit without previously limiting anything.
3389 * User could have pressed Ctrl+L, which refreshed the
3390 * commit queues, it means we can't save previously
3391 * (before limit took place) displayed entries,
3392 * because they would point to already free'ed memory,
3393 * so we are forced to always select first entry of
3396 s->commits = &s->real_commits;
3397 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3398 s->selected_entry = s->first_displayed_entry;
3405 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3410 /* Clear the screen while loading limit view */
3411 s->first_displayed_entry = NULL;
3412 s->last_displayed_entry = NULL;
3413 s->selected_entry = NULL;
3414 s->commits = &s->limit_commits;
3416 /* Prepare limit queue for new search */
3417 free_commits(&s->limit_commits);
3418 s->limit_commits.ncommits = 0;
3420 /* First process commits, which are in queue already */
3421 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3424 err = match_commit(&have_match, entry->id,
3425 entry->commit, &s->limit_regex);
3430 struct commit_queue_entry *matched;
3432 matched = alloc_commit_queue_entry(entry->commit,
3434 if (matched == NULL) {
3435 err = got_error_from_errno(
3436 "alloc_commit_queue_entry");
3439 matched->commit = entry->commit;
3440 got_object_commit_retain(entry->commit);
3442 matched->idx = s->limit_commits.ncommits;
3443 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3445 s->limit_commits.ncommits++;
3449 /* Second process all the commits, until we fill the screen */
3450 if (s->limit_commits.ncommits < view->nlines - 1 &&
3451 !s->thread_args.log_complete) {
3452 s->thread_args.commits_needed +=
3453 view->nlines - s->limit_commits.ncommits - 1;
3454 err = trigger_log_thread(view, 1);
3459 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3460 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3466 static const struct got_error *
3467 search_start_log_view(struct tog_view *view)
3469 struct tog_log_view_state *s = &view->state.log;
3471 s->matched_entry = NULL;
3472 s->search_entry = NULL;
3476 static const struct got_error *
3477 search_next_log_view(struct tog_view *view)
3479 const struct got_error *err = NULL;
3480 struct tog_log_view_state *s = &view->state.log;
3481 struct commit_queue_entry *entry;
3483 /* Display progress update in log view. */
3484 show_log_view(view);
3488 if (s->search_entry) {
3490 errcode = pthread_mutex_unlock(&tog_mutex);
3492 return got_error_set_errno(errcode,
3493 "pthread_mutex_unlock");
3494 ch = wgetch(view->window);
3495 errcode = pthread_mutex_lock(&tog_mutex);
3497 return got_error_set_errno(errcode,
3498 "pthread_mutex_lock");
3499 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3500 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3503 if (view->searching == TOG_SEARCH_FORWARD)
3504 entry = TAILQ_NEXT(s->search_entry, entry);
3506 entry = TAILQ_PREV(s->search_entry,
3507 commit_queue_head, entry);
3508 } else if (s->matched_entry) {
3510 * If the user has moved the cursor after we hit a match,
3511 * the position from where we should continue searching
3512 * might have changed.
3514 if (view->searching == TOG_SEARCH_FORWARD)
3515 entry = TAILQ_NEXT(s->selected_entry, entry);
3517 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3520 entry = s->selected_entry;
3526 if (entry == NULL) {
3527 if (s->thread_args.log_complete ||
3528 view->searching == TOG_SEARCH_BACKWARD) {
3529 view->search_next_done =
3530 (s->matched_entry == NULL ?
3531 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3532 s->search_entry = NULL;
3536 * Poke the log thread for more commits and return,
3537 * allowing the main loop to make progress. Search
3538 * will resume at s->search_entry once we come back.
3540 s->thread_args.commits_needed++;
3541 return trigger_log_thread(view, 0);
3544 err = match_commit(&have_match, entry->id, entry->commit,
3549 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3550 s->matched_entry = entry;
3554 s->search_entry = entry;
3555 if (view->searching == TOG_SEARCH_FORWARD)
3556 entry = TAILQ_NEXT(entry, entry);
3558 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3561 if (s->matched_entry) {
3562 int cur = s->selected_entry->idx;
3563 while (cur < s->matched_entry->idx) {
3564 err = input_log_view(NULL, view, KEY_DOWN);
3569 while (cur > s->matched_entry->idx) {
3570 err = input_log_view(NULL, view, KEY_UP);
3577 s->search_entry = NULL;
3582 static const struct got_error *
3583 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3584 struct got_repository *repo, const char *head_ref_name,
3585 const char *in_repo_path, int log_branches)
3587 const struct got_error *err = NULL;
3588 struct tog_log_view_state *s = &view->state.log;
3589 struct got_repository *thread_repo = NULL;
3590 struct got_commit_graph *thread_graph = NULL;
3593 if (in_repo_path != s->in_repo_path) {
3594 free(s->in_repo_path);
3595 s->in_repo_path = strdup(in_repo_path);
3596 if (s->in_repo_path == NULL)
3597 return got_error_from_errno("strdup");
3600 /* The commit queue only contains commits being displayed. */
3601 TAILQ_INIT(&s->real_commits.head);
3602 s->real_commits.ncommits = 0;
3603 s->commits = &s->real_commits;
3605 TAILQ_INIT(&s->limit_commits.head);
3607 s->limit_commits.ncommits = 0;
3610 if (head_ref_name) {
3611 s->head_ref_name = strdup(head_ref_name);
3612 if (s->head_ref_name == NULL) {
3613 err = got_error_from_errno("strdup");
3617 s->start_id = got_object_id_dup(start_id);
3618 if (s->start_id == NULL) {
3619 err = got_error_from_errno("got_object_id_dup");
3622 s->log_branches = log_branches;
3623 s->use_committer = 1;
3625 STAILQ_INIT(&s->colors);
3626 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3627 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3628 get_color_value("TOG_COLOR_COMMIT"));
3631 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3632 get_color_value("TOG_COLOR_AUTHOR"));
3634 free_colors(&s->colors);
3637 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3638 get_color_value("TOG_COLOR_DATE"));
3640 free_colors(&s->colors);
3645 view->show = show_log_view;
3646 view->input = input_log_view;
3647 view->resize = resize_log_view;
3648 view->close = close_log_view;
3649 view->search_start = search_start_log_view;
3650 view->search_next = search_next_log_view;
3652 if (s->thread_args.pack_fds == NULL) {
3653 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3657 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3658 s->thread_args.pack_fds);
3661 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3665 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3666 s->repo, NULL, NULL);
3670 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3672 err = got_error_set_errno(errcode, "pthread_cond_init");
3675 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3677 err = got_error_set_errno(errcode, "pthread_cond_init");
3681 s->thread_args.commits_needed = view->nlines;
3682 s->thread_args.graph = thread_graph;
3683 s->thread_args.real_commits = &s->real_commits;
3684 s->thread_args.limit_commits = &s->limit_commits;
3685 s->thread_args.in_repo_path = s->in_repo_path;
3686 s->thread_args.start_id = s->start_id;
3687 s->thread_args.repo = thread_repo;
3688 s->thread_args.log_complete = 0;
3689 s->thread_args.quit = &s->quit;
3690 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3691 s->thread_args.selected_entry = &s->selected_entry;
3692 s->thread_args.searching = &view->searching;
3693 s->thread_args.search_next_done = &view->search_next_done;
3694 s->thread_args.regex = &view->regex;
3695 s->thread_args.limiting = &s->limit_view;
3696 s->thread_args.limit_regex = &s->limit_regex;
3697 s->thread_args.limit_commits = &s->limit_commits;
3700 close_log_view(view);
3704 static const struct got_error *
3705 show_log_view(struct tog_view *view)
3707 const struct got_error *err;
3708 struct tog_log_view_state *s = &view->state.log;
3710 if (s->thread == 0) { //NULL) {
3711 int errcode = pthread_create(&s->thread, NULL, log_thread,
3714 return got_error_set_errno(errcode, "pthread_create");
3715 if (s->thread_args.commits_needed > 0) {
3716 err = trigger_log_thread(view, 1);
3722 return draw_commits(view);
3726 log_move_cursor_up(struct tog_view *view, int page, int home)
3728 struct tog_log_view_state *s = &view->state.log;
3730 if (s->first_displayed_entry == NULL)
3732 if (s->selected_entry->idx == 0)
3735 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3737 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3739 if (!page && !home && s->selected > 0)
3742 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3748 static const struct got_error *
3749 log_move_cursor_down(struct tog_view *view, int page)
3751 struct tog_log_view_state *s = &view->state.log;
3752 const struct got_error *err = NULL;
3753 int eos = view->nlines - 2;
3755 if (s->first_displayed_entry == NULL)
3758 if (s->thread_args.log_complete &&
3759 s->selected_entry->idx >= s->commits->ncommits - 1)
3762 if (view_is_hsplit_top(view))
3763 --eos; /* border consumes the last line */
3766 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3769 err = log_scroll_down(view, 1);
3770 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3771 struct commit_queue_entry *entry;
3775 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3776 s->last_displayed_entry = entry;
3777 for (n = 0; n <= eos; n++) {
3780 s->first_displayed_entry = entry;
3781 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3784 s->selected = n - 1;
3786 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3787 s->thread_args.log_complete)
3788 s->selected += MIN(page,
3789 s->commits->ncommits - s->selected_entry->idx - 1);
3791 err = log_scroll_down(view, page);
3797 * We might necessarily overshoot in horizontal
3798 * splits; if so, select the last displayed commit.
3800 if (s->first_displayed_entry && s->last_displayed_entry) {
3801 s->selected = MIN(s->selected,
3802 s->last_displayed_entry->idx -
3803 s->first_displayed_entry->idx);
3808 if (s->thread_args.log_complete &&
3809 s->selected_entry->idx == s->commits->ncommits - 1)
3816 view_get_split(struct tog_view *view, int *y, int *x)
3821 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3822 if (view->child && view->child->resized_y)
3823 *y = view->child->resized_y;
3824 else if (view->resized_y)
3825 *y = view->resized_y;
3827 *y = view_split_begin_y(view->lines);
3828 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3829 if (view->child && view->child->resized_x)
3830 *x = view->child->resized_x;
3831 else if (view->resized_x)
3832 *x = view->resized_x;
3834 *x = view_split_begin_x(view->begin_x);
3838 /* Split view horizontally at y and offset view->state->selected line. */
3839 static const struct got_error *
3840 view_init_hsplit(struct tog_view *view, int y)
3842 const struct got_error *err = NULL;
3846 err = view_resize(view);
3850 err = offset_selection_down(view);
3855 static const struct got_error *
3856 log_goto_line(struct tog_view *view, int nlines)
3858 const struct got_error *err = NULL;
3859 struct tog_log_view_state *s = &view->state.log;
3860 int g, idx = s->selected_entry->idx;
3862 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3868 if (g >= s->first_displayed_entry->idx + 1 &&
3869 g <= s->last_displayed_entry->idx + 1 &&
3870 g - s->first_displayed_entry->idx - 1 < nlines) {
3871 s->selected = g - s->first_displayed_entry->idx - 1;
3877 err = log_move_cursor_down(view, g - idx - 1);
3878 if (!err && g > s->selected_entry->idx + 1)
3879 err = log_move_cursor_down(view,
3880 g - s->first_displayed_entry->idx - 1);
3883 } else if (idx + 1 > g)
3884 log_move_cursor_up(view, idx - g + 1, 0);
3886 if (g < nlines && s->first_displayed_entry->idx == 0)
3887 s->selected = g - 1;
3895 horizontal_scroll_input(struct tog_view *view, int ch)
3901 view->x -= MIN(view->x, 2);
3907 if (view->x + view->ncols / 2 < view->maxx)
3916 view->x = MAX(view->maxx - view->ncols / 2, 0);
3924 static const struct got_error *
3925 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3927 const struct got_error *err = NULL;
3928 struct tog_log_view_state *s = &view->state.log;
3931 if (s->thread_args.load_all) {
3932 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3933 s->thread_args.load_all = 0;
3934 else if (s->thread_args.log_complete) {
3935 err = log_move_cursor_down(view, s->commits->ncommits);
3936 s->thread_args.load_all = 0;
3942 eos = nscroll = view->nlines - 1;
3943 if (view_is_hsplit_top(view))
3947 return log_goto_line(view, eos);
3951 err = limit_log_view(view);
3962 horizontal_scroll_input(view, ch);
3969 log_move_cursor_up(view, 0, 0);
3974 log_move_cursor_up(view, 0, 1);
3984 log_move_cursor_up(view, nscroll, 0);
3991 err = log_move_cursor_down(view, 0);
3994 s->use_committer = !s->use_committer;
3995 view->action = s->use_committer ?
3996 "show committer" : "show commit author";
4001 /* We don't know yet how many commits, so we're forced to
4002 * traverse them all. */
4004 s->thread_args.load_all = 1;
4005 if (!s->thread_args.log_complete)
4006 return trigger_log_thread(view, 0);
4007 err = log_move_cursor_down(view, s->commits->ncommits);
4008 s->thread_args.load_all = 0;
4019 err = log_move_cursor_down(view, nscroll);
4022 if (s->selected > view->nlines - 2)
4023 s->selected = view->nlines - 2;
4024 if (s->selected > s->commits->ncommits - 1)
4025 s->selected = s->commits->ncommits - 1;
4027 if (s->commits->ncommits < view->nlines - 1 &&
4028 !s->thread_args.log_complete) {
4029 s->thread_args.commits_needed += (view->nlines - 1) -
4030 s->commits->ncommits;
4031 err = trigger_log_thread(view, 1);
4037 if (s->selected_entry == NULL)
4039 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4043 if (s->selected_entry == NULL)
4045 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4051 if (ch == KEY_BACKSPACE &&
4052 got_path_is_root_dir(s->in_repo_path))
4054 err = stop_log_thread(s);
4057 if (ch == KEY_BACKSPACE) {
4059 err = got_path_dirname(&parent_path, s->in_repo_path);
4062 free(s->in_repo_path);
4063 s->in_repo_path = parent_path;
4064 s->thread_args.in_repo_path = s->in_repo_path;
4065 } else if (ch == CTRL('l')) {
4066 struct got_object_id *start_id;
4067 err = got_repo_match_object_id(&start_id, NULL,
4068 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4069 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4071 if (s->head_ref_name == NULL ||
4072 err->code != GOT_ERR_NOT_REF)
4074 /* Try to cope with deleted references. */
4075 free(s->head_ref_name);
4076 s->head_ref_name = NULL;
4077 err = got_repo_match_object_id(&start_id,
4078 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4079 &tog_refs, s->repo);
4084 s->start_id = start_id;
4085 s->thread_args.start_id = s->start_id;
4087 s->log_branches = !s->log_branches;
4089 if (s->thread_args.pack_fds == NULL) {
4090 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4094 err = got_repo_open(&s->thread_args.repo,
4095 got_repo_get_path(s->repo), NULL,
4096 s->thread_args.pack_fds);
4100 err = tog_load_refs(s->repo, 0);
4103 err = got_commit_graph_open(&s->thread_args.graph,
4104 s->in_repo_path, !s->log_branches);
4107 err = got_commit_graph_iter_start(s->thread_args.graph,
4108 s->start_id, s->repo, NULL, NULL);
4111 free_commits(&s->real_commits);
4112 free_commits(&s->limit_commits);
4113 s->first_displayed_entry = NULL;
4114 s->last_displayed_entry = NULL;
4115 s->selected_entry = NULL;
4117 s->thread_args.log_complete = 0;
4119 s->thread_args.commits_needed = view->lines;
4120 s->matched_entry = NULL;
4121 s->search_entry = NULL;
4126 err = view_request_new(new_view, view, TOG_VIEW_REF);
4136 static const struct got_error *
4137 apply_unveil(const char *repo_path, const char *worktree_path)
4139 const struct got_error *error;
4142 if (unveil("gmon.out", "rwc") != 0)
4143 return got_error_from_errno2("unveil", "gmon.out");
4145 if (repo_path && unveil(repo_path, "r") != 0)
4146 return got_error_from_errno2("unveil", repo_path);
4148 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4149 return got_error_from_errno2("unveil", worktree_path);
4151 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4152 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4154 error = got_privsep_unveil_exec_helpers();
4158 if (unveil(NULL, NULL) != 0)
4159 return got_error_from_errno("unveil");
4164 static const struct got_error *
4165 init_mock_term(struct tog_io **tog_io, const char *test_script_path)
4167 const struct got_error *err = NULL;
4173 if (test_script_path == NULL || *test_script_path == '\0')
4174 return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4176 io = calloc(1, sizeof(*io));
4178 return got_error_from_errno("calloc");
4180 io->f = fopen(test_script_path, "re");
4181 if (io->f == NULL) {
4182 err = got_error_from_errno_fmt("fopen: %s",
4187 /* test mode, we don't want any output */
4188 io->cout = fopen("/dev/null", "w+");
4189 if (io->cout == NULL) {
4190 err = got_error_from_errno("fopen: /dev/null");
4194 io->cin = fopen("/dev/tty", "r+");
4195 if (io->cin == NULL) {
4196 err = got_error_from_errno("fopen: /dev/tty");
4200 if (fseeko(io->f, 0L, SEEK_SET) == -1) {
4201 err = got_error_from_errno("fseeko");
4206 * XXX Perhaps we should define "xterm" as the terminal
4207 * type for standardised testing instead of using $TERM?
4209 if (newterm(NULL, io->cout, io->cin) == NULL)
4210 err = got_error_msg(GOT_ERR_IO,
4211 "newterm: failed to initialise curses");
4220 static const struct got_error *
4221 init_curses(struct tog_io **tog_io)
4223 const struct got_error *err = NULL;
4224 const char *test_script_path;
4227 * Override default signal handlers before starting ncurses.
4228 * This should prevent ncurses from installing its own
4229 * broken cleanup() signal handler.
4231 signal(SIGWINCH, tog_sigwinch);
4232 signal(SIGPIPE, tog_sigpipe);
4233 signal(SIGCONT, tog_sigcont);
4234 signal(SIGINT, tog_sigint);
4235 signal(SIGTERM, tog_sigterm);
4237 test_script_path = getenv("GOT_TOG_TEST");
4238 if (test_script_path != NULL) {
4239 err = init_mock_term(tog_io, test_script_path);
4246 halfdelay(1); /* Do fast refresh while initial view is loading. */
4249 intrflush(stdscr, FALSE);
4250 keypad(stdscr, TRUE);
4252 if (getenv("TOG_COLORS") != NULL) {
4254 use_default_colors();
4260 static const struct got_error *
4261 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4262 struct got_repository *repo, struct got_worktree *worktree)
4264 const struct got_error *err = NULL;
4267 *in_repo_path = strdup("/");
4268 if (*in_repo_path == NULL)
4269 return got_error_from_errno("strdup");
4274 const char *prefix = got_worktree_get_path_prefix(worktree);
4277 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4280 if (asprintf(in_repo_path, "%s%s%s", prefix,
4281 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4283 err = got_error_from_errno("asprintf");
4284 *in_repo_path = NULL;
4288 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4293 static const struct got_error *
4294 cmd_log(int argc, char *argv[])
4296 const struct got_error *io_err, *error;
4297 struct got_repository *repo = NULL;
4298 struct got_worktree *worktree = NULL;
4299 struct got_object_id *start_id = NULL;
4300 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4301 char *start_commit = NULL, *label = NULL;
4302 struct got_reference *ref = NULL;
4303 const char *head_ref_name = NULL;
4304 int ch, log_branches = 0;
4305 struct tog_view *view;
4306 struct tog_io *tog_io = NULL;
4307 int *pack_fds = NULL;
4309 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4315 start_commit = optarg;
4318 repo_path = realpath(optarg, NULL);
4319 if (repo_path == NULL)
4320 return got_error_from_errno2("realpath",
4335 error = got_repo_pack_fds_open(&pack_fds);
4339 if (repo_path == NULL) {
4340 cwd = getcwd(NULL, 0);
4342 return got_error_from_errno("getcwd");
4343 error = got_worktree_open(&worktree, cwd);
4344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4348 strdup(got_worktree_get_repo_path(worktree));
4350 repo_path = strdup(cwd);
4351 if (repo_path == NULL) {
4352 error = got_error_from_errno("strdup");
4357 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4361 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4366 error = init_curses(&tog_io);
4370 error = apply_unveil(got_repo_get_path(repo),
4371 worktree ? got_worktree_get_root_path(worktree) : NULL);
4375 /* already loaded by tog_log_with_path()? */
4376 if (TAILQ_EMPTY(&tog_refs)) {
4377 error = tog_load_refs(repo, 0);
4382 if (start_commit == NULL) {
4383 error = got_repo_match_object_id(&start_id, &label,
4384 worktree ? got_worktree_get_head_ref_name(worktree) :
4385 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4388 head_ref_name = label;
4390 error = got_ref_open(&ref, repo, start_commit, 0);
4392 head_ref_name = got_ref_get_name(ref);
4393 else if (error->code != GOT_ERR_NOT_REF)
4395 error = got_repo_match_object_id(&start_id, NULL,
4396 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4401 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4403 error = got_error_from_errno("view_open");
4406 error = open_log_view(view, start_id, repo, head_ref_name,
4407 in_repo_path, log_branches);
4411 /* Release work tree lock. */
4412 got_worktree_close(worktree);
4415 error = view_loop(view, tog_io);
4425 const struct got_error *close_err = got_repo_close(repo);
4430 got_worktree_close(worktree);
4432 const struct got_error *pack_err =
4433 got_repo_pack_fds_close(pack_fds);
4437 if (tog_io != NULL) {
4438 io_err = tog_io_close(tog_io);
4450 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4451 "object1 object2\n", getprogname());
4456 match_line(const char *line, regex_t *regex, size_t nmatch,
4457 regmatch_t *regmatch)
4459 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4462 static struct tog_color *
4463 match_color(struct tog_colors *colors, const char *line)
4465 struct tog_color *tc = NULL;
4467 STAILQ_FOREACH(tc, colors, entry) {
4468 if (match_line(line, &tc->regex, 0, NULL))
4475 static const struct got_error *
4476 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4477 WINDOW *window, int skipcol, regmatch_t *regmatch)
4479 const struct got_error *err = NULL;
4481 wchar_t *wline = NULL;
4482 int rme, rms, n, width, scrollx;
4483 int width0 = 0, width1 = 0, width2 = 0;
4484 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4488 rms = regmatch->rm_so;
4489 rme = regmatch->rm_eo;
4491 err = expand_tab(&exstr, line);
4495 /* Split the line into 3 segments, according to match offsets. */
4496 seg0 = strndup(exstr, rms);
4498 err = got_error_from_errno("strndup");
4501 seg1 = strndup(exstr + rms, rme - rms);
4503 err = got_error_from_errno("strndup");
4506 seg2 = strdup(exstr + rme);
4508 err = got_error_from_errno("strndup");
4512 /* draw up to matched token if we haven't scrolled past it */
4513 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4517 n = MAX(width0 - skipcol, 0);
4520 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4521 wlimit, col_tab_align, 1);
4524 waddwstr(window, &wline[scrollx]);
4534 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4538 wlen = wcslen(wline);
4540 width = wcwidth(wline[i]);
4542 /* should not happen, tabs are expanded */
4543 err = got_error(GOT_ERR_RANGE);
4546 if (width0 + w + width > skipcol)
4551 /* draw (visible part of) matched token (if scrolled into it) */
4552 if (width1 - w > 0) {
4553 wattron(window, A_STANDOUT);
4554 waddwstr(window, &wline[i]);
4555 wattroff(window, A_STANDOUT);
4556 wlimit -= (width1 - w);
4557 *wtotal += (width1 - w);
4561 if (wlimit > 0) { /* draw rest of line */
4563 if (skipcol > width0 + width1) {
4564 err = format_line(&wline, &width2, &scrollx, seg2,
4565 skipcol - (width0 + width1), wlimit,
4569 waddwstr(window, &wline[scrollx]);
4571 err = format_line(&wline, &width2, NULL, seg2, 0,
4572 wlimit, col_tab_align, 1);
4575 waddwstr(window, wline);
4589 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4592 int *eof, *first, *selected;
4594 if (view->type == TOG_VIEW_DIFF) {
4595 struct tog_diff_view_state *s = &view->state.diff;
4597 first = &s->first_displayed_line;
4601 } else if (view->type == TOG_VIEW_HELP) {
4602 struct tog_help_view_state *s = &view->state.help;
4604 first = &s->first_displayed_line;
4608 } else if (view->type == TOG_VIEW_BLAME) {
4609 struct tog_blame_view_state *s = &view->state.blame;
4611 first = &s->first_displayed_line;
4612 selected = &s->selected_line;
4618 /* Center gline in the middle of the page like vi(1). */
4619 if (*lineno < view->gline - (view->nlines - 3) / 2)
4621 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4630 *selected = view->gline <= (view->nlines - 3) / 2 ?
4631 view->gline : (view->nlines - 3) / 2 + 1;
4637 static const struct got_error *
4638 draw_file(struct tog_view *view, const char *header)
4640 struct tog_diff_view_state *s = &view->state.diff;
4641 regmatch_t *regmatch = &view->regmatch;
4642 const struct got_error *err;
4645 size_t linesize = 0;
4649 int max_lines = view->nlines;
4650 int nlines = s->nlines;
4653 s->lineno = s->first_displayed_line - 1;
4654 line_offset = s->lines[s->first_displayed_line - 1].offset;
4655 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4656 return got_error_from_errno("fseek");
4658 werase(view->window);
4660 if (view->gline > s->nlines - 1)
4661 view->gline = s->nlines - 1;
4664 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4665 1 : view->gline - (view->nlines - 3) / 2 :
4666 s->lineno + s->selected_line;
4668 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4669 return got_error_from_errno("asprintf");
4670 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4676 if (view_needs_focus_indication(view))
4677 wstandout(view->window);
4678 waddwstr(view->window, wline);
4681 while (width++ < view->ncols)
4682 waddch(view->window, ' ');
4683 if (view_needs_focus_indication(view))
4684 wstandend(view->window);
4694 while (max_lines > 0 && nprinted < max_lines) {
4695 enum got_diff_line_type linetype;
4698 linelen = getline(&line, &linesize, s->f);
4699 if (linelen == -1) {
4705 return got_ferror(s->f, GOT_ERR_IO);
4708 if (++s->lineno < s->first_displayed_line)
4710 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4712 if (s->lineno == view->hiline)
4715 /* Set view->maxx based on full line length. */
4716 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4722 view->maxx = MAX(view->maxx, width);
4726 linetype = s->lines[s->lineno].type;
4727 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4728 linetype < GOT_DIFF_LINE_CONTEXT)
4729 attr |= COLOR_PAIR(linetype);
4731 wattron(view->window, attr);
4732 if (s->first_displayed_line + nprinted == s->matched_line &&
4733 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4734 err = add_matched_line(&width, line, view->ncols, 0,
4735 view->window, view->x, regmatch);
4742 err = format_line(&wline, &width, &skip, line,
4743 view->x, view->ncols, 0, view->x ? 1 : 0);
4748 waddwstr(view->window, &wline[skip]);
4752 if (s->lineno == view->hiline) {
4753 /* highlight full gline length */
4754 while (width++ < view->ncols)
4755 waddch(view->window, ' ');
4757 if (width <= view->ncols - 1)
4758 waddch(view->window, '\n');
4761 wattroff(view->window, attr);
4762 if (++nprinted == 1)
4763 s->first_displayed_line = s->lineno;
4767 s->last_displayed_line = s->first_displayed_line +
4770 s->last_displayed_line = s->first_displayed_line;
4775 while (nprinted < view->nlines) {
4776 waddch(view->window, '\n');
4780 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4786 wstandout(view->window);
4787 waddwstr(view->window, wline);
4790 wstandend(view->window);
4797 get_datestr(time_t *time, char *datebuf)
4799 struct tm mytm, *tm;
4802 tm = gmtime_r(time, &mytm);
4805 s = asctime_r(tm, datebuf);
4808 p = strchr(s, '\n');
4814 static const struct got_error *
4815 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4816 off_t off, uint8_t type)
4818 struct got_diff_line *p;
4820 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4822 return got_error_from_errno("reallocarray");
4824 (*lines)[*nlines].offset = off;
4825 (*lines)[*nlines].type = type;
4831 static const struct got_error *
4832 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4833 struct got_diff_line *s_lines, size_t s_nlines)
4835 struct got_diff_line *p;
4839 if (fseeko(src, 0L, SEEK_SET) == -1)
4840 return got_error_from_errno("fseeko");
4843 r = fread(buf, 1, sizeof(buf), src);
4846 return got_error_from_errno("fread");
4850 if (fwrite(buf, 1, r, dst) != r)
4851 return got_ferror(dst, GOT_ERR_IO);
4854 if (s_nlines == 0 && *d_nlines == 0)
4858 * If commit info was in dst, increment line offsets
4859 * of the appended diff content, but skip s_lines[0]
4860 * because offset zero is already in *d_lines.
4862 if (*d_nlines > 0) {
4863 for (i = 1; i < s_nlines; ++i)
4864 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4872 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4874 /* d_lines is freed in close_diff_view() */
4875 return got_error_from_errno("reallocarray");
4880 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4881 *d_nlines += s_nlines;
4886 static const struct got_error *
4887 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4888 struct got_object_id *commit_id, struct got_reflist_head *refs,
4889 struct got_repository *repo, int ignore_ws, int force_text_diff,
4890 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4892 const struct got_error *err = NULL;
4893 char datebuf[26], *datestr;
4894 struct got_commit_object *commit;
4895 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4896 time_t committer_time;
4897 const char *author, *committer;
4898 char *refs_str = NULL;
4899 struct got_pathlist_entry *pe;
4904 err = build_refs_str(&refs_str, refs, commit_id, repo);
4909 err = got_object_open_as_commit(&commit, repo, commit_id);
4913 err = got_object_id_str(&id_str, commit_id);
4915 err = got_error_from_errno("got_object_id_str");
4919 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4923 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4924 refs_str ? refs_str : "", refs_str ? ")" : "");
4926 err = got_error_from_errno("fprintf");
4930 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4934 n = fprintf(outfile, "from: %s\n",
4935 got_object_commit_get_author(commit));
4937 err = got_error_from_errno("fprintf");
4941 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4945 author = got_object_commit_get_author(commit);
4946 committer = got_object_commit_get_committer(commit);
4947 if (strcmp(author, committer) != 0) {
4948 n = fprintf(outfile, "via: %s\n", committer);
4950 err = got_error_from_errno("fprintf");
4954 err = add_line_metadata(lines, nlines, outoff,
4955 GOT_DIFF_LINE_AUTHOR);
4959 committer_time = got_object_commit_get_committer_time(commit);
4960 datestr = get_datestr(&committer_time, datebuf);
4962 n = fprintf(outfile, "date: %s UTC\n", datestr);
4964 err = got_error_from_errno("fprintf");
4968 err = add_line_metadata(lines, nlines, outoff,
4969 GOT_DIFF_LINE_DATE);
4973 if (got_object_commit_get_nparents(commit) > 1) {
4974 const struct got_object_id_queue *parent_ids;
4975 struct got_object_qid *qid;
4977 parent_ids = got_object_commit_get_parent_ids(commit);
4978 STAILQ_FOREACH(qid, parent_ids, entry) {
4979 err = got_object_id_str(&id_str, &qid->id);
4982 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4984 err = got_error_from_errno("fprintf");
4988 err = add_line_metadata(lines, nlines, outoff,
4989 GOT_DIFF_LINE_META);
4997 err = got_object_commit_get_logmsg(&logmsg, commit);
5001 while ((line = strsep(&s, "\n")) != NULL) {
5002 n = fprintf(outfile, "%s\n", line);
5004 err = got_error_from_errno("fprintf");
5008 err = add_line_metadata(lines, nlines, outoff,
5009 GOT_DIFF_LINE_LOGMSG);
5014 TAILQ_FOREACH(pe, dsa->paths, entry) {
5015 struct got_diff_changed_path *cp = pe->data;
5016 int pad = dsa->max_path_len - pe->path_len + 1;
5018 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5019 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5020 dsa->rm_cols + 1, cp->rm);
5022 err = got_error_from_errno("fprintf");
5026 err = add_line_metadata(lines, nlines, outoff,
5027 GOT_DIFF_LINE_CHANGES);
5032 fputc('\n', outfile);
5034 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5038 n = fprintf(outfile,
5039 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5040 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5041 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5043 err = got_error_from_errno("fprintf");
5047 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5051 fputc('\n', outfile);
5053 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5058 got_object_commit_close(commit);
5067 static const struct got_error *
5068 create_diff(struct tog_diff_view_state *s)
5070 const struct got_error *err = NULL;
5071 FILE *f = NULL, *tmp_diff_file = NULL;
5073 struct got_diff_line *lines = NULL;
5074 struct got_pathlist_head changed_paths;
5076 TAILQ_INIT(&changed_paths);
5079 s->lines = malloc(sizeof(*s->lines));
5080 if (s->lines == NULL)
5081 return got_error_from_errno("malloc");
5086 err = got_error_from_errno("got_opentemp");
5089 tmp_diff_file = got_opentemp();
5090 if (tmp_diff_file == NULL) {
5091 err = got_error_from_errno("got_opentemp");
5094 if (s->f && fclose(s->f) == EOF) {
5095 err = got_error_from_errno("fclose");
5101 err = got_object_get_type(&obj_type, s->repo, s->id1);
5103 err = got_object_get_type(&obj_type, s->repo, s->id2);
5108 case GOT_OBJ_TYPE_BLOB:
5109 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5110 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5111 s->label1, s->label2, tog_diff_algo, s->diff_context,
5112 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5115 case GOT_OBJ_TYPE_TREE:
5116 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5117 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5118 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5119 s->force_text_diff, NULL, s->repo, s->f);
5121 case GOT_OBJ_TYPE_COMMIT: {
5122 const struct got_object_id_queue *parent_ids;
5123 struct got_object_qid *pid;
5124 struct got_commit_object *commit2;
5125 struct got_reflist_head *refs;
5127 struct got_diffstat_cb_arg dsa = {
5130 s->ignore_whitespace,
5135 lines = malloc(sizeof(*lines));
5136 if (lines == NULL) {
5137 err = got_error_from_errno("malloc");
5141 /* build diff first in tmp file then append to commit info */
5142 err = got_diff_objects_as_commits(&lines, &nlines,
5143 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5144 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5145 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5149 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5152 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5153 /* Show commit info if we're diffing to a parent/root commit. */
5154 if (s->id1 == NULL) {
5155 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5156 refs, s->repo, s->ignore_whitespace,
5157 s->force_text_diff, &dsa, s->f);
5161 parent_ids = got_object_commit_get_parent_ids(commit2);
5162 STAILQ_FOREACH(pid, parent_ids, entry) {
5163 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5164 err = write_commit_info(&s->lines,
5165 &s->nlines, s->id2, refs, s->repo,
5166 s->ignore_whitespace,
5167 s->force_text_diff, &dsa, s->f);
5174 got_object_commit_close(commit2);
5176 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5181 err = got_error(GOT_ERR_OBJ_TYPE);
5186 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5187 if (s->f && fflush(s->f) != 0 && err == NULL)
5188 err = got_error_from_errno("fflush");
5189 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5190 err = got_error_from_errno("fclose");
5195 diff_view_indicate_progress(struct tog_view *view)
5197 mvwaddstr(view->window, 0, 0, "diffing...");
5202 static const struct got_error *
5203 search_start_diff_view(struct tog_view *view)
5205 struct tog_diff_view_state *s = &view->state.diff;
5207 s->matched_line = 0;
5212 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5213 size_t *nlines, int **first, int **last, int **match, int **selected)
5215 struct tog_diff_view_state *s = &view->state.diff;
5218 *nlines = s->nlines;
5219 *line_offsets = NULL;
5220 *match = &s->matched_line;
5221 *first = &s->first_displayed_line;
5222 *last = &s->last_displayed_line;
5223 *selected = &s->selected_line;
5226 static const struct got_error *
5227 search_next_view_match(struct tog_view *view)
5229 const struct got_error *err = NULL;
5233 size_t linesize = 0;
5235 off_t *line_offsets;
5237 int *first, *last, *match, *selected;
5239 if (!view->search_setup)
5240 return got_error_msg(GOT_ERR_NOT_IMPL,
5241 "view search not supported");
5242 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5245 if (!view->searching) {
5246 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5251 if (view->searching == TOG_SEARCH_FORWARD)
5252 lineno = *first + 1;
5254 lineno = *first - 1;
5256 lineno = *first - 1 + *selected;
5261 if (lineno <= 0 || lineno > nlines) {
5263 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5267 if (view->searching == TOG_SEARCH_FORWARD)
5273 offset = view->type == TOG_VIEW_DIFF ?
5274 view->state.diff.lines[lineno - 1].offset :
5275 line_offsets[lineno - 1];
5276 if (fseeko(f, offset, SEEK_SET) != 0) {
5278 return got_error_from_errno("fseeko");
5280 linelen = getline(&line, &linesize, f);
5281 if (linelen != -1) {
5283 err = expand_tab(&exstr, line);
5286 if (match_line(exstr, &view->regex, 1,
5288 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5295 if (view->searching == TOG_SEARCH_FORWARD)
5310 static const struct got_error *
5311 close_diff_view(struct tog_view *view)
5313 const struct got_error *err = NULL;
5314 struct tog_diff_view_state *s = &view->state.diff;
5320 if (s->f && fclose(s->f) == EOF)
5321 err = got_error_from_errno("fclose");
5323 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5324 err = got_error_from_errno("fclose");
5326 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5327 err = got_error_from_errno("fclose");
5329 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5330 err = got_error_from_errno("close");
5332 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5333 err = got_error_from_errno("close");
5341 static const struct got_error *
5342 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5343 struct got_object_id *id2, const char *label1, const char *label2,
5344 int diff_context, int ignore_whitespace, int force_text_diff,
5345 struct tog_view *parent_view, struct got_repository *repo)
5347 const struct got_error *err;
5348 struct tog_diff_view_state *s = &view->state.diff;
5350 memset(s, 0, sizeof(*s));
5354 if (id1 != NULL && id2 != NULL) {
5356 err = got_object_get_type(&type1, repo, id1);
5359 err = got_object_get_type(&type2, repo, id2);
5364 return got_error(GOT_ERR_OBJ_TYPE);
5366 s->first_displayed_line = 1;
5367 s->last_displayed_line = view->nlines;
5368 s->selected_line = 1;
5376 s->id1 = got_object_id_dup(id1);
5378 return got_error_from_errno("got_object_id_dup");
5382 s->id2 = got_object_id_dup(id2);
5383 if (s->id2 == NULL) {
5384 err = got_error_from_errno("got_object_id_dup");
5388 s->f1 = got_opentemp();
5389 if (s->f1 == NULL) {
5390 err = got_error_from_errno("got_opentemp");
5394 s->f2 = got_opentemp();
5395 if (s->f2 == NULL) {
5396 err = got_error_from_errno("got_opentemp");
5400 s->fd1 = got_opentempfd();
5402 err = got_error_from_errno("got_opentempfd");
5406 s->fd2 = got_opentempfd();
5408 err = got_error_from_errno("got_opentempfd");
5412 s->diff_context = diff_context;
5413 s->ignore_whitespace = ignore_whitespace;
5414 s->force_text_diff = force_text_diff;
5415 s->parent_view = parent_view;
5418 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5421 rc = init_pair(GOT_DIFF_LINE_MINUS,
5422 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5424 rc = init_pair(GOT_DIFF_LINE_PLUS,
5425 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5427 rc = init_pair(GOT_DIFF_LINE_HUNK,
5428 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5430 rc = init_pair(GOT_DIFF_LINE_META,
5431 get_color_value("TOG_COLOR_DIFF_META"), -1);
5433 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5434 get_color_value("TOG_COLOR_DIFF_META"), -1);
5436 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5437 get_color_value("TOG_COLOR_DIFF_META"), -1);
5439 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5440 get_color_value("TOG_COLOR_DIFF_META"), -1);
5442 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5443 get_color_value("TOG_COLOR_AUTHOR"), -1);
5445 rc = init_pair(GOT_DIFF_LINE_DATE,
5446 get_color_value("TOG_COLOR_DATE"), -1);
5448 err = got_error(GOT_ERR_RANGE);
5453 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5454 view_is_splitscreen(view))
5455 show_log_view(parent_view); /* draw border */
5456 diff_view_indicate_progress(view);
5458 err = create_diff(s);
5460 view->show = show_diff_view;
5461 view->input = input_diff_view;
5462 view->reset = reset_diff_view;
5463 view->close = close_diff_view;
5464 view->search_start = search_start_diff_view;
5465 view->search_setup = search_setup_diff_view;
5466 view->search_next = search_next_view_match;
5469 close_diff_view(view);
5473 static const struct got_error *
5474 show_diff_view(struct tog_view *view)
5476 const struct got_error *err;
5477 struct tog_diff_view_state *s = &view->state.diff;
5478 char *id_str1 = NULL, *id_str2, *header;
5479 const char *label1, *label2;
5482 err = got_object_id_str(&id_str1, s->id1);
5485 label1 = s->label1 ? s->label1 : id_str1;
5487 label1 = "/dev/null";
5489 err = got_object_id_str(&id_str2, s->id2);
5492 label2 = s->label2 ? s->label2 : id_str2;
5494 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5495 err = got_error_from_errno("asprintf");
5503 err = draw_file(view, header);
5508 static const struct got_error *
5509 set_selected_commit(struct tog_diff_view_state *s,
5510 struct commit_queue_entry *entry)
5512 const struct got_error *err;
5513 const struct got_object_id_queue *parent_ids;
5514 struct got_commit_object *selected_commit;
5515 struct got_object_qid *pid;
5518 s->id2 = got_object_id_dup(entry->id);
5520 return got_error_from_errno("got_object_id_dup");
5522 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5525 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5527 pid = STAILQ_FIRST(parent_ids);
5528 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5529 got_object_commit_close(selected_commit);
5533 static const struct got_error *
5534 reset_diff_view(struct tog_view *view)
5536 struct tog_diff_view_state *s = &view->state.diff;
5539 wclear(view->window);
5540 s->first_displayed_line = 1;
5541 s->last_displayed_line = view->nlines;
5542 s->matched_line = 0;
5543 diff_view_indicate_progress(view);
5544 return create_diff(s);
5548 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5552 i = start = s->first_displayed_line - 1;
5554 while (s->lines[i].type != type) {
5558 return; /* do nothing, requested type not in file */
5561 s->selected_line = 1;
5562 s->first_displayed_line = i;
5566 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5570 i = start = s->first_displayed_line + 1;
5572 while (s->lines[i].type != type) {
5573 if (i == s->nlines - 1)
5576 return; /* do nothing, requested type not in file */
5579 s->selected_line = 1;
5580 s->first_displayed_line = i;
5583 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5585 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5588 static const struct got_error *
5589 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5591 const struct got_error *err = NULL;
5592 struct tog_diff_view_state *s = &view->state.diff;
5593 struct tog_log_view_state *ls;
5594 struct commit_queue_entry *old_selected_entry;
5596 size_t linesize = 0;
5598 int i, nscroll = view->nlines - 1, up = 0;
5600 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5609 horizontal_scroll_input(view, ch);
5614 s->force_text_diff = !s->force_text_diff;
5615 view->action = s->force_text_diff ?
5616 "force ASCII text enabled" :
5617 "force ASCII text disabled";
5619 else if (ch == 'w') {
5620 s->ignore_whitespace = !s->ignore_whitespace;
5621 view->action = s->ignore_whitespace ?
5622 "ignore whitespace enabled" :
5623 "ignore whitespace disabled";
5625 err = reset_diff_view(view);
5629 s->first_displayed_line = 1;
5638 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5644 if (s->first_displayed_line > 1)
5645 s->first_displayed_line--;
5656 if (s->first_displayed_line == 1) {
5661 while (i++ < nscroll && s->first_displayed_line > 1)
5662 s->first_displayed_line--;
5668 s->first_displayed_line++;
5685 while (!s->eof && i++ < nscroll) {
5686 linelen = getline(&line, &linesize, s->f);
5687 s->first_displayed_line++;
5688 if (linelen == -1) {
5692 err = got_ferror(s->f, GOT_ERR_IO);
5699 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5702 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5705 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5708 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5711 if (s->diff_context > 0) {
5713 s->matched_line = 0;
5714 diff_view_indicate_progress(view);
5715 err = create_diff(s);
5716 if (s->first_displayed_line + view->nlines - 1 >
5718 s->first_displayed_line = 1;
5719 s->last_displayed_line = view->nlines;
5725 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5727 s->matched_line = 0;
5728 diff_view_indicate_progress(view);
5729 err = create_diff(s);
5741 if (s->parent_view == NULL) {
5745 s->parent_view->count = view->count;
5747 if (s->parent_view->type == TOG_VIEW_LOG) {
5748 ls = &s->parent_view->state.log;
5749 old_selected_entry = ls->selected_entry;
5751 err = input_log_view(NULL, s->parent_view,
5752 up ? KEY_UP : KEY_DOWN);
5755 view->count = s->parent_view->count;
5757 if (old_selected_entry == ls->selected_entry)
5760 err = set_selected_commit(s, ls->selected_entry);
5763 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5764 struct tog_blame_view_state *bs;
5765 struct got_object_id *id, *prev_id;
5767 bs = &s->parent_view->state.blame;
5768 prev_id = get_annotation_for_line(bs->blame.lines,
5769 bs->blame.nlines, bs->last_diffed_line);
5771 err = input_blame_view(&view, s->parent_view,
5772 up ? KEY_UP : KEY_DOWN);
5775 view->count = s->parent_view->count;
5777 if (prev_id == NULL)
5779 id = get_selected_commit_id(bs->blame.lines,
5780 bs->blame.nlines, bs->first_displayed_line,
5785 if (!got_object_id_cmp(prev_id, id))
5788 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5792 s->first_displayed_line = 1;
5793 s->last_displayed_line = view->nlines;
5794 s->matched_line = 0;
5797 diff_view_indicate_progress(view);
5798 err = create_diff(s);
5808 static const struct got_error *
5809 cmd_diff(int argc, char *argv[])
5811 const struct got_error *io_err, *error;
5812 struct got_repository *repo = NULL;
5813 struct got_worktree *worktree = NULL;
5814 struct got_object_id *id1 = NULL, *id2 = NULL;
5815 char *repo_path = NULL, *cwd = NULL;
5816 char *id_str1 = NULL, *id_str2 = NULL;
5817 char *label1 = NULL, *label2 = NULL;
5818 int diff_context = 3, ignore_whitespace = 0;
5819 int ch, force_text_diff = 0;
5821 struct tog_view *view;
5822 struct tog_io *tog_io = NULL;
5823 int *pack_fds = NULL;
5825 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5828 force_text_diff = 1;
5831 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5834 errx(1, "number of context lines is %s: %s",
5838 repo_path = realpath(optarg, NULL);
5839 if (repo_path == NULL)
5840 return got_error_from_errno2("realpath",
5842 got_path_strip_trailing_slashes(repo_path);
5845 ignore_whitespace = 1;
5857 usage_diff(); /* TODO show local worktree changes */
5858 } else if (argc == 2) {
5864 error = got_repo_pack_fds_open(&pack_fds);
5868 if (repo_path == NULL) {
5869 cwd = getcwd(NULL, 0);
5871 return got_error_from_errno("getcwd");
5872 error = got_worktree_open(&worktree, cwd);
5873 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5877 strdup(got_worktree_get_repo_path(worktree));
5879 repo_path = strdup(cwd);
5880 if (repo_path == NULL) {
5881 error = got_error_from_errno("strdup");
5886 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5890 error = init_curses(&tog_io);
5894 error = apply_unveil(got_repo_get_path(repo), NULL);
5898 error = tog_load_refs(repo, 0);
5902 error = got_repo_match_object_id(&id1, &label1, id_str1,
5903 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5907 error = got_repo_match_object_id(&id2, &label2, id_str2,
5908 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5912 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5914 error = got_error_from_errno("view_open");
5917 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5918 ignore_whitespace, force_text_diff, NULL, repo);
5921 error = view_loop(view, tog_io);
5928 const struct got_error *close_err = got_repo_close(repo);
5933 got_worktree_close(worktree);
5935 const struct got_error *pack_err =
5936 got_repo_pack_fds_close(pack_fds);
5940 if (tog_io != NULL) {
5941 io_err = tog_io_close(tog_io);
5954 "usage: %s blame [-c commit] [-r repository-path] path\n",
5959 struct tog_blame_line {
5961 struct got_object_id *id;
5964 static const struct got_error *
5965 draw_blame(struct tog_view *view)
5967 struct tog_blame_view_state *s = &view->state.blame;
5968 struct tog_blame *blame = &s->blame;
5969 regmatch_t *regmatch = &view->regmatch;
5970 const struct got_error *err;
5971 int lineno = 0, nprinted = 0;
5973 size_t linesize = 0;
5977 struct tog_blame_line *blame_line;
5978 struct got_object_id *prev_id = NULL;
5980 struct tog_color *tc;
5982 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5987 werase(view->window);
5989 if (asprintf(&line, "commit %s", id_str) == -1) {
5990 err = got_error_from_errno("asprintf");
5995 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6000 if (view_needs_focus_indication(view))
6001 wstandout(view->window);
6002 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6004 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6005 waddwstr(view->window, wline);
6006 while (width++ < view->ncols)
6007 waddch(view->window, ' ');
6009 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6010 if (view_needs_focus_indication(view))
6011 wstandend(view->window);
6015 if (view->gline > blame->nlines)
6016 view->gline = blame->nlines;
6018 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6019 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6020 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6022 return got_error_from_errno("asprintf");
6025 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6030 waddwstr(view->window, wline);
6033 if (width < view->ncols - 1)
6034 waddch(view->window, '\n');
6038 while (nprinted < view->nlines - 2) {
6039 linelen = getline(&line, &linesize, blame->f);
6040 if (linelen == -1) {
6041 if (feof(blame->f)) {
6046 return got_ferror(blame->f, GOT_ERR_IO);
6048 if (++lineno < s->first_displayed_line)
6050 if (view->gline && !gotoline(view, &lineno, &nprinted))
6053 /* Set view->maxx based on full line length. */
6054 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6061 view->maxx = MAX(view->maxx, width);
6063 if (nprinted == s->selected_line - 1)
6064 wstandout(view->window);
6066 if (blame->nlines > 0) {
6067 blame_line = &blame->lines[lineno - 1];
6068 if (blame_line->annotated && prev_id &&
6069 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6070 !(nprinted == s->selected_line - 1)) {
6071 waddstr(view->window, " ");
6072 } else if (blame_line->annotated) {
6074 err = got_object_id_str(&id_str,
6080 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6082 wattr_on(view->window,
6083 COLOR_PAIR(tc->colorpair), NULL);
6084 wprintw(view->window, "%.8s", id_str);
6086 wattr_off(view->window,
6087 COLOR_PAIR(tc->colorpair), NULL);
6089 prev_id = blame_line->id;
6091 waddstr(view->window, "........");
6095 waddstr(view->window, "........");
6099 if (nprinted == s->selected_line - 1)
6100 wstandend(view->window);
6101 waddstr(view->window, " ");
6103 if (view->ncols <= 9) {
6105 } else if (s->first_displayed_line + nprinted ==
6107 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6108 err = add_matched_line(&width, line, view->ncols - 9, 9,
6109 view->window, view->x, regmatch);
6117 err = format_line(&wline, &width, &skip, line,
6118 view->x, view->ncols - 9, 9, 1);
6123 waddwstr(view->window, &wline[skip]);
6129 if (width <= view->ncols - 1)
6130 waddch(view->window, '\n');
6131 if (++nprinted == 1)
6132 s->first_displayed_line = lineno;
6135 s->last_displayed_line = lineno;
6142 static const struct got_error *
6143 blame_cb(void *arg, int nlines, int lineno,
6144 struct got_commit_object *commit, struct got_object_id *id)
6146 const struct got_error *err = NULL;
6147 struct tog_blame_cb_args *a = arg;
6148 struct tog_blame_line *line;
6151 if (nlines != a->nlines ||
6152 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6153 return got_error(GOT_ERR_RANGE);
6155 errcode = pthread_mutex_lock(&tog_mutex);
6157 return got_error_set_errno(errcode, "pthread_mutex_lock");
6159 if (*a->quit) { /* user has quit the blame view */
6160 err = got_error(GOT_ERR_ITER_COMPLETED);
6165 goto done; /* no change in this commit */
6167 line = &a->lines[lineno - 1];
6168 if (line->annotated)
6171 line->id = got_object_id_dup(id);
6172 if (line->id == NULL) {
6173 err = got_error_from_errno("got_object_id_dup");
6176 line->annotated = 1;
6178 errcode = pthread_mutex_unlock(&tog_mutex);
6180 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6185 blame_thread(void *arg)
6187 const struct got_error *err, *close_err;
6188 struct tog_blame_thread_args *ta = arg;
6189 struct tog_blame_cb_args *a = ta->cb_args;
6190 int errcode, fd1 = -1, fd2 = -1;
6191 FILE *f1 = NULL, *f2 = NULL;
6193 fd1 = got_opentempfd();
6195 return (void *)got_error_from_errno("got_opentempfd");
6197 fd2 = got_opentempfd();
6199 err = got_error_from_errno("got_opentempfd");
6203 f1 = got_opentemp();
6205 err = (void *)got_error_from_errno("got_opentemp");
6208 f2 = got_opentemp();
6210 err = (void *)got_error_from_errno("got_opentemp");
6214 err = block_signals_used_by_main_thread();
6218 err = got_blame(ta->path, a->commit_id, ta->repo,
6219 tog_diff_algo, blame_cb, ta->cb_args,
6220 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6221 if (err && err->code == GOT_ERR_CANCELLED)
6224 errcode = pthread_mutex_lock(&tog_mutex);
6226 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6230 close_err = got_repo_close(ta->repo);
6236 errcode = pthread_mutex_unlock(&tog_mutex);
6237 if (errcode && err == NULL)
6238 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6241 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6242 err = got_error_from_errno("close");
6243 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6244 err = got_error_from_errno("close");
6245 if (f1 && fclose(f1) == EOF && err == NULL)
6246 err = got_error_from_errno("fclose");
6247 if (f2 && fclose(f2) == EOF && err == NULL)
6248 err = got_error_from_errno("fclose");
6253 static struct got_object_id *
6254 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6255 int first_displayed_line, int selected_line)
6257 struct tog_blame_line *line;
6262 line = &lines[first_displayed_line - 1 + selected_line - 1];
6263 if (!line->annotated)
6269 static struct got_object_id *
6270 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6273 struct tog_blame_line *line;
6275 if (nlines <= 0 || lineno >= nlines)
6278 line = &lines[lineno - 1];
6279 if (!line->annotated)
6285 static const struct got_error *
6286 stop_blame(struct tog_blame *blame)
6288 const struct got_error *err = NULL;
6291 if (blame->thread) {
6293 errcode = pthread_mutex_unlock(&tog_mutex);
6295 return got_error_set_errno(errcode,
6296 "pthread_mutex_unlock");
6297 errcode = pthread_join(blame->thread, (void **)&err);
6299 return got_error_set_errno(errcode, "pthread_join");
6300 errcode = pthread_mutex_lock(&tog_mutex);
6302 return got_error_set_errno(errcode,
6303 "pthread_mutex_lock");
6304 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6306 blame->thread = 0; //NULL;
6308 if (blame->thread_args.repo) {
6309 const struct got_error *close_err;
6310 close_err = got_repo_close(blame->thread_args.repo);
6313 blame->thread_args.repo = NULL;
6316 if (fclose(blame->f) == EOF && err == NULL)
6317 err = got_error_from_errno("fclose");
6321 for (i = 0; i < blame->nlines; i++)
6322 free(blame->lines[i].id);
6324 blame->lines = NULL;
6326 free(blame->cb_args.commit_id);
6327 blame->cb_args.commit_id = NULL;
6328 if (blame->pack_fds) {
6329 const struct got_error *pack_err =
6330 got_repo_pack_fds_close(blame->pack_fds);
6333 blame->pack_fds = NULL;
6338 static const struct got_error *
6339 cancel_blame_view(void *arg)
6341 const struct got_error *err = NULL;
6345 errcode = pthread_mutex_lock(&tog_mutex);
6347 return got_error_set_errno(errcode,
6348 "pthread_mutex_unlock");
6351 err = got_error(GOT_ERR_CANCELLED);
6353 errcode = pthread_mutex_unlock(&tog_mutex);
6355 return got_error_set_errno(errcode,
6356 "pthread_mutex_lock");
6361 static const struct got_error *
6362 run_blame(struct tog_view *view)
6364 struct tog_blame_view_state *s = &view->state.blame;
6365 struct tog_blame *blame = &s->blame;
6366 const struct got_error *err = NULL;
6367 struct got_commit_object *commit = NULL;
6368 struct got_blob_object *blob = NULL;
6369 struct got_repository *thread_repo = NULL;
6370 struct got_object_id *obj_id = NULL;
6371 int obj_type, fd = -1;
6372 int *pack_fds = NULL;
6374 err = got_object_open_as_commit(&commit, s->repo,
6375 &s->blamed_commit->id);
6379 fd = got_opentempfd();
6381 err = got_error_from_errno("got_opentempfd");
6385 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6389 err = got_object_get_type(&obj_type, s->repo, obj_id);
6393 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6394 err = got_error(GOT_ERR_OBJ_TYPE);
6398 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6401 blame->f = got_opentemp();
6402 if (blame->f == NULL) {
6403 err = got_error_from_errno("got_opentemp");
6406 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6407 &blame->line_offsets, blame->f, blob);
6410 if (blame->nlines == 0) {
6411 s->blame_complete = 1;
6415 /* Don't include \n at EOF in the blame line count. */
6416 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6419 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6420 if (blame->lines == NULL) {
6421 err = got_error_from_errno("calloc");
6425 err = got_repo_pack_fds_open(&pack_fds);
6428 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6433 blame->pack_fds = pack_fds;
6434 blame->cb_args.view = view;
6435 blame->cb_args.lines = blame->lines;
6436 blame->cb_args.nlines = blame->nlines;
6437 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6438 if (blame->cb_args.commit_id == NULL) {
6439 err = got_error_from_errno("got_object_id_dup");
6442 blame->cb_args.quit = &s->done;
6444 blame->thread_args.path = s->path;
6445 blame->thread_args.repo = thread_repo;
6446 blame->thread_args.cb_args = &blame->cb_args;
6447 blame->thread_args.complete = &s->blame_complete;
6448 blame->thread_args.cancel_cb = cancel_blame_view;
6449 blame->thread_args.cancel_arg = &s->done;
6450 s->blame_complete = 0;
6452 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6453 s->first_displayed_line = 1;
6454 s->last_displayed_line = view->nlines;
6455 s->selected_line = 1;
6457 s->matched_line = 0;
6461 got_object_commit_close(commit);
6462 if (fd != -1 && close(fd) == -1 && err == NULL)
6463 err = got_error_from_errno("close");
6465 got_object_blob_close(blob);
6472 static const struct got_error *
6473 open_blame_view(struct tog_view *view, char *path,
6474 struct got_object_id *commit_id, struct got_repository *repo)
6476 const struct got_error *err = NULL;
6477 struct tog_blame_view_state *s = &view->state.blame;
6479 STAILQ_INIT(&s->blamed_commits);
6481 s->path = strdup(path);
6482 if (s->path == NULL)
6483 return got_error_from_errno("strdup");
6485 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6491 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6492 s->first_displayed_line = 1;
6493 s->last_displayed_line = view->nlines;
6494 s->selected_line = 1;
6495 s->blame_complete = 0;
6497 s->commit_id = commit_id;
6498 memset(&s->blame, 0, sizeof(s->blame));
6500 STAILQ_INIT(&s->colors);
6501 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6502 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6503 get_color_value("TOG_COLOR_COMMIT"));
6508 view->show = show_blame_view;
6509 view->input = input_blame_view;
6510 view->reset = reset_blame_view;
6511 view->close = close_blame_view;
6512 view->search_start = search_start_blame_view;
6513 view->search_setup = search_setup_blame_view;
6514 view->search_next = search_next_view_match;
6516 return run_blame(view);
6519 static const struct got_error *
6520 close_blame_view(struct tog_view *view)
6522 const struct got_error *err = NULL;
6523 struct tog_blame_view_state *s = &view->state.blame;
6525 if (s->blame.thread)
6526 err = stop_blame(&s->blame);
6528 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6529 struct got_object_qid *blamed_commit;
6530 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6531 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6532 got_object_qid_free(blamed_commit);
6536 free_colors(&s->colors);
6540 static const struct got_error *
6541 search_start_blame_view(struct tog_view *view)
6543 struct tog_blame_view_state *s = &view->state.blame;
6545 s->matched_line = 0;
6550 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6551 size_t *nlines, int **first, int **last, int **match, int **selected)
6553 struct tog_blame_view_state *s = &view->state.blame;
6556 *nlines = s->blame.nlines;
6557 *line_offsets = s->blame.line_offsets;
6558 *match = &s->matched_line;
6559 *first = &s->first_displayed_line;
6560 *last = &s->last_displayed_line;
6561 *selected = &s->selected_line;
6564 static const struct got_error *
6565 show_blame_view(struct tog_view *view)
6567 const struct got_error *err = NULL;
6568 struct tog_blame_view_state *s = &view->state.blame;
6571 if (s->blame.thread == 0 && !s->blame_complete) {
6572 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6573 &s->blame.thread_args);
6575 return got_error_set_errno(errcode, "pthread_create");
6577 halfdelay(1); /* fast refresh while annotating */
6580 if (s->blame_complete)
6581 halfdelay(10); /* disable fast refresh */
6583 err = draw_blame(view);
6589 static const struct got_error *
6590 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6591 struct got_repository *repo, struct got_object_id *id)
6593 struct tog_view *log_view;
6594 const struct got_error *err = NULL;
6598 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6599 if (log_view == NULL)
6600 return got_error_from_errno("view_open");
6602 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6604 view_close(log_view);
6606 *new_view = log_view;
6611 static const struct got_error *
6612 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6614 const struct got_error *err = NULL, *thread_err = NULL;
6615 struct tog_view *diff_view;
6616 struct tog_blame_view_state *s = &view->state.blame;
6617 int eos, nscroll, begin_y = 0, begin_x = 0;
6619 eos = nscroll = view->nlines - 2;
6620 if (view_is_hsplit_top(view))
6630 horizontal_scroll_input(view, ch);
6637 s->selected_line = 1;
6638 s->first_displayed_line = 1;
6643 if (s->blame.nlines < eos) {
6644 s->selected_line = s->blame.nlines;
6645 s->first_displayed_line = 1;
6647 s->selected_line = eos;
6648 s->first_displayed_line = s->blame.nlines - (eos - 1);
6655 if (s->selected_line > 1)
6657 else if (s->selected_line == 1 &&
6658 s->first_displayed_line > 1)
6659 s->first_displayed_line--;
6670 if (s->first_displayed_line == 1) {
6671 if (view->count > 1)
6673 s->selected_line = MAX(1, s->selected_line - nscroll);
6677 if (s->first_displayed_line > nscroll)
6678 s->first_displayed_line -= nscroll;
6680 s->first_displayed_line = 1;
6685 if (s->selected_line < eos && s->first_displayed_line +
6686 s->selected_line <= s->blame.nlines)
6688 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6689 s->first_displayed_line++;
6695 struct got_object_id *id = NULL;
6698 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6699 s->first_displayed_line, s->selected_line);
6703 struct got_commit_object *commit, *pcommit;
6704 struct got_object_qid *pid;
6705 struct got_object_id *blob_id = NULL;
6707 err = got_object_open_as_commit(&commit,
6712 got_object_commit_get_parent_ids(commit));
6714 got_object_commit_close(commit);
6717 /* Check if path history ends here. */
6718 err = got_object_open_as_commit(&pcommit,
6722 err = got_object_id_by_path(&blob_id, s->repo,
6724 got_object_commit_close(pcommit);
6726 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6728 got_object_commit_close(commit);
6731 err = got_object_get_type(&obj_type, s->repo,
6734 /* Can't blame non-blob type objects. */
6735 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6736 got_object_commit_close(commit);
6739 err = got_object_qid_alloc(&s->blamed_commit,
6741 got_object_commit_close(commit);
6743 if (got_object_id_cmp(id,
6744 &s->blamed_commit->id) == 0)
6746 err = got_object_qid_alloc(&s->blamed_commit,
6752 thread_err = stop_blame(&s->blame);
6756 STAILQ_INSERT_HEAD(&s->blamed_commits,
6757 s->blamed_commit, entry);
6758 err = run_blame(view);
6764 struct got_object_qid *first;
6767 first = STAILQ_FIRST(&s->blamed_commits);
6768 if (!got_object_id_cmp(&first->id, s->commit_id))
6771 thread_err = stop_blame(&s->blame);
6775 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6776 got_object_qid_free(s->blamed_commit);
6778 STAILQ_FIRST(&s->blamed_commits);
6779 err = run_blame(view);
6786 s->id_to_log = get_selected_commit_id(s->blame.lines,
6787 s->blame.nlines, s->first_displayed_line, s->selected_line);
6789 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6793 struct got_object_id *id = NULL;
6794 struct got_object_qid *pid;
6795 struct got_commit_object *commit = NULL;
6798 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6799 s->first_displayed_line, s->selected_line);
6802 err = got_object_open_as_commit(&commit, s->repo, id);
6805 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6807 /* traversed from diff view, release diff resources */
6808 err = close_diff_view(*new_view);
6811 diff_view = *new_view;
6813 if (view_is_parent_view(view))
6814 view_get_split(view, &begin_y, &begin_x);
6816 diff_view = view_open(0, 0, begin_y, begin_x,
6818 if (diff_view == NULL) {
6819 got_object_commit_close(commit);
6820 err = got_error_from_errno("view_open");
6824 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6825 id, NULL, NULL, 3, 0, 0, view, s->repo);
6826 got_object_commit_close(commit);
6828 view_close(diff_view);
6831 s->last_diffed_line = s->first_displayed_line - 1 +
6834 break; /* still open from active diff view */
6835 if (view_is_parent_view(view) &&
6836 view->mode == TOG_VIEW_SPLIT_HRZN) {
6837 err = view_init_hsplit(view, begin_y);
6843 diff_view->focussed = 1;
6844 diff_view->mode = view->mode;
6845 diff_view->nlines = view->lines - begin_y;
6846 if (view_is_parent_view(view)) {
6847 view_transfer_size(diff_view, view);
6848 err = view_close_child(view);
6851 err = view_set_child(view, diff_view);
6854 view->focus_child = 1;
6856 *new_view = diff_view;
6869 if (s->last_displayed_line >= s->blame.nlines &&
6870 s->selected_line >= MIN(s->blame.nlines,
6871 view->nlines - 2)) {
6875 if (s->last_displayed_line >= s->blame.nlines &&
6876 s->selected_line < view->nlines - 2) {
6878 MIN(nscroll, s->last_displayed_line -
6879 s->first_displayed_line - s->selected_line + 1);
6881 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6882 s->first_displayed_line += nscroll;
6884 s->first_displayed_line =
6885 s->blame.nlines - (view->nlines - 3);
6888 if (s->selected_line > view->nlines - 2) {
6889 s->selected_line = MIN(s->blame.nlines,
6897 return thread_err ? thread_err : err;
6900 static const struct got_error *
6901 reset_blame_view(struct tog_view *view)
6903 const struct got_error *err;
6904 struct tog_blame_view_state *s = &view->state.blame;
6908 err = stop_blame(&s->blame);
6912 return run_blame(view);
6915 static const struct got_error *
6916 cmd_blame(int argc, char *argv[])
6918 const struct got_error *io_err, *error;
6919 struct got_repository *repo = NULL;
6920 struct got_worktree *worktree = NULL;
6921 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6922 char *link_target = NULL;
6923 struct got_object_id *commit_id = NULL;
6924 struct got_commit_object *commit = NULL;
6925 char *commit_id_str = NULL;
6927 struct tog_view *view;
6928 struct tog_io *tog_io = NULL;
6929 int *pack_fds = NULL;
6931 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6934 commit_id_str = optarg;
6937 repo_path = realpath(optarg, NULL);
6938 if (repo_path == NULL)
6939 return got_error_from_errno2("realpath",
6954 error = got_repo_pack_fds_open(&pack_fds);
6958 if (repo_path == NULL) {
6959 cwd = getcwd(NULL, 0);
6961 return got_error_from_errno("getcwd");
6962 error = got_worktree_open(&worktree, cwd);
6963 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6967 strdup(got_worktree_get_repo_path(worktree));
6969 repo_path = strdup(cwd);
6970 if (repo_path == NULL) {
6971 error = got_error_from_errno("strdup");
6976 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6980 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6985 error = init_curses(&tog_io);
6989 error = apply_unveil(got_repo_get_path(repo), NULL);
6993 error = tog_load_refs(repo, 0);
6997 if (commit_id_str == NULL) {
6998 struct got_reference *head_ref;
6999 error = got_ref_open(&head_ref, repo, worktree ?
7000 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7003 error = got_ref_resolve(&commit_id, repo, head_ref);
7004 got_ref_close(head_ref);
7006 error = got_repo_match_object_id(&commit_id, NULL,
7007 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7012 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7014 error = got_error_from_errno("view_open");
7018 error = got_object_open_as_commit(&commit, repo, commit_id);
7022 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7027 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7032 /* Release work tree lock. */
7033 got_worktree_close(worktree);
7036 error = view_loop(view, tog_io);
7044 got_object_commit_close(commit);
7046 got_worktree_close(worktree);
7048 const struct got_error *close_err = got_repo_close(repo);
7053 const struct got_error *pack_err =
7054 got_repo_pack_fds_close(pack_fds);
7058 if (tog_io != NULL) {
7059 io_err = tog_io_close(tog_io);
7067 static const struct got_error *
7068 draw_tree_entries(struct tog_view *view, const char *parent_path)
7070 struct tog_tree_view_state *s = &view->state.tree;
7071 const struct got_error *err = NULL;
7072 struct got_tree_entry *te;
7075 struct tog_color *tc;
7076 int width, n, nentries, scrollx, i = 1;
7077 int limit = view->nlines;
7080 if (view_is_hsplit_top(view))
7081 --limit; /* border */
7083 werase(view->window);
7088 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7092 if (view_needs_focus_indication(view))
7093 wstandout(view->window);
7094 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7096 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7097 waddwstr(view->window, wline);
7100 while (width++ < view->ncols)
7101 waddch(view->window, ' ');
7103 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7104 if (view_needs_focus_indication(view))
7105 wstandend(view->window);
7110 if (s->first_displayed_entry) {
7111 i += got_tree_entry_get_index(s->first_displayed_entry);
7112 if (s->tree != s->root)
7113 ++i; /* account for ".." entry */
7115 nentries = got_object_tree_get_nentries(s->tree);
7116 if (asprintf(&index, "[%d/%d] %s",
7117 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7118 return got_error_from_errno("asprintf");
7119 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7123 waddwstr(view->window, wline);
7126 if (width < view->ncols - 1)
7127 waddch(view->window, '\n');
7130 waddch(view->window, '\n');
7134 if (s->first_displayed_entry == NULL) {
7135 te = got_object_tree_get_first_entry(s->tree);
7136 if (s->selected == 0) {
7138 wstandout(view->window);
7139 s->selected_entry = NULL;
7141 waddstr(view->window, " ..\n"); /* parent directory */
7142 if (s->selected == 0 && view->focussed)
7143 wstandend(view->window);
7150 te = s->first_displayed_entry;
7154 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7155 char *line = NULL, *id_str = NULL, *link_target = NULL;
7156 const char *modestr = "";
7159 te = got_object_tree_get_entry(s->tree, i);
7160 mode = got_tree_entry_get_mode(te);
7163 err = got_object_id_str(&id_str,
7164 got_tree_entry_get_id(te));
7166 return got_error_from_errno(
7167 "got_object_id_str");
7169 if (got_object_tree_entry_is_submodule(te))
7171 else if (S_ISLNK(mode)) {
7174 err = got_tree_entry_get_symlink_target(&link_target,
7180 for (i = 0; i < strlen(link_target); i++) {
7181 if (!isprint((unsigned char)link_target[i]))
7182 link_target[i] = '?';
7186 else if (S_ISDIR(mode))
7188 else if (mode & S_IXUSR)
7190 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7191 got_tree_entry_get_name(te), modestr,
7192 link_target ? " -> ": "",
7193 link_target ? link_target : "") == -1) {
7196 return got_error_from_errno("asprintf");
7201 /* use full line width to determine view->maxx */
7202 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7207 view->maxx = MAX(view->maxx, width);
7211 err = format_line(&wline, &width, &scrollx, line, view->x,
7217 if (n == s->selected) {
7219 wstandout(view->window);
7220 s->selected_entry = te;
7222 tc = match_color(&s->colors, line);
7224 wattr_on(view->window,
7225 COLOR_PAIR(tc->colorpair), NULL);
7226 waddwstr(view->window, &wline[scrollx]);
7228 wattr_off(view->window,
7229 COLOR_PAIR(tc->colorpair), NULL);
7230 if (width < view->ncols)
7231 waddch(view->window, '\n');
7232 if (n == s->selected && view->focussed)
7233 wstandend(view->window);
7239 s->last_displayed_entry = te;
7248 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7250 struct got_tree_entry *te;
7251 int isroot = s->tree == s->root;
7254 if (s->first_displayed_entry == NULL)
7257 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7258 while (i++ < maxscroll) {
7261 s->first_displayed_entry = NULL;
7264 s->first_displayed_entry = te;
7265 te = got_tree_entry_get_prev(s->tree, te);
7269 static const struct got_error *
7270 tree_scroll_down(struct tog_view *view, int maxscroll)
7272 struct tog_tree_view_state *s = &view->state.tree;
7273 struct got_tree_entry *next, *last;
7276 if (s->first_displayed_entry)
7277 next = got_tree_entry_get_next(s->tree,
7278 s->first_displayed_entry);
7280 next = got_object_tree_get_first_entry(s->tree);
7282 last = s->last_displayed_entry;
7283 while (next && n++ < maxscroll) {
7285 s->last_displayed_entry = last;
7286 last = got_tree_entry_get_next(s->tree, last);
7288 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7289 s->first_displayed_entry = next;
7290 next = got_tree_entry_get_next(s->tree, next);
7297 static const struct got_error *
7298 tree_entry_path(char **path, struct tog_parent_trees *parents,
7299 struct got_tree_entry *te)
7301 const struct got_error *err = NULL;
7302 struct tog_parent_tree *pt;
7303 size_t len = 2; /* for leading slash and NUL */
7305 TAILQ_FOREACH(pt, parents, entry)
7306 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7309 len += strlen(got_tree_entry_get_name(te));
7311 *path = calloc(1, len);
7313 return got_error_from_errno("calloc");
7316 pt = TAILQ_LAST(parents, tog_parent_trees);
7318 const char *name = got_tree_entry_get_name(pt->selected_entry);
7319 if (strlcat(*path, name, len) >= len) {
7320 err = got_error(GOT_ERR_NO_SPACE);
7323 if (strlcat(*path, "/", len) >= len) {
7324 err = got_error(GOT_ERR_NO_SPACE);
7327 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7330 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7331 err = got_error(GOT_ERR_NO_SPACE);
7343 static const struct got_error *
7344 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7345 struct got_tree_entry *te, struct tog_parent_trees *parents,
7346 struct got_object_id *commit_id, struct got_repository *repo)
7348 const struct got_error *err = NULL;
7350 struct tog_view *blame_view;
7354 err = tree_entry_path(&path, parents, te);
7358 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7359 if (blame_view == NULL) {
7360 err = got_error_from_errno("view_open");
7364 err = open_blame_view(blame_view, path, commit_id, repo);
7366 if (err->code == GOT_ERR_CANCELLED)
7368 view_close(blame_view);
7370 *new_view = blame_view;
7376 static const struct got_error *
7377 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7378 struct tog_tree_view_state *s)
7380 struct tog_view *log_view;
7381 const struct got_error *err = NULL;
7386 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7387 if (log_view == NULL)
7388 return got_error_from_errno("view_open");
7390 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7394 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7397 view_close(log_view);
7399 *new_view = log_view;
7404 static const struct got_error *
7405 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7406 const char *head_ref_name, struct got_repository *repo)
7408 const struct got_error *err = NULL;
7409 char *commit_id_str = NULL;
7410 struct tog_tree_view_state *s = &view->state.tree;
7411 struct got_commit_object *commit = NULL;
7413 TAILQ_INIT(&s->parents);
7414 STAILQ_INIT(&s->colors);
7416 s->commit_id = got_object_id_dup(commit_id);
7417 if (s->commit_id == NULL)
7418 return got_error_from_errno("got_object_id_dup");
7420 err = got_object_open_as_commit(&commit, repo, commit_id);
7425 * The root is opened here and will be closed when the view is closed.
7426 * Any visited subtrees and their path-wise parents are opened and
7429 err = got_object_open_as_tree(&s->root, repo,
7430 got_object_commit_get_tree_id(commit));
7435 err = got_object_id_str(&commit_id_str, commit_id);
7439 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7440 err = got_error_from_errno("asprintf");
7444 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7445 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7446 if (head_ref_name) {
7447 s->head_ref_name = strdup(head_ref_name);
7448 if (s->head_ref_name == NULL) {
7449 err = got_error_from_errno("strdup");
7455 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7456 err = add_color(&s->colors, "\\$$",
7457 TOG_COLOR_TREE_SUBMODULE,
7458 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7461 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7462 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7465 err = add_color(&s->colors, "/$",
7466 TOG_COLOR_TREE_DIRECTORY,
7467 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7471 err = add_color(&s->colors, "\\*$",
7472 TOG_COLOR_TREE_EXECUTABLE,
7473 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7477 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7478 get_color_value("TOG_COLOR_COMMIT"));
7483 view->show = show_tree_view;
7484 view->input = input_tree_view;
7485 view->close = close_tree_view;
7486 view->search_start = search_start_tree_view;
7487 view->search_next = search_next_tree_view;
7489 free(commit_id_str);
7491 got_object_commit_close(commit);
7493 close_tree_view(view);
7497 static const struct got_error *
7498 close_tree_view(struct tog_view *view)
7500 struct tog_tree_view_state *s = &view->state.tree;
7502 free_colors(&s->colors);
7503 free(s->tree_label);
7504 s->tree_label = NULL;
7506 s->commit_id = NULL;
7507 free(s->head_ref_name);
7508 s->head_ref_name = NULL;
7509 while (!TAILQ_EMPTY(&s->parents)) {
7510 struct tog_parent_tree *parent;
7511 parent = TAILQ_FIRST(&s->parents);
7512 TAILQ_REMOVE(&s->parents, parent, entry);
7513 if (parent->tree != s->root)
7514 got_object_tree_close(parent->tree);
7518 if (s->tree != NULL && s->tree != s->root)
7519 got_object_tree_close(s->tree);
7521 got_object_tree_close(s->root);
7525 static const struct got_error *
7526 search_start_tree_view(struct tog_view *view)
7528 struct tog_tree_view_state *s = &view->state.tree;
7530 s->matched_entry = NULL;
7535 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7537 regmatch_t regmatch;
7539 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7543 static const struct got_error *
7544 search_next_tree_view(struct tog_view *view)
7546 struct tog_tree_view_state *s = &view->state.tree;
7547 struct got_tree_entry *te = NULL;
7549 if (!view->searching) {
7550 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7554 if (s->matched_entry) {
7555 if (view->searching == TOG_SEARCH_FORWARD) {
7556 if (s->selected_entry)
7557 te = got_tree_entry_get_next(s->tree,
7560 te = got_object_tree_get_first_entry(s->tree);
7562 if (s->selected_entry == NULL)
7563 te = got_object_tree_get_last_entry(s->tree);
7565 te = got_tree_entry_get_prev(s->tree,
7569 if (s->selected_entry)
7570 te = s->selected_entry;
7571 else if (view->searching == TOG_SEARCH_FORWARD)
7572 te = got_object_tree_get_first_entry(s->tree);
7574 te = got_object_tree_get_last_entry(s->tree);
7579 if (s->matched_entry == NULL) {
7580 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7583 if (view->searching == TOG_SEARCH_FORWARD)
7584 te = got_object_tree_get_first_entry(s->tree);
7586 te = got_object_tree_get_last_entry(s->tree);
7589 if (match_tree_entry(te, &view->regex)) {
7590 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7591 s->matched_entry = te;
7595 if (view->searching == TOG_SEARCH_FORWARD)
7596 te = got_tree_entry_get_next(s->tree, te);
7598 te = got_tree_entry_get_prev(s->tree, te);
7601 if (s->matched_entry) {
7602 s->first_displayed_entry = s->matched_entry;
7609 static const struct got_error *
7610 show_tree_view(struct tog_view *view)
7612 const struct got_error *err = NULL;
7613 struct tog_tree_view_state *s = &view->state.tree;
7616 err = tree_entry_path(&parent_path, &s->parents, NULL);
7620 err = draw_tree_entries(view, parent_path);
7627 static const struct got_error *
7628 tree_goto_line(struct tog_view *view, int nlines)
7630 const struct got_error *err = NULL;
7631 struct tog_tree_view_state *s = &view->state.tree;
7632 struct got_tree_entry **fte, **lte, **ste;
7633 int g, last, first = 1, i = 1;
7634 int root = s->tree == s->root;
7635 int off = root ? 1 : 2;
7642 else if (g > got_object_tree_get_nentries(s->tree))
7643 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7645 fte = &s->first_displayed_entry;
7646 lte = &s->last_displayed_entry;
7647 ste = &s->selected_entry;
7650 first = got_tree_entry_get_index(*fte);
7651 first += off; /* account for ".." */
7653 last = got_tree_entry_get_index(*lte);
7656 if (g >= first && g <= last && g - first < nlines) {
7657 s->selected = g - first;
7658 return NULL; /* gline is on the current page */
7662 i = got_tree_entry_get_index(*ste);
7667 err = tree_scroll_down(view, g - i);
7670 if (got_tree_entry_get_index(*lte) >=
7671 got_object_tree_get_nentries(s->tree) - 1 &&
7672 first + s->selected < g &&
7673 s->selected < s->ndisplayed - 1) {
7674 first = got_tree_entry_get_index(*fte);
7676 s->selected = g - first;
7679 tree_scroll_up(s, i - g);
7682 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7683 s->selected = g - 1;
7688 static const struct got_error *
7689 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7691 const struct got_error *err = NULL;
7692 struct tog_tree_view_state *s = &view->state.tree;
7693 struct got_tree_entry *te;
7694 int n, nscroll = view->nlines - 3;
7697 return tree_goto_line(view, nscroll);
7706 horizontal_scroll_input(view, ch);
7709 s->show_ids = !s->show_ids;
7714 if (!s->selected_entry)
7716 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7720 err = view_request_new(new_view, view, TOG_VIEW_REF);
7727 if (s->tree == s->root)
7728 s->first_displayed_entry =
7729 got_object_tree_get_first_entry(s->tree);
7731 s->first_displayed_entry = NULL;
7736 int eos = view->nlines - 3;
7738 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7742 te = got_object_tree_get_last_entry(s->tree);
7743 for (n = 0; n < eos; n++) {
7745 if (s->tree != s->root) {
7746 s->first_displayed_entry = NULL;
7751 s->first_displayed_entry = te;
7752 te = got_tree_entry_get_prev(s->tree, te);
7755 s->selected = n - 1;
7761 if (s->selected > 0) {
7765 tree_scroll_up(s, 1);
7766 if (s->selected_entry == NULL ||
7767 (s->tree == s->root && s->selected_entry ==
7768 got_object_tree_get_first_entry(s->tree)))
7778 if (s->tree == s->root) {
7779 if (got_object_tree_get_first_entry(s->tree) ==
7780 s->first_displayed_entry)
7781 s->selected -= MIN(s->selected, nscroll);
7783 if (s->first_displayed_entry == NULL)
7784 s->selected -= MIN(s->selected, nscroll);
7786 tree_scroll_up(s, MAX(0, nscroll));
7787 if (s->selected_entry == NULL ||
7788 (s->tree == s->root && s->selected_entry ==
7789 got_object_tree_get_first_entry(s->tree)))
7795 if (s->selected < s->ndisplayed - 1) {
7799 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7801 /* can't scroll any further */
7805 tree_scroll_down(view, 1);
7815 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7817 /* can't scroll any further; move cursor down */
7818 if (s->selected < s->ndisplayed - 1)
7819 s->selected += MIN(nscroll,
7820 s->ndisplayed - s->selected - 1);
7825 tree_scroll_down(view, nscroll);
7830 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7831 struct tog_parent_tree *parent;
7832 /* user selected '..' */
7833 if (s->tree == s->root) {
7837 parent = TAILQ_FIRST(&s->parents);
7838 TAILQ_REMOVE(&s->parents, parent,
7840 got_object_tree_close(s->tree);
7841 s->tree = parent->tree;
7842 s->first_displayed_entry =
7843 parent->first_displayed_entry;
7845 parent->selected_entry;
7846 s->selected = parent->selected;
7847 if (s->selected > view->nlines - 3) {
7848 err = offset_selection_down(view);
7853 } else if (S_ISDIR(got_tree_entry_get_mode(
7854 s->selected_entry))) {
7855 struct got_tree_object *subtree;
7857 err = got_object_open_as_tree(&subtree, s->repo,
7858 got_tree_entry_get_id(s->selected_entry));
7861 err = tree_view_visit_subtree(s, subtree);
7863 got_object_tree_close(subtree);
7866 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7867 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7870 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7871 s->selected = view->nlines - 4;
7887 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7892 static const struct got_error *
7893 cmd_tree(int argc, char *argv[])
7895 const struct got_error *io_err, *error;
7896 struct got_repository *repo = NULL;
7897 struct got_worktree *worktree = NULL;
7898 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7899 struct got_object_id *commit_id = NULL;
7900 struct got_commit_object *commit = NULL;
7901 const char *commit_id_arg = NULL;
7903 struct got_reference *ref = NULL;
7904 const char *head_ref_name = NULL;
7906 struct tog_view *view;
7907 struct tog_io *tog_io = NULL;
7908 int *pack_fds = NULL;
7910 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7913 commit_id_arg = optarg;
7916 repo_path = realpath(optarg, NULL);
7917 if (repo_path == NULL)
7918 return got_error_from_errno2("realpath",
7933 error = got_repo_pack_fds_open(&pack_fds);
7937 if (repo_path == NULL) {
7938 cwd = getcwd(NULL, 0);
7940 return got_error_from_errno("getcwd");
7941 error = got_worktree_open(&worktree, cwd);
7942 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7946 strdup(got_worktree_get_repo_path(worktree));
7948 repo_path = strdup(cwd);
7949 if (repo_path == NULL) {
7950 error = got_error_from_errno("strdup");
7955 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7959 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7964 error = init_curses(&tog_io);
7968 error = apply_unveil(got_repo_get_path(repo), NULL);
7972 error = tog_load_refs(repo, 0);
7976 if (commit_id_arg == NULL) {
7977 error = got_repo_match_object_id(&commit_id, &label,
7978 worktree ? got_worktree_get_head_ref_name(worktree) :
7979 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7982 head_ref_name = label;
7984 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7986 head_ref_name = got_ref_get_name(ref);
7987 else if (error->code != GOT_ERR_NOT_REF)
7989 error = got_repo_match_object_id(&commit_id, NULL,
7990 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7995 error = got_object_open_as_commit(&commit, repo, commit_id);
7999 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8001 error = got_error_from_errno("view_open");
8004 error = open_tree_view(view, commit_id, head_ref_name, repo);
8007 if (!got_path_is_root_dir(in_repo_path)) {
8008 error = tree_view_walk_path(&view->state.tree, commit,
8015 /* Release work tree lock. */
8016 got_worktree_close(worktree);
8019 error = view_loop(view, tog_io);
8028 const struct got_error *close_err = got_repo_close(repo);
8033 const struct got_error *pack_err =
8034 got_repo_pack_fds_close(pack_fds);
8038 if (tog_io != NULL) {
8039 io_err = tog_io_close(tog_io);
8047 static const struct got_error *
8048 ref_view_load_refs(struct tog_ref_view_state *s)
8050 struct got_reflist_entry *sre;
8051 struct tog_reflist_entry *re;
8054 TAILQ_FOREACH(sre, &tog_refs, entry) {
8055 if (strncmp(got_ref_get_name(sre->ref),
8056 "refs/got/", 9) == 0 &&
8057 strncmp(got_ref_get_name(sre->ref),
8058 "refs/got/backup/", 16) != 0)
8061 re = malloc(sizeof(*re));
8063 return got_error_from_errno("malloc");
8065 re->ref = got_ref_dup(sre->ref);
8066 if (re->ref == NULL)
8067 return got_error_from_errno("got_ref_dup");
8068 re->idx = s->nrefs++;
8069 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8072 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8077 ref_view_free_refs(struct tog_ref_view_state *s)
8079 struct tog_reflist_entry *re;
8081 while (!TAILQ_EMPTY(&s->refs)) {
8082 re = TAILQ_FIRST(&s->refs);
8083 TAILQ_REMOVE(&s->refs, re, entry);
8084 got_ref_close(re->ref);
8089 static const struct got_error *
8090 open_ref_view(struct tog_view *view, struct got_repository *repo)
8092 const struct got_error *err = NULL;
8093 struct tog_ref_view_state *s = &view->state.ref;
8095 s->selected_entry = 0;
8098 TAILQ_INIT(&s->refs);
8099 STAILQ_INIT(&s->colors);
8101 err = ref_view_load_refs(s);
8105 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8106 err = add_color(&s->colors, "^refs/heads/",
8107 TOG_COLOR_REFS_HEADS,
8108 get_color_value("TOG_COLOR_REFS_HEADS"));
8112 err = add_color(&s->colors, "^refs/tags/",
8113 TOG_COLOR_REFS_TAGS,
8114 get_color_value("TOG_COLOR_REFS_TAGS"));
8118 err = add_color(&s->colors, "^refs/remotes/",
8119 TOG_COLOR_REFS_REMOTES,
8120 get_color_value("TOG_COLOR_REFS_REMOTES"));
8124 err = add_color(&s->colors, "^refs/got/backup/",
8125 TOG_COLOR_REFS_BACKUP,
8126 get_color_value("TOG_COLOR_REFS_BACKUP"));
8131 view->show = show_ref_view;
8132 view->input = input_ref_view;
8133 view->close = close_ref_view;
8134 view->search_start = search_start_ref_view;
8135 view->search_next = search_next_ref_view;
8138 free_colors(&s->colors);
8142 static const struct got_error *
8143 close_ref_view(struct tog_view *view)
8145 struct tog_ref_view_state *s = &view->state.ref;
8147 ref_view_free_refs(s);
8148 free_colors(&s->colors);
8153 static const struct got_error *
8154 resolve_reflist_entry(struct got_object_id **commit_id,
8155 struct tog_reflist_entry *re, struct got_repository *repo)
8157 const struct got_error *err = NULL;
8158 struct got_object_id *obj_id;
8159 struct got_tag_object *tag = NULL;
8164 err = got_ref_resolve(&obj_id, repo, re->ref);
8168 err = got_object_get_type(&obj_type, repo, obj_id);
8173 case GOT_OBJ_TYPE_COMMIT:
8174 *commit_id = obj_id;
8176 case GOT_OBJ_TYPE_TAG:
8177 err = got_object_open_as_tag(&tag, repo, obj_id);
8181 err = got_object_get_type(&obj_type, repo,
8182 got_object_tag_get_object_id(tag));
8185 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8186 err = got_error(GOT_ERR_OBJ_TYPE);
8189 *commit_id = got_object_id_dup(
8190 got_object_tag_get_object_id(tag));
8191 if (*commit_id == NULL) {
8192 err = got_error_from_errno("got_object_id_dup");
8197 err = got_error(GOT_ERR_OBJ_TYPE);
8203 got_object_tag_close(tag);
8211 static const struct got_error *
8212 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8213 struct tog_reflist_entry *re, struct got_repository *repo)
8215 struct tog_view *log_view;
8216 const struct got_error *err = NULL;
8217 struct got_object_id *commit_id = NULL;
8221 err = resolve_reflist_entry(&commit_id, re, repo);
8223 if (err->code != GOT_ERR_OBJ_TYPE)
8229 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8230 if (log_view == NULL) {
8231 err = got_error_from_errno("view_open");
8235 err = open_log_view(log_view, commit_id, repo,
8236 got_ref_get_name(re->ref), "", 0);
8239 view_close(log_view);
8241 *new_view = log_view;
8247 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8249 struct tog_reflist_entry *re;
8252 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8255 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8256 while (i++ < maxscroll) {
8259 s->first_displayed_entry = re;
8260 re = TAILQ_PREV(re, tog_reflist_head, entry);
8264 static const struct got_error *
8265 ref_scroll_down(struct tog_view *view, int maxscroll)
8267 struct tog_ref_view_state *s = &view->state.ref;
8268 struct tog_reflist_entry *next, *last;
8271 if (s->first_displayed_entry)
8272 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8274 next = TAILQ_FIRST(&s->refs);
8276 last = s->last_displayed_entry;
8277 while (next && n++ < maxscroll) {
8279 s->last_displayed_entry = last;
8280 last = TAILQ_NEXT(last, entry);
8282 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8283 s->first_displayed_entry = next;
8284 next = TAILQ_NEXT(next, entry);
8291 static const struct got_error *
8292 search_start_ref_view(struct tog_view *view)
8294 struct tog_ref_view_state *s = &view->state.ref;
8296 s->matched_entry = NULL;
8301 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8303 regmatch_t regmatch;
8305 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8309 static const struct got_error *
8310 search_next_ref_view(struct tog_view *view)
8312 struct tog_ref_view_state *s = &view->state.ref;
8313 struct tog_reflist_entry *re = NULL;
8315 if (!view->searching) {
8316 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8320 if (s->matched_entry) {
8321 if (view->searching == TOG_SEARCH_FORWARD) {
8322 if (s->selected_entry)
8323 re = TAILQ_NEXT(s->selected_entry, entry);
8325 re = TAILQ_PREV(s->selected_entry,
8326 tog_reflist_head, entry);
8328 if (s->selected_entry == NULL)
8329 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8331 re = TAILQ_PREV(s->selected_entry,
8332 tog_reflist_head, entry);
8335 if (s->selected_entry)
8336 re = s->selected_entry;
8337 else if (view->searching == TOG_SEARCH_FORWARD)
8338 re = TAILQ_FIRST(&s->refs);
8340 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8345 if (s->matched_entry == NULL) {
8346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8349 if (view->searching == TOG_SEARCH_FORWARD)
8350 re = TAILQ_FIRST(&s->refs);
8352 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8355 if (match_reflist_entry(re, &view->regex)) {
8356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8357 s->matched_entry = re;
8361 if (view->searching == TOG_SEARCH_FORWARD)
8362 re = TAILQ_NEXT(re, entry);
8364 re = TAILQ_PREV(re, tog_reflist_head, entry);
8367 if (s->matched_entry) {
8368 s->first_displayed_entry = s->matched_entry;
8375 static const struct got_error *
8376 show_ref_view(struct tog_view *view)
8378 const struct got_error *err = NULL;
8379 struct tog_ref_view_state *s = &view->state.ref;
8380 struct tog_reflist_entry *re;
8383 struct tog_color *tc;
8384 int width, n, scrollx;
8385 int limit = view->nlines;
8387 werase(view->window);
8390 if (view_is_hsplit_top(view))
8391 --limit; /* border */
8396 re = s->first_displayed_entry;
8398 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8400 return got_error_from_errno("asprintf");
8402 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8407 if (view_needs_focus_indication(view))
8408 wstandout(view->window);
8409 waddwstr(view->window, wline);
8410 while (width++ < view->ncols)
8411 waddch(view->window, ' ');
8412 if (view_needs_focus_indication(view))
8413 wstandend(view->window);
8423 while (re && limit > 0) {
8425 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8428 struct got_commit_object *ci;
8429 struct got_tag_object *tag;
8430 struct got_object_id *id;
8434 err = got_ref_resolve(&id, s->repo, re->ref);
8437 err = got_object_open_as_tag(&tag, s->repo, id);
8439 if (err->code != GOT_ERR_OBJ_TYPE) {
8443 err = got_object_open_as_commit(&ci, s->repo,
8449 t = got_object_commit_get_committer_time(ci);
8450 got_object_commit_close(ci);
8452 t = got_object_tag_get_tagger_time(tag);
8453 got_object_tag_close(tag);
8456 if (gmtime_r(&t, &tm) == NULL)
8457 return got_error_from_errno("gmtime_r");
8458 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8459 return got_error(GOT_ERR_NO_SPACE);
8461 if (got_ref_is_symbolic(re->ref)) {
8462 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8463 ymd : "", got_ref_get_name(re->ref),
8464 got_ref_get_symref_target(re->ref)) == -1)
8465 return got_error_from_errno("asprintf");
8466 } else if (s->show_ids) {
8467 struct got_object_id *id;
8469 err = got_ref_resolve(&id, s->repo, re->ref);
8472 err = got_object_id_str(&id_str, id);
8477 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8478 got_ref_get_name(re->ref), id_str) == -1) {
8479 err = got_error_from_errno("asprintf");
8486 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8487 got_ref_get_name(re->ref)) == -1)
8488 return got_error_from_errno("asprintf");
8490 /* use full line width to determine view->maxx */
8491 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8496 view->maxx = MAX(view->maxx, width);
8500 err = format_line(&wline, &width, &scrollx, line, view->x,
8506 if (n == s->selected) {
8508 wstandout(view->window);
8509 s->selected_entry = re;
8511 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8513 wattr_on(view->window,
8514 COLOR_PAIR(tc->colorpair), NULL);
8515 waddwstr(view->window, &wline[scrollx]);
8517 wattr_off(view->window,
8518 COLOR_PAIR(tc->colorpair), NULL);
8519 if (width < view->ncols)
8520 waddch(view->window, '\n');
8521 if (n == s->selected && view->focussed)
8522 wstandend(view->window);
8528 s->last_displayed_entry = re;
8531 re = TAILQ_NEXT(re, entry);
8538 static const struct got_error *
8539 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8540 struct tog_reflist_entry *re, struct got_repository *repo)
8542 const struct got_error *err = NULL;
8543 struct got_object_id *commit_id = NULL;
8544 struct tog_view *tree_view;
8548 err = resolve_reflist_entry(&commit_id, re, repo);
8550 if (err->code != GOT_ERR_OBJ_TYPE)
8557 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8558 if (tree_view == NULL) {
8559 err = got_error_from_errno("view_open");
8563 err = open_tree_view(tree_view, commit_id,
8564 got_ref_get_name(re->ref), repo);
8568 *new_view = tree_view;
8574 static const struct got_error *
8575 ref_goto_line(struct tog_view *view, int nlines)
8577 const struct got_error *err = NULL;
8578 struct tog_ref_view_state *s = &view->state.ref;
8579 int g, idx = s->selected_entry->idx;
8586 else if (g > s->nrefs)
8589 if (g >= s->first_displayed_entry->idx + 1 &&
8590 g <= s->last_displayed_entry->idx + 1 &&
8591 g - s->first_displayed_entry->idx - 1 < nlines) {
8592 s->selected = g - s->first_displayed_entry->idx - 1;
8597 err = ref_scroll_down(view, g - idx - 1);
8600 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8601 s->first_displayed_entry->idx + s->selected < g &&
8602 s->selected < s->ndisplayed - 1)
8603 s->selected = g - s->first_displayed_entry->idx - 1;
8604 } else if (idx + 1 > g)
8605 ref_scroll_up(s, idx - g + 1);
8607 if (g < nlines && s->first_displayed_entry->idx == 0)
8608 s->selected = g - 1;
8614 static const struct got_error *
8615 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8617 const struct got_error *err = NULL;
8618 struct tog_ref_view_state *s = &view->state.ref;
8619 struct tog_reflist_entry *re;
8620 int n, nscroll = view->nlines - 1;
8623 return ref_goto_line(view, nscroll);
8632 horizontal_scroll_input(view, ch);
8635 s->show_ids = !s->show_ids;
8639 s->show_date = !s->show_date;
8643 s->sort_by_date = !s->sort_by_date;
8644 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8646 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8647 got_ref_cmp_by_commit_timestamp_descending :
8648 tog_ref_cmp_by_name, s->repo);
8651 got_reflist_object_id_map_free(tog_refs_idmap);
8652 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8653 &tog_refs, s->repo);
8656 ref_view_free_refs(s);
8657 err = ref_view_load_refs(s);
8662 if (!s->selected_entry)
8664 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8668 if (!s->selected_entry)
8670 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8677 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8682 int eos = view->nlines - 1;
8684 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8688 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8689 for (n = 0; n < eos; n++) {
8692 s->first_displayed_entry = re;
8693 re = TAILQ_PREV(re, tog_reflist_head, entry);
8696 s->selected = n - 1;
8702 if (s->selected > 0) {
8706 ref_scroll_up(s, 1);
8707 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8717 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8718 s->selected -= MIN(nscroll, s->selected);
8719 ref_scroll_up(s, MAX(0, nscroll));
8720 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8726 if (s->selected < s->ndisplayed - 1) {
8730 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8731 /* can't scroll any further */
8735 ref_scroll_down(view, 1);
8745 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8746 /* can't scroll any further; move cursor down */
8747 if (s->selected < s->ndisplayed - 1)
8748 s->selected += MIN(nscroll,
8749 s->ndisplayed - s->selected - 1);
8750 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8751 s->selected += s->ndisplayed - s->selected - 1;
8755 ref_scroll_down(view, nscroll);
8760 err = tog_load_refs(s->repo, s->sort_by_date);
8763 ref_view_free_refs(s);
8764 err = ref_view_load_refs(s);
8767 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8768 s->selected = view->nlines - 2;
8782 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8787 static const struct got_error *
8788 cmd_ref(int argc, char *argv[])
8790 const struct got_error *io_err, *error;
8791 struct got_repository *repo = NULL;
8792 struct got_worktree *worktree = NULL;
8793 char *cwd = NULL, *repo_path = NULL;
8795 struct tog_view *view;
8796 struct tog_io *tog_io = NULL;
8797 int *pack_fds = NULL;
8799 while ((ch = getopt(argc, argv, "r:")) != -1) {
8802 repo_path = realpath(optarg, NULL);
8803 if (repo_path == NULL)
8804 return got_error_from_errno2("realpath",
8819 error = got_repo_pack_fds_open(&pack_fds);
8823 if (repo_path == NULL) {
8824 cwd = getcwd(NULL, 0);
8826 return got_error_from_errno("getcwd");
8827 error = got_worktree_open(&worktree, cwd);
8828 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8832 strdup(got_worktree_get_repo_path(worktree));
8834 repo_path = strdup(cwd);
8835 if (repo_path == NULL) {
8836 error = got_error_from_errno("strdup");
8841 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8845 error = init_curses(&tog_io);
8849 error = apply_unveil(got_repo_get_path(repo), NULL);
8853 error = tog_load_refs(repo, 0);
8857 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8859 error = got_error_from_errno("view_open");
8863 error = open_ref_view(view, repo);
8868 /* Release work tree lock. */
8869 got_worktree_close(worktree);
8872 error = view_loop(view, tog_io);
8877 const struct got_error *close_err = got_repo_close(repo);
8882 const struct got_error *pack_err =
8883 got_repo_pack_fds_close(pack_fds);
8887 if (tog_io != NULL) {
8888 io_err = tog_io_close(tog_io);
8896 static const struct got_error*
8897 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8906 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8910 if (mvwprintw(win, y, x, "%s", str) == ERR)
8911 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8918 static const struct got_error *
8919 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8923 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8925 free(*line_offsets);
8926 *line_offsets = NULL;
8927 return got_error_from_errno("reallocarray");
8931 (*line_offsets)[*nlines] = off;
8936 static const struct got_error *
8937 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8941 for (;n > 0; --n, ++km) {
8945 if (km->keys == NULL)
8948 t = t0 = strdup(km->keys);
8950 return got_error_from_errno("strdup");
8953 while ((k = strsep(&t, " ")) != NULL)
8954 len += strlen(k) > 1 ? 2 : 0;
8956 *ret = MAX(*ret, len);
8963 * Write keymap section headers, keys, and key info in km to f.
8964 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8965 * wrap control and symbolic keys in guillemets, else use <>.
8967 static const struct got_error *
8968 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8973 static const char *u8_glyph[] = {
8974 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8975 "\xe2\x80\xba" /* U+203A (utf8 >) */
8978 int cs, s, first = 1;
8980 cs = got_locale_is_utf8();
8982 t = t0 = strdup(km->keys);
8984 return got_error_from_errno("strdup");
8986 len = strlen(km->keys);
8987 while ((k = strsep(&t, " ")) != NULL) {
8988 s = strlen(k) > 1; /* control or symbolic key */
8989 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8990 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8991 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8994 return got_error_from_errno("fprintf");
9002 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9004 return got_error_from_errno("fprintf");
9010 static const struct got_error *
9011 format_help(struct tog_help_view_state *s)
9013 const struct got_error *err = NULL;
9015 int i, max, n, show = s->all;
9016 static const struct tog_key_map km[] = {
9017 #define KEYMAP_(info, type) { NULL, (info), type }
9018 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9024 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9029 err = max_key_str(&max, km, n);
9033 for (i = 0; i < n; ++i) {
9034 if (km[i].keys == NULL) {
9036 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9037 km[i].type == s->type || s->all)
9041 err = format_help_line(&off, s->f, &km[i], max);
9044 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9051 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9055 static const struct got_error *
9056 create_help(struct tog_help_view_state *s)
9059 const struct got_error *err;
9061 free(s->line_offsets);
9062 s->line_offsets = NULL;
9067 return got_error_from_errno("got_opentemp");
9070 err = format_help(s);
9074 if (s->f && fflush(s->f) != 0)
9075 return got_error_from_errno("fflush");
9080 static const struct got_error *
9081 search_start_help_view(struct tog_view *view)
9083 view->state.help.matched_line = 0;
9088 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9089 size_t *nlines, int **first, int **last, int **match, int **selected)
9091 struct tog_help_view_state *s = &view->state.help;
9094 *nlines = s->nlines;
9095 *line_offsets = s->line_offsets;
9096 *match = &s->matched_line;
9097 *first = &s->first_displayed_line;
9098 *last = &s->last_displayed_line;
9099 *selected = &s->selected_line;
9102 static const struct got_error *
9103 show_help_view(struct tog_view *view)
9105 struct tog_help_view_state *s = &view->state.help;
9106 const struct got_error *err;
9107 regmatch_t *regmatch = &view->regmatch;
9112 int width, nprinted = 0, rc = 0;
9113 int eos = view->nlines;
9115 if (view_is_hsplit_top(view))
9116 --eos; /* account for border */
9120 werase(view->window);
9122 if (view->gline > s->nlines - 1)
9123 view->gline = s->nlines - 1;
9125 err = win_draw_center(view->window, 0, 0, view->ncols,
9126 view_needs_focus_indication(view),
9127 "tog help (press q to return to tog)");
9132 waddstr(view->window, "\n\n");
9138 while (eos > 0 && nprinted < eos) {
9141 linelen = getline(&line, &linesz, s->f);
9142 if (linelen == -1) {
9145 return got_ferror(s->f, GOT_ERR_IO);
9150 if (++s->lineno < s->first_displayed_line)
9152 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9154 if (s->lineno == view->hiline)
9157 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9163 view->maxx = MAX(view->maxx, width);
9168 wattron(view->window, attr);
9169 if (s->first_displayed_line + nprinted == s->matched_line &&
9170 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9171 err = add_matched_line(&width, line, view->ncols - 1, 0,
9172 view->window, view->x, regmatch);
9180 err = format_line(&wline, &width, &skip, line,
9181 view->x, view->ncols, 0, view->x ? 1 : 0);
9186 waddwstr(view->window, &wline[skip]);
9190 if (s->lineno == view->hiline) {
9191 while (width++ < view->ncols)
9192 waddch(view->window, ' ');
9194 if (width < view->ncols)
9195 waddch(view->window, '\n');
9198 wattroff(view->window, attr);
9199 if (++nprinted == 1)
9200 s->first_displayed_line = s->lineno;
9204 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9206 s->last_displayed_line = s->first_displayed_line;
9211 rc = waddnstr(view->window,
9212 "See the tog(1) manual page for full documentation",
9215 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9217 wmove(view->window, view->nlines - 1, 0);
9218 wclrtoeol(view->window);
9219 wstandout(view->window);
9220 rc = waddnstr(view->window, "scroll down for more...",
9223 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9224 if (getcurx(view->window) < view->ncols - 6) {
9225 rc = wprintw(view->window, "[%.0f%%]",
9226 100.00 * s->last_displayed_line / s->nlines);
9228 return got_error_msg(GOT_ERR_IO, "wprintw");
9230 wstandend(view->window);
9236 static const struct got_error *
9237 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9239 struct tog_help_view_state *s = &view->state.help;
9240 const struct got_error *err = NULL;
9246 eos = nscroll = view->nlines;
9247 if (view_is_hsplit_top(view))
9250 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9259 horizontal_scroll_input(view, ch);
9263 s->first_displayed_line = 1;
9271 s->first_displayed_line = (s->nlines - eos) + 3;
9276 if (s->first_displayed_line > 1)
9277 --s->first_displayed_line;
9288 if (s->first_displayed_line == 1) {
9292 while (--nscroll > 0 && s->first_displayed_line > 1)
9293 s->first_displayed_line--;
9299 ++s->first_displayed_line;
9315 while (!s->eof && --nscroll > 0) {
9316 linelen = getline(&line, &linesz, s->f);
9317 s->first_displayed_line++;
9318 if (linelen == -1) {
9322 err = got_ferror(s->f, GOT_ERR_IO);
9336 static const struct got_error *
9337 close_help_view(struct tog_view *view)
9339 struct tog_help_view_state *s = &view->state.help;
9341 free(s->line_offsets);
9342 s->line_offsets = NULL;
9343 if (fclose(s->f) == EOF)
9344 return got_error_from_errno("fclose");
9349 static const struct got_error *
9350 reset_help_view(struct tog_view *view)
9352 struct tog_help_view_state *s = &view->state.help;
9355 if (s->f && fclose(s->f) == EOF)
9356 return got_error_from_errno("fclose");
9358 wclear(view->window);
9362 s->first_displayed_line = 1;
9363 s->last_displayed_line = view->nlines;
9364 s->matched_line = 0;
9366 return create_help(s);
9369 static const struct got_error *
9370 open_help_view(struct tog_view *view, struct tog_view *parent)
9372 const struct got_error *err = NULL;
9373 struct tog_help_view_state *s = &view->state.help;
9375 s->type = (enum tog_keymap_type)parent->type;
9376 s->first_displayed_line = 1;
9377 s->last_displayed_line = view->nlines;
9378 s->selected_line = 1;
9380 view->show = show_help_view;
9381 view->input = input_help_view;
9382 view->reset = reset_help_view;
9383 view->close = close_help_view;
9384 view->search_start = search_start_help_view;
9385 view->search_setup = search_setup_help_view;
9386 view->search_next = search_next_view_match;
9388 err = create_help(s);
9392 static const struct got_error *
9393 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9394 enum tog_view_type request, int y, int x)
9396 const struct got_error *err = NULL;
9402 if (view->type == TOG_VIEW_LOG) {
9403 struct tog_log_view_state *s = &view->state.log;
9405 err = open_diff_view_for_commit(new_view, y, x,
9406 s->selected_entry->commit, s->selected_entry->id,
9409 return got_error_msg(GOT_ERR_NOT_IMPL,
9410 "parent/child view pair not supported");
9412 case TOG_VIEW_BLAME:
9413 if (view->type == TOG_VIEW_TREE) {
9414 struct tog_tree_view_state *s = &view->state.tree;
9416 err = blame_tree_entry(new_view, y, x,
9417 s->selected_entry, &s->parents, s->commit_id,
9420 return got_error_msg(GOT_ERR_NOT_IMPL,
9421 "parent/child view pair not supported");
9424 if (view->type == TOG_VIEW_BLAME)
9425 err = log_annotated_line(new_view, y, x,
9426 view->state.blame.repo, view->state.blame.id_to_log);
9427 else if (view->type == TOG_VIEW_TREE)
9428 err = log_selected_tree_entry(new_view, y, x,
9430 else if (view->type == TOG_VIEW_REF)
9431 err = log_ref_entry(new_view, y, x,
9432 view->state.ref.selected_entry,
9433 view->state.ref.repo);
9435 return got_error_msg(GOT_ERR_NOT_IMPL,
9436 "parent/child view pair not supported");
9439 if (view->type == TOG_VIEW_LOG)
9440 err = browse_commit_tree(new_view, y, x,
9441 view->state.log.selected_entry,
9442 view->state.log.in_repo_path,
9443 view->state.log.head_ref_name,
9444 view->state.log.repo);
9445 else if (view->type == TOG_VIEW_REF)
9446 err = browse_ref_tree(new_view, y, x,
9447 view->state.ref.selected_entry,
9448 view->state.ref.repo);
9450 return got_error_msg(GOT_ERR_NOT_IMPL,
9451 "parent/child view pair not supported");
9454 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9455 if (*new_view == NULL)
9456 return got_error_from_errno("view_open");
9457 if (view->type == TOG_VIEW_LOG)
9458 err = open_ref_view(*new_view, view->state.log.repo);
9459 else if (view->type == TOG_VIEW_TREE)
9460 err = open_ref_view(*new_view, view->state.tree.repo);
9462 err = got_error_msg(GOT_ERR_NOT_IMPL,
9463 "parent/child view pair not supported");
9465 view_close(*new_view);
9468 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9469 if (*new_view == NULL)
9470 return got_error_from_errno("view_open");
9471 err = open_help_view(*new_view, view);
9473 view_close(*new_view);
9476 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9483 * If view was scrolled down to move the selected line into view when opening a
9484 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9487 offset_selection_up(struct tog_view *view)
9489 switch (view->type) {
9490 case TOG_VIEW_BLAME: {
9491 struct tog_blame_view_state *s = &view->state.blame;
9492 if (s->first_displayed_line == 1) {
9493 s->selected_line = MAX(s->selected_line - view->offset,
9497 if (s->first_displayed_line > view->offset)
9498 s->first_displayed_line -= view->offset;
9500 s->first_displayed_line = 1;
9501 s->selected_line += view->offset;
9505 log_scroll_up(&view->state.log, view->offset);
9506 view->state.log.selected += view->offset;
9509 ref_scroll_up(&view->state.ref, view->offset);
9510 view->state.ref.selected += view->offset;
9513 tree_scroll_up(&view->state.tree, view->offset);
9514 view->state.tree.selected += view->offset;
9524 * If the selected line is in the section of screen covered by the bottom split,
9525 * scroll down offset lines to move it into view and index its new position.
9527 static const struct got_error *
9528 offset_selection_down(struct tog_view *view)
9530 const struct got_error *err = NULL;
9531 const struct got_error *(*scrolld)(struct tog_view *, int);
9532 int *selected = NULL;
9535 switch (view->type) {
9536 case TOG_VIEW_BLAME: {
9537 struct tog_blame_view_state *s = &view->state.blame;
9540 if (s->selected_line > view->nlines - header) {
9541 offset = abs(view->nlines - s->selected_line - header);
9542 s->first_displayed_line += offset;
9543 s->selected_line -= offset;
9544 view->offset = offset;
9548 case TOG_VIEW_LOG: {
9549 struct tog_log_view_state *s = &view->state.log;
9550 scrolld = &log_scroll_down;
9551 header = view_is_parent_view(view) ? 3 : 2;
9552 selected = &s->selected;
9555 case TOG_VIEW_REF: {
9556 struct tog_ref_view_state *s = &view->state.ref;
9557 scrolld = &ref_scroll_down;
9559 selected = &s->selected;
9562 case TOG_VIEW_TREE: {
9563 struct tog_tree_view_state *s = &view->state.tree;
9564 scrolld = &tree_scroll_down;
9566 selected = &s->selected;
9576 if (selected && *selected > view->nlines - header) {
9577 offset = abs(view->nlines - *selected - header);
9578 view->offset = offset;
9579 if (scrolld && offset) {
9580 err = scrolld(view, offset);
9581 *selected -= offset;
9589 list_commands(FILE *fp)
9593 fprintf(fp, "commands:");
9594 for (i = 0; i < nitems(tog_commands); i++) {
9595 const struct tog_cmd *cmd = &tog_commands[i];
9596 fprintf(fp, " %s", cmd->name);
9602 usage(int hflag, int status)
9604 FILE *fp = (status == 0) ? stdout : stderr;
9606 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9609 fprintf(fp, "lazy usage: %s path\n", getprogname());
9616 make_argv(int argc, ...)
9624 argv = calloc(argc, sizeof(char *));
9627 for (i = 0; i < argc; i++) {
9628 argv[i] = strdup(va_arg(ap, char *));
9629 if (argv[i] == NULL)
9638 * Try to convert 'tog path' into a 'tog log path' command.
9639 * The user could simply have mistyped the command rather than knowingly
9640 * provided a path. So check whether argv[0] can in fact be resolved
9641 * to a path in the HEAD commit and print a special error if not.
9642 * This hack is for mpi@ <3
9644 static const struct got_error *
9645 tog_log_with_path(int argc, char *argv[])
9647 const struct got_error *error = NULL, *close_err;
9648 const struct tog_cmd *cmd = NULL;
9649 struct got_repository *repo = NULL;
9650 struct got_worktree *worktree = NULL;
9651 struct got_object_id *commit_id = NULL, *id = NULL;
9652 struct got_commit_object *commit = NULL;
9653 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9654 char *commit_id_str = NULL, **cmd_argv = NULL;
9655 int *pack_fds = NULL;
9657 cwd = getcwd(NULL, 0);
9659 return got_error_from_errno("getcwd");
9661 error = got_repo_pack_fds_open(&pack_fds);
9665 error = got_worktree_open(&worktree, cwd);
9666 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9670 repo_path = strdup(got_worktree_get_repo_path(worktree));
9672 repo_path = strdup(cwd);
9673 if (repo_path == NULL) {
9674 error = got_error_from_errno("strdup");
9678 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9682 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9687 error = tog_load_refs(repo, 0);
9690 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9691 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9692 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9697 got_worktree_close(worktree);
9701 error = got_object_open_as_commit(&commit, repo, commit_id);
9705 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9707 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9709 fprintf(stderr, "%s: '%s' is no known command or path\n",
9710 getprogname(), argv[0]);
9715 error = got_object_id_str(&commit_id_str, commit_id);
9719 cmd = &tog_commands[0]; /* log */
9721 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9722 error = cmd->cmd_main(argc, cmd_argv);
9725 close_err = got_repo_close(repo);
9730 got_object_commit_close(commit);
9732 got_worktree_close(worktree);
9734 const struct got_error *pack_err =
9735 got_repo_pack_fds_close(pack_fds);
9740 free(commit_id_str);
9747 for (i = 0; i < argc; i++)
9756 main(int argc, char *argv[])
9758 const struct got_error *error = NULL;
9759 const struct tog_cmd *cmd = NULL;
9760 int ch, hflag = 0, Vflag = 0;
9761 char **cmd_argv = NULL;
9762 static const struct option longopts[] = {
9763 { "version", no_argument, NULL, 'V' },
9766 char *diff_algo_str = NULL;
9768 setlocale(LC_CTYPE, "");
9770 #if !defined(PROFILE) && !defined(TOG_REGRESS)
9771 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9776 if (!isatty(STDIN_FILENO))
9777 errx(1, "standard input is not a tty");
9779 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9799 got_version_print_str();
9806 /* Build an argument vector which runs a default command. */
9807 cmd = &tog_commands[0];
9809 cmd_argv = make_argv(argc, cmd->name);
9813 /* Did the user specify a command? */
9814 for (i = 0; i < nitems(tog_commands); i++) {
9815 if (strncmp(tog_commands[i].name, argv[0],
9816 strlen(argv[0])) == 0) {
9817 cmd = &tog_commands[i];
9823 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9824 if (diff_algo_str) {
9825 if (strcasecmp(diff_algo_str, "patience") == 0)
9826 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9827 if (strcasecmp(diff_algo_str, "myers") == 0)
9828 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9834 /* No command specified; try log with a path */
9835 error = tog_log_with_path(argc, argv);
9840 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9846 for (i = 0; i < argc; i++)
9851 if (error && error->code != GOT_ERR_CANCELLED &&
9852 error->code != GOT_ERR_EOF &&
9853 error->code != GOT_ERR_PRIVSEP_EXIT &&
9854 error->code != GOT_ERR_PRIVSEP_PIPE &&
9855 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9856 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);