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 echo "#!$SHELL_PATH" >fake-editor.sh
65 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 test_set_editor
"$(pwd)/fake-editor.sh"
95 chmod a
+x fake-editor.sh
97 test_expect_success
'no changes are a nop' '
99 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
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 symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
109 test $(git rev-parse I) = $(git rev-parse branch2) &&
110 test $(git rev-parse I) = $(git rev-parse HEAD)
113 test_expect_success
'test --onto <branch>' '
114 git checkout -b test-onto branch2 &&
115 git rebase -i --onto branch1 F &&
116 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
117 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
118 test $(git rev-parse I) = $(git rev-parse branch2)
121 test_expect_success
'rebase on top of a non-conflicting commit' '
122 git checkout branch1 &&
123 git tag original-branch1 &&
124 git rebase -i branch2 &&
125 test file6 = $(git diff --name-only original-branch1) &&
126 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
127 test $(git rev-parse I) = $(git rev-parse branch2) &&
128 test $(git rev-parse I) = $(git rev-parse HEAD~2)
131 test_expect_success
'reflog for the branch shows state before rebase' '
132 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
135 test_expect_success
'exchange two commits' '
136 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
137 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
138 test G = $(git cat-file commit HEAD | sed -ne \$p)
142 diff --git a/file1 b/file1
143 index e69de29..00750ed 100644
155 >>>>>>> b7ca976... G:file1
158 test_expect_success
'stop on conflicting pick' '
159 git tag new-branch1 &&
160 test_must_fail git rebase -i master &&
161 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
162 test_cmp expect .git/rebase-merge/patch &&
163 test_cmp expect2 file1 &&
164 test "$(git-diff --name-status |
165 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
166 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
167 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
170 test_expect_success
'abort' '
171 git rebase --abort &&
172 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
173 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
174 ! test -d .git/rebase-merge
177 test_expect_success
'retain authorship' '
181 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
183 git rebase -i --onto master HEAD^ &&
184 git show HEAD | grep "^Author: Twerp Snog"
187 test_expect_success
'squash' '
188 git reset --hard twerp &&
191 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
192 echo "******************************" &&
193 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
194 test B = $(cat file7) &&
195 test $(git rev-parse HEAD^) = $(git rev-parse master)
198 test_expect_success
'retain authorship when squashing' '
199 git show HEAD | grep "^Author: Twerp Snog"
202 test_expect_success
'-p handles "no changes" gracefully' '
203 HEAD=$(git rev-parse HEAD) &&
204 git rebase -i -p HEAD^ &&
205 git update-index --refresh &&
206 git diff-files --quiet &&
207 git diff-index --quiet --cached HEAD -- &&
208 test $HEAD = $(git rev-parse HEAD)
211 test_expect_success
'preserve merges with -p' '
212 git checkout -b to-be-preserved master^ &&
213 : > unrelated-file &&
214 git add unrelated-file &&
216 git commit -m "unrelated" &&
217 git checkout -b another-branch master &&
220 git commit -m J file1 &&
222 git merge to-be-preserved &&
225 git commit -m K file1 &&
228 git commit -m L1 file1 &&
229 git checkout HEAD^ &&
230 echo 1 > unrelated-file &&
232 git commit -m L2 unrelated-file &&
234 git merge another-branch &&
237 git commit -m M file1 &&
238 git checkout -b to-be-rebased &&
240 git rebase -i -p --onto branch1 master &&
241 git update-index --refresh &&
242 git diff-files --quiet &&
243 git diff-index --quiet --cached HEAD -- &&
244 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
245 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
246 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
247 test $(git show HEAD~5:file1) = B &&
248 test $(git show HEAD~3:file1) = C &&
249 test $(git show HEAD:file1) = E &&
250 test $(git show HEAD:unrelated-file) = 1
253 test_expect_success
'--continue tries to commit' '
255 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
256 echo resolved > file1 &&
258 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
259 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
260 git show HEAD | grep chouette
263 test_expect_success
'verbose flag is heeded, even after --continue' '
264 git reset --hard HEAD@{1} &&
266 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
267 echo resolved > file1 &&
269 git rebase --continue > output &&
270 grep "^ file1 | 2 +-$" output
273 test_expect_success
'multi-squash only fires up editor once' '
274 base=$(git rev-parse HEAD~4) &&
275 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
276 git rebase -i $base &&
277 test $base = $(git rev-parse HEAD^) &&
278 test 1 = $(git show | grep ONCE | wc -l)
281 test_expect_success
'squash works as expected' '
282 for n in one two three four
288 one=$(git rev-parse HEAD~3) &&
289 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
290 test $one = $(git rev-parse HEAD~2)
293 test_expect_success
'interrupted squash works as expected' '
294 for n in one two three four
296 echo $n >> conflict &&
300 one=$(git rev-parse HEAD~3) &&
302 FAKE_LINES="1 squash 3 2" &&
304 test_must_fail git rebase -i HEAD~3
306 (echo one; echo two; echo four) > conflict &&
308 test_must_fail git rebase --continue &&
309 echo resolved > conflict &&
311 git rebase --continue &&
312 test $one = $(git rev-parse HEAD~2)
315 test_expect_success
'interrupted squash works as expected (case 2)' '
316 for n in one two three four
318 echo $n >> conflict &&
322 one=$(git rev-parse HEAD~3) &&
324 FAKE_LINES="3 squash 1 2" &&
326 test_must_fail git rebase -i HEAD~3
328 (echo one; echo four) > conflict &&
330 test_must_fail git rebase --continue &&
331 (echo one; echo two; echo four) > conflict &&
333 test_must_fail git rebase --continue &&
334 echo resolved > conflict &&
336 git rebase --continue &&
337 test $one = $(git rev-parse HEAD~2)
340 test_expect_success
'ignore patch if in upstream' '
341 HEAD=$(git rev-parse HEAD) &&
342 git checkout -b has-cherry-picked HEAD^ &&
343 echo unrelated > file7 &&
346 git commit -m "unrelated change" &&
347 git cherry-pick $HEAD &&
348 EXPECT_COUNT=1 git rebase -i $HEAD &&
349 test $HEAD = $(git rev-parse HEAD^)
352 test_expect_success
'--continue tries to commit, even for "edit"' '
353 parent=$(git rev-parse HEAD^) &&
355 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
356 echo edited > file7 &&
358 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
359 test edited = $(git show HEAD:file7) &&
360 git show HEAD | grep chouette &&
361 test $parent = $(git rev-parse HEAD^)
364 test_expect_success
'rebase a detached HEAD' '
365 grandparent=$(git rev-parse HEAD~2) &&
366 git checkout $(git rev-parse HEAD) &&
368 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
369 test $grandparent = $(git rev-parse HEAD~2)
372 test_expect_success
'rebase a commit violating pre-commit' '
374 mkdir -p .git/hooks &&
375 PRE_COMMIT=.git/hooks/pre-commit &&
376 echo "#!/bin/sh" > $PRE_COMMIT &&
377 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
378 chmod a+x $PRE_COMMIT &&
379 echo "monde! " >> file1 &&
381 test_must_fail git commit -m doesnt-verify file1 &&
382 git commit -m doesnt-verify --no-verify file1 &&
384 FAKE_LINES=2 git rebase -i HEAD~2
388 test_expect_success
'rebase with a file named HEAD in worktree' '
392 git checkout -b branch3 A &&
395 GIT_AUTHOR_NAME="Squashed Away" &&
396 export GIT_AUTHOR_NAME &&
399 git commit -m "Add head" &&
402 git commit -m "Add body"
405 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
406 test "$(git show -s --pretty=format:%an)" = "Squashed Away"