Sync with 2.44.1
[git/gitster.git] / t / t6406-merge-attr.sh
blob156a1efacfeabcba3cff34d1044efd522183f13d
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_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
14 test_expect_success setup '
16 for f in text binary union
18 echo Initial >$f && git add $f || return 1
19 done &&
20 test_tick &&
21 git commit -m Initial &&
23 git branch side &&
24 for f in text binary union
26 echo Main >>$f && git add $f || return 1
27 done &&
28 test_tick &&
29 git commit -m Main &&
31 git checkout side &&
32 for f in text binary union
34 echo Side >>$f && git add $f || return 1
35 done &&
36 test_tick &&
37 git commit -m Side &&
39 git tag anchor &&
41 cat >./custom-merge <<-\EOF &&
42 #!/bin/sh
44 orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
45 orig_name="$6" our_name="$7" their_name="$8"
47 echo "orig is $orig"
48 echo "ours is $ours"
49 echo "theirs is $theirs"
50 echo "path is $path"
51 echo "orig_name is $orig_name"
52 echo "our_name is $our_name"
53 echo "their_name is $their_name"
54 echo "=== orig ==="
55 cat "$orig"
56 echo "=== ours ==="
57 cat "$ours"
58 echo "=== theirs ==="
59 cat "$theirs"
60 ) >"$ours+"
61 cat "$ours+" >"$ours"
62 rm -f "$ours+"
64 if test -f ./please-abort
65 then
66 echo >>./please-abort killing myself
67 kill -9 $$
69 exit "$exit"
70 EOF
71 chmod +x ./custom-merge
74 test_expect_success merge '
76 cat >.gitattributes <<-\EOF &&
77 binary -merge
78 union merge=union
79 EOF
81 if git merge main
82 then
83 echo Gaah, should have conflicted
84 false
85 else
86 echo Ok, conflicted.
90 test_expect_success 'check merge result in index' '
92 git ls-files -u | grep binary &&
93 git ls-files -u | grep text &&
94 ! (git ls-files -u | grep union)
98 test_expect_success 'check merge result in working tree' '
100 git cat-file -p HEAD:binary >binary-orig &&
101 grep "<<<<<<<" text &&
102 cmp binary-orig binary &&
103 ! grep "<<<<<<<" union &&
104 grep Main union &&
105 grep Side union
109 test_expect_success 'retry the merge with longer context' '
110 echo text conflict-marker-size=32 >>.gitattributes &&
111 git checkout -m text &&
112 sed -ne "/^\([<=>]\)\1\1\1*/{
113 s/ .*$//
115 }" >actual text &&
116 grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
117 grep "================================" actual &&
118 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
121 test_expect_success 'custom merge backend' '
123 echo "* merge=union" >.gitattributes &&
124 echo "text merge=custom" >>.gitattributes &&
126 git reset --hard anchor &&
127 git config --replace-all \
128 merge.custom.driver "./custom-merge %O %A %B 0 %P %S %X %Y" &&
129 git config --replace-all \
130 merge.custom.name "custom merge driver for testing" &&
132 git merge main &&
134 cmp binary union &&
135 sed -e 1,3d text >check-1 &&
136 o=$(git unpack-file main^:text) &&
137 a=$(git unpack-file side^:text) &&
138 b=$(git unpack-file main:text) &&
139 base_revid=$(git rev-parse --short main^) &&
140 sh -c "./custom-merge $o $a $b 0 text $base_revid HEAD main" &&
141 sed -e 1,3d $a >check-2 &&
142 cmp check-1 check-2 &&
143 rm -f $o $a $b
146 test_expect_success 'custom merge backend' '
148 git reset --hard anchor &&
149 git config --replace-all \
150 merge.custom.driver "./custom-merge %O %A %B 1 %P %S %X %Y" &&
151 git config --replace-all \
152 merge.custom.name "custom merge driver for testing" &&
154 if git merge main
155 then
156 echo "Eh? should have conflicted"
157 false
158 else
159 echo "Ok, conflicted"
160 fi &&
162 cmp binary union &&
163 sed -e 1,3d text >check-1 &&
164 o=$(git unpack-file main^:text) &&
165 a=$(git unpack-file anchor:text) &&
166 b=$(git unpack-file main:text) &&
167 base_revid=$(git rev-parse --short main^) &&
168 sh -c "./custom-merge $o $a $b 0 text $base_revid HEAD main" &&
169 sed -e 1,3d $a >check-2 &&
170 cmp check-1 check-2 &&
171 sed -e 1,3d -e 4q $a >check-3 &&
172 echo "path is text" >expect &&
173 cmp expect check-3 &&
174 rm -f $o $a $b
177 test_expect_success !WINDOWS 'custom merge driver that is killed with a signal' '
178 test_when_finished "rm -f output please-abort" &&
180 git reset --hard anchor &&
181 git config --replace-all \
182 merge.custom.driver "./custom-merge %O %A %B 0 %P %S %X %Y" &&
183 git config --replace-all \
184 merge.custom.name "custom merge driver for testing" &&
186 >./please-abort &&
187 echo "* merge=custom" >.gitattributes &&
188 test_must_fail git merge main 2>err &&
189 grep "^error: failed to execute internal merge" err &&
190 git ls-files -u >output &&
191 git diff --name-only HEAD >>output &&
192 test_must_be_empty output
195 test_expect_success 'up-to-date merge without common ancestor' '
196 git init repo1 &&
197 git init repo2 &&
198 test_tick &&
200 cd repo1 &&
201 >a &&
202 git add a &&
203 git commit -m initial
204 ) &&
205 test_tick &&
207 cd repo2 &&
208 git commit --allow-empty -m initial
209 ) &&
210 test_tick &&
212 cd repo1 &&
213 git fetch ../repo2 main &&
214 git merge --allow-unrelated-histories FETCH_HEAD
218 test_expect_success 'custom merge does not lock index' '
219 git reset --hard anchor &&
220 write_script sleep-an-hour.sh <<-\EOF &&
221 sleep 3600 &
222 echo $! >sleep.pid
225 test_write_lines >.gitattributes \
226 "* merge=ours" "text merge=sleep-an-hour" &&
227 test_config merge.ours.driver true &&
228 test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
230 # We are testing that the custom merge driver does not block
231 # index.lock on Windows due to an inherited file handle.
232 # To ensure that the backgrounded process ran sufficiently
233 # long (and has been started in the first place), we do not
234 # ignore the result of the kill command.
235 # By packaging the command in test_when_finished, we get both
236 # the correctness check and the clean-up.
237 test_when_finished "kill \$(cat sleep.pid)" &&
238 git merge main
241 test_expect_success 'binary files with union attribute' '
242 git checkout -b bin-main &&
243 printf "base\0" >bin.txt &&
244 echo "bin.txt merge=union" >.gitattributes &&
245 git add bin.txt .gitattributes &&
246 git commit -m base &&
248 printf "one\0" >bin.txt &&
249 git commit -am one &&
251 git checkout -b bin-side HEAD^ &&
252 printf "two\0" >bin.txt &&
253 git commit -am two &&
255 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
256 then
257 test_must_fail git merge bin-main >output
258 else
259 test_must_fail git merge bin-main 2>output
260 fi &&
261 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
264 test_done