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 <-- main
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
25 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
26 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
28 TEST_PASSES_SANITIZE_LEAK
=true
31 test_expect_success
'setup' '
32 # Create the above repo
36 git add foo bar baz &&
37 git commit -m initial &&
39 git commit -a -m "second" &&
40 git checkout -b conflict_branch HEAD^ &&
43 git commit -a -m "conflict" &&
44 git checkout -b clean_branch HEAD^ &&
46 git commit -a -m "clean" &&
50 pre_merge_head
="$(git rev-parse HEAD)"
52 test_expect_success
'fails without MERGE_HEAD (unstarted merge)' '
53 test_must_fail git merge --abort 2>output &&
54 test_grep MERGE_HEAD output
57 test_expect_success
'fails without MERGE_HEAD (unstarted merge): .git/MERGE_HEAD sanity' '
58 test ! -f .git/MERGE_HEAD &&
59 test "$pre_merge_head" = "$(git rev-parse HEAD)"
62 test_expect_success
'fails without MERGE_HEAD (completed merge)' '
63 git merge clean_branch &&
64 test ! -f .git/MERGE_HEAD &&
65 # Merge successfully completed
66 post_merge_head="$(git rev-parse HEAD)" &&
67 test_must_fail git merge --abort 2>output &&
68 test_grep MERGE_HEAD output
71 test_expect_success
'fails without MERGE_HEAD (completed merge): .git/MERGE_HEAD sanity' '
72 test ! -f .git/MERGE_HEAD &&
73 test "$post_merge_head" = "$(git rev-parse HEAD)"
76 test_expect_success
'Forget previous merge' '
77 git reset --hard "$pre_merge_head"
80 test_expect_success
'Abort after --no-commit' '
81 # Redo merge, but stop before creating merge commit
82 git merge --no-commit clean_branch &&
83 test -f .git/MERGE_HEAD &&
84 # Abort non-conflicting merge
86 test ! -f .git/MERGE_HEAD &&
87 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
88 test -z "$(git diff)" &&
89 test -z "$(git diff --staged)"
92 test_expect_success
'Abort after conflicts' '
93 # Create conflicting merge
94 test_must_fail git merge conflict_branch &&
95 test -f .git/MERGE_HEAD &&
96 # Abort conflicting merge
98 test ! -f .git/MERGE_HEAD &&
99 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
100 test -z "$(git diff)" &&
101 test -z "$(git diff --staged)"
104 test_expect_success
'Clean merge with dirty index fails' '
107 git diff --staged > expect &&
108 test_must_fail git merge clean_branch &&
109 test ! -f .git/MERGE_HEAD &&
110 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
111 test -z "$(git diff)" &&
112 git diff --staged > actual &&
113 test_cmp expect actual
116 test_expect_success
'Conflicting merge with dirty index fails' '
117 test_must_fail git merge conflict_branch &&
118 test ! -f .git/MERGE_HEAD &&
119 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
120 test -z "$(git diff)" &&
121 git diff --staged > actual &&
122 test_cmp expect actual
125 test_expect_success
'Reset index (but preserve worktree changes)' '
126 git reset "$pre_merge_head" &&
128 test_cmp expect actual
131 test_expect_success
'Abort clean merge with non-conflicting dirty worktree' '
132 git merge --no-commit clean_branch &&
133 test -f .git/MERGE_HEAD &&
136 test ! -f .git/MERGE_HEAD &&
137 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
138 test -z "$(git diff --staged)" &&
140 test_cmp expect actual
143 test_expect_success
'Abort conflicting merge with non-conflicting dirty worktree' '
144 test_must_fail git merge conflict_branch &&
145 test -f .git/MERGE_HEAD &&
148 test ! -f .git/MERGE_HEAD &&
149 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
150 test -z "$(git diff --staged)" &&
152 test_cmp expect actual
155 test_expect_success
'Reset worktree changes' '
156 git reset --hard "$pre_merge_head"
159 test_expect_success
'Fail clean merge with conflicting dirty worktree' '
162 test_must_fail git merge --no-commit clean_branch &&
163 test ! -f .git/MERGE_HEAD &&
164 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
165 test -z "$(git diff --staged)" &&
167 test_cmp expect actual
170 test_expect_success
'Fail conflicting merge with conflicting dirty worktree' '
171 test_must_fail git merge conflict_branch &&
172 test ! -f .git/MERGE_HEAD &&
173 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
174 test -z "$(git diff --staged)" &&
176 test_cmp expect actual
179 test_expect_success
'Reset worktree changes' '
180 git reset --hard "$pre_merge_head"
183 test_expect_success
'Fail clean merge with matching dirty worktree' '
186 test_must_fail git merge --no-commit clean_branch &&
187 test ! -f .git/MERGE_HEAD &&
188 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
189 test -z "$(git diff --staged)" &&
191 test_cmp expect actual
194 test_expect_success
'Fail conflicting merge with matching dirty worktree' '
197 test_must_fail git merge conflict_branch &&
198 test ! -f .git/MERGE_HEAD &&
199 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
200 test -z "$(git diff --staged)" &&
202 test_cmp expect actual