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 "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/ioctl.h>
25 #if defined(__FreeBSD__) || defined(__APPLE__)
26 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error
*(*cmd_main
)(int, char *[]);
81 void (*cmd_usage
)(void);
84 __dead
static void usage(int, int);
85 __dead
static void usage_log(void);
86 __dead
static void usage_diff(void);
87 __dead
static void usage_blame(void);
88 __dead
static void usage_tree(void);
89 __dead
static void usage_ref(void);
91 static const struct got_error
* cmd_log(int, char *[]);
92 static const struct got_error
* cmd_diff(int, char *[]);
93 static const struct got_error
* cmd_blame(int, char *[]);
94 static const struct got_error
* cmd_tree(int, char *[]);
95 static const struct got_error
* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands
[] = {
98 { "log", cmd_log
, usage_log
},
99 { "diff", cmd_diff
, usage_diff
},
100 { "blame", cmd_blame
, usage_blame
},
101 { "tree", cmd_tree
, usage_tree
},
102 { "ref", cmd_ref
, usage_ref
},
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type
{
116 TOG_KEYMAP_KEYS
= -2,
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry
{
137 TAILQ_ENTRY(commit_queue_entry
) entry
;
138 struct got_object_id
*id
;
139 struct got_commit_object
*commit
;
142 TAILQ_HEAD(commit_queue_head
, commit_queue_entry
);
143 struct commit_queue
{
145 struct commit_queue_head head
;
149 STAILQ_ENTRY(tog_color
) entry
;
153 STAILQ_HEAD(tog_colors
, tog_color
);
155 static struct got_reflist_head tog_refs
= TAILQ_HEAD_INITIALIZER(tog_refs
);
156 static struct got_reflist_object_id_map
*tog_refs_idmap
;
157 static enum got_diff_algorithm tog_diff_algo
= GOT_DIFF_ALGORITHM_MYERS
;
159 static const struct got_error
*
160 tog_ref_cmp_by_name(void *arg
, int *cmp
, struct got_reference
*re1
,
161 struct got_reference
* re2
)
163 const char *name1
= got_ref_get_name(re1
);
164 const char *name2
= got_ref_get_name(re2
);
165 int isbackup1
, isbackup2
;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1
= strncmp(name1
, "refs/got/backup/", 16) == 0;
169 isbackup2
= strncmp(name2
, "refs/got/backup/", 16) == 0;
170 if (!isbackup1
&& isbackup2
) {
173 } else if (isbackup1
&& !isbackup2
) {
178 *cmp
= got_path_cmp(name1
, name2
, strlen(name1
), strlen(name2
));
182 static const struct got_error
*
183 tog_load_refs(struct got_repository
*repo
, int sort_by_date
)
185 const struct got_error
*err
;
187 err
= got_ref_list(&tog_refs
, repo
, NULL
, sort_by_date
?
188 got_ref_cmp_by_commit_timestamp_descending
: tog_ref_cmp_by_name
,
193 return got_reflist_object_id_map_create(&tog_refs_idmap
, &tog_refs
,
200 if (tog_refs_idmap
) {
201 got_reflist_object_id_map_free(tog_refs_idmap
);
202 tog_refs_idmap
= NULL
;
204 got_ref_list_free(&tog_refs
);
207 static const struct got_error
*
208 add_color(struct tog_colors
*colors
, const char *pattern
,
209 int idx
, short color
)
211 const struct got_error
*err
= NULL
;
212 struct tog_color
*tc
;
215 if (idx
< 1 || idx
> COLOR_PAIRS
- 1)
218 init_pair(idx
, color
, -1);
220 tc
= calloc(1, sizeof(*tc
));
222 return got_error_from_errno("calloc");
223 regerr
= regcomp(&tc
->regex
, pattern
,
224 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
);
226 static char regerr_msg
[512];
227 static char err_msg
[512];
228 regerror(regerr
, &tc
->regex
, regerr_msg
,
230 snprintf(err_msg
, sizeof(err_msg
), "regcomp: %s",
232 err
= got_error_msg(GOT_ERR_REGEX
, err_msg
);
237 STAILQ_INSERT_HEAD(colors
, tc
, entry
);
242 free_colors(struct tog_colors
*colors
)
244 struct tog_color
*tc
;
246 while (!STAILQ_EMPTY(colors
)) {
247 tc
= STAILQ_FIRST(colors
);
248 STAILQ_REMOVE_HEAD(colors
, entry
);
254 static struct tog_color
*
255 get_color(struct tog_colors
*colors
, int colorpair
)
257 struct tog_color
*tc
= NULL
;
259 STAILQ_FOREACH(tc
, colors
, entry
) {
260 if (tc
->colorpair
== colorpair
)
268 default_color_value(const char *envvar
)
270 if (strcmp(envvar
, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA
;
272 if (strcmp(envvar
, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar
, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar
, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar
, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA
;
280 if (strcmp(envvar
, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA
;
282 if (strcmp(envvar
, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar
, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar
, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar
, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar
, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar
, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar
, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA
;
296 if (strcmp(envvar
, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar
, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar
)
307 const char *val
= getenv(envvar
);
310 return default_color_value(envvar
);
312 if (strcasecmp(val
, "black") == 0)
314 if (strcasecmp(val
, "red") == 0)
316 if (strcasecmp(val
, "green") == 0)
318 if (strcasecmp(val
, "yellow") == 0)
320 if (strcasecmp(val
, "blue") == 0)
322 if (strcasecmp(val
, "magenta") == 0)
323 return COLOR_MAGENTA
;
324 if (strcasecmp(val
, "cyan") == 0)
326 if (strcasecmp(val
, "white") == 0)
328 if (strcasecmp(val
, "default") == 0)
331 return default_color_value(envvar
);
334 struct tog_diff_view_state
{
335 struct got_object_id
*id1
, *id2
;
336 const char *label1
, *label2
;
340 int first_displayed_line
;
341 int last_displayed_line
;
344 int ignore_whitespace
;
346 struct got_repository
*repo
;
347 struct got_diff_line
*lines
;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view
*parent_view
;
356 pthread_mutex_t tog_mutex
= PTHREAD_MUTEX_INITIALIZER
;
357 static volatile sig_atomic_t tog_thread_error
;
359 struct tog_log_thread_args
{
360 pthread_cond_t need_commits
;
361 pthread_cond_t commit_loaded
;
364 struct got_commit_graph
*graph
;
365 struct commit_queue
*real_commits
;
366 const char *in_repo_path
;
367 struct got_object_id
*start_id
;
368 struct got_repository
*repo
;
372 struct commit_queue_entry
**first_displayed_entry
;
373 struct commit_queue_entry
**selected_entry
;
375 int *search_next_done
;
379 regex_t
*limit_regex
;
380 struct commit_queue
*limit_commits
;
383 struct tog_log_view_state
{
384 struct commit_queue
*commits
;
385 struct commit_queue_entry
*first_displayed_entry
;
386 struct commit_queue_entry
*last_displayed_entry
;
387 struct commit_queue_entry
*selected_entry
;
388 struct commit_queue real_commits
;
393 struct got_repository
*repo
;
394 struct got_object_id
*start_id
;
397 struct tog_log_thread_args thread_args
;
398 struct commit_queue_entry
*matched_entry
;
399 struct commit_queue_entry
*search_entry
;
400 struct tog_colors colors
;
404 struct commit_queue limit_commits
;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args
{
424 struct tog_blame_line
*lines
; /* one per line */
427 struct tog_view
*view
;
428 struct got_object_id
*commit_id
;
432 struct tog_blame_thread_args
{
434 struct got_repository
*repo
;
435 struct tog_blame_cb_args
*cb_args
;
437 got_cancel_cb cancel_cb
;
444 struct tog_blame_line
*lines
;
448 struct tog_blame_thread_args thread_args
;
449 struct tog_blame_cb_args cb_args
;
454 struct tog_blame_view_state
{
455 int first_displayed_line
;
456 int last_displayed_line
;
458 int last_diffed_line
;
462 struct got_object_id_queue blamed_commits
;
463 struct got_object_qid
*blamed_commit
;
465 struct got_repository
*repo
;
466 struct got_object_id
*commit_id
;
467 struct got_object_id
*id_to_log
;
468 struct tog_blame blame
;
470 struct tog_colors colors
;
473 struct tog_parent_tree
{
474 TAILQ_ENTRY(tog_parent_tree
) entry
;
475 struct got_tree_object
*tree
;
476 struct got_tree_entry
*first_displayed_entry
;
477 struct got_tree_entry
*selected_entry
;
481 TAILQ_HEAD(tog_parent_trees
, tog_parent_tree
);
483 struct tog_tree_view_state
{
485 struct got_object_id
*commit_id
;/* commit which this tree belongs to */
486 struct got_tree_object
*root
; /* the commit's root tree entry */
487 struct got_tree_object
*tree
; /* currently displayed (sub-)tree */
488 struct got_tree_entry
*first_displayed_entry
;
489 struct got_tree_entry
*last_displayed_entry
;
490 struct got_tree_entry
*selected_entry
;
491 int ndisplayed
, selected
, show_ids
;
492 struct tog_parent_trees parents
; /* parent trees of current sub-tree */
494 struct got_repository
*repo
;
495 struct got_tree_entry
*matched_entry
;
496 struct tog_colors colors
;
499 struct tog_reflist_entry
{
500 TAILQ_ENTRY(tog_reflist_entry
) entry
;
501 struct got_reference
*ref
;
505 TAILQ_HEAD(tog_reflist_head
, tog_reflist_entry
);
507 struct tog_ref_view_state
{
508 struct tog_reflist_head refs
;
509 struct tog_reflist_entry
*first_displayed_entry
;
510 struct tog_reflist_entry
*last_displayed_entry
;
511 struct tog_reflist_entry
*selected_entry
;
512 int nrefs
, ndisplayed
, selected
, show_date
, show_ids
, sort_by_date
;
513 struct got_repository
*repo
;
514 struct tog_reflist_entry
*matched_entry
;
515 struct tog_colors colors
;
518 struct tog_help_view_state
{
523 int first_displayed_line
;
524 int last_displayed_line
;
529 enum tog_keymap_type type
;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g", "Go to line N (default: first line)"), \
542 KEY_("Home =", "Go to the first line"), \
543 KEY_("G", "Go to line N (default: last line)"), \
544 KEY_("End *", "Go to the last line"), \
545 KEY_("l Right", "Scroll the view right"), \
546 KEY_("h Left", "Scroll the view left"), \
547 KEY_("$", "Scroll view to the rightmost position"), \
548 KEY_("0", "Scroll view to the leftmost position"), \
549 KEY_("-", "Decrease size of the focussed split"), \
550 KEY_("+", "Increase size of the focussed split"), \
551 KEY_("Tab", "Switch focus between views"), \
552 KEY_("F", "Toggle fullscreen mode"), \
553 KEY_("S", "Switch split-screen layout"), \
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
;
619 /* curses io for tog regress */
625 static int using_mock_io
;
627 #define TOG_SCREEN_DUMP "SCREENDUMP"
628 #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
629 #define TOG_KEY_SCRDUMP SHRT_MIN
632 * We implement two types of views: parent views and child views.
634 * The 'Tab' key switches focus between a parent view and its child view.
635 * Child views are shown side-by-side to their parent view, provided
636 * there is enough screen estate.
638 * When a new view is opened from within a parent view, this new view
639 * becomes a child view of the parent view, replacing any existing child.
641 * When a new view is opened from within a child view, this new view
642 * becomes a parent view which will obscure the views below until the
643 * user quits the new parent view by typing 'q'.
645 * This list of views contains parent views only.
646 * Child views are only pointed to by their parent view.
648 TAILQ_HEAD(tog_view_list_head
, tog_view
);
651 TAILQ_ENTRY(tog_view
) entry
;
654 int nlines
, ncols
, begin_y
, begin_x
; /* based on split height/width */
655 int resized_y
, resized_x
; /* begin_y/x based on user resizing */
656 int maxx
, x
; /* max column and current start column */
657 int lines
, cols
; /* copies of LINES and COLS */
658 int nscrolled
, offset
; /* lines scrolled and hsplit line offset */
659 int gline
, hiline
; /* navigate to and highlight this nG line */
660 int ch
, count
; /* current keymap and count prefix */
661 int resized
; /* set when in a resize event */
662 int focussed
; /* Only set on one parent or child view at a time. */
664 struct tog_view
*parent
;
665 struct tog_view
*child
;
668 * This flag is initially set on parent views when a new child view
669 * is created. It gets toggled when the 'Tab' key switches focus
670 * between parent and child.
671 * The flag indicates whether focus should be passed on to our child
672 * view if this parent view gets picked for focus after another parent
673 * view was closed. This prevents child views from losing focus in such
678 enum tog_view_mode mode
;
679 /* type-specific state */
680 enum tog_view_type type
;
682 struct tog_diff_view_state diff
;
683 struct tog_log_view_state log
;
684 struct tog_blame_view_state blame
;
685 struct tog_tree_view_state tree
;
686 struct tog_ref_view_state ref
;
687 struct tog_help_view_state help
;
690 const struct got_error
*(*show
)(struct tog_view
*);
691 const struct got_error
*(*input
)(struct tog_view
**,
692 struct tog_view
*, int);
693 const struct got_error
*(*reset
)(struct tog_view
*);
694 const struct got_error
*(*resize
)(struct tog_view
*, int);
695 const struct got_error
*(*close
)(struct tog_view
*);
697 const struct got_error
*(*search_start
)(struct tog_view
*);
698 const struct got_error
*(*search_next
)(struct tog_view
*);
699 void (*search_setup
)(struct tog_view
*, FILE **, off_t
**, size_t *,
700 int **, int **, int **, int **);
703 #define TOG_SEARCH_FORWARD 1
704 #define TOG_SEARCH_BACKWARD 2
705 int search_next_done
;
706 #define TOG_SEARCH_HAVE_MORE 1
707 #define TOG_SEARCH_NO_MORE 2
708 #define TOG_SEARCH_HAVE_NONE 3
714 static const struct got_error
*open_diff_view(struct tog_view
*,
715 struct got_object_id
*, struct got_object_id
*,
716 const char *, const char *, int, int, int, struct tog_view
*,
717 struct got_repository
*);
718 static const struct got_error
*show_diff_view(struct tog_view
*);
719 static const struct got_error
*input_diff_view(struct tog_view
**,
720 struct tog_view
*, int);
721 static const struct got_error
*reset_diff_view(struct tog_view
*);
722 static const struct got_error
* close_diff_view(struct tog_view
*);
723 static const struct got_error
*search_start_diff_view(struct tog_view
*);
724 static void search_setup_diff_view(struct tog_view
*, FILE **, off_t
**,
725 size_t *, int **, int **, int **, int **);
726 static const struct got_error
*search_next_view_match(struct tog_view
*);
728 static const struct got_error
*open_log_view(struct tog_view
*,
729 struct got_object_id
*, struct got_repository
*,
730 const char *, const char *, int);
731 static const struct got_error
* show_log_view(struct tog_view
*);
732 static const struct got_error
*input_log_view(struct tog_view
**,
733 struct tog_view
*, int);
734 static const struct got_error
*resize_log_view(struct tog_view
*, int);
735 static const struct got_error
*close_log_view(struct tog_view
*);
736 static const struct got_error
*search_start_log_view(struct tog_view
*);
737 static const struct got_error
*search_next_log_view(struct tog_view
*);
739 static const struct got_error
*open_blame_view(struct tog_view
*, char *,
740 struct got_object_id
*, struct got_repository
*);
741 static const struct got_error
*show_blame_view(struct tog_view
*);
742 static const struct got_error
*input_blame_view(struct tog_view
**,
743 struct tog_view
*, int);
744 static const struct got_error
*reset_blame_view(struct tog_view
*);
745 static const struct got_error
*close_blame_view(struct tog_view
*);
746 static const struct got_error
*search_start_blame_view(struct tog_view
*);
747 static void search_setup_blame_view(struct tog_view
*, FILE **, off_t
**,
748 size_t *, int **, int **, int **, int **);
750 static const struct got_error
*open_tree_view(struct tog_view
*,
751 struct got_object_id
*, const char *, struct got_repository
*);
752 static const struct got_error
*show_tree_view(struct tog_view
*);
753 static const struct got_error
*input_tree_view(struct tog_view
**,
754 struct tog_view
*, int);
755 static const struct got_error
*close_tree_view(struct tog_view
*);
756 static const struct got_error
*search_start_tree_view(struct tog_view
*);
757 static const struct got_error
*search_next_tree_view(struct tog_view
*);
759 static const struct got_error
*open_ref_view(struct tog_view
*,
760 struct got_repository
*);
761 static const struct got_error
*show_ref_view(struct tog_view
*);
762 static const struct got_error
*input_ref_view(struct tog_view
**,
763 struct tog_view
*, int);
764 static const struct got_error
*close_ref_view(struct tog_view
*);
765 static const struct got_error
*search_start_ref_view(struct tog_view
*);
766 static const struct got_error
*search_next_ref_view(struct tog_view
*);
768 static const struct got_error
*open_help_view(struct tog_view
*,
770 static const struct got_error
*show_help_view(struct tog_view
*);
771 static const struct got_error
*input_help_view(struct tog_view
**,
772 struct tog_view
*, int);
773 static const struct got_error
*reset_help_view(struct tog_view
*);
774 static const struct got_error
* close_help_view(struct tog_view
*);
775 static const struct got_error
*search_start_help_view(struct tog_view
*);
776 static void search_setup_help_view(struct tog_view
*, FILE **, off_t
**,
777 size_t *, int **, int **, int **, int **);
779 static volatile sig_atomic_t tog_sigwinch_received
;
780 static volatile sig_atomic_t tog_sigpipe_received
;
781 static volatile sig_atomic_t tog_sigcont_received
;
782 static volatile sig_atomic_t tog_sigint_received
;
783 static volatile sig_atomic_t tog_sigterm_received
;
786 tog_sigwinch(int signo
)
788 tog_sigwinch_received
= 1;
792 tog_sigpipe(int signo
)
794 tog_sigpipe_received
= 1;
798 tog_sigcont(int signo
)
800 tog_sigcont_received
= 1;
804 tog_sigint(int signo
)
806 tog_sigint_received
= 1;
810 tog_sigterm(int signo
)
812 tog_sigterm_received
= 1;
816 tog_fatal_signal_received(void)
818 return (tog_sigpipe_received
||
819 tog_sigint_received
|| tog_sigterm_received
);
822 static const struct got_error
*
823 view_close(struct tog_view
*view
)
825 const struct got_error
*err
= NULL
, *child_err
= NULL
;
828 child_err
= view_close(view
->child
);
832 err
= view
->close(view
);
834 del_panel(view
->panel
);
836 delwin(view
->window
);
838 return err
? err
: child_err
;
841 static struct tog_view
*
842 view_open(int nlines
, int ncols
, int begin_y
, int begin_x
,
843 enum tog_view_type type
)
845 struct tog_view
*view
= calloc(1, sizeof(*view
));
853 view
->nlines
= nlines
? nlines
: LINES
- begin_y
;
854 view
->ncols
= ncols
? ncols
: COLS
- begin_x
;
855 view
->begin_y
= begin_y
;
856 view
->begin_x
= begin_x
;
857 view
->window
= newwin(nlines
, ncols
, begin_y
, begin_x
);
858 if (view
->window
== NULL
) {
862 view
->panel
= new_panel(view
->window
);
863 if (view
->panel
== NULL
||
864 set_panel_userptr(view
->panel
, view
) != OK
) {
869 keypad(view
->window
, TRUE
);
874 view_split_begin_x(int begin_x
)
876 if (begin_x
> 0 || COLS
< 120)
878 return (COLS
- MAX(COLS
/ 2, 80));
881 /* XXX Stub till we decide what to do. */
883 view_split_begin_y(int lines
)
885 return lines
* HSPLIT_SCALE
;
888 static const struct got_error
*view_resize(struct tog_view
*);
890 static const struct got_error
*
891 view_splitscreen(struct tog_view
*view
)
893 const struct got_error
*err
= NULL
;
895 if (!view
->resized
&& view
->mode
== TOG_VIEW_SPLIT_HRZN
) {
896 if (view
->resized_y
&& view
->resized_y
< view
->lines
)
897 view
->begin_y
= view
->resized_y
;
899 view
->begin_y
= view_split_begin_y(view
->nlines
);
901 } else if (!view
->resized
) {
902 if (view
->resized_x
&& view
->resized_x
< view
->cols
- 1 &&
904 view
->begin_x
= view
->resized_x
;
906 view
->begin_x
= view_split_begin_x(0);
909 view
->nlines
= LINES
- view
->begin_y
;
910 view
->ncols
= COLS
- view
->begin_x
;
913 err
= view_resize(view
);
917 if (view
->parent
&& view
->mode
== TOG_VIEW_SPLIT_HRZN
)
918 view
->parent
->nlines
= view
->begin_y
;
920 if (mvwin(view
->window
, view
->begin_y
, view
->begin_x
) == ERR
)
921 return got_error_from_errno("mvwin");
926 static const struct got_error
*
927 view_fullscreen(struct tog_view
*view
)
929 const struct got_error
*err
= NULL
;
932 view
->begin_y
= view
->resized
? view
->begin_y
: 0;
933 view
->nlines
= view
->resized
? view
->nlines
: LINES
;
937 err
= view_resize(view
);
941 if (mvwin(view
->window
, view
->begin_y
, view
->begin_x
) == ERR
)
942 return got_error_from_errno("mvwin");
948 view_is_parent_view(struct tog_view
*view
)
950 return view
->parent
== NULL
;
954 view_is_splitscreen(struct tog_view
*view
)
956 return view
->begin_x
> 0 || view
->begin_y
> 0;
960 view_is_fullscreen(struct tog_view
*view
)
962 return view
->nlines
== LINES
&& view
->ncols
== COLS
;
966 view_is_hsplit_top(struct tog_view
*view
)
968 return view
->mode
== TOG_VIEW_SPLIT_HRZN
&& view
->child
&&
969 view_is_splitscreen(view
->child
);
973 view_border(struct tog_view
*view
)
976 const struct tog_view
*view_above
;
979 return view_border(view
->parent
);
981 panel
= panel_above(view
->panel
);
985 view_above
= panel_userptr(panel
);
986 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
)
987 mvwhline(view
->window
, view_above
->begin_y
- 1,
988 view
->begin_x
, got_locale_is_utf8() ?
989 ACS_HLINE
: '-', view
->ncols
);
991 mvwvline(view
->window
, view
->begin_y
, view_above
->begin_x
- 1,
992 got_locale_is_utf8() ? ACS_VLINE
: '|', view
->nlines
);
995 static const struct got_error
*view_init_hsplit(struct tog_view
*, int);
996 static const struct got_error
*request_log_commits(struct tog_view
*);
997 static const struct got_error
*offset_selection_down(struct tog_view
*);
998 static void offset_selection_up(struct tog_view
*);
999 static void view_get_split(struct tog_view
*, int *, int *);
1001 static const struct got_error
*
1002 view_resize(struct tog_view
*view
)
1004 const struct got_error
*err
= NULL
;
1005 int dif
, nlines
, ncols
;
1007 dif
= LINES
- view
->lines
; /* line difference */
1009 if (view
->lines
> LINES
)
1010 nlines
= view
->nlines
- (view
->lines
- LINES
);
1012 nlines
= view
->nlines
+ (LINES
- view
->lines
);
1013 if (view
->cols
> COLS
)
1014 ncols
= view
->ncols
- (view
->cols
- COLS
);
1016 ncols
= view
->ncols
+ (COLS
- view
->cols
);
1019 int hs
= view
->child
->begin_y
;
1021 if (!view_is_fullscreen(view
))
1022 view
->child
->begin_x
= view_split_begin_x(view
->begin_x
);
1023 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
||
1024 view
->child
->begin_x
== 0) {
1027 view_fullscreen(view
->child
);
1028 if (view
->child
->focussed
)
1029 show_panel(view
->child
->panel
);
1031 show_panel(view
->panel
);
1033 ncols
= view
->child
->begin_x
;
1035 view_splitscreen(view
->child
);
1036 show_panel(view
->child
->panel
);
1039 * XXX This is ugly and needs to be moved into the above
1040 * logic but "works" for now and my attempts at moving it
1041 * break either 'tab' or 'F' key maps in horizontal splits.
1044 err
= view_splitscreen(view
->child
);
1047 if (dif
< 0) { /* top split decreased */
1048 err
= offset_selection_down(view
);
1055 show_panel(view
->child
->panel
);
1056 nlines
= view
->nlines
;
1058 } else if (view
->parent
== NULL
)
1061 if (view
->resize
&& dif
> 0) {
1062 err
= view
->resize(view
, dif
);
1067 if (wresize(view
->window
, nlines
, ncols
) == ERR
)
1068 return got_error_from_errno("wresize");
1069 if (replace_panel(view
->panel
, view
->window
) == ERR
)
1070 return got_error_from_errno("replace_panel");
1071 wclear(view
->window
);
1073 view
->nlines
= nlines
;
1074 view
->ncols
= ncols
;
1075 view
->lines
= LINES
;
1081 static const struct got_error
*
1082 resize_log_view(struct tog_view
*view
, int increase
)
1084 struct tog_log_view_state
*s
= &view
->state
.log
;
1085 const struct got_error
*err
= NULL
;
1088 if (s
->selected_entry
)
1089 n
= s
->selected_entry
->idx
+ view
->lines
- s
->selected
;
1092 * Request commits to account for the increased
1093 * height so we have enough to populate the view.
1095 if (s
->commits
->ncommits
< n
) {
1096 view
->nscrolled
= n
- s
->commits
->ncommits
+ increase
+ 1;
1097 err
= request_log_commits(view
);
1104 view_adjust_offset(struct tog_view
*view
, int n
)
1109 if (view
->parent
&& view
->parent
->offset
) {
1110 if (view
->parent
->offset
+ n
>= 0)
1111 view
->parent
->offset
+= n
;
1113 view
->parent
->offset
= 0;
1114 } else if (view
->offset
) {
1115 if (view
->offset
- n
>= 0)
1122 static const struct got_error
*
1123 view_resize_split(struct tog_view
*view
, int resize
)
1125 const struct got_error
*err
= NULL
;
1126 struct tog_view
*v
= NULL
;
1133 if (!v
->child
|| !view_is_splitscreen(v
->child
))
1136 v
->resized
= v
->child
->resized
= resize
; /* lock for resize event */
1138 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
) {
1139 if (v
->child
->resized_y
)
1140 v
->child
->begin_y
= v
->child
->resized_y
;
1142 v
->child
->begin_y
-= resize
;
1144 v
->child
->begin_y
+= resize
;
1145 if (v
->child
->begin_y
< 3) {
1147 v
->child
->begin_y
= 3;
1148 } else if (v
->child
->begin_y
> LINES
- 1) {
1150 v
->child
->begin_y
= LINES
- 1;
1153 v
->child
->ncols
= COLS
;
1154 view_adjust_offset(view
, resize
);
1155 err
= view_init_hsplit(v
, v
->child
->begin_y
);
1158 v
->child
->resized_y
= v
->child
->begin_y
;
1160 if (v
->child
->resized_x
)
1161 v
->child
->begin_x
= v
->child
->resized_x
;
1163 v
->child
->begin_x
-= resize
;
1165 v
->child
->begin_x
+= resize
;
1166 if (v
->child
->begin_x
< 11) {
1168 v
->child
->begin_x
= 11;
1169 } else if (v
->child
->begin_x
> COLS
- 1) {
1171 v
->child
->begin_x
= COLS
- 1;
1173 v
->child
->resized_x
= v
->child
->begin_x
;
1176 v
->child
->mode
= v
->mode
;
1177 v
->child
->nlines
= v
->lines
- v
->child
->begin_y
;
1178 v
->child
->ncols
= v
->cols
- v
->child
->begin_x
;
1181 err
= view_fullscreen(v
);
1184 err
= view_splitscreen(v
->child
);
1188 if (v
->mode
== TOG_VIEW_SPLIT_HRZN
) {
1189 err
= offset_selection_down(v
->child
);
1195 err
= v
->resize(v
, 0);
1196 else if (v
->child
->resize
)
1197 err
= v
->child
->resize(v
->child
, 0);
1199 v
->resized
= v
->child
->resized
= 0;
1205 view_transfer_size(struct tog_view
*dst
, struct tog_view
*src
)
1207 struct tog_view
*v
= src
->child
? src
->child
: src
;
1209 dst
->resized_x
= v
->resized_x
;
1210 dst
->resized_y
= v
->resized_y
;
1213 static const struct got_error
*
1214 view_close_child(struct tog_view
*view
)
1216 const struct got_error
*err
= NULL
;
1218 if (view
->child
== NULL
)
1221 err
= view_close(view
->child
);
1226 static const struct got_error
*
1227 view_set_child(struct tog_view
*view
, struct tog_view
*child
)
1229 const struct got_error
*err
= NULL
;
1231 view
->child
= child
;
1232 child
->parent
= view
;
1234 err
= view_resize(view
);
1238 if (view
->child
->resized_x
|| view
->child
->resized_y
)
1239 err
= view_resize_split(view
, 0);
1244 static const struct got_error
*view_dispatch_request(struct tog_view
**,
1245 struct tog_view
*, enum tog_view_type
, int, int);
1247 static const struct got_error
*
1248 view_request_new(struct tog_view
**requested
, struct tog_view
*view
,
1249 enum tog_view_type request
)
1251 struct tog_view
*new_view
= NULL
;
1252 const struct got_error
*err
;
1257 if (view_is_parent_view(view
) && request
!= TOG_VIEW_HELP
)
1258 view_get_split(view
, &y
, &x
);
1260 err
= view_dispatch_request(&new_view
, view
, request
, y
, x
);
1264 if (view_is_parent_view(view
) && view
->mode
== TOG_VIEW_SPLIT_HRZN
&&
1265 request
!= TOG_VIEW_HELP
) {
1266 err
= view_init_hsplit(view
, y
);
1272 new_view
->focussed
= 1;
1273 new_view
->mode
= view
->mode
;
1274 new_view
->nlines
= request
== TOG_VIEW_HELP
?
1275 view
->lines
: view
->lines
- y
;
1277 if (view_is_parent_view(view
) && request
!= TOG_VIEW_HELP
) {
1278 view_transfer_size(new_view
, view
);
1279 err
= view_close_child(view
);
1282 err
= view_set_child(view
, new_view
);
1285 view
->focus_child
= 1;
1287 *requested
= new_view
;
1293 tog_resizeterm(void)
1296 struct winsize size
;
1298 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &size
) < 0) {
1299 cols
= 80; /* Default */
1303 lines
= size
.ws_row
;
1305 resize_term(lines
, cols
);
1308 static const struct got_error
*
1309 view_search_start(struct tog_view
*view
, int fast_refresh
)
1311 const struct got_error
*err
= NULL
;
1312 struct tog_view
*v
= view
;
1316 if (view
->search_started
) {
1317 regfree(&view
->regex
);
1318 view
->searching
= 0;
1319 memset(&view
->regmatch
, 0, sizeof(view
->regmatch
));
1321 view
->search_started
= 0;
1323 if (view
->nlines
< 1)
1326 if (view_is_hsplit_top(view
))
1328 else if (view
->mode
== TOG_VIEW_SPLIT_VERT
&& view
->parent
)
1331 mvwaddstr(v
->window
, v
->nlines
- 1, 0, "/");
1332 wclrtoeol(v
->window
);
1334 nodelay(v
->window
, FALSE
); /* block for search term input */
1337 ret
= wgetnstr(v
->window
, pattern
, sizeof(pattern
));
1338 wrefresh(v
->window
);
1341 nodelay(v
->window
, TRUE
);
1342 if (!fast_refresh
&& !using_mock_io
)
1347 if (regcomp(&view
->regex
, pattern
, REG_EXTENDED
| REG_NEWLINE
) == 0) {
1348 err
= view
->search_start(view
);
1350 regfree(&view
->regex
);
1353 view
->search_started
= 1;
1354 view
->searching
= TOG_SEARCH_FORWARD
;
1355 view
->search_next_done
= 0;
1356 view
->search_next(view
);
1362 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1363 static const struct got_error
*
1364 switch_split(struct tog_view
*view
)
1366 const struct got_error
*err
= NULL
;
1367 struct tog_view
*v
= NULL
;
1374 if (v
->mode
== TOG_VIEW_SPLIT_HRZN
)
1375 v
->mode
= TOG_VIEW_SPLIT_VERT
;
1377 v
->mode
= TOG_VIEW_SPLIT_HRZN
;
1381 else if (v
->mode
== TOG_VIEW_SPLIT_VERT
&& v
->cols
< 120)
1382 v
->mode
= TOG_VIEW_SPLIT_NONE
;
1384 view_get_split(v
, &v
->child
->begin_y
, &v
->child
->begin_x
);
1385 if (v
->mode
== TOG_VIEW_SPLIT_HRZN
&& v
->child
->resized_y
)
1386 v
->child
->begin_y
= v
->child
->resized_y
;
1387 else if (v
->mode
== TOG_VIEW_SPLIT_VERT
&& v
->child
->resized_x
)
1388 v
->child
->begin_x
= v
->child
->resized_x
;
1391 if (v
->mode
== TOG_VIEW_SPLIT_HRZN
) {
1393 v
->child
->ncols
= COLS
;
1394 v
->child
->nscrolled
= LINES
- v
->child
->nlines
;
1396 err
= view_init_hsplit(v
, v
->child
->begin_y
);
1400 v
->child
->mode
= v
->mode
;
1401 v
->child
->nlines
= v
->lines
- v
->child
->begin_y
;
1404 err
= view_fullscreen(v
);
1407 err
= view_splitscreen(v
->child
);
1411 if (v
->mode
== TOG_VIEW_SPLIT_NONE
)
1412 v
->mode
= TOG_VIEW_SPLIT_VERT
;
1413 if (v
->mode
== TOG_VIEW_SPLIT_HRZN
) {
1414 err
= offset_selection_down(v
);
1417 err
= offset_selection_down(v
->child
);
1421 offset_selection_up(v
);
1422 offset_selection_up(v
->child
);
1425 err
= v
->resize(v
, 0);
1426 else if (v
->child
->resize
)
1427 err
= v
->child
->resize(v
->child
, 0);
1433 * Strip trailing whitespace from str starting at byte *n;
1434 * if *n < 0, use strlen(str). Return new str length in *n.
1437 strip_trailing_ws(char *str
, int *n
)
1441 if (str
== NULL
|| *str
== '\0')
1447 while (x
-- > 0 && isspace((unsigned char)str
[x
]))
1454 * Extract visible substring of line y from the curses screen
1455 * and strip trailing whitespace. If vline is set and locale is
1456 * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1457 * character is written out as 'x'. Write the line to file f.
1459 static const struct got_error
*
1460 view_write_line(FILE *f
, int y
, int vline
)
1462 char line
[COLS
* MB_LEN_MAX
]; /* allow for multibyte chars */
1465 r
= mvwinnstr(curscr
, y
, 0, line
, sizeof(line
));
1467 return got_error_fmt(GOT_ERR_RANGE
,
1468 "failed to extract line %d", y
);
1471 * In some views, lines are padded with blanks to COLS width.
1472 * Strip them so we can diff without the -b flag when testing.
1474 strip_trailing_ws(line
, &r
);
1476 if (vline
> 0 && got_locale_is_utf8())
1479 w
= fprintf(f
, "%s\n", line
);
1480 if (w
!= r
+ 1) /* \n */
1481 return got_ferror(f
, GOT_ERR_IO
);
1487 * Capture the visible curses screen by writing each line to the
1488 * file at the path set via the TOG_SCR_DUMP environment variable.
1490 static const struct got_error
*
1491 screendump(struct tog_view
*view
)
1493 const struct got_error
*err
;
1498 path
= getenv("TOG_SCR_DUMP");
1499 if (path
== NULL
|| *path
== '\0')
1500 return got_error_msg(GOT_ERR_BAD_PATH
,
1501 "TOG_SCR_DUMP path not set to capture screen dump");
1502 f
= fopen(path
, "wex");
1504 return got_error_from_errno_fmt("fopen: %s", path
);
1506 if ((view
->child
&& view
->child
->begin_x
) ||
1507 (view
->parent
&& view
->begin_x
)) {
1508 int ncols
= view
->child
? view
->ncols
: view
->parent
->ncols
;
1510 /* vertical splitscreen */
1511 for (i
= 0; i
< view
->nlines
; ++i
) {
1512 err
= view_write_line(f
, i
, ncols
- 1);
1519 /* fullscreen or horizontal splitscreen */
1520 if ((view
->child
&& view
->child
->begin_y
) ||
1521 (view
->parent
&& view
->begin_y
)) /* hsplit */
1522 hline
= view
->child
?
1523 view
->child
->begin_y
: view
->begin_y
;
1525 for (i
= 0; i
< view
->lines
; i
++) {
1526 if (hline
&& got_locale_is_utf8() && i
== hline
- 1) {
1529 /* ACS_HLINE writes out as 'q', overwrite it */
1530 for (c
= 0; c
< view
->cols
; ++c
)
1536 err
= view_write_line(f
, i
, 0);
1543 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
1544 err
= got_ferror(f
, GOT_ERR_IO
);
1549 * Compute view->count from numeric input. Assign total to view->count and
1550 * return first non-numeric key entered.
1553 get_compound_key(struct tog_view
*view
, int c
)
1555 struct tog_view
*v
= view
;
1558 if (view_is_hsplit_top(view
))
1560 else if (view
->mode
== TOG_VIEW_SPLIT_VERT
&& view
->parent
)
1564 cbreak(); /* block for input */
1565 nodelay(view
->window
, FALSE
);
1566 wmove(v
->window
, v
->nlines
- 1, 0);
1567 wclrtoeol(v
->window
);
1568 waddch(v
->window
, ':');
1571 x
= getcurx(v
->window
);
1572 if (x
!= ERR
&& x
< view
->ncols
) {
1573 waddch(v
->window
, c
);
1574 wrefresh(v
->window
);
1578 * Don't overflow. Max valid request should be the greatest
1579 * between the longest and total lines; cap at 10 million.
1584 n
= n
* 10 + (c
- '0');
1585 } while (((c
= wgetch(view
->window
))) >= '0' && c
<= '9' && c
!= ERR
);
1587 if (c
== 'G' || c
== 'g') { /* nG key map */
1588 view
->gline
= view
->hiline
= n
;
1593 /* Massage excessive or inapplicable values at the input handler. */
1600 action_report(struct tog_view
*view
)
1602 struct tog_view
*v
= view
;
1604 if (view_is_hsplit_top(view
))
1606 else if (view
->mode
== TOG_VIEW_SPLIT_VERT
&& view
->parent
)
1609 wmove(v
->window
, v
->nlines
- 1, 0);
1610 wclrtoeol(v
->window
);
1611 wprintw(v
->window
, ":%s", view
->action
);
1612 wrefresh(v
->window
);
1615 * Clear action status report. Only clear in blame view
1616 * once annotating is complete, otherwise it's too fast.
1618 if (view
->type
== TOG_VIEW_BLAME
) {
1619 if (view
->state
.blame
.blame_complete
)
1620 view
->action
= NULL
;
1622 view
->action
= NULL
;
1626 * Read the next line from the test script and assign
1627 * key instruction to *ch. If at EOF, set the *done flag.
1629 static const struct got_error
*
1630 tog_read_script_key(FILE *script
, int *ch
, int *done
)
1632 const struct got_error
*err
= NULL
;
1636 if (getline(&line
, &linesz
, script
) == -1) {
1641 err
= got_ferror(script
, GOT_ERR_IO
);
1644 } else if (strncasecmp(line
, "KEY_ENTER", 9) == 0)
1646 else if (strncasecmp(line
, "KEY_RIGHT", 9) == 0)
1648 else if (strncasecmp(line
, "KEY_LEFT", 8) == 0)
1650 else if (strncasecmp(line
, "KEY_DOWN", 8) == 0)
1652 else if (strncasecmp(line
, "KEY_UP", 6) == 0)
1654 else if (strncasecmp(line
, TOG_SCREEN_DUMP
, TOG_SCREEN_DUMP_LEN
) == 0)
1655 *ch
= TOG_KEY_SCRDUMP
;
1664 static const struct got_error
*
1665 view_input(struct tog_view
**new, int *done
, struct tog_view
*view
,
1666 struct tog_view_list_head
*views
, int fast_refresh
)
1668 const struct got_error
*err
= NULL
;
1675 action_report(view
);
1677 /* Clear "no matches" indicator. */
1678 if (view
->search_next_done
== TOG_SEARCH_NO_MORE
||
1679 view
->search_next_done
== TOG_SEARCH_HAVE_NONE
) {
1680 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
1684 if (view
->searching
&& !view
->search_next_done
) {
1685 errcode
= pthread_mutex_unlock(&tog_mutex
);
1687 return got_error_set_errno(errcode
,
1688 "pthread_mutex_unlock");
1690 errcode
= pthread_mutex_lock(&tog_mutex
);
1692 return got_error_set_errno(errcode
,
1693 "pthread_mutex_lock");
1694 view
->search_next(view
);
1698 /* Allow threads to make progress while we are waiting for input. */
1699 errcode
= pthread_mutex_unlock(&tog_mutex
);
1701 return got_error_set_errno(errcode
, "pthread_mutex_unlock");
1703 if (using_mock_io
) {
1704 err
= tog_read_script_key(tog_io
.f
, &ch
, done
);
1707 } else if (view
->count
&& --view
->count
) {
1709 nodelay(view
->window
, TRUE
);
1710 ch
= wgetch(view
->window
);
1711 /* let C-g or backspace abort unfinished count */
1712 if (ch
== CTRL('g') || ch
== KEY_BACKSPACE
)
1717 ch
= wgetch(view
->window
);
1718 if (ch
>= '1' && ch
<= '9')
1719 view
->ch
= ch
= get_compound_key(view
, ch
);
1721 if (view
->hiline
&& ch
!= ERR
&& ch
!= 0)
1722 view
->hiline
= 0; /* key pressed, clear line highlight */
1723 nodelay(view
->window
, TRUE
);
1724 errcode
= pthread_mutex_lock(&tog_mutex
);
1726 return got_error_set_errno(errcode
, "pthread_mutex_lock");
1728 if (tog_sigwinch_received
|| tog_sigcont_received
) {
1730 tog_sigwinch_received
= 0;
1731 tog_sigcont_received
= 0;
1732 TAILQ_FOREACH(v
, views
, entry
) {
1733 err
= view_resize(v
);
1736 err
= v
->input(new, v
, KEY_RESIZE
);
1740 err
= view_resize(v
->child
);
1743 err
= v
->child
->input(new, v
->child
,
1747 if (v
->child
->resized_x
|| v
->child
->resized_y
) {
1748 err
= view_resize_split(v
, 0);
1760 if (view
->type
== TOG_VIEW_HELP
)
1761 err
= view
->reset(view
);
1763 err
= view_request_new(new, view
, TOG_VIEW_HELP
);
1769 view
->child
->focussed
= 1;
1770 view
->focus_child
= 1;
1771 } else if (view
->parent
) {
1773 view
->parent
->focussed
= 1;
1774 view
->parent
->focus_child
= 0;
1775 if (!view_is_splitscreen(view
)) {
1776 if (view
->parent
->resize
) {
1777 err
= view
->parent
->resize(view
->parent
,
1782 offset_selection_up(view
->parent
);
1783 err
= view_fullscreen(view
->parent
);
1790 if (view
->parent
&& view
->mode
== TOG_VIEW_SPLIT_HRZN
) {
1791 if (view
->parent
->resize
) {
1792 /* might need more commits to fill fullscreen */
1793 err
= view
->parent
->resize(view
->parent
, 0);
1797 offset_selection_up(view
->parent
);
1799 err
= view
->input(new, view
, ch
);
1807 if (view_is_parent_view(view
)) {
1808 if (view
->child
== NULL
)
1810 if (view_is_splitscreen(view
->child
)) {
1812 view
->child
->focussed
= 1;
1813 err
= view_fullscreen(view
->child
);
1815 err
= view_splitscreen(view
->child
);
1817 err
= view_resize_split(view
, 0);
1821 err
= view
->child
->input(new, view
->child
,
1824 if (view_is_splitscreen(view
)) {
1825 view
->parent
->focussed
= 0;
1827 err
= view_fullscreen(view
);
1829 err
= view_splitscreen(view
);
1830 if (!err
&& view
->mode
!= TOG_VIEW_SPLIT_HRZN
)
1831 err
= view_resize(view
->parent
);
1833 err
= view_resize_split(view
, 0);
1837 err
= view
->input(new, view
, KEY_RESIZE
);
1842 err
= view
->resize(view
, 0);
1847 err
= offset_selection_down(view
->parent
);
1849 err
= offset_selection_down(view
);
1853 err
= switch_split(view
);
1856 err
= view_resize_split(view
, -1);
1859 err
= view_resize_split(view
, 1);
1865 if (view
->search_start
)
1866 view_search_start(view
, fast_refresh
);
1868 err
= view
->input(new, view
, ch
);
1872 if (view
->search_started
&& view
->search_next
) {
1873 view
->searching
= (ch
== 'n' ?
1874 TOG_SEARCH_FORWARD
: TOG_SEARCH_BACKWARD
);
1875 view
->search_next_done
= 0;
1876 view
->search_next(view
);
1878 err
= view
->input(new, view
, ch
);
1881 if (tog_diff_algo
== GOT_DIFF_ALGORITHM_MYERS
) {
1882 tog_diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
1883 view
->action
= "Patience diff algorithm";
1885 tog_diff_algo
= GOT_DIFF_ALGORITHM_MYERS
;
1886 view
->action
= "Myers diff algorithm";
1888 TAILQ_FOREACH(v
, views
, entry
) {
1894 if (v
->child
&& v
->child
->reset
) {
1895 err
= v
->child
->reset(v
->child
);
1901 case TOG_KEY_SCRDUMP
:
1902 err
= screendump(view
);
1905 err
= view
->input(new, view
, ch
);
1913 view_needs_focus_indication(struct tog_view
*view
)
1915 if (view_is_parent_view(view
)) {
1916 if (view
->child
== NULL
|| view
->child
->focussed
)
1918 if (!view_is_splitscreen(view
->child
))
1920 } else if (!view_is_splitscreen(view
))
1923 return view
->focussed
;
1926 static const struct got_error
*
1929 const struct got_error
*err
= NULL
;
1931 if (tog_io
.cin
&& fclose(tog_io
.cin
) == EOF
)
1932 err
= got_ferror(tog_io
.cin
, GOT_ERR_IO
);
1933 if (tog_io
.cout
&& fclose(tog_io
.cout
) == EOF
&& err
== NULL
)
1934 err
= got_ferror(tog_io
.cout
, GOT_ERR_IO
);
1935 if (tog_io
.f
&& fclose(tog_io
.f
) == EOF
&& err
== NULL
)
1936 err
= got_ferror(tog_io
.f
, GOT_ERR_IO
);
1941 static const struct got_error
*
1942 view_loop(struct tog_view
*view
)
1944 const struct got_error
*err
= NULL
;
1945 struct tog_view_list_head views
;
1946 struct tog_view
*new_view
;
1948 int fast_refresh
= 10;
1949 int done
= 0, errcode
;
1951 mode
= getenv("TOG_VIEW_SPLIT_MODE");
1952 if (!mode
|| !(*mode
== 'h' || *mode
== 'H'))
1953 view
->mode
= TOG_VIEW_SPLIT_VERT
;
1955 view
->mode
= TOG_VIEW_SPLIT_HRZN
;
1957 errcode
= pthread_mutex_lock(&tog_mutex
);
1959 return got_error_set_errno(errcode
, "pthread_mutex_lock");
1962 TAILQ_INSERT_HEAD(&views
, view
, entry
);
1965 err
= view
->show(view
);
1970 while (!TAILQ_EMPTY(&views
) && !done
&& !tog_thread_error
&&
1971 !tog_fatal_signal_received()) {
1972 /* Refresh fast during initialization, then become slower. */
1973 if (fast_refresh
&& --fast_refresh
== 0 && !using_mock_io
)
1974 halfdelay(10); /* switch to once per second */
1976 err
= view_input(&new_view
, &done
, view
, &views
, fast_refresh
);
1980 if (view
->dying
&& view
== TAILQ_FIRST(&views
) &&
1981 TAILQ_NEXT(view
, entry
) == NULL
)
1987 * When we quit, scroll the screen up a single line
1988 * so we don't lose any information.
1990 TAILQ_FOREACH(v
, &views
, entry
) {
1991 wmove(v
->window
, 0, 0);
1992 wdeleteln(v
->window
);
1993 wnoutrefresh(v
->window
);
1994 if (v
->child
&& !view_is_fullscreen(v
)) {
1995 wmove(v
->child
->window
, 0, 0);
1996 wdeleteln(v
->child
->window
);
1997 wnoutrefresh(v
->child
->window
);
2004 struct tog_view
*v
, *prev
= NULL
;
2006 if (view_is_parent_view(view
))
2007 prev
= TAILQ_PREV(view
, tog_view_list_head
,
2009 else if (view
->parent
)
2010 prev
= view
->parent
;
2013 view
->parent
->child
= NULL
;
2014 view
->parent
->focus_child
= 0;
2015 /* Restore fullscreen line height. */
2016 view
->parent
->nlines
= view
->parent
->lines
;
2017 err
= view_resize(view
->parent
);
2020 /* Make resized splits persist. */
2021 view_transfer_size(view
->parent
, view
);
2023 TAILQ_REMOVE(&views
, view
, entry
);
2025 err
= view_close(view
);
2030 TAILQ_FOREACH(v
, &views
, entry
) {
2034 if (view
== NULL
&& new_view
== NULL
) {
2035 /* No view has focus. Try to pick one. */
2038 else if (!TAILQ_EMPTY(&views
)) {
2039 view
= TAILQ_LAST(&views
,
2040 tog_view_list_head
);
2043 if (view
->focus_child
) {
2044 view
->child
->focussed
= 1;
2052 struct tog_view
*v
, *t
;
2053 /* Only allow one parent view per type. */
2054 TAILQ_FOREACH_SAFE(v
, &views
, entry
, t
) {
2055 if (v
->type
!= new_view
->type
)
2057 TAILQ_REMOVE(&views
, v
, entry
);
2058 err
= view_close(v
);
2063 TAILQ_INSERT_TAIL(&views
, new_view
, entry
);
2066 if (view
&& !done
) {
2067 if (view_is_parent_view(view
)) {
2068 if (view
->child
&& view
->child
->focussed
)
2071 if (view
->parent
&& view
->parent
->focussed
)
2072 view
= view
->parent
;
2074 show_panel(view
->panel
);
2075 if (view
->child
&& view_is_splitscreen(view
->child
))
2076 show_panel(view
->child
->panel
);
2077 if (view
->parent
&& view_is_splitscreen(view
)) {
2078 err
= view
->parent
->show(view
->parent
);
2082 err
= view
->show(view
);
2086 err
= view
->child
->show(view
->child
);
2095 while (!TAILQ_EMPTY(&views
)) {
2096 const struct got_error
*close_err
;
2097 view
= TAILQ_FIRST(&views
);
2098 TAILQ_REMOVE(&views
, view
, entry
);
2099 close_err
= view_close(view
);
2100 if (close_err
&& err
== NULL
)
2104 errcode
= pthread_mutex_unlock(&tog_mutex
);
2105 if (errcode
&& err
== NULL
)
2106 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
2116 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2121 /* Create newly allocated wide-character string equivalent to a byte string. */
2122 static const struct got_error
*
2123 mbs2ws(wchar_t **ws
, size_t *wlen
, const char *s
)
2126 const struct got_error
*err
= NULL
;
2129 *wlen
= mbstowcs(NULL
, s
, 0);
2130 if (*wlen
== (size_t)-1) {
2132 if (errno
!= EILSEQ
)
2133 return got_error_from_errno("mbstowcs");
2135 /* byte string invalid in current encoding; try to "fix" it */
2136 err
= got_mbsavis(&vis
, &vislen
, s
);
2139 *wlen
= mbstowcs(NULL
, vis
, 0);
2140 if (*wlen
== (size_t)-1) {
2141 err
= got_error_from_errno("mbstowcs"); /* give up */
2146 *ws
= calloc(*wlen
+ 1, sizeof(**ws
));
2148 err
= got_error_from_errno("calloc");
2152 if (mbstowcs(*ws
, vis
? vis
: s
, *wlen
) != *wlen
)
2153 err
= got_error_from_errno("mbstowcs");
2164 static const struct got_error
*
2165 expand_tab(char **ptr
, const char *src
)
2168 size_t len
, n
, idx
= 0, sz
= 0;
2171 n
= len
= strlen(src
);
2172 dst
= malloc(n
+ 1);
2174 return got_error_from_errno("malloc");
2176 while (idx
< len
&& src
[idx
]) {
2177 const char c
= src
[idx
];
2180 size_t nb
= TABSIZE
- sz
% TABSIZE
;
2183 p
= realloc(dst
, n
+ nb
);
2186 return got_error_from_errno("realloc");
2191 memset(dst
+ sz
, ' ', nb
);
2194 dst
[sz
++] = src
[idx
];
2204 * Advance at most n columns from wline starting at offset off.
2205 * Return the index to the first character after the span operation.
2206 * Return the combined column width of all spanned wide character in
2210 span_wline(int *rcol
, int off
, wchar_t *wline
, int n
, int col_tab_align
)
2212 int width
, i
, cols
= 0;
2219 for (i
= off
; wline
[i
] != L
'\0'; ++i
) {
2220 if (wline
[i
] == L
'\t')
2221 width
= TABSIZE
- ((cols
+ col_tab_align
) % TABSIZE
);
2223 width
= wcwidth(wline
[i
]);
2230 if (cols
+ width
> n
)
2240 * Format a line for display, ensuring that it won't overflow a width limit.
2241 * With scrolling, the width returned refers to the scrolled version of the
2242 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2244 static const struct got_error
*
2245 format_line(wchar_t **wlinep
, int *widthp
, int *scrollxp
,
2246 const char *line
, int nscroll
, int wlimit
, int col_tab_align
, int expand
)
2248 const struct got_error
*err
= NULL
;
2250 wchar_t *wline
= NULL
;
2259 err
= expand_tab(&exstr
, line
);
2264 err
= mbs2ws(&wline
, &wlen
, expand
? exstr
: line
);
2269 scrollx
= span_wline(&cols
, 0, wline
, nscroll
, col_tab_align
);
2271 if (wlen
> 0 && wline
[wlen
- 1] == L
'\n') {
2272 wline
[wlen
- 1] = L
'\0';
2275 if (wlen
> 0 && wline
[wlen
- 1] == L
'\r') {
2276 wline
[wlen
- 1] = L
'\0';
2280 i
= span_wline(&cols
, scrollx
, wline
, wlimit
, col_tab_align
);
2286 *scrollxp
= scrollx
;
2294 static const struct got_error
*
2295 build_refs_str(char **refs_str
, struct got_reflist_head
*refs
,
2296 struct got_object_id
*id
, struct got_repository
*repo
)
2298 static const struct got_error
*err
= NULL
;
2299 struct got_reflist_entry
*re
;
2305 TAILQ_FOREACH(re
, refs
, entry
) {
2306 struct got_tag_object
*tag
= NULL
;
2307 struct got_object_id
*ref_id
;
2310 name
= got_ref_get_name(re
->ref
);
2311 if (strcmp(name
, GOT_REF_HEAD
) == 0)
2313 if (strncmp(name
, "refs/", 5) == 0)
2315 if (strncmp(name
, "got/", 4) == 0 &&
2316 strncmp(name
, "got/backup/", 11) != 0)
2318 if (strncmp(name
, "heads/", 6) == 0)
2320 if (strncmp(name
, "remotes/", 8) == 0) {
2322 s
= strstr(name
, "/" GOT_REF_HEAD
);
2323 if (s
!= NULL
&& s
[strlen(s
)] == '\0')
2326 err
= got_ref_resolve(&ref_id
, repo
, re
->ref
);
2329 if (strncmp(name
, "tags/", 5) == 0) {
2330 err
= got_object_open_as_tag(&tag
, repo
, ref_id
);
2332 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
2336 /* Ref points at something other than a tag. */
2341 cmp
= got_object_id_cmp(tag
?
2342 got_object_tag_get_object_id(tag
) : ref_id
, id
);
2345 got_object_tag_close(tag
);
2349 if (asprintf(refs_str
, "%s%s%s", s
? s
: "",
2350 s
? ", " : "", name
) == -1) {
2351 err
= got_error_from_errno("asprintf");
2362 static const struct got_error
*
2363 format_author(wchar_t **wauthor
, int *author_width
, char *author
, int limit
,
2368 smallerthan
= strchr(author
, '<');
2369 if (smallerthan
&& smallerthan
[1] != '\0')
2370 author
= smallerthan
+ 1;
2371 author
[strcspn(author
, "@>")] = '\0';
2372 return format_line(wauthor
, author_width
, NULL
, author
, 0, limit
,
2376 static const struct got_error
*
2377 draw_commit(struct tog_view
*view
, struct got_commit_object
*commit
,
2378 struct got_object_id
*id
, const size_t date_display_cols
,
2379 int author_display_cols
)
2381 struct tog_log_view_state
*s
= &view
->state
.log
;
2382 const struct got_error
*err
= NULL
;
2383 char datebuf
[12]; /* YYYY-MM-DD + SPACE + NUL */
2384 char *logmsg0
= NULL
, *logmsg
= NULL
;
2385 char *author
= NULL
;
2386 wchar_t *wlogmsg
= NULL
, *wauthor
= NULL
;
2387 int author_width
, logmsg_width
;
2388 char *newline
, *line
= NULL
;
2389 int col
, limit
, scrollx
;
2390 const int avail
= view
->ncols
;
2392 time_t committer_time
;
2393 struct tog_color
*tc
;
2395 committer_time
= got_object_commit_get_committer_time(commit
);
2396 if (gmtime_r(&committer_time
, &tm
) == NULL
)
2397 return got_error_from_errno("gmtime_r");
2398 if (strftime(datebuf
, sizeof(datebuf
), "%G-%m-%d ", &tm
) == 0)
2399 return got_error(GOT_ERR_NO_SPACE
);
2401 if (avail
<= date_display_cols
)
2402 limit
= MIN(sizeof(datebuf
) - 1, avail
);
2404 limit
= MIN(date_display_cols
, sizeof(datebuf
) - 1);
2405 tc
= get_color(&s
->colors
, TOG_COLOR_DATE
);
2407 wattr_on(view
->window
,
2408 COLOR_PAIR(tc
->colorpair
), NULL
);
2409 waddnstr(view
->window
, datebuf
, limit
);
2411 wattr_off(view
->window
,
2412 COLOR_PAIR(tc
->colorpair
), NULL
);
2419 err
= got_object_id_str(&id_str
, id
);
2422 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
2424 wattr_on(view
->window
,
2425 COLOR_PAIR(tc
->colorpair
), NULL
);
2426 wprintw(view
->window
, "%.8s ", id_str
);
2428 wattr_off(view
->window
,
2429 COLOR_PAIR(tc
->colorpair
), NULL
);
2436 if (s
->use_committer
)
2437 author
= strdup(got_object_commit_get_committer(commit
));
2439 author
= strdup(got_object_commit_get_author(commit
));
2440 if (author
== NULL
) {
2441 err
= got_error_from_errno("strdup");
2444 err
= format_author(&wauthor
, &author_width
, author
, avail
- col
, col
);
2447 tc
= get_color(&s
->colors
, TOG_COLOR_AUTHOR
);
2449 wattr_on(view
->window
,
2450 COLOR_PAIR(tc
->colorpair
), NULL
);
2451 waddwstr(view
->window
, wauthor
);
2452 col
+= author_width
;
2453 while (col
< avail
&& author_width
< author_display_cols
+ 2) {
2454 waddch(view
->window
, ' ');
2459 wattr_off(view
->window
,
2460 COLOR_PAIR(tc
->colorpair
), NULL
);
2464 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
2468 while (*logmsg
== '\n')
2470 newline
= strchr(logmsg
, '\n');
2473 limit
= avail
- col
;
2474 if (view
->child
&& !view_is_hsplit_top(view
) && limit
> 0)
2475 limit
--; /* for the border */
2476 err
= format_line(&wlogmsg
, &logmsg_width
, &scrollx
, logmsg
, view
->x
,
2480 waddwstr(view
->window
, &wlogmsg
[scrollx
]);
2481 col
+= MAX(logmsg_width
, 0);
2482 while (col
< avail
) {
2483 waddch(view
->window
, ' ');
2495 static struct commit_queue_entry
*
2496 alloc_commit_queue_entry(struct got_commit_object
*commit
,
2497 struct got_object_id
*id
)
2499 struct commit_queue_entry
*entry
;
2500 struct got_object_id
*dup
;
2502 entry
= calloc(1, sizeof(*entry
));
2506 dup
= got_object_id_dup(id
);
2513 entry
->commit
= commit
;
2518 pop_commit(struct commit_queue
*commits
)
2520 struct commit_queue_entry
*entry
;
2522 entry
= TAILQ_FIRST(&commits
->head
);
2523 TAILQ_REMOVE(&commits
->head
, entry
, entry
);
2524 got_object_commit_close(entry
->commit
);
2525 commits
->ncommits
--;
2531 free_commits(struct commit_queue
*commits
)
2533 while (!TAILQ_EMPTY(&commits
->head
))
2534 pop_commit(commits
);
2537 static const struct got_error
*
2538 match_commit(int *have_match
, struct got_object_id
*id
,
2539 struct got_commit_object
*commit
, regex_t
*regex
)
2541 const struct got_error
*err
= NULL
;
2542 regmatch_t regmatch
;
2543 char *id_str
= NULL
, *logmsg
= NULL
;
2547 err
= got_object_id_str(&id_str
, id
);
2551 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
2555 if (regexec(regex
, got_object_commit_get_author(commit
), 1,
2556 ®match
, 0) == 0 ||
2557 regexec(regex
, got_object_commit_get_committer(commit
), 1,
2558 ®match
, 0) == 0 ||
2559 regexec(regex
, id_str
, 1, ®match
, 0) == 0 ||
2560 regexec(regex
, logmsg
, 1, ®match
, 0) == 0)
2568 static const struct got_error
*
2569 queue_commits(struct tog_log_thread_args
*a
)
2571 const struct got_error
*err
= NULL
;
2574 * We keep all commits open throughout the lifetime of the log
2575 * view in order to avoid having to re-fetch commits from disk
2576 * while updating the display.
2579 struct got_object_id id
;
2580 struct got_commit_object
*commit
;
2581 struct commit_queue_entry
*entry
;
2582 int limit_match
= 0;
2585 err
= got_commit_graph_iter_next(&id
, a
->graph
, a
->repo
,
2590 err
= got_object_open_as_commit(&commit
, a
->repo
, &id
);
2593 entry
= alloc_commit_queue_entry(commit
, &id
);
2594 if (entry
== NULL
) {
2595 err
= got_error_from_errno("alloc_commit_queue_entry");
2599 errcode
= pthread_mutex_lock(&tog_mutex
);
2601 err
= got_error_set_errno(errcode
,
2602 "pthread_mutex_lock");
2606 entry
->idx
= a
->real_commits
->ncommits
;
2607 TAILQ_INSERT_TAIL(&a
->real_commits
->head
, entry
, entry
);
2608 a
->real_commits
->ncommits
++;
2611 err
= match_commit(&limit_match
, &id
, commit
,
2617 struct commit_queue_entry
*matched
;
2619 matched
= alloc_commit_queue_entry(
2620 entry
->commit
, entry
->id
);
2621 if (matched
== NULL
) {
2622 err
= got_error_from_errno(
2623 "alloc_commit_queue_entry");
2626 matched
->commit
= entry
->commit
;
2627 got_object_commit_retain(entry
->commit
);
2629 matched
->idx
= a
->limit_commits
->ncommits
;
2630 TAILQ_INSERT_TAIL(&a
->limit_commits
->head
,
2632 a
->limit_commits
->ncommits
++;
2636 * This is how we signal log_thread() that we
2637 * have found a match, and that it should be
2638 * counted as a new entry for the view.
2640 a
->limit_match
= limit_match
;
2643 if (*a
->searching
== TOG_SEARCH_FORWARD
&&
2644 !*a
->search_next_done
) {
2646 err
= match_commit(&have_match
, &id
, commit
, a
->regex
);
2651 if (limit_match
&& have_match
)
2652 *a
->search_next_done
=
2653 TOG_SEARCH_HAVE_MORE
;
2654 } else if (have_match
)
2655 *a
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
2658 errcode
= pthread_mutex_unlock(&tog_mutex
);
2659 if (errcode
&& err
== NULL
)
2660 err
= got_error_set_errno(errcode
,
2661 "pthread_mutex_unlock");
2664 } while (*a
->searching
== TOG_SEARCH_FORWARD
&& !*a
->search_next_done
);
2670 select_commit(struct tog_log_view_state
*s
)
2672 struct commit_queue_entry
*entry
;
2675 entry
= s
->first_displayed_entry
;
2677 if (ncommits
== s
->selected
) {
2678 s
->selected_entry
= entry
;
2681 entry
= TAILQ_NEXT(entry
, entry
);
2686 static const struct got_error
*
2687 draw_commits(struct tog_view
*view
)
2689 const struct got_error
*err
= NULL
;
2690 struct tog_log_view_state
*s
= &view
->state
.log
;
2691 struct commit_queue_entry
*entry
= s
->selected_entry
;
2692 int limit
= view
->nlines
;
2694 int ncommits
, author_cols
= 4;
2695 char *id_str
= NULL
, *header
= NULL
, *ncommits_str
= NULL
;
2696 char *refs_str
= NULL
;
2698 struct tog_color
*tc
;
2699 static const size_t date_display_cols
= 12;
2701 if (view_is_hsplit_top(view
))
2702 --limit
; /* account for border */
2704 if (s
->selected_entry
&&
2705 !(view
->searching
&& view
->search_next_done
== 0)) {
2706 struct got_reflist_head
*refs
;
2707 err
= got_object_id_str(&id_str
, s
->selected_entry
->id
);
2710 refs
= got_reflist_object_id_map_lookup(tog_refs_idmap
,
2711 s
->selected_entry
->id
);
2713 err
= build_refs_str(&refs_str
, refs
,
2714 s
->selected_entry
->id
, s
->repo
);
2720 if (s
->thread_args
.commits_needed
== 0 && !using_mock_io
)
2721 halfdelay(10); /* disable fast refresh */
2723 if (s
->thread_args
.commits_needed
> 0 || s
->thread_args
.load_all
) {
2724 if (asprintf(&ncommits_str
, " [%d/%d] %s",
2725 entry
? entry
->idx
+ 1 : 0, s
->commits
->ncommits
,
2726 (view
->searching
&& !view
->search_next_done
) ?
2727 "searching..." : "loading...") == -1) {
2728 err
= got_error_from_errno("asprintf");
2732 const char *search_str
= NULL
;
2733 const char *limit_str
= NULL
;
2735 if (view
->searching
) {
2736 if (view
->search_next_done
== TOG_SEARCH_NO_MORE
)
2737 search_str
= "no more matches";
2738 else if (view
->search_next_done
== TOG_SEARCH_HAVE_NONE
)
2739 search_str
= "no matches found";
2740 else if (!view
->search_next_done
)
2741 search_str
= "searching...";
2744 if (s
->limit_view
&& s
->commits
->ncommits
== 0)
2745 limit_str
= "no matches found";
2747 if (asprintf(&ncommits_str
, " [%d/%d] %s %s",
2748 entry
? entry
->idx
+ 1 : 0, s
->commits
->ncommits
,
2749 search_str
? search_str
: (refs_str
? refs_str
: ""),
2750 limit_str
? limit_str
: "") == -1) {
2751 err
= got_error_from_errno("asprintf");
2756 if (s
->in_repo_path
&& strcmp(s
->in_repo_path
, "/") != 0) {
2757 if (asprintf(&header
, "commit %s %s%s", id_str
? id_str
:
2758 "........................................",
2759 s
->in_repo_path
, ncommits_str
) == -1) {
2760 err
= got_error_from_errno("asprintf");
2764 } else if (asprintf(&header
, "commit %s%s",
2765 id_str
? id_str
: "........................................",
2766 ncommits_str
) == -1) {
2767 err
= got_error_from_errno("asprintf");
2771 err
= format_line(&wline
, &width
, NULL
, header
, 0, view
->ncols
, 0, 0);
2775 werase(view
->window
);
2777 if (view_needs_focus_indication(view
))
2778 wstandout(view
->window
);
2779 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
2781 wattr_on(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
2782 waddwstr(view
->window
, wline
);
2783 while (width
< view
->ncols
) {
2784 waddch(view
->window
, ' ');
2788 wattr_off(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
2789 if (view_needs_focus_indication(view
))
2790 wstandend(view
->window
);
2795 /* Grow author column size if necessary, and set view->maxx. */
2796 entry
= s
->first_displayed_entry
;
2800 struct got_commit_object
*c
= entry
->commit
;
2801 char *author
, *eol
, *msg
, *msg0
;
2802 wchar_t *wauthor
, *wmsg
;
2804 if (ncommits
>= limit
- 1)
2806 if (s
->use_committer
)
2807 author
= strdup(got_object_commit_get_committer(c
));
2809 author
= strdup(got_object_commit_get_author(c
));
2810 if (author
== NULL
) {
2811 err
= got_error_from_errno("strdup");
2814 err
= format_author(&wauthor
, &width
, author
, COLS
,
2816 if (author_cols
< width
)
2817 author_cols
= width
;
2822 err
= got_object_commit_get_logmsg(&msg0
, c
);
2826 while (*msg
== '\n')
2828 if ((eol
= strchr(msg
, '\n')))
2830 err
= format_line(&wmsg
, &width
, NULL
, msg
, 0, INT_MAX
,
2831 date_display_cols
+ author_cols
, 0);
2834 view
->maxx
= MAX(view
->maxx
, width
);
2838 entry
= TAILQ_NEXT(entry
, entry
);
2841 entry
= s
->first_displayed_entry
;
2842 s
->last_displayed_entry
= s
->first_displayed_entry
;
2845 if (ncommits
>= limit
- 1)
2847 if (ncommits
== s
->selected
)
2848 wstandout(view
->window
);
2849 err
= draw_commit(view
, entry
->commit
, entry
->id
,
2850 date_display_cols
, author_cols
);
2851 if (ncommits
== s
->selected
)
2852 wstandend(view
->window
);
2856 s
->last_displayed_entry
= entry
;
2857 entry
= TAILQ_NEXT(entry
, entry
);
2870 log_scroll_up(struct tog_log_view_state
*s
, int maxscroll
)
2872 struct commit_queue_entry
*entry
;
2875 entry
= TAILQ_FIRST(&s
->commits
->head
);
2876 if (s
->first_displayed_entry
== entry
)
2879 entry
= s
->first_displayed_entry
;
2880 while (entry
&& nscrolled
< maxscroll
) {
2881 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
2883 s
->first_displayed_entry
= entry
;
2889 static const struct got_error
*
2890 trigger_log_thread(struct tog_view
*view
, int wait
)
2892 struct tog_log_thread_args
*ta
= &view
->state
.log
.thread_args
;
2896 halfdelay(1); /* fast refresh while loading commits */
2898 while (!ta
->log_complete
&& !tog_thread_error
&&
2899 (ta
->commits_needed
> 0 || ta
->load_all
)) {
2900 /* Wake the log thread. */
2901 errcode
= pthread_cond_signal(&ta
->need_commits
);
2903 return got_error_set_errno(errcode
,
2904 "pthread_cond_signal");
2907 * The mutex will be released while the view loop waits
2908 * in wgetch(), at which time the log thread will run.
2913 /* Display progress update in log view. */
2914 show_log_view(view
);
2918 /* Wait right here while next commit is being loaded. */
2919 errcode
= pthread_cond_wait(&ta
->commit_loaded
, &tog_mutex
);
2921 return got_error_set_errno(errcode
,
2922 "pthread_cond_wait");
2924 /* Display progress update in log view. */
2925 show_log_view(view
);
2933 static const struct got_error
*
2934 request_log_commits(struct tog_view
*view
)
2936 struct tog_log_view_state
*state
= &view
->state
.log
;
2937 const struct got_error
*err
= NULL
;
2939 if (state
->thread_args
.log_complete
)
2942 state
->thread_args
.commits_needed
+= view
->nscrolled
;
2943 err
= trigger_log_thread(view
, 1);
2944 view
->nscrolled
= 0;
2949 static const struct got_error
*
2950 log_scroll_down(struct tog_view
*view
, int maxscroll
)
2952 struct tog_log_view_state
*s
= &view
->state
.log
;
2953 const struct got_error
*err
= NULL
;
2954 struct commit_queue_entry
*pentry
;
2955 int nscrolled
= 0, ncommits_needed
;
2957 if (s
->last_displayed_entry
== NULL
)
2960 ncommits_needed
= s
->last_displayed_entry
->idx
+ 1 + maxscroll
;
2961 if (s
->commits
->ncommits
< ncommits_needed
&&
2962 !s
->thread_args
.log_complete
) {
2964 * Ask the log thread for required amount of commits.
2966 s
->thread_args
.commits_needed
+=
2967 ncommits_needed
- s
->commits
->ncommits
;
2968 err
= trigger_log_thread(view
, 1);
2974 pentry
= TAILQ_NEXT(s
->last_displayed_entry
, entry
);
2975 if (pentry
== NULL
&& view
->mode
!= TOG_VIEW_SPLIT_HRZN
)
2978 s
->last_displayed_entry
= pentry
?
2979 pentry
: s
->last_displayed_entry
;
2981 pentry
= TAILQ_NEXT(s
->first_displayed_entry
, entry
);
2984 s
->first_displayed_entry
= pentry
;
2985 } while (++nscrolled
< maxscroll
);
2987 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
&& !s
->thread_args
.log_complete
)
2988 view
->nscrolled
+= nscrolled
;
2990 view
->nscrolled
= 0;
2995 static const struct got_error
*
2996 open_diff_view_for_commit(struct tog_view
**new_view
, int begin_y
, int begin_x
,
2997 struct got_commit_object
*commit
, struct got_object_id
*commit_id
,
2998 struct tog_view
*log_view
, struct got_repository
*repo
)
3000 const struct got_error
*err
;
3001 struct got_object_qid
*parent_id
;
3002 struct tog_view
*diff_view
;
3004 diff_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_DIFF
);
3005 if (diff_view
== NULL
)
3006 return got_error_from_errno("view_open");
3008 parent_id
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3009 err
= open_diff_view(diff_view
, parent_id
? &parent_id
->id
: NULL
,
3010 commit_id
, NULL
, NULL
, 3, 0, 0, log_view
, repo
);
3012 *new_view
= diff_view
;
3016 static const struct got_error
*
3017 tree_view_visit_subtree(struct tog_tree_view_state
*s
,
3018 struct got_tree_object
*subtree
)
3020 struct tog_parent_tree
*parent
;
3022 parent
= calloc(1, sizeof(*parent
));
3024 return got_error_from_errno("calloc");
3026 parent
->tree
= s
->tree
;
3027 parent
->first_displayed_entry
= s
->first_displayed_entry
;
3028 parent
->selected_entry
= s
->selected_entry
;
3029 parent
->selected
= s
->selected
;
3030 TAILQ_INSERT_HEAD(&s
->parents
, parent
, entry
);
3033 s
->first_displayed_entry
= NULL
;
3037 static const struct got_error
*
3038 tree_view_walk_path(struct tog_tree_view_state
*s
,
3039 struct got_commit_object
*commit
, const char *path
)
3041 const struct got_error
*err
= NULL
;
3042 struct got_tree_object
*tree
= NULL
;
3044 char *slash
, *subpath
= NULL
;
3046 /* Walk the path and open corresponding tree objects. */
3049 struct got_tree_entry
*te
;
3050 struct got_object_id
*tree_id
;
3056 /* Ensure the correct subtree entry is selected. */
3057 slash
= strchr(p
, '/');
3059 te_name
= strdup(p
);
3061 te_name
= strndup(p
, slash
- p
);
3062 if (te_name
== NULL
) {
3063 err
= got_error_from_errno("strndup");
3066 te
= got_object_tree_find_entry(s
->tree
, te_name
);
3068 err
= got_error_path(te_name
, GOT_ERR_NO_TREE_ENTRY
);
3073 s
->first_displayed_entry
= s
->selected_entry
= te
;
3075 if (!S_ISDIR(got_tree_entry_get_mode(s
->selected_entry
)))
3076 break; /* jump to this file's entry */
3078 slash
= strchr(p
, '/');
3080 subpath
= strndup(path
, slash
- path
);
3082 subpath
= strdup(path
);
3083 if (subpath
== NULL
) {
3084 err
= got_error_from_errno("strdup");
3088 err
= got_object_id_by_path(&tree_id
, s
->repo
, commit
,
3093 err
= got_object_open_as_tree(&tree
, s
->repo
, tree_id
);
3098 err
= tree_view_visit_subtree(s
, tree
);
3100 got_object_tree_close(tree
);
3114 static const struct got_error
*
3115 browse_commit_tree(struct tog_view
**new_view
, int begin_y
, int begin_x
,
3116 struct commit_queue_entry
*entry
, const char *path
,
3117 const char *head_ref_name
, struct got_repository
*repo
)
3119 const struct got_error
*err
= NULL
;
3120 struct tog_tree_view_state
*s
;
3121 struct tog_view
*tree_view
;
3123 tree_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_TREE
);
3124 if (tree_view
== NULL
)
3125 return got_error_from_errno("view_open");
3127 err
= open_tree_view(tree_view
, entry
->id
, head_ref_name
, repo
);
3130 s
= &tree_view
->state
.tree
;
3132 *new_view
= tree_view
;
3134 if (got_path_is_root_dir(path
))
3137 return tree_view_walk_path(s
, entry
->commit
, path
);
3140 static const struct got_error
*
3141 block_signals_used_by_main_thread(void)
3146 if (sigemptyset(&sigset
) == -1)
3147 return got_error_from_errno("sigemptyset");
3149 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3150 if (sigaddset(&sigset
, SIGWINCH
) == -1)
3151 return got_error_from_errno("sigaddset");
3152 if (sigaddset(&sigset
, SIGCONT
) == -1)
3153 return got_error_from_errno("sigaddset");
3154 if (sigaddset(&sigset
, SIGINT
) == -1)
3155 return got_error_from_errno("sigaddset");
3156 if (sigaddset(&sigset
, SIGTERM
) == -1)
3157 return got_error_from_errno("sigaddset");
3159 /* ncurses handles SIGTSTP */
3160 if (sigaddset(&sigset
, SIGTSTP
) == -1)
3161 return got_error_from_errno("sigaddset");
3163 errcode
= pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
3165 return got_error_set_errno(errcode
, "pthread_sigmask");
3171 log_thread(void *arg
)
3173 const struct got_error
*err
= NULL
;
3175 struct tog_log_thread_args
*a
= arg
;
3179 * Sync startup with main thread such that we begin our
3180 * work once view_input() has released the mutex.
3182 errcode
= pthread_mutex_lock(&tog_mutex
);
3184 err
= got_error_set_errno(errcode
, "pthread_mutex_lock");
3188 err
= block_signals_used_by_main_thread();
3190 pthread_mutex_unlock(&tog_mutex
);
3194 while (!done
&& !err
&& !tog_fatal_signal_received()) {
3195 errcode
= pthread_mutex_unlock(&tog_mutex
);
3197 err
= got_error_set_errno(errcode
,
3198 "pthread_mutex_unlock");
3201 err
= queue_commits(a
);
3203 if (err
->code
!= GOT_ERR_ITER_COMPLETED
)
3207 } else if (a
->commits_needed
> 0 && !a
->load_all
) {
3210 a
->commits_needed
--;
3212 a
->commits_needed
--;
3215 errcode
= pthread_mutex_lock(&tog_mutex
);
3217 err
= got_error_set_errno(errcode
,
3218 "pthread_mutex_lock");
3220 } else if (*a
->quit
)
3222 else if (*a
->limiting
&& *a
->first_displayed_entry
== NULL
) {
3223 *a
->first_displayed_entry
=
3224 TAILQ_FIRST(&a
->limit_commits
->head
);
3225 *a
->selected_entry
= *a
->first_displayed_entry
;
3226 } else if (*a
->first_displayed_entry
== NULL
) {
3227 *a
->first_displayed_entry
=
3228 TAILQ_FIRST(&a
->real_commits
->head
);
3229 *a
->selected_entry
= *a
->first_displayed_entry
;
3232 errcode
= pthread_cond_signal(&a
->commit_loaded
);
3234 err
= got_error_set_errno(errcode
,
3235 "pthread_cond_signal");
3236 pthread_mutex_unlock(&tog_mutex
);
3241 a
->commits_needed
= 0;
3243 if (a
->commits_needed
== 0 && !a
->load_all
) {
3244 errcode
= pthread_cond_wait(&a
->need_commits
,
3247 err
= got_error_set_errno(errcode
,
3248 "pthread_cond_wait");
3249 pthread_mutex_unlock(&tog_mutex
);
3257 a
->log_complete
= 1;
3258 errcode
= pthread_mutex_unlock(&tog_mutex
);
3260 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
3263 tog_thread_error
= 1;
3264 pthread_cond_signal(&a
->commit_loaded
);
3269 static const struct got_error
*
3270 stop_log_thread(struct tog_log_view_state
*s
)
3272 const struct got_error
*err
= NULL
, *thread_err
= NULL
;
3277 errcode
= pthread_cond_signal(&s
->thread_args
.need_commits
);
3279 return got_error_set_errno(errcode
,
3280 "pthread_cond_signal");
3281 errcode
= pthread_mutex_unlock(&tog_mutex
);
3283 return got_error_set_errno(errcode
,
3284 "pthread_mutex_unlock");
3285 errcode
= pthread_join(s
->thread
, (void **)&thread_err
);
3287 return got_error_set_errno(errcode
, "pthread_join");
3288 errcode
= pthread_mutex_lock(&tog_mutex
);
3290 return got_error_set_errno(errcode
,
3291 "pthread_mutex_lock");
3292 s
->thread
= 0; //NULL;
3295 if (s
->thread_args
.repo
) {
3296 err
= got_repo_close(s
->thread_args
.repo
);
3297 s
->thread_args
.repo
= NULL
;
3300 if (s
->thread_args
.pack_fds
) {
3301 const struct got_error
*pack_err
=
3302 got_repo_pack_fds_close(s
->thread_args
.pack_fds
);
3305 s
->thread_args
.pack_fds
= NULL
;
3308 if (s
->thread_args
.graph
) {
3309 got_commit_graph_close(s
->thread_args
.graph
);
3310 s
->thread_args
.graph
= NULL
;
3313 return err
? err
: thread_err
;
3316 static const struct got_error
*
3317 close_log_view(struct tog_view
*view
)
3319 const struct got_error
*err
= NULL
;
3320 struct tog_log_view_state
*s
= &view
->state
.log
;
3323 err
= stop_log_thread(s
);
3325 errcode
= pthread_cond_destroy(&s
->thread_args
.need_commits
);
3326 if (errcode
&& err
== NULL
)
3327 err
= got_error_set_errno(errcode
, "pthread_cond_destroy");
3329 errcode
= pthread_cond_destroy(&s
->thread_args
.commit_loaded
);
3330 if (errcode
&& err
== NULL
)
3331 err
= got_error_set_errno(errcode
, "pthread_cond_destroy");
3333 free_commits(&s
->limit_commits
);
3334 free_commits(&s
->real_commits
);
3335 free(s
->in_repo_path
);
3336 s
->in_repo_path
= NULL
;
3339 free(s
->head_ref_name
);
3340 s
->head_ref_name
= NULL
;
3345 * We use two queues to implement the limit feature: first consists of
3346 * commits matching the current limit_regex; second is the real queue
3347 * of all known commits (real_commits). When the user starts limiting,
3348 * we swap queues such that all movement and displaying functionality
3349 * works with very slight change.
3351 static const struct got_error
*
3352 limit_log_view(struct tog_view
*view
)
3354 struct tog_log_view_state
*s
= &view
->state
.log
;
3355 struct commit_queue_entry
*entry
;
3356 struct tog_view
*v
= view
;
3357 const struct got_error
*err
= NULL
;
3361 if (view_is_hsplit_top(view
))
3363 else if (view
->mode
== TOG_VIEW_SPLIT_VERT
&& view
->parent
)
3366 /* Get the pattern */
3367 wmove(v
->window
, v
->nlines
- 1, 0);
3368 wclrtoeol(v
->window
);
3369 mvwaddstr(v
->window
, v
->nlines
- 1, 0, "&/");
3370 nodelay(v
->window
, FALSE
);
3373 ret
= wgetnstr(v
->window
, pattern
, sizeof(pattern
));
3376 nodelay(v
->window
, TRUE
);
3380 if (*pattern
== '\0') {
3382 * Safety measure for the situation where the user
3383 * resets limit without previously limiting anything.
3389 * User could have pressed Ctrl+L, which refreshed the
3390 * commit queues, it means we can't save previously
3391 * (before limit took place) displayed entries,
3392 * because they would point to already free'ed memory,
3393 * so we are forced to always select first entry of
3396 s
->commits
= &s
->real_commits
;
3397 s
->first_displayed_entry
= TAILQ_FIRST(&s
->real_commits
.head
);
3398 s
->selected_entry
= s
->first_displayed_entry
;
3405 if (regcomp(&s
->limit_regex
, pattern
, REG_EXTENDED
| REG_NEWLINE
))
3410 /* Clear the screen while loading limit view */
3411 s
->first_displayed_entry
= NULL
;
3412 s
->last_displayed_entry
= NULL
;
3413 s
->selected_entry
= NULL
;
3414 s
->commits
= &s
->limit_commits
;
3416 /* Prepare limit queue for new search */
3417 free_commits(&s
->limit_commits
);
3418 s
->limit_commits
.ncommits
= 0;
3420 /* First process commits, which are in queue already */
3421 TAILQ_FOREACH(entry
, &s
->real_commits
.head
, entry
) {
3424 err
= match_commit(&have_match
, entry
->id
,
3425 entry
->commit
, &s
->limit_regex
);
3430 struct commit_queue_entry
*matched
;
3432 matched
= alloc_commit_queue_entry(entry
->commit
,
3434 if (matched
== NULL
) {
3435 err
= got_error_from_errno(
3436 "alloc_commit_queue_entry");
3439 matched
->commit
= entry
->commit
;
3440 got_object_commit_retain(entry
->commit
);
3442 matched
->idx
= s
->limit_commits
.ncommits
;
3443 TAILQ_INSERT_TAIL(&s
->limit_commits
.head
,
3445 s
->limit_commits
.ncommits
++;
3449 /* Second process all the commits, until we fill the screen */
3450 if (s
->limit_commits
.ncommits
< view
->nlines
- 1 &&
3451 !s
->thread_args
.log_complete
) {
3452 s
->thread_args
.commits_needed
+=
3453 view
->nlines
- s
->limit_commits
.ncommits
- 1;
3454 err
= trigger_log_thread(view
, 1);
3459 s
->first_displayed_entry
= TAILQ_FIRST(&s
->commits
->head
);
3460 s
->selected_entry
= TAILQ_FIRST(&s
->commits
->head
);
3466 static const struct got_error
*
3467 search_start_log_view(struct tog_view
*view
)
3469 struct tog_log_view_state
*s
= &view
->state
.log
;
3471 s
->matched_entry
= NULL
;
3472 s
->search_entry
= NULL
;
3476 static const struct got_error
*
3477 search_next_log_view(struct tog_view
*view
)
3479 const struct got_error
*err
= NULL
;
3480 struct tog_log_view_state
*s
= &view
->state
.log
;
3481 struct commit_queue_entry
*entry
;
3483 /* Display progress update in log view. */
3484 show_log_view(view
);
3488 if (s
->search_entry
) {
3490 errcode
= pthread_mutex_unlock(&tog_mutex
);
3492 return got_error_set_errno(errcode
,
3493 "pthread_mutex_unlock");
3494 ch
= wgetch(view
->window
);
3495 errcode
= pthread_mutex_lock(&tog_mutex
);
3497 return got_error_set_errno(errcode
,
3498 "pthread_mutex_lock");
3499 if (ch
== CTRL('g') || ch
== KEY_BACKSPACE
) {
3500 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
3503 if (view
->searching
== TOG_SEARCH_FORWARD
)
3504 entry
= TAILQ_NEXT(s
->search_entry
, entry
);
3506 entry
= TAILQ_PREV(s
->search_entry
,
3507 commit_queue_head
, entry
);
3508 } else if (s
->matched_entry
) {
3510 * If the user has moved the cursor after we hit a match,
3511 * the position from where we should continue searching
3512 * might have changed.
3514 if (view
->searching
== TOG_SEARCH_FORWARD
)
3515 entry
= TAILQ_NEXT(s
->selected_entry
, entry
);
3517 entry
= TAILQ_PREV(s
->selected_entry
, commit_queue_head
,
3520 entry
= s
->selected_entry
;
3526 if (entry
== NULL
) {
3527 if (s
->thread_args
.log_complete
||
3528 view
->searching
== TOG_SEARCH_BACKWARD
) {
3529 view
->search_next_done
=
3530 (s
->matched_entry
== NULL
?
3531 TOG_SEARCH_HAVE_NONE
: TOG_SEARCH_NO_MORE
);
3532 s
->search_entry
= NULL
;
3536 * Poke the log thread for more commits and return,
3537 * allowing the main loop to make progress. Search
3538 * will resume at s->search_entry once we come back.
3540 s
->thread_args
.commits_needed
++;
3541 return trigger_log_thread(view
, 0);
3544 err
= match_commit(&have_match
, entry
->id
, entry
->commit
,
3549 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
3550 s
->matched_entry
= entry
;
3554 s
->search_entry
= entry
;
3555 if (view
->searching
== TOG_SEARCH_FORWARD
)
3556 entry
= TAILQ_NEXT(entry
, entry
);
3558 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
3561 if (s
->matched_entry
) {
3562 int cur
= s
->selected_entry
->idx
;
3563 while (cur
< s
->matched_entry
->idx
) {
3564 err
= input_log_view(NULL
, view
, KEY_DOWN
);
3569 while (cur
> s
->matched_entry
->idx
) {
3570 err
= input_log_view(NULL
, view
, KEY_UP
);
3577 s
->search_entry
= NULL
;
3582 static const struct got_error
*
3583 open_log_view(struct tog_view
*view
, struct got_object_id
*start_id
,
3584 struct got_repository
*repo
, const char *head_ref_name
,
3585 const char *in_repo_path
, int log_branches
)
3587 const struct got_error
*err
= NULL
;
3588 struct tog_log_view_state
*s
= &view
->state
.log
;
3589 struct got_repository
*thread_repo
= NULL
;
3590 struct got_commit_graph
*thread_graph
= NULL
;
3593 if (in_repo_path
!= s
->in_repo_path
) {
3594 free(s
->in_repo_path
);
3595 s
->in_repo_path
= strdup(in_repo_path
);
3596 if (s
->in_repo_path
== NULL
) {
3597 err
= got_error_from_errno("strdup");
3602 /* The commit queue only contains commits being displayed. */
3603 TAILQ_INIT(&s
->real_commits
.head
);
3604 s
->real_commits
.ncommits
= 0;
3605 s
->commits
= &s
->real_commits
;
3607 TAILQ_INIT(&s
->limit_commits
.head
);
3609 s
->limit_commits
.ncommits
= 0;
3612 if (head_ref_name
) {
3613 s
->head_ref_name
= strdup(head_ref_name
);
3614 if (s
->head_ref_name
== NULL
) {
3615 err
= got_error_from_errno("strdup");
3619 s
->start_id
= got_object_id_dup(start_id
);
3620 if (s
->start_id
== NULL
) {
3621 err
= got_error_from_errno("got_object_id_dup");
3624 s
->log_branches
= log_branches
;
3625 s
->use_committer
= 1;
3627 STAILQ_INIT(&s
->colors
);
3628 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
3629 err
= add_color(&s
->colors
, "^$", TOG_COLOR_COMMIT
,
3630 get_color_value("TOG_COLOR_COMMIT"));
3633 err
= add_color(&s
->colors
, "^$", TOG_COLOR_AUTHOR
,
3634 get_color_value("TOG_COLOR_AUTHOR"));
3636 free_colors(&s
->colors
);
3639 err
= add_color(&s
->colors
, "^$", TOG_COLOR_DATE
,
3640 get_color_value("TOG_COLOR_DATE"));
3642 free_colors(&s
->colors
);
3647 view
->show
= show_log_view
;
3648 view
->input
= input_log_view
;
3649 view
->resize
= resize_log_view
;
3650 view
->close
= close_log_view
;
3651 view
->search_start
= search_start_log_view
;
3652 view
->search_next
= search_next_log_view
;
3654 if (s
->thread_args
.pack_fds
== NULL
) {
3655 err
= got_repo_pack_fds_open(&s
->thread_args
.pack_fds
);
3659 err
= got_repo_open(&thread_repo
, got_repo_get_path(repo
), NULL
,
3660 s
->thread_args
.pack_fds
);
3663 err
= got_commit_graph_open(&thread_graph
, s
->in_repo_path
,
3667 err
= got_commit_graph_iter_start(thread_graph
, s
->start_id
,
3668 s
->repo
, NULL
, NULL
);
3672 errcode
= pthread_cond_init(&s
->thread_args
.need_commits
, NULL
);
3674 err
= got_error_set_errno(errcode
, "pthread_cond_init");
3677 errcode
= pthread_cond_init(&s
->thread_args
.commit_loaded
, NULL
);
3679 err
= got_error_set_errno(errcode
, "pthread_cond_init");
3683 s
->thread_args
.commits_needed
= view
->nlines
;
3684 s
->thread_args
.graph
= thread_graph
;
3685 s
->thread_args
.real_commits
= &s
->real_commits
;
3686 s
->thread_args
.limit_commits
= &s
->limit_commits
;
3687 s
->thread_args
.in_repo_path
= s
->in_repo_path
;
3688 s
->thread_args
.start_id
= s
->start_id
;
3689 s
->thread_args
.repo
= thread_repo
;
3690 s
->thread_args
.log_complete
= 0;
3691 s
->thread_args
.quit
= &s
->quit
;
3692 s
->thread_args
.first_displayed_entry
= &s
->first_displayed_entry
;
3693 s
->thread_args
.selected_entry
= &s
->selected_entry
;
3694 s
->thread_args
.searching
= &view
->searching
;
3695 s
->thread_args
.search_next_done
= &view
->search_next_done
;
3696 s
->thread_args
.regex
= &view
->regex
;
3697 s
->thread_args
.limiting
= &s
->limit_view
;
3698 s
->thread_args
.limit_regex
= &s
->limit_regex
;
3699 s
->thread_args
.limit_commits
= &s
->limit_commits
;
3702 if (view
->close
== NULL
)
3703 close_log_view(view
);
3709 static const struct got_error
*
3710 show_log_view(struct tog_view
*view
)
3712 const struct got_error
*err
;
3713 struct tog_log_view_state
*s
= &view
->state
.log
;
3715 if (s
->thread
== 0) { //NULL) {
3716 int errcode
= pthread_create(&s
->thread
, NULL
, log_thread
,
3719 return got_error_set_errno(errcode
, "pthread_create");
3720 if (s
->thread_args
.commits_needed
> 0) {
3721 err
= trigger_log_thread(view
, 1);
3727 return draw_commits(view
);
3731 log_move_cursor_up(struct tog_view
*view
, int page
, int home
)
3733 struct tog_log_view_state
*s
= &view
->state
.log
;
3735 if (s
->first_displayed_entry
== NULL
)
3737 if (s
->selected_entry
->idx
== 0)
3740 if ((page
&& TAILQ_FIRST(&s
->commits
->head
) == s
->first_displayed_entry
)
3742 s
->selected
= home
? 0 : MAX(0, s
->selected
- page
- 1);
3744 if (!page
&& !home
&& s
->selected
> 0)
3747 log_scroll_up(s
, home
? s
->commits
->ncommits
: MAX(page
, 1));
3753 static const struct got_error
*
3754 log_move_cursor_down(struct tog_view
*view
, int page
)
3756 struct tog_log_view_state
*s
= &view
->state
.log
;
3757 const struct got_error
*err
= NULL
;
3758 int eos
= view
->nlines
- 2;
3760 if (s
->first_displayed_entry
== NULL
)
3763 if (s
->thread_args
.log_complete
&&
3764 s
->selected_entry
->idx
>= s
->commits
->ncommits
- 1)
3767 if (view_is_hsplit_top(view
))
3768 --eos
; /* border consumes the last line */
3771 if (s
->selected
< MIN(eos
, s
->commits
->ncommits
- 1))
3774 err
= log_scroll_down(view
, 1);
3775 } else if (s
->thread_args
.load_all
&& s
->thread_args
.log_complete
) {
3776 struct commit_queue_entry
*entry
;
3780 entry
= TAILQ_LAST(&s
->commits
->head
, commit_queue_head
);
3781 s
->last_displayed_entry
= entry
;
3782 for (n
= 0; n
<= eos
; n
++) {
3785 s
->first_displayed_entry
= entry
;
3786 entry
= TAILQ_PREV(entry
, commit_queue_head
, entry
);
3789 s
->selected
= n
- 1;
3791 if (s
->last_displayed_entry
->idx
== s
->commits
->ncommits
- 1 &&
3792 s
->thread_args
.log_complete
)
3793 s
->selected
+= MIN(page
,
3794 s
->commits
->ncommits
- s
->selected_entry
->idx
- 1);
3796 err
= log_scroll_down(view
, page
);
3802 * We might necessarily overshoot in horizontal
3803 * splits; if so, select the last displayed commit.
3805 if (s
->first_displayed_entry
&& s
->last_displayed_entry
) {
3806 s
->selected
= MIN(s
->selected
,
3807 s
->last_displayed_entry
->idx
-
3808 s
->first_displayed_entry
->idx
);
3813 if (s
->thread_args
.log_complete
&&
3814 s
->selected_entry
->idx
== s
->commits
->ncommits
- 1)
3821 view_get_split(struct tog_view
*view
, int *y
, int *x
)
3826 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
) {
3827 if (view
->child
&& view
->child
->resized_y
)
3828 *y
= view
->child
->resized_y
;
3829 else if (view
->resized_y
)
3830 *y
= view
->resized_y
;
3832 *y
= view_split_begin_y(view
->lines
);
3833 } else if (view
->mode
== TOG_VIEW_SPLIT_VERT
) {
3834 if (view
->child
&& view
->child
->resized_x
)
3835 *x
= view
->child
->resized_x
;
3836 else if (view
->resized_x
)
3837 *x
= view
->resized_x
;
3839 *x
= view_split_begin_x(view
->begin_x
);
3843 /* Split view horizontally at y and offset view->state->selected line. */
3844 static const struct got_error
*
3845 view_init_hsplit(struct tog_view
*view
, int y
)
3847 const struct got_error
*err
= NULL
;
3851 err
= view_resize(view
);
3855 err
= offset_selection_down(view
);
3860 static const struct got_error
*
3861 log_goto_line(struct tog_view
*view
, int nlines
)
3863 const struct got_error
*err
= NULL
;
3864 struct tog_log_view_state
*s
= &view
->state
.log
;
3865 int g
, idx
= s
->selected_entry
->idx
;
3867 if (s
->first_displayed_entry
== NULL
|| s
->last_displayed_entry
== NULL
)
3873 if (g
>= s
->first_displayed_entry
->idx
+ 1 &&
3874 g
<= s
->last_displayed_entry
->idx
+ 1 &&
3875 g
- s
->first_displayed_entry
->idx
- 1 < nlines
) {
3876 s
->selected
= g
- s
->first_displayed_entry
->idx
- 1;
3882 err
= log_move_cursor_down(view
, g
- idx
- 1);
3883 if (!err
&& g
> s
->selected_entry
->idx
+ 1)
3884 err
= log_move_cursor_down(view
,
3885 g
- s
->first_displayed_entry
->idx
- 1);
3888 } else if (idx
+ 1 > g
)
3889 log_move_cursor_up(view
, idx
- g
+ 1, 0);
3891 if (g
< nlines
&& s
->first_displayed_entry
->idx
== 0)
3892 s
->selected
= g
- 1;
3900 horizontal_scroll_input(struct tog_view
*view
, int ch
)
3906 view
->x
-= MIN(view
->x
, 2);
3912 if (view
->x
+ view
->ncols
/ 2 < view
->maxx
)
3921 view
->x
= MAX(view
->maxx
- view
->ncols
/ 2, 0);
3929 static const struct got_error
*
3930 input_log_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
3932 const struct got_error
*err
= NULL
;
3933 struct tog_log_view_state
*s
= &view
->state
.log
;
3936 if (s
->thread_args
.load_all
) {
3937 if (ch
== CTRL('g') || ch
== KEY_BACKSPACE
)
3938 s
->thread_args
.load_all
= 0;
3939 else if (s
->thread_args
.log_complete
) {
3940 err
= log_move_cursor_down(view
, s
->commits
->ncommits
);
3941 s
->thread_args
.load_all
= 0;
3947 eos
= nscroll
= view
->nlines
- 1;
3948 if (view_is_hsplit_top(view
))
3952 return log_goto_line(view
, eos
);
3956 err
= limit_log_view(view
);
3967 horizontal_scroll_input(view
, ch
);
3974 log_move_cursor_up(view
, 0, 0);
3979 log_move_cursor_up(view
, 0, 1);
3989 log_move_cursor_up(view
, nscroll
, 0);
3996 err
= log_move_cursor_down(view
, 0);
3999 s
->use_committer
= !s
->use_committer
;
4000 view
->action
= s
->use_committer
?
4001 "show committer" : "show commit author";
4006 /* We don't know yet how many commits, so we're forced to
4007 * traverse them all. */
4009 s
->thread_args
.load_all
= 1;
4010 if (!s
->thread_args
.log_complete
)
4011 return trigger_log_thread(view
, 0);
4012 err
= log_move_cursor_down(view
, s
->commits
->ncommits
);
4013 s
->thread_args
.load_all
= 0;
4024 err
= log_move_cursor_down(view
, nscroll
);
4027 if (s
->selected
> view
->nlines
- 2)
4028 s
->selected
= view
->nlines
- 2;
4029 if (s
->selected
> s
->commits
->ncommits
- 1)
4030 s
->selected
= s
->commits
->ncommits
- 1;
4032 if (s
->commits
->ncommits
< view
->nlines
- 1 &&
4033 !s
->thread_args
.log_complete
) {
4034 s
->thread_args
.commits_needed
+= (view
->nlines
- 1) -
4035 s
->commits
->ncommits
;
4036 err
= trigger_log_thread(view
, 1);
4042 if (s
->selected_entry
== NULL
)
4044 err
= view_request_new(new_view
, view
, TOG_VIEW_DIFF
);
4048 if (s
->selected_entry
== NULL
)
4050 err
= view_request_new(new_view
, view
, TOG_VIEW_TREE
);
4056 if (ch
== KEY_BACKSPACE
&&
4057 got_path_is_root_dir(s
->in_repo_path
))
4059 err
= stop_log_thread(s
);
4062 if (ch
== KEY_BACKSPACE
) {
4064 err
= got_path_dirname(&parent_path
, s
->in_repo_path
);
4067 free(s
->in_repo_path
);
4068 s
->in_repo_path
= parent_path
;
4069 s
->thread_args
.in_repo_path
= s
->in_repo_path
;
4070 } else if (ch
== CTRL('l')) {
4071 struct got_object_id
*start_id
;
4072 err
= got_repo_match_object_id(&start_id
, NULL
,
4073 s
->head_ref_name
? s
->head_ref_name
: GOT_REF_HEAD
,
4074 GOT_OBJ_TYPE_COMMIT
, &tog_refs
, s
->repo
);
4076 if (s
->head_ref_name
== NULL
||
4077 err
->code
!= GOT_ERR_NOT_REF
)
4079 /* Try to cope with deleted references. */
4080 free(s
->head_ref_name
);
4081 s
->head_ref_name
= NULL
;
4082 err
= got_repo_match_object_id(&start_id
,
4083 NULL
, GOT_REF_HEAD
, GOT_OBJ_TYPE_COMMIT
,
4084 &tog_refs
, s
->repo
);
4089 s
->start_id
= start_id
;
4090 s
->thread_args
.start_id
= s
->start_id
;
4092 s
->log_branches
= !s
->log_branches
;
4094 if (s
->thread_args
.pack_fds
== NULL
) {
4095 err
= got_repo_pack_fds_open(&s
->thread_args
.pack_fds
);
4099 err
= got_repo_open(&s
->thread_args
.repo
,
4100 got_repo_get_path(s
->repo
), NULL
,
4101 s
->thread_args
.pack_fds
);
4105 err
= tog_load_refs(s
->repo
, 0);
4108 err
= got_commit_graph_open(&s
->thread_args
.graph
,
4109 s
->in_repo_path
, !s
->log_branches
);
4112 err
= got_commit_graph_iter_start(s
->thread_args
.graph
,
4113 s
->start_id
, s
->repo
, NULL
, NULL
);
4116 free_commits(&s
->real_commits
);
4117 free_commits(&s
->limit_commits
);
4118 s
->first_displayed_entry
= NULL
;
4119 s
->last_displayed_entry
= NULL
;
4120 s
->selected_entry
= NULL
;
4122 s
->thread_args
.log_complete
= 0;
4124 s
->thread_args
.commits_needed
= view
->lines
;
4125 s
->matched_entry
= NULL
;
4126 s
->search_entry
= NULL
;
4131 err
= view_request_new(new_view
, view
, TOG_VIEW_REF
);
4141 static const struct got_error
*
4142 apply_unveil(const char *repo_path
, const char *worktree_path
)
4144 const struct got_error
*error
;
4147 if (unveil("gmon.out", "rwc") != 0)
4148 return got_error_from_errno2("unveil", "gmon.out");
4150 if (repo_path
&& unveil(repo_path
, "r") != 0)
4151 return got_error_from_errno2("unveil", repo_path
);
4153 if (worktree_path
&& unveil(worktree_path
, "rwc") != 0)
4154 return got_error_from_errno2("unveil", worktree_path
);
4156 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
4157 return got_error_from_errno2("unveil", GOT_TMPDIR_STR
);
4159 error
= got_privsep_unveil_exec_helpers();
4163 if (unveil(NULL
, NULL
) != 0)
4164 return got_error_from_errno("unveil");
4169 static const struct got_error
*
4170 init_mock_term(const char *test_script_path
)
4172 const struct got_error
*err
= NULL
;
4174 if (test_script_path
== NULL
|| *test_script_path
== '\0')
4175 return got_error_msg(GOT_ERR_IO
, "TOG_TEST_SCRIPT not defined");
4177 tog_io
.f
= fopen(test_script_path
, "re");
4178 if (tog_io
.f
== NULL
) {
4179 err
= got_error_from_errno_fmt("fopen: %s",
4184 /* test mode, we don't want any output */
4185 tog_io
.cout
= fopen("/dev/null", "w+");
4186 if (tog_io
.cout
== NULL
) {
4187 err
= got_error_from_errno("fopen: /dev/null");
4191 tog_io
.cin
= fopen("/dev/tty", "r+");
4192 if (tog_io
.cin
== NULL
) {
4193 err
= got_error_from_errno("fopen: /dev/tty");
4197 if (fseeko(tog_io
.f
, 0L, SEEK_SET
) == -1) {
4198 err
= got_error_from_errno("fseeko");
4202 /* use local TERM so we test in different environments */
4203 if (newterm(NULL
, tog_io
.cout
, tog_io
.cin
) == NULL
)
4204 err
= got_error_msg(GOT_ERR_IO
,
4205 "newterm: failed to initialise curses");
4218 * Override default signal handlers before starting ncurses.
4219 * This should prevent ncurses from installing its own
4220 * broken cleanup() signal handler.
4222 signal(SIGWINCH
, tog_sigwinch
);
4223 signal(SIGPIPE
, tog_sigpipe
);
4224 signal(SIGCONT
, tog_sigcont
);
4225 signal(SIGINT
, tog_sigint
);
4226 signal(SIGTERM
, tog_sigterm
);
4228 if (using_mock_io
) /* In test mode we use a fake terminal */
4234 halfdelay(1); /* Fast refresh while initial view is loading. */
4237 intrflush(stdscr
, FALSE
);
4238 keypad(stdscr
, TRUE
);
4240 if (getenv("TOG_COLORS") != NULL
) {
4242 use_default_colors();
4248 static const struct got_error
*
4249 get_in_repo_path_from_argv0(char **in_repo_path
, int argc
, char *argv
[],
4250 struct got_repository
*repo
, struct got_worktree
*worktree
)
4252 const struct got_error
*err
= NULL
;
4255 *in_repo_path
= strdup("/");
4256 if (*in_repo_path
== NULL
)
4257 return got_error_from_errno("strdup");
4262 const char *prefix
= got_worktree_get_path_prefix(worktree
);
4265 err
= got_worktree_resolve_path(&p
, worktree
, argv
[0]);
4268 if (asprintf(in_repo_path
, "%s%s%s", prefix
,
4269 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
4271 err
= got_error_from_errno("asprintf");
4272 *in_repo_path
= NULL
;
4276 err
= got_repo_map_path(in_repo_path
, repo
, argv
[0]);
4281 static const struct got_error
*
4282 cmd_log(int argc
, char *argv
[])
4284 const struct got_error
*error
;
4285 struct got_repository
*repo
= NULL
;
4286 struct got_worktree
*worktree
= NULL
;
4287 struct got_object_id
*start_id
= NULL
;
4288 char *in_repo_path
= NULL
, *repo_path
= NULL
, *cwd
= NULL
;
4289 char *start_commit
= NULL
, *label
= NULL
;
4290 struct got_reference
*ref
= NULL
;
4291 const char *head_ref_name
= NULL
;
4292 int ch
, log_branches
= 0;
4293 struct tog_view
*view
;
4294 int *pack_fds
= NULL
;
4296 while ((ch
= getopt(argc
, argv
, "bc:r:")) != -1) {
4302 start_commit
= optarg
;
4305 repo_path
= realpath(optarg
, NULL
);
4306 if (repo_path
== NULL
)
4307 return got_error_from_errno2("realpath",
4322 error
= got_repo_pack_fds_open(&pack_fds
);
4326 if (repo_path
== NULL
) {
4327 cwd
= getcwd(NULL
, 0);
4329 return got_error_from_errno("getcwd");
4330 error
= got_worktree_open(&worktree
, cwd
);
4331 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4335 strdup(got_worktree_get_repo_path(worktree
));
4337 repo_path
= strdup(cwd
);
4338 if (repo_path
== NULL
) {
4339 error
= got_error_from_errno("strdup");
4344 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
4348 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
4355 error
= apply_unveil(got_repo_get_path(repo
),
4356 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4360 /* already loaded by tog_log_with_path()? */
4361 if (TAILQ_EMPTY(&tog_refs
)) {
4362 error
= tog_load_refs(repo
, 0);
4367 if (start_commit
== NULL
) {
4368 error
= got_repo_match_object_id(&start_id
, &label
,
4369 worktree
? got_worktree_get_head_ref_name(worktree
) :
4370 GOT_REF_HEAD
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
4373 head_ref_name
= label
;
4375 error
= got_ref_open(&ref
, repo
, start_commit
, 0);
4377 head_ref_name
= got_ref_get_name(ref
);
4378 else if (error
->code
!= GOT_ERR_NOT_REF
)
4380 error
= got_repo_match_object_id(&start_id
, NULL
,
4381 start_commit
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
4386 view
= view_open(0, 0, 0, 0, TOG_VIEW_LOG
);
4388 error
= got_error_from_errno("view_open");
4391 error
= open_log_view(view
, start_id
, repo
, head_ref_name
,
4392 in_repo_path
, log_branches
);
4396 /* Release work tree lock. */
4397 got_worktree_close(worktree
);
4400 error
= view_loop(view
);
4410 const struct got_error
*close_err
= got_repo_close(repo
);
4415 got_worktree_close(worktree
);
4417 const struct got_error
*pack_err
=
4418 got_repo_pack_fds_close(pack_fds
);
4430 fprintf(stderr
, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4431 "object1 object2\n", getprogname());
4436 match_line(const char *line
, regex_t
*regex
, size_t nmatch
,
4437 regmatch_t
*regmatch
)
4439 return regexec(regex
, line
, nmatch
, regmatch
, 0) == 0;
4442 static struct tog_color
*
4443 match_color(struct tog_colors
*colors
, const char *line
)
4445 struct tog_color
*tc
= NULL
;
4447 STAILQ_FOREACH(tc
, colors
, entry
) {
4448 if (match_line(line
, &tc
->regex
, 0, NULL
))
4455 static const struct got_error
*
4456 add_matched_line(int *wtotal
, const char *line
, int wlimit
, int col_tab_align
,
4457 WINDOW
*window
, int skipcol
, regmatch_t
*regmatch
)
4459 const struct got_error
*err
= NULL
;
4461 wchar_t *wline
= NULL
;
4462 int rme
, rms
, n
, width
, scrollx
;
4463 int width0
= 0, width1
= 0, width2
= 0;
4464 char *seg0
= NULL
, *seg1
= NULL
, *seg2
= NULL
;
4468 rms
= regmatch
->rm_so
;
4469 rme
= regmatch
->rm_eo
;
4471 err
= expand_tab(&exstr
, line
);
4475 /* Split the line into 3 segments, according to match offsets. */
4476 seg0
= strndup(exstr
, rms
);
4478 err
= got_error_from_errno("strndup");
4481 seg1
= strndup(exstr
+ rms
, rme
- rms
);
4483 err
= got_error_from_errno("strndup");
4486 seg2
= strdup(exstr
+ rme
);
4488 err
= got_error_from_errno("strndup");
4492 /* draw up to matched token if we haven't scrolled past it */
4493 err
= format_line(&wline
, &width0
, NULL
, seg0
, 0, wlimit
,
4497 n
= MAX(width0
- skipcol
, 0);
4500 err
= format_line(&wline
, &width
, &scrollx
, seg0
, skipcol
,
4501 wlimit
, col_tab_align
, 1);
4504 waddwstr(window
, &wline
[scrollx
]);
4514 err
= format_line(&wline
, &width1
, NULL
, seg1
, 0, wlimit
,
4518 wlen
= wcslen(wline
);
4520 width
= wcwidth(wline
[i
]);
4522 /* should not happen, tabs are expanded */
4523 err
= got_error(GOT_ERR_RANGE
);
4526 if (width0
+ w
+ width
> skipcol
)
4531 /* draw (visible part of) matched token (if scrolled into it) */
4532 if (width1
- w
> 0) {
4533 wattron(window
, A_STANDOUT
);
4534 waddwstr(window
, &wline
[i
]);
4535 wattroff(window
, A_STANDOUT
);
4536 wlimit
-= (width1
- w
);
4537 *wtotal
+= (width1
- w
);
4541 if (wlimit
> 0) { /* draw rest of line */
4543 if (skipcol
> width0
+ width1
) {
4544 err
= format_line(&wline
, &width2
, &scrollx
, seg2
,
4545 skipcol
- (width0
+ width1
), wlimit
,
4549 waddwstr(window
, &wline
[scrollx
]);
4551 err
= format_line(&wline
, &width2
, NULL
, seg2
, 0,
4552 wlimit
, col_tab_align
, 1);
4555 waddwstr(window
, wline
);
4569 gotoline(struct tog_view
*view
, int *lineno
, int *nprinted
)
4572 int *eof
, *first
, *selected
;
4574 if (view
->type
== TOG_VIEW_DIFF
) {
4575 struct tog_diff_view_state
*s
= &view
->state
.diff
;
4577 first
= &s
->first_displayed_line
;
4581 } else if (view
->type
== TOG_VIEW_HELP
) {
4582 struct tog_help_view_state
*s
= &view
->state
.help
;
4584 first
= &s
->first_displayed_line
;
4588 } else if (view
->type
== TOG_VIEW_BLAME
) {
4589 struct tog_blame_view_state
*s
= &view
->state
.blame
;
4591 first
= &s
->first_displayed_line
;
4592 selected
= &s
->selected_line
;
4598 /* Center gline in the middle of the page like vi(1). */
4599 if (*lineno
< view
->gline
- (view
->nlines
- 3) / 2)
4601 if (*first
!= 1 && (*lineno
> view
->gline
- (view
->nlines
- 3) / 2)) {
4610 *selected
= view
->gline
<= (view
->nlines
- 3) / 2 ?
4611 view
->gline
: (view
->nlines
- 3) / 2 + 1;
4617 static const struct got_error
*
4618 draw_file(struct tog_view
*view
, const char *header
)
4620 struct tog_diff_view_state
*s
= &view
->state
.diff
;
4621 regmatch_t
*regmatch
= &view
->regmatch
;
4622 const struct got_error
*err
;
4625 size_t linesize
= 0;
4629 int max_lines
= view
->nlines
;
4630 int nlines
= s
->nlines
;
4633 s
->lineno
= s
->first_displayed_line
- 1;
4634 line_offset
= s
->lines
[s
->first_displayed_line
- 1].offset
;
4635 if (fseeko(s
->f
, line_offset
, SEEK_SET
) == -1)
4636 return got_error_from_errno("fseek");
4638 werase(view
->window
);
4640 if (view
->gline
> s
->nlines
- 1)
4641 view
->gline
= s
->nlines
- 1;
4644 int ln
= view
->gline
? view
->gline
<= (view
->nlines
- 3) / 2 ?
4645 1 : view
->gline
- (view
->nlines
- 3) / 2 :
4646 s
->lineno
+ s
->selected_line
;
4648 if (asprintf(&line
, "[%d/%d] %s", ln
, nlines
, header
) == -1)
4649 return got_error_from_errno("asprintf");
4650 err
= format_line(&wline
, &width
, NULL
, line
, 0, view
->ncols
,
4656 if (view_needs_focus_indication(view
))
4657 wstandout(view
->window
);
4658 waddwstr(view
->window
, wline
);
4661 while (width
++ < view
->ncols
)
4662 waddch(view
->window
, ' ');
4663 if (view_needs_focus_indication(view
))
4664 wstandend(view
->window
);
4674 while (max_lines
> 0 && nprinted
< max_lines
) {
4675 enum got_diff_line_type linetype
;
4678 linelen
= getline(&line
, &linesize
, s
->f
);
4679 if (linelen
== -1) {
4685 return got_ferror(s
->f
, GOT_ERR_IO
);
4688 if (++s
->lineno
< s
->first_displayed_line
)
4690 if (view
->gline
&& !gotoline(view
, &s
->lineno
, &nprinted
))
4692 if (s
->lineno
== view
->hiline
)
4695 /* Set view->maxx based on full line length. */
4696 err
= format_line(&wline
, &width
, NULL
, line
, 0, INT_MAX
, 0,
4702 view
->maxx
= MAX(view
->maxx
, width
);
4706 linetype
= s
->lines
[s
->lineno
].type
;
4707 if (linetype
> GOT_DIFF_LINE_LOGMSG
&&
4708 linetype
< GOT_DIFF_LINE_CONTEXT
)
4709 attr
|= COLOR_PAIR(linetype
);
4711 wattron(view
->window
, attr
);
4712 if (s
->first_displayed_line
+ nprinted
== s
->matched_line
&&
4713 regmatch
->rm_so
>= 0 && regmatch
->rm_so
< regmatch
->rm_eo
) {
4714 err
= add_matched_line(&width
, line
, view
->ncols
, 0,
4715 view
->window
, view
->x
, regmatch
);
4722 err
= format_line(&wline
, &width
, &skip
, line
,
4723 view
->x
, view
->ncols
, 0, view
->x
? 1 : 0);
4728 waddwstr(view
->window
, &wline
[skip
]);
4732 if (s
->lineno
== view
->hiline
) {
4733 /* highlight full gline length */
4734 while (width
++ < view
->ncols
)
4735 waddch(view
->window
, ' ');
4737 if (width
<= view
->ncols
- 1)
4738 waddch(view
->window
, '\n');
4741 wattroff(view
->window
, attr
);
4742 if (++nprinted
== 1)
4743 s
->first_displayed_line
= s
->lineno
;
4747 s
->last_displayed_line
= s
->first_displayed_line
+
4750 s
->last_displayed_line
= s
->first_displayed_line
;
4755 while (nprinted
< view
->nlines
) {
4756 waddch(view
->window
, '\n');
4760 err
= format_line(&wline
, &width
, NULL
, TOG_EOF_STRING
, 0,
4766 wstandout(view
->window
);
4767 waddwstr(view
->window
, wline
);
4770 wstandend(view
->window
);
4777 get_datestr(time_t *time
, char *datebuf
)
4779 struct tm mytm
, *tm
;
4782 tm
= gmtime_r(time
, &mytm
);
4785 s
= asctime_r(tm
, datebuf
);
4788 p
= strchr(s
, '\n');
4794 static const struct got_error
*
4795 add_line_metadata(struct got_diff_line
**lines
, size_t *nlines
,
4796 off_t off
, uint8_t type
)
4798 struct got_diff_line
*p
;
4800 p
= reallocarray(*lines
, *nlines
+ 1, sizeof(**lines
));
4802 return got_error_from_errno("reallocarray");
4804 (*lines
)[*nlines
].offset
= off
;
4805 (*lines
)[*nlines
].type
= type
;
4811 static const struct got_error
*
4812 cat_diff(FILE *dst
, FILE *src
, struct got_diff_line
**d_lines
, size_t *d_nlines
,
4813 struct got_diff_line
*s_lines
, size_t s_nlines
)
4815 struct got_diff_line
*p
;
4819 if (fseeko(src
, 0L, SEEK_SET
) == -1)
4820 return got_error_from_errno("fseeko");
4823 r
= fread(buf
, 1, sizeof(buf
), src
);
4826 return got_error_from_errno("fread");
4830 if (fwrite(buf
, 1, r
, dst
) != r
)
4831 return got_ferror(dst
, GOT_ERR_IO
);
4834 if (s_nlines
== 0 && *d_nlines
== 0)
4838 * If commit info was in dst, increment line offsets
4839 * of the appended diff content, but skip s_lines[0]
4840 * because offset zero is already in *d_lines.
4842 if (*d_nlines
> 0) {
4843 for (i
= 1; i
< s_nlines
; ++i
)
4844 s_lines
[i
].offset
+= (*d_lines
)[*d_nlines
- 1].offset
;
4852 p
= reallocarray(*d_lines
, *d_nlines
+ s_nlines
, sizeof(*p
));
4854 /* d_lines is freed in close_diff_view() */
4855 return got_error_from_errno("reallocarray");
4860 memcpy(*d_lines
+ *d_nlines
, s_lines
, s_nlines
* sizeof(*s_lines
));
4861 *d_nlines
+= s_nlines
;
4866 static const struct got_error
*
4867 write_commit_info(struct got_diff_line
**lines
, size_t *nlines
,
4868 struct got_object_id
*commit_id
, struct got_reflist_head
*refs
,
4869 struct got_repository
*repo
, int ignore_ws
, int force_text_diff
,
4870 struct got_diffstat_cb_arg
*dsa
, FILE *outfile
)
4872 const struct got_error
*err
= NULL
;
4873 char datebuf
[26], *datestr
;
4874 struct got_commit_object
*commit
;
4875 char *id_str
= NULL
, *logmsg
= NULL
, *s
= NULL
, *line
;
4876 time_t committer_time
;
4877 const char *author
, *committer
;
4878 char *refs_str
= NULL
;
4879 struct got_pathlist_entry
*pe
;
4884 err
= build_refs_str(&refs_str
, refs
, commit_id
, repo
);
4889 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
4893 err
= got_object_id_str(&id_str
, commit_id
);
4895 err
= got_error_from_errno("got_object_id_str");
4899 err
= add_line_metadata(lines
, nlines
, 0, GOT_DIFF_LINE_NONE
);
4903 n
= fprintf(outfile
, "commit %s%s%s%s\n", id_str
, refs_str
? " (" : "",
4904 refs_str
? refs_str
: "", refs_str
? ")" : "");
4906 err
= got_error_from_errno("fprintf");
4910 err
= add_line_metadata(lines
, nlines
, outoff
, GOT_DIFF_LINE_META
);
4914 n
= fprintf(outfile
, "from: %s\n",
4915 got_object_commit_get_author(commit
));
4917 err
= got_error_from_errno("fprintf");
4921 err
= add_line_metadata(lines
, nlines
, outoff
, GOT_DIFF_LINE_AUTHOR
);
4925 author
= got_object_commit_get_author(commit
);
4926 committer
= got_object_commit_get_committer(commit
);
4927 if (strcmp(author
, committer
) != 0) {
4928 n
= fprintf(outfile
, "via: %s\n", committer
);
4930 err
= got_error_from_errno("fprintf");
4934 err
= add_line_metadata(lines
, nlines
, outoff
,
4935 GOT_DIFF_LINE_AUTHOR
);
4939 committer_time
= got_object_commit_get_committer_time(commit
);
4940 datestr
= get_datestr(&committer_time
, datebuf
);
4942 n
= fprintf(outfile
, "date: %s UTC\n", datestr
);
4944 err
= got_error_from_errno("fprintf");
4948 err
= add_line_metadata(lines
, nlines
, outoff
,
4949 GOT_DIFF_LINE_DATE
);
4953 if (got_object_commit_get_nparents(commit
) > 1) {
4954 const struct got_object_id_queue
*parent_ids
;
4955 struct got_object_qid
*qid
;
4957 parent_ids
= got_object_commit_get_parent_ids(commit
);
4958 STAILQ_FOREACH(qid
, parent_ids
, entry
) {
4959 err
= got_object_id_str(&id_str
, &qid
->id
);
4962 n
= fprintf(outfile
, "parent %d: %s\n", pn
++, id_str
);
4964 err
= got_error_from_errno("fprintf");
4968 err
= add_line_metadata(lines
, nlines
, outoff
,
4969 GOT_DIFF_LINE_META
);
4977 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
4981 while ((line
= strsep(&s
, "\n")) != NULL
) {
4982 n
= fprintf(outfile
, "%s\n", line
);
4984 err
= got_error_from_errno("fprintf");
4988 err
= add_line_metadata(lines
, nlines
, outoff
,
4989 GOT_DIFF_LINE_LOGMSG
);
4994 TAILQ_FOREACH(pe
, dsa
->paths
, entry
) {
4995 struct got_diff_changed_path
*cp
= pe
->data
;
4996 int pad
= dsa
->max_path_len
- pe
->path_len
+ 1;
4998 n
= fprintf(outfile
, "%c %s%*c | %*d+ %*d-\n", cp
->status
,
4999 pe
->path
, pad
, ' ', dsa
->add_cols
+ 1, cp
->add
,
5000 dsa
->rm_cols
+ 1, cp
->rm
);
5002 err
= got_error_from_errno("fprintf");
5006 err
= add_line_metadata(lines
, nlines
, outoff
,
5007 GOT_DIFF_LINE_CHANGES
);
5012 fputc('\n', outfile
);
5014 err
= add_line_metadata(lines
, nlines
, outoff
, GOT_DIFF_LINE_NONE
);
5018 n
= fprintf(outfile
,
5019 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5020 dsa
->nfiles
, dsa
->nfiles
> 1 ? "s" : "", dsa
->ins
,
5021 dsa
->ins
!= 1 ? "s" : "", dsa
->del
, dsa
->del
!= 1 ? "s" : "");
5023 err
= got_error_from_errno("fprintf");
5027 err
= add_line_metadata(lines
, nlines
, outoff
, GOT_DIFF_LINE_NONE
);
5031 fputc('\n', outfile
);
5033 err
= add_line_metadata(lines
, nlines
, outoff
, GOT_DIFF_LINE_NONE
);
5038 got_object_commit_close(commit
);
5047 static const struct got_error
*
5048 create_diff(struct tog_diff_view_state
*s
)
5050 const struct got_error
*err
= NULL
;
5051 FILE *f
= NULL
, *tmp_diff_file
= NULL
;
5053 struct got_diff_line
*lines
= NULL
;
5054 struct got_pathlist_head changed_paths
;
5056 TAILQ_INIT(&changed_paths
);
5059 s
->lines
= malloc(sizeof(*s
->lines
));
5060 if (s
->lines
== NULL
)
5061 return got_error_from_errno("malloc");
5066 err
= got_error_from_errno("got_opentemp");
5069 tmp_diff_file
= got_opentemp();
5070 if (tmp_diff_file
== NULL
) {
5071 err
= got_error_from_errno("got_opentemp");
5074 if (s
->f
&& fclose(s
->f
) == EOF
) {
5075 err
= got_error_from_errno("fclose");
5081 err
= got_object_get_type(&obj_type
, s
->repo
, s
->id1
);
5083 err
= got_object_get_type(&obj_type
, s
->repo
, s
->id2
);
5088 case GOT_OBJ_TYPE_BLOB
:
5089 err
= got_diff_objects_as_blobs(&s
->lines
, &s
->nlines
,
5090 s
->f1
, s
->f2
, s
->fd1
, s
->fd2
, s
->id1
, s
->id2
,
5091 s
->label1
, s
->label2
, tog_diff_algo
, s
->diff_context
,
5092 s
->ignore_whitespace
, s
->force_text_diff
, NULL
, s
->repo
,
5095 case GOT_OBJ_TYPE_TREE
:
5096 err
= got_diff_objects_as_trees(&s
->lines
, &s
->nlines
,
5097 s
->f1
, s
->f2
, s
->fd1
, s
->fd2
, s
->id1
, s
->id2
, NULL
, "", "",
5098 tog_diff_algo
, s
->diff_context
, s
->ignore_whitespace
,
5099 s
->force_text_diff
, NULL
, s
->repo
, s
->f
);
5101 case GOT_OBJ_TYPE_COMMIT
: {
5102 const struct got_object_id_queue
*parent_ids
;
5103 struct got_object_qid
*pid
;
5104 struct got_commit_object
*commit2
;
5105 struct got_reflist_head
*refs
;
5107 struct got_diffstat_cb_arg dsa
= {
5110 s
->ignore_whitespace
,
5115 lines
= malloc(sizeof(*lines
));
5116 if (lines
== NULL
) {
5117 err
= got_error_from_errno("malloc");
5121 /* build diff first in tmp file then append to commit info */
5122 err
= got_diff_objects_as_commits(&lines
, &nlines
,
5123 s
->f1
, s
->f2
, s
->fd1
, s
->fd2
, s
->id1
, s
->id2
, NULL
,
5124 tog_diff_algo
, s
->diff_context
, s
->ignore_whitespace
,
5125 s
->force_text_diff
, &dsa
, s
->repo
, tmp_diff_file
);
5129 err
= got_object_open_as_commit(&commit2
, s
->repo
, s
->id2
);
5132 refs
= got_reflist_object_id_map_lookup(tog_refs_idmap
, s
->id2
);
5133 /* Show commit info if we're diffing to a parent/root commit. */
5134 if (s
->id1
== NULL
) {
5135 err
= write_commit_info(&s
->lines
, &s
->nlines
, s
->id2
,
5136 refs
, s
->repo
, s
->ignore_whitespace
,
5137 s
->force_text_diff
, &dsa
, s
->f
);
5141 parent_ids
= got_object_commit_get_parent_ids(commit2
);
5142 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
5143 if (got_object_id_cmp(s
->id1
, &pid
->id
) == 0) {
5144 err
= write_commit_info(&s
->lines
,
5145 &s
->nlines
, s
->id2
, refs
, s
->repo
,
5146 s
->ignore_whitespace
,
5147 s
->force_text_diff
, &dsa
, s
->f
);
5154 got_object_commit_close(commit2
);
5156 err
= cat_diff(s
->f
, tmp_diff_file
, &s
->lines
, &s
->nlines
,
5161 err
= got_error(GOT_ERR_OBJ_TYPE
);
5166 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
5167 if (s
->f
&& fflush(s
->f
) != 0 && err
== NULL
)
5168 err
= got_error_from_errno("fflush");
5169 if (tmp_diff_file
&& fclose(tmp_diff_file
) == EOF
&& err
== NULL
)
5170 err
= got_error_from_errno("fclose");
5175 diff_view_indicate_progress(struct tog_view
*view
)
5177 mvwaddstr(view
->window
, 0, 0, "diffing...");
5182 static const struct got_error
*
5183 search_start_diff_view(struct tog_view
*view
)
5185 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5187 s
->matched_line
= 0;
5192 search_setup_diff_view(struct tog_view
*view
, FILE **f
, off_t
**line_offsets
,
5193 size_t *nlines
, int **first
, int **last
, int **match
, int **selected
)
5195 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5198 *nlines
= s
->nlines
;
5199 *line_offsets
= NULL
;
5200 *match
= &s
->matched_line
;
5201 *first
= &s
->first_displayed_line
;
5202 *last
= &s
->last_displayed_line
;
5203 *selected
= &s
->selected_line
;
5206 static const struct got_error
*
5207 search_next_view_match(struct tog_view
*view
)
5209 const struct got_error
*err
= NULL
;
5213 size_t linesize
= 0;
5215 off_t
*line_offsets
;
5217 int *first
, *last
, *match
, *selected
;
5219 if (!view
->search_setup
)
5220 return got_error_msg(GOT_ERR_NOT_IMPL
,
5221 "view search not supported");
5222 view
->search_setup(view
, &f
, &line_offsets
, &nlines
, &first
, &last
,
5225 if (!view
->searching
) {
5226 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5231 if (view
->searching
== TOG_SEARCH_FORWARD
)
5232 lineno
= *first
+ 1;
5234 lineno
= *first
- 1;
5236 lineno
= *first
- 1 + *selected
;
5241 if (lineno
<= 0 || lineno
> nlines
) {
5243 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5247 if (view
->searching
== TOG_SEARCH_FORWARD
)
5253 offset
= view
->type
== TOG_VIEW_DIFF
?
5254 view
->state
.diff
.lines
[lineno
- 1].offset
:
5255 line_offsets
[lineno
- 1];
5256 if (fseeko(f
, offset
, SEEK_SET
) != 0) {
5258 return got_error_from_errno("fseeko");
5260 linelen
= getline(&line
, &linesize
, f
);
5261 if (linelen
!= -1) {
5263 err
= expand_tab(&exstr
, line
);
5266 if (match_line(exstr
, &view
->regex
, 1,
5268 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
5275 if (view
->searching
== TOG_SEARCH_FORWARD
)
5290 static const struct got_error
*
5291 close_diff_view(struct tog_view
*view
)
5293 const struct got_error
*err
= NULL
;
5294 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5300 if (s
->f
&& fclose(s
->f
) == EOF
)
5301 err
= got_error_from_errno("fclose");
5303 if (s
->f1
&& fclose(s
->f1
) == EOF
&& err
== NULL
)
5304 err
= got_error_from_errno("fclose");
5306 if (s
->f2
&& fclose(s
->f2
) == EOF
&& err
== NULL
)
5307 err
= got_error_from_errno("fclose");
5309 if (s
->fd1
!= -1 && close(s
->fd1
) == -1 && err
== NULL
)
5310 err
= got_error_from_errno("close");
5312 if (s
->fd2
!= -1 && close(s
->fd2
) == -1 && err
== NULL
)
5313 err
= got_error_from_errno("close");
5321 static const struct got_error
*
5322 open_diff_view(struct tog_view
*view
, struct got_object_id
*id1
,
5323 struct got_object_id
*id2
, const char *label1
, const char *label2
,
5324 int diff_context
, int ignore_whitespace
, int force_text_diff
,
5325 struct tog_view
*parent_view
, struct got_repository
*repo
)
5327 const struct got_error
*err
;
5328 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5330 memset(s
, 0, sizeof(*s
));
5334 if (id1
!= NULL
&& id2
!= NULL
) {
5337 err
= got_object_get_type(&type1
, repo
, id1
);
5340 err
= got_object_get_type(&type2
, repo
, id2
);
5344 if (type1
!= type2
) {
5345 err
= got_error(GOT_ERR_OBJ_TYPE
);
5349 s
->first_displayed_line
= 1;
5350 s
->last_displayed_line
= view
->nlines
;
5351 s
->selected_line
= 1;
5359 s
->id1
= got_object_id_dup(id1
);
5360 if (s
->id1
== NULL
) {
5361 err
= got_error_from_errno("got_object_id_dup");
5367 s
->id2
= got_object_id_dup(id2
);
5368 if (s
->id2
== NULL
) {
5369 err
= got_error_from_errno("got_object_id_dup");
5373 s
->f1
= got_opentemp();
5374 if (s
->f1
== NULL
) {
5375 err
= got_error_from_errno("got_opentemp");
5379 s
->f2
= got_opentemp();
5380 if (s
->f2
== NULL
) {
5381 err
= got_error_from_errno("got_opentemp");
5385 s
->fd1
= got_opentempfd();
5387 err
= got_error_from_errno("got_opentempfd");
5391 s
->fd2
= got_opentempfd();
5393 err
= got_error_from_errno("got_opentempfd");
5397 s
->diff_context
= diff_context
;
5398 s
->ignore_whitespace
= ignore_whitespace
;
5399 s
->force_text_diff
= force_text_diff
;
5400 s
->parent_view
= parent_view
;
5403 if (has_colors() && getenv("TOG_COLORS") != NULL
&& !using_mock_io
) {
5406 rc
= init_pair(GOT_DIFF_LINE_MINUS
,
5407 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5409 rc
= init_pair(GOT_DIFF_LINE_PLUS
,
5410 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5412 rc
= init_pair(GOT_DIFF_LINE_HUNK
,
5413 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5415 rc
= init_pair(GOT_DIFF_LINE_META
,
5416 get_color_value("TOG_COLOR_DIFF_META"), -1);
5418 rc
= init_pair(GOT_DIFF_LINE_CHANGES
,
5419 get_color_value("TOG_COLOR_DIFF_META"), -1);
5421 rc
= init_pair(GOT_DIFF_LINE_BLOB_MIN
,
5422 get_color_value("TOG_COLOR_DIFF_META"), -1);
5424 rc
= init_pair(GOT_DIFF_LINE_BLOB_PLUS
,
5425 get_color_value("TOG_COLOR_DIFF_META"), -1);
5427 rc
= init_pair(GOT_DIFF_LINE_AUTHOR
,
5428 get_color_value("TOG_COLOR_AUTHOR"), -1);
5430 rc
= init_pair(GOT_DIFF_LINE_DATE
,
5431 get_color_value("TOG_COLOR_DATE"), -1);
5433 err
= got_error(GOT_ERR_RANGE
);
5438 if (parent_view
&& parent_view
->type
== TOG_VIEW_LOG
&&
5439 view_is_splitscreen(view
))
5440 show_log_view(parent_view
); /* draw border */
5441 diff_view_indicate_progress(view
);
5443 err
= create_diff(s
);
5445 view
->show
= show_diff_view
;
5446 view
->input
= input_diff_view
;
5447 view
->reset
= reset_diff_view
;
5448 view
->close
= close_diff_view
;
5449 view
->search_start
= search_start_diff_view
;
5450 view
->search_setup
= search_setup_diff_view
;
5451 view
->search_next
= search_next_view_match
;
5454 if (view
->close
== NULL
)
5455 close_diff_view(view
);
5461 static const struct got_error
*
5462 show_diff_view(struct tog_view
*view
)
5464 const struct got_error
*err
;
5465 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5466 char *id_str1
= NULL
, *id_str2
, *header
;
5467 const char *label1
, *label2
;
5470 err
= got_object_id_str(&id_str1
, s
->id1
);
5473 label1
= s
->label1
? s
->label1
: id_str1
;
5475 label1
= "/dev/null";
5477 err
= got_object_id_str(&id_str2
, s
->id2
);
5480 label2
= s
->label2
? s
->label2
: id_str2
;
5482 if (asprintf(&header
, "diff %s %s", label1
, label2
) == -1) {
5483 err
= got_error_from_errno("asprintf");
5491 err
= draw_file(view
, header
);
5496 static const struct got_error
*
5497 set_selected_commit(struct tog_diff_view_state
*s
,
5498 struct commit_queue_entry
*entry
)
5500 const struct got_error
*err
;
5501 const struct got_object_id_queue
*parent_ids
;
5502 struct got_commit_object
*selected_commit
;
5503 struct got_object_qid
*pid
;
5506 s
->id2
= got_object_id_dup(entry
->id
);
5508 return got_error_from_errno("got_object_id_dup");
5510 err
= got_object_open_as_commit(&selected_commit
, s
->repo
, entry
->id
);
5513 parent_ids
= got_object_commit_get_parent_ids(selected_commit
);
5515 pid
= STAILQ_FIRST(parent_ids
);
5516 s
->id1
= pid
? got_object_id_dup(&pid
->id
) : NULL
;
5517 got_object_commit_close(selected_commit
);
5521 static const struct got_error
*
5522 reset_diff_view(struct tog_view
*view
)
5524 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5527 wclear(view
->window
);
5528 s
->first_displayed_line
= 1;
5529 s
->last_displayed_line
= view
->nlines
;
5530 s
->matched_line
= 0;
5531 diff_view_indicate_progress(view
);
5532 return create_diff(s
);
5536 diff_prev_index(struct tog_diff_view_state
*s
, enum got_diff_line_type type
)
5540 i
= start
= s
->first_displayed_line
- 1;
5542 while (s
->lines
[i
].type
!= type
) {
5546 return; /* do nothing, requested type not in file */
5549 s
->selected_line
= 1;
5550 s
->first_displayed_line
= i
;
5554 diff_next_index(struct tog_diff_view_state
*s
, enum got_diff_line_type type
)
5558 i
= start
= s
->first_displayed_line
+ 1;
5560 while (s
->lines
[i
].type
!= type
) {
5561 if (i
== s
->nlines
- 1)
5564 return; /* do nothing, requested type not in file */
5567 s
->selected_line
= 1;
5568 s
->first_displayed_line
= i
;
5571 static struct got_object_id
*get_selected_commit_id(struct tog_blame_line
*,
5573 static struct got_object_id
*get_annotation_for_line(struct tog_blame_line
*,
5576 static const struct got_error
*
5577 input_diff_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
5579 const struct got_error
*err
= NULL
;
5580 struct tog_diff_view_state
*s
= &view
->state
.diff
;
5581 struct tog_log_view_state
*ls
;
5582 struct commit_queue_entry
*old_selected_entry
;
5584 size_t linesize
= 0;
5586 int i
, nscroll
= view
->nlines
- 1, up
= 0;
5588 s
->lineno
= s
->first_displayed_line
- 1 + s
->selected_line
;
5597 horizontal_scroll_input(view
, ch
);
5602 s
->force_text_diff
= !s
->force_text_diff
;
5603 view
->action
= s
->force_text_diff
?
5604 "force ASCII text enabled" :
5605 "force ASCII text disabled";
5607 else if (ch
== 'w') {
5608 s
->ignore_whitespace
= !s
->ignore_whitespace
;
5609 view
->action
= s
->ignore_whitespace
?
5610 "ignore whitespace enabled" :
5611 "ignore whitespace disabled";
5613 err
= reset_diff_view(view
);
5617 s
->first_displayed_line
= 1;
5626 s
->first_displayed_line
= (s
->nlines
- view
->nlines
) + 2;
5632 if (s
->first_displayed_line
> 1)
5633 s
->first_displayed_line
--;
5644 if (s
->first_displayed_line
== 1) {
5649 while (i
++ < nscroll
&& s
->first_displayed_line
> 1)
5650 s
->first_displayed_line
--;
5656 s
->first_displayed_line
++;
5673 while (!s
->eof
&& i
++ < nscroll
) {
5674 linelen
= getline(&line
, &linesize
, s
->f
);
5675 s
->first_displayed_line
++;
5676 if (linelen
== -1) {
5680 err
= got_ferror(s
->f
, GOT_ERR_IO
);
5687 diff_prev_index(s
, GOT_DIFF_LINE_BLOB_MIN
);
5690 diff_next_index(s
, GOT_DIFF_LINE_BLOB_MIN
);
5693 diff_prev_index(s
, GOT_DIFF_LINE_HUNK
);
5696 diff_next_index(s
, GOT_DIFF_LINE_HUNK
);
5699 if (s
->diff_context
> 0) {
5701 s
->matched_line
= 0;
5702 diff_view_indicate_progress(view
);
5703 err
= create_diff(s
);
5704 if (s
->first_displayed_line
+ view
->nlines
- 1 >
5706 s
->first_displayed_line
= 1;
5707 s
->last_displayed_line
= view
->nlines
;
5713 if (s
->diff_context
< GOT_DIFF_MAX_CONTEXT
) {
5715 s
->matched_line
= 0;
5716 diff_view_indicate_progress(view
);
5717 err
= create_diff(s
);
5729 if (s
->parent_view
== NULL
) {
5733 s
->parent_view
->count
= view
->count
;
5735 if (s
->parent_view
->type
== TOG_VIEW_LOG
) {
5736 ls
= &s
->parent_view
->state
.log
;
5737 old_selected_entry
= ls
->selected_entry
;
5739 err
= input_log_view(NULL
, s
->parent_view
,
5740 up
? KEY_UP
: KEY_DOWN
);
5743 view
->count
= s
->parent_view
->count
;
5745 if (old_selected_entry
== ls
->selected_entry
)
5748 err
= set_selected_commit(s
, ls
->selected_entry
);
5751 } else if (s
->parent_view
->type
== TOG_VIEW_BLAME
) {
5752 struct tog_blame_view_state
*bs
;
5753 struct got_object_id
*id
, *prev_id
;
5755 bs
= &s
->parent_view
->state
.blame
;
5756 prev_id
= get_annotation_for_line(bs
->blame
.lines
,
5757 bs
->blame
.nlines
, bs
->last_diffed_line
);
5759 err
= input_blame_view(&view
, s
->parent_view
,
5760 up
? KEY_UP
: KEY_DOWN
);
5763 view
->count
= s
->parent_view
->count
;
5765 if (prev_id
== NULL
)
5767 id
= get_selected_commit_id(bs
->blame
.lines
,
5768 bs
->blame
.nlines
, bs
->first_displayed_line
,
5773 if (!got_object_id_cmp(prev_id
, id
))
5776 err
= input_blame_view(&view
, s
->parent_view
, KEY_ENTER
);
5780 s
->first_displayed_line
= 1;
5781 s
->last_displayed_line
= view
->nlines
;
5782 s
->matched_line
= 0;
5785 diff_view_indicate_progress(view
);
5786 err
= create_diff(s
);
5796 static const struct got_error
*
5797 cmd_diff(int argc
, char *argv
[])
5799 const struct got_error
*error
;
5800 struct got_repository
*repo
= NULL
;
5801 struct got_worktree
*worktree
= NULL
;
5802 struct got_object_id
*id1
= NULL
, *id2
= NULL
;
5803 char *repo_path
= NULL
, *cwd
= NULL
;
5804 char *id_str1
= NULL
, *id_str2
= NULL
;
5805 char *label1
= NULL
, *label2
= NULL
;
5806 int diff_context
= 3, ignore_whitespace
= 0;
5807 int ch
, force_text_diff
= 0;
5809 struct tog_view
*view
;
5810 int *pack_fds
= NULL
;
5812 while ((ch
= getopt(argc
, argv
, "aC:r:w")) != -1) {
5815 force_text_diff
= 1;
5818 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
5821 errx(1, "number of context lines is %s: %s",
5825 repo_path
= realpath(optarg
, NULL
);
5826 if (repo_path
== NULL
)
5827 return got_error_from_errno2("realpath",
5829 got_path_strip_trailing_slashes(repo_path
);
5832 ignore_whitespace
= 1;
5844 usage_diff(); /* TODO show local worktree changes */
5845 } else if (argc
== 2) {
5851 error
= got_repo_pack_fds_open(&pack_fds
);
5855 if (repo_path
== NULL
) {
5856 cwd
= getcwd(NULL
, 0);
5858 return got_error_from_errno("getcwd");
5859 error
= got_worktree_open(&worktree
, cwd
);
5860 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5864 strdup(got_worktree_get_repo_path(worktree
));
5866 repo_path
= strdup(cwd
);
5867 if (repo_path
== NULL
) {
5868 error
= got_error_from_errno("strdup");
5873 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
5879 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
5883 error
= tog_load_refs(repo
, 0);
5887 error
= got_repo_match_object_id(&id1
, &label1
, id_str1
,
5888 GOT_OBJ_TYPE_ANY
, &tog_refs
, repo
);
5892 error
= got_repo_match_object_id(&id2
, &label2
, id_str2
,
5893 GOT_OBJ_TYPE_ANY
, &tog_refs
, repo
);
5897 view
= view_open(0, 0, 0, 0, TOG_VIEW_DIFF
);
5899 error
= got_error_from_errno("view_open");
5902 error
= open_diff_view(view
, id1
, id2
, label1
, label2
, diff_context
,
5903 ignore_whitespace
, force_text_diff
, NULL
, repo
);
5906 error
= view_loop(view
);
5913 const struct got_error
*close_err
= got_repo_close(repo
);
5918 got_worktree_close(worktree
);
5920 const struct got_error
*pack_err
=
5921 got_repo_pack_fds_close(pack_fds
);
5934 "usage: %s blame [-c commit] [-r repository-path] path\n",
5939 struct tog_blame_line
{
5941 struct got_object_id
*id
;
5944 static const struct got_error
*
5945 draw_blame(struct tog_view
*view
)
5947 struct tog_blame_view_state
*s
= &view
->state
.blame
;
5948 struct tog_blame
*blame
= &s
->blame
;
5949 regmatch_t
*regmatch
= &view
->regmatch
;
5950 const struct got_error
*err
;
5951 int lineno
= 0, nprinted
= 0;
5953 size_t linesize
= 0;
5957 struct tog_blame_line
*blame_line
;
5958 struct got_object_id
*prev_id
= NULL
;
5960 struct tog_color
*tc
;
5962 err
= got_object_id_str(&id_str
, &s
->blamed_commit
->id
);
5967 werase(view
->window
);
5969 if (asprintf(&line
, "commit %s", id_str
) == -1) {
5970 err
= got_error_from_errno("asprintf");
5975 err
= format_line(&wline
, &width
, NULL
, line
, 0, view
->ncols
, 0, 0);
5980 if (view_needs_focus_indication(view
))
5981 wstandout(view
->window
);
5982 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
5984 wattr_on(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
5985 waddwstr(view
->window
, wline
);
5986 while (width
++ < view
->ncols
)
5987 waddch(view
->window
, ' ');
5989 wattr_off(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
5990 if (view_needs_focus_indication(view
))
5991 wstandend(view
->window
);
5995 if (view
->gline
> blame
->nlines
)
5996 view
->gline
= blame
->nlines
;
5998 if (asprintf(&line
, "[%d/%d] %s%s", view
->gline
? view
->gline
:
5999 s
->first_displayed_line
- 1 + s
->selected_line
, blame
->nlines
,
6000 s
->blame_complete
? "" : "annotating... ", s
->path
) == -1) {
6002 return got_error_from_errno("asprintf");
6005 err
= format_line(&wline
, &width
, NULL
, line
, 0, view
->ncols
, 0, 0);
6010 waddwstr(view
->window
, wline
);
6013 if (width
< view
->ncols
- 1)
6014 waddch(view
->window
, '\n');
6018 while (nprinted
< view
->nlines
- 2) {
6019 linelen
= getline(&line
, &linesize
, blame
->f
);
6020 if (linelen
== -1) {
6021 if (feof(blame
->f
)) {
6026 return got_ferror(blame
->f
, GOT_ERR_IO
);
6028 if (++lineno
< s
->first_displayed_line
)
6030 if (view
->gline
&& !gotoline(view
, &lineno
, &nprinted
))
6033 /* Set view->maxx based on full line length. */
6034 err
= format_line(&wline
, &width
, NULL
, line
, 0, INT_MAX
, 9, 1);
6041 view
->maxx
= MAX(view
->maxx
, width
);
6043 if (nprinted
== s
->selected_line
- 1)
6044 wstandout(view
->window
);
6046 if (blame
->nlines
> 0) {
6047 blame_line
= &blame
->lines
[lineno
- 1];
6048 if (blame_line
->annotated
&& prev_id
&&
6049 got_object_id_cmp(prev_id
, blame_line
->id
) == 0 &&
6050 !(nprinted
== s
->selected_line
- 1)) {
6051 waddstr(view
->window
, " ");
6052 } else if (blame_line
->annotated
) {
6054 err
= got_object_id_str(&id_str
,
6060 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
6062 wattr_on(view
->window
,
6063 COLOR_PAIR(tc
->colorpair
), NULL
);
6064 wprintw(view
->window
, "%.8s", id_str
);
6066 wattr_off(view
->window
,
6067 COLOR_PAIR(tc
->colorpair
), NULL
);
6069 prev_id
= blame_line
->id
;
6071 waddstr(view
->window
, "........");
6075 waddstr(view
->window
, "........");
6079 if (nprinted
== s
->selected_line
- 1)
6080 wstandend(view
->window
);
6081 waddstr(view
->window
, " ");
6083 if (view
->ncols
<= 9) {
6085 } else if (s
->first_displayed_line
+ nprinted
==
6087 regmatch
->rm_so
>= 0 && regmatch
->rm_so
< regmatch
->rm_eo
) {
6088 err
= add_matched_line(&width
, line
, view
->ncols
- 9, 9,
6089 view
->window
, view
->x
, regmatch
);
6097 err
= format_line(&wline
, &width
, &skip
, line
,
6098 view
->x
, view
->ncols
- 9, 9, 1);
6103 waddwstr(view
->window
, &wline
[skip
]);
6109 if (width
<= view
->ncols
- 1)
6110 waddch(view
->window
, '\n');
6111 if (++nprinted
== 1)
6112 s
->first_displayed_line
= lineno
;
6115 s
->last_displayed_line
= lineno
;
6122 static const struct got_error
*
6123 blame_cb(void *arg
, int nlines
, int lineno
,
6124 struct got_commit_object
*commit
, struct got_object_id
*id
)
6126 const struct got_error
*err
= NULL
;
6127 struct tog_blame_cb_args
*a
= arg
;
6128 struct tog_blame_line
*line
;
6131 if (nlines
!= a
->nlines
||
6132 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
6133 return got_error(GOT_ERR_RANGE
);
6135 errcode
= pthread_mutex_lock(&tog_mutex
);
6137 return got_error_set_errno(errcode
, "pthread_mutex_lock");
6139 if (*a
->quit
) { /* user has quit the blame view */
6140 err
= got_error(GOT_ERR_ITER_COMPLETED
);
6145 goto done
; /* no change in this commit */
6147 line
= &a
->lines
[lineno
- 1];
6148 if (line
->annotated
)
6151 line
->id
= got_object_id_dup(id
);
6152 if (line
->id
== NULL
) {
6153 err
= got_error_from_errno("got_object_id_dup");
6156 line
->annotated
= 1;
6158 errcode
= pthread_mutex_unlock(&tog_mutex
);
6160 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
6165 blame_thread(void *arg
)
6167 const struct got_error
*err
, *close_err
;
6168 struct tog_blame_thread_args
*ta
= arg
;
6169 struct tog_blame_cb_args
*a
= ta
->cb_args
;
6170 int errcode
, fd1
= -1, fd2
= -1;
6171 FILE *f1
= NULL
, *f2
= NULL
;
6173 fd1
= got_opentempfd();
6175 return (void *)got_error_from_errno("got_opentempfd");
6177 fd2
= got_opentempfd();
6179 err
= got_error_from_errno("got_opentempfd");
6183 f1
= got_opentemp();
6185 err
= (void *)got_error_from_errno("got_opentemp");
6188 f2
= got_opentemp();
6190 err
= (void *)got_error_from_errno("got_opentemp");
6194 err
= block_signals_used_by_main_thread();
6198 err
= got_blame(ta
->path
, a
->commit_id
, ta
->repo
,
6199 tog_diff_algo
, blame_cb
, ta
->cb_args
,
6200 ta
->cancel_cb
, ta
->cancel_arg
, fd1
, fd2
, f1
, f2
);
6201 if (err
&& err
->code
== GOT_ERR_CANCELLED
)
6204 errcode
= pthread_mutex_lock(&tog_mutex
);
6206 err
= got_error_set_errno(errcode
, "pthread_mutex_lock");
6210 close_err
= got_repo_close(ta
->repo
);
6216 errcode
= pthread_mutex_unlock(&tog_mutex
);
6217 if (errcode
&& err
== NULL
)
6218 err
= got_error_set_errno(errcode
, "pthread_mutex_unlock");
6221 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
6222 err
= got_error_from_errno("close");
6223 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
6224 err
= got_error_from_errno("close");
6225 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
6226 err
= got_error_from_errno("fclose");
6227 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
6228 err
= got_error_from_errno("fclose");
6233 static struct got_object_id
*
6234 get_selected_commit_id(struct tog_blame_line
*lines
, int nlines
,
6235 int first_displayed_line
, int selected_line
)
6237 struct tog_blame_line
*line
;
6242 line
= &lines
[first_displayed_line
- 1 + selected_line
- 1];
6243 if (!line
->annotated
)
6249 static struct got_object_id
*
6250 get_annotation_for_line(struct tog_blame_line
*lines
, int nlines
,
6253 struct tog_blame_line
*line
;
6255 if (nlines
<= 0 || lineno
>= nlines
)
6258 line
= &lines
[lineno
- 1];
6259 if (!line
->annotated
)
6265 static const struct got_error
*
6266 stop_blame(struct tog_blame
*blame
)
6268 const struct got_error
*err
= NULL
;
6271 if (blame
->thread
) {
6273 errcode
= pthread_mutex_unlock(&tog_mutex
);
6275 return got_error_set_errno(errcode
,
6276 "pthread_mutex_unlock");
6277 errcode
= pthread_join(blame
->thread
, (void **)&err
);
6279 return got_error_set_errno(errcode
, "pthread_join");
6280 errcode
= pthread_mutex_lock(&tog_mutex
);
6282 return got_error_set_errno(errcode
,
6283 "pthread_mutex_lock");
6284 if (err
&& err
->code
== GOT_ERR_ITER_COMPLETED
)
6286 blame
->thread
= 0; //NULL;
6288 if (blame
->thread_args
.repo
) {
6289 const struct got_error
*close_err
;
6290 close_err
= got_repo_close(blame
->thread_args
.repo
);
6293 blame
->thread_args
.repo
= NULL
;
6296 if (fclose(blame
->f
) == EOF
&& err
== NULL
)
6297 err
= got_error_from_errno("fclose");
6301 for (i
= 0; i
< blame
->nlines
; i
++)
6302 free(blame
->lines
[i
].id
);
6304 blame
->lines
= NULL
;
6306 free(blame
->cb_args
.commit_id
);
6307 blame
->cb_args
.commit_id
= NULL
;
6308 if (blame
->pack_fds
) {
6309 const struct got_error
*pack_err
=
6310 got_repo_pack_fds_close(blame
->pack_fds
);
6313 blame
->pack_fds
= NULL
;
6318 static const struct got_error
*
6319 cancel_blame_view(void *arg
)
6321 const struct got_error
*err
= NULL
;
6325 errcode
= pthread_mutex_lock(&tog_mutex
);
6327 return got_error_set_errno(errcode
,
6328 "pthread_mutex_unlock");
6331 err
= got_error(GOT_ERR_CANCELLED
);
6333 errcode
= pthread_mutex_unlock(&tog_mutex
);
6335 return got_error_set_errno(errcode
,
6336 "pthread_mutex_lock");
6341 static const struct got_error
*
6342 run_blame(struct tog_view
*view
)
6344 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6345 struct tog_blame
*blame
= &s
->blame
;
6346 const struct got_error
*err
= NULL
;
6347 struct got_commit_object
*commit
= NULL
;
6348 struct got_blob_object
*blob
= NULL
;
6349 struct got_repository
*thread_repo
= NULL
;
6350 struct got_object_id
*obj_id
= NULL
;
6351 int obj_type
, fd
= -1;
6352 int *pack_fds
= NULL
;
6354 err
= got_object_open_as_commit(&commit
, s
->repo
,
6355 &s
->blamed_commit
->id
);
6359 fd
= got_opentempfd();
6361 err
= got_error_from_errno("got_opentempfd");
6365 err
= got_object_id_by_path(&obj_id
, s
->repo
, commit
, s
->path
);
6369 err
= got_object_get_type(&obj_type
, s
->repo
, obj_id
);
6373 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
6374 err
= got_error(GOT_ERR_OBJ_TYPE
);
6378 err
= got_object_open_as_blob(&blob
, s
->repo
, obj_id
, 8192, fd
);
6381 blame
->f
= got_opentemp();
6382 if (blame
->f
== NULL
) {
6383 err
= got_error_from_errno("got_opentemp");
6386 err
= got_object_blob_dump_to_file(&blame
->filesize
, &blame
->nlines
,
6387 &blame
->line_offsets
, blame
->f
, blob
);
6390 if (blame
->nlines
== 0) {
6391 s
->blame_complete
= 1;
6395 /* Don't include \n at EOF in the blame line count. */
6396 if (blame
->line_offsets
[blame
->nlines
- 1] == blame
->filesize
)
6399 blame
->lines
= calloc(blame
->nlines
, sizeof(*blame
->lines
));
6400 if (blame
->lines
== NULL
) {
6401 err
= got_error_from_errno("calloc");
6405 err
= got_repo_pack_fds_open(&pack_fds
);
6408 err
= got_repo_open(&thread_repo
, got_repo_get_path(s
->repo
), NULL
,
6413 blame
->pack_fds
= pack_fds
;
6414 blame
->cb_args
.view
= view
;
6415 blame
->cb_args
.lines
= blame
->lines
;
6416 blame
->cb_args
.nlines
= blame
->nlines
;
6417 blame
->cb_args
.commit_id
= got_object_id_dup(&s
->blamed_commit
->id
);
6418 if (blame
->cb_args
.commit_id
== NULL
) {
6419 err
= got_error_from_errno("got_object_id_dup");
6422 blame
->cb_args
.quit
= &s
->done
;
6424 blame
->thread_args
.path
= s
->path
;
6425 blame
->thread_args
.repo
= thread_repo
;
6426 blame
->thread_args
.cb_args
= &blame
->cb_args
;
6427 blame
->thread_args
.complete
= &s
->blame_complete
;
6428 blame
->thread_args
.cancel_cb
= cancel_blame_view
;
6429 blame
->thread_args
.cancel_arg
= &s
->done
;
6430 s
->blame_complete
= 0;
6432 if (s
->first_displayed_line
+ view
->nlines
- 1 > blame
->nlines
) {
6433 s
->first_displayed_line
= 1;
6434 s
->last_displayed_line
= view
->nlines
;
6435 s
->selected_line
= 1;
6437 s
->matched_line
= 0;
6441 got_object_commit_close(commit
);
6442 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
6443 err
= got_error_from_errno("close");
6445 got_object_blob_close(blob
);
6452 static const struct got_error
*
6453 open_blame_view(struct tog_view
*view
, char *path
,
6454 struct got_object_id
*commit_id
, struct got_repository
*repo
)
6456 const struct got_error
*err
= NULL
;
6457 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6459 STAILQ_INIT(&s
->blamed_commits
);
6461 s
->path
= strdup(path
);
6462 if (s
->path
== NULL
)
6463 return got_error_from_errno("strdup");
6465 err
= got_object_qid_alloc(&s
->blamed_commit
, commit_id
);
6471 STAILQ_INSERT_HEAD(&s
->blamed_commits
, s
->blamed_commit
, entry
);
6472 s
->first_displayed_line
= 1;
6473 s
->last_displayed_line
= view
->nlines
;
6474 s
->selected_line
= 1;
6475 s
->blame_complete
= 0;
6477 s
->commit_id
= commit_id
;
6478 memset(&s
->blame
, 0, sizeof(s
->blame
));
6480 STAILQ_INIT(&s
->colors
);
6481 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
6482 err
= add_color(&s
->colors
, "^", TOG_COLOR_COMMIT
,
6483 get_color_value("TOG_COLOR_COMMIT"));
6488 view
->show
= show_blame_view
;
6489 view
->input
= input_blame_view
;
6490 view
->reset
= reset_blame_view
;
6491 view
->close
= close_blame_view
;
6492 view
->search_start
= search_start_blame_view
;
6493 view
->search_setup
= search_setup_blame_view
;
6494 view
->search_next
= search_next_view_match
;
6496 return run_blame(view
);
6499 static const struct got_error
*
6500 close_blame_view(struct tog_view
*view
)
6502 const struct got_error
*err
= NULL
;
6503 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6505 if (s
->blame
.thread
)
6506 err
= stop_blame(&s
->blame
);
6508 while (!STAILQ_EMPTY(&s
->blamed_commits
)) {
6509 struct got_object_qid
*blamed_commit
;
6510 blamed_commit
= STAILQ_FIRST(&s
->blamed_commits
);
6511 STAILQ_REMOVE_HEAD(&s
->blamed_commits
, entry
);
6512 got_object_qid_free(blamed_commit
);
6516 free_colors(&s
->colors
);
6520 static const struct got_error
*
6521 search_start_blame_view(struct tog_view
*view
)
6523 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6525 s
->matched_line
= 0;
6530 search_setup_blame_view(struct tog_view
*view
, FILE **f
, off_t
**line_offsets
,
6531 size_t *nlines
, int **first
, int **last
, int **match
, int **selected
)
6533 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6536 *nlines
= s
->blame
.nlines
;
6537 *line_offsets
= s
->blame
.line_offsets
;
6538 *match
= &s
->matched_line
;
6539 *first
= &s
->first_displayed_line
;
6540 *last
= &s
->last_displayed_line
;
6541 *selected
= &s
->selected_line
;
6544 static const struct got_error
*
6545 show_blame_view(struct tog_view
*view
)
6547 const struct got_error
*err
= NULL
;
6548 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6551 if (s
->blame
.thread
== 0 && !s
->blame_complete
) {
6552 errcode
= pthread_create(&s
->blame
.thread
, NULL
, blame_thread
,
6553 &s
->blame
.thread_args
);
6555 return got_error_set_errno(errcode
, "pthread_create");
6558 halfdelay(1); /* fast refresh while annotating */
6561 if (s
->blame_complete
&& !using_mock_io
)
6562 halfdelay(10); /* disable fast refresh */
6564 err
= draw_blame(view
);
6570 static const struct got_error
*
6571 log_annotated_line(struct tog_view
**new_view
, int begin_y
, int begin_x
,
6572 struct got_repository
*repo
, struct got_object_id
*id
)
6574 struct tog_view
*log_view
;
6575 const struct got_error
*err
= NULL
;
6579 log_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_LOG
);
6580 if (log_view
== NULL
)
6581 return got_error_from_errno("view_open");
6583 err
= open_log_view(log_view
, id
, repo
, GOT_REF_HEAD
, "", 0);
6585 view_close(log_view
);
6587 *new_view
= log_view
;
6592 static const struct got_error
*
6593 input_blame_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
6595 const struct got_error
*err
= NULL
, *thread_err
= NULL
;
6596 struct tog_view
*diff_view
;
6597 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6598 int eos
, nscroll
, begin_y
= 0, begin_x
= 0;
6600 eos
= nscroll
= view
->nlines
- 2;
6601 if (view_is_hsplit_top(view
))
6611 horizontal_scroll_input(view
, ch
);
6618 s
->selected_line
= 1;
6619 s
->first_displayed_line
= 1;
6624 if (s
->blame
.nlines
< eos
) {
6625 s
->selected_line
= s
->blame
.nlines
;
6626 s
->first_displayed_line
= 1;
6628 s
->selected_line
= eos
;
6629 s
->first_displayed_line
= s
->blame
.nlines
- (eos
- 1);
6636 if (s
->selected_line
> 1)
6638 else if (s
->selected_line
== 1 &&
6639 s
->first_displayed_line
> 1)
6640 s
->first_displayed_line
--;
6651 if (s
->first_displayed_line
== 1) {
6652 if (view
->count
> 1)
6654 s
->selected_line
= MAX(1, s
->selected_line
- nscroll
);
6658 if (s
->first_displayed_line
> nscroll
)
6659 s
->first_displayed_line
-= nscroll
;
6661 s
->first_displayed_line
= 1;
6666 if (s
->selected_line
< eos
&& s
->first_displayed_line
+
6667 s
->selected_line
<= s
->blame
.nlines
)
6669 else if (s
->first_displayed_line
< s
->blame
.nlines
- (eos
- 1))
6670 s
->first_displayed_line
++;
6676 struct got_object_id
*id
= NULL
;
6679 id
= get_selected_commit_id(s
->blame
.lines
, s
->blame
.nlines
,
6680 s
->first_displayed_line
, s
->selected_line
);
6684 struct got_commit_object
*commit
, *pcommit
;
6685 struct got_object_qid
*pid
;
6686 struct got_object_id
*blob_id
= NULL
;
6688 err
= got_object_open_as_commit(&commit
,
6693 got_object_commit_get_parent_ids(commit
));
6695 got_object_commit_close(commit
);
6698 /* Check if path history ends here. */
6699 err
= got_object_open_as_commit(&pcommit
,
6703 err
= got_object_id_by_path(&blob_id
, s
->repo
,
6705 got_object_commit_close(pcommit
);
6707 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
)
6709 got_object_commit_close(commit
);
6712 err
= got_object_get_type(&obj_type
, s
->repo
,
6715 /* Can't blame non-blob type objects. */
6716 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
6717 got_object_commit_close(commit
);
6720 err
= got_object_qid_alloc(&s
->blamed_commit
,
6722 got_object_commit_close(commit
);
6724 if (got_object_id_cmp(id
,
6725 &s
->blamed_commit
->id
) == 0)
6727 err
= got_object_qid_alloc(&s
->blamed_commit
,
6733 thread_err
= stop_blame(&s
->blame
);
6737 STAILQ_INSERT_HEAD(&s
->blamed_commits
,
6738 s
->blamed_commit
, entry
);
6739 err
= run_blame(view
);
6745 struct got_object_qid
*first
;
6748 first
= STAILQ_FIRST(&s
->blamed_commits
);
6749 if (!got_object_id_cmp(&first
->id
, s
->commit_id
))
6752 thread_err
= stop_blame(&s
->blame
);
6756 STAILQ_REMOVE_HEAD(&s
->blamed_commits
, entry
);
6757 got_object_qid_free(s
->blamed_commit
);
6759 STAILQ_FIRST(&s
->blamed_commits
);
6760 err
= run_blame(view
);
6767 s
->id_to_log
= get_selected_commit_id(s
->blame
.lines
,
6768 s
->blame
.nlines
, s
->first_displayed_line
, s
->selected_line
);
6770 err
= view_request_new(new_view
, view
, TOG_VIEW_LOG
);
6774 struct got_object_id
*id
= NULL
;
6775 struct got_object_qid
*pid
;
6776 struct got_commit_object
*commit
= NULL
;
6779 id
= get_selected_commit_id(s
->blame
.lines
, s
->blame
.nlines
,
6780 s
->first_displayed_line
, s
->selected_line
);
6783 err
= got_object_open_as_commit(&commit
, s
->repo
, id
);
6786 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
6788 /* traversed from diff view, release diff resources */
6789 err
= close_diff_view(*new_view
);
6792 diff_view
= *new_view
;
6794 if (view_is_parent_view(view
))
6795 view_get_split(view
, &begin_y
, &begin_x
);
6797 diff_view
= view_open(0, 0, begin_y
, begin_x
,
6799 if (diff_view
== NULL
) {
6800 got_object_commit_close(commit
);
6801 err
= got_error_from_errno("view_open");
6805 err
= open_diff_view(diff_view
, pid
? &pid
->id
: NULL
,
6806 id
, NULL
, NULL
, 3, 0, 0, view
, s
->repo
);
6807 got_object_commit_close(commit
);
6809 view_close(diff_view
);
6812 s
->last_diffed_line
= s
->first_displayed_line
- 1 +
6815 break; /* still open from active diff view */
6816 if (view_is_parent_view(view
) &&
6817 view
->mode
== TOG_VIEW_SPLIT_HRZN
) {
6818 err
= view_init_hsplit(view
, begin_y
);
6824 diff_view
->focussed
= 1;
6825 diff_view
->mode
= view
->mode
;
6826 diff_view
->nlines
= view
->lines
- begin_y
;
6827 if (view_is_parent_view(view
)) {
6828 view_transfer_size(diff_view
, view
);
6829 err
= view_close_child(view
);
6832 err
= view_set_child(view
, diff_view
);
6835 view
->focus_child
= 1;
6837 *new_view
= diff_view
;
6850 if (s
->last_displayed_line
>= s
->blame
.nlines
&&
6851 s
->selected_line
>= MIN(s
->blame
.nlines
,
6852 view
->nlines
- 2)) {
6856 if (s
->last_displayed_line
>= s
->blame
.nlines
&&
6857 s
->selected_line
< view
->nlines
- 2) {
6859 MIN(nscroll
, s
->last_displayed_line
-
6860 s
->first_displayed_line
- s
->selected_line
+ 1);
6862 if (s
->last_displayed_line
+ nscroll
<= s
->blame
.nlines
)
6863 s
->first_displayed_line
+= nscroll
;
6865 s
->first_displayed_line
=
6866 s
->blame
.nlines
- (view
->nlines
- 3);
6869 if (s
->selected_line
> view
->nlines
- 2) {
6870 s
->selected_line
= MIN(s
->blame
.nlines
,
6878 return thread_err
? thread_err
: err
;
6881 static const struct got_error
*
6882 reset_blame_view(struct tog_view
*view
)
6884 const struct got_error
*err
;
6885 struct tog_blame_view_state
*s
= &view
->state
.blame
;
6889 err
= stop_blame(&s
->blame
);
6893 return run_blame(view
);
6896 static const struct got_error
*
6897 cmd_blame(int argc
, char *argv
[])
6899 const struct got_error
*error
;
6900 struct got_repository
*repo
= NULL
;
6901 struct got_worktree
*worktree
= NULL
;
6902 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
6903 char *link_target
= NULL
;
6904 struct got_object_id
*commit_id
= NULL
;
6905 struct got_commit_object
*commit
= NULL
;
6906 char *commit_id_str
= NULL
;
6908 struct tog_view
*view
= NULL
;
6909 int *pack_fds
= NULL
;
6911 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
6914 commit_id_str
= optarg
;
6917 repo_path
= realpath(optarg
, NULL
);
6918 if (repo_path
== NULL
)
6919 return got_error_from_errno2("realpath",
6934 error
= got_repo_pack_fds_open(&pack_fds
);
6938 if (repo_path
== NULL
) {
6939 cwd
= getcwd(NULL
, 0);
6941 return got_error_from_errno("getcwd");
6942 error
= got_worktree_open(&worktree
, cwd
);
6943 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6947 strdup(got_worktree_get_repo_path(worktree
));
6949 repo_path
= strdup(cwd
);
6950 if (repo_path
== NULL
) {
6951 error
= got_error_from_errno("strdup");
6956 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
6960 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
, repo
,
6967 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
6971 error
= tog_load_refs(repo
, 0);
6975 if (commit_id_str
== NULL
) {
6976 struct got_reference
*head_ref
;
6977 error
= got_ref_open(&head_ref
, repo
, worktree
?
6978 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
, 0);
6981 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
6982 got_ref_close(head_ref
);
6984 error
= got_repo_match_object_id(&commit_id
, NULL
,
6985 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
6990 view
= view_open(0, 0, 0, 0, TOG_VIEW_BLAME
);
6992 error
= got_error_from_errno("view_open");
6996 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
7000 error
= got_object_resolve_symlinks(&link_target
, in_repo_path
,
7005 error
= open_blame_view(view
, link_target
? link_target
: in_repo_path
,
7010 /* Release work tree lock. */
7011 got_worktree_close(worktree
);
7014 error
= view_loop(view
);
7021 if (error
!= NULL
&& view
!= NULL
) {
7022 if (view
->close
== NULL
)
7023 close_blame_view(view
);
7027 got_object_commit_close(commit
);
7029 got_worktree_close(worktree
);
7031 const struct got_error
*close_err
= got_repo_close(repo
);
7036 const struct got_error
*pack_err
=
7037 got_repo_pack_fds_close(pack_fds
);
7045 static const struct got_error
*
7046 draw_tree_entries(struct tog_view
*view
, const char *parent_path
)
7048 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7049 const struct got_error
*err
= NULL
;
7050 struct got_tree_entry
*te
;
7053 struct tog_color
*tc
;
7054 int width
, n
, nentries
, scrollx
, i
= 1;
7055 int limit
= view
->nlines
;
7058 if (view_is_hsplit_top(view
))
7059 --limit
; /* border */
7061 werase(view
->window
);
7066 err
= format_line(&wline
, &width
, NULL
, s
->tree_label
, 0, view
->ncols
,
7070 if (view_needs_focus_indication(view
))
7071 wstandout(view
->window
);
7072 tc
= get_color(&s
->colors
, TOG_COLOR_COMMIT
);
7074 wattr_on(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
7075 waddwstr(view
->window
, wline
);
7078 while (width
++ < view
->ncols
)
7079 waddch(view
->window
, ' ');
7081 wattr_off(view
->window
, COLOR_PAIR(tc
->colorpair
), NULL
);
7082 if (view_needs_focus_indication(view
))
7083 wstandend(view
->window
);
7088 if (s
->first_displayed_entry
) {
7089 i
+= got_tree_entry_get_index(s
->first_displayed_entry
);
7090 if (s
->tree
!= s
->root
)
7091 ++i
; /* account for ".." entry */
7093 nentries
= got_object_tree_get_nentries(s
->tree
);
7094 if (asprintf(&index
, "[%d/%d] %s",
7095 i
, nentries
+ (s
->tree
== s
->root
? 0 : 1), parent_path
) == -1)
7096 return got_error_from_errno("asprintf");
7097 err
= format_line(&wline
, &width
, NULL
, index
, 0, view
->ncols
, 0, 0);
7101 waddwstr(view
->window
, wline
);
7104 if (width
< view
->ncols
- 1)
7105 waddch(view
->window
, '\n');
7108 waddch(view
->window
, '\n');
7112 if (s
->first_displayed_entry
== NULL
) {
7113 te
= got_object_tree_get_first_entry(s
->tree
);
7114 if (s
->selected
== 0) {
7116 wstandout(view
->window
);
7117 s
->selected_entry
= NULL
;
7119 waddstr(view
->window
, " ..\n"); /* parent directory */
7120 if (s
->selected
== 0 && view
->focussed
)
7121 wstandend(view
->window
);
7128 te
= s
->first_displayed_entry
;
7132 for (i
= got_tree_entry_get_index(te
); i
< nentries
; i
++) {
7133 char *line
= NULL
, *id_str
= NULL
, *link_target
= NULL
;
7134 const char *modestr
= "";
7137 te
= got_object_tree_get_entry(s
->tree
, i
);
7138 mode
= got_tree_entry_get_mode(te
);
7141 err
= got_object_id_str(&id_str
,
7142 got_tree_entry_get_id(te
));
7144 return got_error_from_errno(
7145 "got_object_id_str");
7147 if (got_object_tree_entry_is_submodule(te
))
7149 else if (S_ISLNK(mode
)) {
7152 err
= got_tree_entry_get_symlink_target(&link_target
,
7158 for (i
= 0; i
< strlen(link_target
); i
++) {
7159 if (!isprint((unsigned char)link_target
[i
]))
7160 link_target
[i
] = '?';
7164 else if (S_ISDIR(mode
))
7166 else if (mode
& S_IXUSR
)
7168 if (asprintf(&line
, "%s %s%s%s%s", id_str
? id_str
: "",
7169 got_tree_entry_get_name(te
), modestr
,
7170 link_target
? " -> ": "",
7171 link_target
? link_target
: "") == -1) {
7174 return got_error_from_errno("asprintf");
7179 /* use full line width to determine view->maxx */
7180 err
= format_line(&wline
, &width
, NULL
, line
, 0, INT_MAX
, 0, 0);
7185 view
->maxx
= MAX(view
->maxx
, width
);
7189 err
= format_line(&wline
, &width
, &scrollx
, line
, view
->x
,
7195 if (n
== s
->selected
) {
7197 wstandout(view
->window
);
7198 s
->selected_entry
= te
;
7200 tc
= match_color(&s
->colors
, line
);
7202 wattr_on(view
->window
,
7203 COLOR_PAIR(tc
->colorpair
), NULL
);
7204 waddwstr(view
->window
, &wline
[scrollx
]);
7206 wattr_off(view
->window
,
7207 COLOR_PAIR(tc
->colorpair
), NULL
);
7208 if (width
< view
->ncols
)
7209 waddch(view
->window
, '\n');
7210 if (n
== s
->selected
&& view
->focussed
)
7211 wstandend(view
->window
);
7217 s
->last_displayed_entry
= te
;
7226 tree_scroll_up(struct tog_tree_view_state
*s
, int maxscroll
)
7228 struct got_tree_entry
*te
;
7229 int isroot
= s
->tree
== s
->root
;
7232 if (s
->first_displayed_entry
== NULL
)
7235 te
= got_tree_entry_get_prev(s
->tree
, s
->first_displayed_entry
);
7236 while (i
++ < maxscroll
) {
7239 s
->first_displayed_entry
= NULL
;
7242 s
->first_displayed_entry
= te
;
7243 te
= got_tree_entry_get_prev(s
->tree
, te
);
7247 static const struct got_error
*
7248 tree_scroll_down(struct tog_view
*view
, int maxscroll
)
7250 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7251 struct got_tree_entry
*next
, *last
;
7254 if (s
->first_displayed_entry
)
7255 next
= got_tree_entry_get_next(s
->tree
,
7256 s
->first_displayed_entry
);
7258 next
= got_object_tree_get_first_entry(s
->tree
);
7260 last
= s
->last_displayed_entry
;
7261 while (next
&& n
++ < maxscroll
) {
7263 s
->last_displayed_entry
= last
;
7264 last
= got_tree_entry_get_next(s
->tree
, last
);
7266 if (last
|| (view
->mode
== TOG_VIEW_SPLIT_HRZN
&& next
)) {
7267 s
->first_displayed_entry
= next
;
7268 next
= got_tree_entry_get_next(s
->tree
, next
);
7275 static const struct got_error
*
7276 tree_entry_path(char **path
, struct tog_parent_trees
*parents
,
7277 struct got_tree_entry
*te
)
7279 const struct got_error
*err
= NULL
;
7280 struct tog_parent_tree
*pt
;
7281 size_t len
= 2; /* for leading slash and NUL */
7283 TAILQ_FOREACH(pt
, parents
, entry
)
7284 len
+= strlen(got_tree_entry_get_name(pt
->selected_entry
))
7287 len
+= strlen(got_tree_entry_get_name(te
));
7289 *path
= calloc(1, len
);
7291 return got_error_from_errno("calloc");
7294 pt
= TAILQ_LAST(parents
, tog_parent_trees
);
7296 const char *name
= got_tree_entry_get_name(pt
->selected_entry
);
7297 if (strlcat(*path
, name
, len
) >= len
) {
7298 err
= got_error(GOT_ERR_NO_SPACE
);
7301 if (strlcat(*path
, "/", len
) >= len
) {
7302 err
= got_error(GOT_ERR_NO_SPACE
);
7305 pt
= TAILQ_PREV(pt
, tog_parent_trees
, entry
);
7308 if (strlcat(*path
, got_tree_entry_get_name(te
), len
) >= len
) {
7309 err
= got_error(GOT_ERR_NO_SPACE
);
7321 static const struct got_error
*
7322 blame_tree_entry(struct tog_view
**new_view
, int begin_y
, int begin_x
,
7323 struct got_tree_entry
*te
, struct tog_parent_trees
*parents
,
7324 struct got_object_id
*commit_id
, struct got_repository
*repo
)
7326 const struct got_error
*err
= NULL
;
7328 struct tog_view
*blame_view
;
7332 err
= tree_entry_path(&path
, parents
, te
);
7336 blame_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_BLAME
);
7337 if (blame_view
== NULL
) {
7338 err
= got_error_from_errno("view_open");
7342 err
= open_blame_view(blame_view
, path
, commit_id
, repo
);
7344 if (err
->code
== GOT_ERR_CANCELLED
)
7346 view_close(blame_view
);
7348 *new_view
= blame_view
;
7354 static const struct got_error
*
7355 log_selected_tree_entry(struct tog_view
**new_view
, int begin_y
, int begin_x
,
7356 struct tog_tree_view_state
*s
)
7358 struct tog_view
*log_view
;
7359 const struct got_error
*err
= NULL
;
7364 log_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_LOG
);
7365 if (log_view
== NULL
)
7366 return got_error_from_errno("view_open");
7368 err
= tree_entry_path(&path
, &s
->parents
, s
->selected_entry
);
7372 err
= open_log_view(log_view
, s
->commit_id
, s
->repo
, s
->head_ref_name
,
7375 view_close(log_view
);
7377 *new_view
= log_view
;
7382 static const struct got_error
*
7383 open_tree_view(struct tog_view
*view
, struct got_object_id
*commit_id
,
7384 const char *head_ref_name
, struct got_repository
*repo
)
7386 const struct got_error
*err
= NULL
;
7387 char *commit_id_str
= NULL
;
7388 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7389 struct got_commit_object
*commit
= NULL
;
7391 TAILQ_INIT(&s
->parents
);
7392 STAILQ_INIT(&s
->colors
);
7394 s
->commit_id
= got_object_id_dup(commit_id
);
7395 if (s
->commit_id
== NULL
) {
7396 err
= got_error_from_errno("got_object_id_dup");
7400 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
7405 * The root is opened here and will be closed when the view is closed.
7406 * Any visited subtrees and their path-wise parents are opened and
7409 err
= got_object_open_as_tree(&s
->root
, repo
,
7410 got_object_commit_get_tree_id(commit
));
7415 err
= got_object_id_str(&commit_id_str
, commit_id
);
7419 if (asprintf(&s
->tree_label
, "commit %s", commit_id_str
) == -1) {
7420 err
= got_error_from_errno("asprintf");
7424 s
->first_displayed_entry
= got_object_tree_get_entry(s
->tree
, 0);
7425 s
->selected_entry
= got_object_tree_get_entry(s
->tree
, 0);
7426 if (head_ref_name
) {
7427 s
->head_ref_name
= strdup(head_ref_name
);
7428 if (s
->head_ref_name
== NULL
) {
7429 err
= got_error_from_errno("strdup");
7435 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
7436 err
= add_color(&s
->colors
, "\\$$",
7437 TOG_COLOR_TREE_SUBMODULE
,
7438 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7441 err
= add_color(&s
->colors
, "@$", TOG_COLOR_TREE_SYMLINK
,
7442 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7445 err
= add_color(&s
->colors
, "/$",
7446 TOG_COLOR_TREE_DIRECTORY
,
7447 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7451 err
= add_color(&s
->colors
, "\\*$",
7452 TOG_COLOR_TREE_EXECUTABLE
,
7453 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7457 err
= add_color(&s
->colors
, "^$", TOG_COLOR_COMMIT
,
7458 get_color_value("TOG_COLOR_COMMIT"));
7463 view
->show
= show_tree_view
;
7464 view
->input
= input_tree_view
;
7465 view
->close
= close_tree_view
;
7466 view
->search_start
= search_start_tree_view
;
7467 view
->search_next
= search_next_tree_view
;
7469 free(commit_id_str
);
7471 got_object_commit_close(commit
);
7473 if (view
->close
== NULL
)
7474 close_tree_view(view
);
7480 static const struct got_error
*
7481 close_tree_view(struct tog_view
*view
)
7483 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7485 free_colors(&s
->colors
);
7486 free(s
->tree_label
);
7487 s
->tree_label
= NULL
;
7489 s
->commit_id
= NULL
;
7490 free(s
->head_ref_name
);
7491 s
->head_ref_name
= NULL
;
7492 while (!TAILQ_EMPTY(&s
->parents
)) {
7493 struct tog_parent_tree
*parent
;
7494 parent
= TAILQ_FIRST(&s
->parents
);
7495 TAILQ_REMOVE(&s
->parents
, parent
, entry
);
7496 if (parent
->tree
!= s
->root
)
7497 got_object_tree_close(parent
->tree
);
7501 if (s
->tree
!= NULL
&& s
->tree
!= s
->root
)
7502 got_object_tree_close(s
->tree
);
7504 got_object_tree_close(s
->root
);
7508 static const struct got_error
*
7509 search_start_tree_view(struct tog_view
*view
)
7511 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7513 s
->matched_entry
= NULL
;
7518 match_tree_entry(struct got_tree_entry
*te
, regex_t
*regex
)
7520 regmatch_t regmatch
;
7522 return regexec(regex
, got_tree_entry_get_name(te
), 1, ®match
,
7526 static const struct got_error
*
7527 search_next_tree_view(struct tog_view
*view
)
7529 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7530 struct got_tree_entry
*te
= NULL
;
7532 if (!view
->searching
) {
7533 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
7537 if (s
->matched_entry
) {
7538 if (view
->searching
== TOG_SEARCH_FORWARD
) {
7539 if (s
->selected_entry
)
7540 te
= got_tree_entry_get_next(s
->tree
,
7543 te
= got_object_tree_get_first_entry(s
->tree
);
7545 if (s
->selected_entry
== NULL
)
7546 te
= got_object_tree_get_last_entry(s
->tree
);
7548 te
= got_tree_entry_get_prev(s
->tree
,
7552 if (s
->selected_entry
)
7553 te
= s
->selected_entry
;
7554 else if (view
->searching
== TOG_SEARCH_FORWARD
)
7555 te
= got_object_tree_get_first_entry(s
->tree
);
7557 te
= got_object_tree_get_last_entry(s
->tree
);
7562 if (s
->matched_entry
== NULL
) {
7563 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
7566 if (view
->searching
== TOG_SEARCH_FORWARD
)
7567 te
= got_object_tree_get_first_entry(s
->tree
);
7569 te
= got_object_tree_get_last_entry(s
->tree
);
7572 if (match_tree_entry(te
, &view
->regex
)) {
7573 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
7574 s
->matched_entry
= te
;
7578 if (view
->searching
== TOG_SEARCH_FORWARD
)
7579 te
= got_tree_entry_get_next(s
->tree
, te
);
7581 te
= got_tree_entry_get_prev(s
->tree
, te
);
7584 if (s
->matched_entry
) {
7585 s
->first_displayed_entry
= s
->matched_entry
;
7592 static const struct got_error
*
7593 show_tree_view(struct tog_view
*view
)
7595 const struct got_error
*err
= NULL
;
7596 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7599 err
= tree_entry_path(&parent_path
, &s
->parents
, NULL
);
7603 err
= draw_tree_entries(view
, parent_path
);
7610 static const struct got_error
*
7611 tree_goto_line(struct tog_view
*view
, int nlines
)
7613 const struct got_error
*err
= NULL
;
7614 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7615 struct got_tree_entry
**fte
, **lte
, **ste
;
7616 int g
, last
, first
= 1, i
= 1;
7617 int root
= s
->tree
== s
->root
;
7618 int off
= root
? 1 : 2;
7625 else if (g
> got_object_tree_get_nentries(s
->tree
))
7626 g
= got_object_tree_get_nentries(s
->tree
) + (root
? 0 : 1);
7628 fte
= &s
->first_displayed_entry
;
7629 lte
= &s
->last_displayed_entry
;
7630 ste
= &s
->selected_entry
;
7633 first
= got_tree_entry_get_index(*fte
);
7634 first
+= off
; /* account for ".." */
7636 last
= got_tree_entry_get_index(*lte
);
7639 if (g
>= first
&& g
<= last
&& g
- first
< nlines
) {
7640 s
->selected
= g
- first
;
7641 return NULL
; /* gline is on the current page */
7645 i
= got_tree_entry_get_index(*ste
);
7650 err
= tree_scroll_down(view
, g
- i
);
7653 if (got_tree_entry_get_index(*lte
) >=
7654 got_object_tree_get_nentries(s
->tree
) - 1 &&
7655 first
+ s
->selected
< g
&&
7656 s
->selected
< s
->ndisplayed
- 1) {
7657 first
= got_tree_entry_get_index(*fte
);
7659 s
->selected
= g
- first
;
7662 tree_scroll_up(s
, i
- g
);
7665 (*fte
== NULL
|| (root
&& !got_tree_entry_get_index(*fte
))))
7666 s
->selected
= g
- 1;
7671 static const struct got_error
*
7672 input_tree_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
7674 const struct got_error
*err
= NULL
;
7675 struct tog_tree_view_state
*s
= &view
->state
.tree
;
7676 struct got_tree_entry
*te
;
7677 int n
, nscroll
= view
->nlines
- 3;
7680 return tree_goto_line(view
, nscroll
);
7689 horizontal_scroll_input(view
, ch
);
7692 s
->show_ids
= !s
->show_ids
;
7697 if (!s
->selected_entry
)
7699 err
= view_request_new(new_view
, view
, TOG_VIEW_LOG
);
7703 err
= view_request_new(new_view
, view
, TOG_VIEW_REF
);
7710 if (s
->tree
== s
->root
)
7711 s
->first_displayed_entry
=
7712 got_object_tree_get_first_entry(s
->tree
);
7714 s
->first_displayed_entry
= NULL
;
7719 int eos
= view
->nlines
- 3;
7721 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
)
7725 te
= got_object_tree_get_last_entry(s
->tree
);
7726 for (n
= 0; n
< eos
; n
++) {
7728 if (s
->tree
!= s
->root
) {
7729 s
->first_displayed_entry
= NULL
;
7734 s
->first_displayed_entry
= te
;
7735 te
= got_tree_entry_get_prev(s
->tree
, te
);
7738 s
->selected
= n
- 1;
7744 if (s
->selected
> 0) {
7748 tree_scroll_up(s
, 1);
7749 if (s
->selected_entry
== NULL
||
7750 (s
->tree
== s
->root
&& s
->selected_entry
==
7751 got_object_tree_get_first_entry(s
->tree
)))
7761 if (s
->tree
== s
->root
) {
7762 if (got_object_tree_get_first_entry(s
->tree
) ==
7763 s
->first_displayed_entry
)
7764 s
->selected
-= MIN(s
->selected
, nscroll
);
7766 if (s
->first_displayed_entry
== NULL
)
7767 s
->selected
-= MIN(s
->selected
, nscroll
);
7769 tree_scroll_up(s
, MAX(0, nscroll
));
7770 if (s
->selected_entry
== NULL
||
7771 (s
->tree
== s
->root
&& s
->selected_entry
==
7772 got_object_tree_get_first_entry(s
->tree
)))
7778 if (s
->selected
< s
->ndisplayed
- 1) {
7782 if (got_tree_entry_get_next(s
->tree
, s
->last_displayed_entry
)
7784 /* can't scroll any further */
7788 tree_scroll_down(view
, 1);
7798 if (got_tree_entry_get_next(s
->tree
, s
->last_displayed_entry
)
7800 /* can't scroll any further; move cursor down */
7801 if (s
->selected
< s
->ndisplayed
- 1)
7802 s
->selected
+= MIN(nscroll
,
7803 s
->ndisplayed
- s
->selected
- 1);
7808 tree_scroll_down(view
, nscroll
);
7813 if (s
->selected_entry
== NULL
|| ch
== KEY_BACKSPACE
) {
7814 struct tog_parent_tree
*parent
;
7815 /* user selected '..' */
7816 if (s
->tree
== s
->root
) {
7820 parent
= TAILQ_FIRST(&s
->parents
);
7821 TAILQ_REMOVE(&s
->parents
, parent
,
7823 got_object_tree_close(s
->tree
);
7824 s
->tree
= parent
->tree
;
7825 s
->first_displayed_entry
=
7826 parent
->first_displayed_entry
;
7828 parent
->selected_entry
;
7829 s
->selected
= parent
->selected
;
7830 if (s
->selected
> view
->nlines
- 3) {
7831 err
= offset_selection_down(view
);
7836 } else if (S_ISDIR(got_tree_entry_get_mode(
7837 s
->selected_entry
))) {
7838 struct got_tree_object
*subtree
;
7840 err
= got_object_open_as_tree(&subtree
, s
->repo
,
7841 got_tree_entry_get_id(s
->selected_entry
));
7844 err
= tree_view_visit_subtree(s
, subtree
);
7846 got_object_tree_close(subtree
);
7849 } else if (S_ISREG(got_tree_entry_get_mode(s
->selected_entry
)))
7850 err
= view_request_new(new_view
, view
, TOG_VIEW_BLAME
);
7853 if (view
->nlines
>= 4 && s
->selected
>= view
->nlines
- 3)
7854 s
->selected
= view
->nlines
- 4;
7870 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7875 static const struct got_error
*
7876 cmd_tree(int argc
, char *argv
[])
7878 const struct got_error
*error
;
7879 struct got_repository
*repo
= NULL
;
7880 struct got_worktree
*worktree
= NULL
;
7881 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
7882 struct got_object_id
*commit_id
= NULL
;
7883 struct got_commit_object
*commit
= NULL
;
7884 const char *commit_id_arg
= NULL
;
7886 struct got_reference
*ref
= NULL
;
7887 const char *head_ref_name
= NULL
;
7889 struct tog_view
*view
;
7890 int *pack_fds
= NULL
;
7892 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
7895 commit_id_arg
= optarg
;
7898 repo_path
= realpath(optarg
, NULL
);
7899 if (repo_path
== NULL
)
7900 return got_error_from_errno2("realpath",
7915 error
= got_repo_pack_fds_open(&pack_fds
);
7919 if (repo_path
== NULL
) {
7920 cwd
= getcwd(NULL
, 0);
7922 return got_error_from_errno("getcwd");
7923 error
= got_worktree_open(&worktree
, cwd
);
7924 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
7928 strdup(got_worktree_get_repo_path(worktree
));
7930 repo_path
= strdup(cwd
);
7931 if (repo_path
== NULL
) {
7932 error
= got_error_from_errno("strdup");
7937 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
7941 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
7948 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
7952 error
= tog_load_refs(repo
, 0);
7956 if (commit_id_arg
== NULL
) {
7957 error
= got_repo_match_object_id(&commit_id
, &label
,
7958 worktree
? got_worktree_get_head_ref_name(worktree
) :
7959 GOT_REF_HEAD
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
7962 head_ref_name
= label
;
7964 error
= got_ref_open(&ref
, repo
, commit_id_arg
, 0);
7966 head_ref_name
= got_ref_get_name(ref
);
7967 else if (error
->code
!= GOT_ERR_NOT_REF
)
7969 error
= got_repo_match_object_id(&commit_id
, NULL
,
7970 commit_id_arg
, GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
7975 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
7979 view
= view_open(0, 0, 0, 0, TOG_VIEW_TREE
);
7981 error
= got_error_from_errno("view_open");
7984 error
= open_tree_view(view
, commit_id
, head_ref_name
, repo
);
7987 if (!got_path_is_root_dir(in_repo_path
)) {
7988 error
= tree_view_walk_path(&view
->state
.tree
, commit
,
7995 /* Release work tree lock. */
7996 got_worktree_close(worktree
);
7999 error
= view_loop(view
);
8008 const struct got_error
*close_err
= got_repo_close(repo
);
8013 const struct got_error
*pack_err
=
8014 got_repo_pack_fds_close(pack_fds
);
8022 static const struct got_error
*
8023 ref_view_load_refs(struct tog_ref_view_state
*s
)
8025 struct got_reflist_entry
*sre
;
8026 struct tog_reflist_entry
*re
;
8029 TAILQ_FOREACH(sre
, &tog_refs
, entry
) {
8030 if (strncmp(got_ref_get_name(sre
->ref
),
8031 "refs/got/", 9) == 0 &&
8032 strncmp(got_ref_get_name(sre
->ref
),
8033 "refs/got/backup/", 16) != 0)
8036 re
= malloc(sizeof(*re
));
8038 return got_error_from_errno("malloc");
8040 re
->ref
= got_ref_dup(sre
->ref
);
8041 if (re
->ref
== NULL
)
8042 return got_error_from_errno("got_ref_dup");
8043 re
->idx
= s
->nrefs
++;
8044 TAILQ_INSERT_TAIL(&s
->refs
, re
, entry
);
8047 s
->first_displayed_entry
= TAILQ_FIRST(&s
->refs
);
8052 ref_view_free_refs(struct tog_ref_view_state
*s
)
8054 struct tog_reflist_entry
*re
;
8056 while (!TAILQ_EMPTY(&s
->refs
)) {
8057 re
= TAILQ_FIRST(&s
->refs
);
8058 TAILQ_REMOVE(&s
->refs
, re
, entry
);
8059 got_ref_close(re
->ref
);
8064 static const struct got_error
*
8065 open_ref_view(struct tog_view
*view
, struct got_repository
*repo
)
8067 const struct got_error
*err
= NULL
;
8068 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8070 s
->selected_entry
= 0;
8073 TAILQ_INIT(&s
->refs
);
8074 STAILQ_INIT(&s
->colors
);
8076 err
= ref_view_load_refs(s
);
8080 if (has_colors() && getenv("TOG_COLORS") != NULL
) {
8081 err
= add_color(&s
->colors
, "^refs/heads/",
8082 TOG_COLOR_REFS_HEADS
,
8083 get_color_value("TOG_COLOR_REFS_HEADS"));
8087 err
= add_color(&s
->colors
, "^refs/tags/",
8088 TOG_COLOR_REFS_TAGS
,
8089 get_color_value("TOG_COLOR_REFS_TAGS"));
8093 err
= add_color(&s
->colors
, "^refs/remotes/",
8094 TOG_COLOR_REFS_REMOTES
,
8095 get_color_value("TOG_COLOR_REFS_REMOTES"));
8099 err
= add_color(&s
->colors
, "^refs/got/backup/",
8100 TOG_COLOR_REFS_BACKUP
,
8101 get_color_value("TOG_COLOR_REFS_BACKUP"));
8106 view
->show
= show_ref_view
;
8107 view
->input
= input_ref_view
;
8108 view
->close
= close_ref_view
;
8109 view
->search_start
= search_start_ref_view
;
8110 view
->search_next
= search_next_ref_view
;
8113 if (view
->close
== NULL
)
8114 close_ref_view(view
);
8120 static const struct got_error
*
8121 close_ref_view(struct tog_view
*view
)
8123 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8125 ref_view_free_refs(s
);
8126 free_colors(&s
->colors
);
8131 static const struct got_error
*
8132 resolve_reflist_entry(struct got_object_id
**commit_id
,
8133 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
8135 const struct got_error
*err
= NULL
;
8136 struct got_object_id
*obj_id
;
8137 struct got_tag_object
*tag
= NULL
;
8142 err
= got_ref_resolve(&obj_id
, repo
, re
->ref
);
8146 err
= got_object_get_type(&obj_type
, repo
, obj_id
);
8151 case GOT_OBJ_TYPE_COMMIT
:
8152 *commit_id
= obj_id
;
8154 case GOT_OBJ_TYPE_TAG
:
8155 err
= got_object_open_as_tag(&tag
, repo
, obj_id
);
8159 err
= got_object_get_type(&obj_type
, repo
,
8160 got_object_tag_get_object_id(tag
));
8163 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
) {
8164 err
= got_error(GOT_ERR_OBJ_TYPE
);
8167 *commit_id
= got_object_id_dup(
8168 got_object_tag_get_object_id(tag
));
8169 if (*commit_id
== NULL
) {
8170 err
= got_error_from_errno("got_object_id_dup");
8175 err
= got_error(GOT_ERR_OBJ_TYPE
);
8181 got_object_tag_close(tag
);
8189 static const struct got_error
*
8190 log_ref_entry(struct tog_view
**new_view
, int begin_y
, int begin_x
,
8191 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
8193 struct tog_view
*log_view
;
8194 const struct got_error
*err
= NULL
;
8195 struct got_object_id
*commit_id
= NULL
;
8199 err
= resolve_reflist_entry(&commit_id
, re
, repo
);
8201 if (err
->code
!= GOT_ERR_OBJ_TYPE
)
8207 log_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_LOG
);
8208 if (log_view
== NULL
) {
8209 err
= got_error_from_errno("view_open");
8213 err
= open_log_view(log_view
, commit_id
, repo
,
8214 got_ref_get_name(re
->ref
), "", 0);
8217 view_close(log_view
);
8219 *new_view
= log_view
;
8225 ref_scroll_up(struct tog_ref_view_state
*s
, int maxscroll
)
8227 struct tog_reflist_entry
*re
;
8230 if (s
->first_displayed_entry
== TAILQ_FIRST(&s
->refs
))
8233 re
= TAILQ_PREV(s
->first_displayed_entry
, tog_reflist_head
, entry
);
8234 while (i
++ < maxscroll
) {
8237 s
->first_displayed_entry
= re
;
8238 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
8242 static const struct got_error
*
8243 ref_scroll_down(struct tog_view
*view
, int maxscroll
)
8245 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8246 struct tog_reflist_entry
*next
, *last
;
8249 if (s
->first_displayed_entry
)
8250 next
= TAILQ_NEXT(s
->first_displayed_entry
, entry
);
8252 next
= TAILQ_FIRST(&s
->refs
);
8254 last
= s
->last_displayed_entry
;
8255 while (next
&& n
++ < maxscroll
) {
8257 s
->last_displayed_entry
= last
;
8258 last
= TAILQ_NEXT(last
, entry
);
8260 if (last
|| (view
->mode
== TOG_VIEW_SPLIT_HRZN
)) {
8261 s
->first_displayed_entry
= next
;
8262 next
= TAILQ_NEXT(next
, entry
);
8269 static const struct got_error
*
8270 search_start_ref_view(struct tog_view
*view
)
8272 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8274 s
->matched_entry
= NULL
;
8279 match_reflist_entry(struct tog_reflist_entry
*re
, regex_t
*regex
)
8281 regmatch_t regmatch
;
8283 return regexec(regex
, got_ref_get_name(re
->ref
), 1, ®match
,
8287 static const struct got_error
*
8288 search_next_ref_view(struct tog_view
*view
)
8290 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8291 struct tog_reflist_entry
*re
= NULL
;
8293 if (!view
->searching
) {
8294 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
8298 if (s
->matched_entry
) {
8299 if (view
->searching
== TOG_SEARCH_FORWARD
) {
8300 if (s
->selected_entry
)
8301 re
= TAILQ_NEXT(s
->selected_entry
, entry
);
8303 re
= TAILQ_PREV(s
->selected_entry
,
8304 tog_reflist_head
, entry
);
8306 if (s
->selected_entry
== NULL
)
8307 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
8309 re
= TAILQ_PREV(s
->selected_entry
,
8310 tog_reflist_head
, entry
);
8313 if (s
->selected_entry
)
8314 re
= s
->selected_entry
;
8315 else if (view
->searching
== TOG_SEARCH_FORWARD
)
8316 re
= TAILQ_FIRST(&s
->refs
);
8318 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
8323 if (s
->matched_entry
== NULL
) {
8324 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
8327 if (view
->searching
== TOG_SEARCH_FORWARD
)
8328 re
= TAILQ_FIRST(&s
->refs
);
8330 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
8333 if (match_reflist_entry(re
, &view
->regex
)) {
8334 view
->search_next_done
= TOG_SEARCH_HAVE_MORE
;
8335 s
->matched_entry
= re
;
8339 if (view
->searching
== TOG_SEARCH_FORWARD
)
8340 re
= TAILQ_NEXT(re
, entry
);
8342 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
8345 if (s
->matched_entry
) {
8346 s
->first_displayed_entry
= s
->matched_entry
;
8353 static const struct got_error
*
8354 show_ref_view(struct tog_view
*view
)
8356 const struct got_error
*err
= NULL
;
8357 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8358 struct tog_reflist_entry
*re
;
8361 struct tog_color
*tc
;
8362 int width
, n
, scrollx
;
8363 int limit
= view
->nlines
;
8365 werase(view
->window
);
8368 if (view_is_hsplit_top(view
))
8369 --limit
; /* border */
8374 re
= s
->first_displayed_entry
;
8376 if (asprintf(&line
, "references [%d/%d]", re
->idx
+ s
->selected
+ 1,
8378 return got_error_from_errno("asprintf");
8380 err
= format_line(&wline
, &width
, NULL
, line
, 0, view
->ncols
, 0, 0);
8385 if (view_needs_focus_indication(view
))
8386 wstandout(view
->window
);
8387 waddwstr(view
->window
, wline
);
8388 while (width
++ < view
->ncols
)
8389 waddch(view
->window
, ' ');
8390 if (view_needs_focus_indication(view
))
8391 wstandend(view
->window
);
8401 while (re
&& limit
> 0) {
8403 char ymd
[13]; /* YYYY-MM-DD + " " + NUL */
8406 struct got_commit_object
*ci
;
8407 struct got_tag_object
*tag
;
8408 struct got_object_id
*id
;
8412 err
= got_ref_resolve(&id
, s
->repo
, re
->ref
);
8415 err
= got_object_open_as_tag(&tag
, s
->repo
, id
);
8417 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
8421 err
= got_object_open_as_commit(&ci
, s
->repo
,
8427 t
= got_object_commit_get_committer_time(ci
);
8428 got_object_commit_close(ci
);
8430 t
= got_object_tag_get_tagger_time(tag
);
8431 got_object_tag_close(tag
);
8434 if (gmtime_r(&t
, &tm
) == NULL
)
8435 return got_error_from_errno("gmtime_r");
8436 if (strftime(ymd
, sizeof(ymd
), "%G-%m-%d ", &tm
) == 0)
8437 return got_error(GOT_ERR_NO_SPACE
);
8439 if (got_ref_is_symbolic(re
->ref
)) {
8440 if (asprintf(&line
, "%s%s -> %s", s
->show_date
?
8441 ymd
: "", got_ref_get_name(re
->ref
),
8442 got_ref_get_symref_target(re
->ref
)) == -1)
8443 return got_error_from_errno("asprintf");
8444 } else if (s
->show_ids
) {
8445 struct got_object_id
*id
;
8447 err
= got_ref_resolve(&id
, s
->repo
, re
->ref
);
8450 err
= got_object_id_str(&id_str
, id
);
8455 if (asprintf(&line
, "%s%s: %s", s
->show_date
? ymd
: "",
8456 got_ref_get_name(re
->ref
), id_str
) == -1) {
8457 err
= got_error_from_errno("asprintf");
8464 } else if (asprintf(&line
, "%s%s", s
->show_date
? ymd
: "",
8465 got_ref_get_name(re
->ref
)) == -1)
8466 return got_error_from_errno("asprintf");
8468 /* use full line width to determine view->maxx */
8469 err
= format_line(&wline
, &width
, NULL
, line
, 0, INT_MAX
, 0, 0);
8474 view
->maxx
= MAX(view
->maxx
, width
);
8478 err
= format_line(&wline
, &width
, &scrollx
, line
, view
->x
,
8484 if (n
== s
->selected
) {
8486 wstandout(view
->window
);
8487 s
->selected_entry
= re
;
8489 tc
= match_color(&s
->colors
, got_ref_get_name(re
->ref
));
8491 wattr_on(view
->window
,
8492 COLOR_PAIR(tc
->colorpair
), NULL
);
8493 waddwstr(view
->window
, &wline
[scrollx
]);
8495 wattr_off(view
->window
,
8496 COLOR_PAIR(tc
->colorpair
), NULL
);
8497 if (width
< view
->ncols
)
8498 waddch(view
->window
, '\n');
8499 if (n
== s
->selected
&& view
->focussed
)
8500 wstandend(view
->window
);
8506 s
->last_displayed_entry
= re
;
8509 re
= TAILQ_NEXT(re
, entry
);
8516 static const struct got_error
*
8517 browse_ref_tree(struct tog_view
**new_view
, int begin_y
, int begin_x
,
8518 struct tog_reflist_entry
*re
, struct got_repository
*repo
)
8520 const struct got_error
*err
= NULL
;
8521 struct got_object_id
*commit_id
= NULL
;
8522 struct tog_view
*tree_view
;
8526 err
= resolve_reflist_entry(&commit_id
, re
, repo
);
8528 if (err
->code
!= GOT_ERR_OBJ_TYPE
)
8535 tree_view
= view_open(0, 0, begin_y
, begin_x
, TOG_VIEW_TREE
);
8536 if (tree_view
== NULL
) {
8537 err
= got_error_from_errno("view_open");
8541 err
= open_tree_view(tree_view
, commit_id
,
8542 got_ref_get_name(re
->ref
), repo
);
8546 *new_view
= tree_view
;
8552 static const struct got_error
*
8553 ref_goto_line(struct tog_view
*view
, int nlines
)
8555 const struct got_error
*err
= NULL
;
8556 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8557 int g
, idx
= s
->selected_entry
->idx
;
8564 else if (g
> s
->nrefs
)
8567 if (g
>= s
->first_displayed_entry
->idx
+ 1 &&
8568 g
<= s
->last_displayed_entry
->idx
+ 1 &&
8569 g
- s
->first_displayed_entry
->idx
- 1 < nlines
) {
8570 s
->selected
= g
- s
->first_displayed_entry
->idx
- 1;
8575 err
= ref_scroll_down(view
, g
- idx
- 1);
8578 if (TAILQ_NEXT(s
->last_displayed_entry
, entry
) == NULL
&&
8579 s
->first_displayed_entry
->idx
+ s
->selected
< g
&&
8580 s
->selected
< s
->ndisplayed
- 1)
8581 s
->selected
= g
- s
->first_displayed_entry
->idx
- 1;
8582 } else if (idx
+ 1 > g
)
8583 ref_scroll_up(s
, idx
- g
+ 1);
8585 if (g
< nlines
&& s
->first_displayed_entry
->idx
== 0)
8586 s
->selected
= g
- 1;
8592 static const struct got_error
*
8593 input_ref_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
8595 const struct got_error
*err
= NULL
;
8596 struct tog_ref_view_state
*s
= &view
->state
.ref
;
8597 struct tog_reflist_entry
*re
;
8598 int n
, nscroll
= view
->nlines
- 1;
8601 return ref_goto_line(view
, nscroll
);
8610 horizontal_scroll_input(view
, ch
);
8613 s
->show_ids
= !s
->show_ids
;
8617 s
->show_date
= !s
->show_date
;
8621 s
->sort_by_date
= !s
->sort_by_date
;
8622 view
->action
= s
->sort_by_date
? "sort by date" : "sort by name";
8624 err
= got_reflist_sort(&tog_refs
, s
->sort_by_date
?
8625 got_ref_cmp_by_commit_timestamp_descending
:
8626 tog_ref_cmp_by_name
, s
->repo
);
8629 got_reflist_object_id_map_free(tog_refs_idmap
);
8630 err
= got_reflist_object_id_map_create(&tog_refs_idmap
,
8631 &tog_refs
, s
->repo
);
8634 ref_view_free_refs(s
);
8635 err
= ref_view_load_refs(s
);
8640 if (!s
->selected_entry
)
8642 err
= view_request_new(new_view
, view
, TOG_VIEW_LOG
);
8646 if (!s
->selected_entry
)
8648 err
= view_request_new(new_view
, view
, TOG_VIEW_TREE
);
8655 s
->first_displayed_entry
= TAILQ_FIRST(&s
->refs
);
8660 int eos
= view
->nlines
- 1;
8662 if (view
->mode
== TOG_VIEW_SPLIT_HRZN
)
8666 re
= TAILQ_LAST(&s
->refs
, tog_reflist_head
);
8667 for (n
= 0; n
< eos
; n
++) {
8670 s
->first_displayed_entry
= re
;
8671 re
= TAILQ_PREV(re
, tog_reflist_head
, entry
);
8674 s
->selected
= n
- 1;
8680 if (s
->selected
> 0) {
8684 ref_scroll_up(s
, 1);
8685 if (s
->selected_entry
== TAILQ_FIRST(&s
->refs
))
8695 if (s
->first_displayed_entry
== TAILQ_FIRST(&s
->refs
))
8696 s
->selected
-= MIN(nscroll
, s
->selected
);
8697 ref_scroll_up(s
, MAX(0, nscroll
));
8698 if (s
->selected_entry
== TAILQ_FIRST(&s
->refs
))
8704 if (s
->selected
< s
->ndisplayed
- 1) {
8708 if (TAILQ_NEXT(s
->last_displayed_entry
, entry
) == NULL
) {
8709 /* can't scroll any further */
8713 ref_scroll_down(view
, 1);
8723 if (TAILQ_NEXT(s
->last_displayed_entry
, entry
) == NULL
) {
8724 /* can't scroll any further; move cursor down */
8725 if (s
->selected
< s
->ndisplayed
- 1)
8726 s
->selected
+= MIN(nscroll
,
8727 s
->ndisplayed
- s
->selected
- 1);
8728 if (view
->count
> 1 && s
->selected
< s
->ndisplayed
- 1)
8729 s
->selected
+= s
->ndisplayed
- s
->selected
- 1;
8733 ref_scroll_down(view
, nscroll
);
8738 err
= tog_load_refs(s
->repo
, s
->sort_by_date
);
8741 ref_view_free_refs(s
);
8742 err
= ref_view_load_refs(s
);
8745 if (view
->nlines
>= 2 && s
->selected
>= view
->nlines
- 1)
8746 s
->selected
= view
->nlines
- 2;
8760 fprintf(stderr
, "usage: %s ref [-r repository-path]\n",
8765 static const struct got_error
*
8766 cmd_ref(int argc
, char *argv
[])
8768 const struct got_error
*error
;
8769 struct got_repository
*repo
= NULL
;
8770 struct got_worktree
*worktree
= NULL
;
8771 char *cwd
= NULL
, *repo_path
= NULL
;
8773 struct tog_view
*view
;
8774 int *pack_fds
= NULL
;
8776 while ((ch
= getopt(argc
, argv
, "r:")) != -1) {
8779 repo_path
= realpath(optarg
, NULL
);
8780 if (repo_path
== NULL
)
8781 return got_error_from_errno2("realpath",
8796 error
= got_repo_pack_fds_open(&pack_fds
);
8800 if (repo_path
== NULL
) {
8801 cwd
= getcwd(NULL
, 0);
8803 return got_error_from_errno("getcwd");
8804 error
= got_worktree_open(&worktree
, cwd
);
8805 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
8809 strdup(got_worktree_get_repo_path(worktree
));
8811 repo_path
= strdup(cwd
);
8812 if (repo_path
== NULL
) {
8813 error
= got_error_from_errno("strdup");
8818 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
8824 error
= apply_unveil(got_repo_get_path(repo
), NULL
);
8828 error
= tog_load_refs(repo
, 0);
8832 view
= view_open(0, 0, 0, 0, TOG_VIEW_REF
);
8834 error
= got_error_from_errno("view_open");
8838 error
= open_ref_view(view
, repo
);
8843 /* Release work tree lock. */
8844 got_worktree_close(worktree
);
8847 error
= view_loop(view
);
8852 const struct got_error
*close_err
= got_repo_close(repo
);
8857 const struct got_error
*pack_err
=
8858 got_repo_pack_fds_close(pack_fds
);
8866 static const struct got_error
*
8867 win_draw_center(WINDOW
*win
, size_t y
, size_t x
, size_t maxx
, int focus
,
8876 x
= x
? x
: maxx
> len
? (maxx
- len
) / 2 : 0;
8880 if (mvwprintw(win
, y
, x
, "%s", str
) == ERR
)
8881 return got_error_msg(GOT_ERR_RANGE
, "mvwprintw");
8888 static const struct got_error
*
8889 add_line_offset(off_t
**line_offsets
, size_t *nlines
, off_t off
)
8893 p
= reallocarray(*line_offsets
, *nlines
+ 1, sizeof(off_t
));
8895 free(*line_offsets
);
8896 *line_offsets
= NULL
;
8897 return got_error_from_errno("reallocarray");
8901 (*line_offsets
)[*nlines
] = off
;
8906 static const struct got_error
*
8907 max_key_str(int *ret
, const struct tog_key_map
*km
, size_t n
)
8911 for (;n
> 0; --n
, ++km
) {
8915 if (km
->keys
== NULL
)
8918 t
= t0
= strdup(km
->keys
);
8920 return got_error_from_errno("strdup");
8923 while ((k
= strsep(&t
, " ")) != NULL
)
8924 len
+= strlen(k
) > 1 ? 2 : 0;
8926 *ret
= MAX(*ret
, len
);
8933 * Write keymap section headers, keys, and key info in km to f.
8934 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8935 * wrap control and symbolic keys in guillemets, else use <>.
8937 static const struct got_error
*
8938 format_help_line(off_t
*off
, FILE *f
, const struct tog_key_map
*km
, int width
)
8943 static const char *u8_glyph
[] = {
8944 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8945 "\xe2\x80\xba" /* U+203A (utf8 >) */
8948 int cs
, s
, first
= 1;
8950 cs
= got_locale_is_utf8();
8952 t
= t0
= strdup(km
->keys
);
8954 return got_error_from_errno("strdup");
8956 len
= strlen(km
->keys
);
8957 while ((k
= strsep(&t
, " ")) != NULL
) {
8958 s
= strlen(k
) > 1; /* control or symbolic key */
8959 n
= fprintf(f
, "%s%s%s%s%s", first
? " " : "",
8960 cs
&& s
? u8_glyph
[0] : s
? "<" : "", k
,
8961 cs
&& s
? u8_glyph
[1] : s
? ">" : "", t
? " " : "");
8964 return got_error_from_errno("fprintf");
8972 n
= fprintf(f
, "%*s%s\n", width
- len
, width
- len
? " " : "", km
->info
);
8974 return got_error_from_errno("fprintf");
8980 static const struct got_error
*
8981 format_help(struct tog_help_view_state
*s
)
8983 const struct got_error
*err
= NULL
;
8985 int i
, max
, n
, show
= s
->all
;
8986 static const struct tog_key_map km
[] = {
8987 #define KEYMAP_(info, type) { NULL, (info), type }
8988 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8994 err
= add_line_offset(&s
->line_offsets
, &s
->nlines
, 0);
8999 err
= max_key_str(&max
, km
, n
);
9003 for (i
= 0; i
< n
; ++i
) {
9004 if (km
[i
].keys
== NULL
) {
9006 if (km
[i
].type
== TOG_KEYMAP_GLOBAL
||
9007 km
[i
].type
== s
->type
|| s
->all
)
9011 err
= format_help_line(&off
, s
->f
, &km
[i
], max
);
9014 err
= add_line_offset(&s
->line_offsets
, &s
->nlines
, off
);
9021 err
= add_line_offset(&s
->line_offsets
, &s
->nlines
, off
);
9025 static const struct got_error
*
9026 create_help(struct tog_help_view_state
*s
)
9029 const struct got_error
*err
;
9031 free(s
->line_offsets
);
9032 s
->line_offsets
= NULL
;
9037 return got_error_from_errno("got_opentemp");
9040 err
= format_help(s
);
9044 if (s
->f
&& fflush(s
->f
) != 0)
9045 return got_error_from_errno("fflush");
9050 static const struct got_error
*
9051 search_start_help_view(struct tog_view
*view
)
9053 view
->state
.help
.matched_line
= 0;
9058 search_setup_help_view(struct tog_view
*view
, FILE **f
, off_t
**line_offsets
,
9059 size_t *nlines
, int **first
, int **last
, int **match
, int **selected
)
9061 struct tog_help_view_state
*s
= &view
->state
.help
;
9064 *nlines
= s
->nlines
;
9065 *line_offsets
= s
->line_offsets
;
9066 *match
= &s
->matched_line
;
9067 *first
= &s
->first_displayed_line
;
9068 *last
= &s
->last_displayed_line
;
9069 *selected
= &s
->selected_line
;
9072 static const struct got_error
*
9073 show_help_view(struct tog_view
*view
)
9075 struct tog_help_view_state
*s
= &view
->state
.help
;
9076 const struct got_error
*err
;
9077 regmatch_t
*regmatch
= &view
->regmatch
;
9082 int width
, nprinted
= 0, rc
= 0;
9083 int eos
= view
->nlines
;
9085 if (view_is_hsplit_top(view
))
9086 --eos
; /* account for border */
9090 werase(view
->window
);
9092 if (view
->gline
> s
->nlines
- 1)
9093 view
->gline
= s
->nlines
- 1;
9095 err
= win_draw_center(view
->window
, 0, 0, view
->ncols
,
9096 view_needs_focus_indication(view
),
9097 "tog help (press q to return to tog)");
9102 waddstr(view
->window
, "\n\n");
9108 while (eos
> 0 && nprinted
< eos
) {
9111 linelen
= getline(&line
, &linesz
, s
->f
);
9112 if (linelen
== -1) {
9115 return got_ferror(s
->f
, GOT_ERR_IO
);
9120 if (++s
->lineno
< s
->first_displayed_line
)
9122 if (view
->gline
&& !gotoline(view
, &s
->lineno
, &nprinted
))
9124 if (s
->lineno
== view
->hiline
)
9127 err
= format_line(&wline
, &width
, NULL
, line
, 0, INT_MAX
, 0,
9133 view
->maxx
= MAX(view
->maxx
, width
);
9138 wattron(view
->window
, attr
);
9139 if (s
->first_displayed_line
+ nprinted
== s
->matched_line
&&
9140 regmatch
->rm_so
>= 0 && regmatch
->rm_so
< regmatch
->rm_eo
) {
9141 err
= add_matched_line(&width
, line
, view
->ncols
- 1, 0,
9142 view
->window
, view
->x
, regmatch
);
9150 err
= format_line(&wline
, &width
, &skip
, line
,
9151 view
->x
, view
->ncols
, 0, view
->x
? 1 : 0);
9156 waddwstr(view
->window
, &wline
[skip
]);
9160 if (s
->lineno
== view
->hiline
) {
9161 while (width
++ < view
->ncols
)
9162 waddch(view
->window
, ' ');
9164 if (width
< view
->ncols
)
9165 waddch(view
->window
, '\n');
9168 wattroff(view
->window
, attr
);
9169 if (++nprinted
== 1)
9170 s
->first_displayed_line
= s
->lineno
;
9174 s
->last_displayed_line
= s
->first_displayed_line
+ nprinted
- 1;
9176 s
->last_displayed_line
= s
->first_displayed_line
;
9181 rc
= waddnstr(view
->window
,
9182 "See the tog(1) manual page for full documentation",
9185 return got_error_msg(GOT_ERR_RANGE
, "waddnstr");
9187 wmove(view
->window
, view
->nlines
- 1, 0);
9188 wclrtoeol(view
->window
);
9189 wstandout(view
->window
);
9190 rc
= waddnstr(view
->window
, "scroll down for more...",
9193 return got_error_msg(GOT_ERR_RANGE
, "waddnstr");
9194 if (getcurx(view
->window
) < view
->ncols
- 6) {
9195 rc
= wprintw(view
->window
, "[%.0f%%]",
9196 100.00 * s
->last_displayed_line
/ s
->nlines
);
9198 return got_error_msg(GOT_ERR_IO
, "wprintw");
9200 wstandend(view
->window
);
9206 static const struct got_error
*
9207 input_help_view(struct tog_view
**new_view
, struct tog_view
*view
, int ch
)
9209 struct tog_help_view_state
*s
= &view
->state
.help
;
9210 const struct got_error
*err
= NULL
;
9216 eos
= nscroll
= view
->nlines
;
9217 if (view_is_hsplit_top(view
))
9220 s
->lineno
= s
->first_displayed_line
- 1 + s
->selected_line
;
9229 horizontal_scroll_input(view
, ch
);
9233 s
->first_displayed_line
= 1;
9241 s
->first_displayed_line
= (s
->nlines
- eos
) + 3;
9246 if (s
->first_displayed_line
> 1)
9247 --s
->first_displayed_line
;
9258 if (s
->first_displayed_line
== 1) {
9262 while (--nscroll
> 0 && s
->first_displayed_line
> 1)
9263 s
->first_displayed_line
--;
9269 ++s
->first_displayed_line
;
9285 while (!s
->eof
&& --nscroll
> 0) {
9286 linelen
= getline(&line
, &linesz
, s
->f
);
9287 s
->first_displayed_line
++;
9288 if (linelen
== -1) {
9292 err
= got_ferror(s
->f
, GOT_ERR_IO
);
9306 static const struct got_error
*
9307 close_help_view(struct tog_view
*view
)
9309 struct tog_help_view_state
*s
= &view
->state
.help
;
9311 free(s
->line_offsets
);
9312 s
->line_offsets
= NULL
;
9313 if (fclose(s
->f
) == EOF
)
9314 return got_error_from_errno("fclose");
9319 static const struct got_error
*
9320 reset_help_view(struct tog_view
*view
)
9322 struct tog_help_view_state
*s
= &view
->state
.help
;
9325 if (s
->f
&& fclose(s
->f
) == EOF
)
9326 return got_error_from_errno("fclose");
9328 wclear(view
->window
);
9332 s
->first_displayed_line
= 1;
9333 s
->last_displayed_line
= view
->nlines
;
9334 s
->matched_line
= 0;
9336 return create_help(s
);
9339 static const struct got_error
*
9340 open_help_view(struct tog_view
*view
, struct tog_view
*parent
)
9342 const struct got_error
*err
= NULL
;
9343 struct tog_help_view_state
*s
= &view
->state
.help
;
9345 s
->type
= (enum tog_keymap_type
)parent
->type
;
9346 s
->first_displayed_line
= 1;
9347 s
->last_displayed_line
= view
->nlines
;
9348 s
->selected_line
= 1;
9350 view
->show
= show_help_view
;
9351 view
->input
= input_help_view
;
9352 view
->reset
= reset_help_view
;
9353 view
->close
= close_help_view
;
9354 view
->search_start
= search_start_help_view
;
9355 view
->search_setup
= search_setup_help_view
;
9356 view
->search_next
= search_next_view_match
;
9358 err
= create_help(s
);
9362 static const struct got_error
*
9363 view_dispatch_request(struct tog_view
**new_view
, struct tog_view
*view
,
9364 enum tog_view_type request
, int y
, int x
)
9366 const struct got_error
*err
= NULL
;
9372 if (view
->type
== TOG_VIEW_LOG
) {
9373 struct tog_log_view_state
*s
= &view
->state
.log
;
9375 err
= open_diff_view_for_commit(new_view
, y
, x
,
9376 s
->selected_entry
->commit
, s
->selected_entry
->id
,
9379 return got_error_msg(GOT_ERR_NOT_IMPL
,
9380 "parent/child view pair not supported");
9382 case TOG_VIEW_BLAME
:
9383 if (view
->type
== TOG_VIEW_TREE
) {
9384 struct tog_tree_view_state
*s
= &view
->state
.tree
;
9386 err
= blame_tree_entry(new_view
, y
, x
,
9387 s
->selected_entry
, &s
->parents
, s
->commit_id
,
9390 return got_error_msg(GOT_ERR_NOT_IMPL
,
9391 "parent/child view pair not supported");
9394 if (view
->type
== TOG_VIEW_BLAME
)
9395 err
= log_annotated_line(new_view
, y
, x
,
9396 view
->state
.blame
.repo
, view
->state
.blame
.id_to_log
);
9397 else if (view
->type
== TOG_VIEW_TREE
)
9398 err
= log_selected_tree_entry(new_view
, y
, x
,
9400 else if (view
->type
== TOG_VIEW_REF
)
9401 err
= log_ref_entry(new_view
, y
, x
,
9402 view
->state
.ref
.selected_entry
,
9403 view
->state
.ref
.repo
);
9405 return got_error_msg(GOT_ERR_NOT_IMPL
,
9406 "parent/child view pair not supported");
9409 if (view
->type
== TOG_VIEW_LOG
)
9410 err
= browse_commit_tree(new_view
, y
, x
,
9411 view
->state
.log
.selected_entry
,
9412 view
->state
.log
.in_repo_path
,
9413 view
->state
.log
.head_ref_name
,
9414 view
->state
.log
.repo
);
9415 else if (view
->type
== TOG_VIEW_REF
)
9416 err
= browse_ref_tree(new_view
, y
, x
,
9417 view
->state
.ref
.selected_entry
,
9418 view
->state
.ref
.repo
);
9420 return got_error_msg(GOT_ERR_NOT_IMPL
,
9421 "parent/child view pair not supported");
9424 *new_view
= view_open(0, 0, y
, x
, TOG_VIEW_REF
);
9425 if (*new_view
== NULL
)
9426 return got_error_from_errno("view_open");
9427 if (view
->type
== TOG_VIEW_LOG
)
9428 err
= open_ref_view(*new_view
, view
->state
.log
.repo
);
9429 else if (view
->type
== TOG_VIEW_TREE
)
9430 err
= open_ref_view(*new_view
, view
->state
.tree
.repo
);
9432 err
= got_error_msg(GOT_ERR_NOT_IMPL
,
9433 "parent/child view pair not supported");
9435 view_close(*new_view
);
9438 *new_view
= view_open(0, 0, 0, 0, TOG_VIEW_HELP
);
9439 if (*new_view
== NULL
)
9440 return got_error_from_errno("view_open");
9441 err
= open_help_view(*new_view
, view
);
9443 view_close(*new_view
);
9446 return got_error_msg(GOT_ERR_NOT_IMPL
, "invalid view");
9453 * If view was scrolled down to move the selected line into view when opening a
9454 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9457 offset_selection_up(struct tog_view
*view
)
9459 switch (view
->type
) {
9460 case TOG_VIEW_BLAME
: {
9461 struct tog_blame_view_state
*s
= &view
->state
.blame
;
9462 if (s
->first_displayed_line
== 1) {
9463 s
->selected_line
= MAX(s
->selected_line
- view
->offset
,
9467 if (s
->first_displayed_line
> view
->offset
)
9468 s
->first_displayed_line
-= view
->offset
;
9470 s
->first_displayed_line
= 1;
9471 s
->selected_line
+= view
->offset
;
9475 log_scroll_up(&view
->state
.log
, view
->offset
);
9476 view
->state
.log
.selected
+= view
->offset
;
9479 ref_scroll_up(&view
->state
.ref
, view
->offset
);
9480 view
->state
.ref
.selected
+= view
->offset
;
9483 tree_scroll_up(&view
->state
.tree
, view
->offset
);
9484 view
->state
.tree
.selected
+= view
->offset
;
9494 * If the selected line is in the section of screen covered by the bottom split,
9495 * scroll down offset lines to move it into view and index its new position.
9497 static const struct got_error
*
9498 offset_selection_down(struct tog_view
*view
)
9500 const struct got_error
*err
= NULL
;
9501 const struct got_error
*(*scrolld
)(struct tog_view
*, int);
9502 int *selected
= NULL
;
9505 switch (view
->type
) {
9506 case TOG_VIEW_BLAME
: {
9507 struct tog_blame_view_state
*s
= &view
->state
.blame
;
9510 if (s
->selected_line
> view
->nlines
- header
) {
9511 offset
= abs(view
->nlines
- s
->selected_line
- header
);
9512 s
->first_displayed_line
+= offset
;
9513 s
->selected_line
-= offset
;
9514 view
->offset
= offset
;
9518 case TOG_VIEW_LOG
: {
9519 struct tog_log_view_state
*s
= &view
->state
.log
;
9520 scrolld
= &log_scroll_down
;
9521 header
= view_is_parent_view(view
) ? 3 : 2;
9522 selected
= &s
->selected
;
9525 case TOG_VIEW_REF
: {
9526 struct tog_ref_view_state
*s
= &view
->state
.ref
;
9527 scrolld
= &ref_scroll_down
;
9529 selected
= &s
->selected
;
9532 case TOG_VIEW_TREE
: {
9533 struct tog_tree_view_state
*s
= &view
->state
.tree
;
9534 scrolld
= &tree_scroll_down
;
9536 selected
= &s
->selected
;
9546 if (selected
&& *selected
> view
->nlines
- header
) {
9547 offset
= abs(view
->nlines
- *selected
- header
);
9548 view
->offset
= offset
;
9549 if (scrolld
&& offset
) {
9550 err
= scrolld(view
, offset
);
9551 *selected
-= offset
;
9559 list_commands(FILE *fp
)
9563 fprintf(fp
, "commands:");
9564 for (i
= 0; i
< nitems(tog_commands
); i
++) {
9565 const struct tog_cmd
*cmd
= &tog_commands
[i
];
9566 fprintf(fp
, " %s", cmd
->name
);
9572 usage(int hflag
, int status
)
9574 FILE *fp
= (status
== 0) ? stdout
: stderr
;
9576 fprintf(fp
, "usage: %s [-hV] command [arg ...]\n",
9579 fprintf(fp
, "lazy usage: %s path\n", getprogname());
9586 make_argv(int argc
, ...)
9594 argv
= calloc(argc
, sizeof(char *));
9597 for (i
= 0; i
< argc
; i
++) {
9598 argv
[i
] = strdup(va_arg(ap
, char *));
9599 if (argv
[i
] == NULL
)
9608 * Try to convert 'tog path' into a 'tog log path' command.
9609 * The user could simply have mistyped the command rather than knowingly
9610 * provided a path. So check whether argv[0] can in fact be resolved
9611 * to a path in the HEAD commit and print a special error if not.
9612 * This hack is for mpi@ <3
9614 static const struct got_error
*
9615 tog_log_with_path(int argc
, char *argv
[])
9617 const struct got_error
*error
= NULL
, *close_err
;
9618 const struct tog_cmd
*cmd
= NULL
;
9619 struct got_repository
*repo
= NULL
;
9620 struct got_worktree
*worktree
= NULL
;
9621 struct got_object_id
*commit_id
= NULL
, *id
= NULL
;
9622 struct got_commit_object
*commit
= NULL
;
9623 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
9624 char *commit_id_str
= NULL
, **cmd_argv
= NULL
;
9625 int *pack_fds
= NULL
;
9627 cwd
= getcwd(NULL
, 0);
9629 return got_error_from_errno("getcwd");
9631 error
= got_repo_pack_fds_open(&pack_fds
);
9635 error
= got_worktree_open(&worktree
, cwd
);
9636 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
9640 repo_path
= strdup(got_worktree_get_repo_path(worktree
));
9642 repo_path
= strdup(cwd
);
9643 if (repo_path
== NULL
) {
9644 error
= got_error_from_errno("strdup");
9648 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
9652 error
= get_in_repo_path_from_argv0(&in_repo_path
, argc
, argv
,
9657 error
= tog_load_refs(repo
, 0);
9660 error
= got_repo_match_object_id(&commit_id
, NULL
, worktree
?
9661 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
,
9662 GOT_OBJ_TYPE_COMMIT
, &tog_refs
, repo
);
9667 got_worktree_close(worktree
);
9671 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
9675 error
= got_object_id_by_path(&id
, repo
, commit
, in_repo_path
);
9677 if (error
->code
!= GOT_ERR_NO_TREE_ENTRY
)
9679 fprintf(stderr
, "%s: '%s' is no known command or path\n",
9680 getprogname(), argv
[0]);
9685 error
= got_object_id_str(&commit_id_str
, commit_id
);
9689 cmd
= &tog_commands
[0]; /* log */
9691 cmd_argv
= make_argv(argc
, cmd
->name
, "-c", commit_id_str
, argv
[0]);
9692 error
= cmd
->cmd_main(argc
, cmd_argv
);
9695 close_err
= got_repo_close(repo
);
9700 got_object_commit_close(commit
);
9702 got_worktree_close(worktree
);
9704 const struct got_error
*pack_err
=
9705 got_repo_pack_fds_close(pack_fds
);
9710 free(commit_id_str
);
9717 for (i
= 0; i
< argc
; i
++)
9726 main(int argc
, char *argv
[])
9728 const struct got_error
*io_err
, *error
= NULL
;
9729 const struct tog_cmd
*cmd
= NULL
;
9730 int ch
, hflag
= 0, Vflag
= 0;
9731 char **cmd_argv
= NULL
;
9732 static const struct option longopts
[] = {
9733 { "version", no_argument
, NULL
, 'V' },
9736 char *diff_algo_str
= NULL
;
9737 const char *test_script_path
;
9739 setlocale(LC_CTYPE
, "");
9742 * Test mode init must happen before pledge() because "tty" will
9743 * not allow TTY-related ioctls to occur via regular files.
9745 test_script_path
= getenv("TOG_TEST_SCRIPT");
9746 if (test_script_path
!= NULL
) {
9747 error
= init_mock_term(test_script_path
);
9749 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
9754 #if !defined(PROFILE)
9755 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9760 if (!isatty(STDIN_FILENO
))
9761 errx(1, "standard input is not a tty");
9763 while ((ch
= getopt_long(argc
, argv
, "+hV", longopts
, NULL
)) != -1) {
9783 got_version_print_str();
9790 /* Build an argument vector which runs a default command. */
9791 cmd
= &tog_commands
[0];
9793 cmd_argv
= make_argv(argc
, cmd
->name
);
9797 /* Did the user specify a command? */
9798 for (i
= 0; i
< nitems(tog_commands
); i
++) {
9799 if (strncmp(tog_commands
[i
].name
, argv
[0],
9800 strlen(argv
[0])) == 0) {
9801 cmd
= &tog_commands
[i
];
9807 diff_algo_str
= getenv("TOG_DIFF_ALGORITHM");
9808 if (diff_algo_str
) {
9809 if (strcasecmp(diff_algo_str
, "patience") == 0)
9810 tog_diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
9811 if (strcasecmp(diff_algo_str
, "myers") == 0)
9812 tog_diff_algo
= GOT_DIFF_ALGORITHM_MYERS
;
9818 /* No command specified; try log with a path */
9819 error
= tog_log_with_path(argc
, argv
);
9824 error
= cmd
->cmd_main(argc
, cmd_argv
? cmd_argv
: argv
);
9827 if (using_mock_io
) {
9828 io_err
= tog_io_close();
9835 for (i
= 0; i
< argc
; i
++)
9840 if (error
&& error
->code
!= GOT_ERR_CANCELLED
&&
9841 error
->code
!= GOT_ERR_EOF
&&
9842 error
->code
!= GOT_ERR_PRIVSEP_EXIT
&&
9843 error
->code
!= GOT_ERR_PRIVSEP_PIPE
&&
9844 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EINTR
))
9845 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);