1#!/bin/sh
2# Copyright (c) 2006, Junio C Hamano.
3
4test_description='Per branch config variables affects "git fetch".
5
6'
7
8. ./test-lib.sh
9
10D=`pwd`
11
12test_bundle_object_count () {
13 git verify-pack -v "$1" >verify.out &&
14 test "$2" = $(grep '^[0-9a-f]\{40\} ' verify.out | wc -l)
15}
16
17test_expect_success setup '
18 echo >file original &&
19 git add file &&
20 git commit -a -m original'
21
22test_expect_success "clone and setup child repos" '
23 git clone . one &&
24 (
25 cd one &&
26 echo >file updated by one &&
27 git commit -a -m "updated by one"
28 ) &&
29 git clone . two &&
30 (
31 cd two &&
32 git config branch.master.remote one &&
33 git config remote.one.url ../one/.git/ &&
34 git config remote.one.fetch refs/heads/master:refs/heads/one
35 ) &&
36 git clone . three &&
37 (
38 cd three &&
39 git config branch.master.remote two &&
40 git config branch.master.merge refs/heads/one &&
41 mkdir -p .git/remotes &&
42 {
43 echo "URL: ../two/.git/"
44 echo "Pull: refs/heads/master:refs/heads/two"
45 echo "Pull: refs/heads/one:refs/heads/one"
46 } >.git/remotes/two
47 ) &&
48 git clone . bundle &&
49 git clone . seven
50'
51
52test_expect_success "fetch test" '
53 cd "$D" &&
54 echo >file updated by origin &&
55 git commit -a -m "updated by origin" &&
56 cd two &&
57 git fetch &&
58 test -f .git/refs/heads/one &&
59 mine=`git rev-parse refs/heads/one` &&
60 his=`cd ../one && git rev-parse refs/heads/master` &&
61 test "z$mine" = "z$his"
62'
63
64test_expect_success "fetch test for-merge" '
65 cd "$D" &&
66 cd three &&
67 git fetch &&
68 test -f .git/refs/heads/two &&
69 test -f .git/refs/heads/one &&
70 master_in_two=`cd ../two && git rev-parse master` &&
71 one_in_two=`cd ../two && git rev-parse one` &&
72 {
73 echo "$master_in_two not-for-merge"
74 echo "$one_in_two "
75 } >expected &&
76 cut -f -2 .git/FETCH_HEAD >actual &&
77 test_cmp expected actual'
78
79test_expect_success 'fetch tags when there is no tags' '
80
81 cd "$D" &&
82
83 mkdir notags &&
84 cd notags &&
85 git init &&
86
87 git fetch -t ..
88
89'
90
91test_expect_success 'fetch following tags' '
92
93 cd "$D" &&
94 git tag -a -m 'annotated' anno HEAD &&
95 git tag light HEAD &&
96
97 mkdir four &&
98 cd four &&
99 git init &&
100
101 git fetch .. :track &&
102 git show-ref --verify refs/tags/anno &&
103 git show-ref --verify refs/tags/light
104
105'
106
107test_expect_success 'fetch must not resolve short tag name' '
108
109 cd "$D" &&
110
111 mkdir five &&
112 cd five &&
113 git init &&
114
115 test_must_fail git fetch .. anno:five
116
117'
118
119test_expect_success 'fetch can now resolve short remote name' '
120
121 cd "$D" &&
122 git update-ref refs/remotes/six/HEAD HEAD &&
123
124 mkdir six &&
125 cd six &&
126 git init &&
127
128 git fetch .. six:six
129'
130
131test_expect_success 'create bundle 1' '
132 cd "$D" &&
133 echo >file updated again by origin &&
134 git commit -a -m "tip" &&
135 git bundle create bundle1 master^..master
136'
137
138test_expect_success 'header of bundle looks right' '
139 head -n 1 "$D"/bundle1 | grep "^#" &&
140 head -n 2 "$D"/bundle1 | grep "^-[0-9a-f]\{40\} " &&
141 head -n 3 "$D"/bundle1 | grep "^[0-9a-f]\{40\} " &&
142 head -n 4 "$D"/bundle1 | grep "^$"
143'
144
145test_expect_success 'create bundle 2' '
146 cd "$D" &&
147 git bundle create bundle2 master~2..master
148'
149
150test_expect_success 'unbundle 1' '
151 cd "$D/bundle" &&
152 git checkout -b some-branch &&
153 test_must_fail git fetch "$D/bundle1" master:master
154'
155
156
157test_expect_success 'bundle 1 has only 3 files ' '
158 cd "$D" &&
159 (
160 while read x && test -n "$x"
161 do
162 :;
163 done
164 cat
165 ) <bundle1 >bundle.pack &&
166 git index-pack bundle.pack &&
167 test_bundle_object_count bundle.pack 3
168'
169
170test_expect_success 'unbundle 2' '
171 cd "$D/bundle" &&
172 git fetch ../bundle2 master:master &&
173 test "tip" = "$(git log -1 --pretty=oneline master | cut -b42-)"
174'
175
176test_expect_success 'bundle does not prerequisite objects' '
177 cd "$D" &&
178 touch file2 &&
179 git add file2 &&
180 git commit -m add.file2 file2 &&
181 git bundle create bundle3 -1 HEAD &&
182 (
183 while read x && test -n "$x"
184 do
185 :;
186 done
187 cat
188 ) <bundle3 >bundle.pack &&
189 git index-pack bundle.pack &&
190 test_bundle_object_count bundle.pack 3
191'
192
193test_expect_success 'bundle should be able to create a full history' '
194
195 cd "$D" &&
196 git tag -a -m '1.0' v1.0 master &&
197 git bundle create bundle4 v1.0
198
199'
200
201! rsync --help > /dev/null 2> /dev/null &&
202say 'Skipping rsync tests because rsync was not found' || {
203test_expect_success 'fetch via rsync' '
204 git pack-refs &&
205 mkdir rsynced &&
206 (cd rsynced &&
207 git init --bare &&
208 git fetch "rsync:$(pwd)/../.git" master:refs/heads/master &&
209 git gc --prune &&
210 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
211 git fsck --full)
212'
213
214test_expect_success 'push via rsync' '
215 mkdir rsynced2 &&
216 (cd rsynced2 &&
217 git init) &&
218 (cd rsynced &&
219 git push "rsync:$(pwd)/../rsynced2/.git" master) &&
220 (cd rsynced2 &&
221 git gc --prune &&
222 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
223 git fsck --full)
224'
225
226test_expect_success 'push via rsync' '
227 mkdir rsynced3 &&
228 (cd rsynced3 &&
229 git init) &&
230 git push --all "rsync:$(pwd)/rsynced3/.git" &&
231 (cd rsynced3 &&
232 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
233 git fsck --full)
234'
235}
236
237test_expect_success 'fetch with a non-applying branch.<name>.merge' '
238 git config branch.master.remote yeti &&
239 git config branch.master.merge refs/heads/bigfoot &&
240 git config remote.blub.url one &&
241 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
242 git fetch blub
243'
244
245# URL supplied to fetch does not match the url of the configured branch's remote
246test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
247 one_head=$(cd one && git rev-parse HEAD) &&
248 this_head=$(git rev-parse HEAD) &&
249 git update-ref -d FETCH_HEAD &&
250 git fetch one &&
251 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
252 test $this_head = "$(git rev-parse --verify HEAD)"
253'
254
255# URL supplied to fetch matches the url of the configured branch's remote and
256# the merge spec matches the branch the remote HEAD points to
257test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
258 one_ref=$(cd one && git symbolic-ref HEAD) &&
259 git config branch.master.remote blub &&
260 git config branch.master.merge "$one_ref" &&
261 git update-ref -d FETCH_HEAD &&
262 git fetch one &&
263 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
264 test $this_head = "$(git rev-parse --verify HEAD)"
265'
266
267# URL supplied to fetch matches the url of the configured branch's remote, but
268# the merge spec does not match the branch the remote HEAD points to
269test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
270 git config branch.master.merge "${one_ref}_not" &&
271 git update-ref -d FETCH_HEAD &&
272 git fetch one &&
273 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
274 test $this_head = "$(git rev-parse --verify HEAD)"
275'
276
277# the strange name is: a\!'b
278test_expect_success 'quoting of a strangely named repo' '
279 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
280 cat result &&
281 grep "fatal: '\''a\\\\!'\''b'\''" result
282'
283
284test_expect_success 'bundle should record HEAD correctly' '
285
286 cd "$D" &&
287 git bundle create bundle5 HEAD master &&
288 git bundle list-heads bundle5 >actual &&
289 for h in HEAD refs/heads/master
290 do
291 echo "$(git rev-parse --verify $h) $h"
292 done >expect &&
293 test_cmp expect actual
294
295'
296
297test_expect_success 'explicit fetch should not update tracking' '
298
299 cd "$D" &&
300 git branch -f side &&
301 (
302 cd three &&
303 o=$(git rev-parse --verify refs/remotes/origin/master) &&
304 git fetch origin master &&
305 n=$(git rev-parse --verify refs/remotes/origin/master) &&
306 test "$o" = "$n" &&
307 test_must_fail git rev-parse --verify refs/remotes/origin/side
308 )
309'
310
311test_expect_success 'explicit pull should not update tracking' '
312
313 cd "$D" &&
314 git branch -f side &&
315 (
316 cd three &&
317 o=$(git rev-parse --verify refs/remotes/origin/master) &&
318 git pull origin master &&
319 n=$(git rev-parse --verify refs/remotes/origin/master) &&
320 test "$o" = "$n" &&
321 test_must_fail git rev-parse --verify refs/remotes/origin/side
322 )
323'
324
325test_expect_success 'configured fetch updates tracking' '
326
327 cd "$D" &&
328 git branch -f side &&
329 (
330 cd three &&
331 o=$(git rev-parse --verify refs/remotes/origin/master) &&
332 git fetch origin &&
333 n=$(git rev-parse --verify refs/remotes/origin/master) &&
334 test "$o" != "$n" &&
335 git rev-parse --verify refs/remotes/origin/side
336 )
337'
338
339test_expect_success 'pushing nonexistent branch by mistake should not segv' '
340
341 cd "$D" &&
342 test_must_fail git push seven no:no
343
344'
345
346test_expect_success 'auto tag following fetches minimum' '
347
348 cd "$D" &&
349 git clone .git follow &&
350 git checkout HEAD^0 &&
351 (
352 for i in 1 2 3 4 5 6 7
353 do
354 echo $i >>file &&
355 git commit -m $i -a &&
356 git tag -a -m $i excess-$i || exit 1
357 done
358 ) &&
359 git checkout master &&
360 (
361 cd follow &&
362 git fetch
363 )
364'
365
366test_expect_success 'refuse to fetch into the current branch' '
367
368 test_must_fail git fetch . side:master
369
370'
371
372test_expect_success 'fetch into the current branch with --update-head-ok' '
373
374 git fetch --update-head-ok . side:master
375
376'
377
378test_expect_success 'fetch --dry-run' '
379
380 rm -f .git/FETCH_HEAD &&
381 git fetch --dry-run . &&
382 ! test -f .git/FETCH_HEAD
383'
384
385test_expect_success "should be able to fetch with duplicate refspecs" '
386 mkdir dups &&
387 cd dups &&
388 git init &&
389 git config branch.master.remote three &&
390 git config remote.three.url ../three/.git &&
391 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
392 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
393 git fetch three
394'
395
396test_done