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: non-conflicting rebase, conflicting stash" '
188 test_config rebase.autostash true &&
190 git checkout -b rebased-feature-branch feature-branch &&
193 git rebase$type unrelated-onto-branch >actual 2>&1 &&
194 test_path_is_missing $dotest &&
196 grep unrelated file4 &&
197 ! grep dirty file4 &&
198 git checkout feature-branch &&
203 test_expect_success "rebase$type: check output with conflicting stash" '
204 test_when_finished git branch -D rebased-feature-branch &&
205 suffix=${type#\ --} && suffix=${suffix:-apply} &&
206 if test ${suffix} = "interactive"; then
209 create_expected_failure_$suffix &&
210 sed "$remove_progress_re" <actual >actual2 &&
211 test_i18ncmp expected actual2
215 test_expect_success "rebase: fast-forward rebase" '
216 test_config rebase.autostash true &&
218 git checkout -b behind-feature-branch feature-branch~1 &&
219 test_when_finished git branch -D behind-feature-branch &&
220 echo dirty >>file1 &&
221 git rebase feature-branch &&
223 git checkout feature-branch
226 test_expect_success "rebase: noop rebase" '
227 test_config rebase.autostash true &&
229 git checkout -b same-feature-branch feature-branch &&
230 test_when_finished git branch -D same-feature-branch &&
231 echo dirty >>file1 &&
232 git rebase feature-branch &&
234 git checkout feature-branch
237 testrebase " --apply" .git/rebase-apply
238 testrebase " --merge" .git/rebase-merge
239 testrebase " --interactive" .git/rebase-merge
241 test_expect_success 'abort rebase -i with --autostash' '
242 test_when_finished "git reset --hard" &&
243 echo uncommitted-content >file0 &&
245 write_script abort-editor.sh <<-\EOF &&
248 test_set_editor "$(pwd)/abort-editor.sh" &&
249 test_must_fail git rebase -i --autostash HEAD^ &&
250 rm -f abort-editor.sh
252 echo uncommitted-content >expected &&
253 test_cmp expected file0
256 test_expect_success 'restore autostash on editor failure' '
257 test_when_finished "git reset --hard" &&
258 echo uncommitted-content >file0 &&
260 test_set_editor "false" &&
261 test_must_fail git rebase -i --autostash HEAD^
263 echo uncommitted-content >expected &&
264 test_cmp expected file0
267 test_expect_success 'autostash is saved on editor failure with conflict' '
268 test_when_finished "git reset --hard" &&
269 echo uncommitted-content >file0 &&
271 write_script abort-editor.sh <<-\EOF &&
272 echo conflicting-content >file0
275 test_set_editor "$(pwd)/abort-editor.sh" &&
276 test_must_fail git rebase -i --autostash HEAD^ &&
277 rm -f abort-editor.sh
279 echo conflicting-content >expected &&
280 test_cmp expected file0 &&
281 git checkout file0 &&
283 echo uncommitted-content >expected &&
284 test_cmp expected file0
287 test_expect_success 'autostash with dirty submodules' '
288 test_when_finished "git reset --hard && git checkout master" &&
289 git checkout -b with-submodule &&
290 git submodule add ./ sub &&
292 git commit -m add-submodule &&
293 echo changed >sub/file0 &&
294 git rebase -i --autostash HEAD
297 test_expect_success 'branch is left alone when possible' '
298 git checkout -b unchanged-branch &&
299 echo changed >file0 &&
300 git rebase --autostash unchanged-branch &&
301 test changed = "$(cat file0)" &&
302 test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)"
305 test_expect_success 'never change active branch' '
306 git checkout -b not-the-feature-branch unrelated-onto-branch &&
307 test_when_finished "git reset --hard && git checkout master" &&
308 echo changed >file0 &&
309 git rebase --autostash not-the-feature-branch feature-branch &&
310 test_cmp_rev not-the-feature-branch unrelated-onto-branch