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, int fast_refresh)
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);
1330 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1331 err = view->search_start(view);
1333 regfree(&view->regex);
1336 view->search_started = 1;
1337 view->searching = TOG_SEARCH_FORWARD;
1338 view->search_next_done = 0;
1339 view->search_next(view);
1345 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1346 static const struct got_error *
1347 switch_split(struct tog_view *view)
1349 const struct got_error *err = NULL;
1350 struct tog_view *v = NULL;
1357 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1358 v->mode = TOG_VIEW_SPLIT_VERT;
1360 v->mode = TOG_VIEW_SPLIT_HRZN;
1364 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1365 v->mode = TOG_VIEW_SPLIT_NONE;
1367 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1368 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1369 v->child->begin_y = v->child->resized_y;
1370 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1371 v->child->begin_x = v->child->resized_x;
1374 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1376 v->child->ncols = COLS;
1377 v->child->nscrolled = LINES - v->child->nlines;
1379 err = view_init_hsplit(v, v->child->begin_y);
1383 v->child->mode = v->mode;
1384 v->child->nlines = v->lines - v->child->begin_y;
1387 err = view_fullscreen(v);
1390 err = view_splitscreen(v->child);
1394 if (v->mode == TOG_VIEW_SPLIT_NONE)
1395 v->mode = TOG_VIEW_SPLIT_VERT;
1396 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1397 err = offset_selection_down(v);
1400 err = offset_selection_down(v->child);
1404 offset_selection_up(v);
1405 offset_selection_up(v->child);
1408 err = v->resize(v, 0);
1409 else if (v->child->resize)
1410 err = v->child->resize(v->child, 0);
1416 * Compute view->count from numeric input. Assign total to view->count and
1417 * return first non-numeric key entered.
1420 get_compound_key(struct tog_view *view, int c)
1422 struct tog_view *v = view;
1425 if (view_is_hsplit_top(view))
1427 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1431 cbreak(); /* block for input */
1432 nodelay(view->window, FALSE);
1433 wmove(v->window, v->nlines - 1, 0);
1434 wclrtoeol(v->window);
1435 waddch(v->window, ':');
1438 x = getcurx(v->window);
1439 if (x != ERR && x < view->ncols) {
1440 waddch(v->window, c);
1441 wrefresh(v->window);
1445 * Don't overflow. Max valid request should be the greatest
1446 * between the longest and total lines; cap at 10 million.
1451 n = n * 10 + (c - '0');
1452 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1454 if (c == 'G' || c == 'g') { /* nG key map */
1455 view->gline = view->hiline = n;
1460 /* Massage excessive or inapplicable values at the input handler. */
1467 action_report(struct tog_view *view)
1469 struct tog_view *v = view;
1471 if (view_is_hsplit_top(view))
1473 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1476 wmove(v->window, v->nlines - 1, 0);
1477 wclrtoeol(v->window);
1478 wprintw(v->window, ":%s", view->action);
1479 wrefresh(v->window);
1482 * Clear action status report. Only clear in blame view
1483 * once annotating is complete, otherwise it's too fast.
1485 if (view->type == TOG_VIEW_BLAME) {
1486 if (view->state.blame.blame_complete)
1487 view->action = NULL;
1489 view->action = NULL;
1492 static const struct got_error *
1493 view_input(struct tog_view **new, int *done, struct tog_view *view,
1494 struct tog_view_list_head *views, int fast_refresh)
1496 const struct got_error *err = NULL;
1503 action_report(view);
1505 /* Clear "no matches" indicator. */
1506 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1507 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1508 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1512 if (view->searching && !view->search_next_done) {
1513 errcode = pthread_mutex_unlock(&tog_mutex);
1515 return got_error_set_errno(errcode,
1516 "pthread_mutex_unlock");
1518 errcode = pthread_mutex_lock(&tog_mutex);
1520 return got_error_set_errno(errcode,
1521 "pthread_mutex_lock");
1522 view->search_next(view);
1526 /* Allow threads to make progress while we are waiting for input. */
1527 errcode = pthread_mutex_unlock(&tog_mutex);
1529 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1530 /* If we have an unfinished count, let C-g or backspace abort. */
1531 if (view->count && --view->count) {
1533 nodelay(view->window, TRUE);
1534 ch = wgetch(view->window);
1535 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1540 ch = wgetch(view->window);
1541 if (ch >= '1' && ch <= '9')
1542 view->ch = ch = get_compound_key(view, ch);
1544 if (view->hiline && ch != ERR && ch != 0)
1545 view->hiline = 0; /* key pressed, clear line highlight */
1546 nodelay(view->window, TRUE);
1547 errcode = pthread_mutex_lock(&tog_mutex);
1549 return got_error_set_errno(errcode, "pthread_mutex_lock");
1551 if (tog_sigwinch_received || tog_sigcont_received) {
1553 tog_sigwinch_received = 0;
1554 tog_sigcont_received = 0;
1555 TAILQ_FOREACH(v, views, entry) {
1556 err = view_resize(v);
1559 err = v->input(new, v, KEY_RESIZE);
1563 err = view_resize(v->child);
1566 err = v->child->input(new, v->child,
1570 if (v->child->resized_x || v->child->resized_y) {
1571 err = view_resize_split(v, 0);
1583 if (view->type == TOG_VIEW_HELP)
1584 err = view->reset(view);
1586 err = view_request_new(new, view, TOG_VIEW_HELP);
1592 view->child->focussed = 1;
1593 view->focus_child = 1;
1594 } else if (view->parent) {
1596 view->parent->focussed = 1;
1597 view->parent->focus_child = 0;
1598 if (!view_is_splitscreen(view)) {
1599 if (view->parent->resize) {
1600 err = view->parent->resize(view->parent,
1605 offset_selection_up(view->parent);
1606 err = view_fullscreen(view->parent);
1613 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1614 if (view->parent->resize) {
1615 /* might need more commits to fill fullscreen */
1616 err = view->parent->resize(view->parent, 0);
1620 offset_selection_up(view->parent);
1622 err = view->input(new, view, ch);
1630 if (view_is_parent_view(view)) {
1631 if (view->child == NULL)
1633 if (view_is_splitscreen(view->child)) {
1635 view->child->focussed = 1;
1636 err = view_fullscreen(view->child);
1638 err = view_splitscreen(view->child);
1640 err = view_resize_split(view, 0);
1644 err = view->child->input(new, view->child,
1647 if (view_is_splitscreen(view)) {
1648 view->parent->focussed = 0;
1650 err = view_fullscreen(view);
1652 err = view_splitscreen(view);
1653 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1654 err = view_resize(view->parent);
1656 err = view_resize_split(view, 0);
1660 err = view->input(new, view, KEY_RESIZE);
1665 err = view->resize(view, 0);
1670 err = offset_selection_down(view->parent);
1672 err = offset_selection_down(view);
1676 err = switch_split(view);
1679 err = view_resize_split(view, -1);
1682 err = view_resize_split(view, 1);
1688 if (view->search_start)
1689 view_search_start(view, fast_refresh);
1691 err = view->input(new, view, ch);
1695 if (view->search_started && view->search_next) {
1696 view->searching = (ch == 'n' ?
1697 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1698 view->search_next_done = 0;
1699 view->search_next(view);
1701 err = view->input(new, view, ch);
1704 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1705 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1706 view->action = "Patience diff algorithm";
1708 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1709 view->action = "Myers diff algorithm";
1711 TAILQ_FOREACH(v, views, entry) {
1717 if (v->child && v->child->reset) {
1718 err = v->child->reset(v->child);
1725 err = view->input(new, view, ch);
1733 view_needs_focus_indication(struct tog_view *view)
1735 if (view_is_parent_view(view)) {
1736 if (view->child == NULL || view->child->focussed)
1738 if (!view_is_splitscreen(view->child))
1740 } else if (!view_is_splitscreen(view))
1743 return view->focussed;
1746 static const struct got_error *
1747 view_loop(struct tog_view *view)
1749 const struct got_error *err = NULL;
1750 struct tog_view_list_head views;
1751 struct tog_view *new_view;
1753 int fast_refresh = 10;
1754 int done = 0, errcode;
1756 mode = getenv("TOG_VIEW_SPLIT_MODE");
1757 if (!mode || !(*mode == 'h' || *mode == 'H'))
1758 view->mode = TOG_VIEW_SPLIT_VERT;
1760 view->mode = TOG_VIEW_SPLIT_HRZN;
1762 errcode = pthread_mutex_lock(&tog_mutex);
1764 return got_error_set_errno(errcode, "pthread_mutex_lock");
1767 TAILQ_INSERT_HEAD(&views, view, entry);
1770 err = view->show(view);
1775 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1776 !tog_fatal_signal_received()) {
1777 /* Refresh fast during initialization, then become slower. */
1778 if (fast_refresh && --fast_refresh == 0)
1779 halfdelay(10); /* switch to once per second */
1781 err = view_input(&new_view, &done, view, &views, fast_refresh);
1785 if (view->dying && view == TAILQ_FIRST(&views) &&
1786 TAILQ_NEXT(view, entry) == NULL)
1792 * When we quit, scroll the screen up a single line
1793 * so we don't lose any information.
1795 TAILQ_FOREACH(v, &views, entry) {
1796 wmove(v->window, 0, 0);
1797 wdeleteln(v->window);
1798 wnoutrefresh(v->window);
1799 if (v->child && !view_is_fullscreen(v)) {
1800 wmove(v->child->window, 0, 0);
1801 wdeleteln(v->child->window);
1802 wnoutrefresh(v->child->window);
1809 struct tog_view *v, *prev = NULL;
1811 if (view_is_parent_view(view))
1812 prev = TAILQ_PREV(view, tog_view_list_head,
1814 else if (view->parent)
1815 prev = view->parent;
1818 view->parent->child = NULL;
1819 view->parent->focus_child = 0;
1820 /* Restore fullscreen line height. */
1821 view->parent->nlines = view->parent->lines;
1822 err = view_resize(view->parent);
1825 /* Make resized splits persist. */
1826 view_transfer_size(view->parent, view);
1828 TAILQ_REMOVE(&views, view, entry);
1830 err = view_close(view);
1835 TAILQ_FOREACH(v, &views, entry) {
1839 if (view == NULL && new_view == NULL) {
1840 /* No view has focus. Try to pick one. */
1843 else if (!TAILQ_EMPTY(&views)) {
1844 view = TAILQ_LAST(&views,
1845 tog_view_list_head);
1848 if (view->focus_child) {
1849 view->child->focussed = 1;
1857 struct tog_view *v, *t;
1858 /* Only allow one parent view per type. */
1859 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1860 if (v->type != new_view->type)
1862 TAILQ_REMOVE(&views, v, entry);
1863 err = view_close(v);
1868 TAILQ_INSERT_TAIL(&views, new_view, entry);
1871 if (view && !done) {
1872 if (view_is_parent_view(view)) {
1873 if (view->child && view->child->focussed)
1876 if (view->parent && view->parent->focussed)
1877 view = view->parent;
1879 show_panel(view->panel);
1880 if (view->child && view_is_splitscreen(view->child))
1881 show_panel(view->child->panel);
1882 if (view->parent && view_is_splitscreen(view)) {
1883 err = view->parent->show(view->parent);
1887 err = view->show(view);
1891 err = view->child->show(view->child);
1900 while (!TAILQ_EMPTY(&views)) {
1901 const struct got_error *close_err;
1902 view = TAILQ_FIRST(&views);
1903 TAILQ_REMOVE(&views, view, entry);
1904 close_err = view_close(view);
1905 if (close_err && err == NULL)
1909 errcode = pthread_mutex_unlock(&tog_mutex);
1910 if (errcode && err == NULL)
1911 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1921 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1926 /* Create newly allocated wide-character string equivalent to a byte string. */
1927 static const struct got_error *
1928 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1931 const struct got_error *err = NULL;
1934 *wlen = mbstowcs(NULL, s, 0);
1935 if (*wlen == (size_t)-1) {
1937 if (errno != EILSEQ)
1938 return got_error_from_errno("mbstowcs");
1940 /* byte string invalid in current encoding; try to "fix" it */
1941 err = got_mbsavis(&vis, &vislen, s);
1944 *wlen = mbstowcs(NULL, vis, 0);
1945 if (*wlen == (size_t)-1) {
1946 err = got_error_from_errno("mbstowcs"); /* give up */
1951 *ws = calloc(*wlen + 1, sizeof(**ws));
1953 err = got_error_from_errno("calloc");
1957 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1958 err = got_error_from_errno("mbstowcs");
1969 static const struct got_error *
1970 expand_tab(char **ptr, const char *src)
1973 size_t len, n, idx = 0, sz = 0;
1976 n = len = strlen(src);
1977 dst = malloc(n + 1);
1979 return got_error_from_errno("malloc");
1981 while (idx < len && src[idx]) {
1982 const char c = src[idx];
1985 size_t nb = TABSIZE - sz % TABSIZE;
1988 p = realloc(dst, n + nb);
1991 return got_error_from_errno("realloc");
1996 memset(dst + sz, ' ', nb);
1999 dst[sz++] = src[idx];
2009 * Advance at most n columns from wline starting at offset off.
2010 * Return the index to the first character after the span operation.
2011 * Return the combined column width of all spanned wide character in
2015 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2017 int width, i, cols = 0;
2024 for (i = off; wline[i] != L'\0'; ++i) {
2025 if (wline[i] == L'\t')
2026 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2028 width = wcwidth(wline[i]);
2035 if (cols + width > n)
2045 * Format a line for display, ensuring that it won't overflow a width limit.
2046 * With scrolling, the width returned refers to the scrolled version of the
2047 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2049 static const struct got_error *
2050 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2051 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2053 const struct got_error *err = NULL;
2055 wchar_t *wline = NULL;
2064 err = expand_tab(&exstr, line);
2069 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2074 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2076 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2077 wline[wlen - 1] = L'\0';
2080 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2081 wline[wlen - 1] = L'\0';
2085 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2091 *scrollxp = scrollx;
2099 static const struct got_error*
2100 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2101 struct got_object_id *id, struct got_repository *repo)
2103 static const struct got_error *err = NULL;
2104 struct got_reflist_entry *re;
2110 TAILQ_FOREACH(re, refs, entry) {
2111 struct got_tag_object *tag = NULL;
2112 struct got_object_id *ref_id;
2115 name = got_ref_get_name(re->ref);
2116 if (strcmp(name, GOT_REF_HEAD) == 0)
2118 if (strncmp(name, "refs/", 5) == 0)
2120 if (strncmp(name, "got/", 4) == 0 &&
2121 strncmp(name, "got/backup/", 11) != 0)
2123 if (strncmp(name, "heads/", 6) == 0)
2125 if (strncmp(name, "remotes/", 8) == 0) {
2127 s = strstr(name, "/" GOT_REF_HEAD);
2128 if (s != NULL && s[strlen(s)] == '\0')
2131 err = got_ref_resolve(&ref_id, repo, re->ref);
2134 if (strncmp(name, "tags/", 5) == 0) {
2135 err = got_object_open_as_tag(&tag, repo, ref_id);
2137 if (err->code != GOT_ERR_OBJ_TYPE) {
2141 /* Ref points at something other than a tag. */
2146 cmp = got_object_id_cmp(tag ?
2147 got_object_tag_get_object_id(tag) : ref_id, id);
2150 got_object_tag_close(tag);
2154 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2155 s ? ", " : "", name) == -1) {
2156 err = got_error_from_errno("asprintf");
2167 static const struct got_error *
2168 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2173 smallerthan = strchr(author, '<');
2174 if (smallerthan && smallerthan[1] != '\0')
2175 author = smallerthan + 1;
2176 author[strcspn(author, "@>")] = '\0';
2177 return format_line(wauthor, author_width, NULL, author, 0, limit,
2181 static const struct got_error *
2182 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2183 struct got_object_id *id, const size_t date_display_cols,
2184 int author_display_cols)
2186 struct tog_log_view_state *s = &view->state.log;
2187 const struct got_error *err = NULL;
2188 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2189 char *logmsg0 = NULL, *logmsg = NULL;
2190 char *author = NULL;
2191 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2192 int author_width, logmsg_width;
2193 char *newline, *line = NULL;
2194 int col, limit, scrollx;
2195 const int avail = view->ncols;
2197 time_t committer_time;
2198 struct tog_color *tc;
2200 committer_time = got_object_commit_get_committer_time(commit);
2201 if (gmtime_r(&committer_time, &tm) == NULL)
2202 return got_error_from_errno("gmtime_r");
2203 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2204 return got_error(GOT_ERR_NO_SPACE);
2206 if (avail <= date_display_cols)
2207 limit = MIN(sizeof(datebuf) - 1, avail);
2209 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2210 tc = get_color(&s->colors, TOG_COLOR_DATE);
2212 wattr_on(view->window,
2213 COLOR_PAIR(tc->colorpair), NULL);
2214 waddnstr(view->window, datebuf, limit);
2216 wattr_off(view->window,
2217 COLOR_PAIR(tc->colorpair), NULL);
2224 err = got_object_id_str(&id_str, id);
2227 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2229 wattr_on(view->window,
2230 COLOR_PAIR(tc->colorpair), NULL);
2231 wprintw(view->window, "%.8s ", id_str);
2233 wattr_off(view->window,
2234 COLOR_PAIR(tc->colorpair), NULL);
2241 if (s->use_committer)
2242 author = strdup(got_object_commit_get_committer(commit));
2244 author = strdup(got_object_commit_get_author(commit));
2245 if (author == NULL) {
2246 err = got_error_from_errno("strdup");
2249 err = format_author(&wauthor, &author_width, author, avail - col, col);
2252 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2254 wattr_on(view->window,
2255 COLOR_PAIR(tc->colorpair), NULL);
2256 waddwstr(view->window, wauthor);
2257 col += author_width;
2258 while (col < avail && author_width < author_display_cols + 2) {
2259 waddch(view->window, ' ');
2264 wattr_off(view->window,
2265 COLOR_PAIR(tc->colorpair), NULL);
2269 err = got_object_commit_get_logmsg(&logmsg0, commit);
2273 while (*logmsg == '\n')
2275 newline = strchr(logmsg, '\n');
2278 limit = avail - col;
2279 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2280 limit--; /* for the border */
2281 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2285 waddwstr(view->window, &wlogmsg[scrollx]);
2286 col += MAX(logmsg_width, 0);
2287 while (col < avail) {
2288 waddch(view->window, ' ');
2300 static struct commit_queue_entry *
2301 alloc_commit_queue_entry(struct got_commit_object *commit,
2302 struct got_object_id *id)
2304 struct commit_queue_entry *entry;
2305 struct got_object_id *dup;
2307 entry = calloc(1, sizeof(*entry));
2311 dup = got_object_id_dup(id);
2318 entry->commit = commit;
2323 pop_commit(struct commit_queue *commits)
2325 struct commit_queue_entry *entry;
2327 entry = TAILQ_FIRST(&commits->head);
2328 TAILQ_REMOVE(&commits->head, entry, entry);
2329 got_object_commit_close(entry->commit);
2330 commits->ncommits--;
2336 free_commits(struct commit_queue *commits)
2338 while (!TAILQ_EMPTY(&commits->head))
2339 pop_commit(commits);
2342 static const struct got_error *
2343 match_commit(int *have_match, struct got_object_id *id,
2344 struct got_commit_object *commit, regex_t *regex)
2346 const struct got_error *err = NULL;
2347 regmatch_t regmatch;
2348 char *id_str = NULL, *logmsg = NULL;
2352 err = got_object_id_str(&id_str, id);
2356 err = got_object_commit_get_logmsg(&logmsg, commit);
2360 if (regexec(regex, got_object_commit_get_author(commit), 1,
2361 ®match, 0) == 0 ||
2362 regexec(regex, got_object_commit_get_committer(commit), 1,
2363 ®match, 0) == 0 ||
2364 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2365 regexec(regex, logmsg, 1, ®match, 0) == 0)
2373 static const struct got_error *
2374 queue_commits(struct tog_log_thread_args *a)
2376 const struct got_error *err = NULL;
2379 * We keep all commits open throughout the lifetime of the log
2380 * view in order to avoid having to re-fetch commits from disk
2381 * while updating the display.
2384 struct got_object_id id;
2385 struct got_commit_object *commit;
2386 struct commit_queue_entry *entry;
2387 int limit_match = 0;
2390 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2395 err = got_object_open_as_commit(&commit, a->repo, &id);
2398 entry = alloc_commit_queue_entry(commit, &id);
2399 if (entry == NULL) {
2400 err = got_error_from_errno("alloc_commit_queue_entry");
2404 errcode = pthread_mutex_lock(&tog_mutex);
2406 err = got_error_set_errno(errcode,
2407 "pthread_mutex_lock");
2411 entry->idx = a->real_commits->ncommits;
2412 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2413 a->real_commits->ncommits++;
2416 err = match_commit(&limit_match, &id, commit,
2422 struct commit_queue_entry *matched;
2424 matched = alloc_commit_queue_entry(
2425 entry->commit, entry->id);
2426 if (matched == NULL) {
2427 err = got_error_from_errno(
2428 "alloc_commit_queue_entry");
2431 matched->commit = entry->commit;
2432 got_object_commit_retain(entry->commit);
2434 matched->idx = a->limit_commits->ncommits;
2435 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2437 a->limit_commits->ncommits++;
2441 * This is how we signal log_thread() that we
2442 * have found a match, and that it should be
2443 * counted as a new entry for the view.
2445 a->limit_match = limit_match;
2448 if (*a->searching == TOG_SEARCH_FORWARD &&
2449 !*a->search_next_done) {
2451 err = match_commit(&have_match, &id, commit, a->regex);
2456 if (limit_match && have_match)
2457 *a->search_next_done =
2458 TOG_SEARCH_HAVE_MORE;
2459 } else if (have_match)
2460 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2463 errcode = pthread_mutex_unlock(&tog_mutex);
2464 if (errcode && err == NULL)
2465 err = got_error_set_errno(errcode,
2466 "pthread_mutex_unlock");
2469 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2475 select_commit(struct tog_log_view_state *s)
2477 struct commit_queue_entry *entry;
2480 entry = s->first_displayed_entry;
2482 if (ncommits == s->selected) {
2483 s->selected_entry = entry;
2486 entry = TAILQ_NEXT(entry, entry);
2491 static const struct got_error *
2492 draw_commits(struct tog_view *view)
2494 const struct got_error *err = NULL;
2495 struct tog_log_view_state *s = &view->state.log;
2496 struct commit_queue_entry *entry = s->selected_entry;
2497 int limit = view->nlines;
2499 int ncommits, author_cols = 4;
2500 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2501 char *refs_str = NULL;
2503 struct tog_color *tc;
2504 static const size_t date_display_cols = 12;
2506 if (view_is_hsplit_top(view))
2507 --limit; /* account for border */
2509 if (s->selected_entry &&
2510 !(view->searching && view->search_next_done == 0)) {
2511 struct got_reflist_head *refs;
2512 err = got_object_id_str(&id_str, s->selected_entry->id);
2515 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2516 s->selected_entry->id);
2518 err = build_refs_str(&refs_str, refs,
2519 s->selected_entry->id, s->repo);
2525 if (s->thread_args.commits_needed == 0)
2526 halfdelay(10); /* disable fast refresh */
2528 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2529 if (asprintf(&ncommits_str, " [%d/%d] %s",
2530 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2531 (view->searching && !view->search_next_done) ?
2532 "searching..." : "loading...") == -1) {
2533 err = got_error_from_errno("asprintf");
2537 const char *search_str = NULL;
2538 const char *limit_str = NULL;
2540 if (view->searching) {
2541 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2542 search_str = "no more matches";
2543 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2544 search_str = "no matches found";
2545 else if (!view->search_next_done)
2546 search_str = "searching...";
2549 if (s->limit_view && s->commits->ncommits == 0)
2550 limit_str = "no matches found";
2552 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2553 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2554 search_str ? search_str : (refs_str ? refs_str : ""),
2555 limit_str ? limit_str : "") == -1) {
2556 err = got_error_from_errno("asprintf");
2561 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2562 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2563 "........................................",
2564 s->in_repo_path, ncommits_str) == -1) {
2565 err = got_error_from_errno("asprintf");
2569 } else if (asprintf(&header, "commit %s%s",
2570 id_str ? id_str : "........................................",
2571 ncommits_str) == -1) {
2572 err = got_error_from_errno("asprintf");
2576 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2580 werase(view->window);
2582 if (view_needs_focus_indication(view))
2583 wstandout(view->window);
2584 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2586 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2587 waddwstr(view->window, wline);
2588 while (width < view->ncols) {
2589 waddch(view->window, ' ');
2593 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2594 if (view_needs_focus_indication(view))
2595 wstandend(view->window);
2600 /* Grow author column size if necessary, and set view->maxx. */
2601 entry = s->first_displayed_entry;
2605 struct got_commit_object *c = entry->commit;
2606 char *author, *eol, *msg, *msg0;
2607 wchar_t *wauthor, *wmsg;
2609 if (ncommits >= limit - 1)
2611 if (s->use_committer)
2612 author = strdup(got_object_commit_get_committer(c));
2614 author = strdup(got_object_commit_get_author(c));
2615 if (author == NULL) {
2616 err = got_error_from_errno("strdup");
2619 err = format_author(&wauthor, &width, author, COLS,
2621 if (author_cols < width)
2622 author_cols = width;
2627 err = got_object_commit_get_logmsg(&msg0, c);
2631 while (*msg == '\n')
2633 if ((eol = strchr(msg, '\n')))
2635 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2636 date_display_cols + author_cols, 0);
2639 view->maxx = MAX(view->maxx, width);
2643 entry = TAILQ_NEXT(entry, entry);
2646 entry = s->first_displayed_entry;
2647 s->last_displayed_entry = s->first_displayed_entry;
2650 if (ncommits >= limit - 1)
2652 if (ncommits == s->selected)
2653 wstandout(view->window);
2654 err = draw_commit(view, entry->commit, entry->id,
2655 date_display_cols, author_cols);
2656 if (ncommits == s->selected)
2657 wstandend(view->window);
2661 s->last_displayed_entry = entry;
2662 entry = TAILQ_NEXT(entry, entry);
2675 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2677 struct commit_queue_entry *entry;
2680 entry = TAILQ_FIRST(&s->commits->head);
2681 if (s->first_displayed_entry == entry)
2684 entry = s->first_displayed_entry;
2685 while (entry && nscrolled < maxscroll) {
2686 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2688 s->first_displayed_entry = entry;
2694 static const struct got_error *
2695 trigger_log_thread(struct tog_view *view, int wait)
2697 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2700 halfdelay(1); /* fast refresh while loading commits */
2702 while (!ta->log_complete && !tog_thread_error &&
2703 (ta->commits_needed > 0 || ta->load_all)) {
2704 /* Wake the log thread. */
2705 errcode = pthread_cond_signal(&ta->need_commits);
2707 return got_error_set_errno(errcode,
2708 "pthread_cond_signal");
2711 * The mutex will be released while the view loop waits
2712 * in wgetch(), at which time the log thread will run.
2717 /* Display progress update in log view. */
2718 show_log_view(view);
2722 /* Wait right here while next commit is being loaded. */
2723 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2725 return got_error_set_errno(errcode,
2726 "pthread_cond_wait");
2728 /* Display progress update in log view. */
2729 show_log_view(view);
2737 static const struct got_error *
2738 request_log_commits(struct tog_view *view)
2740 struct tog_log_view_state *state = &view->state.log;
2741 const struct got_error *err = NULL;
2743 if (state->thread_args.log_complete)
2746 state->thread_args.commits_needed += view->nscrolled;
2747 err = trigger_log_thread(view, 1);
2748 view->nscrolled = 0;
2753 static const struct got_error *
2754 log_scroll_down(struct tog_view *view, int maxscroll)
2756 struct tog_log_view_state *s = &view->state.log;
2757 const struct got_error *err = NULL;
2758 struct commit_queue_entry *pentry;
2759 int nscrolled = 0, ncommits_needed;
2761 if (s->last_displayed_entry == NULL)
2764 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2765 if (s->commits->ncommits < ncommits_needed &&
2766 !s->thread_args.log_complete) {
2768 * Ask the log thread for required amount of commits.
2770 s->thread_args.commits_needed +=
2771 ncommits_needed - s->commits->ncommits;
2772 err = trigger_log_thread(view, 1);
2778 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2779 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2782 s->last_displayed_entry = pentry ?
2783 pentry : s->last_displayed_entry;
2785 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2788 s->first_displayed_entry = pentry;
2789 } while (++nscrolled < maxscroll);
2791 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2792 view->nscrolled += nscrolled;
2794 view->nscrolled = 0;
2799 static const struct got_error *
2800 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2801 struct got_commit_object *commit, struct got_object_id *commit_id,
2802 struct tog_view *log_view, struct got_repository *repo)
2804 const struct got_error *err;
2805 struct got_object_qid *parent_id;
2806 struct tog_view *diff_view;
2808 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2809 if (diff_view == NULL)
2810 return got_error_from_errno("view_open");
2812 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2813 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2814 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2816 *new_view = diff_view;
2820 static const struct got_error *
2821 tree_view_visit_subtree(struct tog_tree_view_state *s,
2822 struct got_tree_object *subtree)
2824 struct tog_parent_tree *parent;
2826 parent = calloc(1, sizeof(*parent));
2828 return got_error_from_errno("calloc");
2830 parent->tree = s->tree;
2831 parent->first_displayed_entry = s->first_displayed_entry;
2832 parent->selected_entry = s->selected_entry;
2833 parent->selected = s->selected;
2834 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2837 s->first_displayed_entry = NULL;
2841 static const struct got_error *
2842 tree_view_walk_path(struct tog_tree_view_state *s,
2843 struct got_commit_object *commit, const char *path)
2845 const struct got_error *err = NULL;
2846 struct got_tree_object *tree = NULL;
2848 char *slash, *subpath = NULL;
2850 /* Walk the path and open corresponding tree objects. */
2853 struct got_tree_entry *te;
2854 struct got_object_id *tree_id;
2860 /* Ensure the correct subtree entry is selected. */
2861 slash = strchr(p, '/');
2863 te_name = strdup(p);
2865 te_name = strndup(p, slash - p);
2866 if (te_name == NULL) {
2867 err = got_error_from_errno("strndup");
2870 te = got_object_tree_find_entry(s->tree, te_name);
2872 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2877 s->first_displayed_entry = s->selected_entry = te;
2879 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2880 break; /* jump to this file's entry */
2882 slash = strchr(p, '/');
2884 subpath = strndup(path, slash - path);
2886 subpath = strdup(path);
2887 if (subpath == NULL) {
2888 err = got_error_from_errno("strdup");
2892 err = got_object_id_by_path(&tree_id, s->repo, commit,
2897 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2902 err = tree_view_visit_subtree(s, tree);
2904 got_object_tree_close(tree);
2918 static const struct got_error *
2919 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2920 struct commit_queue_entry *entry, const char *path,
2921 const char *head_ref_name, struct got_repository *repo)
2923 const struct got_error *err = NULL;
2924 struct tog_tree_view_state *s;
2925 struct tog_view *tree_view;
2927 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2928 if (tree_view == NULL)
2929 return got_error_from_errno("view_open");
2931 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2934 s = &tree_view->state.tree;
2936 *new_view = tree_view;
2938 if (got_path_is_root_dir(path))
2941 return tree_view_walk_path(s, entry->commit, path);
2944 static const struct got_error *
2945 block_signals_used_by_main_thread(void)
2950 if (sigemptyset(&sigset) == -1)
2951 return got_error_from_errno("sigemptyset");
2953 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2954 if (sigaddset(&sigset, SIGWINCH) == -1)
2955 return got_error_from_errno("sigaddset");
2956 if (sigaddset(&sigset, SIGCONT) == -1)
2957 return got_error_from_errno("sigaddset");
2958 if (sigaddset(&sigset, SIGINT) == -1)
2959 return got_error_from_errno("sigaddset");
2960 if (sigaddset(&sigset, SIGTERM) == -1)
2961 return got_error_from_errno("sigaddset");
2963 /* ncurses handles SIGTSTP */
2964 if (sigaddset(&sigset, SIGTSTP) == -1)
2965 return got_error_from_errno("sigaddset");
2967 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2969 return got_error_set_errno(errcode, "pthread_sigmask");
2975 log_thread(void *arg)
2977 const struct got_error *err = NULL;
2979 struct tog_log_thread_args *a = arg;
2983 * Sync startup with main thread such that we begin our
2984 * work once view_input() has released the mutex.
2986 errcode = pthread_mutex_lock(&tog_mutex);
2988 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2992 err = block_signals_used_by_main_thread();
2994 pthread_mutex_unlock(&tog_mutex);
2998 while (!done && !err && !tog_fatal_signal_received()) {
2999 errcode = pthread_mutex_unlock(&tog_mutex);
3001 err = got_error_set_errno(errcode,
3002 "pthread_mutex_unlock");
3005 err = queue_commits(a);
3007 if (err->code != GOT_ERR_ITER_COMPLETED)
3011 } else if (a->commits_needed > 0 && !a->load_all) {
3014 a->commits_needed--;
3016 a->commits_needed--;
3019 errcode = pthread_mutex_lock(&tog_mutex);
3021 err = got_error_set_errno(errcode,
3022 "pthread_mutex_lock");
3024 } else if (*a->quit)
3026 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3027 *a->first_displayed_entry =
3028 TAILQ_FIRST(&a->limit_commits->head);
3029 *a->selected_entry = *a->first_displayed_entry;
3030 } else if (*a->first_displayed_entry == NULL) {
3031 *a->first_displayed_entry =
3032 TAILQ_FIRST(&a->real_commits->head);
3033 *a->selected_entry = *a->first_displayed_entry;
3036 errcode = pthread_cond_signal(&a->commit_loaded);
3038 err = got_error_set_errno(errcode,
3039 "pthread_cond_signal");
3040 pthread_mutex_unlock(&tog_mutex);
3045 a->commits_needed = 0;
3047 if (a->commits_needed == 0 && !a->load_all) {
3048 errcode = pthread_cond_wait(&a->need_commits,
3051 err = got_error_set_errno(errcode,
3052 "pthread_cond_wait");
3053 pthread_mutex_unlock(&tog_mutex);
3061 a->log_complete = 1;
3062 errcode = pthread_mutex_unlock(&tog_mutex);
3064 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3067 tog_thread_error = 1;
3068 pthread_cond_signal(&a->commit_loaded);
3073 static const struct got_error *
3074 stop_log_thread(struct tog_log_view_state *s)
3076 const struct got_error *err = NULL, *thread_err = NULL;
3081 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3083 return got_error_set_errno(errcode,
3084 "pthread_cond_signal");
3085 errcode = pthread_mutex_unlock(&tog_mutex);
3087 return got_error_set_errno(errcode,
3088 "pthread_mutex_unlock");
3089 errcode = pthread_join(s->thread, (void **)&thread_err);
3091 return got_error_set_errno(errcode, "pthread_join");
3092 errcode = pthread_mutex_lock(&tog_mutex);
3094 return got_error_set_errno(errcode,
3095 "pthread_mutex_lock");
3099 if (s->thread_args.repo) {
3100 err = got_repo_close(s->thread_args.repo);
3101 s->thread_args.repo = NULL;
3104 if (s->thread_args.pack_fds) {
3105 const struct got_error *pack_err =
3106 got_repo_pack_fds_close(s->thread_args.pack_fds);
3109 s->thread_args.pack_fds = NULL;
3112 if (s->thread_args.graph) {
3113 got_commit_graph_close(s->thread_args.graph);
3114 s->thread_args.graph = NULL;
3117 return err ? err : thread_err;
3120 static const struct got_error *
3121 close_log_view(struct tog_view *view)
3123 const struct got_error *err = NULL;
3124 struct tog_log_view_state *s = &view->state.log;
3127 err = stop_log_thread(s);
3129 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3130 if (errcode && err == NULL)
3131 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3133 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3134 if (errcode && err == NULL)
3135 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3137 free_commits(&s->limit_commits);
3138 free_commits(&s->real_commits);
3139 free(s->in_repo_path);
3140 s->in_repo_path = NULL;
3143 free(s->head_ref_name);
3144 s->head_ref_name = NULL;
3149 * We use two queues to implement the limit feature: first consists of
3150 * commits matching the current limit_regex; second is the real queue
3151 * of all known commits (real_commits). When the user starts limiting,
3152 * we swap queues such that all movement and displaying functionality
3153 * works with very slight change.
3155 static const struct got_error *
3156 limit_log_view(struct tog_view *view)
3158 struct tog_log_view_state *s = &view->state.log;
3159 struct commit_queue_entry *entry;
3160 struct tog_view *v = view;
3161 const struct got_error *err = NULL;
3165 if (view_is_hsplit_top(view))
3167 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3170 /* Get the pattern */
3171 wmove(v->window, v->nlines - 1, 0);
3172 wclrtoeol(v->window);
3173 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3174 nodelay(v->window, FALSE);
3177 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3180 nodelay(v->window, TRUE);
3184 if (*pattern == '\0') {
3186 * Safety measure for the situation where the user
3187 * resets limit without previously limiting anything.
3193 * User could have pressed Ctrl+L, which refreshed the
3194 * commit queues, it means we can't save previously
3195 * (before limit took place) displayed entries,
3196 * because they would point to already free'ed memory,
3197 * so we are forced to always select first entry of
3200 s->commits = &s->real_commits;
3201 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3202 s->selected_entry = s->first_displayed_entry;
3209 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3214 /* Clear the screen while loading limit view */
3215 s->first_displayed_entry = NULL;
3216 s->last_displayed_entry = NULL;
3217 s->selected_entry = NULL;
3218 s->commits = &s->limit_commits;
3220 /* Prepare limit queue for new search */
3221 free_commits(&s->limit_commits);
3222 s->limit_commits.ncommits = 0;
3224 /* First process commits, which are in queue already */
3225 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3228 err = match_commit(&have_match, entry->id,
3229 entry->commit, &s->limit_regex);
3234 struct commit_queue_entry *matched;
3236 matched = alloc_commit_queue_entry(entry->commit,
3238 if (matched == NULL) {
3239 err = got_error_from_errno(
3240 "alloc_commit_queue_entry");
3243 matched->commit = entry->commit;
3244 got_object_commit_retain(entry->commit);
3246 matched->idx = s->limit_commits.ncommits;
3247 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3249 s->limit_commits.ncommits++;
3253 /* Second process all the commits, until we fill the screen */
3254 if (s->limit_commits.ncommits < view->nlines - 1 &&
3255 !s->thread_args.log_complete) {
3256 s->thread_args.commits_needed +=
3257 view->nlines - s->limit_commits.ncommits - 1;
3258 err = trigger_log_thread(view, 1);
3263 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3264 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3270 static const struct got_error *
3271 search_start_log_view(struct tog_view *view)
3273 struct tog_log_view_state *s = &view->state.log;
3275 s->matched_entry = NULL;
3276 s->search_entry = NULL;
3280 static const struct got_error *
3281 search_next_log_view(struct tog_view *view)
3283 const struct got_error *err = NULL;
3284 struct tog_log_view_state *s = &view->state.log;
3285 struct commit_queue_entry *entry;
3287 /* Display progress update in log view. */
3288 show_log_view(view);
3292 if (s->search_entry) {
3294 errcode = pthread_mutex_unlock(&tog_mutex);
3296 return got_error_set_errno(errcode,
3297 "pthread_mutex_unlock");
3298 ch = wgetch(view->window);
3299 errcode = pthread_mutex_lock(&tog_mutex);
3301 return got_error_set_errno(errcode,
3302 "pthread_mutex_lock");
3303 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3304 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3307 if (view->searching == TOG_SEARCH_FORWARD)
3308 entry = TAILQ_NEXT(s->search_entry, entry);
3310 entry = TAILQ_PREV(s->search_entry,
3311 commit_queue_head, entry);
3312 } else if (s->matched_entry) {
3314 * If the user has moved the cursor after we hit a match,
3315 * the position from where we should continue searching
3316 * might have changed.
3318 if (view->searching == TOG_SEARCH_FORWARD)
3319 entry = TAILQ_NEXT(s->selected_entry, entry);
3321 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3324 entry = s->selected_entry;
3330 if (entry == NULL) {
3331 if (s->thread_args.log_complete ||
3332 view->searching == TOG_SEARCH_BACKWARD) {
3333 view->search_next_done =
3334 (s->matched_entry == NULL ?
3335 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3336 s->search_entry = NULL;
3340 * Poke the log thread for more commits and return,
3341 * allowing the main loop to make progress. Search
3342 * will resume at s->search_entry once we come back.
3344 s->thread_args.commits_needed++;
3345 return trigger_log_thread(view, 0);
3348 err = match_commit(&have_match, entry->id, entry->commit,
3353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3354 s->matched_entry = entry;
3358 s->search_entry = entry;
3359 if (view->searching == TOG_SEARCH_FORWARD)
3360 entry = TAILQ_NEXT(entry, entry);
3362 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3365 if (s->matched_entry) {
3366 int cur = s->selected_entry->idx;
3367 while (cur < s->matched_entry->idx) {
3368 err = input_log_view(NULL, view, KEY_DOWN);
3373 while (cur > s->matched_entry->idx) {
3374 err = input_log_view(NULL, view, KEY_UP);
3381 s->search_entry = NULL;
3386 static const struct got_error *
3387 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3388 struct got_repository *repo, const char *head_ref_name,
3389 const char *in_repo_path, int log_branches)
3391 const struct got_error *err = NULL;
3392 struct tog_log_view_state *s = &view->state.log;
3393 struct got_repository *thread_repo = NULL;
3394 struct got_commit_graph *thread_graph = NULL;
3397 if (in_repo_path != s->in_repo_path) {
3398 free(s->in_repo_path);
3399 s->in_repo_path = strdup(in_repo_path);
3400 if (s->in_repo_path == NULL)
3401 return got_error_from_errno("strdup");
3404 /* The commit queue only contains commits being displayed. */
3405 TAILQ_INIT(&s->real_commits.head);
3406 s->real_commits.ncommits = 0;
3407 s->commits = &s->real_commits;
3409 TAILQ_INIT(&s->limit_commits.head);
3411 s->limit_commits.ncommits = 0;
3414 if (head_ref_name) {
3415 s->head_ref_name = strdup(head_ref_name);
3416 if (s->head_ref_name == NULL) {
3417 err = got_error_from_errno("strdup");
3421 s->start_id = got_object_id_dup(start_id);
3422 if (s->start_id == NULL) {
3423 err = got_error_from_errno("got_object_id_dup");
3426 s->log_branches = log_branches;
3427 s->use_committer = 1;
3429 STAILQ_INIT(&s->colors);
3430 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3431 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3432 get_color_value("TOG_COLOR_COMMIT"));
3435 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3436 get_color_value("TOG_COLOR_AUTHOR"));
3438 free_colors(&s->colors);
3441 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3442 get_color_value("TOG_COLOR_DATE"));
3444 free_colors(&s->colors);
3449 view->show = show_log_view;
3450 view->input = input_log_view;
3451 view->resize = resize_log_view;
3452 view->close = close_log_view;
3453 view->search_start = search_start_log_view;
3454 view->search_next = search_next_log_view;
3456 if (s->thread_args.pack_fds == NULL) {
3457 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3461 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3462 s->thread_args.pack_fds);
3465 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3469 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3470 s->repo, NULL, NULL);
3474 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3476 err = got_error_set_errno(errcode, "pthread_cond_init");
3479 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3481 err = got_error_set_errno(errcode, "pthread_cond_init");
3485 s->thread_args.commits_needed = view->nlines;
3486 s->thread_args.graph = thread_graph;
3487 s->thread_args.real_commits = &s->real_commits;
3488 s->thread_args.limit_commits = &s->limit_commits;
3489 s->thread_args.in_repo_path = s->in_repo_path;
3490 s->thread_args.start_id = s->start_id;
3491 s->thread_args.repo = thread_repo;
3492 s->thread_args.log_complete = 0;
3493 s->thread_args.quit = &s->quit;
3494 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3495 s->thread_args.selected_entry = &s->selected_entry;
3496 s->thread_args.searching = &view->searching;
3497 s->thread_args.search_next_done = &view->search_next_done;
3498 s->thread_args.regex = &view->regex;
3499 s->thread_args.limiting = &s->limit_view;
3500 s->thread_args.limit_regex = &s->limit_regex;
3501 s->thread_args.limit_commits = &s->limit_commits;
3504 close_log_view(view);
3508 static const struct got_error *
3509 show_log_view(struct tog_view *view)
3511 const struct got_error *err;
3512 struct tog_log_view_state *s = &view->state.log;
3514 if (s->thread == NULL) {
3515 int errcode = pthread_create(&s->thread, NULL, log_thread,
3518 return got_error_set_errno(errcode, "pthread_create");
3519 if (s->thread_args.commits_needed > 0) {
3520 err = trigger_log_thread(view, 1);
3526 return draw_commits(view);
3530 log_move_cursor_up(struct tog_view *view, int page, int home)
3532 struct tog_log_view_state *s = &view->state.log;
3534 if (s->first_displayed_entry == NULL)
3536 if (s->selected_entry->idx == 0)
3539 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3541 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3543 if (!page && !home && s->selected > 0)
3546 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3552 static const struct got_error *
3553 log_move_cursor_down(struct tog_view *view, int page)
3555 struct tog_log_view_state *s = &view->state.log;
3556 const struct got_error *err = NULL;
3557 int eos = view->nlines - 2;
3559 if (s->first_displayed_entry == NULL)
3562 if (s->thread_args.log_complete &&
3563 s->selected_entry->idx >= s->commits->ncommits - 1)
3566 if (view_is_hsplit_top(view))
3567 --eos; /* border consumes the last line */
3570 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3573 err = log_scroll_down(view, 1);
3574 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3575 struct commit_queue_entry *entry;
3579 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3580 s->last_displayed_entry = entry;
3581 for (n = 0; n <= eos; n++) {
3584 s->first_displayed_entry = entry;
3585 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3588 s->selected = n - 1;
3590 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3591 s->thread_args.log_complete)
3592 s->selected += MIN(page,
3593 s->commits->ncommits - s->selected_entry->idx - 1);
3595 err = log_scroll_down(view, page);
3601 * We might necessarily overshoot in horizontal
3602 * splits; if so, select the last displayed commit.
3604 if (s->first_displayed_entry && s->last_displayed_entry) {
3605 s->selected = MIN(s->selected,
3606 s->last_displayed_entry->idx -
3607 s->first_displayed_entry->idx);
3612 if (s->thread_args.log_complete &&
3613 s->selected_entry->idx == s->commits->ncommits - 1)
3620 view_get_split(struct tog_view *view, int *y, int *x)
3625 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3626 if (view->child && view->child->resized_y)
3627 *y = view->child->resized_y;
3628 else if (view->resized_y)
3629 *y = view->resized_y;
3631 *y = view_split_begin_y(view->lines);
3632 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3633 if (view->child && view->child->resized_x)
3634 *x = view->child->resized_x;
3635 else if (view->resized_x)
3636 *x = view->resized_x;
3638 *x = view_split_begin_x(view->begin_x);
3642 /* Split view horizontally at y and offset view->state->selected line. */
3643 static const struct got_error *
3644 view_init_hsplit(struct tog_view *view, int y)
3646 const struct got_error *err = NULL;
3650 err = view_resize(view);
3654 err = offset_selection_down(view);
3659 static const struct got_error *
3660 log_goto_line(struct tog_view *view, int nlines)
3662 const struct got_error *err = NULL;
3663 struct tog_log_view_state *s = &view->state.log;
3664 int g, idx = s->selected_entry->idx;
3666 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3672 if (g >= s->first_displayed_entry->idx + 1 &&
3673 g <= s->last_displayed_entry->idx + 1 &&
3674 g - s->first_displayed_entry->idx - 1 < nlines) {
3675 s->selected = g - s->first_displayed_entry->idx - 1;
3681 err = log_move_cursor_down(view, g - idx - 1);
3682 if (!err && g > s->selected_entry->idx + 1)
3683 err = log_move_cursor_down(view,
3684 g - s->first_displayed_entry->idx - 1);
3687 } else if (idx + 1 > g)
3688 log_move_cursor_up(view, idx - g + 1, 0);
3690 if (g < nlines && s->first_displayed_entry->idx == 0)
3691 s->selected = g - 1;
3699 horizontal_scroll_input(struct tog_view *view, int ch)
3705 view->x -= MIN(view->x, 2);
3711 if (view->x + view->ncols / 2 < view->maxx)
3720 view->x = MAX(view->maxx - view->ncols / 2, 0);
3728 static const struct got_error *
3729 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3731 const struct got_error *err = NULL;
3732 struct tog_log_view_state *s = &view->state.log;
3735 if (s->thread_args.load_all) {
3736 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3737 s->thread_args.load_all = 0;
3738 else if (s->thread_args.log_complete) {
3739 err = log_move_cursor_down(view, s->commits->ncommits);
3740 s->thread_args.load_all = 0;
3746 eos = nscroll = view->nlines - 1;
3747 if (view_is_hsplit_top(view))
3751 return log_goto_line(view, eos);
3755 err = limit_log_view(view);
3766 horizontal_scroll_input(view, ch);
3773 log_move_cursor_up(view, 0, 0);
3778 log_move_cursor_up(view, 0, 1);
3788 log_move_cursor_up(view, nscroll, 0);
3795 err = log_move_cursor_down(view, 0);
3798 s->use_committer = !s->use_committer;
3799 view->action = s->use_committer ?
3800 "show committer" : "show commit author";
3805 /* We don't know yet how many commits, so we're forced to
3806 * traverse them all. */
3808 s->thread_args.load_all = 1;
3809 if (!s->thread_args.log_complete)
3810 return trigger_log_thread(view, 0);
3811 err = log_move_cursor_down(view, s->commits->ncommits);
3812 s->thread_args.load_all = 0;
3823 err = log_move_cursor_down(view, nscroll);
3826 if (s->selected > view->nlines - 2)
3827 s->selected = view->nlines - 2;
3828 if (s->selected > s->commits->ncommits - 1)
3829 s->selected = s->commits->ncommits - 1;
3831 if (s->commits->ncommits < view->nlines - 1 &&
3832 !s->thread_args.log_complete) {
3833 s->thread_args.commits_needed += (view->nlines - 1) -
3834 s->commits->ncommits;
3835 err = trigger_log_thread(view, 1);
3841 if (s->selected_entry == NULL)
3843 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3847 if (s->selected_entry == NULL)
3849 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3855 if (ch == KEY_BACKSPACE &&
3856 got_path_is_root_dir(s->in_repo_path))
3858 err = stop_log_thread(s);
3861 if (ch == KEY_BACKSPACE) {
3863 err = got_path_dirname(&parent_path, s->in_repo_path);
3866 free(s->in_repo_path);
3867 s->in_repo_path = parent_path;
3868 s->thread_args.in_repo_path = s->in_repo_path;
3869 } else if (ch == CTRL('l')) {
3870 struct got_object_id *start_id;
3871 err = got_repo_match_object_id(&start_id, NULL,
3872 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3873 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3875 if (s->head_ref_name == NULL ||
3876 err->code != GOT_ERR_NOT_REF)
3878 /* Try to cope with deleted references. */
3879 free(s->head_ref_name);
3880 s->head_ref_name = NULL;
3881 err = got_repo_match_object_id(&start_id,
3882 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3883 &tog_refs, s->repo);
3888 s->start_id = start_id;
3889 s->thread_args.start_id = s->start_id;
3891 s->log_branches = !s->log_branches;
3893 if (s->thread_args.pack_fds == NULL) {
3894 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3898 err = got_repo_open(&s->thread_args.repo,
3899 got_repo_get_path(s->repo), NULL,
3900 s->thread_args.pack_fds);
3904 err = tog_load_refs(s->repo, 0);
3907 err = got_commit_graph_open(&s->thread_args.graph,
3908 s->in_repo_path, !s->log_branches);
3911 err = got_commit_graph_iter_start(s->thread_args.graph,
3912 s->start_id, s->repo, NULL, NULL);
3915 free_commits(&s->real_commits);
3916 free_commits(&s->limit_commits);
3917 s->first_displayed_entry = NULL;
3918 s->last_displayed_entry = NULL;
3919 s->selected_entry = NULL;
3921 s->thread_args.log_complete = 0;
3923 s->thread_args.commits_needed = view->lines;
3924 s->matched_entry = NULL;
3925 s->search_entry = NULL;
3930 err = view_request_new(new_view, view, TOG_VIEW_REF);
3940 static const struct got_error *
3941 apply_unveil(const char *repo_path, const char *worktree_path)
3943 const struct got_error *error;
3946 if (unveil("gmon.out", "rwc") != 0)
3947 return got_error_from_errno2("unveil", "gmon.out");
3949 if (repo_path && unveil(repo_path, "r") != 0)
3950 return got_error_from_errno2("unveil", repo_path);
3952 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3953 return got_error_from_errno2("unveil", worktree_path);
3955 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3956 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3958 error = got_privsep_unveil_exec_helpers();
3962 if (unveil(NULL, NULL) != 0)
3963 return got_error_from_errno("unveil");
3972 * Override default signal handlers before starting ncurses.
3973 * This should prevent ncurses from installing its own
3974 * broken cleanup() signal handler.
3976 signal(SIGWINCH, tog_sigwinch);
3977 signal(SIGPIPE, tog_sigpipe);
3978 signal(SIGCONT, tog_sigcont);
3979 signal(SIGINT, tog_sigint);
3980 signal(SIGTERM, tog_sigterm);
3984 halfdelay(1); /* Do fast refresh while initial view is loading. */
3987 intrflush(stdscr, FALSE);
3988 keypad(stdscr, TRUE);
3990 if (getenv("TOG_COLORS") != NULL) {
3992 use_default_colors();
3996 static const struct got_error *
3997 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3998 struct got_repository *repo, struct got_worktree *worktree)
4000 const struct got_error *err = NULL;
4003 *in_repo_path = strdup("/");
4004 if (*in_repo_path == NULL)
4005 return got_error_from_errno("strdup");
4010 const char *prefix = got_worktree_get_path_prefix(worktree);
4013 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4016 if (asprintf(in_repo_path, "%s%s%s", prefix,
4017 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4019 err = got_error_from_errno("asprintf");
4020 *in_repo_path = NULL;
4024 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4029 static const struct got_error *
4030 cmd_log(int argc, char *argv[])
4032 const struct got_error *error;
4033 struct got_repository *repo = NULL;
4034 struct got_worktree *worktree = NULL;
4035 struct got_object_id *start_id = NULL;
4036 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4037 char *start_commit = NULL, *label = NULL;
4038 struct got_reference *ref = NULL;
4039 const char *head_ref_name = NULL;
4040 int ch, log_branches = 0;
4041 struct tog_view *view;
4042 int *pack_fds = NULL;
4044 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4050 start_commit = optarg;
4053 repo_path = realpath(optarg, NULL);
4054 if (repo_path == NULL)
4055 return got_error_from_errno2("realpath",
4070 error = got_repo_pack_fds_open(&pack_fds);
4074 if (repo_path == NULL) {
4075 cwd = getcwd(NULL, 0);
4077 return got_error_from_errno("getcwd");
4078 error = got_worktree_open(&worktree, cwd);
4079 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4083 strdup(got_worktree_get_repo_path(worktree));
4085 repo_path = strdup(cwd);
4086 if (repo_path == NULL) {
4087 error = got_error_from_errno("strdup");
4092 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4096 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4103 error = apply_unveil(got_repo_get_path(repo),
4104 worktree ? got_worktree_get_root_path(worktree) : NULL);
4108 /* already loaded by tog_log_with_path()? */
4109 if (TAILQ_EMPTY(&tog_refs)) {
4110 error = tog_load_refs(repo, 0);
4115 if (start_commit == NULL) {
4116 error = got_repo_match_object_id(&start_id, &label,
4117 worktree ? got_worktree_get_head_ref_name(worktree) :
4118 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4121 head_ref_name = label;
4123 error = got_ref_open(&ref, repo, start_commit, 0);
4125 head_ref_name = got_ref_get_name(ref);
4126 else if (error->code != GOT_ERR_NOT_REF)
4128 error = got_repo_match_object_id(&start_id, NULL,
4129 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4134 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4136 error = got_error_from_errno("view_open");
4139 error = open_log_view(view, start_id, repo, head_ref_name,
4140 in_repo_path, log_branches);
4144 /* Release work tree lock. */
4145 got_worktree_close(worktree);
4148 error = view_loop(view);
4158 const struct got_error *close_err = got_repo_close(repo);
4163 got_worktree_close(worktree);
4165 const struct got_error *pack_err =
4166 got_repo_pack_fds_close(pack_fds);
4178 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4179 "object1 object2\n", getprogname());
4184 match_line(const char *line, regex_t *regex, size_t nmatch,
4185 regmatch_t *regmatch)
4187 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4190 static struct tog_color *
4191 match_color(struct tog_colors *colors, const char *line)
4193 struct tog_color *tc = NULL;
4195 STAILQ_FOREACH(tc, colors, entry) {
4196 if (match_line(line, &tc->regex, 0, NULL))
4203 static const struct got_error *
4204 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4205 WINDOW *window, int skipcol, regmatch_t *regmatch)
4207 const struct got_error *err = NULL;
4209 wchar_t *wline = NULL;
4210 int rme, rms, n, width, scrollx;
4211 int width0 = 0, width1 = 0, width2 = 0;
4212 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4216 rms = regmatch->rm_so;
4217 rme = regmatch->rm_eo;
4219 err = expand_tab(&exstr, line);
4223 /* Split the line into 3 segments, according to match offsets. */
4224 seg0 = strndup(exstr, rms);
4226 err = got_error_from_errno("strndup");
4229 seg1 = strndup(exstr + rms, rme - rms);
4231 err = got_error_from_errno("strndup");
4234 seg2 = strdup(exstr + rme);
4236 err = got_error_from_errno("strndup");
4240 /* draw up to matched token if we haven't scrolled past it */
4241 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4245 n = MAX(width0 - skipcol, 0);
4248 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4249 wlimit, col_tab_align, 1);
4252 waddwstr(window, &wline[scrollx]);
4262 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4266 wlen = wcslen(wline);
4268 width = wcwidth(wline[i]);
4270 /* should not happen, tabs are expanded */
4271 err = got_error(GOT_ERR_RANGE);
4274 if (width0 + w + width > skipcol)
4279 /* draw (visible part of) matched token (if scrolled into it) */
4280 if (width1 - w > 0) {
4281 wattron(window, A_STANDOUT);
4282 waddwstr(window, &wline[i]);
4283 wattroff(window, A_STANDOUT);
4284 wlimit -= (width1 - w);
4285 *wtotal += (width1 - w);
4289 if (wlimit > 0) { /* draw rest of line */
4291 if (skipcol > width0 + width1) {
4292 err = format_line(&wline, &width2, &scrollx, seg2,
4293 skipcol - (width0 + width1), wlimit,
4297 waddwstr(window, &wline[scrollx]);
4299 err = format_line(&wline, &width2, NULL, seg2, 0,
4300 wlimit, col_tab_align, 1);
4303 waddwstr(window, wline);
4317 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4320 int *eof, *first, *selected;
4322 if (view->type == TOG_VIEW_DIFF) {
4323 struct tog_diff_view_state *s = &view->state.diff;
4325 first = &s->first_displayed_line;
4329 } else if (view->type == TOG_VIEW_HELP) {
4330 struct tog_help_view_state *s = &view->state.help;
4332 first = &s->first_displayed_line;
4336 } else if (view->type == TOG_VIEW_BLAME) {
4337 struct tog_blame_view_state *s = &view->state.blame;
4339 first = &s->first_displayed_line;
4340 selected = &s->selected_line;
4346 /* Center gline in the middle of the page like vi(1). */
4347 if (*lineno < view->gline - (view->nlines - 3) / 2)
4349 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4358 *selected = view->gline <= (view->nlines - 3) / 2 ?
4359 view->gline : (view->nlines - 3) / 2 + 1;
4365 static const struct got_error *
4366 draw_file(struct tog_view *view, const char *header)
4368 struct tog_diff_view_state *s = &view->state.diff;
4369 regmatch_t *regmatch = &view->regmatch;
4370 const struct got_error *err;
4373 size_t linesize = 0;
4377 int max_lines = view->nlines;
4378 int nlines = s->nlines;
4381 s->lineno = s->first_displayed_line - 1;
4382 line_offset = s->lines[s->first_displayed_line - 1].offset;
4383 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4384 return got_error_from_errno("fseek");
4386 werase(view->window);
4388 if (view->gline > s->nlines - 1)
4389 view->gline = s->nlines - 1;
4392 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4393 1 : view->gline - (view->nlines - 3) / 2 :
4394 s->lineno + s->selected_line;
4396 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4397 return got_error_from_errno("asprintf");
4398 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4404 if (view_needs_focus_indication(view))
4405 wstandout(view->window);
4406 waddwstr(view->window, wline);
4409 while (width++ < view->ncols)
4410 waddch(view->window, ' ');
4411 if (view_needs_focus_indication(view))
4412 wstandend(view->window);
4422 while (max_lines > 0 && nprinted < max_lines) {
4423 enum got_diff_line_type linetype;
4426 linelen = getline(&line, &linesize, s->f);
4427 if (linelen == -1) {
4433 return got_ferror(s->f, GOT_ERR_IO);
4436 if (++s->lineno < s->first_displayed_line)
4438 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4440 if (s->lineno == view->hiline)
4443 /* Set view->maxx based on full line length. */
4444 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4450 view->maxx = MAX(view->maxx, width);
4454 linetype = s->lines[s->lineno].type;
4455 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4456 linetype < GOT_DIFF_LINE_CONTEXT)
4457 attr |= COLOR_PAIR(linetype);
4459 wattron(view->window, attr);
4460 if (s->first_displayed_line + nprinted == s->matched_line &&
4461 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4462 err = add_matched_line(&width, line, view->ncols, 0,
4463 view->window, view->x, regmatch);
4470 err = format_line(&wline, &width, &skip, line,
4471 view->x, view->ncols, 0, view->x ? 1 : 0);
4476 waddwstr(view->window, &wline[skip]);
4480 if (s->lineno == view->hiline) {
4481 /* highlight full gline length */
4482 while (width++ < view->ncols)
4483 waddch(view->window, ' ');
4485 if (width <= view->ncols - 1)
4486 waddch(view->window, '\n');
4489 wattroff(view->window, attr);
4490 if (++nprinted == 1)
4491 s->first_displayed_line = s->lineno;
4495 s->last_displayed_line = s->first_displayed_line +
4498 s->last_displayed_line = s->first_displayed_line;
4503 while (nprinted < view->nlines) {
4504 waddch(view->window, '\n');
4508 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4514 wstandout(view->window);
4515 waddwstr(view->window, wline);
4518 wstandend(view->window);
4525 get_datestr(time_t *time, char *datebuf)
4527 struct tm mytm, *tm;
4530 tm = gmtime_r(time, &mytm);
4533 s = asctime_r(tm, datebuf);
4536 p = strchr(s, '\n');
4542 static const struct got_error *
4543 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4544 off_t off, uint8_t type)
4546 struct got_diff_line *p;
4548 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4550 return got_error_from_errno("reallocarray");
4552 (*lines)[*nlines].offset = off;
4553 (*lines)[*nlines].type = type;
4559 static const struct got_error *
4560 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4561 struct got_diff_line *s_lines, size_t s_nlines)
4563 struct got_diff_line *p;
4567 if (fseeko(src, 0L, SEEK_SET) == -1)
4568 return got_error_from_errno("fseeko");
4571 r = fread(buf, 1, sizeof(buf), src);
4574 return got_error_from_errno("fread");
4578 if (fwrite(buf, 1, r, dst) != r)
4579 return got_ferror(dst, GOT_ERR_IO);
4582 if (s_nlines == 0 && *d_nlines == 0)
4586 * If commit info was in dst, increment line offsets
4587 * of the appended diff content, but skip s_lines[0]
4588 * because offset zero is already in *d_lines.
4590 if (*d_nlines > 0) {
4591 for (i = 1; i < s_nlines; ++i)
4592 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4600 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4602 /* d_lines is freed in close_diff_view() */
4603 return got_error_from_errno("reallocarray");
4608 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4609 *d_nlines += s_nlines;
4614 static const struct got_error *
4615 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4616 struct got_object_id *commit_id, struct got_reflist_head *refs,
4617 struct got_repository *repo, int ignore_ws, int force_text_diff,
4618 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4620 const struct got_error *err = NULL;
4621 char datebuf[26], *datestr;
4622 struct got_commit_object *commit;
4623 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4624 time_t committer_time;
4625 const char *author, *committer;
4626 char *refs_str = NULL;
4627 struct got_pathlist_entry *pe;
4632 err = build_refs_str(&refs_str, refs, commit_id, repo);
4637 err = got_object_open_as_commit(&commit, repo, commit_id);
4641 err = got_object_id_str(&id_str, commit_id);
4643 err = got_error_from_errno("got_object_id_str");
4647 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4651 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4652 refs_str ? refs_str : "", refs_str ? ")" : "");
4654 err = got_error_from_errno("fprintf");
4658 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4662 n = fprintf(outfile, "from: %s\n",
4663 got_object_commit_get_author(commit));
4665 err = got_error_from_errno("fprintf");
4669 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4673 author = got_object_commit_get_author(commit);
4674 committer = got_object_commit_get_committer(commit);
4675 if (strcmp(author, committer) != 0) {
4676 n = fprintf(outfile, "via: %s\n", committer);
4678 err = got_error_from_errno("fprintf");
4682 err = add_line_metadata(lines, nlines, outoff,
4683 GOT_DIFF_LINE_AUTHOR);
4687 committer_time = got_object_commit_get_committer_time(commit);
4688 datestr = get_datestr(&committer_time, datebuf);
4690 n = fprintf(outfile, "date: %s UTC\n", datestr);
4692 err = got_error_from_errno("fprintf");
4696 err = add_line_metadata(lines, nlines, outoff,
4697 GOT_DIFF_LINE_DATE);
4701 if (got_object_commit_get_nparents(commit) > 1) {
4702 const struct got_object_id_queue *parent_ids;
4703 struct got_object_qid *qid;
4705 parent_ids = got_object_commit_get_parent_ids(commit);
4706 STAILQ_FOREACH(qid, parent_ids, entry) {
4707 err = got_object_id_str(&id_str, &qid->id);
4710 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4712 err = got_error_from_errno("fprintf");
4716 err = add_line_metadata(lines, nlines, outoff,
4717 GOT_DIFF_LINE_META);
4725 err = got_object_commit_get_logmsg(&logmsg, commit);
4729 while ((line = strsep(&s, "\n")) != NULL) {
4730 n = fprintf(outfile, "%s\n", line);
4732 err = got_error_from_errno("fprintf");
4736 err = add_line_metadata(lines, nlines, outoff,
4737 GOT_DIFF_LINE_LOGMSG);
4742 TAILQ_FOREACH(pe, dsa->paths, entry) {
4743 struct got_diff_changed_path *cp = pe->data;
4744 int pad = dsa->max_path_len - pe->path_len + 1;
4746 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4747 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4748 dsa->rm_cols + 1, cp->rm);
4750 err = got_error_from_errno("fprintf");
4754 err = add_line_metadata(lines, nlines, outoff,
4755 GOT_DIFF_LINE_CHANGES);
4760 fputc('\n', outfile);
4762 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4766 n = fprintf(outfile,
4767 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4768 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4769 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4771 err = got_error_from_errno("fprintf");
4775 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4779 fputc('\n', outfile);
4781 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4786 got_object_commit_close(commit);
4795 static const struct got_error *
4796 create_diff(struct tog_diff_view_state *s)
4798 const struct got_error *err = NULL;
4799 FILE *f = NULL, *tmp_diff_file = NULL;
4801 struct got_diff_line *lines = NULL;
4802 struct got_pathlist_head changed_paths;
4804 TAILQ_INIT(&changed_paths);
4807 s->lines = malloc(sizeof(*s->lines));
4808 if (s->lines == NULL)
4809 return got_error_from_errno("malloc");
4814 err = got_error_from_errno("got_opentemp");
4817 tmp_diff_file = got_opentemp();
4818 if (tmp_diff_file == NULL) {
4819 err = got_error_from_errno("got_opentemp");
4822 if (s->f && fclose(s->f) == EOF) {
4823 err = got_error_from_errno("fclose");
4829 err = got_object_get_type(&obj_type, s->repo, s->id1);
4831 err = got_object_get_type(&obj_type, s->repo, s->id2);
4836 case GOT_OBJ_TYPE_BLOB:
4837 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4838 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4839 s->label1, s->label2, tog_diff_algo, s->diff_context,
4840 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4843 case GOT_OBJ_TYPE_TREE:
4844 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4845 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4846 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4847 s->force_text_diff, NULL, s->repo, s->f);
4849 case GOT_OBJ_TYPE_COMMIT: {
4850 const struct got_object_id_queue *parent_ids;
4851 struct got_object_qid *pid;
4852 struct got_commit_object *commit2;
4853 struct got_reflist_head *refs;
4855 struct got_diffstat_cb_arg dsa = {
4858 s->ignore_whitespace,
4863 lines = malloc(sizeof(*lines));
4864 if (lines == NULL) {
4865 err = got_error_from_errno("malloc");
4869 /* build diff first in tmp file then append to commit info */
4870 err = got_diff_objects_as_commits(&lines, &nlines,
4871 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4872 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4873 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4877 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4880 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4881 /* Show commit info if we're diffing to a parent/root commit. */
4882 if (s->id1 == NULL) {
4883 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4884 refs, s->repo, s->ignore_whitespace,
4885 s->force_text_diff, &dsa, s->f);
4889 parent_ids = got_object_commit_get_parent_ids(commit2);
4890 STAILQ_FOREACH(pid, parent_ids, entry) {
4891 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4892 err = write_commit_info(&s->lines,
4893 &s->nlines, s->id2, refs, s->repo,
4894 s->ignore_whitespace,
4895 s->force_text_diff, &dsa, s->f);
4902 got_object_commit_close(commit2);
4904 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4909 err = got_error(GOT_ERR_OBJ_TYPE);
4914 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4915 if (s->f && fflush(s->f) != 0 && err == NULL)
4916 err = got_error_from_errno("fflush");
4917 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4918 err = got_error_from_errno("fclose");
4923 diff_view_indicate_progress(struct tog_view *view)
4925 mvwaddstr(view->window, 0, 0, "diffing...");
4930 static const struct got_error *
4931 search_start_diff_view(struct tog_view *view)
4933 struct tog_diff_view_state *s = &view->state.diff;
4935 s->matched_line = 0;
4940 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4941 size_t *nlines, int **first, int **last, int **match, int **selected)
4943 struct tog_diff_view_state *s = &view->state.diff;
4946 *nlines = s->nlines;
4947 *line_offsets = NULL;
4948 *match = &s->matched_line;
4949 *first = &s->first_displayed_line;
4950 *last = &s->last_displayed_line;
4951 *selected = &s->selected_line;
4954 static const struct got_error *
4955 search_next_view_match(struct tog_view *view)
4957 const struct got_error *err = NULL;
4961 size_t linesize = 0;
4963 off_t *line_offsets;
4965 int *first, *last, *match, *selected;
4967 if (!view->search_setup)
4968 return got_error_msg(GOT_ERR_NOT_IMPL,
4969 "view search not supported");
4970 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4973 if (!view->searching) {
4974 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4979 if (view->searching == TOG_SEARCH_FORWARD)
4980 lineno = *match + 1;
4982 lineno = *match - 1;
4984 lineno = *first - 1 + *selected;
4989 if (lineno <= 0 || lineno > nlines) {
4991 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4995 if (view->searching == TOG_SEARCH_FORWARD)
5001 offset = view->type == TOG_VIEW_DIFF ?
5002 view->state.diff.lines[lineno - 1].offset :
5003 line_offsets[lineno - 1];
5004 if (fseeko(f, offset, SEEK_SET) != 0) {
5006 return got_error_from_errno("fseeko");
5008 linelen = getline(&line, &linesize, f);
5009 if (linelen != -1) {
5011 err = expand_tab(&exstr, line);
5014 if (match_line(exstr, &view->regex, 1,
5016 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5023 if (view->searching == TOG_SEARCH_FORWARD)
5038 static const struct got_error *
5039 close_diff_view(struct tog_view *view)
5041 const struct got_error *err = NULL;
5042 struct tog_diff_view_state *s = &view->state.diff;
5048 if (s->f && fclose(s->f) == EOF)
5049 err = got_error_from_errno("fclose");
5051 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5052 err = got_error_from_errno("fclose");
5054 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5055 err = got_error_from_errno("fclose");
5057 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5058 err = got_error_from_errno("close");
5060 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5061 err = got_error_from_errno("close");
5069 static const struct got_error *
5070 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5071 struct got_object_id *id2, const char *label1, const char *label2,
5072 int diff_context, int ignore_whitespace, int force_text_diff,
5073 struct tog_view *parent_view, struct got_repository *repo)
5075 const struct got_error *err;
5076 struct tog_diff_view_state *s = &view->state.diff;
5078 memset(s, 0, sizeof(*s));
5082 if (id1 != NULL && id2 != NULL) {
5084 err = got_object_get_type(&type1, repo, id1);
5087 err = got_object_get_type(&type2, repo, id2);
5092 return got_error(GOT_ERR_OBJ_TYPE);
5094 s->first_displayed_line = 1;
5095 s->last_displayed_line = view->nlines;
5096 s->selected_line = 1;
5104 s->id1 = got_object_id_dup(id1);
5106 return got_error_from_errno("got_object_id_dup");
5110 s->id2 = got_object_id_dup(id2);
5111 if (s->id2 == NULL) {
5112 err = got_error_from_errno("got_object_id_dup");
5116 s->f1 = got_opentemp();
5117 if (s->f1 == NULL) {
5118 err = got_error_from_errno("got_opentemp");
5122 s->f2 = got_opentemp();
5123 if (s->f2 == NULL) {
5124 err = got_error_from_errno("got_opentemp");
5128 s->fd1 = got_opentempfd();
5130 err = got_error_from_errno("got_opentempfd");
5134 s->fd2 = got_opentempfd();
5136 err = got_error_from_errno("got_opentempfd");
5140 s->diff_context = diff_context;
5141 s->ignore_whitespace = ignore_whitespace;
5142 s->force_text_diff = force_text_diff;
5143 s->parent_view = parent_view;
5146 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5149 rc = init_pair(GOT_DIFF_LINE_MINUS,
5150 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5152 rc = init_pair(GOT_DIFF_LINE_PLUS,
5153 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5155 rc = init_pair(GOT_DIFF_LINE_HUNK,
5156 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5158 rc = init_pair(GOT_DIFF_LINE_META,
5159 get_color_value("TOG_COLOR_DIFF_META"), -1);
5161 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5162 get_color_value("TOG_COLOR_DIFF_META"), -1);
5164 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5165 get_color_value("TOG_COLOR_DIFF_META"), -1);
5167 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5168 get_color_value("TOG_COLOR_DIFF_META"), -1);
5170 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5171 get_color_value("TOG_COLOR_AUTHOR"), -1);
5173 rc = init_pair(GOT_DIFF_LINE_DATE,
5174 get_color_value("TOG_COLOR_DATE"), -1);
5176 err = got_error(GOT_ERR_RANGE);
5181 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5182 view_is_splitscreen(view))
5183 show_log_view(parent_view); /* draw border */
5184 diff_view_indicate_progress(view);
5186 err = create_diff(s);
5188 view->show = show_diff_view;
5189 view->input = input_diff_view;
5190 view->reset = reset_diff_view;
5191 view->close = close_diff_view;
5192 view->search_start = search_start_diff_view;
5193 view->search_setup = search_setup_diff_view;
5194 view->search_next = search_next_view_match;
5197 close_diff_view(view);
5201 static const struct got_error *
5202 show_diff_view(struct tog_view *view)
5204 const struct got_error *err;
5205 struct tog_diff_view_state *s = &view->state.diff;
5206 char *id_str1 = NULL, *id_str2, *header;
5207 const char *label1, *label2;
5210 err = got_object_id_str(&id_str1, s->id1);
5213 label1 = s->label1 ? s->label1 : id_str1;
5215 label1 = "/dev/null";
5217 err = got_object_id_str(&id_str2, s->id2);
5220 label2 = s->label2 ? s->label2 : id_str2;
5222 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5223 err = got_error_from_errno("asprintf");
5231 err = draw_file(view, header);
5236 static const struct got_error *
5237 set_selected_commit(struct tog_diff_view_state *s,
5238 struct commit_queue_entry *entry)
5240 const struct got_error *err;
5241 const struct got_object_id_queue *parent_ids;
5242 struct got_commit_object *selected_commit;
5243 struct got_object_qid *pid;
5246 s->id2 = got_object_id_dup(entry->id);
5248 return got_error_from_errno("got_object_id_dup");
5250 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5253 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5255 pid = STAILQ_FIRST(parent_ids);
5256 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5257 got_object_commit_close(selected_commit);
5261 static const struct got_error *
5262 reset_diff_view(struct tog_view *view)
5264 struct tog_diff_view_state *s = &view->state.diff;
5267 wclear(view->window);
5268 s->first_displayed_line = 1;
5269 s->last_displayed_line = view->nlines;
5270 s->matched_line = 0;
5271 diff_view_indicate_progress(view);
5272 return create_diff(s);
5276 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5280 i = start = s->first_displayed_line - 1;
5282 while (s->lines[i].type != type) {
5286 return; /* do nothing, requested type not in file */
5289 s->selected_line = 1;
5290 s->first_displayed_line = i;
5294 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5298 i = start = s->first_displayed_line + 1;
5300 while (s->lines[i].type != type) {
5301 if (i == s->nlines - 1)
5304 return; /* do nothing, requested type not in file */
5307 s->selected_line = 1;
5308 s->first_displayed_line = i;
5311 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5313 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5316 static const struct got_error *
5317 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5319 const struct got_error *err = NULL;
5320 struct tog_diff_view_state *s = &view->state.diff;
5321 struct tog_log_view_state *ls;
5322 struct commit_queue_entry *old_selected_entry;
5324 size_t linesize = 0;
5326 int i, nscroll = view->nlines - 1, up = 0;
5328 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5337 horizontal_scroll_input(view, ch);
5342 s->force_text_diff = !s->force_text_diff;
5343 view->action = s->force_text_diff ?
5344 "force ASCII text enabled" :
5345 "force ASCII text disabled";
5347 else if (ch == 'w') {
5348 s->ignore_whitespace = !s->ignore_whitespace;
5349 view->action = s->ignore_whitespace ?
5350 "ignore whitespace enabled" :
5351 "ignore whitespace disabled";
5353 err = reset_diff_view(view);
5357 s->first_displayed_line = 1;
5366 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5372 if (s->first_displayed_line > 1)
5373 s->first_displayed_line--;
5384 if (s->first_displayed_line == 1) {
5389 while (i++ < nscroll && s->first_displayed_line > 1)
5390 s->first_displayed_line--;
5396 s->first_displayed_line++;
5413 while (!s->eof && i++ < nscroll) {
5414 linelen = getline(&line, &linesize, s->f);
5415 s->first_displayed_line++;
5416 if (linelen == -1) {
5420 err = got_ferror(s->f, GOT_ERR_IO);
5427 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5430 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5433 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5436 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5439 if (s->diff_context > 0) {
5441 s->matched_line = 0;
5442 diff_view_indicate_progress(view);
5443 err = create_diff(s);
5444 if (s->first_displayed_line + view->nlines - 1 >
5446 s->first_displayed_line = 1;
5447 s->last_displayed_line = view->nlines;
5453 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5455 s->matched_line = 0;
5456 diff_view_indicate_progress(view);
5457 err = create_diff(s);
5469 if (s->parent_view == NULL) {
5473 s->parent_view->count = view->count;
5475 if (s->parent_view->type == TOG_VIEW_LOG) {
5476 ls = &s->parent_view->state.log;
5477 old_selected_entry = ls->selected_entry;
5479 err = input_log_view(NULL, s->parent_view,
5480 up ? KEY_UP : KEY_DOWN);
5483 view->count = s->parent_view->count;
5485 if (old_selected_entry == ls->selected_entry)
5488 err = set_selected_commit(s, ls->selected_entry);
5491 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5492 struct tog_blame_view_state *bs;
5493 struct got_object_id *id, *prev_id;
5495 bs = &s->parent_view->state.blame;
5496 prev_id = get_annotation_for_line(bs->blame.lines,
5497 bs->blame.nlines, bs->last_diffed_line);
5499 err = input_blame_view(&view, s->parent_view,
5500 up ? KEY_UP : KEY_DOWN);
5503 view->count = s->parent_view->count;
5505 if (prev_id == NULL)
5507 id = get_selected_commit_id(bs->blame.lines,
5508 bs->blame.nlines, bs->first_displayed_line,
5513 if (!got_object_id_cmp(prev_id, id))
5516 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5520 s->first_displayed_line = 1;
5521 s->last_displayed_line = view->nlines;
5522 s->matched_line = 0;
5525 diff_view_indicate_progress(view);
5526 err = create_diff(s);
5536 static const struct got_error *
5537 cmd_diff(int argc, char *argv[])
5539 const struct got_error *error = NULL;
5540 struct got_repository *repo = NULL;
5541 struct got_worktree *worktree = NULL;
5542 struct got_object_id *id1 = NULL, *id2 = NULL;
5543 char *repo_path = NULL, *cwd = NULL;
5544 char *id_str1 = NULL, *id_str2 = NULL;
5545 char *label1 = NULL, *label2 = NULL;
5546 int diff_context = 3, ignore_whitespace = 0;
5547 int ch, force_text_diff = 0;
5549 struct tog_view *view;
5550 int *pack_fds = NULL;
5552 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5555 force_text_diff = 1;
5558 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5561 errx(1, "number of context lines is %s: %s",
5565 repo_path = realpath(optarg, NULL);
5566 if (repo_path == NULL)
5567 return got_error_from_errno2("realpath",
5569 got_path_strip_trailing_slashes(repo_path);
5572 ignore_whitespace = 1;
5584 usage_diff(); /* TODO show local worktree changes */
5585 } else if (argc == 2) {
5591 error = got_repo_pack_fds_open(&pack_fds);
5595 if (repo_path == NULL) {
5596 cwd = getcwd(NULL, 0);
5598 return got_error_from_errno("getcwd");
5599 error = got_worktree_open(&worktree, cwd);
5600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5604 strdup(got_worktree_get_repo_path(worktree));
5606 repo_path = strdup(cwd);
5607 if (repo_path == NULL) {
5608 error = got_error_from_errno("strdup");
5613 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5619 error = apply_unveil(got_repo_get_path(repo), NULL);
5623 error = tog_load_refs(repo, 0);
5627 error = got_repo_match_object_id(&id1, &label1, id_str1,
5628 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5632 error = got_repo_match_object_id(&id2, &label2, id_str2,
5633 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5637 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5639 error = got_error_from_errno("view_open");
5642 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5643 ignore_whitespace, force_text_diff, NULL, repo);
5646 error = view_loop(view);
5653 const struct got_error *close_err = got_repo_close(repo);
5658 got_worktree_close(worktree);
5660 const struct got_error *pack_err =
5661 got_repo_pack_fds_close(pack_fds);
5674 "usage: %s blame [-c commit] [-r repository-path] path\n",
5679 struct tog_blame_line {
5681 struct got_object_id *id;
5684 static const struct got_error *
5685 draw_blame(struct tog_view *view)
5687 struct tog_blame_view_state *s = &view->state.blame;
5688 struct tog_blame *blame = &s->blame;
5689 regmatch_t *regmatch = &view->regmatch;
5690 const struct got_error *err;
5691 int lineno = 0, nprinted = 0;
5693 size_t linesize = 0;
5697 struct tog_blame_line *blame_line;
5698 struct got_object_id *prev_id = NULL;
5700 struct tog_color *tc;
5702 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5707 werase(view->window);
5709 if (asprintf(&line, "commit %s", id_str) == -1) {
5710 err = got_error_from_errno("asprintf");
5715 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5720 if (view_needs_focus_indication(view))
5721 wstandout(view->window);
5722 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5724 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5725 waddwstr(view->window, wline);
5726 while (width++ < view->ncols)
5727 waddch(view->window, ' ');
5729 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5730 if (view_needs_focus_indication(view))
5731 wstandend(view->window);
5735 if (view->gline > blame->nlines)
5736 view->gline = blame->nlines;
5738 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5739 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5740 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5742 return got_error_from_errno("asprintf");
5745 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5750 waddwstr(view->window, wline);
5753 if (width < view->ncols - 1)
5754 waddch(view->window, '\n');
5758 while (nprinted < view->nlines - 2) {
5759 linelen = getline(&line, &linesize, blame->f);
5760 if (linelen == -1) {
5761 if (feof(blame->f)) {
5766 return got_ferror(blame->f, GOT_ERR_IO);
5768 if (++lineno < s->first_displayed_line)
5770 if (view->gline && !gotoline(view, &lineno, &nprinted))
5773 /* Set view->maxx based on full line length. */
5774 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5781 view->maxx = MAX(view->maxx, width);
5783 if (nprinted == s->selected_line - 1)
5784 wstandout(view->window);
5786 if (blame->nlines > 0) {
5787 blame_line = &blame->lines[lineno - 1];
5788 if (blame_line->annotated && prev_id &&
5789 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5790 !(nprinted == s->selected_line - 1)) {
5791 waddstr(view->window, " ");
5792 } else if (blame_line->annotated) {
5794 err = got_object_id_str(&id_str,
5800 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5802 wattr_on(view->window,
5803 COLOR_PAIR(tc->colorpair), NULL);
5804 wprintw(view->window, "%.8s", id_str);
5806 wattr_off(view->window,
5807 COLOR_PAIR(tc->colorpair), NULL);
5809 prev_id = blame_line->id;
5811 waddstr(view->window, "........");
5815 waddstr(view->window, "........");
5819 if (nprinted == s->selected_line - 1)
5820 wstandend(view->window);
5821 waddstr(view->window, " ");
5823 if (view->ncols <= 9) {
5825 } else if (s->first_displayed_line + nprinted ==
5827 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5828 err = add_matched_line(&width, line, view->ncols - 9, 9,
5829 view->window, view->x, regmatch);
5837 err = format_line(&wline, &width, &skip, line,
5838 view->x, view->ncols - 9, 9, 1);
5843 waddwstr(view->window, &wline[skip]);
5849 if (width <= view->ncols - 1)
5850 waddch(view->window, '\n');
5851 if (++nprinted == 1)
5852 s->first_displayed_line = lineno;
5855 s->last_displayed_line = lineno;
5862 static const struct got_error *
5863 blame_cb(void *arg, int nlines, int lineno,
5864 struct got_commit_object *commit, struct got_object_id *id)
5866 const struct got_error *err = NULL;
5867 struct tog_blame_cb_args *a = arg;
5868 struct tog_blame_line *line;
5871 if (nlines != a->nlines ||
5872 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5873 return got_error(GOT_ERR_RANGE);
5875 errcode = pthread_mutex_lock(&tog_mutex);
5877 return got_error_set_errno(errcode, "pthread_mutex_lock");
5879 if (*a->quit) { /* user has quit the blame view */
5880 err = got_error(GOT_ERR_ITER_COMPLETED);
5885 goto done; /* no change in this commit */
5887 line = &a->lines[lineno - 1];
5888 if (line->annotated)
5891 line->id = got_object_id_dup(id);
5892 if (line->id == NULL) {
5893 err = got_error_from_errno("got_object_id_dup");
5896 line->annotated = 1;
5898 errcode = pthread_mutex_unlock(&tog_mutex);
5900 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5905 blame_thread(void *arg)
5907 const struct got_error *err, *close_err;
5908 struct tog_blame_thread_args *ta = arg;
5909 struct tog_blame_cb_args *a = ta->cb_args;
5910 int errcode, fd1 = -1, fd2 = -1;
5911 FILE *f1 = NULL, *f2 = NULL;
5913 fd1 = got_opentempfd();
5915 return (void *)got_error_from_errno("got_opentempfd");
5917 fd2 = got_opentempfd();
5919 err = got_error_from_errno("got_opentempfd");
5923 f1 = got_opentemp();
5925 err = (void *)got_error_from_errno("got_opentemp");
5928 f2 = got_opentemp();
5930 err = (void *)got_error_from_errno("got_opentemp");
5934 err = block_signals_used_by_main_thread();
5938 err = got_blame(ta->path, a->commit_id, ta->repo,
5939 tog_diff_algo, blame_cb, ta->cb_args,
5940 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5941 if (err && err->code == GOT_ERR_CANCELLED)
5944 errcode = pthread_mutex_lock(&tog_mutex);
5946 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5950 close_err = got_repo_close(ta->repo);
5956 errcode = pthread_mutex_unlock(&tog_mutex);
5957 if (errcode && err == NULL)
5958 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5961 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5962 err = got_error_from_errno("close");
5963 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5964 err = got_error_from_errno("close");
5965 if (f1 && fclose(f1) == EOF && err == NULL)
5966 err = got_error_from_errno("fclose");
5967 if (f2 && fclose(f2) == EOF && err == NULL)
5968 err = got_error_from_errno("fclose");
5973 static struct got_object_id *
5974 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5975 int first_displayed_line, int selected_line)
5977 struct tog_blame_line *line;
5982 line = &lines[first_displayed_line - 1 + selected_line - 1];
5983 if (!line->annotated)
5989 static struct got_object_id *
5990 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5993 struct tog_blame_line *line;
5995 if (nlines <= 0 || lineno >= nlines)
5998 line = &lines[lineno - 1];
5999 if (!line->annotated)
6005 static const struct got_error *
6006 stop_blame(struct tog_blame *blame)
6008 const struct got_error *err = NULL;
6011 if (blame->thread) {
6013 errcode = pthread_mutex_unlock(&tog_mutex);
6015 return got_error_set_errno(errcode,
6016 "pthread_mutex_unlock");
6017 errcode = pthread_join(blame->thread, (void **)&err);
6019 return got_error_set_errno(errcode, "pthread_join");
6020 errcode = pthread_mutex_lock(&tog_mutex);
6022 return got_error_set_errno(errcode,
6023 "pthread_mutex_lock");
6024 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6026 blame->thread = NULL;
6028 if (blame->thread_args.repo) {
6029 const struct got_error *close_err;
6030 close_err = got_repo_close(blame->thread_args.repo);
6033 blame->thread_args.repo = NULL;
6036 if (fclose(blame->f) == EOF && err == NULL)
6037 err = got_error_from_errno("fclose");
6041 for (i = 0; i < blame->nlines; i++)
6042 free(blame->lines[i].id);
6044 blame->lines = NULL;
6046 free(blame->cb_args.commit_id);
6047 blame->cb_args.commit_id = NULL;
6048 if (blame->pack_fds) {
6049 const struct got_error *pack_err =
6050 got_repo_pack_fds_close(blame->pack_fds);
6053 blame->pack_fds = NULL;
6058 static const struct got_error *
6059 cancel_blame_view(void *arg)
6061 const struct got_error *err = NULL;
6065 errcode = pthread_mutex_lock(&tog_mutex);
6067 return got_error_set_errno(errcode,
6068 "pthread_mutex_unlock");
6071 err = got_error(GOT_ERR_CANCELLED);
6073 errcode = pthread_mutex_unlock(&tog_mutex);
6075 return got_error_set_errno(errcode,
6076 "pthread_mutex_lock");
6081 static const struct got_error *
6082 run_blame(struct tog_view *view)
6084 struct tog_blame_view_state *s = &view->state.blame;
6085 struct tog_blame *blame = &s->blame;
6086 const struct got_error *err = NULL;
6087 struct got_commit_object *commit = NULL;
6088 struct got_blob_object *blob = NULL;
6089 struct got_repository *thread_repo = NULL;
6090 struct got_object_id *obj_id = NULL;
6091 int obj_type, fd = -1;
6092 int *pack_fds = NULL;
6094 err = got_object_open_as_commit(&commit, s->repo,
6095 &s->blamed_commit->id);
6099 fd = got_opentempfd();
6101 err = got_error_from_errno("got_opentempfd");
6105 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6109 err = got_object_get_type(&obj_type, s->repo, obj_id);
6113 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6114 err = got_error(GOT_ERR_OBJ_TYPE);
6118 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6121 blame->f = got_opentemp();
6122 if (blame->f == NULL) {
6123 err = got_error_from_errno("got_opentemp");
6126 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6127 &blame->line_offsets, blame->f, blob);
6130 if (blame->nlines == 0) {
6131 s->blame_complete = 1;
6135 /* Don't include \n at EOF in the blame line count. */
6136 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6139 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6140 if (blame->lines == NULL) {
6141 err = got_error_from_errno("calloc");
6145 err = got_repo_pack_fds_open(&pack_fds);
6148 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6153 blame->pack_fds = pack_fds;
6154 blame->cb_args.view = view;
6155 blame->cb_args.lines = blame->lines;
6156 blame->cb_args.nlines = blame->nlines;
6157 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6158 if (blame->cb_args.commit_id == NULL) {
6159 err = got_error_from_errno("got_object_id_dup");
6162 blame->cb_args.quit = &s->done;
6164 blame->thread_args.path = s->path;
6165 blame->thread_args.repo = thread_repo;
6166 blame->thread_args.cb_args = &blame->cb_args;
6167 blame->thread_args.complete = &s->blame_complete;
6168 blame->thread_args.cancel_cb = cancel_blame_view;
6169 blame->thread_args.cancel_arg = &s->done;
6170 s->blame_complete = 0;
6172 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6173 s->first_displayed_line = 1;
6174 s->last_displayed_line = view->nlines;
6175 s->selected_line = 1;
6177 s->matched_line = 0;
6181 got_object_commit_close(commit);
6182 if (fd != -1 && close(fd) == -1 && err == NULL)
6183 err = got_error_from_errno("close");
6185 got_object_blob_close(blob);
6192 static const struct got_error *
6193 open_blame_view(struct tog_view *view, char *path,
6194 struct got_object_id *commit_id, struct got_repository *repo)
6196 const struct got_error *err = NULL;
6197 struct tog_blame_view_state *s = &view->state.blame;
6199 STAILQ_INIT(&s->blamed_commits);
6201 s->path = strdup(path);
6202 if (s->path == NULL)
6203 return got_error_from_errno("strdup");
6205 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6211 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6212 s->first_displayed_line = 1;
6213 s->last_displayed_line = view->nlines;
6214 s->selected_line = 1;
6215 s->blame_complete = 0;
6217 s->commit_id = commit_id;
6218 memset(&s->blame, 0, sizeof(s->blame));
6220 STAILQ_INIT(&s->colors);
6221 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6222 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6223 get_color_value("TOG_COLOR_COMMIT"));
6228 view->show = show_blame_view;
6229 view->input = input_blame_view;
6230 view->reset = reset_blame_view;
6231 view->close = close_blame_view;
6232 view->search_start = search_start_blame_view;
6233 view->search_setup = search_setup_blame_view;
6234 view->search_next = search_next_view_match;
6236 return run_blame(view);
6239 static const struct got_error *
6240 close_blame_view(struct tog_view *view)
6242 const struct got_error *err = NULL;
6243 struct tog_blame_view_state *s = &view->state.blame;
6245 if (s->blame.thread)
6246 err = stop_blame(&s->blame);
6248 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6249 struct got_object_qid *blamed_commit;
6250 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6251 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6252 got_object_qid_free(blamed_commit);
6256 free_colors(&s->colors);
6260 static const struct got_error *
6261 search_start_blame_view(struct tog_view *view)
6263 struct tog_blame_view_state *s = &view->state.blame;
6265 s->matched_line = 0;
6270 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6271 size_t *nlines, int **first, int **last, int **match, int **selected)
6273 struct tog_blame_view_state *s = &view->state.blame;
6276 *nlines = s->blame.nlines;
6277 *line_offsets = s->blame.line_offsets;
6278 *match = &s->matched_line;
6279 *first = &s->first_displayed_line;
6280 *last = &s->last_displayed_line;
6281 *selected = &s->selected_line;
6284 static const struct got_error *
6285 show_blame_view(struct tog_view *view)
6287 const struct got_error *err = NULL;
6288 struct tog_blame_view_state *s = &view->state.blame;
6291 if (s->blame.thread == NULL && !s->blame_complete) {
6292 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6293 &s->blame.thread_args);
6295 return got_error_set_errno(errcode, "pthread_create");
6297 halfdelay(1); /* fast refresh while annotating */
6300 if (s->blame_complete)
6301 halfdelay(10); /* disable fast refresh */
6303 err = draw_blame(view);
6309 static const struct got_error *
6310 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6311 struct got_repository *repo, struct got_object_id *id)
6313 struct tog_view *log_view;
6314 const struct got_error *err = NULL;
6318 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6319 if (log_view == NULL)
6320 return got_error_from_errno("view_open");
6322 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6324 view_close(log_view);
6326 *new_view = log_view;
6331 static const struct got_error *
6332 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6334 const struct got_error *err = NULL, *thread_err = NULL;
6335 struct tog_view *diff_view;
6336 struct tog_blame_view_state *s = &view->state.blame;
6337 int eos, nscroll, begin_y = 0, begin_x = 0;
6339 eos = nscroll = view->nlines - 2;
6340 if (view_is_hsplit_top(view))
6350 horizontal_scroll_input(view, ch);
6357 s->selected_line = 1;
6358 s->first_displayed_line = 1;
6363 if (s->blame.nlines < eos) {
6364 s->selected_line = s->blame.nlines;
6365 s->first_displayed_line = 1;
6367 s->selected_line = eos;
6368 s->first_displayed_line = s->blame.nlines - (eos - 1);
6375 if (s->selected_line > 1)
6377 else if (s->selected_line == 1 &&
6378 s->first_displayed_line > 1)
6379 s->first_displayed_line--;
6390 if (s->first_displayed_line == 1) {
6391 if (view->count > 1)
6393 s->selected_line = MAX(1, s->selected_line - nscroll);
6397 if (s->first_displayed_line > nscroll)
6398 s->first_displayed_line -= nscroll;
6400 s->first_displayed_line = 1;
6405 if (s->selected_line < eos && s->first_displayed_line +
6406 s->selected_line <= s->blame.nlines)
6408 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6409 s->first_displayed_line++;
6415 struct got_object_id *id = NULL;
6418 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6419 s->first_displayed_line, s->selected_line);
6423 struct got_commit_object *commit, *pcommit;
6424 struct got_object_qid *pid;
6425 struct got_object_id *blob_id = NULL;
6427 err = got_object_open_as_commit(&commit,
6432 got_object_commit_get_parent_ids(commit));
6434 got_object_commit_close(commit);
6437 /* Check if path history ends here. */
6438 err = got_object_open_as_commit(&pcommit,
6442 err = got_object_id_by_path(&blob_id, s->repo,
6444 got_object_commit_close(pcommit);
6446 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6448 got_object_commit_close(commit);
6451 err = got_object_get_type(&obj_type, s->repo,
6454 /* Can't blame non-blob type objects. */
6455 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6456 got_object_commit_close(commit);
6459 err = got_object_qid_alloc(&s->blamed_commit,
6461 got_object_commit_close(commit);
6463 if (got_object_id_cmp(id,
6464 &s->blamed_commit->id) == 0)
6466 err = got_object_qid_alloc(&s->blamed_commit,
6472 thread_err = stop_blame(&s->blame);
6476 STAILQ_INSERT_HEAD(&s->blamed_commits,
6477 s->blamed_commit, entry);
6478 err = run_blame(view);
6484 struct got_object_qid *first;
6487 first = STAILQ_FIRST(&s->blamed_commits);
6488 if (!got_object_id_cmp(&first->id, s->commit_id))
6491 thread_err = stop_blame(&s->blame);
6495 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6496 got_object_qid_free(s->blamed_commit);
6498 STAILQ_FIRST(&s->blamed_commits);
6499 err = run_blame(view);
6506 s->id_to_log = get_selected_commit_id(s->blame.lines,
6507 s->blame.nlines, s->first_displayed_line, s->selected_line);
6509 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6513 struct got_object_id *id = NULL;
6514 struct got_object_qid *pid;
6515 struct got_commit_object *commit = NULL;
6518 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6519 s->first_displayed_line, s->selected_line);
6522 err = got_object_open_as_commit(&commit, s->repo, id);
6525 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6527 /* traversed from diff view, release diff resources */
6528 err = close_diff_view(*new_view);
6531 diff_view = *new_view;
6533 if (view_is_parent_view(view))
6534 view_get_split(view, &begin_y, &begin_x);
6536 diff_view = view_open(0, 0, begin_y, begin_x,
6538 if (diff_view == NULL) {
6539 got_object_commit_close(commit);
6540 err = got_error_from_errno("view_open");
6544 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6545 id, NULL, NULL, 3, 0, 0, view, s->repo);
6546 got_object_commit_close(commit);
6548 view_close(diff_view);
6551 s->last_diffed_line = s->first_displayed_line - 1 +
6554 break; /* still open from active diff view */
6555 if (view_is_parent_view(view) &&
6556 view->mode == TOG_VIEW_SPLIT_HRZN) {
6557 err = view_init_hsplit(view, begin_y);
6563 diff_view->focussed = 1;
6564 diff_view->mode = view->mode;
6565 diff_view->nlines = view->lines - begin_y;
6566 if (view_is_parent_view(view)) {
6567 view_transfer_size(diff_view, view);
6568 err = view_close_child(view);
6571 err = view_set_child(view, diff_view);
6574 view->focus_child = 1;
6576 *new_view = diff_view;
6589 if (s->last_displayed_line >= s->blame.nlines &&
6590 s->selected_line >= MIN(s->blame.nlines,
6591 view->nlines - 2)) {
6595 if (s->last_displayed_line >= s->blame.nlines &&
6596 s->selected_line < view->nlines - 2) {
6598 MIN(nscroll, s->last_displayed_line -
6599 s->first_displayed_line - s->selected_line + 1);
6601 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6602 s->first_displayed_line += nscroll;
6604 s->first_displayed_line =
6605 s->blame.nlines - (view->nlines - 3);
6608 if (s->selected_line > view->nlines - 2) {
6609 s->selected_line = MIN(s->blame.nlines,
6617 return thread_err ? thread_err : err;
6620 static const struct got_error *
6621 reset_blame_view(struct tog_view *view)
6623 const struct got_error *err;
6624 struct tog_blame_view_state *s = &view->state.blame;
6628 err = stop_blame(&s->blame);
6632 return run_blame(view);
6635 static const struct got_error *
6636 cmd_blame(int argc, char *argv[])
6638 const struct got_error *error;
6639 struct got_repository *repo = NULL;
6640 struct got_worktree *worktree = NULL;
6641 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6642 char *link_target = NULL;
6643 struct got_object_id *commit_id = NULL;
6644 struct got_commit_object *commit = NULL;
6645 char *commit_id_str = NULL;
6647 struct tog_view *view;
6648 int *pack_fds = NULL;
6650 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6653 commit_id_str = optarg;
6656 repo_path = realpath(optarg, NULL);
6657 if (repo_path == NULL)
6658 return got_error_from_errno2("realpath",
6673 error = got_repo_pack_fds_open(&pack_fds);
6677 if (repo_path == NULL) {
6678 cwd = getcwd(NULL, 0);
6680 return got_error_from_errno("getcwd");
6681 error = got_worktree_open(&worktree, cwd);
6682 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6686 strdup(got_worktree_get_repo_path(worktree));
6688 repo_path = strdup(cwd);
6689 if (repo_path == NULL) {
6690 error = got_error_from_errno("strdup");
6695 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6699 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6706 error = apply_unveil(got_repo_get_path(repo), NULL);
6710 error = tog_load_refs(repo, 0);
6714 if (commit_id_str == NULL) {
6715 struct got_reference *head_ref;
6716 error = got_ref_open(&head_ref, repo, worktree ?
6717 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6720 error = got_ref_resolve(&commit_id, repo, head_ref);
6721 got_ref_close(head_ref);
6723 error = got_repo_match_object_id(&commit_id, NULL,
6724 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6729 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6731 error = got_error_from_errno("view_open");
6735 error = got_object_open_as_commit(&commit, repo, commit_id);
6739 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6744 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6749 /* Release work tree lock. */
6750 got_worktree_close(worktree);
6753 error = view_loop(view);
6761 got_object_commit_close(commit);
6763 got_worktree_close(worktree);
6765 const struct got_error *close_err = got_repo_close(repo);
6770 const struct got_error *pack_err =
6771 got_repo_pack_fds_close(pack_fds);
6779 static const struct got_error *
6780 draw_tree_entries(struct tog_view *view, const char *parent_path)
6782 struct tog_tree_view_state *s = &view->state.tree;
6783 const struct got_error *err = NULL;
6784 struct got_tree_entry *te;
6787 struct tog_color *tc;
6788 int width, n, nentries, scrollx, i = 1;
6789 int limit = view->nlines;
6792 if (view_is_hsplit_top(view))
6793 --limit; /* border */
6795 werase(view->window);
6800 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6804 if (view_needs_focus_indication(view))
6805 wstandout(view->window);
6806 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6808 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6809 waddwstr(view->window, wline);
6812 while (width++ < view->ncols)
6813 waddch(view->window, ' ');
6815 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6816 if (view_needs_focus_indication(view))
6817 wstandend(view->window);
6822 if (s->first_displayed_entry) {
6823 i += got_tree_entry_get_index(s->first_displayed_entry);
6824 if (s->tree != s->root)
6825 ++i; /* account for ".." entry */
6827 nentries = got_object_tree_get_nentries(s->tree);
6828 if (asprintf(&index, "[%d/%d] %s",
6829 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6830 return got_error_from_errno("asprintf");
6831 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6835 waddwstr(view->window, wline);
6838 if (width < view->ncols - 1)
6839 waddch(view->window, '\n');
6842 waddch(view->window, '\n');
6846 if (s->first_displayed_entry == NULL) {
6847 te = got_object_tree_get_first_entry(s->tree);
6848 if (s->selected == 0) {
6850 wstandout(view->window);
6851 s->selected_entry = NULL;
6853 waddstr(view->window, " ..\n"); /* parent directory */
6854 if (s->selected == 0 && view->focussed)
6855 wstandend(view->window);
6862 te = s->first_displayed_entry;
6866 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6867 char *line = NULL, *id_str = NULL, *link_target = NULL;
6868 const char *modestr = "";
6871 te = got_object_tree_get_entry(s->tree, i);
6872 mode = got_tree_entry_get_mode(te);
6875 err = got_object_id_str(&id_str,
6876 got_tree_entry_get_id(te));
6878 return got_error_from_errno(
6879 "got_object_id_str");
6881 if (got_object_tree_entry_is_submodule(te))
6883 else if (S_ISLNK(mode)) {
6886 err = got_tree_entry_get_symlink_target(&link_target,
6892 for (i = 0; i < strlen(link_target); i++) {
6893 if (!isprint((unsigned char)link_target[i]))
6894 link_target[i] = '?';
6898 else if (S_ISDIR(mode))
6900 else if (mode & S_IXUSR)
6902 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6903 got_tree_entry_get_name(te), modestr,
6904 link_target ? " -> ": "",
6905 link_target ? link_target : "") == -1) {
6908 return got_error_from_errno("asprintf");
6913 /* use full line width to determine view->maxx */
6914 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6919 view->maxx = MAX(view->maxx, width);
6923 err = format_line(&wline, &width, &scrollx, line, view->x,
6929 if (n == s->selected) {
6931 wstandout(view->window);
6932 s->selected_entry = te;
6934 tc = match_color(&s->colors, line);
6936 wattr_on(view->window,
6937 COLOR_PAIR(tc->colorpair), NULL);
6938 waddwstr(view->window, &wline[scrollx]);
6940 wattr_off(view->window,
6941 COLOR_PAIR(tc->colorpair), NULL);
6942 if (width < view->ncols)
6943 waddch(view->window, '\n');
6944 if (n == s->selected && view->focussed)
6945 wstandend(view->window);
6951 s->last_displayed_entry = te;
6960 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6962 struct got_tree_entry *te;
6963 int isroot = s->tree == s->root;
6966 if (s->first_displayed_entry == NULL)
6969 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6970 while (i++ < maxscroll) {
6973 s->first_displayed_entry = NULL;
6976 s->first_displayed_entry = te;
6977 te = got_tree_entry_get_prev(s->tree, te);
6981 static const struct got_error *
6982 tree_scroll_down(struct tog_view *view, int maxscroll)
6984 struct tog_tree_view_state *s = &view->state.tree;
6985 struct got_tree_entry *next, *last;
6988 if (s->first_displayed_entry)
6989 next = got_tree_entry_get_next(s->tree,
6990 s->first_displayed_entry);
6992 next = got_object_tree_get_first_entry(s->tree);
6994 last = s->last_displayed_entry;
6995 while (next && n++ < maxscroll) {
6997 s->last_displayed_entry = last;
6998 last = got_tree_entry_get_next(s->tree, last);
7000 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7001 s->first_displayed_entry = next;
7002 next = got_tree_entry_get_next(s->tree, next);
7009 static const struct got_error *
7010 tree_entry_path(char **path, struct tog_parent_trees *parents,
7011 struct got_tree_entry *te)
7013 const struct got_error *err = NULL;
7014 struct tog_parent_tree *pt;
7015 size_t len = 2; /* for leading slash and NUL */
7017 TAILQ_FOREACH(pt, parents, entry)
7018 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7021 len += strlen(got_tree_entry_get_name(te));
7023 *path = calloc(1, len);
7025 return got_error_from_errno("calloc");
7028 pt = TAILQ_LAST(parents, tog_parent_trees);
7030 const char *name = got_tree_entry_get_name(pt->selected_entry);
7031 if (strlcat(*path, name, len) >= len) {
7032 err = got_error(GOT_ERR_NO_SPACE);
7035 if (strlcat(*path, "/", len) >= len) {
7036 err = got_error(GOT_ERR_NO_SPACE);
7039 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7042 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7043 err = got_error(GOT_ERR_NO_SPACE);
7055 static const struct got_error *
7056 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7057 struct got_tree_entry *te, struct tog_parent_trees *parents,
7058 struct got_object_id *commit_id, struct got_repository *repo)
7060 const struct got_error *err = NULL;
7062 struct tog_view *blame_view;
7066 err = tree_entry_path(&path, parents, te);
7070 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7071 if (blame_view == NULL) {
7072 err = got_error_from_errno("view_open");
7076 err = open_blame_view(blame_view, path, commit_id, repo);
7078 if (err->code == GOT_ERR_CANCELLED)
7080 view_close(blame_view);
7082 *new_view = blame_view;
7088 static const struct got_error *
7089 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7090 struct tog_tree_view_state *s)
7092 struct tog_view *log_view;
7093 const struct got_error *err = NULL;
7098 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7099 if (log_view == NULL)
7100 return got_error_from_errno("view_open");
7102 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7106 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7109 view_close(log_view);
7111 *new_view = log_view;
7116 static const struct got_error *
7117 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7118 const char *head_ref_name, struct got_repository *repo)
7120 const struct got_error *err = NULL;
7121 char *commit_id_str = NULL;
7122 struct tog_tree_view_state *s = &view->state.tree;
7123 struct got_commit_object *commit = NULL;
7125 TAILQ_INIT(&s->parents);
7126 STAILQ_INIT(&s->colors);
7128 s->commit_id = got_object_id_dup(commit_id);
7129 if (s->commit_id == NULL)
7130 return got_error_from_errno("got_object_id_dup");
7132 err = got_object_open_as_commit(&commit, repo, commit_id);
7137 * The root is opened here and will be closed when the view is closed.
7138 * Any visited subtrees and their path-wise parents are opened and
7141 err = got_object_open_as_tree(&s->root, repo,
7142 got_object_commit_get_tree_id(commit));
7147 err = got_object_id_str(&commit_id_str, commit_id);
7151 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7152 err = got_error_from_errno("asprintf");
7156 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7157 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7158 if (head_ref_name) {
7159 s->head_ref_name = strdup(head_ref_name);
7160 if (s->head_ref_name == NULL) {
7161 err = got_error_from_errno("strdup");
7167 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7168 err = add_color(&s->colors, "\\$$",
7169 TOG_COLOR_TREE_SUBMODULE,
7170 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7173 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7174 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7177 err = add_color(&s->colors, "/$",
7178 TOG_COLOR_TREE_DIRECTORY,
7179 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7183 err = add_color(&s->colors, "\\*$",
7184 TOG_COLOR_TREE_EXECUTABLE,
7185 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7189 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7190 get_color_value("TOG_COLOR_COMMIT"));
7195 view->show = show_tree_view;
7196 view->input = input_tree_view;
7197 view->close = close_tree_view;
7198 view->search_start = search_start_tree_view;
7199 view->search_next = search_next_tree_view;
7201 free(commit_id_str);
7203 got_object_commit_close(commit);
7205 close_tree_view(view);
7209 static const struct got_error *
7210 close_tree_view(struct tog_view *view)
7212 struct tog_tree_view_state *s = &view->state.tree;
7214 free_colors(&s->colors);
7215 free(s->tree_label);
7216 s->tree_label = NULL;
7218 s->commit_id = NULL;
7219 free(s->head_ref_name);
7220 s->head_ref_name = NULL;
7221 while (!TAILQ_EMPTY(&s->parents)) {
7222 struct tog_parent_tree *parent;
7223 parent = TAILQ_FIRST(&s->parents);
7224 TAILQ_REMOVE(&s->parents, parent, entry);
7225 if (parent->tree != s->root)
7226 got_object_tree_close(parent->tree);
7230 if (s->tree != NULL && s->tree != s->root)
7231 got_object_tree_close(s->tree);
7233 got_object_tree_close(s->root);
7237 static const struct got_error *
7238 search_start_tree_view(struct tog_view *view)
7240 struct tog_tree_view_state *s = &view->state.tree;
7242 s->matched_entry = NULL;
7247 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7249 regmatch_t regmatch;
7251 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7255 static const struct got_error *
7256 search_next_tree_view(struct tog_view *view)
7258 struct tog_tree_view_state *s = &view->state.tree;
7259 struct got_tree_entry *te = NULL;
7261 if (!view->searching) {
7262 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7266 if (s->matched_entry) {
7267 if (view->searching == TOG_SEARCH_FORWARD) {
7268 if (s->selected_entry)
7269 te = got_tree_entry_get_next(s->tree,
7272 te = got_object_tree_get_first_entry(s->tree);
7274 if (s->selected_entry == NULL)
7275 te = got_object_tree_get_last_entry(s->tree);
7277 te = got_tree_entry_get_prev(s->tree,
7281 if (s->selected_entry)
7282 te = s->selected_entry;
7283 else if (view->searching == TOG_SEARCH_FORWARD)
7284 te = got_object_tree_get_first_entry(s->tree);
7286 te = got_object_tree_get_last_entry(s->tree);
7291 if (s->matched_entry == NULL) {
7292 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7295 if (view->searching == TOG_SEARCH_FORWARD)
7296 te = got_object_tree_get_first_entry(s->tree);
7298 te = got_object_tree_get_last_entry(s->tree);
7301 if (match_tree_entry(te, &view->regex)) {
7302 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7303 s->matched_entry = te;
7307 if (view->searching == TOG_SEARCH_FORWARD)
7308 te = got_tree_entry_get_next(s->tree, te);
7310 te = got_tree_entry_get_prev(s->tree, te);
7313 if (s->matched_entry) {
7314 s->first_displayed_entry = s->matched_entry;
7321 static const struct got_error *
7322 show_tree_view(struct tog_view *view)
7324 const struct got_error *err = NULL;
7325 struct tog_tree_view_state *s = &view->state.tree;
7328 err = tree_entry_path(&parent_path, &s->parents, NULL);
7332 err = draw_tree_entries(view, parent_path);
7339 static const struct got_error *
7340 tree_goto_line(struct tog_view *view, int nlines)
7342 const struct got_error *err = NULL;
7343 struct tog_tree_view_state *s = &view->state.tree;
7344 struct got_tree_entry **fte, **lte, **ste;
7345 int g, last, first = 1, i = 1;
7346 int root = s->tree == s->root;
7347 int off = root ? 1 : 2;
7354 else if (g > got_object_tree_get_nentries(s->tree))
7355 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7357 fte = &s->first_displayed_entry;
7358 lte = &s->last_displayed_entry;
7359 ste = &s->selected_entry;
7362 first = got_tree_entry_get_index(*fte);
7363 first += off; /* account for ".." */
7365 last = got_tree_entry_get_index(*lte);
7368 if (g >= first && g <= last && g - first < nlines) {
7369 s->selected = g - first;
7370 return NULL; /* gline is on the current page */
7374 i = got_tree_entry_get_index(*ste);
7379 err = tree_scroll_down(view, g - i);
7382 if (got_tree_entry_get_index(*lte) >=
7383 got_object_tree_get_nentries(s->tree) - 1 &&
7384 first + s->selected < g &&
7385 s->selected < s->ndisplayed - 1) {
7386 first = got_tree_entry_get_index(*fte);
7388 s->selected = g - first;
7391 tree_scroll_up(s, i - g);
7394 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7395 s->selected = g - 1;
7400 static const struct got_error *
7401 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7403 const struct got_error *err = NULL;
7404 struct tog_tree_view_state *s = &view->state.tree;
7405 struct got_tree_entry *te;
7406 int n, nscroll = view->nlines - 3;
7409 return tree_goto_line(view, nscroll);
7418 horizontal_scroll_input(view, ch);
7421 s->show_ids = !s->show_ids;
7426 if (!s->selected_entry)
7428 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7432 err = view_request_new(new_view, view, TOG_VIEW_REF);
7439 if (s->tree == s->root)
7440 s->first_displayed_entry =
7441 got_object_tree_get_first_entry(s->tree);
7443 s->first_displayed_entry = NULL;
7448 int eos = view->nlines - 3;
7450 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7454 te = got_object_tree_get_last_entry(s->tree);
7455 for (n = 0; n < eos; n++) {
7457 if (s->tree != s->root) {
7458 s->first_displayed_entry = NULL;
7463 s->first_displayed_entry = te;
7464 te = got_tree_entry_get_prev(s->tree, te);
7467 s->selected = n - 1;
7473 if (s->selected > 0) {
7477 tree_scroll_up(s, 1);
7478 if (s->selected_entry == NULL ||
7479 (s->tree == s->root && s->selected_entry ==
7480 got_object_tree_get_first_entry(s->tree)))
7490 if (s->tree == s->root) {
7491 if (got_object_tree_get_first_entry(s->tree) ==
7492 s->first_displayed_entry)
7493 s->selected -= MIN(s->selected, nscroll);
7495 if (s->first_displayed_entry == NULL)
7496 s->selected -= MIN(s->selected, nscroll);
7498 tree_scroll_up(s, MAX(0, nscroll));
7499 if (s->selected_entry == NULL ||
7500 (s->tree == s->root && s->selected_entry ==
7501 got_object_tree_get_first_entry(s->tree)))
7507 if (s->selected < s->ndisplayed - 1) {
7511 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7513 /* can't scroll any further */
7517 tree_scroll_down(view, 1);
7527 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7529 /* can't scroll any further; move cursor down */
7530 if (s->selected < s->ndisplayed - 1)
7531 s->selected += MIN(nscroll,
7532 s->ndisplayed - s->selected - 1);
7537 tree_scroll_down(view, nscroll);
7542 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7543 struct tog_parent_tree *parent;
7544 /* user selected '..' */
7545 if (s->tree == s->root) {
7549 parent = TAILQ_FIRST(&s->parents);
7550 TAILQ_REMOVE(&s->parents, parent,
7552 got_object_tree_close(s->tree);
7553 s->tree = parent->tree;
7554 s->first_displayed_entry =
7555 parent->first_displayed_entry;
7557 parent->selected_entry;
7558 s->selected = parent->selected;
7559 if (s->selected > view->nlines - 3) {
7560 err = offset_selection_down(view);
7565 } else if (S_ISDIR(got_tree_entry_get_mode(
7566 s->selected_entry))) {
7567 struct got_tree_object *subtree;
7569 err = got_object_open_as_tree(&subtree, s->repo,
7570 got_tree_entry_get_id(s->selected_entry));
7573 err = tree_view_visit_subtree(s, subtree);
7575 got_object_tree_close(subtree);
7578 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7579 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7582 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7583 s->selected = view->nlines - 4;
7599 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7604 static const struct got_error *
7605 cmd_tree(int argc, char *argv[])
7607 const struct got_error *error;
7608 struct got_repository *repo = NULL;
7609 struct got_worktree *worktree = NULL;
7610 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7611 struct got_object_id *commit_id = NULL;
7612 struct got_commit_object *commit = NULL;
7613 const char *commit_id_arg = NULL;
7615 struct got_reference *ref = NULL;
7616 const char *head_ref_name = NULL;
7618 struct tog_view *view;
7619 int *pack_fds = NULL;
7621 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7624 commit_id_arg = optarg;
7627 repo_path = realpath(optarg, NULL);
7628 if (repo_path == NULL)
7629 return got_error_from_errno2("realpath",
7644 error = got_repo_pack_fds_open(&pack_fds);
7648 if (repo_path == NULL) {
7649 cwd = getcwd(NULL, 0);
7651 return got_error_from_errno("getcwd");
7652 error = got_worktree_open(&worktree, cwd);
7653 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7657 strdup(got_worktree_get_repo_path(worktree));
7659 repo_path = strdup(cwd);
7660 if (repo_path == NULL) {
7661 error = got_error_from_errno("strdup");
7666 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7670 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7677 error = apply_unveil(got_repo_get_path(repo), NULL);
7681 error = tog_load_refs(repo, 0);
7685 if (commit_id_arg == NULL) {
7686 error = got_repo_match_object_id(&commit_id, &label,
7687 worktree ? got_worktree_get_head_ref_name(worktree) :
7688 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7691 head_ref_name = label;
7693 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7695 head_ref_name = got_ref_get_name(ref);
7696 else if (error->code != GOT_ERR_NOT_REF)
7698 error = got_repo_match_object_id(&commit_id, NULL,
7699 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7704 error = got_object_open_as_commit(&commit, repo, commit_id);
7708 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7710 error = got_error_from_errno("view_open");
7713 error = open_tree_view(view, commit_id, head_ref_name, repo);
7716 if (!got_path_is_root_dir(in_repo_path)) {
7717 error = tree_view_walk_path(&view->state.tree, commit,
7724 /* Release work tree lock. */
7725 got_worktree_close(worktree);
7728 error = view_loop(view);
7737 const struct got_error *close_err = got_repo_close(repo);
7742 const struct got_error *pack_err =
7743 got_repo_pack_fds_close(pack_fds);
7751 static const struct got_error *
7752 ref_view_load_refs(struct tog_ref_view_state *s)
7754 struct got_reflist_entry *sre;
7755 struct tog_reflist_entry *re;
7758 TAILQ_FOREACH(sre, &tog_refs, entry) {
7759 if (strncmp(got_ref_get_name(sre->ref),
7760 "refs/got/", 9) == 0 &&
7761 strncmp(got_ref_get_name(sre->ref),
7762 "refs/got/backup/", 16) != 0)
7765 re = malloc(sizeof(*re));
7767 return got_error_from_errno("malloc");
7769 re->ref = got_ref_dup(sre->ref);
7770 if (re->ref == NULL)
7771 return got_error_from_errno("got_ref_dup");
7772 re->idx = s->nrefs++;
7773 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7776 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7781 ref_view_free_refs(struct tog_ref_view_state *s)
7783 struct tog_reflist_entry *re;
7785 while (!TAILQ_EMPTY(&s->refs)) {
7786 re = TAILQ_FIRST(&s->refs);
7787 TAILQ_REMOVE(&s->refs, re, entry);
7788 got_ref_close(re->ref);
7793 static const struct got_error *
7794 open_ref_view(struct tog_view *view, struct got_repository *repo)
7796 const struct got_error *err = NULL;
7797 struct tog_ref_view_state *s = &view->state.ref;
7799 s->selected_entry = 0;
7802 TAILQ_INIT(&s->refs);
7803 STAILQ_INIT(&s->colors);
7805 err = ref_view_load_refs(s);
7809 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7810 err = add_color(&s->colors, "^refs/heads/",
7811 TOG_COLOR_REFS_HEADS,
7812 get_color_value("TOG_COLOR_REFS_HEADS"));
7816 err = add_color(&s->colors, "^refs/tags/",
7817 TOG_COLOR_REFS_TAGS,
7818 get_color_value("TOG_COLOR_REFS_TAGS"));
7822 err = add_color(&s->colors, "^refs/remotes/",
7823 TOG_COLOR_REFS_REMOTES,
7824 get_color_value("TOG_COLOR_REFS_REMOTES"));
7828 err = add_color(&s->colors, "^refs/got/backup/",
7829 TOG_COLOR_REFS_BACKUP,
7830 get_color_value("TOG_COLOR_REFS_BACKUP"));
7835 view->show = show_ref_view;
7836 view->input = input_ref_view;
7837 view->close = close_ref_view;
7838 view->search_start = search_start_ref_view;
7839 view->search_next = search_next_ref_view;
7842 free_colors(&s->colors);
7846 static const struct got_error *
7847 close_ref_view(struct tog_view *view)
7849 struct tog_ref_view_state *s = &view->state.ref;
7851 ref_view_free_refs(s);
7852 free_colors(&s->colors);
7857 static const struct got_error *
7858 resolve_reflist_entry(struct got_object_id **commit_id,
7859 struct tog_reflist_entry *re, struct got_repository *repo)
7861 const struct got_error *err = NULL;
7862 struct got_object_id *obj_id;
7863 struct got_tag_object *tag = NULL;
7868 err = got_ref_resolve(&obj_id, repo, re->ref);
7872 err = got_object_get_type(&obj_type, repo, obj_id);
7877 case GOT_OBJ_TYPE_COMMIT:
7878 *commit_id = obj_id;
7880 case GOT_OBJ_TYPE_TAG:
7881 err = got_object_open_as_tag(&tag, repo, obj_id);
7885 err = got_object_get_type(&obj_type, repo,
7886 got_object_tag_get_object_id(tag));
7889 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7890 err = got_error(GOT_ERR_OBJ_TYPE);
7893 *commit_id = got_object_id_dup(
7894 got_object_tag_get_object_id(tag));
7895 if (*commit_id == NULL) {
7896 err = got_error_from_errno("got_object_id_dup");
7901 err = got_error(GOT_ERR_OBJ_TYPE);
7907 got_object_tag_close(tag);
7915 static const struct got_error *
7916 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7917 struct tog_reflist_entry *re, struct got_repository *repo)
7919 struct tog_view *log_view;
7920 const struct got_error *err = NULL;
7921 struct got_object_id *commit_id = NULL;
7925 err = resolve_reflist_entry(&commit_id, re, repo);
7927 if (err->code != GOT_ERR_OBJ_TYPE)
7933 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7934 if (log_view == NULL) {
7935 err = got_error_from_errno("view_open");
7939 err = open_log_view(log_view, commit_id, repo,
7940 got_ref_get_name(re->ref), "", 0);
7943 view_close(log_view);
7945 *new_view = log_view;
7951 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7953 struct tog_reflist_entry *re;
7956 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7959 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7960 while (i++ < maxscroll) {
7963 s->first_displayed_entry = re;
7964 re = TAILQ_PREV(re, tog_reflist_head, entry);
7968 static const struct got_error *
7969 ref_scroll_down(struct tog_view *view, int maxscroll)
7971 struct tog_ref_view_state *s = &view->state.ref;
7972 struct tog_reflist_entry *next, *last;
7975 if (s->first_displayed_entry)
7976 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7978 next = TAILQ_FIRST(&s->refs);
7980 last = s->last_displayed_entry;
7981 while (next && n++ < maxscroll) {
7983 s->last_displayed_entry = last;
7984 last = TAILQ_NEXT(last, entry);
7986 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7987 s->first_displayed_entry = next;
7988 next = TAILQ_NEXT(next, entry);
7995 static const struct got_error *
7996 search_start_ref_view(struct tog_view *view)
7998 struct tog_ref_view_state *s = &view->state.ref;
8000 s->matched_entry = NULL;
8005 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8007 regmatch_t regmatch;
8009 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8013 static const struct got_error *
8014 search_next_ref_view(struct tog_view *view)
8016 struct tog_ref_view_state *s = &view->state.ref;
8017 struct tog_reflist_entry *re = NULL;
8019 if (!view->searching) {
8020 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8024 if (s->matched_entry) {
8025 if (view->searching == TOG_SEARCH_FORWARD) {
8026 if (s->selected_entry)
8027 re = TAILQ_NEXT(s->selected_entry, entry);
8029 re = TAILQ_PREV(s->selected_entry,
8030 tog_reflist_head, entry);
8032 if (s->selected_entry == NULL)
8033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8035 re = TAILQ_PREV(s->selected_entry,
8036 tog_reflist_head, entry);
8039 if (s->selected_entry)
8040 re = s->selected_entry;
8041 else if (view->searching == TOG_SEARCH_FORWARD)
8042 re = TAILQ_FIRST(&s->refs);
8044 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8049 if (s->matched_entry == NULL) {
8050 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8053 if (view->searching == TOG_SEARCH_FORWARD)
8054 re = TAILQ_FIRST(&s->refs);
8056 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8059 if (match_reflist_entry(re, &view->regex)) {
8060 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8061 s->matched_entry = re;
8065 if (view->searching == TOG_SEARCH_FORWARD)
8066 re = TAILQ_NEXT(re, entry);
8068 re = TAILQ_PREV(re, tog_reflist_head, entry);
8071 if (s->matched_entry) {
8072 s->first_displayed_entry = s->matched_entry;
8079 static const struct got_error *
8080 show_ref_view(struct tog_view *view)
8082 const struct got_error *err = NULL;
8083 struct tog_ref_view_state *s = &view->state.ref;
8084 struct tog_reflist_entry *re;
8087 struct tog_color *tc;
8088 int width, n, scrollx;
8089 int limit = view->nlines;
8091 werase(view->window);
8094 if (view_is_hsplit_top(view))
8095 --limit; /* border */
8100 re = s->first_displayed_entry;
8102 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8104 return got_error_from_errno("asprintf");
8106 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8111 if (view_needs_focus_indication(view))
8112 wstandout(view->window);
8113 waddwstr(view->window, wline);
8114 while (width++ < view->ncols)
8115 waddch(view->window, ' ');
8116 if (view_needs_focus_indication(view))
8117 wstandend(view->window);
8127 while (re && limit > 0) {
8129 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8132 struct got_commit_object *ci;
8133 struct got_tag_object *tag;
8134 struct got_object_id *id;
8138 err = got_ref_resolve(&id, s->repo, re->ref);
8141 err = got_object_open_as_tag(&tag, s->repo, id);
8143 if (err->code != GOT_ERR_OBJ_TYPE) {
8147 err = got_object_open_as_commit(&ci, s->repo,
8153 t = got_object_commit_get_committer_time(ci);
8154 got_object_commit_close(ci);
8156 t = got_object_tag_get_tagger_time(tag);
8157 got_object_tag_close(tag);
8160 if (gmtime_r(&t, &tm) == NULL)
8161 return got_error_from_errno("gmtime_r");
8162 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8163 return got_error(GOT_ERR_NO_SPACE);
8165 if (got_ref_is_symbolic(re->ref)) {
8166 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8167 ymd : "", got_ref_get_name(re->ref),
8168 got_ref_get_symref_target(re->ref)) == -1)
8169 return got_error_from_errno("asprintf");
8170 } else if (s->show_ids) {
8171 struct got_object_id *id;
8173 err = got_ref_resolve(&id, s->repo, re->ref);
8176 err = got_object_id_str(&id_str, id);
8181 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8182 got_ref_get_name(re->ref), id_str) == -1) {
8183 err = got_error_from_errno("asprintf");
8190 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8191 got_ref_get_name(re->ref)) == -1)
8192 return got_error_from_errno("asprintf");
8194 /* use full line width to determine view->maxx */
8195 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8200 view->maxx = MAX(view->maxx, width);
8204 err = format_line(&wline, &width, &scrollx, line, view->x,
8210 if (n == s->selected) {
8212 wstandout(view->window);
8213 s->selected_entry = re;
8215 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8217 wattr_on(view->window,
8218 COLOR_PAIR(tc->colorpair), NULL);
8219 waddwstr(view->window, &wline[scrollx]);
8221 wattr_off(view->window,
8222 COLOR_PAIR(tc->colorpair), NULL);
8223 if (width < view->ncols)
8224 waddch(view->window, '\n');
8225 if (n == s->selected && view->focussed)
8226 wstandend(view->window);
8232 s->last_displayed_entry = re;
8235 re = TAILQ_NEXT(re, entry);
8242 static const struct got_error *
8243 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8244 struct tog_reflist_entry *re, struct got_repository *repo)
8246 const struct got_error *err = NULL;
8247 struct got_object_id *commit_id = NULL;
8248 struct tog_view *tree_view;
8252 err = resolve_reflist_entry(&commit_id, re, repo);
8254 if (err->code != GOT_ERR_OBJ_TYPE)
8261 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8262 if (tree_view == NULL) {
8263 err = got_error_from_errno("view_open");
8267 err = open_tree_view(tree_view, commit_id,
8268 got_ref_get_name(re->ref), repo);
8272 *new_view = tree_view;
8278 static const struct got_error *
8279 ref_goto_line(struct tog_view *view, int nlines)
8281 const struct got_error *err = NULL;
8282 struct tog_ref_view_state *s = &view->state.ref;
8283 int g, idx = s->selected_entry->idx;
8290 else if (g > s->nrefs)
8293 if (g >= s->first_displayed_entry->idx + 1 &&
8294 g <= s->last_displayed_entry->idx + 1 &&
8295 g - s->first_displayed_entry->idx - 1 < nlines) {
8296 s->selected = g - s->first_displayed_entry->idx - 1;
8301 err = ref_scroll_down(view, g - idx - 1);
8304 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8305 s->first_displayed_entry->idx + s->selected < g &&
8306 s->selected < s->ndisplayed - 1)
8307 s->selected = g - s->first_displayed_entry->idx - 1;
8308 } else if (idx + 1 > g)
8309 ref_scroll_up(s, idx - g + 1);
8311 if (g < nlines && s->first_displayed_entry->idx == 0)
8312 s->selected = g - 1;
8318 static const struct got_error *
8319 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8321 const struct got_error *err = NULL;
8322 struct tog_ref_view_state *s = &view->state.ref;
8323 struct tog_reflist_entry *re;
8324 int n, nscroll = view->nlines - 1;
8327 return ref_goto_line(view, nscroll);
8336 horizontal_scroll_input(view, ch);
8339 s->show_ids = !s->show_ids;
8343 s->show_date = !s->show_date;
8347 s->sort_by_date = !s->sort_by_date;
8348 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8350 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8351 got_ref_cmp_by_commit_timestamp_descending :
8352 tog_ref_cmp_by_name, s->repo);
8355 got_reflist_object_id_map_free(tog_refs_idmap);
8356 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8357 &tog_refs, s->repo);
8360 ref_view_free_refs(s);
8361 err = ref_view_load_refs(s);
8366 if (!s->selected_entry)
8368 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8372 if (!s->selected_entry)
8374 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8381 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8386 int eos = view->nlines - 1;
8388 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8392 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8393 for (n = 0; n < eos; n++) {
8396 s->first_displayed_entry = re;
8397 re = TAILQ_PREV(re, tog_reflist_head, entry);
8400 s->selected = n - 1;
8406 if (s->selected > 0) {
8410 ref_scroll_up(s, 1);
8411 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8421 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8422 s->selected -= MIN(nscroll, s->selected);
8423 ref_scroll_up(s, MAX(0, nscroll));
8424 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8430 if (s->selected < s->ndisplayed - 1) {
8434 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8435 /* can't scroll any further */
8439 ref_scroll_down(view, 1);
8449 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8450 /* can't scroll any further; move cursor down */
8451 if (s->selected < s->ndisplayed - 1)
8452 s->selected += MIN(nscroll,
8453 s->ndisplayed - s->selected - 1);
8454 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8455 s->selected += s->ndisplayed - s->selected - 1;
8459 ref_scroll_down(view, nscroll);
8464 err = tog_load_refs(s->repo, s->sort_by_date);
8467 ref_view_free_refs(s);
8468 err = ref_view_load_refs(s);
8471 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8472 s->selected = view->nlines - 2;
8486 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8491 static const struct got_error *
8492 cmd_ref(int argc, char *argv[])
8494 const struct got_error *error;
8495 struct got_repository *repo = NULL;
8496 struct got_worktree *worktree = NULL;
8497 char *cwd = NULL, *repo_path = NULL;
8499 struct tog_view *view;
8500 int *pack_fds = NULL;
8502 while ((ch = getopt(argc, argv, "r:")) != -1) {
8505 repo_path = realpath(optarg, NULL);
8506 if (repo_path == NULL)
8507 return got_error_from_errno2("realpath",
8522 error = got_repo_pack_fds_open(&pack_fds);
8526 if (repo_path == NULL) {
8527 cwd = getcwd(NULL, 0);
8529 return got_error_from_errno("getcwd");
8530 error = got_worktree_open(&worktree, cwd);
8531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8535 strdup(got_worktree_get_repo_path(worktree));
8537 repo_path = strdup(cwd);
8538 if (repo_path == NULL) {
8539 error = got_error_from_errno("strdup");
8544 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8550 error = apply_unveil(got_repo_get_path(repo), NULL);
8554 error = tog_load_refs(repo, 0);
8558 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8560 error = got_error_from_errno("view_open");
8564 error = open_ref_view(view, repo);
8569 /* Release work tree lock. */
8570 got_worktree_close(worktree);
8573 error = view_loop(view);
8578 const struct got_error *close_err = got_repo_close(repo);
8583 const struct got_error *pack_err =
8584 got_repo_pack_fds_close(pack_fds);
8592 static const struct got_error*
8593 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8602 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8606 if (mvwprintw(win, y, x, "%s", str) == ERR)
8607 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8614 static const struct got_error *
8615 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8619 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8621 free(*line_offsets);
8622 *line_offsets = NULL;
8623 return got_error_from_errno("reallocarray");
8627 (*line_offsets)[*nlines] = off;
8632 static const struct got_error *
8633 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8637 for (;n > 0; --n, ++km) {
8641 if (km->keys == NULL)
8644 t = t0 = strdup(km->keys);
8646 return got_error_from_errno("strdup");
8649 while ((k = strsep(&t, " ")) != NULL)
8650 len += strlen(k) > 1 ? 2 : 0;
8652 *ret = MAX(*ret, len);
8659 * Write keymap section headers, keys, and key info in km to f.
8660 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8661 * wrap control and symbolic keys in guillemets, else use <>.
8663 static const struct got_error *
8664 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8669 static const char *u8_glyph[] = {
8670 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8671 "\xe2\x80\xba" /* U+203A (utf8 >) */
8674 int cs, s, first = 1;
8676 cs = got_locale_is_utf8();
8678 t = t0 = strdup(km->keys);
8680 return got_error_from_errno("strdup");
8682 len = strlen(km->keys);
8683 while ((k = strsep(&t, " ")) != NULL) {
8684 s = strlen(k) > 1; /* control or symbolic key */
8685 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8686 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8687 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8690 return got_error_from_errno("fprintf");
8698 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8700 return got_error_from_errno("fprintf");
8706 static const struct got_error *
8707 format_help(struct tog_help_view_state *s)
8709 const struct got_error *err = NULL;
8711 int i, max, n, show = s->all;
8712 static const struct tog_key_map km[] = {
8713 #define KEYMAP_(info, type) { NULL, (info), type }
8714 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8720 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8725 err = max_key_str(&max, km, n);
8729 for (i = 0; i < n; ++i) {
8730 if (km[i].keys == NULL) {
8732 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8733 km[i].type == s->type || s->all)
8737 err = format_help_line(&off, s->f, &km[i], max);
8740 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8747 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8751 static const struct got_error *
8752 create_help(struct tog_help_view_state *s)
8755 const struct got_error *err;
8757 free(s->line_offsets);
8758 s->line_offsets = NULL;
8763 return got_error_from_errno("got_opentemp");
8766 err = format_help(s);
8770 if (s->f && fflush(s->f) != 0)
8771 return got_error_from_errno("fflush");
8776 static const struct got_error *
8777 search_start_help_view(struct tog_view *view)
8779 view->state.help.matched_line = 0;
8784 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8785 size_t *nlines, int **first, int **last, int **match, int **selected)
8787 struct tog_help_view_state *s = &view->state.help;
8790 *nlines = s->nlines;
8791 *line_offsets = s->line_offsets;
8792 *match = &s->matched_line;
8793 *first = &s->first_displayed_line;
8794 *last = &s->last_displayed_line;
8795 *selected = &s->selected_line;
8798 static const struct got_error *
8799 show_help_view(struct tog_view *view)
8801 struct tog_help_view_state *s = &view->state.help;
8802 const struct got_error *err;
8803 regmatch_t *regmatch = &view->regmatch;
8808 int width, nprinted = 0, rc = 0;
8809 int eos = view->nlines;
8811 if (view_is_hsplit_top(view))
8812 --eos; /* account for border */
8816 werase(view->window);
8818 if (view->gline > s->nlines - 1)
8819 view->gline = s->nlines - 1;
8821 err = win_draw_center(view->window, 0, 0, view->ncols,
8822 view_needs_focus_indication(view),
8823 "tog help (press q to return to tog)");
8828 waddstr(view->window, "\n\n");
8834 while (eos > 0 && nprinted < eos) {
8837 linelen = getline(&line, &linesz, s->f);
8838 if (linelen == -1) {
8841 return got_ferror(s->f, GOT_ERR_IO);
8846 if (++s->lineno < s->first_displayed_line)
8848 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8850 if (s->lineno == view->hiline)
8853 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8859 view->maxx = MAX(view->maxx, width);
8864 wattron(view->window, attr);
8865 if (s->first_displayed_line + nprinted == s->matched_line &&
8866 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8867 err = add_matched_line(&width, line, view->ncols - 1, 0,
8868 view->window, view->x, regmatch);
8876 err = format_line(&wline, &width, &skip, line,
8877 view->x, view->ncols, 0, view->x ? 1 : 0);
8882 waddwstr(view->window, &wline[skip]);
8886 if (s->lineno == view->hiline) {
8887 while (width++ < view->ncols)
8888 waddch(view->window, ' ');
8890 if (width < view->ncols)
8891 waddch(view->window, '\n');
8894 wattroff(view->window, attr);
8895 if (++nprinted == 1)
8896 s->first_displayed_line = s->lineno;
8900 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8902 s->last_displayed_line = s->first_displayed_line;
8907 rc = waddnstr(view->window,
8908 "See the tog(1) manual page for full documentation",
8911 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8913 wmove(view->window, view->nlines - 1, 0);
8914 wclrtoeol(view->window);
8915 wstandout(view->window);
8916 rc = waddnstr(view->window, "scroll down for more...",
8919 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8920 if (getcurx(view->window) < view->ncols - 6) {
8921 rc = wprintw(view->window, "[%.0f%%]",
8922 100.00 * s->last_displayed_line / s->nlines);
8924 return got_error_msg(GOT_ERR_IO, "wprintw");
8926 wstandend(view->window);
8932 static const struct got_error *
8933 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8935 struct tog_help_view_state *s = &view->state.help;
8936 const struct got_error *err = NULL;
8942 eos = nscroll = view->nlines;
8943 if (view_is_hsplit_top(view))
8946 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8955 horizontal_scroll_input(view, ch);
8959 s->first_displayed_line = 1;
8967 s->first_displayed_line = (s->nlines - eos) + 3;
8972 if (s->first_displayed_line > 1)
8973 --s->first_displayed_line;
8984 if (s->first_displayed_line == 1) {
8988 while (--nscroll > 0 && s->first_displayed_line > 1)
8989 s->first_displayed_line--;
8995 ++s->first_displayed_line;
9011 while (!s->eof && --nscroll > 0) {
9012 linelen = getline(&line, &linesz, s->f);
9013 s->first_displayed_line++;
9014 if (linelen == -1) {
9018 err = got_ferror(s->f, GOT_ERR_IO);
9032 static const struct got_error *
9033 close_help_view(struct tog_view *view)
9035 struct tog_help_view_state *s = &view->state.help;
9037 free(s->line_offsets);
9038 s->line_offsets = NULL;
9039 if (fclose(s->f) == EOF)
9040 return got_error_from_errno("fclose");
9045 static const struct got_error *
9046 reset_help_view(struct tog_view *view)
9048 struct tog_help_view_state *s = &view->state.help;
9051 if (s->f && fclose(s->f) == EOF)
9052 return got_error_from_errno("fclose");
9054 wclear(view->window);
9058 s->first_displayed_line = 1;
9059 s->last_displayed_line = view->nlines;
9060 s->matched_line = 0;
9062 return create_help(s);
9065 static const struct got_error *
9066 open_help_view(struct tog_view *view, struct tog_view *parent)
9068 const struct got_error *err = NULL;
9069 struct tog_help_view_state *s = &view->state.help;
9071 s->type = (enum tog_keymap_type)parent->type;
9072 s->first_displayed_line = 1;
9073 s->last_displayed_line = view->nlines;
9074 s->selected_line = 1;
9076 view->show = show_help_view;
9077 view->input = input_help_view;
9078 view->reset = reset_help_view;
9079 view->close = close_help_view;
9080 view->search_start = search_start_help_view;
9081 view->search_setup = search_setup_help_view;
9082 view->search_next = search_next_view_match;
9084 err = create_help(s);
9088 static const struct got_error *
9089 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9090 enum tog_view_type request, int y, int x)
9092 const struct got_error *err = NULL;
9098 if (view->type == TOG_VIEW_LOG) {
9099 struct tog_log_view_state *s = &view->state.log;
9101 err = open_diff_view_for_commit(new_view, y, x,
9102 s->selected_entry->commit, s->selected_entry->id,
9105 return got_error_msg(GOT_ERR_NOT_IMPL,
9106 "parent/child view pair not supported");
9108 case TOG_VIEW_BLAME:
9109 if (view->type == TOG_VIEW_TREE) {
9110 struct tog_tree_view_state *s = &view->state.tree;
9112 err = blame_tree_entry(new_view, y, x,
9113 s->selected_entry, &s->parents, s->commit_id,
9116 return got_error_msg(GOT_ERR_NOT_IMPL,
9117 "parent/child view pair not supported");
9120 if (view->type == TOG_VIEW_BLAME)
9121 err = log_annotated_line(new_view, y, x,
9122 view->state.blame.repo, view->state.blame.id_to_log);
9123 else if (view->type == TOG_VIEW_TREE)
9124 err = log_selected_tree_entry(new_view, y, x,
9126 else if (view->type == TOG_VIEW_REF)
9127 err = log_ref_entry(new_view, y, x,
9128 view->state.ref.selected_entry,
9129 view->state.ref.repo);
9131 return got_error_msg(GOT_ERR_NOT_IMPL,
9132 "parent/child view pair not supported");
9135 if (view->type == TOG_VIEW_LOG)
9136 err = browse_commit_tree(new_view, y, x,
9137 view->state.log.selected_entry,
9138 view->state.log.in_repo_path,
9139 view->state.log.head_ref_name,
9140 view->state.log.repo);
9141 else if (view->type == TOG_VIEW_REF)
9142 err = browse_ref_tree(new_view, y, x,
9143 view->state.ref.selected_entry,
9144 view->state.ref.repo);
9146 return got_error_msg(GOT_ERR_NOT_IMPL,
9147 "parent/child view pair not supported");
9150 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9151 if (*new_view == NULL)
9152 return got_error_from_errno("view_open");
9153 if (view->type == TOG_VIEW_LOG)
9154 err = open_ref_view(*new_view, view->state.log.repo);
9155 else if (view->type == TOG_VIEW_TREE)
9156 err = open_ref_view(*new_view, view->state.tree.repo);
9158 err = got_error_msg(GOT_ERR_NOT_IMPL,
9159 "parent/child view pair not supported");
9161 view_close(*new_view);
9164 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9165 if (*new_view == NULL)
9166 return got_error_from_errno("view_open");
9167 err = open_help_view(*new_view, view);
9169 view_close(*new_view);
9172 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9179 * If view was scrolled down to move the selected line into view when opening a
9180 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9183 offset_selection_up(struct tog_view *view)
9185 switch (view->type) {
9186 case TOG_VIEW_BLAME: {
9187 struct tog_blame_view_state *s = &view->state.blame;
9188 if (s->first_displayed_line == 1) {
9189 s->selected_line = MAX(s->selected_line - view->offset,
9193 if (s->first_displayed_line > view->offset)
9194 s->first_displayed_line -= view->offset;
9196 s->first_displayed_line = 1;
9197 s->selected_line += view->offset;
9201 log_scroll_up(&view->state.log, view->offset);
9202 view->state.log.selected += view->offset;
9205 ref_scroll_up(&view->state.ref, view->offset);
9206 view->state.ref.selected += view->offset;
9209 tree_scroll_up(&view->state.tree, view->offset);
9210 view->state.tree.selected += view->offset;
9220 * If the selected line is in the section of screen covered by the bottom split,
9221 * scroll down offset lines to move it into view and index its new position.
9223 static const struct got_error *
9224 offset_selection_down(struct tog_view *view)
9226 const struct got_error *err = NULL;
9227 const struct got_error *(*scrolld)(struct tog_view *, int);
9228 int *selected = NULL;
9231 switch (view->type) {
9232 case TOG_VIEW_BLAME: {
9233 struct tog_blame_view_state *s = &view->state.blame;
9236 if (s->selected_line > view->nlines - header) {
9237 offset = abs(view->nlines - s->selected_line - header);
9238 s->first_displayed_line += offset;
9239 s->selected_line -= offset;
9240 view->offset = offset;
9244 case TOG_VIEW_LOG: {
9245 struct tog_log_view_state *s = &view->state.log;
9246 scrolld = &log_scroll_down;
9247 header = view_is_parent_view(view) ? 3 : 2;
9248 selected = &s->selected;
9251 case TOG_VIEW_REF: {
9252 struct tog_ref_view_state *s = &view->state.ref;
9253 scrolld = &ref_scroll_down;
9255 selected = &s->selected;
9258 case TOG_VIEW_TREE: {
9259 struct tog_tree_view_state *s = &view->state.tree;
9260 scrolld = &tree_scroll_down;
9262 selected = &s->selected;
9272 if (selected && *selected > view->nlines - header) {
9273 offset = abs(view->nlines - *selected - header);
9274 view->offset = offset;
9275 if (scrolld && offset) {
9276 err = scrolld(view, offset);
9277 *selected -= offset;
9285 list_commands(FILE *fp)
9289 fprintf(fp, "commands:");
9290 for (i = 0; i < nitems(tog_commands); i++) {
9291 const struct tog_cmd *cmd = &tog_commands[i];
9292 fprintf(fp, " %s", cmd->name);
9298 usage(int hflag, int status)
9300 FILE *fp = (status == 0) ? stdout : stderr;
9302 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9305 fprintf(fp, "lazy usage: %s path\n", getprogname());
9312 make_argv(int argc, ...)
9320 argv = calloc(argc, sizeof(char *));
9323 for (i = 0; i < argc; i++) {
9324 argv[i] = strdup(va_arg(ap, char *));
9325 if (argv[i] == NULL)
9334 * Try to convert 'tog path' into a 'tog log path' command.
9335 * The user could simply have mistyped the command rather than knowingly
9336 * provided a path. So check whether argv[0] can in fact be resolved
9337 * to a path in the HEAD commit and print a special error if not.
9338 * This hack is for mpi@ <3
9340 static const struct got_error *
9341 tog_log_with_path(int argc, char *argv[])
9343 const struct got_error *error = NULL, *close_err;
9344 const struct tog_cmd *cmd = NULL;
9345 struct got_repository *repo = NULL;
9346 struct got_worktree *worktree = NULL;
9347 struct got_object_id *commit_id = NULL, *id = NULL;
9348 struct got_commit_object *commit = NULL;
9349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9350 char *commit_id_str = NULL, **cmd_argv = NULL;
9351 int *pack_fds = NULL;
9353 cwd = getcwd(NULL, 0);
9355 return got_error_from_errno("getcwd");
9357 error = got_repo_pack_fds_open(&pack_fds);
9361 error = got_worktree_open(&worktree, cwd);
9362 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9366 repo_path = strdup(got_worktree_get_repo_path(worktree));
9368 repo_path = strdup(cwd);
9369 if (repo_path == NULL) {
9370 error = got_error_from_errno("strdup");
9374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9378 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9383 error = tog_load_refs(repo, 0);
9386 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9387 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9388 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9393 got_worktree_close(worktree);
9397 error = got_object_open_as_commit(&commit, repo, commit_id);
9401 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9403 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9405 fprintf(stderr, "%s: '%s' is no known command or path\n",
9406 getprogname(), argv[0]);
9411 error = got_object_id_str(&commit_id_str, commit_id);
9415 cmd = &tog_commands[0]; /* log */
9417 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9418 error = cmd->cmd_main(argc, cmd_argv);
9421 close_err = got_repo_close(repo);
9426 got_object_commit_close(commit);
9428 got_worktree_close(worktree);
9430 const struct got_error *pack_err =
9431 got_repo_pack_fds_close(pack_fds);
9436 free(commit_id_str);
9443 for (i = 0; i < argc; i++)
9452 main(int argc, char *argv[])
9454 const struct got_error *error = NULL;
9455 const struct tog_cmd *cmd = NULL;
9456 int ch, hflag = 0, Vflag = 0;
9457 char **cmd_argv = NULL;
9458 static const struct option longopts[] = {
9459 { "version", no_argument, NULL, 'V' },
9462 char *diff_algo_str = NULL;
9464 setlocale(LC_CTYPE, "");
9467 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9472 if (!isatty(STDIN_FILENO))
9473 errx(1, "standard input is not a tty");
9475 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9495 got_version_print_str();
9502 /* Build an argument vector which runs a default command. */
9503 cmd = &tog_commands[0];
9505 cmd_argv = make_argv(argc, cmd->name);
9509 /* Did the user specify a command? */
9510 for (i = 0; i < nitems(tog_commands); i++) {
9511 if (strncmp(tog_commands[i].name, argv[0],
9512 strlen(argv[0])) == 0) {
9513 cmd = &tog_commands[i];
9519 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9520 if (diff_algo_str) {
9521 if (strcasecmp(diff_algo_str, "patience") == 0)
9522 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9523 if (strcasecmp(diff_algo_str, "myers") == 0)
9524 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9530 /* No command specified; try log with a path */
9531 error = tog_log_with_path(argc, argv);
9536 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9542 for (i = 0; i < argc; i++)
9547 if (error && error->code != GOT_ERR_CANCELLED &&
9548 error->code != GOT_ERR_EOF &&
9549 error->code != GOT_ERR_PRIVSEP_EXIT &&
9550 error->code != GOT_ERR_PRIVSEP_PIPE &&
9551 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9552 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);