3 test_description
='test aborting in-progress merges
5 Set up repo with conflicting and non-conflicting branches:
7 There are three files foo/bar/baz, and the following graph illustrates the
8 content of these files in each commit:
10 # foo/bar/baz --- foo/bar/bazz <-- master
12 # --- foo/barf/bazf <-- conflict_branch
14 # --- foo/bart/baz <-- clean_branch
16 Next, test git merge --abort with the following variables:
17 - before/after successful merge (should fail when not in merge context)
18 - with/without conflicts
19 - clean/dirty index before merge
20 - clean/dirty worktree before merge
21 - dirty index before merge matches contents on remote branch
22 - changed/unchanged worktree after merge
23 - changed/unchanged index after merge
27 test_expect_success
'setup' '
28 # Create the above repo
32 git add foo bar baz &&
33 git commit -m initial &&
35 git commit -a -m "second" &&
36 git checkout -b conflict_branch HEAD^ &&
39 git commit -a -m "conflict" &&
40 git checkout -b clean_branch HEAD^ &&
42 git commit -a -m "clean" &&
46 pre_merge_head
="$(git rev-parse HEAD)"
48 test_expect_success
'fails without MERGE_HEAD (unstarted merge)' '
49 test_must_fail git merge --abort 2>output &&
50 grep -q MERGE_HEAD output &&
51 test ! -f .git/MERGE_HEAD &&
52 test "$pre_merge_head" = "$(git rev-parse HEAD)"
55 test_expect_success
'fails without MERGE_HEAD (completed merge)' '
56 git merge clean_branch &&
57 test ! -f .git/MERGE_HEAD &&
58 # Merge successfully completed
59 post_merge_head="$(git rev-parse HEAD)" &&
60 test_must_fail git merge --abort 2>output &&
61 grep -q MERGE_HEAD output &&
62 test ! -f .git/MERGE_HEAD &&
63 test "$post_merge_head" = "$(git rev-parse HEAD)"
66 test_expect_success
'Forget previous merge' '
67 git reset --hard "$pre_merge_head"
70 test_expect_success
'Abort after --no-commit' '
71 # Redo merge, but stop before creating merge commit
72 git merge --no-commit clean_branch &&
73 test -f .git/MERGE_HEAD &&
74 # Abort non-conflicting merge
76 test ! -f .git/MERGE_HEAD &&
77 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
78 test -z "$(git diff)" &&
79 test -z "$(git diff --staged)"
82 test_expect_success
'Abort after conflicts' '
83 # Create conflicting merge
84 test_must_fail git merge conflict_branch &&
85 test -f .git/MERGE_HEAD &&
86 # Abort conflicting merge
88 test ! -f .git/MERGE_HEAD &&
89 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
90 test -z "$(git diff)" &&
91 test -z "$(git diff --staged)"
94 test_expect_success
'Clean merge with dirty index fails' '
97 git diff --staged > expect &&
98 test_must_fail git merge clean_branch &&
99 test ! -f .git/MERGE_HEAD &&
100 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
101 test -z "$(git diff)" &&
102 git diff --staged > actual &&
103 test_cmp expect actual
106 test_expect_success
'Conflicting merge with dirty index fails' '
107 test_must_fail git merge conflict_branch &&
108 test ! -f .git/MERGE_HEAD &&
109 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
110 test -z "$(git diff)" &&
111 git diff --staged > actual &&
112 test_cmp expect actual
115 test_expect_success
'Reset index (but preserve worktree changes)' '
116 git reset "$pre_merge_head" &&
118 test_cmp expect actual
121 test_expect_success
'Abort clean merge with non-conflicting dirty worktree' '
122 git merge --no-commit clean_branch &&
123 test -f .git/MERGE_HEAD &&
126 test ! -f .git/MERGE_HEAD &&
127 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
128 test -z "$(git diff --staged)" &&
130 test_cmp expect actual
133 test_expect_success
'Abort conflicting merge with non-conflicting dirty worktree' '
134 test_must_fail git merge conflict_branch &&
135 test -f .git/MERGE_HEAD &&
138 test ! -f .git/MERGE_HEAD &&
139 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
140 test -z "$(git diff --staged)" &&
142 test_cmp expect actual
145 test_expect_success
'Reset worktree changes' '
146 git reset --hard "$pre_merge_head"
149 test_expect_success
'Fail clean merge with conflicting dirty worktree' '
152 test_must_fail git merge --no-commit clean_branch &&
153 test ! -f .git/MERGE_HEAD &&
154 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
155 test -z "$(git diff --staged)" &&
157 test_cmp expect actual
160 test_expect_success
'Fail conflicting merge with conflicting dirty worktree' '
161 test_must_fail git merge conflict_branch &&
162 test ! -f .git/MERGE_HEAD &&
163 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
164 test -z "$(git diff --staged)" &&
166 test_cmp expect actual
169 test_expect_success
'Reset worktree changes' '
170 git reset --hard "$pre_merge_head"
173 test_expect_success
'Fail clean merge with matching dirty worktree' '
176 test_must_fail git merge --no-commit clean_branch &&
177 test ! -f .git/MERGE_HEAD &&
178 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
179 test -z "$(git diff --staged)" &&
181 test_cmp expect actual
184 test_expect_success
'Abort clean merge with matching dirty index' '
186 git diff --staged > expect &&
187 git merge --no-commit clean_branch &&
188 test -f .git/MERGE_HEAD &&
189 ### When aborting the merge, git will discard all staged changes,
190 ### including those that were staged pre-merge. In other words,
191 ### --abort will LOSE any staged changes (the staged changes that
192 ### are lost must match the merge result, or the merge would not
193 ### have been allowed to start). Change expectations accordingly:
198 test ! -f .git/MERGE_HEAD &&
199 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
200 git diff --staged > actual &&
201 test_cmp expect actual &&
202 test -z "$(git diff)"
205 test_expect_success
'Reset worktree changes' '
206 git reset --hard "$pre_merge_head"
209 test_expect_success
'Fail conflicting merge with matching dirty worktree' '
212 test_must_fail git merge conflict_branch &&
213 test ! -f .git/MERGE_HEAD &&
214 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
215 test -z "$(git diff --staged)" &&
217 test_cmp expect actual
220 test_expect_success
'Abort conflicting merge with matching dirty index' '
222 git diff --staged > expect &&
223 test_must_fail git merge conflict_branch &&
224 test -f .git/MERGE_HEAD &&
225 ### When aborting the merge, git will discard all staged changes,
226 ### including those that were staged pre-merge. In other words,
227 ### --abort will LOSE any staged changes (the staged changes that
228 ### are lost must match the merge result, or the merge would not
229 ### have been allowed to start). Change expectations accordingly:
234 test ! -f .git/MERGE_HEAD &&
235 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
236 git diff --staged > actual &&
237 test_cmp expect actual &&
238 test -z "$(git diff)"
241 test_expect_success
'Reset worktree changes' '
242 git reset --hard "$pre_merge_head"
245 test_expect_success
'Abort merge with pre- and post-merge worktree changes' '
246 # Pre-merge worktree changes
251 git diff --staged > expect-staged &&
253 test_must_fail git merge conflict_branch &&
254 test -f .git/MERGE_HEAD &&
255 # Post-merge worktree changes
258 ### When aborting the merge, git will discard staged changes (bar)
259 ### and unmerged changes (baz). Other changes that are neither
260 ### staged nor marked as unmerged (foo), will be preserved. For
261 ### these changed, git cannot tell pre-merge changes apart from
262 ### post-merge changes, so the post-merge changes will be
263 ### preserved. Change expectations accordingly:
264 git diff -- foo > expect &&
266 touch expect-staged &&
269 test ! -f .git/MERGE_HEAD &&
270 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
272 test_cmp expect actual &&
273 git diff --staged > actual-staged &&
274 test_cmp expect-staged actual-staged
277 test_expect_success
'Reset worktree changes' '
278 git reset --hard "$pre_merge_head"
281 test_expect_success
'Abort merge with pre- and post-merge index changes' '
282 # Pre-merge worktree changes
287 git diff --staged > expect-staged &&
289 test_must_fail git merge conflict_branch &&
290 test -f .git/MERGE_HEAD &&
291 # Post-merge worktree changes
295 ### When aborting the merge, git will discard all staged changes
296 ### (foo, bar and baz), and no changes will be preserved. Whether
297 ### the changes were staged pre- or post-merge does not matter
298 ### (except for not preventing starting the merge).
299 ### Change expectations accordingly:
300 rm expect expect-staged &&
302 touch expect-staged &&
305 test ! -f .git/MERGE_HEAD &&
306 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
308 test_cmp expect actual &&
309 git diff --staged > actual-staged &&
310 test_cmp expect-staged actual-staged