clone_submodule: avoid using `access()` on directories
[git.git] / t / t6406-merge-attr.sh
blob8650a88c40a2a58182ee39f36cdc59315ae1a35c
1 #!/bin/sh
3 # Copyright (c) 2007 Junio C Hamano
6 test_description='per path merge controlled by merge attribute'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 . ./test-lib.sh
13 test_expect_success setup '
15 for f in text binary union
17 echo Initial >$f && git add $f || return 1
18 done &&
19 test_tick &&
20 git commit -m Initial &&
22 git branch side &&
23 for f in text binary union
25 echo Main >>$f && git add $f || return 1
26 done &&
27 test_tick &&
28 git commit -m Main &&
30 git checkout side &&
31 for f in text binary union
33 echo Side >>$f && git add $f || return 1
34 done &&
35 test_tick &&
36 git commit -m Side &&
38 git tag anchor &&
40 cat >./custom-merge <<-\EOF &&
41 #!/bin/sh
43 orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
45 echo "orig is $orig"
46 echo "ours is $ours"
47 echo "theirs is $theirs"
48 echo "path is $path"
49 echo "=== orig ==="
50 cat "$orig"
51 echo "=== ours ==="
52 cat "$ours"
53 echo "=== theirs ==="
54 cat "$theirs"
55 ) >"$ours+"
56 cat "$ours+" >"$ours"
57 rm -f "$ours+"
58 exit "$exit"
59 EOF
60 chmod +x ./custom-merge
63 test_expect_success merge '
65 cat >.gitattributes <<-\EOF &&
66 binary -merge
67 union merge=union
68 EOF
70 if git merge main
71 then
72 echo Gaah, should have conflicted
73 false
74 else
75 echo Ok, conflicted.
79 test_expect_success 'check merge result in index' '
81 git ls-files -u | grep binary &&
82 git ls-files -u | grep text &&
83 ! (git ls-files -u | grep union)
87 test_expect_success 'check merge result in working tree' '
89 git cat-file -p HEAD:binary >binary-orig &&
90 grep "<<<<<<<" text &&
91 cmp binary-orig binary &&
92 ! grep "<<<<<<<" union &&
93 grep Main union &&
94 grep Side union
98 test_expect_success 'retry the merge with longer context' '
99 echo text conflict-marker-size=32 >>.gitattributes &&
100 git checkout -m text &&
101 sed -ne "/^\([<=>]\)\1\1\1*/{
102 s/ .*$//
104 }" >actual text &&
105 grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
106 grep "================================" actual &&
107 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
110 test_expect_success 'custom merge backend' '
112 echo "* merge=union" >.gitattributes &&
113 echo "text merge=custom" >>.gitattributes &&
115 git reset --hard anchor &&
116 git config --replace-all \
117 merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
118 git config --replace-all \
119 merge.custom.name "custom merge driver for testing" &&
121 git merge main &&
123 cmp binary union &&
124 sed -e 1,3d text >check-1 &&
125 o=$(git unpack-file main^:text) &&
126 a=$(git unpack-file side^:text) &&
127 b=$(git unpack-file main:text) &&
128 sh -c "./custom-merge $o $a $b 0 text" &&
129 sed -e 1,3d $a >check-2 &&
130 cmp check-1 check-2 &&
131 rm -f $o $a $b
134 test_expect_success 'custom merge backend' '
136 git reset --hard anchor &&
137 git config --replace-all \
138 merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
139 git config --replace-all \
140 merge.custom.name "custom merge driver for testing" &&
142 if git merge main
143 then
144 echo "Eh? should have conflicted"
145 false
146 else
147 echo "Ok, conflicted"
148 fi &&
150 cmp binary union &&
151 sed -e 1,3d text >check-1 &&
152 o=$(git unpack-file main^:text) &&
153 a=$(git unpack-file anchor:text) &&
154 b=$(git unpack-file main:text) &&
155 sh -c "./custom-merge $o $a $b 0 text" &&
156 sed -e 1,3d $a >check-2 &&
157 cmp check-1 check-2 &&
158 sed -e 1,3d -e 4q $a >check-3 &&
159 echo "path is text" >expect &&
160 cmp expect check-3 &&
161 rm -f $o $a $b
164 test_expect_success 'up-to-date merge without common ancestor' '
165 git init repo1 &&
166 git init repo2 &&
167 test_tick &&
169 cd repo1 &&
170 >a &&
171 git add a &&
172 git commit -m initial
173 ) &&
174 test_tick &&
176 cd repo2 &&
177 git commit --allow-empty -m initial
178 ) &&
179 test_tick &&
181 cd repo1 &&
182 git fetch ../repo2 main &&
183 git merge --allow-unrelated-histories FETCH_HEAD
187 test_expect_success 'custom merge does not lock index' '
188 git reset --hard anchor &&
189 write_script sleep-an-hour.sh <<-\EOF &&
190 sleep 3600 &
191 echo $! >sleep.pid
194 test_write_lines >.gitattributes \
195 "* merge=ours" "text merge=sleep-an-hour" &&
196 test_config merge.ours.driver true &&
197 test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
199 # We are testing that the custom merge driver does not block
200 # index.lock on Windows due to an inherited file handle.
201 # To ensure that the backgrounded process ran sufficiently
202 # long (and has been started in the first place), we do not
203 # ignore the result of the kill command.
204 # By packaging the command in test_when_finished, we get both
205 # the correctness check and the clean-up.
206 test_when_finished "kill \$(cat sleep.pid)" &&
207 git merge main
210 test_expect_success 'binary files with union attribute' '
211 git checkout -b bin-main &&
212 printf "base\0" >bin.txt &&
213 echo "bin.txt merge=union" >.gitattributes &&
214 git add bin.txt .gitattributes &&
215 git commit -m base &&
217 printf "one\0" >bin.txt &&
218 git commit -am one &&
220 git checkout -b bin-side HEAD^ &&
221 printf "two\0" >bin.txt &&
222 git commit -am two &&
224 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
225 then
226 test_must_fail git merge bin-main >output
227 else
228 test_must_fail git merge bin-main 2>output
229 fi &&
230 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
233 test_done