2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
145 struct commit_queue_head head;
149 STAILQ_ENTRY(tog_color) entry;
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
173 } else if (isbackup1 && !isbackup2) {
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
237 STAILQ_INSERT_HEAD(colors, tc, entry);
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
314 if (strcasecmp(val, "red") == 0)
316 if (strcasecmp(val, "green") == 0)
318 if (strcasecmp(val, "yellow") == 0)
320 if (strcasecmp(val, "blue") == 0)
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
326 if (strcasecmp(val, "white") == 0)
328 if (strcasecmp(val, "default") == 0)
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
340 int first_displayed_line;
341 int last_displayed_line;
344 int ignore_whitespace;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
375 int *search_next_done;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
404 struct commit_queue limit_commits;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
427 struct tog_view *view;
428 struct got_object_id *commit_id;
432 struct tog_blame_thread_args {
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
437 got_cancel_cb cancel_cb;
444 struct tog_blame_line *lines;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
458 int last_diffed_line;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
470 struct tog_colors colors;
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
518 struct tog_help_view_state {
523 int first_displayed_line;
524 int last_displayed_line;
529 enum tog_keymap_type type;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g", "Go to line N (default: first line)"), \
542 KEY_("Home =", "Go to the first line"), \
543 KEY_("G", "Go to line N (default: last line)"), \
544 KEY_("End *", "Go to the last line"), \
545 KEY_("l Right", "Scroll the view right"), \
546 KEY_("h Left", "Scroll the view left"), \
547 KEY_("$", "Scroll view to the rightmost position"), \
548 KEY_("0", "Scroll view to the leftmost position"), \
549 KEY_("-", "Decrease size of the focussed split"), \
550 KEY_("+", "Increase size of the focussed split"), \
551 KEY_("Tab", "Switch focus between views"), \
552 KEY_("F", "Toggle fullscreen mode"), \
553 KEY_("/", "Open prompt to enter search term"), \
554 KEY_("n", "Find next line/token matching the current search term"), \
555 KEY_("N", "Find previous line/token matching the current search term"),\
556 KEY_("q", "Quit the focussed view; Quit help screen"), \
557 KEY_("Q", "Quit tog"), \
559 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 KEY_("< ,", "Move cursor up one commit"), \
561 KEY_("> .", "Move cursor down one commit"), \
562 KEY_("Enter", "Open diff view of the selected commit"), \
563 KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 KEY_("R", "Open ref view of all repository references"), \
565 KEY_("T", "Display tree view of the repository from the selected" \
567 KEY_("@", "Toggle between displaying author and committer name"), \
568 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
578 KEY_("(", "Go to the previous file in the diff"), \
579 KEY_(")", "Go to the next file in the diff"), \
580 KEY_("{", "Go to the previous hunk in the diff"), \
581 KEY_("}", "Go to the next hunk in the diff"), \
582 KEY_("[", "Decrease the number of context lines"), \
583 KEY_("]", "Increase the number of context lines"), \
584 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 KEY_("Enter", "Display diff view of the selected line's commit"), \
588 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 KEY_("L", "Open log view for the currently selected annotated line"), \
590 KEY_("C", "Reload view with the previously blamed commit"), \
591 KEY_("c", "Reload view with the version of the file found in the" \
592 " selected line's commit"), \
593 KEY_("p", "Reload view with the version of the file found in the" \
594 " selected line's parent commit"), \
596 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 KEY_("Enter", "Enter selected directory or open blame view of the" \
599 KEY_("L", "Open log view for the selected entry"), \
600 KEY_("R", "Open ref view of all repository references"), \
601 KEY_("i", "Show object IDs for all tree entries"), \
602 KEY_("Backspace", "Return to the parent directory"), \
604 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 KEY_("Enter", "Display log view of the selected reference"), \
606 KEY_("T", "Display tree view of the selected reference"), \
607 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 KEY_("m", "Toggle display of last modified date for each reference"), \
609 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 KEY_("C-l", "Reload view with all repository references")
615 enum tog_keymap_type type;
619 * We implement two types of views: parent views and child views.
621 * The 'Tab' key switches focus between a parent view and its child view.
622 * Child views are shown side-by-side to their parent view, provided
623 * there is enough screen estate.
625 * When a new view is opened from within a parent view, this new view
626 * becomes a child view of the parent view, replacing any existing child.
628 * When a new view is opened from within a child view, this new view
629 * becomes a parent view which will obscure the views below until the
630 * user quits the new parent view by typing 'q'.
632 * This list of views contains parent views only.
633 * Child views are only pointed to by their parent view.
635 TAILQ_HEAD(tog_view_list_head, tog_view);
638 TAILQ_ENTRY(tog_view) entry;
641 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 int resized_y, resized_x; /* begin_y/x based on user resizing */
643 int maxx, x; /* max column and current start column */
644 int lines, cols; /* copies of LINES and COLS */
645 int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 int gline, hiline; /* navigate to and highlight this nG line */
647 int ch, count; /* current keymap and count prefix */
648 int resized; /* set when in a resize event */
649 int focussed; /* Only set on one parent or child view at a time. */
651 struct tog_view *parent;
652 struct tog_view *child;
655 * This flag is initially set on parent views when a new child view
656 * is created. It gets toggled when the 'Tab' key switches focus
657 * between parent and child.
658 * The flag indicates whether focus should be passed on to our child
659 * view if this parent view gets picked for focus after another parent
660 * view was closed. This prevents child views from losing focus in such
665 enum tog_view_mode mode;
666 /* type-specific state */
667 enum tog_view_type type;
669 struct tog_diff_view_state diff;
670 struct tog_log_view_state log;
671 struct tog_blame_view_state blame;
672 struct tog_tree_view_state tree;
673 struct tog_ref_view_state ref;
674 struct tog_help_view_state help;
677 const struct got_error *(*show)(struct tog_view *);
678 const struct got_error *(*input)(struct tog_view **,
679 struct tog_view *, int);
680 const struct got_error *(*reset)(struct tog_view *);
681 const struct got_error *(*resize)(struct tog_view *, int);
682 const struct got_error *(*close)(struct tog_view *);
684 const struct got_error *(*search_start)(struct tog_view *);
685 const struct got_error *(*search_next)(struct tog_view *);
686 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 int **, int **, int **, int **);
690 #define TOG_SEARCH_FORWARD 1
691 #define TOG_SEARCH_BACKWARD 2
692 int search_next_done;
693 #define TOG_SEARCH_HAVE_MORE 1
694 #define TOG_SEARCH_NO_MORE 2
695 #define TOG_SEARCH_HAVE_NONE 3
700 static const struct got_error *open_diff_view(struct tog_view *,
701 struct got_object_id *, struct got_object_id *,
702 const char *, const char *, int, int, int, struct tog_view *,
703 struct got_repository *);
704 static const struct got_error *show_diff_view(struct tog_view *);
705 static const struct got_error *input_diff_view(struct tog_view **,
706 struct tog_view *, int);
707 static const struct got_error *reset_diff_view(struct tog_view *);
708 static const struct got_error* close_diff_view(struct tog_view *);
709 static const struct got_error *search_start_diff_view(struct tog_view *);
710 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
711 size_t *, int **, int **, int **, int **);
712 static const struct got_error *search_next_view_match(struct tog_view *);
714 static const struct got_error *open_log_view(struct tog_view *,
715 struct got_object_id *, struct got_repository *,
716 const char *, const char *, int);
717 static const struct got_error * show_log_view(struct tog_view *);
718 static const struct got_error *input_log_view(struct tog_view **,
719 struct tog_view *, int);
720 static const struct got_error *resize_log_view(struct tog_view *, int);
721 static const struct got_error *close_log_view(struct tog_view *);
722 static const struct got_error *search_start_log_view(struct tog_view *);
723 static const struct got_error *search_next_log_view(struct tog_view *);
725 static const struct got_error *open_blame_view(struct tog_view *, char *,
726 struct got_object_id *, struct got_repository *);
727 static const struct got_error *show_blame_view(struct tog_view *);
728 static const struct got_error *input_blame_view(struct tog_view **,
729 struct tog_view *, int);
730 static const struct got_error *reset_blame_view(struct tog_view *);
731 static const struct got_error *close_blame_view(struct tog_view *);
732 static const struct got_error *search_start_blame_view(struct tog_view *);
733 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
734 size_t *, int **, int **, int **, int **);
736 static const struct got_error *open_tree_view(struct tog_view *,
737 struct got_object_id *, const char *, struct got_repository *);
738 static const struct got_error *show_tree_view(struct tog_view *);
739 static const struct got_error *input_tree_view(struct tog_view **,
740 struct tog_view *, int);
741 static const struct got_error *close_tree_view(struct tog_view *);
742 static const struct got_error *search_start_tree_view(struct tog_view *);
743 static const struct got_error *search_next_tree_view(struct tog_view *);
745 static const struct got_error *open_ref_view(struct tog_view *,
746 struct got_repository *);
747 static const struct got_error *show_ref_view(struct tog_view *);
748 static const struct got_error *input_ref_view(struct tog_view **,
749 struct tog_view *, int);
750 static const struct got_error *close_ref_view(struct tog_view *);
751 static const struct got_error *search_start_ref_view(struct tog_view *);
752 static const struct got_error *search_next_ref_view(struct tog_view *);
754 static const struct got_error *open_help_view(struct tog_view *,
756 static const struct got_error *show_help_view(struct tog_view *);
757 static const struct got_error *input_help_view(struct tog_view **,
758 struct tog_view *, int);
759 static const struct got_error *reset_help_view(struct tog_view *);
760 static const struct got_error* close_help_view(struct tog_view *);
761 static const struct got_error *search_start_help_view(struct tog_view *);
762 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
763 size_t *, int **, int **, int **, int **);
765 static volatile sig_atomic_t tog_sigwinch_received;
766 static volatile sig_atomic_t tog_sigpipe_received;
767 static volatile sig_atomic_t tog_sigcont_received;
768 static volatile sig_atomic_t tog_sigint_received;
769 static volatile sig_atomic_t tog_sigterm_received;
772 tog_sigwinch(int signo)
774 tog_sigwinch_received = 1;
778 tog_sigpipe(int signo)
780 tog_sigpipe_received = 1;
784 tog_sigcont(int signo)
786 tog_sigcont_received = 1;
790 tog_sigint(int signo)
792 tog_sigint_received = 1;
796 tog_sigterm(int signo)
798 tog_sigterm_received = 1;
802 tog_fatal_signal_received(void)
804 return (tog_sigpipe_received ||
805 tog_sigint_received || tog_sigterm_received);
808 static const struct got_error *
809 view_close(struct tog_view *view)
811 const struct got_error *err = NULL, *child_err = NULL;
814 child_err = view_close(view->child);
818 err = view->close(view);
820 del_panel(view->panel);
822 delwin(view->window);
824 return err ? err : child_err;
827 static struct tog_view *
828 view_open(int nlines, int ncols, int begin_y, int begin_x,
829 enum tog_view_type type)
831 struct tog_view *view = calloc(1, sizeof(*view));
839 view->nlines = nlines ? nlines : LINES - begin_y;
840 view->ncols = ncols ? ncols : COLS - begin_x;
841 view->begin_y = begin_y;
842 view->begin_x = begin_x;
843 view->window = newwin(nlines, ncols, begin_y, begin_x);
844 if (view->window == NULL) {
848 view->panel = new_panel(view->window);
849 if (view->panel == NULL ||
850 set_panel_userptr(view->panel, view) != OK) {
855 keypad(view->window, TRUE);
860 view_split_begin_x(int begin_x)
862 if (begin_x > 0 || COLS < 120)
864 return (COLS - MAX(COLS / 2, 80));
867 /* XXX Stub till we decide what to do. */
869 view_split_begin_y(int lines)
871 return lines * HSPLIT_SCALE;
874 static const struct got_error *view_resize(struct tog_view *);
876 static const struct got_error *
877 view_splitscreen(struct tog_view *view)
879 const struct got_error *err = NULL;
881 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
882 if (view->resized_y && view->resized_y < view->lines)
883 view->begin_y = view->resized_y;
885 view->begin_y = view_split_begin_y(view->nlines);
887 } else if (!view->resized) {
888 if (view->resized_x && view->resized_x < view->cols - 1 &&
890 view->begin_x = view->resized_x;
892 view->begin_x = view_split_begin_x(0);
895 view->nlines = LINES - view->begin_y;
896 view->ncols = COLS - view->begin_x;
899 err = view_resize(view);
903 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
904 view->parent->nlines = view->begin_y;
906 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
907 return got_error_from_errno("mvwin");
912 static const struct got_error *
913 view_fullscreen(struct tog_view *view)
915 const struct got_error *err = NULL;
918 view->begin_y = view->resized ? view->begin_y : 0;
919 view->nlines = view->resized ? view->nlines : LINES;
923 err = view_resize(view);
927 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
928 return got_error_from_errno("mvwin");
934 view_is_parent_view(struct tog_view *view)
936 return view->parent == NULL;
940 view_is_splitscreen(struct tog_view *view)
942 return view->begin_x > 0 || view->begin_y > 0;
946 view_is_fullscreen(struct tog_view *view)
948 return view->nlines == LINES && view->ncols == COLS;
952 view_is_hsplit_top(struct tog_view *view)
954 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
955 view_is_splitscreen(view->child);
959 view_border(struct tog_view *view)
962 const struct tog_view *view_above;
965 return view_border(view->parent);
967 panel = panel_above(view->panel);
971 view_above = panel_userptr(panel);
972 if (view->mode == TOG_VIEW_SPLIT_HRZN)
973 mvwhline(view->window, view_above->begin_y - 1,
974 view->begin_x, got_locale_is_utf8() ?
975 ACS_HLINE : '-', view->ncols);
977 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
978 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 static const struct got_error *view_init_hsplit(struct tog_view *, int);
982 static const struct got_error *request_log_commits(struct tog_view *);
983 static const struct got_error *offset_selection_down(struct tog_view *);
984 static void offset_selection_up(struct tog_view *);
985 static void view_get_split(struct tog_view *, int *, int *);
987 static const struct got_error *
988 view_resize(struct tog_view *view)
990 const struct got_error *err = NULL;
991 int dif, nlines, ncols;
993 dif = LINES - view->lines; /* line difference */
995 if (view->lines > LINES)
996 nlines = view->nlines - (view->lines - LINES);
998 nlines = view->nlines + (LINES - view->lines);
999 if (view->cols > COLS)
1000 ncols = view->ncols - (view->cols - COLS);
1002 ncols = view->ncols + (COLS - view->cols);
1005 int hs = view->child->begin_y;
1007 if (!view_is_fullscreen(view))
1008 view->child->begin_x = view_split_begin_x(view->begin_x);
1009 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1010 view->child->begin_x == 0) {
1013 view_fullscreen(view->child);
1014 if (view->child->focussed)
1015 show_panel(view->child->panel);
1017 show_panel(view->panel);
1019 ncols = view->child->begin_x;
1021 view_splitscreen(view->child);
1022 show_panel(view->child->panel);
1025 * XXX This is ugly and needs to be moved into the above
1026 * logic but "works" for now and my attempts at moving it
1027 * break either 'tab' or 'F' key maps in horizontal splits.
1030 err = view_splitscreen(view->child);
1033 if (dif < 0) { /* top split decreased */
1034 err = offset_selection_down(view);
1041 show_panel(view->child->panel);
1042 nlines = view->nlines;
1044 } else if (view->parent == NULL)
1047 if (view->resize && dif > 0) {
1048 err = view->resize(view, dif);
1053 if (wresize(view->window, nlines, ncols) == ERR)
1054 return got_error_from_errno("wresize");
1055 if (replace_panel(view->panel, view->window) == ERR)
1056 return got_error_from_errno("replace_panel");
1057 wclear(view->window);
1059 view->nlines = nlines;
1060 view->ncols = ncols;
1061 view->lines = LINES;
1067 static const struct got_error *
1068 resize_log_view(struct tog_view *view, int increase)
1070 struct tog_log_view_state *s = &view->state.log;
1071 const struct got_error *err = NULL;
1074 if (s->selected_entry)
1075 n = s->selected_entry->idx + view->lines - s->selected;
1078 * Request commits to account for the increased
1079 * height so we have enough to populate the view.
1081 if (s->commits->ncommits < n) {
1082 view->nscrolled = n - s->commits->ncommits + increase + 1;
1083 err = request_log_commits(view);
1090 view_adjust_offset(struct tog_view *view, int n)
1095 if (view->parent && view->parent->offset) {
1096 if (view->parent->offset + n >= 0)
1097 view->parent->offset += n;
1099 view->parent->offset = 0;
1100 } else if (view->offset) {
1101 if (view->offset - n >= 0)
1108 static const struct got_error *
1109 view_resize_split(struct tog_view *view, int resize)
1111 const struct got_error *err = NULL;
1112 struct tog_view *v = NULL;
1119 if (!v->child || !view_is_splitscreen(v->child))
1122 v->resized = v->child->resized = resize; /* lock for resize event */
1124 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1125 if (v->child->resized_y)
1126 v->child->begin_y = v->child->resized_y;
1128 v->child->begin_y -= resize;
1130 v->child->begin_y += resize;
1131 if (v->child->begin_y < 3) {
1133 v->child->begin_y = 3;
1134 } else if (v->child->begin_y > LINES - 1) {
1136 v->child->begin_y = LINES - 1;
1139 v->child->ncols = COLS;
1140 view_adjust_offset(view, resize);
1141 err = view_init_hsplit(v, v->child->begin_y);
1144 v->child->resized_y = v->child->begin_y;
1146 if (v->child->resized_x)
1147 v->child->begin_x = v->child->resized_x;
1149 v->child->begin_x -= resize;
1151 v->child->begin_x += resize;
1152 if (v->child->begin_x < 11) {
1154 v->child->begin_x = 11;
1155 } else if (v->child->begin_x > COLS - 1) {
1157 v->child->begin_x = COLS - 1;
1159 v->child->resized_x = v->child->begin_x;
1162 v->child->mode = v->mode;
1163 v->child->nlines = v->lines - v->child->begin_y;
1164 v->child->ncols = v->cols - v->child->begin_x;
1167 err = view_fullscreen(v);
1170 err = view_splitscreen(v->child);
1174 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1175 err = offset_selection_down(v->child);
1181 err = v->resize(v, 0);
1182 else if (v->child->resize)
1183 err = v->child->resize(v->child, 0);
1185 v->resized = v->child->resized = 0;
1191 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1193 struct tog_view *v = src->child ? src->child : src;
1195 dst->resized_x = v->resized_x;
1196 dst->resized_y = v->resized_y;
1199 static const struct got_error *
1200 view_close_child(struct tog_view *view)
1202 const struct got_error *err = NULL;
1204 if (view->child == NULL)
1207 err = view_close(view->child);
1212 static const struct got_error *
1213 view_set_child(struct tog_view *view, struct tog_view *child)
1215 const struct got_error *err = NULL;
1217 view->child = child;
1218 child->parent = view;
1220 err = view_resize(view);
1224 if (view->child->resized_x || view->child->resized_y)
1225 err = view_resize_split(view, 0);
1230 static const struct got_error *view_dispatch_request(struct tog_view **,
1231 struct tog_view *, enum tog_view_type, int, int);
1233 static const struct got_error *
1234 view_request_new(struct tog_view **requested, struct tog_view *view,
1235 enum tog_view_type request)
1237 struct tog_view *new_view = NULL;
1238 const struct got_error *err;
1243 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1244 view_get_split(view, &y, &x);
1246 err = view_dispatch_request(&new_view, view, request, y, x);
1250 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1251 request != TOG_VIEW_HELP) {
1252 err = view_init_hsplit(view, y);
1258 new_view->focussed = 1;
1259 new_view->mode = view->mode;
1260 new_view->nlines = request == TOG_VIEW_HELP ?
1261 view->lines : view->lines - y;
1263 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1264 view_transfer_size(new_view, view);
1265 err = view_close_child(view);
1268 err = view_set_child(view, new_view);
1271 view->focus_child = 1;
1273 *requested = new_view;
1279 tog_resizeterm(void)
1282 struct winsize size;
1284 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1285 cols = 80; /* Default */
1289 lines = size.ws_row;
1291 resize_term(lines, cols);
1294 static const struct got_error *
1295 view_search_start(struct tog_view *view)
1297 const struct got_error *err = NULL;
1298 struct tog_view *v = view;
1302 if (view->search_started) {
1303 regfree(&view->regex);
1304 view->searching = 0;
1305 memset(&view->regmatch, 0, sizeof(view->regmatch));
1307 view->search_started = 0;
1309 if (view->nlines < 1)
1312 if (view_is_hsplit_top(view))
1314 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1317 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1318 wclrtoeol(v->window);
1320 nodelay(v->window, FALSE); /* block for search term input */
1323 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1324 wrefresh(v->window);
1327 nodelay(v->window, TRUE);
1331 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1332 err = view->search_start(view);
1334 regfree(&view->regex);
1337 view->search_started = 1;
1338 view->searching = TOG_SEARCH_FORWARD;
1339 view->search_next_done = 0;
1340 view->search_next(view);
1346 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1347 static const struct got_error *
1348 switch_split(struct tog_view *view)
1350 const struct got_error *err = NULL;
1351 struct tog_view *v = NULL;
1358 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1359 v->mode = TOG_VIEW_SPLIT_VERT;
1361 v->mode = TOG_VIEW_SPLIT_HRZN;
1365 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1366 v->mode = TOG_VIEW_SPLIT_NONE;
1368 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1369 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1370 v->child->begin_y = v->child->resized_y;
1371 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1372 v->child->begin_x = v->child->resized_x;
1375 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1377 v->child->ncols = COLS;
1378 v->child->nscrolled = LINES - v->child->nlines;
1380 err = view_init_hsplit(v, v->child->begin_y);
1384 v->child->mode = v->mode;
1385 v->child->nlines = v->lines - v->child->begin_y;
1388 err = view_fullscreen(v);
1391 err = view_splitscreen(v->child);
1395 if (v->mode == TOG_VIEW_SPLIT_NONE)
1396 v->mode = TOG_VIEW_SPLIT_VERT;
1397 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1398 err = offset_selection_down(v);
1401 err = offset_selection_down(v->child);
1405 offset_selection_up(v);
1406 offset_selection_up(v->child);
1409 err = v->resize(v, 0);
1410 else if (v->child->resize)
1411 err = v->child->resize(v->child, 0);
1417 * Compute view->count from numeric input. Assign total to view->count and
1418 * return first non-numeric key entered.
1421 get_compound_key(struct tog_view *view, int c)
1423 struct tog_view *v = view;
1426 if (view_is_hsplit_top(view))
1428 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1432 cbreak(); /* block for input */
1433 nodelay(view->window, FALSE);
1434 wmove(v->window, v->nlines - 1, 0);
1435 wclrtoeol(v->window);
1436 waddch(v->window, ':');
1439 x = getcurx(v->window);
1440 if (x != ERR && x < view->ncols) {
1441 waddch(v->window, c);
1442 wrefresh(v->window);
1446 * Don't overflow. Max valid request should be the greatest
1447 * between the longest and total lines; cap at 10 million.
1452 n = n * 10 + (c - '0');
1453 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1455 if (c == 'G' || c == 'g') { /* nG key map */
1456 view->gline = view->hiline = n;
1461 /* Massage excessive or inapplicable values at the input handler. */
1467 static const struct got_error *
1468 view_input(struct tog_view **new, int *done, struct tog_view *view,
1469 struct tog_view_list_head *views)
1471 const struct got_error *err = NULL;
1477 /* Clear "no matches" indicator. */
1478 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1479 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1480 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1484 if (view->searching && !view->search_next_done) {
1485 errcode = pthread_mutex_unlock(&tog_mutex);
1487 return got_error_set_errno(errcode,
1488 "pthread_mutex_unlock");
1490 errcode = pthread_mutex_lock(&tog_mutex);
1492 return got_error_set_errno(errcode,
1493 "pthread_mutex_lock");
1494 view->search_next(view);
1498 /* Allow threads to make progress while we are waiting for input. */
1499 errcode = pthread_mutex_unlock(&tog_mutex);
1501 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1502 /* If we have an unfinished count, let C-g or backspace abort. */
1503 if (view->count && --view->count) {
1505 nodelay(view->window, TRUE);
1506 ch = wgetch(view->window);
1507 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1512 ch = wgetch(view->window);
1513 if (ch >= '1' && ch <= '9')
1514 view->ch = ch = get_compound_key(view, ch);
1516 if (view->hiline && ch != ERR && ch != 0)
1517 view->hiline = 0; /* key pressed, clear line highlight */
1518 nodelay(view->window, TRUE);
1519 errcode = pthread_mutex_lock(&tog_mutex);
1521 return got_error_set_errno(errcode, "pthread_mutex_lock");
1523 if (tog_sigwinch_received || tog_sigcont_received) {
1525 tog_sigwinch_received = 0;
1526 tog_sigcont_received = 0;
1527 TAILQ_FOREACH(v, views, entry) {
1528 err = view_resize(v);
1531 err = v->input(new, v, KEY_RESIZE);
1535 err = view_resize(v->child);
1538 err = v->child->input(new, v->child,
1542 if (v->child->resized_x || v->child->resized_y) {
1543 err = view_resize_split(v, 0);
1555 if (view->type == TOG_VIEW_HELP)
1556 err = view->reset(view);
1558 err = view_request_new(new, view, TOG_VIEW_HELP);
1564 view->child->focussed = 1;
1565 view->focus_child = 1;
1566 } else if (view->parent) {
1568 view->parent->focussed = 1;
1569 view->parent->focus_child = 0;
1570 if (!view_is_splitscreen(view)) {
1571 if (view->parent->resize) {
1572 err = view->parent->resize(view->parent,
1577 offset_selection_up(view->parent);
1578 err = view_fullscreen(view->parent);
1585 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1586 if (view->parent->resize) {
1587 /* might need more commits to fill fullscreen */
1588 err = view->parent->resize(view->parent, 0);
1592 offset_selection_up(view->parent);
1594 err = view->input(new, view, ch);
1602 if (view_is_parent_view(view)) {
1603 if (view->child == NULL)
1605 if (view_is_splitscreen(view->child)) {
1607 view->child->focussed = 1;
1608 err = view_fullscreen(view->child);
1610 err = view_splitscreen(view->child);
1612 err = view_resize_split(view, 0);
1616 err = view->child->input(new, view->child,
1619 if (view_is_splitscreen(view)) {
1620 view->parent->focussed = 0;
1622 err = view_fullscreen(view);
1624 err = view_splitscreen(view);
1625 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1626 err = view_resize(view->parent);
1628 err = view_resize_split(view, 0);
1632 err = view->input(new, view, KEY_RESIZE);
1637 err = view->resize(view, 0);
1642 err = offset_selection_down(view->parent);
1644 err = offset_selection_down(view);
1648 err = switch_split(view);
1651 err = view_resize_split(view, -1);
1654 err = view_resize_split(view, 1);
1660 if (view->search_start)
1661 view_search_start(view);
1663 err = view->input(new, view, ch);
1667 if (view->search_started && view->search_next) {
1668 view->searching = (ch == 'n' ?
1669 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1670 view->search_next_done = 0;
1671 view->search_next(view);
1673 err = view->input(new, view, ch);
1676 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1677 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1679 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1680 TAILQ_FOREACH(v, views, entry) {
1686 if (v->child && v->child->reset) {
1687 err = v->child->reset(v->child);
1694 err = view->input(new, view, ch);
1702 view_needs_focus_indication(struct tog_view *view)
1704 if (view_is_parent_view(view)) {
1705 if (view->child == NULL || view->child->focussed)
1707 if (!view_is_splitscreen(view->child))
1709 } else if (!view_is_splitscreen(view))
1712 return view->focussed;
1715 static const struct got_error *
1716 view_loop(struct tog_view *view)
1718 const struct got_error *err = NULL;
1719 struct tog_view_list_head views;
1720 struct tog_view *new_view;
1722 int fast_refresh = 10;
1723 int done = 0, errcode;
1725 mode = getenv("TOG_VIEW_SPLIT_MODE");
1726 if (!mode || !(*mode == 'h' || *mode == 'H'))
1727 view->mode = TOG_VIEW_SPLIT_VERT;
1729 view->mode = TOG_VIEW_SPLIT_HRZN;
1731 errcode = pthread_mutex_lock(&tog_mutex);
1733 return got_error_set_errno(errcode, "pthread_mutex_lock");
1736 TAILQ_INSERT_HEAD(&views, view, entry);
1739 err = view->show(view);
1744 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1745 !tog_fatal_signal_received()) {
1746 /* Refresh fast during initialization, then become slower. */
1747 if (fast_refresh && fast_refresh-- == 0)
1748 halfdelay(10); /* switch to once per second */
1750 err = view_input(&new_view, &done, view, &views);
1754 struct tog_view *v, *prev = NULL;
1756 if (view_is_parent_view(view))
1757 prev = TAILQ_PREV(view, tog_view_list_head,
1759 else if (view->parent)
1760 prev = view->parent;
1763 view->parent->child = NULL;
1764 view->parent->focus_child = 0;
1765 /* Restore fullscreen line height. */
1766 view->parent->nlines = view->parent->lines;
1767 err = view_resize(view->parent);
1770 /* Make resized splits persist. */
1771 view_transfer_size(view->parent, view);
1773 TAILQ_REMOVE(&views, view, entry);
1775 err = view_close(view);
1780 TAILQ_FOREACH(v, &views, entry) {
1784 if (view == NULL && new_view == NULL) {
1785 /* No view has focus. Try to pick one. */
1788 else if (!TAILQ_EMPTY(&views)) {
1789 view = TAILQ_LAST(&views,
1790 tog_view_list_head);
1793 if (view->focus_child) {
1794 view->child->focussed = 1;
1802 struct tog_view *v, *t;
1803 /* Only allow one parent view per type. */
1804 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1805 if (v->type != new_view->type)
1807 TAILQ_REMOVE(&views, v, entry);
1808 err = view_close(v);
1813 TAILQ_INSERT_TAIL(&views, new_view, entry);
1817 if (view_is_parent_view(view)) {
1818 if (view->child && view->child->focussed)
1821 if (view->parent && view->parent->focussed)
1822 view = view->parent;
1824 show_panel(view->panel);
1825 if (view->child && view_is_splitscreen(view->child))
1826 show_panel(view->child->panel);
1827 if (view->parent && view_is_splitscreen(view)) {
1828 err = view->parent->show(view->parent);
1832 err = view->show(view);
1836 err = view->child->show(view->child);
1845 while (!TAILQ_EMPTY(&views)) {
1846 const struct got_error *close_err;
1847 view = TAILQ_FIRST(&views);
1848 TAILQ_REMOVE(&views, view, entry);
1849 close_err = view_close(view);
1850 if (close_err && err == NULL)
1854 errcode = pthread_mutex_unlock(&tog_mutex);
1855 if (errcode && err == NULL)
1856 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1866 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1871 /* Create newly allocated wide-character string equivalent to a byte string. */
1872 static const struct got_error *
1873 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1876 const struct got_error *err = NULL;
1879 *wlen = mbstowcs(NULL, s, 0);
1880 if (*wlen == (size_t)-1) {
1882 if (errno != EILSEQ)
1883 return got_error_from_errno("mbstowcs");
1885 /* byte string invalid in current encoding; try to "fix" it */
1886 err = got_mbsavis(&vis, &vislen, s);
1889 *wlen = mbstowcs(NULL, vis, 0);
1890 if (*wlen == (size_t)-1) {
1891 err = got_error_from_errno("mbstowcs"); /* give up */
1896 *ws = calloc(*wlen + 1, sizeof(**ws));
1898 err = got_error_from_errno("calloc");
1902 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1903 err = got_error_from_errno("mbstowcs");
1914 static const struct got_error *
1915 expand_tab(char **ptr, const char *src)
1918 size_t len, n, idx = 0, sz = 0;
1921 n = len = strlen(src);
1922 dst = malloc(n + 1);
1924 return got_error_from_errno("malloc");
1926 while (idx < len && src[idx]) {
1927 const char c = src[idx];
1930 size_t nb = TABSIZE - sz % TABSIZE;
1933 p = realloc(dst, n + nb);
1936 return got_error_from_errno("realloc");
1941 memset(dst + sz, ' ', nb);
1944 dst[sz++] = src[idx];
1954 * Advance at most n columns from wline starting at offset off.
1955 * Return the index to the first character after the span operation.
1956 * Return the combined column width of all spanned wide character in
1960 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1962 int width, i, cols = 0;
1969 for (i = off; wline[i] != L'\0'; ++i) {
1970 if (wline[i] == L'\t')
1971 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1973 width = wcwidth(wline[i]);
1980 if (cols + width > n)
1990 * Format a line for display, ensuring that it won't overflow a width limit.
1991 * With scrolling, the width returned refers to the scrolled version of the
1992 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1994 static const struct got_error *
1995 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1996 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1998 const struct got_error *err = NULL;
2000 wchar_t *wline = NULL;
2009 err = expand_tab(&exstr, line);
2014 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2019 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2021 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2022 wline[wlen - 1] = L'\0';
2025 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2026 wline[wlen - 1] = L'\0';
2030 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2036 *scrollxp = scrollx;
2044 static const struct got_error*
2045 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2046 struct got_object_id *id, struct got_repository *repo)
2048 static const struct got_error *err = NULL;
2049 struct got_reflist_entry *re;
2055 TAILQ_FOREACH(re, refs, entry) {
2056 struct got_tag_object *tag = NULL;
2057 struct got_object_id *ref_id;
2060 name = got_ref_get_name(re->ref);
2061 if (strcmp(name, GOT_REF_HEAD) == 0)
2063 if (strncmp(name, "refs/", 5) == 0)
2065 if (strncmp(name, "got/", 4) == 0 &&
2066 strncmp(name, "got/backup/", 11) != 0)
2068 if (strncmp(name, "heads/", 6) == 0)
2070 if (strncmp(name, "remotes/", 8) == 0) {
2072 s = strstr(name, "/" GOT_REF_HEAD);
2073 if (s != NULL && s[strlen(s)] == '\0')
2076 err = got_ref_resolve(&ref_id, repo, re->ref);
2079 if (strncmp(name, "tags/", 5) == 0) {
2080 err = got_object_open_as_tag(&tag, repo, ref_id);
2082 if (err->code != GOT_ERR_OBJ_TYPE) {
2086 /* Ref points at something other than a tag. */
2091 cmp = got_object_id_cmp(tag ?
2092 got_object_tag_get_object_id(tag) : ref_id, id);
2095 got_object_tag_close(tag);
2099 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2100 s ? ", " : "", name) == -1) {
2101 err = got_error_from_errno("asprintf");
2112 static const struct got_error *
2113 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2118 smallerthan = strchr(author, '<');
2119 if (smallerthan && smallerthan[1] != '\0')
2120 author = smallerthan + 1;
2121 author[strcspn(author, "@>")] = '\0';
2122 return format_line(wauthor, author_width, NULL, author, 0, limit,
2126 static const struct got_error *
2127 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2128 struct got_object_id *id, const size_t date_display_cols,
2129 int author_display_cols)
2131 struct tog_log_view_state *s = &view->state.log;
2132 const struct got_error *err = NULL;
2133 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2134 char *logmsg0 = NULL, *logmsg = NULL;
2135 char *author = NULL;
2136 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2137 int author_width, logmsg_width;
2138 char *newline, *line = NULL;
2139 int col, limit, scrollx;
2140 const int avail = view->ncols;
2142 time_t committer_time;
2143 struct tog_color *tc;
2145 committer_time = got_object_commit_get_committer_time(commit);
2146 if (gmtime_r(&committer_time, &tm) == NULL)
2147 return got_error_from_errno("gmtime_r");
2148 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2149 return got_error(GOT_ERR_NO_SPACE);
2151 if (avail <= date_display_cols)
2152 limit = MIN(sizeof(datebuf) - 1, avail);
2154 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2155 tc = get_color(&s->colors, TOG_COLOR_DATE);
2157 wattr_on(view->window,
2158 COLOR_PAIR(tc->colorpair), NULL);
2159 waddnstr(view->window, datebuf, limit);
2161 wattr_off(view->window,
2162 COLOR_PAIR(tc->colorpair), NULL);
2169 err = got_object_id_str(&id_str, id);
2172 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2174 wattr_on(view->window,
2175 COLOR_PAIR(tc->colorpair), NULL);
2176 wprintw(view->window, "%.8s ", id_str);
2178 wattr_off(view->window,
2179 COLOR_PAIR(tc->colorpair), NULL);
2186 if (s->use_committer)
2187 author = strdup(got_object_commit_get_committer(commit));
2189 author = strdup(got_object_commit_get_author(commit));
2190 if (author == NULL) {
2191 err = got_error_from_errno("strdup");
2194 err = format_author(&wauthor, &author_width, author, avail - col, col);
2197 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2199 wattr_on(view->window,
2200 COLOR_PAIR(tc->colorpair), NULL);
2201 waddwstr(view->window, wauthor);
2202 col += author_width;
2203 while (col < avail && author_width < author_display_cols + 2) {
2204 waddch(view->window, ' ');
2209 wattr_off(view->window,
2210 COLOR_PAIR(tc->colorpair), NULL);
2214 err = got_object_commit_get_logmsg(&logmsg0, commit);
2218 while (*logmsg == '\n')
2220 newline = strchr(logmsg, '\n');
2223 limit = avail - col;
2224 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2225 limit--; /* for the border */
2226 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2230 waddwstr(view->window, &wlogmsg[scrollx]);
2231 col += MAX(logmsg_width, 0);
2232 while (col < avail) {
2233 waddch(view->window, ' ');
2245 static struct commit_queue_entry *
2246 alloc_commit_queue_entry(struct got_commit_object *commit,
2247 struct got_object_id *id)
2249 struct commit_queue_entry *entry;
2250 struct got_object_id *dup;
2252 entry = calloc(1, sizeof(*entry));
2256 dup = got_object_id_dup(id);
2263 entry->commit = commit;
2268 pop_commit(struct commit_queue *commits)
2270 struct commit_queue_entry *entry;
2272 entry = TAILQ_FIRST(&commits->head);
2273 TAILQ_REMOVE(&commits->head, entry, entry);
2274 got_object_commit_close(entry->commit);
2275 commits->ncommits--;
2281 free_commits(struct commit_queue *commits)
2283 while (!TAILQ_EMPTY(&commits->head))
2284 pop_commit(commits);
2287 static const struct got_error *
2288 match_commit(int *have_match, struct got_object_id *id,
2289 struct got_commit_object *commit, regex_t *regex)
2291 const struct got_error *err = NULL;
2292 regmatch_t regmatch;
2293 char *id_str = NULL, *logmsg = NULL;
2297 err = got_object_id_str(&id_str, id);
2301 err = got_object_commit_get_logmsg(&logmsg, commit);
2305 if (regexec(regex, got_object_commit_get_author(commit), 1,
2306 ®match, 0) == 0 ||
2307 regexec(regex, got_object_commit_get_committer(commit), 1,
2308 ®match, 0) == 0 ||
2309 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2310 regexec(regex, logmsg, 1, ®match, 0) == 0)
2318 static const struct got_error *
2319 queue_commits(struct tog_log_thread_args *a)
2321 const struct got_error *err = NULL;
2324 * We keep all commits open throughout the lifetime of the log
2325 * view in order to avoid having to re-fetch commits from disk
2326 * while updating the display.
2329 struct got_object_id id;
2330 struct got_commit_object *commit;
2331 struct commit_queue_entry *entry;
2332 int limit_match = 0;
2335 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2340 err = got_object_open_as_commit(&commit, a->repo, &id);
2343 entry = alloc_commit_queue_entry(commit, &id);
2344 if (entry == NULL) {
2345 err = got_error_from_errno("alloc_commit_queue_entry");
2349 errcode = pthread_mutex_lock(&tog_mutex);
2351 err = got_error_set_errno(errcode,
2352 "pthread_mutex_lock");
2356 entry->idx = a->real_commits->ncommits;
2357 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2358 a->real_commits->ncommits++;
2361 err = match_commit(&limit_match, &id, commit,
2367 struct commit_queue_entry *matched;
2369 matched = alloc_commit_queue_entry(
2370 entry->commit, entry->id);
2371 if (matched == NULL) {
2372 err = got_error_from_errno(
2373 "alloc_commit_queue_entry");
2376 matched->commit = entry->commit;
2377 got_object_commit_retain(entry->commit);
2379 matched->idx = a->limit_commits->ncommits;
2380 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2382 a->limit_commits->ncommits++;
2386 * This is how we signal log_thread() that we
2387 * have found a match, and that it should be
2388 * counted as a new entry for the view.
2390 a->limit_match = limit_match;
2393 if (*a->searching == TOG_SEARCH_FORWARD &&
2394 !*a->search_next_done) {
2396 err = match_commit(&have_match, &id, commit, a->regex);
2401 if (limit_match && have_match)
2402 *a->search_next_done =
2403 TOG_SEARCH_HAVE_MORE;
2404 } else if (have_match)
2405 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2408 errcode = pthread_mutex_unlock(&tog_mutex);
2409 if (errcode && err == NULL)
2410 err = got_error_set_errno(errcode,
2411 "pthread_mutex_unlock");
2414 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2420 select_commit(struct tog_log_view_state *s)
2422 struct commit_queue_entry *entry;
2425 entry = s->first_displayed_entry;
2427 if (ncommits == s->selected) {
2428 s->selected_entry = entry;
2431 entry = TAILQ_NEXT(entry, entry);
2436 static const struct got_error *
2437 draw_commits(struct tog_view *view)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct commit_queue_entry *entry = s->selected_entry;
2442 int limit = view->nlines;
2444 int ncommits, author_cols = 4;
2445 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2446 char *refs_str = NULL;
2448 struct tog_color *tc;
2449 static const size_t date_display_cols = 12;
2451 if (view_is_hsplit_top(view))
2452 --limit; /* account for border */
2454 if (s->selected_entry &&
2455 !(view->searching && view->search_next_done == 0)) {
2456 struct got_reflist_head *refs;
2457 err = got_object_id_str(&id_str, s->selected_entry->id);
2460 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2461 s->selected_entry->id);
2463 err = build_refs_str(&refs_str, refs,
2464 s->selected_entry->id, s->repo);
2470 if (s->thread_args.commits_needed == 0)
2471 halfdelay(10); /* disable fast refresh */
2473 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2474 if (asprintf(&ncommits_str, " [%d/%d] %s",
2475 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2476 (view->searching && !view->search_next_done) ?
2477 "searching..." : "loading...") == -1) {
2478 err = got_error_from_errno("asprintf");
2482 const char *search_str = NULL;
2483 const char *limit_str = NULL;
2485 if (view->searching) {
2486 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2487 search_str = "no more matches";
2488 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2489 search_str = "no matches found";
2490 else if (!view->search_next_done)
2491 search_str = "searching...";
2494 if (s->limit_view && s->commits->ncommits == 0)
2495 limit_str = "no matches found";
2497 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2498 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2499 search_str ? search_str : (refs_str ? refs_str : ""),
2500 limit_str ? limit_str : "") == -1) {
2501 err = got_error_from_errno("asprintf");
2506 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2507 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2508 "........................................",
2509 s->in_repo_path, ncommits_str) == -1) {
2510 err = got_error_from_errno("asprintf");
2514 } else if (asprintf(&header, "commit %s%s",
2515 id_str ? id_str : "........................................",
2516 ncommits_str) == -1) {
2517 err = got_error_from_errno("asprintf");
2521 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2525 werase(view->window);
2527 if (view_needs_focus_indication(view))
2528 wstandout(view->window);
2529 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2531 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 waddwstr(view->window, wline);
2533 while (width < view->ncols) {
2534 waddch(view->window, ' ');
2538 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2539 if (view_needs_focus_indication(view))
2540 wstandend(view->window);
2545 /* Grow author column size if necessary, and set view->maxx. */
2546 entry = s->first_displayed_entry;
2550 struct got_commit_object *c = entry->commit;
2551 char *author, *eol, *msg, *msg0;
2552 wchar_t *wauthor, *wmsg;
2554 if (ncommits >= limit - 1)
2556 if (s->use_committer)
2557 author = strdup(got_object_commit_get_committer(c));
2559 author = strdup(got_object_commit_get_author(c));
2560 if (author == NULL) {
2561 err = got_error_from_errno("strdup");
2564 err = format_author(&wauthor, &width, author, COLS,
2566 if (author_cols < width)
2567 author_cols = width;
2572 err = got_object_commit_get_logmsg(&msg0, c);
2576 while (*msg == '\n')
2578 if ((eol = strchr(msg, '\n')))
2580 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2581 date_display_cols + author_cols, 0);
2584 view->maxx = MAX(view->maxx, width);
2588 entry = TAILQ_NEXT(entry, entry);
2591 entry = s->first_displayed_entry;
2592 s->last_displayed_entry = s->first_displayed_entry;
2595 if (ncommits >= limit - 1)
2597 if (ncommits == s->selected)
2598 wstandout(view->window);
2599 err = draw_commit(view, entry->commit, entry->id,
2600 date_display_cols, author_cols);
2601 if (ncommits == s->selected)
2602 wstandend(view->window);
2606 s->last_displayed_entry = entry;
2607 entry = TAILQ_NEXT(entry, entry);
2620 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2622 struct commit_queue_entry *entry;
2625 entry = TAILQ_FIRST(&s->commits->head);
2626 if (s->first_displayed_entry == entry)
2629 entry = s->first_displayed_entry;
2630 while (entry && nscrolled < maxscroll) {
2631 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2633 s->first_displayed_entry = entry;
2639 static const struct got_error *
2640 trigger_log_thread(struct tog_view *view, int wait)
2642 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2645 halfdelay(1); /* fast refresh while loading commits */
2647 while (!ta->log_complete && !tog_thread_error &&
2648 (ta->commits_needed > 0 || ta->load_all)) {
2649 /* Wake the log thread. */
2650 errcode = pthread_cond_signal(&ta->need_commits);
2652 return got_error_set_errno(errcode,
2653 "pthread_cond_signal");
2656 * The mutex will be released while the view loop waits
2657 * in wgetch(), at which time the log thread will run.
2662 /* Display progress update in log view. */
2663 show_log_view(view);
2667 /* Wait right here while next commit is being loaded. */
2668 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2670 return got_error_set_errno(errcode,
2671 "pthread_cond_wait");
2673 /* Display progress update in log view. */
2674 show_log_view(view);
2682 static const struct got_error *
2683 request_log_commits(struct tog_view *view)
2685 struct tog_log_view_state *state = &view->state.log;
2686 const struct got_error *err = NULL;
2688 if (state->thread_args.log_complete)
2691 state->thread_args.commits_needed += view->nscrolled;
2692 err = trigger_log_thread(view, 1);
2693 view->nscrolled = 0;
2698 static const struct got_error *
2699 log_scroll_down(struct tog_view *view, int maxscroll)
2701 struct tog_log_view_state *s = &view->state.log;
2702 const struct got_error *err = NULL;
2703 struct commit_queue_entry *pentry;
2704 int nscrolled = 0, ncommits_needed;
2706 if (s->last_displayed_entry == NULL)
2709 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2710 if (s->commits->ncommits < ncommits_needed &&
2711 !s->thread_args.log_complete) {
2713 * Ask the log thread for required amount of commits.
2715 s->thread_args.commits_needed +=
2716 ncommits_needed - s->commits->ncommits;
2717 err = trigger_log_thread(view, 1);
2723 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2724 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2727 s->last_displayed_entry = pentry ?
2728 pentry : s->last_displayed_entry;;
2730 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2733 s->first_displayed_entry = pentry;
2734 } while (++nscrolled < maxscroll);
2736 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2737 view->nscrolled += nscrolled;
2739 view->nscrolled = 0;
2744 static const struct got_error *
2745 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2746 struct got_commit_object *commit, struct got_object_id *commit_id,
2747 struct tog_view *log_view, struct got_repository *repo)
2749 const struct got_error *err;
2750 struct got_object_qid *parent_id;
2751 struct tog_view *diff_view;
2753 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2754 if (diff_view == NULL)
2755 return got_error_from_errno("view_open");
2757 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2758 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2759 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2761 *new_view = diff_view;
2765 static const struct got_error *
2766 tree_view_visit_subtree(struct tog_tree_view_state *s,
2767 struct got_tree_object *subtree)
2769 struct tog_parent_tree *parent;
2771 parent = calloc(1, sizeof(*parent));
2773 return got_error_from_errno("calloc");
2775 parent->tree = s->tree;
2776 parent->first_displayed_entry = s->first_displayed_entry;
2777 parent->selected_entry = s->selected_entry;
2778 parent->selected = s->selected;
2779 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2782 s->first_displayed_entry = NULL;
2786 static const struct got_error *
2787 tree_view_walk_path(struct tog_tree_view_state *s,
2788 struct got_commit_object *commit, const char *path)
2790 const struct got_error *err = NULL;
2791 struct got_tree_object *tree = NULL;
2793 char *slash, *subpath = NULL;
2795 /* Walk the path and open corresponding tree objects. */
2798 struct got_tree_entry *te;
2799 struct got_object_id *tree_id;
2805 /* Ensure the correct subtree entry is selected. */
2806 slash = strchr(p, '/');
2808 te_name = strdup(p);
2810 te_name = strndup(p, slash - p);
2811 if (te_name == NULL) {
2812 err = got_error_from_errno("strndup");
2815 te = got_object_tree_find_entry(s->tree, te_name);
2817 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2822 s->first_displayed_entry = s->selected_entry = te;
2824 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2825 break; /* jump to this file's entry */
2827 slash = strchr(p, '/');
2829 subpath = strndup(path, slash - path);
2831 subpath = strdup(path);
2832 if (subpath == NULL) {
2833 err = got_error_from_errno("strdup");
2837 err = got_object_id_by_path(&tree_id, s->repo, commit,
2842 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2847 err = tree_view_visit_subtree(s, tree);
2849 got_object_tree_close(tree);
2863 static const struct got_error *
2864 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2865 struct commit_queue_entry *entry, const char *path,
2866 const char *head_ref_name, struct got_repository *repo)
2868 const struct got_error *err = NULL;
2869 struct tog_tree_view_state *s;
2870 struct tog_view *tree_view;
2872 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2873 if (tree_view == NULL)
2874 return got_error_from_errno("view_open");
2876 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2879 s = &tree_view->state.tree;
2881 *new_view = tree_view;
2883 if (got_path_is_root_dir(path))
2886 return tree_view_walk_path(s, entry->commit, path);
2889 static const struct got_error *
2890 block_signals_used_by_main_thread(void)
2895 if (sigemptyset(&sigset) == -1)
2896 return got_error_from_errno("sigemptyset");
2898 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2899 if (sigaddset(&sigset, SIGWINCH) == -1)
2900 return got_error_from_errno("sigaddset");
2901 if (sigaddset(&sigset, SIGCONT) == -1)
2902 return got_error_from_errno("sigaddset");
2903 if (sigaddset(&sigset, SIGINT) == -1)
2904 return got_error_from_errno("sigaddset");
2905 if (sigaddset(&sigset, SIGTERM) == -1)
2906 return got_error_from_errno("sigaddset");
2908 /* ncurses handles SIGTSTP */
2909 if (sigaddset(&sigset, SIGTSTP) == -1)
2910 return got_error_from_errno("sigaddset");
2912 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2914 return got_error_set_errno(errcode, "pthread_sigmask");
2920 log_thread(void *arg)
2922 const struct got_error *err = NULL;
2924 struct tog_log_thread_args *a = arg;
2928 * Sync startup with main thread such that we begin our
2929 * work once view_input() has released the mutex.
2931 errcode = pthread_mutex_lock(&tog_mutex);
2933 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2937 err = block_signals_used_by_main_thread();
2939 pthread_mutex_unlock(&tog_mutex);
2943 while (!done && !err && !tog_fatal_signal_received()) {
2944 errcode = pthread_mutex_unlock(&tog_mutex);
2946 err = got_error_set_errno(errcode,
2947 "pthread_mutex_unlock");
2950 err = queue_commits(a);
2952 if (err->code != GOT_ERR_ITER_COMPLETED)
2956 } else if (a->commits_needed > 0 && !a->load_all) {
2959 a->commits_needed--;
2961 a->commits_needed--;
2964 errcode = pthread_mutex_lock(&tog_mutex);
2966 err = got_error_set_errno(errcode,
2967 "pthread_mutex_lock");
2969 } else if (*a->quit)
2971 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2972 *a->first_displayed_entry =
2973 TAILQ_FIRST(&a->limit_commits->head);
2974 *a->selected_entry = *a->first_displayed_entry;
2975 } else if (*a->first_displayed_entry == NULL) {
2976 *a->first_displayed_entry =
2977 TAILQ_FIRST(&a->real_commits->head);
2978 *a->selected_entry = *a->first_displayed_entry;
2981 errcode = pthread_cond_signal(&a->commit_loaded);
2983 err = got_error_set_errno(errcode,
2984 "pthread_cond_signal");
2985 pthread_mutex_unlock(&tog_mutex);
2990 a->commits_needed = 0;
2992 if (a->commits_needed == 0 && !a->load_all) {
2993 errcode = pthread_cond_wait(&a->need_commits,
2996 err = got_error_set_errno(errcode,
2997 "pthread_cond_wait");
2998 pthread_mutex_unlock(&tog_mutex);
3006 a->log_complete = 1;
3007 errcode = pthread_mutex_unlock(&tog_mutex);
3009 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3012 tog_thread_error = 1;
3013 pthread_cond_signal(&a->commit_loaded);
3018 static const struct got_error *
3019 stop_log_thread(struct tog_log_view_state *s)
3021 const struct got_error *err = NULL, *thread_err = NULL;
3026 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3028 return got_error_set_errno(errcode,
3029 "pthread_cond_signal");
3030 errcode = pthread_mutex_unlock(&tog_mutex);
3032 return got_error_set_errno(errcode,
3033 "pthread_mutex_unlock");
3034 errcode = pthread_join(s->thread, (void **)&thread_err);
3036 return got_error_set_errno(errcode, "pthread_join");
3037 errcode = pthread_mutex_lock(&tog_mutex);
3039 return got_error_set_errno(errcode,
3040 "pthread_mutex_lock");
3041 s->thread = 0; //NULL;
3044 if (s->thread_args.repo) {
3045 err = got_repo_close(s->thread_args.repo);
3046 s->thread_args.repo = NULL;
3049 if (s->thread_args.pack_fds) {
3050 const struct got_error *pack_err =
3051 got_repo_pack_fds_close(s->thread_args.pack_fds);
3054 s->thread_args.pack_fds = NULL;
3057 if (s->thread_args.graph) {
3058 got_commit_graph_close(s->thread_args.graph);
3059 s->thread_args.graph = NULL;
3062 return err ? err : thread_err;
3065 static const struct got_error *
3066 close_log_view(struct tog_view *view)
3068 const struct got_error *err = NULL;
3069 struct tog_log_view_state *s = &view->state.log;
3072 err = stop_log_thread(s);
3074 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3075 if (errcode && err == NULL)
3076 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3078 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3079 if (errcode && err == NULL)
3080 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3082 free_commits(&s->limit_commits);
3083 free_commits(&s->real_commits);
3084 free(s->in_repo_path);
3085 s->in_repo_path = NULL;
3088 free(s->head_ref_name);
3089 s->head_ref_name = NULL;
3094 * We use two queues to implement the limit feature: first consists of
3095 * commits matching the current limit_regex; second is the real queue
3096 * of all known commits (real_commits). When the user starts limiting,
3097 * we swap queues such that all movement and displaying functionality
3098 * works with very slight change.
3100 static const struct got_error *
3101 limit_log_view(struct tog_view *view)
3103 struct tog_log_view_state *s = &view->state.log;
3104 struct commit_queue_entry *entry;
3105 struct tog_view *v = view;
3106 const struct got_error *err = NULL;
3110 if (view_is_hsplit_top(view))
3112 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3115 /* Get the pattern */
3116 wmove(v->window, v->nlines - 1, 0);
3117 wclrtoeol(v->window);
3118 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3119 nodelay(v->window, FALSE);
3122 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3125 nodelay(v->window, TRUE);
3129 if (*pattern == '\0') {
3131 * Safety measure for the situation where the user
3132 * resets limit without previously limiting anything.
3138 * User could have pressed Ctrl+L, which refreshed the
3139 * commit queues, it means we can't save previously
3140 * (before limit took place) displayed entries,
3141 * because they would point to already free'ed memory,
3142 * so we are forced to always select first entry of
3145 s->commits = &s->real_commits;
3146 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3147 s->selected_entry = s->first_displayed_entry;
3154 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3159 /* Clear the screen while loading limit view */
3160 s->first_displayed_entry = NULL;
3161 s->last_displayed_entry = NULL;
3162 s->selected_entry = NULL;
3163 s->commits = &s->limit_commits;
3165 /* Prepare limit queue for new search */
3166 free_commits(&s->limit_commits);
3167 s->limit_commits.ncommits = 0;
3169 /* First process commits, which are in queue already */
3170 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3173 err = match_commit(&have_match, entry->id,
3174 entry->commit, &s->limit_regex);
3179 struct commit_queue_entry *matched;
3181 matched = alloc_commit_queue_entry(entry->commit,
3183 if (matched == NULL) {
3184 err = got_error_from_errno(
3185 "alloc_commit_queue_entry");
3188 matched->commit = entry->commit;
3189 got_object_commit_retain(entry->commit);
3191 matched->idx = s->limit_commits.ncommits;
3192 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3194 s->limit_commits.ncommits++;
3198 /* Second process all the commits, until we fill the screen */
3199 if (s->limit_commits.ncommits < view->nlines - 1 &&
3200 !s->thread_args.log_complete) {
3201 s->thread_args.commits_needed +=
3202 view->nlines - s->limit_commits.ncommits - 1;
3203 err = trigger_log_thread(view, 1);
3208 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3209 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3215 static const struct got_error *
3216 search_start_log_view(struct tog_view *view)
3218 struct tog_log_view_state *s = &view->state.log;
3220 s->matched_entry = NULL;
3221 s->search_entry = NULL;
3225 static const struct got_error *
3226 search_next_log_view(struct tog_view *view)
3228 const struct got_error *err = NULL;
3229 struct tog_log_view_state *s = &view->state.log;
3230 struct commit_queue_entry *entry;
3232 /* Display progress update in log view. */
3233 show_log_view(view);
3237 if (s->search_entry) {
3239 errcode = pthread_mutex_unlock(&tog_mutex);
3241 return got_error_set_errno(errcode,
3242 "pthread_mutex_unlock");
3243 ch = wgetch(view->window);
3244 errcode = pthread_mutex_lock(&tog_mutex);
3246 return got_error_set_errno(errcode,
3247 "pthread_mutex_lock");
3248 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3252 if (view->searching == TOG_SEARCH_FORWARD)
3253 entry = TAILQ_NEXT(s->search_entry, entry);
3255 entry = TAILQ_PREV(s->search_entry,
3256 commit_queue_head, entry);
3257 } else if (s->matched_entry) {
3259 * If the user has moved the cursor after we hit a match,
3260 * the position from where we should continue searching
3261 * might have changed.
3263 if (view->searching == TOG_SEARCH_FORWARD)
3264 entry = TAILQ_NEXT(s->selected_entry, entry);
3266 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3269 entry = s->selected_entry;
3275 if (entry == NULL) {
3276 if (s->thread_args.log_complete ||
3277 view->searching == TOG_SEARCH_BACKWARD) {
3278 view->search_next_done =
3279 (s->matched_entry == NULL ?
3280 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3281 s->search_entry = NULL;
3285 * Poke the log thread for more commits and return,
3286 * allowing the main loop to make progress. Search
3287 * will resume at s->search_entry once we come back.
3289 s->thread_args.commits_needed++;
3290 return trigger_log_thread(view, 0);
3293 err = match_commit(&have_match, entry->id, entry->commit,
3298 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3299 s->matched_entry = entry;
3303 s->search_entry = entry;
3304 if (view->searching == TOG_SEARCH_FORWARD)
3305 entry = TAILQ_NEXT(entry, entry);
3307 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3310 if (s->matched_entry) {
3311 int cur = s->selected_entry->idx;
3312 while (cur < s->matched_entry->idx) {
3313 err = input_log_view(NULL, view, KEY_DOWN);
3318 while (cur > s->matched_entry->idx) {
3319 err = input_log_view(NULL, view, KEY_UP);
3326 s->search_entry = NULL;
3331 static const struct got_error *
3332 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3333 struct got_repository *repo, const char *head_ref_name,
3334 const char *in_repo_path, int log_branches)
3336 const struct got_error *err = NULL;
3337 struct tog_log_view_state *s = &view->state.log;
3338 struct got_repository *thread_repo = NULL;
3339 struct got_commit_graph *thread_graph = NULL;
3342 if (in_repo_path != s->in_repo_path) {
3343 free(s->in_repo_path);
3344 s->in_repo_path = strdup(in_repo_path);
3345 if (s->in_repo_path == NULL)
3346 return got_error_from_errno("strdup");
3349 /* The commit queue only contains commits being displayed. */
3350 TAILQ_INIT(&s->real_commits.head);
3351 s->real_commits.ncommits = 0;
3352 s->commits = &s->real_commits;
3354 TAILQ_INIT(&s->limit_commits.head);
3356 s->limit_commits.ncommits = 0;
3359 if (head_ref_name) {
3360 s->head_ref_name = strdup(head_ref_name);
3361 if (s->head_ref_name == NULL) {
3362 err = got_error_from_errno("strdup");
3366 s->start_id = got_object_id_dup(start_id);
3367 if (s->start_id == NULL) {
3368 err = got_error_from_errno("got_object_id_dup");
3371 s->log_branches = log_branches;
3372 s->use_committer = 1;
3374 STAILQ_INIT(&s->colors);
3375 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3376 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3377 get_color_value("TOG_COLOR_COMMIT"));
3380 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3381 get_color_value("TOG_COLOR_AUTHOR"));
3383 free_colors(&s->colors);
3386 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3387 get_color_value("TOG_COLOR_DATE"));
3389 free_colors(&s->colors);
3394 view->show = show_log_view;
3395 view->input = input_log_view;
3396 view->resize = resize_log_view;
3397 view->close = close_log_view;
3398 view->search_start = search_start_log_view;
3399 view->search_next = search_next_log_view;
3401 if (s->thread_args.pack_fds == NULL) {
3402 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3406 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3407 s->thread_args.pack_fds);
3410 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3414 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3415 s->repo, NULL, NULL);
3419 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3421 err = got_error_set_errno(errcode, "pthread_cond_init");
3424 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3426 err = got_error_set_errno(errcode, "pthread_cond_init");
3430 s->thread_args.commits_needed = view->nlines;
3431 s->thread_args.graph = thread_graph;
3432 s->thread_args.real_commits = &s->real_commits;
3433 s->thread_args.limit_commits = &s->limit_commits;
3434 s->thread_args.in_repo_path = s->in_repo_path;
3435 s->thread_args.start_id = s->start_id;
3436 s->thread_args.repo = thread_repo;
3437 s->thread_args.log_complete = 0;
3438 s->thread_args.quit = &s->quit;
3439 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3440 s->thread_args.selected_entry = &s->selected_entry;
3441 s->thread_args.searching = &view->searching;
3442 s->thread_args.search_next_done = &view->search_next_done;
3443 s->thread_args.regex = &view->regex;
3444 s->thread_args.limiting = &s->limit_view;
3445 s->thread_args.limit_regex = &s->limit_regex;
3446 s->thread_args.limit_commits = &s->limit_commits;
3449 close_log_view(view);
3453 static const struct got_error *
3454 show_log_view(struct tog_view *view)
3456 const struct got_error *err;
3457 struct tog_log_view_state *s = &view->state.log;
3459 if (s->thread == 0) { //NULL) {
3460 int errcode = pthread_create(&s->thread, NULL, log_thread,
3463 return got_error_set_errno(errcode, "pthread_create");
3464 if (s->thread_args.commits_needed > 0) {
3465 err = trigger_log_thread(view, 1);
3471 return draw_commits(view);
3475 log_move_cursor_up(struct tog_view *view, int page, int home)
3477 struct tog_log_view_state *s = &view->state.log;
3479 if (s->first_displayed_entry == NULL)
3481 if (s->selected_entry->idx == 0)
3484 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3486 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3488 if (!page && !home && s->selected > 0)
3491 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3497 static const struct got_error *
3498 log_move_cursor_down(struct tog_view *view, int page)
3500 struct tog_log_view_state *s = &view->state.log;
3501 const struct got_error *err = NULL;
3502 int eos = view->nlines - 2;
3504 if (s->first_displayed_entry == NULL)
3507 if (s->thread_args.log_complete &&
3508 s->selected_entry->idx >= s->commits->ncommits - 1)
3511 if (view_is_hsplit_top(view))
3512 --eos; /* border consumes the last line */
3515 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3518 err = log_scroll_down(view, 1);
3519 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3520 struct commit_queue_entry *entry;
3524 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3525 s->last_displayed_entry = entry;
3526 for (n = 0; n <= eos; n++) {
3529 s->first_displayed_entry = entry;
3530 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3533 s->selected = n - 1;
3535 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3536 s->thread_args.log_complete)
3537 s->selected += MIN(page,
3538 s->commits->ncommits - s->selected_entry->idx - 1);
3540 err = log_scroll_down(view, page);
3546 * We might necessarily overshoot in horizontal
3547 * splits; if so, select the last displayed commit.
3549 if (s->first_displayed_entry && s->last_displayed_entry) {
3550 s->selected = MIN(s->selected,
3551 s->last_displayed_entry->idx -
3552 s->first_displayed_entry->idx);
3557 if (s->thread_args.log_complete &&
3558 s->selected_entry->idx == s->commits->ncommits - 1)
3565 view_get_split(struct tog_view *view, int *y, int *x)
3570 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3571 if (view->child && view->child->resized_y)
3572 *y = view->child->resized_y;
3573 else if (view->resized_y)
3574 *y = view->resized_y;
3576 *y = view_split_begin_y(view->lines);
3577 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3578 if (view->child && view->child->resized_x)
3579 *x = view->child->resized_x;
3580 else if (view->resized_x)
3581 *x = view->resized_x;
3583 *x = view_split_begin_x(view->begin_x);
3587 /* Split view horizontally at y and offset view->state->selected line. */
3588 static const struct got_error *
3589 view_init_hsplit(struct tog_view *view, int y)
3591 const struct got_error *err = NULL;
3595 err = view_resize(view);
3599 err = offset_selection_down(view);
3604 static const struct got_error *
3605 log_goto_line(struct tog_view *view, int nlines)
3607 const struct got_error *err = NULL;
3608 struct tog_log_view_state *s = &view->state.log;
3609 int g, idx = s->selected_entry->idx;
3611 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3617 if (g >= s->first_displayed_entry->idx + 1 &&
3618 g <= s->last_displayed_entry->idx + 1 &&
3619 g - s->first_displayed_entry->idx - 1 < nlines) {
3620 s->selected = g - s->first_displayed_entry->idx - 1;
3626 err = log_move_cursor_down(view, g - idx - 1);
3627 if (!err && g > s->selected_entry->idx + 1)
3628 err = log_move_cursor_down(view,
3629 g - s->first_displayed_entry->idx - 1);
3632 } else if (idx + 1 > g)
3633 log_move_cursor_up(view, idx - g + 1, 0);
3635 if (g < nlines && s->first_displayed_entry->idx == 0)
3636 s->selected = g - 1;
3643 static const struct got_error *
3644 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3646 const struct got_error *err = NULL;
3647 struct tog_log_view_state *s = &view->state.log;
3650 if (s->thread_args.load_all) {
3651 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3652 s->thread_args.load_all = 0;
3653 else if (s->thread_args.log_complete) {
3654 err = log_move_cursor_down(view, s->commits->ncommits);
3655 s->thread_args.load_all = 0;
3661 eos = nscroll = view->nlines - 1;
3662 if (view_is_hsplit_top(view))
3666 return log_goto_line(view, eos);
3670 err = limit_log_view(view);
3679 view->x = MAX(view->maxx - view->ncols / 2, 0);
3684 if (view->x + view->ncols / 2 < view->maxx)
3685 view->x += 2; /* move two columns right */
3691 view->x -= MIN(view->x, 2); /* move two columns back */
3700 log_move_cursor_up(view, 0, 0);
3705 log_move_cursor_up(view, 0, 1);
3715 log_move_cursor_up(view, nscroll, 0);
3722 err = log_move_cursor_down(view, 0);
3725 s->use_committer = !s->use_committer;
3730 /* We don't know yet how many commits, so we're forced to
3731 * traverse them all. */
3733 s->thread_args.load_all = 1;
3734 if (!s->thread_args.log_complete)
3735 return trigger_log_thread(view, 0);
3736 err = log_move_cursor_down(view, s->commits->ncommits);
3737 s->thread_args.load_all = 0;
3748 err = log_move_cursor_down(view, nscroll);
3751 if (s->selected > view->nlines - 2)
3752 s->selected = view->nlines - 2;
3753 if (s->selected > s->commits->ncommits - 1)
3754 s->selected = s->commits->ncommits - 1;
3756 if (s->commits->ncommits < view->nlines - 1 &&
3757 !s->thread_args.log_complete) {
3758 s->thread_args.commits_needed += (view->nlines - 1) -
3759 s->commits->ncommits;
3760 err = trigger_log_thread(view, 1);
3766 if (s->selected_entry == NULL)
3768 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3772 if (s->selected_entry == NULL)
3774 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3780 if (ch == KEY_BACKSPACE &&
3781 got_path_is_root_dir(s->in_repo_path))
3783 err = stop_log_thread(s);
3786 if (ch == KEY_BACKSPACE) {
3788 err = got_path_dirname(&parent_path, s->in_repo_path);
3791 free(s->in_repo_path);
3792 s->in_repo_path = parent_path;
3793 s->thread_args.in_repo_path = s->in_repo_path;
3794 } else if (ch == CTRL('l')) {
3795 struct got_object_id *start_id;
3796 err = got_repo_match_object_id(&start_id, NULL,
3797 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3798 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3800 if (s->head_ref_name == NULL ||
3801 err->code != GOT_ERR_NOT_REF)
3803 /* Try to cope with deleted references. */
3804 free(s->head_ref_name);
3805 s->head_ref_name = NULL;
3806 err = got_repo_match_object_id(&start_id,
3807 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3808 &tog_refs, s->repo);
3813 s->start_id = start_id;
3814 s->thread_args.start_id = s->start_id;
3816 s->log_branches = !s->log_branches;
3818 if (s->thread_args.pack_fds == NULL) {
3819 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3823 err = got_repo_open(&s->thread_args.repo,
3824 got_repo_get_path(s->repo), NULL,
3825 s->thread_args.pack_fds);
3829 err = tog_load_refs(s->repo, 0);
3832 err = got_commit_graph_open(&s->thread_args.graph,
3833 s->in_repo_path, !s->log_branches);
3836 err = got_commit_graph_iter_start(s->thread_args.graph,
3837 s->start_id, s->repo, NULL, NULL);
3840 free_commits(&s->real_commits);
3841 free_commits(&s->limit_commits);
3842 s->first_displayed_entry = NULL;
3843 s->last_displayed_entry = NULL;
3844 s->selected_entry = NULL;
3846 s->thread_args.log_complete = 0;
3848 s->thread_args.commits_needed = view->lines;
3849 s->matched_entry = NULL;
3850 s->search_entry = NULL;
3855 err = view_request_new(new_view, view, TOG_VIEW_REF);
3865 static const struct got_error *
3866 apply_unveil(const char *repo_path, const char *worktree_path)
3868 const struct got_error *error;
3871 if (unveil("gmon.out", "rwc") != 0)
3872 return got_error_from_errno2("unveil", "gmon.out");
3874 if (repo_path && unveil(repo_path, "r") != 0)
3875 return got_error_from_errno2("unveil", repo_path);
3877 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3878 return got_error_from_errno2("unveil", worktree_path);
3880 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3881 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3883 error = got_privsep_unveil_exec_helpers();
3887 if (unveil(NULL, NULL) != 0)
3888 return got_error_from_errno("unveil");
3897 * Override default signal handlers before starting ncurses.
3898 * This should prevent ncurses from installing its own
3899 * broken cleanup() signal handler.
3901 signal(SIGWINCH, tog_sigwinch);
3902 signal(SIGPIPE, tog_sigpipe);
3903 signal(SIGCONT, tog_sigcont);
3904 signal(SIGINT, tog_sigint);
3905 signal(SIGTERM, tog_sigterm);
3909 halfdelay(1); /* Do fast refresh while initial view is loading. */
3912 intrflush(stdscr, FALSE);
3913 keypad(stdscr, TRUE);
3915 if (getenv("TOG_COLORS") != NULL) {
3917 use_default_colors();
3921 static const struct got_error *
3922 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3923 struct got_repository *repo, struct got_worktree *worktree)
3925 const struct got_error *err = NULL;
3928 *in_repo_path = strdup("/");
3929 if (*in_repo_path == NULL)
3930 return got_error_from_errno("strdup");
3935 const char *prefix = got_worktree_get_path_prefix(worktree);
3938 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3941 if (asprintf(in_repo_path, "%s%s%s", prefix,
3942 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3944 err = got_error_from_errno("asprintf");
3945 *in_repo_path = NULL;
3949 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3954 static const struct got_error *
3955 cmd_log(int argc, char *argv[])
3957 const struct got_error *error;
3958 struct got_repository *repo = NULL;
3959 struct got_worktree *worktree = NULL;
3960 struct got_object_id *start_id = NULL;
3961 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3962 char *start_commit = NULL, *label = NULL;
3963 struct got_reference *ref = NULL;
3964 const char *head_ref_name = NULL;
3965 int ch, log_branches = 0;
3966 struct tog_view *view;
3967 int *pack_fds = NULL;
3969 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3975 start_commit = optarg;
3978 repo_path = realpath(optarg, NULL);
3979 if (repo_path == NULL)
3980 return got_error_from_errno2("realpath",
3995 error = got_repo_pack_fds_open(&pack_fds);
3999 if (repo_path == NULL) {
4000 cwd = getcwd(NULL, 0);
4002 return got_error_from_errno("getcwd");
4003 error = got_worktree_open(&worktree, cwd);
4004 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4008 strdup(got_worktree_get_repo_path(worktree));
4010 repo_path = strdup(cwd);
4011 if (repo_path == NULL) {
4012 error = got_error_from_errno("strdup");
4017 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4021 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4028 error = apply_unveil(got_repo_get_path(repo),
4029 worktree ? got_worktree_get_root_path(worktree) : NULL);
4033 /* already loaded by tog_log_with_path()? */
4034 if (TAILQ_EMPTY(&tog_refs)) {
4035 error = tog_load_refs(repo, 0);
4040 if (start_commit == NULL) {
4041 error = got_repo_match_object_id(&start_id, &label,
4042 worktree ? got_worktree_get_head_ref_name(worktree) :
4043 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4046 head_ref_name = label;
4048 error = got_ref_open(&ref, repo, start_commit, 0);
4050 head_ref_name = got_ref_get_name(ref);
4051 else if (error->code != GOT_ERR_NOT_REF)
4053 error = got_repo_match_object_id(&start_id, NULL,
4054 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4059 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4061 error = got_error_from_errno("view_open");
4064 error = open_log_view(view, start_id, repo, head_ref_name,
4065 in_repo_path, log_branches);
4069 /* Release work tree lock. */
4070 got_worktree_close(worktree);
4073 error = view_loop(view);
4083 const struct got_error *close_err = got_repo_close(repo);
4088 got_worktree_close(worktree);
4090 const struct got_error *pack_err =
4091 got_repo_pack_fds_close(pack_fds);
4103 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4104 "object1 object2\n", getprogname());
4109 match_line(const char *line, regex_t *regex, size_t nmatch,
4110 regmatch_t *regmatch)
4112 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4115 static struct tog_color *
4116 match_color(struct tog_colors *colors, const char *line)
4118 struct tog_color *tc = NULL;
4120 STAILQ_FOREACH(tc, colors, entry) {
4121 if (match_line(line, &tc->regex, 0, NULL))
4128 static const struct got_error *
4129 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4130 WINDOW *window, int skipcol, regmatch_t *regmatch)
4132 const struct got_error *err = NULL;
4134 wchar_t *wline = NULL;
4135 int rme, rms, n, width, scrollx;
4136 int width0 = 0, width1 = 0, width2 = 0;
4137 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4141 rms = regmatch->rm_so;
4142 rme = regmatch->rm_eo;
4144 err = expand_tab(&exstr, line);
4148 /* Split the line into 3 segments, according to match offsets. */
4149 seg0 = strndup(exstr, rms);
4151 err = got_error_from_errno("strndup");
4154 seg1 = strndup(exstr + rms, rme - rms);
4156 err = got_error_from_errno("strndup");
4159 seg2 = strdup(exstr + rme);
4161 err = got_error_from_errno("strndup");
4165 /* draw up to matched token if we haven't scrolled past it */
4166 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4170 n = MAX(width0 - skipcol, 0);
4173 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4174 wlimit, col_tab_align, 1);
4177 waddwstr(window, &wline[scrollx]);
4187 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4191 wlen = wcslen(wline);
4193 width = wcwidth(wline[i]);
4195 /* should not happen, tabs are expanded */
4196 err = got_error(GOT_ERR_RANGE);
4199 if (width0 + w + width > skipcol)
4204 /* draw (visible part of) matched token (if scrolled into it) */
4205 if (width1 - w > 0) {
4206 wattron(window, A_STANDOUT);
4207 waddwstr(window, &wline[i]);
4208 wattroff(window, A_STANDOUT);
4209 wlimit -= (width1 - w);
4210 *wtotal += (width1 - w);
4214 if (wlimit > 0) { /* draw rest of line */
4216 if (skipcol > width0 + width1) {
4217 err = format_line(&wline, &width2, &scrollx, seg2,
4218 skipcol - (width0 + width1), wlimit,
4222 waddwstr(window, &wline[scrollx]);
4224 err = format_line(&wline, &width2, NULL, seg2, 0,
4225 wlimit, col_tab_align, 1);
4228 waddwstr(window, wline);
4242 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4245 int *eof, *first, *selected;
4247 if (view->type == TOG_VIEW_DIFF) {
4248 struct tog_diff_view_state *s = &view->state.diff;
4250 first = &s->first_displayed_line;
4254 } else if (view->type == TOG_VIEW_HELP) {
4255 struct tog_help_view_state *s = &view->state.help;
4257 first = &s->first_displayed_line;
4261 } else if (view->type == TOG_VIEW_BLAME) {
4262 struct tog_blame_view_state *s = &view->state.blame;
4264 first = &s->first_displayed_line;
4265 selected = &s->selected_line;
4271 /* Center gline in the middle of the page like vi(1). */
4272 if (*lineno < view->gline - (view->nlines - 3) / 2)
4274 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4283 *selected = view->gline <= (view->nlines - 3) / 2 ?
4284 view->gline : (view->nlines - 3) / 2 + 1;
4290 static const struct got_error *
4291 draw_file(struct tog_view *view, const char *header)
4293 struct tog_diff_view_state *s = &view->state.diff;
4294 regmatch_t *regmatch = &view->regmatch;
4295 const struct got_error *err;
4298 size_t linesize = 0;
4302 int max_lines = view->nlines;
4303 int nlines = s->nlines;
4306 s->lineno = s->first_displayed_line - 1;
4307 line_offset = s->lines[s->first_displayed_line - 1].offset;
4308 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4309 return got_error_from_errno("fseek");
4311 werase(view->window);
4313 if (view->gline > s->nlines - 1)
4314 view->gline = s->nlines - 1;
4317 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4318 1 : view->gline - (view->nlines - 3) / 2 :
4319 s->lineno + s->selected_line;
4321 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4322 return got_error_from_errno("asprintf");
4323 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4329 if (view_needs_focus_indication(view))
4330 wstandout(view->window);
4331 waddwstr(view->window, wline);
4334 while (width++ < view->ncols)
4335 waddch(view->window, ' ');
4336 if (view_needs_focus_indication(view))
4337 wstandend(view->window);
4347 while (max_lines > 0 && nprinted < max_lines) {
4348 enum got_diff_line_type linetype;
4351 linelen = getline(&line, &linesize, s->f);
4352 if (linelen == -1) {
4358 return got_ferror(s->f, GOT_ERR_IO);
4361 if (++s->lineno < s->first_displayed_line)
4363 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4365 if (s->lineno == view->hiline)
4368 /* Set view->maxx based on full line length. */
4369 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4375 view->maxx = MAX(view->maxx, width);
4379 linetype = s->lines[s->lineno].type;
4380 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4381 linetype < GOT_DIFF_LINE_CONTEXT)
4382 attr |= COLOR_PAIR(linetype);
4384 wattron(view->window, attr);
4385 if (s->first_displayed_line + nprinted == s->matched_line &&
4386 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4387 err = add_matched_line(&width, line, view->ncols, 0,
4388 view->window, view->x, regmatch);
4395 err = format_line(&wline, &width, &skip, line,
4396 view->x, view->ncols, 0, view->x ? 1 : 0);
4401 waddwstr(view->window, &wline[skip]);
4405 if (s->lineno == view->hiline) {
4406 /* highlight full gline length */
4407 while (width++ < view->ncols)
4408 waddch(view->window, ' ');
4410 if (width <= view->ncols - 1)
4411 waddch(view->window, '\n');
4414 wattroff(view->window, attr);
4415 if (++nprinted == 1)
4416 s->first_displayed_line = s->lineno;
4420 s->last_displayed_line = s->first_displayed_line +
4423 s->last_displayed_line = s->first_displayed_line;
4428 while (nprinted < view->nlines) {
4429 waddch(view->window, '\n');
4433 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4439 wstandout(view->window);
4440 waddwstr(view->window, wline);
4443 wstandend(view->window);
4450 get_datestr(time_t *time, char *datebuf)
4452 struct tm mytm, *tm;
4455 tm = gmtime_r(time, &mytm);
4458 s = asctime_r(tm, datebuf);
4461 p = strchr(s, '\n');
4467 static const struct got_error *
4468 get_changed_paths(struct got_pathlist_head *paths,
4469 struct got_commit_object *commit, struct got_repository *repo,
4470 struct got_diffstat_cb_arg *dsa)
4472 const struct got_error *err = NULL;
4473 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4474 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4475 struct got_object_qid *qid;
4476 FILE *f1 = NULL, *f2 = NULL;
4477 int fd1 = -1, fd2 = -1;
4479 f1 = got_opentemp();
4481 err = got_error_from_errno("got_opentemp");
4484 f2 = got_opentemp();
4486 err = got_error_from_errno("got_opentemp");
4490 fd1 = got_opentempfd();
4492 err = got_error_from_errno("got_opentempfd");
4495 fd2 = got_opentempfd();
4497 err = got_error_from_errno("got_opentempfd");
4501 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4503 struct got_commit_object *pcommit;
4504 err = got_object_open_as_commit(&pcommit, repo,
4509 tree_id1 = got_object_id_dup(
4510 got_object_commit_get_tree_id(pcommit));
4511 if (tree_id1 == NULL) {
4512 got_object_commit_close(pcommit);
4513 return got_error_from_errno("got_object_id_dup");
4515 got_object_commit_close(pcommit);
4520 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4525 tree_id2 = got_object_commit_get_tree_id(commit);
4526 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4530 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4531 got_diff_tree_compute_diffstat, dsa, 1);
4534 got_object_tree_close(tree1);
4536 got_object_tree_close(tree2);
4537 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4538 err = got_error_from_errno("close");
4539 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4540 err = got_error_from_errno("close");
4541 if (f1 && fclose(f1) == EOF && err == NULL)
4542 err = got_error_from_errno("fclose");
4543 if (f2 && fclose(f2) == EOF && err == NULL)
4544 err = got_error_from_errno("fclose");
4549 static const struct got_error *
4550 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4551 off_t off, uint8_t type)
4553 struct got_diff_line *p;
4555 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4557 return got_error_from_errno("reallocarray");
4559 (*lines)[*nlines].offset = off;
4560 (*lines)[*nlines].type = type;
4566 static const struct got_error *
4567 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4568 struct got_object_id *commit_id, struct got_reflist_head *refs,
4569 struct got_repository *repo, int ignore_ws, int force_text_diff,
4572 const struct got_error *err = NULL;
4573 char datebuf[26], *datestr;
4574 struct got_commit_object *commit;
4575 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4576 time_t committer_time;
4577 const char *author, *committer;
4578 char *refs_str = NULL;
4579 struct got_pathlist_head changed_paths;
4580 struct got_pathlist_entry *pe;
4581 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0, &changed_paths,
4582 ignore_ws, force_text_diff, tog_diff_algo };
4586 TAILQ_INIT(&changed_paths);
4589 err = build_refs_str(&refs_str, refs, commit_id, repo);
4594 err = got_object_open_as_commit(&commit, repo, commit_id);
4598 err = got_object_id_str(&id_str, commit_id);
4600 err = got_error_from_errno("got_object_id_str");
4604 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4608 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4609 refs_str ? refs_str : "", refs_str ? ")" : "");
4611 err = got_error_from_errno("fprintf");
4615 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4619 n = fprintf(outfile, "from: %s\n",
4620 got_object_commit_get_author(commit));
4622 err = got_error_from_errno("fprintf");
4626 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4630 author = got_object_commit_get_author(commit);
4631 committer = got_object_commit_get_committer(commit);
4632 if (strcmp(author, committer) != 0) {
4633 n = fprintf(outfile, "via: %s\n", committer);
4635 err = got_error_from_errno("fprintf");
4639 err = add_line_metadata(lines, nlines, outoff,
4640 GOT_DIFF_LINE_AUTHOR);
4644 committer_time = got_object_commit_get_committer_time(commit);
4645 datestr = get_datestr(&committer_time, datebuf);
4647 n = fprintf(outfile, "date: %s UTC\n", datestr);
4649 err = got_error_from_errno("fprintf");
4653 err = add_line_metadata(lines, nlines, outoff,
4654 GOT_DIFF_LINE_DATE);
4658 if (got_object_commit_get_nparents(commit) > 1) {
4659 const struct got_object_id_queue *parent_ids;
4660 struct got_object_qid *qid;
4662 parent_ids = got_object_commit_get_parent_ids(commit);
4663 STAILQ_FOREACH(qid, parent_ids, entry) {
4664 err = got_object_id_str(&id_str, &qid->id);
4667 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4669 err = got_error_from_errno("fprintf");
4673 err = add_line_metadata(lines, nlines, outoff,
4674 GOT_DIFF_LINE_META);
4682 err = got_object_commit_get_logmsg(&logmsg, commit);
4686 while ((line = strsep(&s, "\n")) != NULL) {
4687 n = fprintf(outfile, "%s\n", line);
4689 err = got_error_from_errno("fprintf");
4693 err = add_line_metadata(lines, nlines, outoff,
4694 GOT_DIFF_LINE_LOGMSG);
4699 err = get_changed_paths(&changed_paths, commit, repo, &dsa);
4703 TAILQ_FOREACH(pe, &changed_paths, entry) {
4704 struct got_diff_changed_path *cp = pe->data;
4705 int pad = dsa.max_path_len - pe->path_len + 1;
4707 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4708 pe->path, pad, ' ', dsa.add_cols + 1, cp->add,
4709 dsa.rm_cols + 1, cp->rm);
4711 err = got_error_from_errno("fprintf");
4715 err = add_line_metadata(lines, nlines, outoff,
4716 GOT_DIFF_LINE_CHANGES);
4721 fputc('\n', outfile);
4723 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4727 n = fprintf(outfile,
4728 "%d file%s changed, %d insertions(+), %d deletions(-)\n",
4729 dsa.nfiles, dsa.nfiles > 1 ? "s" : "", dsa.ins, dsa.del);
4731 err = got_error_from_errno("fprintf");
4735 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4739 fputc('\n', outfile);
4741 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4743 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4747 got_object_commit_close(commit);
4756 static const struct got_error *
4757 create_diff(struct tog_diff_view_state *s)
4759 const struct got_error *err = NULL;
4764 s->lines = malloc(sizeof(*s->lines));
4765 if (s->lines == NULL)
4766 return got_error_from_errno("malloc");
4771 err = got_error_from_errno("got_opentemp");
4774 if (s->f && fclose(s->f) == EOF) {
4775 err = got_error_from_errno("fclose");
4781 err = got_object_get_type(&obj_type, s->repo, s->id1);
4783 err = got_object_get_type(&obj_type, s->repo, s->id2);
4788 case GOT_OBJ_TYPE_BLOB:
4789 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4790 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4791 s->label1, s->label2, tog_diff_algo, s->diff_context,
4792 s->ignore_whitespace, s->force_text_diff, 0, NULL, s->repo,
4795 case GOT_OBJ_TYPE_TREE:
4796 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4797 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4798 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4799 s->force_text_diff, 0, NULL, s->repo, s->f);
4801 case GOT_OBJ_TYPE_COMMIT: {
4802 const struct got_object_id_queue *parent_ids;
4803 struct got_object_qid *pid;
4804 struct got_commit_object *commit2;
4805 struct got_reflist_head *refs;
4807 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4810 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4811 /* Show commit info if we're diffing to a parent/root commit. */
4812 if (s->id1 == NULL) {
4813 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4814 refs, s->repo, s->ignore_whitespace,
4815 s->force_text_diff, s->f);
4819 parent_ids = got_object_commit_get_parent_ids(commit2);
4820 STAILQ_FOREACH(pid, parent_ids, entry) {
4821 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4822 err = write_commit_info(&s->lines,
4823 &s->nlines, s->id2, refs, s->repo,
4824 s->ignore_whitespace,
4825 s->force_text_diff, s->f);
4832 got_object_commit_close(commit2);
4834 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4835 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4836 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4837 s->force_text_diff, 0, NULL, s->repo, s->f);
4841 err = got_error(GOT_ERR_OBJ_TYPE);
4845 if (s->f && fflush(s->f) != 0 && err == NULL)
4846 err = got_error_from_errno("fflush");
4851 diff_view_indicate_progress(struct tog_view *view)
4853 mvwaddstr(view->window, 0, 0, "diffing...");
4858 static const struct got_error *
4859 search_start_diff_view(struct tog_view *view)
4861 struct tog_diff_view_state *s = &view->state.diff;
4863 s->matched_line = 0;
4868 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4869 size_t *nlines, int **first, int **last, int **match, int **selected)
4871 struct tog_diff_view_state *s = &view->state.diff;
4874 *nlines = s->nlines;
4875 *line_offsets = NULL;
4876 *match = &s->matched_line;
4877 *first = &s->first_displayed_line;
4878 *last = &s->last_displayed_line;
4879 *selected = &s->selected_line;
4882 static const struct got_error *
4883 search_next_view_match(struct tog_view *view)
4885 const struct got_error *err = NULL;
4889 size_t linesize = 0;
4891 off_t *line_offsets;
4893 int *first, *last, *match, *selected;
4895 if (!view->search_setup)
4896 return got_error_msg(GOT_ERR_NOT_IMPL,
4897 "view search not supported");
4898 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4901 if (!view->searching) {
4902 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 if (view->searching == TOG_SEARCH_FORWARD)
4908 lineno = *match + 1;
4910 lineno = *match - 1;
4912 lineno = *first - 1 + *selected;
4917 if (lineno <= 0 || lineno > nlines) {
4919 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4923 if (view->searching == TOG_SEARCH_FORWARD)
4929 offset = view->type == TOG_VIEW_DIFF ?
4930 view->state.diff.lines[lineno - 1].offset :
4931 line_offsets[lineno - 1];
4932 if (fseeko(f, offset, SEEK_SET) != 0) {
4934 return got_error_from_errno("fseeko");
4936 linelen = getline(&line, &linesize, f);
4937 if (linelen != -1) {
4939 err = expand_tab(&exstr, line);
4942 if (match_line(exstr, &view->regex, 1,
4944 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4951 if (view->searching == TOG_SEARCH_FORWARD)
4966 static const struct got_error *
4967 close_diff_view(struct tog_view *view)
4969 const struct got_error *err = NULL;
4970 struct tog_diff_view_state *s = &view->state.diff;
4976 if (s->f && fclose(s->f) == EOF)
4977 err = got_error_from_errno("fclose");
4979 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4980 err = got_error_from_errno("fclose");
4982 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4983 err = got_error_from_errno("fclose");
4985 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4986 err = got_error_from_errno("close");
4988 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4989 err = got_error_from_errno("close");
4997 static const struct got_error *
4998 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4999 struct got_object_id *id2, const char *label1, const char *label2,
5000 int diff_context, int ignore_whitespace, int force_text_diff,
5001 struct tog_view *parent_view, struct got_repository *repo)
5003 const struct got_error *err;
5004 struct tog_diff_view_state *s = &view->state.diff;
5006 memset(s, 0, sizeof(*s));
5010 if (id1 != NULL && id2 != NULL) {
5012 err = got_object_get_type(&type1, repo, id1);
5015 err = got_object_get_type(&type2, repo, id2);
5020 return got_error(GOT_ERR_OBJ_TYPE);
5022 s->first_displayed_line = 1;
5023 s->last_displayed_line = view->nlines;
5024 s->selected_line = 1;
5032 s->id1 = got_object_id_dup(id1);
5034 return got_error_from_errno("got_object_id_dup");
5038 s->id2 = got_object_id_dup(id2);
5039 if (s->id2 == NULL) {
5040 err = got_error_from_errno("got_object_id_dup");
5044 s->f1 = got_opentemp();
5045 if (s->f1 == NULL) {
5046 err = got_error_from_errno("got_opentemp");
5050 s->f2 = got_opentemp();
5051 if (s->f2 == NULL) {
5052 err = got_error_from_errno("got_opentemp");
5056 s->fd1 = got_opentempfd();
5058 err = got_error_from_errno("got_opentempfd");
5062 s->fd2 = got_opentempfd();
5064 err = got_error_from_errno("got_opentempfd");
5068 s->first_displayed_line = 1;
5069 s->last_displayed_line = view->nlines;
5070 s->diff_context = diff_context;
5071 s->ignore_whitespace = ignore_whitespace;
5072 s->force_text_diff = force_text_diff;
5073 s->parent_view = parent_view;
5076 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5079 rc = init_pair(GOT_DIFF_LINE_MINUS,
5080 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5082 rc = init_pair(GOT_DIFF_LINE_PLUS,
5083 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5085 rc = init_pair(GOT_DIFF_LINE_HUNK,
5086 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5088 rc = init_pair(GOT_DIFF_LINE_META,
5089 get_color_value("TOG_COLOR_DIFF_META"), -1);
5091 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5092 get_color_value("TOG_COLOR_DIFF_META"), -1);
5094 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5095 get_color_value("TOG_COLOR_DIFF_META"), -1);
5097 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5098 get_color_value("TOG_COLOR_DIFF_META"), -1);
5100 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5101 get_color_value("TOG_COLOR_AUTHOR"), -1);
5103 rc = init_pair(GOT_DIFF_LINE_DATE,
5104 get_color_value("TOG_COLOR_DATE"), -1);
5106 err = got_error(GOT_ERR_RANGE);
5111 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5112 view_is_splitscreen(view))
5113 show_log_view(parent_view); /* draw border */
5114 diff_view_indicate_progress(view);
5116 err = create_diff(s);
5118 view->show = show_diff_view;
5119 view->input = input_diff_view;
5120 view->reset = reset_diff_view;
5121 view->close = close_diff_view;
5122 view->search_start = search_start_diff_view;
5123 view->search_setup = search_setup_diff_view;
5124 view->search_next = search_next_view_match;
5127 close_diff_view(view);
5131 static const struct got_error *
5132 show_diff_view(struct tog_view *view)
5134 const struct got_error *err;
5135 struct tog_diff_view_state *s = &view->state.diff;
5136 char *id_str1 = NULL, *id_str2, *header;
5137 const char *label1, *label2;
5140 err = got_object_id_str(&id_str1, s->id1);
5143 label1 = s->label1 ? s->label1 : id_str1;
5145 label1 = "/dev/null";
5147 err = got_object_id_str(&id_str2, s->id2);
5150 label2 = s->label2 ? s->label2 : id_str2;
5152 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5153 err = got_error_from_errno("asprintf");
5161 err = draw_file(view, header);
5166 static const struct got_error *
5167 set_selected_commit(struct tog_diff_view_state *s,
5168 struct commit_queue_entry *entry)
5170 const struct got_error *err;
5171 const struct got_object_id_queue *parent_ids;
5172 struct got_commit_object *selected_commit;
5173 struct got_object_qid *pid;
5176 s->id2 = got_object_id_dup(entry->id);
5178 return got_error_from_errno("got_object_id_dup");
5180 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5183 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5185 pid = STAILQ_FIRST(parent_ids);
5186 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5187 got_object_commit_close(selected_commit);
5191 static const struct got_error *
5192 reset_diff_view(struct tog_view *view)
5194 struct tog_diff_view_state *s = &view->state.diff;
5197 wclear(view->window);
5198 s->first_displayed_line = 1;
5199 s->last_displayed_line = view->nlines;
5200 s->matched_line = 0;
5201 diff_view_indicate_progress(view);
5202 return create_diff(s);
5206 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5210 i = start = s->first_displayed_line - 1;
5212 while (s->lines[i].type != type) {
5216 return; /* do nothing, requested type not in file */
5219 s->selected_line = 1;
5220 s->first_displayed_line = i;
5224 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5228 i = start = s->first_displayed_line + 1;
5230 while (s->lines[i].type != type) {
5231 if (i == s->nlines - 1)
5234 return; /* do nothing, requested type not in file */
5237 s->selected_line = 1;
5238 s->first_displayed_line = i;
5241 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5243 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5246 static const struct got_error *
5247 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5249 const struct got_error *err = NULL;
5250 struct tog_diff_view_state *s = &view->state.diff;
5251 struct tog_log_view_state *ls;
5252 struct commit_queue_entry *old_selected_entry;
5254 size_t linesize = 0;
5256 int i, nscroll = view->nlines - 1, up = 0;
5258 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5265 view->x = MAX(view->maxx - view->ncols / 3, 0);
5270 if (view->x + view->ncols / 3 < view->maxx)
5271 view->x += 2; /* move two columns right */
5277 view->x -= MIN(view->x, 2); /* move two columns back */
5284 s->force_text_diff = !s->force_text_diff;
5286 s->ignore_whitespace = !s->ignore_whitespace;
5287 err = reset_diff_view(view);
5291 s->first_displayed_line = 1;
5300 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5306 if (s->first_displayed_line > 1)
5307 s->first_displayed_line--;
5318 if (s->first_displayed_line == 1) {
5323 while (i++ < nscroll && s->first_displayed_line > 1)
5324 s->first_displayed_line--;
5330 s->first_displayed_line++;
5347 while (!s->eof && i++ < nscroll) {
5348 linelen = getline(&line, &linesize, s->f);
5349 s->first_displayed_line++;
5350 if (linelen == -1) {
5354 err = got_ferror(s->f, GOT_ERR_IO);
5361 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5364 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5367 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5370 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5373 if (s->diff_context > 0) {
5375 s->matched_line = 0;
5376 diff_view_indicate_progress(view);
5377 err = create_diff(s);
5378 if (s->first_displayed_line + view->nlines - 1 >
5380 s->first_displayed_line = 1;
5381 s->last_displayed_line = view->nlines;
5387 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5389 s->matched_line = 0;
5390 diff_view_indicate_progress(view);
5391 err = create_diff(s);
5403 if (s->parent_view == NULL) {
5407 s->parent_view->count = view->count;
5409 if (s->parent_view->type == TOG_VIEW_LOG) {
5410 ls = &s->parent_view->state.log;
5411 old_selected_entry = ls->selected_entry;
5413 err = input_log_view(NULL, s->parent_view,
5414 up ? KEY_UP : KEY_DOWN);
5417 view->count = s->parent_view->count;
5419 if (old_selected_entry == ls->selected_entry)
5422 err = set_selected_commit(s, ls->selected_entry);
5425 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5426 struct tog_blame_view_state *bs;
5427 struct got_object_id *id, *prev_id;
5429 bs = &s->parent_view->state.blame;
5430 prev_id = get_annotation_for_line(bs->blame.lines,
5431 bs->blame.nlines, bs->last_diffed_line);
5433 err = input_blame_view(&view, s->parent_view,
5434 up ? KEY_UP : KEY_DOWN);
5437 view->count = s->parent_view->count;
5439 if (prev_id == NULL)
5441 id = get_selected_commit_id(bs->blame.lines,
5442 bs->blame.nlines, bs->first_displayed_line,
5447 if (!got_object_id_cmp(prev_id, id))
5450 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5454 s->first_displayed_line = 1;
5455 s->last_displayed_line = view->nlines;
5456 s->matched_line = 0;
5459 diff_view_indicate_progress(view);
5460 err = create_diff(s);
5470 static const struct got_error *
5471 cmd_diff(int argc, char *argv[])
5473 const struct got_error *error = NULL;
5474 struct got_repository *repo = NULL;
5475 struct got_worktree *worktree = NULL;
5476 struct got_object_id *id1 = NULL, *id2 = NULL;
5477 char *repo_path = NULL, *cwd = NULL;
5478 char *id_str1 = NULL, *id_str2 = NULL;
5479 char *label1 = NULL, *label2 = NULL;
5480 int diff_context = 3, ignore_whitespace = 0;
5481 int ch, force_text_diff = 0;
5483 struct tog_view *view;
5484 int *pack_fds = NULL;
5486 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5489 force_text_diff = 1;
5492 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5495 errx(1, "number of context lines is %s: %s",
5499 repo_path = realpath(optarg, NULL);
5500 if (repo_path == NULL)
5501 return got_error_from_errno2("realpath",
5503 got_path_strip_trailing_slashes(repo_path);
5506 ignore_whitespace = 1;
5518 usage_diff(); /* TODO show local worktree changes */
5519 } else if (argc == 2) {
5525 error = got_repo_pack_fds_open(&pack_fds);
5529 if (repo_path == NULL) {
5530 cwd = getcwd(NULL, 0);
5532 return got_error_from_errno("getcwd");
5533 error = got_worktree_open(&worktree, cwd);
5534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5538 strdup(got_worktree_get_repo_path(worktree));
5540 repo_path = strdup(cwd);
5541 if (repo_path == NULL) {
5542 error = got_error_from_errno("strdup");
5547 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5553 error = apply_unveil(got_repo_get_path(repo), NULL);
5557 error = tog_load_refs(repo, 0);
5561 error = got_repo_match_object_id(&id1, &label1, id_str1,
5562 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5566 error = got_repo_match_object_id(&id2, &label2, id_str2,
5567 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5571 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5573 error = got_error_from_errno("view_open");
5576 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5577 ignore_whitespace, force_text_diff, NULL, repo);
5580 error = view_loop(view);
5587 const struct got_error *close_err = got_repo_close(repo);
5592 got_worktree_close(worktree);
5594 const struct got_error *pack_err =
5595 got_repo_pack_fds_close(pack_fds);
5608 "usage: %s blame [-c commit] [-r repository-path] path\n",
5613 struct tog_blame_line {
5615 struct got_object_id *id;
5618 static const struct got_error *
5619 draw_blame(struct tog_view *view)
5621 struct tog_blame_view_state *s = &view->state.blame;
5622 struct tog_blame *blame = &s->blame;
5623 regmatch_t *regmatch = &view->regmatch;
5624 const struct got_error *err;
5625 int lineno = 0, nprinted = 0;
5627 size_t linesize = 0;
5631 struct tog_blame_line *blame_line;
5632 struct got_object_id *prev_id = NULL;
5634 struct tog_color *tc;
5636 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5641 werase(view->window);
5643 if (asprintf(&line, "commit %s", id_str) == -1) {
5644 err = got_error_from_errno("asprintf");
5649 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5654 if (view_needs_focus_indication(view))
5655 wstandout(view->window);
5656 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5658 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5659 waddwstr(view->window, wline);
5660 while (width++ < view->ncols)
5661 waddch(view->window, ' ');
5663 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5664 if (view_needs_focus_indication(view))
5665 wstandend(view->window);
5669 if (view->gline > blame->nlines)
5670 view->gline = blame->nlines;
5672 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5673 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5674 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5676 return got_error_from_errno("asprintf");
5679 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5684 waddwstr(view->window, wline);
5687 if (width < view->ncols - 1)
5688 waddch(view->window, '\n');
5692 while (nprinted < view->nlines - 2) {
5693 linelen = getline(&line, &linesize, blame->f);
5694 if (linelen == -1) {
5695 if (feof(blame->f)) {
5700 return got_ferror(blame->f, GOT_ERR_IO);
5702 if (++lineno < s->first_displayed_line)
5704 if (view->gline && !gotoline(view, &lineno, &nprinted))
5707 /* Set view->maxx based on full line length. */
5708 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5715 view->maxx = MAX(view->maxx, width);
5717 if (nprinted == s->selected_line - 1)
5718 wstandout(view->window);
5720 if (blame->nlines > 0) {
5721 blame_line = &blame->lines[lineno - 1];
5722 if (blame_line->annotated && prev_id &&
5723 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5724 !(nprinted == s->selected_line - 1)) {
5725 waddstr(view->window, " ");
5726 } else if (blame_line->annotated) {
5728 err = got_object_id_str(&id_str,
5734 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5736 wattr_on(view->window,
5737 COLOR_PAIR(tc->colorpair), NULL);
5738 wprintw(view->window, "%.8s", id_str);
5740 wattr_off(view->window,
5741 COLOR_PAIR(tc->colorpair), NULL);
5743 prev_id = blame_line->id;
5745 waddstr(view->window, "........");
5749 waddstr(view->window, "........");
5753 if (nprinted == s->selected_line - 1)
5754 wstandend(view->window);
5755 waddstr(view->window, " ");
5757 if (view->ncols <= 9) {
5759 } else if (s->first_displayed_line + nprinted ==
5761 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5762 err = add_matched_line(&width, line, view->ncols - 9, 9,
5763 view->window, view->x, regmatch);
5771 err = format_line(&wline, &width, &skip, line,
5772 view->x, view->ncols - 9, 9, 1);
5777 waddwstr(view->window, &wline[skip]);
5783 if (width <= view->ncols - 1)
5784 waddch(view->window, '\n');
5785 if (++nprinted == 1)
5786 s->first_displayed_line = lineno;
5789 s->last_displayed_line = lineno;
5796 static const struct got_error *
5797 blame_cb(void *arg, int nlines, int lineno,
5798 struct got_commit_object *commit, struct got_object_id *id)
5800 const struct got_error *err = NULL;
5801 struct tog_blame_cb_args *a = arg;
5802 struct tog_blame_line *line;
5805 if (nlines != a->nlines ||
5806 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5807 return got_error(GOT_ERR_RANGE);
5809 errcode = pthread_mutex_lock(&tog_mutex);
5811 return got_error_set_errno(errcode, "pthread_mutex_lock");
5813 if (*a->quit) { /* user has quit the blame view */
5814 err = got_error(GOT_ERR_ITER_COMPLETED);
5819 goto done; /* no change in this commit */
5821 line = &a->lines[lineno - 1];
5822 if (line->annotated)
5825 line->id = got_object_id_dup(id);
5826 if (line->id == NULL) {
5827 err = got_error_from_errno("got_object_id_dup");
5830 line->annotated = 1;
5832 errcode = pthread_mutex_unlock(&tog_mutex);
5834 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5839 blame_thread(void *arg)
5841 const struct got_error *err, *close_err;
5842 struct tog_blame_thread_args *ta = arg;
5843 struct tog_blame_cb_args *a = ta->cb_args;
5844 int errcode, fd1 = -1, fd2 = -1;
5845 FILE *f1 = NULL, *f2 = NULL;
5847 fd1 = got_opentempfd();
5849 return (void *)got_error_from_errno("got_opentempfd");
5851 fd2 = got_opentempfd();
5853 err = got_error_from_errno("got_opentempfd");
5857 f1 = got_opentemp();
5859 err = (void *)got_error_from_errno("got_opentemp");
5862 f2 = got_opentemp();
5864 err = (void *)got_error_from_errno("got_opentemp");
5868 err = block_signals_used_by_main_thread();
5872 err = got_blame(ta->path, a->commit_id, ta->repo,
5873 tog_diff_algo, blame_cb, ta->cb_args,
5874 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5875 if (err && err->code == GOT_ERR_CANCELLED)
5878 errcode = pthread_mutex_lock(&tog_mutex);
5880 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5884 close_err = got_repo_close(ta->repo);
5890 errcode = pthread_mutex_unlock(&tog_mutex);
5891 if (errcode && err == NULL)
5892 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5895 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5896 err = got_error_from_errno("close");
5897 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5898 err = got_error_from_errno("close");
5899 if (f1 && fclose(f1) == EOF && err == NULL)
5900 err = got_error_from_errno("fclose");
5901 if (f2 && fclose(f2) == EOF && err == NULL)
5902 err = got_error_from_errno("fclose");
5907 static struct got_object_id *
5908 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5909 int first_displayed_line, int selected_line)
5911 struct tog_blame_line *line;
5916 line = &lines[first_displayed_line - 1 + selected_line - 1];
5917 if (!line->annotated)
5923 static struct got_object_id *
5924 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5927 struct tog_blame_line *line;
5929 if (nlines <= 0 || lineno >= nlines)
5932 line = &lines[lineno - 1];
5933 if (!line->annotated)
5939 static const struct got_error *
5940 stop_blame(struct tog_blame *blame)
5942 const struct got_error *err = NULL;
5945 if (blame->thread) {
5947 errcode = pthread_mutex_unlock(&tog_mutex);
5949 return got_error_set_errno(errcode,
5950 "pthread_mutex_unlock");
5951 errcode = pthread_join(blame->thread, (void **)&err);
5953 return got_error_set_errno(errcode, "pthread_join");
5954 errcode = pthread_mutex_lock(&tog_mutex);
5956 return got_error_set_errno(errcode,
5957 "pthread_mutex_lock");
5958 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5960 blame->thread = 0; //NULL;
5962 if (blame->thread_args.repo) {
5963 const struct got_error *close_err;
5964 close_err = got_repo_close(blame->thread_args.repo);
5967 blame->thread_args.repo = NULL;
5970 if (fclose(blame->f) == EOF && err == NULL)
5971 err = got_error_from_errno("fclose");
5975 for (i = 0; i < blame->nlines; i++)
5976 free(blame->lines[i].id);
5978 blame->lines = NULL;
5980 free(blame->cb_args.commit_id);
5981 blame->cb_args.commit_id = NULL;
5982 if (blame->pack_fds) {
5983 const struct got_error *pack_err =
5984 got_repo_pack_fds_close(blame->pack_fds);
5987 blame->pack_fds = NULL;
5992 static const struct got_error *
5993 cancel_blame_view(void *arg)
5995 const struct got_error *err = NULL;
5999 errcode = pthread_mutex_lock(&tog_mutex);
6001 return got_error_set_errno(errcode,
6002 "pthread_mutex_unlock");
6005 err = got_error(GOT_ERR_CANCELLED);
6007 errcode = pthread_mutex_unlock(&tog_mutex);
6009 return got_error_set_errno(errcode,
6010 "pthread_mutex_lock");
6015 static const struct got_error *
6016 run_blame(struct tog_view *view)
6018 struct tog_blame_view_state *s = &view->state.blame;
6019 struct tog_blame *blame = &s->blame;
6020 const struct got_error *err = NULL;
6021 struct got_commit_object *commit = NULL;
6022 struct got_blob_object *blob = NULL;
6023 struct got_repository *thread_repo = NULL;
6024 struct got_object_id *obj_id = NULL;
6025 int obj_type, fd = -1;
6026 int *pack_fds = NULL;
6028 err = got_object_open_as_commit(&commit, s->repo,
6029 &s->blamed_commit->id);
6033 fd = got_opentempfd();
6035 err = got_error_from_errno("got_opentempfd");
6039 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6043 err = got_object_get_type(&obj_type, s->repo, obj_id);
6047 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6048 err = got_error(GOT_ERR_OBJ_TYPE);
6052 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6055 blame->f = got_opentemp();
6056 if (blame->f == NULL) {
6057 err = got_error_from_errno("got_opentemp");
6060 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6061 &blame->line_offsets, blame->f, blob);
6064 if (blame->nlines == 0) {
6065 s->blame_complete = 1;
6069 /* Don't include \n at EOF in the blame line count. */
6070 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6073 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6074 if (blame->lines == NULL) {
6075 err = got_error_from_errno("calloc");
6079 err = got_repo_pack_fds_open(&pack_fds);
6082 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6087 blame->pack_fds = pack_fds;
6088 blame->cb_args.view = view;
6089 blame->cb_args.lines = blame->lines;
6090 blame->cb_args.nlines = blame->nlines;
6091 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6092 if (blame->cb_args.commit_id == NULL) {
6093 err = got_error_from_errno("got_object_id_dup");
6096 blame->cb_args.quit = &s->done;
6098 blame->thread_args.path = s->path;
6099 blame->thread_args.repo = thread_repo;
6100 blame->thread_args.cb_args = &blame->cb_args;
6101 blame->thread_args.complete = &s->blame_complete;
6102 blame->thread_args.cancel_cb = cancel_blame_view;
6103 blame->thread_args.cancel_arg = &s->done;
6104 s->blame_complete = 0;
6106 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6107 s->first_displayed_line = 1;
6108 s->last_displayed_line = view->nlines;
6109 s->selected_line = 1;
6111 s->matched_line = 0;
6115 got_object_commit_close(commit);
6116 if (fd != -1 && close(fd) == -1 && err == NULL)
6117 err = got_error_from_errno("close");
6119 got_object_blob_close(blob);
6126 static const struct got_error *
6127 open_blame_view(struct tog_view *view, char *path,
6128 struct got_object_id *commit_id, struct got_repository *repo)
6130 const struct got_error *err = NULL;
6131 struct tog_blame_view_state *s = &view->state.blame;
6133 STAILQ_INIT(&s->blamed_commits);
6135 s->path = strdup(path);
6136 if (s->path == NULL)
6137 return got_error_from_errno("strdup");
6139 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6145 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6146 s->first_displayed_line = 1;
6147 s->last_displayed_line = view->nlines;
6148 s->selected_line = 1;
6149 s->blame_complete = 0;
6151 s->commit_id = commit_id;
6152 memset(&s->blame, 0, sizeof(s->blame));
6154 STAILQ_INIT(&s->colors);
6155 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6156 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6157 get_color_value("TOG_COLOR_COMMIT"));
6162 view->show = show_blame_view;
6163 view->input = input_blame_view;
6164 view->reset = reset_blame_view;
6165 view->close = close_blame_view;
6166 view->search_start = search_start_blame_view;
6167 view->search_setup = search_setup_blame_view;
6168 view->search_next = search_next_view_match;
6170 return run_blame(view);
6173 static const struct got_error *
6174 close_blame_view(struct tog_view *view)
6176 const struct got_error *err = NULL;
6177 struct tog_blame_view_state *s = &view->state.blame;
6179 if (s->blame.thread)
6180 err = stop_blame(&s->blame);
6182 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6183 struct got_object_qid *blamed_commit;
6184 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6185 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6186 got_object_qid_free(blamed_commit);
6190 free_colors(&s->colors);
6194 static const struct got_error *
6195 search_start_blame_view(struct tog_view *view)
6197 struct tog_blame_view_state *s = &view->state.blame;
6199 s->matched_line = 0;
6204 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6205 size_t *nlines, int **first, int **last, int **match, int **selected)
6207 struct tog_blame_view_state *s = &view->state.blame;
6210 *nlines = s->blame.nlines;
6211 *line_offsets = s->blame.line_offsets;
6212 *match = &s->matched_line;
6213 *first = &s->first_displayed_line;
6214 *last = &s->last_displayed_line;
6215 *selected = &s->selected_line;
6218 static const struct got_error *
6219 show_blame_view(struct tog_view *view)
6221 const struct got_error *err = NULL;
6222 struct tog_blame_view_state *s = &view->state.blame;
6225 if (s->blame.thread == 0 && !s->blame_complete) {
6226 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6227 &s->blame.thread_args);
6229 return got_error_set_errno(errcode, "pthread_create");
6231 halfdelay(1); /* fast refresh while annotating */
6234 if (s->blame_complete)
6235 halfdelay(10); /* disable fast refresh */
6237 err = draw_blame(view);
6243 static const struct got_error *
6244 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6245 struct got_repository *repo, struct got_object_id *id)
6247 struct tog_view *log_view;
6248 const struct got_error *err = NULL;
6252 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6253 if (log_view == NULL)
6254 return got_error_from_errno("view_open");
6256 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6258 view_close(log_view);
6260 *new_view = log_view;
6265 static const struct got_error *
6266 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6268 const struct got_error *err = NULL, *thread_err = NULL;
6269 struct tog_view *diff_view;
6270 struct tog_blame_view_state *s = &view->state.blame;
6271 int eos, nscroll, begin_y = 0, begin_x = 0;
6273 eos = nscroll = view->nlines - 2;
6274 if (view_is_hsplit_top(view))
6282 view->x = MAX(view->maxx - view->ncols / 3, 0);
6287 if (view->x + view->ncols / 3 < view->maxx)
6288 view->x += 2; /* move two columns right */
6294 view->x -= MIN(view->x, 2); /* move two columns back */
6303 s->selected_line = 1;
6304 s->first_displayed_line = 1;
6309 if (s->blame.nlines < eos) {
6310 s->selected_line = s->blame.nlines;
6311 s->first_displayed_line = 1;
6313 s->selected_line = eos;
6314 s->first_displayed_line = s->blame.nlines - (eos - 1);
6321 if (s->selected_line > 1)
6323 else if (s->selected_line == 1 &&
6324 s->first_displayed_line > 1)
6325 s->first_displayed_line--;
6336 if (s->first_displayed_line == 1) {
6337 if (view->count > 1)
6339 s->selected_line = MAX(1, s->selected_line - nscroll);
6343 if (s->first_displayed_line > nscroll)
6344 s->first_displayed_line -= nscroll;
6346 s->first_displayed_line = 1;
6351 if (s->selected_line < eos && s->first_displayed_line +
6352 s->selected_line <= s->blame.nlines)
6354 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6355 s->first_displayed_line++;
6361 struct got_object_id *id = NULL;
6364 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6365 s->first_displayed_line, s->selected_line);
6369 struct got_commit_object *commit, *pcommit;
6370 struct got_object_qid *pid;
6371 struct got_object_id *blob_id = NULL;
6373 err = got_object_open_as_commit(&commit,
6378 got_object_commit_get_parent_ids(commit));
6380 got_object_commit_close(commit);
6383 /* Check if path history ends here. */
6384 err = got_object_open_as_commit(&pcommit,
6388 err = got_object_id_by_path(&blob_id, s->repo,
6390 got_object_commit_close(pcommit);
6392 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6394 got_object_commit_close(commit);
6397 err = got_object_get_type(&obj_type, s->repo,
6400 /* Can't blame non-blob type objects. */
6401 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6402 got_object_commit_close(commit);
6405 err = got_object_qid_alloc(&s->blamed_commit,
6407 got_object_commit_close(commit);
6409 if (got_object_id_cmp(id,
6410 &s->blamed_commit->id) == 0)
6412 err = got_object_qid_alloc(&s->blamed_commit,
6418 thread_err = stop_blame(&s->blame);
6422 STAILQ_INSERT_HEAD(&s->blamed_commits,
6423 s->blamed_commit, entry);
6424 err = run_blame(view);
6430 struct got_object_qid *first;
6433 first = STAILQ_FIRST(&s->blamed_commits);
6434 if (!got_object_id_cmp(&first->id, s->commit_id))
6437 thread_err = stop_blame(&s->blame);
6441 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6442 got_object_qid_free(s->blamed_commit);
6444 STAILQ_FIRST(&s->blamed_commits);
6445 err = run_blame(view);
6452 s->id_to_log = get_selected_commit_id(s->blame.lines,
6453 s->blame.nlines, s->first_displayed_line, s->selected_line);
6455 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6459 struct got_object_id *id = NULL;
6460 struct got_object_qid *pid;
6461 struct got_commit_object *commit = NULL;
6464 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6465 s->first_displayed_line, s->selected_line);
6468 err = got_object_open_as_commit(&commit, s->repo, id);
6471 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6473 /* traversed from diff view, release diff resources */
6474 err = close_diff_view(*new_view);
6477 diff_view = *new_view;
6479 if (view_is_parent_view(view))
6480 view_get_split(view, &begin_y, &begin_x);
6482 diff_view = view_open(0, 0, begin_y, begin_x,
6484 if (diff_view == NULL) {
6485 got_object_commit_close(commit);
6486 err = got_error_from_errno("view_open");
6490 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6491 id, NULL, NULL, 3, 0, 0, view, s->repo);
6492 got_object_commit_close(commit);
6494 view_close(diff_view);
6497 s->last_diffed_line = s->first_displayed_line - 1 +
6500 break; /* still open from active diff view */
6501 if (view_is_parent_view(view) &&
6502 view->mode == TOG_VIEW_SPLIT_HRZN) {
6503 err = view_init_hsplit(view, begin_y);
6509 diff_view->focussed = 1;
6510 diff_view->mode = view->mode;
6511 diff_view->nlines = view->lines - begin_y;
6512 if (view_is_parent_view(view)) {
6513 view_transfer_size(diff_view, view);
6514 err = view_close_child(view);
6517 err = view_set_child(view, diff_view);
6520 view->focus_child = 1;
6522 *new_view = diff_view;
6535 if (s->last_displayed_line >= s->blame.nlines &&
6536 s->selected_line >= MIN(s->blame.nlines,
6537 view->nlines - 2)) {
6541 if (s->last_displayed_line >= s->blame.nlines &&
6542 s->selected_line < view->nlines - 2) {
6544 MIN(nscroll, s->last_displayed_line -
6545 s->first_displayed_line - s->selected_line + 1);
6547 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6548 s->first_displayed_line += nscroll;
6550 s->first_displayed_line =
6551 s->blame.nlines - (view->nlines - 3);
6554 if (s->selected_line > view->nlines - 2) {
6555 s->selected_line = MIN(s->blame.nlines,
6563 return thread_err ? thread_err : err;
6566 static const struct got_error *
6567 reset_blame_view(struct tog_view *view)
6569 const struct got_error *err;
6570 struct tog_blame_view_state *s = &view->state.blame;
6574 err = stop_blame(&s->blame);
6578 return run_blame(view);
6581 static const struct got_error *
6582 cmd_blame(int argc, char *argv[])
6584 const struct got_error *error;
6585 struct got_repository *repo = NULL;
6586 struct got_worktree *worktree = NULL;
6587 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6588 char *link_target = NULL;
6589 struct got_object_id *commit_id = NULL;
6590 struct got_commit_object *commit = NULL;
6591 char *commit_id_str = NULL;
6593 struct tog_view *view;
6594 int *pack_fds = NULL;
6596 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6599 commit_id_str = optarg;
6602 repo_path = realpath(optarg, NULL);
6603 if (repo_path == NULL)
6604 return got_error_from_errno2("realpath",
6619 error = got_repo_pack_fds_open(&pack_fds);
6623 if (repo_path == NULL) {
6624 cwd = getcwd(NULL, 0);
6626 return got_error_from_errno("getcwd");
6627 error = got_worktree_open(&worktree, cwd);
6628 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6632 strdup(got_worktree_get_repo_path(worktree));
6634 repo_path = strdup(cwd);
6635 if (repo_path == NULL) {
6636 error = got_error_from_errno("strdup");
6641 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6645 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6652 error = apply_unveil(got_repo_get_path(repo), NULL);
6656 error = tog_load_refs(repo, 0);
6660 if (commit_id_str == NULL) {
6661 struct got_reference *head_ref;
6662 error = got_ref_open(&head_ref, repo, worktree ?
6663 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6666 error = got_ref_resolve(&commit_id, repo, head_ref);
6667 got_ref_close(head_ref);
6669 error = got_repo_match_object_id(&commit_id, NULL,
6670 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6675 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6677 error = got_error_from_errno("view_open");
6681 error = got_object_open_as_commit(&commit, repo, commit_id);
6685 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6690 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6695 /* Release work tree lock. */
6696 got_worktree_close(worktree);
6699 error = view_loop(view);
6707 got_object_commit_close(commit);
6709 got_worktree_close(worktree);
6711 const struct got_error *close_err = got_repo_close(repo);
6716 const struct got_error *pack_err =
6717 got_repo_pack_fds_close(pack_fds);
6725 static const struct got_error *
6726 draw_tree_entries(struct tog_view *view, const char *parent_path)
6728 struct tog_tree_view_state *s = &view->state.tree;
6729 const struct got_error *err = NULL;
6730 struct got_tree_entry *te;
6733 struct tog_color *tc;
6734 int width, n, nentries, i = 1;
6735 int limit = view->nlines;
6738 if (view_is_hsplit_top(view))
6739 --limit; /* border */
6741 werase(view->window);
6746 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6750 if (view_needs_focus_indication(view))
6751 wstandout(view->window);
6752 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6754 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6755 waddwstr(view->window, wline);
6758 while (width++ < view->ncols)
6759 waddch(view->window, ' ');
6761 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6762 if (view_needs_focus_indication(view))
6763 wstandend(view->window);
6768 if (s->first_displayed_entry) {
6769 i += got_tree_entry_get_index(s->first_displayed_entry);
6770 if (s->tree != s->root)
6771 ++i; /* account for ".." entry */
6773 nentries = got_object_tree_get_nentries(s->tree);
6774 if (asprintf(&index, "[%d/%d] %s",
6775 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6776 return got_error_from_errno("asprintf");
6777 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6781 waddwstr(view->window, wline);
6784 if (width < view->ncols - 1)
6785 waddch(view->window, '\n');
6788 waddch(view->window, '\n');
6792 if (s->first_displayed_entry == NULL) {
6793 te = got_object_tree_get_first_entry(s->tree);
6794 if (s->selected == 0) {
6796 wstandout(view->window);
6797 s->selected_entry = NULL;
6799 waddstr(view->window, " ..\n"); /* parent directory */
6800 if (s->selected == 0 && view->focussed)
6801 wstandend(view->window);
6808 te = s->first_displayed_entry;
6811 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6812 char *line = NULL, *id_str = NULL, *link_target = NULL;
6813 const char *modestr = "";
6816 te = got_object_tree_get_entry(s->tree, i);
6817 mode = got_tree_entry_get_mode(te);
6820 err = got_object_id_str(&id_str,
6821 got_tree_entry_get_id(te));
6823 return got_error_from_errno(
6824 "got_object_id_str");
6826 if (got_object_tree_entry_is_submodule(te))
6828 else if (S_ISLNK(mode)) {
6831 err = got_tree_entry_get_symlink_target(&link_target,
6837 for (i = 0; i < strlen(link_target); i++) {
6838 if (!isprint((unsigned char)link_target[i]))
6839 link_target[i] = '?';
6843 else if (S_ISDIR(mode))
6845 else if (mode & S_IXUSR)
6847 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6848 got_tree_entry_get_name(te), modestr,
6849 link_target ? " -> ": "",
6850 link_target ? link_target : "") == -1) {
6853 return got_error_from_errno("asprintf");
6857 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6863 if (n == s->selected) {
6865 wstandout(view->window);
6866 s->selected_entry = te;
6868 tc = match_color(&s->colors, line);
6870 wattr_on(view->window,
6871 COLOR_PAIR(tc->colorpair), NULL);
6872 waddwstr(view->window, wline);
6874 wattr_off(view->window,
6875 COLOR_PAIR(tc->colorpair), NULL);
6876 if (width < view->ncols - 1)
6877 waddch(view->window, '\n');
6878 if (n == s->selected && view->focussed)
6879 wstandend(view->window);
6885 s->last_displayed_entry = te;
6894 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6896 struct got_tree_entry *te;
6897 int isroot = s->tree == s->root;
6900 if (s->first_displayed_entry == NULL)
6903 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6904 while (i++ < maxscroll) {
6907 s->first_displayed_entry = NULL;
6910 s->first_displayed_entry = te;
6911 te = got_tree_entry_get_prev(s->tree, te);
6915 static const struct got_error *
6916 tree_scroll_down(struct tog_view *view, int maxscroll)
6918 struct tog_tree_view_state *s = &view->state.tree;
6919 struct got_tree_entry *next, *last;
6922 if (s->first_displayed_entry)
6923 next = got_tree_entry_get_next(s->tree,
6924 s->first_displayed_entry);
6926 next = got_object_tree_get_first_entry(s->tree);
6928 last = s->last_displayed_entry;
6929 while (next && n++ < maxscroll) {
6931 s->last_displayed_entry = last;
6932 last = got_tree_entry_get_next(s->tree, last);
6934 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6935 s->first_displayed_entry = next;
6936 next = got_tree_entry_get_next(s->tree, next);
6943 static const struct got_error *
6944 tree_entry_path(char **path, struct tog_parent_trees *parents,
6945 struct got_tree_entry *te)
6947 const struct got_error *err = NULL;
6948 struct tog_parent_tree *pt;
6949 size_t len = 2; /* for leading slash and NUL */
6951 TAILQ_FOREACH(pt, parents, entry)
6952 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6955 len += strlen(got_tree_entry_get_name(te));
6957 *path = calloc(1, len);
6959 return got_error_from_errno("calloc");
6962 pt = TAILQ_LAST(parents, tog_parent_trees);
6964 const char *name = got_tree_entry_get_name(pt->selected_entry);
6965 if (strlcat(*path, name, len) >= len) {
6966 err = got_error(GOT_ERR_NO_SPACE);
6969 if (strlcat(*path, "/", len) >= len) {
6970 err = got_error(GOT_ERR_NO_SPACE);
6973 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6976 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6977 err = got_error(GOT_ERR_NO_SPACE);
6989 static const struct got_error *
6990 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6991 struct got_tree_entry *te, struct tog_parent_trees *parents,
6992 struct got_object_id *commit_id, struct got_repository *repo)
6994 const struct got_error *err = NULL;
6996 struct tog_view *blame_view;
7000 err = tree_entry_path(&path, parents, te);
7004 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7005 if (blame_view == NULL) {
7006 err = got_error_from_errno("view_open");
7010 err = open_blame_view(blame_view, path, commit_id, repo);
7012 if (err->code == GOT_ERR_CANCELLED)
7014 view_close(blame_view);
7016 *new_view = blame_view;
7022 static const struct got_error *
7023 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7024 struct tog_tree_view_state *s)
7026 struct tog_view *log_view;
7027 const struct got_error *err = NULL;
7032 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7033 if (log_view == NULL)
7034 return got_error_from_errno("view_open");
7036 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7040 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7043 view_close(log_view);
7045 *new_view = log_view;
7050 static const struct got_error *
7051 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7052 const char *head_ref_name, struct got_repository *repo)
7054 const struct got_error *err = NULL;
7055 char *commit_id_str = NULL;
7056 struct tog_tree_view_state *s = &view->state.tree;
7057 struct got_commit_object *commit = NULL;
7059 TAILQ_INIT(&s->parents);
7060 STAILQ_INIT(&s->colors);
7062 s->commit_id = got_object_id_dup(commit_id);
7063 if (s->commit_id == NULL)
7064 return got_error_from_errno("got_object_id_dup");
7066 err = got_object_open_as_commit(&commit, repo, commit_id);
7071 * The root is opened here and will be closed when the view is closed.
7072 * Any visited subtrees and their path-wise parents are opened and
7075 err = got_object_open_as_tree(&s->root, repo,
7076 got_object_commit_get_tree_id(commit));
7081 err = got_object_id_str(&commit_id_str, commit_id);
7085 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7086 err = got_error_from_errno("asprintf");
7090 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7091 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7092 if (head_ref_name) {
7093 s->head_ref_name = strdup(head_ref_name);
7094 if (s->head_ref_name == NULL) {
7095 err = got_error_from_errno("strdup");
7101 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7102 err = add_color(&s->colors, "\\$$",
7103 TOG_COLOR_TREE_SUBMODULE,
7104 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7107 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7108 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7111 err = add_color(&s->colors, "/$",
7112 TOG_COLOR_TREE_DIRECTORY,
7113 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7117 err = add_color(&s->colors, "\\*$",
7118 TOG_COLOR_TREE_EXECUTABLE,
7119 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7123 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7124 get_color_value("TOG_COLOR_COMMIT"));
7129 view->show = show_tree_view;
7130 view->input = input_tree_view;
7131 view->close = close_tree_view;
7132 view->search_start = search_start_tree_view;
7133 view->search_next = search_next_tree_view;
7135 free(commit_id_str);
7137 got_object_commit_close(commit);
7139 close_tree_view(view);
7143 static const struct got_error *
7144 close_tree_view(struct tog_view *view)
7146 struct tog_tree_view_state *s = &view->state.tree;
7148 free_colors(&s->colors);
7149 free(s->tree_label);
7150 s->tree_label = NULL;
7152 s->commit_id = NULL;
7153 free(s->head_ref_name);
7154 s->head_ref_name = NULL;
7155 while (!TAILQ_EMPTY(&s->parents)) {
7156 struct tog_parent_tree *parent;
7157 parent = TAILQ_FIRST(&s->parents);
7158 TAILQ_REMOVE(&s->parents, parent, entry);
7159 if (parent->tree != s->root)
7160 got_object_tree_close(parent->tree);
7164 if (s->tree != NULL && s->tree != s->root)
7165 got_object_tree_close(s->tree);
7167 got_object_tree_close(s->root);
7171 static const struct got_error *
7172 search_start_tree_view(struct tog_view *view)
7174 struct tog_tree_view_state *s = &view->state.tree;
7176 s->matched_entry = NULL;
7181 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7183 regmatch_t regmatch;
7185 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7189 static const struct got_error *
7190 search_next_tree_view(struct tog_view *view)
7192 struct tog_tree_view_state *s = &view->state.tree;
7193 struct got_tree_entry *te = NULL;
7195 if (!view->searching) {
7196 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7200 if (s->matched_entry) {
7201 if (view->searching == TOG_SEARCH_FORWARD) {
7202 if (s->selected_entry)
7203 te = got_tree_entry_get_next(s->tree,
7206 te = got_object_tree_get_first_entry(s->tree);
7208 if (s->selected_entry == NULL)
7209 te = got_object_tree_get_last_entry(s->tree);
7211 te = got_tree_entry_get_prev(s->tree,
7215 if (s->selected_entry)
7216 te = s->selected_entry;
7217 else if (view->searching == TOG_SEARCH_FORWARD)
7218 te = got_object_tree_get_first_entry(s->tree);
7220 te = got_object_tree_get_last_entry(s->tree);
7225 if (s->matched_entry == NULL) {
7226 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7229 if (view->searching == TOG_SEARCH_FORWARD)
7230 te = got_object_tree_get_first_entry(s->tree);
7232 te = got_object_tree_get_last_entry(s->tree);
7235 if (match_tree_entry(te, &view->regex)) {
7236 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7237 s->matched_entry = te;
7241 if (view->searching == TOG_SEARCH_FORWARD)
7242 te = got_tree_entry_get_next(s->tree, te);
7244 te = got_tree_entry_get_prev(s->tree, te);
7247 if (s->matched_entry) {
7248 s->first_displayed_entry = s->matched_entry;
7255 static const struct got_error *
7256 show_tree_view(struct tog_view *view)
7258 const struct got_error *err = NULL;
7259 struct tog_tree_view_state *s = &view->state.tree;
7262 err = tree_entry_path(&parent_path, &s->parents, NULL);
7266 err = draw_tree_entries(view, parent_path);
7273 static const struct got_error *
7274 tree_goto_line(struct tog_view *view, int nlines)
7276 const struct got_error *err = NULL;
7277 struct tog_tree_view_state *s = &view->state.tree;
7278 struct got_tree_entry **fte, **lte, **ste;
7279 int g, last, first = 1, i = 1;
7280 int root = s->tree == s->root;
7281 int off = root ? 1 : 2;
7288 else if (g > got_object_tree_get_nentries(s->tree))
7289 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7291 fte = &s->first_displayed_entry;
7292 lte = &s->last_displayed_entry;
7293 ste = &s->selected_entry;
7296 first = got_tree_entry_get_index(*fte);
7297 first += off; /* account for ".." */
7299 last = got_tree_entry_get_index(*lte);
7302 if (g >= first && g <= last && g - first < nlines) {
7303 s->selected = g - first;
7304 return NULL; /* gline is on the current page */
7308 i = got_tree_entry_get_index(*ste);
7313 err = tree_scroll_down(view, g - i);
7316 if (got_tree_entry_get_index(*lte) >=
7317 got_object_tree_get_nentries(s->tree) - 1 &&
7318 first + s->selected < g &&
7319 s->selected < s->ndisplayed - 1) {
7320 first = got_tree_entry_get_index(*fte);
7322 s->selected = g - first;
7325 tree_scroll_up(s, i - g);
7328 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7329 s->selected = g - 1;
7334 static const struct got_error *
7335 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7337 const struct got_error *err = NULL;
7338 struct tog_tree_view_state *s = &view->state.tree;
7339 struct got_tree_entry *te;
7340 int n, nscroll = view->nlines - 3;
7343 return tree_goto_line(view, nscroll);
7347 s->show_ids = !s->show_ids;
7352 if (!s->selected_entry)
7354 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7358 err = view_request_new(new_view, view, TOG_VIEW_REF);
7365 if (s->tree == s->root)
7366 s->first_displayed_entry =
7367 got_object_tree_get_first_entry(s->tree);
7369 s->first_displayed_entry = NULL;
7374 int eos = view->nlines - 3;
7376 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7380 te = got_object_tree_get_last_entry(s->tree);
7381 for (n = 0; n < eos; n++) {
7383 if (s->tree != s->root) {
7384 s->first_displayed_entry = NULL;
7389 s->first_displayed_entry = te;
7390 te = got_tree_entry_get_prev(s->tree, te);
7393 s->selected = n - 1;
7399 if (s->selected > 0) {
7403 tree_scroll_up(s, 1);
7404 if (s->selected_entry == NULL ||
7405 (s->tree == s->root && s->selected_entry ==
7406 got_object_tree_get_first_entry(s->tree)))
7416 if (s->tree == s->root) {
7417 if (got_object_tree_get_first_entry(s->tree) ==
7418 s->first_displayed_entry)
7419 s->selected -= MIN(s->selected, nscroll);
7421 if (s->first_displayed_entry == NULL)
7422 s->selected -= MIN(s->selected, nscroll);
7424 tree_scroll_up(s, MAX(0, nscroll));
7425 if (s->selected_entry == NULL ||
7426 (s->tree == s->root && s->selected_entry ==
7427 got_object_tree_get_first_entry(s->tree)))
7433 if (s->selected < s->ndisplayed - 1) {
7437 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7439 /* can't scroll any further */
7443 tree_scroll_down(view, 1);
7453 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7455 /* can't scroll any further; move cursor down */
7456 if (s->selected < s->ndisplayed - 1)
7457 s->selected += MIN(nscroll,
7458 s->ndisplayed - s->selected - 1);
7463 tree_scroll_down(view, nscroll);
7468 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7469 struct tog_parent_tree *parent;
7470 /* user selected '..' */
7471 if (s->tree == s->root) {
7475 parent = TAILQ_FIRST(&s->parents);
7476 TAILQ_REMOVE(&s->parents, parent,
7478 got_object_tree_close(s->tree);
7479 s->tree = parent->tree;
7480 s->first_displayed_entry =
7481 parent->first_displayed_entry;
7483 parent->selected_entry;
7484 s->selected = parent->selected;
7485 if (s->selected > view->nlines - 3) {
7486 err = offset_selection_down(view);
7491 } else if (S_ISDIR(got_tree_entry_get_mode(
7492 s->selected_entry))) {
7493 struct got_tree_object *subtree;
7495 err = got_object_open_as_tree(&subtree, s->repo,
7496 got_tree_entry_get_id(s->selected_entry));
7499 err = tree_view_visit_subtree(s, subtree);
7501 got_object_tree_close(subtree);
7504 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7505 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7508 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7509 s->selected = view->nlines - 4;
7525 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7530 static const struct got_error *
7531 cmd_tree(int argc, char *argv[])
7533 const struct got_error *error;
7534 struct got_repository *repo = NULL;
7535 struct got_worktree *worktree = NULL;
7536 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7537 struct got_object_id *commit_id = NULL;
7538 struct got_commit_object *commit = NULL;
7539 const char *commit_id_arg = NULL;
7541 struct got_reference *ref = NULL;
7542 const char *head_ref_name = NULL;
7544 struct tog_view *view;
7545 int *pack_fds = NULL;
7547 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7550 commit_id_arg = optarg;
7553 repo_path = realpath(optarg, NULL);
7554 if (repo_path == NULL)
7555 return got_error_from_errno2("realpath",
7570 error = got_repo_pack_fds_open(&pack_fds);
7574 if (repo_path == NULL) {
7575 cwd = getcwd(NULL, 0);
7577 return got_error_from_errno("getcwd");
7578 error = got_worktree_open(&worktree, cwd);
7579 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7583 strdup(got_worktree_get_repo_path(worktree));
7585 repo_path = strdup(cwd);
7586 if (repo_path == NULL) {
7587 error = got_error_from_errno("strdup");
7592 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7596 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7603 error = apply_unveil(got_repo_get_path(repo), NULL);
7607 error = tog_load_refs(repo, 0);
7611 if (commit_id_arg == NULL) {
7612 error = got_repo_match_object_id(&commit_id, &label,
7613 worktree ? got_worktree_get_head_ref_name(worktree) :
7614 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7617 head_ref_name = label;
7619 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7621 head_ref_name = got_ref_get_name(ref);
7622 else if (error->code != GOT_ERR_NOT_REF)
7624 error = got_repo_match_object_id(&commit_id, NULL,
7625 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7630 error = got_object_open_as_commit(&commit, repo, commit_id);
7634 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7636 error = got_error_from_errno("view_open");
7639 error = open_tree_view(view, commit_id, head_ref_name, repo);
7642 if (!got_path_is_root_dir(in_repo_path)) {
7643 error = tree_view_walk_path(&view->state.tree, commit,
7650 /* Release work tree lock. */
7651 got_worktree_close(worktree);
7654 error = view_loop(view);
7663 const struct got_error *close_err = got_repo_close(repo);
7668 const struct got_error *pack_err =
7669 got_repo_pack_fds_close(pack_fds);
7677 static const struct got_error *
7678 ref_view_load_refs(struct tog_ref_view_state *s)
7680 struct got_reflist_entry *sre;
7681 struct tog_reflist_entry *re;
7684 TAILQ_FOREACH(sre, &tog_refs, entry) {
7685 if (strncmp(got_ref_get_name(sre->ref),
7686 "refs/got/", 9) == 0 &&
7687 strncmp(got_ref_get_name(sre->ref),
7688 "refs/got/backup/", 16) != 0)
7691 re = malloc(sizeof(*re));
7693 return got_error_from_errno("malloc");
7695 re->ref = got_ref_dup(sre->ref);
7696 if (re->ref == NULL)
7697 return got_error_from_errno("got_ref_dup");
7698 re->idx = s->nrefs++;
7699 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7702 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7707 ref_view_free_refs(struct tog_ref_view_state *s)
7709 struct tog_reflist_entry *re;
7711 while (!TAILQ_EMPTY(&s->refs)) {
7712 re = TAILQ_FIRST(&s->refs);
7713 TAILQ_REMOVE(&s->refs, re, entry);
7714 got_ref_close(re->ref);
7719 static const struct got_error *
7720 open_ref_view(struct tog_view *view, struct got_repository *repo)
7722 const struct got_error *err = NULL;
7723 struct tog_ref_view_state *s = &view->state.ref;
7725 s->selected_entry = 0;
7728 TAILQ_INIT(&s->refs);
7729 STAILQ_INIT(&s->colors);
7731 err = ref_view_load_refs(s);
7735 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7736 err = add_color(&s->colors, "^refs/heads/",
7737 TOG_COLOR_REFS_HEADS,
7738 get_color_value("TOG_COLOR_REFS_HEADS"));
7742 err = add_color(&s->colors, "^refs/tags/",
7743 TOG_COLOR_REFS_TAGS,
7744 get_color_value("TOG_COLOR_REFS_TAGS"));
7748 err = add_color(&s->colors, "^refs/remotes/",
7749 TOG_COLOR_REFS_REMOTES,
7750 get_color_value("TOG_COLOR_REFS_REMOTES"));
7754 err = add_color(&s->colors, "^refs/got/backup/",
7755 TOG_COLOR_REFS_BACKUP,
7756 get_color_value("TOG_COLOR_REFS_BACKUP"));
7761 view->show = show_ref_view;
7762 view->input = input_ref_view;
7763 view->close = close_ref_view;
7764 view->search_start = search_start_ref_view;
7765 view->search_next = search_next_ref_view;
7768 free_colors(&s->colors);
7772 static const struct got_error *
7773 close_ref_view(struct tog_view *view)
7775 struct tog_ref_view_state *s = &view->state.ref;
7777 ref_view_free_refs(s);
7778 free_colors(&s->colors);
7783 static const struct got_error *
7784 resolve_reflist_entry(struct got_object_id **commit_id,
7785 struct tog_reflist_entry *re, struct got_repository *repo)
7787 const struct got_error *err = NULL;
7788 struct got_object_id *obj_id;
7789 struct got_tag_object *tag = NULL;
7794 err = got_ref_resolve(&obj_id, repo, re->ref);
7798 err = got_object_get_type(&obj_type, repo, obj_id);
7803 case GOT_OBJ_TYPE_COMMIT:
7804 *commit_id = obj_id;
7806 case GOT_OBJ_TYPE_TAG:
7807 err = got_object_open_as_tag(&tag, repo, obj_id);
7811 err = got_object_get_type(&obj_type, repo,
7812 got_object_tag_get_object_id(tag));
7815 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7816 err = got_error(GOT_ERR_OBJ_TYPE);
7819 *commit_id = got_object_id_dup(
7820 got_object_tag_get_object_id(tag));
7821 if (*commit_id == NULL) {
7822 err = got_error_from_errno("got_object_id_dup");
7827 err = got_error(GOT_ERR_OBJ_TYPE);
7833 got_object_tag_close(tag);
7841 static const struct got_error *
7842 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7843 struct tog_reflist_entry *re, struct got_repository *repo)
7845 struct tog_view *log_view;
7846 const struct got_error *err = NULL;
7847 struct got_object_id *commit_id = NULL;
7851 err = resolve_reflist_entry(&commit_id, re, repo);
7853 if (err->code != GOT_ERR_OBJ_TYPE)
7859 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7860 if (log_view == NULL) {
7861 err = got_error_from_errno("view_open");
7865 err = open_log_view(log_view, commit_id, repo,
7866 got_ref_get_name(re->ref), "", 0);
7869 view_close(log_view);
7871 *new_view = log_view;
7877 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7879 struct tog_reflist_entry *re;
7882 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7885 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7886 while (i++ < maxscroll) {
7889 s->first_displayed_entry = re;
7890 re = TAILQ_PREV(re, tog_reflist_head, entry);
7894 static const struct got_error *
7895 ref_scroll_down(struct tog_view *view, int maxscroll)
7897 struct tog_ref_view_state *s = &view->state.ref;
7898 struct tog_reflist_entry *next, *last;
7901 if (s->first_displayed_entry)
7902 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7904 next = TAILQ_FIRST(&s->refs);
7906 last = s->last_displayed_entry;
7907 while (next && n++ < maxscroll) {
7909 s->last_displayed_entry = last;
7910 last = TAILQ_NEXT(last, entry);
7912 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7913 s->first_displayed_entry = next;
7914 next = TAILQ_NEXT(next, entry);
7921 static const struct got_error *
7922 search_start_ref_view(struct tog_view *view)
7924 struct tog_ref_view_state *s = &view->state.ref;
7926 s->matched_entry = NULL;
7931 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7933 regmatch_t regmatch;
7935 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7939 static const struct got_error *
7940 search_next_ref_view(struct tog_view *view)
7942 struct tog_ref_view_state *s = &view->state.ref;
7943 struct tog_reflist_entry *re = NULL;
7945 if (!view->searching) {
7946 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7950 if (s->matched_entry) {
7951 if (view->searching == TOG_SEARCH_FORWARD) {
7952 if (s->selected_entry)
7953 re = TAILQ_NEXT(s->selected_entry, entry);
7955 re = TAILQ_PREV(s->selected_entry,
7956 tog_reflist_head, entry);
7958 if (s->selected_entry == NULL)
7959 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7961 re = TAILQ_PREV(s->selected_entry,
7962 tog_reflist_head, entry);
7965 if (s->selected_entry)
7966 re = s->selected_entry;
7967 else if (view->searching == TOG_SEARCH_FORWARD)
7968 re = TAILQ_FIRST(&s->refs);
7970 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7975 if (s->matched_entry == NULL) {
7976 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7979 if (view->searching == TOG_SEARCH_FORWARD)
7980 re = TAILQ_FIRST(&s->refs);
7982 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7985 if (match_reflist_entry(re, &view->regex)) {
7986 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7987 s->matched_entry = re;
7991 if (view->searching == TOG_SEARCH_FORWARD)
7992 re = TAILQ_NEXT(re, entry);
7994 re = TAILQ_PREV(re, tog_reflist_head, entry);
7997 if (s->matched_entry) {
7998 s->first_displayed_entry = s->matched_entry;
8005 static const struct got_error *
8006 show_ref_view(struct tog_view *view)
8008 const struct got_error *err = NULL;
8009 struct tog_ref_view_state *s = &view->state.ref;
8010 struct tog_reflist_entry *re;
8013 struct tog_color *tc;
8015 int limit = view->nlines;
8017 werase(view->window);
8020 if (view_is_hsplit_top(view))
8021 --limit; /* border */
8026 re = s->first_displayed_entry;
8028 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8030 return got_error_from_errno("asprintf");
8032 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8037 if (view_needs_focus_indication(view))
8038 wstandout(view->window);
8039 waddwstr(view->window, wline);
8040 while (width++ < view->ncols)
8041 waddch(view->window, ' ');
8042 if (view_needs_focus_indication(view))
8043 wstandend(view->window);
8052 while (re && limit > 0) {
8054 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8057 struct got_commit_object *ci;
8058 struct got_tag_object *tag;
8059 struct got_object_id *id;
8063 err = got_ref_resolve(&id, s->repo, re->ref);
8066 err = got_object_open_as_tag(&tag, s->repo, id);
8068 if (err->code != GOT_ERR_OBJ_TYPE) {
8072 err = got_object_open_as_commit(&ci, s->repo,
8078 t = got_object_commit_get_committer_time(ci);
8079 got_object_commit_close(ci);
8081 t = got_object_tag_get_tagger_time(tag);
8082 got_object_tag_close(tag);
8085 if (gmtime_r(&t, &tm) == NULL)
8086 return got_error_from_errno("gmtime_r");
8087 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8088 return got_error(GOT_ERR_NO_SPACE);
8090 if (got_ref_is_symbolic(re->ref)) {
8091 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8092 ymd : "", got_ref_get_name(re->ref),
8093 got_ref_get_symref_target(re->ref)) == -1)
8094 return got_error_from_errno("asprintf");
8095 } else if (s->show_ids) {
8096 struct got_object_id *id;
8098 err = got_ref_resolve(&id, s->repo, re->ref);
8101 err = got_object_id_str(&id_str, id);
8106 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8107 got_ref_get_name(re->ref), id_str) == -1) {
8108 err = got_error_from_errno("asprintf");
8115 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8116 got_ref_get_name(re->ref)) == -1)
8117 return got_error_from_errno("asprintf");
8119 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8125 if (n == s->selected) {
8127 wstandout(view->window);
8128 s->selected_entry = re;
8130 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8132 wattr_on(view->window,
8133 COLOR_PAIR(tc->colorpair), NULL);
8134 waddwstr(view->window, wline);
8136 wattr_off(view->window,
8137 COLOR_PAIR(tc->colorpair), NULL);
8138 if (width < view->ncols - 1)
8139 waddch(view->window, '\n');
8140 if (n == s->selected && view->focussed)
8141 wstandend(view->window);
8147 s->last_displayed_entry = re;
8150 re = TAILQ_NEXT(re, entry);
8157 static const struct got_error *
8158 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8159 struct tog_reflist_entry *re, struct got_repository *repo)
8161 const struct got_error *err = NULL;
8162 struct got_object_id *commit_id = NULL;
8163 struct tog_view *tree_view;
8167 err = resolve_reflist_entry(&commit_id, re, repo);
8169 if (err->code != GOT_ERR_OBJ_TYPE)
8176 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8177 if (tree_view == NULL) {
8178 err = got_error_from_errno("view_open");
8182 err = open_tree_view(tree_view, commit_id,
8183 got_ref_get_name(re->ref), repo);
8187 *new_view = tree_view;
8193 static const struct got_error *
8194 ref_goto_line(struct tog_view *view, int nlines)
8196 const struct got_error *err = NULL;
8197 struct tog_ref_view_state *s = &view->state.ref;
8198 int g, idx = s->selected_entry->idx;
8205 else if (g > s->nrefs)
8208 if (g >= s->first_displayed_entry->idx + 1 &&
8209 g <= s->last_displayed_entry->idx + 1 &&
8210 g - s->first_displayed_entry->idx - 1 < nlines) {
8211 s->selected = g - s->first_displayed_entry->idx - 1;
8216 err = ref_scroll_down(view, g - idx - 1);
8219 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8220 s->first_displayed_entry->idx + s->selected < g &&
8221 s->selected < s->ndisplayed - 1)
8222 s->selected = g - s->first_displayed_entry->idx - 1;
8223 } else if (idx + 1 > g)
8224 ref_scroll_up(s, idx - g + 1);
8226 if (g < nlines && s->first_displayed_entry->idx == 0)
8227 s->selected = g - 1;
8233 static const struct got_error *
8234 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8236 const struct got_error *err = NULL;
8237 struct tog_ref_view_state *s = &view->state.ref;
8238 struct tog_reflist_entry *re;
8239 int n, nscroll = view->nlines - 1;
8242 return ref_goto_line(view, nscroll);
8246 s->show_ids = !s->show_ids;
8250 s->show_date = !s->show_date;
8254 s->sort_by_date = !s->sort_by_date;
8256 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8257 got_ref_cmp_by_commit_timestamp_descending :
8258 tog_ref_cmp_by_name, s->repo);
8261 got_reflist_object_id_map_free(tog_refs_idmap);
8262 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8263 &tog_refs, s->repo);
8266 ref_view_free_refs(s);
8267 err = ref_view_load_refs(s);
8272 if (!s->selected_entry)
8274 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8278 if (!s->selected_entry)
8280 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8287 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8292 int eos = view->nlines - 1;
8294 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8298 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8299 for (n = 0; n < eos; n++) {
8302 s->first_displayed_entry = re;
8303 re = TAILQ_PREV(re, tog_reflist_head, entry);
8306 s->selected = n - 1;
8312 if (s->selected > 0) {
8316 ref_scroll_up(s, 1);
8317 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8327 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8328 s->selected -= MIN(nscroll, s->selected);
8329 ref_scroll_up(s, MAX(0, nscroll));
8330 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8336 if (s->selected < s->ndisplayed - 1) {
8340 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8341 /* can't scroll any further */
8345 ref_scroll_down(view, 1);
8355 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8356 /* can't scroll any further; move cursor down */
8357 if (s->selected < s->ndisplayed - 1)
8358 s->selected += MIN(nscroll,
8359 s->ndisplayed - s->selected - 1);
8360 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8361 s->selected += s->ndisplayed - s->selected - 1;
8365 ref_scroll_down(view, nscroll);
8370 err = tog_load_refs(s->repo, s->sort_by_date);
8373 ref_view_free_refs(s);
8374 err = ref_view_load_refs(s);
8377 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8378 s->selected = view->nlines - 2;
8392 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8397 static const struct got_error *
8398 cmd_ref(int argc, char *argv[])
8400 const struct got_error *error;
8401 struct got_repository *repo = NULL;
8402 struct got_worktree *worktree = NULL;
8403 char *cwd = NULL, *repo_path = NULL;
8405 struct tog_view *view;
8406 int *pack_fds = NULL;
8408 while ((ch = getopt(argc, argv, "r:")) != -1) {
8411 repo_path = realpath(optarg, NULL);
8412 if (repo_path == NULL)
8413 return got_error_from_errno2("realpath",
8428 error = got_repo_pack_fds_open(&pack_fds);
8432 if (repo_path == NULL) {
8433 cwd = getcwd(NULL, 0);
8435 return got_error_from_errno("getcwd");
8436 error = got_worktree_open(&worktree, cwd);
8437 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8441 strdup(got_worktree_get_repo_path(worktree));
8443 repo_path = strdup(cwd);
8444 if (repo_path == NULL) {
8445 error = got_error_from_errno("strdup");
8450 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8456 error = apply_unveil(got_repo_get_path(repo), NULL);
8460 error = tog_load_refs(repo, 0);
8464 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8466 error = got_error_from_errno("view_open");
8470 error = open_ref_view(view, repo);
8475 /* Release work tree lock. */
8476 got_worktree_close(worktree);
8479 error = view_loop(view);
8484 const struct got_error *close_err = got_repo_close(repo);
8489 const struct got_error *pack_err =
8490 got_repo_pack_fds_close(pack_fds);
8498 static const struct got_error*
8499 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8508 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8512 if (mvwprintw(win, y, x, "%s", str) == ERR)
8513 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8520 static const struct got_error *
8521 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8525 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8527 free(*line_offsets);
8528 *line_offsets = NULL;
8529 return got_error_from_errno("reallocarray");
8533 (*line_offsets)[*nlines] = off;
8538 static const struct got_error *
8539 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8543 for (;n > 0; --n, ++km) {
8547 if (km->keys == NULL)
8550 t = t0 = strdup(km->keys);
8552 return got_error_from_errno("strdup");
8555 while ((k = strsep(&t, " ")) != NULL)
8556 len += strlen(k) > 1 ? 2 : 0;
8558 *ret = MAX(*ret, len);
8565 * Write keymap section headers, keys, and key info in km to f.
8566 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8567 * wrap control and symbolic keys in guillemets, else use <>.
8569 static const struct got_error *
8570 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8575 static const char *u8_glyph[] = {
8576 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8577 "\xe2\x80\xba" /* U+203A (utf8 >) */
8580 int cs, s, first = 1;
8582 cs = got_locale_is_utf8();
8584 t = t0 = strdup(km->keys);
8586 return got_error_from_errno("strdup");
8588 len = strlen(km->keys);
8589 while ((k = strsep(&t, " ")) != NULL) {
8590 s = strlen(k) > 1; /* control or symbolic key */
8591 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8592 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8593 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8596 return got_error_from_errno("fprintf");
8604 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8606 return got_error_from_errno("fprintf");
8612 static const struct got_error *
8613 format_help(struct tog_help_view_state *s)
8615 const struct got_error *err = NULL;
8617 int i, max, n, show = s->all;
8618 static const struct tog_key_map km[] = {
8619 #define KEYMAP_(info, type) { NULL, (info), type }
8620 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8626 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8631 err = max_key_str(&max, km, n);
8635 for (i = 0; i < n; ++i) {
8636 if (km[i].keys == NULL) {
8638 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8639 km[i].type == s->type || s->all)
8643 err = format_help_line(&off, s->f, &km[i], max);
8646 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8653 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8657 static const struct got_error *
8658 create_help(struct tog_help_view_state *s)
8661 const struct got_error *err;
8663 free(s->line_offsets);
8664 s->line_offsets = NULL;
8669 return got_error_from_errno("got_opentemp");
8672 err = format_help(s);
8676 if (s->f && fflush(s->f) != 0)
8677 return got_error_from_errno("fflush");
8682 static const struct got_error *
8683 search_start_help_view(struct tog_view *view)
8685 view->state.help.matched_line = 0;
8690 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8691 size_t *nlines, int **first, int **last, int **match, int **selected)
8693 struct tog_help_view_state *s = &view->state.help;
8696 *nlines = s->nlines;
8697 *line_offsets = s->line_offsets;
8698 *match = &s->matched_line;
8699 *first = &s->first_displayed_line;
8700 *last = &s->last_displayed_line;
8701 *selected = &s->selected_line;
8704 static const struct got_error *
8705 show_help_view(struct tog_view *view)
8707 struct tog_help_view_state *s = &view->state.help;
8708 const struct got_error *err;
8709 regmatch_t *regmatch = &view->regmatch;
8714 int width, nprinted = 0, rc = 0;
8715 int eos = view->nlines;
8717 if (view_is_hsplit_top(view))
8718 --eos; /* account for border */
8722 werase(view->window);
8724 if (view->gline > s->nlines - 1)
8725 view->gline = s->nlines - 1;
8727 err = win_draw_center(view->window, 0, 0, view->ncols,
8728 view_needs_focus_indication(view),
8729 "tog help (press q to return to tog)");
8734 waddstr(view->window, "\n\n");
8740 while (eos > 0 && nprinted < eos) {
8743 linelen = getline(&line, &linesz, s->f);
8744 if (linelen == -1) {
8747 return got_ferror(s->f, GOT_ERR_IO);
8752 if (++s->lineno < s->first_displayed_line)
8754 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8756 if (s->lineno == view->hiline)
8759 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8765 view->maxx = MAX(view->maxx, width);
8770 wattron(view->window, attr);
8771 if (s->first_displayed_line + nprinted == s->matched_line &&
8772 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8773 err = add_matched_line(&width, line, view->ncols - 1, 0,
8774 view->window, view->x, regmatch);
8782 err = format_line(&wline, &width, &skip, line,
8783 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8788 rc = waddwstr(view->window, &wline[skip]);
8792 return got_error_msg(GOT_ERR_IO, "waddwstr");
8794 if (s->lineno == view->hiline) {
8795 while (width++ < view->ncols)
8796 waddch(view->window, ' ');
8798 if (width <= view->ncols)
8799 waddch(view->window, '\n');
8802 wattroff(view->window, attr);
8803 if (++nprinted == 1)
8804 s->first_displayed_line = s->lineno;
8808 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8810 s->last_displayed_line = s->first_displayed_line;
8815 rc = waddnstr(view->window,
8816 "See the tog(1) manual page for full documentation",
8819 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8821 wmove(view->window, view->nlines - 1, 0);
8822 wclrtoeol(view->window);
8823 wstandout(view->window);
8824 rc = waddnstr(view->window, "scroll down for more...",
8827 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8828 if (getcurx(view->window) < view->ncols - 6) {
8829 rc = wprintw(view->window, "[%.0f%%]",
8830 100.00 * s->last_displayed_line / s->nlines);
8832 return got_error_msg(GOT_ERR_IO, "wprintw");
8834 wstandend(view->window);
8840 static const struct got_error *
8841 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8843 struct tog_help_view_state *s = &view->state.help;
8844 const struct got_error *err = NULL;
8850 eos = nscroll = view->nlines;
8851 if (view_is_hsplit_top(view))
8854 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8861 view->x = MAX(view->maxx - view->ncols / 3, 0);
8866 if (view->x + view->ncols / 3 < view->maxx)
8873 view->x -= MIN(view->x, 2);
8879 s->first_displayed_line = 1;
8887 s->first_displayed_line = (s->nlines - eos) + 3;
8892 if (s->first_displayed_line > 1)
8893 --s->first_displayed_line;
8904 if (s->first_displayed_line == 1) {
8908 while (--nscroll > 0 && s->first_displayed_line > 1)
8909 s->first_displayed_line--;
8915 ++s->first_displayed_line;
8931 while (!s->eof && --nscroll > 0) {
8932 linelen = getline(&line, &linesz, s->f);
8933 s->first_displayed_line++;
8934 if (linelen == -1) {
8938 err = got_ferror(s->f, GOT_ERR_IO);
8952 static const struct got_error *
8953 close_help_view(struct tog_view *view)
8955 struct tog_help_view_state *s = &view->state.help;
8957 free(s->line_offsets);
8958 s->line_offsets = NULL;
8959 if (fclose(s->f) == EOF)
8960 return got_error_from_errno("fclose");
8965 static const struct got_error *
8966 reset_help_view(struct tog_view *view)
8968 struct tog_help_view_state *s = &view->state.help;
8971 if (s->f && fclose(s->f) == EOF)
8972 return got_error_from_errno("fclose");
8974 wclear(view->window);
8978 s->first_displayed_line = 1;
8979 s->last_displayed_line = view->nlines;
8980 s->matched_line = 0;
8982 return create_help(s);
8985 static const struct got_error *
8986 open_help_view(struct tog_view *view, struct tog_view *parent)
8988 const struct got_error *err = NULL;
8989 struct tog_help_view_state *s = &view->state.help;
8991 s->type = (enum tog_keymap_type)parent->type;
8992 s->first_displayed_line = 1;
8993 s->last_displayed_line = view->nlines;
8994 s->selected_line = 1;
8996 view->show = show_help_view;
8997 view->input = input_help_view;
8998 view->reset = reset_help_view;
8999 view->close = close_help_view;
9000 view->search_start = search_start_help_view;
9001 view->search_setup = search_setup_help_view;
9002 view->search_next = search_next_view_match;
9004 err = create_help(s);
9008 static const struct got_error *
9009 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9010 enum tog_view_type request, int y, int x)
9012 const struct got_error *err = NULL;
9018 if (view->type == TOG_VIEW_LOG) {
9019 struct tog_log_view_state *s = &view->state.log;
9021 err = open_diff_view_for_commit(new_view, y, x,
9022 s->selected_entry->commit, s->selected_entry->id,
9025 return got_error_msg(GOT_ERR_NOT_IMPL,
9026 "parent/child view pair not supported");
9028 case TOG_VIEW_BLAME:
9029 if (view->type == TOG_VIEW_TREE) {
9030 struct tog_tree_view_state *s = &view->state.tree;
9032 err = blame_tree_entry(new_view, y, x,
9033 s->selected_entry, &s->parents, s->commit_id,
9036 return got_error_msg(GOT_ERR_NOT_IMPL,
9037 "parent/child view pair not supported");
9040 if (view->type == TOG_VIEW_BLAME)
9041 err = log_annotated_line(new_view, y, x,
9042 view->state.blame.repo, view->state.blame.id_to_log);
9043 else if (view->type == TOG_VIEW_TREE)
9044 err = log_selected_tree_entry(new_view, y, x,
9046 else if (view->type == TOG_VIEW_REF)
9047 err = log_ref_entry(new_view, y, x,
9048 view->state.ref.selected_entry,
9049 view->state.ref.repo);
9051 return got_error_msg(GOT_ERR_NOT_IMPL,
9052 "parent/child view pair not supported");
9055 if (view->type == TOG_VIEW_LOG)
9056 err = browse_commit_tree(new_view, y, x,
9057 view->state.log.selected_entry,
9058 view->state.log.in_repo_path,
9059 view->state.log.head_ref_name,
9060 view->state.log.repo);
9061 else if (view->type == TOG_VIEW_REF)
9062 err = browse_ref_tree(new_view, y, x,
9063 view->state.ref.selected_entry,
9064 view->state.ref.repo);
9066 return got_error_msg(GOT_ERR_NOT_IMPL,
9067 "parent/child view pair not supported");
9070 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9071 if (*new_view == NULL)
9072 return got_error_from_errno("view_open");
9073 if (view->type == TOG_VIEW_LOG)
9074 err = open_ref_view(*new_view, view->state.log.repo);
9075 else if (view->type == TOG_VIEW_TREE)
9076 err = open_ref_view(*new_view, view->state.tree.repo);
9078 err = got_error_msg(GOT_ERR_NOT_IMPL,
9079 "parent/child view pair not supported");
9081 view_close(*new_view);
9084 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9085 if (*new_view == NULL)
9086 return got_error_from_errno("view_open");
9087 err = open_help_view(*new_view, view);
9089 view_close(*new_view);
9092 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9099 * If view was scrolled down to move the selected line into view when opening a
9100 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9103 offset_selection_up(struct tog_view *view)
9105 switch (view->type) {
9106 case TOG_VIEW_BLAME: {
9107 struct tog_blame_view_state *s = &view->state.blame;
9108 if (s->first_displayed_line == 1) {
9109 s->selected_line = MAX(s->selected_line - view->offset,
9113 if (s->first_displayed_line > view->offset)
9114 s->first_displayed_line -= view->offset;
9116 s->first_displayed_line = 1;
9117 s->selected_line += view->offset;
9121 log_scroll_up(&view->state.log, view->offset);
9122 view->state.log.selected += view->offset;
9125 ref_scroll_up(&view->state.ref, view->offset);
9126 view->state.ref.selected += view->offset;
9129 tree_scroll_up(&view->state.tree, view->offset);
9130 view->state.tree.selected += view->offset;
9140 * If the selected line is in the section of screen covered by the bottom split,
9141 * scroll down offset lines to move it into view and index its new position.
9143 static const struct got_error *
9144 offset_selection_down(struct tog_view *view)
9146 const struct got_error *err = NULL;
9147 const struct got_error *(*scrolld)(struct tog_view *, int);
9148 int *selected = NULL;
9151 switch (view->type) {
9152 case TOG_VIEW_BLAME: {
9153 struct tog_blame_view_state *s = &view->state.blame;
9156 if (s->selected_line > view->nlines - header) {
9157 offset = abs(view->nlines - s->selected_line - header);
9158 s->first_displayed_line += offset;
9159 s->selected_line -= offset;
9160 view->offset = offset;
9164 case TOG_VIEW_LOG: {
9165 struct tog_log_view_state *s = &view->state.log;
9166 scrolld = &log_scroll_down;
9167 header = view_is_parent_view(view) ? 3 : 2;
9168 selected = &s->selected;
9171 case TOG_VIEW_REF: {
9172 struct tog_ref_view_state *s = &view->state.ref;
9173 scrolld = &ref_scroll_down;
9175 selected = &s->selected;
9178 case TOG_VIEW_TREE: {
9179 struct tog_tree_view_state *s = &view->state.tree;
9180 scrolld = &tree_scroll_down;
9182 selected = &s->selected;
9192 if (selected && *selected > view->nlines - header) {
9193 offset = abs(view->nlines - *selected - header);
9194 view->offset = offset;
9195 if (scrolld && offset) {
9196 err = scrolld(view, offset);
9197 *selected -= offset;
9205 list_commands(FILE *fp)
9209 fprintf(fp, "commands:");
9210 for (i = 0; i < nitems(tog_commands); i++) {
9211 const struct tog_cmd *cmd = &tog_commands[i];
9212 fprintf(fp, " %s", cmd->name);
9218 usage(int hflag, int status)
9220 FILE *fp = (status == 0) ? stdout : stderr;
9222 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9225 fprintf(fp, "lazy usage: %s path\n", getprogname());
9232 make_argv(int argc, ...)
9240 argv = calloc(argc, sizeof(char *));
9243 for (i = 0; i < argc; i++) {
9244 argv[i] = strdup(va_arg(ap, char *));
9245 if (argv[i] == NULL)
9254 * Try to convert 'tog path' into a 'tog log path' command.
9255 * The user could simply have mistyped the command rather than knowingly
9256 * provided a path. So check whether argv[0] can in fact be resolved
9257 * to a path in the HEAD commit and print a special error if not.
9258 * This hack is for mpi@ <3
9260 static const struct got_error *
9261 tog_log_with_path(int argc, char *argv[])
9263 const struct got_error *error = NULL, *close_err;
9264 const struct tog_cmd *cmd = NULL;
9265 struct got_repository *repo = NULL;
9266 struct got_worktree *worktree = NULL;
9267 struct got_object_id *commit_id = NULL, *id = NULL;
9268 struct got_commit_object *commit = NULL;
9269 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9270 char *commit_id_str = NULL, **cmd_argv = NULL;
9271 int *pack_fds = NULL;
9273 cwd = getcwd(NULL, 0);
9275 return got_error_from_errno("getcwd");
9277 error = got_repo_pack_fds_open(&pack_fds);
9281 error = got_worktree_open(&worktree, cwd);
9282 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9286 repo_path = strdup(got_worktree_get_repo_path(worktree));
9288 repo_path = strdup(cwd);
9289 if (repo_path == NULL) {
9290 error = got_error_from_errno("strdup");
9294 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9298 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9303 error = tog_load_refs(repo, 0);
9306 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9307 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9308 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9313 got_worktree_close(worktree);
9317 error = got_object_open_as_commit(&commit, repo, commit_id);
9321 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9323 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9325 fprintf(stderr, "%s: '%s' is no known command or path\n",
9326 getprogname(), argv[0]);
9331 error = got_object_id_str(&commit_id_str, commit_id);
9335 cmd = &tog_commands[0]; /* log */
9337 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9338 error = cmd->cmd_main(argc, cmd_argv);
9341 close_err = got_repo_close(repo);
9346 got_object_commit_close(commit);
9348 got_worktree_close(worktree);
9350 const struct got_error *pack_err =
9351 got_repo_pack_fds_close(pack_fds);
9356 free(commit_id_str);
9363 for (i = 0; i < argc; i++)
9372 main(int argc, char *argv[])
9374 const struct got_error *error = NULL;
9375 const struct tog_cmd *cmd = NULL;
9376 int ch, hflag = 0, Vflag = 0;
9377 char **cmd_argv = NULL;
9378 static const struct option longopts[] = {
9379 { "version", no_argument, NULL, 'V' },
9382 char *diff_algo_str = NULL;
9384 if (!isatty(STDIN_FILENO))
9385 errx(1, "standard input is not a tty");
9387 setlocale(LC_CTYPE, "");
9389 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9409 got_version_print_str();
9414 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9422 /* Build an argument vector which runs a default command. */
9423 cmd = &tog_commands[0];
9425 cmd_argv = make_argv(argc, cmd->name);
9429 /* Did the user specify a command? */
9430 for (i = 0; i < nitems(tog_commands); i++) {
9431 if (strncmp(tog_commands[i].name, argv[0],
9432 strlen(argv[0])) == 0) {
9433 cmd = &tog_commands[i];
9439 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9440 if (diff_algo_str) {
9441 if (strcasecmp(diff_algo_str, "patience") == 0)
9442 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9443 if (strcasecmp(diff_algo_str, "myers") == 0)
9444 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9450 /* No command specified; try log with a path */
9451 error = tog_log_with_path(argc, argv);
9456 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9463 for (i = 0; i < argc; i++)
9468 if (error && error->code != GOT_ERR_CANCELLED &&
9469 error->code != GOT_ERR_EOF &&
9470 error->code != GOT_ERR_PRIVSEP_EXIT &&
9471 error->code != GOT_ERR_PRIVSEP_PIPE &&
9472 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9473 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);