]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6406-merge-attr.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t6406-merge-attr.sh
CommitLineData
47579efc
JH
1#!/bin/sh
2#
3# Copyright (c) 2007 Junio C Hamano
4#
5
6test_description='per path merge controlled by merge attribute'
7
5902f5f4 8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
47579efc
JH
11. ./test-lib.sh
12
13test_expect_success setup '
14
15 for f in text binary union
16 do
e6821d09 17 echo Initial >$f && git add $f || return 1
47579efc
JH
18 done &&
19 test_tick &&
20 git commit -m Initial &&
21
22 git branch side &&
23 for f in text binary union
24 do
5902f5f4 25 echo Main >>$f && git add $f || return 1
47579efc
JH
26 done &&
27 test_tick &&
5902f5f4 28 git commit -m Main &&
47579efc
JH
29
30 git checkout side &&
31 for f in text binary union
32 do
e6821d09 33 echo Side >>$f && git add $f || return 1
47579efc
JH
34 done &&
35 test_tick &&
f3ef6b6b 36 git commit -m Side &&
47579efc 37
42d180dd
EN
38 git tag anchor &&
39
40 cat >./custom-merge <<-\EOF &&
41 #!/bin/sh
42
43 orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
44 (
45 echo "orig is $orig"
46 echo "ours is $ours"
47 echo "theirs is $theirs"
48 echo "path is $path"
49 echo "=== orig ==="
50 cat "$orig"
51 echo "=== ours ==="
52 cat "$ours"
53 echo "=== theirs ==="
54 cat "$theirs"
55 ) >"$ours+"
56 cat "$ours+" >"$ours"
57 rm -f "$ours+"
58 exit "$exit"
59 EOF
60 chmod +x ./custom-merge
47579efc
JH
61'
62
63test_expect_success merge '
64
020b813f
ES
65 cat >.gitattributes <<-\EOF &&
66 binary -merge
67 union merge=union
68 EOF
47579efc 69
5902f5f4 70 if git merge main
47579efc
JH
71 then
72 echo Gaah, should have conflicted
73 false
74 else
75 echo Ok, conflicted.
76 fi
77'
78
79test_expect_success 'check merge result in index' '
80
81 git ls-files -u | grep binary &&
82 git ls-files -u | grep text &&
83 ! (git ls-files -u | grep union)
84
85'
86
87test_expect_success 'check merge result in working tree' '
88
89 git cat-file -p HEAD:binary >binary-orig &&
90 grep "<<<<<<<" text &&
91 cmp binary-orig binary &&
92 ! grep "<<<<<<<" union &&
5902f5f4 93 grep Main union &&
47579efc
JH
94 grep Side union
95
96'
97
4c734803
JH
98test_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*/{
102 s/ .*$//
103 p
104 }" >actual text &&
105 grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
106 grep "================================" actual &&
107 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
108'
109
f3ef6b6b
JH
110test_expect_success 'custom merge backend' '
111
112 echo "* merge=union" >.gitattributes &&
113 echo "text merge=custom" >>.gitattributes &&
114
115 git reset --hard anchor &&
116 git config --replace-all \
ef45bb1f 117 merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
153920da
JH
118 git config --replace-all \
119 merge.custom.name "custom merge driver for testing" &&
f3ef6b6b 120
5902f5f4 121 git merge main &&
f3ef6b6b
JH
122
123 cmp binary union &&
124 sed -e 1,3d text >check-1 &&
5902f5f4 125 o=$(git unpack-file main^:text) &&
3604e7c5 126 a=$(git unpack-file side^:text) &&
5902f5f4 127 b=$(git unpack-file main:text) &&
c76b84a1 128 sh -c "./custom-merge $o $a $b 0 text" &&
f3ef6b6b
JH
129 sed -e 1,3d $a >check-2 &&
130 cmp check-1 check-2 &&
131 rm -f $o $a $b
132'
133
134test_expect_success 'custom merge backend' '
135
136 git reset --hard anchor &&
137 git config --replace-all \
ef45bb1f 138 merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
153920da
JH
139 git config --replace-all \
140 merge.custom.name "custom merge driver for testing" &&
f3ef6b6b 141
5902f5f4 142 if git merge main
f3ef6b6b
JH
143 then
144 echo "Eh? should have conflicted"
145 false
146 else
147 echo "Ok, conflicted"
148 fi &&
149
150 cmp binary union &&
151 sed -e 1,3d text >check-1 &&
5902f5f4 152 o=$(git unpack-file main^:text) &&
3604e7c5 153 a=$(git unpack-file anchor:text) &&
5902f5f4 154 b=$(git unpack-file main:text) &&
c76b84a1 155 sh -c "./custom-merge $o $a $b 0 text" &&
f3ef6b6b
JH
156 sed -e 1,3d $a >check-2 &&
157 cmp check-1 check-2 &&
ef45bb1f
JH
158 sed -e 1,3d -e 4q $a >check-3 &&
159 echo "path is text" >expect &&
160 cmp expect check-3 &&
f3ef6b6b
JH
161 rm -f $o $a $b
162'
163
42716660 164test_expect_success 'up-to-date merge without common ancestor' '
6693fb3f
EN
165 git init repo1 &&
166 git init repo2 &&
42716660
MV
167 test_tick &&
168 (
169 cd repo1 &&
170 >a &&
171 git add a &&
172 git commit -m initial
173 ) &&
174 test_tick &&
175 (
176 cd repo2 &&
177 git commit --allow-empty -m initial
178 ) &&
179 test_tick &&
180 (
181 cd repo1 &&
5902f5f4 182 git fetch ../repo2 main &&
e379fdf3 183 git merge --allow-unrelated-histories FETCH_HEAD
42716660
MV
184 )
185'
186
05d1ed61 187test_expect_success 'custom merge does not lock index' '
ad65f7e3 188 git reset --hard anchor &&
a7d6bcc3
JS
189 write_script sleep-an-hour.sh <<-\EOF &&
190 sleep 3600 &
5babb5bd 191 echo $! >sleep.pid
ad65f7e3
BW
192 EOF
193
194 test_write_lines >.gitattributes \
a7d6bcc3 195 "* merge=ours" "text merge=sleep-an-hour" &&
ad65f7e3 196 test_config merge.ours.driver true &&
a7d6bcc3 197 test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
fdf4f6c7
JS
198
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)" &&
5902f5f4 207 git merge main
ad65f7e3
BW
208'
209
7d879ad7
JK
210test_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 &&
216
217 printf "one\0" >bin.txt &&
218 git commit -am one &&
219
220 git checkout -b bin-side HEAD^ &&
221 printf "two\0" >bin.txt &&
222 git commit -am two &&
223
24dbdab5
EN
224 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
225 then
226 test_must_fail git merge bin-main >output
227 else
228 test_must_fail git merge bin-main 2>output
229 fi &&
230 grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
7d879ad7
JK
231'
232
47579efc 233test_done