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
66test "\$1" = .git/COMMIT_EDITMSG && {
67 test -z "\$FAKE_COMMIT_MESSAGE" || echo "\$FAKE_COMMIT_MESSAGE" > "\$1"
68 test -z "\$FAKE_COMMIT_AMEND" || echo "\$FAKE_COMMIT_AMEND" >> "\$1"
69 exit
70}
71test -z "\$FAKE_LINES" && exit
72grep -v "^#" < "\$1" > "\$1".tmp
73rm "\$1"
74cat "\$1".tmp
75action=pick
76for line in \$FAKE_LINES; do
77 case \$line in
78 squash)
79 action="\$line";;
80 *)
81 echo sed -n "\${line}s/^pick/\$action/p"
82 sed -n "\${line}p" < "\$1".tmp
83 sed -n "\${line}s/^pick/\$action/p" < "\$1".tmp >> "\$1"
84 action=pick;;
85 esac
86done
87EOF
88
89chmod a+x fake-editor.sh
90VISUAL="$(pwd)/fake-editor.sh"
91export VISUAL
92
93test_expect_success 'no changes are a nop' '
94 git rebase -i F &&
95 test $(git rev-parse I) = $(git rev-parse HEAD)
96'
97
98test_expect_success 'rebase on top of a non-conflicting commit' '
99 git checkout branch1 &&
100 git tag original-branch1 &&
101 git rebase -i branch2 &&
102 test file6 = $(git diff --name-only original-branch1) &&
103 test $(git rev-parse I) = $(git rev-parse HEAD~2)
104'
105
106test_expect_success 'reflog for the branch shows state before rebase' '
107 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
108'
109
110test_expect_success 'exchange two commits' '
111 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
112 test H = $(git cat-file commit HEAD^ | tail -n 1) &&
113 test G = $(git cat-file commit HEAD | tail -n 1)
114'
115
116cat > expect << EOF
117diff --git a/file1 b/file1
118index e69de29..00750ed 100644
119--- a/file1
120+++ b/file1
121@@ -0,0 +1 @@
122+3
123EOF
124
125cat > expect2 << EOF
126<<<<<<< HEAD:file1
1272
128=======
1293
130>>>>>>> b7ca976... G:file1
131EOF
132
133test_expect_success 'stop on conflicting pick' '
134 git tag new-branch1 &&
135 ! git rebase -i master &&
136 diff -u expect .git/.dotest-merge/patch &&
137 diff -u expect2 file1 &&
138 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
139 test 0 = $(grep -v "^#" < .git/.dotest-merge/todo | wc -l)
140'
141
142test_expect_success 'abort' '
143 git rebase --abort &&
144 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
145 ! test -d .git/.dotest-merge
146'
147
148test_expect_success 'retain authorship' '
149 echo A > file7 &&
150 git add file7 &&
151 test_tick &&
152 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
153 git tag twerp &&
154 git rebase -i --onto master HEAD^ &&
155 git show HEAD | grep "^Author: Twerp Snog"
156'
157
158test_expect_success 'squash' '
159 git reset --hard twerp &&
160 echo B > file7 &&
161 test_tick &&
162 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
163 echo "******************************" &&
164 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
165 test B = $(cat file7) &&
166 test $(git rev-parse HEAD^) = $(git rev-parse master)
167'
168
169test_expect_success 'retain authorship when squashing' '
170 git show HEAD | grep "^Author: Nitfol"
171'
172
173test_expect_success 'preserve merges with -p' '
174 git checkout -b to-be-preserved master^ &&
175 : > unrelated-file &&
176 git add unrelated-file &&
177 test_tick &&
178 git commit -m "unrelated" &&
179 git checkout -b to-be-rebased master &&
180 echo B > file1 &&
181 test_tick &&
182 git commit -m J file1 &&
183 test_tick &&
184 git merge to-be-preserved &&
185 echo C > file1 &&
186 test_tick &&
187 git commit -m K file1 &&
188 test_tick &&
189 git rebase -i -p --onto branch1 master &&
190 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
191 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
192 test $(git show HEAD:file1) = C &&
193 test $(git show HEAD~2:file1) = B
194'
195
196test_expect_success '--continue tries to commit' '
197 test_tick &&
198 ! git rebase -i --onto new-branch1 HEAD^ &&
199 echo resolved > file1 &&
200 git add file1 &&
201 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
202 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
203 git show HEAD | grep chouette
204'
205
206test_expect_success 'verbose flag is heeded, even after --continue' '
207 git reset --hard HEAD@{1} &&
208 test_tick &&
209 ! git rebase -v -i --onto new-branch1 HEAD^ &&
210 echo resolved > file1 &&
211 git add file1 &&
212 git rebase --continue > output &&
213 grep "^ file1 | 2 +-$" output
214'
215
216test_expect_success 'multi-squash only fires up editor once' '
217 base=$(git rev-parse HEAD~4) &&
218 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
219 git rebase -i $base &&
220 test $base = $(git rev-parse HEAD^) &&
221 test 1 = $(git show | grep ONCE | wc -l)
222'
223
224test_done