3 # Copyright (c) 2007 Johannes E. Schindelin
6 test_description='git rebase interactive
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
13 # set up two branches like this:
21 # where B, D and G touch the same file.
23 test_expect_success 'setup' '
31 git commit -m B file1 &&
38 git commit -m D file1 &&
43 git checkout -b branch1 A &&
51 git commit -m G file1 &&
56 git checkout -b branch2 F &&
64 cat > fake-editor.sh <<\EOF
68 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
69 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
73 test -z "$EXPECT_COUNT" ||
74 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
76 test -z "$FAKE_LINES" && exit
77 grep -v '^#' < "$1" > "$1".tmp
81 for line in $FAKE_LINES; do
86 echo sed -n "${line}s/^pick/$action/p"
87 sed -n "${line}p" < "$1".tmp
88 sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
94 chmod a+x fake-editor.sh
95 VISUAL="$(pwd)/fake-editor.sh"
98 test_expect_success 'no changes are a nop' '
100 test $(git rev-parse I) = $(git rev-parse HEAD)
103 test_expect_success 'test the [branch] option' '
104 git checkout -b dead-end &&
106 git commit -m "stop here" &&
107 git rebase -i F branch2 &&
108 test $(git rev-parse I) = $(git rev-parse HEAD)
111 test_expect_success 'rebase on top of a non-conflicting commit' '
112 git checkout branch1 &&
113 git tag original-branch1 &&
114 git rebase -i branch2 &&
115 test file6 = $(git diff --name-only original-branch1) &&
116 test $(git rev-parse I) = $(git rev-parse HEAD~2)
119 test_expect_success 'reflog for the branch shows state before rebase' '
120 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
123 test_expect_success 'exchange two commits' '
124 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
125 test H = $(git cat-file commit HEAD^ | tail -n 1) &&
126 test G = $(git cat-file commit HEAD | tail -n 1)
130 diff --git a/file1 b/file1
131 index e69de29..00750ed 100644
143 >>>>>>> b7ca976... G:file1
146 test_expect_success 'stop on conflicting pick' '
147 git tag new-branch1 &&
148 ! git rebase -i master &&
149 diff -u expect .git/.dotest-merge/patch &&
150 diff -u expect2 file1 &&
151 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
152 test 0 = $(grep -ve "^#" -e "^$" < .git/.dotest-merge/git-rebase-todo |
156 test_expect_success 'abort' '
157 git rebase --abort &&
158 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
159 ! test -d .git/.dotest-merge
162 test_expect_success 'retain authorship' '
166 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
168 git rebase -i --onto master HEAD^ &&
169 git show HEAD | grep "^Author: Twerp Snog"
172 test_expect_success 'squash' '
173 git reset --hard twerp &&
176 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
177 echo "******************************" &&
178 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
179 test B = $(cat file7) &&
180 test $(git rev-parse HEAD^) = $(git rev-parse master)
183 test_expect_success 'retain authorship when squashing' '
184 git show HEAD | grep "^Author: Twerp Snog"
187 test_expect_success 'preserve merges with -p' '
188 git checkout -b to-be-preserved master^ &&
189 : > unrelated-file &&
190 git add unrelated-file &&
192 git commit -m "unrelated" &&
193 git checkout -b to-be-rebased master &&
196 git commit -m J file1 &&
198 git merge to-be-preserved &&
201 git commit -m K file1 &&
203 git rebase -i -p --onto branch1 master &&
204 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
205 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
206 test $(git show HEAD:file1) = C &&
207 test $(git show HEAD~2:file1) = B
210 test_expect_success '--continue tries to commit' '
212 ! git rebase -i --onto new-branch1 HEAD^ &&
213 echo resolved > file1 &&
215 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
216 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
217 git show HEAD | grep chouette
220 test_expect_success 'verbose flag is heeded, even after --continue' '
221 git reset --hard HEAD@{1} &&
223 ! git rebase -v -i --onto new-branch1 HEAD^ &&
224 echo resolved > file1 &&
226 git rebase --continue > output &&
227 grep "^ file1 | 2 +-$" output
230 test_expect_success 'multi-squash only fires up editor once' '
231 base=$(git rev-parse HEAD~4) &&
232 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
233 git rebase -i $base &&
234 test $base = $(git rev-parse HEAD^) &&
235 test 1 = $(git show | grep ONCE | wc -l)
238 test_expect_success 'squash works as expected' '
239 for n in one two three four
245 one=$(git rev-parse HEAD~3) &&
246 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
247 test $one = $(git rev-parse HEAD~2)
250 test_expect_success 'interrupted squash works as expected' '
251 for n in one two three four
253 echo $n >> conflict &&
257 one=$(git rev-parse HEAD~3) &&
258 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
259 (echo one; echo two; echo four) > conflict &&
261 ! git rebase --continue &&
262 echo resolved > conflict &&
264 git rebase --continue &&
265 test $one = $(git rev-parse HEAD~2)
268 test_expect_success 'interrupted squash works as expected (case 2)' '
269 for n in one two three four
271 echo $n >> conflict &&
275 one=$(git rev-parse HEAD~3) &&
276 ! FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
277 (echo one; echo four) > conflict &&
279 ! git rebase --continue &&
280 (echo one; echo two; echo four) > conflict &&
282 ! git rebase --continue &&
283 echo resolved > conflict &&
285 git rebase --continue &&
286 test $one = $(git rev-parse HEAD~2)
289 test_expect_success 'ignore patch if in upstream' '
290 HEAD=$(git rev-parse HEAD) &&
291 git checkout -b has-cherry-picked HEAD^ &&
292 echo unrelated > file7 &&
295 git commit -m "unrelated change" &&
296 git cherry-pick $HEAD &&
297 EXPECT_COUNT=1 git rebase -i $HEAD &&
298 test $HEAD = $(git rev-parse HEAD^)
301 test_expect_success '--continue tries to commit, even for "edit"' '
302 parent=$(git rev-parse HEAD^) &&
304 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
305 echo edited > file7 &&
307 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
308 test edited = $(git show HEAD:file7) &&
309 git show HEAD | grep chouette &&
310 test $parent = $(git rev-parse HEAD^)
313 test_expect_success 'rebase a detached HEAD' '
314 grandparent=$(git rev-parse HEAD~2) &&
315 git checkout $(git rev-parse HEAD) &&
317 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
318 test $grandparent = $(git rev-parse HEAD~2)