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
701 static const struct got_error *open_diff_view(struct tog_view *,
702 struct got_object_id *, struct got_object_id *,
703 const char *, const char *, int, int, int, struct tog_view *,
704 struct got_repository *);
705 static const struct got_error *show_diff_view(struct tog_view *);
706 static const struct got_error *input_diff_view(struct tog_view **,
707 struct tog_view *, int);
708 static const struct got_error *reset_diff_view(struct tog_view *);
709 static const struct got_error* close_diff_view(struct tog_view *);
710 static const struct got_error *search_start_diff_view(struct tog_view *);
711 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
712 size_t *, int **, int **, int **, int **);
713 static const struct got_error *search_next_view_match(struct tog_view *);
715 static const struct got_error *open_log_view(struct tog_view *,
716 struct got_object_id *, struct got_repository *,
717 const char *, const char *, int);
718 static const struct got_error * show_log_view(struct tog_view *);
719 static const struct got_error *input_log_view(struct tog_view **,
720 struct tog_view *, int);
721 static const struct got_error *resize_log_view(struct tog_view *, int);
722 static const struct got_error *close_log_view(struct tog_view *);
723 static const struct got_error *search_start_log_view(struct tog_view *);
724 static const struct got_error *search_next_log_view(struct tog_view *);
726 static const struct got_error *open_blame_view(struct tog_view *, char *,
727 struct got_object_id *, struct got_repository *);
728 static const struct got_error *show_blame_view(struct tog_view *);
729 static const struct got_error *input_blame_view(struct tog_view **,
730 struct tog_view *, int);
731 static const struct got_error *reset_blame_view(struct tog_view *);
732 static const struct got_error *close_blame_view(struct tog_view *);
733 static const struct got_error *search_start_blame_view(struct tog_view *);
734 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
735 size_t *, int **, int **, int **, int **);
737 static const struct got_error *open_tree_view(struct tog_view *,
738 struct got_object_id *, const char *, struct got_repository *);
739 static const struct got_error *show_tree_view(struct tog_view *);
740 static const struct got_error *input_tree_view(struct tog_view **,
741 struct tog_view *, int);
742 static const struct got_error *close_tree_view(struct tog_view *);
743 static const struct got_error *search_start_tree_view(struct tog_view *);
744 static const struct got_error *search_next_tree_view(struct tog_view *);
746 static const struct got_error *open_ref_view(struct tog_view *,
747 struct got_repository *);
748 static const struct got_error *show_ref_view(struct tog_view *);
749 static const struct got_error *input_ref_view(struct tog_view **,
750 struct tog_view *, int);
751 static const struct got_error *close_ref_view(struct tog_view *);
752 static const struct got_error *search_start_ref_view(struct tog_view *);
753 static const struct got_error *search_next_ref_view(struct tog_view *);
755 static const struct got_error *open_help_view(struct tog_view *,
757 static const struct got_error *show_help_view(struct tog_view *);
758 static const struct got_error *input_help_view(struct tog_view **,
759 struct tog_view *, int);
760 static const struct got_error *reset_help_view(struct tog_view *);
761 static const struct got_error* close_help_view(struct tog_view *);
762 static const struct got_error *search_start_help_view(struct tog_view *);
763 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
764 size_t *, int **, int **, int **, int **);
766 static volatile sig_atomic_t tog_sigwinch_received;
767 static volatile sig_atomic_t tog_sigpipe_received;
768 static volatile sig_atomic_t tog_sigcont_received;
769 static volatile sig_atomic_t tog_sigint_received;
770 static volatile sig_atomic_t tog_sigterm_received;
773 tog_sigwinch(int signo)
775 tog_sigwinch_received = 1;
779 tog_sigpipe(int signo)
781 tog_sigpipe_received = 1;
785 tog_sigcont(int signo)
787 tog_sigcont_received = 1;
791 tog_sigint(int signo)
793 tog_sigint_received = 1;
797 tog_sigterm(int signo)
799 tog_sigterm_received = 1;
803 tog_fatal_signal_received(void)
805 return (tog_sigpipe_received ||
806 tog_sigint_received || tog_sigterm_received);
809 static const struct got_error *
810 view_close(struct tog_view *view)
812 const struct got_error *err = NULL, *child_err = NULL;
815 child_err = view_close(view->child);
819 err = view->close(view);
821 del_panel(view->panel);
823 delwin(view->window);
825 return err ? err : child_err;
828 static struct tog_view *
829 view_open(int nlines, int ncols, int begin_y, int begin_x,
830 enum tog_view_type type)
832 struct tog_view *view = calloc(1, sizeof(*view));
840 view->nlines = nlines ? nlines : LINES - begin_y;
841 view->ncols = ncols ? ncols : COLS - begin_x;
842 view->begin_y = begin_y;
843 view->begin_x = begin_x;
844 view->window = newwin(nlines, ncols, begin_y, begin_x);
845 if (view->window == NULL) {
849 view->panel = new_panel(view->window);
850 if (view->panel == NULL ||
851 set_panel_userptr(view->panel, view) != OK) {
856 keypad(view->window, TRUE);
861 view_split_begin_x(int begin_x)
863 if (begin_x > 0 || COLS < 120)
865 return (COLS - MAX(COLS / 2, 80));
868 /* XXX Stub till we decide what to do. */
870 view_split_begin_y(int lines)
872 return lines * HSPLIT_SCALE;
875 static const struct got_error *view_resize(struct tog_view *);
877 static const struct got_error *
878 view_splitscreen(struct tog_view *view)
880 const struct got_error *err = NULL;
882 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
883 if (view->resized_y && view->resized_y < view->lines)
884 view->begin_y = view->resized_y;
886 view->begin_y = view_split_begin_y(view->nlines);
888 } else if (!view->resized) {
889 if (view->resized_x && view->resized_x < view->cols - 1 &&
891 view->begin_x = view->resized_x;
893 view->begin_x = view_split_begin_x(0);
896 view->nlines = LINES - view->begin_y;
897 view->ncols = COLS - view->begin_x;
900 err = view_resize(view);
904 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
905 view->parent->nlines = view->begin_y;
907 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
908 return got_error_from_errno("mvwin");
913 static const struct got_error *
914 view_fullscreen(struct tog_view *view)
916 const struct got_error *err = NULL;
919 view->begin_y = view->resized ? view->begin_y : 0;
920 view->nlines = view->resized ? view->nlines : LINES;
924 err = view_resize(view);
928 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
929 return got_error_from_errno("mvwin");
935 view_is_parent_view(struct tog_view *view)
937 return view->parent == NULL;
941 view_is_splitscreen(struct tog_view *view)
943 return view->begin_x > 0 || view->begin_y > 0;
947 view_is_fullscreen(struct tog_view *view)
949 return view->nlines == LINES && view->ncols == COLS;
953 view_is_hsplit_top(struct tog_view *view)
955 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
956 view_is_splitscreen(view->child);
960 view_border(struct tog_view *view)
963 const struct tog_view *view_above;
966 return view_border(view->parent);
968 panel = panel_above(view->panel);
972 view_above = panel_userptr(panel);
973 if (view->mode == TOG_VIEW_SPLIT_HRZN)
974 mvwhline(view->window, view_above->begin_y - 1,
975 view->begin_x, got_locale_is_utf8() ?
976 ACS_HLINE : '-', view->ncols);
978 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
979 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
982 static const struct got_error *view_init_hsplit(struct tog_view *, int);
983 static const struct got_error *request_log_commits(struct tog_view *);
984 static const struct got_error *offset_selection_down(struct tog_view *);
985 static void offset_selection_up(struct tog_view *);
986 static void view_get_split(struct tog_view *, int *, int *);
988 static const struct got_error *
989 view_resize(struct tog_view *view)
991 const struct got_error *err = NULL;
992 int dif, nlines, ncols;
994 dif = LINES - view->lines; /* line difference */
996 if (view->lines > LINES)
997 nlines = view->nlines - (view->lines - LINES);
999 nlines = view->nlines + (LINES - view->lines);
1000 if (view->cols > COLS)
1001 ncols = view->ncols - (view->cols - COLS);
1003 ncols = view->ncols + (COLS - view->cols);
1006 int hs = view->child->begin_y;
1008 if (!view_is_fullscreen(view))
1009 view->child->begin_x = view_split_begin_x(view->begin_x);
1010 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1011 view->child->begin_x == 0) {
1014 view_fullscreen(view->child);
1015 if (view->child->focussed)
1016 show_panel(view->child->panel);
1018 show_panel(view->panel);
1020 ncols = view->child->begin_x;
1022 view_splitscreen(view->child);
1023 show_panel(view->child->panel);
1026 * XXX This is ugly and needs to be moved into the above
1027 * logic but "works" for now and my attempts at moving it
1028 * break either 'tab' or 'F' key maps in horizontal splits.
1031 err = view_splitscreen(view->child);
1034 if (dif < 0) { /* top split decreased */
1035 err = offset_selection_down(view);
1042 show_panel(view->child->panel);
1043 nlines = view->nlines;
1045 } else if (view->parent == NULL)
1048 if (view->resize && dif > 0) {
1049 err = view->resize(view, dif);
1054 if (wresize(view->window, nlines, ncols) == ERR)
1055 return got_error_from_errno("wresize");
1056 if (replace_panel(view->panel, view->window) == ERR)
1057 return got_error_from_errno("replace_panel");
1058 wclear(view->window);
1060 view->nlines = nlines;
1061 view->ncols = ncols;
1062 view->lines = LINES;
1068 static const struct got_error *
1069 resize_log_view(struct tog_view *view, int increase)
1071 struct tog_log_view_state *s = &view->state.log;
1072 const struct got_error *err = NULL;
1075 if (s->selected_entry)
1076 n = s->selected_entry->idx + view->lines - s->selected;
1079 * Request commits to account for the increased
1080 * height so we have enough to populate the view.
1082 if (s->commits->ncommits < n) {
1083 view->nscrolled = n - s->commits->ncommits + increase + 1;
1084 err = request_log_commits(view);
1091 view_adjust_offset(struct tog_view *view, int n)
1096 if (view->parent && view->parent->offset) {
1097 if (view->parent->offset + n >= 0)
1098 view->parent->offset += n;
1100 view->parent->offset = 0;
1101 } else if (view->offset) {
1102 if (view->offset - n >= 0)
1109 static const struct got_error *
1110 view_resize_split(struct tog_view *view, int resize)
1112 const struct got_error *err = NULL;
1113 struct tog_view *v = NULL;
1120 if (!v->child || !view_is_splitscreen(v->child))
1123 v->resized = v->child->resized = resize; /* lock for resize event */
1125 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1126 if (v->child->resized_y)
1127 v->child->begin_y = v->child->resized_y;
1129 v->child->begin_y -= resize;
1131 v->child->begin_y += resize;
1132 if (v->child->begin_y < 3) {
1134 v->child->begin_y = 3;
1135 } else if (v->child->begin_y > LINES - 1) {
1137 v->child->begin_y = LINES - 1;
1140 v->child->ncols = COLS;
1141 view_adjust_offset(view, resize);
1142 err = view_init_hsplit(v, v->child->begin_y);
1145 v->child->resized_y = v->child->begin_y;
1147 if (v->child->resized_x)
1148 v->child->begin_x = v->child->resized_x;
1150 v->child->begin_x -= resize;
1152 v->child->begin_x += resize;
1153 if (v->child->begin_x < 11) {
1155 v->child->begin_x = 11;
1156 } else if (v->child->begin_x > COLS - 1) {
1158 v->child->begin_x = COLS - 1;
1160 v->child->resized_x = v->child->begin_x;
1163 v->child->mode = v->mode;
1164 v->child->nlines = v->lines - v->child->begin_y;
1165 v->child->ncols = v->cols - v->child->begin_x;
1168 err = view_fullscreen(v);
1171 err = view_splitscreen(v->child);
1175 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 err = offset_selection_down(v->child);
1182 err = v->resize(v, 0);
1183 else if (v->child->resize)
1184 err = v->child->resize(v->child, 0);
1186 v->resized = v->child->resized = 0;
1192 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1194 struct tog_view *v = src->child ? src->child : src;
1196 dst->resized_x = v->resized_x;
1197 dst->resized_y = v->resized_y;
1200 static const struct got_error *
1201 view_close_child(struct tog_view *view)
1203 const struct got_error *err = NULL;
1205 if (view->child == NULL)
1208 err = view_close(view->child);
1213 static const struct got_error *
1214 view_set_child(struct tog_view *view, struct tog_view *child)
1216 const struct got_error *err = NULL;
1218 view->child = child;
1219 child->parent = view;
1221 err = view_resize(view);
1225 if (view->child->resized_x || view->child->resized_y)
1226 err = view_resize_split(view, 0);
1231 static const struct got_error *view_dispatch_request(struct tog_view **,
1232 struct tog_view *, enum tog_view_type, int, int);
1234 static const struct got_error *
1235 view_request_new(struct tog_view **requested, struct tog_view *view,
1236 enum tog_view_type request)
1238 struct tog_view *new_view = NULL;
1239 const struct got_error *err;
1244 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1245 view_get_split(view, &y, &x);
1247 err = view_dispatch_request(&new_view, view, request, y, x);
1251 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1252 request != TOG_VIEW_HELP) {
1253 err = view_init_hsplit(view, y);
1259 new_view->focussed = 1;
1260 new_view->mode = view->mode;
1261 new_view->nlines = request == TOG_VIEW_HELP ?
1262 view->lines : view->lines - y;
1264 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1265 view_transfer_size(new_view, view);
1266 err = view_close_child(view);
1269 err = view_set_child(view, new_view);
1272 view->focus_child = 1;
1274 *requested = new_view;
1280 tog_resizeterm(void)
1283 struct winsize size;
1285 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1286 cols = 80; /* Default */
1290 lines = size.ws_row;
1292 resize_term(lines, cols);
1295 static const struct got_error *
1296 view_search_start(struct tog_view *view, int fast_refresh)
1298 const struct got_error *err = NULL;
1299 struct tog_view *v = view;
1303 if (view->search_started) {
1304 regfree(&view->regex);
1305 view->searching = 0;
1306 memset(&view->regmatch, 0, sizeof(view->regmatch));
1308 view->search_started = 0;
1310 if (view->nlines < 1)
1313 if (view_is_hsplit_top(view))
1315 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1318 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1319 wclrtoeol(v->window);
1321 nodelay(v->window, FALSE); /* block for search term input */
1324 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1325 wrefresh(v->window);
1328 nodelay(v->window, TRUE);
1334 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1335 err = view->search_start(view);
1337 regfree(&view->regex);
1340 view->search_started = 1;
1341 view->searching = TOG_SEARCH_FORWARD;
1342 view->search_next_done = 0;
1343 view->search_next(view);
1349 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1350 static const struct got_error *
1351 switch_split(struct tog_view *view)
1353 const struct got_error *err = NULL;
1354 struct tog_view *v = NULL;
1361 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1362 v->mode = TOG_VIEW_SPLIT_VERT;
1364 v->mode = TOG_VIEW_SPLIT_HRZN;
1368 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1369 v->mode = TOG_VIEW_SPLIT_NONE;
1371 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1372 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1373 v->child->begin_y = v->child->resized_y;
1374 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1375 v->child->begin_x = v->child->resized_x;
1378 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1380 v->child->ncols = COLS;
1381 v->child->nscrolled = LINES - v->child->nlines;
1383 err = view_init_hsplit(v, v->child->begin_y);
1387 v->child->mode = v->mode;
1388 v->child->nlines = v->lines - v->child->begin_y;
1391 err = view_fullscreen(v);
1394 err = view_splitscreen(v->child);
1398 if (v->mode == TOG_VIEW_SPLIT_NONE)
1399 v->mode = TOG_VIEW_SPLIT_VERT;
1400 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1401 err = offset_selection_down(v);
1404 err = offset_selection_down(v->child);
1408 offset_selection_up(v);
1409 offset_selection_up(v->child);
1412 err = v->resize(v, 0);
1413 else if (v->child->resize)
1414 err = v->child->resize(v->child, 0);
1420 * Compute view->count from numeric input. Assign total to view->count and
1421 * return first non-numeric key entered.
1424 get_compound_key(struct tog_view *view, int c)
1426 struct tog_view *v = view;
1429 if (view_is_hsplit_top(view))
1431 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1435 cbreak(); /* block for input */
1436 nodelay(view->window, FALSE);
1437 wmove(v->window, v->nlines - 1, 0);
1438 wclrtoeol(v->window);
1439 waddch(v->window, ':');
1442 x = getcurx(v->window);
1443 if (x != ERR && x < view->ncols) {
1444 waddch(v->window, c);
1445 wrefresh(v->window);
1449 * Don't overflow. Max valid request should be the greatest
1450 * between the longest and total lines; cap at 10 million.
1455 n = n * 10 + (c - '0');
1456 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1458 if (c == 'G' || c == 'g') { /* nG key map */
1459 view->gline = view->hiline = n;
1464 /* Massage excessive or inapplicable values at the input handler. */
1471 action_report(struct tog_view *view)
1473 struct tog_view *v = view;
1475 if (view_is_hsplit_top(view))
1477 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1480 wmove(v->window, v->nlines - 1, 0);
1481 wclrtoeol(v->window);
1482 wprintw(v->window, ":%s", view->action);
1483 wrefresh(v->window);
1486 * Clear action status report. Only clear in blame view
1487 * once annotating is complete, otherwise it's too fast.
1489 if (view->type == TOG_VIEW_BLAME) {
1490 if (view->state.blame.blame_complete)
1491 view->action = NULL;
1493 view->action = NULL;
1496 static const struct got_error *
1497 view_input(struct tog_view **new, int *done, struct tog_view *view,
1498 struct tog_view_list_head *views, int fast_refresh)
1500 const struct got_error *err = NULL;
1507 action_report(view);
1509 /* Clear "no matches" indicator. */
1510 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1511 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1512 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1516 if (view->searching && !view->search_next_done) {
1517 errcode = pthread_mutex_unlock(&tog_mutex);
1519 return got_error_set_errno(errcode,
1520 "pthread_mutex_unlock");
1522 errcode = pthread_mutex_lock(&tog_mutex);
1524 return got_error_set_errno(errcode,
1525 "pthread_mutex_lock");
1526 view->search_next(view);
1530 /* Allow threads to make progress while we are waiting for input. */
1531 errcode = pthread_mutex_unlock(&tog_mutex);
1533 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1534 /* If we have an unfinished count, let C-g or backspace abort. */
1535 if (view->count && --view->count) {
1537 nodelay(view->window, TRUE);
1538 ch = wgetch(view->window);
1539 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1544 ch = wgetch(view->window);
1545 if (ch >= '1' && ch <= '9')
1546 view->ch = ch = get_compound_key(view, ch);
1548 if (view->hiline && ch != ERR && ch != 0)
1549 view->hiline = 0; /* key pressed, clear line highlight */
1550 nodelay(view->window, TRUE);
1551 errcode = pthread_mutex_lock(&tog_mutex);
1553 return got_error_set_errno(errcode, "pthread_mutex_lock");
1555 if (tog_sigwinch_received || tog_sigcont_received) {
1557 tog_sigwinch_received = 0;
1558 tog_sigcont_received = 0;
1559 TAILQ_FOREACH(v, views, entry) {
1560 err = view_resize(v);
1563 err = v->input(new, v, KEY_RESIZE);
1567 err = view_resize(v->child);
1570 err = v->child->input(new, v->child,
1574 if (v->child->resized_x || v->child->resized_y) {
1575 err = view_resize_split(v, 0);
1587 if (view->type == TOG_VIEW_HELP)
1588 err = view->reset(view);
1590 err = view_request_new(new, view, TOG_VIEW_HELP);
1596 view->child->focussed = 1;
1597 view->focus_child = 1;
1598 } else if (view->parent) {
1600 view->parent->focussed = 1;
1601 view->parent->focus_child = 0;
1602 if (!view_is_splitscreen(view)) {
1603 if (view->parent->resize) {
1604 err = view->parent->resize(view->parent,
1609 offset_selection_up(view->parent);
1610 err = view_fullscreen(view->parent);
1617 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1618 if (view->parent->resize) {
1619 /* might need more commits to fill fullscreen */
1620 err = view->parent->resize(view->parent, 0);
1624 offset_selection_up(view->parent);
1626 err = view->input(new, view, ch);
1634 if (view_is_parent_view(view)) {
1635 if (view->child == NULL)
1637 if (view_is_splitscreen(view->child)) {
1639 view->child->focussed = 1;
1640 err = view_fullscreen(view->child);
1642 err = view_splitscreen(view->child);
1644 err = view_resize_split(view, 0);
1648 err = view->child->input(new, view->child,
1651 if (view_is_splitscreen(view)) {
1652 view->parent->focussed = 0;
1654 err = view_fullscreen(view);
1656 err = view_splitscreen(view);
1657 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1658 err = view_resize(view->parent);
1660 err = view_resize_split(view, 0);
1664 err = view->input(new, view, KEY_RESIZE);
1669 err = view->resize(view, 0);
1674 err = offset_selection_down(view->parent);
1676 err = offset_selection_down(view);
1680 err = switch_split(view);
1683 err = view_resize_split(view, -1);
1686 err = view_resize_split(view, 1);
1692 if (view->search_start)
1693 view_search_start(view, fast_refresh);
1695 err = view->input(new, view, ch);
1699 if (view->search_started && view->search_next) {
1700 view->searching = (ch == 'n' ?
1701 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1702 view->search_next_done = 0;
1703 view->search_next(view);
1705 err = view->input(new, view, ch);
1708 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1709 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1710 view->action = "Patience diff algorithm";
1712 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1713 view->action = "Myers diff algorithm";
1715 TAILQ_FOREACH(v, views, entry) {
1721 if (v->child && v->child->reset) {
1722 err = v->child->reset(v->child);
1729 err = view->input(new, view, ch);
1737 view_needs_focus_indication(struct tog_view *view)
1739 if (view_is_parent_view(view)) {
1740 if (view->child == NULL || view->child->focussed)
1742 if (!view_is_splitscreen(view->child))
1744 } else if (!view_is_splitscreen(view))
1747 return view->focussed;
1750 static const struct got_error *
1751 view_loop(struct tog_view *view)
1753 const struct got_error *err = NULL;
1754 struct tog_view_list_head views;
1755 struct tog_view *new_view;
1757 int fast_refresh = 10;
1758 int done = 0, errcode;
1760 mode = getenv("TOG_VIEW_SPLIT_MODE");
1761 if (!mode || !(*mode == 'h' || *mode == 'H'))
1762 view->mode = TOG_VIEW_SPLIT_VERT;
1764 view->mode = TOG_VIEW_SPLIT_HRZN;
1766 errcode = pthread_mutex_lock(&tog_mutex);
1768 return got_error_set_errno(errcode, "pthread_mutex_lock");
1771 TAILQ_INSERT_HEAD(&views, view, entry);
1774 err = view->show(view);
1779 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1780 !tog_fatal_signal_received()) {
1781 /* Refresh fast during initialization, then become slower. */
1782 if (fast_refresh && --fast_refresh == 0)
1783 halfdelay(10); /* switch to once per second */
1785 err = view_input(&new_view, &done, view, &views, fast_refresh);
1789 if (view->dying && view == TAILQ_FIRST(&views) &&
1790 TAILQ_NEXT(view, entry) == NULL)
1796 * When we quit, scroll the screen up a single line
1797 * so we don't lose any information.
1799 TAILQ_FOREACH(v, &views, entry) {
1800 wmove(v->window, 0, 0);
1801 wdeleteln(v->window);
1802 wnoutrefresh(v->window);
1803 if (v->child && !view_is_fullscreen(v)) {
1804 wmove(v->child->window, 0, 0);
1805 wdeleteln(v->child->window);
1806 wnoutrefresh(v->child->window);
1813 struct tog_view *v, *prev = NULL;
1815 if (view_is_parent_view(view))
1816 prev = TAILQ_PREV(view, tog_view_list_head,
1818 else if (view->parent)
1819 prev = view->parent;
1822 view->parent->child = NULL;
1823 view->parent->focus_child = 0;
1824 /* Restore fullscreen line height. */
1825 view->parent->nlines = view->parent->lines;
1826 err = view_resize(view->parent);
1829 /* Make resized splits persist. */
1830 view_transfer_size(view->parent, view);
1832 TAILQ_REMOVE(&views, view, entry);
1834 err = view_close(view);
1839 TAILQ_FOREACH(v, &views, entry) {
1843 if (view == NULL && new_view == NULL) {
1844 /* No view has focus. Try to pick one. */
1847 else if (!TAILQ_EMPTY(&views)) {
1848 view = TAILQ_LAST(&views,
1849 tog_view_list_head);
1852 if (view->focus_child) {
1853 view->child->focussed = 1;
1861 struct tog_view *v, *t;
1862 /* Only allow one parent view per type. */
1863 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1864 if (v->type != new_view->type)
1866 TAILQ_REMOVE(&views, v, entry);
1867 err = view_close(v);
1872 TAILQ_INSERT_TAIL(&views, new_view, entry);
1875 if (view && !done) {
1876 if (view_is_parent_view(view)) {
1877 if (view->child && view->child->focussed)
1880 if (view->parent && view->parent->focussed)
1881 view = view->parent;
1883 show_panel(view->panel);
1884 if (view->child && view_is_splitscreen(view->child))
1885 show_panel(view->child->panel);
1886 if (view->parent && view_is_splitscreen(view)) {
1887 err = view->parent->show(view->parent);
1891 err = view->show(view);
1895 err = view->child->show(view->child);
1904 while (!TAILQ_EMPTY(&views)) {
1905 const struct got_error *close_err;
1906 view = TAILQ_FIRST(&views);
1907 TAILQ_REMOVE(&views, view, entry);
1908 close_err = view_close(view);
1909 if (close_err && err == NULL)
1913 errcode = pthread_mutex_unlock(&tog_mutex);
1914 if (errcode && err == NULL)
1915 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1925 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1930 /* Create newly allocated wide-character string equivalent to a byte string. */
1931 static const struct got_error *
1932 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1935 const struct got_error *err = NULL;
1938 *wlen = mbstowcs(NULL, s, 0);
1939 if (*wlen == (size_t)-1) {
1941 if (errno != EILSEQ)
1942 return got_error_from_errno("mbstowcs");
1944 /* byte string invalid in current encoding; try to "fix" it */
1945 err = got_mbsavis(&vis, &vislen, s);
1948 *wlen = mbstowcs(NULL, vis, 0);
1949 if (*wlen == (size_t)-1) {
1950 err = got_error_from_errno("mbstowcs"); /* give up */
1955 *ws = calloc(*wlen + 1, sizeof(**ws));
1957 err = got_error_from_errno("calloc");
1961 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1962 err = got_error_from_errno("mbstowcs");
1973 static const struct got_error *
1974 expand_tab(char **ptr, const char *src)
1977 size_t len, n, idx = 0, sz = 0;
1980 n = len = strlen(src);
1981 dst = malloc(n + 1);
1983 return got_error_from_errno("malloc");
1985 while (idx < len && src[idx]) {
1986 const char c = src[idx];
1989 size_t nb = TABSIZE - sz % TABSIZE;
1992 p = realloc(dst, n + nb);
1995 return got_error_from_errno("realloc");
2000 memset(dst + sz, ' ', nb);
2003 dst[sz++] = src[idx];
2013 * Advance at most n columns from wline starting at offset off.
2014 * Return the index to the first character after the span operation.
2015 * Return the combined column width of all spanned wide character in
2019 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2021 int width, i, cols = 0;
2028 for (i = off; wline[i] != L'\0'; ++i) {
2029 if (wline[i] == L'\t')
2030 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2032 width = wcwidth(wline[i]);
2039 if (cols + width > n)
2049 * Format a line for display, ensuring that it won't overflow a width limit.
2050 * With scrolling, the width returned refers to the scrolled version of the
2051 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2053 static const struct got_error *
2054 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2055 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2057 const struct got_error *err = NULL;
2059 wchar_t *wline = NULL;
2068 err = expand_tab(&exstr, line);
2073 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2078 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2080 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2081 wline[wlen - 1] = L'\0';
2084 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2085 wline[wlen - 1] = L'\0';
2089 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2095 *scrollxp = scrollx;
2103 static const struct got_error*
2104 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2105 struct got_object_id *id, struct got_repository *repo)
2107 static const struct got_error *err = NULL;
2108 struct got_reflist_entry *re;
2114 TAILQ_FOREACH(re, refs, entry) {
2115 struct got_tag_object *tag = NULL;
2116 struct got_object_id *ref_id;
2119 name = got_ref_get_name(re->ref);
2120 if (strcmp(name, GOT_REF_HEAD) == 0)
2122 if (strncmp(name, "refs/", 5) == 0)
2124 if (strncmp(name, "got/", 4) == 0 &&
2125 strncmp(name, "got/backup/", 11) != 0)
2127 if (strncmp(name, "heads/", 6) == 0)
2129 if (strncmp(name, "remotes/", 8) == 0) {
2131 s = strstr(name, "/" GOT_REF_HEAD);
2132 if (s != NULL && s[strlen(s)] == '\0')
2135 err = got_ref_resolve(&ref_id, repo, re->ref);
2138 if (strncmp(name, "tags/", 5) == 0) {
2139 err = got_object_open_as_tag(&tag, repo, ref_id);
2141 if (err->code != GOT_ERR_OBJ_TYPE) {
2145 /* Ref points at something other than a tag. */
2150 cmp = got_object_id_cmp(tag ?
2151 got_object_tag_get_object_id(tag) : ref_id, id);
2154 got_object_tag_close(tag);
2158 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2159 s ? ", " : "", name) == -1) {
2160 err = got_error_from_errno("asprintf");
2171 static const struct got_error *
2172 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2177 smallerthan = strchr(author, '<');
2178 if (smallerthan && smallerthan[1] != '\0')
2179 author = smallerthan + 1;
2180 author[strcspn(author, "@>")] = '\0';
2181 return format_line(wauthor, author_width, NULL, author, 0, limit,
2185 static const struct got_error *
2186 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2187 struct got_object_id *id, const size_t date_display_cols,
2188 int author_display_cols)
2190 struct tog_log_view_state *s = &view->state.log;
2191 const struct got_error *err = NULL;
2192 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2193 char *logmsg0 = NULL, *logmsg = NULL;
2194 char *author = NULL;
2195 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2196 int author_width, logmsg_width;
2197 char *newline, *line = NULL;
2198 int col, limit, scrollx;
2199 const int avail = view->ncols;
2201 time_t committer_time;
2202 struct tog_color *tc;
2204 committer_time = got_object_commit_get_committer_time(commit);
2205 if (gmtime_r(&committer_time, &tm) == NULL)
2206 return got_error_from_errno("gmtime_r");
2207 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2208 return got_error(GOT_ERR_NO_SPACE);
2210 if (avail <= date_display_cols)
2211 limit = MIN(sizeof(datebuf) - 1, avail);
2213 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2214 tc = get_color(&s->colors, TOG_COLOR_DATE);
2216 wattr_on(view->window,
2217 COLOR_PAIR(tc->colorpair), NULL);
2218 waddnstr(view->window, datebuf, limit);
2220 wattr_off(view->window,
2221 COLOR_PAIR(tc->colorpair), NULL);
2228 err = got_object_id_str(&id_str, id);
2231 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2233 wattr_on(view->window,
2234 COLOR_PAIR(tc->colorpair), NULL);
2235 wprintw(view->window, "%.8s ", id_str);
2237 wattr_off(view->window,
2238 COLOR_PAIR(tc->colorpair), NULL);
2245 if (s->use_committer)
2246 author = strdup(got_object_commit_get_committer(commit));
2248 author = strdup(got_object_commit_get_author(commit));
2249 if (author == NULL) {
2250 err = got_error_from_errno("strdup");
2253 err = format_author(&wauthor, &author_width, author, avail - col, col);
2256 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2258 wattr_on(view->window,
2259 COLOR_PAIR(tc->colorpair), NULL);
2260 waddwstr(view->window, wauthor);
2261 col += author_width;
2262 while (col < avail && author_width < author_display_cols + 2) {
2263 waddch(view->window, ' ');
2268 wattr_off(view->window,
2269 COLOR_PAIR(tc->colorpair), NULL);
2273 err = got_object_commit_get_logmsg(&logmsg0, commit);
2277 while (*logmsg == '\n')
2279 newline = strchr(logmsg, '\n');
2282 limit = avail - col;
2283 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2284 limit--; /* for the border */
2285 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2289 waddwstr(view->window, &wlogmsg[scrollx]);
2290 col += MAX(logmsg_width, 0);
2291 while (col < avail) {
2292 waddch(view->window, ' ');
2304 static struct commit_queue_entry *
2305 alloc_commit_queue_entry(struct got_commit_object *commit,
2306 struct got_object_id *id)
2308 struct commit_queue_entry *entry;
2309 struct got_object_id *dup;
2311 entry = calloc(1, sizeof(*entry));
2315 dup = got_object_id_dup(id);
2322 entry->commit = commit;
2327 pop_commit(struct commit_queue *commits)
2329 struct commit_queue_entry *entry;
2331 entry = TAILQ_FIRST(&commits->head);
2332 TAILQ_REMOVE(&commits->head, entry, entry);
2333 got_object_commit_close(entry->commit);
2334 commits->ncommits--;
2340 free_commits(struct commit_queue *commits)
2342 while (!TAILQ_EMPTY(&commits->head))
2343 pop_commit(commits);
2346 static const struct got_error *
2347 match_commit(int *have_match, struct got_object_id *id,
2348 struct got_commit_object *commit, regex_t *regex)
2350 const struct got_error *err = NULL;
2351 regmatch_t regmatch;
2352 char *id_str = NULL, *logmsg = NULL;
2356 err = got_object_id_str(&id_str, id);
2360 err = got_object_commit_get_logmsg(&logmsg, commit);
2364 if (regexec(regex, got_object_commit_get_author(commit), 1,
2365 ®match, 0) == 0 ||
2366 regexec(regex, got_object_commit_get_committer(commit), 1,
2367 ®match, 0) == 0 ||
2368 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2369 regexec(regex, logmsg, 1, ®match, 0) == 0)
2377 static const struct got_error *
2378 queue_commits(struct tog_log_thread_args *a)
2380 const struct got_error *err = NULL;
2383 * We keep all commits open throughout the lifetime of the log
2384 * view in order to avoid having to re-fetch commits from disk
2385 * while updating the display.
2388 struct got_object_id id;
2389 struct got_commit_object *commit;
2390 struct commit_queue_entry *entry;
2391 int limit_match = 0;
2394 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2399 err = got_object_open_as_commit(&commit, a->repo, &id);
2402 entry = alloc_commit_queue_entry(commit, &id);
2403 if (entry == NULL) {
2404 err = got_error_from_errno("alloc_commit_queue_entry");
2408 errcode = pthread_mutex_lock(&tog_mutex);
2410 err = got_error_set_errno(errcode,
2411 "pthread_mutex_lock");
2415 entry->idx = a->real_commits->ncommits;
2416 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2417 a->real_commits->ncommits++;
2420 err = match_commit(&limit_match, &id, commit,
2426 struct commit_queue_entry *matched;
2428 matched = alloc_commit_queue_entry(
2429 entry->commit, entry->id);
2430 if (matched == NULL) {
2431 err = got_error_from_errno(
2432 "alloc_commit_queue_entry");
2435 matched->commit = entry->commit;
2436 got_object_commit_retain(entry->commit);
2438 matched->idx = a->limit_commits->ncommits;
2439 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2441 a->limit_commits->ncommits++;
2445 * This is how we signal log_thread() that we
2446 * have found a match, and that it should be
2447 * counted as a new entry for the view.
2449 a->limit_match = limit_match;
2452 if (*a->searching == TOG_SEARCH_FORWARD &&
2453 !*a->search_next_done) {
2455 err = match_commit(&have_match, &id, commit, a->regex);
2460 if (limit_match && have_match)
2461 *a->search_next_done =
2462 TOG_SEARCH_HAVE_MORE;
2463 } else if (have_match)
2464 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2467 errcode = pthread_mutex_unlock(&tog_mutex);
2468 if (errcode && err == NULL)
2469 err = got_error_set_errno(errcode,
2470 "pthread_mutex_unlock");
2473 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2479 select_commit(struct tog_log_view_state *s)
2481 struct commit_queue_entry *entry;
2484 entry = s->first_displayed_entry;
2486 if (ncommits == s->selected) {
2487 s->selected_entry = entry;
2490 entry = TAILQ_NEXT(entry, entry);
2495 static const struct got_error *
2496 draw_commits(struct tog_view *view)
2498 const struct got_error *err = NULL;
2499 struct tog_log_view_state *s = &view->state.log;
2500 struct commit_queue_entry *entry = s->selected_entry;
2501 int limit = view->nlines;
2503 int ncommits, author_cols = 4;
2504 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2505 char *refs_str = NULL;
2507 struct tog_color *tc;
2508 static const size_t date_display_cols = 12;
2510 if (view_is_hsplit_top(view))
2511 --limit; /* account for border */
2513 if (s->selected_entry &&
2514 !(view->searching && view->search_next_done == 0)) {
2515 struct got_reflist_head *refs;
2516 err = got_object_id_str(&id_str, s->selected_entry->id);
2519 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2520 s->selected_entry->id);
2522 err = build_refs_str(&refs_str, refs,
2523 s->selected_entry->id, s->repo);
2529 if (s->thread_args.commits_needed == 0)
2530 halfdelay(10); /* disable fast refresh */
2532 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2533 if (asprintf(&ncommits_str, " [%d/%d] %s",
2534 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2535 (view->searching && !view->search_next_done) ?
2536 "searching..." : "loading...") == -1) {
2537 err = got_error_from_errno("asprintf");
2541 const char *search_str = NULL;
2542 const char *limit_str = NULL;
2544 if (view->searching) {
2545 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2546 search_str = "no more matches";
2547 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2548 search_str = "no matches found";
2549 else if (!view->search_next_done)
2550 search_str = "searching...";
2553 if (s->limit_view && s->commits->ncommits == 0)
2554 limit_str = "no matches found";
2556 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2557 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2558 search_str ? search_str : (refs_str ? refs_str : ""),
2559 limit_str ? limit_str : "") == -1) {
2560 err = got_error_from_errno("asprintf");
2565 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2566 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2567 "........................................",
2568 s->in_repo_path, ncommits_str) == -1) {
2569 err = got_error_from_errno("asprintf");
2573 } else if (asprintf(&header, "commit %s%s",
2574 id_str ? id_str : "........................................",
2575 ncommits_str) == -1) {
2576 err = got_error_from_errno("asprintf");
2580 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2584 werase(view->window);
2586 if (view_needs_focus_indication(view))
2587 wstandout(view->window);
2588 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2590 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2591 waddwstr(view->window, wline);
2592 while (width < view->ncols) {
2593 waddch(view->window, ' ');
2597 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2598 if (view_needs_focus_indication(view))
2599 wstandend(view->window);
2604 /* Grow author column size if necessary, and set view->maxx. */
2605 entry = s->first_displayed_entry;
2609 struct got_commit_object *c = entry->commit;
2610 char *author, *eol, *msg, *msg0;
2611 wchar_t *wauthor, *wmsg;
2613 if (ncommits >= limit - 1)
2615 if (s->use_committer)
2616 author = strdup(got_object_commit_get_committer(c));
2618 author = strdup(got_object_commit_get_author(c));
2619 if (author == NULL) {
2620 err = got_error_from_errno("strdup");
2623 err = format_author(&wauthor, &width, author, COLS,
2625 if (author_cols < width)
2626 author_cols = width;
2631 err = got_object_commit_get_logmsg(&msg0, c);
2635 while (*msg == '\n')
2637 if ((eol = strchr(msg, '\n')))
2639 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2640 date_display_cols + author_cols, 0);
2643 view->maxx = MAX(view->maxx, width);
2647 entry = TAILQ_NEXT(entry, entry);
2650 entry = s->first_displayed_entry;
2651 s->last_displayed_entry = s->first_displayed_entry;
2654 if (ncommits >= limit - 1)
2656 if (ncommits == s->selected)
2657 wstandout(view->window);
2658 err = draw_commit(view, entry->commit, entry->id,
2659 date_display_cols, author_cols);
2660 if (ncommits == s->selected)
2661 wstandend(view->window);
2665 s->last_displayed_entry = entry;
2666 entry = TAILQ_NEXT(entry, entry);
2679 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2681 struct commit_queue_entry *entry;
2684 entry = TAILQ_FIRST(&s->commits->head);
2685 if (s->first_displayed_entry == entry)
2688 entry = s->first_displayed_entry;
2689 while (entry && nscrolled < maxscroll) {
2690 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2692 s->first_displayed_entry = entry;
2698 static const struct got_error *
2699 trigger_log_thread(struct tog_view *view, int wait)
2701 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2704 halfdelay(1); /* fast refresh while loading commits */
2706 while (!ta->log_complete && !tog_thread_error &&
2707 (ta->commits_needed > 0 || ta->load_all)) {
2708 /* Wake the log thread. */
2709 errcode = pthread_cond_signal(&ta->need_commits);
2711 return got_error_set_errno(errcode,
2712 "pthread_cond_signal");
2715 * The mutex will be released while the view loop waits
2716 * in wgetch(), at which time the log thread will run.
2721 /* Display progress update in log view. */
2722 show_log_view(view);
2726 /* Wait right here while next commit is being loaded. */
2727 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2729 return got_error_set_errno(errcode,
2730 "pthread_cond_wait");
2732 /* Display progress update in log view. */
2733 show_log_view(view);
2741 static const struct got_error *
2742 request_log_commits(struct tog_view *view)
2744 struct tog_log_view_state *state = &view->state.log;
2745 const struct got_error *err = NULL;
2747 if (state->thread_args.log_complete)
2750 state->thread_args.commits_needed += view->nscrolled;
2751 err = trigger_log_thread(view, 1);
2752 view->nscrolled = 0;
2757 static const struct got_error *
2758 log_scroll_down(struct tog_view *view, int maxscroll)
2760 struct tog_log_view_state *s = &view->state.log;
2761 const struct got_error *err = NULL;
2762 struct commit_queue_entry *pentry;
2763 int nscrolled = 0, ncommits_needed;
2765 if (s->last_displayed_entry == NULL)
2768 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2769 if (s->commits->ncommits < ncommits_needed &&
2770 !s->thread_args.log_complete) {
2772 * Ask the log thread for required amount of commits.
2774 s->thread_args.commits_needed +=
2775 ncommits_needed - s->commits->ncommits;
2776 err = trigger_log_thread(view, 1);
2782 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2783 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2786 s->last_displayed_entry = pentry ?
2787 pentry : s->last_displayed_entry;
2789 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2792 s->first_displayed_entry = pentry;
2793 } while (++nscrolled < maxscroll);
2795 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2796 view->nscrolled += nscrolled;
2798 view->nscrolled = 0;
2803 static const struct got_error *
2804 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2805 struct got_commit_object *commit, struct got_object_id *commit_id,
2806 struct tog_view *log_view, struct got_repository *repo)
2808 const struct got_error *err;
2809 struct got_object_qid *parent_id;
2810 struct tog_view *diff_view;
2812 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2813 if (diff_view == NULL)
2814 return got_error_from_errno("view_open");
2816 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2817 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2818 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2820 *new_view = diff_view;
2824 static const struct got_error *
2825 tree_view_visit_subtree(struct tog_tree_view_state *s,
2826 struct got_tree_object *subtree)
2828 struct tog_parent_tree *parent;
2830 parent = calloc(1, sizeof(*parent));
2832 return got_error_from_errno("calloc");
2834 parent->tree = s->tree;
2835 parent->first_displayed_entry = s->first_displayed_entry;
2836 parent->selected_entry = s->selected_entry;
2837 parent->selected = s->selected;
2838 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2841 s->first_displayed_entry = NULL;
2845 static const struct got_error *
2846 tree_view_walk_path(struct tog_tree_view_state *s,
2847 struct got_commit_object *commit, const char *path)
2849 const struct got_error *err = NULL;
2850 struct got_tree_object *tree = NULL;
2852 char *slash, *subpath = NULL;
2854 /* Walk the path and open corresponding tree objects. */
2857 struct got_tree_entry *te;
2858 struct got_object_id *tree_id;
2864 /* Ensure the correct subtree entry is selected. */
2865 slash = strchr(p, '/');
2867 te_name = strdup(p);
2869 te_name = strndup(p, slash - p);
2870 if (te_name == NULL) {
2871 err = got_error_from_errno("strndup");
2874 te = got_object_tree_find_entry(s->tree, te_name);
2876 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2881 s->first_displayed_entry = s->selected_entry = te;
2883 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2884 break; /* jump to this file's entry */
2886 slash = strchr(p, '/');
2888 subpath = strndup(path, slash - path);
2890 subpath = strdup(path);
2891 if (subpath == NULL) {
2892 err = got_error_from_errno("strdup");
2896 err = got_object_id_by_path(&tree_id, s->repo, commit,
2901 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2906 err = tree_view_visit_subtree(s, tree);
2908 got_object_tree_close(tree);
2922 static const struct got_error *
2923 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2924 struct commit_queue_entry *entry, const char *path,
2925 const char *head_ref_name, struct got_repository *repo)
2927 const struct got_error *err = NULL;
2928 struct tog_tree_view_state *s;
2929 struct tog_view *tree_view;
2931 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2932 if (tree_view == NULL)
2933 return got_error_from_errno("view_open");
2935 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2938 s = &tree_view->state.tree;
2940 *new_view = tree_view;
2942 if (got_path_is_root_dir(path))
2945 return tree_view_walk_path(s, entry->commit, path);
2948 static const struct got_error *
2949 block_signals_used_by_main_thread(void)
2954 if (sigemptyset(&sigset) == -1)
2955 return got_error_from_errno("sigemptyset");
2957 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2958 if (sigaddset(&sigset, SIGWINCH) == -1)
2959 return got_error_from_errno("sigaddset");
2960 if (sigaddset(&sigset, SIGCONT) == -1)
2961 return got_error_from_errno("sigaddset");
2962 if (sigaddset(&sigset, SIGINT) == -1)
2963 return got_error_from_errno("sigaddset");
2964 if (sigaddset(&sigset, SIGTERM) == -1)
2965 return got_error_from_errno("sigaddset");
2967 /* ncurses handles SIGTSTP */
2968 if (sigaddset(&sigset, SIGTSTP) == -1)
2969 return got_error_from_errno("sigaddset");
2971 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2973 return got_error_set_errno(errcode, "pthread_sigmask");
2979 log_thread(void *arg)
2981 const struct got_error *err = NULL;
2983 struct tog_log_thread_args *a = arg;
2987 * Sync startup with main thread such that we begin our
2988 * work once view_input() has released the mutex.
2990 errcode = pthread_mutex_lock(&tog_mutex);
2992 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2996 err = block_signals_used_by_main_thread();
2998 pthread_mutex_unlock(&tog_mutex);
3002 while (!done && !err && !tog_fatal_signal_received()) {
3003 errcode = pthread_mutex_unlock(&tog_mutex);
3005 err = got_error_set_errno(errcode,
3006 "pthread_mutex_unlock");
3009 err = queue_commits(a);
3011 if (err->code != GOT_ERR_ITER_COMPLETED)
3015 } else if (a->commits_needed > 0 && !a->load_all) {
3018 a->commits_needed--;
3020 a->commits_needed--;
3023 errcode = pthread_mutex_lock(&tog_mutex);
3025 err = got_error_set_errno(errcode,
3026 "pthread_mutex_lock");
3028 } else if (*a->quit)
3030 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3031 *a->first_displayed_entry =
3032 TAILQ_FIRST(&a->limit_commits->head);
3033 *a->selected_entry = *a->first_displayed_entry;
3034 } else if (*a->first_displayed_entry == NULL) {
3035 *a->first_displayed_entry =
3036 TAILQ_FIRST(&a->real_commits->head);
3037 *a->selected_entry = *a->first_displayed_entry;
3040 errcode = pthread_cond_signal(&a->commit_loaded);
3042 err = got_error_set_errno(errcode,
3043 "pthread_cond_signal");
3044 pthread_mutex_unlock(&tog_mutex);
3049 a->commits_needed = 0;
3051 if (a->commits_needed == 0 && !a->load_all) {
3052 errcode = pthread_cond_wait(&a->need_commits,
3055 err = got_error_set_errno(errcode,
3056 "pthread_cond_wait");
3057 pthread_mutex_unlock(&tog_mutex);
3065 a->log_complete = 1;
3066 errcode = pthread_mutex_unlock(&tog_mutex);
3068 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3071 tog_thread_error = 1;
3072 pthread_cond_signal(&a->commit_loaded);
3077 static const struct got_error *
3078 stop_log_thread(struct tog_log_view_state *s)
3080 const struct got_error *err = NULL, *thread_err = NULL;
3085 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3087 return got_error_set_errno(errcode,
3088 "pthread_cond_signal");
3089 errcode = pthread_mutex_unlock(&tog_mutex);
3091 return got_error_set_errno(errcode,
3092 "pthread_mutex_unlock");
3093 errcode = pthread_join(s->thread, (void **)&thread_err);
3095 return got_error_set_errno(errcode, "pthread_join");
3096 errcode = pthread_mutex_lock(&tog_mutex);
3098 return got_error_set_errno(errcode,
3099 "pthread_mutex_lock");
3100 s->thread = 0; //NULL;
3103 if (s->thread_args.repo) {
3104 err = got_repo_close(s->thread_args.repo);
3105 s->thread_args.repo = NULL;
3108 if (s->thread_args.pack_fds) {
3109 const struct got_error *pack_err =
3110 got_repo_pack_fds_close(s->thread_args.pack_fds);
3113 s->thread_args.pack_fds = NULL;
3116 if (s->thread_args.graph) {
3117 got_commit_graph_close(s->thread_args.graph);
3118 s->thread_args.graph = NULL;
3121 return err ? err : thread_err;
3124 static const struct got_error *
3125 close_log_view(struct tog_view *view)
3127 const struct got_error *err = NULL;
3128 struct tog_log_view_state *s = &view->state.log;
3131 err = stop_log_thread(s);
3133 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3134 if (errcode && err == NULL)
3135 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3137 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3138 if (errcode && err == NULL)
3139 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3141 free_commits(&s->limit_commits);
3142 free_commits(&s->real_commits);
3143 free(s->in_repo_path);
3144 s->in_repo_path = NULL;
3147 free(s->head_ref_name);
3148 s->head_ref_name = NULL;
3153 * We use two queues to implement the limit feature: first consists of
3154 * commits matching the current limit_regex; second is the real queue
3155 * of all known commits (real_commits). When the user starts limiting,
3156 * we swap queues such that all movement and displaying functionality
3157 * works with very slight change.
3159 static const struct got_error *
3160 limit_log_view(struct tog_view *view)
3162 struct tog_log_view_state *s = &view->state.log;
3163 struct commit_queue_entry *entry;
3164 struct tog_view *v = view;
3165 const struct got_error *err = NULL;
3169 if (view_is_hsplit_top(view))
3171 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3174 /* Get the pattern */
3175 wmove(v->window, v->nlines - 1, 0);
3176 wclrtoeol(v->window);
3177 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3178 nodelay(v->window, FALSE);
3181 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3184 nodelay(v->window, TRUE);
3188 if (*pattern == '\0') {
3190 * Safety measure for the situation where the user
3191 * resets limit without previously limiting anything.
3197 * User could have pressed Ctrl+L, which refreshed the
3198 * commit queues, it means we can't save previously
3199 * (before limit took place) displayed entries,
3200 * because they would point to already free'ed memory,
3201 * so we are forced to always select first entry of
3204 s->commits = &s->real_commits;
3205 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3206 s->selected_entry = s->first_displayed_entry;
3213 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3218 /* Clear the screen while loading limit view */
3219 s->first_displayed_entry = NULL;
3220 s->last_displayed_entry = NULL;
3221 s->selected_entry = NULL;
3222 s->commits = &s->limit_commits;
3224 /* Prepare limit queue for new search */
3225 free_commits(&s->limit_commits);
3226 s->limit_commits.ncommits = 0;
3228 /* First process commits, which are in queue already */
3229 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3232 err = match_commit(&have_match, entry->id,
3233 entry->commit, &s->limit_regex);
3238 struct commit_queue_entry *matched;
3240 matched = alloc_commit_queue_entry(entry->commit,
3242 if (matched == NULL) {
3243 err = got_error_from_errno(
3244 "alloc_commit_queue_entry");
3247 matched->commit = entry->commit;
3248 got_object_commit_retain(entry->commit);
3250 matched->idx = s->limit_commits.ncommits;
3251 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3253 s->limit_commits.ncommits++;
3257 /* Second process all the commits, until we fill the screen */
3258 if (s->limit_commits.ncommits < view->nlines - 1 &&
3259 !s->thread_args.log_complete) {
3260 s->thread_args.commits_needed +=
3261 view->nlines - s->limit_commits.ncommits - 1;
3262 err = trigger_log_thread(view, 1);
3267 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3268 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3274 static const struct got_error *
3275 search_start_log_view(struct tog_view *view)
3277 struct tog_log_view_state *s = &view->state.log;
3279 s->matched_entry = NULL;
3280 s->search_entry = NULL;
3284 static const struct got_error *
3285 search_next_log_view(struct tog_view *view)
3287 const struct got_error *err = NULL;
3288 struct tog_log_view_state *s = &view->state.log;
3289 struct commit_queue_entry *entry;
3291 /* Display progress update in log view. */
3292 show_log_view(view);
3296 if (s->search_entry) {
3298 errcode = pthread_mutex_unlock(&tog_mutex);
3300 return got_error_set_errno(errcode,
3301 "pthread_mutex_unlock");
3302 ch = wgetch(view->window);
3303 errcode = pthread_mutex_lock(&tog_mutex);
3305 return got_error_set_errno(errcode,
3306 "pthread_mutex_lock");
3307 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3308 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3311 if (view->searching == TOG_SEARCH_FORWARD)
3312 entry = TAILQ_NEXT(s->search_entry, entry);
3314 entry = TAILQ_PREV(s->search_entry,
3315 commit_queue_head, entry);
3316 } else if (s->matched_entry) {
3318 * If the user has moved the cursor after we hit a match,
3319 * the position from where we should continue searching
3320 * might have changed.
3322 if (view->searching == TOG_SEARCH_FORWARD)
3323 entry = TAILQ_NEXT(s->selected_entry, entry);
3325 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3328 entry = s->selected_entry;
3334 if (entry == NULL) {
3335 if (s->thread_args.log_complete ||
3336 view->searching == TOG_SEARCH_BACKWARD) {
3337 view->search_next_done =
3338 (s->matched_entry == NULL ?
3339 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3340 s->search_entry = NULL;
3344 * Poke the log thread for more commits and return,
3345 * allowing the main loop to make progress. Search
3346 * will resume at s->search_entry once we come back.
3348 s->thread_args.commits_needed++;
3349 return trigger_log_thread(view, 0);
3352 err = match_commit(&have_match, entry->id, entry->commit,
3357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 s->matched_entry = entry;
3362 s->search_entry = entry;
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 entry = TAILQ_NEXT(entry, entry);
3366 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3369 if (s->matched_entry) {
3370 int cur = s->selected_entry->idx;
3371 while (cur < s->matched_entry->idx) {
3372 err = input_log_view(NULL, view, KEY_DOWN);
3377 while (cur > s->matched_entry->idx) {
3378 err = input_log_view(NULL, view, KEY_UP);
3385 s->search_entry = NULL;
3390 static const struct got_error *
3391 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3392 struct got_repository *repo, const char *head_ref_name,
3393 const char *in_repo_path, int log_branches)
3395 const struct got_error *err = NULL;
3396 struct tog_log_view_state *s = &view->state.log;
3397 struct got_repository *thread_repo = NULL;
3398 struct got_commit_graph *thread_graph = NULL;
3401 if (in_repo_path != s->in_repo_path) {
3402 free(s->in_repo_path);
3403 s->in_repo_path = strdup(in_repo_path);
3404 if (s->in_repo_path == NULL)
3405 return got_error_from_errno("strdup");
3408 /* The commit queue only contains commits being displayed. */
3409 TAILQ_INIT(&s->real_commits.head);
3410 s->real_commits.ncommits = 0;
3411 s->commits = &s->real_commits;
3413 TAILQ_INIT(&s->limit_commits.head);
3415 s->limit_commits.ncommits = 0;
3418 if (head_ref_name) {
3419 s->head_ref_name = strdup(head_ref_name);
3420 if (s->head_ref_name == NULL) {
3421 err = got_error_from_errno("strdup");
3425 s->start_id = got_object_id_dup(start_id);
3426 if (s->start_id == NULL) {
3427 err = got_error_from_errno("got_object_id_dup");
3430 s->log_branches = log_branches;
3431 s->use_committer = 1;
3433 STAILQ_INIT(&s->colors);
3434 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3435 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3436 get_color_value("TOG_COLOR_COMMIT"));
3439 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3440 get_color_value("TOG_COLOR_AUTHOR"));
3442 free_colors(&s->colors);
3445 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3446 get_color_value("TOG_COLOR_DATE"));
3448 free_colors(&s->colors);
3453 view->show = show_log_view;
3454 view->input = input_log_view;
3455 view->resize = resize_log_view;
3456 view->close = close_log_view;
3457 view->search_start = search_start_log_view;
3458 view->search_next = search_next_log_view;
3460 if (s->thread_args.pack_fds == NULL) {
3461 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3465 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3466 s->thread_args.pack_fds);
3469 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3473 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3474 s->repo, NULL, NULL);
3478 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3480 err = got_error_set_errno(errcode, "pthread_cond_init");
3483 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3485 err = got_error_set_errno(errcode, "pthread_cond_init");
3489 s->thread_args.commits_needed = view->nlines;
3490 s->thread_args.graph = thread_graph;
3491 s->thread_args.real_commits = &s->real_commits;
3492 s->thread_args.limit_commits = &s->limit_commits;
3493 s->thread_args.in_repo_path = s->in_repo_path;
3494 s->thread_args.start_id = s->start_id;
3495 s->thread_args.repo = thread_repo;
3496 s->thread_args.log_complete = 0;
3497 s->thread_args.quit = &s->quit;
3498 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3499 s->thread_args.selected_entry = &s->selected_entry;
3500 s->thread_args.searching = &view->searching;
3501 s->thread_args.search_next_done = &view->search_next_done;
3502 s->thread_args.regex = &view->regex;
3503 s->thread_args.limiting = &s->limit_view;
3504 s->thread_args.limit_regex = &s->limit_regex;
3505 s->thread_args.limit_commits = &s->limit_commits;
3508 close_log_view(view);
3512 static const struct got_error *
3513 show_log_view(struct tog_view *view)
3515 const struct got_error *err;
3516 struct tog_log_view_state *s = &view->state.log;
3518 if (s->thread == 0) { //NULL) {
3519 int errcode = pthread_create(&s->thread, NULL, log_thread,
3522 return got_error_set_errno(errcode, "pthread_create");
3523 if (s->thread_args.commits_needed > 0) {
3524 err = trigger_log_thread(view, 1);
3530 return draw_commits(view);
3534 log_move_cursor_up(struct tog_view *view, int page, int home)
3536 struct tog_log_view_state *s = &view->state.log;
3538 if (s->first_displayed_entry == NULL)
3540 if (s->selected_entry->idx == 0)
3543 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3545 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3547 if (!page && !home && s->selected > 0)
3550 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3556 static const struct got_error *
3557 log_move_cursor_down(struct tog_view *view, int page)
3559 struct tog_log_view_state *s = &view->state.log;
3560 const struct got_error *err = NULL;
3561 int eos = view->nlines - 2;
3563 if (s->first_displayed_entry == NULL)
3566 if (s->thread_args.log_complete &&
3567 s->selected_entry->idx >= s->commits->ncommits - 1)
3570 if (view_is_hsplit_top(view))
3571 --eos; /* border consumes the last line */
3574 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3577 err = log_scroll_down(view, 1);
3578 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3579 struct commit_queue_entry *entry;
3583 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3584 s->last_displayed_entry = entry;
3585 for (n = 0; n <= eos; n++) {
3588 s->first_displayed_entry = entry;
3589 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3592 s->selected = n - 1;
3594 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3595 s->thread_args.log_complete)
3596 s->selected += MIN(page,
3597 s->commits->ncommits - s->selected_entry->idx - 1);
3599 err = log_scroll_down(view, page);
3605 * We might necessarily overshoot in horizontal
3606 * splits; if so, select the last displayed commit.
3608 if (s->first_displayed_entry && s->last_displayed_entry) {
3609 s->selected = MIN(s->selected,
3610 s->last_displayed_entry->idx -
3611 s->first_displayed_entry->idx);
3616 if (s->thread_args.log_complete &&
3617 s->selected_entry->idx == s->commits->ncommits - 1)
3624 view_get_split(struct tog_view *view, int *y, int *x)
3629 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3630 if (view->child && view->child->resized_y)
3631 *y = view->child->resized_y;
3632 else if (view->resized_y)
3633 *y = view->resized_y;
3635 *y = view_split_begin_y(view->lines);
3636 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3637 if (view->child && view->child->resized_x)
3638 *x = view->child->resized_x;
3639 else if (view->resized_x)
3640 *x = view->resized_x;
3642 *x = view_split_begin_x(view->begin_x);
3646 /* Split view horizontally at y and offset view->state->selected line. */
3647 static const struct got_error *
3648 view_init_hsplit(struct tog_view *view, int y)
3650 const struct got_error *err = NULL;
3654 err = view_resize(view);
3658 err = offset_selection_down(view);
3663 static const struct got_error *
3664 log_goto_line(struct tog_view *view, int nlines)
3666 const struct got_error *err = NULL;
3667 struct tog_log_view_state *s = &view->state.log;
3668 int g, idx = s->selected_entry->idx;
3670 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3676 if (g >= s->first_displayed_entry->idx + 1 &&
3677 g <= s->last_displayed_entry->idx + 1 &&
3678 g - s->first_displayed_entry->idx - 1 < nlines) {
3679 s->selected = g - s->first_displayed_entry->idx - 1;
3685 err = log_move_cursor_down(view, g - idx - 1);
3686 if (!err && g > s->selected_entry->idx + 1)
3687 err = log_move_cursor_down(view,
3688 g - s->first_displayed_entry->idx - 1);
3691 } else if (idx + 1 > g)
3692 log_move_cursor_up(view, idx - g + 1, 0);
3694 if (g < nlines && s->first_displayed_entry->idx == 0)
3695 s->selected = g - 1;
3703 horizontal_scroll_input(struct tog_view *view, int ch)
3709 view->x -= MIN(view->x, 2);
3715 if (view->x + view->ncols / 2 < view->maxx)
3724 view->x = MAX(view->maxx - view->ncols / 2, 0);
3732 static const struct got_error *
3733 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3735 const struct got_error *err = NULL;
3736 struct tog_log_view_state *s = &view->state.log;
3739 if (s->thread_args.load_all) {
3740 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3741 s->thread_args.load_all = 0;
3742 else if (s->thread_args.log_complete) {
3743 err = log_move_cursor_down(view, s->commits->ncommits);
3744 s->thread_args.load_all = 0;
3750 eos = nscroll = view->nlines - 1;
3751 if (view_is_hsplit_top(view))
3755 return log_goto_line(view, eos);
3759 err = limit_log_view(view);
3770 horizontal_scroll_input(view, ch);
3777 log_move_cursor_up(view, 0, 0);
3782 log_move_cursor_up(view, 0, 1);
3792 log_move_cursor_up(view, nscroll, 0);
3799 err = log_move_cursor_down(view, 0);
3802 s->use_committer = !s->use_committer;
3803 view->action = s->use_committer ?
3804 "show committer" : "show commit author";
3809 /* We don't know yet how many commits, so we're forced to
3810 * traverse them all. */
3812 s->thread_args.load_all = 1;
3813 if (!s->thread_args.log_complete)
3814 return trigger_log_thread(view, 0);
3815 err = log_move_cursor_down(view, s->commits->ncommits);
3816 s->thread_args.load_all = 0;
3827 err = log_move_cursor_down(view, nscroll);
3830 if (s->selected > view->nlines - 2)
3831 s->selected = view->nlines - 2;
3832 if (s->selected > s->commits->ncommits - 1)
3833 s->selected = s->commits->ncommits - 1;
3835 if (s->commits->ncommits < view->nlines - 1 &&
3836 !s->thread_args.log_complete) {
3837 s->thread_args.commits_needed += (view->nlines - 1) -
3838 s->commits->ncommits;
3839 err = trigger_log_thread(view, 1);
3845 if (s->selected_entry == NULL)
3847 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3851 if (s->selected_entry == NULL)
3853 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3859 if (ch == KEY_BACKSPACE &&
3860 got_path_is_root_dir(s->in_repo_path))
3862 err = stop_log_thread(s);
3865 if (ch == KEY_BACKSPACE) {
3867 err = got_path_dirname(&parent_path, s->in_repo_path);
3870 free(s->in_repo_path);
3871 s->in_repo_path = parent_path;
3872 s->thread_args.in_repo_path = s->in_repo_path;
3873 } else if (ch == CTRL('l')) {
3874 struct got_object_id *start_id;
3875 err = got_repo_match_object_id(&start_id, NULL,
3876 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3877 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3879 if (s->head_ref_name == NULL ||
3880 err->code != GOT_ERR_NOT_REF)
3882 /* Try to cope with deleted references. */
3883 free(s->head_ref_name);
3884 s->head_ref_name = NULL;
3885 err = got_repo_match_object_id(&start_id,
3886 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3887 &tog_refs, s->repo);
3892 s->start_id = start_id;
3893 s->thread_args.start_id = s->start_id;
3895 s->log_branches = !s->log_branches;
3897 if (s->thread_args.pack_fds == NULL) {
3898 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3902 err = got_repo_open(&s->thread_args.repo,
3903 got_repo_get_path(s->repo), NULL,
3904 s->thread_args.pack_fds);
3908 err = tog_load_refs(s->repo, 0);
3911 err = got_commit_graph_open(&s->thread_args.graph,
3912 s->in_repo_path, !s->log_branches);
3915 err = got_commit_graph_iter_start(s->thread_args.graph,
3916 s->start_id, s->repo, NULL, NULL);
3919 free_commits(&s->real_commits);
3920 free_commits(&s->limit_commits);
3921 s->first_displayed_entry = NULL;
3922 s->last_displayed_entry = NULL;
3923 s->selected_entry = NULL;
3925 s->thread_args.log_complete = 0;
3927 s->thread_args.commits_needed = view->lines;
3928 s->matched_entry = NULL;
3929 s->search_entry = NULL;
3934 err = view_request_new(new_view, view, TOG_VIEW_REF);
3944 static const struct got_error *
3945 apply_unveil(const char *repo_path, const char *worktree_path)
3947 const struct got_error *error;
3950 if (unveil("gmon.out", "rwc") != 0)
3951 return got_error_from_errno2("unveil", "gmon.out");
3953 if (repo_path && unveil(repo_path, "r") != 0)
3954 return got_error_from_errno2("unveil", repo_path);
3956 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3957 return got_error_from_errno2("unveil", worktree_path);
3959 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3960 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3962 error = got_privsep_unveil_exec_helpers();
3966 if (unveil(NULL, NULL) != 0)
3967 return got_error_from_errno("unveil");
3976 * Override default signal handlers before starting ncurses.
3977 * This should prevent ncurses from installing its own
3978 * broken cleanup() signal handler.
3980 signal(SIGWINCH, tog_sigwinch);
3981 signal(SIGPIPE, tog_sigpipe);
3982 signal(SIGCONT, tog_sigcont);
3983 signal(SIGINT, tog_sigint);
3984 signal(SIGTERM, tog_sigterm);
3988 halfdelay(1); /* Do fast refresh while initial view is loading. */
3991 intrflush(stdscr, FALSE);
3992 keypad(stdscr, TRUE);
3994 if (getenv("TOG_COLORS") != NULL) {
3996 use_default_colors();
4000 static const struct got_error *
4001 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4002 struct got_repository *repo, struct got_worktree *worktree)
4004 const struct got_error *err = NULL;
4007 *in_repo_path = strdup("/");
4008 if (*in_repo_path == NULL)
4009 return got_error_from_errno("strdup");
4014 const char *prefix = got_worktree_get_path_prefix(worktree);
4017 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4020 if (asprintf(in_repo_path, "%s%s%s", prefix,
4021 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4023 err = got_error_from_errno("asprintf");
4024 *in_repo_path = NULL;
4028 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4033 static const struct got_error *
4034 cmd_log(int argc, char *argv[])
4036 const struct got_error *error;
4037 struct got_repository *repo = NULL;
4038 struct got_worktree *worktree = NULL;
4039 struct got_object_id *start_id = NULL;
4040 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4041 char *start_commit = NULL, *label = NULL;
4042 struct got_reference *ref = NULL;
4043 const char *head_ref_name = NULL;
4044 int ch, log_branches = 0;
4045 struct tog_view *view;
4046 int *pack_fds = NULL;
4048 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4054 start_commit = optarg;
4057 repo_path = realpath(optarg, NULL);
4058 if (repo_path == NULL)
4059 return got_error_from_errno2("realpath",
4074 error = got_repo_pack_fds_open(&pack_fds);
4078 if (repo_path == NULL) {
4079 cwd = getcwd(NULL, 0);
4081 return got_error_from_errno("getcwd");
4082 error = got_worktree_open(&worktree, cwd);
4083 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4087 strdup(got_worktree_get_repo_path(worktree));
4089 repo_path = strdup(cwd);
4090 if (repo_path == NULL) {
4091 error = got_error_from_errno("strdup");
4096 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4100 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4107 error = apply_unveil(got_repo_get_path(repo),
4108 worktree ? got_worktree_get_root_path(worktree) : NULL);
4112 /* already loaded by tog_log_with_path()? */
4113 if (TAILQ_EMPTY(&tog_refs)) {
4114 error = tog_load_refs(repo, 0);
4119 if (start_commit == NULL) {
4120 error = got_repo_match_object_id(&start_id, &label,
4121 worktree ? got_worktree_get_head_ref_name(worktree) :
4122 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4125 head_ref_name = label;
4127 error = got_ref_open(&ref, repo, start_commit, 0);
4129 head_ref_name = got_ref_get_name(ref);
4130 else if (error->code != GOT_ERR_NOT_REF)
4132 error = got_repo_match_object_id(&start_id, NULL,
4133 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4138 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4140 error = got_error_from_errno("view_open");
4143 error = open_log_view(view, start_id, repo, head_ref_name,
4144 in_repo_path, log_branches);
4148 /* Release work tree lock. */
4149 got_worktree_close(worktree);
4152 error = view_loop(view);
4162 const struct got_error *close_err = got_repo_close(repo);
4167 got_worktree_close(worktree);
4169 const struct got_error *pack_err =
4170 got_repo_pack_fds_close(pack_fds);
4182 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4183 "object1 object2\n", getprogname());
4188 match_line(const char *line, regex_t *regex, size_t nmatch,
4189 regmatch_t *regmatch)
4191 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4194 static struct tog_color *
4195 match_color(struct tog_colors *colors, const char *line)
4197 struct tog_color *tc = NULL;
4199 STAILQ_FOREACH(tc, colors, entry) {
4200 if (match_line(line, &tc->regex, 0, NULL))
4207 static const struct got_error *
4208 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4209 WINDOW *window, int skipcol, regmatch_t *regmatch)
4211 const struct got_error *err = NULL;
4213 wchar_t *wline = NULL;
4214 int rme, rms, n, width, scrollx;
4215 int width0 = 0, width1 = 0, width2 = 0;
4216 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4220 rms = regmatch->rm_so;
4221 rme = regmatch->rm_eo;
4223 err = expand_tab(&exstr, line);
4227 /* Split the line into 3 segments, according to match offsets. */
4228 seg0 = strndup(exstr, rms);
4230 err = got_error_from_errno("strndup");
4233 seg1 = strndup(exstr + rms, rme - rms);
4235 err = got_error_from_errno("strndup");
4238 seg2 = strdup(exstr + rme);
4240 err = got_error_from_errno("strndup");
4244 /* draw up to matched token if we haven't scrolled past it */
4245 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4249 n = MAX(width0 - skipcol, 0);
4252 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4253 wlimit, col_tab_align, 1);
4256 waddwstr(window, &wline[scrollx]);
4266 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4270 wlen = wcslen(wline);
4272 width = wcwidth(wline[i]);
4274 /* should not happen, tabs are expanded */
4275 err = got_error(GOT_ERR_RANGE);
4278 if (width0 + w + width > skipcol)
4283 /* draw (visible part of) matched token (if scrolled into it) */
4284 if (width1 - w > 0) {
4285 wattron(window, A_STANDOUT);
4286 waddwstr(window, &wline[i]);
4287 wattroff(window, A_STANDOUT);
4288 wlimit -= (width1 - w);
4289 *wtotal += (width1 - w);
4293 if (wlimit > 0) { /* draw rest of line */
4295 if (skipcol > width0 + width1) {
4296 err = format_line(&wline, &width2, &scrollx, seg2,
4297 skipcol - (width0 + width1), wlimit,
4301 waddwstr(window, &wline[scrollx]);
4303 err = format_line(&wline, &width2, NULL, seg2, 0,
4304 wlimit, col_tab_align, 1);
4307 waddwstr(window, wline);
4321 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4324 int *eof, *first, *selected;
4326 if (view->type == TOG_VIEW_DIFF) {
4327 struct tog_diff_view_state *s = &view->state.diff;
4329 first = &s->first_displayed_line;
4333 } else if (view->type == TOG_VIEW_HELP) {
4334 struct tog_help_view_state *s = &view->state.help;
4336 first = &s->first_displayed_line;
4340 } else if (view->type == TOG_VIEW_BLAME) {
4341 struct tog_blame_view_state *s = &view->state.blame;
4343 first = &s->first_displayed_line;
4344 selected = &s->selected_line;
4350 /* Center gline in the middle of the page like vi(1). */
4351 if (*lineno < view->gline - (view->nlines - 3) / 2)
4353 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4362 *selected = view->gline <= (view->nlines - 3) / 2 ?
4363 view->gline : (view->nlines - 3) / 2 + 1;
4369 static const struct got_error *
4370 draw_file(struct tog_view *view, const char *header)
4372 struct tog_diff_view_state *s = &view->state.diff;
4373 regmatch_t *regmatch = &view->regmatch;
4374 const struct got_error *err;
4377 size_t linesize = 0;
4381 int max_lines = view->nlines;
4382 int nlines = s->nlines;
4385 s->lineno = s->first_displayed_line - 1;
4386 line_offset = s->lines[s->first_displayed_line - 1].offset;
4387 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4388 return got_error_from_errno("fseek");
4390 werase(view->window);
4392 if (view->gline > s->nlines - 1)
4393 view->gline = s->nlines - 1;
4396 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4397 1 : view->gline - (view->nlines - 3) / 2 :
4398 s->lineno + s->selected_line;
4400 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4401 return got_error_from_errno("asprintf");
4402 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4408 if (view_needs_focus_indication(view))
4409 wstandout(view->window);
4410 waddwstr(view->window, wline);
4413 while (width++ < view->ncols)
4414 waddch(view->window, ' ');
4415 if (view_needs_focus_indication(view))
4416 wstandend(view->window);
4426 while (max_lines > 0 && nprinted < max_lines) {
4427 enum got_diff_line_type linetype;
4430 linelen = getline(&line, &linesize, s->f);
4431 if (linelen == -1) {
4437 return got_ferror(s->f, GOT_ERR_IO);
4440 if (++s->lineno < s->first_displayed_line)
4442 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4444 if (s->lineno == view->hiline)
4447 /* Set view->maxx based on full line length. */
4448 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4454 view->maxx = MAX(view->maxx, width);
4458 linetype = s->lines[s->lineno].type;
4459 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4460 linetype < GOT_DIFF_LINE_CONTEXT)
4461 attr |= COLOR_PAIR(linetype);
4463 wattron(view->window, attr);
4464 if (s->first_displayed_line + nprinted == s->matched_line &&
4465 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4466 err = add_matched_line(&width, line, view->ncols, 0,
4467 view->window, view->x, regmatch);
4474 err = format_line(&wline, &width, &skip, line,
4475 view->x, view->ncols, 0, view->x ? 1 : 0);
4480 waddwstr(view->window, &wline[skip]);
4484 if (s->lineno == view->hiline) {
4485 /* highlight full gline length */
4486 while (width++ < view->ncols)
4487 waddch(view->window, ' ');
4489 if (width <= view->ncols - 1)
4490 waddch(view->window, '\n');
4493 wattroff(view->window, attr);
4494 if (++nprinted == 1)
4495 s->first_displayed_line = s->lineno;
4499 s->last_displayed_line = s->first_displayed_line +
4502 s->last_displayed_line = s->first_displayed_line;
4507 while (nprinted < view->nlines) {
4508 waddch(view->window, '\n');
4512 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4518 wstandout(view->window);
4519 waddwstr(view->window, wline);
4522 wstandend(view->window);
4529 get_datestr(time_t *time, char *datebuf)
4531 struct tm mytm, *tm;
4534 tm = gmtime_r(time, &mytm);
4537 s = asctime_r(tm, datebuf);
4540 p = strchr(s, '\n');
4546 static const struct got_error *
4547 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4548 off_t off, uint8_t type)
4550 struct got_diff_line *p;
4552 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4554 return got_error_from_errno("reallocarray");
4556 (*lines)[*nlines].offset = off;
4557 (*lines)[*nlines].type = type;
4563 static const struct got_error *
4564 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4565 struct got_diff_line *s_lines, size_t s_nlines)
4567 struct got_diff_line *p;
4571 if (fseeko(src, 0L, SEEK_SET) == -1)
4572 return got_error_from_errno("fseeko");
4575 r = fread(buf, 1, sizeof(buf), src);
4578 return got_error_from_errno("fread");
4582 if (fwrite(buf, 1, r, dst) != r)
4583 return got_ferror(dst, GOT_ERR_IO);
4586 if (s_nlines == 0 && *d_nlines == 0)
4590 * If commit info was in dst, increment line offsets
4591 * of the appended diff content, but skip s_lines[0]
4592 * because offset zero is already in *d_lines.
4594 if (*d_nlines > 0) {
4595 for (i = 1; i < s_nlines; ++i)
4596 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4604 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4606 /* d_lines is freed in close_diff_view() */
4607 return got_error_from_errno("reallocarray");
4612 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4613 *d_nlines += s_nlines;
4618 static const struct got_error *
4619 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4620 struct got_object_id *commit_id, struct got_reflist_head *refs,
4621 struct got_repository *repo, int ignore_ws, int force_text_diff,
4622 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4624 const struct got_error *err = NULL;
4625 char datebuf[26], *datestr;
4626 struct got_commit_object *commit;
4627 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4628 time_t committer_time;
4629 const char *author, *committer;
4630 char *refs_str = NULL;
4631 struct got_pathlist_entry *pe;
4636 err = build_refs_str(&refs_str, refs, commit_id, repo);
4641 err = got_object_open_as_commit(&commit, repo, commit_id);
4645 err = got_object_id_str(&id_str, commit_id);
4647 err = got_error_from_errno("got_object_id_str");
4651 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4655 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4656 refs_str ? refs_str : "", refs_str ? ")" : "");
4658 err = got_error_from_errno("fprintf");
4662 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4666 n = fprintf(outfile, "from: %s\n",
4667 got_object_commit_get_author(commit));
4669 err = got_error_from_errno("fprintf");
4673 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4677 author = got_object_commit_get_author(commit);
4678 committer = got_object_commit_get_committer(commit);
4679 if (strcmp(author, committer) != 0) {
4680 n = fprintf(outfile, "via: %s\n", committer);
4682 err = got_error_from_errno("fprintf");
4686 err = add_line_metadata(lines, nlines, outoff,
4687 GOT_DIFF_LINE_AUTHOR);
4691 committer_time = got_object_commit_get_committer_time(commit);
4692 datestr = get_datestr(&committer_time, datebuf);
4694 n = fprintf(outfile, "date: %s UTC\n", datestr);
4696 err = got_error_from_errno("fprintf");
4700 err = add_line_metadata(lines, nlines, outoff,
4701 GOT_DIFF_LINE_DATE);
4705 if (got_object_commit_get_nparents(commit) > 1) {
4706 const struct got_object_id_queue *parent_ids;
4707 struct got_object_qid *qid;
4709 parent_ids = got_object_commit_get_parent_ids(commit);
4710 STAILQ_FOREACH(qid, parent_ids, entry) {
4711 err = got_object_id_str(&id_str, &qid->id);
4714 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4716 err = got_error_from_errno("fprintf");
4720 err = add_line_metadata(lines, nlines, outoff,
4721 GOT_DIFF_LINE_META);
4729 err = got_object_commit_get_logmsg(&logmsg, commit);
4733 while ((line = strsep(&s, "\n")) != NULL) {
4734 n = fprintf(outfile, "%s\n", line);
4736 err = got_error_from_errno("fprintf");
4740 err = add_line_metadata(lines, nlines, outoff,
4741 GOT_DIFF_LINE_LOGMSG);
4746 TAILQ_FOREACH(pe, dsa->paths, entry) {
4747 struct got_diff_changed_path *cp = pe->data;
4748 int pad = dsa->max_path_len - pe->path_len + 1;
4750 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4751 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4752 dsa->rm_cols + 1, cp->rm);
4754 err = got_error_from_errno("fprintf");
4758 err = add_line_metadata(lines, nlines, outoff,
4759 GOT_DIFF_LINE_CHANGES);
4764 fputc('\n', outfile);
4766 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4770 n = fprintf(outfile,
4771 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4772 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4773 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4775 err = got_error_from_errno("fprintf");
4779 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4783 fputc('\n', outfile);
4785 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4790 got_object_commit_close(commit);
4799 static const struct got_error *
4800 create_diff(struct tog_diff_view_state *s)
4802 const struct got_error *err = NULL;
4803 FILE *f = NULL, *tmp_diff_file = NULL;
4805 struct got_diff_line *lines = NULL;
4806 struct got_pathlist_head changed_paths;
4808 TAILQ_INIT(&changed_paths);
4811 s->lines = malloc(sizeof(*s->lines));
4812 if (s->lines == NULL)
4813 return got_error_from_errno("malloc");
4818 err = got_error_from_errno("got_opentemp");
4821 tmp_diff_file = got_opentemp();
4822 if (tmp_diff_file == NULL) {
4823 err = got_error_from_errno("got_opentemp");
4826 if (s->f && fclose(s->f) == EOF) {
4827 err = got_error_from_errno("fclose");
4833 err = got_object_get_type(&obj_type, s->repo, s->id1);
4835 err = got_object_get_type(&obj_type, s->repo, s->id2);
4840 case GOT_OBJ_TYPE_BLOB:
4841 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4842 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4843 s->label1, s->label2, tog_diff_algo, s->diff_context,
4844 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4847 case GOT_OBJ_TYPE_TREE:
4848 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4849 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4850 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4851 s->force_text_diff, NULL, s->repo, s->f);
4853 case GOT_OBJ_TYPE_COMMIT: {
4854 const struct got_object_id_queue *parent_ids;
4855 struct got_object_qid *pid;
4856 struct got_commit_object *commit2;
4857 struct got_reflist_head *refs;
4859 struct got_diffstat_cb_arg dsa = {
4862 s->ignore_whitespace,
4867 lines = malloc(sizeof(*lines));
4868 if (lines == NULL) {
4869 err = got_error_from_errno("malloc");
4873 /* build diff first in tmp file then append to commit info */
4874 err = got_diff_objects_as_commits(&lines, &nlines,
4875 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4876 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4877 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4881 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4884 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4885 /* Show commit info if we're diffing to a parent/root commit. */
4886 if (s->id1 == NULL) {
4887 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4888 refs, s->repo, s->ignore_whitespace,
4889 s->force_text_diff, &dsa, s->f);
4893 parent_ids = got_object_commit_get_parent_ids(commit2);
4894 STAILQ_FOREACH(pid, parent_ids, entry) {
4895 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4896 err = write_commit_info(&s->lines,
4897 &s->nlines, s->id2, refs, s->repo,
4898 s->ignore_whitespace,
4899 s->force_text_diff, &dsa, s->f);
4906 got_object_commit_close(commit2);
4908 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4913 err = got_error(GOT_ERR_OBJ_TYPE);
4918 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4919 if (s->f && fflush(s->f) != 0 && err == NULL)
4920 err = got_error_from_errno("fflush");
4921 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4922 err = got_error_from_errno("fclose");
4927 diff_view_indicate_progress(struct tog_view *view)
4929 mvwaddstr(view->window, 0, 0, "diffing...");
4934 static const struct got_error *
4935 search_start_diff_view(struct tog_view *view)
4937 struct tog_diff_view_state *s = &view->state.diff;
4939 s->matched_line = 0;
4944 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4945 size_t *nlines, int **first, int **last, int **match, int **selected)
4947 struct tog_diff_view_state *s = &view->state.diff;
4950 *nlines = s->nlines;
4951 *line_offsets = NULL;
4952 *match = &s->matched_line;
4953 *first = &s->first_displayed_line;
4954 *last = &s->last_displayed_line;
4955 *selected = &s->selected_line;
4958 static const struct got_error *
4959 search_next_view_match(struct tog_view *view)
4961 const struct got_error *err = NULL;
4965 size_t linesize = 0;
4967 off_t *line_offsets;
4969 int *first, *last, *match, *selected;
4971 if (!view->search_setup)
4972 return got_error_msg(GOT_ERR_NOT_IMPL,
4973 "view search not supported");
4974 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4977 if (!view->searching) {
4978 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4983 if (view->searching == TOG_SEARCH_FORWARD)
4984 lineno = *match + 1;
4986 lineno = *match - 1;
4988 lineno = *first - 1 + *selected;
4993 if (lineno <= 0 || lineno > nlines) {
4995 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4999 if (view->searching == TOG_SEARCH_FORWARD)
5005 offset = view->type == TOG_VIEW_DIFF ?
5006 view->state.diff.lines[lineno - 1].offset :
5007 line_offsets[lineno - 1];
5008 if (fseeko(f, offset, SEEK_SET) != 0) {
5010 return got_error_from_errno("fseeko");
5012 linelen = getline(&line, &linesize, f);
5013 if (linelen != -1) {
5015 err = expand_tab(&exstr, line);
5018 if (match_line(exstr, &view->regex, 1,
5020 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5027 if (view->searching == TOG_SEARCH_FORWARD)
5042 static const struct got_error *
5043 close_diff_view(struct tog_view *view)
5045 const struct got_error *err = NULL;
5046 struct tog_diff_view_state *s = &view->state.diff;
5052 if (s->f && fclose(s->f) == EOF)
5053 err = got_error_from_errno("fclose");
5055 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5056 err = got_error_from_errno("fclose");
5058 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5059 err = got_error_from_errno("fclose");
5061 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5062 err = got_error_from_errno("close");
5064 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5065 err = got_error_from_errno("close");
5073 static const struct got_error *
5074 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5075 struct got_object_id *id2, const char *label1, const char *label2,
5076 int diff_context, int ignore_whitespace, int force_text_diff,
5077 struct tog_view *parent_view, struct got_repository *repo)
5079 const struct got_error *err;
5080 struct tog_diff_view_state *s = &view->state.diff;
5082 memset(s, 0, sizeof(*s));
5086 if (id1 != NULL && id2 != NULL) {
5088 err = got_object_get_type(&type1, repo, id1);
5091 err = got_object_get_type(&type2, repo, id2);
5096 return got_error(GOT_ERR_OBJ_TYPE);
5098 s->first_displayed_line = 1;
5099 s->last_displayed_line = view->nlines;
5100 s->selected_line = 1;
5108 s->id1 = got_object_id_dup(id1);
5110 return got_error_from_errno("got_object_id_dup");
5114 s->id2 = got_object_id_dup(id2);
5115 if (s->id2 == NULL) {
5116 err = got_error_from_errno("got_object_id_dup");
5120 s->f1 = got_opentemp();
5121 if (s->f1 == NULL) {
5122 err = got_error_from_errno("got_opentemp");
5126 s->f2 = got_opentemp();
5127 if (s->f2 == NULL) {
5128 err = got_error_from_errno("got_opentemp");
5132 s->fd1 = got_opentempfd();
5134 err = got_error_from_errno("got_opentempfd");
5138 s->fd2 = got_opentempfd();
5140 err = got_error_from_errno("got_opentempfd");
5144 s->diff_context = diff_context;
5145 s->ignore_whitespace = ignore_whitespace;
5146 s->force_text_diff = force_text_diff;
5147 s->parent_view = parent_view;
5150 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5153 rc = init_pair(GOT_DIFF_LINE_MINUS,
5154 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5156 rc = init_pair(GOT_DIFF_LINE_PLUS,
5157 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5159 rc = init_pair(GOT_DIFF_LINE_HUNK,
5160 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5162 rc = init_pair(GOT_DIFF_LINE_META,
5163 get_color_value("TOG_COLOR_DIFF_META"), -1);
5165 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5166 get_color_value("TOG_COLOR_DIFF_META"), -1);
5168 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5169 get_color_value("TOG_COLOR_DIFF_META"), -1);
5171 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5172 get_color_value("TOG_COLOR_DIFF_META"), -1);
5174 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5175 get_color_value("TOG_COLOR_AUTHOR"), -1);
5177 rc = init_pair(GOT_DIFF_LINE_DATE,
5178 get_color_value("TOG_COLOR_DATE"), -1);
5180 err = got_error(GOT_ERR_RANGE);
5185 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5186 view_is_splitscreen(view))
5187 show_log_view(parent_view); /* draw border */
5188 diff_view_indicate_progress(view);
5190 err = create_diff(s);
5192 view->show = show_diff_view;
5193 view->input = input_diff_view;
5194 view->reset = reset_diff_view;
5195 view->close = close_diff_view;
5196 view->search_start = search_start_diff_view;
5197 view->search_setup = search_setup_diff_view;
5198 view->search_next = search_next_view_match;
5201 close_diff_view(view);
5205 static const struct got_error *
5206 show_diff_view(struct tog_view *view)
5208 const struct got_error *err;
5209 struct tog_diff_view_state *s = &view->state.diff;
5210 char *id_str1 = NULL, *id_str2, *header;
5211 const char *label1, *label2;
5214 err = got_object_id_str(&id_str1, s->id1);
5217 label1 = s->label1 ? s->label1 : id_str1;
5219 label1 = "/dev/null";
5221 err = got_object_id_str(&id_str2, s->id2);
5224 label2 = s->label2 ? s->label2 : id_str2;
5226 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5227 err = got_error_from_errno("asprintf");
5235 err = draw_file(view, header);
5240 static const struct got_error *
5241 set_selected_commit(struct tog_diff_view_state *s,
5242 struct commit_queue_entry *entry)
5244 const struct got_error *err;
5245 const struct got_object_id_queue *parent_ids;
5246 struct got_commit_object *selected_commit;
5247 struct got_object_qid *pid;
5250 s->id2 = got_object_id_dup(entry->id);
5252 return got_error_from_errno("got_object_id_dup");
5254 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5257 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5259 pid = STAILQ_FIRST(parent_ids);
5260 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5261 got_object_commit_close(selected_commit);
5265 static const struct got_error *
5266 reset_diff_view(struct tog_view *view)
5268 struct tog_diff_view_state *s = &view->state.diff;
5271 wclear(view->window);
5272 s->first_displayed_line = 1;
5273 s->last_displayed_line = view->nlines;
5274 s->matched_line = 0;
5275 diff_view_indicate_progress(view);
5276 return create_diff(s);
5280 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5284 i = start = s->first_displayed_line - 1;
5286 while (s->lines[i].type != type) {
5290 return; /* do nothing, requested type not in file */
5293 s->selected_line = 1;
5294 s->first_displayed_line = i;
5298 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5302 i = start = s->first_displayed_line + 1;
5304 while (s->lines[i].type != type) {
5305 if (i == s->nlines - 1)
5308 return; /* do nothing, requested type not in file */
5311 s->selected_line = 1;
5312 s->first_displayed_line = i;
5315 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5317 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5320 static const struct got_error *
5321 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5323 const struct got_error *err = NULL;
5324 struct tog_diff_view_state *s = &view->state.diff;
5325 struct tog_log_view_state *ls;
5326 struct commit_queue_entry *old_selected_entry;
5328 size_t linesize = 0;
5330 int i, nscroll = view->nlines - 1, up = 0;
5332 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5341 horizontal_scroll_input(view, ch);
5346 s->force_text_diff = !s->force_text_diff;
5347 view->action = s->force_text_diff ?
5348 "force ASCII text enabled" :
5349 "force ASCII text disabled";
5351 else if (ch == 'w') {
5352 s->ignore_whitespace = !s->ignore_whitespace;
5353 view->action = s->ignore_whitespace ?
5354 "ignore whitespace enabled" :
5355 "ignore whitespace disabled";
5357 err = reset_diff_view(view);
5361 s->first_displayed_line = 1;
5370 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5376 if (s->first_displayed_line > 1)
5377 s->first_displayed_line--;
5388 if (s->first_displayed_line == 1) {
5393 while (i++ < nscroll && s->first_displayed_line > 1)
5394 s->first_displayed_line--;
5400 s->first_displayed_line++;
5417 while (!s->eof && i++ < nscroll) {
5418 linelen = getline(&line, &linesize, s->f);
5419 s->first_displayed_line++;
5420 if (linelen == -1) {
5424 err = got_ferror(s->f, GOT_ERR_IO);
5431 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5434 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5437 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5440 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5443 if (s->diff_context > 0) {
5445 s->matched_line = 0;
5446 diff_view_indicate_progress(view);
5447 err = create_diff(s);
5448 if (s->first_displayed_line + view->nlines - 1 >
5450 s->first_displayed_line = 1;
5451 s->last_displayed_line = view->nlines;
5457 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5459 s->matched_line = 0;
5460 diff_view_indicate_progress(view);
5461 err = create_diff(s);
5473 if (s->parent_view == NULL) {
5477 s->parent_view->count = view->count;
5479 if (s->parent_view->type == TOG_VIEW_LOG) {
5480 ls = &s->parent_view->state.log;
5481 old_selected_entry = ls->selected_entry;
5483 err = input_log_view(NULL, s->parent_view,
5484 up ? KEY_UP : KEY_DOWN);
5487 view->count = s->parent_view->count;
5489 if (old_selected_entry == ls->selected_entry)
5492 err = set_selected_commit(s, ls->selected_entry);
5495 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5496 struct tog_blame_view_state *bs;
5497 struct got_object_id *id, *prev_id;
5499 bs = &s->parent_view->state.blame;
5500 prev_id = get_annotation_for_line(bs->blame.lines,
5501 bs->blame.nlines, bs->last_diffed_line);
5503 err = input_blame_view(&view, s->parent_view,
5504 up ? KEY_UP : KEY_DOWN);
5507 view->count = s->parent_view->count;
5509 if (prev_id == NULL)
5511 id = get_selected_commit_id(bs->blame.lines,
5512 bs->blame.nlines, bs->first_displayed_line,
5517 if (!got_object_id_cmp(prev_id, id))
5520 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5524 s->first_displayed_line = 1;
5525 s->last_displayed_line = view->nlines;
5526 s->matched_line = 0;
5529 diff_view_indicate_progress(view);
5530 err = create_diff(s);
5540 static const struct got_error *
5541 cmd_diff(int argc, char *argv[])
5543 const struct got_error *error = NULL;
5544 struct got_repository *repo = NULL;
5545 struct got_worktree *worktree = NULL;
5546 struct got_object_id *id1 = NULL, *id2 = NULL;
5547 char *repo_path = NULL, *cwd = NULL;
5548 char *id_str1 = NULL, *id_str2 = NULL;
5549 char *label1 = NULL, *label2 = NULL;
5550 int diff_context = 3, ignore_whitespace = 0;
5551 int ch, force_text_diff = 0;
5553 struct tog_view *view;
5554 int *pack_fds = NULL;
5556 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5559 force_text_diff = 1;
5562 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5565 errx(1, "number of context lines is %s: %s",
5569 repo_path = realpath(optarg, NULL);
5570 if (repo_path == NULL)
5571 return got_error_from_errno2("realpath",
5573 got_path_strip_trailing_slashes(repo_path);
5576 ignore_whitespace = 1;
5588 usage_diff(); /* TODO show local worktree changes */
5589 } else if (argc == 2) {
5595 error = got_repo_pack_fds_open(&pack_fds);
5599 if (repo_path == NULL) {
5600 cwd = getcwd(NULL, 0);
5602 return got_error_from_errno("getcwd");
5603 error = got_worktree_open(&worktree, cwd);
5604 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5608 strdup(got_worktree_get_repo_path(worktree));
5610 repo_path = strdup(cwd);
5611 if (repo_path == NULL) {
5612 error = got_error_from_errno("strdup");
5617 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5623 error = apply_unveil(got_repo_get_path(repo), NULL);
5627 error = tog_load_refs(repo, 0);
5631 error = got_repo_match_object_id(&id1, &label1, id_str1,
5632 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5636 error = got_repo_match_object_id(&id2, &label2, id_str2,
5637 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5641 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5643 error = got_error_from_errno("view_open");
5646 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5647 ignore_whitespace, force_text_diff, NULL, repo);
5650 error = view_loop(view);
5657 const struct got_error *close_err = got_repo_close(repo);
5662 got_worktree_close(worktree);
5664 const struct got_error *pack_err =
5665 got_repo_pack_fds_close(pack_fds);
5678 "usage: %s blame [-c commit] [-r repository-path] path\n",
5683 struct tog_blame_line {
5685 struct got_object_id *id;
5688 static const struct got_error *
5689 draw_blame(struct tog_view *view)
5691 struct tog_blame_view_state *s = &view->state.blame;
5692 struct tog_blame *blame = &s->blame;
5693 regmatch_t *regmatch = &view->regmatch;
5694 const struct got_error *err;
5695 int lineno = 0, nprinted = 0;
5697 size_t linesize = 0;
5701 struct tog_blame_line *blame_line;
5702 struct got_object_id *prev_id = NULL;
5704 struct tog_color *tc;
5706 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5711 werase(view->window);
5713 if (asprintf(&line, "commit %s", id_str) == -1) {
5714 err = got_error_from_errno("asprintf");
5719 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5724 if (view_needs_focus_indication(view))
5725 wstandout(view->window);
5726 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5728 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5729 waddwstr(view->window, wline);
5730 while (width++ < view->ncols)
5731 waddch(view->window, ' ');
5733 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5734 if (view_needs_focus_indication(view))
5735 wstandend(view->window);
5739 if (view->gline > blame->nlines)
5740 view->gline = blame->nlines;
5742 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5743 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5744 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5746 return got_error_from_errno("asprintf");
5749 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5754 waddwstr(view->window, wline);
5757 if (width < view->ncols - 1)
5758 waddch(view->window, '\n');
5762 while (nprinted < view->nlines - 2) {
5763 linelen = getline(&line, &linesize, blame->f);
5764 if (linelen == -1) {
5765 if (feof(blame->f)) {
5770 return got_ferror(blame->f, GOT_ERR_IO);
5772 if (++lineno < s->first_displayed_line)
5774 if (view->gline && !gotoline(view, &lineno, &nprinted))
5777 /* Set view->maxx based on full line length. */
5778 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5785 view->maxx = MAX(view->maxx, width);
5787 if (nprinted == s->selected_line - 1)
5788 wstandout(view->window);
5790 if (blame->nlines > 0) {
5791 blame_line = &blame->lines[lineno - 1];
5792 if (blame_line->annotated && prev_id &&
5793 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5794 !(nprinted == s->selected_line - 1)) {
5795 waddstr(view->window, " ");
5796 } else if (blame_line->annotated) {
5798 err = got_object_id_str(&id_str,
5804 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5806 wattr_on(view->window,
5807 COLOR_PAIR(tc->colorpair), NULL);
5808 wprintw(view->window, "%.8s", id_str);
5810 wattr_off(view->window,
5811 COLOR_PAIR(tc->colorpair), NULL);
5813 prev_id = blame_line->id;
5815 waddstr(view->window, "........");
5819 waddstr(view->window, "........");
5823 if (nprinted == s->selected_line - 1)
5824 wstandend(view->window);
5825 waddstr(view->window, " ");
5827 if (view->ncols <= 9) {
5829 } else if (s->first_displayed_line + nprinted ==
5831 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5832 err = add_matched_line(&width, line, view->ncols - 9, 9,
5833 view->window, view->x, regmatch);
5841 err = format_line(&wline, &width, &skip, line,
5842 view->x, view->ncols - 9, 9, 1);
5847 waddwstr(view->window, &wline[skip]);
5853 if (width <= view->ncols - 1)
5854 waddch(view->window, '\n');
5855 if (++nprinted == 1)
5856 s->first_displayed_line = lineno;
5859 s->last_displayed_line = lineno;
5866 static const struct got_error *
5867 blame_cb(void *arg, int nlines, int lineno,
5868 struct got_commit_object *commit, struct got_object_id *id)
5870 const struct got_error *err = NULL;
5871 struct tog_blame_cb_args *a = arg;
5872 struct tog_blame_line *line;
5875 if (nlines != a->nlines ||
5876 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5877 return got_error(GOT_ERR_RANGE);
5879 errcode = pthread_mutex_lock(&tog_mutex);
5881 return got_error_set_errno(errcode, "pthread_mutex_lock");
5883 if (*a->quit) { /* user has quit the blame view */
5884 err = got_error(GOT_ERR_ITER_COMPLETED);
5889 goto done; /* no change in this commit */
5891 line = &a->lines[lineno - 1];
5892 if (line->annotated)
5895 line->id = got_object_id_dup(id);
5896 if (line->id == NULL) {
5897 err = got_error_from_errno("got_object_id_dup");
5900 line->annotated = 1;
5902 errcode = pthread_mutex_unlock(&tog_mutex);
5904 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5909 blame_thread(void *arg)
5911 const struct got_error *err, *close_err;
5912 struct tog_blame_thread_args *ta = arg;
5913 struct tog_blame_cb_args *a = ta->cb_args;
5914 int errcode, fd1 = -1, fd2 = -1;
5915 FILE *f1 = NULL, *f2 = NULL;
5917 fd1 = got_opentempfd();
5919 return (void *)got_error_from_errno("got_opentempfd");
5921 fd2 = got_opentempfd();
5923 err = got_error_from_errno("got_opentempfd");
5927 f1 = got_opentemp();
5929 err = (void *)got_error_from_errno("got_opentemp");
5932 f2 = got_opentemp();
5934 err = (void *)got_error_from_errno("got_opentemp");
5938 err = block_signals_used_by_main_thread();
5942 err = got_blame(ta->path, a->commit_id, ta->repo,
5943 tog_diff_algo, blame_cb, ta->cb_args,
5944 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5945 if (err && err->code == GOT_ERR_CANCELLED)
5948 errcode = pthread_mutex_lock(&tog_mutex);
5950 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5954 close_err = got_repo_close(ta->repo);
5960 errcode = pthread_mutex_unlock(&tog_mutex);
5961 if (errcode && err == NULL)
5962 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5965 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5966 err = got_error_from_errno("close");
5967 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5968 err = got_error_from_errno("close");
5969 if (f1 && fclose(f1) == EOF && err == NULL)
5970 err = got_error_from_errno("fclose");
5971 if (f2 && fclose(f2) == EOF && err == NULL)
5972 err = got_error_from_errno("fclose");
5977 static struct got_object_id *
5978 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5979 int first_displayed_line, int selected_line)
5981 struct tog_blame_line *line;
5986 line = &lines[first_displayed_line - 1 + selected_line - 1];
5987 if (!line->annotated)
5993 static struct got_object_id *
5994 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5997 struct tog_blame_line *line;
5999 if (nlines <= 0 || lineno >= nlines)
6002 line = &lines[lineno - 1];
6003 if (!line->annotated)
6009 static const struct got_error *
6010 stop_blame(struct tog_blame *blame)
6012 const struct got_error *err = NULL;
6015 if (blame->thread) {
6017 errcode = pthread_mutex_unlock(&tog_mutex);
6019 return got_error_set_errno(errcode,
6020 "pthread_mutex_unlock");
6021 errcode = pthread_join(blame->thread, (void **)&err);
6023 return got_error_set_errno(errcode, "pthread_join");
6024 errcode = pthread_mutex_lock(&tog_mutex);
6026 return got_error_set_errno(errcode,
6027 "pthread_mutex_lock");
6028 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6030 blame->thread = 0; //NULL;
6032 if (blame->thread_args.repo) {
6033 const struct got_error *close_err;
6034 close_err = got_repo_close(blame->thread_args.repo);
6037 blame->thread_args.repo = NULL;
6040 if (fclose(blame->f) == EOF && err == NULL)
6041 err = got_error_from_errno("fclose");
6045 for (i = 0; i < blame->nlines; i++)
6046 free(blame->lines[i].id);
6048 blame->lines = NULL;
6050 free(blame->cb_args.commit_id);
6051 blame->cb_args.commit_id = NULL;
6052 if (blame->pack_fds) {
6053 const struct got_error *pack_err =
6054 got_repo_pack_fds_close(blame->pack_fds);
6057 blame->pack_fds = NULL;
6062 static const struct got_error *
6063 cancel_blame_view(void *arg)
6065 const struct got_error *err = NULL;
6069 errcode = pthread_mutex_lock(&tog_mutex);
6071 return got_error_set_errno(errcode,
6072 "pthread_mutex_unlock");
6075 err = got_error(GOT_ERR_CANCELLED);
6077 errcode = pthread_mutex_unlock(&tog_mutex);
6079 return got_error_set_errno(errcode,
6080 "pthread_mutex_lock");
6085 static const struct got_error *
6086 run_blame(struct tog_view *view)
6088 struct tog_blame_view_state *s = &view->state.blame;
6089 struct tog_blame *blame = &s->blame;
6090 const struct got_error *err = NULL;
6091 struct got_commit_object *commit = NULL;
6092 struct got_blob_object *blob = NULL;
6093 struct got_repository *thread_repo = NULL;
6094 struct got_object_id *obj_id = NULL;
6095 int obj_type, fd = -1;
6096 int *pack_fds = NULL;
6098 err = got_object_open_as_commit(&commit, s->repo,
6099 &s->blamed_commit->id);
6103 fd = got_opentempfd();
6105 err = got_error_from_errno("got_opentempfd");
6109 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6113 err = got_object_get_type(&obj_type, s->repo, obj_id);
6117 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6118 err = got_error(GOT_ERR_OBJ_TYPE);
6122 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6125 blame->f = got_opentemp();
6126 if (blame->f == NULL) {
6127 err = got_error_from_errno("got_opentemp");
6130 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6131 &blame->line_offsets, blame->f, blob);
6134 if (blame->nlines == 0) {
6135 s->blame_complete = 1;
6139 /* Don't include \n at EOF in the blame line count. */
6140 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6143 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6144 if (blame->lines == NULL) {
6145 err = got_error_from_errno("calloc");
6149 err = got_repo_pack_fds_open(&pack_fds);
6152 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6157 blame->pack_fds = pack_fds;
6158 blame->cb_args.view = view;
6159 blame->cb_args.lines = blame->lines;
6160 blame->cb_args.nlines = blame->nlines;
6161 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6162 if (blame->cb_args.commit_id == NULL) {
6163 err = got_error_from_errno("got_object_id_dup");
6166 blame->cb_args.quit = &s->done;
6168 blame->thread_args.path = s->path;
6169 blame->thread_args.repo = thread_repo;
6170 blame->thread_args.cb_args = &blame->cb_args;
6171 blame->thread_args.complete = &s->blame_complete;
6172 blame->thread_args.cancel_cb = cancel_blame_view;
6173 blame->thread_args.cancel_arg = &s->done;
6174 s->blame_complete = 0;
6176 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6177 s->first_displayed_line = 1;
6178 s->last_displayed_line = view->nlines;
6179 s->selected_line = 1;
6181 s->matched_line = 0;
6185 got_object_commit_close(commit);
6186 if (fd != -1 && close(fd) == -1 && err == NULL)
6187 err = got_error_from_errno("close");
6189 got_object_blob_close(blob);
6196 static const struct got_error *
6197 open_blame_view(struct tog_view *view, char *path,
6198 struct got_object_id *commit_id, struct got_repository *repo)
6200 const struct got_error *err = NULL;
6201 struct tog_blame_view_state *s = &view->state.blame;
6203 STAILQ_INIT(&s->blamed_commits);
6205 s->path = strdup(path);
6206 if (s->path == NULL)
6207 return got_error_from_errno("strdup");
6209 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6215 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6216 s->first_displayed_line = 1;
6217 s->last_displayed_line = view->nlines;
6218 s->selected_line = 1;
6219 s->blame_complete = 0;
6221 s->commit_id = commit_id;
6222 memset(&s->blame, 0, sizeof(s->blame));
6224 STAILQ_INIT(&s->colors);
6225 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6226 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6227 get_color_value("TOG_COLOR_COMMIT"));
6232 view->show = show_blame_view;
6233 view->input = input_blame_view;
6234 view->reset = reset_blame_view;
6235 view->close = close_blame_view;
6236 view->search_start = search_start_blame_view;
6237 view->search_setup = search_setup_blame_view;
6238 view->search_next = search_next_view_match;
6240 return run_blame(view);
6243 static const struct got_error *
6244 close_blame_view(struct tog_view *view)
6246 const struct got_error *err = NULL;
6247 struct tog_blame_view_state *s = &view->state.blame;
6249 if (s->blame.thread)
6250 err = stop_blame(&s->blame);
6252 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6253 struct got_object_qid *blamed_commit;
6254 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6255 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6256 got_object_qid_free(blamed_commit);
6260 free_colors(&s->colors);
6264 static const struct got_error *
6265 search_start_blame_view(struct tog_view *view)
6267 struct tog_blame_view_state *s = &view->state.blame;
6269 s->matched_line = 0;
6274 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6275 size_t *nlines, int **first, int **last, int **match, int **selected)
6277 struct tog_blame_view_state *s = &view->state.blame;
6280 *nlines = s->blame.nlines;
6281 *line_offsets = s->blame.line_offsets;
6282 *match = &s->matched_line;
6283 *first = &s->first_displayed_line;
6284 *last = &s->last_displayed_line;
6285 *selected = &s->selected_line;
6288 static const struct got_error *
6289 show_blame_view(struct tog_view *view)
6291 const struct got_error *err = NULL;
6292 struct tog_blame_view_state *s = &view->state.blame;
6295 if (s->blame.thread == 0 && !s->blame_complete) {
6296 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6297 &s->blame.thread_args);
6299 return got_error_set_errno(errcode, "pthread_create");
6301 halfdelay(1); /* fast refresh while annotating */
6304 if (s->blame_complete)
6305 halfdelay(10); /* disable fast refresh */
6307 err = draw_blame(view);
6313 static const struct got_error *
6314 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6315 struct got_repository *repo, struct got_object_id *id)
6317 struct tog_view *log_view;
6318 const struct got_error *err = NULL;
6322 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6323 if (log_view == NULL)
6324 return got_error_from_errno("view_open");
6326 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6328 view_close(log_view);
6330 *new_view = log_view;
6335 static const struct got_error *
6336 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6338 const struct got_error *err = NULL, *thread_err = NULL;
6339 struct tog_view *diff_view;
6340 struct tog_blame_view_state *s = &view->state.blame;
6341 int eos, nscroll, begin_y = 0, begin_x = 0;
6343 eos = nscroll = view->nlines - 2;
6344 if (view_is_hsplit_top(view))
6354 horizontal_scroll_input(view, ch);
6361 s->selected_line = 1;
6362 s->first_displayed_line = 1;
6367 if (s->blame.nlines < eos) {
6368 s->selected_line = s->blame.nlines;
6369 s->first_displayed_line = 1;
6371 s->selected_line = eos;
6372 s->first_displayed_line = s->blame.nlines - (eos - 1);
6379 if (s->selected_line > 1)
6381 else if (s->selected_line == 1 &&
6382 s->first_displayed_line > 1)
6383 s->first_displayed_line--;
6394 if (s->first_displayed_line == 1) {
6395 if (view->count > 1)
6397 s->selected_line = MAX(1, s->selected_line - nscroll);
6401 if (s->first_displayed_line > nscroll)
6402 s->first_displayed_line -= nscroll;
6404 s->first_displayed_line = 1;
6409 if (s->selected_line < eos && s->first_displayed_line +
6410 s->selected_line <= s->blame.nlines)
6412 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6413 s->first_displayed_line++;
6419 struct got_object_id *id = NULL;
6422 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6423 s->first_displayed_line, s->selected_line);
6427 struct got_commit_object *commit, *pcommit;
6428 struct got_object_qid *pid;
6429 struct got_object_id *blob_id = NULL;
6431 err = got_object_open_as_commit(&commit,
6436 got_object_commit_get_parent_ids(commit));
6438 got_object_commit_close(commit);
6441 /* Check if path history ends here. */
6442 err = got_object_open_as_commit(&pcommit,
6446 err = got_object_id_by_path(&blob_id, s->repo,
6448 got_object_commit_close(pcommit);
6450 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6452 got_object_commit_close(commit);
6455 err = got_object_get_type(&obj_type, s->repo,
6458 /* Can't blame non-blob type objects. */
6459 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6460 got_object_commit_close(commit);
6463 err = got_object_qid_alloc(&s->blamed_commit,
6465 got_object_commit_close(commit);
6467 if (got_object_id_cmp(id,
6468 &s->blamed_commit->id) == 0)
6470 err = got_object_qid_alloc(&s->blamed_commit,
6476 thread_err = stop_blame(&s->blame);
6480 STAILQ_INSERT_HEAD(&s->blamed_commits,
6481 s->blamed_commit, entry);
6482 err = run_blame(view);
6488 struct got_object_qid *first;
6491 first = STAILQ_FIRST(&s->blamed_commits);
6492 if (!got_object_id_cmp(&first->id, s->commit_id))
6495 thread_err = stop_blame(&s->blame);
6499 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6500 got_object_qid_free(s->blamed_commit);
6502 STAILQ_FIRST(&s->blamed_commits);
6503 err = run_blame(view);
6510 s->id_to_log = get_selected_commit_id(s->blame.lines,
6511 s->blame.nlines, s->first_displayed_line, s->selected_line);
6513 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6517 struct got_object_id *id = NULL;
6518 struct got_object_qid *pid;
6519 struct got_commit_object *commit = NULL;
6522 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6523 s->first_displayed_line, s->selected_line);
6526 err = got_object_open_as_commit(&commit, s->repo, id);
6529 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6531 /* traversed from diff view, release diff resources */
6532 err = close_diff_view(*new_view);
6535 diff_view = *new_view;
6537 if (view_is_parent_view(view))
6538 view_get_split(view, &begin_y, &begin_x);
6540 diff_view = view_open(0, 0, begin_y, begin_x,
6542 if (diff_view == NULL) {
6543 got_object_commit_close(commit);
6544 err = got_error_from_errno("view_open");
6548 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6549 id, NULL, NULL, 3, 0, 0, view, s->repo);
6550 got_object_commit_close(commit);
6552 view_close(diff_view);
6555 s->last_diffed_line = s->first_displayed_line - 1 +
6558 break; /* still open from active diff view */
6559 if (view_is_parent_view(view) &&
6560 view->mode == TOG_VIEW_SPLIT_HRZN) {
6561 err = view_init_hsplit(view, begin_y);
6567 diff_view->focussed = 1;
6568 diff_view->mode = view->mode;
6569 diff_view->nlines = view->lines - begin_y;
6570 if (view_is_parent_view(view)) {
6571 view_transfer_size(diff_view, view);
6572 err = view_close_child(view);
6575 err = view_set_child(view, diff_view);
6578 view->focus_child = 1;
6580 *new_view = diff_view;
6593 if (s->last_displayed_line >= s->blame.nlines &&
6594 s->selected_line >= MIN(s->blame.nlines,
6595 view->nlines - 2)) {
6599 if (s->last_displayed_line >= s->blame.nlines &&
6600 s->selected_line < view->nlines - 2) {
6602 MIN(nscroll, s->last_displayed_line -
6603 s->first_displayed_line - s->selected_line + 1);
6605 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6606 s->first_displayed_line += nscroll;
6608 s->first_displayed_line =
6609 s->blame.nlines - (view->nlines - 3);
6612 if (s->selected_line > view->nlines - 2) {
6613 s->selected_line = MIN(s->blame.nlines,
6621 return thread_err ? thread_err : err;
6624 static const struct got_error *
6625 reset_blame_view(struct tog_view *view)
6627 const struct got_error *err;
6628 struct tog_blame_view_state *s = &view->state.blame;
6632 err = stop_blame(&s->blame);
6636 return run_blame(view);
6639 static const struct got_error *
6640 cmd_blame(int argc, char *argv[])
6642 const struct got_error *error;
6643 struct got_repository *repo = NULL;
6644 struct got_worktree *worktree = NULL;
6645 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6646 char *link_target = NULL;
6647 struct got_object_id *commit_id = NULL;
6648 struct got_commit_object *commit = NULL;
6649 char *commit_id_str = NULL;
6651 struct tog_view *view;
6652 int *pack_fds = NULL;
6654 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6657 commit_id_str = optarg;
6660 repo_path = realpath(optarg, NULL);
6661 if (repo_path == NULL)
6662 return got_error_from_errno2("realpath",
6677 error = got_repo_pack_fds_open(&pack_fds);
6681 if (repo_path == NULL) {
6682 cwd = getcwd(NULL, 0);
6684 return got_error_from_errno("getcwd");
6685 error = got_worktree_open(&worktree, cwd);
6686 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6690 strdup(got_worktree_get_repo_path(worktree));
6692 repo_path = strdup(cwd);
6693 if (repo_path == NULL) {
6694 error = got_error_from_errno("strdup");
6699 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6703 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6710 error = apply_unveil(got_repo_get_path(repo), NULL);
6714 error = tog_load_refs(repo, 0);
6718 if (commit_id_str == NULL) {
6719 struct got_reference *head_ref;
6720 error = got_ref_open(&head_ref, repo, worktree ?
6721 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6724 error = got_ref_resolve(&commit_id, repo, head_ref);
6725 got_ref_close(head_ref);
6727 error = got_repo_match_object_id(&commit_id, NULL,
6728 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6733 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6735 error = got_error_from_errno("view_open");
6739 error = got_object_open_as_commit(&commit, repo, commit_id);
6743 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6748 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6753 /* Release work tree lock. */
6754 got_worktree_close(worktree);
6757 error = view_loop(view);
6765 got_object_commit_close(commit);
6767 got_worktree_close(worktree);
6769 const struct got_error *close_err = got_repo_close(repo);
6774 const struct got_error *pack_err =
6775 got_repo_pack_fds_close(pack_fds);
6783 static const struct got_error *
6784 draw_tree_entries(struct tog_view *view, const char *parent_path)
6786 struct tog_tree_view_state *s = &view->state.tree;
6787 const struct got_error *err = NULL;
6788 struct got_tree_entry *te;
6791 struct tog_color *tc;
6792 int width, n, nentries, scrollx, i = 1;
6793 int limit = view->nlines;
6796 if (view_is_hsplit_top(view))
6797 --limit; /* border */
6799 werase(view->window);
6804 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6808 if (view_needs_focus_indication(view))
6809 wstandout(view->window);
6810 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6812 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6813 waddwstr(view->window, wline);
6816 while (width++ < view->ncols)
6817 waddch(view->window, ' ');
6819 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6820 if (view_needs_focus_indication(view))
6821 wstandend(view->window);
6826 if (s->first_displayed_entry) {
6827 i += got_tree_entry_get_index(s->first_displayed_entry);
6828 if (s->tree != s->root)
6829 ++i; /* account for ".." entry */
6831 nentries = got_object_tree_get_nentries(s->tree);
6832 if (asprintf(&index, "[%d/%d] %s",
6833 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6834 return got_error_from_errno("asprintf");
6835 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6839 waddwstr(view->window, wline);
6842 if (width < view->ncols - 1)
6843 waddch(view->window, '\n');
6846 waddch(view->window, '\n');
6850 if (s->first_displayed_entry == NULL) {
6851 te = got_object_tree_get_first_entry(s->tree);
6852 if (s->selected == 0) {
6854 wstandout(view->window);
6855 s->selected_entry = NULL;
6857 waddstr(view->window, " ..\n"); /* parent directory */
6858 if (s->selected == 0 && view->focussed)
6859 wstandend(view->window);
6866 te = s->first_displayed_entry;
6870 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6871 char *line = NULL, *id_str = NULL, *link_target = NULL;
6872 const char *modestr = "";
6875 te = got_object_tree_get_entry(s->tree, i);
6876 mode = got_tree_entry_get_mode(te);
6879 err = got_object_id_str(&id_str,
6880 got_tree_entry_get_id(te));
6882 return got_error_from_errno(
6883 "got_object_id_str");
6885 if (got_object_tree_entry_is_submodule(te))
6887 else if (S_ISLNK(mode)) {
6890 err = got_tree_entry_get_symlink_target(&link_target,
6896 for (i = 0; i < strlen(link_target); i++) {
6897 if (!isprint((unsigned char)link_target[i]))
6898 link_target[i] = '?';
6902 else if (S_ISDIR(mode))
6904 else if (mode & S_IXUSR)
6906 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6907 got_tree_entry_get_name(te), modestr,
6908 link_target ? " -> ": "",
6909 link_target ? link_target : "") == -1) {
6912 return got_error_from_errno("asprintf");
6917 /* use full line width to determine view->maxx */
6918 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6923 view->maxx = MAX(view->maxx, width);
6927 err = format_line(&wline, &width, &scrollx, line, view->x,
6933 if (n == s->selected) {
6935 wstandout(view->window);
6936 s->selected_entry = te;
6938 tc = match_color(&s->colors, line);
6940 wattr_on(view->window,
6941 COLOR_PAIR(tc->colorpair), NULL);
6942 waddwstr(view->window, &wline[scrollx]);
6944 wattr_off(view->window,
6945 COLOR_PAIR(tc->colorpair), NULL);
6946 if (width < view->ncols)
6947 waddch(view->window, '\n');
6948 if (n == s->selected && view->focussed)
6949 wstandend(view->window);
6955 s->last_displayed_entry = te;
6964 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6966 struct got_tree_entry *te;
6967 int isroot = s->tree == s->root;
6970 if (s->first_displayed_entry == NULL)
6973 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6974 while (i++ < maxscroll) {
6977 s->first_displayed_entry = NULL;
6980 s->first_displayed_entry = te;
6981 te = got_tree_entry_get_prev(s->tree, te);
6985 static const struct got_error *
6986 tree_scroll_down(struct tog_view *view, int maxscroll)
6988 struct tog_tree_view_state *s = &view->state.tree;
6989 struct got_tree_entry *next, *last;
6992 if (s->first_displayed_entry)
6993 next = got_tree_entry_get_next(s->tree,
6994 s->first_displayed_entry);
6996 next = got_object_tree_get_first_entry(s->tree);
6998 last = s->last_displayed_entry;
6999 while (next && n++ < maxscroll) {
7001 s->last_displayed_entry = last;
7002 last = got_tree_entry_get_next(s->tree, last);
7004 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7005 s->first_displayed_entry = next;
7006 next = got_tree_entry_get_next(s->tree, next);
7013 static const struct got_error *
7014 tree_entry_path(char **path, struct tog_parent_trees *parents,
7015 struct got_tree_entry *te)
7017 const struct got_error *err = NULL;
7018 struct tog_parent_tree *pt;
7019 size_t len = 2; /* for leading slash and NUL */
7021 TAILQ_FOREACH(pt, parents, entry)
7022 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7025 len += strlen(got_tree_entry_get_name(te));
7027 *path = calloc(1, len);
7029 return got_error_from_errno("calloc");
7032 pt = TAILQ_LAST(parents, tog_parent_trees);
7034 const char *name = got_tree_entry_get_name(pt->selected_entry);
7035 if (strlcat(*path, name, len) >= len) {
7036 err = got_error(GOT_ERR_NO_SPACE);
7039 if (strlcat(*path, "/", len) >= len) {
7040 err = got_error(GOT_ERR_NO_SPACE);
7043 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7046 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7047 err = got_error(GOT_ERR_NO_SPACE);
7059 static const struct got_error *
7060 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7061 struct got_tree_entry *te, struct tog_parent_trees *parents,
7062 struct got_object_id *commit_id, struct got_repository *repo)
7064 const struct got_error *err = NULL;
7066 struct tog_view *blame_view;
7070 err = tree_entry_path(&path, parents, te);
7074 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7075 if (blame_view == NULL) {
7076 err = got_error_from_errno("view_open");
7080 err = open_blame_view(blame_view, path, commit_id, repo);
7082 if (err->code == GOT_ERR_CANCELLED)
7084 view_close(blame_view);
7086 *new_view = blame_view;
7092 static const struct got_error *
7093 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7094 struct tog_tree_view_state *s)
7096 struct tog_view *log_view;
7097 const struct got_error *err = NULL;
7102 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7103 if (log_view == NULL)
7104 return got_error_from_errno("view_open");
7106 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7110 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7113 view_close(log_view);
7115 *new_view = log_view;
7120 static const struct got_error *
7121 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7122 const char *head_ref_name, struct got_repository *repo)
7124 const struct got_error *err = NULL;
7125 char *commit_id_str = NULL;
7126 struct tog_tree_view_state *s = &view->state.tree;
7127 struct got_commit_object *commit = NULL;
7129 TAILQ_INIT(&s->parents);
7130 STAILQ_INIT(&s->colors);
7132 s->commit_id = got_object_id_dup(commit_id);
7133 if (s->commit_id == NULL)
7134 return got_error_from_errno("got_object_id_dup");
7136 err = got_object_open_as_commit(&commit, repo, commit_id);
7141 * The root is opened here and will be closed when the view is closed.
7142 * Any visited subtrees and their path-wise parents are opened and
7145 err = got_object_open_as_tree(&s->root, repo,
7146 got_object_commit_get_tree_id(commit));
7151 err = got_object_id_str(&commit_id_str, commit_id);
7155 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7156 err = got_error_from_errno("asprintf");
7160 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7161 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7162 if (head_ref_name) {
7163 s->head_ref_name = strdup(head_ref_name);
7164 if (s->head_ref_name == NULL) {
7165 err = got_error_from_errno("strdup");
7171 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7172 err = add_color(&s->colors, "\\$$",
7173 TOG_COLOR_TREE_SUBMODULE,
7174 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7177 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7178 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7181 err = add_color(&s->colors, "/$",
7182 TOG_COLOR_TREE_DIRECTORY,
7183 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7187 err = add_color(&s->colors, "\\*$",
7188 TOG_COLOR_TREE_EXECUTABLE,
7189 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7193 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7194 get_color_value("TOG_COLOR_COMMIT"));
7199 view->show = show_tree_view;
7200 view->input = input_tree_view;
7201 view->close = close_tree_view;
7202 view->search_start = search_start_tree_view;
7203 view->search_next = search_next_tree_view;
7205 free(commit_id_str);
7207 got_object_commit_close(commit);
7209 close_tree_view(view);
7213 static const struct got_error *
7214 close_tree_view(struct tog_view *view)
7216 struct tog_tree_view_state *s = &view->state.tree;
7218 free_colors(&s->colors);
7219 free(s->tree_label);
7220 s->tree_label = NULL;
7222 s->commit_id = NULL;
7223 free(s->head_ref_name);
7224 s->head_ref_name = NULL;
7225 while (!TAILQ_EMPTY(&s->parents)) {
7226 struct tog_parent_tree *parent;
7227 parent = TAILQ_FIRST(&s->parents);
7228 TAILQ_REMOVE(&s->parents, parent, entry);
7229 if (parent->tree != s->root)
7230 got_object_tree_close(parent->tree);
7234 if (s->tree != NULL && s->tree != s->root)
7235 got_object_tree_close(s->tree);
7237 got_object_tree_close(s->root);
7241 static const struct got_error *
7242 search_start_tree_view(struct tog_view *view)
7244 struct tog_tree_view_state *s = &view->state.tree;
7246 s->matched_entry = NULL;
7251 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7253 regmatch_t regmatch;
7255 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7259 static const struct got_error *
7260 search_next_tree_view(struct tog_view *view)
7262 struct tog_tree_view_state *s = &view->state.tree;
7263 struct got_tree_entry *te = NULL;
7265 if (!view->searching) {
7266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7270 if (s->matched_entry) {
7271 if (view->searching == TOG_SEARCH_FORWARD) {
7272 if (s->selected_entry)
7273 te = got_tree_entry_get_next(s->tree,
7276 te = got_object_tree_get_first_entry(s->tree);
7278 if (s->selected_entry == NULL)
7279 te = got_object_tree_get_last_entry(s->tree);
7281 te = got_tree_entry_get_prev(s->tree,
7285 if (s->selected_entry)
7286 te = s->selected_entry;
7287 else if (view->searching == TOG_SEARCH_FORWARD)
7288 te = got_object_tree_get_first_entry(s->tree);
7290 te = got_object_tree_get_last_entry(s->tree);
7295 if (s->matched_entry == NULL) {
7296 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7299 if (view->searching == TOG_SEARCH_FORWARD)
7300 te = got_object_tree_get_first_entry(s->tree);
7302 te = got_object_tree_get_last_entry(s->tree);
7305 if (match_tree_entry(te, &view->regex)) {
7306 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7307 s->matched_entry = te;
7311 if (view->searching == TOG_SEARCH_FORWARD)
7312 te = got_tree_entry_get_next(s->tree, te);
7314 te = got_tree_entry_get_prev(s->tree, te);
7317 if (s->matched_entry) {
7318 s->first_displayed_entry = s->matched_entry;
7325 static const struct got_error *
7326 show_tree_view(struct tog_view *view)
7328 const struct got_error *err = NULL;
7329 struct tog_tree_view_state *s = &view->state.tree;
7332 err = tree_entry_path(&parent_path, &s->parents, NULL);
7336 err = draw_tree_entries(view, parent_path);
7343 static const struct got_error *
7344 tree_goto_line(struct tog_view *view, int nlines)
7346 const struct got_error *err = NULL;
7347 struct tog_tree_view_state *s = &view->state.tree;
7348 struct got_tree_entry **fte, **lte, **ste;
7349 int g, last, first = 1, i = 1;
7350 int root = s->tree == s->root;
7351 int off = root ? 1 : 2;
7358 else if (g > got_object_tree_get_nentries(s->tree))
7359 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7361 fte = &s->first_displayed_entry;
7362 lte = &s->last_displayed_entry;
7363 ste = &s->selected_entry;
7366 first = got_tree_entry_get_index(*fte);
7367 first += off; /* account for ".." */
7369 last = got_tree_entry_get_index(*lte);
7372 if (g >= first && g <= last && g - first < nlines) {
7373 s->selected = g - first;
7374 return NULL; /* gline is on the current page */
7378 i = got_tree_entry_get_index(*ste);
7383 err = tree_scroll_down(view, g - i);
7386 if (got_tree_entry_get_index(*lte) >=
7387 got_object_tree_get_nentries(s->tree) - 1 &&
7388 first + s->selected < g &&
7389 s->selected < s->ndisplayed - 1) {
7390 first = got_tree_entry_get_index(*fte);
7392 s->selected = g - first;
7395 tree_scroll_up(s, i - g);
7398 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7399 s->selected = g - 1;
7404 static const struct got_error *
7405 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7407 const struct got_error *err = NULL;
7408 struct tog_tree_view_state *s = &view->state.tree;
7409 struct got_tree_entry *te;
7410 int n, nscroll = view->nlines - 3;
7413 return tree_goto_line(view, nscroll);
7422 horizontal_scroll_input(view, ch);
7425 s->show_ids = !s->show_ids;
7430 if (!s->selected_entry)
7432 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7436 err = view_request_new(new_view, view, TOG_VIEW_REF);
7443 if (s->tree == s->root)
7444 s->first_displayed_entry =
7445 got_object_tree_get_first_entry(s->tree);
7447 s->first_displayed_entry = NULL;
7452 int eos = view->nlines - 3;
7454 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7458 te = got_object_tree_get_last_entry(s->tree);
7459 for (n = 0; n < eos; n++) {
7461 if (s->tree != s->root) {
7462 s->first_displayed_entry = NULL;
7467 s->first_displayed_entry = te;
7468 te = got_tree_entry_get_prev(s->tree, te);
7471 s->selected = n - 1;
7477 if (s->selected > 0) {
7481 tree_scroll_up(s, 1);
7482 if (s->selected_entry == NULL ||
7483 (s->tree == s->root && s->selected_entry ==
7484 got_object_tree_get_first_entry(s->tree)))
7494 if (s->tree == s->root) {
7495 if (got_object_tree_get_first_entry(s->tree) ==
7496 s->first_displayed_entry)
7497 s->selected -= MIN(s->selected, nscroll);
7499 if (s->first_displayed_entry == NULL)
7500 s->selected -= MIN(s->selected, nscroll);
7502 tree_scroll_up(s, MAX(0, nscroll));
7503 if (s->selected_entry == NULL ||
7504 (s->tree == s->root && s->selected_entry ==
7505 got_object_tree_get_first_entry(s->tree)))
7511 if (s->selected < s->ndisplayed - 1) {
7515 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7517 /* can't scroll any further */
7521 tree_scroll_down(view, 1);
7531 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7533 /* can't scroll any further; move cursor down */
7534 if (s->selected < s->ndisplayed - 1)
7535 s->selected += MIN(nscroll,
7536 s->ndisplayed - s->selected - 1);
7541 tree_scroll_down(view, nscroll);
7546 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7547 struct tog_parent_tree *parent;
7548 /* user selected '..' */
7549 if (s->tree == s->root) {
7553 parent = TAILQ_FIRST(&s->parents);
7554 TAILQ_REMOVE(&s->parents, parent,
7556 got_object_tree_close(s->tree);
7557 s->tree = parent->tree;
7558 s->first_displayed_entry =
7559 parent->first_displayed_entry;
7561 parent->selected_entry;
7562 s->selected = parent->selected;
7563 if (s->selected > view->nlines - 3) {
7564 err = offset_selection_down(view);
7569 } else if (S_ISDIR(got_tree_entry_get_mode(
7570 s->selected_entry))) {
7571 struct got_tree_object *subtree;
7573 err = got_object_open_as_tree(&subtree, s->repo,
7574 got_tree_entry_get_id(s->selected_entry));
7577 err = tree_view_visit_subtree(s, subtree);
7579 got_object_tree_close(subtree);
7582 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7583 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7586 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7587 s->selected = view->nlines - 4;
7603 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7608 static const struct got_error *
7609 cmd_tree(int argc, char *argv[])
7611 const struct got_error *error;
7612 struct got_repository *repo = NULL;
7613 struct got_worktree *worktree = NULL;
7614 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7615 struct got_object_id *commit_id = NULL;
7616 struct got_commit_object *commit = NULL;
7617 const char *commit_id_arg = NULL;
7619 struct got_reference *ref = NULL;
7620 const char *head_ref_name = NULL;
7622 struct tog_view *view;
7623 int *pack_fds = NULL;
7625 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7628 commit_id_arg = optarg;
7631 repo_path = realpath(optarg, NULL);
7632 if (repo_path == NULL)
7633 return got_error_from_errno2("realpath",
7648 error = got_repo_pack_fds_open(&pack_fds);
7652 if (repo_path == NULL) {
7653 cwd = getcwd(NULL, 0);
7655 return got_error_from_errno("getcwd");
7656 error = got_worktree_open(&worktree, cwd);
7657 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7661 strdup(got_worktree_get_repo_path(worktree));
7663 repo_path = strdup(cwd);
7664 if (repo_path == NULL) {
7665 error = got_error_from_errno("strdup");
7670 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7674 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7681 error = apply_unveil(got_repo_get_path(repo), NULL);
7685 error = tog_load_refs(repo, 0);
7689 if (commit_id_arg == NULL) {
7690 error = got_repo_match_object_id(&commit_id, &label,
7691 worktree ? got_worktree_get_head_ref_name(worktree) :
7692 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7695 head_ref_name = label;
7697 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7699 head_ref_name = got_ref_get_name(ref);
7700 else if (error->code != GOT_ERR_NOT_REF)
7702 error = got_repo_match_object_id(&commit_id, NULL,
7703 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7708 error = got_object_open_as_commit(&commit, repo, commit_id);
7712 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7714 error = got_error_from_errno("view_open");
7717 error = open_tree_view(view, commit_id, head_ref_name, repo);
7720 if (!got_path_is_root_dir(in_repo_path)) {
7721 error = tree_view_walk_path(&view->state.tree, commit,
7728 /* Release work tree lock. */
7729 got_worktree_close(worktree);
7732 error = view_loop(view);
7741 const struct got_error *close_err = got_repo_close(repo);
7746 const struct got_error *pack_err =
7747 got_repo_pack_fds_close(pack_fds);
7755 static const struct got_error *
7756 ref_view_load_refs(struct tog_ref_view_state *s)
7758 struct got_reflist_entry *sre;
7759 struct tog_reflist_entry *re;
7762 TAILQ_FOREACH(sre, &tog_refs, entry) {
7763 if (strncmp(got_ref_get_name(sre->ref),
7764 "refs/got/", 9) == 0 &&
7765 strncmp(got_ref_get_name(sre->ref),
7766 "refs/got/backup/", 16) != 0)
7769 re = malloc(sizeof(*re));
7771 return got_error_from_errno("malloc");
7773 re->ref = got_ref_dup(sre->ref);
7774 if (re->ref == NULL)
7775 return got_error_from_errno("got_ref_dup");
7776 re->idx = s->nrefs++;
7777 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7780 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7785 ref_view_free_refs(struct tog_ref_view_state *s)
7787 struct tog_reflist_entry *re;
7789 while (!TAILQ_EMPTY(&s->refs)) {
7790 re = TAILQ_FIRST(&s->refs);
7791 TAILQ_REMOVE(&s->refs, re, entry);
7792 got_ref_close(re->ref);
7797 static const struct got_error *
7798 open_ref_view(struct tog_view *view, struct got_repository *repo)
7800 const struct got_error *err = NULL;
7801 struct tog_ref_view_state *s = &view->state.ref;
7803 s->selected_entry = 0;
7806 TAILQ_INIT(&s->refs);
7807 STAILQ_INIT(&s->colors);
7809 err = ref_view_load_refs(s);
7813 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7814 err = add_color(&s->colors, "^refs/heads/",
7815 TOG_COLOR_REFS_HEADS,
7816 get_color_value("TOG_COLOR_REFS_HEADS"));
7820 err = add_color(&s->colors, "^refs/tags/",
7821 TOG_COLOR_REFS_TAGS,
7822 get_color_value("TOG_COLOR_REFS_TAGS"));
7826 err = add_color(&s->colors, "^refs/remotes/",
7827 TOG_COLOR_REFS_REMOTES,
7828 get_color_value("TOG_COLOR_REFS_REMOTES"));
7832 err = add_color(&s->colors, "^refs/got/backup/",
7833 TOG_COLOR_REFS_BACKUP,
7834 get_color_value("TOG_COLOR_REFS_BACKUP"));
7839 view->show = show_ref_view;
7840 view->input = input_ref_view;
7841 view->close = close_ref_view;
7842 view->search_start = search_start_ref_view;
7843 view->search_next = search_next_ref_view;
7846 free_colors(&s->colors);
7850 static const struct got_error *
7851 close_ref_view(struct tog_view *view)
7853 struct tog_ref_view_state *s = &view->state.ref;
7855 ref_view_free_refs(s);
7856 free_colors(&s->colors);
7861 static const struct got_error *
7862 resolve_reflist_entry(struct got_object_id **commit_id,
7863 struct tog_reflist_entry *re, struct got_repository *repo)
7865 const struct got_error *err = NULL;
7866 struct got_object_id *obj_id;
7867 struct got_tag_object *tag = NULL;
7872 err = got_ref_resolve(&obj_id, repo, re->ref);
7876 err = got_object_get_type(&obj_type, repo, obj_id);
7881 case GOT_OBJ_TYPE_COMMIT:
7882 *commit_id = obj_id;
7884 case GOT_OBJ_TYPE_TAG:
7885 err = got_object_open_as_tag(&tag, repo, obj_id);
7889 err = got_object_get_type(&obj_type, repo,
7890 got_object_tag_get_object_id(tag));
7893 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7894 err = got_error(GOT_ERR_OBJ_TYPE);
7897 *commit_id = got_object_id_dup(
7898 got_object_tag_get_object_id(tag));
7899 if (*commit_id == NULL) {
7900 err = got_error_from_errno("got_object_id_dup");
7905 err = got_error(GOT_ERR_OBJ_TYPE);
7911 got_object_tag_close(tag);
7919 static const struct got_error *
7920 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7921 struct tog_reflist_entry *re, struct got_repository *repo)
7923 struct tog_view *log_view;
7924 const struct got_error *err = NULL;
7925 struct got_object_id *commit_id = NULL;
7929 err = resolve_reflist_entry(&commit_id, re, repo);
7931 if (err->code != GOT_ERR_OBJ_TYPE)
7937 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7938 if (log_view == NULL) {
7939 err = got_error_from_errno("view_open");
7943 err = open_log_view(log_view, commit_id, repo,
7944 got_ref_get_name(re->ref), "", 0);
7947 view_close(log_view);
7949 *new_view = log_view;
7955 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7957 struct tog_reflist_entry *re;
7960 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7963 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7964 while (i++ < maxscroll) {
7967 s->first_displayed_entry = re;
7968 re = TAILQ_PREV(re, tog_reflist_head, entry);
7972 static const struct got_error *
7973 ref_scroll_down(struct tog_view *view, int maxscroll)
7975 struct tog_ref_view_state *s = &view->state.ref;
7976 struct tog_reflist_entry *next, *last;
7979 if (s->first_displayed_entry)
7980 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7982 next = TAILQ_FIRST(&s->refs);
7984 last = s->last_displayed_entry;
7985 while (next && n++ < maxscroll) {
7987 s->last_displayed_entry = last;
7988 last = TAILQ_NEXT(last, entry);
7990 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7991 s->first_displayed_entry = next;
7992 next = TAILQ_NEXT(next, entry);
7999 static const struct got_error *
8000 search_start_ref_view(struct tog_view *view)
8002 struct tog_ref_view_state *s = &view->state.ref;
8004 s->matched_entry = NULL;
8009 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8011 regmatch_t regmatch;
8013 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8017 static const struct got_error *
8018 search_next_ref_view(struct tog_view *view)
8020 struct tog_ref_view_state *s = &view->state.ref;
8021 struct tog_reflist_entry *re = NULL;
8023 if (!view->searching) {
8024 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8028 if (s->matched_entry) {
8029 if (view->searching == TOG_SEARCH_FORWARD) {
8030 if (s->selected_entry)
8031 re = TAILQ_NEXT(s->selected_entry, entry);
8033 re = TAILQ_PREV(s->selected_entry,
8034 tog_reflist_head, entry);
8036 if (s->selected_entry == NULL)
8037 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8039 re = TAILQ_PREV(s->selected_entry,
8040 tog_reflist_head, entry);
8043 if (s->selected_entry)
8044 re = s->selected_entry;
8045 else if (view->searching == TOG_SEARCH_FORWARD)
8046 re = TAILQ_FIRST(&s->refs);
8048 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8053 if (s->matched_entry == NULL) {
8054 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8057 if (view->searching == TOG_SEARCH_FORWARD)
8058 re = TAILQ_FIRST(&s->refs);
8060 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8063 if (match_reflist_entry(re, &view->regex)) {
8064 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8065 s->matched_entry = re;
8069 if (view->searching == TOG_SEARCH_FORWARD)
8070 re = TAILQ_NEXT(re, entry);
8072 re = TAILQ_PREV(re, tog_reflist_head, entry);
8075 if (s->matched_entry) {
8076 s->first_displayed_entry = s->matched_entry;
8083 static const struct got_error *
8084 show_ref_view(struct tog_view *view)
8086 const struct got_error *err = NULL;
8087 struct tog_ref_view_state *s = &view->state.ref;
8088 struct tog_reflist_entry *re;
8091 struct tog_color *tc;
8092 int width, n, scrollx;
8093 int limit = view->nlines;
8095 werase(view->window);
8098 if (view_is_hsplit_top(view))
8099 --limit; /* border */
8104 re = s->first_displayed_entry;
8106 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8108 return got_error_from_errno("asprintf");
8110 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8115 if (view_needs_focus_indication(view))
8116 wstandout(view->window);
8117 waddwstr(view->window, wline);
8118 while (width++ < view->ncols)
8119 waddch(view->window, ' ');
8120 if (view_needs_focus_indication(view))
8121 wstandend(view->window);
8131 while (re && limit > 0) {
8133 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8136 struct got_commit_object *ci;
8137 struct got_tag_object *tag;
8138 struct got_object_id *id;
8142 err = got_ref_resolve(&id, s->repo, re->ref);
8145 err = got_object_open_as_tag(&tag, s->repo, id);
8147 if (err->code != GOT_ERR_OBJ_TYPE) {
8151 err = got_object_open_as_commit(&ci, s->repo,
8157 t = got_object_commit_get_committer_time(ci);
8158 got_object_commit_close(ci);
8160 t = got_object_tag_get_tagger_time(tag);
8161 got_object_tag_close(tag);
8164 if (gmtime_r(&t, &tm) == NULL)
8165 return got_error_from_errno("gmtime_r");
8166 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8167 return got_error(GOT_ERR_NO_SPACE);
8169 if (got_ref_is_symbolic(re->ref)) {
8170 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8171 ymd : "", got_ref_get_name(re->ref),
8172 got_ref_get_symref_target(re->ref)) == -1)
8173 return got_error_from_errno("asprintf");
8174 } else if (s->show_ids) {
8175 struct got_object_id *id;
8177 err = got_ref_resolve(&id, s->repo, re->ref);
8180 err = got_object_id_str(&id_str, id);
8185 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8186 got_ref_get_name(re->ref), id_str) == -1) {
8187 err = got_error_from_errno("asprintf");
8194 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8195 got_ref_get_name(re->ref)) == -1)
8196 return got_error_from_errno("asprintf");
8198 /* use full line width to determine view->maxx */
8199 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8204 view->maxx = MAX(view->maxx, width);
8208 err = format_line(&wline, &width, &scrollx, line, view->x,
8214 if (n == s->selected) {
8216 wstandout(view->window);
8217 s->selected_entry = re;
8219 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8221 wattr_on(view->window,
8222 COLOR_PAIR(tc->colorpair), NULL);
8223 waddwstr(view->window, &wline[scrollx]);
8225 wattr_off(view->window,
8226 COLOR_PAIR(tc->colorpair), NULL);
8227 if (width < view->ncols)
8228 waddch(view->window, '\n');
8229 if (n == s->selected && view->focussed)
8230 wstandend(view->window);
8236 s->last_displayed_entry = re;
8239 re = TAILQ_NEXT(re, entry);
8246 static const struct got_error *
8247 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8248 struct tog_reflist_entry *re, struct got_repository *repo)
8250 const struct got_error *err = NULL;
8251 struct got_object_id *commit_id = NULL;
8252 struct tog_view *tree_view;
8256 err = resolve_reflist_entry(&commit_id, re, repo);
8258 if (err->code != GOT_ERR_OBJ_TYPE)
8265 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8266 if (tree_view == NULL) {
8267 err = got_error_from_errno("view_open");
8271 err = open_tree_view(tree_view, commit_id,
8272 got_ref_get_name(re->ref), repo);
8276 *new_view = tree_view;
8282 static const struct got_error *
8283 ref_goto_line(struct tog_view *view, int nlines)
8285 const struct got_error *err = NULL;
8286 struct tog_ref_view_state *s = &view->state.ref;
8287 int g, idx = s->selected_entry->idx;
8294 else if (g > s->nrefs)
8297 if (g >= s->first_displayed_entry->idx + 1 &&
8298 g <= s->last_displayed_entry->idx + 1 &&
8299 g - s->first_displayed_entry->idx - 1 < nlines) {
8300 s->selected = g - s->first_displayed_entry->idx - 1;
8305 err = ref_scroll_down(view, g - idx - 1);
8308 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8309 s->first_displayed_entry->idx + s->selected < g &&
8310 s->selected < s->ndisplayed - 1)
8311 s->selected = g - s->first_displayed_entry->idx - 1;
8312 } else if (idx + 1 > g)
8313 ref_scroll_up(s, idx - g + 1);
8315 if (g < nlines && s->first_displayed_entry->idx == 0)
8316 s->selected = g - 1;
8322 static const struct got_error *
8323 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8325 const struct got_error *err = NULL;
8326 struct tog_ref_view_state *s = &view->state.ref;
8327 struct tog_reflist_entry *re;
8328 int n, nscroll = view->nlines - 1;
8331 return ref_goto_line(view, nscroll);
8340 horizontal_scroll_input(view, ch);
8343 s->show_ids = !s->show_ids;
8347 s->show_date = !s->show_date;
8351 s->sort_by_date = !s->sort_by_date;
8352 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8354 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8355 got_ref_cmp_by_commit_timestamp_descending :
8356 tog_ref_cmp_by_name, s->repo);
8359 got_reflist_object_id_map_free(tog_refs_idmap);
8360 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8361 &tog_refs, s->repo);
8364 ref_view_free_refs(s);
8365 err = ref_view_load_refs(s);
8370 if (!s->selected_entry)
8372 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8376 if (!s->selected_entry)
8378 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8385 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8390 int eos = view->nlines - 1;
8392 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8396 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8397 for (n = 0; n < eos; n++) {
8400 s->first_displayed_entry = re;
8401 re = TAILQ_PREV(re, tog_reflist_head, entry);
8404 s->selected = n - 1;
8410 if (s->selected > 0) {
8414 ref_scroll_up(s, 1);
8415 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8425 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8426 s->selected -= MIN(nscroll, s->selected);
8427 ref_scroll_up(s, MAX(0, nscroll));
8428 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8434 if (s->selected < s->ndisplayed - 1) {
8438 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8439 /* can't scroll any further */
8443 ref_scroll_down(view, 1);
8453 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8454 /* can't scroll any further; move cursor down */
8455 if (s->selected < s->ndisplayed - 1)
8456 s->selected += MIN(nscroll,
8457 s->ndisplayed - s->selected - 1);
8458 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8459 s->selected += s->ndisplayed - s->selected - 1;
8463 ref_scroll_down(view, nscroll);
8468 err = tog_load_refs(s->repo, s->sort_by_date);
8471 ref_view_free_refs(s);
8472 err = ref_view_load_refs(s);
8475 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8476 s->selected = view->nlines - 2;
8490 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8495 static const struct got_error *
8496 cmd_ref(int argc, char *argv[])
8498 const struct got_error *error;
8499 struct got_repository *repo = NULL;
8500 struct got_worktree *worktree = NULL;
8501 char *cwd = NULL, *repo_path = NULL;
8503 struct tog_view *view;
8504 int *pack_fds = NULL;
8506 while ((ch = getopt(argc, argv, "r:")) != -1) {
8509 repo_path = realpath(optarg, NULL);
8510 if (repo_path == NULL)
8511 return got_error_from_errno2("realpath",
8526 error = got_repo_pack_fds_open(&pack_fds);
8530 if (repo_path == NULL) {
8531 cwd = getcwd(NULL, 0);
8533 return got_error_from_errno("getcwd");
8534 error = got_worktree_open(&worktree, cwd);
8535 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8539 strdup(got_worktree_get_repo_path(worktree));
8541 repo_path = strdup(cwd);
8542 if (repo_path == NULL) {
8543 error = got_error_from_errno("strdup");
8548 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8554 error = apply_unveil(got_repo_get_path(repo), NULL);
8558 error = tog_load_refs(repo, 0);
8562 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8564 error = got_error_from_errno("view_open");
8568 error = open_ref_view(view, repo);
8573 /* Release work tree lock. */
8574 got_worktree_close(worktree);
8577 error = view_loop(view);
8582 const struct got_error *close_err = got_repo_close(repo);
8587 const struct got_error *pack_err =
8588 got_repo_pack_fds_close(pack_fds);
8596 static const struct got_error*
8597 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8606 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8610 if (mvwprintw(win, y, x, "%s", str) == ERR)
8611 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8618 static const struct got_error *
8619 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8623 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8625 free(*line_offsets);
8626 *line_offsets = NULL;
8627 return got_error_from_errno("reallocarray");
8631 (*line_offsets)[*nlines] = off;
8636 static const struct got_error *
8637 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8641 for (;n > 0; --n, ++km) {
8645 if (km->keys == NULL)
8648 t = t0 = strdup(km->keys);
8650 return got_error_from_errno("strdup");
8653 while ((k = strsep(&t, " ")) != NULL)
8654 len += strlen(k) > 1 ? 2 : 0;
8656 *ret = MAX(*ret, len);
8663 * Write keymap section headers, keys, and key info in km to f.
8664 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8665 * wrap control and symbolic keys in guillemets, else use <>.
8667 static const struct got_error *
8668 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8673 static const char *u8_glyph[] = {
8674 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8675 "\xe2\x80\xba" /* U+203A (utf8 >) */
8678 int cs, s, first = 1;
8680 cs = got_locale_is_utf8();
8682 t = t0 = strdup(km->keys);
8684 return got_error_from_errno("strdup");
8686 len = strlen(km->keys);
8687 while ((k = strsep(&t, " ")) != NULL) {
8688 s = strlen(k) > 1; /* control or symbolic key */
8689 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8690 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8691 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8694 return got_error_from_errno("fprintf");
8702 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8704 return got_error_from_errno("fprintf");
8710 static const struct got_error *
8711 format_help(struct tog_help_view_state *s)
8713 const struct got_error *err = NULL;
8715 int i, max, n, show = s->all;
8716 static const struct tog_key_map km[] = {
8717 #define KEYMAP_(info, type) { NULL, (info), type }
8718 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8724 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8729 err = max_key_str(&max, km, n);
8733 for (i = 0; i < n; ++i) {
8734 if (km[i].keys == NULL) {
8736 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8737 km[i].type == s->type || s->all)
8741 err = format_help_line(&off, s->f, &km[i], max);
8744 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8751 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8755 static const struct got_error *
8756 create_help(struct tog_help_view_state *s)
8759 const struct got_error *err;
8761 free(s->line_offsets);
8762 s->line_offsets = NULL;
8767 return got_error_from_errno("got_opentemp");
8770 err = format_help(s);
8774 if (s->f && fflush(s->f) != 0)
8775 return got_error_from_errno("fflush");
8780 static const struct got_error *
8781 search_start_help_view(struct tog_view *view)
8783 view->state.help.matched_line = 0;
8788 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8789 size_t *nlines, int **first, int **last, int **match, int **selected)
8791 struct tog_help_view_state *s = &view->state.help;
8794 *nlines = s->nlines;
8795 *line_offsets = s->line_offsets;
8796 *match = &s->matched_line;
8797 *first = &s->first_displayed_line;
8798 *last = &s->last_displayed_line;
8799 *selected = &s->selected_line;
8802 static const struct got_error *
8803 show_help_view(struct tog_view *view)
8805 struct tog_help_view_state *s = &view->state.help;
8806 const struct got_error *err;
8807 regmatch_t *regmatch = &view->regmatch;
8812 int width, nprinted = 0, rc = 0;
8813 int eos = view->nlines;
8815 if (view_is_hsplit_top(view))
8816 --eos; /* account for border */
8820 werase(view->window);
8822 if (view->gline > s->nlines - 1)
8823 view->gline = s->nlines - 1;
8825 err = win_draw_center(view->window, 0, 0, view->ncols,
8826 view_needs_focus_indication(view),
8827 "tog help (press q to return to tog)");
8832 waddstr(view->window, "\n\n");
8838 while (eos > 0 && nprinted < eos) {
8841 linelen = getline(&line, &linesz, s->f);
8842 if (linelen == -1) {
8845 return got_ferror(s->f, GOT_ERR_IO);
8850 if (++s->lineno < s->first_displayed_line)
8852 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8854 if (s->lineno == view->hiline)
8857 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8863 view->maxx = MAX(view->maxx, width);
8868 wattron(view->window, attr);
8869 if (s->first_displayed_line + nprinted == s->matched_line &&
8870 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8871 err = add_matched_line(&width, line, view->ncols - 1, 0,
8872 view->window, view->x, regmatch);
8880 err = format_line(&wline, &width, &skip, line,
8881 view->x, view->ncols, 0, view->x ? 1 : 0);
8886 waddwstr(view->window, &wline[skip]);
8890 if (s->lineno == view->hiline) {
8891 while (width++ < view->ncols)
8892 waddch(view->window, ' ');
8894 if (width < view->ncols)
8895 waddch(view->window, '\n');
8898 wattroff(view->window, attr);
8899 if (++nprinted == 1)
8900 s->first_displayed_line = s->lineno;
8904 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8906 s->last_displayed_line = s->first_displayed_line;
8911 rc = waddnstr(view->window,
8912 "See the tog(1) manual page for full documentation",
8915 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8917 wmove(view->window, view->nlines - 1, 0);
8918 wclrtoeol(view->window);
8919 wstandout(view->window);
8920 rc = waddnstr(view->window, "scroll down for more...",
8923 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8924 if (getcurx(view->window) < view->ncols - 6) {
8925 rc = wprintw(view->window, "[%.0f%%]",
8926 100.00 * s->last_displayed_line / s->nlines);
8928 return got_error_msg(GOT_ERR_IO, "wprintw");
8930 wstandend(view->window);
8936 static const struct got_error *
8937 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8939 struct tog_help_view_state *s = &view->state.help;
8940 const struct got_error *err = NULL;
8946 eos = nscroll = view->nlines;
8947 if (view_is_hsplit_top(view))
8950 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8959 horizontal_scroll_input(view, ch);
8963 s->first_displayed_line = 1;
8971 s->first_displayed_line = (s->nlines - eos) + 3;
8976 if (s->first_displayed_line > 1)
8977 --s->first_displayed_line;
8988 if (s->first_displayed_line == 1) {
8992 while (--nscroll > 0 && s->first_displayed_line > 1)
8993 s->first_displayed_line--;
8999 ++s->first_displayed_line;
9015 while (!s->eof && --nscroll > 0) {
9016 linelen = getline(&line, &linesz, s->f);
9017 s->first_displayed_line++;
9018 if (linelen == -1) {
9022 err = got_ferror(s->f, GOT_ERR_IO);
9036 static const struct got_error *
9037 close_help_view(struct tog_view *view)
9039 struct tog_help_view_state *s = &view->state.help;
9041 free(s->line_offsets);
9042 s->line_offsets = NULL;
9043 if (fclose(s->f) == EOF)
9044 return got_error_from_errno("fclose");
9049 static const struct got_error *
9050 reset_help_view(struct tog_view *view)
9052 struct tog_help_view_state *s = &view->state.help;
9055 if (s->f && fclose(s->f) == EOF)
9056 return got_error_from_errno("fclose");
9058 wclear(view->window);
9062 s->first_displayed_line = 1;
9063 s->last_displayed_line = view->nlines;
9064 s->matched_line = 0;
9066 return create_help(s);
9069 static const struct got_error *
9070 open_help_view(struct tog_view *view, struct tog_view *parent)
9072 const struct got_error *err = NULL;
9073 struct tog_help_view_state *s = &view->state.help;
9075 s->type = (enum tog_keymap_type)parent->type;
9076 s->first_displayed_line = 1;
9077 s->last_displayed_line = view->nlines;
9078 s->selected_line = 1;
9080 view->show = show_help_view;
9081 view->input = input_help_view;
9082 view->reset = reset_help_view;
9083 view->close = close_help_view;
9084 view->search_start = search_start_help_view;
9085 view->search_setup = search_setup_help_view;
9086 view->search_next = search_next_view_match;
9088 err = create_help(s);
9092 static const struct got_error *
9093 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9094 enum tog_view_type request, int y, int x)
9096 const struct got_error *err = NULL;
9102 if (view->type == TOG_VIEW_LOG) {
9103 struct tog_log_view_state *s = &view->state.log;
9105 err = open_diff_view_for_commit(new_view, y, x,
9106 s->selected_entry->commit, s->selected_entry->id,
9109 return got_error_msg(GOT_ERR_NOT_IMPL,
9110 "parent/child view pair not supported");
9112 case TOG_VIEW_BLAME:
9113 if (view->type == TOG_VIEW_TREE) {
9114 struct tog_tree_view_state *s = &view->state.tree;
9116 err = blame_tree_entry(new_view, y, x,
9117 s->selected_entry, &s->parents, s->commit_id,
9120 return got_error_msg(GOT_ERR_NOT_IMPL,
9121 "parent/child view pair not supported");
9124 if (view->type == TOG_VIEW_BLAME)
9125 err = log_annotated_line(new_view, y, x,
9126 view->state.blame.repo, view->state.blame.id_to_log);
9127 else if (view->type == TOG_VIEW_TREE)
9128 err = log_selected_tree_entry(new_view, y, x,
9130 else if (view->type == TOG_VIEW_REF)
9131 err = log_ref_entry(new_view, y, x,
9132 view->state.ref.selected_entry,
9133 view->state.ref.repo);
9135 return got_error_msg(GOT_ERR_NOT_IMPL,
9136 "parent/child view pair not supported");
9139 if (view->type == TOG_VIEW_LOG)
9140 err = browse_commit_tree(new_view, y, x,
9141 view->state.log.selected_entry,
9142 view->state.log.in_repo_path,
9143 view->state.log.head_ref_name,
9144 view->state.log.repo);
9145 else if (view->type == TOG_VIEW_REF)
9146 err = browse_ref_tree(new_view, y, x,
9147 view->state.ref.selected_entry,
9148 view->state.ref.repo);
9150 return got_error_msg(GOT_ERR_NOT_IMPL,
9151 "parent/child view pair not supported");
9154 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9155 if (*new_view == NULL)
9156 return got_error_from_errno("view_open");
9157 if (view->type == TOG_VIEW_LOG)
9158 err = open_ref_view(*new_view, view->state.log.repo);
9159 else if (view->type == TOG_VIEW_TREE)
9160 err = open_ref_view(*new_view, view->state.tree.repo);
9162 err = got_error_msg(GOT_ERR_NOT_IMPL,
9163 "parent/child view pair not supported");
9165 view_close(*new_view);
9168 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9169 if (*new_view == NULL)
9170 return got_error_from_errno("view_open");
9171 err = open_help_view(*new_view, view);
9173 view_close(*new_view);
9176 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9183 * If view was scrolled down to move the selected line into view when opening a
9184 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9187 offset_selection_up(struct tog_view *view)
9189 switch (view->type) {
9190 case TOG_VIEW_BLAME: {
9191 struct tog_blame_view_state *s = &view->state.blame;
9192 if (s->first_displayed_line == 1) {
9193 s->selected_line = MAX(s->selected_line - view->offset,
9197 if (s->first_displayed_line > view->offset)
9198 s->first_displayed_line -= view->offset;
9200 s->first_displayed_line = 1;
9201 s->selected_line += view->offset;
9205 log_scroll_up(&view->state.log, view->offset);
9206 view->state.log.selected += view->offset;
9209 ref_scroll_up(&view->state.ref, view->offset);
9210 view->state.ref.selected += view->offset;
9213 tree_scroll_up(&view->state.tree, view->offset);
9214 view->state.tree.selected += view->offset;
9224 * If the selected line is in the section of screen covered by the bottom split,
9225 * scroll down offset lines to move it into view and index its new position.
9227 static const struct got_error *
9228 offset_selection_down(struct tog_view *view)
9230 const struct got_error *err = NULL;
9231 const struct got_error *(*scrolld)(struct tog_view *, int);
9232 int *selected = NULL;
9235 switch (view->type) {
9236 case TOG_VIEW_BLAME: {
9237 struct tog_blame_view_state *s = &view->state.blame;
9240 if (s->selected_line > view->nlines - header) {
9241 offset = abs(view->nlines - s->selected_line - header);
9242 s->first_displayed_line += offset;
9243 s->selected_line -= offset;
9244 view->offset = offset;
9248 case TOG_VIEW_LOG: {
9249 struct tog_log_view_state *s = &view->state.log;
9250 scrolld = &log_scroll_down;
9251 header = view_is_parent_view(view) ? 3 : 2;
9252 selected = &s->selected;
9255 case TOG_VIEW_REF: {
9256 struct tog_ref_view_state *s = &view->state.ref;
9257 scrolld = &ref_scroll_down;
9259 selected = &s->selected;
9262 case TOG_VIEW_TREE: {
9263 struct tog_tree_view_state *s = &view->state.tree;
9264 scrolld = &tree_scroll_down;
9266 selected = &s->selected;
9276 if (selected && *selected > view->nlines - header) {
9277 offset = abs(view->nlines - *selected - header);
9278 view->offset = offset;
9279 if (scrolld && offset) {
9280 err = scrolld(view, offset);
9281 *selected -= offset;
9289 list_commands(FILE *fp)
9293 fprintf(fp, "commands:");
9294 for (i = 0; i < nitems(tog_commands); i++) {
9295 const struct tog_cmd *cmd = &tog_commands[i];
9296 fprintf(fp, " %s", cmd->name);
9302 usage(int hflag, int status)
9304 FILE *fp = (status == 0) ? stdout : stderr;
9306 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9309 fprintf(fp, "lazy usage: %s path\n", getprogname());
9316 make_argv(int argc, ...)
9324 argv = calloc(argc, sizeof(char *));
9327 for (i = 0; i < argc; i++) {
9328 argv[i] = strdup(va_arg(ap, char *));
9329 if (argv[i] == NULL)
9338 * Try to convert 'tog path' into a 'tog log path' command.
9339 * The user could simply have mistyped the command rather than knowingly
9340 * provided a path. So check whether argv[0] can in fact be resolved
9341 * to a path in the HEAD commit and print a special error if not.
9342 * This hack is for mpi@ <3
9344 static const struct got_error *
9345 tog_log_with_path(int argc, char *argv[])
9347 const struct got_error *error = NULL, *close_err;
9348 const struct tog_cmd *cmd = NULL;
9349 struct got_repository *repo = NULL;
9350 struct got_worktree *worktree = NULL;
9351 struct got_object_id *commit_id = NULL, *id = NULL;
9352 struct got_commit_object *commit = NULL;
9353 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9354 char *commit_id_str = NULL, **cmd_argv = NULL;
9355 int *pack_fds = NULL;
9357 cwd = getcwd(NULL, 0);
9359 return got_error_from_errno("getcwd");
9361 error = got_repo_pack_fds_open(&pack_fds);
9365 error = got_worktree_open(&worktree, cwd);
9366 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9370 repo_path = strdup(got_worktree_get_repo_path(worktree));
9372 repo_path = strdup(cwd);
9373 if (repo_path == NULL) {
9374 error = got_error_from_errno("strdup");
9378 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9382 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9387 error = tog_load_refs(repo, 0);
9390 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9391 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9392 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9397 got_worktree_close(worktree);
9401 error = got_object_open_as_commit(&commit, repo, commit_id);
9405 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9407 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9409 fprintf(stderr, "%s: '%s' is no known command or path\n",
9410 getprogname(), argv[0]);
9415 error = got_object_id_str(&commit_id_str, commit_id);
9419 cmd = &tog_commands[0]; /* log */
9421 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9422 error = cmd->cmd_main(argc, cmd_argv);
9425 close_err = got_repo_close(repo);
9430 got_object_commit_close(commit);
9432 got_worktree_close(worktree);
9434 const struct got_error *pack_err =
9435 got_repo_pack_fds_close(pack_fds);
9440 free(commit_id_str);
9447 for (i = 0; i < argc; i++)
9456 main(int argc, char *argv[])
9458 const struct got_error *error = NULL;
9459 const struct tog_cmd *cmd = NULL;
9460 int ch, hflag = 0, Vflag = 0;
9461 char **cmd_argv = NULL;
9462 static const struct option longopts[] = {
9463 { "version", no_argument, NULL, 'V' },
9466 char *diff_algo_str = NULL;
9468 setlocale(LC_CTYPE, "");
9471 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9476 if (!isatty(STDIN_FILENO))
9477 errx(1, "standard input is not a tty");
9479 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9499 got_version_print_str();
9506 /* Build an argument vector which runs a default command. */
9507 cmd = &tog_commands[0];
9509 cmd_argv = make_argv(argc, cmd->name);
9513 /* Did the user specify a command? */
9514 for (i = 0; i < nitems(tog_commands); i++) {
9515 if (strncmp(tog_commands[i].name, argv[0],
9516 strlen(argv[0])) == 0) {
9517 cmd = &tog_commands[i];
9523 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9524 if (diff_algo_str) {
9525 if (strcasecmp(diff_algo_str, "patience") == 0)
9526 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9527 if (strcasecmp(diff_algo_str, "myers") == 0)
9528 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9534 /* No command specified; try log with a path */
9535 error = tog_log_with_path(argc, argv);
9540 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9546 for (i = 0; i < argc; i++)
9551 if (error && error->code != GOT_ERR_CANCELLED &&
9552 error->code != GOT_ERR_EOF &&
9553 error->code != GOT_ERR_PRIVSEP_EXIT &&
9554 error->code != GOT_ERR_PRIVSEP_PIPE &&
9555 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9556 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);