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 test_i18ngrep MERGE_HEAD output
53 test_expect_success 'fails without MERGE_HEAD (unstarted merge): .git/MERGE_HEAD sanity' '
54 test ! -f .git/MERGE_HEAD &&
55 test "$pre_merge_head" = "$(git rev-parse HEAD)"
58 test_expect_success 'fails without MERGE_HEAD (completed merge)' '
59 git merge clean_branch &&
60 test ! -f .git/MERGE_HEAD &&
61 # Merge successfully completed
62 post_merge_head="$(git rev-parse HEAD)" &&
63 test_must_fail git merge --abort 2>output &&
64 test_i18ngrep MERGE_HEAD output
67 test_expect_success 'fails without MERGE_HEAD (completed merge): .git/MERGE_HEAD sanity' '
68 test ! -f .git/MERGE_HEAD &&
69 test "$post_merge_head" = "$(git rev-parse HEAD)"
72 test_expect_success 'Forget previous merge' '
73 git reset --hard "$pre_merge_head"
76 test_expect_success 'Abort after --no-commit' '
77 # Redo merge, but stop before creating merge commit
78 git merge --no-commit clean_branch &&
79 test -f .git/MERGE_HEAD &&
80 # Abort non-conflicting merge
82 test ! -f .git/MERGE_HEAD &&
83 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
84 test -z "$(git diff)" &&
85 test -z "$(git diff --staged)"
88 test_expect_success 'Abort after conflicts' '
89 # Create conflicting merge
90 test_must_fail git merge conflict_branch &&
91 test -f .git/MERGE_HEAD &&
92 # Abort conflicting merge
94 test ! -f .git/MERGE_HEAD &&
95 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
96 test -z "$(git diff)" &&
97 test -z "$(git diff --staged)"
100 test_expect_success 'Clean merge with dirty index fails' '
103 git diff --staged > expect &&
104 test_must_fail git merge clean_branch &&
105 test ! -f .git/MERGE_HEAD &&
106 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
107 test -z "$(git diff)" &&
108 git diff --staged > actual &&
109 test_cmp expect actual
112 test_expect_success 'Conflicting merge with dirty index fails' '
113 test_must_fail git merge conflict_branch &&
114 test ! -f .git/MERGE_HEAD &&
115 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
116 test -z "$(git diff)" &&
117 git diff --staged > actual &&
118 test_cmp expect actual
121 test_expect_success 'Reset index (but preserve worktree changes)' '
122 git reset "$pre_merge_head" &&
124 test_cmp expect actual
127 test_expect_success 'Abort clean merge with non-conflicting dirty worktree' '
128 git merge --no-commit clean_branch &&
129 test -f .git/MERGE_HEAD &&
132 test ! -f .git/MERGE_HEAD &&
133 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
134 test -z "$(git diff --staged)" &&
136 test_cmp expect actual
139 test_expect_success 'Abort conflicting merge with non-conflicting dirty worktree' '
140 test_must_fail git merge conflict_branch &&
141 test -f .git/MERGE_HEAD &&
144 test ! -f .git/MERGE_HEAD &&
145 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
146 test -z "$(git diff --staged)" &&
148 test_cmp expect actual
151 test_expect_success 'Reset worktree changes' '
152 git reset --hard "$pre_merge_head"
155 test_expect_success 'Fail clean merge with conflicting dirty worktree' '
158 test_must_fail git merge --no-commit clean_branch &&
159 test ! -f .git/MERGE_HEAD &&
160 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
161 test -z "$(git diff --staged)" &&
163 test_cmp expect actual
166 test_expect_success 'Fail conflicting merge with conflicting dirty worktree' '
167 test_must_fail git merge conflict_branch &&
168 test ! -f .git/MERGE_HEAD &&
169 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
170 test -z "$(git diff --staged)" &&
172 test_cmp expect actual
175 test_expect_success 'Reset worktree changes' '
176 git reset --hard "$pre_merge_head"
179 test_expect_success 'Fail clean merge with matching dirty worktree' '
182 test_must_fail git merge --no-commit clean_branch &&
183 test ! -f .git/MERGE_HEAD &&
184 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
185 test -z "$(git diff --staged)" &&
187 test_cmp expect actual
190 test_expect_success 'Fail conflicting merge with matching dirty worktree' '
193 test_must_fail git merge conflict_branch &&
194 test ! -f .git/MERGE_HEAD &&
195 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
196 test -z "$(git diff --staged)" &&
198 test_cmp expect actual