Merge commit 'junio/next' into next
[git/platforms/storm.git] / t / t3404-rebase-interactive.sh
blobe6f3fadca3858f56638b8c908095516dbc62e77c
1 #!/bin/sh
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.
11 . ./test-lib.sh
13 # set up two branches like this:
15 # A - B - C - D - E
16 # \
17 # F - G - H
18 # \
19 # I
21 # where B, D and G touch the same file.
23 test_expect_success 'setup' '
24 : > file1 &&
25 git add file1 &&
26 test_tick &&
27 git commit -m A &&
28 git tag A &&
29 echo 1 > file1 &&
30 test_tick &&
31 git commit -m B file1 &&
32 : > file2 &&
33 git add file2 &&
34 test_tick &&
35 git commit -m C &&
36 echo 2 > file1 &&
37 test_tick &&
38 git commit -m D file1 &&
39 : > file3 &&
40 git add file3 &&
41 test_tick &&
42 git commit -m E &&
43 git checkout -b branch1 A &&
44 : > file4 &&
45 git add file4 &&
46 test_tick &&
47 git commit -m F &&
48 git tag F &&
49 echo 3 > file1 &&
50 test_tick &&
51 git commit -m G file1 &&
52 : > file5 &&
53 git add file5 &&
54 test_tick &&
55 git commit -m H &&
56 git checkout -b branch2 F &&
57 : > file6 &&
58 git add file6 &&
59 test_tick &&
60 git commit -m I &&
61 git tag I
64 echo "#!$SHELL_PATH" >fake-editor.sh
65 cat >> fake-editor.sh <<\EOF
66 case "$1" in
67 */COMMIT_EDITMSG)
68 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
69 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
70 exit
72 esac
73 test -z "$EXPECT_COUNT" ||
74 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
75 exit
76 test -z "$FAKE_LINES" && { grep -v '^#' "$1"; exit; }
77 grep -v '^#' < "$1" > "$1".tmp
78 rm -f "$1"
79 cat "$1".tmp
80 action=pick
81 for line in $FAKE_LINES; do
82 case $line in
83 squash|edit)
84 action="$line";;
85 mark*)
86 echo "mark ${line#mark}"
87 echo "mark ${line#mark}" >> "$1";;
88 reset*)
89 echo "reset ${line#reset}"
90 echo "reset ${line#reset}" >> "$1";;
91 merge*)
92 echo "merge ${line#merge}" | tr / ' '
93 echo "merge ${line#merge}" | tr / ' ' >> "$1";;
94 tag*)
95 echo "tag ${line#tag}"
96 echo "tag ${line#tag}" >> "$1";;
98 sed -n "${line}{s/^pick/$action/; p;}" < "$1".tmp
99 sed -n "${line}{s/^pick/$action/; p;}" < "$1".tmp >> "$1"
100 action=pick;;
101 esac
102 done
105 test_set_editor "$(pwd)/fake-editor.sh"
106 chmod a+x fake-editor.sh
108 test_expect_success 'no changes are a nop' '
109 git rebase -i F &&
110 test $(git rev-parse I) = $(git rev-parse HEAD)
113 test_expect_success 'test the [branch] option' '
114 git checkout -b dead-end &&
115 git rm file6 &&
116 git commit -m "stop here" &&
117 git rebase -i F branch2 &&
118 test $(git rev-parse I) = $(git rev-parse HEAD)
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 rev-parse I) = $(git rev-parse HEAD~2)
129 test_expect_success 'reflog for the branch shows state before rebase' '
130 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
133 test_expect_success 'exchange two commits' '
134 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
135 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
136 test G = $(git cat-file commit HEAD | sed -ne \$p)
139 cat > expect << EOF
140 diff --git a/file1 b/file1
141 index e69de29..00750ed 100644
142 --- a/file1
143 +++ b/file1
144 @@ -0,0 +1 @@
148 cat > expect2 << EOF
149 <<<<<<< HEAD:file1
151 =======
153 >>>>>>> b7ca976... G:file1
156 test_expect_success 'stop on conflicting pick' '
157 git tag new-branch1 &&
158 ! git rebase -i master &&
159 test_cmp expect .git/.dotest-merge/patch &&
160 test_cmp expect2 file1 &&
161 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
162 test 0 = $(grep -c "^[^#]" < .git/.dotest-merge/git-rebase-todo)
165 test_expect_success 'abort' '
166 git rebase --abort &&
167 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
168 ! test -d .git/.dotest-merge
171 test_expect_success 'retain authorship' '
172 echo A > file7 &&
173 git add file7 &&
174 test_tick &&
175 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
176 git tag twerp &&
177 git rebase -i --onto master HEAD^ &&
178 git show HEAD | grep "^Author: Twerp Snog"
181 test_expect_success 'squash' '
182 git reset --hard twerp &&
183 echo B > file7 &&
184 test_tick &&
185 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
186 echo "******************************" &&
187 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
188 test B = $(cat file7) &&
189 test $(git rev-parse HEAD^) = $(git rev-parse master)
192 test_expect_success 'retain authorship when squashing' '
193 git show HEAD | grep "^Author: Twerp Snog"
196 test_expect_success '-p handles "no changes" gracefully' '
197 HEAD=$(git rev-parse HEAD) &&
198 git rebase -i -p HEAD^ &&
199 test $HEAD = $(git rev-parse HEAD)
202 test_expect_success 'setting marks works' '
203 git checkout master &&
204 FAKE_LINES="mark:0 2 1 mark:42 3 edit 4" git rebase -i HEAD~4 &&
205 marks_dir=.git/refs/rebase-marks &&
206 test -d $marks_dir &&
207 test $(ls $marks_dir | wc -l) -eq 2 &&
208 test "$(git rev-parse HEAD~4)" = \
209 "$(git rev-parse refs/rebase-marks/0)" &&
210 test "$(git rev-parse HEAD~2)" = \
211 "$(git rev-parse refs/rebase-marks/42)" &&
212 git rebase --abort &&
213 test 0 = $(ls $marks_dir | wc -l)
216 test_expect_success 'reset with nonexistent mark fails' '
217 export FAKE_LINES="reset:0 1" &&
218 test_must_fail git rebase -i HEAD~1 &&
219 unset FAKE_LINES &&
220 git rebase --abort
223 test_expect_success 'reset to HEAD is a nop' '
224 test_tick &&
225 head=$(git rev-parse --short HEAD) &&
226 FAKE_LINES="reset$head" git rebase -i HEAD~4 &&
227 test "$(git rev-parse --short HEAD)" = "$head"
230 test_expect_success 'merge redoes merges' '
231 test_tick &&
232 git merge dead-end &&
233 merge=$(git rev-parse HEAD) &&
234 git reset --hard HEAD~1 &&
235 FAKE_LINES="1 merge$merge/dead-end" git rebase -i HEAD~1 &&
236 test $merge = "$(git rev-parse HEAD)" &&
237 git reset --hard HEAD~1
240 test_expect_success 'preserve merges with -p' '
241 git checkout -b to-be-preserved master^ &&
242 : > unrelated-file &&
243 git add unrelated-file &&
244 test_tick &&
245 git commit -m "unrelated" &&
246 git checkout -b to-be-rebased master &&
247 echo B > file1 &&
248 test_tick &&
249 git commit -m J file1 &&
250 test_tick &&
251 git merge to-be-preserved &&
252 echo C > file1 &&
253 test_tick &&
254 git commit -m K file1 &&
255 test_tick &&
256 git rebase -i -p --onto branch1 master &&
257 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
258 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
259 test $(git show HEAD:file1) = C &&
260 test $(git show HEAD~2:file1) = B
263 test_expect_success 'rebase with preserve merge forth and back is a noop' '
264 git checkout -b big-branch-1 master &&
265 test_tick &&
266 : > bb1a &&
267 git add bb1a &&
268 git commit -m "big branch commit 1" &&
269 : > bb1b &&
270 git add bb1b &&
271 git commit -m "big branch commit 2" &&
272 : > bb1c &&
273 git add bb1c &&
274 git commit -m "big branch commit 3" &&
275 git checkout -b big-branch-2 master &&
276 : > bb2a &&
277 git add bb2a &&
278 git commit -m "big branch commit 4" &&
279 : > bb2b &&
280 git add bb2b &&
281 git commit -m "big branch commit 5" &&
282 git merge big-branch-1~1 &&
283 git merge to-be-preserved &&
284 tbp_merge=$(git rev-parse HEAD) &&
285 : > bb2c &&
286 git add bb2c &&
287 git commit -m "big branch commit 6" &&
288 git merge big-branch-1 &&
289 head=$(git rev-parse HEAD) &&
290 FAKE_LINES="16 6 19 20 4 1 2 5 22" \
291 git rebase -i -p --onto dead-end master &&
292 test "$head" != "$(git rev-parse HEAD)" &&
293 FAKE_LINES="3 7 mark:10 8 9 5 1 2 merge$tbp_merge~1/:10 \
294 merge$tbp_merge/to-be-preserved 6 11" \
295 git rebase -i -p --onto master dead-end &&
296 test "$head" = "$(git rev-parse HEAD)"
299 test_expect_success 'interactive --first-parent gives a linear list' '
300 head=$(git rev-parse HEAD) &&
301 EXPECT_COUNT=6 FAKE_LINES="2 1 4 3 6 5" \
302 git rebase -i -f --onto dead-end master &&
303 test "$head" != "$(git rev-parse HEAD)" &&
304 git rev-parse HEAD^^2 &&
305 test "$(git rev-parse HEAD~6)" = "$(git rev-parse dead-end)" &&
306 EXPECT_COUNT=6 FAKE_LINES="2 1 4 3 6 5" \
307 git rebase -i -f --onto master dead-end &&
308 test "$head" = "$(git rev-parse HEAD)"
311 test_expect_success 'tag sets tags' '
312 head=$(git rev-parse HEAD) &&
313 FAKE_LINES="1 2 3 4 5 tagbb-tag1 6 7 8 9 10 11 12 13 14 15 \
314 tagbb-tag2 16 tagbb-tag3a tagbb-tag3b 17 18 19 20 21 22" \
315 EXPECT_COUNT=22 git rebase -i -p master &&
316 test "$head" = "$(git rev-parse HEAD)" &&
317 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
318 "$(git rev-parse HEAD^2~2 HEAD~2 HEAD~1 HEAD~1)"
321 test_expect_success 'interactive -t preserves tags' '
322 git rebase -i -p -t --onto dead-end master &&
323 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
324 "$(git rev-parse HEAD^2~2 HEAD~2 HEAD~1 HEAD~1)" &&
325 head=$(git rev-parse HEAD) &&
326 git rebase -i -t dead-end &&
327 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
328 "$(git rev-parse HEAD~7 $head~2 HEAD~1 HEAD~1)"
331 test_expect_success '--continue tries to commit' '
332 git checkout to-be-rebased &&
333 test_tick &&
334 ! git rebase -i --onto new-branch1 HEAD^ &&
335 echo resolved > file1 &&
336 git add file1 &&
337 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
338 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
339 git show HEAD | grep chouette
342 test_expect_success 'verbose flag is heeded, even after --continue' '
343 git reset --hard HEAD@{1} &&
344 test_tick &&
345 ! git rebase -v -i --onto new-branch1 HEAD^ &&
346 echo resolved > file1 &&
347 git add file1 &&
348 git rebase --continue > output &&
349 grep "^ file1 | 2 +-$" output
352 test_expect_success 'multi-squash only fires up editor once' '
353 base=$(git rev-parse HEAD~4) &&
354 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
355 git rebase -i $base &&
356 test $base = $(git rev-parse HEAD^) &&
357 test 1 = $(git show | grep ONCE | wc -l)
360 test_expect_success 'squash works as expected' '
361 for n in one two three four
363 echo $n >> file$n &&
364 git add file$n &&
365 git commit -m $n
366 done &&
367 one=$(git rev-parse HEAD~3) &&
368 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
369 test $one = $(git rev-parse HEAD~2)
372 test_expect_success 'interrupted squash works as expected' '
373 for n in one two three four
375 echo $n >> conflict &&
376 git add conflict &&
377 git commit -m $n
378 done &&
379 one=$(git rev-parse HEAD~3) &&
380 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
381 (echo one; echo two; echo four) > conflict &&
382 git add conflict &&
383 ! git rebase --continue &&
384 echo resolved > conflict &&
385 git add conflict &&
386 git rebase --continue &&
387 test $one = $(git rev-parse HEAD~2)
390 test_expect_success 'interrupted squash works as expected (case 2)' '
391 for n in one two three four
393 echo $n >> conflict &&
394 git add conflict &&
395 git commit -m $n
396 done &&
397 one=$(git rev-parse HEAD~3) &&
398 ! FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
399 (echo one; echo four) > conflict &&
400 git add conflict &&
401 ! git rebase --continue &&
402 (echo one; echo two; echo four) > conflict &&
403 git add conflict &&
404 ! git rebase --continue &&
405 echo resolved > conflict &&
406 git add conflict &&
407 git rebase --continue &&
408 test $one = $(git rev-parse HEAD~2)
411 test_expect_success 'ignore patch if in upstream' '
412 HEAD=$(git rev-parse HEAD) &&
413 git checkout -b has-cherry-picked HEAD^ &&
414 echo unrelated > file7 &&
415 git add file7 &&
416 test_tick &&
417 git commit -m "unrelated change" &&
418 git cherry-pick $HEAD &&
419 EXPECT_COUNT=1 git rebase -i $HEAD &&
420 test $HEAD = $(git rev-parse HEAD^)
423 test_expect_success '--continue tries to commit, even for "edit"' '
424 parent=$(git rev-parse HEAD^) &&
425 test_tick &&
426 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
427 echo edited > file7 &&
428 git add file7 &&
429 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
430 test edited = $(git show HEAD:file7) &&
431 git show HEAD | grep chouette &&
432 test $parent = $(git rev-parse HEAD^)
435 test_expect_success 'rebase a detached HEAD' '
436 grandparent=$(git rev-parse HEAD~2) &&
437 git checkout $(git rev-parse HEAD) &&
438 test_tick &&
439 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
440 test $grandparent = $(git rev-parse HEAD~2)
443 test_expect_success 'rebase a commit violating pre-commit' '
445 mkdir -p .git/hooks &&
446 PRE_COMMIT=.git/hooks/pre-commit &&
447 echo "#!/bin/sh" > $PRE_COMMIT &&
448 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
449 chmod a+x $PRE_COMMIT &&
450 echo "monde! " >> file1 &&
451 test_tick &&
452 ! git commit -m doesnt-verify file1 &&
453 git commit -m doesnt-verify --no-verify file1 &&
454 test_tick &&
455 FAKE_LINES=2 git rebase -i HEAD~2
459 test_expect_success 'rebase with a file named HEAD in worktree' '
461 rm -fr .git/hooks &&
462 git reset --hard &&
463 git checkout -b branch3 A &&
466 GIT_AUTHOR_NAME="Squashed Away" &&
467 export GIT_AUTHOR_NAME &&
468 >HEAD &&
469 git add HEAD &&
470 git commit -m "Add head" &&
471 >BODY &&
472 git add BODY &&
473 git commit -m "Add body"
474 ) &&
476 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
477 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
481 test_done