Merge branch 'jc/tree-walk-drop-base-offset'
[git/debian.git] / t / t6406-merge-attr.sh
blob9677180a5b3392da63ab32f4462c5ec89a9958f3
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
46 echo "orig is $orig"
47 echo "ours is $ours"
48 echo "theirs is $theirs"
49 echo "path is $path"
50 echo "=== orig ==="
51 cat "$orig"
52 echo "=== ours ==="
53 cat "$ours"
54 echo "=== theirs ==="
55 cat "$theirs"
56 ) >"$ours+"
57 cat "$ours+" >"$ours"
58 rm -f "$ours+"
60 if test -f ./please-abort
61 then
62 echo >>./please-abort killing myself
63 kill -9 $$
65 exit "$exit"
66 EOF
67 chmod +x ./custom-merge
70 test_expect_success merge '
72 cat >.gitattributes <<-\EOF &&
73 binary -merge
74 union merge=union
75 EOF
77 if git merge main
78 then
79 echo Gaah, should have conflicted
80 false
81 else
82 echo Ok, conflicted.
86 test_expect_success 'check merge result in index' '
88 git ls-files -u | grep binary &&
89 git ls-files -u | grep text &&
90 ! (git ls-files -u | grep union)
94 test_expect_success 'check merge result in working tree' '
96 git cat-file -p HEAD:binary >binary-orig &&
97 grep "<<<<<<<" text &&
98 cmp binary-orig binary &&
99 ! grep "<<<<<<<" union &&
100 grep Main union &&
101 grep Side union
105 test_expect_success 'retry the merge with longer context' '
106 echo text conflict-marker-size=32 >>.gitattributes &&
107 git checkout -m text &&
108 sed -ne "/^\([<=>]\)\1\1\1*/{
109 s/ .*$//
111 }" >actual text &&
112 grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
113 grep "================================" actual &&
114 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
117 test_expect_success 'custom merge backend' '
119 echo "* merge=union" >.gitattributes &&
120 echo "text merge=custom" >>.gitattributes &&
122 git reset --hard anchor &&
123 git config --replace-all \
124 merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
125 git config --replace-all \
126 merge.custom.name "custom merge driver for testing" &&
128 git merge main &&
130 cmp binary union &&
131 sed -e 1,3d text >check-1 &&
132 o=$(git unpack-file main^:text) &&
133 a=$(git unpack-file side^:text) &&
134 b=$(git unpack-file main:text) &&
135 sh -c "./custom-merge $o $a $b 0 text" &&
136 sed -e 1,3d $a >check-2 &&
137 cmp check-1 check-2 &&
138 rm -f $o $a $b
141 test_expect_success 'custom merge backend' '
143 git reset --hard anchor &&
144 git config --replace-all \
145 merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
146 git config --replace-all \
147 merge.custom.name "custom merge driver for testing" &&
149 if git merge main
150 then
151 echo "Eh? should have conflicted"
152 false
153 else
154 echo "Ok, conflicted"
155 fi &&
157 cmp binary union &&
158 sed -e 1,3d text >check-1 &&
159 o=$(git unpack-file main^:text) &&
160 a=$(git unpack-file anchor:text) &&
161 b=$(git unpack-file main:text) &&
162 sh -c "./custom-merge $o $a $b 0 text" &&
163 sed -e 1,3d $a >check-2 &&
164 cmp check-1 check-2 &&
165 sed -e 1,3d -e 4q $a >check-3 &&
166 echo "path is text" >expect &&
167 cmp expect check-3 &&
168 rm -f $o $a $b
171 test_expect_success !WINDOWS 'custom merge driver that is killed with a signal' '
172 test_when_finished "rm -f output please-abort" &&
174 git reset --hard anchor &&
175 git config --replace-all \
176 merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
177 git config --replace-all \
178 merge.custom.name "custom merge driver for testing" &&
180 >./please-abort &&
181 echo "* merge=custom" >.gitattributes &&
182 test_must_fail git merge main &&
183 git ls-files -u >output &&
184 git diff --name-only HEAD >>output &&
185 test_must_be_empty output
188 test_expect_success 'up-to-date merge without common ancestor' '
189 git init repo1 &&
190 git init repo2 &&
191 test_tick &&
193 cd repo1 &&
194 >a &&
195 git add a &&
196 git commit -m initial
197 ) &&
198 test_tick &&
200 cd repo2 &&
201 git commit --allow-empty -m initial
202 ) &&
203 test_tick &&
205 cd repo1 &&
206 git fetch ../repo2 main &&
207 git merge --allow-unrelated-histories FETCH_HEAD
211 test_expect_success 'custom merge does not lock index' '
212 git reset --hard anchor &&
213 write_script sleep-an-hour.sh <<-\EOF &&
214 sleep 3600 &
215 echo $! >sleep.pid
218 test_write_lines >.gitattributes \
219 "* merge=ours" "text merge=sleep-an-hour" &&
220 test_config merge.ours.driver true &&
221 test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
223 # We are testing that the custom merge driver does not block
224 # index.lock on Windows due to an inherited file handle.
225 # To ensure that the backgrounded process ran sufficiently
226 # long (and has been started in the first place), we do not
227 # ignore the result of the kill command.
228 # By packaging the command in test_when_finished, we get both
229 # the correctness check and the clean-up.
230 test_when_finished "kill \$(cat sleep.pid)" &&
231 git merge main
234 test_expect_success 'binary files with union attribute' '
235 git checkout -b bin-main &&
236 printf "base\0" >bin.txt &&
237 echo "bin.txt merge=union" >.gitattributes &&
238 git add bin.txt .gitattributes &&
239 git commit -m base &&
241 printf "one\0" >bin.txt &&
242 git commit -am one &&
244 git checkout -b bin-side HEAD^ &&
245 printf "two\0" >bin.txt &&
246 git commit -am two &&
248 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
249 then
250 test_must_fail git merge bin-main >output
251 else
252 test_must_fail git merge bin-main 2>output
253 fi &&
254 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
257 test_done