]> git.ipfire.org Git - thirdparty/git.git/blame - t/lib-rebase.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / lib-rebase.sh
CommitLineData
c74c7203 1# Helper functions used by interactive rebase tests.
29a03348 2
03af0870
JS
3# After setting the fake editor with this function, you can
4#
959c0d06 5# - override the commit message with $FAKE_COMMIT_MESSAGE
03af0870 6# - amend the commit message with $FAKE_COMMIT_AMEND
1d410cd8 7# - copy the original commit message to a file with $FAKE_MESSAGE_COPY
03af0870 8# - check that non-commit messages have a certain line count with $EXPECT_COUNT
959c0d06 9# - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
05c95dbe 10# - rewrite a rebase -i script as directed by $FAKE_LINES.
20a0bd45
OB
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:
03af0870 14#
20a0bd45
OB
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.
03af0870 18#
20a0bd45
OB
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.
05c95dbe 22#
20a0bd45
OB
23# "fakesha" -- add a command ("pick" by default), using a fake SHA1.
24#
25# "exec_[command...]", "break" -- add the specified command.
296fa993 26#
05c95dbe
MH
27# "#" -- Add a comment line.
28#
29# ">" -- Add a blank line.
03af0870 30
29a03348 31set_fake_editor () {
a4952815
AP
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"
1d410cd8 40 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
959c0d06 41 exit
a4952815 42 ;;
29a03348 43 esac
a4952815
AP
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
5dcdd740 52 action=\&
a4952815
AP
53 for line in $FAKE_LINES; do
54 case $line in
1cc46244 55 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|t|merge|m)
a4952815 56 action="$line";;
b3635c15 57 exec_*|x_*|break|b)
a4952815 58 echo "$line" | sed 's/_/ /g' >> "$1";;
1d410cd8
CM
59 merge_*|fixup_*)
60 action=$(echo "$line" | sed 's/_/ /g');;
a4952815
AP
61 "#")
62 echo '# comment' >> "$1";;
63 ">")
64 echo >> "$1";;
804098bb 65 bad)
7aed2c05 66 action="pickled";;
804098bb 67 fakesha)
5dcdd740 68 test \& != "$action" || action=pick
804098bb 69 echo "$action XXXXXXX False commit" >> "$1"
010a0b62 70 action=\&;;
a4952815 71 *)
5dcdd740
JS
72 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
73 action=\&;;
a4952815
AP
74 esac
75 done
76 echo 'rebase -i script after editing:'
77 cat "$1"
78 EOF
29a03348
JS
79
80 test_set_editor "$(pwd)/fake-editor.sh"
29a03348 81}
2aad7cac 82
22c5b136
AP
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.
88
89set_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"
95}
96
2aad7cac
MZ
97# checks that the revisions in "$2" represent a linear range with the
98# subjects in "$1"
99test_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" = "$*"
105}
106
107reset_rebase () {
108 test_might_fail git rebase --abort &&
109 git reset --hard &&
110 git clean -f
111}
5b5e1c7c
MZ
112
113cherry_pick () {
114 git cherry-pick -n "$2" &&
115 git commit -m "$1" &&
116 git tag "$1"
117}
118
119revert () {
120 git revert -n "$2" &&
121 git commit -m "$1" &&
122 git tag "$1"
123}
124
125make_empty () {
126 git commit --allow-empty -m "$1" &&
127 git tag "$1"
128}
6a619ca0
PW
129
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
133test_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=
154 EOF
155 test_cmp expect actual
156}
2be6b6f4
PW
157
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
161set_reword_editor () {
162 >reword-actual &&
163 >reword-oid &&
164
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
169
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"
175 EOF
176
177 write_script reword-editor.sh <<-EOF &&
178 # Save the oid of the first reworded commit so we can check rebase
baf8ec8d
PW
179 # fast-forwards to it. Also check that we do not write .git/MERGE_MSG
180 # when fast-forwarding
2be6b6f4
PW
181 if ! test -s reword-oid
182 then
baf8ec8d
PW
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
188 fi
2be6b6f4
PW
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"
196 EOF
197
198 test_set_editor "$PWD/reword-editor.sh"
199}
200
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
204check_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
212}
b3b1a21d
DS
213
214# usage: set_replace_editor <file>
215#
216# Replace the todo file with the exact contents of the given file.
666b6e11
PW
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
b3b1a21d
DS
220set_replace_editor () {
221 cat >script <<-\EOF &&
222 cat FILENAME >"$1"
223
224 echo 'rebase -i script after editing:'
225 cat "$1"
226 EOF
227
666b6e11
PW
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"
b3b1a21d 231}