1#!/bin/sh
2
3test_description='test aborting in-progress merges
4
5Set up repo with conflicting and non-conflicting branches:
6
7There are three files foo/bar/baz, and the following graph illustrates the
8content of these files in each commit:
9
10# foo/bar/baz --- foo/bar/bazz <-- master
11# \
12# --- foo/barf/bazf <-- conflict_branch
13# \
14# --- foo/bart/baz <-- clean_branch
15
16Next, 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
24'
25. ./test-lib.sh
26
27test_expect_success 'setup' '
28 # Create the above repo
29 echo foo > foo &&
30 echo bar > bar &&
31 echo baz > baz &&
32 git add foo bar baz &&
33 git commit -m initial &&
34 echo bazz > baz &&
35 git commit -a -m "second" &&
36 git checkout -b conflict_branch HEAD^ &&
37 echo barf > bar &&
38 echo bazf > baz &&
39 git commit -a -m "conflict" &&
40 git checkout -b clean_branch HEAD^ &&
41 echo bart > bar &&
42 git commit -a -m "clean" &&
43 git checkout master
44'
45
46pre_merge_head="$(git rev-parse HEAD)"
47
48test_expect_success 'fails without MERGE_HEAD (unstarted merge)' '
49 test_must_fail git merge --abort 2>output &&
50 test_i18ngrep MERGE_HEAD output
51'
52
53test_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)"
56'
57
58test_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
65'
66
67test_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)"
70'
71
72test_expect_success 'Forget previous merge' '
73 git reset --hard "$pre_merge_head"
74'
75
76test_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
81 git merge --abort &&
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)"
86'
87
88test_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
93 git merge --abort &&
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)"
98'
99
100test_expect_success 'Clean merge with dirty index fails' '
101 echo xyzzy >> foo &&
102 git add foo &&
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
110'
111
112test_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
119'
120
121test_expect_success 'Reset index (but preserve worktree changes)' '
122 git reset "$pre_merge_head" &&
123 git diff > actual &&
124 test_cmp expect actual
125'
126
127test_expect_success 'Abort clean merge with non-conflicting dirty worktree' '
128 git merge --no-commit clean_branch &&
129 test -f .git/MERGE_HEAD &&
130 # Abort merge
131 git merge --abort &&
132 test ! -f .git/MERGE_HEAD &&
133 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
134 test -z "$(git diff --staged)" &&
135 git diff > actual &&
136 test_cmp expect actual
137'
138
139test_expect_success 'Abort conflicting merge with non-conflicting dirty worktree' '
140 test_must_fail git merge conflict_branch &&
141 test -f .git/MERGE_HEAD &&
142 # Abort merge
143 git merge --abort &&
144 test ! -f .git/MERGE_HEAD &&
145 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
146 test -z "$(git diff --staged)" &&
147 git diff > actual &&
148 test_cmp expect actual
149'
150
151test_expect_success 'Reset worktree changes' '
152 git reset --hard "$pre_merge_head"
153'
154
155test_expect_success 'Fail clean merge with conflicting dirty worktree' '
156 echo xyzzy >> bar &&
157 git diff > expect &&
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)" &&
162 git diff > actual &&
163 test_cmp expect actual
164'
165
166test_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)" &&
171 git diff > actual &&
172 test_cmp expect actual
173'
174
175test_expect_success 'Reset worktree changes' '
176 git reset --hard "$pre_merge_head"
177'
178
179test_expect_success 'Fail clean merge with matching dirty worktree' '
180 echo bart > bar &&
181 git diff > expect &&
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)" &&
186 git diff > actual &&
187 test_cmp expect actual
188'
189
190test_expect_success 'Fail conflicting merge with matching dirty worktree' '
191 echo barf > bar &&
192 git diff > expect &&
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)" &&
197 git diff > actual &&
198 test_cmp expect actual
199'
200
201test_done