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
13 test_expect_success setup
'
15 for f in text binary union
17 echo Initial >$f && git add $f || return 1
20 git commit -m Initial &&
23 for f in text binary union
25 echo Main >>$f && git add $f || return 1
31 for f in text binary union
33 echo Side >>$f && git add $f || return 1
40 cat >./custom-merge <<-\EOF &&
43 orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
47 echo "theirs is $theirs"
60 chmod +x ./custom-merge
63 test_expect_success merge
'
65 cat >.gitattributes <<-\EOF &&
72 echo Gaah, should have 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 &&
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*/{
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" &&
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 &&
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" &&
144 echo "Eh? should have conflicted"
147 echo "Ok, conflicted"
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 &&
164 test_expect_success
'up-to-date merge without common ancestor' '
172 git commit -m initial
177 git commit --allow-empty -m initial
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 &&
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)" &&
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
226 test_must_fail git merge bin-main >output
228 test_must_fail git merge bin-main 2>output
230 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output