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
30 test_expect_success 'setup' '
31 # Create the above repo
35 git add foo bar baz &&
36 git commit -m initial &&
38 git commit -a -m "second" &&
39 git checkout -b conflict_branch HEAD^ &&
42 git commit -a -m "conflict" &&
43 git checkout -b clean_branch HEAD^ &&
45 git commit -a -m "clean" &&
49 pre_merge_head="$(git rev-parse HEAD)"
51 test_expect_success 'fails without MERGE_HEAD (unstarted merge)' '
52 test_must_fail git merge --abort 2>output &&
53 test_i18ngrep MERGE_HEAD output
56 test_expect_success 'fails without MERGE_HEAD (unstarted merge): .git/MERGE_HEAD sanity' '
57 test ! -f .git/MERGE_HEAD &&
58 test "$pre_merge_head" = "$(git rev-parse HEAD)"
61 test_expect_success 'fails without MERGE_HEAD (completed merge)' '
62 git merge clean_branch &&
63 test ! -f .git/MERGE_HEAD &&
64 # Merge successfully completed
65 post_merge_head="$(git rev-parse HEAD)" &&
66 test_must_fail git merge --abort 2>output &&
67 test_i18ngrep MERGE_HEAD output
70 test_expect_success 'fails without MERGE_HEAD (completed merge): .git/MERGE_HEAD sanity' '
71 test ! -f .git/MERGE_HEAD &&
72 test "$post_merge_head" = "$(git rev-parse HEAD)"
75 test_expect_success 'Forget previous merge' '
76 git reset --hard "$pre_merge_head"
79 test_expect_success 'Abort after --no-commit' '
80 # Redo merge, but stop before creating merge commit
81 git merge --no-commit clean_branch &&
82 test -f .git/MERGE_HEAD &&
83 # Abort non-conflicting merge
85 test ! -f .git/MERGE_HEAD &&
86 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
87 test -z "$(git diff)" &&
88 test -z "$(git diff --staged)"
91 test_expect_success 'Abort after conflicts' '
92 # Create conflicting merge
93 test_must_fail git merge conflict_branch &&
94 test -f .git/MERGE_HEAD &&
95 # Abort conflicting merge
97 test ! -f .git/MERGE_HEAD &&
98 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
99 test -z "$(git diff)" &&
100 test -z "$(git diff --staged)"
103 test_expect_success 'Clean merge with dirty index fails' '
106 git diff --staged > expect &&
107 test_must_fail git merge clean_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 'Conflicting merge with dirty index fails' '
116 test_must_fail git merge conflict_branch &&
117 test ! -f .git/MERGE_HEAD &&
118 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
119 test -z "$(git diff)" &&
120 git diff --staged > actual &&
121 test_cmp expect actual
124 test_expect_success 'Reset index (but preserve worktree changes)' '
125 git reset "$pre_merge_head" &&
127 test_cmp expect actual
130 test_expect_success 'Abort clean merge with non-conflicting dirty worktree' '
131 git merge --no-commit clean_branch &&
132 test -f .git/MERGE_HEAD &&
135 test ! -f .git/MERGE_HEAD &&
136 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
137 test -z "$(git diff --staged)" &&
139 test_cmp expect actual
142 test_expect_success 'Abort conflicting merge with non-conflicting dirty worktree' '
143 test_must_fail git merge conflict_branch &&
144 test -f .git/MERGE_HEAD &&
147 test ! -f .git/MERGE_HEAD &&
148 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
149 test -z "$(git diff --staged)" &&
151 test_cmp expect actual
154 test_expect_success 'Reset worktree changes' '
155 git reset --hard "$pre_merge_head"
158 test_expect_success 'Fail clean merge with conflicting dirty worktree' '
161 test_must_fail git merge --no-commit clean_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 'Fail conflicting merge with conflicting dirty worktree' '
170 test_must_fail git merge conflict_branch &&
171 test ! -f .git/MERGE_HEAD &&
172 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
173 test -z "$(git diff --staged)" &&
175 test_cmp expect actual
178 test_expect_success 'Reset worktree changes' '
179 git reset --hard "$pre_merge_head"
182 test_expect_success 'Fail clean merge with matching dirty worktree' '
185 test_must_fail git merge --no-commit clean_branch &&
186 test ! -f .git/MERGE_HEAD &&
187 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
188 test -z "$(git diff --staged)" &&
190 test_cmp expect actual
193 test_expect_success 'Fail conflicting merge with matching dirty worktree' '
196 test_must_fail git merge conflict_branch &&
197 test ! -f .git/MERGE_HEAD &&
198 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
199 test -z "$(git diff --staged)" &&
201 test_cmp expect actual