96012558909e8f25c5dc8ba28d9bb0d2d2acbc95
1#!/bin/sh
2
3test_description="merges with unrelated index changes"
4
5. ./test-lib.sh
6
7# Testcase for some simple merges
8# A
9# o-------o B
10# \
11# \-----o C
12# \
13# \---o D
14# \
15# \-o E
16# \
17# o F
18# Commit A: some file a
19# Commit B: adds file b, modifies end of a
20# Commit C: adds file c
21# Commit D: adds file d, modifies beginning of a
22# Commit E: renames a->subdir/a, adds subdir/e
23# Commit F: empty commit
24
25test_expect_success 'setup trivial merges' '
26 test_seq 1 10 >a &&
27 git add a &&
28 test_tick && git commit -m A &&
29
30 git branch A &&
31 git branch B &&
32 git branch C &&
33 git branch D &&
34 git branch E &&
35 git branch F &&
36
37 git checkout B &&
38 echo b >b &&
39 echo 11 >>a &&
40 git add a b &&
41 test_tick && git commit -m B &&
42
43 git checkout C &&
44 echo c >c &&
45 git add c &&
46 test_tick && git commit -m C &&
47
48 git checkout D &&
49 test_seq 2 10 >a &&
50 echo d >d &&
51 git add a d &&
52 test_tick && git commit -m D &&
53
54 git checkout E &&
55 mkdir subdir &&
56 git mv a subdir/a &&
57 echo e >subdir/e &&
58 git add subdir &&
59 test_tick && git commit -m E &&
60
61 git checkout F &&
62 test_tick && git commit --allow-empty -m F
63'
64
65test_expect_success 'ff update' '
66 git reset --hard &&
67 git checkout A^0 &&
68
69 touch random_file && git add random_file &&
70
71 git merge E^0 &&
72
73 test_must_fail git rev-parse HEAD:random_file &&
74 test "$(git diff --name-only --cached E)" = "random_file"
75'
76
77test_expect_success 'ff update, important file modified' '
78 git reset --hard &&
79 git checkout A^0 &&
80
81 mkdir subdir &&
82 touch subdir/e &&
83 git add subdir/e &&
84
85 test_must_fail git merge E^0 &&
86 test_path_is_missing .git/MERGE_HEAD
87'
88
89test_expect_success 'resolve, trivial' '
90 git reset --hard &&
91 git checkout B^0 &&
92
93 touch random_file && git add random_file &&
94
95 test_must_fail git merge -s resolve C^0 &&
96 test_path_is_missing .git/MERGE_HEAD
97'
98
99test_expect_success 'resolve, non-trivial' '
100 git reset --hard &&
101 git checkout B^0 &&
102
103 touch random_file && git add random_file &&
104
105 test_must_fail git merge -s resolve D^0 &&
106 test_path_is_missing .git/MERGE_HEAD
107'
108
109test_expect_success 'recursive' '
110 git reset --hard &&
111 git checkout B^0 &&
112
113 touch random_file && git add random_file &&
114
115 test_must_fail git merge -s recursive C^0 &&
116 test_path_is_missing .git/MERGE_HEAD
117'
118
119test_expect_success 'recursive, when merge branch matches merge base' '
120 git reset --hard &&
121 git checkout B^0 &&
122
123 touch random_file && git add random_file &&
124
125 test_must_fail git merge -s recursive F^0 &&
126 test_path_is_missing .git/MERGE_HEAD
127'
128
129test_expect_success 'merge-recursive, when index==head but head!=HEAD' '
130 git reset --hard &&
131 git checkout C^0 &&
132
133 # Make index match B
134 git diff C B -- | git apply --cached &&
135 # Merge B & F, with B as "head"
136 git merge-recursive A -- B F > out &&
137 test_i18ngrep "Already up to date" out
138'
139
140test_expect_failure 'recursive, when file has staged changes not matching HEAD nor what a merge would give' '
141 git reset --hard &&
142 git checkout B^0 &&
143
144 mkdir subdir &&
145 test_seq 1 10 >subdir/a &&
146 git add subdir/a &&
147
148 # HEAD has no subdir/a; merge would write 1..11 to subdir/a;
149 # Since subdir/a matches neither HEAD nor what the merge would write
150 # to that file, the merge should fail to avoid overwriting what is
151 # currently found in subdir/a
152 test_must_fail git merge -s recursive E^0
153'
154
155test_expect_failure 'recursive, when file has staged changes matching what a merge would give' '
156 git reset --hard &&
157 git checkout B^0 &&
158
159 mkdir subdir &&
160 test_seq 1 11 >subdir/a &&
161 git add subdir/a &&
162
163 # HEAD has no subdir/a; merge would write 1..11 to subdir/a;
164 # Since subdir/a matches what the merge would write to that file,
165 # the merge should be safe to proceed
166 git merge -s recursive E^0
167'
168
169test_expect_success 'octopus, unrelated file touched' '
170 git reset --hard &&
171 git checkout B^0 &&
172
173 touch random_file && git add random_file &&
174
175 test_must_fail git merge C^0 D^0 &&
176 test_path_is_missing .git/MERGE_HEAD
177'
178
179test_expect_success 'octopus, related file removed' '
180 git reset --hard &&
181 git checkout B^0 &&
182
183 git rm b &&
184
185 test_must_fail git merge C^0 D^0 &&
186 test_path_is_missing .git/MERGE_HEAD
187'
188
189test_expect_success 'octopus, related file modified' '
190 git reset --hard &&
191 git checkout B^0 &&
192
193 echo 12 >>a && git add a &&
194
195 test_must_fail git merge C^0 D^0 &&
196 test_path_is_missing .git/MERGE_HEAD
197'
198
199test_expect_success 'ours' '
200 git reset --hard &&
201 git checkout B^0 &&
202
203 touch random_file && git add random_file &&
204
205 test_must_fail git merge -s ours C^0 &&
206 test_path_is_missing .git/MERGE_HEAD
207'
208
209test_expect_success 'subtree' '
210 git reset --hard &&
211 git checkout B^0 &&
212
213 touch random_file && git add random_file &&
214
215 test_must_fail git merge -s subtree E^0 &&
216 test_path_is_missing .git/MERGE_HEAD
217'
218
219test_done