The twelfth batch
[git/gitster.git] / t / t7611-merge-abort.sh
blob992a8f98749baecbe109f8b839d270aecfb77fe8
1 #!/bin/sh
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
11 # \
12 # --- foo/barf/bazf <-- conflict_branch
13 # \
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
29 . ./test-lib.sh
31 test_expect_success 'setup' '
32 # Create the above repo
33 echo foo > foo &&
34 echo bar > bar &&
35 echo baz > baz &&
36 git add foo bar baz &&
37 git commit -m initial &&
38 echo bazz > baz &&
39 git commit -a -m "second" &&
40 git checkout -b conflict_branch HEAD^ &&
41 echo barf > bar &&
42 echo bazf > baz &&
43 git commit -a -m "conflict" &&
44 git checkout -b clean_branch HEAD^ &&
45 echo bart > bar &&
46 git commit -a -m "clean" &&
47 git checkout main
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
85 git merge --abort &&
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
97 git merge --abort &&
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' '
105 echo xyzzy >> foo &&
106 git add foo &&
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" &&
127 git diff > actual &&
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 &&
134 # Abort merge
135 git merge --abort &&
136 test ! -f .git/MERGE_HEAD &&
137 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
138 test -z "$(git diff --staged)" &&
139 git diff > actual &&
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 &&
146 # Abort merge
147 git merge --abort &&
148 test ! -f .git/MERGE_HEAD &&
149 test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
150 test -z "$(git diff --staged)" &&
151 git diff > actual &&
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' '
160 echo xyzzy >> bar &&
161 git diff > expect &&
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)" &&
166 git diff > actual &&
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)" &&
175 git diff > actual &&
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' '
184 echo bart > bar &&
185 git diff > expect &&
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)" &&
190 git diff > actual &&
191 test_cmp expect actual
194 test_expect_success 'Fail conflicting merge with matching dirty worktree' '
195 echo barf > bar &&
196 git diff > expect &&
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)" &&
201 git diff > actual &&
202 test_cmp expect actual
205 test_done