t4200: use cut instead of sed
[git/peff.git] / t / t3404-rebase-interactive.sh
blob9cf873f7ebfa8f8a4fd215cbbb468f955d6a47e3
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" && 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";;
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
91 done
92 EOF
94 chmod a+x fake-editor.sh
95 VISUAL="$(pwd)/fake-editor.sh"
96 export VISUAL
98 test_expect_success 'no changes are a nop' '
99 git rebase -i F &&
100 test $(git rev-parse I) = $(git rev-parse HEAD)
103 test_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)
111 test_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)
119 test_expect_success 'reflog for the branch shows state before rebase' '
120 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
123 test_expect_success 'exchange two commits' '
124 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
125 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
126 test G = $(git cat-file commit HEAD | sed -ne \$p)
129 cat > expect << EOF
130 diff --git a/file1 b/file1
131 index e69de29..00750ed 100644
132 --- a/file1
133 +++ b/file1
134 @@ -0,0 +1 @@
138 cat > expect2 << EOF
139 <<<<<<< HEAD:file1
141 =======
143 >>>>>>> b7ca976... G:file1
146 test_expect_success 'stop on conflicting pick' '
147 git tag new-branch1 &&
148 ! git rebase -i master &&
149 test_cmp expect .git/.dotest-merge/patch &&
150 test_cmp expect2 file1 &&
151 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
152 test 0 = $(grep -c "^[^#]" < .git/.dotest-merge/git-rebase-todo)
155 test_expect_success 'abort' '
156 git rebase --abort &&
157 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
158 ! test -d .git/.dotest-merge
161 test_expect_success 'retain authorship' '
162 echo A > file7 &&
163 git add file7 &&
164 test_tick &&
165 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
166 git tag twerp &&
167 git rebase -i --onto master HEAD^ &&
168 git show HEAD | grep "^Author: Twerp Snog"
171 test_expect_success 'squash' '
172 git reset --hard twerp &&
173 echo B > file7 &&
174 test_tick &&
175 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
176 echo "******************************" &&
177 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
178 test B = $(cat file7) &&
179 test $(git rev-parse HEAD^) = $(git rev-parse master)
182 test_expect_success 'retain authorship when squashing' '
183 git show HEAD | grep "^Author: Twerp Snog"
186 test_expect_success '-p handles "no changes" gracefully' '
187 HEAD=$(git rev-parse HEAD) &&
188 git rebase -i -p HEAD^ &&
189 test $HEAD = $(git rev-parse HEAD)
192 test_expect_success 'preserve merges with -p' '
193 git checkout -b to-be-preserved master^ &&
194 : > unrelated-file &&
195 git add unrelated-file &&
196 test_tick &&
197 git commit -m "unrelated" &&
198 git checkout -b to-be-rebased master &&
199 echo B > file1 &&
200 test_tick &&
201 git commit -m J file1 &&
202 test_tick &&
203 git merge to-be-preserved &&
204 echo C > file1 &&
205 test_tick &&
206 git commit -m K file1 &&
207 test_tick &&
208 git rebase -i -p --onto branch1 master &&
209 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
210 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
211 test $(git show HEAD:file1) = C &&
212 test $(git show HEAD~2:file1) = B
215 test_expect_success '--continue tries to commit' '
216 test_tick &&
217 ! git rebase -i --onto new-branch1 HEAD^ &&
218 echo resolved > file1 &&
219 git add file1 &&
220 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
221 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
222 git show HEAD | grep chouette
225 test_expect_success 'verbose flag is heeded, even after --continue' '
226 git reset --hard HEAD@{1} &&
227 test_tick &&
228 ! git rebase -v -i --onto new-branch1 HEAD^ &&
229 echo resolved > file1 &&
230 git add file1 &&
231 git rebase --continue > output &&
232 grep "^ file1 | 2 +-$" output
235 test_expect_success 'multi-squash only fires up editor once' '
236 base=$(git rev-parse HEAD~4) &&
237 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
238 git rebase -i $base &&
239 test $base = $(git rev-parse HEAD^) &&
240 test 1 = $(git show | grep ONCE | wc -l)
243 test_expect_success 'squash works as expected' '
244 for n in one two three four
246 echo $n >> file$n &&
247 git add file$n &&
248 git commit -m $n
249 done &&
250 one=$(git rev-parse HEAD~3) &&
251 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
252 test $one = $(git rev-parse HEAD~2)
255 test_expect_success 'interrupted squash works as expected' '
256 for n in one two three four
258 echo $n >> conflict &&
259 git add conflict &&
260 git commit -m $n
261 done &&
262 one=$(git rev-parse HEAD~3) &&
263 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
264 (echo one; echo two; echo four) > conflict &&
265 git add conflict &&
266 ! git rebase --continue &&
267 echo resolved > conflict &&
268 git add conflict &&
269 git rebase --continue &&
270 test $one = $(git rev-parse HEAD~2)
273 test_expect_success 'interrupted squash works as expected (case 2)' '
274 for n in one two three four
276 echo $n >> conflict &&
277 git add conflict &&
278 git commit -m $n
279 done &&
280 one=$(git rev-parse HEAD~3) &&
281 ! FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
282 (echo one; echo four) > conflict &&
283 git add conflict &&
284 ! git rebase --continue &&
285 (echo one; echo two; echo four) > conflict &&
286 git add conflict &&
287 ! git rebase --continue &&
288 echo resolved > conflict &&
289 git add conflict &&
290 git rebase --continue &&
291 test $one = $(git rev-parse HEAD~2)
294 test_expect_success 'ignore patch if in upstream' '
295 HEAD=$(git rev-parse HEAD) &&
296 git checkout -b has-cherry-picked HEAD^ &&
297 echo unrelated > file7 &&
298 git add file7 &&
299 test_tick &&
300 git commit -m "unrelated change" &&
301 git cherry-pick $HEAD &&
302 EXPECT_COUNT=1 git rebase -i $HEAD &&
303 test $HEAD = $(git rev-parse HEAD^)
306 test_expect_success '--continue tries to commit, even for "edit"' '
307 parent=$(git rev-parse HEAD^) &&
308 test_tick &&
309 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
310 echo edited > file7 &&
311 git add file7 &&
312 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
313 test edited = $(git show HEAD:file7) &&
314 git show HEAD | grep chouette &&
315 test $parent = $(git rev-parse HEAD^)
318 test_expect_success 'rebase a detached HEAD' '
319 grandparent=$(git rev-parse HEAD~2) &&
320 git checkout $(git rev-parse HEAD) &&
321 test_tick &&
322 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
323 test $grandparent = $(git rev-parse HEAD~2)
326 test_expect_success 'rebase a commit violating pre-commit' '
328 mkdir -p .git/hooks &&
329 PRE_COMMIT=.git/hooks/pre-commit &&
330 echo "#!/bin/sh" > $PRE_COMMIT &&
331 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
332 chmod a+x $PRE_COMMIT &&
333 echo "monde! " >> file1 &&
334 test_tick &&
335 ! git commit -m doesnt-verify file1 &&
336 git commit -m doesnt-verify --no-verify file1 &&
337 test_tick &&
338 FAKE_LINES=2 git rebase -i HEAD~2
342 test_expect_success 'rebase with a file named HEAD in worktree' '
344 rm -fr .git/hooks &&
345 git reset --hard &&
346 git checkout -b branch3 A &&
349 GIT_AUTHOR_NAME="Squashed Away" &&
350 export GIT_AUTHOR_NAME &&
351 >HEAD &&
352 git add HEAD &&
353 git commit -m "Add head" &&
354 >BODY &&
355 git add BODY &&
356 git commit -m "Add body"
357 ) &&
359 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
360 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
364 test_done