3 # Copyright (c) 2013 Ramkumar Ramachandra
6 test_description='git rebase --autostash tests'
9 test_expect_success setup '
10 echo hello-world >file0 &&
13 git commit -m "initial commit" &&
14 git checkout -b feature-branch &&
15 echo another-hello >file1 &&
16 echo goodbye >file2 &&
19 git commit -m "second commit" &&
20 echo final-goodbye >file3 &&
23 git commit -m "third commit" &&
24 git checkout -b unrelated-onto-branch master &&
25 echo unrelated >file4 &&
28 git commit -m "unrelated commit" &&
29 git checkout -b related-onto-branch master &&
30 echo conflicting-change >file2 &&
33 git commit -m "related commit" &&
34 remove_progress_re="$(printf "s/.*\\r//")"
37 create_expected_success_apply () {
39 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
40 First, rewinding head to replay your work on top of it...
41 Applying: second commit
42 Applying: third commit
47 create_expected_success_merge () {
48 q_to_cr >expected <<-EOF
49 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
51 Successfully rebased and updated refs/heads/rebased-feature-branch.
55 create_expected_failure_apply () {
57 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
58 First, rewinding head to replay your work on top of it...
59 Applying: second commit
60 Applying: third commit
61 Applying autostash resulted in conflicts.
62 Your changes are safe in the stash.
63 You can run "git stash pop" or "git stash drop" at any time.
67 create_expected_failure_merge () {
69 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
70 Applying autostash resulted in conflicts.
71 Your changes are safe in the stash.
72 You can run "git stash pop" or "git stash drop" at any time.
73 Successfully rebased and updated refs/heads/rebased-feature-branch.
81 test_expect_success "rebase$type: dirty worktree, --no-autostash" '
82 test_config rebase.autostash true &&
84 git checkout -b rebased-feature-branch feature-branch &&
85 test_when_finished git branch -D rebased-feature-branch &&
86 test_when_finished git checkout feature-branch &&
88 test_must_fail git rebase$type --no-autostash unrelated-onto-branch
91 test_expect_success "rebase$type: dirty worktree, non-conflicting rebase" '
92 test_config rebase.autostash true &&
94 git checkout -b rebased-feature-branch feature-branch &&
96 git rebase$type unrelated-onto-branch >actual 2>&1 &&
97 grep unrelated file4 &&
99 git checkout feature-branch
102 test_expect_success "rebase$type --autostash: check output" '
103 test_when_finished git branch -D rebased-feature-branch &&
104 suffix=${type#\ --} && suffix=${suffix:-apply} &&
105 if test ${suffix} = "interactive"; then
108 create_expected_success_$suffix &&
109 sed "$remove_progress_re" <actual >actual2 &&
110 test_i18ncmp expected actual2
113 test_expect_success "rebase$type: dirty index, non-conflicting rebase" '
114 test_config rebase.autostash true &&
116 git checkout -b rebased-feature-branch feature-branch &&
117 test_when_finished git branch -D rebased-feature-branch &&
118 echo dirty >>file3 &&
120 git rebase$type unrelated-onto-branch &&
121 grep unrelated file4 &&
123 git checkout feature-branch
126 test_expect_success "rebase$type: conflicting rebase" '
127 test_config rebase.autostash true &&
129 git checkout -b rebased-feature-branch feature-branch &&
130 test_when_finished git branch -D rebased-feature-branch &&
131 echo dirty >>file3 &&
132 test_must_fail git rebase$type related-onto-branch &&
133 test_path_is_file $dotest/autostash &&
134 test_path_is_missing file3 &&
137 git checkout feature-branch
140 test_expect_success "rebase$type: --continue" '
141 test_config rebase.autostash true &&
143 git checkout -b rebased-feature-branch feature-branch &&
144 test_when_finished git branch -D rebased-feature-branch &&
145 echo dirty >>file3 &&
146 test_must_fail git rebase$type related-onto-branch &&
147 test_path_is_file $dotest/autostash &&
148 test_path_is_missing file3 &&
149 echo "conflicting-plus-goodbye" >file2 &&
151 git rebase --continue &&
152 test_path_is_missing $dotest/autostash &&
154 git checkout feature-branch
157 test_expect_success "rebase$type: --skip" '
158 test_config rebase.autostash true &&
160 git checkout -b rebased-feature-branch feature-branch &&
161 test_when_finished git branch -D rebased-feature-branch &&
162 echo dirty >>file3 &&
163 test_must_fail git rebase$type related-onto-branch &&
164 test_path_is_file $dotest/autostash &&
165 test_path_is_missing file3 &&
167 test_path_is_missing $dotest/autostash &&
169 git checkout feature-branch
172 test_expect_success "rebase$type: --abort" '
173 test_config rebase.autostash true &&
175 git checkout -b rebased-feature-branch feature-branch &&
176 test_when_finished git branch -D rebased-feature-branch &&
177 echo dirty >>file3 &&
178 test_must_fail git rebase$type related-onto-branch &&
179 test_path_is_file $dotest/autostash &&
180 test_path_is_missing file3 &&
181 git rebase --abort &&
182 test_path_is_missing $dotest/autostash &&
184 git checkout feature-branch
187 test_expect_success "rebase$type: --quit" '
188 test_config rebase.autostash true &&
190 git checkout -b rebased-feature-branch feature-branch &&
191 test_when_finished git branch -D rebased-feature-branch &&
192 echo dirty >>file3 &&
194 test_must_fail git rebase$type related-onto-branch &&
195 test_path_is_file $dotest/autostash &&
196 test_path_is_missing file3 &&
198 test_when_finished git stash drop &&
199 test_path_is_missing $dotest/autostash &&
200 ! grep dirty file3 &&
201 git stash show -p >actual &&
202 test_cmp expect actual &&
204 git checkout feature-branch
207 test_expect_success "rebase$type: non-conflicting rebase, conflicting stash" '
208 test_config rebase.autostash true &&
210 git checkout -b rebased-feature-branch feature-branch &&
213 git rebase$type unrelated-onto-branch >actual 2>&1 &&
214 test_path_is_missing $dotest &&
216 grep unrelated file4 &&
217 ! grep dirty file4 &&
218 git checkout feature-branch &&
223 test_expect_success "rebase$type: check output with conflicting stash" '
224 test_when_finished git branch -D rebased-feature-branch &&
225 suffix=${type#\ --} && suffix=${suffix:-apply} &&
226 if test ${suffix} = "interactive"; then
229 create_expected_failure_$suffix &&
230 sed "$remove_progress_re" <actual >actual2 &&
231 test_i18ncmp expected actual2
235 test_expect_success "rebase: fast-forward rebase" '
236 test_config rebase.autostash true &&
238 git checkout -b behind-feature-branch feature-branch~1 &&
239 test_when_finished git branch -D behind-feature-branch &&
240 echo dirty >>file1 &&
241 git rebase feature-branch &&
243 git checkout feature-branch
246 test_expect_success "rebase: noop rebase" '
247 test_config rebase.autostash true &&
249 git checkout -b same-feature-branch feature-branch &&
250 test_when_finished git branch -D same-feature-branch &&
251 echo dirty >>file1 &&
252 git rebase feature-branch &&
254 git checkout feature-branch
257 testrebase " --apply" .git/rebase-apply
258 testrebase " --merge" .git/rebase-merge
259 testrebase " --interactive" .git/rebase-merge
261 test_expect_success 'abort rebase -i with --autostash' '
262 test_when_finished "git reset --hard" &&
263 echo uncommitted-content >file0 &&
265 write_script abort-editor.sh <<-\EOF &&
268 test_set_editor "$(pwd)/abort-editor.sh" &&
269 test_must_fail git rebase -i --autostash HEAD^ &&
270 rm -f abort-editor.sh
272 echo uncommitted-content >expected &&
273 test_cmp expected file0
276 test_expect_success 'restore autostash on editor failure' '
277 test_when_finished "git reset --hard" &&
278 echo uncommitted-content >file0 &&
280 test_set_editor "false" &&
281 test_must_fail git rebase -i --autostash HEAD^
283 echo uncommitted-content >expected &&
284 test_cmp expected file0
287 test_expect_success 'autostash is saved on editor failure with conflict' '
288 test_when_finished "git reset --hard" &&
289 echo uncommitted-content >file0 &&
291 write_script abort-editor.sh <<-\EOF &&
292 echo conflicting-content >file0
295 test_set_editor "$(pwd)/abort-editor.sh" &&
296 test_must_fail git rebase -i --autostash HEAD^ &&
297 rm -f abort-editor.sh
299 echo conflicting-content >expected &&
300 test_cmp expected file0 &&
301 git checkout file0 &&
303 echo uncommitted-content >expected &&
304 test_cmp expected file0
307 test_expect_success 'autostash with dirty submodules' '
308 test_when_finished "git reset --hard && git checkout master" &&
309 git checkout -b with-submodule &&
310 git submodule add ./ sub &&
312 git commit -m add-submodule &&
313 echo changed >sub/file0 &&
314 git rebase -i --autostash HEAD
317 test_expect_success 'branch is left alone when possible' '
318 git checkout -b unchanged-branch &&
319 echo changed >file0 &&
320 git rebase --autostash unchanged-branch &&
321 test changed = "$(cat file0)" &&
322 test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)"
325 test_expect_success 'never change active branch' '
326 git checkout -b not-the-feature-branch unrelated-onto-branch &&
327 test_when_finished "git reset --hard && git checkout master" &&
328 echo changed >file0 &&
329 git rebase --autostash not-the-feature-branch feature-branch &&
330 test_cmp_rev not-the-feature-branch unrelated-onto-branch