]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3420-rebase-autostash.sh
Merge branch 'js/update-index-ignore-removal-for-skip-worktree'
[thirdparty/git.git] / t / t3420-rebase-autostash.sh
CommitLineData
58794775
RR
1#!/bin/sh
2#
3# Copyright (c) 2013 Ramkumar Ramachandra
4#
5
6test_description='git rebase --autostash tests'
7. ./test-lib.sh
8
9test_expect_success setup '
10 echo hello-world >file0 &&
11 git add . &&
12 test_tick &&
13 git commit -m "initial commit" &&
14 git checkout -b feature-branch &&
15 echo another-hello >file1 &&
16 echo goodbye >file2 &&
17 git add . &&
18 test_tick &&
19 git commit -m "second commit" &&
20 echo final-goodbye >file3 &&
21 git add . &&
22 test_tick &&
23 git commit -m "third commit" &&
24 git checkout -b unrelated-onto-branch master &&
25 echo unrelated >file4 &&
26 git add . &&
27 test_tick &&
28 git commit -m "unrelated commit" &&
29 git checkout -b related-onto-branch master &&
30 echo conflicting-change >file2 &&
31 git add . &&
32 test_tick &&
bb431c3d
PW
33 git commit -m "related commit" &&
34 remove_progress_re="$(printf "s/.*\\r//")"
58794775
RR
35'
36
b76aeae5
PW
37create_expected_success_am () {
38 cat >expected <<-EOF
39 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
b76aeae5
PW
40 First, rewinding head to replay your work on top of it...
41 Applying: second commit
42 Applying: third commit
43 Applied autostash.
44 EOF
45}
46
47create_expected_success_interactive () {
48 q_to_cr >expected <<-EOF
49 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
bb431c3d
PW
50 Applied autostash.
51 Successfully rebased and updated refs/heads/rebased-feature-branch.
b76aeae5
PW
52 EOF
53}
54
7d70e6b9
PW
55create_expected_failure_am () {
56 cat >expected <<-EOF
57 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
7d70e6b9
PW
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.
64 EOF
65}
66
67create_expected_failure_interactive () {
bb431c3d 68 cat >expected <<-EOF
7d70e6b9 69 $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
bb431c3d 70 Applying autostash resulted in conflicts.
7d70e6b9
PW
71 Your changes are safe in the stash.
72 You can run "git stash pop" or "git stash drop" at any time.
bb431c3d 73 Successfully rebased and updated refs/heads/rebased-feature-branch.
7d70e6b9
PW
74 EOF
75}
76
b76aeae5 77testrebase () {
58794775
RR
78 type=$1
79 dotest=$2
80
619e3604
JK
81 test_expect_success "rebase$type: dirty worktree, --no-autostash" '
82 test_config rebase.autostash true &&
83 git reset --hard &&
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 &&
87 echo dirty >>file3 &&
88 test_must_fail git rebase$type --no-autostash unrelated-onto-branch
89 '
90
58794775
RR
91 test_expect_success "rebase$type: dirty worktree, non-conflicting rebase" '
92 test_config rebase.autostash true &&
93 git reset --hard &&
94 git checkout -b rebased-feature-branch feature-branch &&
58794775 95 echo dirty >>file3 &&
b76aeae5 96 git rebase$type unrelated-onto-branch >actual 2>&1 &&
58794775
RR
97 grep unrelated file4 &&
98 grep dirty file3 &&
99 git checkout feature-branch
100 '
101
b76aeae5
PW
102 test_expect_success "rebase$type --autostash: check output" '
103 test_when_finished git branch -D rebased-feature-branch &&
104 suffix=${type#\ --} && suffix=${suffix:-am} &&
68aa495b
EN
105 if test ${suffix} = "merge"; then
106 suffix=interactive
107 fi &&
b76aeae5 108 create_expected_success_$suffix &&
bb431c3d
PW
109 sed "$remove_progress_re" <actual >actual2 &&
110 test_i18ncmp expected actual2
b76aeae5
PW
111 '
112
58794775
RR
113 test_expect_success "rebase$type: dirty index, non-conflicting rebase" '
114 test_config rebase.autostash true &&
115 git reset --hard &&
116 git checkout -b rebased-feature-branch feature-branch &&
117 test_when_finished git branch -D rebased-feature-branch &&
118 echo dirty >>file3 &&
119 git add file3 &&
120 git rebase$type unrelated-onto-branch &&
121 grep unrelated file4 &&
122 grep dirty file3 &&
123 git checkout feature-branch
124 '
125
126 test_expect_success "rebase$type: conflicting rebase" '
127 test_config rebase.autostash true &&
128 git reset --hard &&
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 &&
27458170 134 test_path_is_missing file3 &&
58794775
RR
135 rm -rf $dotest &&
136 git reset --hard &&
137 git checkout feature-branch
138 '
139
140 test_expect_success "rebase$type: --continue" '
141 test_config rebase.autostash true &&
142 git reset --hard &&
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 &&
27458170 148 test_path_is_missing file3 &&
58794775
RR
149 echo "conflicting-plus-goodbye" >file2 &&
150 git add file2 &&
151 git rebase --continue &&
152 test_path_is_missing $dotest/autostash &&
153 grep dirty file3 &&
154 git checkout feature-branch
155 '
156
157 test_expect_success "rebase$type: --skip" '
158 test_config rebase.autostash true &&
159 git reset --hard &&
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 &&
27458170 165 test_path_is_missing file3 &&
58794775
RR
166 git rebase --skip &&
167 test_path_is_missing $dotest/autostash &&
168 grep dirty file3 &&
169 git checkout feature-branch
170 '
171
172 test_expect_success "rebase$type: --abort" '
173 test_config rebase.autostash true &&
174 git reset --hard &&
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 &&
27458170 180 test_path_is_missing file3 &&
58794775
RR
181 git rebase --abort &&
182 test_path_is_missing $dotest/autostash &&
183 grep dirty file3 &&
184 git checkout feature-branch
185 '
186
187 test_expect_success "rebase$type: non-conflicting rebase, conflicting stash" '
188 test_config rebase.autostash true &&
189 git reset --hard &&
190 git checkout -b rebased-feature-branch feature-branch &&
58794775
RR
191 echo dirty >file4 &&
192 git add file4 &&
7d70e6b9 193 git rebase$type unrelated-onto-branch >actual 2>&1 &&
58794775
RR
194 test_path_is_missing $dotest &&
195 git reset --hard &&
196 grep unrelated file4 &&
197 ! grep dirty file4 &&
198 git checkout feature-branch &&
199 git stash pop &&
200 grep dirty file4
201 '
7d70e6b9
PW
202
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:-am} &&
68aa495b
EN
206 if test ${suffix} = "merge"; then
207 suffix=interactive
208 fi &&
7d70e6b9 209 create_expected_failure_$suffix &&
bb431c3d
PW
210 sed "$remove_progress_re" <actual >actual2 &&
211 test_i18ncmp expected actual2
7d70e6b9 212 '
58794775
RR
213}
214
af2f0ebc
RR
215test_expect_success "rebase: fast-forward rebase" '
216 test_config rebase.autostash true &&
217 git reset --hard &&
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 &&
222 grep dirty file1 &&
223 git checkout feature-branch
224'
225
96e2b99e
RR
226test_expect_success "rebase: noop rebase" '
227 test_config rebase.autostash true &&
228 git reset --hard &&
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 &&
233 grep dirty file1 &&
234 git checkout feature-branch
235'
236
58794775
RR
237testrebase "" .git/rebase-apply
238testrebase " --merge" .git/rebase-merge
239testrebase " --interactive" .git/rebase-merge
240
ddb5432d
MM
241test_expect_success 'abort rebase -i with --autostash' '
242 test_when_finished "git reset --hard" &&
2e3a16b2 243 echo uncommitted-content >file0 &&
ddb5432d
MM
244 (
245 write_script abort-editor.sh <<-\EOF &&
246 echo >"$1"
247 EOF
248 test_set_editor "$(pwd)/abort-editor.sh" &&
249 test_must_fail git rebase -i --autostash HEAD^ &&
250 rm -f abort-editor.sh
251 ) &&
2e3a16b2 252 echo uncommitted-content >expected &&
ddb5432d
MM
253 test_cmp expected file0
254'
255
33ba9c64
PS
256test_expect_success 'restore autostash on editor failure' '
257 test_when_finished "git reset --hard" &&
258 echo uncommitted-content >file0 &&
259 (
260 test_set_editor "false" &&
261 test_must_fail git rebase -i --autostash HEAD^
262 ) &&
263 echo uncommitted-content >expected &&
264 test_cmp expected file0
265'
266
267test_expect_success 'autostash is saved on editor failure with conflict' '
268 test_when_finished "git reset --hard" &&
269 echo uncommitted-content >file0 &&
270 (
271 write_script abort-editor.sh <<-\EOF &&
272 echo conflicting-content >file0
273 exit 1
274 EOF
275 test_set_editor "$(pwd)/abort-editor.sh" &&
276 test_must_fail git rebase -i --autostash HEAD^ &&
277 rm -f abort-editor.sh
278 ) &&
279 echo conflicting-content >expected &&
280 test_cmp expected file0 &&
281 git checkout file0 &&
282 git stash pop &&
283 echo uncommitted-content >expected &&
284 test_cmp expected file0
285'
286
ffae8b2f 287test_expect_success 'autostash with dirty submodules' '
97bd162c
JS
288 test_when_finished "git reset --hard && git checkout master" &&
289 git checkout -b with-submodule &&
290 git submodule add ./ sub &&
291 test_tick &&
292 git commit -m add-submodule &&
293 echo changed >sub/file0 &&
294 git rebase -i --autostash HEAD
295'
296
176f5d96 297test_expect_success 'branch is left alone when possible' '
2dac2bc8
JS
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)"
303'
304
d2172ef0
BW
305test_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
311'
312
58794775 313test_done