dc436d768e20f368fb4f517a2ba528e6da8f27a5
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E. Schindelin
4 #
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.
10 '
11 . ./test-lib.sh
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.
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
62 '
64 cat > fake-editor.sh << EOF
65 #!/bin/sh
66 test "\$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 }
71 test -z "\$EXPECT_COUNT" ||
72 test "\$EXPECT_COUNT" = \$(grep -ve "^#" -e "^$" < "\$1" | wc -l) ||
73 exit
74 test -z "\$FAKE_LINES" && exit
75 grep -v "^#" < "\$1" > "\$1".tmp
76 rm "\$1"
77 cat "\$1".tmp
78 action=pick
79 for line in \$FAKE_LINES; do
80 case \$line in
81 squash)
82 action="\$line";;
83 *)
84 echo sed -n "\${line}s/^pick/\$action/p"
85 sed -n "\${line}p" < "\$1".tmp
86 sed -n "\${line}s/^pick/\$action/p" < "\$1".tmp >> "\$1"
87 action=pick;;
88 esac
89 done
90 EOF
92 chmod a+x fake-editor.sh
93 VISUAL="$(pwd)/fake-editor.sh"
94 export VISUAL
96 test_expect_success 'no changes are a nop' '
97 git rebase -i F &&
98 test $(git rev-parse I) = $(git rev-parse HEAD)
99 '
101 test_expect_success 'rebase on top of a non-conflicting commit' '
102 git checkout branch1 &&
103 git tag original-branch1 &&
104 git rebase -i branch2 &&
105 test file6 = $(git diff --name-only original-branch1) &&
106 test $(git rev-parse I) = $(git rev-parse HEAD~2)
107 '
109 test_expect_success 'reflog for the branch shows state before rebase' '
110 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
111 '
113 test_expect_success 'exchange two commits' '
114 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
115 test H = $(git cat-file commit HEAD^ | tail -n 1) &&
116 test G = $(git cat-file commit HEAD | tail -n 1)
117 '
119 cat > expect << EOF
120 diff --git a/file1 b/file1
121 index e69de29..00750ed 100644
122 --- a/file1
123 +++ b/file1
124 @@ -0,0 +1 @@
125 +3
126 EOF
128 cat > expect2 << EOF
129 <<<<<<< HEAD:file1
130 2
131 =======
132 3
133 >>>>>>> b7ca976... G:file1
134 EOF
136 test_expect_success 'stop on conflicting pick' '
137 git tag new-branch1 &&
138 ! git rebase -i master &&
139 diff -u expect .git/.dotest-merge/patch &&
140 diff -u expect2 file1 &&
141 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
142 test 0 = $(grep -v "^#" < .git/.dotest-merge/todo | wc -l)
143 '
145 test_expect_success 'abort' '
146 git rebase --abort &&
147 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
148 ! test -d .git/.dotest-merge
149 '
151 test_expect_success 'retain authorship' '
152 echo A > file7 &&
153 git add file7 &&
154 test_tick &&
155 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
156 git tag twerp &&
157 git rebase -i --onto master HEAD^ &&
158 git show HEAD | grep "^Author: Twerp Snog"
159 '
161 test_expect_success 'squash' '
162 git reset --hard twerp &&
163 echo B > file7 &&
164 test_tick &&
165 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
166 echo "******************************" &&
167 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
168 test B = $(cat file7) &&
169 test $(git rev-parse HEAD^) = $(git rev-parse master)
170 '
172 test_expect_success 'retain authorship when squashing' '
173 git show HEAD | grep "^Author: Nitfol"
174 '
176 test_expect_success 'preserve merges with -p' '
177 git checkout -b to-be-preserved master^ &&
178 : > unrelated-file &&
179 git add unrelated-file &&
180 test_tick &&
181 git commit -m "unrelated" &&
182 git checkout -b to-be-rebased master &&
183 echo B > file1 &&
184 test_tick &&
185 git commit -m J file1 &&
186 test_tick &&
187 git merge to-be-preserved &&
188 echo C > file1 &&
189 test_tick &&
190 git commit -m K file1 &&
191 test_tick &&
192 git rebase -i -p --onto branch1 master &&
193 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
194 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
195 test $(git show HEAD:file1) = C &&
196 test $(git show HEAD~2:file1) = B
197 '
199 test_expect_success '--continue tries to commit' '
200 test_tick &&
201 ! git rebase -i --onto new-branch1 HEAD^ &&
202 echo resolved > file1 &&
203 git add file1 &&
204 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
205 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
206 git show HEAD | grep chouette
207 '
209 test_expect_success 'verbose flag is heeded, even after --continue' '
210 git reset --hard HEAD@{1} &&
211 test_tick &&
212 ! git rebase -v -i --onto new-branch1 HEAD^ &&
213 echo resolved > file1 &&
214 git add file1 &&
215 git rebase --continue > output &&
216 grep "^ file1 | 2 +-$" output
217 '
219 test_expect_success 'multi-squash only fires up editor once' '
220 base=$(git rev-parse HEAD~4) &&
221 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
222 git rebase -i $base &&
223 test $base = $(git rev-parse HEAD^) &&
224 test 1 = $(git show | grep ONCE | wc -l)
225 '
227 test_expect_success 'squash works as expected' '
228 for n in one two three four
229 do
230 echo $n >> file$n &&
231 git add file$n &&
232 git commit -m $n
233 done &&
234 one=$(git rev-parse HEAD~3) &&
235 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
236 test $one = $(git rev-parse HEAD~2)
237 '
239 test_expect_success 'interrupted squash works as expected' '
240 for n in one two three four
241 do
242 echo $n >> conflict &&
243 git add conflict &&
244 git commit -m $n
245 done &&
246 one=$(git rev-parse HEAD~3) &&
247 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
248 (echo one; echo two; echo four) > conflict &&
249 git add conflict &&
250 ! git rebase --continue &&
251 echo resolved > conflict &&
252 git add conflict &&
253 git rebase --continue &&
254 test $one = $(git rev-parse HEAD~2)
255 '
257 test_expect_success 'ignore patch if in upstream' '
258 HEAD=$(git rev-parse HEAD) &&
259 git checkout -b has-cherry-picked HEAD^ &&
260 echo unrelated > file7 &&
261 git add file7 &&
262 test_tick &&
263 git commit -m "unrelated change" &&
264 git cherry-pick $HEAD &&
265 EXPECT_COUNT=1 git rebase -i $HEAD &&
266 test $HEAD = $(git rev-parse HEAD^)
267 '
269 test_done