2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_opentemp.h"
53 #include "got_cancel.h"
54 #include "got_commit_graph.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
58 #include "got_worktree.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define CTRL(x) ((x) & 0x1f)
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
80 __dead static void usage(int, int);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_ref(void);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct tog_cmd tog_commands[] = {
94 { "log", cmd_log, usage_log },
95 { "diff", cmd_diff, usage_diff },
96 { "blame", cmd_blame, usage_blame },
97 { "tree", cmd_tree, usage_tree },
98 { "ref", cmd_ref, usage_ref },
110 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 enum tog_keymap_type {
112 TOG_KEYMAP_KEYS = -2,
128 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 #define TOG_EOF_STRING "(END)"
132 struct commit_queue_entry {
133 TAILQ_ENTRY(commit_queue_entry) entry;
134 struct got_object_id *id;
135 struct got_commit_object *commit;
138 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 struct commit_queue {
141 struct commit_queue_head head;
145 STAILQ_ENTRY(tog_color) entry;
149 STAILQ_HEAD(tog_colors, tog_color);
151 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 static struct got_reflist_object_id_map *tog_refs_idmap;
153 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 static const struct got_error *
156 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 struct got_reference* re2)
159 const char *name1 = got_ref_get_name(re1);
160 const char *name2 = got_ref_get_name(re2);
161 int isbackup1, isbackup2;
163 /* Sort backup refs towards the bottom of the list. */
164 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 if (!isbackup1 && isbackup2) {
169 } else if (isbackup1 && !isbackup2) {
174 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
178 static const struct got_error *
179 tog_load_refs(struct got_repository *repo, int sort_by_date)
181 const struct got_error *err;
183 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
196 if (tog_refs_idmap) {
197 got_reflist_object_id_map_free(tog_refs_idmap);
198 tog_refs_idmap = NULL;
200 got_ref_list_free(&tog_refs);
203 static const struct got_error *
204 add_color(struct tog_colors *colors, const char *pattern,
205 int idx, short color)
207 const struct got_error *err = NULL;
208 struct tog_color *tc;
211 if (idx < 1 || idx > COLOR_PAIRS - 1)
214 init_pair(idx, color, -1);
216 tc = calloc(1, sizeof(*tc));
218 return got_error_from_errno("calloc");
219 regerr = regcomp(&tc->regex, pattern,
220 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
222 static char regerr_msg[512];
223 static char err_msg[512];
224 regerror(regerr, &tc->regex, regerr_msg,
226 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
228 err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 STAILQ_INSERT_HEAD(colors, tc, entry);
238 free_colors(struct tog_colors *colors)
240 struct tog_color *tc;
242 while (!STAILQ_EMPTY(colors)) {
243 tc = STAILQ_FIRST(colors);
244 STAILQ_REMOVE_HEAD(colors, entry);
250 static struct tog_color *
251 get_color(struct tog_colors *colors, int colorpair)
253 struct tog_color *tc = NULL;
255 STAILQ_FOREACH(tc, colors, entry) {
256 if (tc->colorpair == colorpair)
264 default_color_value(const char *envvar)
266 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
274 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
282 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
284 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
286 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
290 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 return COLOR_MAGENTA;
292 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
301 get_color_value(const char *envvar)
303 const char *val = getenv(envvar);
306 return default_color_value(envvar);
308 if (strcasecmp(val, "black") == 0)
310 if (strcasecmp(val, "red") == 0)
312 if (strcasecmp(val, "green") == 0)
314 if (strcasecmp(val, "yellow") == 0)
316 if (strcasecmp(val, "blue") == 0)
318 if (strcasecmp(val, "magenta") == 0)
319 return COLOR_MAGENTA;
320 if (strcasecmp(val, "cyan") == 0)
322 if (strcasecmp(val, "white") == 0)
324 if (strcasecmp(val, "default") == 0)
327 return default_color_value(envvar);
330 struct tog_diff_view_state {
331 struct got_object_id *id1, *id2;
332 const char *label1, *label2;
336 int first_displayed_line;
337 int last_displayed_line;
340 int ignore_whitespace;
342 struct got_repository *repo;
343 struct got_diff_line *lines;
348 /* passed from log or blame view; may be NULL */
349 struct tog_view *parent_view;
352 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 static volatile sig_atomic_t tog_thread_error;
355 struct tog_log_thread_args {
356 pthread_cond_t need_commits;
357 pthread_cond_t commit_loaded;
360 struct got_commit_graph *graph;
361 struct commit_queue *real_commits;
362 const char *in_repo_path;
363 struct got_object_id *start_id;
364 struct got_repository *repo;
368 struct commit_queue_entry **first_displayed_entry;
369 struct commit_queue_entry **selected_entry;
371 int *search_next_done;
375 regex_t *limit_regex;
376 struct commit_queue *limit_commits;
379 struct tog_log_view_state {
380 struct commit_queue *commits;
381 struct commit_queue_entry *first_displayed_entry;
382 struct commit_queue_entry *last_displayed_entry;
383 struct commit_queue_entry *selected_entry;
384 struct commit_queue real_commits;
389 struct got_repository *repo;
390 struct got_object_id *start_id;
393 struct tog_log_thread_args thread_args;
394 struct commit_queue_entry *matched_entry;
395 struct commit_queue_entry *search_entry;
396 struct tog_colors colors;
400 struct commit_queue limit_commits;
403 #define TOG_COLOR_DIFF_MINUS 1
404 #define TOG_COLOR_DIFF_PLUS 2
405 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 #define TOG_COLOR_DIFF_META 4
407 #define TOG_COLOR_TREE_SUBMODULE 5
408 #define TOG_COLOR_TREE_SYMLINK 6
409 #define TOG_COLOR_TREE_DIRECTORY 7
410 #define TOG_COLOR_TREE_EXECUTABLE 8
411 #define TOG_COLOR_COMMIT 9
412 #define TOG_COLOR_AUTHOR 10
413 #define TOG_COLOR_DATE 11
414 #define TOG_COLOR_REFS_HEADS 12
415 #define TOG_COLOR_REFS_TAGS 13
416 #define TOG_COLOR_REFS_REMOTES 14
417 #define TOG_COLOR_REFS_BACKUP 15
419 struct tog_blame_cb_args {
420 struct tog_blame_line *lines; /* one per line */
423 struct tog_view *view;
424 struct got_object_id *commit_id;
428 struct tog_blame_thread_args {
430 struct got_repository *repo;
431 struct tog_blame_cb_args *cb_args;
433 got_cancel_cb cancel_cb;
440 struct tog_blame_line *lines;
444 struct tog_blame_thread_args thread_args;
445 struct tog_blame_cb_args cb_args;
450 struct tog_blame_view_state {
451 int first_displayed_line;
452 int last_displayed_line;
454 int last_diffed_line;
458 struct got_object_id_queue blamed_commits;
459 struct got_object_qid *blamed_commit;
461 struct got_repository *repo;
462 struct got_object_id *commit_id;
463 struct got_object_id *id_to_log;
464 struct tog_blame blame;
466 struct tog_colors colors;
469 struct tog_parent_tree {
470 TAILQ_ENTRY(tog_parent_tree) entry;
471 struct got_tree_object *tree;
472 struct got_tree_entry *first_displayed_entry;
473 struct got_tree_entry *selected_entry;
477 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 struct tog_tree_view_state {
481 struct got_object_id *commit_id;/* commit which this tree belongs to */
482 struct got_tree_object *root; /* the commit's root tree entry */
483 struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 struct got_tree_entry *first_displayed_entry;
485 struct got_tree_entry *last_displayed_entry;
486 struct got_tree_entry *selected_entry;
487 int ndisplayed, selected, show_ids;
488 struct tog_parent_trees parents; /* parent trees of current sub-tree */
490 struct got_repository *repo;
491 struct got_tree_entry *matched_entry;
492 struct tog_colors colors;
495 struct tog_reflist_entry {
496 TAILQ_ENTRY(tog_reflist_entry) entry;
497 struct got_reference *ref;
501 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 struct tog_ref_view_state {
504 struct tog_reflist_head refs;
505 struct tog_reflist_entry *first_displayed_entry;
506 struct tog_reflist_entry *last_displayed_entry;
507 struct tog_reflist_entry *selected_entry;
508 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 struct got_repository *repo;
510 struct tog_reflist_entry *matched_entry;
511 struct tog_colors colors;
514 struct tog_help_view_state {
519 int first_displayed_line;
520 int last_displayed_line;
525 enum tog_keymap_type type;
528 #define GENERATE_HELP \
529 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 KEY_("k C-p Up", "Move cursor or page up one line"), \
532 KEY_("j C-n Down", "Move cursor or page down one line"), \
533 KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 KEY_("C-u u", "Scroll the view up one half page"), \
536 KEY_("C-d d", "Scroll the view down one half page"), \
537 KEY_("g", "Go to line N (default: first line)"), \
538 KEY_("Home =", "Go to the first line"), \
539 KEY_("G", "Go to line N (default: last line)"), \
540 KEY_("End *", "Go to the last line"), \
541 KEY_("l Right", "Scroll the view right"), \
542 KEY_("h Left", "Scroll the view left"), \
543 KEY_("$", "Scroll view to the rightmost position"), \
544 KEY_("0", "Scroll view to the leftmost position"), \
545 KEY_("-", "Decrease size of the focussed split"), \
546 KEY_("+", "Increase size of the focussed split"), \
547 KEY_("Tab", "Switch focus between views"), \
548 KEY_("F", "Toggle fullscreen mode"), \
549 KEY_("/", "Open prompt to enter search term"), \
550 KEY_("n", "Find next line/token matching the current search term"), \
551 KEY_("N", "Find previous line/token matching the current search term"),\
552 KEY_("q", "Quit the focussed view; Quit help screen"), \
553 KEY_("Q", "Quit tog"), \
555 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
556 KEY_("< ,", "Move cursor up one commit"), \
557 KEY_("> .", "Move cursor down one commit"), \
558 KEY_("Enter", "Open diff view of the selected commit"), \
559 KEY_("B", "Reload the log view and toggle display of merged commits"), \
560 KEY_("R", "Open ref view of all repository references"), \
561 KEY_("T", "Display tree view of the repository from the selected" \
563 KEY_("@", "Toggle between displaying author and committer name"), \
564 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
565 KEY_("C-g Backspace", "Cancel current search or log operation"), \
566 KEY_("C-l", "Reload the log view with new commits in the repository"), \
568 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
569 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
570 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
571 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
572 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
574 KEY_("(", "Go to the previous file in the diff"), \
575 KEY_(")", "Go to the next file in the diff"), \
576 KEY_("{", "Go to the previous hunk in the diff"), \
577 KEY_("}", "Go to the next hunk in the diff"), \
578 KEY_("[", "Decrease the number of context lines"), \
579 KEY_("]", "Increase the number of context lines"), \
580 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
582 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
583 KEY_("Enter", "Display diff view of the selected line's commit"), \
584 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
585 KEY_("L", "Open log view for the currently selected annotated line"), \
586 KEY_("C", "Reload view with the previously blamed commit"), \
587 KEY_("c", "Reload view with the version of the file found in the" \
588 " selected line's commit"), \
589 KEY_("p", "Reload view with the version of the file found in the" \
590 " selected line's parent commit"), \
592 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
593 KEY_("Enter", "Enter selected directory or open blame view of the" \
595 KEY_("L", "Open log view for the selected entry"), \
596 KEY_("R", "Open ref view of all repository references"), \
597 KEY_("i", "Show object IDs for all tree entries"), \
598 KEY_("Backspace", "Return to the parent directory"), \
600 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
601 KEY_("Enter", "Display log view of the selected reference"), \
602 KEY_("T", "Display tree view of the selected reference"), \
603 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
604 KEY_("m", "Toggle display of last modified date for each reference"), \
605 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
606 KEY_("C-l", "Reload view with all repository references")
611 enum tog_keymap_type type;
615 * We implement two types of views: parent views and child views.
617 * The 'Tab' key switches focus between a parent view and its child view.
618 * Child views are shown side-by-side to their parent view, provided
619 * there is enough screen estate.
621 * When a new view is opened from within a parent view, this new view
622 * becomes a child view of the parent view, replacing any existing child.
624 * When a new view is opened from within a child view, this new view
625 * becomes a parent view which will obscure the views below until the
626 * user quits the new parent view by typing 'q'.
628 * This list of views contains parent views only.
629 * Child views are only pointed to by their parent view.
631 TAILQ_HEAD(tog_view_list_head, tog_view);
634 TAILQ_ENTRY(tog_view) entry;
637 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
638 int resized_y, resized_x; /* begin_y/x based on user resizing */
639 int maxx, x; /* max column and current start column */
640 int lines, cols; /* copies of LINES and COLS */
641 int nscrolled, offset; /* lines scrolled and hsplit line offset */
642 int gline, hiline; /* navigate to and highlight this nG line */
643 int ch, count; /* current keymap and count prefix */
644 int resized; /* set when in a resize event */
645 int focussed; /* Only set on one parent or child view at a time. */
647 struct tog_view *parent;
648 struct tog_view *child;
651 * This flag is initially set on parent views when a new child view
652 * is created. It gets toggled when the 'Tab' key switches focus
653 * between parent and child.
654 * The flag indicates whether focus should be passed on to our child
655 * view if this parent view gets picked for focus after another parent
656 * view was closed. This prevents child views from losing focus in such
661 enum tog_view_mode mode;
662 /* type-specific state */
663 enum tog_view_type type;
665 struct tog_diff_view_state diff;
666 struct tog_log_view_state log;
667 struct tog_blame_view_state blame;
668 struct tog_tree_view_state tree;
669 struct tog_ref_view_state ref;
670 struct tog_help_view_state help;
673 const struct got_error *(*show)(struct tog_view *);
674 const struct got_error *(*input)(struct tog_view **,
675 struct tog_view *, int);
676 const struct got_error *(*reset)(struct tog_view *);
677 const struct got_error *(*resize)(struct tog_view *, int);
678 const struct got_error *(*close)(struct tog_view *);
680 const struct got_error *(*search_start)(struct tog_view *);
681 const struct got_error *(*search_next)(struct tog_view *);
682 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
683 int **, int **, int **, int **);
686 #define TOG_SEARCH_FORWARD 1
687 #define TOG_SEARCH_BACKWARD 2
688 int search_next_done;
689 #define TOG_SEARCH_HAVE_MORE 1
690 #define TOG_SEARCH_NO_MORE 2
691 #define TOG_SEARCH_HAVE_NONE 3
697 static const struct got_error *open_diff_view(struct tog_view *,
698 struct got_object_id *, struct got_object_id *,
699 const char *, const char *, int, int, int, struct tog_view *,
700 struct got_repository *);
701 static const struct got_error *show_diff_view(struct tog_view *);
702 static const struct got_error *input_diff_view(struct tog_view **,
703 struct tog_view *, int);
704 static const struct got_error *reset_diff_view(struct tog_view *);
705 static const struct got_error* close_diff_view(struct tog_view *);
706 static const struct got_error *search_start_diff_view(struct tog_view *);
707 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
708 size_t *, int **, int **, int **, int **);
709 static const struct got_error *search_next_view_match(struct tog_view *);
711 static const struct got_error *open_log_view(struct tog_view *,
712 struct got_object_id *, struct got_repository *,
713 const char *, const char *, int);
714 static const struct got_error * show_log_view(struct tog_view *);
715 static const struct got_error *input_log_view(struct tog_view **,
716 struct tog_view *, int);
717 static const struct got_error *resize_log_view(struct tog_view *, int);
718 static const struct got_error *close_log_view(struct tog_view *);
719 static const struct got_error *search_start_log_view(struct tog_view *);
720 static const struct got_error *search_next_log_view(struct tog_view *);
722 static const struct got_error *open_blame_view(struct tog_view *, char *,
723 struct got_object_id *, struct got_repository *);
724 static const struct got_error *show_blame_view(struct tog_view *);
725 static const struct got_error *input_blame_view(struct tog_view **,
726 struct tog_view *, int);
727 static const struct got_error *reset_blame_view(struct tog_view *);
728 static const struct got_error *close_blame_view(struct tog_view *);
729 static const struct got_error *search_start_blame_view(struct tog_view *);
730 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
731 size_t *, int **, int **, int **, int **);
733 static const struct got_error *open_tree_view(struct tog_view *,
734 struct got_object_id *, const char *, struct got_repository *);
735 static const struct got_error *show_tree_view(struct tog_view *);
736 static const struct got_error *input_tree_view(struct tog_view **,
737 struct tog_view *, int);
738 static const struct got_error *close_tree_view(struct tog_view *);
739 static const struct got_error *search_start_tree_view(struct tog_view *);
740 static const struct got_error *search_next_tree_view(struct tog_view *);
742 static const struct got_error *open_ref_view(struct tog_view *,
743 struct got_repository *);
744 static const struct got_error *show_ref_view(struct tog_view *);
745 static const struct got_error *input_ref_view(struct tog_view **,
746 struct tog_view *, int);
747 static const struct got_error *close_ref_view(struct tog_view *);
748 static const struct got_error *search_start_ref_view(struct tog_view *);
749 static const struct got_error *search_next_ref_view(struct tog_view *);
751 static const struct got_error *open_help_view(struct tog_view *,
753 static const struct got_error *show_help_view(struct tog_view *);
754 static const struct got_error *input_help_view(struct tog_view **,
755 struct tog_view *, int);
756 static const struct got_error *reset_help_view(struct tog_view *);
757 static const struct got_error* close_help_view(struct tog_view *);
758 static const struct got_error *search_start_help_view(struct tog_view *);
759 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
760 size_t *, int **, int **, int **, int **);
762 static volatile sig_atomic_t tog_sigwinch_received;
763 static volatile sig_atomic_t tog_sigpipe_received;
764 static volatile sig_atomic_t tog_sigcont_received;
765 static volatile sig_atomic_t tog_sigint_received;
766 static volatile sig_atomic_t tog_sigterm_received;
769 tog_sigwinch(int signo)
771 tog_sigwinch_received = 1;
775 tog_sigpipe(int signo)
777 tog_sigpipe_received = 1;
781 tog_sigcont(int signo)
783 tog_sigcont_received = 1;
787 tog_sigint(int signo)
789 tog_sigint_received = 1;
793 tog_sigterm(int signo)
795 tog_sigterm_received = 1;
799 tog_fatal_signal_received(void)
801 return (tog_sigpipe_received ||
802 tog_sigint_received || tog_sigterm_received);
805 static const struct got_error *
806 view_close(struct tog_view *view)
808 const struct got_error *err = NULL, *child_err = NULL;
811 child_err = view_close(view->child);
815 err = view->close(view);
817 del_panel(view->panel);
819 delwin(view->window);
821 return err ? err : child_err;
824 static struct tog_view *
825 view_open(int nlines, int ncols, int begin_y, int begin_x,
826 enum tog_view_type type)
828 struct tog_view *view = calloc(1, sizeof(*view));
836 view->nlines = nlines ? nlines : LINES - begin_y;
837 view->ncols = ncols ? ncols : COLS - begin_x;
838 view->begin_y = begin_y;
839 view->begin_x = begin_x;
840 view->window = newwin(nlines, ncols, begin_y, begin_x);
841 if (view->window == NULL) {
845 view->panel = new_panel(view->window);
846 if (view->panel == NULL ||
847 set_panel_userptr(view->panel, view) != OK) {
852 keypad(view->window, TRUE);
857 view_split_begin_x(int begin_x)
859 if (begin_x > 0 || COLS < 120)
861 return (COLS - MAX(COLS / 2, 80));
864 /* XXX Stub till we decide what to do. */
866 view_split_begin_y(int lines)
868 return lines * HSPLIT_SCALE;
871 static const struct got_error *view_resize(struct tog_view *);
873 static const struct got_error *
874 view_splitscreen(struct tog_view *view)
876 const struct got_error *err = NULL;
878 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
879 if (view->resized_y && view->resized_y < view->lines)
880 view->begin_y = view->resized_y;
882 view->begin_y = view_split_begin_y(view->nlines);
884 } else if (!view->resized) {
885 if (view->resized_x && view->resized_x < view->cols - 1 &&
887 view->begin_x = view->resized_x;
889 view->begin_x = view_split_begin_x(0);
892 view->nlines = LINES - view->begin_y;
893 view->ncols = COLS - view->begin_x;
896 err = view_resize(view);
900 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
901 view->parent->nlines = view->begin_y;
903 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
904 return got_error_from_errno("mvwin");
909 static const struct got_error *
910 view_fullscreen(struct tog_view *view)
912 const struct got_error *err = NULL;
915 view->begin_y = view->resized ? view->begin_y : 0;
916 view->nlines = view->resized ? view->nlines : LINES;
920 err = view_resize(view);
924 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
925 return got_error_from_errno("mvwin");
931 view_is_parent_view(struct tog_view *view)
933 return view->parent == NULL;
937 view_is_splitscreen(struct tog_view *view)
939 return view->begin_x > 0 || view->begin_y > 0;
943 view_is_fullscreen(struct tog_view *view)
945 return view->nlines == LINES && view->ncols == COLS;
949 view_is_hsplit_top(struct tog_view *view)
951 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
952 view_is_splitscreen(view->child);
956 view_border(struct tog_view *view)
959 const struct tog_view *view_above;
962 return view_border(view->parent);
964 panel = panel_above(view->panel);
968 view_above = panel_userptr(panel);
969 if (view->mode == TOG_VIEW_SPLIT_HRZN)
970 mvwhline(view->window, view_above->begin_y - 1,
971 view->begin_x, got_locale_is_utf8() ?
972 ACS_HLINE : '-', view->ncols);
974 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
978 static const struct got_error *view_init_hsplit(struct tog_view *, int);
979 static const struct got_error *request_log_commits(struct tog_view *);
980 static const struct got_error *offset_selection_down(struct tog_view *);
981 static void offset_selection_up(struct tog_view *);
982 static void view_get_split(struct tog_view *, int *, int *);
984 static const struct got_error *
985 view_resize(struct tog_view *view)
987 const struct got_error *err = NULL;
988 int dif, nlines, ncols;
990 dif = LINES - view->lines; /* line difference */
992 if (view->lines > LINES)
993 nlines = view->nlines - (view->lines - LINES);
995 nlines = view->nlines + (LINES - view->lines);
996 if (view->cols > COLS)
997 ncols = view->ncols - (view->cols - COLS);
999 ncols = view->ncols + (COLS - view->cols);
1002 int hs = view->child->begin_y;
1004 if (!view_is_fullscreen(view))
1005 view->child->begin_x = view_split_begin_x(view->begin_x);
1006 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1007 view->child->begin_x == 0) {
1010 view_fullscreen(view->child);
1011 if (view->child->focussed)
1012 show_panel(view->child->panel);
1014 show_panel(view->panel);
1016 ncols = view->child->begin_x;
1018 view_splitscreen(view->child);
1019 show_panel(view->child->panel);
1022 * XXX This is ugly and needs to be moved into the above
1023 * logic but "works" for now and my attempts at moving it
1024 * break either 'tab' or 'F' key maps in horizontal splits.
1027 err = view_splitscreen(view->child);
1030 if (dif < 0) { /* top split decreased */
1031 err = offset_selection_down(view);
1038 show_panel(view->child->panel);
1039 nlines = view->nlines;
1041 } else if (view->parent == NULL)
1044 if (view->resize && dif > 0) {
1045 err = view->resize(view, dif);
1050 if (wresize(view->window, nlines, ncols) == ERR)
1051 return got_error_from_errno("wresize");
1052 if (replace_panel(view->panel, view->window) == ERR)
1053 return got_error_from_errno("replace_panel");
1054 wclear(view->window);
1056 view->nlines = nlines;
1057 view->ncols = ncols;
1058 view->lines = LINES;
1064 static const struct got_error *
1065 resize_log_view(struct tog_view *view, int increase)
1067 struct tog_log_view_state *s = &view->state.log;
1068 const struct got_error *err = NULL;
1071 if (s->selected_entry)
1072 n = s->selected_entry->idx + view->lines - s->selected;
1075 * Request commits to account for the increased
1076 * height so we have enough to populate the view.
1078 if (s->commits->ncommits < n) {
1079 view->nscrolled = n - s->commits->ncommits + increase + 1;
1080 err = request_log_commits(view);
1087 view_adjust_offset(struct tog_view *view, int n)
1092 if (view->parent && view->parent->offset) {
1093 if (view->parent->offset + n >= 0)
1094 view->parent->offset += n;
1096 view->parent->offset = 0;
1097 } else if (view->offset) {
1098 if (view->offset - n >= 0)
1105 static const struct got_error *
1106 view_resize_split(struct tog_view *view, int resize)
1108 const struct got_error *err = NULL;
1109 struct tog_view *v = NULL;
1116 if (!v->child || !view_is_splitscreen(v->child))
1119 v->resized = v->child->resized = resize; /* lock for resize event */
1121 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1122 if (v->child->resized_y)
1123 v->child->begin_y = v->child->resized_y;
1125 v->child->begin_y -= resize;
1127 v->child->begin_y += resize;
1128 if (v->child->begin_y < 3) {
1130 v->child->begin_y = 3;
1131 } else if (v->child->begin_y > LINES - 1) {
1133 v->child->begin_y = LINES - 1;
1136 v->child->ncols = COLS;
1137 view_adjust_offset(view, resize);
1138 err = view_init_hsplit(v, v->child->begin_y);
1141 v->child->resized_y = v->child->begin_y;
1143 if (v->child->resized_x)
1144 v->child->begin_x = v->child->resized_x;
1146 v->child->begin_x -= resize;
1148 v->child->begin_x += resize;
1149 if (v->child->begin_x < 11) {
1151 v->child->begin_x = 11;
1152 } else if (v->child->begin_x > COLS - 1) {
1154 v->child->begin_x = COLS - 1;
1156 v->child->resized_x = v->child->begin_x;
1159 v->child->mode = v->mode;
1160 v->child->nlines = v->lines - v->child->begin_y;
1161 v->child->ncols = v->cols - v->child->begin_x;
1164 err = view_fullscreen(v);
1167 err = view_splitscreen(v->child);
1171 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1172 err = offset_selection_down(v->child);
1178 err = v->resize(v, 0);
1179 else if (v->child->resize)
1180 err = v->child->resize(v->child, 0);
1182 v->resized = v->child->resized = 0;
1188 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1190 struct tog_view *v = src->child ? src->child : src;
1192 dst->resized_x = v->resized_x;
1193 dst->resized_y = v->resized_y;
1196 static const struct got_error *
1197 view_close_child(struct tog_view *view)
1199 const struct got_error *err = NULL;
1201 if (view->child == NULL)
1204 err = view_close(view->child);
1209 static const struct got_error *
1210 view_set_child(struct tog_view *view, struct tog_view *child)
1212 const struct got_error *err = NULL;
1214 view->child = child;
1215 child->parent = view;
1217 err = view_resize(view);
1221 if (view->child->resized_x || view->child->resized_y)
1222 err = view_resize_split(view, 0);
1227 static const struct got_error *view_dispatch_request(struct tog_view **,
1228 struct tog_view *, enum tog_view_type, int, int);
1230 static const struct got_error *
1231 view_request_new(struct tog_view **requested, struct tog_view *view,
1232 enum tog_view_type request)
1234 struct tog_view *new_view = NULL;
1235 const struct got_error *err;
1240 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1241 view_get_split(view, &y, &x);
1243 err = view_dispatch_request(&new_view, view, request, y, x);
1247 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1248 request != TOG_VIEW_HELP) {
1249 err = view_init_hsplit(view, y);
1255 new_view->focussed = 1;
1256 new_view->mode = view->mode;
1257 new_view->nlines = request == TOG_VIEW_HELP ?
1258 view->lines : view->lines - y;
1260 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1261 view_transfer_size(new_view, view);
1262 err = view_close_child(view);
1265 err = view_set_child(view, new_view);
1268 view->focus_child = 1;
1270 *requested = new_view;
1276 tog_resizeterm(void)
1279 struct winsize size;
1281 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1282 cols = 80; /* Default */
1286 lines = size.ws_row;
1288 resize_term(lines, cols);
1291 static const struct got_error *
1292 view_search_start(struct tog_view *view)
1294 const struct got_error *err = NULL;
1295 struct tog_view *v = view;
1299 if (view->search_started) {
1300 regfree(&view->regex);
1301 view->searching = 0;
1302 memset(&view->regmatch, 0, sizeof(view->regmatch));
1304 view->search_started = 0;
1306 if (view->nlines < 1)
1309 if (view_is_hsplit_top(view))
1311 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1314 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1315 wclrtoeol(v->window);
1317 nodelay(v->window, FALSE); /* block for search term input */
1320 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1321 wrefresh(v->window);
1324 nodelay(v->window, TRUE);
1328 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1329 err = view->search_start(view);
1331 regfree(&view->regex);
1334 view->search_started = 1;
1335 view->searching = TOG_SEARCH_FORWARD;
1336 view->search_next_done = 0;
1337 view->search_next(view);
1343 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1344 static const struct got_error *
1345 switch_split(struct tog_view *view)
1347 const struct got_error *err = NULL;
1348 struct tog_view *v = NULL;
1355 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1356 v->mode = TOG_VIEW_SPLIT_VERT;
1358 v->mode = TOG_VIEW_SPLIT_HRZN;
1362 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1363 v->mode = TOG_VIEW_SPLIT_NONE;
1365 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1366 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1367 v->child->begin_y = v->child->resized_y;
1368 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1369 v->child->begin_x = v->child->resized_x;
1372 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1374 v->child->ncols = COLS;
1375 v->child->nscrolled = LINES - v->child->nlines;
1377 err = view_init_hsplit(v, v->child->begin_y);
1381 v->child->mode = v->mode;
1382 v->child->nlines = v->lines - v->child->begin_y;
1385 err = view_fullscreen(v);
1388 err = view_splitscreen(v->child);
1392 if (v->mode == TOG_VIEW_SPLIT_NONE)
1393 v->mode = TOG_VIEW_SPLIT_VERT;
1394 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1395 err = offset_selection_down(v);
1398 err = offset_selection_down(v->child);
1402 offset_selection_up(v);
1403 offset_selection_up(v->child);
1406 err = v->resize(v, 0);
1407 else if (v->child->resize)
1408 err = v->child->resize(v->child, 0);
1414 * Compute view->count from numeric input. Assign total to view->count and
1415 * return first non-numeric key entered.
1418 get_compound_key(struct tog_view *view, int c)
1420 struct tog_view *v = view;
1423 if (view_is_hsplit_top(view))
1425 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1429 cbreak(); /* block for input */
1430 nodelay(view->window, FALSE);
1431 wmove(v->window, v->nlines - 1, 0);
1432 wclrtoeol(v->window);
1433 waddch(v->window, ':');
1436 x = getcurx(v->window);
1437 if (x != ERR && x < view->ncols) {
1438 waddch(v->window, c);
1439 wrefresh(v->window);
1443 * Don't overflow. Max valid request should be the greatest
1444 * between the longest and total lines; cap at 10 million.
1449 n = n * 10 + (c - '0');
1450 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1452 if (c == 'G' || c == 'g') { /* nG key map */
1453 view->gline = view->hiline = n;
1458 /* Massage excessive or inapplicable values at the input handler. */
1465 action_report(struct tog_view *view)
1467 struct tog_view *v = view;
1469 if (view_is_hsplit_top(view))
1471 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1474 wmove(v->window, v->nlines - 1, 0);
1475 wclrtoeol(v->window);
1476 wprintw(v->window, ":%s", view->action);
1477 wrefresh(v->window);
1480 * Clear action status report. Only clear in blame view
1481 * once annotating is complete, otherwise it's too fast.
1483 if (view->type == TOG_VIEW_BLAME) {
1484 if (view->state.blame.blame_complete)
1485 view->action = NULL;
1487 view->action = NULL;
1490 static const struct got_error *
1491 view_input(struct tog_view **new, int *done, struct tog_view *view,
1492 struct tog_view_list_head *views)
1494 const struct got_error *err = NULL;
1501 action_report(view);
1503 /* Clear "no matches" indicator. */
1504 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1505 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1510 if (view->searching && !view->search_next_done) {
1511 errcode = pthread_mutex_unlock(&tog_mutex);
1513 return got_error_set_errno(errcode,
1514 "pthread_mutex_unlock");
1516 errcode = pthread_mutex_lock(&tog_mutex);
1518 return got_error_set_errno(errcode,
1519 "pthread_mutex_lock");
1520 view->search_next(view);
1524 /* Allow threads to make progress while we are waiting for input. */
1525 errcode = pthread_mutex_unlock(&tog_mutex);
1527 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1528 /* If we have an unfinished count, let C-g or backspace abort. */
1529 if (view->count && --view->count) {
1531 nodelay(view->window, TRUE);
1532 ch = wgetch(view->window);
1533 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1538 ch = wgetch(view->window);
1539 if (ch >= '1' && ch <= '9')
1540 view->ch = ch = get_compound_key(view, ch);
1542 if (view->hiline && ch != ERR && ch != 0)
1543 view->hiline = 0; /* key pressed, clear line highlight */
1544 nodelay(view->window, TRUE);
1545 errcode = pthread_mutex_lock(&tog_mutex);
1547 return got_error_set_errno(errcode, "pthread_mutex_lock");
1549 if (tog_sigwinch_received || tog_sigcont_received) {
1551 tog_sigwinch_received = 0;
1552 tog_sigcont_received = 0;
1553 TAILQ_FOREACH(v, views, entry) {
1554 err = view_resize(v);
1557 err = v->input(new, v, KEY_RESIZE);
1561 err = view_resize(v->child);
1564 err = v->child->input(new, v->child,
1568 if (v->child->resized_x || v->child->resized_y) {
1569 err = view_resize_split(v, 0);
1581 if (view->type == TOG_VIEW_HELP)
1582 err = view->reset(view);
1584 err = view_request_new(new, view, TOG_VIEW_HELP);
1590 view->child->focussed = 1;
1591 view->focus_child = 1;
1592 } else if (view->parent) {
1594 view->parent->focussed = 1;
1595 view->parent->focus_child = 0;
1596 if (!view_is_splitscreen(view)) {
1597 if (view->parent->resize) {
1598 err = view->parent->resize(view->parent,
1603 offset_selection_up(view->parent);
1604 err = view_fullscreen(view->parent);
1611 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1612 if (view->parent->resize) {
1613 /* might need more commits to fill fullscreen */
1614 err = view->parent->resize(view->parent, 0);
1618 offset_selection_up(view->parent);
1620 err = view->input(new, view, ch);
1628 if (view_is_parent_view(view)) {
1629 if (view->child == NULL)
1631 if (view_is_splitscreen(view->child)) {
1633 view->child->focussed = 1;
1634 err = view_fullscreen(view->child);
1636 err = view_splitscreen(view->child);
1638 err = view_resize_split(view, 0);
1642 err = view->child->input(new, view->child,
1645 if (view_is_splitscreen(view)) {
1646 view->parent->focussed = 0;
1648 err = view_fullscreen(view);
1650 err = view_splitscreen(view);
1651 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1652 err = view_resize(view->parent);
1654 err = view_resize_split(view, 0);
1658 err = view->input(new, view, KEY_RESIZE);
1663 err = view->resize(view, 0);
1668 err = offset_selection_down(view->parent);
1670 err = offset_selection_down(view);
1674 err = switch_split(view);
1677 err = view_resize_split(view, -1);
1680 err = view_resize_split(view, 1);
1686 if (view->search_start)
1687 view_search_start(view);
1689 err = view->input(new, view, ch);
1693 if (view->search_started && view->search_next) {
1694 view->searching = (ch == 'n' ?
1695 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1696 view->search_next_done = 0;
1697 view->search_next(view);
1699 err = view->input(new, view, ch);
1702 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1703 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1704 view->action = "Patience diff algorithm";
1706 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1707 view->action = "Myers diff algorithm";
1709 TAILQ_FOREACH(v, views, entry) {
1715 if (v->child && v->child->reset) {
1716 err = v->child->reset(v->child);
1723 err = view->input(new, view, ch);
1731 view_needs_focus_indication(struct tog_view *view)
1733 if (view_is_parent_view(view)) {
1734 if (view->child == NULL || view->child->focussed)
1736 if (!view_is_splitscreen(view->child))
1738 } else if (!view_is_splitscreen(view))
1741 return view->focussed;
1744 static const struct got_error *
1745 view_loop(struct tog_view *view)
1747 const struct got_error *err = NULL;
1748 struct tog_view_list_head views;
1749 struct tog_view *new_view;
1751 int fast_refresh = 10;
1752 int done = 0, errcode;
1754 mode = getenv("TOG_VIEW_SPLIT_MODE");
1755 if (!mode || !(*mode == 'h' || *mode == 'H'))
1756 view->mode = TOG_VIEW_SPLIT_VERT;
1758 view->mode = TOG_VIEW_SPLIT_HRZN;
1760 errcode = pthread_mutex_lock(&tog_mutex);
1762 return got_error_set_errno(errcode, "pthread_mutex_lock");
1765 TAILQ_INSERT_HEAD(&views, view, entry);
1768 err = view->show(view);
1773 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1774 !tog_fatal_signal_received()) {
1775 /* Refresh fast during initialization, then become slower. */
1776 if (fast_refresh && --fast_refresh == 0)
1777 halfdelay(10); /* switch to once per second */
1779 err = view_input(&new_view, &done, view, &views);
1783 if (view->dying && view == TAILQ_FIRST(&views) &&
1784 TAILQ_NEXT(view, entry) == NULL)
1790 * When we quit, scroll the screen up a single line
1791 * so we don't lose any information.
1793 TAILQ_FOREACH(v, &views, entry) {
1794 wmove(v->window, 0, 0);
1795 wdeleteln(v->window);
1796 wnoutrefresh(v->window);
1797 if (v->child && !view_is_fullscreen(v)) {
1798 wmove(v->child->window, 0, 0);
1799 wdeleteln(v->child->window);
1800 wnoutrefresh(v->child->window);
1807 struct tog_view *v, *prev = NULL;
1809 if (view_is_parent_view(view))
1810 prev = TAILQ_PREV(view, tog_view_list_head,
1812 else if (view->parent)
1813 prev = view->parent;
1816 view->parent->child = NULL;
1817 view->parent->focus_child = 0;
1818 /* Restore fullscreen line height. */
1819 view->parent->nlines = view->parent->lines;
1820 err = view_resize(view->parent);
1823 /* Make resized splits persist. */
1824 view_transfer_size(view->parent, view);
1826 TAILQ_REMOVE(&views, view, entry);
1828 err = view_close(view);
1833 TAILQ_FOREACH(v, &views, entry) {
1837 if (view == NULL && new_view == NULL) {
1838 /* No view has focus. Try to pick one. */
1841 else if (!TAILQ_EMPTY(&views)) {
1842 view = TAILQ_LAST(&views,
1843 tog_view_list_head);
1846 if (view->focus_child) {
1847 view->child->focussed = 1;
1855 struct tog_view *v, *t;
1856 /* Only allow one parent view per type. */
1857 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1858 if (v->type != new_view->type)
1860 TAILQ_REMOVE(&views, v, entry);
1861 err = view_close(v);
1866 TAILQ_INSERT_TAIL(&views, new_view, entry);
1869 if (view && !done) {
1870 if (view_is_parent_view(view)) {
1871 if (view->child && view->child->focussed)
1874 if (view->parent && view->parent->focussed)
1875 view = view->parent;
1877 show_panel(view->panel);
1878 if (view->child && view_is_splitscreen(view->child))
1879 show_panel(view->child->panel);
1880 if (view->parent && view_is_splitscreen(view)) {
1881 err = view->parent->show(view->parent);
1885 err = view->show(view);
1889 err = view->child->show(view->child);
1898 while (!TAILQ_EMPTY(&views)) {
1899 const struct got_error *close_err;
1900 view = TAILQ_FIRST(&views);
1901 TAILQ_REMOVE(&views, view, entry);
1902 close_err = view_close(view);
1903 if (close_err && err == NULL)
1907 errcode = pthread_mutex_unlock(&tog_mutex);
1908 if (errcode && err == NULL)
1909 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1919 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1924 /* Create newly allocated wide-character string equivalent to a byte string. */
1925 static const struct got_error *
1926 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1929 const struct got_error *err = NULL;
1932 *wlen = mbstowcs(NULL, s, 0);
1933 if (*wlen == (size_t)-1) {
1935 if (errno != EILSEQ)
1936 return got_error_from_errno("mbstowcs");
1938 /* byte string invalid in current encoding; try to "fix" it */
1939 err = got_mbsavis(&vis, &vislen, s);
1942 *wlen = mbstowcs(NULL, vis, 0);
1943 if (*wlen == (size_t)-1) {
1944 err = got_error_from_errno("mbstowcs"); /* give up */
1949 *ws = calloc(*wlen + 1, sizeof(**ws));
1951 err = got_error_from_errno("calloc");
1955 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1956 err = got_error_from_errno("mbstowcs");
1967 static const struct got_error *
1968 expand_tab(char **ptr, const char *src)
1971 size_t len, n, idx = 0, sz = 0;
1974 n = len = strlen(src);
1975 dst = malloc(n + 1);
1977 return got_error_from_errno("malloc");
1979 while (idx < len && src[idx]) {
1980 const char c = src[idx];
1983 size_t nb = TABSIZE - sz % TABSIZE;
1986 p = realloc(dst, n + nb);
1989 return got_error_from_errno("realloc");
1994 memset(dst + sz, ' ', nb);
1997 dst[sz++] = src[idx];
2007 * Advance at most n columns from wline starting at offset off.
2008 * Return the index to the first character after the span operation.
2009 * Return the combined column width of all spanned wide character in
2013 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2015 int width, i, cols = 0;
2022 for (i = off; wline[i] != L'\0'; ++i) {
2023 if (wline[i] == L'\t')
2024 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2026 width = wcwidth(wline[i]);
2033 if (cols + width > n)
2043 * Format a line for display, ensuring that it won't overflow a width limit.
2044 * With scrolling, the width returned refers to the scrolled version of the
2045 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2047 static const struct got_error *
2048 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2049 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2051 const struct got_error *err = NULL;
2053 wchar_t *wline = NULL;
2062 err = expand_tab(&exstr, line);
2067 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2072 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2074 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2075 wline[wlen - 1] = L'\0';
2078 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2079 wline[wlen - 1] = L'\0';
2083 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2089 *scrollxp = scrollx;
2097 static const struct got_error*
2098 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2099 struct got_object_id *id, struct got_repository *repo)
2101 static const struct got_error *err = NULL;
2102 struct got_reflist_entry *re;
2108 TAILQ_FOREACH(re, refs, entry) {
2109 struct got_tag_object *tag = NULL;
2110 struct got_object_id *ref_id;
2113 name = got_ref_get_name(re->ref);
2114 if (strcmp(name, GOT_REF_HEAD) == 0)
2116 if (strncmp(name, "refs/", 5) == 0)
2118 if (strncmp(name, "got/", 4) == 0 &&
2119 strncmp(name, "got/backup/", 11) != 0)
2121 if (strncmp(name, "heads/", 6) == 0)
2123 if (strncmp(name, "remotes/", 8) == 0) {
2125 s = strstr(name, "/" GOT_REF_HEAD);
2126 if (s != NULL && s[strlen(s)] == '\0')
2129 err = got_ref_resolve(&ref_id, repo, re->ref);
2132 if (strncmp(name, "tags/", 5) == 0) {
2133 err = got_object_open_as_tag(&tag, repo, ref_id);
2135 if (err->code != GOT_ERR_OBJ_TYPE) {
2139 /* Ref points at something other than a tag. */
2144 cmp = got_object_id_cmp(tag ?
2145 got_object_tag_get_object_id(tag) : ref_id, id);
2148 got_object_tag_close(tag);
2152 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2153 s ? ", " : "", name) == -1) {
2154 err = got_error_from_errno("asprintf");
2165 static const struct got_error *
2166 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2171 smallerthan = strchr(author, '<');
2172 if (smallerthan && smallerthan[1] != '\0')
2173 author = smallerthan + 1;
2174 author[strcspn(author, "@>")] = '\0';
2175 return format_line(wauthor, author_width, NULL, author, 0, limit,
2179 static const struct got_error *
2180 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2181 struct got_object_id *id, const size_t date_display_cols,
2182 int author_display_cols)
2184 struct tog_log_view_state *s = &view->state.log;
2185 const struct got_error *err = NULL;
2186 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2187 char *logmsg0 = NULL, *logmsg = NULL;
2188 char *author = NULL;
2189 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2190 int author_width, logmsg_width;
2191 char *newline, *line = NULL;
2192 int col, limit, scrollx;
2193 const int avail = view->ncols;
2195 time_t committer_time;
2196 struct tog_color *tc;
2198 committer_time = got_object_commit_get_committer_time(commit);
2199 if (gmtime_r(&committer_time, &tm) == NULL)
2200 return got_error_from_errno("gmtime_r");
2201 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2202 return got_error(GOT_ERR_NO_SPACE);
2204 if (avail <= date_display_cols)
2205 limit = MIN(sizeof(datebuf) - 1, avail);
2207 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2208 tc = get_color(&s->colors, TOG_COLOR_DATE);
2210 wattr_on(view->window,
2211 COLOR_PAIR(tc->colorpair), NULL);
2212 waddnstr(view->window, datebuf, limit);
2214 wattr_off(view->window,
2215 COLOR_PAIR(tc->colorpair), NULL);
2222 err = got_object_id_str(&id_str, id);
2225 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2227 wattr_on(view->window,
2228 COLOR_PAIR(tc->colorpair), NULL);
2229 wprintw(view->window, "%.8s ", id_str);
2231 wattr_off(view->window,
2232 COLOR_PAIR(tc->colorpair), NULL);
2239 if (s->use_committer)
2240 author = strdup(got_object_commit_get_committer(commit));
2242 author = strdup(got_object_commit_get_author(commit));
2243 if (author == NULL) {
2244 err = got_error_from_errno("strdup");
2247 err = format_author(&wauthor, &author_width, author, avail - col, col);
2250 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2252 wattr_on(view->window,
2253 COLOR_PAIR(tc->colorpair), NULL);
2254 waddwstr(view->window, wauthor);
2255 col += author_width;
2256 while (col < avail && author_width < author_display_cols + 2) {
2257 waddch(view->window, ' ');
2262 wattr_off(view->window,
2263 COLOR_PAIR(tc->colorpair), NULL);
2267 err = got_object_commit_get_logmsg(&logmsg0, commit);
2271 while (*logmsg == '\n')
2273 newline = strchr(logmsg, '\n');
2276 limit = avail - col;
2277 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2278 limit--; /* for the border */
2279 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2283 waddwstr(view->window, &wlogmsg[scrollx]);
2284 col += MAX(logmsg_width, 0);
2285 while (col < avail) {
2286 waddch(view->window, ' ');
2298 static struct commit_queue_entry *
2299 alloc_commit_queue_entry(struct got_commit_object *commit,
2300 struct got_object_id *id)
2302 struct commit_queue_entry *entry;
2303 struct got_object_id *dup;
2305 entry = calloc(1, sizeof(*entry));
2309 dup = got_object_id_dup(id);
2316 entry->commit = commit;
2321 pop_commit(struct commit_queue *commits)
2323 struct commit_queue_entry *entry;
2325 entry = TAILQ_FIRST(&commits->head);
2326 TAILQ_REMOVE(&commits->head, entry, entry);
2327 got_object_commit_close(entry->commit);
2328 commits->ncommits--;
2334 free_commits(struct commit_queue *commits)
2336 while (!TAILQ_EMPTY(&commits->head))
2337 pop_commit(commits);
2340 static const struct got_error *
2341 match_commit(int *have_match, struct got_object_id *id,
2342 struct got_commit_object *commit, regex_t *regex)
2344 const struct got_error *err = NULL;
2345 regmatch_t regmatch;
2346 char *id_str = NULL, *logmsg = NULL;
2350 err = got_object_id_str(&id_str, id);
2354 err = got_object_commit_get_logmsg(&logmsg, commit);
2358 if (regexec(regex, got_object_commit_get_author(commit), 1,
2359 ®match, 0) == 0 ||
2360 regexec(regex, got_object_commit_get_committer(commit), 1,
2361 ®match, 0) == 0 ||
2362 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2363 regexec(regex, logmsg, 1, ®match, 0) == 0)
2371 static const struct got_error *
2372 queue_commits(struct tog_log_thread_args *a)
2374 const struct got_error *err = NULL;
2377 * We keep all commits open throughout the lifetime of the log
2378 * view in order to avoid having to re-fetch commits from disk
2379 * while updating the display.
2382 struct got_object_id id;
2383 struct got_commit_object *commit;
2384 struct commit_queue_entry *entry;
2385 int limit_match = 0;
2388 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2393 err = got_object_open_as_commit(&commit, a->repo, &id);
2396 entry = alloc_commit_queue_entry(commit, &id);
2397 if (entry == NULL) {
2398 err = got_error_from_errno("alloc_commit_queue_entry");
2402 errcode = pthread_mutex_lock(&tog_mutex);
2404 err = got_error_set_errno(errcode,
2405 "pthread_mutex_lock");
2409 entry->idx = a->real_commits->ncommits;
2410 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2411 a->real_commits->ncommits++;
2414 err = match_commit(&limit_match, &id, commit,
2420 struct commit_queue_entry *matched;
2422 matched = alloc_commit_queue_entry(
2423 entry->commit, entry->id);
2424 if (matched == NULL) {
2425 err = got_error_from_errno(
2426 "alloc_commit_queue_entry");
2429 matched->commit = entry->commit;
2430 got_object_commit_retain(entry->commit);
2432 matched->idx = a->limit_commits->ncommits;
2433 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2435 a->limit_commits->ncommits++;
2439 * This is how we signal log_thread() that we
2440 * have found a match, and that it should be
2441 * counted as a new entry for the view.
2443 a->limit_match = limit_match;
2446 if (*a->searching == TOG_SEARCH_FORWARD &&
2447 !*a->search_next_done) {
2449 err = match_commit(&have_match, &id, commit, a->regex);
2454 if (limit_match && have_match)
2455 *a->search_next_done =
2456 TOG_SEARCH_HAVE_MORE;
2457 } else if (have_match)
2458 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2461 errcode = pthread_mutex_unlock(&tog_mutex);
2462 if (errcode && err == NULL)
2463 err = got_error_set_errno(errcode,
2464 "pthread_mutex_unlock");
2467 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2473 select_commit(struct tog_log_view_state *s)
2475 struct commit_queue_entry *entry;
2478 entry = s->first_displayed_entry;
2480 if (ncommits == s->selected) {
2481 s->selected_entry = entry;
2484 entry = TAILQ_NEXT(entry, entry);
2489 static const struct got_error *
2490 draw_commits(struct tog_view *view)
2492 const struct got_error *err = NULL;
2493 struct tog_log_view_state *s = &view->state.log;
2494 struct commit_queue_entry *entry = s->selected_entry;
2495 int limit = view->nlines;
2497 int ncommits, author_cols = 4;
2498 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2499 char *refs_str = NULL;
2501 struct tog_color *tc;
2502 static const size_t date_display_cols = 12;
2504 if (view_is_hsplit_top(view))
2505 --limit; /* account for border */
2507 if (s->selected_entry &&
2508 !(view->searching && view->search_next_done == 0)) {
2509 struct got_reflist_head *refs;
2510 err = got_object_id_str(&id_str, s->selected_entry->id);
2513 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2514 s->selected_entry->id);
2516 err = build_refs_str(&refs_str, refs,
2517 s->selected_entry->id, s->repo);
2523 if (s->thread_args.commits_needed == 0)
2524 halfdelay(10); /* disable fast refresh */
2526 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2527 if (asprintf(&ncommits_str, " [%d/%d] %s",
2528 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2529 (view->searching && !view->search_next_done) ?
2530 "searching..." : "loading...") == -1) {
2531 err = got_error_from_errno("asprintf");
2535 const char *search_str = NULL;
2536 const char *limit_str = NULL;
2538 if (view->searching) {
2539 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2540 search_str = "no more matches";
2541 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2542 search_str = "no matches found";
2543 else if (!view->search_next_done)
2544 search_str = "searching...";
2547 if (s->limit_view && s->commits->ncommits == 0)
2548 limit_str = "no matches found";
2550 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2551 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2552 search_str ? search_str : (refs_str ? refs_str : ""),
2553 limit_str ? limit_str : "") == -1) {
2554 err = got_error_from_errno("asprintf");
2559 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2560 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2561 "........................................",
2562 s->in_repo_path, ncommits_str) == -1) {
2563 err = got_error_from_errno("asprintf");
2567 } else if (asprintf(&header, "commit %s%s",
2568 id_str ? id_str : "........................................",
2569 ncommits_str) == -1) {
2570 err = got_error_from_errno("asprintf");
2574 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2578 werase(view->window);
2580 if (view_needs_focus_indication(view))
2581 wstandout(view->window);
2582 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2584 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2585 waddwstr(view->window, wline);
2586 while (width < view->ncols) {
2587 waddch(view->window, ' ');
2591 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2592 if (view_needs_focus_indication(view))
2593 wstandend(view->window);
2598 /* Grow author column size if necessary, and set view->maxx. */
2599 entry = s->first_displayed_entry;
2603 struct got_commit_object *c = entry->commit;
2604 char *author, *eol, *msg, *msg0;
2605 wchar_t *wauthor, *wmsg;
2607 if (ncommits >= limit - 1)
2609 if (s->use_committer)
2610 author = strdup(got_object_commit_get_committer(c));
2612 author = strdup(got_object_commit_get_author(c));
2613 if (author == NULL) {
2614 err = got_error_from_errno("strdup");
2617 err = format_author(&wauthor, &width, author, COLS,
2619 if (author_cols < width)
2620 author_cols = width;
2625 err = got_object_commit_get_logmsg(&msg0, c);
2629 while (*msg == '\n')
2631 if ((eol = strchr(msg, '\n')))
2633 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2634 date_display_cols + author_cols, 0);
2637 view->maxx = MAX(view->maxx, width);
2641 entry = TAILQ_NEXT(entry, entry);
2644 entry = s->first_displayed_entry;
2645 s->last_displayed_entry = s->first_displayed_entry;
2648 if (ncommits >= limit - 1)
2650 if (ncommits == s->selected)
2651 wstandout(view->window);
2652 err = draw_commit(view, entry->commit, entry->id,
2653 date_display_cols, author_cols);
2654 if (ncommits == s->selected)
2655 wstandend(view->window);
2659 s->last_displayed_entry = entry;
2660 entry = TAILQ_NEXT(entry, entry);
2673 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2675 struct commit_queue_entry *entry;
2678 entry = TAILQ_FIRST(&s->commits->head);
2679 if (s->first_displayed_entry == entry)
2682 entry = s->first_displayed_entry;
2683 while (entry && nscrolled < maxscroll) {
2684 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2686 s->first_displayed_entry = entry;
2692 static const struct got_error *
2693 trigger_log_thread(struct tog_view *view, int wait)
2695 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2698 halfdelay(1); /* fast refresh while loading commits */
2700 while (!ta->log_complete && !tog_thread_error &&
2701 (ta->commits_needed > 0 || ta->load_all)) {
2702 /* Wake the log thread. */
2703 errcode = pthread_cond_signal(&ta->need_commits);
2705 return got_error_set_errno(errcode,
2706 "pthread_cond_signal");
2709 * The mutex will be released while the view loop waits
2710 * in wgetch(), at which time the log thread will run.
2715 /* Display progress update in log view. */
2716 show_log_view(view);
2720 /* Wait right here while next commit is being loaded. */
2721 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2723 return got_error_set_errno(errcode,
2724 "pthread_cond_wait");
2726 /* Display progress update in log view. */
2727 show_log_view(view);
2735 static const struct got_error *
2736 request_log_commits(struct tog_view *view)
2738 struct tog_log_view_state *state = &view->state.log;
2739 const struct got_error *err = NULL;
2741 if (state->thread_args.log_complete)
2744 state->thread_args.commits_needed += view->nscrolled;
2745 err = trigger_log_thread(view, 1);
2746 view->nscrolled = 0;
2751 static const struct got_error *
2752 log_scroll_down(struct tog_view *view, int maxscroll)
2754 struct tog_log_view_state *s = &view->state.log;
2755 const struct got_error *err = NULL;
2756 struct commit_queue_entry *pentry;
2757 int nscrolled = 0, ncommits_needed;
2759 if (s->last_displayed_entry == NULL)
2762 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2763 if (s->commits->ncommits < ncommits_needed &&
2764 !s->thread_args.log_complete) {
2766 * Ask the log thread for required amount of commits.
2768 s->thread_args.commits_needed +=
2769 ncommits_needed - s->commits->ncommits;
2770 err = trigger_log_thread(view, 1);
2776 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2777 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2780 s->last_displayed_entry = pentry ?
2781 pentry : s->last_displayed_entry;
2783 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2786 s->first_displayed_entry = pentry;
2787 } while (++nscrolled < maxscroll);
2789 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2790 view->nscrolled += nscrolled;
2792 view->nscrolled = 0;
2797 static const struct got_error *
2798 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2799 struct got_commit_object *commit, struct got_object_id *commit_id,
2800 struct tog_view *log_view, struct got_repository *repo)
2802 const struct got_error *err;
2803 struct got_object_qid *parent_id;
2804 struct tog_view *diff_view;
2806 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2807 if (diff_view == NULL)
2808 return got_error_from_errno("view_open");
2810 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2811 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2812 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2814 *new_view = diff_view;
2818 static const struct got_error *
2819 tree_view_visit_subtree(struct tog_tree_view_state *s,
2820 struct got_tree_object *subtree)
2822 struct tog_parent_tree *parent;
2824 parent = calloc(1, sizeof(*parent));
2826 return got_error_from_errno("calloc");
2828 parent->tree = s->tree;
2829 parent->first_displayed_entry = s->first_displayed_entry;
2830 parent->selected_entry = s->selected_entry;
2831 parent->selected = s->selected;
2832 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2835 s->first_displayed_entry = NULL;
2839 static const struct got_error *
2840 tree_view_walk_path(struct tog_tree_view_state *s,
2841 struct got_commit_object *commit, const char *path)
2843 const struct got_error *err = NULL;
2844 struct got_tree_object *tree = NULL;
2846 char *slash, *subpath = NULL;
2848 /* Walk the path and open corresponding tree objects. */
2851 struct got_tree_entry *te;
2852 struct got_object_id *tree_id;
2858 /* Ensure the correct subtree entry is selected. */
2859 slash = strchr(p, '/');
2861 te_name = strdup(p);
2863 te_name = strndup(p, slash - p);
2864 if (te_name == NULL) {
2865 err = got_error_from_errno("strndup");
2868 te = got_object_tree_find_entry(s->tree, te_name);
2870 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2875 s->first_displayed_entry = s->selected_entry = te;
2877 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2878 break; /* jump to this file's entry */
2880 slash = strchr(p, '/');
2882 subpath = strndup(path, slash - path);
2884 subpath = strdup(path);
2885 if (subpath == NULL) {
2886 err = got_error_from_errno("strdup");
2890 err = got_object_id_by_path(&tree_id, s->repo, commit,
2895 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2900 err = tree_view_visit_subtree(s, tree);
2902 got_object_tree_close(tree);
2916 static const struct got_error *
2917 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2918 struct commit_queue_entry *entry, const char *path,
2919 const char *head_ref_name, struct got_repository *repo)
2921 const struct got_error *err = NULL;
2922 struct tog_tree_view_state *s;
2923 struct tog_view *tree_view;
2925 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2926 if (tree_view == NULL)
2927 return got_error_from_errno("view_open");
2929 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2932 s = &tree_view->state.tree;
2934 *new_view = tree_view;
2936 if (got_path_is_root_dir(path))
2939 return tree_view_walk_path(s, entry->commit, path);
2942 static const struct got_error *
2943 block_signals_used_by_main_thread(void)
2948 if (sigemptyset(&sigset) == -1)
2949 return got_error_from_errno("sigemptyset");
2951 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2952 if (sigaddset(&sigset, SIGWINCH) == -1)
2953 return got_error_from_errno("sigaddset");
2954 if (sigaddset(&sigset, SIGCONT) == -1)
2955 return got_error_from_errno("sigaddset");
2956 if (sigaddset(&sigset, SIGINT) == -1)
2957 return got_error_from_errno("sigaddset");
2958 if (sigaddset(&sigset, SIGTERM) == -1)
2959 return got_error_from_errno("sigaddset");
2961 /* ncurses handles SIGTSTP */
2962 if (sigaddset(&sigset, SIGTSTP) == -1)
2963 return got_error_from_errno("sigaddset");
2965 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2967 return got_error_set_errno(errcode, "pthread_sigmask");
2973 log_thread(void *arg)
2975 const struct got_error *err = NULL;
2977 struct tog_log_thread_args *a = arg;
2981 * Sync startup with main thread such that we begin our
2982 * work once view_input() has released the mutex.
2984 errcode = pthread_mutex_lock(&tog_mutex);
2986 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2990 err = block_signals_used_by_main_thread();
2992 pthread_mutex_unlock(&tog_mutex);
2996 while (!done && !err && !tog_fatal_signal_received()) {
2997 errcode = pthread_mutex_unlock(&tog_mutex);
2999 err = got_error_set_errno(errcode,
3000 "pthread_mutex_unlock");
3003 err = queue_commits(a);
3005 if (err->code != GOT_ERR_ITER_COMPLETED)
3009 } else if (a->commits_needed > 0 && !a->load_all) {
3012 a->commits_needed--;
3014 a->commits_needed--;
3017 errcode = pthread_mutex_lock(&tog_mutex);
3019 err = got_error_set_errno(errcode,
3020 "pthread_mutex_lock");
3022 } else if (*a->quit)
3024 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3025 *a->first_displayed_entry =
3026 TAILQ_FIRST(&a->limit_commits->head);
3027 *a->selected_entry = *a->first_displayed_entry;
3028 } else if (*a->first_displayed_entry == NULL) {
3029 *a->first_displayed_entry =
3030 TAILQ_FIRST(&a->real_commits->head);
3031 *a->selected_entry = *a->first_displayed_entry;
3034 errcode = pthread_cond_signal(&a->commit_loaded);
3036 err = got_error_set_errno(errcode,
3037 "pthread_cond_signal");
3038 pthread_mutex_unlock(&tog_mutex);
3043 a->commits_needed = 0;
3045 if (a->commits_needed == 0 && !a->load_all) {
3046 errcode = pthread_cond_wait(&a->need_commits,
3049 err = got_error_set_errno(errcode,
3050 "pthread_cond_wait");
3051 pthread_mutex_unlock(&tog_mutex);
3059 a->log_complete = 1;
3060 errcode = pthread_mutex_unlock(&tog_mutex);
3062 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3065 tog_thread_error = 1;
3066 pthread_cond_signal(&a->commit_loaded);
3071 static const struct got_error *
3072 stop_log_thread(struct tog_log_view_state *s)
3074 const struct got_error *err = NULL, *thread_err = NULL;
3079 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3081 return got_error_set_errno(errcode,
3082 "pthread_cond_signal");
3083 errcode = pthread_mutex_unlock(&tog_mutex);
3085 return got_error_set_errno(errcode,
3086 "pthread_mutex_unlock");
3087 errcode = pthread_join(s->thread, (void **)&thread_err);
3089 return got_error_set_errno(errcode, "pthread_join");
3090 errcode = pthread_mutex_lock(&tog_mutex);
3092 return got_error_set_errno(errcode,
3093 "pthread_mutex_lock");
3097 if (s->thread_args.repo) {
3098 err = got_repo_close(s->thread_args.repo);
3099 s->thread_args.repo = NULL;
3102 if (s->thread_args.pack_fds) {
3103 const struct got_error *pack_err =
3104 got_repo_pack_fds_close(s->thread_args.pack_fds);
3107 s->thread_args.pack_fds = NULL;
3110 if (s->thread_args.graph) {
3111 got_commit_graph_close(s->thread_args.graph);
3112 s->thread_args.graph = NULL;
3115 return err ? err : thread_err;
3118 static const struct got_error *
3119 close_log_view(struct tog_view *view)
3121 const struct got_error *err = NULL;
3122 struct tog_log_view_state *s = &view->state.log;
3125 err = stop_log_thread(s);
3127 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3128 if (errcode && err == NULL)
3129 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3131 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3132 if (errcode && err == NULL)
3133 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3135 free_commits(&s->limit_commits);
3136 free_commits(&s->real_commits);
3137 free(s->in_repo_path);
3138 s->in_repo_path = NULL;
3141 free(s->head_ref_name);
3142 s->head_ref_name = NULL;
3147 * We use two queues to implement the limit feature: first consists of
3148 * commits matching the current limit_regex; second is the real queue
3149 * of all known commits (real_commits). When the user starts limiting,
3150 * we swap queues such that all movement and displaying functionality
3151 * works with very slight change.
3153 static const struct got_error *
3154 limit_log_view(struct tog_view *view)
3156 struct tog_log_view_state *s = &view->state.log;
3157 struct commit_queue_entry *entry;
3158 struct tog_view *v = view;
3159 const struct got_error *err = NULL;
3163 if (view_is_hsplit_top(view))
3165 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3168 /* Get the pattern */
3169 wmove(v->window, v->nlines - 1, 0);
3170 wclrtoeol(v->window);
3171 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3172 nodelay(v->window, FALSE);
3175 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3178 nodelay(v->window, TRUE);
3182 if (*pattern == '\0') {
3184 * Safety measure for the situation where the user
3185 * resets limit without previously limiting anything.
3191 * User could have pressed Ctrl+L, which refreshed the
3192 * commit queues, it means we can't save previously
3193 * (before limit took place) displayed entries,
3194 * because they would point to already free'ed memory,
3195 * so we are forced to always select first entry of
3198 s->commits = &s->real_commits;
3199 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3200 s->selected_entry = s->first_displayed_entry;
3207 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3212 /* Clear the screen while loading limit view */
3213 s->first_displayed_entry = NULL;
3214 s->last_displayed_entry = NULL;
3215 s->selected_entry = NULL;
3216 s->commits = &s->limit_commits;
3218 /* Prepare limit queue for new search */
3219 free_commits(&s->limit_commits);
3220 s->limit_commits.ncommits = 0;
3222 /* First process commits, which are in queue already */
3223 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3226 err = match_commit(&have_match, entry->id,
3227 entry->commit, &s->limit_regex);
3232 struct commit_queue_entry *matched;
3234 matched = alloc_commit_queue_entry(entry->commit,
3236 if (matched == NULL) {
3237 err = got_error_from_errno(
3238 "alloc_commit_queue_entry");
3241 matched->commit = entry->commit;
3242 got_object_commit_retain(entry->commit);
3244 matched->idx = s->limit_commits.ncommits;
3245 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3247 s->limit_commits.ncommits++;
3251 /* Second process all the commits, until we fill the screen */
3252 if (s->limit_commits.ncommits < view->nlines - 1 &&
3253 !s->thread_args.log_complete) {
3254 s->thread_args.commits_needed +=
3255 view->nlines - s->limit_commits.ncommits - 1;
3256 err = trigger_log_thread(view, 1);
3261 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3262 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3268 static const struct got_error *
3269 search_start_log_view(struct tog_view *view)
3271 struct tog_log_view_state *s = &view->state.log;
3273 s->matched_entry = NULL;
3274 s->search_entry = NULL;
3278 static const struct got_error *
3279 search_next_log_view(struct tog_view *view)
3281 const struct got_error *err = NULL;
3282 struct tog_log_view_state *s = &view->state.log;
3283 struct commit_queue_entry *entry;
3285 /* Display progress update in log view. */
3286 show_log_view(view);
3290 if (s->search_entry) {
3292 errcode = pthread_mutex_unlock(&tog_mutex);
3294 return got_error_set_errno(errcode,
3295 "pthread_mutex_unlock");
3296 ch = wgetch(view->window);
3297 errcode = pthread_mutex_lock(&tog_mutex);
3299 return got_error_set_errno(errcode,
3300 "pthread_mutex_lock");
3301 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3302 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3305 if (view->searching == TOG_SEARCH_FORWARD)
3306 entry = TAILQ_NEXT(s->search_entry, entry);
3308 entry = TAILQ_PREV(s->search_entry,
3309 commit_queue_head, entry);
3310 } else if (s->matched_entry) {
3312 * If the user has moved the cursor after we hit a match,
3313 * the position from where we should continue searching
3314 * might have changed.
3316 if (view->searching == TOG_SEARCH_FORWARD)
3317 entry = TAILQ_NEXT(s->selected_entry, entry);
3319 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3322 entry = s->selected_entry;
3328 if (entry == NULL) {
3329 if (s->thread_args.log_complete ||
3330 view->searching == TOG_SEARCH_BACKWARD) {
3331 view->search_next_done =
3332 (s->matched_entry == NULL ?
3333 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3334 s->search_entry = NULL;
3338 * Poke the log thread for more commits and return,
3339 * allowing the main loop to make progress. Search
3340 * will resume at s->search_entry once we come back.
3342 s->thread_args.commits_needed++;
3343 return trigger_log_thread(view, 0);
3346 err = match_commit(&have_match, entry->id, entry->commit,
3351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3352 s->matched_entry = entry;
3356 s->search_entry = entry;
3357 if (view->searching == TOG_SEARCH_FORWARD)
3358 entry = TAILQ_NEXT(entry, entry);
3360 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3363 if (s->matched_entry) {
3364 int cur = s->selected_entry->idx;
3365 while (cur < s->matched_entry->idx) {
3366 err = input_log_view(NULL, view, KEY_DOWN);
3371 while (cur > s->matched_entry->idx) {
3372 err = input_log_view(NULL, view, KEY_UP);
3379 s->search_entry = NULL;
3384 static const struct got_error *
3385 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3386 struct got_repository *repo, const char *head_ref_name,
3387 const char *in_repo_path, int log_branches)
3389 const struct got_error *err = NULL;
3390 struct tog_log_view_state *s = &view->state.log;
3391 struct got_repository *thread_repo = NULL;
3392 struct got_commit_graph *thread_graph = NULL;
3395 if (in_repo_path != s->in_repo_path) {
3396 free(s->in_repo_path);
3397 s->in_repo_path = strdup(in_repo_path);
3398 if (s->in_repo_path == NULL)
3399 return got_error_from_errno("strdup");
3402 /* The commit queue only contains commits being displayed. */
3403 TAILQ_INIT(&s->real_commits.head);
3404 s->real_commits.ncommits = 0;
3405 s->commits = &s->real_commits;
3407 TAILQ_INIT(&s->limit_commits.head);
3409 s->limit_commits.ncommits = 0;
3412 if (head_ref_name) {
3413 s->head_ref_name = strdup(head_ref_name);
3414 if (s->head_ref_name == NULL) {
3415 err = got_error_from_errno("strdup");
3419 s->start_id = got_object_id_dup(start_id);
3420 if (s->start_id == NULL) {
3421 err = got_error_from_errno("got_object_id_dup");
3424 s->log_branches = log_branches;
3425 s->use_committer = 1;
3427 STAILQ_INIT(&s->colors);
3428 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3429 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3430 get_color_value("TOG_COLOR_COMMIT"));
3433 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3434 get_color_value("TOG_COLOR_AUTHOR"));
3436 free_colors(&s->colors);
3439 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3440 get_color_value("TOG_COLOR_DATE"));
3442 free_colors(&s->colors);
3447 view->show = show_log_view;
3448 view->input = input_log_view;
3449 view->resize = resize_log_view;
3450 view->close = close_log_view;
3451 view->search_start = search_start_log_view;
3452 view->search_next = search_next_log_view;
3454 if (s->thread_args.pack_fds == NULL) {
3455 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3459 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3460 s->thread_args.pack_fds);
3463 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3467 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3468 s->repo, NULL, NULL);
3472 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3474 err = got_error_set_errno(errcode, "pthread_cond_init");
3477 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3479 err = got_error_set_errno(errcode, "pthread_cond_init");
3483 s->thread_args.commits_needed = view->nlines;
3484 s->thread_args.graph = thread_graph;
3485 s->thread_args.real_commits = &s->real_commits;
3486 s->thread_args.limit_commits = &s->limit_commits;
3487 s->thread_args.in_repo_path = s->in_repo_path;
3488 s->thread_args.start_id = s->start_id;
3489 s->thread_args.repo = thread_repo;
3490 s->thread_args.log_complete = 0;
3491 s->thread_args.quit = &s->quit;
3492 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3493 s->thread_args.selected_entry = &s->selected_entry;
3494 s->thread_args.searching = &view->searching;
3495 s->thread_args.search_next_done = &view->search_next_done;
3496 s->thread_args.regex = &view->regex;
3497 s->thread_args.limiting = &s->limit_view;
3498 s->thread_args.limit_regex = &s->limit_regex;
3499 s->thread_args.limit_commits = &s->limit_commits;
3502 close_log_view(view);
3506 static const struct got_error *
3507 show_log_view(struct tog_view *view)
3509 const struct got_error *err;
3510 struct tog_log_view_state *s = &view->state.log;
3512 if (s->thread == NULL) {
3513 int errcode = pthread_create(&s->thread, NULL, log_thread,
3516 return got_error_set_errno(errcode, "pthread_create");
3517 if (s->thread_args.commits_needed > 0) {
3518 err = trigger_log_thread(view, 1);
3524 return draw_commits(view);
3528 log_move_cursor_up(struct tog_view *view, int page, int home)
3530 struct tog_log_view_state *s = &view->state.log;
3532 if (s->first_displayed_entry == NULL)
3534 if (s->selected_entry->idx == 0)
3537 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3539 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3541 if (!page && !home && s->selected > 0)
3544 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3550 static const struct got_error *
3551 log_move_cursor_down(struct tog_view *view, int page)
3553 struct tog_log_view_state *s = &view->state.log;
3554 const struct got_error *err = NULL;
3555 int eos = view->nlines - 2;
3557 if (s->first_displayed_entry == NULL)
3560 if (s->thread_args.log_complete &&
3561 s->selected_entry->idx >= s->commits->ncommits - 1)
3564 if (view_is_hsplit_top(view))
3565 --eos; /* border consumes the last line */
3568 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3571 err = log_scroll_down(view, 1);
3572 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3573 struct commit_queue_entry *entry;
3577 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3578 s->last_displayed_entry = entry;
3579 for (n = 0; n <= eos; n++) {
3582 s->first_displayed_entry = entry;
3583 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3586 s->selected = n - 1;
3588 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3589 s->thread_args.log_complete)
3590 s->selected += MIN(page,
3591 s->commits->ncommits - s->selected_entry->idx - 1);
3593 err = log_scroll_down(view, page);
3599 * We might necessarily overshoot in horizontal
3600 * splits; if so, select the last displayed commit.
3602 if (s->first_displayed_entry && s->last_displayed_entry) {
3603 s->selected = MIN(s->selected,
3604 s->last_displayed_entry->idx -
3605 s->first_displayed_entry->idx);
3610 if (s->thread_args.log_complete &&
3611 s->selected_entry->idx == s->commits->ncommits - 1)
3618 view_get_split(struct tog_view *view, int *y, int *x)
3623 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3624 if (view->child && view->child->resized_y)
3625 *y = view->child->resized_y;
3626 else if (view->resized_y)
3627 *y = view->resized_y;
3629 *y = view_split_begin_y(view->lines);
3630 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3631 if (view->child && view->child->resized_x)
3632 *x = view->child->resized_x;
3633 else if (view->resized_x)
3634 *x = view->resized_x;
3636 *x = view_split_begin_x(view->begin_x);
3640 /* Split view horizontally at y and offset view->state->selected line. */
3641 static const struct got_error *
3642 view_init_hsplit(struct tog_view *view, int y)
3644 const struct got_error *err = NULL;
3648 err = view_resize(view);
3652 err = offset_selection_down(view);
3657 static const struct got_error *
3658 log_goto_line(struct tog_view *view, int nlines)
3660 const struct got_error *err = NULL;
3661 struct tog_log_view_state *s = &view->state.log;
3662 int g, idx = s->selected_entry->idx;
3664 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3670 if (g >= s->first_displayed_entry->idx + 1 &&
3671 g <= s->last_displayed_entry->idx + 1 &&
3672 g - s->first_displayed_entry->idx - 1 < nlines) {
3673 s->selected = g - s->first_displayed_entry->idx - 1;
3679 err = log_move_cursor_down(view, g - idx - 1);
3680 if (!err && g > s->selected_entry->idx + 1)
3681 err = log_move_cursor_down(view,
3682 g - s->first_displayed_entry->idx - 1);
3685 } else if (idx + 1 > g)
3686 log_move_cursor_up(view, idx - g + 1, 0);
3688 if (g < nlines && s->first_displayed_entry->idx == 0)
3689 s->selected = g - 1;
3697 horizontal_scroll_input(struct tog_view *view, int ch)
3703 view->x -= MIN(view->x, 2);
3709 if (view->x + view->ncols / 2 < view->maxx)
3718 view->x = MAX(view->maxx - view->ncols / 2, 0);
3726 static const struct got_error *
3727 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3729 const struct got_error *err = NULL;
3730 struct tog_log_view_state *s = &view->state.log;
3733 if (s->thread_args.load_all) {
3734 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3735 s->thread_args.load_all = 0;
3736 else if (s->thread_args.log_complete) {
3737 err = log_move_cursor_down(view, s->commits->ncommits);
3738 s->thread_args.load_all = 0;
3744 eos = nscroll = view->nlines - 1;
3745 if (view_is_hsplit_top(view))
3749 return log_goto_line(view, eos);
3753 err = limit_log_view(view);
3764 horizontal_scroll_input(view, ch);
3771 log_move_cursor_up(view, 0, 0);
3776 log_move_cursor_up(view, 0, 1);
3786 log_move_cursor_up(view, nscroll, 0);
3793 err = log_move_cursor_down(view, 0);
3796 s->use_committer = !s->use_committer;
3797 view->action = s->use_committer ?
3798 "show committer" : "show commit author";
3803 /* We don't know yet how many commits, so we're forced to
3804 * traverse them all. */
3806 s->thread_args.load_all = 1;
3807 if (!s->thread_args.log_complete)
3808 return trigger_log_thread(view, 0);
3809 err = log_move_cursor_down(view, s->commits->ncommits);
3810 s->thread_args.load_all = 0;
3821 err = log_move_cursor_down(view, nscroll);
3824 if (s->selected > view->nlines - 2)
3825 s->selected = view->nlines - 2;
3826 if (s->selected > s->commits->ncommits - 1)
3827 s->selected = s->commits->ncommits - 1;
3829 if (s->commits->ncommits < view->nlines - 1 &&
3830 !s->thread_args.log_complete) {
3831 s->thread_args.commits_needed += (view->nlines - 1) -
3832 s->commits->ncommits;
3833 err = trigger_log_thread(view, 1);
3839 if (s->selected_entry == NULL)
3841 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3845 if (s->selected_entry == NULL)
3847 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3853 if (ch == KEY_BACKSPACE &&
3854 got_path_is_root_dir(s->in_repo_path))
3856 err = stop_log_thread(s);
3859 if (ch == KEY_BACKSPACE) {
3861 err = got_path_dirname(&parent_path, s->in_repo_path);
3864 free(s->in_repo_path);
3865 s->in_repo_path = parent_path;
3866 s->thread_args.in_repo_path = s->in_repo_path;
3867 } else if (ch == CTRL('l')) {
3868 struct got_object_id *start_id;
3869 err = got_repo_match_object_id(&start_id, NULL,
3870 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3871 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3873 if (s->head_ref_name == NULL ||
3874 err->code != GOT_ERR_NOT_REF)
3876 /* Try to cope with deleted references. */
3877 free(s->head_ref_name);
3878 s->head_ref_name = NULL;
3879 err = got_repo_match_object_id(&start_id,
3880 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3881 &tog_refs, s->repo);
3886 s->start_id = start_id;
3887 s->thread_args.start_id = s->start_id;
3889 s->log_branches = !s->log_branches;
3891 if (s->thread_args.pack_fds == NULL) {
3892 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3896 err = got_repo_open(&s->thread_args.repo,
3897 got_repo_get_path(s->repo), NULL,
3898 s->thread_args.pack_fds);
3902 err = tog_load_refs(s->repo, 0);
3905 err = got_commit_graph_open(&s->thread_args.graph,
3906 s->in_repo_path, !s->log_branches);
3909 err = got_commit_graph_iter_start(s->thread_args.graph,
3910 s->start_id, s->repo, NULL, NULL);
3913 free_commits(&s->real_commits);
3914 free_commits(&s->limit_commits);
3915 s->first_displayed_entry = NULL;
3916 s->last_displayed_entry = NULL;
3917 s->selected_entry = NULL;
3919 s->thread_args.log_complete = 0;
3921 s->thread_args.commits_needed = view->lines;
3922 s->matched_entry = NULL;
3923 s->search_entry = NULL;
3928 err = view_request_new(new_view, view, TOG_VIEW_REF);
3938 static const struct got_error *
3939 apply_unveil(const char *repo_path, const char *worktree_path)
3941 const struct got_error *error;
3944 if (unveil("gmon.out", "rwc") != 0)
3945 return got_error_from_errno2("unveil", "gmon.out");
3947 if (repo_path && unveil(repo_path, "r") != 0)
3948 return got_error_from_errno2("unveil", repo_path);
3950 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3951 return got_error_from_errno2("unveil", worktree_path);
3953 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3954 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3956 error = got_privsep_unveil_exec_helpers();
3960 if (unveil(NULL, NULL) != 0)
3961 return got_error_from_errno("unveil");
3970 * Override default signal handlers before starting ncurses.
3971 * This should prevent ncurses from installing its own
3972 * broken cleanup() signal handler.
3974 signal(SIGWINCH, tog_sigwinch);
3975 signal(SIGPIPE, tog_sigpipe);
3976 signal(SIGCONT, tog_sigcont);
3977 signal(SIGINT, tog_sigint);
3978 signal(SIGTERM, tog_sigterm);
3982 halfdelay(1); /* Do fast refresh while initial view is loading. */
3985 intrflush(stdscr, FALSE);
3986 keypad(stdscr, TRUE);
3988 if (getenv("TOG_COLORS") != NULL) {
3990 use_default_colors();
3994 static const struct got_error *
3995 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3996 struct got_repository *repo, struct got_worktree *worktree)
3998 const struct got_error *err = NULL;
4001 *in_repo_path = strdup("/");
4002 if (*in_repo_path == NULL)
4003 return got_error_from_errno("strdup");
4008 const char *prefix = got_worktree_get_path_prefix(worktree);
4011 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4014 if (asprintf(in_repo_path, "%s%s%s", prefix,
4015 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4017 err = got_error_from_errno("asprintf");
4018 *in_repo_path = NULL;
4022 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4027 static const struct got_error *
4028 cmd_log(int argc, char *argv[])
4030 const struct got_error *error;
4031 struct got_repository *repo = NULL;
4032 struct got_worktree *worktree = NULL;
4033 struct got_object_id *start_id = NULL;
4034 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4035 char *start_commit = NULL, *label = NULL;
4036 struct got_reference *ref = NULL;
4037 const char *head_ref_name = NULL;
4038 int ch, log_branches = 0;
4039 struct tog_view *view;
4040 int *pack_fds = NULL;
4042 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4048 start_commit = optarg;
4051 repo_path = realpath(optarg, NULL);
4052 if (repo_path == NULL)
4053 return got_error_from_errno2("realpath",
4068 error = got_repo_pack_fds_open(&pack_fds);
4072 if (repo_path == NULL) {
4073 cwd = getcwd(NULL, 0);
4075 return got_error_from_errno("getcwd");
4076 error = got_worktree_open(&worktree, cwd);
4077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4081 strdup(got_worktree_get_repo_path(worktree));
4083 repo_path = strdup(cwd);
4084 if (repo_path == NULL) {
4085 error = got_error_from_errno("strdup");
4090 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4094 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4101 error = apply_unveil(got_repo_get_path(repo),
4102 worktree ? got_worktree_get_root_path(worktree) : NULL);
4106 /* already loaded by tog_log_with_path()? */
4107 if (TAILQ_EMPTY(&tog_refs)) {
4108 error = tog_load_refs(repo, 0);
4113 if (start_commit == NULL) {
4114 error = got_repo_match_object_id(&start_id, &label,
4115 worktree ? got_worktree_get_head_ref_name(worktree) :
4116 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4119 head_ref_name = label;
4121 error = got_ref_open(&ref, repo, start_commit, 0);
4123 head_ref_name = got_ref_get_name(ref);
4124 else if (error->code != GOT_ERR_NOT_REF)
4126 error = got_repo_match_object_id(&start_id, NULL,
4127 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4132 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4134 error = got_error_from_errno("view_open");
4137 error = open_log_view(view, start_id, repo, head_ref_name,
4138 in_repo_path, log_branches);
4142 /* Release work tree lock. */
4143 got_worktree_close(worktree);
4146 error = view_loop(view);
4156 const struct got_error *close_err = got_repo_close(repo);
4161 got_worktree_close(worktree);
4163 const struct got_error *pack_err =
4164 got_repo_pack_fds_close(pack_fds);
4176 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4177 "object1 object2\n", getprogname());
4182 match_line(const char *line, regex_t *regex, size_t nmatch,
4183 regmatch_t *regmatch)
4185 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4188 static struct tog_color *
4189 match_color(struct tog_colors *colors, const char *line)
4191 struct tog_color *tc = NULL;
4193 STAILQ_FOREACH(tc, colors, entry) {
4194 if (match_line(line, &tc->regex, 0, NULL))
4201 static const struct got_error *
4202 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4203 WINDOW *window, int skipcol, regmatch_t *regmatch)
4205 const struct got_error *err = NULL;
4207 wchar_t *wline = NULL;
4208 int rme, rms, n, width, scrollx;
4209 int width0 = 0, width1 = 0, width2 = 0;
4210 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4214 rms = regmatch->rm_so;
4215 rme = regmatch->rm_eo;
4217 err = expand_tab(&exstr, line);
4221 /* Split the line into 3 segments, according to match offsets. */
4222 seg0 = strndup(exstr, rms);
4224 err = got_error_from_errno("strndup");
4227 seg1 = strndup(exstr + rms, rme - rms);
4229 err = got_error_from_errno("strndup");
4232 seg2 = strdup(exstr + rme);
4234 err = got_error_from_errno("strndup");
4238 /* draw up to matched token if we haven't scrolled past it */
4239 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4243 n = MAX(width0 - skipcol, 0);
4246 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4247 wlimit, col_tab_align, 1);
4250 waddwstr(window, &wline[scrollx]);
4260 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4264 wlen = wcslen(wline);
4266 width = wcwidth(wline[i]);
4268 /* should not happen, tabs are expanded */
4269 err = got_error(GOT_ERR_RANGE);
4272 if (width0 + w + width > skipcol)
4277 /* draw (visible part of) matched token (if scrolled into it) */
4278 if (width1 - w > 0) {
4279 wattron(window, A_STANDOUT);
4280 waddwstr(window, &wline[i]);
4281 wattroff(window, A_STANDOUT);
4282 wlimit -= (width1 - w);
4283 *wtotal += (width1 - w);
4287 if (wlimit > 0) { /* draw rest of line */
4289 if (skipcol > width0 + width1) {
4290 err = format_line(&wline, &width2, &scrollx, seg2,
4291 skipcol - (width0 + width1), wlimit,
4295 waddwstr(window, &wline[scrollx]);
4297 err = format_line(&wline, &width2, NULL, seg2, 0,
4298 wlimit, col_tab_align, 1);
4301 waddwstr(window, wline);
4315 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4318 int *eof, *first, *selected;
4320 if (view->type == TOG_VIEW_DIFF) {
4321 struct tog_diff_view_state *s = &view->state.diff;
4323 first = &s->first_displayed_line;
4327 } else if (view->type == TOG_VIEW_HELP) {
4328 struct tog_help_view_state *s = &view->state.help;
4330 first = &s->first_displayed_line;
4334 } else if (view->type == TOG_VIEW_BLAME) {
4335 struct tog_blame_view_state *s = &view->state.blame;
4337 first = &s->first_displayed_line;
4338 selected = &s->selected_line;
4344 /* Center gline in the middle of the page like vi(1). */
4345 if (*lineno < view->gline - (view->nlines - 3) / 2)
4347 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4356 *selected = view->gline <= (view->nlines - 3) / 2 ?
4357 view->gline : (view->nlines - 3) / 2 + 1;
4363 static const struct got_error *
4364 draw_file(struct tog_view *view, const char *header)
4366 struct tog_diff_view_state *s = &view->state.diff;
4367 regmatch_t *regmatch = &view->regmatch;
4368 const struct got_error *err;
4371 size_t linesize = 0;
4375 int max_lines = view->nlines;
4376 int nlines = s->nlines;
4379 s->lineno = s->first_displayed_line - 1;
4380 line_offset = s->lines[s->first_displayed_line - 1].offset;
4381 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4382 return got_error_from_errno("fseek");
4384 werase(view->window);
4386 if (view->gline > s->nlines - 1)
4387 view->gline = s->nlines - 1;
4390 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4391 1 : view->gline - (view->nlines - 3) / 2 :
4392 s->lineno + s->selected_line;
4394 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4395 return got_error_from_errno("asprintf");
4396 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4402 if (view_needs_focus_indication(view))
4403 wstandout(view->window);
4404 waddwstr(view->window, wline);
4407 while (width++ < view->ncols)
4408 waddch(view->window, ' ');
4409 if (view_needs_focus_indication(view))
4410 wstandend(view->window);
4420 while (max_lines > 0 && nprinted < max_lines) {
4421 enum got_diff_line_type linetype;
4424 linelen = getline(&line, &linesize, s->f);
4425 if (linelen == -1) {
4431 return got_ferror(s->f, GOT_ERR_IO);
4434 if (++s->lineno < s->first_displayed_line)
4436 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4438 if (s->lineno == view->hiline)
4441 /* Set view->maxx based on full line length. */
4442 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4448 view->maxx = MAX(view->maxx, width);
4452 linetype = s->lines[s->lineno].type;
4453 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4454 linetype < GOT_DIFF_LINE_CONTEXT)
4455 attr |= COLOR_PAIR(linetype);
4457 wattron(view->window, attr);
4458 if (s->first_displayed_line + nprinted == s->matched_line &&
4459 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4460 err = add_matched_line(&width, line, view->ncols, 0,
4461 view->window, view->x, regmatch);
4468 err = format_line(&wline, &width, &skip, line,
4469 view->x, view->ncols, 0, view->x ? 1 : 0);
4474 waddwstr(view->window, &wline[skip]);
4478 if (s->lineno == view->hiline) {
4479 /* highlight full gline length */
4480 while (width++ < view->ncols)
4481 waddch(view->window, ' ');
4483 if (width <= view->ncols - 1)
4484 waddch(view->window, '\n');
4487 wattroff(view->window, attr);
4488 if (++nprinted == 1)
4489 s->first_displayed_line = s->lineno;
4493 s->last_displayed_line = s->first_displayed_line +
4496 s->last_displayed_line = s->first_displayed_line;
4501 while (nprinted < view->nlines) {
4502 waddch(view->window, '\n');
4506 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4512 wstandout(view->window);
4513 waddwstr(view->window, wline);
4516 wstandend(view->window);
4523 get_datestr(time_t *time, char *datebuf)
4525 struct tm mytm, *tm;
4528 tm = gmtime_r(time, &mytm);
4531 s = asctime_r(tm, datebuf);
4534 p = strchr(s, '\n');
4540 static const struct got_error *
4541 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4542 off_t off, uint8_t type)
4544 struct got_diff_line *p;
4546 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4548 return got_error_from_errno("reallocarray");
4550 (*lines)[*nlines].offset = off;
4551 (*lines)[*nlines].type = type;
4557 static const struct got_error *
4558 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4559 struct got_diff_line *s_lines, size_t s_nlines)
4561 struct got_diff_line *p;
4565 if (fseeko(src, 0L, SEEK_SET) == -1)
4566 return got_error_from_errno("fseeko");
4569 r = fread(buf, 1, sizeof(buf), src);
4572 return got_error_from_errno("fread");
4576 if (fwrite(buf, 1, r, dst) != r)
4577 return got_ferror(dst, GOT_ERR_IO);
4581 * The diff driver initialises the first line at offset zero when the
4582 * array isn't prepopulated, skip it; we already have it in *d_lines.
4584 for (i = 1; i < s_nlines; ++i)
4585 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4589 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4591 /* d_lines is freed in close_diff_view() */
4592 return got_error_from_errno("reallocarray");
4597 memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4598 *d_nlines += s_nlines;
4603 static const struct got_error *
4604 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4605 struct got_object_id *commit_id, struct got_reflist_head *refs,
4606 struct got_repository *repo, int ignore_ws, int force_text_diff,
4607 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4609 const struct got_error *err = NULL;
4610 char datebuf[26], *datestr;
4611 struct got_commit_object *commit;
4612 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4613 time_t committer_time;
4614 const char *author, *committer;
4615 char *refs_str = NULL;
4616 struct got_pathlist_entry *pe;
4621 err = build_refs_str(&refs_str, refs, commit_id, repo);
4626 err = got_object_open_as_commit(&commit, repo, commit_id);
4630 err = got_object_id_str(&id_str, commit_id);
4632 err = got_error_from_errno("got_object_id_str");
4636 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4640 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4641 refs_str ? refs_str : "", refs_str ? ")" : "");
4643 err = got_error_from_errno("fprintf");
4647 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4651 n = fprintf(outfile, "from: %s\n",
4652 got_object_commit_get_author(commit));
4654 err = got_error_from_errno("fprintf");
4658 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4662 author = got_object_commit_get_author(commit);
4663 committer = got_object_commit_get_committer(commit);
4664 if (strcmp(author, committer) != 0) {
4665 n = fprintf(outfile, "via: %s\n", committer);
4667 err = got_error_from_errno("fprintf");
4671 err = add_line_metadata(lines, nlines, outoff,
4672 GOT_DIFF_LINE_AUTHOR);
4676 committer_time = got_object_commit_get_committer_time(commit);
4677 datestr = get_datestr(&committer_time, datebuf);
4679 n = fprintf(outfile, "date: %s UTC\n", datestr);
4681 err = got_error_from_errno("fprintf");
4685 err = add_line_metadata(lines, nlines, outoff,
4686 GOT_DIFF_LINE_DATE);
4690 if (got_object_commit_get_nparents(commit) > 1) {
4691 const struct got_object_id_queue *parent_ids;
4692 struct got_object_qid *qid;
4694 parent_ids = got_object_commit_get_parent_ids(commit);
4695 STAILQ_FOREACH(qid, parent_ids, entry) {
4696 err = got_object_id_str(&id_str, &qid->id);
4699 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4701 err = got_error_from_errno("fprintf");
4705 err = add_line_metadata(lines, nlines, outoff,
4706 GOT_DIFF_LINE_META);
4714 err = got_object_commit_get_logmsg(&logmsg, commit);
4718 while ((line = strsep(&s, "\n")) != NULL) {
4719 n = fprintf(outfile, "%s\n", line);
4721 err = got_error_from_errno("fprintf");
4725 err = add_line_metadata(lines, nlines, outoff,
4726 GOT_DIFF_LINE_LOGMSG);
4731 TAILQ_FOREACH(pe, dsa->paths, entry) {
4732 struct got_diff_changed_path *cp = pe->data;
4733 int pad = dsa->max_path_len - pe->path_len + 1;
4735 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4736 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4737 dsa->rm_cols + 1, cp->rm);
4739 err = got_error_from_errno("fprintf");
4743 err = add_line_metadata(lines, nlines, outoff,
4744 GOT_DIFF_LINE_CHANGES);
4749 fputc('\n', outfile);
4751 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4755 n = fprintf(outfile,
4756 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4757 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4758 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4760 err = got_error_from_errno("fprintf");
4764 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4768 fputc('\n', outfile);
4770 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4775 got_object_commit_close(commit);
4784 static const struct got_error *
4785 create_diff(struct tog_diff_view_state *s)
4787 const struct got_error *err = NULL;
4788 FILE *f = NULL, *tmp_diff_file = NULL;
4790 struct got_diff_line *lines = NULL;
4791 struct got_pathlist_head changed_paths;
4793 TAILQ_INIT(&changed_paths);
4796 s->lines = malloc(sizeof(*s->lines));
4797 if (s->lines == NULL)
4798 return got_error_from_errno("malloc");
4803 err = got_error_from_errno("got_opentemp");
4806 tmp_diff_file = got_opentemp();
4807 if (tmp_diff_file == NULL) {
4808 err = got_error_from_errno("got_opentemp");
4811 if (s->f && fclose(s->f) == EOF) {
4812 err = got_error_from_errno("fclose");
4818 err = got_object_get_type(&obj_type, s->repo, s->id1);
4820 err = got_object_get_type(&obj_type, s->repo, s->id2);
4825 case GOT_OBJ_TYPE_BLOB:
4826 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4827 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4828 s->label1, s->label2, tog_diff_algo, s->diff_context,
4829 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4832 case GOT_OBJ_TYPE_TREE:
4833 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4834 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4835 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4836 s->force_text_diff, NULL, s->repo, s->f);
4838 case GOT_OBJ_TYPE_COMMIT: {
4839 const struct got_object_id_queue *parent_ids;
4840 struct got_object_qid *pid;
4841 struct got_commit_object *commit2;
4842 struct got_reflist_head *refs;
4844 struct got_diffstat_cb_arg dsa = {
4847 s->ignore_whitespace,
4852 lines = malloc(sizeof(*lines));
4853 if (lines == NULL) {
4854 err = got_error_from_errno("malloc");
4858 /* build diff first in tmp file then append to commit info */
4859 err = got_diff_objects_as_commits(&lines, &nlines,
4860 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4861 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4862 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4866 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4869 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4870 /* Show commit info if we're diffing to a parent/root commit. */
4871 if (s->id1 == NULL) {
4872 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4873 refs, s->repo, s->ignore_whitespace,
4874 s->force_text_diff, &dsa, s->f);
4878 parent_ids = got_object_commit_get_parent_ids(commit2);
4879 STAILQ_FOREACH(pid, parent_ids, entry) {
4880 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4881 err = write_commit_info(&s->lines,
4882 &s->nlines, s->id2, refs, s->repo,
4883 s->ignore_whitespace,
4884 s->force_text_diff, &dsa, s->f);
4891 got_object_commit_close(commit2);
4893 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4898 err = got_error(GOT_ERR_OBJ_TYPE);
4903 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4904 if (s->f && fflush(s->f) != 0 && err == NULL)
4905 err = got_error_from_errno("fflush");
4906 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4907 err = got_error_from_errno("fclose");
4912 diff_view_indicate_progress(struct tog_view *view)
4914 mvwaddstr(view->window, 0, 0, "diffing...");
4919 static const struct got_error *
4920 search_start_diff_view(struct tog_view *view)
4922 struct tog_diff_view_state *s = &view->state.diff;
4924 s->matched_line = 0;
4929 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4930 size_t *nlines, int **first, int **last, int **match, int **selected)
4932 struct tog_diff_view_state *s = &view->state.diff;
4935 *nlines = s->nlines;
4936 *line_offsets = NULL;
4937 *match = &s->matched_line;
4938 *first = &s->first_displayed_line;
4939 *last = &s->last_displayed_line;
4940 *selected = &s->selected_line;
4943 static const struct got_error *
4944 search_next_view_match(struct tog_view *view)
4946 const struct got_error *err = NULL;
4950 size_t linesize = 0;
4952 off_t *line_offsets;
4954 int *first, *last, *match, *selected;
4956 if (!view->search_setup)
4957 return got_error_msg(GOT_ERR_NOT_IMPL,
4958 "view search not supported");
4959 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4962 if (!view->searching) {
4963 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4968 if (view->searching == TOG_SEARCH_FORWARD)
4969 lineno = *match + 1;
4971 lineno = *match - 1;
4973 lineno = *first - 1 + *selected;
4978 if (lineno <= 0 || lineno > nlines) {
4980 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4984 if (view->searching == TOG_SEARCH_FORWARD)
4990 offset = view->type == TOG_VIEW_DIFF ?
4991 view->state.diff.lines[lineno - 1].offset :
4992 line_offsets[lineno - 1];
4993 if (fseeko(f, offset, SEEK_SET) != 0) {
4995 return got_error_from_errno("fseeko");
4997 linelen = getline(&line, &linesize, f);
4998 if (linelen != -1) {
5000 err = expand_tab(&exstr, line);
5003 if (match_line(exstr, &view->regex, 1,
5005 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5012 if (view->searching == TOG_SEARCH_FORWARD)
5027 static const struct got_error *
5028 close_diff_view(struct tog_view *view)
5030 const struct got_error *err = NULL;
5031 struct tog_diff_view_state *s = &view->state.diff;
5037 if (s->f && fclose(s->f) == EOF)
5038 err = got_error_from_errno("fclose");
5040 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5041 err = got_error_from_errno("fclose");
5043 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5044 err = got_error_from_errno("fclose");
5046 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5047 err = got_error_from_errno("close");
5049 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5050 err = got_error_from_errno("close");
5058 static const struct got_error *
5059 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5060 struct got_object_id *id2, const char *label1, const char *label2,
5061 int diff_context, int ignore_whitespace, int force_text_diff,
5062 struct tog_view *parent_view, struct got_repository *repo)
5064 const struct got_error *err;
5065 struct tog_diff_view_state *s = &view->state.diff;
5067 memset(s, 0, sizeof(*s));
5071 if (id1 != NULL && id2 != NULL) {
5073 err = got_object_get_type(&type1, repo, id1);
5076 err = got_object_get_type(&type2, repo, id2);
5081 return got_error(GOT_ERR_OBJ_TYPE);
5083 s->first_displayed_line = 1;
5084 s->last_displayed_line = view->nlines;
5085 s->selected_line = 1;
5093 s->id1 = got_object_id_dup(id1);
5095 return got_error_from_errno("got_object_id_dup");
5099 s->id2 = got_object_id_dup(id2);
5100 if (s->id2 == NULL) {
5101 err = got_error_from_errno("got_object_id_dup");
5105 s->f1 = got_opentemp();
5106 if (s->f1 == NULL) {
5107 err = got_error_from_errno("got_opentemp");
5111 s->f2 = got_opentemp();
5112 if (s->f2 == NULL) {
5113 err = got_error_from_errno("got_opentemp");
5117 s->fd1 = got_opentempfd();
5119 err = got_error_from_errno("got_opentempfd");
5123 s->fd2 = got_opentempfd();
5125 err = got_error_from_errno("got_opentempfd");
5129 s->diff_context = diff_context;
5130 s->ignore_whitespace = ignore_whitespace;
5131 s->force_text_diff = force_text_diff;
5132 s->parent_view = parent_view;
5135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5138 rc = init_pair(GOT_DIFF_LINE_MINUS,
5139 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5141 rc = init_pair(GOT_DIFF_LINE_PLUS,
5142 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5144 rc = init_pair(GOT_DIFF_LINE_HUNK,
5145 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5147 rc = init_pair(GOT_DIFF_LINE_META,
5148 get_color_value("TOG_COLOR_DIFF_META"), -1);
5150 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5151 get_color_value("TOG_COLOR_DIFF_META"), -1);
5153 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5154 get_color_value("TOG_COLOR_DIFF_META"), -1);
5156 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5157 get_color_value("TOG_COLOR_DIFF_META"), -1);
5159 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5160 get_color_value("TOG_COLOR_AUTHOR"), -1);
5162 rc = init_pair(GOT_DIFF_LINE_DATE,
5163 get_color_value("TOG_COLOR_DATE"), -1);
5165 err = got_error(GOT_ERR_RANGE);
5170 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5171 view_is_splitscreen(view))
5172 show_log_view(parent_view); /* draw border */
5173 diff_view_indicate_progress(view);
5175 err = create_diff(s);
5177 view->show = show_diff_view;
5178 view->input = input_diff_view;
5179 view->reset = reset_diff_view;
5180 view->close = close_diff_view;
5181 view->search_start = search_start_diff_view;
5182 view->search_setup = search_setup_diff_view;
5183 view->search_next = search_next_view_match;
5186 close_diff_view(view);
5190 static const struct got_error *
5191 show_diff_view(struct tog_view *view)
5193 const struct got_error *err;
5194 struct tog_diff_view_state *s = &view->state.diff;
5195 char *id_str1 = NULL, *id_str2, *header;
5196 const char *label1, *label2;
5199 err = got_object_id_str(&id_str1, s->id1);
5202 label1 = s->label1 ? s->label1 : id_str1;
5204 label1 = "/dev/null";
5206 err = got_object_id_str(&id_str2, s->id2);
5209 label2 = s->label2 ? s->label2 : id_str2;
5211 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5212 err = got_error_from_errno("asprintf");
5220 err = draw_file(view, header);
5225 static const struct got_error *
5226 set_selected_commit(struct tog_diff_view_state *s,
5227 struct commit_queue_entry *entry)
5229 const struct got_error *err;
5230 const struct got_object_id_queue *parent_ids;
5231 struct got_commit_object *selected_commit;
5232 struct got_object_qid *pid;
5235 s->id2 = got_object_id_dup(entry->id);
5237 return got_error_from_errno("got_object_id_dup");
5239 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5242 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5244 pid = STAILQ_FIRST(parent_ids);
5245 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5246 got_object_commit_close(selected_commit);
5250 static const struct got_error *
5251 reset_diff_view(struct tog_view *view)
5253 struct tog_diff_view_state *s = &view->state.diff;
5256 wclear(view->window);
5257 s->first_displayed_line = 1;
5258 s->last_displayed_line = view->nlines;
5259 s->matched_line = 0;
5260 diff_view_indicate_progress(view);
5261 return create_diff(s);
5265 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5269 i = start = s->first_displayed_line - 1;
5271 while (s->lines[i].type != type) {
5275 return; /* do nothing, requested type not in file */
5278 s->selected_line = 1;
5279 s->first_displayed_line = i;
5283 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5287 i = start = s->first_displayed_line + 1;
5289 while (s->lines[i].type != type) {
5290 if (i == s->nlines - 1)
5293 return; /* do nothing, requested type not in file */
5296 s->selected_line = 1;
5297 s->first_displayed_line = i;
5300 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5302 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5305 static const struct got_error *
5306 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5308 const struct got_error *err = NULL;
5309 struct tog_diff_view_state *s = &view->state.diff;
5310 struct tog_log_view_state *ls;
5311 struct commit_queue_entry *old_selected_entry;
5313 size_t linesize = 0;
5315 int i, nscroll = view->nlines - 1, up = 0;
5317 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5326 horizontal_scroll_input(view, ch);
5331 s->force_text_diff = !s->force_text_diff;
5332 view->action = s->force_text_diff ?
5333 "force ASCII text enabled" :
5334 "force ASCII text disabled";
5336 else if (ch == 'w') {
5337 s->ignore_whitespace = !s->ignore_whitespace;
5338 view->action = s->ignore_whitespace ?
5339 "ignore whitespace enabled" :
5340 "ignore whitespace disabled";
5342 err = reset_diff_view(view);
5346 s->first_displayed_line = 1;
5355 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5361 if (s->first_displayed_line > 1)
5362 s->first_displayed_line--;
5373 if (s->first_displayed_line == 1) {
5378 while (i++ < nscroll && s->first_displayed_line > 1)
5379 s->first_displayed_line--;
5385 s->first_displayed_line++;
5402 while (!s->eof && i++ < nscroll) {
5403 linelen = getline(&line, &linesize, s->f);
5404 s->first_displayed_line++;
5405 if (linelen == -1) {
5409 err = got_ferror(s->f, GOT_ERR_IO);
5416 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5419 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5422 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5425 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5428 if (s->diff_context > 0) {
5430 s->matched_line = 0;
5431 diff_view_indicate_progress(view);
5432 err = create_diff(s);
5433 if (s->first_displayed_line + view->nlines - 1 >
5435 s->first_displayed_line = 1;
5436 s->last_displayed_line = view->nlines;
5442 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5444 s->matched_line = 0;
5445 diff_view_indicate_progress(view);
5446 err = create_diff(s);
5458 if (s->parent_view == NULL) {
5462 s->parent_view->count = view->count;
5464 if (s->parent_view->type == TOG_VIEW_LOG) {
5465 ls = &s->parent_view->state.log;
5466 old_selected_entry = ls->selected_entry;
5468 err = input_log_view(NULL, s->parent_view,
5469 up ? KEY_UP : KEY_DOWN);
5472 view->count = s->parent_view->count;
5474 if (old_selected_entry == ls->selected_entry)
5477 err = set_selected_commit(s, ls->selected_entry);
5480 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5481 struct tog_blame_view_state *bs;
5482 struct got_object_id *id, *prev_id;
5484 bs = &s->parent_view->state.blame;
5485 prev_id = get_annotation_for_line(bs->blame.lines,
5486 bs->blame.nlines, bs->last_diffed_line);
5488 err = input_blame_view(&view, s->parent_view,
5489 up ? KEY_UP : KEY_DOWN);
5492 view->count = s->parent_view->count;
5494 if (prev_id == NULL)
5496 id = get_selected_commit_id(bs->blame.lines,
5497 bs->blame.nlines, bs->first_displayed_line,
5502 if (!got_object_id_cmp(prev_id, id))
5505 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5509 s->first_displayed_line = 1;
5510 s->last_displayed_line = view->nlines;
5511 s->matched_line = 0;
5514 diff_view_indicate_progress(view);
5515 err = create_diff(s);
5525 static const struct got_error *
5526 cmd_diff(int argc, char *argv[])
5528 const struct got_error *error = NULL;
5529 struct got_repository *repo = NULL;
5530 struct got_worktree *worktree = NULL;
5531 struct got_object_id *id1 = NULL, *id2 = NULL;
5532 char *repo_path = NULL, *cwd = NULL;
5533 char *id_str1 = NULL, *id_str2 = NULL;
5534 char *label1 = NULL, *label2 = NULL;
5535 int diff_context = 3, ignore_whitespace = 0;
5536 int ch, force_text_diff = 0;
5538 struct tog_view *view;
5539 int *pack_fds = NULL;
5541 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5544 force_text_diff = 1;
5547 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5550 errx(1, "number of context lines is %s: %s",
5554 repo_path = realpath(optarg, NULL);
5555 if (repo_path == NULL)
5556 return got_error_from_errno2("realpath",
5558 got_path_strip_trailing_slashes(repo_path);
5561 ignore_whitespace = 1;
5573 usage_diff(); /* TODO show local worktree changes */
5574 } else if (argc == 2) {
5580 error = got_repo_pack_fds_open(&pack_fds);
5584 if (repo_path == NULL) {
5585 cwd = getcwd(NULL, 0);
5587 return got_error_from_errno("getcwd");
5588 error = got_worktree_open(&worktree, cwd);
5589 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5593 strdup(got_worktree_get_repo_path(worktree));
5595 repo_path = strdup(cwd);
5596 if (repo_path == NULL) {
5597 error = got_error_from_errno("strdup");
5602 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5608 error = apply_unveil(got_repo_get_path(repo), NULL);
5612 error = tog_load_refs(repo, 0);
5616 error = got_repo_match_object_id(&id1, &label1, id_str1,
5617 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5621 error = got_repo_match_object_id(&id2, &label2, id_str2,
5622 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5626 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5628 error = got_error_from_errno("view_open");
5631 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5632 ignore_whitespace, force_text_diff, NULL, repo);
5635 error = view_loop(view);
5642 const struct got_error *close_err = got_repo_close(repo);
5647 got_worktree_close(worktree);
5649 const struct got_error *pack_err =
5650 got_repo_pack_fds_close(pack_fds);
5663 "usage: %s blame [-c commit] [-r repository-path] path\n",
5668 struct tog_blame_line {
5670 struct got_object_id *id;
5673 static const struct got_error *
5674 draw_blame(struct tog_view *view)
5676 struct tog_blame_view_state *s = &view->state.blame;
5677 struct tog_blame *blame = &s->blame;
5678 regmatch_t *regmatch = &view->regmatch;
5679 const struct got_error *err;
5680 int lineno = 0, nprinted = 0;
5682 size_t linesize = 0;
5686 struct tog_blame_line *blame_line;
5687 struct got_object_id *prev_id = NULL;
5689 struct tog_color *tc;
5691 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5696 werase(view->window);
5698 if (asprintf(&line, "commit %s", id_str) == -1) {
5699 err = got_error_from_errno("asprintf");
5704 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5709 if (view_needs_focus_indication(view))
5710 wstandout(view->window);
5711 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5713 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5714 waddwstr(view->window, wline);
5715 while (width++ < view->ncols)
5716 waddch(view->window, ' ');
5718 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5719 if (view_needs_focus_indication(view))
5720 wstandend(view->window);
5724 if (view->gline > blame->nlines)
5725 view->gline = blame->nlines;
5727 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5728 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5729 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5731 return got_error_from_errno("asprintf");
5734 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5739 waddwstr(view->window, wline);
5742 if (width < view->ncols - 1)
5743 waddch(view->window, '\n');
5747 while (nprinted < view->nlines - 2) {
5748 linelen = getline(&line, &linesize, blame->f);
5749 if (linelen == -1) {
5750 if (feof(blame->f)) {
5755 return got_ferror(blame->f, GOT_ERR_IO);
5757 if (++lineno < s->first_displayed_line)
5759 if (view->gline && !gotoline(view, &lineno, &nprinted))
5762 /* Set view->maxx based on full line length. */
5763 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5770 view->maxx = MAX(view->maxx, width);
5772 if (nprinted == s->selected_line - 1)
5773 wstandout(view->window);
5775 if (blame->nlines > 0) {
5776 blame_line = &blame->lines[lineno - 1];
5777 if (blame_line->annotated && prev_id &&
5778 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5779 !(nprinted == s->selected_line - 1)) {
5780 waddstr(view->window, " ");
5781 } else if (blame_line->annotated) {
5783 err = got_object_id_str(&id_str,
5789 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5791 wattr_on(view->window,
5792 COLOR_PAIR(tc->colorpair), NULL);
5793 wprintw(view->window, "%.8s", id_str);
5795 wattr_off(view->window,
5796 COLOR_PAIR(tc->colorpair), NULL);
5798 prev_id = blame_line->id;
5800 waddstr(view->window, "........");
5804 waddstr(view->window, "........");
5808 if (nprinted == s->selected_line - 1)
5809 wstandend(view->window);
5810 waddstr(view->window, " ");
5812 if (view->ncols <= 9) {
5814 } else if (s->first_displayed_line + nprinted ==
5816 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5817 err = add_matched_line(&width, line, view->ncols - 9, 9,
5818 view->window, view->x, regmatch);
5826 err = format_line(&wline, &width, &skip, line,
5827 view->x, view->ncols - 9, 9, 1);
5832 waddwstr(view->window, &wline[skip]);
5838 if (width <= view->ncols - 1)
5839 waddch(view->window, '\n');
5840 if (++nprinted == 1)
5841 s->first_displayed_line = lineno;
5844 s->last_displayed_line = lineno;
5851 static const struct got_error *
5852 blame_cb(void *arg, int nlines, int lineno,
5853 struct got_commit_object *commit, struct got_object_id *id)
5855 const struct got_error *err = NULL;
5856 struct tog_blame_cb_args *a = arg;
5857 struct tog_blame_line *line;
5860 if (nlines != a->nlines ||
5861 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5862 return got_error(GOT_ERR_RANGE);
5864 errcode = pthread_mutex_lock(&tog_mutex);
5866 return got_error_set_errno(errcode, "pthread_mutex_lock");
5868 if (*a->quit) { /* user has quit the blame view */
5869 err = got_error(GOT_ERR_ITER_COMPLETED);
5874 goto done; /* no change in this commit */
5876 line = &a->lines[lineno - 1];
5877 if (line->annotated)
5880 line->id = got_object_id_dup(id);
5881 if (line->id == NULL) {
5882 err = got_error_from_errno("got_object_id_dup");
5885 line->annotated = 1;
5887 errcode = pthread_mutex_unlock(&tog_mutex);
5889 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5894 blame_thread(void *arg)
5896 const struct got_error *err, *close_err;
5897 struct tog_blame_thread_args *ta = arg;
5898 struct tog_blame_cb_args *a = ta->cb_args;
5899 int errcode, fd1 = -1, fd2 = -1;
5900 FILE *f1 = NULL, *f2 = NULL;
5902 fd1 = got_opentempfd();
5904 return (void *)got_error_from_errno("got_opentempfd");
5906 fd2 = got_opentempfd();
5908 err = got_error_from_errno("got_opentempfd");
5912 f1 = got_opentemp();
5914 err = (void *)got_error_from_errno("got_opentemp");
5917 f2 = got_opentemp();
5919 err = (void *)got_error_from_errno("got_opentemp");
5923 err = block_signals_used_by_main_thread();
5927 err = got_blame(ta->path, a->commit_id, ta->repo,
5928 tog_diff_algo, blame_cb, ta->cb_args,
5929 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5930 if (err && err->code == GOT_ERR_CANCELLED)
5933 errcode = pthread_mutex_lock(&tog_mutex);
5935 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5939 close_err = got_repo_close(ta->repo);
5945 errcode = pthread_mutex_unlock(&tog_mutex);
5946 if (errcode && err == NULL)
5947 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5950 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5951 err = got_error_from_errno("close");
5952 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5953 err = got_error_from_errno("close");
5954 if (f1 && fclose(f1) == EOF && err == NULL)
5955 err = got_error_from_errno("fclose");
5956 if (f2 && fclose(f2) == EOF && err == NULL)
5957 err = got_error_from_errno("fclose");
5962 static struct got_object_id *
5963 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5964 int first_displayed_line, int selected_line)
5966 struct tog_blame_line *line;
5971 line = &lines[first_displayed_line - 1 + selected_line - 1];
5972 if (!line->annotated)
5978 static struct got_object_id *
5979 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5982 struct tog_blame_line *line;
5984 if (nlines <= 0 || lineno >= nlines)
5987 line = &lines[lineno - 1];
5988 if (!line->annotated)
5994 static const struct got_error *
5995 stop_blame(struct tog_blame *blame)
5997 const struct got_error *err = NULL;
6000 if (blame->thread) {
6002 errcode = pthread_mutex_unlock(&tog_mutex);
6004 return got_error_set_errno(errcode,
6005 "pthread_mutex_unlock");
6006 errcode = pthread_join(blame->thread, (void **)&err);
6008 return got_error_set_errno(errcode, "pthread_join");
6009 errcode = pthread_mutex_lock(&tog_mutex);
6011 return got_error_set_errno(errcode,
6012 "pthread_mutex_lock");
6013 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6015 blame->thread = NULL;
6017 if (blame->thread_args.repo) {
6018 const struct got_error *close_err;
6019 close_err = got_repo_close(blame->thread_args.repo);
6022 blame->thread_args.repo = NULL;
6025 if (fclose(blame->f) == EOF && err == NULL)
6026 err = got_error_from_errno("fclose");
6030 for (i = 0; i < blame->nlines; i++)
6031 free(blame->lines[i].id);
6033 blame->lines = NULL;
6035 free(blame->cb_args.commit_id);
6036 blame->cb_args.commit_id = NULL;
6037 if (blame->pack_fds) {
6038 const struct got_error *pack_err =
6039 got_repo_pack_fds_close(blame->pack_fds);
6042 blame->pack_fds = NULL;
6047 static const struct got_error *
6048 cancel_blame_view(void *arg)
6050 const struct got_error *err = NULL;
6054 errcode = pthread_mutex_lock(&tog_mutex);
6056 return got_error_set_errno(errcode,
6057 "pthread_mutex_unlock");
6060 err = got_error(GOT_ERR_CANCELLED);
6062 errcode = pthread_mutex_unlock(&tog_mutex);
6064 return got_error_set_errno(errcode,
6065 "pthread_mutex_lock");
6070 static const struct got_error *
6071 run_blame(struct tog_view *view)
6073 struct tog_blame_view_state *s = &view->state.blame;
6074 struct tog_blame *blame = &s->blame;
6075 const struct got_error *err = NULL;
6076 struct got_commit_object *commit = NULL;
6077 struct got_blob_object *blob = NULL;
6078 struct got_repository *thread_repo = NULL;
6079 struct got_object_id *obj_id = NULL;
6080 int obj_type, fd = -1;
6081 int *pack_fds = NULL;
6083 err = got_object_open_as_commit(&commit, s->repo,
6084 &s->blamed_commit->id);
6088 fd = got_opentempfd();
6090 err = got_error_from_errno("got_opentempfd");
6094 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6098 err = got_object_get_type(&obj_type, s->repo, obj_id);
6102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6103 err = got_error(GOT_ERR_OBJ_TYPE);
6107 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6110 blame->f = got_opentemp();
6111 if (blame->f == NULL) {
6112 err = got_error_from_errno("got_opentemp");
6115 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6116 &blame->line_offsets, blame->f, blob);
6119 if (blame->nlines == 0) {
6120 s->blame_complete = 1;
6124 /* Don't include \n at EOF in the blame line count. */
6125 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6128 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6129 if (blame->lines == NULL) {
6130 err = got_error_from_errno("calloc");
6134 err = got_repo_pack_fds_open(&pack_fds);
6137 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6142 blame->pack_fds = pack_fds;
6143 blame->cb_args.view = view;
6144 blame->cb_args.lines = blame->lines;
6145 blame->cb_args.nlines = blame->nlines;
6146 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6147 if (blame->cb_args.commit_id == NULL) {
6148 err = got_error_from_errno("got_object_id_dup");
6151 blame->cb_args.quit = &s->done;
6153 blame->thread_args.path = s->path;
6154 blame->thread_args.repo = thread_repo;
6155 blame->thread_args.cb_args = &blame->cb_args;
6156 blame->thread_args.complete = &s->blame_complete;
6157 blame->thread_args.cancel_cb = cancel_blame_view;
6158 blame->thread_args.cancel_arg = &s->done;
6159 s->blame_complete = 0;
6161 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6162 s->first_displayed_line = 1;
6163 s->last_displayed_line = view->nlines;
6164 s->selected_line = 1;
6166 s->matched_line = 0;
6170 got_object_commit_close(commit);
6171 if (fd != -1 && close(fd) == -1 && err == NULL)
6172 err = got_error_from_errno("close");
6174 got_object_blob_close(blob);
6181 static const struct got_error *
6182 open_blame_view(struct tog_view *view, char *path,
6183 struct got_object_id *commit_id, struct got_repository *repo)
6185 const struct got_error *err = NULL;
6186 struct tog_blame_view_state *s = &view->state.blame;
6188 STAILQ_INIT(&s->blamed_commits);
6190 s->path = strdup(path);
6191 if (s->path == NULL)
6192 return got_error_from_errno("strdup");
6194 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6200 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6201 s->first_displayed_line = 1;
6202 s->last_displayed_line = view->nlines;
6203 s->selected_line = 1;
6204 s->blame_complete = 0;
6206 s->commit_id = commit_id;
6207 memset(&s->blame, 0, sizeof(s->blame));
6209 STAILQ_INIT(&s->colors);
6210 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6211 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6212 get_color_value("TOG_COLOR_COMMIT"));
6217 view->show = show_blame_view;
6218 view->input = input_blame_view;
6219 view->reset = reset_blame_view;
6220 view->close = close_blame_view;
6221 view->search_start = search_start_blame_view;
6222 view->search_setup = search_setup_blame_view;
6223 view->search_next = search_next_view_match;
6225 return run_blame(view);
6228 static const struct got_error *
6229 close_blame_view(struct tog_view *view)
6231 const struct got_error *err = NULL;
6232 struct tog_blame_view_state *s = &view->state.blame;
6234 if (s->blame.thread)
6235 err = stop_blame(&s->blame);
6237 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6238 struct got_object_qid *blamed_commit;
6239 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6240 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6241 got_object_qid_free(blamed_commit);
6245 free_colors(&s->colors);
6249 static const struct got_error *
6250 search_start_blame_view(struct tog_view *view)
6252 struct tog_blame_view_state *s = &view->state.blame;
6254 s->matched_line = 0;
6259 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6260 size_t *nlines, int **first, int **last, int **match, int **selected)
6262 struct tog_blame_view_state *s = &view->state.blame;
6265 *nlines = s->blame.nlines;
6266 *line_offsets = s->blame.line_offsets;
6267 *match = &s->matched_line;
6268 *first = &s->first_displayed_line;
6269 *last = &s->last_displayed_line;
6270 *selected = &s->selected_line;
6273 static const struct got_error *
6274 show_blame_view(struct tog_view *view)
6276 const struct got_error *err = NULL;
6277 struct tog_blame_view_state *s = &view->state.blame;
6280 if (s->blame.thread == NULL && !s->blame_complete) {
6281 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6282 &s->blame.thread_args);
6284 return got_error_set_errno(errcode, "pthread_create");
6286 halfdelay(1); /* fast refresh while annotating */
6289 if (s->blame_complete)
6290 halfdelay(10); /* disable fast refresh */
6292 err = draw_blame(view);
6298 static const struct got_error *
6299 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6300 struct got_repository *repo, struct got_object_id *id)
6302 struct tog_view *log_view;
6303 const struct got_error *err = NULL;
6307 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6308 if (log_view == NULL)
6309 return got_error_from_errno("view_open");
6311 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6313 view_close(log_view);
6315 *new_view = log_view;
6320 static const struct got_error *
6321 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6323 const struct got_error *err = NULL, *thread_err = NULL;
6324 struct tog_view *diff_view;
6325 struct tog_blame_view_state *s = &view->state.blame;
6326 int eos, nscroll, begin_y = 0, begin_x = 0;
6328 eos = nscroll = view->nlines - 2;
6329 if (view_is_hsplit_top(view))
6339 horizontal_scroll_input(view, ch);
6346 s->selected_line = 1;
6347 s->first_displayed_line = 1;
6352 if (s->blame.nlines < eos) {
6353 s->selected_line = s->blame.nlines;
6354 s->first_displayed_line = 1;
6356 s->selected_line = eos;
6357 s->first_displayed_line = s->blame.nlines - (eos - 1);
6364 if (s->selected_line > 1)
6366 else if (s->selected_line == 1 &&
6367 s->first_displayed_line > 1)
6368 s->first_displayed_line--;
6379 if (s->first_displayed_line == 1) {
6380 if (view->count > 1)
6382 s->selected_line = MAX(1, s->selected_line - nscroll);
6386 if (s->first_displayed_line > nscroll)
6387 s->first_displayed_line -= nscroll;
6389 s->first_displayed_line = 1;
6394 if (s->selected_line < eos && s->first_displayed_line +
6395 s->selected_line <= s->blame.nlines)
6397 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6398 s->first_displayed_line++;
6404 struct got_object_id *id = NULL;
6407 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6408 s->first_displayed_line, s->selected_line);
6412 struct got_commit_object *commit, *pcommit;
6413 struct got_object_qid *pid;
6414 struct got_object_id *blob_id = NULL;
6416 err = got_object_open_as_commit(&commit,
6421 got_object_commit_get_parent_ids(commit));
6423 got_object_commit_close(commit);
6426 /* Check if path history ends here. */
6427 err = got_object_open_as_commit(&pcommit,
6431 err = got_object_id_by_path(&blob_id, s->repo,
6433 got_object_commit_close(pcommit);
6435 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6437 got_object_commit_close(commit);
6440 err = got_object_get_type(&obj_type, s->repo,
6443 /* Can't blame non-blob type objects. */
6444 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6445 got_object_commit_close(commit);
6448 err = got_object_qid_alloc(&s->blamed_commit,
6450 got_object_commit_close(commit);
6452 if (got_object_id_cmp(id,
6453 &s->blamed_commit->id) == 0)
6455 err = got_object_qid_alloc(&s->blamed_commit,
6461 thread_err = stop_blame(&s->blame);
6465 STAILQ_INSERT_HEAD(&s->blamed_commits,
6466 s->blamed_commit, entry);
6467 err = run_blame(view);
6473 struct got_object_qid *first;
6476 first = STAILQ_FIRST(&s->blamed_commits);
6477 if (!got_object_id_cmp(&first->id, s->commit_id))
6480 thread_err = stop_blame(&s->blame);
6484 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6485 got_object_qid_free(s->blamed_commit);
6487 STAILQ_FIRST(&s->blamed_commits);
6488 err = run_blame(view);
6495 s->id_to_log = get_selected_commit_id(s->blame.lines,
6496 s->blame.nlines, s->first_displayed_line, s->selected_line);
6498 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6502 struct got_object_id *id = NULL;
6503 struct got_object_qid *pid;
6504 struct got_commit_object *commit = NULL;
6507 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6508 s->first_displayed_line, s->selected_line);
6511 err = got_object_open_as_commit(&commit, s->repo, id);
6514 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6516 /* traversed from diff view, release diff resources */
6517 err = close_diff_view(*new_view);
6520 diff_view = *new_view;
6522 if (view_is_parent_view(view))
6523 view_get_split(view, &begin_y, &begin_x);
6525 diff_view = view_open(0, 0, begin_y, begin_x,
6527 if (diff_view == NULL) {
6528 got_object_commit_close(commit);
6529 err = got_error_from_errno("view_open");
6533 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6534 id, NULL, NULL, 3, 0, 0, view, s->repo);
6535 got_object_commit_close(commit);
6537 view_close(diff_view);
6540 s->last_diffed_line = s->first_displayed_line - 1 +
6543 break; /* still open from active diff view */
6544 if (view_is_parent_view(view) &&
6545 view->mode == TOG_VIEW_SPLIT_HRZN) {
6546 err = view_init_hsplit(view, begin_y);
6552 diff_view->focussed = 1;
6553 diff_view->mode = view->mode;
6554 diff_view->nlines = view->lines - begin_y;
6555 if (view_is_parent_view(view)) {
6556 view_transfer_size(diff_view, view);
6557 err = view_close_child(view);
6560 err = view_set_child(view, diff_view);
6563 view->focus_child = 1;
6565 *new_view = diff_view;
6578 if (s->last_displayed_line >= s->blame.nlines &&
6579 s->selected_line >= MIN(s->blame.nlines,
6580 view->nlines - 2)) {
6584 if (s->last_displayed_line >= s->blame.nlines &&
6585 s->selected_line < view->nlines - 2) {
6587 MIN(nscroll, s->last_displayed_line -
6588 s->first_displayed_line - s->selected_line + 1);
6590 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6591 s->first_displayed_line += nscroll;
6593 s->first_displayed_line =
6594 s->blame.nlines - (view->nlines - 3);
6597 if (s->selected_line > view->nlines - 2) {
6598 s->selected_line = MIN(s->blame.nlines,
6606 return thread_err ? thread_err : err;
6609 static const struct got_error *
6610 reset_blame_view(struct tog_view *view)
6612 const struct got_error *err;
6613 struct tog_blame_view_state *s = &view->state.blame;
6617 err = stop_blame(&s->blame);
6621 return run_blame(view);
6624 static const struct got_error *
6625 cmd_blame(int argc, char *argv[])
6627 const struct got_error *error;
6628 struct got_repository *repo = NULL;
6629 struct got_worktree *worktree = NULL;
6630 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6631 char *link_target = NULL;
6632 struct got_object_id *commit_id = NULL;
6633 struct got_commit_object *commit = NULL;
6634 char *commit_id_str = NULL;
6636 struct tog_view *view;
6637 int *pack_fds = NULL;
6639 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6642 commit_id_str = optarg;
6645 repo_path = realpath(optarg, NULL);
6646 if (repo_path == NULL)
6647 return got_error_from_errno2("realpath",
6662 error = got_repo_pack_fds_open(&pack_fds);
6666 if (repo_path == NULL) {
6667 cwd = getcwd(NULL, 0);
6669 return got_error_from_errno("getcwd");
6670 error = got_worktree_open(&worktree, cwd);
6671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6675 strdup(got_worktree_get_repo_path(worktree));
6677 repo_path = strdup(cwd);
6678 if (repo_path == NULL) {
6679 error = got_error_from_errno("strdup");
6684 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6688 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6695 error = apply_unveil(got_repo_get_path(repo), NULL);
6699 error = tog_load_refs(repo, 0);
6703 if (commit_id_str == NULL) {
6704 struct got_reference *head_ref;
6705 error = got_ref_open(&head_ref, repo, worktree ?
6706 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6709 error = got_ref_resolve(&commit_id, repo, head_ref);
6710 got_ref_close(head_ref);
6712 error = got_repo_match_object_id(&commit_id, NULL,
6713 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6718 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6720 error = got_error_from_errno("view_open");
6724 error = got_object_open_as_commit(&commit, repo, commit_id);
6728 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6733 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6738 /* Release work tree lock. */
6739 got_worktree_close(worktree);
6742 error = view_loop(view);
6750 got_object_commit_close(commit);
6752 got_worktree_close(worktree);
6754 const struct got_error *close_err = got_repo_close(repo);
6759 const struct got_error *pack_err =
6760 got_repo_pack_fds_close(pack_fds);
6768 static const struct got_error *
6769 draw_tree_entries(struct tog_view *view, const char *parent_path)
6771 struct tog_tree_view_state *s = &view->state.tree;
6772 const struct got_error *err = NULL;
6773 struct got_tree_entry *te;
6776 struct tog_color *tc;
6777 int width, n, nentries, scrollx, i = 1;
6778 int limit = view->nlines;
6781 if (view_is_hsplit_top(view))
6782 --limit; /* border */
6784 werase(view->window);
6789 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6793 if (view_needs_focus_indication(view))
6794 wstandout(view->window);
6795 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6797 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6798 waddwstr(view->window, wline);
6801 while (width++ < view->ncols)
6802 waddch(view->window, ' ');
6804 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6805 if (view_needs_focus_indication(view))
6806 wstandend(view->window);
6811 if (s->first_displayed_entry) {
6812 i += got_tree_entry_get_index(s->first_displayed_entry);
6813 if (s->tree != s->root)
6814 ++i; /* account for ".." entry */
6816 nentries = got_object_tree_get_nentries(s->tree);
6817 if (asprintf(&index, "[%d/%d] %s",
6818 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6819 return got_error_from_errno("asprintf");
6820 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6824 waddwstr(view->window, wline);
6827 if (width < view->ncols - 1)
6828 waddch(view->window, '\n');
6831 waddch(view->window, '\n');
6835 if (s->first_displayed_entry == NULL) {
6836 te = got_object_tree_get_first_entry(s->tree);
6837 if (s->selected == 0) {
6839 wstandout(view->window);
6840 s->selected_entry = NULL;
6842 waddstr(view->window, " ..\n"); /* parent directory */
6843 if (s->selected == 0 && view->focussed)
6844 wstandend(view->window);
6851 te = s->first_displayed_entry;
6855 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6856 char *line = NULL, *id_str = NULL, *link_target = NULL;
6857 const char *modestr = "";
6860 te = got_object_tree_get_entry(s->tree, i);
6861 mode = got_tree_entry_get_mode(te);
6864 err = got_object_id_str(&id_str,
6865 got_tree_entry_get_id(te));
6867 return got_error_from_errno(
6868 "got_object_id_str");
6870 if (got_object_tree_entry_is_submodule(te))
6872 else if (S_ISLNK(mode)) {
6875 err = got_tree_entry_get_symlink_target(&link_target,
6881 for (i = 0; i < strlen(link_target); i++) {
6882 if (!isprint((unsigned char)link_target[i]))
6883 link_target[i] = '?';
6887 else if (S_ISDIR(mode))
6889 else if (mode & S_IXUSR)
6891 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6892 got_tree_entry_get_name(te), modestr,
6893 link_target ? " -> ": "",
6894 link_target ? link_target : "") == -1) {
6897 return got_error_from_errno("asprintf");
6902 /* use full line width to determine view->maxx */
6903 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6908 view->maxx = MAX(view->maxx, width);
6912 err = format_line(&wline, &width, &scrollx, line, view->x,
6918 if (n == s->selected) {
6920 wstandout(view->window);
6921 s->selected_entry = te;
6923 tc = match_color(&s->colors, line);
6925 wattr_on(view->window,
6926 COLOR_PAIR(tc->colorpair), NULL);
6927 waddwstr(view->window, &wline[scrollx]);
6929 wattr_off(view->window,
6930 COLOR_PAIR(tc->colorpair), NULL);
6931 if (width < view->ncols)
6932 waddch(view->window, '\n');
6933 if (n == s->selected && view->focussed)
6934 wstandend(view->window);
6940 s->last_displayed_entry = te;
6949 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6951 struct got_tree_entry *te;
6952 int isroot = s->tree == s->root;
6955 if (s->first_displayed_entry == NULL)
6958 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6959 while (i++ < maxscroll) {
6962 s->first_displayed_entry = NULL;
6965 s->first_displayed_entry = te;
6966 te = got_tree_entry_get_prev(s->tree, te);
6970 static const struct got_error *
6971 tree_scroll_down(struct tog_view *view, int maxscroll)
6973 struct tog_tree_view_state *s = &view->state.tree;
6974 struct got_tree_entry *next, *last;
6977 if (s->first_displayed_entry)
6978 next = got_tree_entry_get_next(s->tree,
6979 s->first_displayed_entry);
6981 next = got_object_tree_get_first_entry(s->tree);
6983 last = s->last_displayed_entry;
6984 while (next && n++ < maxscroll) {
6986 s->last_displayed_entry = last;
6987 last = got_tree_entry_get_next(s->tree, last);
6989 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6990 s->first_displayed_entry = next;
6991 next = got_tree_entry_get_next(s->tree, next);
6998 static const struct got_error *
6999 tree_entry_path(char **path, struct tog_parent_trees *parents,
7000 struct got_tree_entry *te)
7002 const struct got_error *err = NULL;
7003 struct tog_parent_tree *pt;
7004 size_t len = 2; /* for leading slash and NUL */
7006 TAILQ_FOREACH(pt, parents, entry)
7007 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7010 len += strlen(got_tree_entry_get_name(te));
7012 *path = calloc(1, len);
7014 return got_error_from_errno("calloc");
7017 pt = TAILQ_LAST(parents, tog_parent_trees);
7019 const char *name = got_tree_entry_get_name(pt->selected_entry);
7020 if (strlcat(*path, name, len) >= len) {
7021 err = got_error(GOT_ERR_NO_SPACE);
7024 if (strlcat(*path, "/", len) >= len) {
7025 err = got_error(GOT_ERR_NO_SPACE);
7028 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7031 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7032 err = got_error(GOT_ERR_NO_SPACE);
7044 static const struct got_error *
7045 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7046 struct got_tree_entry *te, struct tog_parent_trees *parents,
7047 struct got_object_id *commit_id, struct got_repository *repo)
7049 const struct got_error *err = NULL;
7051 struct tog_view *blame_view;
7055 err = tree_entry_path(&path, parents, te);
7059 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7060 if (blame_view == NULL) {
7061 err = got_error_from_errno("view_open");
7065 err = open_blame_view(blame_view, path, commit_id, repo);
7067 if (err->code == GOT_ERR_CANCELLED)
7069 view_close(blame_view);
7071 *new_view = blame_view;
7077 static const struct got_error *
7078 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7079 struct tog_tree_view_state *s)
7081 struct tog_view *log_view;
7082 const struct got_error *err = NULL;
7087 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7088 if (log_view == NULL)
7089 return got_error_from_errno("view_open");
7091 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7095 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7098 view_close(log_view);
7100 *new_view = log_view;
7105 static const struct got_error *
7106 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7107 const char *head_ref_name, struct got_repository *repo)
7109 const struct got_error *err = NULL;
7110 char *commit_id_str = NULL;
7111 struct tog_tree_view_state *s = &view->state.tree;
7112 struct got_commit_object *commit = NULL;
7114 TAILQ_INIT(&s->parents);
7115 STAILQ_INIT(&s->colors);
7117 s->commit_id = got_object_id_dup(commit_id);
7118 if (s->commit_id == NULL)
7119 return got_error_from_errno("got_object_id_dup");
7121 err = got_object_open_as_commit(&commit, repo, commit_id);
7126 * The root is opened here and will be closed when the view is closed.
7127 * Any visited subtrees and their path-wise parents are opened and
7130 err = got_object_open_as_tree(&s->root, repo,
7131 got_object_commit_get_tree_id(commit));
7136 err = got_object_id_str(&commit_id_str, commit_id);
7140 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7141 err = got_error_from_errno("asprintf");
7145 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7146 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7147 if (head_ref_name) {
7148 s->head_ref_name = strdup(head_ref_name);
7149 if (s->head_ref_name == NULL) {
7150 err = got_error_from_errno("strdup");
7156 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7157 err = add_color(&s->colors, "\\$$",
7158 TOG_COLOR_TREE_SUBMODULE,
7159 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7162 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7163 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7166 err = add_color(&s->colors, "/$",
7167 TOG_COLOR_TREE_DIRECTORY,
7168 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7172 err = add_color(&s->colors, "\\*$",
7173 TOG_COLOR_TREE_EXECUTABLE,
7174 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7178 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7179 get_color_value("TOG_COLOR_COMMIT"));
7184 view->show = show_tree_view;
7185 view->input = input_tree_view;
7186 view->close = close_tree_view;
7187 view->search_start = search_start_tree_view;
7188 view->search_next = search_next_tree_view;
7190 free(commit_id_str);
7192 got_object_commit_close(commit);
7194 close_tree_view(view);
7198 static const struct got_error *
7199 close_tree_view(struct tog_view *view)
7201 struct tog_tree_view_state *s = &view->state.tree;
7203 free_colors(&s->colors);
7204 free(s->tree_label);
7205 s->tree_label = NULL;
7207 s->commit_id = NULL;
7208 free(s->head_ref_name);
7209 s->head_ref_name = NULL;
7210 while (!TAILQ_EMPTY(&s->parents)) {
7211 struct tog_parent_tree *parent;
7212 parent = TAILQ_FIRST(&s->parents);
7213 TAILQ_REMOVE(&s->parents, parent, entry);
7214 if (parent->tree != s->root)
7215 got_object_tree_close(parent->tree);
7219 if (s->tree != NULL && s->tree != s->root)
7220 got_object_tree_close(s->tree);
7222 got_object_tree_close(s->root);
7226 static const struct got_error *
7227 search_start_tree_view(struct tog_view *view)
7229 struct tog_tree_view_state *s = &view->state.tree;
7231 s->matched_entry = NULL;
7236 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7238 regmatch_t regmatch;
7240 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7244 static const struct got_error *
7245 search_next_tree_view(struct tog_view *view)
7247 struct tog_tree_view_state *s = &view->state.tree;
7248 struct got_tree_entry *te = NULL;
7250 if (!view->searching) {
7251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7255 if (s->matched_entry) {
7256 if (view->searching == TOG_SEARCH_FORWARD) {
7257 if (s->selected_entry)
7258 te = got_tree_entry_get_next(s->tree,
7261 te = got_object_tree_get_first_entry(s->tree);
7263 if (s->selected_entry == NULL)
7264 te = got_object_tree_get_last_entry(s->tree);
7266 te = got_tree_entry_get_prev(s->tree,
7270 if (s->selected_entry)
7271 te = s->selected_entry;
7272 else if (view->searching == TOG_SEARCH_FORWARD)
7273 te = got_object_tree_get_first_entry(s->tree);
7275 te = got_object_tree_get_last_entry(s->tree);
7280 if (s->matched_entry == NULL) {
7281 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7284 if (view->searching == TOG_SEARCH_FORWARD)
7285 te = got_object_tree_get_first_entry(s->tree);
7287 te = got_object_tree_get_last_entry(s->tree);
7290 if (match_tree_entry(te, &view->regex)) {
7291 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7292 s->matched_entry = te;
7296 if (view->searching == TOG_SEARCH_FORWARD)
7297 te = got_tree_entry_get_next(s->tree, te);
7299 te = got_tree_entry_get_prev(s->tree, te);
7302 if (s->matched_entry) {
7303 s->first_displayed_entry = s->matched_entry;
7310 static const struct got_error *
7311 show_tree_view(struct tog_view *view)
7313 const struct got_error *err = NULL;
7314 struct tog_tree_view_state *s = &view->state.tree;
7317 err = tree_entry_path(&parent_path, &s->parents, NULL);
7321 err = draw_tree_entries(view, parent_path);
7328 static const struct got_error *
7329 tree_goto_line(struct tog_view *view, int nlines)
7331 const struct got_error *err = NULL;
7332 struct tog_tree_view_state *s = &view->state.tree;
7333 struct got_tree_entry **fte, **lte, **ste;
7334 int g, last, first = 1, i = 1;
7335 int root = s->tree == s->root;
7336 int off = root ? 1 : 2;
7343 else if (g > got_object_tree_get_nentries(s->tree))
7344 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7346 fte = &s->first_displayed_entry;
7347 lte = &s->last_displayed_entry;
7348 ste = &s->selected_entry;
7351 first = got_tree_entry_get_index(*fte);
7352 first += off; /* account for ".." */
7354 last = got_tree_entry_get_index(*lte);
7357 if (g >= first && g <= last && g - first < nlines) {
7358 s->selected = g - first;
7359 return NULL; /* gline is on the current page */
7363 i = got_tree_entry_get_index(*ste);
7368 err = tree_scroll_down(view, g - i);
7371 if (got_tree_entry_get_index(*lte) >=
7372 got_object_tree_get_nentries(s->tree) - 1 &&
7373 first + s->selected < g &&
7374 s->selected < s->ndisplayed - 1) {
7375 first = got_tree_entry_get_index(*fte);
7377 s->selected = g - first;
7380 tree_scroll_up(s, i - g);
7383 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7384 s->selected = g - 1;
7389 static const struct got_error *
7390 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7392 const struct got_error *err = NULL;
7393 struct tog_tree_view_state *s = &view->state.tree;
7394 struct got_tree_entry *te;
7395 int n, nscroll = view->nlines - 3;
7398 return tree_goto_line(view, nscroll);
7407 horizontal_scroll_input(view, ch);
7410 s->show_ids = !s->show_ids;
7415 if (!s->selected_entry)
7417 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7421 err = view_request_new(new_view, view, TOG_VIEW_REF);
7428 if (s->tree == s->root)
7429 s->first_displayed_entry =
7430 got_object_tree_get_first_entry(s->tree);
7432 s->first_displayed_entry = NULL;
7437 int eos = view->nlines - 3;
7439 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7443 te = got_object_tree_get_last_entry(s->tree);
7444 for (n = 0; n < eos; n++) {
7446 if (s->tree != s->root) {
7447 s->first_displayed_entry = NULL;
7452 s->first_displayed_entry = te;
7453 te = got_tree_entry_get_prev(s->tree, te);
7456 s->selected = n - 1;
7462 if (s->selected > 0) {
7466 tree_scroll_up(s, 1);
7467 if (s->selected_entry == NULL ||
7468 (s->tree == s->root && s->selected_entry ==
7469 got_object_tree_get_first_entry(s->tree)))
7479 if (s->tree == s->root) {
7480 if (got_object_tree_get_first_entry(s->tree) ==
7481 s->first_displayed_entry)
7482 s->selected -= MIN(s->selected, nscroll);
7484 if (s->first_displayed_entry == NULL)
7485 s->selected -= MIN(s->selected, nscroll);
7487 tree_scroll_up(s, MAX(0, nscroll));
7488 if (s->selected_entry == NULL ||
7489 (s->tree == s->root && s->selected_entry ==
7490 got_object_tree_get_first_entry(s->tree)))
7496 if (s->selected < s->ndisplayed - 1) {
7500 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7502 /* can't scroll any further */
7506 tree_scroll_down(view, 1);
7516 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7518 /* can't scroll any further; move cursor down */
7519 if (s->selected < s->ndisplayed - 1)
7520 s->selected += MIN(nscroll,
7521 s->ndisplayed - s->selected - 1);
7526 tree_scroll_down(view, nscroll);
7531 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7532 struct tog_parent_tree *parent;
7533 /* user selected '..' */
7534 if (s->tree == s->root) {
7538 parent = TAILQ_FIRST(&s->parents);
7539 TAILQ_REMOVE(&s->parents, parent,
7541 got_object_tree_close(s->tree);
7542 s->tree = parent->tree;
7543 s->first_displayed_entry =
7544 parent->first_displayed_entry;
7546 parent->selected_entry;
7547 s->selected = parent->selected;
7548 if (s->selected > view->nlines - 3) {
7549 err = offset_selection_down(view);
7554 } else if (S_ISDIR(got_tree_entry_get_mode(
7555 s->selected_entry))) {
7556 struct got_tree_object *subtree;
7558 err = got_object_open_as_tree(&subtree, s->repo,
7559 got_tree_entry_get_id(s->selected_entry));
7562 err = tree_view_visit_subtree(s, subtree);
7564 got_object_tree_close(subtree);
7567 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7568 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7571 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7572 s->selected = view->nlines - 4;
7588 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7593 static const struct got_error *
7594 cmd_tree(int argc, char *argv[])
7596 const struct got_error *error;
7597 struct got_repository *repo = NULL;
7598 struct got_worktree *worktree = NULL;
7599 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7600 struct got_object_id *commit_id = NULL;
7601 struct got_commit_object *commit = NULL;
7602 const char *commit_id_arg = NULL;
7604 struct got_reference *ref = NULL;
7605 const char *head_ref_name = NULL;
7607 struct tog_view *view;
7608 int *pack_fds = NULL;
7610 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7613 commit_id_arg = optarg;
7616 repo_path = realpath(optarg, NULL);
7617 if (repo_path == NULL)
7618 return got_error_from_errno2("realpath",
7633 error = got_repo_pack_fds_open(&pack_fds);
7637 if (repo_path == NULL) {
7638 cwd = getcwd(NULL, 0);
7640 return got_error_from_errno("getcwd");
7641 error = got_worktree_open(&worktree, cwd);
7642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7646 strdup(got_worktree_get_repo_path(worktree));
7648 repo_path = strdup(cwd);
7649 if (repo_path == NULL) {
7650 error = got_error_from_errno("strdup");
7655 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7659 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7666 error = apply_unveil(got_repo_get_path(repo), NULL);
7670 error = tog_load_refs(repo, 0);
7674 if (commit_id_arg == NULL) {
7675 error = got_repo_match_object_id(&commit_id, &label,
7676 worktree ? got_worktree_get_head_ref_name(worktree) :
7677 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7680 head_ref_name = label;
7682 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7684 head_ref_name = got_ref_get_name(ref);
7685 else if (error->code != GOT_ERR_NOT_REF)
7687 error = got_repo_match_object_id(&commit_id, NULL,
7688 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7693 error = got_object_open_as_commit(&commit, repo, commit_id);
7697 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7699 error = got_error_from_errno("view_open");
7702 error = open_tree_view(view, commit_id, head_ref_name, repo);
7705 if (!got_path_is_root_dir(in_repo_path)) {
7706 error = tree_view_walk_path(&view->state.tree, commit,
7713 /* Release work tree lock. */
7714 got_worktree_close(worktree);
7717 error = view_loop(view);
7726 const struct got_error *close_err = got_repo_close(repo);
7731 const struct got_error *pack_err =
7732 got_repo_pack_fds_close(pack_fds);
7740 static const struct got_error *
7741 ref_view_load_refs(struct tog_ref_view_state *s)
7743 struct got_reflist_entry *sre;
7744 struct tog_reflist_entry *re;
7747 TAILQ_FOREACH(sre, &tog_refs, entry) {
7748 if (strncmp(got_ref_get_name(sre->ref),
7749 "refs/got/", 9) == 0 &&
7750 strncmp(got_ref_get_name(sre->ref),
7751 "refs/got/backup/", 16) != 0)
7754 re = malloc(sizeof(*re));
7756 return got_error_from_errno("malloc");
7758 re->ref = got_ref_dup(sre->ref);
7759 if (re->ref == NULL)
7760 return got_error_from_errno("got_ref_dup");
7761 re->idx = s->nrefs++;
7762 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7765 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7770 ref_view_free_refs(struct tog_ref_view_state *s)
7772 struct tog_reflist_entry *re;
7774 while (!TAILQ_EMPTY(&s->refs)) {
7775 re = TAILQ_FIRST(&s->refs);
7776 TAILQ_REMOVE(&s->refs, re, entry);
7777 got_ref_close(re->ref);
7782 static const struct got_error *
7783 open_ref_view(struct tog_view *view, struct got_repository *repo)
7785 const struct got_error *err = NULL;
7786 struct tog_ref_view_state *s = &view->state.ref;
7788 s->selected_entry = 0;
7791 TAILQ_INIT(&s->refs);
7792 STAILQ_INIT(&s->colors);
7794 err = ref_view_load_refs(s);
7798 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7799 err = add_color(&s->colors, "^refs/heads/",
7800 TOG_COLOR_REFS_HEADS,
7801 get_color_value("TOG_COLOR_REFS_HEADS"));
7805 err = add_color(&s->colors, "^refs/tags/",
7806 TOG_COLOR_REFS_TAGS,
7807 get_color_value("TOG_COLOR_REFS_TAGS"));
7811 err = add_color(&s->colors, "^refs/remotes/",
7812 TOG_COLOR_REFS_REMOTES,
7813 get_color_value("TOG_COLOR_REFS_REMOTES"));
7817 err = add_color(&s->colors, "^refs/got/backup/",
7818 TOG_COLOR_REFS_BACKUP,
7819 get_color_value("TOG_COLOR_REFS_BACKUP"));
7824 view->show = show_ref_view;
7825 view->input = input_ref_view;
7826 view->close = close_ref_view;
7827 view->search_start = search_start_ref_view;
7828 view->search_next = search_next_ref_view;
7831 free_colors(&s->colors);
7835 static const struct got_error *
7836 close_ref_view(struct tog_view *view)
7838 struct tog_ref_view_state *s = &view->state.ref;
7840 ref_view_free_refs(s);
7841 free_colors(&s->colors);
7846 static const struct got_error *
7847 resolve_reflist_entry(struct got_object_id **commit_id,
7848 struct tog_reflist_entry *re, struct got_repository *repo)
7850 const struct got_error *err = NULL;
7851 struct got_object_id *obj_id;
7852 struct got_tag_object *tag = NULL;
7857 err = got_ref_resolve(&obj_id, repo, re->ref);
7861 err = got_object_get_type(&obj_type, repo, obj_id);
7866 case GOT_OBJ_TYPE_COMMIT:
7867 *commit_id = obj_id;
7869 case GOT_OBJ_TYPE_TAG:
7870 err = got_object_open_as_tag(&tag, repo, obj_id);
7874 err = got_object_get_type(&obj_type, repo,
7875 got_object_tag_get_object_id(tag));
7878 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7879 err = got_error(GOT_ERR_OBJ_TYPE);
7882 *commit_id = got_object_id_dup(
7883 got_object_tag_get_object_id(tag));
7884 if (*commit_id == NULL) {
7885 err = got_error_from_errno("got_object_id_dup");
7890 err = got_error(GOT_ERR_OBJ_TYPE);
7896 got_object_tag_close(tag);
7904 static const struct got_error *
7905 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7906 struct tog_reflist_entry *re, struct got_repository *repo)
7908 struct tog_view *log_view;
7909 const struct got_error *err = NULL;
7910 struct got_object_id *commit_id = NULL;
7914 err = resolve_reflist_entry(&commit_id, re, repo);
7916 if (err->code != GOT_ERR_OBJ_TYPE)
7922 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7923 if (log_view == NULL) {
7924 err = got_error_from_errno("view_open");
7928 err = open_log_view(log_view, commit_id, repo,
7929 got_ref_get_name(re->ref), "", 0);
7932 view_close(log_view);
7934 *new_view = log_view;
7940 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7942 struct tog_reflist_entry *re;
7945 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7948 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7949 while (i++ < maxscroll) {
7952 s->first_displayed_entry = re;
7953 re = TAILQ_PREV(re, tog_reflist_head, entry);
7957 static const struct got_error *
7958 ref_scroll_down(struct tog_view *view, int maxscroll)
7960 struct tog_ref_view_state *s = &view->state.ref;
7961 struct tog_reflist_entry *next, *last;
7964 if (s->first_displayed_entry)
7965 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7967 next = TAILQ_FIRST(&s->refs);
7969 last = s->last_displayed_entry;
7970 while (next && n++ < maxscroll) {
7972 s->last_displayed_entry = last;
7973 last = TAILQ_NEXT(last, entry);
7975 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7976 s->first_displayed_entry = next;
7977 next = TAILQ_NEXT(next, entry);
7984 static const struct got_error *
7985 search_start_ref_view(struct tog_view *view)
7987 struct tog_ref_view_state *s = &view->state.ref;
7989 s->matched_entry = NULL;
7994 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7996 regmatch_t regmatch;
7998 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8002 static const struct got_error *
8003 search_next_ref_view(struct tog_view *view)
8005 struct tog_ref_view_state *s = &view->state.ref;
8006 struct tog_reflist_entry *re = NULL;
8008 if (!view->searching) {
8009 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8013 if (s->matched_entry) {
8014 if (view->searching == TOG_SEARCH_FORWARD) {
8015 if (s->selected_entry)
8016 re = TAILQ_NEXT(s->selected_entry, entry);
8018 re = TAILQ_PREV(s->selected_entry,
8019 tog_reflist_head, entry);
8021 if (s->selected_entry == NULL)
8022 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8024 re = TAILQ_PREV(s->selected_entry,
8025 tog_reflist_head, entry);
8028 if (s->selected_entry)
8029 re = s->selected_entry;
8030 else if (view->searching == TOG_SEARCH_FORWARD)
8031 re = TAILQ_FIRST(&s->refs);
8033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8038 if (s->matched_entry == NULL) {
8039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8042 if (view->searching == TOG_SEARCH_FORWARD)
8043 re = TAILQ_FIRST(&s->refs);
8045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8048 if (match_reflist_entry(re, &view->regex)) {
8049 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8050 s->matched_entry = re;
8054 if (view->searching == TOG_SEARCH_FORWARD)
8055 re = TAILQ_NEXT(re, entry);
8057 re = TAILQ_PREV(re, tog_reflist_head, entry);
8060 if (s->matched_entry) {
8061 s->first_displayed_entry = s->matched_entry;
8068 static const struct got_error *
8069 show_ref_view(struct tog_view *view)
8071 const struct got_error *err = NULL;
8072 struct tog_ref_view_state *s = &view->state.ref;
8073 struct tog_reflist_entry *re;
8076 struct tog_color *tc;
8077 int width, n, scrollx;
8078 int limit = view->nlines;
8080 werase(view->window);
8083 if (view_is_hsplit_top(view))
8084 --limit; /* border */
8089 re = s->first_displayed_entry;
8091 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8093 return got_error_from_errno("asprintf");
8095 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8100 if (view_needs_focus_indication(view))
8101 wstandout(view->window);
8102 waddwstr(view->window, wline);
8103 while (width++ < view->ncols)
8104 waddch(view->window, ' ');
8105 if (view_needs_focus_indication(view))
8106 wstandend(view->window);
8116 while (re && limit > 0) {
8118 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8121 struct got_commit_object *ci;
8122 struct got_tag_object *tag;
8123 struct got_object_id *id;
8127 err = got_ref_resolve(&id, s->repo, re->ref);
8130 err = got_object_open_as_tag(&tag, s->repo, id);
8132 if (err->code != GOT_ERR_OBJ_TYPE) {
8136 err = got_object_open_as_commit(&ci, s->repo,
8142 t = got_object_commit_get_committer_time(ci);
8143 got_object_commit_close(ci);
8145 t = got_object_tag_get_tagger_time(tag);
8146 got_object_tag_close(tag);
8149 if (gmtime_r(&t, &tm) == NULL)
8150 return got_error_from_errno("gmtime_r");
8151 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8152 return got_error(GOT_ERR_NO_SPACE);
8154 if (got_ref_is_symbolic(re->ref)) {
8155 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8156 ymd : "", got_ref_get_name(re->ref),
8157 got_ref_get_symref_target(re->ref)) == -1)
8158 return got_error_from_errno("asprintf");
8159 } else if (s->show_ids) {
8160 struct got_object_id *id;
8162 err = got_ref_resolve(&id, s->repo, re->ref);
8165 err = got_object_id_str(&id_str, id);
8170 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8171 got_ref_get_name(re->ref), id_str) == -1) {
8172 err = got_error_from_errno("asprintf");
8179 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8180 got_ref_get_name(re->ref)) == -1)
8181 return got_error_from_errno("asprintf");
8183 /* use full line width to determine view->maxx */
8184 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8189 view->maxx = MAX(view->maxx, width);
8193 err = format_line(&wline, &width, &scrollx, line, view->x,
8199 if (n == s->selected) {
8201 wstandout(view->window);
8202 s->selected_entry = re;
8204 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8206 wattr_on(view->window,
8207 COLOR_PAIR(tc->colorpair), NULL);
8208 waddwstr(view->window, &wline[scrollx]);
8210 wattr_off(view->window,
8211 COLOR_PAIR(tc->colorpair), NULL);
8212 if (width < view->ncols)
8213 waddch(view->window, '\n');
8214 if (n == s->selected && view->focussed)
8215 wstandend(view->window);
8221 s->last_displayed_entry = re;
8224 re = TAILQ_NEXT(re, entry);
8231 static const struct got_error *
8232 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8233 struct tog_reflist_entry *re, struct got_repository *repo)
8235 const struct got_error *err = NULL;
8236 struct got_object_id *commit_id = NULL;
8237 struct tog_view *tree_view;
8241 err = resolve_reflist_entry(&commit_id, re, repo);
8243 if (err->code != GOT_ERR_OBJ_TYPE)
8250 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8251 if (tree_view == NULL) {
8252 err = got_error_from_errno("view_open");
8256 err = open_tree_view(tree_view, commit_id,
8257 got_ref_get_name(re->ref), repo);
8261 *new_view = tree_view;
8267 static const struct got_error *
8268 ref_goto_line(struct tog_view *view, int nlines)
8270 const struct got_error *err = NULL;
8271 struct tog_ref_view_state *s = &view->state.ref;
8272 int g, idx = s->selected_entry->idx;
8279 else if (g > s->nrefs)
8282 if (g >= s->first_displayed_entry->idx + 1 &&
8283 g <= s->last_displayed_entry->idx + 1 &&
8284 g - s->first_displayed_entry->idx - 1 < nlines) {
8285 s->selected = g - s->first_displayed_entry->idx - 1;
8290 err = ref_scroll_down(view, g - idx - 1);
8293 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8294 s->first_displayed_entry->idx + s->selected < g &&
8295 s->selected < s->ndisplayed - 1)
8296 s->selected = g - s->first_displayed_entry->idx - 1;
8297 } else if (idx + 1 > g)
8298 ref_scroll_up(s, idx - g + 1);
8300 if (g < nlines && s->first_displayed_entry->idx == 0)
8301 s->selected = g - 1;
8307 static const struct got_error *
8308 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8310 const struct got_error *err = NULL;
8311 struct tog_ref_view_state *s = &view->state.ref;
8312 struct tog_reflist_entry *re;
8313 int n, nscroll = view->nlines - 1;
8316 return ref_goto_line(view, nscroll);
8325 horizontal_scroll_input(view, ch);
8328 s->show_ids = !s->show_ids;
8332 s->show_date = !s->show_date;
8336 s->sort_by_date = !s->sort_by_date;
8337 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8339 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8340 got_ref_cmp_by_commit_timestamp_descending :
8341 tog_ref_cmp_by_name, s->repo);
8344 got_reflist_object_id_map_free(tog_refs_idmap);
8345 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8346 &tog_refs, s->repo);
8349 ref_view_free_refs(s);
8350 err = ref_view_load_refs(s);
8355 if (!s->selected_entry)
8357 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8361 if (!s->selected_entry)
8363 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8370 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8375 int eos = view->nlines - 1;
8377 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8381 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8382 for (n = 0; n < eos; n++) {
8385 s->first_displayed_entry = re;
8386 re = TAILQ_PREV(re, tog_reflist_head, entry);
8389 s->selected = n - 1;
8395 if (s->selected > 0) {
8399 ref_scroll_up(s, 1);
8400 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8410 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8411 s->selected -= MIN(nscroll, s->selected);
8412 ref_scroll_up(s, MAX(0, nscroll));
8413 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8419 if (s->selected < s->ndisplayed - 1) {
8423 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8424 /* can't scroll any further */
8428 ref_scroll_down(view, 1);
8438 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8439 /* can't scroll any further; move cursor down */
8440 if (s->selected < s->ndisplayed - 1)
8441 s->selected += MIN(nscroll,
8442 s->ndisplayed - s->selected - 1);
8443 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8444 s->selected += s->ndisplayed - s->selected - 1;
8448 ref_scroll_down(view, nscroll);
8453 err = tog_load_refs(s->repo, s->sort_by_date);
8456 ref_view_free_refs(s);
8457 err = ref_view_load_refs(s);
8460 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8461 s->selected = view->nlines - 2;
8475 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8480 static const struct got_error *
8481 cmd_ref(int argc, char *argv[])
8483 const struct got_error *error;
8484 struct got_repository *repo = NULL;
8485 struct got_worktree *worktree = NULL;
8486 char *cwd = NULL, *repo_path = NULL;
8488 struct tog_view *view;
8489 int *pack_fds = NULL;
8491 while ((ch = getopt(argc, argv, "r:")) != -1) {
8494 repo_path = realpath(optarg, NULL);
8495 if (repo_path == NULL)
8496 return got_error_from_errno2("realpath",
8511 error = got_repo_pack_fds_open(&pack_fds);
8515 if (repo_path == NULL) {
8516 cwd = getcwd(NULL, 0);
8518 return got_error_from_errno("getcwd");
8519 error = got_worktree_open(&worktree, cwd);
8520 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8524 strdup(got_worktree_get_repo_path(worktree));
8526 repo_path = strdup(cwd);
8527 if (repo_path == NULL) {
8528 error = got_error_from_errno("strdup");
8533 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8539 error = apply_unveil(got_repo_get_path(repo), NULL);
8543 error = tog_load_refs(repo, 0);
8547 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8549 error = got_error_from_errno("view_open");
8553 error = open_ref_view(view, repo);
8558 /* Release work tree lock. */
8559 got_worktree_close(worktree);
8562 error = view_loop(view);
8567 const struct got_error *close_err = got_repo_close(repo);
8572 const struct got_error *pack_err =
8573 got_repo_pack_fds_close(pack_fds);
8581 static const struct got_error*
8582 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8591 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8595 if (mvwprintw(win, y, x, "%s", str) == ERR)
8596 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8603 static const struct got_error *
8604 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8608 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8610 free(*line_offsets);
8611 *line_offsets = NULL;
8612 return got_error_from_errno("reallocarray");
8616 (*line_offsets)[*nlines] = off;
8621 static const struct got_error *
8622 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8626 for (;n > 0; --n, ++km) {
8630 if (km->keys == NULL)
8633 t = t0 = strdup(km->keys);
8635 return got_error_from_errno("strdup");
8638 while ((k = strsep(&t, " ")) != NULL)
8639 len += strlen(k) > 1 ? 2 : 0;
8641 *ret = MAX(*ret, len);
8648 * Write keymap section headers, keys, and key info in km to f.
8649 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8650 * wrap control and symbolic keys in guillemets, else use <>.
8652 static const struct got_error *
8653 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8658 static const char *u8_glyph[] = {
8659 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8660 "\xe2\x80\xba" /* U+203A (utf8 >) */
8663 int cs, s, first = 1;
8665 cs = got_locale_is_utf8();
8667 t = t0 = strdup(km->keys);
8669 return got_error_from_errno("strdup");
8671 len = strlen(km->keys);
8672 while ((k = strsep(&t, " ")) != NULL) {
8673 s = strlen(k) > 1; /* control or symbolic key */
8674 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8675 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8676 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8679 return got_error_from_errno("fprintf");
8687 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8689 return got_error_from_errno("fprintf");
8695 static const struct got_error *
8696 format_help(struct tog_help_view_state *s)
8698 const struct got_error *err = NULL;
8700 int i, max, n, show = s->all;
8701 static const struct tog_key_map km[] = {
8702 #define KEYMAP_(info, type) { NULL, (info), type }
8703 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8709 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8714 err = max_key_str(&max, km, n);
8718 for (i = 0; i < n; ++i) {
8719 if (km[i].keys == NULL) {
8721 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8722 km[i].type == s->type || s->all)
8726 err = format_help_line(&off, s->f, &km[i], max);
8729 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8736 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8740 static const struct got_error *
8741 create_help(struct tog_help_view_state *s)
8744 const struct got_error *err;
8746 free(s->line_offsets);
8747 s->line_offsets = NULL;
8752 return got_error_from_errno("got_opentemp");
8755 err = format_help(s);
8759 if (s->f && fflush(s->f) != 0)
8760 return got_error_from_errno("fflush");
8765 static const struct got_error *
8766 search_start_help_view(struct tog_view *view)
8768 view->state.help.matched_line = 0;
8773 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8774 size_t *nlines, int **first, int **last, int **match, int **selected)
8776 struct tog_help_view_state *s = &view->state.help;
8779 *nlines = s->nlines;
8780 *line_offsets = s->line_offsets;
8781 *match = &s->matched_line;
8782 *first = &s->first_displayed_line;
8783 *last = &s->last_displayed_line;
8784 *selected = &s->selected_line;
8787 static const struct got_error *
8788 show_help_view(struct tog_view *view)
8790 struct tog_help_view_state *s = &view->state.help;
8791 const struct got_error *err;
8792 regmatch_t *regmatch = &view->regmatch;
8797 int width, nprinted = 0, rc = 0;
8798 int eos = view->nlines;
8800 if (view_is_hsplit_top(view))
8801 --eos; /* account for border */
8805 werase(view->window);
8807 if (view->gline > s->nlines - 1)
8808 view->gline = s->nlines - 1;
8810 err = win_draw_center(view->window, 0, 0, view->ncols,
8811 view_needs_focus_indication(view),
8812 "tog help (press q to return to tog)");
8817 waddstr(view->window, "\n\n");
8823 while (eos > 0 && nprinted < eos) {
8826 linelen = getline(&line, &linesz, s->f);
8827 if (linelen == -1) {
8830 return got_ferror(s->f, GOT_ERR_IO);
8835 if (++s->lineno < s->first_displayed_line)
8837 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8839 if (s->lineno == view->hiline)
8842 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8848 view->maxx = MAX(view->maxx, width);
8853 wattron(view->window, attr);
8854 if (s->first_displayed_line + nprinted == s->matched_line &&
8855 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8856 err = add_matched_line(&width, line, view->ncols - 1, 0,
8857 view->window, view->x, regmatch);
8865 err = format_line(&wline, &width, &skip, line,
8866 view->x, view->ncols, 0, view->x ? 1 : 0);
8871 waddwstr(view->window, &wline[skip]);
8875 if (s->lineno == view->hiline) {
8876 while (width++ < view->ncols)
8877 waddch(view->window, ' ');
8879 if (width < view->ncols)
8880 waddch(view->window, '\n');
8883 wattroff(view->window, attr);
8884 if (++nprinted == 1)
8885 s->first_displayed_line = s->lineno;
8889 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8891 s->last_displayed_line = s->first_displayed_line;
8896 rc = waddnstr(view->window,
8897 "See the tog(1) manual page for full documentation",
8900 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8902 wmove(view->window, view->nlines - 1, 0);
8903 wclrtoeol(view->window);
8904 wstandout(view->window);
8905 rc = waddnstr(view->window, "scroll down for more...",
8908 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8909 if (getcurx(view->window) < view->ncols - 6) {
8910 rc = wprintw(view->window, "[%.0f%%]",
8911 100.00 * s->last_displayed_line / s->nlines);
8913 return got_error_msg(GOT_ERR_IO, "wprintw");
8915 wstandend(view->window);
8921 static const struct got_error *
8922 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8924 struct tog_help_view_state *s = &view->state.help;
8925 const struct got_error *err = NULL;
8931 eos = nscroll = view->nlines;
8932 if (view_is_hsplit_top(view))
8935 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8944 horizontal_scroll_input(view, ch);
8948 s->first_displayed_line = 1;
8956 s->first_displayed_line = (s->nlines - eos) + 3;
8961 if (s->first_displayed_line > 1)
8962 --s->first_displayed_line;
8973 if (s->first_displayed_line == 1) {
8977 while (--nscroll > 0 && s->first_displayed_line > 1)
8978 s->first_displayed_line--;
8984 ++s->first_displayed_line;
9000 while (!s->eof && --nscroll > 0) {
9001 linelen = getline(&line, &linesz, s->f);
9002 s->first_displayed_line++;
9003 if (linelen == -1) {
9007 err = got_ferror(s->f, GOT_ERR_IO);
9021 static const struct got_error *
9022 close_help_view(struct tog_view *view)
9024 struct tog_help_view_state *s = &view->state.help;
9026 free(s->line_offsets);
9027 s->line_offsets = NULL;
9028 if (fclose(s->f) == EOF)
9029 return got_error_from_errno("fclose");
9034 static const struct got_error *
9035 reset_help_view(struct tog_view *view)
9037 struct tog_help_view_state *s = &view->state.help;
9040 if (s->f && fclose(s->f) == EOF)
9041 return got_error_from_errno("fclose");
9043 wclear(view->window);
9047 s->first_displayed_line = 1;
9048 s->last_displayed_line = view->nlines;
9049 s->matched_line = 0;
9051 return create_help(s);
9054 static const struct got_error *
9055 open_help_view(struct tog_view *view, struct tog_view *parent)
9057 const struct got_error *err = NULL;
9058 struct tog_help_view_state *s = &view->state.help;
9060 s->type = (enum tog_keymap_type)parent->type;
9061 s->first_displayed_line = 1;
9062 s->last_displayed_line = view->nlines;
9063 s->selected_line = 1;
9065 view->show = show_help_view;
9066 view->input = input_help_view;
9067 view->reset = reset_help_view;
9068 view->close = close_help_view;
9069 view->search_start = search_start_help_view;
9070 view->search_setup = search_setup_help_view;
9071 view->search_next = search_next_view_match;
9073 err = create_help(s);
9077 static const struct got_error *
9078 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9079 enum tog_view_type request, int y, int x)
9081 const struct got_error *err = NULL;
9087 if (view->type == TOG_VIEW_LOG) {
9088 struct tog_log_view_state *s = &view->state.log;
9090 err = open_diff_view_for_commit(new_view, y, x,
9091 s->selected_entry->commit, s->selected_entry->id,
9094 return got_error_msg(GOT_ERR_NOT_IMPL,
9095 "parent/child view pair not supported");
9097 case TOG_VIEW_BLAME:
9098 if (view->type == TOG_VIEW_TREE) {
9099 struct tog_tree_view_state *s = &view->state.tree;
9101 err = blame_tree_entry(new_view, y, x,
9102 s->selected_entry, &s->parents, s->commit_id,
9105 return got_error_msg(GOT_ERR_NOT_IMPL,
9106 "parent/child view pair not supported");
9109 if (view->type == TOG_VIEW_BLAME)
9110 err = log_annotated_line(new_view, y, x,
9111 view->state.blame.repo, view->state.blame.id_to_log);
9112 else if (view->type == TOG_VIEW_TREE)
9113 err = log_selected_tree_entry(new_view, y, x,
9115 else if (view->type == TOG_VIEW_REF)
9116 err = log_ref_entry(new_view, y, x,
9117 view->state.ref.selected_entry,
9118 view->state.ref.repo);
9120 return got_error_msg(GOT_ERR_NOT_IMPL,
9121 "parent/child view pair not supported");
9124 if (view->type == TOG_VIEW_LOG)
9125 err = browse_commit_tree(new_view, y, x,
9126 view->state.log.selected_entry,
9127 view->state.log.in_repo_path,
9128 view->state.log.head_ref_name,
9129 view->state.log.repo);
9130 else if (view->type == TOG_VIEW_REF)
9131 err = browse_ref_tree(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 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9140 if (*new_view == NULL)
9141 return got_error_from_errno("view_open");
9142 if (view->type == TOG_VIEW_LOG)
9143 err = open_ref_view(*new_view, view->state.log.repo);
9144 else if (view->type == TOG_VIEW_TREE)
9145 err = open_ref_view(*new_view, view->state.tree.repo);
9147 err = got_error_msg(GOT_ERR_NOT_IMPL,
9148 "parent/child view pair not supported");
9150 view_close(*new_view);
9153 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9154 if (*new_view == NULL)
9155 return got_error_from_errno("view_open");
9156 err = open_help_view(*new_view, view);
9158 view_close(*new_view);
9161 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9168 * If view was scrolled down to move the selected line into view when opening a
9169 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9172 offset_selection_up(struct tog_view *view)
9174 switch (view->type) {
9175 case TOG_VIEW_BLAME: {
9176 struct tog_blame_view_state *s = &view->state.blame;
9177 if (s->first_displayed_line == 1) {
9178 s->selected_line = MAX(s->selected_line - view->offset,
9182 if (s->first_displayed_line > view->offset)
9183 s->first_displayed_line -= view->offset;
9185 s->first_displayed_line = 1;
9186 s->selected_line += view->offset;
9190 log_scroll_up(&view->state.log, view->offset);
9191 view->state.log.selected += view->offset;
9194 ref_scroll_up(&view->state.ref, view->offset);
9195 view->state.ref.selected += view->offset;
9198 tree_scroll_up(&view->state.tree, view->offset);
9199 view->state.tree.selected += view->offset;
9209 * If the selected line is in the section of screen covered by the bottom split,
9210 * scroll down offset lines to move it into view and index its new position.
9212 static const struct got_error *
9213 offset_selection_down(struct tog_view *view)
9215 const struct got_error *err = NULL;
9216 const struct got_error *(*scrolld)(struct tog_view *, int);
9217 int *selected = NULL;
9220 switch (view->type) {
9221 case TOG_VIEW_BLAME: {
9222 struct tog_blame_view_state *s = &view->state.blame;
9225 if (s->selected_line > view->nlines - header) {
9226 offset = abs(view->nlines - s->selected_line - header);
9227 s->first_displayed_line += offset;
9228 s->selected_line -= offset;
9229 view->offset = offset;
9233 case TOG_VIEW_LOG: {
9234 struct tog_log_view_state *s = &view->state.log;
9235 scrolld = &log_scroll_down;
9236 header = view_is_parent_view(view) ? 3 : 2;
9237 selected = &s->selected;
9240 case TOG_VIEW_REF: {
9241 struct tog_ref_view_state *s = &view->state.ref;
9242 scrolld = &ref_scroll_down;
9244 selected = &s->selected;
9247 case TOG_VIEW_TREE: {
9248 struct tog_tree_view_state *s = &view->state.tree;
9249 scrolld = &tree_scroll_down;
9251 selected = &s->selected;
9261 if (selected && *selected > view->nlines - header) {
9262 offset = abs(view->nlines - *selected - header);
9263 view->offset = offset;
9264 if (scrolld && offset) {
9265 err = scrolld(view, offset);
9266 *selected -= offset;
9274 list_commands(FILE *fp)
9278 fprintf(fp, "commands:");
9279 for (i = 0; i < nitems(tog_commands); i++) {
9280 const struct tog_cmd *cmd = &tog_commands[i];
9281 fprintf(fp, " %s", cmd->name);
9287 usage(int hflag, int status)
9289 FILE *fp = (status == 0) ? stdout : stderr;
9291 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9294 fprintf(fp, "lazy usage: %s path\n", getprogname());
9301 make_argv(int argc, ...)
9309 argv = calloc(argc, sizeof(char *));
9312 for (i = 0; i < argc; i++) {
9313 argv[i] = strdup(va_arg(ap, char *));
9314 if (argv[i] == NULL)
9323 * Try to convert 'tog path' into a 'tog log path' command.
9324 * The user could simply have mistyped the command rather than knowingly
9325 * provided a path. So check whether argv[0] can in fact be resolved
9326 * to a path in the HEAD commit and print a special error if not.
9327 * This hack is for mpi@ <3
9329 static const struct got_error *
9330 tog_log_with_path(int argc, char *argv[])
9332 const struct got_error *error = NULL, *close_err;
9333 const struct tog_cmd *cmd = NULL;
9334 struct got_repository *repo = NULL;
9335 struct got_worktree *worktree = NULL;
9336 struct got_object_id *commit_id = NULL, *id = NULL;
9337 struct got_commit_object *commit = NULL;
9338 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9339 char *commit_id_str = NULL, **cmd_argv = NULL;
9340 int *pack_fds = NULL;
9342 cwd = getcwd(NULL, 0);
9344 return got_error_from_errno("getcwd");
9346 error = got_repo_pack_fds_open(&pack_fds);
9350 error = got_worktree_open(&worktree, cwd);
9351 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9355 repo_path = strdup(got_worktree_get_repo_path(worktree));
9357 repo_path = strdup(cwd);
9358 if (repo_path == NULL) {
9359 error = got_error_from_errno("strdup");
9363 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9367 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9372 error = tog_load_refs(repo, 0);
9375 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9376 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9377 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9382 got_worktree_close(worktree);
9386 error = got_object_open_as_commit(&commit, repo, commit_id);
9390 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9392 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9394 fprintf(stderr, "%s: '%s' is no known command or path\n",
9395 getprogname(), argv[0]);
9400 error = got_object_id_str(&commit_id_str, commit_id);
9404 cmd = &tog_commands[0]; /* log */
9406 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9407 error = cmd->cmd_main(argc, cmd_argv);
9410 close_err = got_repo_close(repo);
9415 got_object_commit_close(commit);
9417 got_worktree_close(worktree);
9419 const struct got_error *pack_err =
9420 got_repo_pack_fds_close(pack_fds);
9425 free(commit_id_str);
9432 for (i = 0; i < argc; i++)
9441 main(int argc, char *argv[])
9443 const struct got_error *error = NULL;
9444 const struct tog_cmd *cmd = NULL;
9445 int ch, hflag = 0, Vflag = 0;
9446 char **cmd_argv = NULL;
9447 static const struct option longopts[] = {
9448 { "version", no_argument, NULL, 'V' },
9451 char *diff_algo_str = NULL;
9453 if (!isatty(STDIN_FILENO))
9454 errx(1, "standard input is not a tty");
9456 setlocale(LC_CTYPE, "");
9458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9478 got_version_print_str();
9483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9491 /* Build an argument vector which runs a default command. */
9492 cmd = &tog_commands[0];
9494 cmd_argv = make_argv(argc, cmd->name);
9498 /* Did the user specify a command? */
9499 for (i = 0; i < nitems(tog_commands); i++) {
9500 if (strncmp(tog_commands[i].name, argv[0],
9501 strlen(argv[0])) == 0) {
9502 cmd = &tog_commands[i];
9508 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9509 if (diff_algo_str) {
9510 if (strcasecmp(diff_algo_str, "patience") == 0)
9511 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9512 if (strcasecmp(diff_algo_str, "myers") == 0)
9513 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9519 /* No command specified; try log with a path */
9520 error = tog_log_with_path(argc, argv);
9525 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9531 for (i = 0; i < argc; i++)
9536 if (error && error->code != GOT_ERR_CANCELLED &&
9537 error->code != GOT_ERR_EOF &&
9538 error->code != GOT_ERR_PRIVSEP_EXIT &&
9539 error->code != GOT_ERR_PRIVSEP_PIPE &&
9540 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9541 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);