git-prompt: use here-doc instead of here-string
[git/gitster.git] / t / lib-rebase.sh
blob11d2dc9fe341b61d22c467ea8ae6a9d13ff1ac18
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.
31 set_fake_editor () {
32 write_script fake-editor.sh <<-\EOF
33 case "$1" in
34 */COMMIT_EDITMSG)
35 test -z "$EXPECT_HEADER_COUNT" ||
36 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
37 exit
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"
41 exit
43 esac
44 test -z "$EXPECT_COUNT" ||
45 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
46 exit
47 test -z "$FAKE_LINES" && exit
48 grep -v '^#' < "$1" > "$1".tmp
49 rm -f "$1"
50 echo 'rebase -i script before editing:'
51 cat "$1".tmp
52 action=\&
53 for line in $FAKE_LINES; do
54 case $line in
55 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|t|merge|m)
56 action="$line";;
57 exec_*|x_*|break|b)
58 echo "$line" | sed 's/_/ /g' >> "$1";;
59 merge_*|fixup_*)
60 action=$(echo "$line" | sed 's/_/ /g');;
61 "#")
62 echo '# comment' >> "$1";;
63 ">")
64 echo >> "$1";;
65 bad)
66 action="pickled";;
67 fakesha)
68 test \& != "$action" || action=pick
69 echo "$action XXXXXXX False commit" >> "$1"
70 action=\&;;
72 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
73 action=\&;;
74 esac
75 done
76 echo 'rebase -i script after editing:'
77 cat "$1"
78 EOF
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
91 grep "^[^#]" "$1"
92 exit 1
93 EOF
94 test_set_editor "$(pwd)/fake-editor.sh"
97 # checks that the revisions in "$2" represent a linear range with the
98 # subjects in "$1"
99 test_linear_range () {
100 revlist_merges=$(git rev-list --merges "$2") &&
101 test -z "$revlist_merges" &&
102 expected=$1
103 set -- $(git log --reverse --format=%s "$2")
104 test "$expected" = "$*"
107 reset_rebase () {
108 test_might_fail git rebase --abort &&
109 git reset --hard &&
110 git clean -f
113 cherry_pick () {
114 git cherry-pick -n "$2" &&
115 git commit -m "$1" &&
116 git tag "$1"
119 revert () {
120 git revert -n "$2" &&
121 git commit -m "$1" &&
122 git tag "$1"
125 make_empty () {
126 git commit --allow-empty -m "$1" &&
127 git tag "$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
136 EDITOR=$EDITOR
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)
144 EOF'
145 cat >expect <<-\EOF
146 EDITOR=:
147 FAKE_COMMIT_AMEND=
148 FAKE_COMMIT_MESSAGE=
149 FAKE_LINES=
150 GIT_EDITOR=
151 GIT_SEQUENCE_EDITOR=
152 core.editor=
153 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 () {
162 >reword-actual &&
163 >reword-oid &&
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
182 then
183 git rev-parse HEAD >reword-oid &&
184 if test -f .git/MERGE_MSG
185 then
186 echo 1>&2 "error: .git/MERGE_MSG exists"
187 exit 1
189 fi &&
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 &&
195 echo edited >>"\$1"
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 "$@" \
207 >reword-expected &&
208 test_cmp reword-expected reword-actual &&
209 git log --format="%an <%ae> %at%n%B" -n $# --first-parent --reverse \
210 >reword-log &&
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
219 # todo list
220 set_replace_editor () {
221 cat >script <<-\EOF &&
222 cat FILENAME >"$1"
224 echo 'rebase -i script after editing:'
225 cat "$1"
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"