2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
46 #include "got_compat.h"
48 #include "got_version.h"
49 #include "got_error.h"
50 #include "got_object.h"
51 #include "got_reference.h"
52 #include "got_repository.h"
54 #include "got_opentemp.h"
56 #include "got_cancel.h"
57 #include "got_commit_graph.h"
58 #include "got_blame.h"
59 #include "got_privsep.h"
61 #include "got_worktree.h"
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 #define CTRL(x) ((x) & 0x1f)
76 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
85 __dead static void usage(int, int);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_ref(void);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct tog_cmd tog_commands[] = {
99 { "log", cmd_log, usage_log },
100 { "diff", cmd_diff, usage_diff },
101 { "blame", cmd_blame, usage_blame },
102 { "tree", cmd_tree, usage_tree },
103 { "ref", cmd_ref, usage_ref },
115 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
116 enum tog_keymap_type {
117 TOG_KEYMAP_KEYS = -2,
133 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
135 #define TOG_EOF_STRING "(END)"
137 struct commit_queue_entry {
138 TAILQ_ENTRY(commit_queue_entry) entry;
139 struct got_object_id *id;
140 struct got_commit_object *commit;
143 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
144 struct commit_queue {
146 struct commit_queue_head head;
150 STAILQ_ENTRY(tog_color) entry;
154 STAILQ_HEAD(tog_colors, tog_color);
156 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
157 static struct got_reflist_object_id_map *tog_refs_idmap;
158 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
160 static const struct got_error *
161 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
162 struct got_reference* re2)
164 const char *name1 = got_ref_get_name(re1);
165 const char *name2 = got_ref_get_name(re2);
166 int isbackup1, isbackup2;
168 /* Sort backup refs towards the bottom of the list. */
169 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
170 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
171 if (!isbackup1 && isbackup2) {
174 } else if (isbackup1 && !isbackup2) {
179 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
183 static const struct got_error *
184 tog_load_refs(struct got_repository *repo, int sort_by_date)
186 const struct got_error *err;
188 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
189 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
194 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
201 if (tog_refs_idmap) {
202 got_reflist_object_id_map_free(tog_refs_idmap);
203 tog_refs_idmap = NULL;
205 got_ref_list_free(&tog_refs);
208 static const struct got_error *
209 add_color(struct tog_colors *colors, const char *pattern,
210 int idx, short color)
212 const struct got_error *err = NULL;
213 struct tog_color *tc;
216 if (idx < 1 || idx > COLOR_PAIRS - 1)
219 init_pair(idx, color, -1);
221 tc = calloc(1, sizeof(*tc));
223 return got_error_from_errno("calloc");
224 regerr = regcomp(&tc->regex, pattern,
225 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
227 static char regerr_msg[512];
228 static char err_msg[512];
229 regerror(regerr, &tc->regex, regerr_msg,
231 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
233 err = got_error_msg(GOT_ERR_REGEX, err_msg);
238 STAILQ_INSERT_HEAD(colors, tc, entry);
243 free_colors(struct tog_colors *colors)
245 struct tog_color *tc;
247 while (!STAILQ_EMPTY(colors)) {
248 tc = STAILQ_FIRST(colors);
249 STAILQ_REMOVE_HEAD(colors, entry);
255 static struct tog_color *
256 get_color(struct tog_colors *colors, int colorpair)
258 struct tog_color *tc = NULL;
260 STAILQ_FOREACH(tc, colors, entry) {
261 if (tc->colorpair == colorpair)
269 default_color_value(const char *envvar)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
272 return COLOR_MAGENTA;
273 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
275 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
279 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
280 return COLOR_MAGENTA;
281 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
285 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
287 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
289 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
291 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
293 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
295 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
296 return COLOR_MAGENTA;
297 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
299 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
306 get_color_value(const char *envvar)
308 const char *val = getenv(envvar);
311 return default_color_value(envvar);
313 if (strcasecmp(val, "black") == 0)
315 if (strcasecmp(val, "red") == 0)
317 if (strcasecmp(val, "green") == 0)
319 if (strcasecmp(val, "yellow") == 0)
321 if (strcasecmp(val, "blue") == 0)
323 if (strcasecmp(val, "magenta") == 0)
324 return COLOR_MAGENTA;
325 if (strcasecmp(val, "cyan") == 0)
327 if (strcasecmp(val, "white") == 0)
329 if (strcasecmp(val, "default") == 0)
332 return default_color_value(envvar);
335 struct tog_diff_view_state {
336 struct got_object_id *id1, *id2;
337 const char *label1, *label2;
341 int first_displayed_line;
342 int last_displayed_line;
345 int ignore_whitespace;
347 struct got_repository *repo;
348 struct got_diff_line *lines;
353 /* passed from log or blame view; may be NULL */
354 struct tog_view *parent_view;
357 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
358 static volatile sig_atomic_t tog_thread_error;
360 struct tog_log_thread_args {
361 pthread_cond_t need_commits;
362 pthread_cond_t commit_loaded;
365 struct got_commit_graph *graph;
366 struct commit_queue *real_commits;
367 const char *in_repo_path;
368 struct got_object_id *start_id;
369 struct got_repository *repo;
373 struct commit_queue_entry **first_displayed_entry;
374 struct commit_queue_entry **selected_entry;
376 int *search_next_done;
380 regex_t *limit_regex;
381 struct commit_queue *limit_commits;
384 struct tog_log_view_state {
385 struct commit_queue *commits;
386 struct commit_queue_entry *first_displayed_entry;
387 struct commit_queue_entry *last_displayed_entry;
388 struct commit_queue_entry *selected_entry;
389 struct commit_queue real_commits;
394 struct got_repository *repo;
395 struct got_object_id *start_id;
398 struct tog_log_thread_args thread_args;
399 struct commit_queue_entry *matched_entry;
400 struct commit_queue_entry *search_entry;
401 struct tog_colors colors;
405 struct commit_queue limit_commits;
408 #define TOG_COLOR_DIFF_MINUS 1
409 #define TOG_COLOR_DIFF_PLUS 2
410 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
411 #define TOG_COLOR_DIFF_META 4
412 #define TOG_COLOR_TREE_SUBMODULE 5
413 #define TOG_COLOR_TREE_SYMLINK 6
414 #define TOG_COLOR_TREE_DIRECTORY 7
415 #define TOG_COLOR_TREE_EXECUTABLE 8
416 #define TOG_COLOR_COMMIT 9
417 #define TOG_COLOR_AUTHOR 10
418 #define TOG_COLOR_DATE 11
419 #define TOG_COLOR_REFS_HEADS 12
420 #define TOG_COLOR_REFS_TAGS 13
421 #define TOG_COLOR_REFS_REMOTES 14
422 #define TOG_COLOR_REFS_BACKUP 15
424 struct tog_blame_cb_args {
425 struct tog_blame_line *lines; /* one per line */
428 struct tog_view *view;
429 struct got_object_id *commit_id;
433 struct tog_blame_thread_args {
435 struct got_repository *repo;
436 struct tog_blame_cb_args *cb_args;
438 got_cancel_cb cancel_cb;
445 struct tog_blame_line *lines;
449 struct tog_blame_thread_args thread_args;
450 struct tog_blame_cb_args cb_args;
455 struct tog_blame_view_state {
456 int first_displayed_line;
457 int last_displayed_line;
459 int last_diffed_line;
463 struct got_object_id_queue blamed_commits;
464 struct got_object_qid *blamed_commit;
466 struct got_repository *repo;
467 struct got_object_id *commit_id;
468 struct got_object_id *id_to_log;
469 struct tog_blame blame;
471 struct tog_colors colors;
474 struct tog_parent_tree {
475 TAILQ_ENTRY(tog_parent_tree) entry;
476 struct got_tree_object *tree;
477 struct got_tree_entry *first_displayed_entry;
478 struct got_tree_entry *selected_entry;
482 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
484 struct tog_tree_view_state {
486 struct got_object_id *commit_id;/* commit which this tree belongs to */
487 struct got_tree_object *root; /* the commit's root tree entry */
488 struct got_tree_object *tree; /* currently displayed (sub-)tree */
489 struct got_tree_entry *first_displayed_entry;
490 struct got_tree_entry *last_displayed_entry;
491 struct got_tree_entry *selected_entry;
492 int ndisplayed, selected, show_ids;
493 struct tog_parent_trees parents; /* parent trees of current sub-tree */
495 struct got_repository *repo;
496 struct got_tree_entry *matched_entry;
497 struct tog_colors colors;
500 struct tog_reflist_entry {
501 TAILQ_ENTRY(tog_reflist_entry) entry;
502 struct got_reference *ref;
506 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
508 struct tog_ref_view_state {
509 struct tog_reflist_head refs;
510 struct tog_reflist_entry *first_displayed_entry;
511 struct tog_reflist_entry *last_displayed_entry;
512 struct tog_reflist_entry *selected_entry;
513 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
514 struct got_repository *repo;
515 struct tog_reflist_entry *matched_entry;
516 struct tog_colors colors;
519 struct tog_help_view_state {
524 int first_displayed_line;
525 int last_displayed_line;
530 enum tog_keymap_type type;
533 #define GENERATE_HELP \
534 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
535 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
536 KEY_("k C-p Up", "Move cursor or page up one line"), \
537 KEY_("j C-n Down", "Move cursor or page down one line"), \
538 KEY_("C-b b PgUp", "Scroll the view up one page"), \
539 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
540 KEY_("C-u u", "Scroll the view up one half page"), \
541 KEY_("C-d d", "Scroll the view down one half page"), \
542 KEY_("g", "Go to line N (default: first line)"), \
543 KEY_("Home =", "Go to the first line"), \
544 KEY_("G", "Go to line N (default: last line)"), \
545 KEY_("End *", "Go to the last line"), \
546 KEY_("l Right", "Scroll the view right"), \
547 KEY_("h Left", "Scroll the view left"), \
548 KEY_("$", "Scroll view to the rightmost position"), \
549 KEY_("0", "Scroll view to the leftmost position"), \
550 KEY_("-", "Decrease size of the focussed split"), \
551 KEY_("+", "Increase size of the focussed split"), \
552 KEY_("Tab", "Switch focus between views"), \
553 KEY_("F", "Toggle fullscreen mode"), \
554 KEY_("/", "Open prompt to enter search term"), \
555 KEY_("n", "Find next line/token matching the current search term"), \
556 KEY_("N", "Find previous line/token matching the current search term"),\
557 KEY_("q", "Quit the focussed view; Quit help screen"), \
558 KEY_("Q", "Quit tog"), \
560 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
561 KEY_("< ,", "Move cursor up one commit"), \
562 KEY_("> .", "Move cursor down one commit"), \
563 KEY_("Enter", "Open diff view of the selected commit"), \
564 KEY_("B", "Reload the log view and toggle display of merged commits"), \
565 KEY_("R", "Open ref view of all repository references"), \
566 KEY_("T", "Display tree view of the repository from the selected" \
568 KEY_("@", "Toggle between displaying author and committer name"), \
569 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
570 KEY_("C-g Backspace", "Cancel current search or log operation"), \
571 KEY_("C-l", "Reload the log view with new commits in the repository"), \
573 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
574 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
575 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
576 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
577 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
579 KEY_("(", "Go to the previous file in the diff"), \
580 KEY_(")", "Go to the next file in the diff"), \
581 KEY_("{", "Go to the previous hunk in the diff"), \
582 KEY_("}", "Go to the next hunk in the diff"), \
583 KEY_("[", "Decrease the number of context lines"), \
584 KEY_("]", "Increase the number of context lines"), \
585 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
587 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
588 KEY_("Enter", "Display diff view of the selected line's commit"), \
589 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
590 KEY_("L", "Open log view for the currently selected annotated line"), \
591 KEY_("C", "Reload view with the previously blamed commit"), \
592 KEY_("c", "Reload view with the version of the file found in the" \
593 " selected line's commit"), \
594 KEY_("p", "Reload view with the version of the file found in the" \
595 " selected line's parent commit"), \
597 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
598 KEY_("Enter", "Enter selected directory or open blame view of the" \
600 KEY_("L", "Open log view for the selected entry"), \
601 KEY_("R", "Open ref view of all repository references"), \
602 KEY_("i", "Show object IDs for all tree entries"), \
603 KEY_("Backspace", "Return to the parent directory"), \
605 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
606 KEY_("Enter", "Display log view of the selected reference"), \
607 KEY_("T", "Display tree view of the selected reference"), \
608 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
609 KEY_("m", "Toggle display of last modified date for each reference"), \
610 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
611 KEY_("C-l", "Reload view with all repository references")
616 enum tog_keymap_type type;
620 * We implement two types of views: parent views and child views.
622 * The 'Tab' key switches focus between a parent view and its child view.
623 * Child views are shown side-by-side to their parent view, provided
624 * there is enough screen estate.
626 * When a new view is opened from within a parent view, this new view
627 * becomes a child view of the parent view, replacing any existing child.
629 * When a new view is opened from within a child view, this new view
630 * becomes a parent view which will obscure the views below until the
631 * user quits the new parent view by typing 'q'.
633 * This list of views contains parent views only.
634 * Child views are only pointed to by their parent view.
636 TAILQ_HEAD(tog_view_list_head, tog_view);
639 TAILQ_ENTRY(tog_view) entry;
642 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
643 int resized_y, resized_x; /* begin_y/x based on user resizing */
644 int maxx, x; /* max column and current start column */
645 int lines, cols; /* copies of LINES and COLS */
646 int nscrolled, offset; /* lines scrolled and hsplit line offset */
647 int gline, hiline; /* navigate to and highlight this nG line */
648 int ch, count; /* current keymap and count prefix */
649 int resized; /* set when in a resize event */
650 int focussed; /* Only set on one parent or child view at a time. */
652 struct tog_view *parent;
653 struct tog_view *child;
656 * This flag is initially set on parent views when a new child view
657 * is created. It gets toggled when the 'Tab' key switches focus
658 * between parent and child.
659 * The flag indicates whether focus should be passed on to our child
660 * view if this parent view gets picked for focus after another parent
661 * view was closed. This prevents child views from losing focus in such
666 enum tog_view_mode mode;
667 /* type-specific state */
668 enum tog_view_type type;
670 struct tog_diff_view_state diff;
671 struct tog_log_view_state log;
672 struct tog_blame_view_state blame;
673 struct tog_tree_view_state tree;
674 struct tog_ref_view_state ref;
675 struct tog_help_view_state help;
678 const struct got_error *(*show)(struct tog_view *);
679 const struct got_error *(*input)(struct tog_view **,
680 struct tog_view *, int);
681 const struct got_error *(*reset)(struct tog_view *);
682 const struct got_error *(*resize)(struct tog_view *, int);
683 const struct got_error *(*close)(struct tog_view *);
685 const struct got_error *(*search_start)(struct tog_view *);
686 const struct got_error *(*search_next)(struct tog_view *);
687 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
688 int **, int **, int **, int **);
691 #define TOG_SEARCH_FORWARD 1
692 #define TOG_SEARCH_BACKWARD 2
693 int search_next_done;
694 #define TOG_SEARCH_HAVE_MORE 1
695 #define TOG_SEARCH_NO_MORE 2
696 #define TOG_SEARCH_HAVE_NONE 3
702 static const struct got_error *open_diff_view(struct tog_view *,
703 struct got_object_id *, struct got_object_id *,
704 const char *, const char *, int, int, int, struct tog_view *,
705 struct got_repository *);
706 static const struct got_error *show_diff_view(struct tog_view *);
707 static const struct got_error *input_diff_view(struct tog_view **,
708 struct tog_view *, int);
709 static const struct got_error *reset_diff_view(struct tog_view *);
710 static const struct got_error* close_diff_view(struct tog_view *);
711 static const struct got_error *search_start_diff_view(struct tog_view *);
712 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
713 size_t *, int **, int **, int **, int **);
714 static const struct got_error *search_next_view_match(struct tog_view *);
716 static const struct got_error *open_log_view(struct tog_view *,
717 struct got_object_id *, struct got_repository *,
718 const char *, const char *, int);
719 static const struct got_error * show_log_view(struct tog_view *);
720 static const struct got_error *input_log_view(struct tog_view **,
721 struct tog_view *, int);
722 static const struct got_error *resize_log_view(struct tog_view *, int);
723 static const struct got_error *close_log_view(struct tog_view *);
724 static const struct got_error *search_start_log_view(struct tog_view *);
725 static const struct got_error *search_next_log_view(struct tog_view *);
727 static const struct got_error *open_blame_view(struct tog_view *, char *,
728 struct got_object_id *, struct got_repository *);
729 static const struct got_error *show_blame_view(struct tog_view *);
730 static const struct got_error *input_blame_view(struct tog_view **,
731 struct tog_view *, int);
732 static const struct got_error *reset_blame_view(struct tog_view *);
733 static const struct got_error *close_blame_view(struct tog_view *);
734 static const struct got_error *search_start_blame_view(struct tog_view *);
735 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
736 size_t *, int **, int **, int **, int **);
738 static const struct got_error *open_tree_view(struct tog_view *,
739 struct got_object_id *, const char *, struct got_repository *);
740 static const struct got_error *show_tree_view(struct tog_view *);
741 static const struct got_error *input_tree_view(struct tog_view **,
742 struct tog_view *, int);
743 static const struct got_error *close_tree_view(struct tog_view *);
744 static const struct got_error *search_start_tree_view(struct tog_view *);
745 static const struct got_error *search_next_tree_view(struct tog_view *);
747 static const struct got_error *open_ref_view(struct tog_view *,
748 struct got_repository *);
749 static const struct got_error *show_ref_view(struct tog_view *);
750 static const struct got_error *input_ref_view(struct tog_view **,
751 struct tog_view *, int);
752 static const struct got_error *close_ref_view(struct tog_view *);
753 static const struct got_error *search_start_ref_view(struct tog_view *);
754 static const struct got_error *search_next_ref_view(struct tog_view *);
756 static const struct got_error *open_help_view(struct tog_view *,
758 static const struct got_error *show_help_view(struct tog_view *);
759 static const struct got_error *input_help_view(struct tog_view **,
760 struct tog_view *, int);
761 static const struct got_error *reset_help_view(struct tog_view *);
762 static const struct got_error* close_help_view(struct tog_view *);
763 static const struct got_error *search_start_help_view(struct tog_view *);
764 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
765 size_t *, int **, int **, int **, int **);
767 static volatile sig_atomic_t tog_sigwinch_received;
768 static volatile sig_atomic_t tog_sigpipe_received;
769 static volatile sig_atomic_t tog_sigcont_received;
770 static volatile sig_atomic_t tog_sigint_received;
771 static volatile sig_atomic_t tog_sigterm_received;
774 tog_sigwinch(int signo)
776 tog_sigwinch_received = 1;
780 tog_sigpipe(int signo)
782 tog_sigpipe_received = 1;
786 tog_sigcont(int signo)
788 tog_sigcont_received = 1;
792 tog_sigint(int signo)
794 tog_sigint_received = 1;
798 tog_sigterm(int signo)
800 tog_sigterm_received = 1;
804 tog_fatal_signal_received(void)
806 return (tog_sigpipe_received ||
807 tog_sigint_received || tog_sigterm_received);
810 static const struct got_error *
811 view_close(struct tog_view *view)
813 const struct got_error *err = NULL, *child_err = NULL;
816 child_err = view_close(view->child);
820 err = view->close(view);
822 del_panel(view->panel);
824 delwin(view->window);
826 return err ? err : child_err;
829 static struct tog_view *
830 view_open(int nlines, int ncols, int begin_y, int begin_x,
831 enum tog_view_type type)
833 struct tog_view *view = calloc(1, sizeof(*view));
841 view->nlines = nlines ? nlines : LINES - begin_y;
842 view->ncols = ncols ? ncols : COLS - begin_x;
843 view->begin_y = begin_y;
844 view->begin_x = begin_x;
845 view->window = newwin(nlines, ncols, begin_y, begin_x);
846 if (view->window == NULL) {
850 view->panel = new_panel(view->window);
851 if (view->panel == NULL ||
852 set_panel_userptr(view->panel, view) != OK) {
857 keypad(view->window, TRUE);
862 view_split_begin_x(int begin_x)
864 if (begin_x > 0 || COLS < 120)
866 return (COLS - MAX(COLS / 2, 80));
869 /* XXX Stub till we decide what to do. */
871 view_split_begin_y(int lines)
873 return lines * HSPLIT_SCALE;
876 static const struct got_error *view_resize(struct tog_view *);
878 static const struct got_error *
879 view_splitscreen(struct tog_view *view)
881 const struct got_error *err = NULL;
883 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
884 if (view->resized_y && view->resized_y < view->lines)
885 view->begin_y = view->resized_y;
887 view->begin_y = view_split_begin_y(view->nlines);
889 } else if (!view->resized) {
890 if (view->resized_x && view->resized_x < view->cols - 1 &&
892 view->begin_x = view->resized_x;
894 view->begin_x = view_split_begin_x(0);
897 view->nlines = LINES - view->begin_y;
898 view->ncols = COLS - view->begin_x;
901 err = view_resize(view);
905 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
906 view->parent->nlines = view->begin_y;
908 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
909 return got_error_from_errno("mvwin");
914 static const struct got_error *
915 view_fullscreen(struct tog_view *view)
917 const struct got_error *err = NULL;
920 view->begin_y = view->resized ? view->begin_y : 0;
921 view->nlines = view->resized ? view->nlines : LINES;
925 err = view_resize(view);
929 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
930 return got_error_from_errno("mvwin");
936 view_is_parent_view(struct tog_view *view)
938 return view->parent == NULL;
942 view_is_splitscreen(struct tog_view *view)
944 return view->begin_x > 0 || view->begin_y > 0;
948 view_is_fullscreen(struct tog_view *view)
950 return view->nlines == LINES && view->ncols == COLS;
954 view_is_hsplit_top(struct tog_view *view)
956 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
957 view_is_splitscreen(view->child);
961 view_border(struct tog_view *view)
964 const struct tog_view *view_above;
967 return view_border(view->parent);
969 panel = panel_above(view->panel);
973 view_above = panel_userptr(panel);
974 if (view->mode == TOG_VIEW_SPLIT_HRZN)
975 mvwhline(view->window, view_above->begin_y - 1,
976 view->begin_x, got_locale_is_utf8() ?
977 ACS_HLINE : '-', view->ncols);
979 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
980 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
983 static const struct got_error *view_init_hsplit(struct tog_view *, int);
984 static const struct got_error *request_log_commits(struct tog_view *);
985 static const struct got_error *offset_selection_down(struct tog_view *);
986 static void offset_selection_up(struct tog_view *);
987 static void view_get_split(struct tog_view *, int *, int *);
989 static const struct got_error *
990 view_resize(struct tog_view *view)
992 const struct got_error *err = NULL;
993 int dif, nlines, ncols;
995 dif = LINES - view->lines; /* line difference */
997 if (view->lines > LINES)
998 nlines = view->nlines - (view->lines - LINES);
1000 nlines = view->nlines + (LINES - view->lines);
1001 if (view->cols > COLS)
1002 ncols = view->ncols - (view->cols - COLS);
1004 ncols = view->ncols + (COLS - view->cols);
1007 int hs = view->child->begin_y;
1009 if (!view_is_fullscreen(view))
1010 view->child->begin_x = view_split_begin_x(view->begin_x);
1011 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1012 view->child->begin_x == 0) {
1015 view_fullscreen(view->child);
1016 if (view->child->focussed)
1017 show_panel(view->child->panel);
1019 show_panel(view->panel);
1021 ncols = view->child->begin_x;
1023 view_splitscreen(view->child);
1024 show_panel(view->child->panel);
1027 * XXX This is ugly and needs to be moved into the above
1028 * logic but "works" for now and my attempts at moving it
1029 * break either 'tab' or 'F' key maps in horizontal splits.
1032 err = view_splitscreen(view->child);
1035 if (dif < 0) { /* top split decreased */
1036 err = offset_selection_down(view);
1043 show_panel(view->child->panel);
1044 nlines = view->nlines;
1046 } else if (view->parent == NULL)
1049 if (view->resize && dif > 0) {
1050 err = view->resize(view, dif);
1055 if (wresize(view->window, nlines, ncols) == ERR)
1056 return got_error_from_errno("wresize");
1057 if (replace_panel(view->panel, view->window) == ERR)
1058 return got_error_from_errno("replace_panel");
1059 wclear(view->window);
1061 view->nlines = nlines;
1062 view->ncols = ncols;
1063 view->lines = LINES;
1069 static const struct got_error *
1070 resize_log_view(struct tog_view *view, int increase)
1072 struct tog_log_view_state *s = &view->state.log;
1073 const struct got_error *err = NULL;
1076 if (s->selected_entry)
1077 n = s->selected_entry->idx + view->lines - s->selected;
1080 * Request commits to account for the increased
1081 * height so we have enough to populate the view.
1083 if (s->commits->ncommits < n) {
1084 view->nscrolled = n - s->commits->ncommits + increase + 1;
1085 err = request_log_commits(view);
1092 view_adjust_offset(struct tog_view *view, int n)
1097 if (view->parent && view->parent->offset) {
1098 if (view->parent->offset + n >= 0)
1099 view->parent->offset += n;
1101 view->parent->offset = 0;
1102 } else if (view->offset) {
1103 if (view->offset - n >= 0)
1110 static const struct got_error *
1111 view_resize_split(struct tog_view *view, int resize)
1113 const struct got_error *err = NULL;
1114 struct tog_view *v = NULL;
1121 if (!v->child || !view_is_splitscreen(v->child))
1124 v->resized = v->child->resized = resize; /* lock for resize event */
1126 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1127 if (v->child->resized_y)
1128 v->child->begin_y = v->child->resized_y;
1130 v->child->begin_y -= resize;
1132 v->child->begin_y += resize;
1133 if (v->child->begin_y < 3) {
1135 v->child->begin_y = 3;
1136 } else if (v->child->begin_y > LINES - 1) {
1138 v->child->begin_y = LINES - 1;
1141 v->child->ncols = COLS;
1142 view_adjust_offset(view, resize);
1143 err = view_init_hsplit(v, v->child->begin_y);
1146 v->child->resized_y = v->child->begin_y;
1148 if (v->child->resized_x)
1149 v->child->begin_x = v->child->resized_x;
1151 v->child->begin_x -= resize;
1153 v->child->begin_x += resize;
1154 if (v->child->begin_x < 11) {
1156 v->child->begin_x = 11;
1157 } else if (v->child->begin_x > COLS - 1) {
1159 v->child->begin_x = COLS - 1;
1161 v->child->resized_x = v->child->begin_x;
1164 v->child->mode = v->mode;
1165 v->child->nlines = v->lines - v->child->begin_y;
1166 v->child->ncols = v->cols - v->child->begin_x;
1169 err = view_fullscreen(v);
1172 err = view_splitscreen(v->child);
1176 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1177 err = offset_selection_down(v->child);
1183 err = v->resize(v, 0);
1184 else if (v->child->resize)
1185 err = v->child->resize(v->child, 0);
1187 v->resized = v->child->resized = 0;
1193 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1195 struct tog_view *v = src->child ? src->child : src;
1197 dst->resized_x = v->resized_x;
1198 dst->resized_y = v->resized_y;
1201 static const struct got_error *
1202 view_close_child(struct tog_view *view)
1204 const struct got_error *err = NULL;
1206 if (view->child == NULL)
1209 err = view_close(view->child);
1214 static const struct got_error *
1215 view_set_child(struct tog_view *view, struct tog_view *child)
1217 const struct got_error *err = NULL;
1219 view->child = child;
1220 child->parent = view;
1222 err = view_resize(view);
1226 if (view->child->resized_x || view->child->resized_y)
1227 err = view_resize_split(view, 0);
1232 static const struct got_error *view_dispatch_request(struct tog_view **,
1233 struct tog_view *, enum tog_view_type, int, int);
1235 static const struct got_error *
1236 view_request_new(struct tog_view **requested, struct tog_view *view,
1237 enum tog_view_type request)
1239 struct tog_view *new_view = NULL;
1240 const struct got_error *err;
1245 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1246 view_get_split(view, &y, &x);
1248 err = view_dispatch_request(&new_view, view, request, y, x);
1252 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1253 request != TOG_VIEW_HELP) {
1254 err = view_init_hsplit(view, y);
1260 new_view->focussed = 1;
1261 new_view->mode = view->mode;
1262 new_view->nlines = request == TOG_VIEW_HELP ?
1263 view->lines : view->lines - y;
1265 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1266 view_transfer_size(new_view, view);
1267 err = view_close_child(view);
1270 err = view_set_child(view, new_view);
1273 view->focus_child = 1;
1275 *requested = new_view;
1281 tog_resizeterm(void)
1284 struct winsize size;
1286 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1287 cols = 80; /* Default */
1291 lines = size.ws_row;
1293 resize_term(lines, cols);
1296 static const struct got_error *
1297 view_search_start(struct tog_view *view, int fast_refresh)
1299 const struct got_error *err = NULL;
1300 struct tog_view *v = view;
1304 if (view->search_started) {
1305 regfree(&view->regex);
1306 view->searching = 0;
1307 memset(&view->regmatch, 0, sizeof(view->regmatch));
1309 view->search_started = 0;
1311 if (view->nlines < 1)
1314 if (view_is_hsplit_top(view))
1316 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1319 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1320 wclrtoeol(v->window);
1322 nodelay(v->window, FALSE); /* block for search term input */
1325 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1326 wrefresh(v->window);
1329 nodelay(v->window, TRUE);
1335 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1336 err = view->search_start(view);
1338 regfree(&view->regex);
1341 view->search_started = 1;
1342 view->searching = TOG_SEARCH_FORWARD;
1343 view->search_next_done = 0;
1344 view->search_next(view);
1350 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1351 static const struct got_error *
1352 switch_split(struct tog_view *view)
1354 const struct got_error *err = NULL;
1355 struct tog_view *v = NULL;
1362 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1363 v->mode = TOG_VIEW_SPLIT_VERT;
1365 v->mode = TOG_VIEW_SPLIT_HRZN;
1369 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1370 v->mode = TOG_VIEW_SPLIT_NONE;
1372 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1373 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1374 v->child->begin_y = v->child->resized_y;
1375 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1376 v->child->begin_x = v->child->resized_x;
1379 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1381 v->child->ncols = COLS;
1382 v->child->nscrolled = LINES - v->child->nlines;
1384 err = view_init_hsplit(v, v->child->begin_y);
1388 v->child->mode = v->mode;
1389 v->child->nlines = v->lines - v->child->begin_y;
1392 err = view_fullscreen(v);
1395 err = view_splitscreen(v->child);
1399 if (v->mode == TOG_VIEW_SPLIT_NONE)
1400 v->mode = TOG_VIEW_SPLIT_VERT;
1401 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1402 err = offset_selection_down(v);
1405 err = offset_selection_down(v->child);
1409 offset_selection_up(v);
1410 offset_selection_up(v->child);
1413 err = v->resize(v, 0);
1414 else if (v->child->resize)
1415 err = v->child->resize(v->child, 0);
1421 * Compute view->count from numeric input. Assign total to view->count and
1422 * return first non-numeric key entered.
1425 get_compound_key(struct tog_view *view, int c)
1427 struct tog_view *v = view;
1430 if (view_is_hsplit_top(view))
1432 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1436 cbreak(); /* block for input */
1437 nodelay(view->window, FALSE);
1438 wmove(v->window, v->nlines - 1, 0);
1439 wclrtoeol(v->window);
1440 waddch(v->window, ':');
1443 x = getcurx(v->window);
1444 if (x != ERR && x < view->ncols) {
1445 waddch(v->window, c);
1446 wrefresh(v->window);
1450 * Don't overflow. Max valid request should be the greatest
1451 * between the longest and total lines; cap at 10 million.
1456 n = n * 10 + (c - '0');
1457 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1459 if (c == 'G' || c == 'g') { /* nG key map */
1460 view->gline = view->hiline = n;
1465 /* Massage excessive or inapplicable values at the input handler. */
1472 action_report(struct tog_view *view)
1474 struct tog_view *v = view;
1476 if (view_is_hsplit_top(view))
1478 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1481 wmove(v->window, v->nlines - 1, 0);
1482 wclrtoeol(v->window);
1483 wprintw(v->window, ":%s", view->action);
1484 wrefresh(v->window);
1487 * Clear action status report. Only clear in blame view
1488 * once annotating is complete, otherwise it's too fast.
1490 if (view->type == TOG_VIEW_BLAME) {
1491 if (view->state.blame.blame_complete)
1492 view->action = NULL;
1494 view->action = NULL;
1497 static const struct got_error *
1498 view_input(struct tog_view **new, int *done, struct tog_view *view,
1499 struct tog_view_list_head *views, int fast_refresh)
1501 const struct got_error *err = NULL;
1508 action_report(view);
1510 /* Clear "no matches" indicator. */
1511 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1512 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1513 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1517 if (view->searching && !view->search_next_done) {
1518 errcode = pthread_mutex_unlock(&tog_mutex);
1520 return got_error_set_errno(errcode,
1521 "pthread_mutex_unlock");
1523 errcode = pthread_mutex_lock(&tog_mutex);
1525 return got_error_set_errno(errcode,
1526 "pthread_mutex_lock");
1527 view->search_next(view);
1531 /* Allow threads to make progress while we are waiting for input. */
1532 errcode = pthread_mutex_unlock(&tog_mutex);
1534 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1535 /* If we have an unfinished count, let C-g or backspace abort. */
1536 if (view->count && --view->count) {
1538 nodelay(view->window, TRUE);
1539 ch = wgetch(view->window);
1540 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1545 ch = wgetch(view->window);
1546 if (ch >= '1' && ch <= '9')
1547 view->ch = ch = get_compound_key(view, ch);
1549 if (view->hiline && ch != ERR && ch != 0)
1550 view->hiline = 0; /* key pressed, clear line highlight */
1551 nodelay(view->window, TRUE);
1552 errcode = pthread_mutex_lock(&tog_mutex);
1554 return got_error_set_errno(errcode, "pthread_mutex_lock");
1556 if (tog_sigwinch_received || tog_sigcont_received) {
1558 tog_sigwinch_received = 0;
1559 tog_sigcont_received = 0;
1560 TAILQ_FOREACH(v, views, entry) {
1561 err = view_resize(v);
1564 err = v->input(new, v, KEY_RESIZE);
1568 err = view_resize(v->child);
1571 err = v->child->input(new, v->child,
1575 if (v->child->resized_x || v->child->resized_y) {
1576 err = view_resize_split(v, 0);
1588 if (view->type == TOG_VIEW_HELP)
1589 err = view->reset(view);
1591 err = view_request_new(new, view, TOG_VIEW_HELP);
1597 view->child->focussed = 1;
1598 view->focus_child = 1;
1599 } else if (view->parent) {
1601 view->parent->focussed = 1;
1602 view->parent->focus_child = 0;
1603 if (!view_is_splitscreen(view)) {
1604 if (view->parent->resize) {
1605 err = view->parent->resize(view->parent,
1610 offset_selection_up(view->parent);
1611 err = view_fullscreen(view->parent);
1618 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1619 if (view->parent->resize) {
1620 /* might need more commits to fill fullscreen */
1621 err = view->parent->resize(view->parent, 0);
1625 offset_selection_up(view->parent);
1627 err = view->input(new, view, ch);
1635 if (view_is_parent_view(view)) {
1636 if (view->child == NULL)
1638 if (view_is_splitscreen(view->child)) {
1640 view->child->focussed = 1;
1641 err = view_fullscreen(view->child);
1643 err = view_splitscreen(view->child);
1645 err = view_resize_split(view, 0);
1649 err = view->child->input(new, view->child,
1652 if (view_is_splitscreen(view)) {
1653 view->parent->focussed = 0;
1655 err = view_fullscreen(view);
1657 err = view_splitscreen(view);
1658 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1659 err = view_resize(view->parent);
1661 err = view_resize_split(view, 0);
1665 err = view->input(new, view, KEY_RESIZE);
1670 err = view->resize(view, 0);
1675 err = offset_selection_down(view->parent);
1677 err = offset_selection_down(view);
1681 err = switch_split(view);
1684 err = view_resize_split(view, -1);
1687 err = view_resize_split(view, 1);
1693 if (view->search_start)
1694 view_search_start(view, fast_refresh);
1696 err = view->input(new, view, ch);
1700 if (view->search_started && view->search_next) {
1701 view->searching = (ch == 'n' ?
1702 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1703 view->search_next_done = 0;
1704 view->search_next(view);
1706 err = view->input(new, view, ch);
1709 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1710 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1711 view->action = "Patience diff algorithm";
1713 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1714 view->action = "Myers diff algorithm";
1716 TAILQ_FOREACH(v, views, entry) {
1722 if (v->child && v->child->reset) {
1723 err = v->child->reset(v->child);
1730 err = view->input(new, view, ch);
1738 view_needs_focus_indication(struct tog_view *view)
1740 if (view_is_parent_view(view)) {
1741 if (view->child == NULL || view->child->focussed)
1743 if (!view_is_splitscreen(view->child))
1745 } else if (!view_is_splitscreen(view))
1748 return view->focussed;
1751 static const struct got_error *
1752 view_loop(struct tog_view *view)
1754 const struct got_error *err = NULL;
1755 struct tog_view_list_head views;
1756 struct tog_view *new_view;
1758 int fast_refresh = 10;
1759 int done = 0, errcode;
1761 mode = getenv("TOG_VIEW_SPLIT_MODE");
1762 if (!mode || !(*mode == 'h' || *mode == 'H'))
1763 view->mode = TOG_VIEW_SPLIT_VERT;
1765 view->mode = TOG_VIEW_SPLIT_HRZN;
1767 errcode = pthread_mutex_lock(&tog_mutex);
1769 return got_error_set_errno(errcode, "pthread_mutex_lock");
1772 TAILQ_INSERT_HEAD(&views, view, entry);
1775 err = view->show(view);
1780 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1781 !tog_fatal_signal_received()) {
1782 /* Refresh fast during initialization, then become slower. */
1783 if (fast_refresh && --fast_refresh == 0)
1784 halfdelay(10); /* switch to once per second */
1786 err = view_input(&new_view, &done, view, &views, fast_refresh);
1790 if (view->dying && view == TAILQ_FIRST(&views) &&
1791 TAILQ_NEXT(view, entry) == NULL)
1797 * When we quit, scroll the screen up a single line
1798 * so we don't lose any information.
1800 TAILQ_FOREACH(v, &views, entry) {
1801 wmove(v->window, 0, 0);
1802 wdeleteln(v->window);
1803 wnoutrefresh(v->window);
1804 if (v->child && !view_is_fullscreen(v)) {
1805 wmove(v->child->window, 0, 0);
1806 wdeleteln(v->child->window);
1807 wnoutrefresh(v->child->window);
1814 struct tog_view *v, *prev = NULL;
1816 if (view_is_parent_view(view))
1817 prev = TAILQ_PREV(view, tog_view_list_head,
1819 else if (view->parent)
1820 prev = view->parent;
1823 view->parent->child = NULL;
1824 view->parent->focus_child = 0;
1825 /* Restore fullscreen line height. */
1826 view->parent->nlines = view->parent->lines;
1827 err = view_resize(view->parent);
1830 /* Make resized splits persist. */
1831 view_transfer_size(view->parent, view);
1833 TAILQ_REMOVE(&views, view, entry);
1835 err = view_close(view);
1840 TAILQ_FOREACH(v, &views, entry) {
1844 if (view == NULL && new_view == NULL) {
1845 /* No view has focus. Try to pick one. */
1848 else if (!TAILQ_EMPTY(&views)) {
1849 view = TAILQ_LAST(&views,
1850 tog_view_list_head);
1853 if (view->focus_child) {
1854 view->child->focussed = 1;
1862 struct tog_view *v, *t;
1863 /* Only allow one parent view per type. */
1864 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1865 if (v->type != new_view->type)
1867 TAILQ_REMOVE(&views, v, entry);
1868 err = view_close(v);
1873 TAILQ_INSERT_TAIL(&views, new_view, entry);
1876 if (view && !done) {
1877 if (view_is_parent_view(view)) {
1878 if (view->child && view->child->focussed)
1881 if (view->parent && view->parent->focussed)
1882 view = view->parent;
1884 show_panel(view->panel);
1885 if (view->child && view_is_splitscreen(view->child))
1886 show_panel(view->child->panel);
1887 if (view->parent && view_is_splitscreen(view)) {
1888 err = view->parent->show(view->parent);
1892 err = view->show(view);
1896 err = view->child->show(view->child);
1905 while (!TAILQ_EMPTY(&views)) {
1906 const struct got_error *close_err;
1907 view = TAILQ_FIRST(&views);
1908 TAILQ_REMOVE(&views, view, entry);
1909 close_err = view_close(view);
1910 if (close_err && err == NULL)
1914 errcode = pthread_mutex_unlock(&tog_mutex);
1915 if (errcode && err == NULL)
1916 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1926 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1931 /* Create newly allocated wide-character string equivalent to a byte string. */
1932 static const struct got_error *
1933 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1936 const struct got_error *err = NULL;
1939 *wlen = mbstowcs(NULL, s, 0);
1940 if (*wlen == (size_t)-1) {
1942 if (errno != EILSEQ)
1943 return got_error_from_errno("mbstowcs");
1945 /* byte string invalid in current encoding; try to "fix" it */
1946 err = got_mbsavis(&vis, &vislen, s);
1949 *wlen = mbstowcs(NULL, vis, 0);
1950 if (*wlen == (size_t)-1) {
1951 err = got_error_from_errno("mbstowcs"); /* give up */
1956 *ws = calloc(*wlen + 1, sizeof(**ws));
1958 err = got_error_from_errno("calloc");
1962 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1963 err = got_error_from_errno("mbstowcs");
1974 static const struct got_error *
1975 expand_tab(char **ptr, const char *src)
1978 size_t len, n, idx = 0, sz = 0;
1981 n = len = strlen(src);
1982 dst = malloc(n + 1);
1984 return got_error_from_errno("malloc");
1986 while (idx < len && src[idx]) {
1987 const char c = src[idx];
1990 size_t nb = TABSIZE - sz % TABSIZE;
1993 p = realloc(dst, n + nb);
1996 return got_error_from_errno("realloc");
2001 memset(dst + sz, ' ', nb);
2004 dst[sz++] = src[idx];
2014 * Advance at most n columns from wline starting at offset off.
2015 * Return the index to the first character after the span operation.
2016 * Return the combined column width of all spanned wide character in
2020 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2022 int width, i, cols = 0;
2029 for (i = off; wline[i] != L'\0'; ++i) {
2030 if (wline[i] == L'\t')
2031 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2033 width = wcwidth(wline[i]);
2040 if (cols + width > n)
2050 * Format a line for display, ensuring that it won't overflow a width limit.
2051 * With scrolling, the width returned refers to the scrolled version of the
2052 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2054 static const struct got_error *
2055 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2056 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2058 const struct got_error *err = NULL;
2060 wchar_t *wline = NULL;
2069 err = expand_tab(&exstr, line);
2074 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2079 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2081 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2082 wline[wlen - 1] = L'\0';
2085 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2086 wline[wlen - 1] = L'\0';
2090 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2096 *scrollxp = scrollx;
2104 static const struct got_error*
2105 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2106 struct got_object_id *id, struct got_repository *repo)
2108 static const struct got_error *err = NULL;
2109 struct got_reflist_entry *re;
2115 TAILQ_FOREACH(re, refs, entry) {
2116 struct got_tag_object *tag = NULL;
2117 struct got_object_id *ref_id;
2120 name = got_ref_get_name(re->ref);
2121 if (strcmp(name, GOT_REF_HEAD) == 0)
2123 if (strncmp(name, "refs/", 5) == 0)
2125 if (strncmp(name, "got/", 4) == 0 &&
2126 strncmp(name, "got/backup/", 11) != 0)
2128 if (strncmp(name, "heads/", 6) == 0)
2130 if (strncmp(name, "remotes/", 8) == 0) {
2132 s = strstr(name, "/" GOT_REF_HEAD);
2133 if (s != NULL && s[strlen(s)] == '\0')
2136 err = got_ref_resolve(&ref_id, repo, re->ref);
2139 if (strncmp(name, "tags/", 5) == 0) {
2140 err = got_object_open_as_tag(&tag, repo, ref_id);
2142 if (err->code != GOT_ERR_OBJ_TYPE) {
2146 /* Ref points at something other than a tag. */
2151 cmp = got_object_id_cmp(tag ?
2152 got_object_tag_get_object_id(tag) : ref_id, id);
2155 got_object_tag_close(tag);
2159 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2160 s ? ", " : "", name) == -1) {
2161 err = got_error_from_errno("asprintf");
2172 static const struct got_error *
2173 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2178 smallerthan = strchr(author, '<');
2179 if (smallerthan && smallerthan[1] != '\0')
2180 author = smallerthan + 1;
2181 author[strcspn(author, "@>")] = '\0';
2182 return format_line(wauthor, author_width, NULL, author, 0, limit,
2186 static const struct got_error *
2187 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2188 struct got_object_id *id, const size_t date_display_cols,
2189 int author_display_cols)
2191 struct tog_log_view_state *s = &view->state.log;
2192 const struct got_error *err = NULL;
2193 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2194 char *logmsg0 = NULL, *logmsg = NULL;
2195 char *author = NULL;
2196 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2197 int author_width, logmsg_width;
2198 char *newline, *line = NULL;
2199 int col, limit, scrollx;
2200 const int avail = view->ncols;
2202 time_t committer_time;
2203 struct tog_color *tc;
2205 committer_time = got_object_commit_get_committer_time(commit);
2206 if (gmtime_r(&committer_time, &tm) == NULL)
2207 return got_error_from_errno("gmtime_r");
2208 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2209 return got_error(GOT_ERR_NO_SPACE);
2211 if (avail <= date_display_cols)
2212 limit = MIN(sizeof(datebuf) - 1, avail);
2214 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2215 tc = get_color(&s->colors, TOG_COLOR_DATE);
2217 wattr_on(view->window,
2218 COLOR_PAIR(tc->colorpair), NULL);
2219 waddnstr(view->window, datebuf, limit);
2221 wattr_off(view->window,
2222 COLOR_PAIR(tc->colorpair), NULL);
2229 err = got_object_id_str(&id_str, id);
2232 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2234 wattr_on(view->window,
2235 COLOR_PAIR(tc->colorpair), NULL);
2236 wprintw(view->window, "%.8s ", id_str);
2238 wattr_off(view->window,
2239 COLOR_PAIR(tc->colorpair), NULL);
2246 if (s->use_committer)
2247 author = strdup(got_object_commit_get_committer(commit));
2249 author = strdup(got_object_commit_get_author(commit));
2250 if (author == NULL) {
2251 err = got_error_from_errno("strdup");
2254 err = format_author(&wauthor, &author_width, author, avail - col, col);
2257 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2259 wattr_on(view->window,
2260 COLOR_PAIR(tc->colorpair), NULL);
2261 waddwstr(view->window, wauthor);
2262 col += author_width;
2263 while (col < avail && author_width < author_display_cols + 2) {
2264 waddch(view->window, ' ');
2269 wattr_off(view->window,
2270 COLOR_PAIR(tc->colorpair), NULL);
2274 err = got_object_commit_get_logmsg(&logmsg0, commit);
2278 while (*logmsg == '\n')
2280 newline = strchr(logmsg, '\n');
2283 limit = avail - col;
2284 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2285 limit--; /* for the border */
2286 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2290 waddwstr(view->window, &wlogmsg[scrollx]);
2291 col += MAX(logmsg_width, 0);
2292 while (col < avail) {
2293 waddch(view->window, ' ');
2305 static struct commit_queue_entry *
2306 alloc_commit_queue_entry(struct got_commit_object *commit,
2307 struct got_object_id *id)
2309 struct commit_queue_entry *entry;
2310 struct got_object_id *dup;
2312 entry = calloc(1, sizeof(*entry));
2316 dup = got_object_id_dup(id);
2323 entry->commit = commit;
2328 pop_commit(struct commit_queue *commits)
2330 struct commit_queue_entry *entry;
2332 entry = TAILQ_FIRST(&commits->head);
2333 TAILQ_REMOVE(&commits->head, entry, entry);
2334 got_object_commit_close(entry->commit);
2335 commits->ncommits--;
2341 free_commits(struct commit_queue *commits)
2343 while (!TAILQ_EMPTY(&commits->head))
2344 pop_commit(commits);
2347 static const struct got_error *
2348 match_commit(int *have_match, struct got_object_id *id,
2349 struct got_commit_object *commit, regex_t *regex)
2351 const struct got_error *err = NULL;
2352 regmatch_t regmatch;
2353 char *id_str = NULL, *logmsg = NULL;
2357 err = got_object_id_str(&id_str, id);
2361 err = got_object_commit_get_logmsg(&logmsg, commit);
2365 if (regexec(regex, got_object_commit_get_author(commit), 1,
2366 ®match, 0) == 0 ||
2367 regexec(regex, got_object_commit_get_committer(commit), 1,
2368 ®match, 0) == 0 ||
2369 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2370 regexec(regex, logmsg, 1, ®match, 0) == 0)
2378 static const struct got_error *
2379 queue_commits(struct tog_log_thread_args *a)
2381 const struct got_error *err = NULL;
2384 * We keep all commits open throughout the lifetime of the log
2385 * view in order to avoid having to re-fetch commits from disk
2386 * while updating the display.
2389 struct got_object_id id;
2390 struct got_commit_object *commit;
2391 struct commit_queue_entry *entry;
2392 int limit_match = 0;
2395 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2400 err = got_object_open_as_commit(&commit, a->repo, &id);
2403 entry = alloc_commit_queue_entry(commit, &id);
2404 if (entry == NULL) {
2405 err = got_error_from_errno("alloc_commit_queue_entry");
2409 errcode = pthread_mutex_lock(&tog_mutex);
2411 err = got_error_set_errno(errcode,
2412 "pthread_mutex_lock");
2416 entry->idx = a->real_commits->ncommits;
2417 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2418 a->real_commits->ncommits++;
2421 err = match_commit(&limit_match, &id, commit,
2427 struct commit_queue_entry *matched;
2429 matched = alloc_commit_queue_entry(
2430 entry->commit, entry->id);
2431 if (matched == NULL) {
2432 err = got_error_from_errno(
2433 "alloc_commit_queue_entry");
2436 matched->commit = entry->commit;
2437 got_object_commit_retain(entry->commit);
2439 matched->idx = a->limit_commits->ncommits;
2440 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2442 a->limit_commits->ncommits++;
2446 * This is how we signal log_thread() that we
2447 * have found a match, and that it should be
2448 * counted as a new entry for the view.
2450 a->limit_match = limit_match;
2453 if (*a->searching == TOG_SEARCH_FORWARD &&
2454 !*a->search_next_done) {
2456 err = match_commit(&have_match, &id, commit, a->regex);
2461 if (limit_match && have_match)
2462 *a->search_next_done =
2463 TOG_SEARCH_HAVE_MORE;
2464 } else if (have_match)
2465 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2468 errcode = pthread_mutex_unlock(&tog_mutex);
2469 if (errcode && err == NULL)
2470 err = got_error_set_errno(errcode,
2471 "pthread_mutex_unlock");
2474 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2480 select_commit(struct tog_log_view_state *s)
2482 struct commit_queue_entry *entry;
2485 entry = s->first_displayed_entry;
2487 if (ncommits == s->selected) {
2488 s->selected_entry = entry;
2491 entry = TAILQ_NEXT(entry, entry);
2496 static const struct got_error *
2497 draw_commits(struct tog_view *view)
2499 const struct got_error *err = NULL;
2500 struct tog_log_view_state *s = &view->state.log;
2501 struct commit_queue_entry *entry = s->selected_entry;
2502 int limit = view->nlines;
2504 int ncommits, author_cols = 4;
2505 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2506 char *refs_str = NULL;
2508 struct tog_color *tc;
2509 static const size_t date_display_cols = 12;
2511 if (view_is_hsplit_top(view))
2512 --limit; /* account for border */
2514 if (s->selected_entry &&
2515 !(view->searching && view->search_next_done == 0)) {
2516 struct got_reflist_head *refs;
2517 err = got_object_id_str(&id_str, s->selected_entry->id);
2520 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2521 s->selected_entry->id);
2523 err = build_refs_str(&refs_str, refs,
2524 s->selected_entry->id, s->repo);
2530 if (s->thread_args.commits_needed == 0)
2531 halfdelay(10); /* disable fast refresh */
2533 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2534 if (asprintf(&ncommits_str, " [%d/%d] %s",
2535 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2536 (view->searching && !view->search_next_done) ?
2537 "searching..." : "loading...") == -1) {
2538 err = got_error_from_errno("asprintf");
2542 const char *search_str = NULL;
2543 const char *limit_str = NULL;
2545 if (view->searching) {
2546 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2547 search_str = "no more matches";
2548 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2549 search_str = "no matches found";
2550 else if (!view->search_next_done)
2551 search_str = "searching...";
2554 if (s->limit_view && s->commits->ncommits == 0)
2555 limit_str = "no matches found";
2557 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2558 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2559 search_str ? search_str : (refs_str ? refs_str : ""),
2560 limit_str ? limit_str : "") == -1) {
2561 err = got_error_from_errno("asprintf");
2566 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2567 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2568 "........................................",
2569 s->in_repo_path, ncommits_str) == -1) {
2570 err = got_error_from_errno("asprintf");
2574 } else if (asprintf(&header, "commit %s%s",
2575 id_str ? id_str : "........................................",
2576 ncommits_str) == -1) {
2577 err = got_error_from_errno("asprintf");
2581 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2585 werase(view->window);
2587 if (view_needs_focus_indication(view))
2588 wstandout(view->window);
2589 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2591 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2592 waddwstr(view->window, wline);
2593 while (width < view->ncols) {
2594 waddch(view->window, ' ');
2598 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2599 if (view_needs_focus_indication(view))
2600 wstandend(view->window);
2605 /* Grow author column size if necessary, and set view->maxx. */
2606 entry = s->first_displayed_entry;
2610 struct got_commit_object *c = entry->commit;
2611 char *author, *eol, *msg, *msg0;
2612 wchar_t *wauthor, *wmsg;
2614 if (ncommits >= limit - 1)
2616 if (s->use_committer)
2617 author = strdup(got_object_commit_get_committer(c));
2619 author = strdup(got_object_commit_get_author(c));
2620 if (author == NULL) {
2621 err = got_error_from_errno("strdup");
2624 err = format_author(&wauthor, &width, author, COLS,
2626 if (author_cols < width)
2627 author_cols = width;
2632 err = got_object_commit_get_logmsg(&msg0, c);
2636 while (*msg == '\n')
2638 if ((eol = strchr(msg, '\n')))
2640 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2641 date_display_cols + author_cols, 0);
2644 view->maxx = MAX(view->maxx, width);
2648 entry = TAILQ_NEXT(entry, entry);
2651 entry = s->first_displayed_entry;
2652 s->last_displayed_entry = s->first_displayed_entry;
2655 if (ncommits >= limit - 1)
2657 if (ncommits == s->selected)
2658 wstandout(view->window);
2659 err = draw_commit(view, entry->commit, entry->id,
2660 date_display_cols, author_cols);
2661 if (ncommits == s->selected)
2662 wstandend(view->window);
2666 s->last_displayed_entry = entry;
2667 entry = TAILQ_NEXT(entry, entry);
2680 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2682 struct commit_queue_entry *entry;
2685 entry = TAILQ_FIRST(&s->commits->head);
2686 if (s->first_displayed_entry == entry)
2689 entry = s->first_displayed_entry;
2690 while (entry && nscrolled < maxscroll) {
2691 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2693 s->first_displayed_entry = entry;
2699 static const struct got_error *
2700 trigger_log_thread(struct tog_view *view, int wait)
2702 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2705 halfdelay(1); /* fast refresh while loading commits */
2707 while (!ta->log_complete && !tog_thread_error &&
2708 (ta->commits_needed > 0 || ta->load_all)) {
2709 /* Wake the log thread. */
2710 errcode = pthread_cond_signal(&ta->need_commits);
2712 return got_error_set_errno(errcode,
2713 "pthread_cond_signal");
2716 * The mutex will be released while the view loop waits
2717 * in wgetch(), at which time the log thread will run.
2722 /* Display progress update in log view. */
2723 show_log_view(view);
2727 /* Wait right here while next commit is being loaded. */
2728 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2730 return got_error_set_errno(errcode,
2731 "pthread_cond_wait");
2733 /* Display progress update in log view. */
2734 show_log_view(view);
2742 static const struct got_error *
2743 request_log_commits(struct tog_view *view)
2745 struct tog_log_view_state *state = &view->state.log;
2746 const struct got_error *err = NULL;
2748 if (state->thread_args.log_complete)
2751 state->thread_args.commits_needed += view->nscrolled;
2752 err = trigger_log_thread(view, 1);
2753 view->nscrolled = 0;
2758 static const struct got_error *
2759 log_scroll_down(struct tog_view *view, int maxscroll)
2761 struct tog_log_view_state *s = &view->state.log;
2762 const struct got_error *err = NULL;
2763 struct commit_queue_entry *pentry;
2764 int nscrolled = 0, ncommits_needed;
2766 if (s->last_displayed_entry == NULL)
2769 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2770 if (s->commits->ncommits < ncommits_needed &&
2771 !s->thread_args.log_complete) {
2773 * Ask the log thread for required amount of commits.
2775 s->thread_args.commits_needed +=
2776 ncommits_needed - s->commits->ncommits;
2777 err = trigger_log_thread(view, 1);
2783 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2784 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2787 s->last_displayed_entry = pentry ?
2788 pentry : s->last_displayed_entry;
2790 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2793 s->first_displayed_entry = pentry;
2794 } while (++nscrolled < maxscroll);
2796 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2797 view->nscrolled += nscrolled;
2799 view->nscrolled = 0;
2804 static const struct got_error *
2805 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2806 struct got_commit_object *commit, struct got_object_id *commit_id,
2807 struct tog_view *log_view, struct got_repository *repo)
2809 const struct got_error *err;
2810 struct got_object_qid *parent_id;
2811 struct tog_view *diff_view;
2813 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2814 if (diff_view == NULL)
2815 return got_error_from_errno("view_open");
2817 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2818 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2819 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2821 *new_view = diff_view;
2825 static const struct got_error *
2826 tree_view_visit_subtree(struct tog_tree_view_state *s,
2827 struct got_tree_object *subtree)
2829 struct tog_parent_tree *parent;
2831 parent = calloc(1, sizeof(*parent));
2833 return got_error_from_errno("calloc");
2835 parent->tree = s->tree;
2836 parent->first_displayed_entry = s->first_displayed_entry;
2837 parent->selected_entry = s->selected_entry;
2838 parent->selected = s->selected;
2839 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2842 s->first_displayed_entry = NULL;
2846 static const struct got_error *
2847 tree_view_walk_path(struct tog_tree_view_state *s,
2848 struct got_commit_object *commit, const char *path)
2850 const struct got_error *err = NULL;
2851 struct got_tree_object *tree = NULL;
2853 char *slash, *subpath = NULL;
2855 /* Walk the path and open corresponding tree objects. */
2858 struct got_tree_entry *te;
2859 struct got_object_id *tree_id;
2865 /* Ensure the correct subtree entry is selected. */
2866 slash = strchr(p, '/');
2868 te_name = strdup(p);
2870 te_name = strndup(p, slash - p);
2871 if (te_name == NULL) {
2872 err = got_error_from_errno("strndup");
2875 te = got_object_tree_find_entry(s->tree, te_name);
2877 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2882 s->first_displayed_entry = s->selected_entry = te;
2884 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2885 break; /* jump to this file's entry */
2887 slash = strchr(p, '/');
2889 subpath = strndup(path, slash - path);
2891 subpath = strdup(path);
2892 if (subpath == NULL) {
2893 err = got_error_from_errno("strdup");
2897 err = got_object_id_by_path(&tree_id, s->repo, commit,
2902 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2907 err = tree_view_visit_subtree(s, tree);
2909 got_object_tree_close(tree);
2923 static const struct got_error *
2924 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2925 struct commit_queue_entry *entry, const char *path,
2926 const char *head_ref_name, struct got_repository *repo)
2928 const struct got_error *err = NULL;
2929 struct tog_tree_view_state *s;
2930 struct tog_view *tree_view;
2932 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2933 if (tree_view == NULL)
2934 return got_error_from_errno("view_open");
2936 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2939 s = &tree_view->state.tree;
2941 *new_view = tree_view;
2943 if (got_path_is_root_dir(path))
2946 return tree_view_walk_path(s, entry->commit, path);
2949 static const struct got_error *
2950 block_signals_used_by_main_thread(void)
2955 if (sigemptyset(&sigset) == -1)
2956 return got_error_from_errno("sigemptyset");
2958 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2959 if (sigaddset(&sigset, SIGWINCH) == -1)
2960 return got_error_from_errno("sigaddset");
2961 if (sigaddset(&sigset, SIGCONT) == -1)
2962 return got_error_from_errno("sigaddset");
2963 if (sigaddset(&sigset, SIGINT) == -1)
2964 return got_error_from_errno("sigaddset");
2965 if (sigaddset(&sigset, SIGTERM) == -1)
2966 return got_error_from_errno("sigaddset");
2968 /* ncurses handles SIGTSTP */
2969 if (sigaddset(&sigset, SIGTSTP) == -1)
2970 return got_error_from_errno("sigaddset");
2972 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2974 return got_error_set_errno(errcode, "pthread_sigmask");
2980 log_thread(void *arg)
2982 const struct got_error *err = NULL;
2984 struct tog_log_thread_args *a = arg;
2988 * Sync startup with main thread such that we begin our
2989 * work once view_input() has released the mutex.
2991 errcode = pthread_mutex_lock(&tog_mutex);
2993 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2997 err = block_signals_used_by_main_thread();
2999 pthread_mutex_unlock(&tog_mutex);
3003 while (!done && !err && !tog_fatal_signal_received()) {
3004 errcode = pthread_mutex_unlock(&tog_mutex);
3006 err = got_error_set_errno(errcode,
3007 "pthread_mutex_unlock");
3010 err = queue_commits(a);
3012 if (err->code != GOT_ERR_ITER_COMPLETED)
3016 } else if (a->commits_needed > 0 && !a->load_all) {
3019 a->commits_needed--;
3021 a->commits_needed--;
3024 errcode = pthread_mutex_lock(&tog_mutex);
3026 err = got_error_set_errno(errcode,
3027 "pthread_mutex_lock");
3029 } else if (*a->quit)
3031 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3032 *a->first_displayed_entry =
3033 TAILQ_FIRST(&a->limit_commits->head);
3034 *a->selected_entry = *a->first_displayed_entry;
3035 } else if (*a->first_displayed_entry == NULL) {
3036 *a->first_displayed_entry =
3037 TAILQ_FIRST(&a->real_commits->head);
3038 *a->selected_entry = *a->first_displayed_entry;
3041 errcode = pthread_cond_signal(&a->commit_loaded);
3043 err = got_error_set_errno(errcode,
3044 "pthread_cond_signal");
3045 pthread_mutex_unlock(&tog_mutex);
3050 a->commits_needed = 0;
3052 if (a->commits_needed == 0 && !a->load_all) {
3053 errcode = pthread_cond_wait(&a->need_commits,
3056 err = got_error_set_errno(errcode,
3057 "pthread_cond_wait");
3058 pthread_mutex_unlock(&tog_mutex);
3066 a->log_complete = 1;
3067 errcode = pthread_mutex_unlock(&tog_mutex);
3069 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3072 tog_thread_error = 1;
3073 pthread_cond_signal(&a->commit_loaded);
3078 static const struct got_error *
3079 stop_log_thread(struct tog_log_view_state *s)
3081 const struct got_error *err = NULL, *thread_err = NULL;
3086 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3088 return got_error_set_errno(errcode,
3089 "pthread_cond_signal");
3090 errcode = pthread_mutex_unlock(&tog_mutex);
3092 return got_error_set_errno(errcode,
3093 "pthread_mutex_unlock");
3094 errcode = pthread_join(s->thread, (void **)&thread_err);
3096 return got_error_set_errno(errcode, "pthread_join");
3097 errcode = pthread_mutex_lock(&tog_mutex);
3099 return got_error_set_errno(errcode,
3100 "pthread_mutex_lock");
3101 s->thread = 0; //NULL;
3104 if (s->thread_args.repo) {
3105 err = got_repo_close(s->thread_args.repo);
3106 s->thread_args.repo = NULL;
3109 if (s->thread_args.pack_fds) {
3110 const struct got_error *pack_err =
3111 got_repo_pack_fds_close(s->thread_args.pack_fds);
3114 s->thread_args.pack_fds = NULL;
3117 if (s->thread_args.graph) {
3118 got_commit_graph_close(s->thread_args.graph);
3119 s->thread_args.graph = NULL;
3122 return err ? err : thread_err;
3125 static const struct got_error *
3126 close_log_view(struct tog_view *view)
3128 const struct got_error *err = NULL;
3129 struct tog_log_view_state *s = &view->state.log;
3132 err = stop_log_thread(s);
3134 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3135 if (errcode && err == NULL)
3136 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3138 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3139 if (errcode && err == NULL)
3140 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3142 free_commits(&s->limit_commits);
3143 free_commits(&s->real_commits);
3144 free(s->in_repo_path);
3145 s->in_repo_path = NULL;
3148 free(s->head_ref_name);
3149 s->head_ref_name = NULL;
3154 * We use two queues to implement the limit feature: first consists of
3155 * commits matching the current limit_regex; second is the real queue
3156 * of all known commits (real_commits). When the user starts limiting,
3157 * we swap queues such that all movement and displaying functionality
3158 * works with very slight change.
3160 static const struct got_error *
3161 limit_log_view(struct tog_view *view)
3163 struct tog_log_view_state *s = &view->state.log;
3164 struct commit_queue_entry *entry;
3165 struct tog_view *v = view;
3166 const struct got_error *err = NULL;
3170 if (view_is_hsplit_top(view))
3172 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3175 /* Get the pattern */
3176 wmove(v->window, v->nlines - 1, 0);
3177 wclrtoeol(v->window);
3178 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3179 nodelay(v->window, FALSE);
3182 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3185 nodelay(v->window, TRUE);
3189 if (*pattern == '\0') {
3191 * Safety measure for the situation where the user
3192 * resets limit without previously limiting anything.
3198 * User could have pressed Ctrl+L, which refreshed the
3199 * commit queues, it means we can't save previously
3200 * (before limit took place) displayed entries,
3201 * because they would point to already free'ed memory,
3202 * so we are forced to always select first entry of
3205 s->commits = &s->real_commits;
3206 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3207 s->selected_entry = s->first_displayed_entry;
3214 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3219 /* Clear the screen while loading limit view */
3220 s->first_displayed_entry = NULL;
3221 s->last_displayed_entry = NULL;
3222 s->selected_entry = NULL;
3223 s->commits = &s->limit_commits;
3225 /* Prepare limit queue for new search */
3226 free_commits(&s->limit_commits);
3227 s->limit_commits.ncommits = 0;
3229 /* First process commits, which are in queue already */
3230 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3233 err = match_commit(&have_match, entry->id,
3234 entry->commit, &s->limit_regex);
3239 struct commit_queue_entry *matched;
3241 matched = alloc_commit_queue_entry(entry->commit,
3243 if (matched == NULL) {
3244 err = got_error_from_errno(
3245 "alloc_commit_queue_entry");
3248 matched->commit = entry->commit;
3249 got_object_commit_retain(entry->commit);
3251 matched->idx = s->limit_commits.ncommits;
3252 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3254 s->limit_commits.ncommits++;
3258 /* Second process all the commits, until we fill the screen */
3259 if (s->limit_commits.ncommits < view->nlines - 1 &&
3260 !s->thread_args.log_complete) {
3261 s->thread_args.commits_needed +=
3262 view->nlines - s->limit_commits.ncommits - 1;
3263 err = trigger_log_thread(view, 1);
3268 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3269 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3275 static const struct got_error *
3276 search_start_log_view(struct tog_view *view)
3278 struct tog_log_view_state *s = &view->state.log;
3280 s->matched_entry = NULL;
3281 s->search_entry = NULL;
3285 static const struct got_error *
3286 search_next_log_view(struct tog_view *view)
3288 const struct got_error *err = NULL;
3289 struct tog_log_view_state *s = &view->state.log;
3290 struct commit_queue_entry *entry;
3292 /* Display progress update in log view. */
3293 show_log_view(view);
3297 if (s->search_entry) {
3299 errcode = pthread_mutex_unlock(&tog_mutex);
3301 return got_error_set_errno(errcode,
3302 "pthread_mutex_unlock");
3303 ch = wgetch(view->window);
3304 errcode = pthread_mutex_lock(&tog_mutex);
3306 return got_error_set_errno(errcode,
3307 "pthread_mutex_lock");
3308 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3309 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3312 if (view->searching == TOG_SEARCH_FORWARD)
3313 entry = TAILQ_NEXT(s->search_entry, entry);
3315 entry = TAILQ_PREV(s->search_entry,
3316 commit_queue_head, entry);
3317 } else if (s->matched_entry) {
3319 * If the user has moved the cursor after we hit a match,
3320 * the position from where we should continue searching
3321 * might have changed.
3323 if (view->searching == TOG_SEARCH_FORWARD)
3324 entry = TAILQ_NEXT(s->selected_entry, entry);
3326 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3329 entry = s->selected_entry;
3335 if (entry == NULL) {
3336 if (s->thread_args.log_complete ||
3337 view->searching == TOG_SEARCH_BACKWARD) {
3338 view->search_next_done =
3339 (s->matched_entry == NULL ?
3340 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3341 s->search_entry = NULL;
3345 * Poke the log thread for more commits and return,
3346 * allowing the main loop to make progress. Search
3347 * will resume at s->search_entry once we come back.
3349 s->thread_args.commits_needed++;
3350 return trigger_log_thread(view, 0);
3353 err = match_commit(&have_match, entry->id, entry->commit,
3358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3359 s->matched_entry = entry;
3363 s->search_entry = entry;
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 entry = TAILQ_NEXT(entry, entry);
3367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3370 if (s->matched_entry) {
3371 int cur = s->selected_entry->idx;
3372 while (cur < s->matched_entry->idx) {
3373 err = input_log_view(NULL, view, KEY_DOWN);
3378 while (cur > s->matched_entry->idx) {
3379 err = input_log_view(NULL, view, KEY_UP);
3386 s->search_entry = NULL;
3391 static const struct got_error *
3392 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3393 struct got_repository *repo, const char *head_ref_name,
3394 const char *in_repo_path, int log_branches)
3396 const struct got_error *err = NULL;
3397 struct tog_log_view_state *s = &view->state.log;
3398 struct got_repository *thread_repo = NULL;
3399 struct got_commit_graph *thread_graph = NULL;
3402 if (in_repo_path != s->in_repo_path) {
3403 free(s->in_repo_path);
3404 s->in_repo_path = strdup(in_repo_path);
3405 if (s->in_repo_path == NULL)
3406 return got_error_from_errno("strdup");
3409 /* The commit queue only contains commits being displayed. */
3410 TAILQ_INIT(&s->real_commits.head);
3411 s->real_commits.ncommits = 0;
3412 s->commits = &s->real_commits;
3414 TAILQ_INIT(&s->limit_commits.head);
3416 s->limit_commits.ncommits = 0;
3419 if (head_ref_name) {
3420 s->head_ref_name = strdup(head_ref_name);
3421 if (s->head_ref_name == NULL) {
3422 err = got_error_from_errno("strdup");
3426 s->start_id = got_object_id_dup(start_id);
3427 if (s->start_id == NULL) {
3428 err = got_error_from_errno("got_object_id_dup");
3431 s->log_branches = log_branches;
3432 s->use_committer = 1;
3434 STAILQ_INIT(&s->colors);
3435 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3436 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3437 get_color_value("TOG_COLOR_COMMIT"));
3440 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3441 get_color_value("TOG_COLOR_AUTHOR"));
3443 free_colors(&s->colors);
3446 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3447 get_color_value("TOG_COLOR_DATE"));
3449 free_colors(&s->colors);
3454 view->show = show_log_view;
3455 view->input = input_log_view;
3456 view->resize = resize_log_view;
3457 view->close = close_log_view;
3458 view->search_start = search_start_log_view;
3459 view->search_next = search_next_log_view;
3461 if (s->thread_args.pack_fds == NULL) {
3462 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3466 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3467 s->thread_args.pack_fds);
3470 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3474 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3475 s->repo, NULL, NULL);
3479 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3481 err = got_error_set_errno(errcode, "pthread_cond_init");
3484 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3486 err = got_error_set_errno(errcode, "pthread_cond_init");
3490 s->thread_args.commits_needed = view->nlines;
3491 s->thread_args.graph = thread_graph;
3492 s->thread_args.real_commits = &s->real_commits;
3493 s->thread_args.limit_commits = &s->limit_commits;
3494 s->thread_args.in_repo_path = s->in_repo_path;
3495 s->thread_args.start_id = s->start_id;
3496 s->thread_args.repo = thread_repo;
3497 s->thread_args.log_complete = 0;
3498 s->thread_args.quit = &s->quit;
3499 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3500 s->thread_args.selected_entry = &s->selected_entry;
3501 s->thread_args.searching = &view->searching;
3502 s->thread_args.search_next_done = &view->search_next_done;
3503 s->thread_args.regex = &view->regex;
3504 s->thread_args.limiting = &s->limit_view;
3505 s->thread_args.limit_regex = &s->limit_regex;
3506 s->thread_args.limit_commits = &s->limit_commits;
3509 close_log_view(view);
3513 static const struct got_error *
3514 show_log_view(struct tog_view *view)
3516 const struct got_error *err;
3517 struct tog_log_view_state *s = &view->state.log;
3519 if (s->thread == 0) { //NULL) {
3520 int errcode = pthread_create(&s->thread, NULL, log_thread,
3523 return got_error_set_errno(errcode, "pthread_create");
3524 if (s->thread_args.commits_needed > 0) {
3525 err = trigger_log_thread(view, 1);
3531 return draw_commits(view);
3535 log_move_cursor_up(struct tog_view *view, int page, int home)
3537 struct tog_log_view_state *s = &view->state.log;
3539 if (s->first_displayed_entry == NULL)
3541 if (s->selected_entry->idx == 0)
3544 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3546 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3548 if (!page && !home && s->selected > 0)
3551 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3557 static const struct got_error *
3558 log_move_cursor_down(struct tog_view *view, int page)
3560 struct tog_log_view_state *s = &view->state.log;
3561 const struct got_error *err = NULL;
3562 int eos = view->nlines - 2;
3564 if (s->first_displayed_entry == NULL)
3567 if (s->thread_args.log_complete &&
3568 s->selected_entry->idx >= s->commits->ncommits - 1)
3571 if (view_is_hsplit_top(view))
3572 --eos; /* border consumes the last line */
3575 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3578 err = log_scroll_down(view, 1);
3579 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3580 struct commit_queue_entry *entry;
3584 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3585 s->last_displayed_entry = entry;
3586 for (n = 0; n <= eos; n++) {
3589 s->first_displayed_entry = entry;
3590 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3593 s->selected = n - 1;
3595 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3596 s->thread_args.log_complete)
3597 s->selected += MIN(page,
3598 s->commits->ncommits - s->selected_entry->idx - 1);
3600 err = log_scroll_down(view, page);
3606 * We might necessarily overshoot in horizontal
3607 * splits; if so, select the last displayed commit.
3609 if (s->first_displayed_entry && s->last_displayed_entry) {
3610 s->selected = MIN(s->selected,
3611 s->last_displayed_entry->idx -
3612 s->first_displayed_entry->idx);
3617 if (s->thread_args.log_complete &&
3618 s->selected_entry->idx == s->commits->ncommits - 1)
3625 view_get_split(struct tog_view *view, int *y, int *x)
3630 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3631 if (view->child && view->child->resized_y)
3632 *y = view->child->resized_y;
3633 else if (view->resized_y)
3634 *y = view->resized_y;
3636 *y = view_split_begin_y(view->lines);
3637 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3638 if (view->child && view->child->resized_x)
3639 *x = view->child->resized_x;
3640 else if (view->resized_x)
3641 *x = view->resized_x;
3643 *x = view_split_begin_x(view->begin_x);
3647 /* Split view horizontally at y and offset view->state->selected line. */
3648 static const struct got_error *
3649 view_init_hsplit(struct tog_view *view, int y)
3651 const struct got_error *err = NULL;
3655 err = view_resize(view);
3659 err = offset_selection_down(view);
3664 static const struct got_error *
3665 log_goto_line(struct tog_view *view, int nlines)
3667 const struct got_error *err = NULL;
3668 struct tog_log_view_state *s = &view->state.log;
3669 int g, idx = s->selected_entry->idx;
3671 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3677 if (g >= s->first_displayed_entry->idx + 1 &&
3678 g <= s->last_displayed_entry->idx + 1 &&
3679 g - s->first_displayed_entry->idx - 1 < nlines) {
3680 s->selected = g - s->first_displayed_entry->idx - 1;
3686 err = log_move_cursor_down(view, g - idx - 1);
3687 if (!err && g > s->selected_entry->idx + 1)
3688 err = log_move_cursor_down(view,
3689 g - s->first_displayed_entry->idx - 1);
3692 } else if (idx + 1 > g)
3693 log_move_cursor_up(view, idx - g + 1, 0);
3695 if (g < nlines && s->first_displayed_entry->idx == 0)
3696 s->selected = g - 1;
3704 horizontal_scroll_input(struct tog_view *view, int ch)
3710 view->x -= MIN(view->x, 2);
3716 if (view->x + view->ncols / 2 < view->maxx)
3725 view->x = MAX(view->maxx - view->ncols / 2, 0);
3733 static const struct got_error *
3734 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3736 const struct got_error *err = NULL;
3737 struct tog_log_view_state *s = &view->state.log;
3740 if (s->thread_args.load_all) {
3741 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3742 s->thread_args.load_all = 0;
3743 else if (s->thread_args.log_complete) {
3744 err = log_move_cursor_down(view, s->commits->ncommits);
3745 s->thread_args.load_all = 0;
3751 eos = nscroll = view->nlines - 1;
3752 if (view_is_hsplit_top(view))
3756 return log_goto_line(view, eos);
3760 err = limit_log_view(view);
3771 horizontal_scroll_input(view, ch);
3778 log_move_cursor_up(view, 0, 0);
3783 log_move_cursor_up(view, 0, 1);
3793 log_move_cursor_up(view, nscroll, 0);
3800 err = log_move_cursor_down(view, 0);
3803 s->use_committer = !s->use_committer;
3804 view->action = s->use_committer ?
3805 "show committer" : "show commit author";
3810 /* We don't know yet how many commits, so we're forced to
3811 * traverse them all. */
3813 s->thread_args.load_all = 1;
3814 if (!s->thread_args.log_complete)
3815 return trigger_log_thread(view, 0);
3816 err = log_move_cursor_down(view, s->commits->ncommits);
3817 s->thread_args.load_all = 0;
3828 err = log_move_cursor_down(view, nscroll);
3831 if (s->selected > view->nlines - 2)
3832 s->selected = view->nlines - 2;
3833 if (s->selected > s->commits->ncommits - 1)
3834 s->selected = s->commits->ncommits - 1;
3836 if (s->commits->ncommits < view->nlines - 1 &&
3837 !s->thread_args.log_complete) {
3838 s->thread_args.commits_needed += (view->nlines - 1) -
3839 s->commits->ncommits;
3840 err = trigger_log_thread(view, 1);
3846 if (s->selected_entry == NULL)
3848 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3852 if (s->selected_entry == NULL)
3854 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3860 if (ch == KEY_BACKSPACE &&
3861 got_path_is_root_dir(s->in_repo_path))
3863 err = stop_log_thread(s);
3866 if (ch == KEY_BACKSPACE) {
3868 err = got_path_dirname(&parent_path, s->in_repo_path);
3871 free(s->in_repo_path);
3872 s->in_repo_path = parent_path;
3873 s->thread_args.in_repo_path = s->in_repo_path;
3874 } else if (ch == CTRL('l')) {
3875 struct got_object_id *start_id;
3876 err = got_repo_match_object_id(&start_id, NULL,
3877 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3878 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3880 if (s->head_ref_name == NULL ||
3881 err->code != GOT_ERR_NOT_REF)
3883 /* Try to cope with deleted references. */
3884 free(s->head_ref_name);
3885 s->head_ref_name = NULL;
3886 err = got_repo_match_object_id(&start_id,
3887 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3888 &tog_refs, s->repo);
3893 s->start_id = start_id;
3894 s->thread_args.start_id = s->start_id;
3896 s->log_branches = !s->log_branches;
3898 if (s->thread_args.pack_fds == NULL) {
3899 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3903 err = got_repo_open(&s->thread_args.repo,
3904 got_repo_get_path(s->repo), NULL,
3905 s->thread_args.pack_fds);
3909 err = tog_load_refs(s->repo, 0);
3912 err = got_commit_graph_open(&s->thread_args.graph,
3913 s->in_repo_path, !s->log_branches);
3916 err = got_commit_graph_iter_start(s->thread_args.graph,
3917 s->start_id, s->repo, NULL, NULL);
3920 free_commits(&s->real_commits);
3921 free_commits(&s->limit_commits);
3922 s->first_displayed_entry = NULL;
3923 s->last_displayed_entry = NULL;
3924 s->selected_entry = NULL;
3926 s->thread_args.log_complete = 0;
3928 s->thread_args.commits_needed = view->lines;
3929 s->matched_entry = NULL;
3930 s->search_entry = NULL;
3935 err = view_request_new(new_view, view, TOG_VIEW_REF);
3945 static const struct got_error *
3946 apply_unveil(const char *repo_path, const char *worktree_path)
3948 const struct got_error *error;
3951 if (unveil("gmon.out", "rwc") != 0)
3952 return got_error_from_errno2("unveil", "gmon.out");
3954 if (repo_path && unveil(repo_path, "r") != 0)
3955 return got_error_from_errno2("unveil", repo_path);
3957 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3958 return got_error_from_errno2("unveil", worktree_path);
3960 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3961 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3963 error = got_privsep_unveil_exec_helpers();
3967 if (unveil(NULL, NULL) != 0)
3968 return got_error_from_errno("unveil");
3977 * Override default signal handlers before starting ncurses.
3978 * This should prevent ncurses from installing its own
3979 * broken cleanup() signal handler.
3981 signal(SIGWINCH, tog_sigwinch);
3982 signal(SIGPIPE, tog_sigpipe);
3983 signal(SIGCONT, tog_sigcont);
3984 signal(SIGINT, tog_sigint);
3985 signal(SIGTERM, tog_sigterm);
3989 halfdelay(1); /* Do fast refresh while initial view is loading. */
3992 intrflush(stdscr, FALSE);
3993 keypad(stdscr, TRUE);
3995 if (getenv("TOG_COLORS") != NULL) {
3997 use_default_colors();
4001 static const struct got_error *
4002 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4003 struct got_repository *repo, struct got_worktree *worktree)
4005 const struct got_error *err = NULL;
4008 *in_repo_path = strdup("/");
4009 if (*in_repo_path == NULL)
4010 return got_error_from_errno("strdup");
4015 const char *prefix = got_worktree_get_path_prefix(worktree);
4018 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4021 if (asprintf(in_repo_path, "%s%s%s", prefix,
4022 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4024 err = got_error_from_errno("asprintf");
4025 *in_repo_path = NULL;
4029 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4034 static const struct got_error *
4035 cmd_log(int argc, char *argv[])
4037 const struct got_error *error;
4038 struct got_repository *repo = NULL;
4039 struct got_worktree *worktree = NULL;
4040 struct got_object_id *start_id = NULL;
4041 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4042 char *start_commit = NULL, *label = NULL;
4043 struct got_reference *ref = NULL;
4044 const char *head_ref_name = NULL;
4045 int ch, log_branches = 0;
4046 struct tog_view *view;
4047 int *pack_fds = NULL;
4049 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4055 start_commit = optarg;
4058 repo_path = realpath(optarg, NULL);
4059 if (repo_path == NULL)
4060 return got_error_from_errno2("realpath",
4075 error = got_repo_pack_fds_open(&pack_fds);
4079 if (repo_path == NULL) {
4080 cwd = getcwd(NULL, 0);
4082 return got_error_from_errno("getcwd");
4083 error = got_worktree_open(&worktree, cwd);
4084 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4088 strdup(got_worktree_get_repo_path(worktree));
4090 repo_path = strdup(cwd);
4091 if (repo_path == NULL) {
4092 error = got_error_from_errno("strdup");
4097 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4101 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4108 error = apply_unveil(got_repo_get_path(repo),
4109 worktree ? got_worktree_get_root_path(worktree) : NULL);
4113 /* already loaded by tog_log_with_path()? */
4114 if (TAILQ_EMPTY(&tog_refs)) {
4115 error = tog_load_refs(repo, 0);
4120 if (start_commit == NULL) {
4121 error = got_repo_match_object_id(&start_id, &label,
4122 worktree ? got_worktree_get_head_ref_name(worktree) :
4123 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4126 head_ref_name = label;
4128 error = got_ref_open(&ref, repo, start_commit, 0);
4130 head_ref_name = got_ref_get_name(ref);
4131 else if (error->code != GOT_ERR_NOT_REF)
4133 error = got_repo_match_object_id(&start_id, NULL,
4134 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4139 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4141 error = got_error_from_errno("view_open");
4144 error = open_log_view(view, start_id, repo, head_ref_name,
4145 in_repo_path, log_branches);
4149 /* Release work tree lock. */
4150 got_worktree_close(worktree);
4153 error = view_loop(view);
4163 const struct got_error *close_err = got_repo_close(repo);
4168 got_worktree_close(worktree);
4170 const struct got_error *pack_err =
4171 got_repo_pack_fds_close(pack_fds);
4183 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4184 "object1 object2\n", getprogname());
4189 match_line(const char *line, regex_t *regex, size_t nmatch,
4190 regmatch_t *regmatch)
4192 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4195 static struct tog_color *
4196 match_color(struct tog_colors *colors, const char *line)
4198 struct tog_color *tc = NULL;
4200 STAILQ_FOREACH(tc, colors, entry) {
4201 if (match_line(line, &tc->regex, 0, NULL))
4208 static const struct got_error *
4209 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4210 WINDOW *window, int skipcol, regmatch_t *regmatch)
4212 const struct got_error *err = NULL;
4214 wchar_t *wline = NULL;
4215 int rme, rms, n, width, scrollx;
4216 int width0 = 0, width1 = 0, width2 = 0;
4217 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4221 rms = regmatch->rm_so;
4222 rme = regmatch->rm_eo;
4224 err = expand_tab(&exstr, line);
4228 /* Split the line into 3 segments, according to match offsets. */
4229 seg0 = strndup(exstr, rms);
4231 err = got_error_from_errno("strndup");
4234 seg1 = strndup(exstr + rms, rme - rms);
4236 err = got_error_from_errno("strndup");
4239 seg2 = strdup(exstr + rme);
4241 err = got_error_from_errno("strndup");
4245 /* draw up to matched token if we haven't scrolled past it */
4246 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4250 n = MAX(width0 - skipcol, 0);
4253 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4254 wlimit, col_tab_align, 1);
4257 waddwstr(window, &wline[scrollx]);
4267 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4271 wlen = wcslen(wline);
4273 width = wcwidth(wline[i]);
4275 /* should not happen, tabs are expanded */
4276 err = got_error(GOT_ERR_RANGE);
4279 if (width0 + w + width > skipcol)
4284 /* draw (visible part of) matched token (if scrolled into it) */
4285 if (width1 - w > 0) {
4286 wattron(window, A_STANDOUT);
4287 waddwstr(window, &wline[i]);
4288 wattroff(window, A_STANDOUT);
4289 wlimit -= (width1 - w);
4290 *wtotal += (width1 - w);
4294 if (wlimit > 0) { /* draw rest of line */
4296 if (skipcol > width0 + width1) {
4297 err = format_line(&wline, &width2, &scrollx, seg2,
4298 skipcol - (width0 + width1), wlimit,
4302 waddwstr(window, &wline[scrollx]);
4304 err = format_line(&wline, &width2, NULL, seg2, 0,
4305 wlimit, col_tab_align, 1);
4308 waddwstr(window, wline);
4322 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4325 int *eof, *first, *selected;
4327 if (view->type == TOG_VIEW_DIFF) {
4328 struct tog_diff_view_state *s = &view->state.diff;
4330 first = &s->first_displayed_line;
4334 } else if (view->type == TOG_VIEW_HELP) {
4335 struct tog_help_view_state *s = &view->state.help;
4337 first = &s->first_displayed_line;
4341 } else if (view->type == TOG_VIEW_BLAME) {
4342 struct tog_blame_view_state *s = &view->state.blame;
4344 first = &s->first_displayed_line;
4345 selected = &s->selected_line;
4351 /* Center gline in the middle of the page like vi(1). */
4352 if (*lineno < view->gline - (view->nlines - 3) / 2)
4354 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4363 *selected = view->gline <= (view->nlines - 3) / 2 ?
4364 view->gline : (view->nlines - 3) / 2 + 1;
4370 static const struct got_error *
4371 draw_file(struct tog_view *view, const char *header)
4373 struct tog_diff_view_state *s = &view->state.diff;
4374 regmatch_t *regmatch = &view->regmatch;
4375 const struct got_error *err;
4378 size_t linesize = 0;
4382 int max_lines = view->nlines;
4383 int nlines = s->nlines;
4386 s->lineno = s->first_displayed_line - 1;
4387 line_offset = s->lines[s->first_displayed_line - 1].offset;
4388 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4389 return got_error_from_errno("fseek");
4391 werase(view->window);
4393 if (view->gline > s->nlines - 1)
4394 view->gline = s->nlines - 1;
4397 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4398 1 : view->gline - (view->nlines - 3) / 2 :
4399 s->lineno + s->selected_line;
4401 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4402 return got_error_from_errno("asprintf");
4403 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4409 if (view_needs_focus_indication(view))
4410 wstandout(view->window);
4411 waddwstr(view->window, wline);
4414 while (width++ < view->ncols)
4415 waddch(view->window, ' ');
4416 if (view_needs_focus_indication(view))
4417 wstandend(view->window);
4427 while (max_lines > 0 && nprinted < max_lines) {
4428 enum got_diff_line_type linetype;
4431 linelen = getline(&line, &linesize, s->f);
4432 if (linelen == -1) {
4438 return got_ferror(s->f, GOT_ERR_IO);
4441 if (++s->lineno < s->first_displayed_line)
4443 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4445 if (s->lineno == view->hiline)
4448 /* Set view->maxx based on full line length. */
4449 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4455 view->maxx = MAX(view->maxx, width);
4459 linetype = s->lines[s->lineno].type;
4460 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4461 linetype < GOT_DIFF_LINE_CONTEXT)
4462 attr |= COLOR_PAIR(linetype);
4464 wattron(view->window, attr);
4465 if (s->first_displayed_line + nprinted == s->matched_line &&
4466 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4467 err = add_matched_line(&width, line, view->ncols, 0,
4468 view->window, view->x, regmatch);
4475 err = format_line(&wline, &width, &skip, line,
4476 view->x, view->ncols, 0, view->x ? 1 : 0);
4481 waddwstr(view->window, &wline[skip]);
4485 if (s->lineno == view->hiline) {
4486 /* highlight full gline length */
4487 while (width++ < view->ncols)
4488 waddch(view->window, ' ');
4490 if (width <= view->ncols - 1)
4491 waddch(view->window, '\n');
4494 wattroff(view->window, attr);
4495 if (++nprinted == 1)
4496 s->first_displayed_line = s->lineno;
4500 s->last_displayed_line = s->first_displayed_line +
4503 s->last_displayed_line = s->first_displayed_line;
4508 while (nprinted < view->nlines) {
4509 waddch(view->window, '\n');
4513 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4519 wstandout(view->window);
4520 waddwstr(view->window, wline);
4523 wstandend(view->window);
4530 get_datestr(time_t *time, char *datebuf)
4532 struct tm mytm, *tm;
4535 tm = gmtime_r(time, &mytm);
4538 s = asctime_r(tm, datebuf);
4541 p = strchr(s, '\n');
4547 static const struct got_error *
4548 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4549 off_t off, uint8_t type)
4551 struct got_diff_line *p;
4553 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4555 return got_error_from_errno("reallocarray");
4557 (*lines)[*nlines].offset = off;
4558 (*lines)[*nlines].type = type;
4564 static const struct got_error *
4565 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4566 struct got_diff_line *s_lines, size_t s_nlines)
4568 struct got_diff_line *p;
4572 if (fseeko(src, 0L, SEEK_SET) == -1)
4573 return got_error_from_errno("fseeko");
4576 r = fread(buf, 1, sizeof(buf), src);
4579 return got_error_from_errno("fread");
4583 if (fwrite(buf, 1, r, dst) != r)
4584 return got_ferror(dst, GOT_ERR_IO);
4587 if (s_nlines == 0 && *d_nlines == 0)
4591 * If commit info was in dst, increment line offsets
4592 * of the appended diff content, but skip s_lines[0]
4593 * because offset zero is already in *d_lines.
4595 if (*d_nlines > 0) {
4596 for (i = 1; i < s_nlines; ++i)
4597 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4605 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4607 /* d_lines is freed in close_diff_view() */
4608 return got_error_from_errno("reallocarray");
4613 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4614 *d_nlines += s_nlines;
4619 static const struct got_error *
4620 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4621 struct got_object_id *commit_id, struct got_reflist_head *refs,
4622 struct got_repository *repo, int ignore_ws, int force_text_diff,
4623 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4625 const struct got_error *err = NULL;
4626 char datebuf[26], *datestr;
4627 struct got_commit_object *commit;
4628 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4629 time_t committer_time;
4630 const char *author, *committer;
4631 char *refs_str = NULL;
4632 struct got_pathlist_entry *pe;
4637 err = build_refs_str(&refs_str, refs, commit_id, repo);
4642 err = got_object_open_as_commit(&commit, repo, commit_id);
4646 err = got_object_id_str(&id_str, commit_id);
4648 err = got_error_from_errno("got_object_id_str");
4652 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4656 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4657 refs_str ? refs_str : "", refs_str ? ")" : "");
4659 err = got_error_from_errno("fprintf");
4663 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4667 n = fprintf(outfile, "from: %s\n",
4668 got_object_commit_get_author(commit));
4670 err = got_error_from_errno("fprintf");
4674 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4678 author = got_object_commit_get_author(commit);
4679 committer = got_object_commit_get_committer(commit);
4680 if (strcmp(author, committer) != 0) {
4681 n = fprintf(outfile, "via: %s\n", committer);
4683 err = got_error_from_errno("fprintf");
4687 err = add_line_metadata(lines, nlines, outoff,
4688 GOT_DIFF_LINE_AUTHOR);
4692 committer_time = got_object_commit_get_committer_time(commit);
4693 datestr = get_datestr(&committer_time, datebuf);
4695 n = fprintf(outfile, "date: %s UTC\n", datestr);
4697 err = got_error_from_errno("fprintf");
4701 err = add_line_metadata(lines, nlines, outoff,
4702 GOT_DIFF_LINE_DATE);
4706 if (got_object_commit_get_nparents(commit) > 1) {
4707 const struct got_object_id_queue *parent_ids;
4708 struct got_object_qid *qid;
4710 parent_ids = got_object_commit_get_parent_ids(commit);
4711 STAILQ_FOREACH(qid, parent_ids, entry) {
4712 err = got_object_id_str(&id_str, &qid->id);
4715 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4717 err = got_error_from_errno("fprintf");
4721 err = add_line_metadata(lines, nlines, outoff,
4722 GOT_DIFF_LINE_META);
4730 err = got_object_commit_get_logmsg(&logmsg, commit);
4734 while ((line = strsep(&s, "\n")) != NULL) {
4735 n = fprintf(outfile, "%s\n", line);
4737 err = got_error_from_errno("fprintf");
4741 err = add_line_metadata(lines, nlines, outoff,
4742 GOT_DIFF_LINE_LOGMSG);
4747 TAILQ_FOREACH(pe, dsa->paths, entry) {
4748 struct got_diff_changed_path *cp = pe->data;
4749 int pad = dsa->max_path_len - pe->path_len + 1;
4751 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4752 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4753 dsa->rm_cols + 1, cp->rm);
4755 err = got_error_from_errno("fprintf");
4759 err = add_line_metadata(lines, nlines, outoff,
4760 GOT_DIFF_LINE_CHANGES);
4765 fputc('\n', outfile);
4767 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4771 n = fprintf(outfile,
4772 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4773 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4774 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4776 err = got_error_from_errno("fprintf");
4780 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4784 fputc('\n', outfile);
4786 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4791 got_object_commit_close(commit);
4800 static const struct got_error *
4801 create_diff(struct tog_diff_view_state *s)
4803 const struct got_error *err = NULL;
4804 FILE *f = NULL, *tmp_diff_file = NULL;
4806 struct got_diff_line *lines = NULL;
4807 struct got_pathlist_head changed_paths;
4809 TAILQ_INIT(&changed_paths);
4812 s->lines = malloc(sizeof(*s->lines));
4813 if (s->lines == NULL)
4814 return got_error_from_errno("malloc");
4819 err = got_error_from_errno("got_opentemp");
4822 tmp_diff_file = got_opentemp();
4823 if (tmp_diff_file == NULL) {
4824 err = got_error_from_errno("got_opentemp");
4827 if (s->f && fclose(s->f) == EOF) {
4828 err = got_error_from_errno("fclose");
4834 err = got_object_get_type(&obj_type, s->repo, s->id1);
4836 err = got_object_get_type(&obj_type, s->repo, s->id2);
4841 case GOT_OBJ_TYPE_BLOB:
4842 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4843 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4844 s->label1, s->label2, tog_diff_algo, s->diff_context,
4845 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4848 case GOT_OBJ_TYPE_TREE:
4849 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4850 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4851 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4852 s->force_text_diff, NULL, s->repo, s->f);
4854 case GOT_OBJ_TYPE_COMMIT: {
4855 const struct got_object_id_queue *parent_ids;
4856 struct got_object_qid *pid;
4857 struct got_commit_object *commit2;
4858 struct got_reflist_head *refs;
4860 struct got_diffstat_cb_arg dsa = {
4863 s->ignore_whitespace,
4868 lines = malloc(sizeof(*lines));
4869 if (lines == NULL) {
4870 err = got_error_from_errno("malloc");
4874 /* build diff first in tmp file then append to commit info */
4875 err = got_diff_objects_as_commits(&lines, &nlines,
4876 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4877 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4878 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4882 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4885 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4886 /* Show commit info if we're diffing to a parent/root commit. */
4887 if (s->id1 == NULL) {
4888 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4889 refs, s->repo, s->ignore_whitespace,
4890 s->force_text_diff, &dsa, s->f);
4894 parent_ids = got_object_commit_get_parent_ids(commit2);
4895 STAILQ_FOREACH(pid, parent_ids, entry) {
4896 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4897 err = write_commit_info(&s->lines,
4898 &s->nlines, s->id2, refs, s->repo,
4899 s->ignore_whitespace,
4900 s->force_text_diff, &dsa, s->f);
4907 got_object_commit_close(commit2);
4909 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4914 err = got_error(GOT_ERR_OBJ_TYPE);
4919 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4920 if (s->f && fflush(s->f) != 0 && err == NULL)
4921 err = got_error_from_errno("fflush");
4922 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4923 err = got_error_from_errno("fclose");
4928 diff_view_indicate_progress(struct tog_view *view)
4930 mvwaddstr(view->window, 0, 0, "diffing...");
4935 static const struct got_error *
4936 search_start_diff_view(struct tog_view *view)
4938 struct tog_diff_view_state *s = &view->state.diff;
4940 s->matched_line = 0;
4945 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4946 size_t *nlines, int **first, int **last, int **match, int **selected)
4948 struct tog_diff_view_state *s = &view->state.diff;
4951 *nlines = s->nlines;
4952 *line_offsets = NULL;
4953 *match = &s->matched_line;
4954 *first = &s->first_displayed_line;
4955 *last = &s->last_displayed_line;
4956 *selected = &s->selected_line;
4959 static const struct got_error *
4960 search_next_view_match(struct tog_view *view)
4962 const struct got_error *err = NULL;
4966 size_t linesize = 0;
4968 off_t *line_offsets;
4970 int *first, *last, *match, *selected;
4972 if (!view->search_setup)
4973 return got_error_msg(GOT_ERR_NOT_IMPL,
4974 "view search not supported");
4975 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4978 if (!view->searching) {
4979 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4984 if (view->searching == TOG_SEARCH_FORWARD)
4985 lineno = *match + 1;
4987 lineno = *match - 1;
4989 lineno = *first - 1 + *selected;
4994 if (lineno <= 0 || lineno > nlines) {
4996 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5000 if (view->searching == TOG_SEARCH_FORWARD)
5006 offset = view->type == TOG_VIEW_DIFF ?
5007 view->state.diff.lines[lineno - 1].offset :
5008 line_offsets[lineno - 1];
5009 if (fseeko(f, offset, SEEK_SET) != 0) {
5011 return got_error_from_errno("fseeko");
5013 linelen = getline(&line, &linesize, f);
5014 if (linelen != -1) {
5016 err = expand_tab(&exstr, line);
5019 if (match_line(exstr, &view->regex, 1,
5021 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5028 if (view->searching == TOG_SEARCH_FORWARD)
5043 static const struct got_error *
5044 close_diff_view(struct tog_view *view)
5046 const struct got_error *err = NULL;
5047 struct tog_diff_view_state *s = &view->state.diff;
5053 if (s->f && fclose(s->f) == EOF)
5054 err = got_error_from_errno("fclose");
5056 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5057 err = got_error_from_errno("fclose");
5059 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5060 err = got_error_from_errno("fclose");
5062 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5063 err = got_error_from_errno("close");
5065 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5066 err = got_error_from_errno("close");
5074 static const struct got_error *
5075 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5076 struct got_object_id *id2, const char *label1, const char *label2,
5077 int diff_context, int ignore_whitespace, int force_text_diff,
5078 struct tog_view *parent_view, struct got_repository *repo)
5080 const struct got_error *err;
5081 struct tog_diff_view_state *s = &view->state.diff;
5083 memset(s, 0, sizeof(*s));
5087 if (id1 != NULL && id2 != NULL) {
5089 err = got_object_get_type(&type1, repo, id1);
5092 err = got_object_get_type(&type2, repo, id2);
5097 return got_error(GOT_ERR_OBJ_TYPE);
5099 s->first_displayed_line = 1;
5100 s->last_displayed_line = view->nlines;
5101 s->selected_line = 1;
5109 s->id1 = got_object_id_dup(id1);
5111 return got_error_from_errno("got_object_id_dup");
5115 s->id2 = got_object_id_dup(id2);
5116 if (s->id2 == NULL) {
5117 err = got_error_from_errno("got_object_id_dup");
5121 s->f1 = got_opentemp();
5122 if (s->f1 == NULL) {
5123 err = got_error_from_errno("got_opentemp");
5127 s->f2 = got_opentemp();
5128 if (s->f2 == NULL) {
5129 err = got_error_from_errno("got_opentemp");
5133 s->fd1 = got_opentempfd();
5135 err = got_error_from_errno("got_opentempfd");
5139 s->fd2 = got_opentempfd();
5141 err = got_error_from_errno("got_opentempfd");
5145 s->diff_context = diff_context;
5146 s->ignore_whitespace = ignore_whitespace;
5147 s->force_text_diff = force_text_diff;
5148 s->parent_view = parent_view;
5151 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5154 rc = init_pair(GOT_DIFF_LINE_MINUS,
5155 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5157 rc = init_pair(GOT_DIFF_LINE_PLUS,
5158 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5160 rc = init_pair(GOT_DIFF_LINE_HUNK,
5161 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5163 rc = init_pair(GOT_DIFF_LINE_META,
5164 get_color_value("TOG_COLOR_DIFF_META"), -1);
5166 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5167 get_color_value("TOG_COLOR_DIFF_META"), -1);
5169 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5170 get_color_value("TOG_COLOR_DIFF_META"), -1);
5172 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5173 get_color_value("TOG_COLOR_DIFF_META"), -1);
5175 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5176 get_color_value("TOG_COLOR_AUTHOR"), -1);
5178 rc = init_pair(GOT_DIFF_LINE_DATE,
5179 get_color_value("TOG_COLOR_DATE"), -1);
5181 err = got_error(GOT_ERR_RANGE);
5186 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5187 view_is_splitscreen(view))
5188 show_log_view(parent_view); /* draw border */
5189 diff_view_indicate_progress(view);
5191 err = create_diff(s);
5193 view->show = show_diff_view;
5194 view->input = input_diff_view;
5195 view->reset = reset_diff_view;
5196 view->close = close_diff_view;
5197 view->search_start = search_start_diff_view;
5198 view->search_setup = search_setup_diff_view;
5199 view->search_next = search_next_view_match;
5202 close_diff_view(view);
5206 static const struct got_error *
5207 show_diff_view(struct tog_view *view)
5209 const struct got_error *err;
5210 struct tog_diff_view_state *s = &view->state.diff;
5211 char *id_str1 = NULL, *id_str2, *header;
5212 const char *label1, *label2;
5215 err = got_object_id_str(&id_str1, s->id1);
5218 label1 = s->label1 ? s->label1 : id_str1;
5220 label1 = "/dev/null";
5222 err = got_object_id_str(&id_str2, s->id2);
5225 label2 = s->label2 ? s->label2 : id_str2;
5227 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5228 err = got_error_from_errno("asprintf");
5236 err = draw_file(view, header);
5241 static const struct got_error *
5242 set_selected_commit(struct tog_diff_view_state *s,
5243 struct commit_queue_entry *entry)
5245 const struct got_error *err;
5246 const struct got_object_id_queue *parent_ids;
5247 struct got_commit_object *selected_commit;
5248 struct got_object_qid *pid;
5251 s->id2 = got_object_id_dup(entry->id);
5253 return got_error_from_errno("got_object_id_dup");
5255 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5258 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5260 pid = STAILQ_FIRST(parent_ids);
5261 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5262 got_object_commit_close(selected_commit);
5266 static const struct got_error *
5267 reset_diff_view(struct tog_view *view)
5269 struct tog_diff_view_state *s = &view->state.diff;
5272 wclear(view->window);
5273 s->first_displayed_line = 1;
5274 s->last_displayed_line = view->nlines;
5275 s->matched_line = 0;
5276 diff_view_indicate_progress(view);
5277 return create_diff(s);
5281 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5285 i = start = s->first_displayed_line - 1;
5287 while (s->lines[i].type != type) {
5291 return; /* do nothing, requested type not in file */
5294 s->selected_line = 1;
5295 s->first_displayed_line = i;
5299 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5303 i = start = s->first_displayed_line + 1;
5305 while (s->lines[i].type != type) {
5306 if (i == s->nlines - 1)
5309 return; /* do nothing, requested type not in file */
5312 s->selected_line = 1;
5313 s->first_displayed_line = i;
5316 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5318 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5321 static const struct got_error *
5322 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5324 const struct got_error *err = NULL;
5325 struct tog_diff_view_state *s = &view->state.diff;
5326 struct tog_log_view_state *ls;
5327 struct commit_queue_entry *old_selected_entry;
5329 size_t linesize = 0;
5331 int i, nscroll = view->nlines - 1, up = 0;
5333 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5342 horizontal_scroll_input(view, ch);
5347 s->force_text_diff = !s->force_text_diff;
5348 view->action = s->force_text_diff ?
5349 "force ASCII text enabled" :
5350 "force ASCII text disabled";
5352 else if (ch == 'w') {
5353 s->ignore_whitespace = !s->ignore_whitespace;
5354 view->action = s->ignore_whitespace ?
5355 "ignore whitespace enabled" :
5356 "ignore whitespace disabled";
5358 err = reset_diff_view(view);
5362 s->first_displayed_line = 1;
5371 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5377 if (s->first_displayed_line > 1)
5378 s->first_displayed_line--;
5389 if (s->first_displayed_line == 1) {
5394 while (i++ < nscroll && s->first_displayed_line > 1)
5395 s->first_displayed_line--;
5401 s->first_displayed_line++;
5418 while (!s->eof && i++ < nscroll) {
5419 linelen = getline(&line, &linesize, s->f);
5420 s->first_displayed_line++;
5421 if (linelen == -1) {
5425 err = got_ferror(s->f, GOT_ERR_IO);
5432 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5435 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5438 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5441 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5444 if (s->diff_context > 0) {
5446 s->matched_line = 0;
5447 diff_view_indicate_progress(view);
5448 err = create_diff(s);
5449 if (s->first_displayed_line + view->nlines - 1 >
5451 s->first_displayed_line = 1;
5452 s->last_displayed_line = view->nlines;
5458 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5460 s->matched_line = 0;
5461 diff_view_indicate_progress(view);
5462 err = create_diff(s);
5474 if (s->parent_view == NULL) {
5478 s->parent_view->count = view->count;
5480 if (s->parent_view->type == TOG_VIEW_LOG) {
5481 ls = &s->parent_view->state.log;
5482 old_selected_entry = ls->selected_entry;
5484 err = input_log_view(NULL, s->parent_view,
5485 up ? KEY_UP : KEY_DOWN);
5488 view->count = s->parent_view->count;
5490 if (old_selected_entry == ls->selected_entry)
5493 err = set_selected_commit(s, ls->selected_entry);
5496 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5497 struct tog_blame_view_state *bs;
5498 struct got_object_id *id, *prev_id;
5500 bs = &s->parent_view->state.blame;
5501 prev_id = get_annotation_for_line(bs->blame.lines,
5502 bs->blame.nlines, bs->last_diffed_line);
5504 err = input_blame_view(&view, s->parent_view,
5505 up ? KEY_UP : KEY_DOWN);
5508 view->count = s->parent_view->count;
5510 if (prev_id == NULL)
5512 id = get_selected_commit_id(bs->blame.lines,
5513 bs->blame.nlines, bs->first_displayed_line,
5518 if (!got_object_id_cmp(prev_id, id))
5521 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5525 s->first_displayed_line = 1;
5526 s->last_displayed_line = view->nlines;
5527 s->matched_line = 0;
5530 diff_view_indicate_progress(view);
5531 err = create_diff(s);
5541 static const struct got_error *
5542 cmd_diff(int argc, char *argv[])
5544 const struct got_error *error = NULL;
5545 struct got_repository *repo = NULL;
5546 struct got_worktree *worktree = NULL;
5547 struct got_object_id *id1 = NULL, *id2 = NULL;
5548 char *repo_path = NULL, *cwd = NULL;
5549 char *id_str1 = NULL, *id_str2 = NULL;
5550 char *label1 = NULL, *label2 = NULL;
5551 int diff_context = 3, ignore_whitespace = 0;
5552 int ch, force_text_diff = 0;
5554 struct tog_view *view;
5555 int *pack_fds = NULL;
5557 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5560 force_text_diff = 1;
5563 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5566 errx(1, "number of context lines is %s: %s",
5570 repo_path = realpath(optarg, NULL);
5571 if (repo_path == NULL)
5572 return got_error_from_errno2("realpath",
5574 got_path_strip_trailing_slashes(repo_path);
5577 ignore_whitespace = 1;
5589 usage_diff(); /* TODO show local worktree changes */
5590 } else if (argc == 2) {
5596 error = got_repo_pack_fds_open(&pack_fds);
5600 if (repo_path == NULL) {
5601 cwd = getcwd(NULL, 0);
5603 return got_error_from_errno("getcwd");
5604 error = got_worktree_open(&worktree, cwd);
5605 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5609 strdup(got_worktree_get_repo_path(worktree));
5611 repo_path = strdup(cwd);
5612 if (repo_path == NULL) {
5613 error = got_error_from_errno("strdup");
5618 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5624 error = apply_unveil(got_repo_get_path(repo), NULL);
5628 error = tog_load_refs(repo, 0);
5632 error = got_repo_match_object_id(&id1, &label1, id_str1,
5633 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5637 error = got_repo_match_object_id(&id2, &label2, id_str2,
5638 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5642 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5644 error = got_error_from_errno("view_open");
5647 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5648 ignore_whitespace, force_text_diff, NULL, repo);
5651 error = view_loop(view);
5658 const struct got_error *close_err = got_repo_close(repo);
5663 got_worktree_close(worktree);
5665 const struct got_error *pack_err =
5666 got_repo_pack_fds_close(pack_fds);
5679 "usage: %s blame [-c commit] [-r repository-path] path\n",
5684 struct tog_blame_line {
5686 struct got_object_id *id;
5689 static const struct got_error *
5690 draw_blame(struct tog_view *view)
5692 struct tog_blame_view_state *s = &view->state.blame;
5693 struct tog_blame *blame = &s->blame;
5694 regmatch_t *regmatch = &view->regmatch;
5695 const struct got_error *err;
5696 int lineno = 0, nprinted = 0;
5698 size_t linesize = 0;
5702 struct tog_blame_line *blame_line;
5703 struct got_object_id *prev_id = NULL;
5705 struct tog_color *tc;
5707 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5712 werase(view->window);
5714 if (asprintf(&line, "commit %s", id_str) == -1) {
5715 err = got_error_from_errno("asprintf");
5720 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5725 if (view_needs_focus_indication(view))
5726 wstandout(view->window);
5727 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5729 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5730 waddwstr(view->window, wline);
5731 while (width++ < view->ncols)
5732 waddch(view->window, ' ');
5734 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5735 if (view_needs_focus_indication(view))
5736 wstandend(view->window);
5740 if (view->gline > blame->nlines)
5741 view->gline = blame->nlines;
5743 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5744 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5745 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5747 return got_error_from_errno("asprintf");
5750 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5755 waddwstr(view->window, wline);
5758 if (width < view->ncols - 1)
5759 waddch(view->window, '\n');
5763 while (nprinted < view->nlines - 2) {
5764 linelen = getline(&line, &linesize, blame->f);
5765 if (linelen == -1) {
5766 if (feof(blame->f)) {
5771 return got_ferror(blame->f, GOT_ERR_IO);
5773 if (++lineno < s->first_displayed_line)
5775 if (view->gline && !gotoline(view, &lineno, &nprinted))
5778 /* Set view->maxx based on full line length. */
5779 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5786 view->maxx = MAX(view->maxx, width);
5788 if (nprinted == s->selected_line - 1)
5789 wstandout(view->window);
5791 if (blame->nlines > 0) {
5792 blame_line = &blame->lines[lineno - 1];
5793 if (blame_line->annotated && prev_id &&
5794 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5795 !(nprinted == s->selected_line - 1)) {
5796 waddstr(view->window, " ");
5797 } else if (blame_line->annotated) {
5799 err = got_object_id_str(&id_str,
5805 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5807 wattr_on(view->window,
5808 COLOR_PAIR(tc->colorpair), NULL);
5809 wprintw(view->window, "%.8s", id_str);
5811 wattr_off(view->window,
5812 COLOR_PAIR(tc->colorpair), NULL);
5814 prev_id = blame_line->id;
5816 waddstr(view->window, "........");
5820 waddstr(view->window, "........");
5824 if (nprinted == s->selected_line - 1)
5825 wstandend(view->window);
5826 waddstr(view->window, " ");
5828 if (view->ncols <= 9) {
5830 } else if (s->first_displayed_line + nprinted ==
5832 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5833 err = add_matched_line(&width, line, view->ncols - 9, 9,
5834 view->window, view->x, regmatch);
5842 err = format_line(&wline, &width, &skip, line,
5843 view->x, view->ncols - 9, 9, 1);
5848 waddwstr(view->window, &wline[skip]);
5854 if (width <= view->ncols - 1)
5855 waddch(view->window, '\n');
5856 if (++nprinted == 1)
5857 s->first_displayed_line = lineno;
5860 s->last_displayed_line = lineno;
5867 static const struct got_error *
5868 blame_cb(void *arg, int nlines, int lineno,
5869 struct got_commit_object *commit, struct got_object_id *id)
5871 const struct got_error *err = NULL;
5872 struct tog_blame_cb_args *a = arg;
5873 struct tog_blame_line *line;
5876 if (nlines != a->nlines ||
5877 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5878 return got_error(GOT_ERR_RANGE);
5880 errcode = pthread_mutex_lock(&tog_mutex);
5882 return got_error_set_errno(errcode, "pthread_mutex_lock");
5884 if (*a->quit) { /* user has quit the blame view */
5885 err = got_error(GOT_ERR_ITER_COMPLETED);
5890 goto done; /* no change in this commit */
5892 line = &a->lines[lineno - 1];
5893 if (line->annotated)
5896 line->id = got_object_id_dup(id);
5897 if (line->id == NULL) {
5898 err = got_error_from_errno("got_object_id_dup");
5901 line->annotated = 1;
5903 errcode = pthread_mutex_unlock(&tog_mutex);
5905 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5910 blame_thread(void *arg)
5912 const struct got_error *err, *close_err;
5913 struct tog_blame_thread_args *ta = arg;
5914 struct tog_blame_cb_args *a = ta->cb_args;
5915 int errcode, fd1 = -1, fd2 = -1;
5916 FILE *f1 = NULL, *f2 = NULL;
5918 fd1 = got_opentempfd();
5920 return (void *)got_error_from_errno("got_opentempfd");
5922 fd2 = got_opentempfd();
5924 err = got_error_from_errno("got_opentempfd");
5928 f1 = got_opentemp();
5930 err = (void *)got_error_from_errno("got_opentemp");
5933 f2 = got_opentemp();
5935 err = (void *)got_error_from_errno("got_opentemp");
5939 err = block_signals_used_by_main_thread();
5943 err = got_blame(ta->path, a->commit_id, ta->repo,
5944 tog_diff_algo, blame_cb, ta->cb_args,
5945 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5946 if (err && err->code == GOT_ERR_CANCELLED)
5949 errcode = pthread_mutex_lock(&tog_mutex);
5951 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5955 close_err = got_repo_close(ta->repo);
5961 errcode = pthread_mutex_unlock(&tog_mutex);
5962 if (errcode && err == NULL)
5963 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5966 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5967 err = got_error_from_errno("close");
5968 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5969 err = got_error_from_errno("close");
5970 if (f1 && fclose(f1) == EOF && err == NULL)
5971 err = got_error_from_errno("fclose");
5972 if (f2 && fclose(f2) == EOF && err == NULL)
5973 err = got_error_from_errno("fclose");
5978 static struct got_object_id *
5979 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5980 int first_displayed_line, int selected_line)
5982 struct tog_blame_line *line;
5987 line = &lines[first_displayed_line - 1 + selected_line - 1];
5988 if (!line->annotated)
5994 static struct got_object_id *
5995 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5998 struct tog_blame_line *line;
6000 if (nlines <= 0 || lineno >= nlines)
6003 line = &lines[lineno - 1];
6004 if (!line->annotated)
6010 static const struct got_error *
6011 stop_blame(struct tog_blame *blame)
6013 const struct got_error *err = NULL;
6016 if (blame->thread) {
6018 errcode = pthread_mutex_unlock(&tog_mutex);
6020 return got_error_set_errno(errcode,
6021 "pthread_mutex_unlock");
6022 errcode = pthread_join(blame->thread, (void **)&err);
6024 return got_error_set_errno(errcode, "pthread_join");
6025 errcode = pthread_mutex_lock(&tog_mutex);
6027 return got_error_set_errno(errcode,
6028 "pthread_mutex_lock");
6029 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6031 blame->thread = 0; //NULL;
6033 if (blame->thread_args.repo) {
6034 const struct got_error *close_err;
6035 close_err = got_repo_close(blame->thread_args.repo);
6038 blame->thread_args.repo = NULL;
6041 if (fclose(blame->f) == EOF && err == NULL)
6042 err = got_error_from_errno("fclose");
6046 for (i = 0; i < blame->nlines; i++)
6047 free(blame->lines[i].id);
6049 blame->lines = NULL;
6051 free(blame->cb_args.commit_id);
6052 blame->cb_args.commit_id = NULL;
6053 if (blame->pack_fds) {
6054 const struct got_error *pack_err =
6055 got_repo_pack_fds_close(blame->pack_fds);
6058 blame->pack_fds = NULL;
6063 static const struct got_error *
6064 cancel_blame_view(void *arg)
6066 const struct got_error *err = NULL;
6070 errcode = pthread_mutex_lock(&tog_mutex);
6072 return got_error_set_errno(errcode,
6073 "pthread_mutex_unlock");
6076 err = got_error(GOT_ERR_CANCELLED);
6078 errcode = pthread_mutex_unlock(&tog_mutex);
6080 return got_error_set_errno(errcode,
6081 "pthread_mutex_lock");
6086 static const struct got_error *
6087 run_blame(struct tog_view *view)
6089 struct tog_blame_view_state *s = &view->state.blame;
6090 struct tog_blame *blame = &s->blame;
6091 const struct got_error *err = NULL;
6092 struct got_commit_object *commit = NULL;
6093 struct got_blob_object *blob = NULL;
6094 struct got_repository *thread_repo = NULL;
6095 struct got_object_id *obj_id = NULL;
6096 int obj_type, fd = -1;
6097 int *pack_fds = NULL;
6099 err = got_object_open_as_commit(&commit, s->repo,
6100 &s->blamed_commit->id);
6104 fd = got_opentempfd();
6106 err = got_error_from_errno("got_opentempfd");
6110 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6114 err = got_object_get_type(&obj_type, s->repo, obj_id);
6118 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6119 err = got_error(GOT_ERR_OBJ_TYPE);
6123 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6126 blame->f = got_opentemp();
6127 if (blame->f == NULL) {
6128 err = got_error_from_errno("got_opentemp");
6131 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6132 &blame->line_offsets, blame->f, blob);
6135 if (blame->nlines == 0) {
6136 s->blame_complete = 1;
6140 /* Don't include \n at EOF in the blame line count. */
6141 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6144 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6145 if (blame->lines == NULL) {
6146 err = got_error_from_errno("calloc");
6150 err = got_repo_pack_fds_open(&pack_fds);
6153 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6158 blame->pack_fds = pack_fds;
6159 blame->cb_args.view = view;
6160 blame->cb_args.lines = blame->lines;
6161 blame->cb_args.nlines = blame->nlines;
6162 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6163 if (blame->cb_args.commit_id == NULL) {
6164 err = got_error_from_errno("got_object_id_dup");
6167 blame->cb_args.quit = &s->done;
6169 blame->thread_args.path = s->path;
6170 blame->thread_args.repo = thread_repo;
6171 blame->thread_args.cb_args = &blame->cb_args;
6172 blame->thread_args.complete = &s->blame_complete;
6173 blame->thread_args.cancel_cb = cancel_blame_view;
6174 blame->thread_args.cancel_arg = &s->done;
6175 s->blame_complete = 0;
6177 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6178 s->first_displayed_line = 1;
6179 s->last_displayed_line = view->nlines;
6180 s->selected_line = 1;
6182 s->matched_line = 0;
6186 got_object_commit_close(commit);
6187 if (fd != -1 && close(fd) == -1 && err == NULL)
6188 err = got_error_from_errno("close");
6190 got_object_blob_close(blob);
6197 static const struct got_error *
6198 open_blame_view(struct tog_view *view, char *path,
6199 struct got_object_id *commit_id, struct got_repository *repo)
6201 const struct got_error *err = NULL;
6202 struct tog_blame_view_state *s = &view->state.blame;
6204 STAILQ_INIT(&s->blamed_commits);
6206 s->path = strdup(path);
6207 if (s->path == NULL)
6208 return got_error_from_errno("strdup");
6210 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6216 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6217 s->first_displayed_line = 1;
6218 s->last_displayed_line = view->nlines;
6219 s->selected_line = 1;
6220 s->blame_complete = 0;
6222 s->commit_id = commit_id;
6223 memset(&s->blame, 0, sizeof(s->blame));
6225 STAILQ_INIT(&s->colors);
6226 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6227 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6228 get_color_value("TOG_COLOR_COMMIT"));
6233 view->show = show_blame_view;
6234 view->input = input_blame_view;
6235 view->reset = reset_blame_view;
6236 view->close = close_blame_view;
6237 view->search_start = search_start_blame_view;
6238 view->search_setup = search_setup_blame_view;
6239 view->search_next = search_next_view_match;
6241 return run_blame(view);
6244 static const struct got_error *
6245 close_blame_view(struct tog_view *view)
6247 const struct got_error *err = NULL;
6248 struct tog_blame_view_state *s = &view->state.blame;
6250 if (s->blame.thread)
6251 err = stop_blame(&s->blame);
6253 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6254 struct got_object_qid *blamed_commit;
6255 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6256 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6257 got_object_qid_free(blamed_commit);
6261 free_colors(&s->colors);
6265 static const struct got_error *
6266 search_start_blame_view(struct tog_view *view)
6268 struct tog_blame_view_state *s = &view->state.blame;
6270 s->matched_line = 0;
6275 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6276 size_t *nlines, int **first, int **last, int **match, int **selected)
6278 struct tog_blame_view_state *s = &view->state.blame;
6281 *nlines = s->blame.nlines;
6282 *line_offsets = s->blame.line_offsets;
6283 *match = &s->matched_line;
6284 *first = &s->first_displayed_line;
6285 *last = &s->last_displayed_line;
6286 *selected = &s->selected_line;
6289 static const struct got_error *
6290 show_blame_view(struct tog_view *view)
6292 const struct got_error *err = NULL;
6293 struct tog_blame_view_state *s = &view->state.blame;
6296 if (s->blame.thread == 0 && !s->blame_complete) {
6297 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6298 &s->blame.thread_args);
6300 return got_error_set_errno(errcode, "pthread_create");
6302 halfdelay(1); /* fast refresh while annotating */
6305 if (s->blame_complete)
6306 halfdelay(10); /* disable fast refresh */
6308 err = draw_blame(view);
6314 static const struct got_error *
6315 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6316 struct got_repository *repo, struct got_object_id *id)
6318 struct tog_view *log_view;
6319 const struct got_error *err = NULL;
6323 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6324 if (log_view == NULL)
6325 return got_error_from_errno("view_open");
6327 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6329 view_close(log_view);
6331 *new_view = log_view;
6336 static const struct got_error *
6337 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6339 const struct got_error *err = NULL, *thread_err = NULL;
6340 struct tog_view *diff_view;
6341 struct tog_blame_view_state *s = &view->state.blame;
6342 int eos, nscroll, begin_y = 0, begin_x = 0;
6344 eos = nscroll = view->nlines - 2;
6345 if (view_is_hsplit_top(view))
6355 horizontal_scroll_input(view, ch);
6362 s->selected_line = 1;
6363 s->first_displayed_line = 1;
6368 if (s->blame.nlines < eos) {
6369 s->selected_line = s->blame.nlines;
6370 s->first_displayed_line = 1;
6372 s->selected_line = eos;
6373 s->first_displayed_line = s->blame.nlines - (eos - 1);
6380 if (s->selected_line > 1)
6382 else if (s->selected_line == 1 &&
6383 s->first_displayed_line > 1)
6384 s->first_displayed_line--;
6395 if (s->first_displayed_line == 1) {
6396 if (view->count > 1)
6398 s->selected_line = MAX(1, s->selected_line - nscroll);
6402 if (s->first_displayed_line > nscroll)
6403 s->first_displayed_line -= nscroll;
6405 s->first_displayed_line = 1;
6410 if (s->selected_line < eos && s->first_displayed_line +
6411 s->selected_line <= s->blame.nlines)
6413 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6414 s->first_displayed_line++;
6420 struct got_object_id *id = NULL;
6423 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6424 s->first_displayed_line, s->selected_line);
6428 struct got_commit_object *commit, *pcommit;
6429 struct got_object_qid *pid;
6430 struct got_object_id *blob_id = NULL;
6432 err = got_object_open_as_commit(&commit,
6437 got_object_commit_get_parent_ids(commit));
6439 got_object_commit_close(commit);
6442 /* Check if path history ends here. */
6443 err = got_object_open_as_commit(&pcommit,
6447 err = got_object_id_by_path(&blob_id, s->repo,
6449 got_object_commit_close(pcommit);
6451 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6453 got_object_commit_close(commit);
6456 err = got_object_get_type(&obj_type, s->repo,
6459 /* Can't blame non-blob type objects. */
6460 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6461 got_object_commit_close(commit);
6464 err = got_object_qid_alloc(&s->blamed_commit,
6466 got_object_commit_close(commit);
6468 if (got_object_id_cmp(id,
6469 &s->blamed_commit->id) == 0)
6471 err = got_object_qid_alloc(&s->blamed_commit,
6477 thread_err = stop_blame(&s->blame);
6481 STAILQ_INSERT_HEAD(&s->blamed_commits,
6482 s->blamed_commit, entry);
6483 err = run_blame(view);
6489 struct got_object_qid *first;
6492 first = STAILQ_FIRST(&s->blamed_commits);
6493 if (!got_object_id_cmp(&first->id, s->commit_id))
6496 thread_err = stop_blame(&s->blame);
6500 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6501 got_object_qid_free(s->blamed_commit);
6503 STAILQ_FIRST(&s->blamed_commits);
6504 err = run_blame(view);
6511 s->id_to_log = get_selected_commit_id(s->blame.lines,
6512 s->blame.nlines, s->first_displayed_line, s->selected_line);
6514 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6518 struct got_object_id *id = NULL;
6519 struct got_object_qid *pid;
6520 struct got_commit_object *commit = NULL;
6523 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6524 s->first_displayed_line, s->selected_line);
6527 err = got_object_open_as_commit(&commit, s->repo, id);
6530 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6532 /* traversed from diff view, release diff resources */
6533 err = close_diff_view(*new_view);
6536 diff_view = *new_view;
6538 if (view_is_parent_view(view))
6539 view_get_split(view, &begin_y, &begin_x);
6541 diff_view = view_open(0, 0, begin_y, begin_x,
6543 if (diff_view == NULL) {
6544 got_object_commit_close(commit);
6545 err = got_error_from_errno("view_open");
6549 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6550 id, NULL, NULL, 3, 0, 0, view, s->repo);
6551 got_object_commit_close(commit);
6553 view_close(diff_view);
6556 s->last_diffed_line = s->first_displayed_line - 1 +
6559 break; /* still open from active diff view */
6560 if (view_is_parent_view(view) &&
6561 view->mode == TOG_VIEW_SPLIT_HRZN) {
6562 err = view_init_hsplit(view, begin_y);
6568 diff_view->focussed = 1;
6569 diff_view->mode = view->mode;
6570 diff_view->nlines = view->lines - begin_y;
6571 if (view_is_parent_view(view)) {
6572 view_transfer_size(diff_view, view);
6573 err = view_close_child(view);
6576 err = view_set_child(view, diff_view);
6579 view->focus_child = 1;
6581 *new_view = diff_view;
6594 if (s->last_displayed_line >= s->blame.nlines &&
6595 s->selected_line >= MIN(s->blame.nlines,
6596 view->nlines - 2)) {
6600 if (s->last_displayed_line >= s->blame.nlines &&
6601 s->selected_line < view->nlines - 2) {
6603 MIN(nscroll, s->last_displayed_line -
6604 s->first_displayed_line - s->selected_line + 1);
6606 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6607 s->first_displayed_line += nscroll;
6609 s->first_displayed_line =
6610 s->blame.nlines - (view->nlines - 3);
6613 if (s->selected_line > view->nlines - 2) {
6614 s->selected_line = MIN(s->blame.nlines,
6622 return thread_err ? thread_err : err;
6625 static const struct got_error *
6626 reset_blame_view(struct tog_view *view)
6628 const struct got_error *err;
6629 struct tog_blame_view_state *s = &view->state.blame;
6633 err = stop_blame(&s->blame);
6637 return run_blame(view);
6640 static const struct got_error *
6641 cmd_blame(int argc, char *argv[])
6643 const struct got_error *error;
6644 struct got_repository *repo = NULL;
6645 struct got_worktree *worktree = NULL;
6646 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6647 char *link_target = NULL;
6648 struct got_object_id *commit_id = NULL;
6649 struct got_commit_object *commit = NULL;
6650 char *commit_id_str = NULL;
6652 struct tog_view *view;
6653 int *pack_fds = NULL;
6655 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6658 commit_id_str = optarg;
6661 repo_path = realpath(optarg, NULL);
6662 if (repo_path == NULL)
6663 return got_error_from_errno2("realpath",
6678 error = got_repo_pack_fds_open(&pack_fds);
6682 if (repo_path == NULL) {
6683 cwd = getcwd(NULL, 0);
6685 return got_error_from_errno("getcwd");
6686 error = got_worktree_open(&worktree, cwd);
6687 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6691 strdup(got_worktree_get_repo_path(worktree));
6693 repo_path = strdup(cwd);
6694 if (repo_path == NULL) {
6695 error = got_error_from_errno("strdup");
6700 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6704 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6711 error = apply_unveil(got_repo_get_path(repo), NULL);
6715 error = tog_load_refs(repo, 0);
6719 if (commit_id_str == NULL) {
6720 struct got_reference *head_ref;
6721 error = got_ref_open(&head_ref, repo, worktree ?
6722 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6725 error = got_ref_resolve(&commit_id, repo, head_ref);
6726 got_ref_close(head_ref);
6728 error = got_repo_match_object_id(&commit_id, NULL,
6729 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6734 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6736 error = got_error_from_errno("view_open");
6740 error = got_object_open_as_commit(&commit, repo, commit_id);
6744 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6749 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6754 /* Release work tree lock. */
6755 got_worktree_close(worktree);
6758 error = view_loop(view);
6766 got_object_commit_close(commit);
6768 got_worktree_close(worktree);
6770 const struct got_error *close_err = got_repo_close(repo);
6775 const struct got_error *pack_err =
6776 got_repo_pack_fds_close(pack_fds);
6784 static const struct got_error *
6785 draw_tree_entries(struct tog_view *view, const char *parent_path)
6787 struct tog_tree_view_state *s = &view->state.tree;
6788 const struct got_error *err = NULL;
6789 struct got_tree_entry *te;
6792 struct tog_color *tc;
6793 int width, n, nentries, scrollx, i = 1;
6794 int limit = view->nlines;
6797 if (view_is_hsplit_top(view))
6798 --limit; /* border */
6800 werase(view->window);
6805 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6809 if (view_needs_focus_indication(view))
6810 wstandout(view->window);
6811 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6813 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6814 waddwstr(view->window, wline);
6817 while (width++ < view->ncols)
6818 waddch(view->window, ' ');
6820 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6821 if (view_needs_focus_indication(view))
6822 wstandend(view->window);
6827 if (s->first_displayed_entry) {
6828 i += got_tree_entry_get_index(s->first_displayed_entry);
6829 if (s->tree != s->root)
6830 ++i; /* account for ".." entry */
6832 nentries = got_object_tree_get_nentries(s->tree);
6833 if (asprintf(&index, "[%d/%d] %s",
6834 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6835 return got_error_from_errno("asprintf");
6836 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6840 waddwstr(view->window, wline);
6843 if (width < view->ncols - 1)
6844 waddch(view->window, '\n');
6847 waddch(view->window, '\n');
6851 if (s->first_displayed_entry == NULL) {
6852 te = got_object_tree_get_first_entry(s->tree);
6853 if (s->selected == 0) {
6855 wstandout(view->window);
6856 s->selected_entry = NULL;
6858 waddstr(view->window, " ..\n"); /* parent directory */
6859 if (s->selected == 0 && view->focussed)
6860 wstandend(view->window);
6867 te = s->first_displayed_entry;
6871 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6872 char *line = NULL, *id_str = NULL, *link_target = NULL;
6873 const char *modestr = "";
6876 te = got_object_tree_get_entry(s->tree, i);
6877 mode = got_tree_entry_get_mode(te);
6880 err = got_object_id_str(&id_str,
6881 got_tree_entry_get_id(te));
6883 return got_error_from_errno(
6884 "got_object_id_str");
6886 if (got_object_tree_entry_is_submodule(te))
6888 else if (S_ISLNK(mode)) {
6891 err = got_tree_entry_get_symlink_target(&link_target,
6897 for (i = 0; i < strlen(link_target); i++) {
6898 if (!isprint((unsigned char)link_target[i]))
6899 link_target[i] = '?';
6903 else if (S_ISDIR(mode))
6905 else if (mode & S_IXUSR)
6907 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6908 got_tree_entry_get_name(te), modestr,
6909 link_target ? " -> ": "",
6910 link_target ? link_target : "") == -1) {
6913 return got_error_from_errno("asprintf");
6918 /* use full line width to determine view->maxx */
6919 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6924 view->maxx = MAX(view->maxx, width);
6928 err = format_line(&wline, &width, &scrollx, line, view->x,
6934 if (n == s->selected) {
6936 wstandout(view->window);
6937 s->selected_entry = te;
6939 tc = match_color(&s->colors, line);
6941 wattr_on(view->window,
6942 COLOR_PAIR(tc->colorpair), NULL);
6943 waddwstr(view->window, &wline[scrollx]);
6945 wattr_off(view->window,
6946 COLOR_PAIR(tc->colorpair), NULL);
6947 if (width < view->ncols)
6948 waddch(view->window, '\n');
6949 if (n == s->selected && view->focussed)
6950 wstandend(view->window);
6956 s->last_displayed_entry = te;
6965 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6967 struct got_tree_entry *te;
6968 int isroot = s->tree == s->root;
6971 if (s->first_displayed_entry == NULL)
6974 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6975 while (i++ < maxscroll) {
6978 s->first_displayed_entry = NULL;
6981 s->first_displayed_entry = te;
6982 te = got_tree_entry_get_prev(s->tree, te);
6986 static const struct got_error *
6987 tree_scroll_down(struct tog_view *view, int maxscroll)
6989 struct tog_tree_view_state *s = &view->state.tree;
6990 struct got_tree_entry *next, *last;
6993 if (s->first_displayed_entry)
6994 next = got_tree_entry_get_next(s->tree,
6995 s->first_displayed_entry);
6997 next = got_object_tree_get_first_entry(s->tree);
6999 last = s->last_displayed_entry;
7000 while (next && n++ < maxscroll) {
7002 s->last_displayed_entry = last;
7003 last = got_tree_entry_get_next(s->tree, last);
7005 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7006 s->first_displayed_entry = next;
7007 next = got_tree_entry_get_next(s->tree, next);
7014 static const struct got_error *
7015 tree_entry_path(char **path, struct tog_parent_trees *parents,
7016 struct got_tree_entry *te)
7018 const struct got_error *err = NULL;
7019 struct tog_parent_tree *pt;
7020 size_t len = 2; /* for leading slash and NUL */
7022 TAILQ_FOREACH(pt, parents, entry)
7023 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7026 len += strlen(got_tree_entry_get_name(te));
7028 *path = calloc(1, len);
7030 return got_error_from_errno("calloc");
7033 pt = TAILQ_LAST(parents, tog_parent_trees);
7035 const char *name = got_tree_entry_get_name(pt->selected_entry);
7036 if (strlcat(*path, name, len) >= len) {
7037 err = got_error(GOT_ERR_NO_SPACE);
7040 if (strlcat(*path, "/", len) >= len) {
7041 err = got_error(GOT_ERR_NO_SPACE);
7044 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7047 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7048 err = got_error(GOT_ERR_NO_SPACE);
7060 static const struct got_error *
7061 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7062 struct got_tree_entry *te, struct tog_parent_trees *parents,
7063 struct got_object_id *commit_id, struct got_repository *repo)
7065 const struct got_error *err = NULL;
7067 struct tog_view *blame_view;
7071 err = tree_entry_path(&path, parents, te);
7075 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7076 if (blame_view == NULL) {
7077 err = got_error_from_errno("view_open");
7081 err = open_blame_view(blame_view, path, commit_id, repo);
7083 if (err->code == GOT_ERR_CANCELLED)
7085 view_close(blame_view);
7087 *new_view = blame_view;
7093 static const struct got_error *
7094 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7095 struct tog_tree_view_state *s)
7097 struct tog_view *log_view;
7098 const struct got_error *err = NULL;
7103 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7104 if (log_view == NULL)
7105 return got_error_from_errno("view_open");
7107 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7111 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7114 view_close(log_view);
7116 *new_view = log_view;
7121 static const struct got_error *
7122 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7123 const char *head_ref_name, struct got_repository *repo)
7125 const struct got_error *err = NULL;
7126 char *commit_id_str = NULL;
7127 struct tog_tree_view_state *s = &view->state.tree;
7128 struct got_commit_object *commit = NULL;
7130 TAILQ_INIT(&s->parents);
7131 STAILQ_INIT(&s->colors);
7133 s->commit_id = got_object_id_dup(commit_id);
7134 if (s->commit_id == NULL)
7135 return got_error_from_errno("got_object_id_dup");
7137 err = got_object_open_as_commit(&commit, repo, commit_id);
7142 * The root is opened here and will be closed when the view is closed.
7143 * Any visited subtrees and their path-wise parents are opened and
7146 err = got_object_open_as_tree(&s->root, repo,
7147 got_object_commit_get_tree_id(commit));
7152 err = got_object_id_str(&commit_id_str, commit_id);
7156 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7157 err = got_error_from_errno("asprintf");
7161 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7162 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7163 if (head_ref_name) {
7164 s->head_ref_name = strdup(head_ref_name);
7165 if (s->head_ref_name == NULL) {
7166 err = got_error_from_errno("strdup");
7172 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7173 err = add_color(&s->colors, "\\$$",
7174 TOG_COLOR_TREE_SUBMODULE,
7175 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7178 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7179 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7182 err = add_color(&s->colors, "/$",
7183 TOG_COLOR_TREE_DIRECTORY,
7184 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7188 err = add_color(&s->colors, "\\*$",
7189 TOG_COLOR_TREE_EXECUTABLE,
7190 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7194 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7195 get_color_value("TOG_COLOR_COMMIT"));
7200 view->show = show_tree_view;
7201 view->input = input_tree_view;
7202 view->close = close_tree_view;
7203 view->search_start = search_start_tree_view;
7204 view->search_next = search_next_tree_view;
7206 free(commit_id_str);
7208 got_object_commit_close(commit);
7210 close_tree_view(view);
7214 static const struct got_error *
7215 close_tree_view(struct tog_view *view)
7217 struct tog_tree_view_state *s = &view->state.tree;
7219 free_colors(&s->colors);
7220 free(s->tree_label);
7221 s->tree_label = NULL;
7223 s->commit_id = NULL;
7224 free(s->head_ref_name);
7225 s->head_ref_name = NULL;
7226 while (!TAILQ_EMPTY(&s->parents)) {
7227 struct tog_parent_tree *parent;
7228 parent = TAILQ_FIRST(&s->parents);
7229 TAILQ_REMOVE(&s->parents, parent, entry);
7230 if (parent->tree != s->root)
7231 got_object_tree_close(parent->tree);
7235 if (s->tree != NULL && s->tree != s->root)
7236 got_object_tree_close(s->tree);
7238 got_object_tree_close(s->root);
7242 static const struct got_error *
7243 search_start_tree_view(struct tog_view *view)
7245 struct tog_tree_view_state *s = &view->state.tree;
7247 s->matched_entry = NULL;
7252 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7254 regmatch_t regmatch;
7256 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7260 static const struct got_error *
7261 search_next_tree_view(struct tog_view *view)
7263 struct tog_tree_view_state *s = &view->state.tree;
7264 struct got_tree_entry *te = NULL;
7266 if (!view->searching) {
7267 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7271 if (s->matched_entry) {
7272 if (view->searching == TOG_SEARCH_FORWARD) {
7273 if (s->selected_entry)
7274 te = got_tree_entry_get_next(s->tree,
7277 te = got_object_tree_get_first_entry(s->tree);
7279 if (s->selected_entry == NULL)
7280 te = got_object_tree_get_last_entry(s->tree);
7282 te = got_tree_entry_get_prev(s->tree,
7286 if (s->selected_entry)
7287 te = s->selected_entry;
7288 else if (view->searching == TOG_SEARCH_FORWARD)
7289 te = got_object_tree_get_first_entry(s->tree);
7291 te = got_object_tree_get_last_entry(s->tree);
7296 if (s->matched_entry == NULL) {
7297 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7300 if (view->searching == TOG_SEARCH_FORWARD)
7301 te = got_object_tree_get_first_entry(s->tree);
7303 te = got_object_tree_get_last_entry(s->tree);
7306 if (match_tree_entry(te, &view->regex)) {
7307 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7308 s->matched_entry = te;
7312 if (view->searching == TOG_SEARCH_FORWARD)
7313 te = got_tree_entry_get_next(s->tree, te);
7315 te = got_tree_entry_get_prev(s->tree, te);
7318 if (s->matched_entry) {
7319 s->first_displayed_entry = s->matched_entry;
7326 static const struct got_error *
7327 show_tree_view(struct tog_view *view)
7329 const struct got_error *err = NULL;
7330 struct tog_tree_view_state *s = &view->state.tree;
7333 err = tree_entry_path(&parent_path, &s->parents, NULL);
7337 err = draw_tree_entries(view, parent_path);
7344 static const struct got_error *
7345 tree_goto_line(struct tog_view *view, int nlines)
7347 const struct got_error *err = NULL;
7348 struct tog_tree_view_state *s = &view->state.tree;
7349 struct got_tree_entry **fte, **lte, **ste;
7350 int g, last, first = 1, i = 1;
7351 int root = s->tree == s->root;
7352 int off = root ? 1 : 2;
7359 else if (g > got_object_tree_get_nentries(s->tree))
7360 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7362 fte = &s->first_displayed_entry;
7363 lte = &s->last_displayed_entry;
7364 ste = &s->selected_entry;
7367 first = got_tree_entry_get_index(*fte);
7368 first += off; /* account for ".." */
7370 last = got_tree_entry_get_index(*lte);
7373 if (g >= first && g <= last && g - first < nlines) {
7374 s->selected = g - first;
7375 return NULL; /* gline is on the current page */
7379 i = got_tree_entry_get_index(*ste);
7384 err = tree_scroll_down(view, g - i);
7387 if (got_tree_entry_get_index(*lte) >=
7388 got_object_tree_get_nentries(s->tree) - 1 &&
7389 first + s->selected < g &&
7390 s->selected < s->ndisplayed - 1) {
7391 first = got_tree_entry_get_index(*fte);
7393 s->selected = g - first;
7396 tree_scroll_up(s, i - g);
7399 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7400 s->selected = g - 1;
7405 static const struct got_error *
7406 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7408 const struct got_error *err = NULL;
7409 struct tog_tree_view_state *s = &view->state.tree;
7410 struct got_tree_entry *te;
7411 int n, nscroll = view->nlines - 3;
7414 return tree_goto_line(view, nscroll);
7423 horizontal_scroll_input(view, ch);
7426 s->show_ids = !s->show_ids;
7431 if (!s->selected_entry)
7433 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7437 err = view_request_new(new_view, view, TOG_VIEW_REF);
7444 if (s->tree == s->root)
7445 s->first_displayed_entry =
7446 got_object_tree_get_first_entry(s->tree);
7448 s->first_displayed_entry = NULL;
7453 int eos = view->nlines - 3;
7455 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7459 te = got_object_tree_get_last_entry(s->tree);
7460 for (n = 0; n < eos; n++) {
7462 if (s->tree != s->root) {
7463 s->first_displayed_entry = NULL;
7468 s->first_displayed_entry = te;
7469 te = got_tree_entry_get_prev(s->tree, te);
7472 s->selected = n - 1;
7478 if (s->selected > 0) {
7482 tree_scroll_up(s, 1);
7483 if (s->selected_entry == NULL ||
7484 (s->tree == s->root && s->selected_entry ==
7485 got_object_tree_get_first_entry(s->tree)))
7495 if (s->tree == s->root) {
7496 if (got_object_tree_get_first_entry(s->tree) ==
7497 s->first_displayed_entry)
7498 s->selected -= MIN(s->selected, nscroll);
7500 if (s->first_displayed_entry == NULL)
7501 s->selected -= MIN(s->selected, nscroll);
7503 tree_scroll_up(s, MAX(0, nscroll));
7504 if (s->selected_entry == NULL ||
7505 (s->tree == s->root && s->selected_entry ==
7506 got_object_tree_get_first_entry(s->tree)))
7512 if (s->selected < s->ndisplayed - 1) {
7516 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7518 /* can't scroll any further */
7522 tree_scroll_down(view, 1);
7532 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7534 /* can't scroll any further; move cursor down */
7535 if (s->selected < s->ndisplayed - 1)
7536 s->selected += MIN(nscroll,
7537 s->ndisplayed - s->selected - 1);
7542 tree_scroll_down(view, nscroll);
7547 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7548 struct tog_parent_tree *parent;
7549 /* user selected '..' */
7550 if (s->tree == s->root) {
7554 parent = TAILQ_FIRST(&s->parents);
7555 TAILQ_REMOVE(&s->parents, parent,
7557 got_object_tree_close(s->tree);
7558 s->tree = parent->tree;
7559 s->first_displayed_entry =
7560 parent->first_displayed_entry;
7562 parent->selected_entry;
7563 s->selected = parent->selected;
7564 if (s->selected > view->nlines - 3) {
7565 err = offset_selection_down(view);
7570 } else if (S_ISDIR(got_tree_entry_get_mode(
7571 s->selected_entry))) {
7572 struct got_tree_object *subtree;
7574 err = got_object_open_as_tree(&subtree, s->repo,
7575 got_tree_entry_get_id(s->selected_entry));
7578 err = tree_view_visit_subtree(s, subtree);
7580 got_object_tree_close(subtree);
7583 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7584 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7587 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7588 s->selected = view->nlines - 4;
7604 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7609 static const struct got_error *
7610 cmd_tree(int argc, char *argv[])
7612 const struct got_error *error;
7613 struct got_repository *repo = NULL;
7614 struct got_worktree *worktree = NULL;
7615 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7616 struct got_object_id *commit_id = NULL;
7617 struct got_commit_object *commit = NULL;
7618 const char *commit_id_arg = NULL;
7620 struct got_reference *ref = NULL;
7621 const char *head_ref_name = NULL;
7623 struct tog_view *view;
7624 int *pack_fds = NULL;
7626 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7629 commit_id_arg = optarg;
7632 repo_path = realpath(optarg, NULL);
7633 if (repo_path == NULL)
7634 return got_error_from_errno2("realpath",
7649 error = got_repo_pack_fds_open(&pack_fds);
7653 if (repo_path == NULL) {
7654 cwd = getcwd(NULL, 0);
7656 return got_error_from_errno("getcwd");
7657 error = got_worktree_open(&worktree, cwd);
7658 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7662 strdup(got_worktree_get_repo_path(worktree));
7664 repo_path = strdup(cwd);
7665 if (repo_path == NULL) {
7666 error = got_error_from_errno("strdup");
7671 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7675 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7682 error = apply_unveil(got_repo_get_path(repo), NULL);
7686 error = tog_load_refs(repo, 0);
7690 if (commit_id_arg == NULL) {
7691 error = got_repo_match_object_id(&commit_id, &label,
7692 worktree ? got_worktree_get_head_ref_name(worktree) :
7693 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7696 head_ref_name = label;
7698 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7700 head_ref_name = got_ref_get_name(ref);
7701 else if (error->code != GOT_ERR_NOT_REF)
7703 error = got_repo_match_object_id(&commit_id, NULL,
7704 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7709 error = got_object_open_as_commit(&commit, repo, commit_id);
7713 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7715 error = got_error_from_errno("view_open");
7718 error = open_tree_view(view, commit_id, head_ref_name, repo);
7721 if (!got_path_is_root_dir(in_repo_path)) {
7722 error = tree_view_walk_path(&view->state.tree, commit,
7729 /* Release work tree lock. */
7730 got_worktree_close(worktree);
7733 error = view_loop(view);
7742 const struct got_error *close_err = got_repo_close(repo);
7747 const struct got_error *pack_err =
7748 got_repo_pack_fds_close(pack_fds);
7756 static const struct got_error *
7757 ref_view_load_refs(struct tog_ref_view_state *s)
7759 struct got_reflist_entry *sre;
7760 struct tog_reflist_entry *re;
7763 TAILQ_FOREACH(sre, &tog_refs, entry) {
7764 if (strncmp(got_ref_get_name(sre->ref),
7765 "refs/got/", 9) == 0 &&
7766 strncmp(got_ref_get_name(sre->ref),
7767 "refs/got/backup/", 16) != 0)
7770 re = malloc(sizeof(*re));
7772 return got_error_from_errno("malloc");
7774 re->ref = got_ref_dup(sre->ref);
7775 if (re->ref == NULL)
7776 return got_error_from_errno("got_ref_dup");
7777 re->idx = s->nrefs++;
7778 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7781 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7786 ref_view_free_refs(struct tog_ref_view_state *s)
7788 struct tog_reflist_entry *re;
7790 while (!TAILQ_EMPTY(&s->refs)) {
7791 re = TAILQ_FIRST(&s->refs);
7792 TAILQ_REMOVE(&s->refs, re, entry);
7793 got_ref_close(re->ref);
7798 static const struct got_error *
7799 open_ref_view(struct tog_view *view, struct got_repository *repo)
7801 const struct got_error *err = NULL;
7802 struct tog_ref_view_state *s = &view->state.ref;
7804 s->selected_entry = 0;
7807 TAILQ_INIT(&s->refs);
7808 STAILQ_INIT(&s->colors);
7810 err = ref_view_load_refs(s);
7814 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7815 err = add_color(&s->colors, "^refs/heads/",
7816 TOG_COLOR_REFS_HEADS,
7817 get_color_value("TOG_COLOR_REFS_HEADS"));
7821 err = add_color(&s->colors, "^refs/tags/",
7822 TOG_COLOR_REFS_TAGS,
7823 get_color_value("TOG_COLOR_REFS_TAGS"));
7827 err = add_color(&s->colors, "^refs/remotes/",
7828 TOG_COLOR_REFS_REMOTES,
7829 get_color_value("TOG_COLOR_REFS_REMOTES"));
7833 err = add_color(&s->colors, "^refs/got/backup/",
7834 TOG_COLOR_REFS_BACKUP,
7835 get_color_value("TOG_COLOR_REFS_BACKUP"));
7840 view->show = show_ref_view;
7841 view->input = input_ref_view;
7842 view->close = close_ref_view;
7843 view->search_start = search_start_ref_view;
7844 view->search_next = search_next_ref_view;
7847 free_colors(&s->colors);
7851 static const struct got_error *
7852 close_ref_view(struct tog_view *view)
7854 struct tog_ref_view_state *s = &view->state.ref;
7856 ref_view_free_refs(s);
7857 free_colors(&s->colors);
7862 static const struct got_error *
7863 resolve_reflist_entry(struct got_object_id **commit_id,
7864 struct tog_reflist_entry *re, struct got_repository *repo)
7866 const struct got_error *err = NULL;
7867 struct got_object_id *obj_id;
7868 struct got_tag_object *tag = NULL;
7873 err = got_ref_resolve(&obj_id, repo, re->ref);
7877 err = got_object_get_type(&obj_type, repo, obj_id);
7882 case GOT_OBJ_TYPE_COMMIT:
7883 *commit_id = obj_id;
7885 case GOT_OBJ_TYPE_TAG:
7886 err = got_object_open_as_tag(&tag, repo, obj_id);
7890 err = got_object_get_type(&obj_type, repo,
7891 got_object_tag_get_object_id(tag));
7894 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7895 err = got_error(GOT_ERR_OBJ_TYPE);
7898 *commit_id = got_object_id_dup(
7899 got_object_tag_get_object_id(tag));
7900 if (*commit_id == NULL) {
7901 err = got_error_from_errno("got_object_id_dup");
7906 err = got_error(GOT_ERR_OBJ_TYPE);
7912 got_object_tag_close(tag);
7920 static const struct got_error *
7921 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7922 struct tog_reflist_entry *re, struct got_repository *repo)
7924 struct tog_view *log_view;
7925 const struct got_error *err = NULL;
7926 struct got_object_id *commit_id = NULL;
7930 err = resolve_reflist_entry(&commit_id, re, repo);
7932 if (err->code != GOT_ERR_OBJ_TYPE)
7938 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7939 if (log_view == NULL) {
7940 err = got_error_from_errno("view_open");
7944 err = open_log_view(log_view, commit_id, repo,
7945 got_ref_get_name(re->ref), "", 0);
7948 view_close(log_view);
7950 *new_view = log_view;
7956 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7958 struct tog_reflist_entry *re;
7961 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7964 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7965 while (i++ < maxscroll) {
7968 s->first_displayed_entry = re;
7969 re = TAILQ_PREV(re, tog_reflist_head, entry);
7973 static const struct got_error *
7974 ref_scroll_down(struct tog_view *view, int maxscroll)
7976 struct tog_ref_view_state *s = &view->state.ref;
7977 struct tog_reflist_entry *next, *last;
7980 if (s->first_displayed_entry)
7981 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7983 next = TAILQ_FIRST(&s->refs);
7985 last = s->last_displayed_entry;
7986 while (next && n++ < maxscroll) {
7988 s->last_displayed_entry = last;
7989 last = TAILQ_NEXT(last, entry);
7991 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7992 s->first_displayed_entry = next;
7993 next = TAILQ_NEXT(next, entry);
8000 static const struct got_error *
8001 search_start_ref_view(struct tog_view *view)
8003 struct tog_ref_view_state *s = &view->state.ref;
8005 s->matched_entry = NULL;
8010 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8012 regmatch_t regmatch;
8014 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8018 static const struct got_error *
8019 search_next_ref_view(struct tog_view *view)
8021 struct tog_ref_view_state *s = &view->state.ref;
8022 struct tog_reflist_entry *re = NULL;
8024 if (!view->searching) {
8025 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8029 if (s->matched_entry) {
8030 if (view->searching == TOG_SEARCH_FORWARD) {
8031 if (s->selected_entry)
8032 re = TAILQ_NEXT(s->selected_entry, entry);
8034 re = TAILQ_PREV(s->selected_entry,
8035 tog_reflist_head, entry);
8037 if (s->selected_entry == NULL)
8038 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8040 re = TAILQ_PREV(s->selected_entry,
8041 tog_reflist_head, entry);
8044 if (s->selected_entry)
8045 re = s->selected_entry;
8046 else if (view->searching == TOG_SEARCH_FORWARD)
8047 re = TAILQ_FIRST(&s->refs);
8049 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8054 if (s->matched_entry == NULL) {
8055 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8058 if (view->searching == TOG_SEARCH_FORWARD)
8059 re = TAILQ_FIRST(&s->refs);
8061 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8064 if (match_reflist_entry(re, &view->regex)) {
8065 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8066 s->matched_entry = re;
8070 if (view->searching == TOG_SEARCH_FORWARD)
8071 re = TAILQ_NEXT(re, entry);
8073 re = TAILQ_PREV(re, tog_reflist_head, entry);
8076 if (s->matched_entry) {
8077 s->first_displayed_entry = s->matched_entry;
8084 static const struct got_error *
8085 show_ref_view(struct tog_view *view)
8087 const struct got_error *err = NULL;
8088 struct tog_ref_view_state *s = &view->state.ref;
8089 struct tog_reflist_entry *re;
8092 struct tog_color *tc;
8093 int width, n, scrollx;
8094 int limit = view->nlines;
8096 werase(view->window);
8099 if (view_is_hsplit_top(view))
8100 --limit; /* border */
8105 re = s->first_displayed_entry;
8107 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8109 return got_error_from_errno("asprintf");
8111 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8116 if (view_needs_focus_indication(view))
8117 wstandout(view->window);
8118 waddwstr(view->window, wline);
8119 while (width++ < view->ncols)
8120 waddch(view->window, ' ');
8121 if (view_needs_focus_indication(view))
8122 wstandend(view->window);
8132 while (re && limit > 0) {
8134 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8137 struct got_commit_object *ci;
8138 struct got_tag_object *tag;
8139 struct got_object_id *id;
8143 err = got_ref_resolve(&id, s->repo, re->ref);
8146 err = got_object_open_as_tag(&tag, s->repo, id);
8148 if (err->code != GOT_ERR_OBJ_TYPE) {
8152 err = got_object_open_as_commit(&ci, s->repo,
8158 t = got_object_commit_get_committer_time(ci);
8159 got_object_commit_close(ci);
8161 t = got_object_tag_get_tagger_time(tag);
8162 got_object_tag_close(tag);
8165 if (gmtime_r(&t, &tm) == NULL)
8166 return got_error_from_errno("gmtime_r");
8167 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8168 return got_error(GOT_ERR_NO_SPACE);
8170 if (got_ref_is_symbolic(re->ref)) {
8171 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8172 ymd : "", got_ref_get_name(re->ref),
8173 got_ref_get_symref_target(re->ref)) == -1)
8174 return got_error_from_errno("asprintf");
8175 } else if (s->show_ids) {
8176 struct got_object_id *id;
8178 err = got_ref_resolve(&id, s->repo, re->ref);
8181 err = got_object_id_str(&id_str, id);
8186 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8187 got_ref_get_name(re->ref), id_str) == -1) {
8188 err = got_error_from_errno("asprintf");
8195 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8196 got_ref_get_name(re->ref)) == -1)
8197 return got_error_from_errno("asprintf");
8199 /* use full line width to determine view->maxx */
8200 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8205 view->maxx = MAX(view->maxx, width);
8209 err = format_line(&wline, &width, &scrollx, line, view->x,
8215 if (n == s->selected) {
8217 wstandout(view->window);
8218 s->selected_entry = re;
8220 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8222 wattr_on(view->window,
8223 COLOR_PAIR(tc->colorpair), NULL);
8224 waddwstr(view->window, &wline[scrollx]);
8226 wattr_off(view->window,
8227 COLOR_PAIR(tc->colorpair), NULL);
8228 if (width < view->ncols)
8229 waddch(view->window, '\n');
8230 if (n == s->selected && view->focussed)
8231 wstandend(view->window);
8237 s->last_displayed_entry = re;
8240 re = TAILQ_NEXT(re, entry);
8247 static const struct got_error *
8248 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8249 struct tog_reflist_entry *re, struct got_repository *repo)
8251 const struct got_error *err = NULL;
8252 struct got_object_id *commit_id = NULL;
8253 struct tog_view *tree_view;
8257 err = resolve_reflist_entry(&commit_id, re, repo);
8259 if (err->code != GOT_ERR_OBJ_TYPE)
8266 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8267 if (tree_view == NULL) {
8268 err = got_error_from_errno("view_open");
8272 err = open_tree_view(tree_view, commit_id,
8273 got_ref_get_name(re->ref), repo);
8277 *new_view = tree_view;
8283 static const struct got_error *
8284 ref_goto_line(struct tog_view *view, int nlines)
8286 const struct got_error *err = NULL;
8287 struct tog_ref_view_state *s = &view->state.ref;
8288 int g, idx = s->selected_entry->idx;
8295 else if (g > s->nrefs)
8298 if (g >= s->first_displayed_entry->idx + 1 &&
8299 g <= s->last_displayed_entry->idx + 1 &&
8300 g - s->first_displayed_entry->idx - 1 < nlines) {
8301 s->selected = g - s->first_displayed_entry->idx - 1;
8306 err = ref_scroll_down(view, g - idx - 1);
8309 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8310 s->first_displayed_entry->idx + s->selected < g &&
8311 s->selected < s->ndisplayed - 1)
8312 s->selected = g - s->first_displayed_entry->idx - 1;
8313 } else if (idx + 1 > g)
8314 ref_scroll_up(s, idx - g + 1);
8316 if (g < nlines && s->first_displayed_entry->idx == 0)
8317 s->selected = g - 1;
8323 static const struct got_error *
8324 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8326 const struct got_error *err = NULL;
8327 struct tog_ref_view_state *s = &view->state.ref;
8328 struct tog_reflist_entry *re;
8329 int n, nscroll = view->nlines - 1;
8332 return ref_goto_line(view, nscroll);
8341 horizontal_scroll_input(view, ch);
8344 s->show_ids = !s->show_ids;
8348 s->show_date = !s->show_date;
8352 s->sort_by_date = !s->sort_by_date;
8353 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8355 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8356 got_ref_cmp_by_commit_timestamp_descending :
8357 tog_ref_cmp_by_name, s->repo);
8360 got_reflist_object_id_map_free(tog_refs_idmap);
8361 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8362 &tog_refs, s->repo);
8365 ref_view_free_refs(s);
8366 err = ref_view_load_refs(s);
8371 if (!s->selected_entry)
8373 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8377 if (!s->selected_entry)
8379 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8386 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8391 int eos = view->nlines - 1;
8393 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8397 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8398 for (n = 0; n < eos; n++) {
8401 s->first_displayed_entry = re;
8402 re = TAILQ_PREV(re, tog_reflist_head, entry);
8405 s->selected = n - 1;
8411 if (s->selected > 0) {
8415 ref_scroll_up(s, 1);
8416 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8426 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8427 s->selected -= MIN(nscroll, s->selected);
8428 ref_scroll_up(s, MAX(0, nscroll));
8429 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8435 if (s->selected < s->ndisplayed - 1) {
8439 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8440 /* can't scroll any further */
8444 ref_scroll_down(view, 1);
8454 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8455 /* can't scroll any further; move cursor down */
8456 if (s->selected < s->ndisplayed - 1)
8457 s->selected += MIN(nscroll,
8458 s->ndisplayed - s->selected - 1);
8459 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8460 s->selected += s->ndisplayed - s->selected - 1;
8464 ref_scroll_down(view, nscroll);
8469 err = tog_load_refs(s->repo, s->sort_by_date);
8472 ref_view_free_refs(s);
8473 err = ref_view_load_refs(s);
8476 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8477 s->selected = view->nlines - 2;
8491 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8496 static const struct got_error *
8497 cmd_ref(int argc, char *argv[])
8499 const struct got_error *error;
8500 struct got_repository *repo = NULL;
8501 struct got_worktree *worktree = NULL;
8502 char *cwd = NULL, *repo_path = NULL;
8504 struct tog_view *view;
8505 int *pack_fds = NULL;
8507 while ((ch = getopt(argc, argv, "r:")) != -1) {
8510 repo_path = realpath(optarg, NULL);
8511 if (repo_path == NULL)
8512 return got_error_from_errno2("realpath",
8527 error = got_repo_pack_fds_open(&pack_fds);
8531 if (repo_path == NULL) {
8532 cwd = getcwd(NULL, 0);
8534 return got_error_from_errno("getcwd");
8535 error = got_worktree_open(&worktree, cwd);
8536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8540 strdup(got_worktree_get_repo_path(worktree));
8542 repo_path = strdup(cwd);
8543 if (repo_path == NULL) {
8544 error = got_error_from_errno("strdup");
8549 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8555 error = apply_unveil(got_repo_get_path(repo), NULL);
8559 error = tog_load_refs(repo, 0);
8563 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8565 error = got_error_from_errno("view_open");
8569 error = open_ref_view(view, repo);
8574 /* Release work tree lock. */
8575 got_worktree_close(worktree);
8578 error = view_loop(view);
8583 const struct got_error *close_err = got_repo_close(repo);
8588 const struct got_error *pack_err =
8589 got_repo_pack_fds_close(pack_fds);
8597 static const struct got_error*
8598 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8607 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8611 if (mvwprintw(win, y, x, "%s", str) == ERR)
8612 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8619 static const struct got_error *
8620 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8624 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8626 free(*line_offsets);
8627 *line_offsets = NULL;
8628 return got_error_from_errno("reallocarray");
8632 (*line_offsets)[*nlines] = off;
8637 static const struct got_error *
8638 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8642 for (;n > 0; --n, ++km) {
8646 if (km->keys == NULL)
8649 t = t0 = strdup(km->keys);
8651 return got_error_from_errno("strdup");
8654 while ((k = strsep(&t, " ")) != NULL)
8655 len += strlen(k) > 1 ? 2 : 0;
8657 *ret = MAX(*ret, len);
8664 * Write keymap section headers, keys, and key info in km to f.
8665 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8666 * wrap control and symbolic keys in guillemets, else use <>.
8668 static const struct got_error *
8669 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8674 static const char *u8_glyph[] = {
8675 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8676 "\xe2\x80\xba" /* U+203A (utf8 >) */
8679 int cs, s, first = 1;
8681 cs = got_locale_is_utf8();
8683 t = t0 = strdup(km->keys);
8685 return got_error_from_errno("strdup");
8687 len = strlen(km->keys);
8688 while ((k = strsep(&t, " ")) != NULL) {
8689 s = strlen(k) > 1; /* control or symbolic key */
8690 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8691 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8692 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8695 return got_error_from_errno("fprintf");
8703 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8705 return got_error_from_errno("fprintf");
8711 static const struct got_error *
8712 format_help(struct tog_help_view_state *s)
8714 const struct got_error *err = NULL;
8716 int i, max, n, show = s->all;
8717 static const struct tog_key_map km[] = {
8718 #define KEYMAP_(info, type) { NULL, (info), type }
8719 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8725 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8730 err = max_key_str(&max, km, n);
8734 for (i = 0; i < n; ++i) {
8735 if (km[i].keys == NULL) {
8737 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8738 km[i].type == s->type || s->all)
8742 err = format_help_line(&off, s->f, &km[i], max);
8745 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8752 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8756 static const struct got_error *
8757 create_help(struct tog_help_view_state *s)
8760 const struct got_error *err;
8762 free(s->line_offsets);
8763 s->line_offsets = NULL;
8768 return got_error_from_errno("got_opentemp");
8771 err = format_help(s);
8775 if (s->f && fflush(s->f) != 0)
8776 return got_error_from_errno("fflush");
8781 static const struct got_error *
8782 search_start_help_view(struct tog_view *view)
8784 view->state.help.matched_line = 0;
8789 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8790 size_t *nlines, int **first, int **last, int **match, int **selected)
8792 struct tog_help_view_state *s = &view->state.help;
8795 *nlines = s->nlines;
8796 *line_offsets = s->line_offsets;
8797 *match = &s->matched_line;
8798 *first = &s->first_displayed_line;
8799 *last = &s->last_displayed_line;
8800 *selected = &s->selected_line;
8803 static const struct got_error *
8804 show_help_view(struct tog_view *view)
8806 struct tog_help_view_state *s = &view->state.help;
8807 const struct got_error *err;
8808 regmatch_t *regmatch = &view->regmatch;
8813 int width, nprinted = 0, rc = 0;
8814 int eos = view->nlines;
8816 if (view_is_hsplit_top(view))
8817 --eos; /* account for border */
8821 werase(view->window);
8823 if (view->gline > s->nlines - 1)
8824 view->gline = s->nlines - 1;
8826 err = win_draw_center(view->window, 0, 0, view->ncols,
8827 view_needs_focus_indication(view),
8828 "tog help (press q to return to tog)");
8833 waddstr(view->window, "\n\n");
8839 while (eos > 0 && nprinted < eos) {
8842 linelen = getline(&line, &linesz, s->f);
8843 if (linelen == -1) {
8846 return got_ferror(s->f, GOT_ERR_IO);
8851 if (++s->lineno < s->first_displayed_line)
8853 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8855 if (s->lineno == view->hiline)
8858 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8864 view->maxx = MAX(view->maxx, width);
8869 wattron(view->window, attr);
8870 if (s->first_displayed_line + nprinted == s->matched_line &&
8871 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8872 err = add_matched_line(&width, line, view->ncols - 1, 0,
8873 view->window, view->x, regmatch);
8881 err = format_line(&wline, &width, &skip, line,
8882 view->x, view->ncols, 0, view->x ? 1 : 0);
8887 waddwstr(view->window, &wline[skip]);
8891 if (s->lineno == view->hiline) {
8892 while (width++ < view->ncols)
8893 waddch(view->window, ' ');
8895 if (width < view->ncols)
8896 waddch(view->window, '\n');
8899 wattroff(view->window, attr);
8900 if (++nprinted == 1)
8901 s->first_displayed_line = s->lineno;
8905 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8907 s->last_displayed_line = s->first_displayed_line;
8912 rc = waddnstr(view->window,
8913 "See the tog(1) manual page for full documentation",
8916 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8918 wmove(view->window, view->nlines - 1, 0);
8919 wclrtoeol(view->window);
8920 wstandout(view->window);
8921 rc = waddnstr(view->window, "scroll down for more...",
8924 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8925 if (getcurx(view->window) < view->ncols - 6) {
8926 rc = wprintw(view->window, "[%.0f%%]",
8927 100.00 * s->last_displayed_line / s->nlines);
8929 return got_error_msg(GOT_ERR_IO, "wprintw");
8931 wstandend(view->window);
8937 static const struct got_error *
8938 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8940 struct tog_help_view_state *s = &view->state.help;
8941 const struct got_error *err = NULL;
8947 eos = nscroll = view->nlines;
8948 if (view_is_hsplit_top(view))
8951 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8960 horizontal_scroll_input(view, ch);
8964 s->first_displayed_line = 1;
8972 s->first_displayed_line = (s->nlines - eos) + 3;
8977 if (s->first_displayed_line > 1)
8978 --s->first_displayed_line;
8989 if (s->first_displayed_line == 1) {
8993 while (--nscroll > 0 && s->first_displayed_line > 1)
8994 s->first_displayed_line--;
9000 ++s->first_displayed_line;
9016 while (!s->eof && --nscroll > 0) {
9017 linelen = getline(&line, &linesz, s->f);
9018 s->first_displayed_line++;
9019 if (linelen == -1) {
9023 err = got_ferror(s->f, GOT_ERR_IO);
9037 static const struct got_error *
9038 close_help_view(struct tog_view *view)
9040 struct tog_help_view_state *s = &view->state.help;
9042 free(s->line_offsets);
9043 s->line_offsets = NULL;
9044 if (fclose(s->f) == EOF)
9045 return got_error_from_errno("fclose");
9050 static const struct got_error *
9051 reset_help_view(struct tog_view *view)
9053 struct tog_help_view_state *s = &view->state.help;
9056 if (s->f && fclose(s->f) == EOF)
9057 return got_error_from_errno("fclose");
9059 wclear(view->window);
9063 s->first_displayed_line = 1;
9064 s->last_displayed_line = view->nlines;
9065 s->matched_line = 0;
9067 return create_help(s);
9070 static const struct got_error *
9071 open_help_view(struct tog_view *view, struct tog_view *parent)
9073 const struct got_error *err = NULL;
9074 struct tog_help_view_state *s = &view->state.help;
9076 s->type = (enum tog_keymap_type)parent->type;
9077 s->first_displayed_line = 1;
9078 s->last_displayed_line = view->nlines;
9079 s->selected_line = 1;
9081 view->show = show_help_view;
9082 view->input = input_help_view;
9083 view->reset = reset_help_view;
9084 view->close = close_help_view;
9085 view->search_start = search_start_help_view;
9086 view->search_setup = search_setup_help_view;
9087 view->search_next = search_next_view_match;
9089 err = create_help(s);
9093 static const struct got_error *
9094 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9095 enum tog_view_type request, int y, int x)
9097 const struct got_error *err = NULL;
9103 if (view->type == TOG_VIEW_LOG) {
9104 struct tog_log_view_state *s = &view->state.log;
9106 err = open_diff_view_for_commit(new_view, y, x,
9107 s->selected_entry->commit, s->selected_entry->id,
9110 return got_error_msg(GOT_ERR_NOT_IMPL,
9111 "parent/child view pair not supported");
9113 case TOG_VIEW_BLAME:
9114 if (view->type == TOG_VIEW_TREE) {
9115 struct tog_tree_view_state *s = &view->state.tree;
9117 err = blame_tree_entry(new_view, y, x,
9118 s->selected_entry, &s->parents, s->commit_id,
9121 return got_error_msg(GOT_ERR_NOT_IMPL,
9122 "parent/child view pair not supported");
9125 if (view->type == TOG_VIEW_BLAME)
9126 err = log_annotated_line(new_view, y, x,
9127 view->state.blame.repo, view->state.blame.id_to_log);
9128 else if (view->type == TOG_VIEW_TREE)
9129 err = log_selected_tree_entry(new_view, y, x,
9131 else if (view->type == TOG_VIEW_REF)
9132 err = log_ref_entry(new_view, y, x,
9133 view->state.ref.selected_entry,
9134 view->state.ref.repo);
9136 return got_error_msg(GOT_ERR_NOT_IMPL,
9137 "parent/child view pair not supported");
9140 if (view->type == TOG_VIEW_LOG)
9141 err = browse_commit_tree(new_view, y, x,
9142 view->state.log.selected_entry,
9143 view->state.log.in_repo_path,
9144 view->state.log.head_ref_name,
9145 view->state.log.repo);
9146 else if (view->type == TOG_VIEW_REF)
9147 err = browse_ref_tree(new_view, y, x,
9148 view->state.ref.selected_entry,
9149 view->state.ref.repo);
9151 return got_error_msg(GOT_ERR_NOT_IMPL,
9152 "parent/child view pair not supported");
9155 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9156 if (*new_view == NULL)
9157 return got_error_from_errno("view_open");
9158 if (view->type == TOG_VIEW_LOG)
9159 err = open_ref_view(*new_view, view->state.log.repo);
9160 else if (view->type == TOG_VIEW_TREE)
9161 err = open_ref_view(*new_view, view->state.tree.repo);
9163 err = got_error_msg(GOT_ERR_NOT_IMPL,
9164 "parent/child view pair not supported");
9166 view_close(*new_view);
9169 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9170 if (*new_view == NULL)
9171 return got_error_from_errno("view_open");
9172 err = open_help_view(*new_view, view);
9174 view_close(*new_view);
9177 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9184 * If view was scrolled down to move the selected line into view when opening a
9185 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9188 offset_selection_up(struct tog_view *view)
9190 switch (view->type) {
9191 case TOG_VIEW_BLAME: {
9192 struct tog_blame_view_state *s = &view->state.blame;
9193 if (s->first_displayed_line == 1) {
9194 s->selected_line = MAX(s->selected_line - view->offset,
9198 if (s->first_displayed_line > view->offset)
9199 s->first_displayed_line -= view->offset;
9201 s->first_displayed_line = 1;
9202 s->selected_line += view->offset;
9206 log_scroll_up(&view->state.log, view->offset);
9207 view->state.log.selected += view->offset;
9210 ref_scroll_up(&view->state.ref, view->offset);
9211 view->state.ref.selected += view->offset;
9214 tree_scroll_up(&view->state.tree, view->offset);
9215 view->state.tree.selected += view->offset;
9225 * If the selected line is in the section of screen covered by the bottom split,
9226 * scroll down offset lines to move it into view and index its new position.
9228 static const struct got_error *
9229 offset_selection_down(struct tog_view *view)
9231 const struct got_error *err = NULL;
9232 const struct got_error *(*scrolld)(struct tog_view *, int);
9233 int *selected = NULL;
9236 switch (view->type) {
9237 case TOG_VIEW_BLAME: {
9238 struct tog_blame_view_state *s = &view->state.blame;
9241 if (s->selected_line > view->nlines - header) {
9242 offset = abs(view->nlines - s->selected_line - header);
9243 s->first_displayed_line += offset;
9244 s->selected_line -= offset;
9245 view->offset = offset;
9249 case TOG_VIEW_LOG: {
9250 struct tog_log_view_state *s = &view->state.log;
9251 scrolld = &log_scroll_down;
9252 header = view_is_parent_view(view) ? 3 : 2;
9253 selected = &s->selected;
9256 case TOG_VIEW_REF: {
9257 struct tog_ref_view_state *s = &view->state.ref;
9258 scrolld = &ref_scroll_down;
9260 selected = &s->selected;
9263 case TOG_VIEW_TREE: {
9264 struct tog_tree_view_state *s = &view->state.tree;
9265 scrolld = &tree_scroll_down;
9267 selected = &s->selected;
9277 if (selected && *selected > view->nlines - header) {
9278 offset = abs(view->nlines - *selected - header);
9279 view->offset = offset;
9280 if (scrolld && offset) {
9281 err = scrolld(view, offset);
9282 *selected -= offset;
9290 list_commands(FILE *fp)
9294 fprintf(fp, "commands:");
9295 for (i = 0; i < nitems(tog_commands); i++) {
9296 const struct tog_cmd *cmd = &tog_commands[i];
9297 fprintf(fp, " %s", cmd->name);
9303 usage(int hflag, int status)
9305 FILE *fp = (status == 0) ? stdout : stderr;
9307 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9310 fprintf(fp, "lazy usage: %s path\n", getprogname());
9317 make_argv(int argc, ...)
9325 argv = calloc(argc, sizeof(char *));
9328 for (i = 0; i < argc; i++) {
9329 argv[i] = strdup(va_arg(ap, char *));
9330 if (argv[i] == NULL)
9339 * Try to convert 'tog path' into a 'tog log path' command.
9340 * The user could simply have mistyped the command rather than knowingly
9341 * provided a path. So check whether argv[0] can in fact be resolved
9342 * to a path in the HEAD commit and print a special error if not.
9343 * This hack is for mpi@ <3
9345 static const struct got_error *
9346 tog_log_with_path(int argc, char *argv[])
9348 const struct got_error *error = NULL, *close_err;
9349 const struct tog_cmd *cmd = NULL;
9350 struct got_repository *repo = NULL;
9351 struct got_worktree *worktree = NULL;
9352 struct got_object_id *commit_id = NULL, *id = NULL;
9353 struct got_commit_object *commit = NULL;
9354 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9355 char *commit_id_str = NULL, **cmd_argv = NULL;
9356 int *pack_fds = NULL;
9358 cwd = getcwd(NULL, 0);
9360 return got_error_from_errno("getcwd");
9362 error = got_repo_pack_fds_open(&pack_fds);
9366 error = got_worktree_open(&worktree, cwd);
9367 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9371 repo_path = strdup(got_worktree_get_repo_path(worktree));
9373 repo_path = strdup(cwd);
9374 if (repo_path == NULL) {
9375 error = got_error_from_errno("strdup");
9379 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9383 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9388 error = tog_load_refs(repo, 0);
9391 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9392 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9393 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9398 got_worktree_close(worktree);
9402 error = got_object_open_as_commit(&commit, repo, commit_id);
9406 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9408 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9410 fprintf(stderr, "%s: '%s' is no known command or path\n",
9411 getprogname(), argv[0]);
9416 error = got_object_id_str(&commit_id_str, commit_id);
9420 cmd = &tog_commands[0]; /* log */
9422 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9423 error = cmd->cmd_main(argc, cmd_argv);
9426 close_err = got_repo_close(repo);
9431 got_object_commit_close(commit);
9433 got_worktree_close(worktree);
9435 const struct got_error *pack_err =
9436 got_repo_pack_fds_close(pack_fds);
9441 free(commit_id_str);
9448 for (i = 0; i < argc; i++)
9457 main(int argc, char *argv[])
9459 const struct got_error *error = NULL;
9460 const struct tog_cmd *cmd = NULL;
9461 int ch, hflag = 0, Vflag = 0;
9462 char **cmd_argv = NULL;
9463 static const struct option longopts[] = {
9464 { "version", no_argument, NULL, 'V' },
9467 char *diff_algo_str = NULL;
9469 setlocale(LC_CTYPE, "");
9472 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9477 if (!isatty(STDIN_FILENO))
9478 errx(1, "standard input is not a tty");
9480 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9500 got_version_print_str();
9507 /* Build an argument vector which runs a default command. */
9508 cmd = &tog_commands[0];
9510 cmd_argv = make_argv(argc, cmd->name);
9514 /* Did the user specify a command? */
9515 for (i = 0; i < nitems(tog_commands); i++) {
9516 if (strncmp(tog_commands[i].name, argv[0],
9517 strlen(argv[0])) == 0) {
9518 cmd = &tog_commands[i];
9524 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9525 if (diff_algo_str) {
9526 if (strcasecmp(diff_algo_str, "patience") == 0)
9527 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9528 if (strcasecmp(diff_algo_str, "myers") == 0)
9529 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9535 /* No command specified; try log with a path */
9536 error = tog_log_with_path(argc, argv);
9541 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9547 for (i = 0; i < argc; i++)
9552 if (error && error->code != GOT_ERR_CANCELLED &&
9553 error->code != GOT_ERR_EOF &&
9554 error->code != GOT_ERR_PRIVSEP_EXIT &&
9555 error->code != GOT_ERR_PRIVSEP_PIPE &&
9556 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9557 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);