1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E. Schindelin
4#
5
6test_description='git rebase interactive
7
8This test runs git rebase "interactively", by faking an edit, and verifies
9that the result still makes sense.
10'
11. ./test-lib.sh
12
13# set up two branches like this:
14#
15# A - B - C - D - E
16# \
17# F - G - H
18# \
19# I
20#
21# where B, D and G touch the same file.
22
23test_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
62'
63
64echo "#!$SHELL_PATH" >fake-editor.sh
65cat >> fake-editor.sh <<\EOF
66case "$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
71 ;;
72esac
73test -z "$EXPECT_COUNT" ||
74 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
75 exit
76test -z "$FAKE_LINES" && exit
77grep -v '^#' < "$1" > "$1".tmp
78rm -f "$1"
79cat "$1".tmp
80action=pick
81for line in $FAKE_LINES; do
82 case $line in
83 squash|edit)
84 action="$line";;
85 *)
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"
89 action=pick;;
90 esac
91done
92EOF
93
94test_set_editor "$(pwd)/fake-editor.sh"
95chmod a+x fake-editor.sh
96
97test_expect_success 'no changes are a nop' '
98 git rebase -i F &&
99 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
100 test $(git rev-parse I) = $(git rev-parse HEAD)
101'
102
103test_expect_success 'test the [branch] option' '
104 git checkout -b dead-end &&
105 git rm file6 &&
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)
111'
112
113test_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)
119'
120
121test_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)
129'
130
131test_expect_success 'reflog for the branch shows state before rebase' '
132 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
133'
134
135test_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)
139'
140
141cat > expect << EOF
142diff --git a/file1 b/file1
143index e69de29..00750ed 100644
144--- a/file1
145+++ b/file1
146@@ -0,0 +1 @@
147+3
148EOF
149
150cat > expect2 << EOF
151<<<<<<< HEAD:file1
1522
153=======
1543
155>>>>>>> b7ca976... G:file1
156EOF
157
158test_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)
168'
169
170test_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
175'
176
177test_expect_success 'retain authorship' '
178 echo A > file7 &&
179 git add file7 &&
180 test_tick &&
181 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
182 git tag twerp &&
183 git rebase -i --onto master HEAD^ &&
184 git show HEAD | grep "^Author: Twerp Snog"
185'
186
187test_expect_success 'squash' '
188 git reset --hard twerp &&
189 echo B > file7 &&
190 test_tick &&
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)
196'
197
198test_expect_success 'retain authorship when squashing' '
199 git show HEAD | grep "^Author: Twerp Snog"
200'
201
202test_expect_success '-p handles "no changes" gracefully' '
203 HEAD=$(git rev-parse HEAD) &&
204 git rebase -i -p HEAD^ &&
205 test $HEAD = $(git rev-parse HEAD)
206'
207
208test_expect_success 'preserve merges with -p' '
209 git checkout -b to-be-preserved master^ &&
210 : > unrelated-file &&
211 git add unrelated-file &&
212 test_tick &&
213 git commit -m "unrelated" &&
214 git checkout -b another-branch master &&
215 echo B > file1 &&
216 test_tick &&
217 git commit -m J file1 &&
218 test_tick &&
219 git merge to-be-preserved &&
220 echo C > file1 &&
221 test_tick &&
222 git commit -m K file1 &&
223 echo D > file1 &&
224 test_tick &&
225 git commit -m L1 file1 &&
226 git checkout HEAD^ &&
227 echo 1 > unrelated-file &&
228 test_tick &&
229 git commit -m L2 unrelated-file &&
230 test_tick &&
231 git merge another-branch &&
232 echo E > file1 &&
233 test_tick &&
234 git commit -m M file1 &&
235 git checkout -b to-be-rebased &&
236 test_tick &&
237 git rebase -i -p --onto branch1 master &&
238 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
239 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
240 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
241 test $(git show HEAD~5:file1) = B &&
242 test $(git show HEAD~3:file1) = C &&
243 test $(git show HEAD:file1) = E &&
244 test $(git show HEAD:unrelated-file) = 1
245'
246
247test_expect_success '--continue tries to commit' '
248 test_tick &&
249 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
250 echo resolved > file1 &&
251 git add file1 &&
252 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
253 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
254 git show HEAD | grep chouette
255'
256
257test_expect_success 'verbose flag is heeded, even after --continue' '
258 git reset --hard HEAD@{1} &&
259 test_tick &&
260 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
261 echo resolved > file1 &&
262 git add file1 &&
263 git rebase --continue > output &&
264 grep "^ file1 | 2 +-$" output
265'
266
267test_expect_success 'multi-squash only fires up editor once' '
268 base=$(git rev-parse HEAD~4) &&
269 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
270 git rebase -i $base &&
271 test $base = $(git rev-parse HEAD^) &&
272 test 1 = $(git show | grep ONCE | wc -l)
273'
274
275test_expect_success 'squash works as expected' '
276 for n in one two three four
277 do
278 echo $n >> file$n &&
279 git add file$n &&
280 git commit -m $n
281 done &&
282 one=$(git rev-parse HEAD~3) &&
283 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
284 test $one = $(git rev-parse HEAD~2)
285'
286
287test_expect_success 'interrupted squash works as expected' '
288 for n in one two three four
289 do
290 echo $n >> conflict &&
291 git add conflict &&
292 git commit -m $n
293 done &&
294 one=$(git rev-parse HEAD~3) &&
295 (
296 FAKE_LINES="1 squash 3 2" &&
297 export FAKE_LINES &&
298 test_must_fail git rebase -i HEAD~3
299 ) &&
300 (echo one; echo two; echo four) > conflict &&
301 git add conflict &&
302 test_must_fail git rebase --continue &&
303 echo resolved > conflict &&
304 git add conflict &&
305 git rebase --continue &&
306 test $one = $(git rev-parse HEAD~2)
307'
308
309test_expect_success 'interrupted squash works as expected (case 2)' '
310 for n in one two three four
311 do
312 echo $n >> conflict &&
313 git add conflict &&
314 git commit -m $n
315 done &&
316 one=$(git rev-parse HEAD~3) &&
317 (
318 FAKE_LINES="3 squash 1 2" &&
319 export FAKE_LINES &&
320 test_must_fail git rebase -i HEAD~3
321 ) &&
322 (echo one; echo four) > conflict &&
323 git add conflict &&
324 test_must_fail git rebase --continue &&
325 (echo one; echo two; echo four) > conflict &&
326 git add conflict &&
327 test_must_fail git rebase --continue &&
328 echo resolved > conflict &&
329 git add conflict &&
330 git rebase --continue &&
331 test $one = $(git rev-parse HEAD~2)
332'
333
334test_expect_success 'ignore patch if in upstream' '
335 HEAD=$(git rev-parse HEAD) &&
336 git checkout -b has-cherry-picked HEAD^ &&
337 echo unrelated > file7 &&
338 git add file7 &&
339 test_tick &&
340 git commit -m "unrelated change" &&
341 git cherry-pick $HEAD &&
342 EXPECT_COUNT=1 git rebase -i $HEAD &&
343 test $HEAD = $(git rev-parse HEAD^)
344'
345
346test_expect_success '--continue tries to commit, even for "edit"' '
347 parent=$(git rev-parse HEAD^) &&
348 test_tick &&
349 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
350 echo edited > file7 &&
351 git add file7 &&
352 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
353 test edited = $(git show HEAD:file7) &&
354 git show HEAD | grep chouette &&
355 test $parent = $(git rev-parse HEAD^)
356'
357
358test_expect_success 'rebase a detached HEAD' '
359 grandparent=$(git rev-parse HEAD~2) &&
360 git checkout $(git rev-parse HEAD) &&
361 test_tick &&
362 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
363 test $grandparent = $(git rev-parse HEAD~2)
364'
365
366test_expect_success 'rebase a commit violating pre-commit' '
367
368 mkdir -p .git/hooks &&
369 PRE_COMMIT=.git/hooks/pre-commit &&
370 echo "#!/bin/sh" > $PRE_COMMIT &&
371 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
372 chmod a+x $PRE_COMMIT &&
373 echo "monde! " >> file1 &&
374 test_tick &&
375 test_must_fail git commit -m doesnt-verify file1 &&
376 git commit -m doesnt-verify --no-verify file1 &&
377 test_tick &&
378 FAKE_LINES=2 git rebase -i HEAD~2
379
380'
381
382test_expect_success 'rebase with a file named HEAD in worktree' '
383
384 rm -fr .git/hooks &&
385 git reset --hard &&
386 git checkout -b branch3 A &&
387
388 (
389 GIT_AUTHOR_NAME="Squashed Away" &&
390 export GIT_AUTHOR_NAME &&
391 >HEAD &&
392 git add HEAD &&
393 git commit -m "Add head" &&
394 >BODY &&
395 git add BODY &&
396 git commit -m "Add body"
397 ) &&
398
399 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
400 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
401
402'
403
404test_done