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_PASSES_SANITIZE_LEAK
=true
14 test_expect_success setup
'
16 for f in text binary union
18 echo Initial >$f && git add $f || return 1
21 git commit -m Initial &&
24 for f in text binary union
26 echo Main >>$f && git add $f || return 1
32 for f in text binary union
34 echo Side >>$f && git add $f || return 1
41 cat >./custom-merge <<-\EOF &&
44 orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
48 echo "theirs is $theirs"
61 chmod +x ./custom-merge
64 test_expect_success merge
'
66 cat >.gitattributes <<-\EOF &&
73 echo Gaah, should have conflicted
80 test_expect_success
'check merge result in index' '
82 git ls-files -u | grep binary &&
83 git ls-files -u | grep text &&
84 ! (git ls-files -u | grep union)
88 test_expect_success
'check merge result in working tree' '
90 git cat-file -p HEAD:binary >binary-orig &&
91 grep "<<<<<<<" text &&
92 cmp binary-orig binary &&
93 ! grep "<<<<<<<" union &&
99 test_expect_success
'retry the merge with longer context' '
100 echo text conflict-marker-size=32 >>.gitattributes &&
101 git checkout -m text &&
102 sed -ne "/^\([<=>]\)\1\1\1*/{
106 grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
107 grep "================================" actual &&
108 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
111 test_expect_success
'custom merge backend' '
113 echo "* merge=union" >.gitattributes &&
114 echo "text merge=custom" >>.gitattributes &&
116 git reset --hard anchor &&
117 git config --replace-all \
118 merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
119 git config --replace-all \
120 merge.custom.name "custom merge driver for testing" &&
125 sed -e 1,3d text >check-1 &&
126 o=$(git unpack-file main^:text) &&
127 a=$(git unpack-file side^:text) &&
128 b=$(git unpack-file main:text) &&
129 sh -c "./custom-merge $o $a $b 0 text" &&
130 sed -e 1,3d $a >check-2 &&
131 cmp check-1 check-2 &&
135 test_expect_success
'custom merge backend' '
137 git reset --hard anchor &&
138 git config --replace-all \
139 merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
140 git config --replace-all \
141 merge.custom.name "custom merge driver for testing" &&
145 echo "Eh? should have conflicted"
148 echo "Ok, conflicted"
152 sed -e 1,3d text >check-1 &&
153 o=$(git unpack-file main^:text) &&
154 a=$(git unpack-file anchor:text) &&
155 b=$(git unpack-file main:text) &&
156 sh -c "./custom-merge $o $a $b 0 text" &&
157 sed -e 1,3d $a >check-2 &&
158 cmp check-1 check-2 &&
159 sed -e 1,3d -e 4q $a >check-3 &&
160 echo "path is text" >expect &&
161 cmp expect check-3 &&
165 test_expect_success
'up-to-date merge without common ancestor' '
173 git commit -m initial
178 git commit --allow-empty -m initial
183 git fetch ../repo2 main &&
184 git merge --allow-unrelated-histories FETCH_HEAD
188 test_expect_success
'custom merge does not lock index' '
189 git reset --hard anchor &&
190 write_script sleep-an-hour.sh <<-\EOF &&
195 test_write_lines >.gitattributes \
196 "* merge=ours" "text merge=sleep-an-hour" &&
197 test_config merge.ours.driver true &&
198 test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
200 # We are testing that the custom merge driver does not block
201 # index.lock on Windows due to an inherited file handle.
202 # To ensure that the backgrounded process ran sufficiently
203 # long (and has been started in the first place), we do not
204 # ignore the result of the kill command.
205 # By packaging the command in test_when_finished, we get both
206 # the correctness check and the clean-up.
207 test_when_finished "kill \$(cat sleep.pid)" &&
211 test_expect_success
'binary files with union attribute' '
212 git checkout -b bin-main &&
213 printf "base\0" >bin.txt &&
214 echo "bin.txt merge=union" >.gitattributes &&
215 git add bin.txt .gitattributes &&
216 git commit -m base &&
218 printf "one\0" >bin.txt &&
219 git commit -am one &&
221 git checkout -b bin-side HEAD^ &&
222 printf "two\0" >bin.txt &&
223 git commit -am two &&
225 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
227 test_must_fail git merge bin-main >output
229 test_must_fail git merge bin-main 2>output
231 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output