checkout-index needs a working tree
[git/jrn.git] / t / t3404-rebase-interactive.sh
bloba9b552ff087fd6e45419e87f892b42da33ffed98
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 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
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";;
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)
101 test_expect_success 'test the [branch] option' '
102 git checkout -b dead-end &&
103 git rm file6 &&
104 git commit -m "stop here" &&
105 git rebase -i F branch2 &&
106 test $(git rev-parse I) = $(git rev-parse HEAD)
109 test_expect_success 'rebase on top of a non-conflicting commit' '
110 git checkout branch1 &&
111 git tag original-branch1 &&
112 git rebase -i branch2 &&
113 test file6 = $(git diff --name-only original-branch1) &&
114 test $(git rev-parse I) = $(git rev-parse HEAD~2)
117 test_expect_success 'reflog for the branch shows state before rebase' '
118 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
121 test_expect_success 'exchange two commits' '
122 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
123 test H = $(git cat-file commit HEAD^ | tail -n 1) &&
124 test G = $(git cat-file commit HEAD | tail -n 1)
127 cat > expect << EOF
128 diff --git a/file1 b/file1
129 index e69de29..00750ed 100644
130 --- a/file1
131 +++ b/file1
132 @@ -0,0 +1 @@
136 cat > expect2 << EOF
137 <<<<<<< HEAD:file1
139 =======
141 >>>>>>> b7ca976... G:file1
144 test_expect_success 'stop on conflicting pick' '
145 git tag new-branch1 &&
146 ! git rebase -i master &&
147 diff -u expect .git/.dotest-merge/patch &&
148 diff -u expect2 file1 &&
149 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
150 test 0 = $(grep -v "^#" < .git/.dotest-merge/todo | wc -l)
153 test_expect_success 'abort' '
154 git rebase --abort &&
155 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
156 ! test -d .git/.dotest-merge
159 test_expect_success 'retain authorship' '
160 echo A > file7 &&
161 git add file7 &&
162 test_tick &&
163 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
164 git tag twerp &&
165 git rebase -i --onto master HEAD^ &&
166 git show HEAD | grep "^Author: Twerp Snog"
169 test_expect_success 'squash' '
170 git reset --hard twerp &&
171 echo B > file7 &&
172 test_tick &&
173 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
174 echo "******************************" &&
175 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
176 test B = $(cat file7) &&
177 test $(git rev-parse HEAD^) = $(git rev-parse master)
180 test_expect_success 'retain authorship when squashing' '
181 git show HEAD | grep "^Author: Nitfol"
184 test_expect_success 'preserve merges with -p' '
185 git checkout -b to-be-preserved master^ &&
186 : > unrelated-file &&
187 git add unrelated-file &&
188 test_tick &&
189 git commit -m "unrelated" &&
190 git checkout -b to-be-rebased master &&
191 echo B > file1 &&
192 test_tick &&
193 git commit -m J file1 &&
194 test_tick &&
195 git merge to-be-preserved &&
196 echo C > file1 &&
197 test_tick &&
198 git commit -m K file1 &&
199 test_tick &&
200 git rebase -i -p --onto branch1 master &&
201 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
202 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
203 test $(git show HEAD:file1) = C &&
204 test $(git show HEAD~2:file1) = B
207 test_expect_success '--continue tries to commit' '
208 test_tick &&
209 ! git rebase -i --onto new-branch1 HEAD^ &&
210 echo resolved > file1 &&
211 git add file1 &&
212 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
213 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
214 git show HEAD | grep chouette
217 test_expect_success 'verbose flag is heeded, even after --continue' '
218 git reset --hard HEAD@{1} &&
219 test_tick &&
220 ! git rebase -v -i --onto new-branch1 HEAD^ &&
221 echo resolved > file1 &&
222 git add file1 &&
223 git rebase --continue > output &&
224 grep "^ file1 | 2 +-$" output
227 test_expect_success 'multi-squash only fires up editor once' '
228 base=$(git rev-parse HEAD~4) &&
229 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
230 git rebase -i $base &&
231 test $base = $(git rev-parse HEAD^) &&
232 test 1 = $(git show | grep ONCE | wc -l)
235 test_expect_success 'squash works as expected' '
236 for n in one two three four
238 echo $n >> file$n &&
239 git add file$n &&
240 git commit -m $n
241 done &&
242 one=$(git rev-parse HEAD~3) &&
243 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
244 test $one = $(git rev-parse HEAD~2)
247 test_expect_success 'interrupted squash works as expected' '
248 for n in one two three four
250 echo $n >> conflict &&
251 git add conflict &&
252 git commit -m $n
253 done &&
254 one=$(git rev-parse HEAD~3) &&
255 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
256 (echo one; echo two; echo four) > conflict &&
257 git add conflict &&
258 ! git rebase --continue &&
259 echo resolved > conflict &&
260 git add conflict &&
261 git rebase --continue &&
262 test $one = $(git rev-parse HEAD~2)
265 test_expect_success 'ignore patch if in upstream' '
266 HEAD=$(git rev-parse HEAD) &&
267 git checkout -b has-cherry-picked HEAD^ &&
268 echo unrelated > file7 &&
269 git add file7 &&
270 test_tick &&
271 git commit -m "unrelated change" &&
272 git cherry-pick $HEAD &&
273 EXPECT_COUNT=1 git rebase -i $HEAD &&
274 test $HEAD = $(git rev-parse HEAD^)
277 test_done