get_expanded_map(): avoid memory leak
[git] / t / t5510-fetch.sh
1 #!/bin/sh
2 # Copyright (c) 2006, Junio C Hamano.
3
4 test_description='Per branch config variables affects "git fetch".
5
6 '
7
8 . ./test-lib.sh
9
10 D=`pwd`
11
12 test_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
17 convert_bundle_to_pack () {
18         while read x && test -n "$x"
19         do
20                 :;
21         done
22         cat
23 }
24
25 test_expect_success setup '
26         echo >file original &&
27         git add file &&
28         git commit -a -m original'
29
30 test_expect_success "clone and setup child repos" '
31         git clone . one &&
32         (
33                 cd one &&
34                 echo >file updated by one &&
35                 git commit -a -m "updated by one"
36         ) &&
37         git clone . two &&
38         (
39                 cd two &&
40                 git config branch.master.remote one &&
41                 git config remote.one.url ../one/.git/ &&
42                 git config remote.one.fetch refs/heads/master:refs/heads/one
43         ) &&
44         git clone . three &&
45         (
46                 cd three &&
47                 git config branch.master.remote two &&
48                 git config branch.master.merge refs/heads/one &&
49                 mkdir -p .git/remotes &&
50                 {
51                         echo "URL: ../two/.git/"
52                         echo "Pull: refs/heads/master:refs/heads/two"
53                         echo "Pull: refs/heads/one:refs/heads/one"
54                 } >.git/remotes/two
55         ) &&
56         git clone . bundle &&
57         git clone . seven
58 '
59
60 test_expect_success "fetch test" '
61         cd "$D" &&
62         echo >file updated by origin &&
63         git commit -a -m "updated by origin" &&
64         cd two &&
65         git fetch &&
66         test -f .git/refs/heads/one &&
67         mine=`git rev-parse refs/heads/one` &&
68         his=`cd ../one && git rev-parse refs/heads/master` &&
69         test "z$mine" = "z$his"
70 '
71
72 test_expect_success "fetch test for-merge" '
73         cd "$D" &&
74         cd three &&
75         git fetch &&
76         test -f .git/refs/heads/two &&
77         test -f .git/refs/heads/one &&
78         master_in_two=`cd ../two && git rev-parse master` &&
79         one_in_two=`cd ../two && git rev-parse one` &&
80         {
81                 echo "$one_in_two       "
82                 echo "$master_in_two    not-for-merge"
83         } >expected &&
84         cut -f -2 .git/FETCH_HEAD >actual &&
85         test_cmp expected actual'
86
87 test_expect_success 'fetch --prune on its own works as expected' '
88         cd "$D" &&
89         git clone . prune &&
90         cd prune &&
91         git update-ref refs/remotes/origin/extrabranch master &&
92
93         git fetch --prune origin &&
94         test_must_fail git rev-parse origin/extrabranch
95 '
96
97 test_expect_success 'fetch --prune with a branch name keeps branches' '
98         cd "$D" &&
99         git clone . prune-branch &&
100         cd prune-branch &&
101         git update-ref refs/remotes/origin/extrabranch master &&
102
103         git fetch --prune origin master &&
104         git rev-parse origin/extrabranch
105 '
106
107 test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
108         cd "$D" &&
109         git clone . prune-namespace &&
110         cd prune-namespace &&
111
112         git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
113         git rev-parse origin/master
114 '
115
116 test_expect_success 'fetch --prune --tags does not delete the remote-tracking branches' '
117         cd "$D" &&
118         git clone . prune-tags &&
119         cd prune-tags &&
120         git tag sometag master &&
121         # Create what looks like a remote-tracking branch from an earlier
122         # fetch that has since been deleted from the remote:
123         git update-ref refs/remotes/origin/fake-remote master &&
124
125         git fetch --prune --tags origin &&
126         git rev-parse origin/master &&
127         git rev-parse origin/fake-remote &&
128         test_must_fail git rev-parse sometag
129 '
130
131 test_expect_success 'fetch --prune --tags with branch does not delete other remote-tracking branches' '
132         cd "$D" &&
133         git clone . prune-tags-branch &&
134         cd prune-tags-branch &&
135         git update-ref refs/remotes/origin/extrabranch master &&
136
137         git fetch --prune --tags origin master &&
138         git rev-parse origin/extrabranch
139 '
140
141 test_expect_success 'fetch tags when there is no tags' '
142
143     cd "$D" &&
144
145     mkdir notags &&
146     cd notags &&
147     git init &&
148
149     git fetch -t ..
150
151 '
152
153 test_expect_success 'fetch following tags' '
154
155         cd "$D" &&
156         git tag -a -m 'annotated' anno HEAD &&
157         git tag light HEAD &&
158
159         mkdir four &&
160         cd four &&
161         git init &&
162
163         git fetch .. :track &&
164         git show-ref --verify refs/tags/anno &&
165         git show-ref --verify refs/tags/light
166
167 '
168
169 test_expect_success 'fetch uses remote ref names to describe new refs' '
170         cd "$D" &&
171         git init descriptive &&
172         (
173                 cd descriptive &&
174                 git config remote.o.url .. &&
175                 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
176                 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
177                 git fetch o
178         ) &&
179         git tag -a -m "Descriptive tag" descriptive-tag &&
180         git branch descriptive-branch &&
181         git checkout descriptive-branch &&
182         echo "Nuts" >crazy &&
183         git add crazy &&
184         git commit -a -m "descriptive commit" &&
185         git update-ref refs/others/crazy HEAD &&
186         (
187                 cd descriptive &&
188                 git fetch o 2>actual &&
189                 grep " -> refs/crazyheads/descriptive-branch$" actual |
190                 test_i18ngrep "new branch" &&
191                 grep " -> descriptive-tag$" actual |
192                 test_i18ngrep "new tag" &&
193                 grep " -> crazy$" actual |
194                 test_i18ngrep "new ref"
195         ) &&
196         git checkout master
197 '
198
199 test_expect_success 'fetch must not resolve short tag name' '
200
201         cd "$D" &&
202
203         mkdir five &&
204         cd five &&
205         git init &&
206
207         test_must_fail git fetch .. anno:five
208
209 '
210
211 test_expect_success 'fetch can now resolve short remote name' '
212
213         cd "$D" &&
214         git update-ref refs/remotes/six/HEAD HEAD &&
215
216         mkdir six &&
217         cd six &&
218         git init &&
219
220         git fetch .. six:six
221 '
222
223 test_expect_success 'create bundle 1' '
224         cd "$D" &&
225         echo >file updated again by origin &&
226         git commit -a -m "tip" &&
227         git bundle create bundle1 master^..master
228 '
229
230 test_expect_success 'header of bundle looks right' '
231         head -n 1 "$D"/bundle1 | grep "^#" &&
232         head -n 2 "$D"/bundle1 | grep "^-[0-9a-f]\{40\} " &&
233         head -n 3 "$D"/bundle1 | grep "^[0-9a-f]\{40\} " &&
234         head -n 4 "$D"/bundle1 | grep "^$"
235 '
236
237 test_expect_success 'create bundle 2' '
238         cd "$D" &&
239         git bundle create bundle2 master~2..master
240 '
241
242 test_expect_success 'unbundle 1' '
243         cd "$D/bundle" &&
244         git checkout -b some-branch &&
245         test_must_fail git fetch "$D/bundle1" master:master
246 '
247
248
249 test_expect_success 'bundle 1 has only 3 files ' '
250         cd "$D" &&
251         convert_bundle_to_pack <bundle1 >bundle.pack &&
252         git index-pack bundle.pack &&
253         test_bundle_object_count bundle.pack 3
254 '
255
256 test_expect_success 'unbundle 2' '
257         cd "$D/bundle" &&
258         git fetch ../bundle2 master:master &&
259         test "tip" = "$(git log -1 --pretty=oneline master | cut -b42-)"
260 '
261
262 test_expect_success 'bundle does not prerequisite objects' '
263         cd "$D" &&
264         touch file2 &&
265         git add file2 &&
266         git commit -m add.file2 file2 &&
267         git bundle create bundle3 -1 HEAD &&
268         convert_bundle_to_pack <bundle3 >bundle.pack &&
269         git index-pack bundle.pack &&
270         test_bundle_object_count bundle.pack 3
271 '
272
273 test_expect_success 'bundle should be able to create a full history' '
274
275         cd "$D" &&
276         git tag -a -m '1.0' v1.0 master &&
277         git bundle create bundle4 v1.0
278
279 '
280
281 ! rsync --help > /dev/null 2> /dev/null &&
282 say 'Skipping rsync tests because rsync was not found' || {
283 test_expect_success 'fetch via rsync' '
284         git pack-refs &&
285         mkdir rsynced &&
286         (cd rsynced &&
287          git init --bare &&
288          git fetch "rsync:$(pwd)/../.git" master:refs/heads/master &&
289          git gc --prune &&
290          test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
291          git fsck --full)
292 '
293
294 test_expect_success 'push via rsync' '
295         mkdir rsynced2 &&
296         (cd rsynced2 &&
297          git init) &&
298         (cd rsynced &&
299          git push "rsync:$(pwd)/../rsynced2/.git" master) &&
300         (cd rsynced2 &&
301          git gc --prune &&
302          test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
303          git fsck --full)
304 '
305
306 test_expect_success 'push via rsync' '
307         mkdir rsynced3 &&
308         (cd rsynced3 &&
309          git init) &&
310         git push --all "rsync:$(pwd)/rsynced3/.git" &&
311         (cd rsynced3 &&
312          test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
313          git fsck --full)
314 '
315 }
316
317 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
318         git config branch.master.remote yeti &&
319         git config branch.master.merge refs/heads/bigfoot &&
320         git config remote.blub.url one &&
321         git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
322         git fetch blub
323 '
324
325 # URL supplied to fetch does not match the url of the configured branch's remote
326 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
327         one_head=$(cd one && git rev-parse HEAD) &&
328         this_head=$(git rev-parse HEAD) &&
329         git update-ref -d FETCH_HEAD &&
330         git fetch one &&
331         test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
332         test $this_head = "$(git rev-parse --verify HEAD)"
333 '
334
335 # URL supplied to fetch matches the url of the configured branch's remote and
336 # the merge spec matches the branch the remote HEAD points to
337 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
338         one_ref=$(cd one && git symbolic-ref HEAD) &&
339         git config branch.master.remote blub &&
340         git config branch.master.merge "$one_ref" &&
341         git update-ref -d FETCH_HEAD &&
342         git fetch one &&
343         test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
344         test $this_head = "$(git rev-parse --verify HEAD)"
345 '
346
347 # URL supplied to fetch matches the url of the configured branch's remote, but
348 # the merge spec does not match the branch the remote HEAD points to
349 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
350         git config branch.master.merge "${one_ref}_not" &&
351         git update-ref -d FETCH_HEAD &&
352         git fetch one &&
353         test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
354         test $this_head = "$(git rev-parse --verify HEAD)"
355 '
356
357 # the strange name is: a\!'b
358 test_expect_success 'quoting of a strangely named repo' '
359         test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
360         cat result &&
361         grep "fatal: '\''a\\\\!'\''b'\''" result
362 '
363
364 test_expect_success 'bundle should record HEAD correctly' '
365
366         cd "$D" &&
367         git bundle create bundle5 HEAD master &&
368         git bundle list-heads bundle5 >actual &&
369         for h in HEAD refs/heads/master
370         do
371                 echo "$(git rev-parse --verify $h) $h"
372         done >expect &&
373         test_cmp expect actual
374
375 '
376
377 test_expect_success 'mark initial state of origin/master' '
378         (
379                 cd three &&
380                 git tag base-origin-master refs/remotes/origin/master
381         )
382 '
383
384 test_expect_success 'explicit fetch should update tracking' '
385
386         cd "$D" &&
387         git branch -f side &&
388         (
389                 cd three &&
390                 git update-ref refs/remotes/origin/master base-origin-master &&
391                 o=$(git rev-parse --verify refs/remotes/origin/master) &&
392                 git fetch origin master &&
393                 n=$(git rev-parse --verify refs/remotes/origin/master) &&
394                 test "$o" != "$n" &&
395                 test_must_fail git rev-parse --verify refs/remotes/origin/side
396         )
397 '
398
399 test_expect_success 'explicit pull should update tracking' '
400
401         cd "$D" &&
402         git branch -f side &&
403         (
404                 cd three &&
405                 git update-ref refs/remotes/origin/master base-origin-master &&
406                 o=$(git rev-parse --verify refs/remotes/origin/master) &&
407                 git pull origin master &&
408                 n=$(git rev-parse --verify refs/remotes/origin/master) &&
409                 test "$o" != "$n" &&
410                 test_must_fail git rev-parse --verify refs/remotes/origin/side
411         )
412 '
413
414 test_expect_success 'configured fetch updates tracking' '
415
416         cd "$D" &&
417         git branch -f side &&
418         (
419                 cd three &&
420                 git update-ref refs/remotes/origin/master base-origin-master &&
421                 o=$(git rev-parse --verify refs/remotes/origin/master) &&
422                 git fetch origin &&
423                 n=$(git rev-parse --verify refs/remotes/origin/master) &&
424                 test "$o" != "$n" &&
425                 git rev-parse --verify refs/remotes/origin/side
426         )
427 '
428
429 test_expect_success 'non-matching refspecs do not confuse tracking update' '
430         cd "$D" &&
431         git update-ref refs/odd/location HEAD &&
432         (
433                 cd three &&
434                 git update-ref refs/remotes/origin/master base-origin-master &&
435                 git config --add remote.origin.fetch \
436                         refs/odd/location:refs/remotes/origin/odd &&
437                 o=$(git rev-parse --verify refs/remotes/origin/master) &&
438                 git fetch origin master &&
439                 n=$(git rev-parse --verify refs/remotes/origin/master) &&
440                 test "$o" != "$n" &&
441                 test_must_fail git rev-parse --verify refs/remotes/origin/odd
442         )
443 '
444
445 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
446
447         cd "$D" &&
448         test_must_fail git push seven no:no
449
450 '
451
452 test_expect_success 'auto tag following fetches minimum' '
453
454         cd "$D" &&
455         git clone .git follow &&
456         git checkout HEAD^0 &&
457         (
458                 for i in 1 2 3 4 5 6 7
459                 do
460                         echo $i >>file &&
461                         git commit -m $i -a &&
462                         git tag -a -m $i excess-$i || exit 1
463                 done
464         ) &&
465         git checkout master &&
466         (
467                 cd follow &&
468                 git fetch
469         )
470 '
471
472 test_expect_success 'refuse to fetch into the current branch' '
473
474         test_must_fail git fetch . side:master
475
476 '
477
478 test_expect_success 'fetch into the current branch with --update-head-ok' '
479
480         git fetch --update-head-ok . side:master
481
482 '
483
484 test_expect_success 'fetch --dry-run' '
485
486         rm -f .git/FETCH_HEAD &&
487         git fetch --dry-run . &&
488         ! test -f .git/FETCH_HEAD
489 '
490
491 test_expect_success "should be able to fetch with duplicate refspecs" '
492         mkdir dups &&
493         (
494                 cd dups &&
495                 git init &&
496                 git config branch.master.remote three &&
497                 git config remote.three.url ../three/.git &&
498                 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
499                 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
500                 git fetch three
501         )
502 '
503
504 # configured prune tests
505
506 set_config_tristate () {
507         # var=$1 val=$2
508         case "$2" in
509         unset)  test_unconfig "$1" ;;
510         *)      git config "$1" "$2" ;;
511         esac
512 }
513
514 test_configured_prune () {
515         fetch_prune=$1 remote_origin_prune=$2 cmdline=$3 expected=$4
516
517         test_expect_success "prune fetch.prune=$1 remote.origin.prune=$2${3:+ $3}; $4" '
518                 # make sure a newbranch is there in . and also in one
519                 git branch -f newbranch &&
520                 (
521                         cd one &&
522                         test_unconfig fetch.prune &&
523                         test_unconfig remote.origin.prune &&
524                         git fetch &&
525                         git rev-parse --verify refs/remotes/origin/newbranch
526                 )
527
528                 # now remove it
529                 git branch -d newbranch &&
530
531                 # then test
532                 (
533                         cd one &&
534                         set_config_tristate fetch.prune $fetch_prune &&
535                         set_config_tristate remote.origin.prune $remote_origin_prune &&
536
537                         git fetch $cmdline &&
538                         case "$expected" in
539                         pruned)
540                                 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
541                                 ;;
542                         kept)
543                                 git rev-parse --verify refs/remotes/origin/newbranch
544                                 ;;
545                         esac
546                 )
547         '
548 }
549
550 test_configured_prune unset unset ""            kept
551 test_configured_prune unset unset "--no-prune"  kept
552 test_configured_prune unset unset "--prune"     pruned
553
554 test_configured_prune false unset ""            kept
555 test_configured_prune false unset "--no-prune"  kept
556 test_configured_prune false unset "--prune"     pruned
557
558 test_configured_prune true  unset ""            pruned
559 test_configured_prune true  unset "--prune"     pruned
560 test_configured_prune true  unset "--no-prune"  kept
561
562 test_configured_prune unset false ""            kept
563 test_configured_prune unset false "--no-prune"  kept
564 test_configured_prune unset false "--prune"     pruned
565
566 test_configured_prune false false ""            kept
567 test_configured_prune false false "--no-prune"  kept
568 test_configured_prune false false "--prune"     pruned
569
570 test_configured_prune true  false ""            kept
571 test_configured_prune true  false "--prune"     pruned
572 test_configured_prune true  false "--no-prune"  kept
573
574 test_configured_prune unset true  ""            pruned
575 test_configured_prune unset true  "--no-prune"  kept
576 test_configured_prune unset true  "--prune"     pruned
577
578 test_configured_prune false true  ""            pruned
579 test_configured_prune false true  "--no-prune"  kept
580 test_configured_prune false true  "--prune"     pruned
581
582 test_configured_prune true  true  ""            pruned
583 test_configured_prune true  true  "--prune"     pruned
584 test_configured_prune true  true  "--no-prune"  kept
585
586 test_expect_success 'all boundary commits are excluded' '
587         test_commit base &&
588         test_commit oneside &&
589         git checkout HEAD^ &&
590         test_commit otherside &&
591         git checkout master &&
592         test_tick &&
593         git merge otherside &&
594         ad=$(git log --no-walk --format=%ad HEAD) &&
595         git bundle create twoside-boundary.bdl master --since="$ad" &&
596         convert_bundle_to_pack <twoside-boundary.bdl >twoside-boundary.pack &&
597         pack=$(git index-pack --fix-thin --stdin <twoside-boundary.pack) &&
598         test_bundle_object_count .git/objects/pack/pack-${pack##pack    }.pack 3
599 '
600
601 test_done