push: refactor refspec_append_mapped() for subsequent leak-fix
[git.git] / t / t6406-merge-attr.sh
blob5e4e4dd6d9e6b967f8c1d94fe7ce1a79c787a177
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+"
59 exit "$exit"
60 EOF
61 chmod +x ./custom-merge
64 test_expect_success merge '
66 cat >.gitattributes <<-\EOF &&
67 binary -merge
68 union merge=union
69 EOF
71 if git merge main
72 then
73 echo Gaah, should have conflicted
74 false
75 else
76 echo Ok, 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 &&
94 grep Main union &&
95 grep Side 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*/{
103 s/ .*$//
105 }" >actual text &&
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" &&
122 git merge main &&
124 cmp binary union &&
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 &&
132 rm -f $o $a $b
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" &&
143 if git merge main
144 then
145 echo "Eh? should have conflicted"
146 false
147 else
148 echo "Ok, conflicted"
149 fi &&
151 cmp binary union &&
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 &&
162 rm -f $o $a $b
165 test_expect_success 'up-to-date merge without common ancestor' '
166 git init repo1 &&
167 git init repo2 &&
168 test_tick &&
170 cd repo1 &&
171 >a &&
172 git add a &&
173 git commit -m initial
174 ) &&
175 test_tick &&
177 cd repo2 &&
178 git commit --allow-empty -m initial
179 ) &&
180 test_tick &&
182 cd repo1 &&
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 &&
191 sleep 3600 &
192 echo $! >sleep.pid
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)" &&
208 git merge main
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
226 then
227 test_must_fail git merge bin-main >output
228 else
229 test_must_fail git merge bin-main 2>output
230 fi &&
231 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
234 test_done