]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1001-read-tree-m-2way.sh
rev-parse: fix --parse-opt --keep-dashdash --stop-at-non-option
[thirdparty/git.git] / t / t1001-read-tree-m-2way.sh
CommitLineData
c8596009
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6test_description='Two way merge with read-tree -m $H $M
7
8This test tries two-way merge (aka fast forward with carry forward).
9
10There is the head (called H) and another commit (called M), which is
11simply ahead of H. The index and the work tree contains a state that
12is derived from H, but may also have local changes. This test checks
13all the combinations described in the two-tree merge "carry forward"
5be60078 14rules, found in <Documentation/git read-tree.txt>.
c8596009
JH
15
16In the test, these paths are used:
17 bozbar - in H, stays in M, modified from bozbar to gnusto
18 frotz - not in H added in M
19 nitfol - in H, stays in M unmodified
20 rezrov - in H, deleted in M
21 yomin - not in H nor M
22'
23. ./test-lib.sh
24
76bc82ca 25read_tree_twoway () {
5be60078 26 git read-tree -m "$1" "$2" && git ls-files --stage
76bc82ca
JH
27}
28
c8596009
JH
29_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
30_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
31compare_change () {
76bc82ca 32 sed -n >current \
c8596009 33 -e '/^--- /d; /^+++ /d; /^@@ /d;' \
76bc82ca
JH
34 -e 's/^\([-+][0-7][0-7][0-7][0-7][0-7][0-7]\) '"$_x40"' /\1 X /p' \
35 "$1"
3af82863 36 test_cmp expected current
c8596009
JH
37}
38
39check_cache_at () {
5be60078 40 clean_if_empty=`git diff-files -- "$1"`
c8596009
JH
41 case "$clean_if_empty" in
42 '') echo "$1: clean" ;;
43 ?*) echo "$1: dirty" ;;
44 esac
45 case "$2,$clean_if_empty" in
46 clean,) : ;;
47 clean,?*) false ;;
48 dirty,) false ;;
49 dirty,?*) : ;;
50 esac
51}
52
1abb3f14
JH
53cat >bozbar-old <<\EOF
54This is a sample file used in two-way fast forward merge
55tests. Its second line ends with a magic word bozbar
56which will be modified by the merged head to gnusto.
57It has some extra lines so that external tools can
58successfully merge independent changes made to later
59lines (such as this one), avoiding line conflicts.
60EOF
61
62sed -e 's/bozbar/gnusto (earlier bozbar)/' bozbar-old >bozbar-new
63
c8596009
JH
64test_expect_success \
65 setup \
66 'echo frotz >frotz &&
67 echo nitfol >nitfol &&
1abb3f14 68 cat bozbar-old >bozbar &&
c8596009
JH
69 echo rezrov >rezrov &&
70 echo yomin >yomin &&
5be60078
JH
71 git update-index --add nitfol bozbar rezrov &&
72 treeH=`git write-tree` &&
c8596009 73 echo treeH $treeH &&
5be60078 74 git ls-tree $treeH &&
c8596009 75
1abb3f14 76 cat bozbar-new >bozbar &&
5be60078
JH
77 git update-index --add frotz bozbar --force-remove rezrov &&
78 git ls-files --stage >M.out &&
79 treeM=`git write-tree` &&
c8596009 80 echo treeM $treeM &&
5be60078
JH
81 git ls-tree $treeM &&
82 git diff-tree $treeH $treeM'
c8596009
JH
83
84test_expect_success \
85 '1, 2, 3 - no carry forward' \
86 'rm -f .git/index &&
76bc82ca 87 read_tree_twoway $treeH $treeM &&
5be60078 88 git ls-files --stage >1-3.out &&
3af82863 89 test_cmp M.out 1-3.out &&
c8596009
JH
90 check_cache_at bozbar dirty &&
91 check_cache_at frotz dirty &&
92 check_cache_at nitfol dirty'
93
94echo '+100644 X 0 yomin' >expected
95
96test_expect_success \
97 '4 - carry forward local addition.' \
98 'rm -f .git/index &&
5be60078
JH
99 git read-tree $treeH &&
100 git checkout-index -u -f -q -a &&
101 git update-index --add yomin &&
76bc82ca 102 read_tree_twoway $treeH $treeM &&
5be60078 103 git ls-files --stage >4.out || return 1
3af82863 104 git diff --no-index M.out 4.out >4diff.out
c8596009
JH
105 compare_change 4diff.out expected &&
106 check_cache_at yomin clean'
107
108test_expect_success \
109 '5 - carry forward local addition.' \
110 'rm -f .git/index &&
5be60078
JH
111 git read-tree $treeH &&
112 git checkout-index -u -f -q -a &&
c8596009 113 echo yomin >yomin &&
5be60078 114 git update-index --add yomin &&
c8596009 115 echo yomin yomin >yomin &&
76bc82ca 116 read_tree_twoway $treeH $treeM &&
5be60078 117 git ls-files --stage >5.out || return 1
3af82863 118 git diff --no-index M.out 5.out >5diff.out
c8596009
JH
119 compare_change 5diff.out expected &&
120 check_cache_at yomin dirty'
121
122test_expect_success \
123 '6 - local addition already has the same.' \
124 'rm -f .git/index &&
5be60078
JH
125 git read-tree $treeH &&
126 git checkout-index -u -f -q -a &&
127 git update-index --add frotz &&
76bc82ca 128 read_tree_twoway $treeH $treeM &&
5be60078 129 git ls-files --stage >6.out &&
3af82863 130 test_cmp M.out 6.out &&
c8596009
JH
131 check_cache_at frotz clean'
132
133test_expect_success \
134 '7 - local addition already has the same.' \
135 'rm -f .git/index &&
5be60078
JH
136 git read-tree $treeH &&
137 git checkout-index -u -f -q -a &&
c8596009 138 echo frotz >frotz &&
5be60078 139 git update-index --add frotz &&
c8596009 140 echo frotz frotz >frotz &&
76bc82ca 141 read_tree_twoway $treeH $treeM &&
5be60078 142 git ls-files --stage >7.out &&
3af82863 143 test_cmp M.out 7.out &&
c8596009
JH
144 check_cache_at frotz dirty'
145
146test_expect_success \
147 '8 - conflicting addition.' \
148 'rm -f .git/index &&
5be60078
JH
149 git read-tree $treeH &&
150 git checkout-index -u -f -q -a &&
c8596009 151 echo frotz frotz >frotz &&
5be60078 152 git update-index --add frotz &&
76bc82ca 153 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
154
155test_expect_success \
156 '9 - conflicting addition.' \
157 'rm -f .git/index &&
5be60078
JH
158 git read-tree $treeH &&
159 git checkout-index -u -f -q -a &&
c8596009 160 echo frotz frotz >frotz &&
5be60078 161 git update-index --add frotz &&
c8596009 162 echo frotz >frotz &&
76bc82ca 163 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
164
165test_expect_success \
166 '10 - path removed.' \
167 'rm -f .git/index &&
5be60078
JH
168 git read-tree $treeH &&
169 git checkout-index -u -f -q -a &&
c8596009 170 echo rezrov >rezrov &&
5be60078 171 git update-index --add rezrov &&
76bc82ca 172 read_tree_twoway $treeH $treeM &&
5be60078 173 git ls-files --stage >10.out &&
3af82863 174 test_cmp M.out 10.out'
c8596009
JH
175
176test_expect_success \
177 '11 - dirty path removed.' \
178 'rm -f .git/index &&
5be60078
JH
179 git read-tree $treeH &&
180 git checkout-index -u -f -q -a &&
c8596009 181 echo rezrov >rezrov &&
5be60078 182 git update-index --add rezrov &&
c8596009 183 echo rezrov rezrov >rezrov &&
76bc82ca 184 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
185
186test_expect_success \
187 '12 - unmatching local changes being removed.' \
188 'rm -f .git/index &&
5be60078
JH
189 git read-tree $treeH &&
190 git checkout-index -u -f -q -a &&
c8596009 191 echo rezrov rezrov >rezrov &&
5be60078 192 git update-index --add rezrov &&
76bc82ca 193 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
194
195test_expect_success \
196 '13 - unmatching local changes being removed.' \
197 'rm -f .git/index &&
5be60078
JH
198 git read-tree $treeH &&
199 git checkout-index -u -f -q -a &&
c8596009 200 echo rezrov rezrov >rezrov &&
5be60078 201 git update-index --add rezrov &&
c8596009 202 echo rezrov >rezrov &&
76bc82ca 203 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
204
205cat >expected <<EOF
206-100644 X 0 nitfol
207+100644 X 0 nitfol
208EOF
209
210test_expect_success \
211 '14 - unchanged in two heads.' \
212 'rm -f .git/index &&
5be60078
JH
213 git read-tree $treeH &&
214 git checkout-index -u -f -q -a &&
c8596009 215 echo nitfol nitfol >nitfol &&
5be60078 216 git update-index --add nitfol &&
76bc82ca 217 read_tree_twoway $treeH $treeM &&
5be60078 218 git ls-files --stage >14.out || return 1
3af82863 219 git diff --no-index M.out 14.out >14diff.out
c8596009
JH
220 compare_change 14diff.out expected &&
221 check_cache_at nitfol clean'
222
223test_expect_success \
224 '15 - unchanged in two heads.' \
225 'rm -f .git/index &&
5be60078
JH
226 git read-tree $treeH &&
227 git checkout-index -u -f -q -a &&
c8596009 228 echo nitfol nitfol >nitfol &&
5be60078 229 git update-index --add nitfol &&
c8596009 230 echo nitfol nitfol nitfol >nitfol &&
76bc82ca 231 read_tree_twoway $treeH $treeM &&
5be60078 232 git ls-files --stage >15.out || return 1
3af82863 233 git diff --no-index M.out 15.out >15diff.out
c8596009
JH
234 compare_change 15diff.out expected &&
235 check_cache_at nitfol dirty'
236
237test_expect_success \
238 '16 - conflicting local change.' \
239 'rm -f .git/index &&
5be60078
JH
240 git read-tree $treeH &&
241 git checkout-index -u -f -q -a &&
c8596009 242 echo bozbar bozbar >bozbar &&
5be60078 243 git update-index --add bozbar &&
76bc82ca 244 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
245
246test_expect_success \
247 '17 - conflicting local change.' \
248 'rm -f .git/index &&
5be60078
JH
249 git read-tree $treeH &&
250 git checkout-index -u -f -q -a &&
c8596009 251 echo bozbar bozbar >bozbar &&
5be60078 252 git update-index --add bozbar &&
c8596009 253 echo bozbar bozbar bozbar >bozbar &&
76bc82ca 254 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009
JH
255
256test_expect_success \
257 '18 - local change already having a good result.' \
258 'rm -f .git/index &&
5be60078
JH
259 git read-tree $treeH &&
260 git checkout-index -u -f -q -a &&
1abb3f14 261 cat bozbar-new >bozbar &&
5be60078 262 git update-index --add bozbar &&
76bc82ca 263 read_tree_twoway $treeH $treeM &&
5be60078 264 git ls-files --stage >18.out &&
3af82863 265 test_cmp M.out 18.out &&
c8596009
JH
266 check_cache_at bozbar clean'
267
268test_expect_success \
269 '19 - local change already having a good result, further modified.' \
270 'rm -f .git/index &&
5be60078
JH
271 git read-tree $treeH &&
272 git checkout-index -u -f -q -a &&
1abb3f14 273 cat bozbar-new >bozbar &&
5be60078 274 git update-index --add bozbar &&
c8596009 275 echo gnusto gnusto >bozbar &&
76bc82ca 276 read_tree_twoway $treeH $treeM &&
5be60078 277 git ls-files --stage >19.out &&
3af82863 278 test_cmp M.out 19.out &&
c8596009
JH
279 check_cache_at bozbar dirty'
280
281test_expect_success \
282 '20 - no local change, use new tree.' \
283 'rm -f .git/index &&
5be60078
JH
284 git read-tree $treeH &&
285 git checkout-index -u -f -q -a &&
1abb3f14 286 cat bozbar-old >bozbar &&
5be60078 287 git update-index --add bozbar &&
76bc82ca 288 read_tree_twoway $treeH $treeM &&
5be60078 289 git ls-files --stage >20.out &&
3af82863 290 test_cmp M.out 20.out &&
c8596009
JH
291 check_cache_at bozbar dirty'
292
293test_expect_success \
294 '21 - no local change, dirty cache.' \
295 'rm -f .git/index &&
5be60078
JH
296 git read-tree $treeH &&
297 git checkout-index -u -f -q -a &&
1abb3f14 298 cat bozbar-old >bozbar &&
5be60078 299 git update-index --add bozbar &&
c8596009 300 echo gnusto gnusto >bozbar &&
76bc82ca 301 if read_tree_twoway $treeH $treeM; then false; else :; fi'
c8596009 302
1abb3f14
JH
303# This fails with straight two-way fast forward.
304test_expect_success \
305 '22 - local change cache updated.' \
306 'rm -f .git/index &&
5be60078
JH
307 git read-tree $treeH &&
308 git checkout-index -u -f -q -a &&
1abb3f14 309 sed -e "s/such as/SUCH AS/" bozbar-old >bozbar &&
5be60078 310 git update-index --add bozbar &&
1abb3f14
JH
311 if read_tree_twoway $treeH $treeM; then false; else :; fi'
312
c8596009
JH
313# Also make sure we did not break DF vs DF/DF case.
314test_expect_success \
315 'DF vs DF/DF case setup.' \
316 'rm -f .git/index &&
317 echo DF >DF &&
5be60078
JH
318 git update-index --add DF &&
319 treeDF=`git write-tree` &&
c8596009 320 echo treeDF $treeDF &&
5be60078 321 git ls-tree $treeDF &&
c8596009
JH
322
323 rm -f DF &&
324 mkdir DF &&
325 echo DF/DF >DF/DF &&
5be60078
JH
326 git update-index --add --remove DF DF/DF &&
327 treeDFDF=`git write-tree` &&
c8596009 328 echo treeDFDF $treeDFDF &&
5be60078
JH
329 git ls-tree $treeDFDF &&
330 git ls-files --stage >DFDF.out'
c8596009
JH
331
332test_expect_success \
333 'DF vs DF/DF case test.' \
334 'rm -f .git/index &&
335 rm -fr DF &&
336 echo DF >DF &&
5be60078 337 git update-index --add DF &&
76bc82ca 338 read_tree_twoway $treeDF $treeDFDF &&
5be60078 339 git ls-files --stage >DFDFcheck.out &&
3af82863 340 test_cmp DFDF.out DFDFcheck.out &&
76bc82ca
JH
341 check_cache_at DF/DF dirty &&
342 :'
c8596009 343
6b9315d5
CB
344test_expect_success \
345 'a/b (untracked) vs a case setup.' \
346 'rm -f .git/index &&
347 : >a &&
348 git update-index --add a &&
349 treeM=`git write-tree` &&
350 echo treeM $treeM &&
351 git ls-tree $treeM &&
352 git ls-files --stage >treeM.out &&
353
354 rm -f a &&
355 git update-index --remove a &&
356 mkdir a &&
357 : >a/b &&
358 treeH=`git write-tree` &&
359 echo treeH $treeH &&
360 git ls-tree $treeH'
361
362test_expect_success \
363 'a/b (untracked) vs a, plus c/d case test.' \
364 '! git read-tree -u -m "$treeH" "$treeM" &&
365 git ls-files --stage &&
366 test -f a/b'
367
837e5fe9
CB
368test_expect_success \
369 'a/b vs a, plus c/d case setup.' \
370 'rm -f .git/index &&
371 rm -fr a &&
372 : >a &&
373 mkdir c &&
374 : >c/d &&
375 git update-index --add a c/d &&
376 treeM=`git write-tree` &&
377 echo treeM $treeM &&
378 git ls-tree $treeM &&
379 git ls-files --stage >treeM.out &&
380
381 rm -f a &&
382 mkdir a
383 : >a/b &&
384 git update-index --add --remove a a/b &&
385 treeH=`git write-tree` &&
386 echo treeH $treeH &&
387 git ls-tree $treeH'
388
389test_expect_success \
390 'a/b vs a, plus c/d case test.' \
391 'git read-tree -u -m "$treeH" "$treeM" &&
392 git ls-files --stage | tee >treeMcheck.out &&
393 test_cmp treeM.out treeMcheck.out'
394
c8596009 395test_done