1 # Helper functions used by interactive rebase tests.
3 # After setting the fake editor with this function, you can
5 # - override the commit message with $FAKE_COMMIT_MESSAGE
6 # - amend the commit message with $FAKE_COMMIT_AMEND
7 # - copy the original commit message to a file with $FAKE_MESSAGE_COPY
8 # - check that non-commit messages have a certain line count with $EXPECT_COUNT
9 # - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
10 # - rewrite a rebase -i script as directed by $FAKE_LINES.
11 # $FAKE_LINES consists of a sequence of words separated by spaces.
12 # The following word combinations are possible:
14 # "<lineno>" -- add a "pick" line with the SHA1 taken from the
17 # "<cmd> <lineno>" -- add a line with the specified command
18 # ("pick", "squash", "fixup"|"fixup_-C"|"fixup_-c", "edit", "reword" or "drop")
19 # and the SHA1 taken from the specified line.
21 # "_" -- add a space, like "fixup_-C" implies "fixup -C" and
22 # "exec_cmd_with_args" add an "exec cmd with args" line.
24 # "#" -- Add a comment line.
26 # ">" -- Add a blank line.
29 write_script fake-editor.sh <<-\EOF
32 test -z "$EXPECT_HEADER_COUNT" ||
33 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
35 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
36 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
37 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
41 test -z "$EXPECT_COUNT" ||
42 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
44 test -z "$FAKE_LINES" && exit
45 grep -v '^#' < "$1" > "$1".tmp
47 echo 'rebase -i script before editing:'
50 for line in $FAKE_LINES; do
52 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
55 echo "$line" | sed 's/_/ /g' >> "$1";;
57 action=$(echo "$line" | sed 's/_/ /g');;
59 echo '# comment' >> "$1";;
65 test \& != "$action" || action=pick
66 echo "$action XXXXXXX False commit" >> "$1"
69 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
73 echo 'rebase -i script after editing:'
77 test_set_editor "$(pwd)/fake-editor.sh"
80 # After set_cat_todo_editor, rebase -i will write the todo list (ignoring
81 # blank lines and comments) to stdout, and exit failure (so you should run
82 # it with test_must_fail). This can be used to verify the expected user
83 # experience, for todo list changes that do not affect the outcome of
84 # rebase; or as an extra check in addition to checking the outcome.
86 set_cat_todo_editor () {
87 write_script fake-editor.sh <<-\EOF
91 test_set_editor "$(pwd)/fake-editor.sh"
94 # checks that the revisions in "$2" represent a linear range with the
96 test_linear_range () {
97 revlist_merges=$(git rev-list --merges "$2") &&
98 test -z "$revlist_merges" &&
100 set -- $(git log --reverse --format=%s "$2")
101 test "$expected" = "$*"
105 test_might_fail git rebase --abort &&
111 git cherry-pick -n "$2" &&
112 git commit -m "$1" &&
117 git revert -n "$2" &&
118 git commit -m "$1" &&
123 git commit --allow-empty -m "$1" &&
127 # Call this (inside test_expect_success) at the end of a test file to
128 # check that no tests have changed editor related environment
129 # variables or config settings
130 test_editor_unchanged () {
131 # We're only interested in exported variables hence 'sh -c'
132 sh -c 'cat >actual <<-EOF
134 FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
135 FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
136 FAKE_LINES=$FAKE_LINES
137 GIT_EDITOR=$GIT_EDITOR
138 GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
139 core.editor=$(git config core.editor)
140 sequence.editor=$(git config sequence.editor)
152 test_cmp expect actual