2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/ioctl.h>
25 #if defined(__FreeBSD__) || defined(__APPLE__)
26 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
61 #include "got_keyword.h"
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 #define CTRL(x) ((x) & 0x1f)
76 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
85 __dead static void usage(int, int);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_ref(void);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct tog_cmd tog_commands[] = {
99 { "log", cmd_log, usage_log },
100 { "diff", cmd_diff, usage_diff },
101 { "blame", cmd_blame, usage_blame },
102 { "tree", cmd_tree, usage_tree },
103 { "ref", cmd_ref, usage_ref },
115 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
116 enum tog_keymap_type {
117 TOG_KEYMAP_KEYS = -2,
133 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
135 #define TOG_EOF_STRING "(END)"
137 struct commit_queue_entry {
138 TAILQ_ENTRY(commit_queue_entry) entry;
139 struct got_object_id *id;
140 struct got_commit_object *commit;
143 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
144 struct commit_queue {
146 struct commit_queue_head head;
150 STAILQ_ENTRY(tog_color) entry;
154 STAILQ_HEAD(tog_colors, tog_color);
156 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
157 static struct got_reflist_object_id_map *tog_refs_idmap;
159 struct got_object_id *id;
163 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
165 static const struct got_error *
166 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
167 struct got_reference* re2)
169 const char *name1 = got_ref_get_name(re1);
170 const char *name2 = got_ref_get_name(re2);
171 int isbackup1, isbackup2;
173 /* Sort backup refs towards the bottom of the list. */
174 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
175 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
176 if (!isbackup1 && isbackup2) {
179 } else if (isbackup1 && !isbackup2) {
184 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
188 static const struct got_error *
189 tog_load_refs(struct got_repository *repo, int sort_by_date)
191 const struct got_error *err;
193 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
194 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
199 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
206 if (tog_refs_idmap) {
207 got_reflist_object_id_map_free(tog_refs_idmap);
208 tog_refs_idmap = NULL;
210 got_ref_list_free(&tog_refs);
213 static const struct got_error *
214 add_color(struct tog_colors *colors, const char *pattern,
215 int idx, short color)
217 const struct got_error *err = NULL;
218 struct tog_color *tc;
221 if (idx < 1 || idx > COLOR_PAIRS - 1)
224 init_pair(idx, color, -1);
226 tc = calloc(1, sizeof(*tc));
228 return got_error_from_errno("calloc");
229 regerr = regcomp(&tc->regex, pattern,
230 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
232 static char regerr_msg[512];
233 static char err_msg[512];
234 regerror(regerr, &tc->regex, regerr_msg,
236 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
238 err = got_error_msg(GOT_ERR_REGEX, err_msg);
243 STAILQ_INSERT_HEAD(colors, tc, entry);
248 free_colors(struct tog_colors *colors)
250 struct tog_color *tc;
252 while (!STAILQ_EMPTY(colors)) {
253 tc = STAILQ_FIRST(colors);
254 STAILQ_REMOVE_HEAD(colors, entry);
260 static struct tog_color *
261 get_color(struct tog_colors *colors, int colorpair)
263 struct tog_color *tc = NULL;
265 STAILQ_FOREACH(tc, colors, entry) {
266 if (tc->colorpair == colorpair)
274 default_color_value(const char *envvar)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
280 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
282 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
285 return COLOR_MAGENTA;
286 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
287 return COLOR_MAGENTA;
288 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
290 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
294 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
296 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
300 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
301 return COLOR_MAGENTA;
302 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
304 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
311 get_color_value(const char *envvar)
313 const char *val = getenv(envvar);
316 return default_color_value(envvar);
318 if (strcasecmp(val, "black") == 0)
320 if (strcasecmp(val, "red") == 0)
322 if (strcasecmp(val, "green") == 0)
324 if (strcasecmp(val, "yellow") == 0)
326 if (strcasecmp(val, "blue") == 0)
328 if (strcasecmp(val, "magenta") == 0)
329 return COLOR_MAGENTA;
330 if (strcasecmp(val, "cyan") == 0)
332 if (strcasecmp(val, "white") == 0)
334 if (strcasecmp(val, "default") == 0)
337 return default_color_value(envvar);
340 struct tog_diff_view_state {
341 struct got_object_id *id1, *id2;
342 const char *label1, *label2;
346 int first_displayed_line;
347 int last_displayed_line;
350 int ignore_whitespace;
352 struct got_repository *repo;
353 struct got_diff_line *lines;
358 /* passed from log or blame view; may be NULL */
359 struct tog_view *parent_view;
362 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
363 static volatile sig_atomic_t tog_thread_error;
365 struct tog_log_thread_args {
366 pthread_cond_t need_commits;
367 pthread_cond_t commit_loaded;
370 struct got_commit_graph *graph;
371 struct commit_queue *real_commits;
372 const char *in_repo_path;
373 struct got_object_id *start_id;
374 struct got_repository *repo;
377 pthread_cond_t log_loaded;
379 struct commit_queue_entry **first_displayed_entry;
380 struct commit_queue_entry **selected_entry;
382 int *search_next_done;
386 regex_t *limit_regex;
387 struct commit_queue *limit_commits;
388 struct got_worktree *worktree;
389 int need_commit_marker;
392 struct tog_log_view_state {
393 struct commit_queue *commits;
394 struct commit_queue_entry *first_displayed_entry;
395 struct commit_queue_entry *last_displayed_entry;
396 struct commit_queue_entry *selected_entry;
397 struct commit_queue real_commits;
402 struct got_repository *repo;
403 struct got_object_id *start_id;
406 struct tog_log_thread_args thread_args;
407 struct commit_queue_entry *matched_entry;
408 struct commit_queue_entry *search_entry;
409 struct tog_colors colors;
413 struct commit_queue limit_commits;
416 #define TOG_COLOR_DIFF_MINUS 1
417 #define TOG_COLOR_DIFF_PLUS 2
418 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
419 #define TOG_COLOR_DIFF_META 4
420 #define TOG_COLOR_TREE_SUBMODULE 5
421 #define TOG_COLOR_TREE_SYMLINK 6
422 #define TOG_COLOR_TREE_DIRECTORY 7
423 #define TOG_COLOR_TREE_EXECUTABLE 8
424 #define TOG_COLOR_COMMIT 9
425 #define TOG_COLOR_AUTHOR 10
426 #define TOG_COLOR_DATE 11
427 #define TOG_COLOR_REFS_HEADS 12
428 #define TOG_COLOR_REFS_TAGS 13
429 #define TOG_COLOR_REFS_REMOTES 14
430 #define TOG_COLOR_REFS_BACKUP 15
432 struct tog_blame_cb_args {
433 struct tog_blame_line *lines; /* one per line */
436 struct tog_view *view;
437 struct got_object_id *commit_id;
441 struct tog_blame_thread_args {
443 struct got_repository *repo;
444 struct tog_blame_cb_args *cb_args;
446 got_cancel_cb cancel_cb;
448 pthread_cond_t blame_complete;
454 struct tog_blame_line *lines;
458 struct tog_blame_thread_args thread_args;
459 struct tog_blame_cb_args cb_args;
464 struct tog_blame_view_state {
465 int first_displayed_line;
466 int last_displayed_line;
468 int last_diffed_line;
472 struct got_object_id_queue blamed_commits;
473 struct got_object_qid *blamed_commit;
475 struct got_repository *repo;
476 struct got_object_id *commit_id;
477 struct got_object_id *id_to_log;
478 struct tog_blame blame;
480 struct tog_colors colors;
483 struct tog_parent_tree {
484 TAILQ_ENTRY(tog_parent_tree) entry;
485 struct got_tree_object *tree;
486 struct got_tree_entry *first_displayed_entry;
487 struct got_tree_entry *selected_entry;
491 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
493 struct tog_tree_view_state {
495 struct got_object_id *commit_id;/* commit which this tree belongs to */
496 struct got_tree_object *root; /* the commit's root tree entry */
497 struct got_tree_object *tree; /* currently displayed (sub-)tree */
498 struct got_tree_entry *first_displayed_entry;
499 struct got_tree_entry *last_displayed_entry;
500 struct got_tree_entry *selected_entry;
501 int ndisplayed, selected, show_ids;
502 struct tog_parent_trees parents; /* parent trees of current sub-tree */
504 struct got_repository *repo;
505 struct got_tree_entry *matched_entry;
506 struct tog_colors colors;
509 struct tog_reflist_entry {
510 TAILQ_ENTRY(tog_reflist_entry) entry;
511 struct got_reference *ref;
515 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
517 struct tog_ref_view_state {
518 struct tog_reflist_head refs;
519 struct tog_reflist_entry *first_displayed_entry;
520 struct tog_reflist_entry *last_displayed_entry;
521 struct tog_reflist_entry *selected_entry;
522 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
523 struct got_repository *repo;
524 struct tog_reflist_entry *matched_entry;
525 struct tog_colors colors;
528 struct tog_help_view_state {
533 int first_displayed_line;
534 int last_displayed_line;
539 enum tog_keymap_type type;
542 #define GENERATE_HELP \
543 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
544 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
545 KEY_("k C-p Up", "Move cursor or page up one line"), \
546 KEY_("j C-n Down", "Move cursor or page down one line"), \
547 KEY_("C-b b PgUp", "Scroll the view up one page"), \
548 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
549 KEY_("C-u u", "Scroll the view up one half page"), \
550 KEY_("C-d d", "Scroll the view down one half page"), \
551 KEY_("g", "Go to line N (default: first line)"), \
552 KEY_("Home =", "Go to the first line"), \
553 KEY_("G", "Go to line N (default: last line)"), \
554 KEY_("End *", "Go to the last line"), \
555 KEY_("l Right", "Scroll the view right"), \
556 KEY_("h Left", "Scroll the view left"), \
557 KEY_("$", "Scroll view to the rightmost position"), \
558 KEY_("0", "Scroll view to the leftmost position"), \
559 KEY_("-", "Decrease size of the focussed split"), \
560 KEY_("+", "Increase size of the focussed split"), \
561 KEY_("Tab", "Switch focus between views"), \
562 KEY_("F", "Toggle fullscreen mode"), \
563 KEY_("S", "Switch split-screen layout"), \
564 KEY_("/", "Open prompt to enter search term"), \
565 KEY_("n", "Find next line/token matching the current search term"), \
566 KEY_("N", "Find previous line/token matching the current search term"),\
567 KEY_("q", "Quit the focussed view; Quit help screen"), \
568 KEY_("Q", "Quit tog"), \
570 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
571 KEY_("< ,", "Move cursor up one commit"), \
572 KEY_("> .", "Move cursor down one commit"), \
573 KEY_("Enter", "Open diff view of the selected commit"), \
574 KEY_("B", "Reload the log view and toggle display of merged commits"), \
575 KEY_("R", "Open ref view of all repository references"), \
576 KEY_("T", "Display tree view of the repository from the selected" \
578 KEY_("@", "Toggle between displaying author and committer name"), \
579 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
580 KEY_("C-g Backspace", "Cancel current search or log operation"), \
581 KEY_("C-l", "Reload the log view with new commits in the repository"), \
583 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
584 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
585 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
586 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
587 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
589 KEY_("(", "Go to the previous file in the diff"), \
590 KEY_(")", "Go to the next file in the diff"), \
591 KEY_("{", "Go to the previous hunk in the diff"), \
592 KEY_("}", "Go to the next hunk in the diff"), \
593 KEY_("[", "Decrease the number of context lines"), \
594 KEY_("]", "Increase the number of context lines"), \
595 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
597 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
598 KEY_("Enter", "Display diff view of the selected line's commit"), \
599 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
600 KEY_("L", "Open log view for the currently selected annotated line"), \
601 KEY_("C", "Reload view with the previously blamed commit"), \
602 KEY_("c", "Reload view with the version of the file found in the" \
603 " selected line's commit"), \
604 KEY_("p", "Reload view with the version of the file found in the" \
605 " selected line's parent commit"), \
607 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
608 KEY_("Enter", "Enter selected directory or open blame view of the" \
610 KEY_("L", "Open log view for the selected entry"), \
611 KEY_("R", "Open ref view of all repository references"), \
612 KEY_("i", "Show object IDs for all tree entries"), \
613 KEY_("Backspace", "Return to the parent directory"), \
615 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
616 KEY_("Enter", "Display log view of the selected reference"), \
617 KEY_("T", "Display tree view of the selected reference"), \
618 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
619 KEY_("m", "Toggle display of last modified date for each reference"), \
620 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
621 KEY_("C-l", "Reload view with all repository references")
626 enum tog_keymap_type type;
629 /* curses io for tog regress */
638 static int using_mock_io;
640 #define TOG_KEY_SCRDUMP SHRT_MIN
643 * We implement two types of views: parent views and child views.
645 * The 'Tab' key switches focus between a parent view and its child view.
646 * Child views are shown side-by-side to their parent view, provided
647 * there is enough screen estate.
649 * When a new view is opened from within a parent view, this new view
650 * becomes a child view of the parent view, replacing any existing child.
652 * When a new view is opened from within a child view, this new view
653 * becomes a parent view which will obscure the views below until the
654 * user quits the new parent view by typing 'q'.
656 * This list of views contains parent views only.
657 * Child views are only pointed to by their parent view.
659 TAILQ_HEAD(tog_view_list_head, tog_view);
662 TAILQ_ENTRY(tog_view) entry;
665 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
666 int resized_y, resized_x; /* begin_y/x based on user resizing */
667 int maxx, x; /* max column and current start column */
668 int lines, cols; /* copies of LINES and COLS */
669 int nscrolled, offset; /* lines scrolled and hsplit line offset */
670 int gline, hiline; /* navigate to and highlight this nG line */
671 int ch, count; /* current keymap and count prefix */
672 int resized; /* set when in a resize event */
673 int focussed; /* Only set on one parent or child view at a time. */
675 struct tog_view *parent;
676 struct tog_view *child;
679 * This flag is initially set on parent views when a new child view
680 * is created. It gets toggled when the 'Tab' key switches focus
681 * between parent and child.
682 * The flag indicates whether focus should be passed on to our child
683 * view if this parent view gets picked for focus after another parent
684 * view was closed. This prevents child views from losing focus in such
689 enum tog_view_mode mode;
690 /* type-specific state */
691 enum tog_view_type type;
693 struct tog_diff_view_state diff;
694 struct tog_log_view_state log;
695 struct tog_blame_view_state blame;
696 struct tog_tree_view_state tree;
697 struct tog_ref_view_state ref;
698 struct tog_help_view_state help;
701 const struct got_error *(*show)(struct tog_view *);
702 const struct got_error *(*input)(struct tog_view **,
703 struct tog_view *, int);
704 const struct got_error *(*reset)(struct tog_view *);
705 const struct got_error *(*resize)(struct tog_view *, int);
706 const struct got_error *(*close)(struct tog_view *);
708 const struct got_error *(*search_start)(struct tog_view *);
709 const struct got_error *(*search_next)(struct tog_view *);
710 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
711 int **, int **, int **, int **);
714 #define TOG_SEARCH_FORWARD 1
715 #define TOG_SEARCH_BACKWARD 2
716 int search_next_done;
717 #define TOG_SEARCH_HAVE_MORE 1
718 #define TOG_SEARCH_NO_MORE 2
719 #define TOG_SEARCH_HAVE_NONE 3
725 static const struct got_error *open_diff_view(struct tog_view *,
726 struct got_object_id *, struct got_object_id *,
727 const char *, const char *, int, int, int, struct tog_view *,
728 struct got_repository *);
729 static const struct got_error *show_diff_view(struct tog_view *);
730 static const struct got_error *input_diff_view(struct tog_view **,
731 struct tog_view *, int);
732 static const struct got_error *reset_diff_view(struct tog_view *);
733 static const struct got_error* close_diff_view(struct tog_view *);
734 static const struct got_error *search_start_diff_view(struct tog_view *);
735 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
736 size_t *, int **, int **, int **, int **);
737 static const struct got_error *search_next_view_match(struct tog_view *);
739 static const struct got_error *open_log_view(struct tog_view *,
740 struct got_object_id *, struct got_repository *,
741 const char *, const char *, int, struct got_worktree *);
742 static const struct got_error * show_log_view(struct tog_view *);
743 static const struct got_error *input_log_view(struct tog_view **,
744 struct tog_view *, int);
745 static const struct got_error *resize_log_view(struct tog_view *, int);
746 static const struct got_error *close_log_view(struct tog_view *);
747 static const struct got_error *search_start_log_view(struct tog_view *);
748 static const struct got_error *search_next_log_view(struct tog_view *);
750 static const struct got_error *open_blame_view(struct tog_view *, char *,
751 struct got_object_id *, struct got_repository *);
752 static const struct got_error *show_blame_view(struct tog_view *);
753 static const struct got_error *input_blame_view(struct tog_view **,
754 struct tog_view *, int);
755 static const struct got_error *reset_blame_view(struct tog_view *);
756 static const struct got_error *close_blame_view(struct tog_view *);
757 static const struct got_error *search_start_blame_view(struct tog_view *);
758 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
759 size_t *, int **, int **, int **, int **);
761 static const struct got_error *open_tree_view(struct tog_view *,
762 struct got_object_id *, const char *, struct got_repository *);
763 static const struct got_error *show_tree_view(struct tog_view *);
764 static const struct got_error *input_tree_view(struct tog_view **,
765 struct tog_view *, int);
766 static const struct got_error *close_tree_view(struct tog_view *);
767 static const struct got_error *search_start_tree_view(struct tog_view *);
768 static const struct got_error *search_next_tree_view(struct tog_view *);
770 static const struct got_error *open_ref_view(struct tog_view *,
771 struct got_repository *);
772 static const struct got_error *show_ref_view(struct tog_view *);
773 static const struct got_error *input_ref_view(struct tog_view **,
774 struct tog_view *, int);
775 static const struct got_error *close_ref_view(struct tog_view *);
776 static const struct got_error *search_start_ref_view(struct tog_view *);
777 static const struct got_error *search_next_ref_view(struct tog_view *);
779 static const struct got_error *open_help_view(struct tog_view *,
781 static const struct got_error *show_help_view(struct tog_view *);
782 static const struct got_error *input_help_view(struct tog_view **,
783 struct tog_view *, int);
784 static const struct got_error *reset_help_view(struct tog_view *);
785 static const struct got_error* close_help_view(struct tog_view *);
786 static const struct got_error *search_start_help_view(struct tog_view *);
787 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
788 size_t *, int **, int **, int **, int **);
790 static volatile sig_atomic_t tog_sigwinch_received;
791 static volatile sig_atomic_t tog_sigpipe_received;
792 static volatile sig_atomic_t tog_sigcont_received;
793 static volatile sig_atomic_t tog_sigint_received;
794 static volatile sig_atomic_t tog_sigterm_received;
797 tog_sigwinch(int signo)
799 tog_sigwinch_received = 1;
803 tog_sigpipe(int signo)
805 tog_sigpipe_received = 1;
809 tog_sigcont(int signo)
811 tog_sigcont_received = 1;
815 tog_sigint(int signo)
817 tog_sigint_received = 1;
821 tog_sigterm(int signo)
823 tog_sigterm_received = 1;
827 tog_fatal_signal_received(void)
829 return (tog_sigpipe_received ||
830 tog_sigint_received || tog_sigterm_received);
833 static const struct got_error *
834 view_close(struct tog_view *view)
836 const struct got_error *err = NULL, *child_err = NULL;
839 child_err = view_close(view->child);
843 err = view->close(view);
845 del_panel(view->panel);
847 delwin(view->window);
849 return err ? err : child_err;
852 static struct tog_view *
853 view_open(int nlines, int ncols, int begin_y, int begin_x,
854 enum tog_view_type type)
856 struct tog_view *view = calloc(1, sizeof(*view));
864 view->nlines = nlines ? nlines : LINES - begin_y;
865 view->ncols = ncols ? ncols : COLS - begin_x;
866 view->begin_y = begin_y;
867 view->begin_x = begin_x;
868 view->window = newwin(nlines, ncols, begin_y, begin_x);
869 if (view->window == NULL) {
873 view->panel = new_panel(view->window);
874 if (view->panel == NULL ||
875 set_panel_userptr(view->panel, view) != OK) {
880 keypad(view->window, TRUE);
885 view_split_begin_x(int begin_x)
887 if (begin_x > 0 || COLS < 120)
889 return (COLS - MAX(COLS / 2, 80));
892 /* XXX Stub till we decide what to do. */
894 view_split_begin_y(int lines)
896 return lines * HSPLIT_SCALE;
899 static const struct got_error *view_resize(struct tog_view *);
901 static const struct got_error *
902 view_splitscreen(struct tog_view *view)
904 const struct got_error *err = NULL;
906 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
907 if (view->resized_y && view->resized_y < view->lines)
908 view->begin_y = view->resized_y;
910 view->begin_y = view_split_begin_y(view->nlines);
912 } else if (!view->resized) {
913 if (view->resized_x && view->resized_x < view->cols - 1 &&
915 view->begin_x = view->resized_x;
917 view->begin_x = view_split_begin_x(0);
920 view->nlines = LINES - view->begin_y;
921 view->ncols = COLS - view->begin_x;
924 err = view_resize(view);
928 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
929 view->parent->nlines = view->begin_y;
931 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
932 return got_error_from_errno("mvwin");
937 static const struct got_error *
938 view_fullscreen(struct tog_view *view)
940 const struct got_error *err = NULL;
943 view->begin_y = view->resized ? view->begin_y : 0;
944 view->nlines = view->resized ? view->nlines : LINES;
948 err = view_resize(view);
952 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
953 return got_error_from_errno("mvwin");
959 view_is_parent_view(struct tog_view *view)
961 return view->parent == NULL;
965 view_is_splitscreen(struct tog_view *view)
967 return view->begin_x > 0 || view->begin_y > 0;
971 view_is_fullscreen(struct tog_view *view)
973 return view->nlines == LINES && view->ncols == COLS;
977 view_is_hsplit_top(struct tog_view *view)
979 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
980 view_is_splitscreen(view->child);
984 view_border(struct tog_view *view)
987 const struct tog_view *view_above;
990 return view_border(view->parent);
992 panel = panel_above(view->panel);
996 view_above = panel_userptr(panel);
997 if (view->mode == TOG_VIEW_SPLIT_HRZN)
998 mvwhline(view->window, view_above->begin_y - 1,
999 view->begin_x, ACS_HLINE, view->ncols);
1001 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1002 ACS_VLINE, view->nlines);
1005 static const struct got_error *view_init_hsplit(struct tog_view *, int);
1006 static const struct got_error *request_log_commits(struct tog_view *);
1007 static const struct got_error *offset_selection_down(struct tog_view *);
1008 static void offset_selection_up(struct tog_view *);
1009 static void view_get_split(struct tog_view *, int *, int *);
1011 static const struct got_error *
1012 view_resize(struct tog_view *view)
1014 const struct got_error *err = NULL;
1015 int dif, nlines, ncols;
1017 dif = LINES - view->lines; /* line difference */
1019 if (view->lines > LINES)
1020 nlines = view->nlines - (view->lines - LINES);
1022 nlines = view->nlines + (LINES - view->lines);
1023 if (view->cols > COLS)
1024 ncols = view->ncols - (view->cols - COLS);
1026 ncols = view->ncols + (COLS - view->cols);
1029 int hs = view->child->begin_y;
1031 if (!view_is_fullscreen(view))
1032 view->child->begin_x = view_split_begin_x(view->begin_x);
1033 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1034 view->child->begin_x == 0) {
1037 view_fullscreen(view->child);
1038 if (view->child->focussed)
1039 show_panel(view->child->panel);
1041 show_panel(view->panel);
1043 ncols = view->child->begin_x;
1045 view_splitscreen(view->child);
1046 show_panel(view->child->panel);
1049 * XXX This is ugly and needs to be moved into the above
1050 * logic but "works" for now and my attempts at moving it
1051 * break either 'tab' or 'F' key maps in horizontal splits.
1054 err = view_splitscreen(view->child);
1057 if (dif < 0) { /* top split decreased */
1058 err = offset_selection_down(view);
1065 show_panel(view->child->panel);
1066 nlines = view->nlines;
1068 } else if (view->parent == NULL)
1071 if (view->resize && dif > 0) {
1072 err = view->resize(view, dif);
1077 if (wresize(view->window, nlines, ncols) == ERR)
1078 return got_error_from_errno("wresize");
1079 if (replace_panel(view->panel, view->window) == ERR)
1080 return got_error_from_errno("replace_panel");
1081 wclear(view->window);
1083 view->nlines = nlines;
1084 view->ncols = ncols;
1085 view->lines = LINES;
1091 static const struct got_error *
1092 resize_log_view(struct tog_view *view, int increase)
1094 struct tog_log_view_state *s = &view->state.log;
1095 const struct got_error *err = NULL;
1098 if (s->selected_entry)
1099 n = s->selected_entry->idx + view->lines - s->selected;
1102 * Request commits to account for the increased
1103 * height so we have enough to populate the view.
1105 if (s->commits->ncommits < n) {
1106 view->nscrolled = n - s->commits->ncommits + increase + 1;
1107 err = request_log_commits(view);
1114 view_adjust_offset(struct tog_view *view, int n)
1119 if (view->parent && view->parent->offset) {
1120 if (view->parent->offset + n >= 0)
1121 view->parent->offset += n;
1123 view->parent->offset = 0;
1124 } else if (view->offset) {
1125 if (view->offset - n >= 0)
1132 static const struct got_error *
1133 view_resize_split(struct tog_view *view, int resize)
1135 const struct got_error *err = NULL;
1136 struct tog_view *v = NULL;
1143 if (!v->child || !view_is_splitscreen(v->child))
1146 v->resized = v->child->resized = resize; /* lock for resize event */
1148 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1149 if (v->child->resized_y)
1150 v->child->begin_y = v->child->resized_y;
1152 v->child->begin_y -= resize;
1154 v->child->begin_y += resize;
1155 if (v->child->begin_y < 3) {
1157 v->child->begin_y = 3;
1158 } else if (v->child->begin_y > LINES - 1) {
1160 v->child->begin_y = LINES - 1;
1163 v->child->ncols = COLS;
1164 view_adjust_offset(view, resize);
1165 err = view_init_hsplit(v, v->child->begin_y);
1168 v->child->resized_y = v->child->begin_y;
1170 if (v->child->resized_x)
1171 v->child->begin_x = v->child->resized_x;
1173 v->child->begin_x -= resize;
1175 v->child->begin_x += resize;
1176 if (v->child->begin_x < 11) {
1178 v->child->begin_x = 11;
1179 } else if (v->child->begin_x > COLS - 1) {
1181 v->child->begin_x = COLS - 1;
1183 v->child->resized_x = v->child->begin_x;
1186 v->child->mode = v->mode;
1187 v->child->nlines = v->lines - v->child->begin_y;
1188 v->child->ncols = v->cols - v->child->begin_x;
1191 err = view_fullscreen(v);
1194 err = view_splitscreen(v->child);
1198 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 err = offset_selection_down(v->child);
1205 err = v->resize(v, 0);
1206 else if (v->child->resize)
1207 err = v->child->resize(v->child, 0);
1209 v->resized = v->child->resized = 0;
1215 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1217 struct tog_view *v = src->child ? src->child : src;
1219 dst->resized_x = v->resized_x;
1220 dst->resized_y = v->resized_y;
1223 static const struct got_error *
1224 view_close_child(struct tog_view *view)
1226 const struct got_error *err = NULL;
1228 if (view->child == NULL)
1231 err = view_close(view->child);
1236 static const struct got_error *
1237 view_set_child(struct tog_view *view, struct tog_view *child)
1239 const struct got_error *err = NULL;
1241 view->child = child;
1242 child->parent = view;
1244 err = view_resize(view);
1248 if (view->child->resized_x || view->child->resized_y)
1249 err = view_resize_split(view, 0);
1254 static const struct got_error *view_dispatch_request(struct tog_view **,
1255 struct tog_view *, enum tog_view_type, int, int);
1257 static const struct got_error *
1258 view_request_new(struct tog_view **requested, struct tog_view *view,
1259 enum tog_view_type request)
1261 struct tog_view *new_view = NULL;
1262 const struct got_error *err;
1267 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1268 view_get_split(view, &y, &x);
1270 err = view_dispatch_request(&new_view, view, request, y, x);
1274 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1275 request != TOG_VIEW_HELP) {
1276 err = view_init_hsplit(view, y);
1282 new_view->focussed = 1;
1283 new_view->mode = view->mode;
1284 new_view->nlines = request == TOG_VIEW_HELP ?
1285 view->lines : view->lines - y;
1287 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1288 view_transfer_size(new_view, view);
1289 err = view_close_child(view);
1292 err = view_set_child(view, new_view);
1295 view->focus_child = 1;
1297 *requested = new_view;
1303 tog_resizeterm(void)
1306 struct winsize size;
1308 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1309 cols = 80; /* Default */
1313 lines = size.ws_row;
1315 resize_term(lines, cols);
1318 static const struct got_error *
1319 view_search_start(struct tog_view *view, int fast_refresh)
1321 const struct got_error *err = NULL;
1322 struct tog_view *v = view;
1326 if (view->search_started) {
1327 regfree(&view->regex);
1328 view->searching = 0;
1329 memset(&view->regmatch, 0, sizeof(view->regmatch));
1331 view->search_started = 0;
1333 if (view->nlines < 1)
1336 if (view_is_hsplit_top(view))
1338 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1341 if (tog_io.input_str != NULL) {
1342 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
1344 return got_error(GOT_ERR_NO_SPACE);
1346 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1347 wclrtoeol(v->window);
1348 nodelay(v->window, FALSE); /* block for search term input */
1351 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1352 wrefresh(v->window);
1355 nodelay(v->window, TRUE);
1356 if (!fast_refresh && !using_mock_io)
1362 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1363 err = view->search_start(view);
1365 regfree(&view->regex);
1368 view->search_started = 1;
1369 view->searching = TOG_SEARCH_FORWARD;
1370 view->search_next_done = 0;
1371 view->search_next(view);
1377 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1378 static const struct got_error *
1379 switch_split(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_view *v = NULL;
1389 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1390 v->mode = TOG_VIEW_SPLIT_VERT;
1392 v->mode = TOG_VIEW_SPLIT_HRZN;
1396 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1397 v->mode = TOG_VIEW_SPLIT_NONE;
1399 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1400 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1401 v->child->begin_y = v->child->resized_y;
1402 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1403 v->child->begin_x = v->child->resized_x;
1406 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1408 v->child->ncols = COLS;
1409 v->child->nscrolled = LINES - v->child->nlines;
1411 err = view_init_hsplit(v, v->child->begin_y);
1415 v->child->mode = v->mode;
1416 v->child->nlines = v->lines - v->child->begin_y;
1419 err = view_fullscreen(v);
1422 err = view_splitscreen(v->child);
1426 if (v->mode == TOG_VIEW_SPLIT_NONE)
1427 v->mode = TOG_VIEW_SPLIT_VERT;
1428 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1429 err = offset_selection_down(v);
1432 err = offset_selection_down(v->child);
1436 offset_selection_up(v);
1437 offset_selection_up(v->child);
1440 err = v->resize(v, 0);
1441 else if (v->child->resize)
1442 err = v->child->resize(v->child, 0);
1448 * Strip trailing whitespace from str starting at byte *n;
1449 * if *n < 0, use strlen(str). Return new str length in *n.
1452 strip_trailing_ws(char *str, int *n)
1456 if (str == NULL || *str == '\0')
1462 while (x-- > 0 && isspace((unsigned char)str[x]))
1469 * Extract visible substring of line y from the curses screen
1470 * and strip trailing whitespace. If vline is set, overwrite
1471 * line[vline] with '|' because the ACS_VLINE character is
1472 * written out as 'x'. Write the line to file f.
1474 static const struct got_error *
1475 view_write_line(FILE *f, int y, int vline)
1477 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1480 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1482 return got_error_fmt(GOT_ERR_RANGE,
1483 "failed to extract line %d", y);
1486 * In some views, lines are padded with blanks to COLS width.
1487 * Strip them so we can diff without the -b flag when testing.
1489 strip_trailing_ws(line, &r);
1494 w = fprintf(f, "%s\n", line);
1495 if (w != r + 1) /* \n */
1496 return got_ferror(f, GOT_ERR_IO);
1502 * Capture the visible curses screen by writing each line to the
1503 * file at the path set via the TOG_SCR_DUMP environment variable.
1505 static const struct got_error *
1506 screendump(struct tog_view *view)
1508 const struct got_error *err;
1511 err = got_opentemp_truncate(tog_io.sdump);
1515 if ((view->child && view->child->begin_x) ||
1516 (view->parent && view->begin_x)) {
1517 int ncols = view->child ? view->ncols : view->parent->ncols;
1519 /* vertical splitscreen */
1520 for (i = 0; i < view->nlines; ++i) {
1521 err = view_write_line(tog_io.sdump, i, ncols - 1);
1528 /* fullscreen or horizontal splitscreen */
1529 if ((view->child && view->child->begin_y) ||
1530 (view->parent && view->begin_y)) /* hsplit */
1531 hline = view->child ?
1532 view->child->begin_y : view->begin_y;
1534 for (i = 0; i < view->lines; i++) {
1535 if (hline && i == hline - 1) {
1538 /* ACS_HLINE writes out as 'q', overwrite it */
1539 for (c = 0; c < view->cols; ++c)
1540 fputc('-', tog_io.sdump);
1541 fputc('\n', tog_io.sdump);
1545 err = view_write_line(tog_io.sdump, i, 0);
1556 * Compute view->count from numeric input. Assign total to view->count and
1557 * return first non-numeric key entered.
1560 get_compound_key(struct tog_view *view, int c)
1562 struct tog_view *v = view;
1565 if (view_is_hsplit_top(view))
1567 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1571 cbreak(); /* block for input */
1572 nodelay(view->window, FALSE);
1573 wmove(v->window, v->nlines - 1, 0);
1574 wclrtoeol(v->window);
1575 waddch(v->window, ':');
1578 x = getcurx(v->window);
1579 if (x != ERR && x < view->ncols) {
1580 waddch(v->window, c);
1581 wrefresh(v->window);
1585 * Don't overflow. Max valid request should be the greatest
1586 * between the longest and total lines; cap at 10 million.
1591 n = n * 10 + (c - '0');
1592 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1594 if (c == 'G' || c == 'g') { /* nG key map */
1595 view->gline = view->hiline = n;
1600 /* Massage excessive or inapplicable values at the input handler. */
1607 action_report(struct tog_view *view)
1609 struct tog_view *v = view;
1611 if (view_is_hsplit_top(view))
1613 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1616 wmove(v->window, v->nlines - 1, 0);
1617 wclrtoeol(v->window);
1618 wprintw(v->window, ":%s", view->action);
1619 wrefresh(v->window);
1622 * Clear action status report. Only clear in blame view
1623 * once annotating is complete, otherwise it's too fast.
1625 if (view->type == TOG_VIEW_BLAME) {
1626 if (view->state.blame.blame_complete)
1627 view->action = NULL;
1629 view->action = NULL;
1633 * Read the next line from the test script and assign
1634 * key instruction to *ch. If at EOF, set the *done flag.
1636 static const struct got_error *
1637 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1639 const struct got_error *err = NULL;
1645 if (view->count && --view->count) {
1651 if ((n = getline(&line, &linesz, script)) == -1) {
1656 err = got_ferror(script, GOT_ERR_IO);
1661 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1662 tog_io.wait_for_ui = 1;
1663 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1665 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1667 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1669 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1671 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1673 else if (strncasecmp(line, "TAB", 3) == 0)
1675 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1676 *ch = TOG_KEY_SCRDUMP;
1677 else if (isdigit((unsigned char)*line)) {
1680 while (isdigit((unsigned char)*t))
1682 view->ch = *ch = *t;
1684 /* ignore error, view->count is 0 if instruction is invalid */
1685 view->count = strtonum(line, 0, INT_MAX, NULL);
1688 if (n > 2 && (*ch == '/' || *ch == '&')) {
1689 /* skip leading keymap and trim trailing newline */
1690 tog_io.input_str = strndup(line + 1, n - 2);
1691 if (tog_io.input_str == NULL) {
1692 err = got_error_from_errno("strndup");
1703 static const struct got_error *
1704 view_input(struct tog_view **new, int *done, struct tog_view *view,
1705 struct tog_view_list_head *views, int fast_refresh)
1707 const struct got_error *err = NULL;
1714 action_report(view);
1716 /* Clear "no matches" indicator. */
1717 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1718 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1719 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1723 if (view->searching && !view->search_next_done) {
1724 errcode = pthread_mutex_unlock(&tog_mutex);
1726 return got_error_set_errno(errcode,
1727 "pthread_mutex_unlock");
1729 errcode = pthread_mutex_lock(&tog_mutex);
1731 return got_error_set_errno(errcode,
1732 "pthread_mutex_lock");
1733 view->search_next(view);
1737 /* Allow threads to make progress while we are waiting for input. */
1738 errcode = pthread_mutex_unlock(&tog_mutex);
1740 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1742 if (using_mock_io) {
1743 err = tog_read_script_key(tog_io.f, view, &ch, done);
1745 errcode = pthread_mutex_lock(&tog_mutex);
1748 } else if (view->count && --view->count) {
1750 nodelay(view->window, TRUE);
1751 ch = wgetch(view->window);
1752 /* let C-g or backspace abort unfinished count */
1753 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1758 ch = wgetch(view->window);
1759 if (ch >= '1' && ch <= '9')
1760 view->ch = ch = get_compound_key(view, ch);
1762 if (view->hiline && ch != ERR && ch != 0)
1763 view->hiline = 0; /* key pressed, clear line highlight */
1764 nodelay(view->window, TRUE);
1765 errcode = pthread_mutex_lock(&tog_mutex);
1767 return got_error_set_errno(errcode, "pthread_mutex_lock");
1769 if (tog_sigwinch_received || tog_sigcont_received) {
1771 tog_sigwinch_received = 0;
1772 tog_sigcont_received = 0;
1773 TAILQ_FOREACH(v, views, entry) {
1774 err = view_resize(v);
1777 err = v->input(new, v, KEY_RESIZE);
1781 err = view_resize(v->child);
1784 err = v->child->input(new, v->child,
1788 if (v->child->resized_x || v->child->resized_y) {
1789 err = view_resize_split(v, 0);
1801 if (view->type == TOG_VIEW_HELP)
1802 err = view->reset(view);
1804 err = view_request_new(new, view, TOG_VIEW_HELP);
1810 view->child->focussed = 1;
1811 view->focus_child = 1;
1812 } else if (view->parent) {
1814 view->parent->focussed = 1;
1815 view->parent->focus_child = 0;
1816 if (!view_is_splitscreen(view)) {
1817 if (view->parent->resize) {
1818 err = view->parent->resize(view->parent,
1823 offset_selection_up(view->parent);
1824 err = view_fullscreen(view->parent);
1831 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1832 if (view->parent->resize) {
1833 /* might need more commits to fill fullscreen */
1834 err = view->parent->resize(view->parent, 0);
1838 offset_selection_up(view->parent);
1840 err = view->input(new, view, ch);
1848 if (view_is_parent_view(view)) {
1849 if (view->child == NULL)
1851 if (view_is_splitscreen(view->child)) {
1853 view->child->focussed = 1;
1854 err = view_fullscreen(view->child);
1856 err = view_splitscreen(view->child);
1858 err = view_resize_split(view, 0);
1862 err = view->child->input(new, view->child,
1865 if (view_is_splitscreen(view)) {
1866 view->parent->focussed = 0;
1868 err = view_fullscreen(view);
1870 err = view_splitscreen(view);
1871 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1872 err = view_resize(view->parent);
1874 err = view_resize_split(view, 0);
1878 err = view->input(new, view, KEY_RESIZE);
1883 err = view->resize(view, 0);
1888 if (view->parent->resize) {
1889 err = view->parent->resize(view->parent, 0);
1893 err = offset_selection_down(view->parent);
1897 err = offset_selection_down(view);
1901 err = switch_split(view);
1904 err = view_resize_split(view, -1);
1907 err = view_resize_split(view, 1);
1913 if (view->search_start)
1914 view_search_start(view, fast_refresh);
1916 err = view->input(new, view, ch);
1920 if (view->search_started && view->search_next) {
1921 view->searching = (ch == 'n' ?
1922 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1923 view->search_next_done = 0;
1924 view->search_next(view);
1926 err = view->input(new, view, ch);
1929 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1930 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1931 view->action = "Patience diff algorithm";
1933 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1934 view->action = "Myers diff algorithm";
1936 TAILQ_FOREACH(v, views, entry) {
1942 if (v->child && v->child->reset) {
1943 err = v->child->reset(v->child);
1949 case TOG_KEY_SCRDUMP:
1950 err = screendump(view);
1953 err = view->input(new, view, ch);
1961 view_needs_focus_indication(struct tog_view *view)
1963 if (view_is_parent_view(view)) {
1964 if (view->child == NULL || view->child->focussed)
1966 if (!view_is_splitscreen(view->child))
1968 } else if (!view_is_splitscreen(view))
1971 return view->focussed;
1974 static const struct got_error *
1977 const struct got_error *err = NULL;
1979 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1980 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1981 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1982 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1983 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1984 err = got_ferror(tog_io.f, GOT_ERR_IO);
1985 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1986 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1987 if (tog_io.input_str != NULL)
1988 free(tog_io.input_str);
1993 static const struct got_error *
1994 view_loop(struct tog_view *view)
1996 const struct got_error *err = NULL;
1997 struct tog_view_list_head views;
1998 struct tog_view *new_view;
2000 int fast_refresh = 10;
2001 int done = 0, errcode;
2003 mode = getenv("TOG_VIEW_SPLIT_MODE");
2004 if (!mode || !(*mode == 'h' || *mode == 'H'))
2005 view->mode = TOG_VIEW_SPLIT_VERT;
2007 view->mode = TOG_VIEW_SPLIT_HRZN;
2009 errcode = pthread_mutex_lock(&tog_mutex);
2011 return got_error_set_errno(errcode, "pthread_mutex_lock");
2014 TAILQ_INSERT_HEAD(&views, view, entry);
2017 err = view->show(view);
2022 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
2023 !tog_fatal_signal_received()) {
2024 /* Refresh fast during initialization, then become slower. */
2025 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2026 halfdelay(10); /* switch to once per second */
2028 err = view_input(&new_view, &done, view, &views, fast_refresh);
2032 if (view->dying && view == TAILQ_FIRST(&views) &&
2033 TAILQ_NEXT(view, entry) == NULL)
2039 * When we quit, scroll the screen up a single line
2040 * so we don't lose any information.
2042 TAILQ_FOREACH(v, &views, entry) {
2043 wmove(v->window, 0, 0);
2044 wdeleteln(v->window);
2045 wnoutrefresh(v->window);
2046 if (v->child && !view_is_fullscreen(v)) {
2047 wmove(v->child->window, 0, 0);
2048 wdeleteln(v->child->window);
2049 wnoutrefresh(v->child->window);
2056 struct tog_view *v, *prev = NULL;
2058 if (view_is_parent_view(view))
2059 prev = TAILQ_PREV(view, tog_view_list_head,
2061 else if (view->parent)
2062 prev = view->parent;
2065 view->parent->child = NULL;
2066 view->parent->focus_child = 0;
2067 /* Restore fullscreen line height. */
2068 view->parent->nlines = view->parent->lines;
2069 err = view_resize(view->parent);
2072 /* Make resized splits persist. */
2073 view_transfer_size(view->parent, view);
2075 TAILQ_REMOVE(&views, view, entry);
2077 err = view_close(view);
2082 TAILQ_FOREACH(v, &views, entry) {
2086 if (view == NULL && new_view == NULL) {
2087 /* No view has focus. Try to pick one. */
2090 else if (!TAILQ_EMPTY(&views)) {
2091 view = TAILQ_LAST(&views,
2092 tog_view_list_head);
2095 if (view->focus_child) {
2096 view->child->focussed = 1;
2104 struct tog_view *v, *t;
2105 /* Only allow one parent view per type. */
2106 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2107 if (v->type != new_view->type)
2109 TAILQ_REMOVE(&views, v, entry);
2110 err = view_close(v);
2115 TAILQ_INSERT_TAIL(&views, new_view, entry);
2118 if (view && !done) {
2119 if (view_is_parent_view(view)) {
2120 if (view->child && view->child->focussed)
2123 if (view->parent && view->parent->focussed)
2124 view = view->parent;
2126 show_panel(view->panel);
2127 if (view->child && view_is_splitscreen(view->child))
2128 show_panel(view->child->panel);
2129 if (view->parent && view_is_splitscreen(view)) {
2130 err = view->parent->show(view->parent);
2134 err = view->show(view);
2138 err = view->child->show(view->child);
2147 while (!TAILQ_EMPTY(&views)) {
2148 const struct got_error *close_err;
2149 view = TAILQ_FIRST(&views);
2150 TAILQ_REMOVE(&views, view, entry);
2151 close_err = view_close(view);
2152 if (close_err && err == NULL)
2156 errcode = pthread_mutex_unlock(&tog_mutex);
2157 if (errcode && err == NULL)
2158 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2168 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2173 /* Create newly allocated wide-character string equivalent to a byte string. */
2174 static const struct got_error *
2175 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2178 const struct got_error *err = NULL;
2181 *wlen = mbstowcs(NULL, s, 0);
2182 if (*wlen == (size_t)-1) {
2184 if (errno != EILSEQ)
2185 return got_error_from_errno("mbstowcs");
2187 /* byte string invalid in current encoding; try to "fix" it */
2188 err = got_mbsavis(&vis, &vislen, s);
2191 *wlen = mbstowcs(NULL, vis, 0);
2192 if (*wlen == (size_t)-1) {
2193 err = got_error_from_errno("mbstowcs"); /* give up */
2198 *ws = calloc(*wlen + 1, sizeof(**ws));
2200 err = got_error_from_errno("calloc");
2204 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2205 err = got_error_from_errno("mbstowcs");
2216 static const struct got_error *
2217 expand_tab(char **ptr, const char *src)
2220 size_t len, n, idx = 0, sz = 0;
2223 n = len = strlen(src);
2224 dst = malloc(n + 1);
2226 return got_error_from_errno("malloc");
2228 while (idx < len && src[idx]) {
2229 const char c = src[idx];
2232 size_t nb = TABSIZE - sz % TABSIZE;
2235 p = realloc(dst, n + nb);
2238 return got_error_from_errno("realloc");
2243 memset(dst + sz, ' ', nb);
2246 dst[sz++] = src[idx];
2256 * Advance at most n columns from wline starting at offset off.
2257 * Return the index to the first character after the span operation.
2258 * Return the combined column width of all spanned wide characters in
2262 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2264 int width, i, cols = 0;
2271 for (i = off; wline[i] != L'\0'; ++i) {
2272 if (wline[i] == L'\t')
2273 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2275 width = wcwidth(wline[i]);
2282 if (cols + width > n)
2292 * Format a line for display, ensuring that it won't overflow a width limit.
2293 * With scrolling, the width returned refers to the scrolled version of the
2294 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2296 static const struct got_error *
2297 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2298 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2300 const struct got_error *err = NULL;
2302 wchar_t *wline = NULL;
2311 err = expand_tab(&exstr, line);
2316 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2321 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2323 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2324 wline[wlen - 1] = L'\0';
2327 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2328 wline[wlen - 1] = L'\0';
2332 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2338 *scrollxp = scrollx;
2346 static const struct got_error*
2347 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2348 struct got_object_id *id, struct got_repository *repo)
2350 static const struct got_error *err = NULL;
2351 struct got_reflist_entry *re;
2360 TAILQ_FOREACH(re, refs, entry) {
2361 struct got_tag_object *tag = NULL;
2362 struct got_object_id *ref_id;
2365 name = got_ref_get_name(re->ref);
2366 if (strcmp(name, GOT_REF_HEAD) == 0)
2368 if (strncmp(name, "refs/", 5) == 0)
2370 if (strncmp(name, "got/", 4) == 0)
2372 if (strncmp(name, "heads/", 6) == 0)
2374 if (strncmp(name, "remotes/", 8) == 0) {
2376 s = strstr(name, "/" GOT_REF_HEAD);
2377 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2380 err = got_ref_resolve(&ref_id, repo, re->ref);
2383 if (strncmp(name, "tags/", 5) == 0) {
2384 err = got_object_open_as_tag(&tag, repo, ref_id);
2386 if (err->code != GOT_ERR_OBJ_TYPE) {
2390 /* Ref points at something other than a tag. */
2395 cmp = got_object_id_cmp(tag ?
2396 got_object_tag_get_object_id(tag) : ref_id, id);
2399 got_object_tag_close(tag);
2403 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2404 s ? ", " : "", name) == -1) {
2405 err = got_error_from_errno("asprintf");
2416 static const struct got_error *
2417 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2422 smallerthan = strchr(author, '<');
2423 if (smallerthan && smallerthan[1] != '\0')
2424 author = smallerthan + 1;
2425 author[strcspn(author, "@>")] = '\0';
2426 return format_line(wauthor, author_width, NULL, author, 0, limit,
2430 static const struct got_error *
2431 draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2432 const size_t date_display_cols, int author_display_cols)
2434 struct tog_log_view_state *s = &view->state.log;
2435 const struct got_error *err = NULL;
2436 struct got_commit_object *commit = entry->commit;
2437 struct got_object_id *id = entry->id;
2438 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2439 char *refs_str = NULL;
2440 char *logmsg0 = NULL, *logmsg = NULL;
2441 char *author = NULL;
2442 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2443 int author_width, refstr_width, logmsg_width;
2444 char *newline, *line = NULL;
2445 int col, limit, scrollx, logmsg_x;
2446 const int avail = view->ncols, marker_column = author_display_cols + 1;
2448 time_t committer_time;
2449 struct tog_color *tc;
2450 struct got_reflist_head *refs;
2452 if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2453 got_object_id_cmp(id, tog_base_commit.id) == 0)
2454 tog_base_commit.idx = entry->idx;
2455 if (tog_io.wait_for_ui && s->thread_args.need_commit_marker) {
2458 rc = pthread_cond_wait(&s->thread_args.log_loaded, &tog_mutex);
2460 return got_error_set_errno(rc, "pthread_cond_wait");
2463 committer_time = got_object_commit_get_committer_time(commit);
2464 if (gmtime_r(&committer_time, &tm) == NULL)
2465 return got_error_from_errno("gmtime_r");
2466 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2467 return got_error(GOT_ERR_NO_SPACE);
2469 if (avail <= date_display_cols)
2470 limit = MIN(sizeof(datebuf) - 1, avail);
2472 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2473 tc = get_color(&s->colors, TOG_COLOR_DATE);
2475 wattr_on(view->window,
2476 COLOR_PAIR(tc->colorpair), NULL);
2477 waddnstr(view->window, datebuf, limit);
2479 wattr_off(view->window,
2480 COLOR_PAIR(tc->colorpair), NULL);
2487 err = got_object_id_str(&id_str, id);
2490 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2492 wattr_on(view->window,
2493 COLOR_PAIR(tc->colorpair), NULL);
2494 wprintw(view->window, "%.8s ", id_str);
2496 wattr_off(view->window,
2497 COLOR_PAIR(tc->colorpair), NULL);
2504 if (s->use_committer)
2505 author = strdup(got_object_commit_get_committer(commit));
2507 author = strdup(got_object_commit_get_author(commit));
2508 if (author == NULL) {
2509 err = got_error_from_errno("strdup");
2512 err = format_author(&wauthor, &author_width, author, avail - col, col);
2515 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2517 wattr_on(view->window,
2518 COLOR_PAIR(tc->colorpair), NULL);
2519 waddwstr(view->window, wauthor);
2520 col += author_width;
2521 while (col < avail && author_width < author_display_cols + 2) {
2522 if (tog_base_commit.marker != GOT_WORKTREE_STATE_UNKNOWN &&
2523 author_width == marker_column &&
2524 entry->idx == tog_base_commit.idx && !s->limit_view) {
2525 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2527 wattr_on(view->window,
2528 COLOR_PAIR(tc->colorpair), NULL);
2529 waddch(view->window, tog_base_commit.marker);
2531 wattr_off(view->window,
2532 COLOR_PAIR(tc->colorpair), NULL);
2534 waddch(view->window, ' ');
2539 wattr_off(view->window,
2540 COLOR_PAIR(tc->colorpair), NULL);
2544 err = got_object_commit_get_logmsg(&logmsg0, commit);
2548 while (*logmsg == '\n')
2550 newline = strchr(logmsg, '\n');
2554 limit = avail - col;
2555 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2556 limit--; /* for the border */
2558 /* Prepend reference labels to log message if possible .*/
2559 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2560 err = build_refs_str(&refs_str, refs, id, s->repo);
2566 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2567 err = got_error_from_errno("asprintf");
2570 err = format_line(&wrefstr, &refstr_width,
2571 &scrollx, rs, view->x, limit, col, 1);
2575 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2577 wattr_on(view->window,
2578 COLOR_PAIR(tc->colorpair), NULL);
2579 waddwstr(view->window, &wrefstr[scrollx]);
2581 wattr_off(view->window,
2582 COLOR_PAIR(tc->colorpair), NULL);
2583 col += MAX(refstr_width, 0);
2588 waddch(view->window, ' ');
2592 if (refstr_width > 0)
2595 int unscrolled_refstr_width;
2596 size_t len = wcslen(wrefstr);
2599 * No need to check for -1 return value here since
2600 * unprintables have been replaced by span_wline().
2602 unscrolled_refstr_width = wcswidth(wrefstr, len);
2603 unscrolled_refstr_width += 1; /* trailing space */
2604 logmsg_x = view->x - unscrolled_refstr_width;
2607 limit = avail - col;
2608 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2609 limit--; /* for the border */
2613 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2617 waddwstr(view->window, &wlogmsg[scrollx]);
2618 col += MAX(logmsg_width, 0);
2619 while (col < avail) {
2620 waddch(view->window, ' ');
2634 static struct commit_queue_entry *
2635 alloc_commit_queue_entry(struct got_commit_object *commit,
2636 struct got_object_id *id)
2638 struct commit_queue_entry *entry;
2639 struct got_object_id *dup;
2641 entry = calloc(1, sizeof(*entry));
2645 dup = got_object_id_dup(id);
2652 entry->commit = commit;
2657 pop_commit(struct commit_queue *commits)
2659 struct commit_queue_entry *entry;
2661 entry = TAILQ_FIRST(&commits->head);
2662 TAILQ_REMOVE(&commits->head, entry, entry);
2663 got_object_commit_close(entry->commit);
2664 commits->ncommits--;
2670 free_commits(struct commit_queue *commits)
2672 while (!TAILQ_EMPTY(&commits->head))
2673 pop_commit(commits);
2676 static const struct got_error *
2677 match_commit(int *have_match, struct got_object_id *id,
2678 struct got_commit_object *commit, regex_t *regex)
2680 const struct got_error *err = NULL;
2681 regmatch_t regmatch;
2682 char *id_str = NULL, *logmsg = NULL;
2686 err = got_object_id_str(&id_str, id);
2690 err = got_object_commit_get_logmsg(&logmsg, commit);
2694 if (regexec(regex, got_object_commit_get_author(commit), 1,
2695 ®match, 0) == 0 ||
2696 regexec(regex, got_object_commit_get_committer(commit), 1,
2697 ®match, 0) == 0 ||
2698 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2699 regexec(regex, logmsg, 1, ®match, 0) == 0)
2707 static const struct got_error *
2708 queue_commits(struct tog_log_thread_args *a)
2710 const struct got_error *err = NULL;
2713 * We keep all commits open throughout the lifetime of the log
2714 * view in order to avoid having to re-fetch commits from disk
2715 * while updating the display.
2718 struct got_object_id id;
2719 struct got_commit_object *commit;
2720 struct commit_queue_entry *entry;
2721 int limit_match = 0;
2724 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2729 err = got_object_open_as_commit(&commit, a->repo, &id);
2732 entry = alloc_commit_queue_entry(commit, &id);
2733 if (entry == NULL) {
2734 err = got_error_from_errno("alloc_commit_queue_entry");
2738 errcode = pthread_mutex_lock(&tog_mutex);
2740 err = got_error_set_errno(errcode,
2741 "pthread_mutex_lock");
2745 entry->idx = a->real_commits->ncommits;
2746 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2747 a->real_commits->ncommits++;
2750 err = match_commit(&limit_match, &id, commit,
2756 struct commit_queue_entry *matched;
2758 matched = alloc_commit_queue_entry(
2759 entry->commit, entry->id);
2760 if (matched == NULL) {
2761 err = got_error_from_errno(
2762 "alloc_commit_queue_entry");
2765 matched->commit = entry->commit;
2766 got_object_commit_retain(entry->commit);
2768 matched->idx = a->limit_commits->ncommits;
2769 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2771 a->limit_commits->ncommits++;
2775 * This is how we signal log_thread() that we
2776 * have found a match, and that it should be
2777 * counted as a new entry for the view.
2779 a->limit_match = limit_match;
2782 if (*a->searching == TOG_SEARCH_FORWARD &&
2783 !*a->search_next_done) {
2785 err = match_commit(&have_match, &id, commit, a->regex);
2790 if (limit_match && have_match)
2791 *a->search_next_done =
2792 TOG_SEARCH_HAVE_MORE;
2793 } else if (have_match)
2794 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2797 errcode = pthread_mutex_unlock(&tog_mutex);
2798 if (errcode && err == NULL)
2799 err = got_error_set_errno(errcode,
2800 "pthread_mutex_unlock");
2803 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2809 select_commit(struct tog_log_view_state *s)
2811 struct commit_queue_entry *entry;
2814 entry = s->first_displayed_entry;
2816 if (ncommits == s->selected) {
2817 s->selected_entry = entry;
2820 entry = TAILQ_NEXT(entry, entry);
2825 static const struct got_error *
2826 draw_commits(struct tog_view *view)
2828 const struct got_error *err = NULL;
2829 struct tog_log_view_state *s = &view->state.log;
2830 struct commit_queue_entry *entry = s->selected_entry;
2831 int limit = view->nlines;
2833 int ncommits, author_cols = 4, refstr_cols;
2834 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2835 char *refs_str = NULL;
2837 struct tog_color *tc;
2838 static const size_t date_display_cols = 12;
2839 struct got_reflist_head *refs;
2841 if (view_is_hsplit_top(view))
2842 --limit; /* account for border */
2844 if (s->selected_entry &&
2845 !(view->searching && view->search_next_done == 0)) {
2846 err = got_object_id_str(&id_str, s->selected_entry->id);
2849 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2850 s->selected_entry->id);
2851 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2857 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2858 halfdelay(10); /* disable fast refresh */
2860 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2861 if (asprintf(&ncommits_str, " [%d/%d] %s",
2862 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2863 (view->searching && !view->search_next_done) ?
2864 "searching..." : "loading...") == -1) {
2865 err = got_error_from_errno("asprintf");
2869 const char *search_str = NULL;
2870 const char *limit_str = NULL;
2872 if (view->searching) {
2873 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2874 search_str = "no more matches";
2875 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2876 search_str = "no matches found";
2877 else if (!view->search_next_done)
2878 search_str = "searching...";
2881 if (s->limit_view && s->commits->ncommits == 0)
2882 limit_str = "no matches found";
2884 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2885 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2886 search_str ? search_str : (refs_str ? refs_str : ""),
2887 limit_str ? limit_str : "") == -1) {
2888 err = got_error_from_errno("asprintf");
2896 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2897 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2898 "........................................",
2899 s->in_repo_path, ncommits_str) == -1) {
2900 err = got_error_from_errno("asprintf");
2904 } else if (asprintf(&header, "commit %s%s",
2905 id_str ? id_str : "........................................",
2906 ncommits_str) == -1) {
2907 err = got_error_from_errno("asprintf");
2911 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2915 werase(view->window);
2917 if (view_needs_focus_indication(view))
2918 wstandout(view->window);
2919 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2921 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2922 waddwstr(view->window, wline);
2923 while (width < view->ncols) {
2924 waddch(view->window, ' ');
2928 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2929 if (view_needs_focus_indication(view))
2930 wstandend(view->window);
2935 /* Grow author column size if necessary, and set view->maxx. */
2936 entry = s->first_displayed_entry;
2940 struct got_commit_object *c = entry->commit;
2941 char *author, *eol, *msg, *msg0;
2942 wchar_t *wauthor, *wmsg;
2944 if (ncommits >= limit - 1)
2946 if (s->use_committer)
2947 author = strdup(got_object_commit_get_committer(c));
2949 author = strdup(got_object_commit_get_author(c));
2950 if (author == NULL) {
2951 err = got_error_from_errno("strdup");
2954 err = format_author(&wauthor, &width, author, COLS,
2956 if (author_cols < width)
2957 author_cols = width;
2962 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2964 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2969 err = format_line(&ws, &width, NULL, refs_str,
2970 0, INT_MAX, date_display_cols + author_cols, 0);
2976 refstr_cols = width + 3; /* account for [ ] + space */
2979 err = got_object_commit_get_logmsg(&msg0, c);
2983 while (*msg == '\n')
2985 if ((eol = strchr(msg, '\n')))
2987 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2988 date_display_cols + author_cols + refstr_cols, 0);
2991 view->maxx = MAX(view->maxx, width + refstr_cols);
2995 entry = TAILQ_NEXT(entry, entry);
2998 entry = s->first_displayed_entry;
2999 s->last_displayed_entry = s->first_displayed_entry;
3002 if (ncommits >= limit - 1)
3004 if (ncommits == s->selected)
3005 wstandout(view->window);
3006 err = draw_commit(view, entry, date_display_cols, author_cols);
3007 if (ncommits == s->selected)
3008 wstandend(view->window);
3012 s->last_displayed_entry = entry;
3013 entry = TAILQ_NEXT(entry, entry);
3026 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
3028 struct commit_queue_entry *entry;
3031 entry = TAILQ_FIRST(&s->commits->head);
3032 if (s->first_displayed_entry == entry)
3035 entry = s->first_displayed_entry;
3036 while (entry && nscrolled < maxscroll) {
3037 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3039 s->first_displayed_entry = entry;
3045 static const struct got_error *
3046 trigger_log_thread(struct tog_view *view, int wait)
3048 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3052 halfdelay(1); /* fast refresh while loading commits */
3054 while (!ta->log_complete && !tog_thread_error &&
3055 (ta->commits_needed > 0 || ta->load_all)) {
3056 /* Wake the log thread. */
3057 errcode = pthread_cond_signal(&ta->need_commits);
3059 return got_error_set_errno(errcode,
3060 "pthread_cond_signal");
3063 * The mutex will be released while the view loop waits
3064 * in wgetch(), at which time the log thread will run.
3069 /* Display progress update in log view. */
3070 show_log_view(view);
3074 /* Wait right here while next commit is being loaded. */
3075 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3077 return got_error_set_errno(errcode,
3078 "pthread_cond_wait");
3080 /* Display progress update in log view. */
3081 show_log_view(view);
3089 static const struct got_error *
3090 request_log_commits(struct tog_view *view)
3092 struct tog_log_view_state *state = &view->state.log;
3093 const struct got_error *err = NULL;
3095 if (state->thread_args.log_complete)
3098 state->thread_args.commits_needed += view->nscrolled;
3099 err = trigger_log_thread(view, 1);
3100 view->nscrolled = 0;
3105 static const struct got_error *
3106 log_scroll_down(struct tog_view *view, int maxscroll)
3108 struct tog_log_view_state *s = &view->state.log;
3109 const struct got_error *err = NULL;
3110 struct commit_queue_entry *pentry;
3111 int nscrolled = 0, ncommits_needed;
3113 if (s->last_displayed_entry == NULL)
3116 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3117 if (s->commits->ncommits < ncommits_needed &&
3118 !s->thread_args.log_complete) {
3120 * Ask the log thread for required amount of commits.
3122 s->thread_args.commits_needed +=
3123 ncommits_needed - s->commits->ncommits;
3124 err = trigger_log_thread(view, 1);
3130 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3131 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3134 s->last_displayed_entry = pentry ?
3135 pentry : s->last_displayed_entry;
3137 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3140 s->first_displayed_entry = pentry;
3141 } while (++nscrolled < maxscroll);
3143 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3144 view->nscrolled += nscrolled;
3146 view->nscrolled = 0;
3151 static const struct got_error *
3152 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3153 struct got_commit_object *commit, struct got_object_id *commit_id,
3154 struct tog_view *log_view, struct got_repository *repo)
3156 const struct got_error *err;
3157 struct got_object_qid *parent_id;
3158 struct tog_view *diff_view;
3160 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3161 if (diff_view == NULL)
3162 return got_error_from_errno("view_open");
3164 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3165 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3166 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3168 *new_view = diff_view;
3172 static const struct got_error *
3173 tree_view_visit_subtree(struct tog_tree_view_state *s,
3174 struct got_tree_object *subtree)
3176 struct tog_parent_tree *parent;
3178 parent = calloc(1, sizeof(*parent));
3180 return got_error_from_errno("calloc");
3182 parent->tree = s->tree;
3183 parent->first_displayed_entry = s->first_displayed_entry;
3184 parent->selected_entry = s->selected_entry;
3185 parent->selected = s->selected;
3186 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3189 s->first_displayed_entry = NULL;
3193 static const struct got_error *
3194 tree_view_walk_path(struct tog_tree_view_state *s,
3195 struct got_commit_object *commit, const char *path)
3197 const struct got_error *err = NULL;
3198 struct got_tree_object *tree = NULL;
3200 char *slash, *subpath = NULL;
3202 /* Walk the path and open corresponding tree objects. */
3205 struct got_tree_entry *te;
3206 struct got_object_id *tree_id;
3212 /* Ensure the correct subtree entry is selected. */
3213 slash = strchr(p, '/');
3215 te_name = strdup(p);
3217 te_name = strndup(p, slash - p);
3218 if (te_name == NULL) {
3219 err = got_error_from_errno("strndup");
3222 te = got_object_tree_find_entry(s->tree, te_name);
3224 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3229 s->first_displayed_entry = s->selected_entry = te;
3231 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3232 break; /* jump to this file's entry */
3234 slash = strchr(p, '/');
3236 subpath = strndup(path, slash - path);
3238 subpath = strdup(path);
3239 if (subpath == NULL) {
3240 err = got_error_from_errno("strdup");
3244 err = got_object_id_by_path(&tree_id, s->repo, commit,
3249 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3254 err = tree_view_visit_subtree(s, tree);
3256 got_object_tree_close(tree);
3270 static const struct got_error *
3271 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3272 struct commit_queue_entry *entry, const char *path,
3273 const char *head_ref_name, struct got_repository *repo)
3275 const struct got_error *err = NULL;
3276 struct tog_tree_view_state *s;
3277 struct tog_view *tree_view;
3279 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3280 if (tree_view == NULL)
3281 return got_error_from_errno("view_open");
3283 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3286 s = &tree_view->state.tree;
3288 *new_view = tree_view;
3290 if (got_path_is_root_dir(path))
3293 return tree_view_walk_path(s, entry->commit, path);
3296 static const struct got_error *
3297 block_signals_used_by_main_thread(void)
3302 if (sigemptyset(&sigset) == -1)
3303 return got_error_from_errno("sigemptyset");
3305 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3306 if (sigaddset(&sigset, SIGWINCH) == -1)
3307 return got_error_from_errno("sigaddset");
3308 if (sigaddset(&sigset, SIGCONT) == -1)
3309 return got_error_from_errno("sigaddset");
3310 if (sigaddset(&sigset, SIGINT) == -1)
3311 return got_error_from_errno("sigaddset");
3312 if (sigaddset(&sigset, SIGTERM) == -1)
3313 return got_error_from_errno("sigaddset");
3315 /* ncurses handles SIGTSTP */
3316 if (sigaddset(&sigset, SIGTSTP) == -1)
3317 return got_error_from_errno("sigaddset");
3319 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3321 return got_error_set_errno(errcode, "pthread_sigmask");
3327 log_thread(void *arg)
3329 const struct got_error *err = NULL;
3331 struct tog_log_thread_args *a = arg;
3335 * Sync startup with main thread such that we begin our
3336 * work once view_input() has released the mutex.
3338 errcode = pthread_mutex_lock(&tog_mutex);
3340 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3344 err = block_signals_used_by_main_thread();
3346 pthread_mutex_unlock(&tog_mutex);
3350 while (!done && !err && !tog_fatal_signal_received()) {
3351 errcode = pthread_mutex_unlock(&tog_mutex);
3353 err = got_error_set_errno(errcode,
3354 "pthread_mutex_unlock");
3357 err = queue_commits(a);
3359 if (err->code != GOT_ERR_ITER_COMPLETED)
3363 a->commits_needed = 0;
3364 } else if (a->commits_needed > 0 && !a->load_all) {
3367 a->commits_needed--;
3369 a->commits_needed--;
3372 errcode = pthread_mutex_lock(&tog_mutex);
3374 err = got_error_set_errno(errcode,
3375 "pthread_mutex_lock");
3377 } else if (*a->quit)
3379 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3380 *a->first_displayed_entry =
3381 TAILQ_FIRST(&a->limit_commits->head);
3382 *a->selected_entry = *a->first_displayed_entry;
3383 } else if (*a->first_displayed_entry == NULL) {
3384 *a->first_displayed_entry =
3385 TAILQ_FIRST(&a->real_commits->head);
3386 *a->selected_entry = *a->first_displayed_entry;
3389 errcode = pthread_cond_signal(&a->commit_loaded);
3391 err = got_error_set_errno(errcode,
3392 "pthread_cond_signal");
3393 pthread_mutex_unlock(&tog_mutex);
3397 if (a->commits_needed == 0 &&
3398 a->need_commit_marker && a->worktree) {
3399 errcode = pthread_mutex_unlock(&tog_mutex);
3401 err = got_error_set_errno(errcode,
3402 "pthread_mutex_unlock");
3405 err = got_worktree_get_state(&tog_base_commit.marker,
3406 a->repo, a->worktree, NULL, NULL);
3409 errcode = pthread_mutex_lock(&tog_mutex);
3411 err = got_error_set_errno(errcode,
3412 "pthread_mutex_lock");
3415 a->need_commit_marker = 0;
3417 * The main thread did not close this
3418 * work tree yet. Close it now.
3420 got_worktree_close(a->worktree);
3428 a->commits_needed = 0;
3430 if (a->commits_needed == 0 && !a->load_all) {
3431 if (tog_io.wait_for_ui) {
3432 errcode = pthread_cond_signal(
3434 if (errcode && err == NULL)
3435 err = got_error_set_errno(
3437 "pthread_cond_signal");
3440 errcode = pthread_cond_wait(&a->need_commits,
3443 err = got_error_set_errno(errcode,
3444 "pthread_cond_wait");
3445 pthread_mutex_unlock(&tog_mutex);
3453 a->log_complete = 1;
3454 if (tog_io.wait_for_ui) {
3455 errcode = pthread_cond_signal(&a->log_loaded);
3456 if (errcode && err == NULL)
3457 err = got_error_set_errno(errcode,
3458 "pthread_cond_signal");
3461 errcode = pthread_mutex_unlock(&tog_mutex);
3463 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3466 tog_thread_error = 1;
3467 pthread_cond_signal(&a->commit_loaded);
3469 got_worktree_close(a->worktree);
3476 static const struct got_error *
3477 stop_log_thread(struct tog_log_view_state *s)
3479 const struct got_error *err = NULL, *thread_err = NULL;
3484 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3486 return got_error_set_errno(errcode,
3487 "pthread_cond_signal");
3488 errcode = pthread_mutex_unlock(&tog_mutex);
3490 return got_error_set_errno(errcode,
3491 "pthread_mutex_unlock");
3492 errcode = pthread_join(s->thread, (void **)&thread_err);
3494 return got_error_set_errno(errcode, "pthread_join");
3495 errcode = pthread_mutex_lock(&tog_mutex);
3497 return got_error_set_errno(errcode,
3498 "pthread_mutex_lock");
3499 s->thread = 0; //NULL;
3502 if (s->thread_args.repo) {
3503 err = got_repo_close(s->thread_args.repo);
3504 s->thread_args.repo = NULL;
3507 if (s->thread_args.pack_fds) {
3508 const struct got_error *pack_err =
3509 got_repo_pack_fds_close(s->thread_args.pack_fds);
3512 s->thread_args.pack_fds = NULL;
3515 if (s->thread_args.graph) {
3516 got_commit_graph_close(s->thread_args.graph);
3517 s->thread_args.graph = NULL;
3520 return err ? err : thread_err;
3523 static const struct got_error *
3524 close_log_view(struct tog_view *view)
3526 const struct got_error *err = NULL;
3527 struct tog_log_view_state *s = &view->state.log;
3530 err = stop_log_thread(s);
3532 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3533 if (errcode && err == NULL)
3534 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3536 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3537 if (errcode && err == NULL)
3538 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3540 free_commits(&s->limit_commits);
3541 free_commits(&s->real_commits);
3542 free_colors(&s->colors);
3543 free(s->in_repo_path);
3544 s->in_repo_path = NULL;
3547 free(s->head_ref_name);
3548 s->head_ref_name = NULL;
3553 * We use two queues to implement the limit feature: first consists of
3554 * commits matching the current limit_regex; second is the real queue
3555 * of all known commits (real_commits). When the user starts limiting,
3556 * we swap queues such that all movement and displaying functionality
3557 * works with very slight change.
3559 static const struct got_error *
3560 limit_log_view(struct tog_view *view)
3562 struct tog_log_view_state *s = &view->state.log;
3563 struct commit_queue_entry *entry;
3564 struct tog_view *v = view;
3565 const struct got_error *err = NULL;
3569 if (view_is_hsplit_top(view))
3571 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3574 if (tog_io.input_str != NULL) {
3575 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
3577 return got_error(GOT_ERR_NO_SPACE);
3579 wmove(v->window, v->nlines - 1, 0);
3580 wclrtoeol(v->window);
3581 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3582 nodelay(v->window, FALSE);
3585 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3588 nodelay(v->window, TRUE);
3593 if (*pattern == '\0') {
3595 * Safety measure for the situation where the user
3596 * resets limit without previously limiting anything.
3602 * User could have pressed Ctrl+L, which refreshed the
3603 * commit queues, it means we can't save previously
3604 * (before limit took place) displayed entries,
3605 * because they would point to already free'ed memory,
3606 * so we are forced to always select first entry of
3609 s->commits = &s->real_commits;
3610 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3611 s->selected_entry = s->first_displayed_entry;
3618 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3623 /* Clear the screen while loading limit view */
3624 s->first_displayed_entry = NULL;
3625 s->last_displayed_entry = NULL;
3626 s->selected_entry = NULL;
3627 s->commits = &s->limit_commits;
3629 /* Prepare limit queue for new search */
3630 free_commits(&s->limit_commits);
3631 s->limit_commits.ncommits = 0;
3633 /* First process commits, which are in queue already */
3634 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3637 err = match_commit(&have_match, entry->id,
3638 entry->commit, &s->limit_regex);
3643 struct commit_queue_entry *matched;
3645 matched = alloc_commit_queue_entry(entry->commit,
3647 if (matched == NULL) {
3648 err = got_error_from_errno(
3649 "alloc_commit_queue_entry");
3652 matched->commit = entry->commit;
3653 got_object_commit_retain(entry->commit);
3655 matched->idx = s->limit_commits.ncommits;
3656 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3658 s->limit_commits.ncommits++;
3662 /* Second process all the commits, until we fill the screen */
3663 if (s->limit_commits.ncommits < view->nlines - 1 &&
3664 !s->thread_args.log_complete) {
3665 s->thread_args.commits_needed +=
3666 view->nlines - s->limit_commits.ncommits - 1;
3667 err = trigger_log_thread(view, 1);
3672 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3673 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3679 static const struct got_error *
3680 search_start_log_view(struct tog_view *view)
3682 struct tog_log_view_state *s = &view->state.log;
3684 s->matched_entry = NULL;
3685 s->search_entry = NULL;
3689 static const struct got_error *
3690 search_next_log_view(struct tog_view *view)
3692 const struct got_error *err = NULL;
3693 struct tog_log_view_state *s = &view->state.log;
3694 struct commit_queue_entry *entry;
3696 /* Display progress update in log view. */
3697 show_log_view(view);
3701 if (s->search_entry) {
3702 if (!using_mock_io) {
3705 errcode = pthread_mutex_unlock(&tog_mutex);
3707 return got_error_set_errno(errcode,
3708 "pthread_mutex_unlock");
3709 ch = wgetch(view->window);
3710 errcode = pthread_mutex_lock(&tog_mutex);
3712 return got_error_set_errno(errcode,
3713 "pthread_mutex_lock");
3714 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3715 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3719 if (view->searching == TOG_SEARCH_FORWARD)
3720 entry = TAILQ_NEXT(s->search_entry, entry);
3722 entry = TAILQ_PREV(s->search_entry,
3723 commit_queue_head, entry);
3724 } else if (s->matched_entry) {
3726 * If the user has moved the cursor after we hit a match,
3727 * the position from where we should continue searching
3728 * might have changed.
3730 if (view->searching == TOG_SEARCH_FORWARD)
3731 entry = TAILQ_NEXT(s->selected_entry, entry);
3733 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3736 entry = s->selected_entry;
3742 if (entry == NULL) {
3743 if (s->thread_args.log_complete ||
3744 view->searching == TOG_SEARCH_BACKWARD) {
3745 view->search_next_done =
3746 (s->matched_entry == NULL ?
3747 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3748 s->search_entry = NULL;
3752 * Poke the log thread for more commits and return,
3753 * allowing the main loop to make progress. Search
3754 * will resume at s->search_entry once we come back.
3756 s->search_entry = s->selected_entry;
3757 s->thread_args.commits_needed++;
3758 return trigger_log_thread(view, 0);
3761 err = match_commit(&have_match, entry->id, entry->commit,
3766 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3767 s->matched_entry = entry;
3771 s->search_entry = entry;
3772 if (view->searching == TOG_SEARCH_FORWARD)
3773 entry = TAILQ_NEXT(entry, entry);
3775 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3778 if (s->matched_entry) {
3779 int cur = s->selected_entry->idx;
3780 while (cur < s->matched_entry->idx) {
3781 err = input_log_view(NULL, view, KEY_DOWN);
3786 while (cur > s->matched_entry->idx) {
3787 err = input_log_view(NULL, view, KEY_UP);
3794 s->search_entry = NULL;
3799 static const struct got_error *
3800 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3801 struct got_repository *repo, const char *head_ref_name,
3802 const char *in_repo_path, int log_branches,
3803 struct got_worktree *worktree)
3805 const struct got_error *err = NULL;
3806 struct tog_log_view_state *s = &view->state.log;
3807 struct got_repository *thread_repo = NULL;
3808 struct got_commit_graph *thread_graph = NULL;
3811 if (in_repo_path != s->in_repo_path) {
3812 free(s->in_repo_path);
3813 s->in_repo_path = strdup(in_repo_path);
3814 if (s->in_repo_path == NULL) {
3815 err = got_error_from_errno("strdup");
3820 /* The commit queue only contains commits being displayed. */
3821 TAILQ_INIT(&s->real_commits.head);
3822 s->real_commits.ncommits = 0;
3823 s->commits = &s->real_commits;
3825 TAILQ_INIT(&s->limit_commits.head);
3827 s->limit_commits.ncommits = 0;
3830 if (head_ref_name) {
3831 s->head_ref_name = strdup(head_ref_name);
3832 if (s->head_ref_name == NULL) {
3833 err = got_error_from_errno("strdup");
3837 s->start_id = got_object_id_dup(start_id);
3838 if (s->start_id == NULL) {
3839 err = got_error_from_errno("got_object_id_dup");
3842 s->log_branches = log_branches;
3843 s->use_committer = 1;
3845 STAILQ_INIT(&s->colors);
3846 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3847 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3848 get_color_value("TOG_COLOR_COMMIT"));
3851 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3852 get_color_value("TOG_COLOR_AUTHOR"));
3855 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3856 get_color_value("TOG_COLOR_DATE"));
3861 view->show = show_log_view;
3862 view->input = input_log_view;
3863 view->resize = resize_log_view;
3864 view->close = close_log_view;
3865 view->search_start = search_start_log_view;
3866 view->search_next = search_next_log_view;
3868 if (s->thread_args.pack_fds == NULL) {
3869 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3873 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3874 s->thread_args.pack_fds);
3877 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3881 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3882 s->repo, NULL, NULL);
3886 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3888 err = got_error_set_errno(errcode, "pthread_cond_init");
3891 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3893 err = got_error_set_errno(errcode, "pthread_cond_init");
3897 if (using_mock_io) {
3900 rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3902 return got_error_set_errno(rc, "pthread_cond_init");
3905 s->thread_args.commits_needed = view->nlines;
3906 s->thread_args.graph = thread_graph;
3907 s->thread_args.real_commits = &s->real_commits;
3908 s->thread_args.limit_commits = &s->limit_commits;
3909 s->thread_args.in_repo_path = s->in_repo_path;
3910 s->thread_args.start_id = s->start_id;
3911 s->thread_args.repo = thread_repo;
3912 s->thread_args.log_complete = 0;
3913 s->thread_args.quit = &s->quit;
3914 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3915 s->thread_args.selected_entry = &s->selected_entry;
3916 s->thread_args.searching = &view->searching;
3917 s->thread_args.search_next_done = &view->search_next_done;
3918 s->thread_args.regex = &view->regex;
3919 s->thread_args.limiting = &s->limit_view;
3920 s->thread_args.limit_regex = &s->limit_regex;
3921 s->thread_args.limit_commits = &s->limit_commits;
3922 s->thread_args.worktree = worktree;
3924 s->thread_args.need_commit_marker = 1;
3927 if (view->close == NULL)
3928 close_log_view(view);
3934 static const struct got_error *
3935 show_log_view(struct tog_view *view)
3937 const struct got_error *err;
3938 struct tog_log_view_state *s = &view->state.log;
3940 if (s->thread == 0) { //NULL) {
3941 int errcode = pthread_create(&s->thread, NULL, log_thread,
3944 return got_error_set_errno(errcode, "pthread_create");
3945 if (s->thread_args.commits_needed > 0) {
3946 err = trigger_log_thread(view, 1);
3952 return draw_commits(view);
3956 log_move_cursor_up(struct tog_view *view, int page, int home)
3958 struct tog_log_view_state *s = &view->state.log;
3960 if (s->first_displayed_entry == NULL)
3962 if (s->selected_entry->idx == 0)
3965 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3967 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3969 if (!page && !home && s->selected > 0)
3972 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3978 static const struct got_error *
3979 log_move_cursor_down(struct tog_view *view, int page)
3981 struct tog_log_view_state *s = &view->state.log;
3982 const struct got_error *err = NULL;
3983 int eos = view->nlines - 2;
3985 if (s->first_displayed_entry == NULL)
3988 if (s->thread_args.log_complete &&
3989 s->selected_entry->idx >= s->commits->ncommits - 1)
3992 if (view_is_hsplit_top(view))
3993 --eos; /* border consumes the last line */
3996 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3999 err = log_scroll_down(view, 1);
4000 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
4001 struct commit_queue_entry *entry;
4005 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
4006 s->last_displayed_entry = entry;
4007 for (n = 0; n <= eos; n++) {
4010 s->first_displayed_entry = entry;
4011 entry = TAILQ_PREV(entry, commit_queue_head, entry);
4014 s->selected = n - 1;
4016 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
4017 s->thread_args.log_complete)
4018 s->selected += MIN(page,
4019 s->commits->ncommits - s->selected_entry->idx - 1);
4021 err = log_scroll_down(view, page);
4027 * We might necessarily overshoot in horizontal
4028 * splits; if so, select the last displayed commit.
4030 if (s->first_displayed_entry && s->last_displayed_entry) {
4031 s->selected = MIN(s->selected,
4032 s->last_displayed_entry->idx -
4033 s->first_displayed_entry->idx);
4038 if (s->thread_args.log_complete &&
4039 s->selected_entry->idx == s->commits->ncommits - 1)
4046 view_get_split(struct tog_view *view, int *y, int *x)
4051 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4052 if (view->child && view->child->resized_y)
4053 *y = view->child->resized_y;
4054 else if (view->resized_y)
4055 *y = view->resized_y;
4057 *y = view_split_begin_y(view->lines);
4058 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4059 if (view->child && view->child->resized_x)
4060 *x = view->child->resized_x;
4061 else if (view->resized_x)
4062 *x = view->resized_x;
4064 *x = view_split_begin_x(view->begin_x);
4068 /* Split view horizontally at y and offset view->state->selected line. */
4069 static const struct got_error *
4070 view_init_hsplit(struct tog_view *view, int y)
4072 const struct got_error *err = NULL;
4076 err = view_resize(view);
4080 err = offset_selection_down(view);
4085 static const struct got_error *
4086 log_goto_line(struct tog_view *view, int nlines)
4088 const struct got_error *err = NULL;
4089 struct tog_log_view_state *s = &view->state.log;
4090 int g, idx = s->selected_entry->idx;
4092 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4098 if (g >= s->first_displayed_entry->idx + 1 &&
4099 g <= s->last_displayed_entry->idx + 1 &&
4100 g - s->first_displayed_entry->idx - 1 < nlines) {
4101 s->selected = g - s->first_displayed_entry->idx - 1;
4107 err = log_move_cursor_down(view, g - idx - 1);
4108 if (!err && g > s->selected_entry->idx + 1)
4109 err = log_move_cursor_down(view,
4110 g - s->first_displayed_entry->idx - 1);
4113 } else if (idx + 1 > g)
4114 log_move_cursor_up(view, idx - g + 1, 0);
4116 if (g < nlines && s->first_displayed_entry->idx == 0)
4117 s->selected = g - 1;
4125 horizontal_scroll_input(struct tog_view *view, int ch)
4131 view->x -= MIN(view->x, 2);
4137 if (view->x + view->ncols / 2 < view->maxx)
4146 view->x = MAX(view->maxx - view->ncols / 2, 0);
4154 static const struct got_error *
4155 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4157 const struct got_error *err = NULL;
4158 struct tog_log_view_state *s = &view->state.log;
4161 if (s->thread_args.load_all) {
4162 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4163 s->thread_args.load_all = 0;
4164 else if (s->thread_args.log_complete) {
4165 err = log_move_cursor_down(view, s->commits->ncommits);
4166 s->thread_args.load_all = 0;
4172 eos = nscroll = view->nlines - 1;
4173 if (view_is_hsplit_top(view))
4177 return log_goto_line(view, eos);
4181 err = limit_log_view(view);
4192 horizontal_scroll_input(view, ch);
4199 log_move_cursor_up(view, 0, 0);
4204 log_move_cursor_up(view, 0, 1);
4214 log_move_cursor_up(view, nscroll, 0);
4221 err = log_move_cursor_down(view, 0);
4224 s->use_committer = !s->use_committer;
4225 view->action = s->use_committer ?
4226 "show committer" : "show commit author";
4231 /* We don't know yet how many commits, so we're forced to
4232 * traverse them all. */
4234 s->thread_args.load_all = 1;
4235 if (!s->thread_args.log_complete)
4236 return trigger_log_thread(view, 0);
4237 err = log_move_cursor_down(view, s->commits->ncommits);
4238 s->thread_args.load_all = 0;
4249 err = log_move_cursor_down(view, nscroll);
4252 if (s->selected > view->nlines - 2)
4253 s->selected = view->nlines - 2;
4254 if (s->selected > s->commits->ncommits - 1)
4255 s->selected = s->commits->ncommits - 1;
4257 if (s->commits->ncommits < view->nlines - 1 &&
4258 !s->thread_args.log_complete) {
4259 s->thread_args.commits_needed += (view->nlines - 1) -
4260 s->commits->ncommits;
4261 err = trigger_log_thread(view, 1);
4267 if (s->selected_entry == NULL)
4269 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4273 if (s->selected_entry == NULL)
4275 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4281 if (ch == KEY_BACKSPACE &&
4282 got_path_is_root_dir(s->in_repo_path))
4284 err = stop_log_thread(s);
4287 if (ch == KEY_BACKSPACE) {
4289 err = got_path_dirname(&parent_path, s->in_repo_path);
4292 free(s->in_repo_path);
4293 s->in_repo_path = parent_path;
4294 s->thread_args.in_repo_path = s->in_repo_path;
4295 } else if (ch == CTRL('l')) {
4296 struct got_object_id *start_id;
4297 err = got_repo_match_object_id(&start_id, NULL,
4298 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4299 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4301 if (s->head_ref_name == NULL ||
4302 err->code != GOT_ERR_NOT_REF)
4304 /* Try to cope with deleted references. */
4305 free(s->head_ref_name);
4306 s->head_ref_name = NULL;
4307 err = got_repo_match_object_id(&start_id,
4308 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4309 &tog_refs, s->repo);
4314 s->start_id = start_id;
4315 s->thread_args.start_id = s->start_id;
4317 s->log_branches = !s->log_branches;
4319 if (s->thread_args.pack_fds == NULL) {
4320 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4324 err = got_repo_open(&s->thread_args.repo,
4325 got_repo_get_path(s->repo), NULL,
4326 s->thread_args.pack_fds);
4330 err = tog_load_refs(s->repo, 0);
4333 err = got_commit_graph_open(&s->thread_args.graph,
4334 s->in_repo_path, !s->log_branches);
4337 err = got_commit_graph_iter_start(s->thread_args.graph,
4338 s->start_id, s->repo, NULL, NULL);
4341 free_commits(&s->real_commits);
4342 free_commits(&s->limit_commits);
4343 s->first_displayed_entry = NULL;
4344 s->last_displayed_entry = NULL;
4345 s->selected_entry = NULL;
4347 s->thread_args.log_complete = 0;
4349 s->thread_args.commits_needed = view->lines;
4350 s->matched_entry = NULL;
4351 s->search_entry = NULL;
4356 err = view_request_new(new_view, view, TOG_VIEW_REF);
4366 static const struct got_error *
4367 apply_unveil(const char *repo_path, const char *worktree_path)
4369 const struct got_error *error;
4372 if (unveil("gmon.out", "rwc") != 0)
4373 return got_error_from_errno2("unveil", "gmon.out");
4375 if (repo_path && unveil(repo_path, "r") != 0)
4376 return got_error_from_errno2("unveil", repo_path);
4378 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4379 return got_error_from_errno2("unveil", worktree_path);
4381 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4382 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4384 error = got_privsep_unveil_exec_helpers();
4388 if (unveil(NULL, NULL) != 0)
4389 return got_error_from_errno("unveil");
4394 static const struct got_error *
4395 init_mock_term(const char *test_script_path)
4397 const struct got_error *err = NULL;
4398 const char *screen_dump_path;
4401 if (test_script_path == NULL || *test_script_path == '\0')
4402 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4404 tog_io.f = fopen(test_script_path, "re");
4405 if (tog_io.f == NULL) {
4406 err = got_error_from_errno_fmt("fopen: %s",
4411 /* test mode, we don't want any output */
4412 tog_io.cout = fopen("/dev/null", "w+");
4413 if (tog_io.cout == NULL) {
4414 err = got_error_from_errno2("fopen", "/dev/null");
4418 in = dup(fileno(tog_io.cout));
4420 err = got_error_from_errno("dup");
4423 tog_io.cin = fdopen(in, "r");
4424 if (tog_io.cin == NULL) {
4425 err = got_error_from_errno("fdopen");
4430 screen_dump_path = getenv("TOG_SCR_DUMP");
4431 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4432 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4433 tog_io.sdump = fopen(screen_dump_path, "we");
4434 if (tog_io.sdump == NULL) {
4435 err = got_error_from_errno2("fopen", screen_dump_path);
4439 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4440 err = got_error_from_errno("fseeko");
4444 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4445 err = got_error_msg(GOT_ERR_IO,
4446 "newterm: failed to initialise curses");
4459 if (using_mock_io) /* In test mode we use a fake terminal */
4465 halfdelay(1); /* Fast refresh while initial view is loading. */
4468 intrflush(stdscr, FALSE);
4469 keypad(stdscr, TRUE);
4471 if (getenv("TOG_COLORS") != NULL) {
4473 use_default_colors();
4479 static const struct got_error *
4480 set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4482 tog_base_commit.id = got_object_id_dup(
4483 got_worktree_get_base_commit_id(worktree));
4484 if (tog_base_commit.id == NULL)
4485 return got_error_from_errno( "got_object_id_dup");
4490 static const struct got_error *
4491 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4492 struct got_repository *repo, struct got_worktree *worktree)
4494 const struct got_error *err = NULL;
4497 *in_repo_path = strdup("/");
4498 if (*in_repo_path == NULL)
4499 return got_error_from_errno("strdup");
4504 const char *prefix = got_worktree_get_path_prefix(worktree);
4507 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4510 if (asprintf(in_repo_path, "%s%s%s", prefix,
4511 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4513 err = got_error_from_errno("asprintf");
4514 *in_repo_path = NULL;
4518 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4523 static const struct got_error *
4524 cmd_log(int argc, char *argv[])
4526 const struct got_error *error;
4527 struct got_repository *repo = NULL;
4528 struct got_worktree *worktree = NULL;
4529 struct got_object_id *start_id = NULL;
4530 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4531 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4532 struct got_reference *ref = NULL;
4533 const char *head_ref_name = NULL;
4534 int ch, log_branches = 0;
4535 struct tog_view *view;
4536 int *pack_fds = NULL;
4538 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4544 start_commit = optarg;
4547 repo_path = realpath(optarg, NULL);
4548 if (repo_path == NULL)
4549 return got_error_from_errno2("realpath",
4564 error = got_repo_pack_fds_open(&pack_fds);
4568 if (repo_path == NULL) {
4569 cwd = getcwd(NULL, 0);
4571 error = got_error_from_errno("getcwd");
4574 error = got_worktree_open(&worktree, cwd, NULL);
4575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4579 strdup(got_worktree_get_repo_path(worktree));
4581 repo_path = strdup(cwd);
4582 if (repo_path == NULL) {
4583 error = got_error_from_errno("strdup");
4588 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4592 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4599 error = apply_unveil(got_repo_get_path(repo),
4600 worktree ? got_worktree_get_root_path(worktree) : NULL);
4604 /* already loaded by tog_log_with_path()? */
4605 if (TAILQ_EMPTY(&tog_refs)) {
4606 error = tog_load_refs(repo, 0);
4611 if (start_commit == NULL) {
4612 error = got_repo_match_object_id(&start_id, &label,
4613 worktree ? got_worktree_get_head_ref_name(worktree) :
4614 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4617 head_ref_name = label;
4619 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4623 if (keyword_idstr != NULL)
4624 start_commit = keyword_idstr;
4626 error = got_ref_open(&ref, repo, start_commit, 0);
4628 head_ref_name = got_ref_get_name(ref);
4629 else if (error->code != GOT_ERR_NOT_REF)
4631 error = got_repo_match_object_id(&start_id, NULL,
4632 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4637 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4639 error = got_error_from_errno("view_open");
4644 error = set_tog_base_commit(repo, worktree);
4649 error = open_log_view(view, start_id, repo, head_ref_name,
4650 in_repo_path, log_branches, worktree);
4655 /* The work tree will be closed by the log thread. */
4659 error = view_loop(view);
4662 free(tog_base_commit.id);
4663 free(keyword_idstr);
4672 const struct got_error *close_err = got_repo_close(repo);
4677 got_worktree_close(worktree);
4679 const struct got_error *pack_err =
4680 got_repo_pack_fds_close(pack_fds);
4692 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4693 "object1 object2\n", getprogname());
4698 match_line(const char *line, regex_t *regex, size_t nmatch,
4699 regmatch_t *regmatch)
4701 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4704 static struct tog_color *
4705 match_color(struct tog_colors *colors, const char *line)
4707 struct tog_color *tc = NULL;
4709 STAILQ_FOREACH(tc, colors, entry) {
4710 if (match_line(line, &tc->regex, 0, NULL))
4717 static const struct got_error *
4718 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4719 WINDOW *window, int skipcol, regmatch_t *regmatch)
4721 const struct got_error *err = NULL;
4723 wchar_t *wline = NULL;
4724 int rme, rms, n, width, scrollx;
4725 int width0 = 0, width1 = 0, width2 = 0;
4726 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4730 rms = regmatch->rm_so;
4731 rme = regmatch->rm_eo;
4733 err = expand_tab(&exstr, line);
4737 /* Split the line into 3 segments, according to match offsets. */
4738 seg0 = strndup(exstr, rms);
4740 err = got_error_from_errno("strndup");
4743 seg1 = strndup(exstr + rms, rme - rms);
4745 err = got_error_from_errno("strndup");
4748 seg2 = strdup(exstr + rme);
4750 err = got_error_from_errno("strndup");
4754 /* draw up to matched token if we haven't scrolled past it */
4755 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4759 n = MAX(width0 - skipcol, 0);
4762 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4763 wlimit, col_tab_align, 1);
4766 waddwstr(window, &wline[scrollx]);
4776 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4780 wlen = wcslen(wline);
4782 width = wcwidth(wline[i]);
4784 /* should not happen, tabs are expanded */
4785 err = got_error(GOT_ERR_RANGE);
4788 if (width0 + w + width > skipcol)
4793 /* draw (visible part of) matched token (if scrolled into it) */
4794 if (width1 - w > 0) {
4795 wattron(window, A_STANDOUT);
4796 waddwstr(window, &wline[i]);
4797 wattroff(window, A_STANDOUT);
4798 wlimit -= (width1 - w);
4799 *wtotal += (width1 - w);
4803 if (wlimit > 0) { /* draw rest of line */
4805 if (skipcol > width0 + width1) {
4806 err = format_line(&wline, &width2, &scrollx, seg2,
4807 skipcol - (width0 + width1), wlimit,
4811 waddwstr(window, &wline[scrollx]);
4813 err = format_line(&wline, &width2, NULL, seg2, 0,
4814 wlimit, col_tab_align, 1);
4817 waddwstr(window, wline);
4831 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4834 int *eof, *first, *selected;
4836 if (view->type == TOG_VIEW_DIFF) {
4837 struct tog_diff_view_state *s = &view->state.diff;
4839 first = &s->first_displayed_line;
4843 } else if (view->type == TOG_VIEW_HELP) {
4844 struct tog_help_view_state *s = &view->state.help;
4846 first = &s->first_displayed_line;
4850 } else if (view->type == TOG_VIEW_BLAME) {
4851 struct tog_blame_view_state *s = &view->state.blame;
4853 first = &s->first_displayed_line;
4854 selected = &s->selected_line;
4860 /* Center gline in the middle of the page like vi(1). */
4861 if (*lineno < view->gline - (view->nlines - 3) / 2)
4863 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4872 *selected = view->gline <= (view->nlines - 3) / 2 ?
4873 view->gline : (view->nlines - 3) / 2 + 1;
4879 static const struct got_error *
4880 draw_file(struct tog_view *view, const char *header)
4882 struct tog_diff_view_state *s = &view->state.diff;
4883 regmatch_t *regmatch = &view->regmatch;
4884 const struct got_error *err;
4887 size_t linesize = 0;
4891 int max_lines = view->nlines;
4892 int nlines = s->nlines;
4895 s->lineno = s->first_displayed_line - 1;
4896 line_offset = s->lines[s->first_displayed_line - 1].offset;
4897 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4898 return got_error_from_errno("fseek");
4900 werase(view->window);
4902 if (view->gline > s->nlines - 1)
4903 view->gline = s->nlines - 1;
4906 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4907 1 : view->gline - (view->nlines - 3) / 2 :
4908 s->lineno + s->selected_line;
4910 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4911 return got_error_from_errno("asprintf");
4912 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4918 if (view_needs_focus_indication(view))
4919 wstandout(view->window);
4920 waddwstr(view->window, wline);
4923 while (width++ < view->ncols)
4924 waddch(view->window, ' ');
4925 if (view_needs_focus_indication(view))
4926 wstandend(view->window);
4936 while (max_lines > 0 && nprinted < max_lines) {
4937 enum got_diff_line_type linetype;
4940 linelen = getline(&line, &linesize, s->f);
4941 if (linelen == -1) {
4947 return got_ferror(s->f, GOT_ERR_IO);
4950 if (++s->lineno < s->first_displayed_line)
4952 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4954 if (s->lineno == view->hiline)
4957 /* Set view->maxx based on full line length. */
4958 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4964 view->maxx = MAX(view->maxx, width);
4968 linetype = s->lines[s->lineno].type;
4969 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4970 linetype < GOT_DIFF_LINE_CONTEXT)
4971 attr |= COLOR_PAIR(linetype);
4973 wattron(view->window, attr);
4974 if (s->first_displayed_line + nprinted == s->matched_line &&
4975 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4976 err = add_matched_line(&width, line, view->ncols, 0,
4977 view->window, view->x, regmatch);
4984 err = format_line(&wline, &width, &skip, line,
4985 view->x, view->ncols, 0, view->x ? 1 : 0);
4990 waddwstr(view->window, &wline[skip]);
4994 if (s->lineno == view->hiline) {
4995 /* highlight full gline length */
4996 while (width++ < view->ncols)
4997 waddch(view->window, ' ');
4999 if (width <= view->ncols - 1)
5000 waddch(view->window, '\n');
5003 wattroff(view->window, attr);
5004 if (++nprinted == 1)
5005 s->first_displayed_line = s->lineno;
5009 s->last_displayed_line = s->first_displayed_line +
5012 s->last_displayed_line = s->first_displayed_line;
5017 while (nprinted < view->nlines) {
5018 waddch(view->window, '\n');
5022 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
5028 wstandout(view->window);
5029 waddwstr(view->window, wline);
5032 wstandend(view->window);
5039 get_datestr(time_t *time, char *datebuf)
5041 struct tm mytm, *tm;
5044 tm = gmtime_r(time, &mytm);
5047 s = asctime_r(tm, datebuf);
5050 p = strchr(s, '\n');
5056 static const struct got_error *
5057 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5058 off_t off, uint8_t type)
5060 struct got_diff_line *p;
5062 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5064 return got_error_from_errno("reallocarray");
5066 (*lines)[*nlines].offset = off;
5067 (*lines)[*nlines].type = type;
5073 static const struct got_error *
5074 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5075 struct got_diff_line *s_lines, size_t s_nlines)
5077 struct got_diff_line *p;
5081 if (fseeko(src, 0L, SEEK_SET) == -1)
5082 return got_error_from_errno("fseeko");
5085 r = fread(buf, 1, sizeof(buf), src);
5088 return got_error_from_errno("fread");
5092 if (fwrite(buf, 1, r, dst) != r)
5093 return got_ferror(dst, GOT_ERR_IO);
5096 if (s_nlines == 0 && *d_nlines == 0)
5100 * If commit info was in dst, increment line offsets
5101 * of the appended diff content, but skip s_lines[0]
5102 * because offset zero is already in *d_lines.
5104 if (*d_nlines > 0) {
5105 for (i = 1; i < s_nlines; ++i)
5106 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5114 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5116 /* d_lines is freed in close_diff_view() */
5117 return got_error_from_errno("reallocarray");
5122 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5123 *d_nlines += s_nlines;
5128 static const struct got_error *
5129 write_commit_info(struct got_diff_line **lines, size_t *nlines,
5130 struct got_object_id *commit_id, struct got_reflist_head *refs,
5131 struct got_repository *repo, int ignore_ws, int force_text_diff,
5132 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5134 const struct got_error *err = NULL;
5135 char datebuf[26], *datestr;
5136 struct got_commit_object *commit;
5137 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5138 time_t committer_time;
5139 const char *author, *committer;
5140 char *refs_str = NULL;
5141 struct got_pathlist_entry *pe;
5145 err = build_refs_str(&refs_str, refs, commit_id, repo);
5149 err = got_object_open_as_commit(&commit, repo, commit_id);
5153 err = got_object_id_str(&id_str, commit_id);
5155 err = got_error_from_errno("got_object_id_str");
5159 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5163 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5164 refs_str ? refs_str : "", refs_str ? ")" : "");
5166 err = got_error_from_errno("fprintf");
5170 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5174 n = fprintf(outfile, "from: %s\n",
5175 got_object_commit_get_author(commit));
5177 err = got_error_from_errno("fprintf");
5181 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5185 author = got_object_commit_get_author(commit);
5186 committer = got_object_commit_get_committer(commit);
5187 if (strcmp(author, committer) != 0) {
5188 n = fprintf(outfile, "via: %s\n", committer);
5190 err = got_error_from_errno("fprintf");
5194 err = add_line_metadata(lines, nlines, outoff,
5195 GOT_DIFF_LINE_AUTHOR);
5199 committer_time = got_object_commit_get_committer_time(commit);
5200 datestr = get_datestr(&committer_time, datebuf);
5202 n = fprintf(outfile, "date: %s UTC\n", datestr);
5204 err = got_error_from_errno("fprintf");
5208 err = add_line_metadata(lines, nlines, outoff,
5209 GOT_DIFF_LINE_DATE);
5213 if (got_object_commit_get_nparents(commit) > 1) {
5214 const struct got_object_id_queue *parent_ids;
5215 struct got_object_qid *qid;
5217 parent_ids = got_object_commit_get_parent_ids(commit);
5218 STAILQ_FOREACH(qid, parent_ids, entry) {
5219 err = got_object_id_str(&id_str, &qid->id);
5222 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5224 err = got_error_from_errno("fprintf");
5228 err = add_line_metadata(lines, nlines, outoff,
5229 GOT_DIFF_LINE_META);
5237 err = got_object_commit_get_logmsg(&logmsg, commit);
5241 while ((line = strsep(&s, "\n")) != NULL) {
5242 n = fprintf(outfile, "%s\n", line);
5244 err = got_error_from_errno("fprintf");
5248 err = add_line_metadata(lines, nlines, outoff,
5249 GOT_DIFF_LINE_LOGMSG);
5254 TAILQ_FOREACH(pe, dsa->paths, entry) {
5255 struct got_diff_changed_path *cp = pe->data;
5256 int pad = dsa->max_path_len - pe->path_len + 1;
5258 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5259 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5260 dsa->rm_cols + 1, cp->rm);
5262 err = got_error_from_errno("fprintf");
5266 err = add_line_metadata(lines, nlines, outoff,
5267 GOT_DIFF_LINE_CHANGES);
5272 fputc('\n', outfile);
5274 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5278 n = fprintf(outfile,
5279 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5280 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5281 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5283 err = got_error_from_errno("fprintf");
5287 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5291 fputc('\n', outfile);
5293 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5298 got_object_commit_close(commit);
5307 static const struct got_error *
5308 create_diff(struct tog_diff_view_state *s)
5310 const struct got_error *err = NULL;
5311 FILE *f = NULL, *tmp_diff_file = NULL;
5313 struct got_diff_line *lines = NULL;
5314 struct got_pathlist_head changed_paths;
5315 struct got_commit_object *commit2 = NULL;
5317 TAILQ_INIT(&changed_paths);
5320 s->lines = malloc(sizeof(*s->lines));
5321 if (s->lines == NULL)
5322 return got_error_from_errno("malloc");
5327 err = got_error_from_errno("got_opentemp");
5330 tmp_diff_file = got_opentemp();
5331 if (tmp_diff_file == NULL) {
5332 err = got_error_from_errno("got_opentemp");
5335 if (s->f && fclose(s->f) == EOF) {
5336 err = got_error_from_errno("fclose");
5342 err = got_object_get_type(&obj_type, s->repo, s->id1);
5344 err = got_object_get_type(&obj_type, s->repo, s->id2);
5349 case GOT_OBJ_TYPE_BLOB:
5350 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5351 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5352 s->label1, s->label2, tog_diff_algo, s->diff_context,
5353 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5356 case GOT_OBJ_TYPE_TREE:
5357 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5358 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5359 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5360 s->force_text_diff, NULL, s->repo, s->f);
5362 case GOT_OBJ_TYPE_COMMIT: {
5363 const struct got_object_id_queue *parent_ids;
5364 struct got_object_qid *pid;
5365 struct got_reflist_head *refs;
5367 struct got_diffstat_cb_arg dsa = {
5370 s->ignore_whitespace,
5375 lines = malloc(sizeof(*lines));
5376 if (lines == NULL) {
5377 err = got_error_from_errno("malloc");
5381 /* build diff first in tmp file then append to commit info */
5382 err = got_diff_objects_as_commits(&lines, &nlines,
5383 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5384 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5385 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5389 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5390 /* Show commit info if we're diffing to a parent/root commit. */
5391 if (s->id1 == NULL) {
5392 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5393 refs, s->repo, s->ignore_whitespace,
5394 s->force_text_diff, &dsa, s->f);
5398 err = got_object_open_as_commit(&commit2, s->repo,
5403 parent_ids = got_object_commit_get_parent_ids(commit2);
5404 STAILQ_FOREACH(pid, parent_ids, entry) {
5405 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5406 err = write_commit_info(&s->lines,
5407 &s->nlines, s->id2, refs, s->repo,
5408 s->ignore_whitespace,
5409 s->force_text_diff, &dsa, s->f);
5417 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5422 err = got_error(GOT_ERR_OBJ_TYPE);
5427 if (commit2 != NULL)
5428 got_object_commit_close(commit2);
5429 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5430 if (s->f && fflush(s->f) != 0 && err == NULL)
5431 err = got_error_from_errno("fflush");
5432 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5433 err = got_error_from_errno("fclose");
5438 diff_view_indicate_progress(struct tog_view *view)
5440 mvwaddstr(view->window, 0, 0, "diffing...");
5445 static const struct got_error *
5446 search_start_diff_view(struct tog_view *view)
5448 struct tog_diff_view_state *s = &view->state.diff;
5450 s->matched_line = 0;
5455 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5456 size_t *nlines, int **first, int **last, int **match, int **selected)
5458 struct tog_diff_view_state *s = &view->state.diff;
5461 *nlines = s->nlines;
5462 *line_offsets = NULL;
5463 *match = &s->matched_line;
5464 *first = &s->first_displayed_line;
5465 *last = &s->last_displayed_line;
5466 *selected = &s->selected_line;
5469 static const struct got_error *
5470 search_next_view_match(struct tog_view *view)
5472 const struct got_error *err = NULL;
5476 size_t linesize = 0;
5478 off_t *line_offsets;
5480 int *first, *last, *match, *selected;
5482 if (!view->search_setup)
5483 return got_error_msg(GOT_ERR_NOT_IMPL,
5484 "view search not supported");
5485 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5488 if (!view->searching) {
5489 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5494 if (view->searching == TOG_SEARCH_FORWARD)
5495 lineno = *first + 1;
5497 lineno = *first - 1;
5499 lineno = *first - 1 + *selected;
5504 if (lineno <= 0 || lineno > nlines) {
5506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5510 if (view->searching == TOG_SEARCH_FORWARD)
5516 offset = view->type == TOG_VIEW_DIFF ?
5517 view->state.diff.lines[lineno - 1].offset :
5518 line_offsets[lineno - 1];
5519 if (fseeko(f, offset, SEEK_SET) != 0) {
5521 return got_error_from_errno("fseeko");
5523 linelen = getline(&line, &linesize, f);
5524 if (linelen != -1) {
5526 err = expand_tab(&exstr, line);
5529 if (match_line(exstr, &view->regex, 1,
5531 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5538 if (view->searching == TOG_SEARCH_FORWARD)
5553 static const struct got_error *
5554 close_diff_view(struct tog_view *view)
5556 const struct got_error *err = NULL;
5557 struct tog_diff_view_state *s = &view->state.diff;
5563 if (s->f && fclose(s->f) == EOF)
5564 err = got_error_from_errno("fclose");
5566 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5567 err = got_error_from_errno("fclose");
5569 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5570 err = got_error_from_errno("fclose");
5572 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5573 err = got_error_from_errno("close");
5575 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5576 err = got_error_from_errno("close");
5584 static const struct got_error *
5585 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5586 struct got_object_id *id2, const char *label1, const char *label2,
5587 int diff_context, int ignore_whitespace, int force_text_diff,
5588 struct tog_view *parent_view, struct got_repository *repo)
5590 const struct got_error *err;
5591 struct tog_diff_view_state *s = &view->state.diff;
5593 memset(s, 0, sizeof(*s));
5597 if (id1 != NULL && id2 != NULL) {
5600 err = got_object_get_type(&type1, repo, id1);
5603 err = got_object_get_type(&type2, repo, id2);
5607 if (type1 != type2) {
5608 err = got_error(GOT_ERR_OBJ_TYPE);
5612 s->first_displayed_line = 1;
5613 s->last_displayed_line = view->nlines;
5614 s->selected_line = 1;
5620 s->id1 = got_object_id_dup(id1);
5621 if (s->id1 == NULL) {
5622 err = got_error_from_errno("got_object_id_dup");
5628 s->id2 = got_object_id_dup(id2);
5629 if (s->id2 == NULL) {
5630 err = got_error_from_errno("got_object_id_dup");
5634 s->f1 = got_opentemp();
5635 if (s->f1 == NULL) {
5636 err = got_error_from_errno("got_opentemp");
5640 s->f2 = got_opentemp();
5641 if (s->f2 == NULL) {
5642 err = got_error_from_errno("got_opentemp");
5646 s->fd1 = got_opentempfd();
5648 err = got_error_from_errno("got_opentempfd");
5652 s->fd2 = got_opentempfd();
5654 err = got_error_from_errno("got_opentempfd");
5658 s->diff_context = diff_context;
5659 s->ignore_whitespace = ignore_whitespace;
5660 s->force_text_diff = force_text_diff;
5661 s->parent_view = parent_view;
5664 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5667 rc = init_pair(GOT_DIFF_LINE_MINUS,
5668 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5670 rc = init_pair(GOT_DIFF_LINE_PLUS,
5671 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5673 rc = init_pair(GOT_DIFF_LINE_HUNK,
5674 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5676 rc = init_pair(GOT_DIFF_LINE_META,
5677 get_color_value("TOG_COLOR_DIFF_META"), -1);
5679 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5680 get_color_value("TOG_COLOR_DIFF_META"), -1);
5682 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5683 get_color_value("TOG_COLOR_DIFF_META"), -1);
5685 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5686 get_color_value("TOG_COLOR_DIFF_META"), -1);
5688 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5689 get_color_value("TOG_COLOR_AUTHOR"), -1);
5691 rc = init_pair(GOT_DIFF_LINE_DATE,
5692 get_color_value("TOG_COLOR_DATE"), -1);
5694 err = got_error(GOT_ERR_RANGE);
5699 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5700 view_is_splitscreen(view))
5701 show_log_view(parent_view); /* draw border */
5702 diff_view_indicate_progress(view);
5704 err = create_diff(s);
5706 view->show = show_diff_view;
5707 view->input = input_diff_view;
5708 view->reset = reset_diff_view;
5709 view->close = close_diff_view;
5710 view->search_start = search_start_diff_view;
5711 view->search_setup = search_setup_diff_view;
5712 view->search_next = search_next_view_match;
5715 if (view->close == NULL)
5716 close_diff_view(view);
5722 static const struct got_error *
5723 show_diff_view(struct tog_view *view)
5725 const struct got_error *err;
5726 struct tog_diff_view_state *s = &view->state.diff;
5727 char *id_str1 = NULL, *id_str2, *header;
5728 const char *label1, *label2;
5731 err = got_object_id_str(&id_str1, s->id1);
5734 label1 = s->label1 ? s->label1 : id_str1;
5736 label1 = "/dev/null";
5738 err = got_object_id_str(&id_str2, s->id2);
5741 label2 = s->label2 ? s->label2 : id_str2;
5743 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5744 err = got_error_from_errno("asprintf");
5752 err = draw_file(view, header);
5757 static const struct got_error *
5758 set_selected_commit(struct tog_diff_view_state *s,
5759 struct commit_queue_entry *entry)
5761 const struct got_error *err;
5762 const struct got_object_id_queue *parent_ids;
5763 struct got_commit_object *selected_commit;
5764 struct got_object_qid *pid;
5767 s->id2 = got_object_id_dup(entry->id);
5769 return got_error_from_errno("got_object_id_dup");
5771 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5774 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5776 pid = STAILQ_FIRST(parent_ids);
5777 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5778 got_object_commit_close(selected_commit);
5782 static const struct got_error *
5783 reset_diff_view(struct tog_view *view)
5785 struct tog_diff_view_state *s = &view->state.diff;
5788 wclear(view->window);
5789 s->first_displayed_line = 1;
5790 s->last_displayed_line = view->nlines;
5791 s->matched_line = 0;
5792 diff_view_indicate_progress(view);
5793 return create_diff(s);
5797 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5801 i = start = s->first_displayed_line - 1;
5803 while (s->lines[i].type != type) {
5807 return; /* do nothing, requested type not in file */
5810 s->selected_line = 1;
5811 s->first_displayed_line = i;
5815 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5819 i = start = s->first_displayed_line + 1;
5821 while (s->lines[i].type != type) {
5822 if (i == s->nlines - 1)
5825 return; /* do nothing, requested type not in file */
5828 s->selected_line = 1;
5829 s->first_displayed_line = i;
5832 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5834 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5837 static const struct got_error *
5838 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5840 const struct got_error *err = NULL;
5841 struct tog_diff_view_state *s = &view->state.diff;
5842 struct tog_log_view_state *ls;
5843 struct commit_queue_entry *old_selected_entry;
5845 size_t linesize = 0;
5847 int i, nscroll = view->nlines - 1, up = 0;
5849 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5858 horizontal_scroll_input(view, ch);
5863 s->force_text_diff = !s->force_text_diff;
5864 view->action = s->force_text_diff ?
5865 "force ASCII text enabled" :
5866 "force ASCII text disabled";
5868 else if (ch == 'w') {
5869 s->ignore_whitespace = !s->ignore_whitespace;
5870 view->action = s->ignore_whitespace ?
5871 "ignore whitespace enabled" :
5872 "ignore whitespace disabled";
5874 err = reset_diff_view(view);
5878 s->first_displayed_line = 1;
5887 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5893 if (s->first_displayed_line > 1)
5894 s->first_displayed_line--;
5905 if (s->first_displayed_line == 1) {
5910 while (i++ < nscroll && s->first_displayed_line > 1)
5911 s->first_displayed_line--;
5917 s->first_displayed_line++;
5934 while (!s->eof && i++ < nscroll) {
5935 linelen = getline(&line, &linesize, s->f);
5936 s->first_displayed_line++;
5937 if (linelen == -1) {
5941 err = got_ferror(s->f, GOT_ERR_IO);
5948 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5951 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5954 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5957 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5960 if (s->diff_context > 0) {
5962 s->matched_line = 0;
5963 diff_view_indicate_progress(view);
5964 err = create_diff(s);
5965 if (s->first_displayed_line + view->nlines - 1 >
5967 s->first_displayed_line = 1;
5968 s->last_displayed_line = view->nlines;
5974 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5976 s->matched_line = 0;
5977 diff_view_indicate_progress(view);
5978 err = create_diff(s);
5990 if (s->parent_view == NULL) {
5994 s->parent_view->count = view->count;
5996 if (s->parent_view->type == TOG_VIEW_LOG) {
5997 ls = &s->parent_view->state.log;
5998 old_selected_entry = ls->selected_entry;
6000 err = input_log_view(NULL, s->parent_view,
6001 up ? KEY_UP : KEY_DOWN);
6004 view->count = s->parent_view->count;
6006 if (old_selected_entry == ls->selected_entry)
6009 err = set_selected_commit(s, ls->selected_entry);
6012 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
6013 struct tog_blame_view_state *bs;
6014 struct got_object_id *id, *prev_id;
6016 bs = &s->parent_view->state.blame;
6017 prev_id = get_annotation_for_line(bs->blame.lines,
6018 bs->blame.nlines, bs->last_diffed_line);
6020 err = input_blame_view(&view, s->parent_view,
6021 up ? KEY_UP : KEY_DOWN);
6024 view->count = s->parent_view->count;
6026 if (prev_id == NULL)
6028 id = get_selected_commit_id(bs->blame.lines,
6029 bs->blame.nlines, bs->first_displayed_line,
6034 if (!got_object_id_cmp(prev_id, id))
6037 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6041 s->first_displayed_line = 1;
6042 s->last_displayed_line = view->nlines;
6043 s->matched_line = 0;
6046 diff_view_indicate_progress(view);
6047 err = create_diff(s);
6057 static const struct got_error *
6058 cmd_diff(int argc, char *argv[])
6060 const struct got_error *error;
6061 struct got_repository *repo = NULL;
6062 struct got_worktree *worktree = NULL;
6063 struct got_object_id *id1 = NULL, *id2 = NULL;
6064 char *repo_path = NULL, *cwd = NULL;
6065 char *id_str1 = NULL, *id_str2 = NULL;
6066 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6067 char *label1 = NULL, *label2 = NULL;
6068 int diff_context = 3, ignore_whitespace = 0;
6069 int ch, force_text_diff = 0;
6071 struct tog_view *view;
6072 int *pack_fds = NULL;
6074 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6077 force_text_diff = 1;
6080 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6083 errx(1, "number of context lines is %s: %s",
6087 repo_path = realpath(optarg, NULL);
6088 if (repo_path == NULL)
6089 return got_error_from_errno2("realpath",
6091 got_path_strip_trailing_slashes(repo_path);
6094 ignore_whitespace = 1;
6106 usage_diff(); /* TODO show local worktree changes */
6107 } else if (argc == 2) {
6113 error = got_repo_pack_fds_open(&pack_fds);
6117 if (repo_path == NULL) {
6118 cwd = getcwd(NULL, 0);
6120 return got_error_from_errno("getcwd");
6121 error = got_worktree_open(&worktree, cwd, NULL);
6122 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6126 strdup(got_worktree_get_repo_path(worktree));
6128 repo_path = strdup(cwd);
6129 if (repo_path == NULL) {
6130 error = got_error_from_errno("strdup");
6135 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6141 error = apply_unveil(got_repo_get_path(repo), NULL);
6145 error = tog_load_refs(repo, 0);
6149 if (id_str1 != NULL) {
6150 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6154 if (keyword_idstr1 != NULL)
6155 id_str1 = keyword_idstr1;
6157 if (id_str2 != NULL) {
6158 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6162 if (keyword_idstr2 != NULL)
6163 id_str2 = keyword_idstr2;
6166 error = got_repo_match_object_id(&id1, &label1, id_str1,
6167 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6171 error = got_repo_match_object_id(&id2, &label2, id_str2,
6172 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6176 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6178 error = got_error_from_errno("view_open");
6181 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6182 ignore_whitespace, force_text_diff, NULL, repo);
6187 error = set_tog_base_commit(repo, worktree);
6191 /* Release work tree lock. */
6192 got_worktree_close(worktree);
6196 error = view_loop(view);
6199 free(tog_base_commit.id);
6200 free(keyword_idstr1);
6201 free(keyword_idstr2);
6209 const struct got_error *close_err = got_repo_close(repo);
6214 got_worktree_close(worktree);
6216 const struct got_error *pack_err =
6217 got_repo_pack_fds_close(pack_fds);
6230 "usage: %s blame [-c commit] [-r repository-path] path\n",
6235 struct tog_blame_line {
6237 struct got_object_id *id;
6240 static const struct got_error *
6241 draw_blame(struct tog_view *view)
6243 struct tog_blame_view_state *s = &view->state.blame;
6244 struct tog_blame *blame = &s->blame;
6245 regmatch_t *regmatch = &view->regmatch;
6246 const struct got_error *err;
6247 int lineno = 0, nprinted = 0;
6249 size_t linesize = 0;
6253 struct tog_blame_line *blame_line;
6254 struct got_object_id *prev_id = NULL;
6256 struct tog_color *tc;
6258 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6263 werase(view->window);
6265 if (asprintf(&line, "commit %s", id_str) == -1) {
6266 err = got_error_from_errno("asprintf");
6271 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6276 if (view_needs_focus_indication(view))
6277 wstandout(view->window);
6278 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6280 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6281 waddwstr(view->window, wline);
6282 while (width++ < view->ncols)
6283 waddch(view->window, ' ');
6285 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6286 if (view_needs_focus_indication(view))
6287 wstandend(view->window);
6291 if (view->gline > blame->nlines)
6292 view->gline = blame->nlines;
6294 if (tog_io.wait_for_ui) {
6295 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6298 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6300 return got_error_set_errno(rc, "pthread_cond_wait");
6301 tog_io.wait_for_ui = 0;
6304 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6305 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6306 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6308 return got_error_from_errno("asprintf");
6311 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6316 waddwstr(view->window, wline);
6319 if (width < view->ncols - 1)
6320 waddch(view->window, '\n');
6324 while (nprinted < view->nlines - 2) {
6325 linelen = getline(&line, &linesize, blame->f);
6326 if (linelen == -1) {
6327 if (feof(blame->f)) {
6332 return got_ferror(blame->f, GOT_ERR_IO);
6334 if (++lineno < s->first_displayed_line)
6336 if (view->gline && !gotoline(view, &lineno, &nprinted))
6339 /* Set view->maxx based on full line length. */
6340 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6347 view->maxx = MAX(view->maxx, width);
6349 if (nprinted == s->selected_line - 1)
6350 wstandout(view->window);
6352 if (blame->nlines > 0) {
6353 blame_line = &blame->lines[lineno - 1];
6354 if (blame_line->annotated && prev_id &&
6355 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6356 !(nprinted == s->selected_line - 1)) {
6357 waddstr(view->window, " ");
6358 } else if (blame_line->annotated) {
6360 err = got_object_id_str(&id_str,
6366 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6368 wattr_on(view->window,
6369 COLOR_PAIR(tc->colorpair), NULL);
6370 wprintw(view->window, "%.8s", id_str);
6372 wattr_off(view->window,
6373 COLOR_PAIR(tc->colorpair), NULL);
6375 prev_id = blame_line->id;
6377 waddstr(view->window, "........");
6381 waddstr(view->window, "........");
6385 if (nprinted == s->selected_line - 1)
6386 wstandend(view->window);
6387 waddstr(view->window, " ");
6389 if (view->ncols <= 9) {
6391 } else if (s->first_displayed_line + nprinted ==
6393 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6394 err = add_matched_line(&width, line, view->ncols - 9, 9,
6395 view->window, view->x, regmatch);
6403 err = format_line(&wline, &width, &skip, line,
6404 view->x, view->ncols - 9, 9, 1);
6409 waddwstr(view->window, &wline[skip]);
6415 if (width <= view->ncols - 1)
6416 waddch(view->window, '\n');
6417 if (++nprinted == 1)
6418 s->first_displayed_line = lineno;
6421 s->last_displayed_line = lineno;
6428 static const struct got_error *
6429 blame_cb(void *arg, int nlines, int lineno,
6430 struct got_commit_object *commit, struct got_object_id *id)
6432 const struct got_error *err = NULL;
6433 struct tog_blame_cb_args *a = arg;
6434 struct tog_blame_line *line;
6437 if (nlines != a->nlines ||
6438 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6439 return got_error(GOT_ERR_RANGE);
6441 errcode = pthread_mutex_lock(&tog_mutex);
6443 return got_error_set_errno(errcode, "pthread_mutex_lock");
6445 if (*a->quit) { /* user has quit the blame view */
6446 err = got_error(GOT_ERR_ITER_COMPLETED);
6451 goto done; /* no change in this commit */
6453 line = &a->lines[lineno - 1];
6454 if (line->annotated)
6457 line->id = got_object_id_dup(id);
6458 if (line->id == NULL) {
6459 err = got_error_from_errno("got_object_id_dup");
6462 line->annotated = 1;
6464 errcode = pthread_mutex_unlock(&tog_mutex);
6466 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6471 blame_thread(void *arg)
6473 const struct got_error *err, *close_err;
6474 struct tog_blame_thread_args *ta = arg;
6475 struct tog_blame_cb_args *a = ta->cb_args;
6476 int errcode, fd1 = -1, fd2 = -1;
6477 FILE *f1 = NULL, *f2 = NULL;
6479 fd1 = got_opentempfd();
6481 return (void *)got_error_from_errno("got_opentempfd");
6483 fd2 = got_opentempfd();
6485 err = got_error_from_errno("got_opentempfd");
6489 f1 = got_opentemp();
6491 err = (void *)got_error_from_errno("got_opentemp");
6494 f2 = got_opentemp();
6496 err = (void *)got_error_from_errno("got_opentemp");
6500 err = block_signals_used_by_main_thread();
6504 err = got_blame(ta->path, a->commit_id, ta->repo,
6505 tog_diff_algo, blame_cb, ta->cb_args,
6506 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6507 if (err && err->code == GOT_ERR_CANCELLED)
6510 errcode = pthread_mutex_lock(&tog_mutex);
6512 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6516 close_err = got_repo_close(ta->repo);
6522 if (tog_io.wait_for_ui) {
6523 errcode = pthread_cond_signal(&ta->blame_complete);
6524 if (errcode && err == NULL)
6525 err = got_error_set_errno(errcode,
6526 "pthread_cond_signal");
6529 errcode = pthread_mutex_unlock(&tog_mutex);
6530 if (errcode && err == NULL)
6531 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6534 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6535 err = got_error_from_errno("close");
6536 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6537 err = got_error_from_errno("close");
6538 if (f1 && fclose(f1) == EOF && err == NULL)
6539 err = got_error_from_errno("fclose");
6540 if (f2 && fclose(f2) == EOF && err == NULL)
6541 err = got_error_from_errno("fclose");
6546 static struct got_object_id *
6547 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6548 int first_displayed_line, int selected_line)
6550 struct tog_blame_line *line;
6555 line = &lines[first_displayed_line - 1 + selected_line - 1];
6556 if (!line->annotated)
6562 static struct got_object_id *
6563 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6566 struct tog_blame_line *line;
6568 if (nlines <= 0 || lineno >= nlines)
6571 line = &lines[lineno - 1];
6572 if (!line->annotated)
6578 static const struct got_error *
6579 stop_blame(struct tog_blame *blame)
6581 const struct got_error *err = NULL;
6584 if (blame->thread) {
6586 errcode = pthread_mutex_unlock(&tog_mutex);
6588 return got_error_set_errno(errcode,
6589 "pthread_mutex_unlock");
6590 errcode = pthread_join(blame->thread, (void **)&err);
6592 return got_error_set_errno(errcode, "pthread_join");
6593 errcode = pthread_mutex_lock(&tog_mutex);
6595 return got_error_set_errno(errcode,
6596 "pthread_mutex_lock");
6597 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6599 blame->thread = 0; //NULL;
6601 if (blame->thread_args.repo) {
6602 const struct got_error *close_err;
6603 close_err = got_repo_close(blame->thread_args.repo);
6606 blame->thread_args.repo = NULL;
6609 if (fclose(blame->f) == EOF && err == NULL)
6610 err = got_error_from_errno("fclose");
6614 for (i = 0; i < blame->nlines; i++)
6615 free(blame->lines[i].id);
6617 blame->lines = NULL;
6619 free(blame->cb_args.commit_id);
6620 blame->cb_args.commit_id = NULL;
6621 if (blame->pack_fds) {
6622 const struct got_error *pack_err =
6623 got_repo_pack_fds_close(blame->pack_fds);
6626 blame->pack_fds = NULL;
6628 free(blame->line_offsets);
6629 blame->line_offsets = NULL;
6633 static const struct got_error *
6634 cancel_blame_view(void *arg)
6636 const struct got_error *err = NULL;
6640 errcode = pthread_mutex_lock(&tog_mutex);
6642 return got_error_set_errno(errcode,
6643 "pthread_mutex_unlock");
6646 err = got_error(GOT_ERR_CANCELLED);
6648 errcode = pthread_mutex_unlock(&tog_mutex);
6650 return got_error_set_errno(errcode,
6651 "pthread_mutex_lock");
6656 static const struct got_error *
6657 run_blame(struct tog_view *view)
6659 struct tog_blame_view_state *s = &view->state.blame;
6660 struct tog_blame *blame = &s->blame;
6661 const struct got_error *err = NULL;
6662 struct got_commit_object *commit = NULL;
6663 struct got_blob_object *blob = NULL;
6664 struct got_repository *thread_repo = NULL;
6665 struct got_object_id *obj_id = NULL;
6666 int obj_type, fd = -1;
6667 int *pack_fds = NULL;
6669 err = got_object_open_as_commit(&commit, s->repo,
6670 &s->blamed_commit->id);
6674 fd = got_opentempfd();
6676 err = got_error_from_errno("got_opentempfd");
6680 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6684 err = got_object_get_type(&obj_type, s->repo, obj_id);
6688 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6689 err = got_error(GOT_ERR_OBJ_TYPE);
6693 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6696 blame->f = got_opentemp();
6697 if (blame->f == NULL) {
6698 err = got_error_from_errno("got_opentemp");
6701 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6702 &blame->line_offsets, blame->f, blob);
6705 if (blame->nlines == 0) {
6706 s->blame_complete = 1;
6710 /* Don't include \n at EOF in the blame line count. */
6711 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6714 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6715 if (blame->lines == NULL) {
6716 err = got_error_from_errno("calloc");
6720 err = got_repo_pack_fds_open(&pack_fds);
6723 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6728 blame->pack_fds = pack_fds;
6729 blame->cb_args.view = view;
6730 blame->cb_args.lines = blame->lines;
6731 blame->cb_args.nlines = blame->nlines;
6732 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6733 if (blame->cb_args.commit_id == NULL) {
6734 err = got_error_from_errno("got_object_id_dup");
6737 blame->cb_args.quit = &s->done;
6739 blame->thread_args.path = s->path;
6740 blame->thread_args.repo = thread_repo;
6741 blame->thread_args.cb_args = &blame->cb_args;
6742 blame->thread_args.complete = &s->blame_complete;
6743 blame->thread_args.cancel_cb = cancel_blame_view;
6744 blame->thread_args.cancel_arg = &s->done;
6745 s->blame_complete = 0;
6747 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6748 s->first_displayed_line = 1;
6749 s->last_displayed_line = view->nlines;
6750 s->selected_line = 1;
6752 s->matched_line = 0;
6756 got_object_commit_close(commit);
6757 if (fd != -1 && close(fd) == -1 && err == NULL)
6758 err = got_error_from_errno("close");
6760 got_object_blob_close(blob);
6767 static const struct got_error *
6768 open_blame_view(struct tog_view *view, char *path,
6769 struct got_object_id *commit_id, struct got_repository *repo)
6771 const struct got_error *err = NULL;
6772 struct tog_blame_view_state *s = &view->state.blame;
6774 STAILQ_INIT(&s->blamed_commits);
6776 s->path = strdup(path);
6777 if (s->path == NULL)
6778 return got_error_from_errno("strdup");
6780 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6786 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6787 s->first_displayed_line = 1;
6788 s->last_displayed_line = view->nlines;
6789 s->selected_line = 1;
6790 s->blame_complete = 0;
6792 s->commit_id = commit_id;
6793 memset(&s->blame, 0, sizeof(s->blame));
6795 STAILQ_INIT(&s->colors);
6796 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6797 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6798 get_color_value("TOG_COLOR_COMMIT"));
6803 view->show = show_blame_view;
6804 view->input = input_blame_view;
6805 view->reset = reset_blame_view;
6806 view->close = close_blame_view;
6807 view->search_start = search_start_blame_view;
6808 view->search_setup = search_setup_blame_view;
6809 view->search_next = search_next_view_match;
6811 if (using_mock_io) {
6812 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6815 rc = pthread_cond_init(&bta->blame_complete, NULL);
6817 return got_error_set_errno(rc, "pthread_cond_init");
6820 return run_blame(view);
6823 static const struct got_error *
6824 close_blame_view(struct tog_view *view)
6826 const struct got_error *err = NULL;
6827 struct tog_blame_view_state *s = &view->state.blame;
6829 if (s->blame.thread)
6830 err = stop_blame(&s->blame);
6832 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6833 struct got_object_qid *blamed_commit;
6834 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6835 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6836 got_object_qid_free(blamed_commit);
6839 if (using_mock_io) {
6840 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6843 rc = pthread_cond_destroy(&bta->blame_complete);
6844 if (rc && err == NULL)
6845 err = got_error_set_errno(rc, "pthread_cond_destroy");
6849 free_colors(&s->colors);
6853 static const struct got_error *
6854 search_start_blame_view(struct tog_view *view)
6856 struct tog_blame_view_state *s = &view->state.blame;
6858 s->matched_line = 0;
6863 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6864 size_t *nlines, int **first, int **last, int **match, int **selected)
6866 struct tog_blame_view_state *s = &view->state.blame;
6869 *nlines = s->blame.nlines;
6870 *line_offsets = s->blame.line_offsets;
6871 *match = &s->matched_line;
6872 *first = &s->first_displayed_line;
6873 *last = &s->last_displayed_line;
6874 *selected = &s->selected_line;
6877 static const struct got_error *
6878 show_blame_view(struct tog_view *view)
6880 const struct got_error *err = NULL;
6881 struct tog_blame_view_state *s = &view->state.blame;
6884 if (s->blame.thread == 0 && !s->blame_complete) {
6885 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6886 &s->blame.thread_args);
6888 return got_error_set_errno(errcode, "pthread_create");
6891 halfdelay(1); /* fast refresh while annotating */
6894 if (s->blame_complete && !using_mock_io)
6895 halfdelay(10); /* disable fast refresh */
6897 err = draw_blame(view);
6903 static const struct got_error *
6904 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6905 struct got_repository *repo, struct got_object_id *id)
6907 struct tog_view *log_view;
6908 const struct got_error *err = NULL;
6912 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6913 if (log_view == NULL)
6914 return got_error_from_errno("view_open");
6916 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6918 view_close(log_view);
6920 *new_view = log_view;
6925 static const struct got_error *
6926 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6928 const struct got_error *err = NULL, *thread_err = NULL;
6929 struct tog_view *diff_view;
6930 struct tog_blame_view_state *s = &view->state.blame;
6931 int eos, nscroll, begin_y = 0, begin_x = 0;
6933 eos = nscroll = view->nlines - 2;
6934 if (view_is_hsplit_top(view))
6944 horizontal_scroll_input(view, ch);
6951 s->selected_line = 1;
6952 s->first_displayed_line = 1;
6957 if (s->blame.nlines < eos) {
6958 s->selected_line = s->blame.nlines;
6959 s->first_displayed_line = 1;
6961 s->selected_line = eos;
6962 s->first_displayed_line = s->blame.nlines - (eos - 1);
6969 if (s->selected_line > 1)
6971 else if (s->selected_line == 1 &&
6972 s->first_displayed_line > 1)
6973 s->first_displayed_line--;
6984 if (s->first_displayed_line == 1) {
6985 if (view->count > 1)
6987 s->selected_line = MAX(1, s->selected_line - nscroll);
6991 if (s->first_displayed_line > nscroll)
6992 s->first_displayed_line -= nscroll;
6994 s->first_displayed_line = 1;
6999 if (s->selected_line < eos && s->first_displayed_line +
7000 s->selected_line <= s->blame.nlines)
7002 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
7003 s->first_displayed_line++;
7009 struct got_object_id *id = NULL;
7012 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7013 s->first_displayed_line, s->selected_line);
7017 struct got_commit_object *commit, *pcommit;
7018 struct got_object_qid *pid;
7019 struct got_object_id *blob_id = NULL;
7021 err = got_object_open_as_commit(&commit,
7026 got_object_commit_get_parent_ids(commit));
7028 got_object_commit_close(commit);
7031 /* Check if path history ends here. */
7032 err = got_object_open_as_commit(&pcommit,
7036 err = got_object_id_by_path(&blob_id, s->repo,
7038 got_object_commit_close(pcommit);
7040 if (err->code == GOT_ERR_NO_TREE_ENTRY)
7042 got_object_commit_close(commit);
7045 err = got_object_get_type(&obj_type, s->repo,
7048 /* Can't blame non-blob type objects. */
7049 if (obj_type != GOT_OBJ_TYPE_BLOB) {
7050 got_object_commit_close(commit);
7053 err = got_object_qid_alloc(&s->blamed_commit,
7055 got_object_commit_close(commit);
7057 if (got_object_id_cmp(id,
7058 &s->blamed_commit->id) == 0)
7060 err = got_object_qid_alloc(&s->blamed_commit,
7066 thread_err = stop_blame(&s->blame);
7070 STAILQ_INSERT_HEAD(&s->blamed_commits,
7071 s->blamed_commit, entry);
7072 err = run_blame(view);
7078 struct got_object_qid *first;
7081 first = STAILQ_FIRST(&s->blamed_commits);
7082 if (!got_object_id_cmp(&first->id, s->commit_id))
7085 thread_err = stop_blame(&s->blame);
7089 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7090 got_object_qid_free(s->blamed_commit);
7092 STAILQ_FIRST(&s->blamed_commits);
7093 err = run_blame(view);
7100 s->id_to_log = get_selected_commit_id(s->blame.lines,
7101 s->blame.nlines, s->first_displayed_line, s->selected_line);
7103 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7107 struct got_object_id *id = NULL;
7108 struct got_object_qid *pid;
7109 struct got_commit_object *commit = NULL;
7112 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7113 s->first_displayed_line, s->selected_line);
7116 err = got_object_open_as_commit(&commit, s->repo, id);
7119 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7121 /* traversed from diff view, release diff resources */
7122 err = close_diff_view(*new_view);
7125 diff_view = *new_view;
7127 if (view_is_parent_view(view))
7128 view_get_split(view, &begin_y, &begin_x);
7130 diff_view = view_open(0, 0, begin_y, begin_x,
7132 if (diff_view == NULL) {
7133 got_object_commit_close(commit);
7134 err = got_error_from_errno("view_open");
7138 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7139 id, NULL, NULL, 3, 0, 0, view, s->repo);
7140 got_object_commit_close(commit);
7143 s->last_diffed_line = s->first_displayed_line - 1 +
7146 break; /* still open from active diff view */
7147 if (view_is_parent_view(view) &&
7148 view->mode == TOG_VIEW_SPLIT_HRZN) {
7149 err = view_init_hsplit(view, begin_y);
7155 diff_view->focussed = 1;
7156 diff_view->mode = view->mode;
7157 diff_view->nlines = view->lines - begin_y;
7158 if (view_is_parent_view(view)) {
7159 view_transfer_size(diff_view, view);
7160 err = view_close_child(view);
7163 err = view_set_child(view, diff_view);
7166 view->focus_child = 1;
7168 *new_view = diff_view;
7181 if (s->last_displayed_line >= s->blame.nlines &&
7182 s->selected_line >= MIN(s->blame.nlines,
7183 view->nlines - 2)) {
7187 if (s->last_displayed_line >= s->blame.nlines &&
7188 s->selected_line < view->nlines - 2) {
7190 MIN(nscroll, s->last_displayed_line -
7191 s->first_displayed_line - s->selected_line + 1);
7193 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7194 s->first_displayed_line += nscroll;
7196 s->first_displayed_line =
7197 s->blame.nlines - (view->nlines - 3);
7200 if (s->selected_line > view->nlines - 2) {
7201 s->selected_line = MIN(s->blame.nlines,
7209 return thread_err ? thread_err : err;
7212 static const struct got_error *
7213 reset_blame_view(struct tog_view *view)
7215 const struct got_error *err;
7216 struct tog_blame_view_state *s = &view->state.blame;
7220 err = stop_blame(&s->blame);
7224 return run_blame(view);
7227 static const struct got_error *
7228 cmd_blame(int argc, char *argv[])
7230 const struct got_error *error;
7231 struct got_repository *repo = NULL;
7232 struct got_worktree *worktree = NULL;
7233 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7234 char *link_target = NULL;
7235 struct got_object_id *commit_id = NULL;
7236 struct got_commit_object *commit = NULL;
7237 char *keyword_idstr = NULL, *commit_id_str = NULL;
7239 struct tog_view *view = NULL;
7240 int *pack_fds = NULL;
7242 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7245 commit_id_str = optarg;
7248 repo_path = realpath(optarg, NULL);
7249 if (repo_path == NULL)
7250 return got_error_from_errno2("realpath",
7265 error = got_repo_pack_fds_open(&pack_fds);
7269 if (repo_path == NULL) {
7270 cwd = getcwd(NULL, 0);
7272 return got_error_from_errno("getcwd");
7273 error = got_worktree_open(&worktree, cwd, NULL);
7274 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7278 strdup(got_worktree_get_repo_path(worktree));
7280 repo_path = strdup(cwd);
7281 if (repo_path == NULL) {
7282 error = got_error_from_errno("strdup");
7287 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7291 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7298 error = apply_unveil(got_repo_get_path(repo), NULL);
7302 error = tog_load_refs(repo, 0);
7306 if (commit_id_str == NULL) {
7307 struct got_reference *head_ref;
7308 error = got_ref_open(&head_ref, repo, worktree ?
7309 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7312 error = got_ref_resolve(&commit_id, repo, head_ref);
7313 got_ref_close(head_ref);
7315 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7319 if (keyword_idstr != NULL)
7320 commit_id_str = keyword_idstr;
7322 error = got_repo_match_object_id(&commit_id, NULL,
7323 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7328 error = got_object_open_as_commit(&commit, repo, commit_id);
7332 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7337 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7339 error = got_error_from_errno("view_open");
7342 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7344 if (error != NULL) {
7345 if (view->close == NULL)
7346 close_blame_view(view);
7352 error = set_tog_base_commit(repo, worktree);
7356 /* Release work tree lock. */
7357 got_worktree_close(worktree);
7361 error = view_loop(view);
7364 free(tog_base_commit.id);
7370 free(keyword_idstr);
7372 got_object_commit_close(commit);
7374 got_worktree_close(worktree);
7376 const struct got_error *close_err = got_repo_close(repo);
7381 const struct got_error *pack_err =
7382 got_repo_pack_fds_close(pack_fds);
7390 static const struct got_error *
7391 draw_tree_entries(struct tog_view *view, const char *parent_path)
7393 struct tog_tree_view_state *s = &view->state.tree;
7394 const struct got_error *err = NULL;
7395 struct got_tree_entry *te;
7398 struct tog_color *tc;
7399 int width, n, nentries, scrollx, i = 1;
7400 int limit = view->nlines;
7403 if (view_is_hsplit_top(view))
7404 --limit; /* border */
7406 werase(view->window);
7411 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7415 if (view_needs_focus_indication(view))
7416 wstandout(view->window);
7417 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7419 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7420 waddwstr(view->window, wline);
7423 while (width++ < view->ncols)
7424 waddch(view->window, ' ');
7426 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7427 if (view_needs_focus_indication(view))
7428 wstandend(view->window);
7433 if (s->first_displayed_entry) {
7434 i += got_tree_entry_get_index(s->first_displayed_entry);
7435 if (s->tree != s->root)
7436 ++i; /* account for ".." entry */
7438 nentries = got_object_tree_get_nentries(s->tree);
7439 if (asprintf(&index, "[%d/%d] %s",
7440 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7441 return got_error_from_errno("asprintf");
7442 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7446 waddwstr(view->window, wline);
7449 if (width < view->ncols - 1)
7450 waddch(view->window, '\n');
7453 waddch(view->window, '\n');
7457 if (s->first_displayed_entry == NULL) {
7458 te = got_object_tree_get_first_entry(s->tree);
7459 if (s->selected == 0) {
7461 wstandout(view->window);
7462 s->selected_entry = NULL;
7464 waddstr(view->window, " ..\n"); /* parent directory */
7465 if (s->selected == 0 && view->focussed)
7466 wstandend(view->window);
7473 te = s->first_displayed_entry;
7477 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7478 char *line = NULL, *id_str = NULL, *link_target = NULL;
7479 const char *modestr = "";
7482 te = got_object_tree_get_entry(s->tree, i);
7483 mode = got_tree_entry_get_mode(te);
7486 err = got_object_id_str(&id_str,
7487 got_tree_entry_get_id(te));
7489 return got_error_from_errno(
7490 "got_object_id_str");
7492 if (got_object_tree_entry_is_submodule(te))
7494 else if (S_ISLNK(mode)) {
7497 err = got_tree_entry_get_symlink_target(&link_target,
7503 for (i = 0; link_target[i] != '\0'; i++) {
7504 if (!isprint((unsigned char)link_target[i]))
7505 link_target[i] = '?';
7509 else if (S_ISDIR(mode))
7511 else if (mode & S_IXUSR)
7513 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7514 got_tree_entry_get_name(te), modestr,
7515 link_target ? " -> ": "",
7516 link_target ? link_target : "") == -1) {
7519 return got_error_from_errno("asprintf");
7524 /* use full line width to determine view->maxx */
7525 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7530 view->maxx = MAX(view->maxx, width);
7534 err = format_line(&wline, &width, &scrollx, line, view->x,
7540 if (n == s->selected) {
7542 wstandout(view->window);
7543 s->selected_entry = te;
7545 tc = match_color(&s->colors, line);
7547 wattr_on(view->window,
7548 COLOR_PAIR(tc->colorpair), NULL);
7549 waddwstr(view->window, &wline[scrollx]);
7551 wattr_off(view->window,
7552 COLOR_PAIR(tc->colorpair), NULL);
7553 if (width < view->ncols)
7554 waddch(view->window, '\n');
7555 if (n == s->selected && view->focussed)
7556 wstandend(view->window);
7562 s->last_displayed_entry = te;
7571 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7573 struct got_tree_entry *te;
7574 int isroot = s->tree == s->root;
7577 if (s->first_displayed_entry == NULL)
7580 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7581 while (i++ < maxscroll) {
7584 s->first_displayed_entry = NULL;
7587 s->first_displayed_entry = te;
7588 te = got_tree_entry_get_prev(s->tree, te);
7592 static const struct got_error *
7593 tree_scroll_down(struct tog_view *view, int maxscroll)
7595 struct tog_tree_view_state *s = &view->state.tree;
7596 struct got_tree_entry *next, *last;
7599 if (s->first_displayed_entry)
7600 next = got_tree_entry_get_next(s->tree,
7601 s->first_displayed_entry);
7603 next = got_object_tree_get_first_entry(s->tree);
7605 last = s->last_displayed_entry;
7606 while (next && n++ < maxscroll) {
7608 s->last_displayed_entry = last;
7609 last = got_tree_entry_get_next(s->tree, last);
7611 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7612 s->first_displayed_entry = next;
7613 next = got_tree_entry_get_next(s->tree, next);
7620 static const struct got_error *
7621 tree_entry_path(char **path, struct tog_parent_trees *parents,
7622 struct got_tree_entry *te)
7624 const struct got_error *err = NULL;
7625 struct tog_parent_tree *pt;
7626 size_t len = 2; /* for leading slash and NUL */
7628 TAILQ_FOREACH(pt, parents, entry)
7629 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7632 len += strlen(got_tree_entry_get_name(te));
7634 *path = calloc(1, len);
7636 return got_error_from_errno("calloc");
7639 pt = TAILQ_LAST(parents, tog_parent_trees);
7641 const char *name = got_tree_entry_get_name(pt->selected_entry);
7642 if (strlcat(*path, name, len) >= len) {
7643 err = got_error(GOT_ERR_NO_SPACE);
7646 if (strlcat(*path, "/", len) >= len) {
7647 err = got_error(GOT_ERR_NO_SPACE);
7650 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7653 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7654 err = got_error(GOT_ERR_NO_SPACE);
7666 static const struct got_error *
7667 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7668 struct got_tree_entry *te, struct tog_parent_trees *parents,
7669 struct got_object_id *commit_id, struct got_repository *repo)
7671 const struct got_error *err = NULL;
7673 struct tog_view *blame_view;
7677 err = tree_entry_path(&path, parents, te);
7681 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7682 if (blame_view == NULL) {
7683 err = got_error_from_errno("view_open");
7687 err = open_blame_view(blame_view, path, commit_id, repo);
7689 if (err->code == GOT_ERR_CANCELLED)
7691 view_close(blame_view);
7693 *new_view = blame_view;
7699 static const struct got_error *
7700 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7701 struct tog_tree_view_state *s)
7703 struct tog_view *log_view;
7704 const struct got_error *err = NULL;
7709 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7710 if (log_view == NULL)
7711 return got_error_from_errno("view_open");
7713 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7717 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7720 view_close(log_view);
7722 *new_view = log_view;
7727 static const struct got_error *
7728 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7729 const char *head_ref_name, struct got_repository *repo)
7731 const struct got_error *err = NULL;
7732 char *commit_id_str = NULL;
7733 struct tog_tree_view_state *s = &view->state.tree;
7734 struct got_commit_object *commit = NULL;
7736 TAILQ_INIT(&s->parents);
7737 STAILQ_INIT(&s->colors);
7739 s->commit_id = got_object_id_dup(commit_id);
7740 if (s->commit_id == NULL) {
7741 err = got_error_from_errno("got_object_id_dup");
7745 err = got_object_open_as_commit(&commit, repo, commit_id);
7750 * The root is opened here and will be closed when the view is closed.
7751 * Any visited subtrees and their path-wise parents are opened and
7754 err = got_object_open_as_tree(&s->root, repo,
7755 got_object_commit_get_tree_id(commit));
7760 err = got_object_id_str(&commit_id_str, commit_id);
7764 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7765 err = got_error_from_errno("asprintf");
7769 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7770 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7771 if (head_ref_name) {
7772 s->head_ref_name = strdup(head_ref_name);
7773 if (s->head_ref_name == NULL) {
7774 err = got_error_from_errno("strdup");
7780 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7781 err = add_color(&s->colors, "\\$$",
7782 TOG_COLOR_TREE_SUBMODULE,
7783 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7786 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7787 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7790 err = add_color(&s->colors, "/$",
7791 TOG_COLOR_TREE_DIRECTORY,
7792 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7796 err = add_color(&s->colors, "\\*$",
7797 TOG_COLOR_TREE_EXECUTABLE,
7798 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7802 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7803 get_color_value("TOG_COLOR_COMMIT"));
7808 view->show = show_tree_view;
7809 view->input = input_tree_view;
7810 view->close = close_tree_view;
7811 view->search_start = search_start_tree_view;
7812 view->search_next = search_next_tree_view;
7814 free(commit_id_str);
7816 got_object_commit_close(commit);
7818 if (view->close == NULL)
7819 close_tree_view(view);
7825 static const struct got_error *
7826 close_tree_view(struct tog_view *view)
7828 struct tog_tree_view_state *s = &view->state.tree;
7830 free_colors(&s->colors);
7831 free(s->tree_label);
7832 s->tree_label = NULL;
7834 s->commit_id = NULL;
7835 free(s->head_ref_name);
7836 s->head_ref_name = NULL;
7837 while (!TAILQ_EMPTY(&s->parents)) {
7838 struct tog_parent_tree *parent;
7839 parent = TAILQ_FIRST(&s->parents);
7840 TAILQ_REMOVE(&s->parents, parent, entry);
7841 if (parent->tree != s->root)
7842 got_object_tree_close(parent->tree);
7846 if (s->tree != NULL && s->tree != s->root)
7847 got_object_tree_close(s->tree);
7849 got_object_tree_close(s->root);
7853 static const struct got_error *
7854 search_start_tree_view(struct tog_view *view)
7856 struct tog_tree_view_state *s = &view->state.tree;
7858 s->matched_entry = NULL;
7863 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7865 regmatch_t regmatch;
7867 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7871 static const struct got_error *
7872 search_next_tree_view(struct tog_view *view)
7874 struct tog_tree_view_state *s = &view->state.tree;
7875 struct got_tree_entry *te = NULL;
7877 if (!view->searching) {
7878 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7882 if (s->matched_entry) {
7883 if (view->searching == TOG_SEARCH_FORWARD) {
7884 if (s->selected_entry)
7885 te = got_tree_entry_get_next(s->tree,
7888 te = got_object_tree_get_first_entry(s->tree);
7890 if (s->selected_entry == NULL)
7891 te = got_object_tree_get_last_entry(s->tree);
7893 te = got_tree_entry_get_prev(s->tree,
7897 if (s->selected_entry)
7898 te = s->selected_entry;
7899 else if (view->searching == TOG_SEARCH_FORWARD)
7900 te = got_object_tree_get_first_entry(s->tree);
7902 te = got_object_tree_get_last_entry(s->tree);
7907 if (s->matched_entry == NULL) {
7908 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7911 if (view->searching == TOG_SEARCH_FORWARD)
7912 te = got_object_tree_get_first_entry(s->tree);
7914 te = got_object_tree_get_last_entry(s->tree);
7917 if (match_tree_entry(te, &view->regex)) {
7918 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7919 s->matched_entry = te;
7923 if (view->searching == TOG_SEARCH_FORWARD)
7924 te = got_tree_entry_get_next(s->tree, te);
7926 te = got_tree_entry_get_prev(s->tree, te);
7929 if (s->matched_entry) {
7930 s->first_displayed_entry = s->matched_entry;
7937 static const struct got_error *
7938 show_tree_view(struct tog_view *view)
7940 const struct got_error *err = NULL;
7941 struct tog_tree_view_state *s = &view->state.tree;
7944 err = tree_entry_path(&parent_path, &s->parents, NULL);
7948 err = draw_tree_entries(view, parent_path);
7955 static const struct got_error *
7956 tree_goto_line(struct tog_view *view, int nlines)
7958 const struct got_error *err = NULL;
7959 struct tog_tree_view_state *s = &view->state.tree;
7960 struct got_tree_entry **fte, **lte, **ste;
7961 int g, last, first = 1, i = 1;
7962 int root = s->tree == s->root;
7963 int off = root ? 1 : 2;
7970 else if (g > got_object_tree_get_nentries(s->tree))
7971 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7973 fte = &s->first_displayed_entry;
7974 lte = &s->last_displayed_entry;
7975 ste = &s->selected_entry;
7978 first = got_tree_entry_get_index(*fte);
7979 first += off; /* account for ".." */
7981 last = got_tree_entry_get_index(*lte);
7984 if (g >= first && g <= last && g - first < nlines) {
7985 s->selected = g - first;
7986 return NULL; /* gline is on the current page */
7990 i = got_tree_entry_get_index(*ste);
7995 err = tree_scroll_down(view, g - i);
7998 if (got_tree_entry_get_index(*lte) >=
7999 got_object_tree_get_nentries(s->tree) - 1 &&
8000 first + s->selected < g &&
8001 s->selected < s->ndisplayed - 1) {
8002 first = got_tree_entry_get_index(*fte);
8004 s->selected = g - first;
8007 tree_scroll_up(s, i - g);
8010 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
8011 s->selected = g - 1;
8016 static const struct got_error *
8017 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
8019 const struct got_error *err = NULL;
8020 struct tog_tree_view_state *s = &view->state.tree;
8021 struct got_tree_entry *te;
8022 int n, nscroll = view->nlines - 3;
8025 return tree_goto_line(view, nscroll);
8034 horizontal_scroll_input(view, ch);
8037 s->show_ids = !s->show_ids;
8042 if (!s->selected_entry)
8044 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8048 err = view_request_new(new_view, view, TOG_VIEW_REF);
8055 if (s->tree == s->root)
8056 s->first_displayed_entry =
8057 got_object_tree_get_first_entry(s->tree);
8059 s->first_displayed_entry = NULL;
8064 int eos = view->nlines - 3;
8066 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8070 te = got_object_tree_get_last_entry(s->tree);
8071 for (n = 0; n < eos; n++) {
8073 if (s->tree != s->root) {
8074 s->first_displayed_entry = NULL;
8079 s->first_displayed_entry = te;
8080 te = got_tree_entry_get_prev(s->tree, te);
8083 s->selected = n - 1;
8089 if (s->selected > 0) {
8093 tree_scroll_up(s, 1);
8094 if (s->selected_entry == NULL ||
8095 (s->tree == s->root && s->selected_entry ==
8096 got_object_tree_get_first_entry(s->tree)))
8106 if (s->tree == s->root) {
8107 if (got_object_tree_get_first_entry(s->tree) ==
8108 s->first_displayed_entry)
8109 s->selected -= MIN(s->selected, nscroll);
8111 if (s->first_displayed_entry == NULL)
8112 s->selected -= MIN(s->selected, nscroll);
8114 tree_scroll_up(s, MAX(0, nscroll));
8115 if (s->selected_entry == NULL ||
8116 (s->tree == s->root && s->selected_entry ==
8117 got_object_tree_get_first_entry(s->tree)))
8123 if (s->selected < s->ndisplayed - 1) {
8127 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8129 /* can't scroll any further */
8133 tree_scroll_down(view, 1);
8143 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8145 /* can't scroll any further; move cursor down */
8146 if (s->selected < s->ndisplayed - 1)
8147 s->selected += MIN(nscroll,
8148 s->ndisplayed - s->selected - 1);
8153 tree_scroll_down(view, nscroll);
8158 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8159 struct tog_parent_tree *parent;
8160 /* user selected '..' */
8161 if (s->tree == s->root) {
8165 parent = TAILQ_FIRST(&s->parents);
8166 TAILQ_REMOVE(&s->parents, parent,
8168 got_object_tree_close(s->tree);
8169 s->tree = parent->tree;
8170 s->first_displayed_entry =
8171 parent->first_displayed_entry;
8173 parent->selected_entry;
8174 s->selected = parent->selected;
8175 if (s->selected > view->nlines - 3) {
8176 err = offset_selection_down(view);
8181 } else if (S_ISDIR(got_tree_entry_get_mode(
8182 s->selected_entry))) {
8183 struct got_tree_object *subtree;
8185 err = got_object_open_as_tree(&subtree, s->repo,
8186 got_tree_entry_get_id(s->selected_entry));
8189 err = tree_view_visit_subtree(s, subtree);
8191 got_object_tree_close(subtree);
8194 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8195 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8198 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8199 s->selected = view->nlines - 4;
8215 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8220 static const struct got_error *
8221 cmd_tree(int argc, char *argv[])
8223 const struct got_error *error;
8224 struct got_repository *repo = NULL;
8225 struct got_worktree *worktree = NULL;
8226 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8227 struct got_object_id *commit_id = NULL;
8228 struct got_commit_object *commit = NULL;
8229 const char *commit_id_arg = NULL;
8230 char *keyword_idstr = NULL, *label = NULL;
8231 struct got_reference *ref = NULL;
8232 const char *head_ref_name = NULL;
8234 struct tog_view *view;
8235 int *pack_fds = NULL;
8237 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8240 commit_id_arg = optarg;
8243 repo_path = realpath(optarg, NULL);
8244 if (repo_path == NULL)
8245 return got_error_from_errno2("realpath",
8260 error = got_repo_pack_fds_open(&pack_fds);
8264 if (repo_path == NULL) {
8265 cwd = getcwd(NULL, 0);
8267 return got_error_from_errno("getcwd");
8268 error = got_worktree_open(&worktree, cwd, NULL);
8269 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8273 strdup(got_worktree_get_repo_path(worktree));
8275 repo_path = strdup(cwd);
8276 if (repo_path == NULL) {
8277 error = got_error_from_errno("strdup");
8282 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8286 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8293 error = apply_unveil(got_repo_get_path(repo), NULL);
8297 error = tog_load_refs(repo, 0);
8301 if (commit_id_arg == NULL) {
8302 error = got_repo_match_object_id(&commit_id, &label,
8303 worktree ? got_worktree_get_head_ref_name(worktree) :
8304 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8307 head_ref_name = label;
8309 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8313 if (keyword_idstr != NULL)
8314 commit_id_arg = keyword_idstr;
8316 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8318 head_ref_name = got_ref_get_name(ref);
8319 else if (error->code != GOT_ERR_NOT_REF)
8321 error = got_repo_match_object_id(&commit_id, NULL,
8322 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8327 error = got_object_open_as_commit(&commit, repo, commit_id);
8331 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8333 error = got_error_from_errno("view_open");
8336 error = open_tree_view(view, commit_id, head_ref_name, repo);
8339 if (!got_path_is_root_dir(in_repo_path)) {
8340 error = tree_view_walk_path(&view->state.tree, commit,
8347 error = set_tog_base_commit(repo, worktree);
8351 /* Release work tree lock. */
8352 got_worktree_close(worktree);
8356 error = view_loop(view);
8359 free(tog_base_commit.id);
8360 free(keyword_idstr);
8366 got_object_commit_close(commit);
8369 if (worktree != NULL)
8370 got_worktree_close(worktree);
8372 const struct got_error *close_err = got_repo_close(repo);
8377 const struct got_error *pack_err =
8378 got_repo_pack_fds_close(pack_fds);
8386 static const struct got_error *
8387 ref_view_load_refs(struct tog_ref_view_state *s)
8389 struct got_reflist_entry *sre;
8390 struct tog_reflist_entry *re;
8393 TAILQ_FOREACH(sre, &tog_refs, entry) {
8394 if (strncmp(got_ref_get_name(sre->ref),
8395 "refs/got/", 9) == 0 &&
8396 strncmp(got_ref_get_name(sre->ref),
8397 "refs/got/backup/", 16) != 0)
8400 re = malloc(sizeof(*re));
8402 return got_error_from_errno("malloc");
8404 re->ref = got_ref_dup(sre->ref);
8405 if (re->ref == NULL)
8406 return got_error_from_errno("got_ref_dup");
8407 re->idx = s->nrefs++;
8408 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8411 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8416 ref_view_free_refs(struct tog_ref_view_state *s)
8418 struct tog_reflist_entry *re;
8420 while (!TAILQ_EMPTY(&s->refs)) {
8421 re = TAILQ_FIRST(&s->refs);
8422 TAILQ_REMOVE(&s->refs, re, entry);
8423 got_ref_close(re->ref);
8428 static const struct got_error *
8429 open_ref_view(struct tog_view *view, struct got_repository *repo)
8431 const struct got_error *err = NULL;
8432 struct tog_ref_view_state *s = &view->state.ref;
8434 s->selected_entry = 0;
8437 TAILQ_INIT(&s->refs);
8438 STAILQ_INIT(&s->colors);
8440 err = ref_view_load_refs(s);
8444 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8445 err = add_color(&s->colors, "^refs/heads/",
8446 TOG_COLOR_REFS_HEADS,
8447 get_color_value("TOG_COLOR_REFS_HEADS"));
8451 err = add_color(&s->colors, "^refs/tags/",
8452 TOG_COLOR_REFS_TAGS,
8453 get_color_value("TOG_COLOR_REFS_TAGS"));
8457 err = add_color(&s->colors, "^refs/remotes/",
8458 TOG_COLOR_REFS_REMOTES,
8459 get_color_value("TOG_COLOR_REFS_REMOTES"));
8463 err = add_color(&s->colors, "^refs/got/backup/",
8464 TOG_COLOR_REFS_BACKUP,
8465 get_color_value("TOG_COLOR_REFS_BACKUP"));
8470 view->show = show_ref_view;
8471 view->input = input_ref_view;
8472 view->close = close_ref_view;
8473 view->search_start = search_start_ref_view;
8474 view->search_next = search_next_ref_view;
8477 if (view->close == NULL)
8478 close_ref_view(view);
8484 static const struct got_error *
8485 close_ref_view(struct tog_view *view)
8487 struct tog_ref_view_state *s = &view->state.ref;
8489 ref_view_free_refs(s);
8490 free_colors(&s->colors);
8495 static const struct got_error *
8496 resolve_reflist_entry(struct got_object_id **commit_id,
8497 struct tog_reflist_entry *re, struct got_repository *repo)
8499 const struct got_error *err = NULL;
8500 struct got_object_id *obj_id;
8501 struct got_tag_object *tag = NULL;
8506 err = got_ref_resolve(&obj_id, repo, re->ref);
8510 err = got_object_get_type(&obj_type, repo, obj_id);
8515 case GOT_OBJ_TYPE_COMMIT:
8516 *commit_id = obj_id;
8518 case GOT_OBJ_TYPE_TAG:
8519 err = got_object_open_as_tag(&tag, repo, obj_id);
8523 err = got_object_get_type(&obj_type, repo,
8524 got_object_tag_get_object_id(tag));
8527 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8528 err = got_error(GOT_ERR_OBJ_TYPE);
8531 *commit_id = got_object_id_dup(
8532 got_object_tag_get_object_id(tag));
8533 if (*commit_id == NULL) {
8534 err = got_error_from_errno("got_object_id_dup");
8539 err = got_error(GOT_ERR_OBJ_TYPE);
8545 got_object_tag_close(tag);
8553 static const struct got_error *
8554 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8555 struct tog_reflist_entry *re, struct got_repository *repo)
8557 struct tog_view *log_view;
8558 const struct got_error *err = NULL;
8559 struct got_object_id *commit_id = NULL;
8563 err = resolve_reflist_entry(&commit_id, re, repo);
8565 if (err->code != GOT_ERR_OBJ_TYPE)
8571 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8572 if (log_view == NULL) {
8573 err = got_error_from_errno("view_open");
8577 err = open_log_view(log_view, commit_id, repo,
8578 got_ref_get_name(re->ref), "", 0, NULL);
8581 view_close(log_view);
8583 *new_view = log_view;
8589 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8591 struct tog_reflist_entry *re;
8594 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8597 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8598 while (i++ < maxscroll) {
8601 s->first_displayed_entry = re;
8602 re = TAILQ_PREV(re, tog_reflist_head, entry);
8606 static const struct got_error *
8607 ref_scroll_down(struct tog_view *view, int maxscroll)
8609 struct tog_ref_view_state *s = &view->state.ref;
8610 struct tog_reflist_entry *next, *last;
8613 if (s->first_displayed_entry)
8614 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8616 next = TAILQ_FIRST(&s->refs);
8618 last = s->last_displayed_entry;
8619 while (next && n++ < maxscroll) {
8621 s->last_displayed_entry = last;
8622 last = TAILQ_NEXT(last, entry);
8624 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8625 s->first_displayed_entry = next;
8626 next = TAILQ_NEXT(next, entry);
8633 static const struct got_error *
8634 search_start_ref_view(struct tog_view *view)
8636 struct tog_ref_view_state *s = &view->state.ref;
8638 s->matched_entry = NULL;
8643 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8645 regmatch_t regmatch;
8647 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8651 static const struct got_error *
8652 search_next_ref_view(struct tog_view *view)
8654 struct tog_ref_view_state *s = &view->state.ref;
8655 struct tog_reflist_entry *re = NULL;
8657 if (!view->searching) {
8658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8662 if (s->matched_entry) {
8663 if (view->searching == TOG_SEARCH_FORWARD) {
8664 if (s->selected_entry)
8665 re = TAILQ_NEXT(s->selected_entry, entry);
8667 re = TAILQ_PREV(s->selected_entry,
8668 tog_reflist_head, entry);
8670 if (s->selected_entry == NULL)
8671 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8673 re = TAILQ_PREV(s->selected_entry,
8674 tog_reflist_head, entry);
8677 if (s->selected_entry)
8678 re = s->selected_entry;
8679 else if (view->searching == TOG_SEARCH_FORWARD)
8680 re = TAILQ_FIRST(&s->refs);
8682 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8687 if (s->matched_entry == NULL) {
8688 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8691 if (view->searching == TOG_SEARCH_FORWARD)
8692 re = TAILQ_FIRST(&s->refs);
8694 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8697 if (match_reflist_entry(re, &view->regex)) {
8698 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8699 s->matched_entry = re;
8703 if (view->searching == TOG_SEARCH_FORWARD)
8704 re = TAILQ_NEXT(re, entry);
8706 re = TAILQ_PREV(re, tog_reflist_head, entry);
8709 if (s->matched_entry) {
8710 s->first_displayed_entry = s->matched_entry;
8717 static const struct got_error *
8718 show_ref_view(struct tog_view *view)
8720 const struct got_error *err = NULL;
8721 struct tog_ref_view_state *s = &view->state.ref;
8722 struct tog_reflist_entry *re;
8725 struct tog_color *tc;
8726 int width, n, scrollx;
8727 int limit = view->nlines;
8729 werase(view->window);
8732 if (view_is_hsplit_top(view))
8733 --limit; /* border */
8738 re = s->first_displayed_entry;
8740 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8742 return got_error_from_errno("asprintf");
8744 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8749 if (view_needs_focus_indication(view))
8750 wstandout(view->window);
8751 waddwstr(view->window, wline);
8752 while (width++ < view->ncols)
8753 waddch(view->window, ' ');
8754 if (view_needs_focus_indication(view))
8755 wstandend(view->window);
8765 while (re && limit > 0) {
8767 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8770 struct got_commit_object *ci;
8771 struct got_tag_object *tag;
8772 struct got_object_id *id;
8776 err = got_ref_resolve(&id, s->repo, re->ref);
8779 err = got_object_open_as_tag(&tag, s->repo, id);
8781 if (err->code != GOT_ERR_OBJ_TYPE) {
8785 err = got_object_open_as_commit(&ci, s->repo,
8791 t = got_object_commit_get_committer_time(ci);
8792 got_object_commit_close(ci);
8794 t = got_object_tag_get_tagger_time(tag);
8795 got_object_tag_close(tag);
8798 if (gmtime_r(&t, &tm) == NULL)
8799 return got_error_from_errno("gmtime_r");
8800 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8801 return got_error(GOT_ERR_NO_SPACE);
8803 if (got_ref_is_symbolic(re->ref)) {
8804 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8805 ymd : "", got_ref_get_name(re->ref),
8806 got_ref_get_symref_target(re->ref)) == -1)
8807 return got_error_from_errno("asprintf");
8808 } else if (s->show_ids) {
8809 struct got_object_id *id;
8811 err = got_ref_resolve(&id, s->repo, re->ref);
8814 err = got_object_id_str(&id_str, id);
8819 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8820 got_ref_get_name(re->ref), id_str) == -1) {
8821 err = got_error_from_errno("asprintf");
8828 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8829 got_ref_get_name(re->ref)) == -1)
8830 return got_error_from_errno("asprintf");
8832 /* use full line width to determine view->maxx */
8833 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8838 view->maxx = MAX(view->maxx, width);
8842 err = format_line(&wline, &width, &scrollx, line, view->x,
8848 if (n == s->selected) {
8850 wstandout(view->window);
8851 s->selected_entry = re;
8853 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8855 wattr_on(view->window,
8856 COLOR_PAIR(tc->colorpair), NULL);
8857 waddwstr(view->window, &wline[scrollx]);
8859 wattr_off(view->window,
8860 COLOR_PAIR(tc->colorpair), NULL);
8861 if (width < view->ncols)
8862 waddch(view->window, '\n');
8863 if (n == s->selected && view->focussed)
8864 wstandend(view->window);
8870 s->last_displayed_entry = re;
8873 re = TAILQ_NEXT(re, entry);
8880 static const struct got_error *
8881 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8882 struct tog_reflist_entry *re, struct got_repository *repo)
8884 const struct got_error *err = NULL;
8885 struct got_object_id *commit_id = NULL;
8886 struct tog_view *tree_view;
8890 err = resolve_reflist_entry(&commit_id, re, repo);
8892 if (err->code != GOT_ERR_OBJ_TYPE)
8899 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8900 if (tree_view == NULL) {
8901 err = got_error_from_errno("view_open");
8905 err = open_tree_view(tree_view, commit_id,
8906 got_ref_get_name(re->ref), repo);
8910 *new_view = tree_view;
8916 static const struct got_error *
8917 ref_goto_line(struct tog_view *view, int nlines)
8919 const struct got_error *err = NULL;
8920 struct tog_ref_view_state *s = &view->state.ref;
8921 int g, idx = s->selected_entry->idx;
8928 else if (g > s->nrefs)
8931 if (g >= s->first_displayed_entry->idx + 1 &&
8932 g <= s->last_displayed_entry->idx + 1 &&
8933 g - s->first_displayed_entry->idx - 1 < nlines) {
8934 s->selected = g - s->first_displayed_entry->idx - 1;
8939 err = ref_scroll_down(view, g - idx - 1);
8942 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8943 s->first_displayed_entry->idx + s->selected < g &&
8944 s->selected < s->ndisplayed - 1)
8945 s->selected = g - s->first_displayed_entry->idx - 1;
8946 } else if (idx + 1 > g)
8947 ref_scroll_up(s, idx - g + 1);
8949 if (g < nlines && s->first_displayed_entry->idx == 0)
8950 s->selected = g - 1;
8956 static const struct got_error *
8957 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8959 const struct got_error *err = NULL;
8960 struct tog_ref_view_state *s = &view->state.ref;
8961 struct tog_reflist_entry *re;
8962 int n, nscroll = view->nlines - 1;
8965 return ref_goto_line(view, nscroll);
8974 horizontal_scroll_input(view, ch);
8977 s->show_ids = !s->show_ids;
8981 s->show_date = !s->show_date;
8985 s->sort_by_date = !s->sort_by_date;
8986 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8988 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8989 got_ref_cmp_by_commit_timestamp_descending :
8990 tog_ref_cmp_by_name, s->repo);
8993 got_reflist_object_id_map_free(tog_refs_idmap);
8994 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8995 &tog_refs, s->repo);
8998 ref_view_free_refs(s);
8999 err = ref_view_load_refs(s);
9004 if (!s->selected_entry)
9006 err = view_request_new(new_view, view, TOG_VIEW_LOG);
9010 if (!s->selected_entry)
9012 err = view_request_new(new_view, view, TOG_VIEW_TREE);
9019 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
9024 int eos = view->nlines - 1;
9026 if (view->mode == TOG_VIEW_SPLIT_HRZN)
9030 re = TAILQ_LAST(&s->refs, tog_reflist_head);
9031 for (n = 0; n < eos; n++) {
9034 s->first_displayed_entry = re;
9035 re = TAILQ_PREV(re, tog_reflist_head, entry);
9038 s->selected = n - 1;
9044 if (s->selected > 0) {
9048 ref_scroll_up(s, 1);
9049 if (s->selected_entry == TAILQ_FIRST(&s->refs))
9059 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9060 s->selected -= MIN(nscroll, s->selected);
9061 ref_scroll_up(s, MAX(0, nscroll));
9062 if (s->selected_entry == TAILQ_FIRST(&s->refs))
9068 if (s->selected < s->ndisplayed - 1) {
9072 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9073 /* can't scroll any further */
9077 ref_scroll_down(view, 1);
9087 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9088 /* can't scroll any further; move cursor down */
9089 if (s->selected < s->ndisplayed - 1)
9090 s->selected += MIN(nscroll,
9091 s->ndisplayed - s->selected - 1);
9092 if (view->count > 1 && s->selected < s->ndisplayed - 1)
9093 s->selected += s->ndisplayed - s->selected - 1;
9097 ref_scroll_down(view, nscroll);
9102 err = tog_load_refs(s->repo, s->sort_by_date);
9105 ref_view_free_refs(s);
9106 err = ref_view_load_refs(s);
9109 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9110 s->selected = view->nlines - 2;
9124 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9129 static const struct got_error *
9130 cmd_ref(int argc, char *argv[])
9132 const struct got_error *error;
9133 struct got_repository *repo = NULL;
9134 struct got_worktree *worktree = NULL;
9135 char *cwd = NULL, *repo_path = NULL;
9137 struct tog_view *view;
9138 int *pack_fds = NULL;
9140 while ((ch = getopt(argc, argv, "r:")) != -1) {
9143 repo_path = realpath(optarg, NULL);
9144 if (repo_path == NULL)
9145 return got_error_from_errno2("realpath",
9160 error = got_repo_pack_fds_open(&pack_fds);
9164 if (repo_path == NULL) {
9165 cwd = getcwd(NULL, 0);
9167 return got_error_from_errno("getcwd");
9168 error = got_worktree_open(&worktree, cwd, NULL);
9169 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9173 strdup(got_worktree_get_repo_path(worktree));
9175 repo_path = strdup(cwd);
9176 if (repo_path == NULL) {
9177 error = got_error_from_errno("strdup");
9182 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9188 error = apply_unveil(got_repo_get_path(repo), NULL);
9192 error = tog_load_refs(repo, 0);
9196 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9198 error = got_error_from_errno("view_open");
9202 error = open_ref_view(view, repo);
9207 error = set_tog_base_commit(repo, worktree);
9211 /* Release work tree lock. */
9212 got_worktree_close(worktree);
9216 error = view_loop(view);
9219 free(tog_base_commit.id);
9222 if (worktree != NULL)
9223 got_worktree_close(worktree);
9225 const struct got_error *close_err = got_repo_close(repo);
9230 const struct got_error *pack_err =
9231 got_repo_pack_fds_close(pack_fds);
9239 static const struct got_error*
9240 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9249 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9253 if (mvwprintw(win, y, x, "%s", str) == ERR)
9254 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9261 static const struct got_error *
9262 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9266 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9268 free(*line_offsets);
9269 *line_offsets = NULL;
9270 return got_error_from_errno("reallocarray");
9274 (*line_offsets)[*nlines] = off;
9279 static const struct got_error *
9280 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9284 for (;n > 0; --n, ++km) {
9288 if (km->keys == NULL)
9291 t = t0 = strdup(km->keys);
9293 return got_error_from_errno("strdup");
9296 while ((k = strsep(&t, " ")) != NULL)
9297 len += strlen(k) > 1 ? 2 : 0;
9299 *ret = MAX(*ret, len);
9306 * Write keymap section headers, keys, and key info in km to f.
9307 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9308 * wrap control and symbolic keys in guillemets, else use <>.
9310 static const struct got_error *
9311 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9316 static const char *u8_glyph[] = {
9317 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9318 "\xe2\x80\xba" /* U+203A (utf8 >) */
9321 int cs, s, first = 1;
9323 cs = got_locale_is_utf8();
9325 t = t0 = strdup(km->keys);
9327 return got_error_from_errno("strdup");
9329 len = strlen(km->keys);
9330 while ((k = strsep(&t, " ")) != NULL) {
9331 s = strlen(k) > 1; /* control or symbolic key */
9332 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9333 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9334 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9337 return got_error_from_errno("fprintf");
9345 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9347 return got_error_from_errno("fprintf");
9353 static const struct got_error *
9354 format_help(struct tog_help_view_state *s)
9356 const struct got_error *err = NULL;
9358 int i, max, n, show = s->all;
9359 static const struct tog_key_map km[] = {
9360 #define KEYMAP_(info, type) { NULL, (info), type }
9361 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9367 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9372 err = max_key_str(&max, km, n);
9376 for (i = 0; i < n; ++i) {
9377 if (km[i].keys == NULL) {
9379 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9380 km[i].type == s->type || s->all)
9384 err = format_help_line(&off, s->f, &km[i], max);
9387 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9394 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9398 static const struct got_error *
9399 create_help(struct tog_help_view_state *s)
9402 const struct got_error *err;
9404 free(s->line_offsets);
9405 s->line_offsets = NULL;
9410 return got_error_from_errno("got_opentemp");
9413 err = format_help(s);
9417 if (s->f && fflush(s->f) != 0)
9418 return got_error_from_errno("fflush");
9423 static const struct got_error *
9424 search_start_help_view(struct tog_view *view)
9426 view->state.help.matched_line = 0;
9431 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9432 size_t *nlines, int **first, int **last, int **match, int **selected)
9434 struct tog_help_view_state *s = &view->state.help;
9437 *nlines = s->nlines;
9438 *line_offsets = s->line_offsets;
9439 *match = &s->matched_line;
9440 *first = &s->first_displayed_line;
9441 *last = &s->last_displayed_line;
9442 *selected = &s->selected_line;
9445 static const struct got_error *
9446 show_help_view(struct tog_view *view)
9448 struct tog_help_view_state *s = &view->state.help;
9449 const struct got_error *err;
9450 regmatch_t *regmatch = &view->regmatch;
9455 int width, nprinted = 0, rc = 0;
9456 int eos = view->nlines;
9458 if (view_is_hsplit_top(view))
9459 --eos; /* account for border */
9463 werase(view->window);
9465 if (view->gline > s->nlines - 1)
9466 view->gline = s->nlines - 1;
9468 err = win_draw_center(view->window, 0, 0, view->ncols,
9469 view_needs_focus_indication(view),
9470 "tog help (press q to return to tog)");
9475 waddstr(view->window, "\n\n");
9481 while (eos > 0 && nprinted < eos) {
9484 linelen = getline(&line, &linesz, s->f);
9485 if (linelen == -1) {
9488 return got_ferror(s->f, GOT_ERR_IO);
9493 if (++s->lineno < s->first_displayed_line)
9495 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9497 if (s->lineno == view->hiline)
9500 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9506 view->maxx = MAX(view->maxx, width);
9511 wattron(view->window, attr);
9512 if (s->first_displayed_line + nprinted == s->matched_line &&
9513 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9514 err = add_matched_line(&width, line, view->ncols - 1, 0,
9515 view->window, view->x, regmatch);
9523 err = format_line(&wline, &width, &skip, line,
9524 view->x, view->ncols, 0, view->x ? 1 : 0);
9529 waddwstr(view->window, &wline[skip]);
9533 if (s->lineno == view->hiline) {
9534 while (width++ < view->ncols)
9535 waddch(view->window, ' ');
9537 if (width < view->ncols)
9538 waddch(view->window, '\n');
9541 wattroff(view->window, attr);
9542 if (++nprinted == 1)
9543 s->first_displayed_line = s->lineno;
9547 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9549 s->last_displayed_line = s->first_displayed_line;
9554 rc = waddnstr(view->window,
9555 "See the tog(1) manual page for full documentation",
9558 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9560 wmove(view->window, view->nlines - 1, 0);
9561 wclrtoeol(view->window);
9562 wstandout(view->window);
9563 rc = waddnstr(view->window, "scroll down for more...",
9566 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9567 if (getcurx(view->window) < view->ncols - 6) {
9568 rc = wprintw(view->window, "[%.0f%%]",
9569 100.00 * s->last_displayed_line / s->nlines);
9571 return got_error_msg(GOT_ERR_IO, "wprintw");
9573 wstandend(view->window);
9579 static const struct got_error *
9580 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9582 struct tog_help_view_state *s = &view->state.help;
9583 const struct got_error *err = NULL;
9589 eos = nscroll = view->nlines;
9590 if (view_is_hsplit_top(view))
9593 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9602 horizontal_scroll_input(view, ch);
9606 s->first_displayed_line = 1;
9614 s->first_displayed_line = (s->nlines - eos) + 3;
9619 if (s->first_displayed_line > 1)
9620 --s->first_displayed_line;
9631 if (s->first_displayed_line == 1) {
9635 while (--nscroll > 0 && s->first_displayed_line > 1)
9636 s->first_displayed_line--;
9642 ++s->first_displayed_line;
9658 while (!s->eof && --nscroll > 0) {
9659 linelen = getline(&line, &linesz, s->f);
9660 s->first_displayed_line++;
9661 if (linelen == -1) {
9665 err = got_ferror(s->f, GOT_ERR_IO);
9679 static const struct got_error *
9680 close_help_view(struct tog_view *view)
9682 struct tog_help_view_state *s = &view->state.help;
9684 free(s->line_offsets);
9685 s->line_offsets = NULL;
9686 if (fclose(s->f) == EOF)
9687 return got_error_from_errno("fclose");
9692 static const struct got_error *
9693 reset_help_view(struct tog_view *view)
9695 struct tog_help_view_state *s = &view->state.help;
9698 if (s->f && fclose(s->f) == EOF)
9699 return got_error_from_errno("fclose");
9701 wclear(view->window);
9705 s->first_displayed_line = 1;
9706 s->last_displayed_line = view->nlines;
9707 s->matched_line = 0;
9709 return create_help(s);
9712 static const struct got_error *
9713 open_help_view(struct tog_view *view, struct tog_view *parent)
9715 const struct got_error *err = NULL;
9716 struct tog_help_view_state *s = &view->state.help;
9718 s->type = (enum tog_keymap_type)parent->type;
9719 s->first_displayed_line = 1;
9720 s->last_displayed_line = view->nlines;
9721 s->selected_line = 1;
9723 view->show = show_help_view;
9724 view->input = input_help_view;
9725 view->reset = reset_help_view;
9726 view->close = close_help_view;
9727 view->search_start = search_start_help_view;
9728 view->search_setup = search_setup_help_view;
9729 view->search_next = search_next_view_match;
9731 err = create_help(s);
9735 static const struct got_error *
9736 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9737 enum tog_view_type request, int y, int x)
9739 const struct got_error *err = NULL;
9745 if (view->type == TOG_VIEW_LOG) {
9746 struct tog_log_view_state *s = &view->state.log;
9748 err = open_diff_view_for_commit(new_view, y, x,
9749 s->selected_entry->commit, s->selected_entry->id,
9752 return got_error_msg(GOT_ERR_NOT_IMPL,
9753 "parent/child view pair not supported");
9755 case TOG_VIEW_BLAME:
9756 if (view->type == TOG_VIEW_TREE) {
9757 struct tog_tree_view_state *s = &view->state.tree;
9759 err = blame_tree_entry(new_view, y, x,
9760 s->selected_entry, &s->parents, s->commit_id,
9763 return got_error_msg(GOT_ERR_NOT_IMPL,
9764 "parent/child view pair not supported");
9767 if (view->type == TOG_VIEW_BLAME)
9768 err = log_annotated_line(new_view, y, x,
9769 view->state.blame.repo, view->state.blame.id_to_log);
9770 else if (view->type == TOG_VIEW_TREE)
9771 err = log_selected_tree_entry(new_view, y, x,
9773 else if (view->type == TOG_VIEW_REF)
9774 err = log_ref_entry(new_view, y, x,
9775 view->state.ref.selected_entry,
9776 view->state.ref.repo);
9778 return got_error_msg(GOT_ERR_NOT_IMPL,
9779 "parent/child view pair not supported");
9782 if (view->type == TOG_VIEW_LOG)
9783 err = browse_commit_tree(new_view, y, x,
9784 view->state.log.selected_entry,
9785 view->state.log.in_repo_path,
9786 view->state.log.head_ref_name,
9787 view->state.log.repo);
9788 else if (view->type == TOG_VIEW_REF)
9789 err = browse_ref_tree(new_view, y, x,
9790 view->state.ref.selected_entry,
9791 view->state.ref.repo);
9793 return got_error_msg(GOT_ERR_NOT_IMPL,
9794 "parent/child view pair not supported");
9797 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9798 if (*new_view == NULL)
9799 return got_error_from_errno("view_open");
9800 if (view->type == TOG_VIEW_LOG)
9801 err = open_ref_view(*new_view, view->state.log.repo);
9802 else if (view->type == TOG_VIEW_TREE)
9803 err = open_ref_view(*new_view, view->state.tree.repo);
9805 err = got_error_msg(GOT_ERR_NOT_IMPL,
9806 "parent/child view pair not supported");
9808 view_close(*new_view);
9811 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9812 if (*new_view == NULL)
9813 return got_error_from_errno("view_open");
9814 err = open_help_view(*new_view, view);
9816 view_close(*new_view);
9819 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9826 * If view was scrolled down to move the selected line into view when opening a
9827 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9830 offset_selection_up(struct tog_view *view)
9832 switch (view->type) {
9833 case TOG_VIEW_BLAME: {
9834 struct tog_blame_view_state *s = &view->state.blame;
9835 if (s->first_displayed_line == 1) {
9836 s->selected_line = MAX(s->selected_line - view->offset,
9840 if (s->first_displayed_line > view->offset)
9841 s->first_displayed_line -= view->offset;
9843 s->first_displayed_line = 1;
9844 s->selected_line += view->offset;
9848 log_scroll_up(&view->state.log, view->offset);
9849 view->state.log.selected += view->offset;
9852 ref_scroll_up(&view->state.ref, view->offset);
9853 view->state.ref.selected += view->offset;
9856 tree_scroll_up(&view->state.tree, view->offset);
9857 view->state.tree.selected += view->offset;
9867 * If the selected line is in the section of screen covered by the bottom split,
9868 * scroll down offset lines to move it into view and index its new position.
9870 static const struct got_error *
9871 offset_selection_down(struct tog_view *view)
9873 const struct got_error *err = NULL;
9874 const struct got_error *(*scrolld)(struct tog_view *, int);
9875 int *selected = NULL;
9878 switch (view->type) {
9879 case TOG_VIEW_BLAME: {
9880 struct tog_blame_view_state *s = &view->state.blame;
9883 if (s->selected_line > view->nlines - header) {
9884 offset = abs(view->nlines - s->selected_line - header);
9885 s->first_displayed_line += offset;
9886 s->selected_line -= offset;
9887 view->offset = offset;
9891 case TOG_VIEW_LOG: {
9892 struct tog_log_view_state *s = &view->state.log;
9893 scrolld = &log_scroll_down;
9894 header = view_is_parent_view(view) ? 3 : 2;
9895 selected = &s->selected;
9898 case TOG_VIEW_REF: {
9899 struct tog_ref_view_state *s = &view->state.ref;
9900 scrolld = &ref_scroll_down;
9902 selected = &s->selected;
9905 case TOG_VIEW_TREE: {
9906 struct tog_tree_view_state *s = &view->state.tree;
9907 scrolld = &tree_scroll_down;
9909 selected = &s->selected;
9919 if (selected && *selected > view->nlines - header) {
9920 offset = abs(view->nlines - *selected - header);
9921 view->offset = offset;
9922 if (scrolld && offset) {
9923 err = scrolld(view, offset);
9924 *selected -= offset;
9932 list_commands(FILE *fp)
9936 fprintf(fp, "commands:");
9937 for (i = 0; i < nitems(tog_commands); i++) {
9938 const struct tog_cmd *cmd = &tog_commands[i];
9939 fprintf(fp, " %s", cmd->name);
9945 usage(int hflag, int status)
9947 FILE *fp = (status == 0) ? stdout : stderr;
9949 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9952 fprintf(fp, "lazy usage: %s path\n", getprogname());
9959 make_argv(int argc, ...)
9967 argv = calloc(argc, sizeof(char *));
9970 for (i = 0; i < argc; i++) {
9971 argv[i] = strdup(va_arg(ap, char *));
9972 if (argv[i] == NULL)
9981 * Try to convert 'tog path' into a 'tog log path' command.
9982 * The user could simply have mistyped the command rather than knowingly
9983 * provided a path. So check whether argv[0] can in fact be resolved
9984 * to a path in the HEAD commit and print a special error if not.
9985 * This hack is for mpi@ <3
9987 static const struct got_error *
9988 tog_log_with_path(int argc, char *argv[])
9990 const struct got_error *error = NULL, *close_err;
9991 const struct tog_cmd *cmd = NULL;
9992 struct got_repository *repo = NULL;
9993 struct got_worktree *worktree = NULL;
9994 struct got_object_id *commit_id = NULL, *id = NULL;
9995 struct got_commit_object *commit = NULL;
9996 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9997 char *commit_id_str = NULL, **cmd_argv = NULL;
9998 int *pack_fds = NULL;
10000 cwd = getcwd(NULL, 0);
10002 return got_error_from_errno("getcwd");
10004 error = got_repo_pack_fds_open(&pack_fds);
10008 error = got_worktree_open(&worktree, cwd, NULL);
10009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
10013 repo_path = strdup(got_worktree_get_repo_path(worktree));
10015 repo_path = strdup(cwd);
10016 if (repo_path == NULL) {
10017 error = got_error_from_errno("strdup");
10021 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
10025 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
10030 error = tog_load_refs(repo, 0);
10033 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
10034 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
10035 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10040 got_worktree_close(worktree);
10044 error = got_object_open_as_commit(&commit, repo, commit_id);
10048 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10050 if (error->code != GOT_ERR_NO_TREE_ENTRY)
10052 fprintf(stderr, "%s: '%s' is no known command or path\n",
10053 getprogname(), argv[0]);
10058 error = got_object_id_str(&commit_id_str, commit_id);
10062 cmd = &tog_commands[0]; /* log */
10064 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10065 error = cmd->cmd_main(argc, cmd_argv);
10068 close_err = got_repo_close(repo);
10073 got_object_commit_close(commit);
10075 got_worktree_close(worktree);
10077 const struct got_error *pack_err =
10078 got_repo_pack_fds_close(pack_fds);
10083 free(commit_id_str);
10087 free(in_repo_path);
10090 for (i = 0; i < argc; i++)
10099 main(int argc, char *argv[])
10101 const struct got_error *io_err, *error = NULL;
10102 const struct tog_cmd *cmd = NULL;
10103 int ch, hflag = 0, Vflag = 0;
10104 char **cmd_argv = NULL;
10105 static const struct option longopts[] = {
10106 { "version", no_argument, NULL, 'V' },
10107 { NULL, 0, NULL, 0}
10109 char *diff_algo_str = NULL;
10110 const char *test_script_path;
10112 setlocale(LC_CTYPE, "");
10115 * Override default signal handlers before starting ncurses.
10116 * This should prevent ncurses from installing its own
10117 * broken cleanup() signal handler.
10119 signal(SIGWINCH, tog_sigwinch);
10120 signal(SIGPIPE, tog_sigpipe);
10121 signal(SIGCONT, tog_sigcont);
10122 signal(SIGINT, tog_sigint);
10123 signal(SIGTERM, tog_sigterm);
10126 * Test mode init must happen before pledge() because "tty" will
10127 * not allow TTY-related ioctls to occur via regular files.
10129 test_script_path = getenv("TOG_TEST_SCRIPT");
10130 if (test_script_path != NULL) {
10131 error = init_mock_term(test_script_path);
10133 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10136 } else if (!isatty(STDIN_FILENO))
10137 errx(1, "standard input is not a tty");
10139 #if !defined(PROFILE)
10140 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10145 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10165 got_version_print_str();
10172 /* Build an argument vector which runs a default command. */
10173 cmd = &tog_commands[0];
10175 cmd_argv = make_argv(argc, cmd->name);
10179 /* Did the user specify a command? */
10180 for (i = 0; i < nitems(tog_commands); i++) {
10181 if (strncmp(tog_commands[i].name, argv[0],
10182 strlen(argv[0])) == 0) {
10183 cmd = &tog_commands[i];
10189 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10190 if (diff_algo_str) {
10191 if (strcasecmp(diff_algo_str, "patience") == 0)
10192 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10193 if (strcasecmp(diff_algo_str, "myers") == 0)
10194 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10197 tog_base_commit.idx = -1;
10198 tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10203 /* No command specified; try log with a path */
10204 error = tog_log_with_path(argc, argv);
10209 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10212 if (using_mock_io) {
10213 io_err = tog_io_close();
10220 for (i = 0; i < argc; i++)
10225 if (error && error->code != GOT_ERR_CANCELLED &&
10226 error->code != GOT_ERR_EOF &&
10227 error->code != GOT_ERR_PRIVSEP_EXIT &&
10228 error->code != GOT_ERR_PRIVSEP_PIPE &&
10229 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10230 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);