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 # spaces inside the words are encoded as underscores.
13 # The following words are possible:
15 # "<cmd>" -- override the command for the next line specification. Can be
16 # "pick", "squash", "fixup[_-(c|C)]", "edit", "reword", "drop",
17 # "merge[_-(c|C)_<rev>]", or "bad" for an invalid command.
19 # "<lineno>" -- add a command, using the specified line as a template.
20 # If the command has not been overridden, the line will be copied
21 # verbatim, usually resulting in a "pick" line.
23 # "fakesha" -- add a command ("pick" by default), using a fake SHA1.
25 # "exec_[command...]", "break" -- add the specified command.
27 # "#" -- Add a comment line.
29 # ">" -- Add a blank line.
32 write_script fake-editor.sh
<<-\EOF
35 test -z "$EXPECT_HEADER_COUNT" ||
36 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
38 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
39 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
40 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
44 test -z "$EXPECT_COUNT" ||
45 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
47 test -z "$FAKE_LINES" && exit
48 grep -v '^#' < "$1" > "$1".tmp
50 echo 'rebase -i script before editing:'
53 for line in $FAKE_LINES; do
55 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|t|merge|m)
58 echo "$line" | sed 's/_/ /g' >> "$1";;
60 action=$(echo "$line" | sed 's/_/ /g');;
62 echo '# comment' >> "$1";;
68 test \& != "$action" || action=pick
69 echo "$action XXXXXXX False commit" >> "$1"
72 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
76 echo 'rebase -i script after editing:'
80 test_set_editor "$(pwd)/fake-editor.sh"
83 # After set_cat_todo_editor, rebase -i will write the todo list (ignoring
84 # blank lines and comments) to stdout, and exit failure (so you should run
85 # it with test_must_fail). This can be used to verify the expected user
86 # experience, for todo list changes that do not affect the outcome of
87 # rebase; or as an extra check in addition to checking the outcome.
89 set_cat_todo_editor () {
90 write_script fake-editor.sh <<-\
EOF
94 test_set_editor "$(pwd)/fake-editor.sh"
97 # checks that the revisions in "$2" represent a linear range with the
99 test_linear_range () {
100 revlist_merges=$(git rev-list --merges "$2") &&
101 test -z "$revlist_merges" &&
103 set -- $(git log --reverse --format=%s "$2")
104 test "$expected" = "$*"
108 test_might_fail git rebase --abort &&
114 git cherry-pick -n "$2" &&
115 git commit -m "$1" &&
120 git revert -n "$2" &&
121 git commit -m "$1" &&
126 git commit --allow-empty -m "$1" &&
130 # Call this (inside test_expect_success) at the end of a test file to
131 # check that no tests have changed editor related environment
132 # variables or config settings
133 test_editor_unchanged () {
134 # We're only interested in exported variables hence 'sh -c'
135 sh -c 'cat >actual <<-EOF
137 FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
138 FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
139 FAKE_LINES=$FAKE_LINES
140 GIT_EDITOR=$GIT_EDITOR
141 GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
142 core.editor=$(git config core.editor)
143 sequence.editor=$(git config sequence.editor)
155 test_cmp expect actual
158 # Set up an editor for testing reword commands
159 # Checks that there are no uncommitted changes when rewording and that the
160 # todo-list is reread after each
161 set_reword_editor () {
165 # Check rewording keeps the original authorship
166 GIT_AUTHOR_NAME="Reword Author"
167 GIT_AUTHOR_EMAIL="reword.author@example.com"
168 GIT_AUTHOR_DATE=@123456
170 write_script reword-sequence-editor.sh <<-\EOF &&
171 todo="$(cat "$1")" &&
172 echo "exec git log -1 --pretty=format:'%an <%ae> %at%n%B%n' \
173 >>reword-actual" >"$1" &&
174 printf "%s\n" "$todo" >>"$1"
177 write_script reword-editor.sh <<-EOF &&
178 # Save the oid of the first reworded commit so we can check rebase
179 # fast-forwards to it. Also check that we do not write .git/MERGE_MSG
180 # when fast-forwarding
181 if ! test -s reword-oid
183 git rev-parse HEAD >reword-oid &&
184 if test -f .git/MERGE_MSG
186 echo 1>&2 "error: .git/MERGE_MSG exists"
190 # There should be no uncommited changes
191 git diff --exit-code HEAD &&
192 # The todo-list should be re-read after a reword
193 GIT_SEQUENCE_EDITOR="\"$PWD/reword-sequence-editor.sh\"" \
194 git rebase --edit-todo &&
198 test_set_editor "$PWD/reword-editor.sh"
201 # Check the results of a rebase after calling set_reword_editor
202 # Pass the commits that were reworded in the order that they were picked
203 # Expects the first pick to be a fast-forward
204 check_reworded_commits () {
205 test_cmp_rev "$(cat reword-oid)" "$1^{commit}" &&
206 git log --format="%an <%ae> %at%n%B%nedited%n" --no-walk=unsorted "$@" \
208 test_cmp reword-expected reword-actual &&
209 git log --format="%an <%ae> %at%n%B" -n $# --first-parent --reverse \
211 test_cmp reword-expected reword-log
214 # usage: set_replace_editor <file>
216 # Replace the todo file with the exact contents of the given file.
217 # N.B. sets GIT_SEQUENCE_EDITOR rather than EDITOR so it can be
218 # combined with set_fake_editor to reword commits and replace the
220 set_replace_editor () {
221 cat >script <<-\EOF &&
224 echo 'rebase -i script after editing:'
228 sed -e "s/FILENAME/$1/g" script |
229 write_script fake-sequence-editor.sh &&
230 test_set_sequence_editor "$(pwd)/fake-sequence-editor.sh"