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
64cat > fake-editor.sh <<\EOF
65#!/bin/sh
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
94chmod a+x fake-editor.sh
95VISUAL="$(pwd)/fake-editor.sh"
96export VISUAL
97
98test_expect_success 'no changes are a nop' '
99 git rebase -i F &&
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 rev-parse I) = $(git rev-parse HEAD)
109'
110
111test_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)
117'
118
119test_expect_success 'reflog for the branch shows state before rebase' '
120 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
121'
122
123test_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)
127'
128
129cat > expect << EOF
130diff --git a/file1 b/file1
131index e69de29..00750ed 100644
132--- a/file1
133+++ b/file1
134@@ -0,0 +1 @@
135+3
136EOF
137
138cat > expect2 << EOF
139<<<<<<< HEAD:file1
1402
141=======
1423
143>>>>>>> b7ca976... G:file1
144EOF
145
146test_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 |
153 wc -l)
154'
155
156test_expect_success 'abort' '
157 git rebase --abort &&
158 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
159 ! test -d .git/.dotest-merge
160'
161
162test_expect_success 'retain authorship' '
163 echo A > file7 &&
164 git add file7 &&
165 test_tick &&
166 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
167 git tag twerp &&
168 git rebase -i --onto master HEAD^ &&
169 git show HEAD | grep "^Author: Twerp Snog"
170'
171
172test_expect_success 'squash' '
173 git reset --hard twerp &&
174 echo B > file7 &&
175 test_tick &&
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)
181'
182
183test_expect_success 'retain authorship when squashing' '
184 git show HEAD | grep "^Author: Twerp Snog"
185'
186
187test_expect_success 'preserve merges with -p' '
188 git checkout -b to-be-preserved master^ &&
189 : > unrelated-file &&
190 git add unrelated-file &&
191 test_tick &&
192 git commit -m "unrelated" &&
193 git checkout -b to-be-rebased master &&
194 echo B > file1 &&
195 test_tick &&
196 git commit -m J file1 &&
197 test_tick &&
198 git merge to-be-preserved &&
199 echo C > file1 &&
200 test_tick &&
201 git commit -m K file1 &&
202 test_tick &&
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
208'
209
210test_expect_success '--continue tries to commit' '
211 test_tick &&
212 ! git rebase -i --onto new-branch1 HEAD^ &&
213 echo resolved > file1 &&
214 git add 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
218'
219
220test_expect_success 'verbose flag is heeded, even after --continue' '
221 git reset --hard HEAD@{1} &&
222 test_tick &&
223 ! git rebase -v -i --onto new-branch1 HEAD^ &&
224 echo resolved > file1 &&
225 git add file1 &&
226 git rebase --continue > output &&
227 grep "^ file1 | 2 +-$" output
228'
229
230test_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)
236'
237
238test_expect_success 'squash works as expected' '
239 for n in one two three four
240 do
241 echo $n >> file$n &&
242 git add file$n &&
243 git commit -m $n
244 done &&
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)
248'
249
250test_expect_success 'interrupted squash works as expected' '
251 for n in one two three four
252 do
253 echo $n >> conflict &&
254 git add conflict &&
255 git commit -m $n
256 done &&
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 &&
260 git add conflict &&
261 ! git rebase --continue &&
262 echo resolved > conflict &&
263 git add conflict &&
264 git rebase --continue &&
265 test $one = $(git rev-parse HEAD~2)
266'
267
268test_expect_success 'interrupted squash works as expected (case 2)' '
269 for n in one two three four
270 do
271 echo $n >> conflict &&
272 git add conflict &&
273 git commit -m $n
274 done &&
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 &&
278 git add conflict &&
279 ! git rebase --continue &&
280 (echo one; echo two; echo four) > conflict &&
281 git add conflict &&
282 ! git rebase --continue &&
283 echo resolved > conflict &&
284 git add conflict &&
285 git rebase --continue &&
286 test $one = $(git rev-parse HEAD~2)
287'
288
289test_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 &&
293 git add file7 &&
294 test_tick &&
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^)
299'
300
301test_expect_success '--continue tries to commit, even for "edit"' '
302 parent=$(git rev-parse HEAD^) &&
303 test_tick &&
304 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
305 echo edited > file7 &&
306 git add 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^)
311'
312
313test_expect_success 'rebase a detached HEAD' '
314 grandparent=$(git rev-parse HEAD~2) &&
315 git checkout $(git rev-parse HEAD) &&
316 test_tick &&
317 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
318 test $grandparent = $(git rev-parse HEAD~2)
319'
320
321test_done