t5510-fetch.sh 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/bin/sh
# Copyright (c) 2006, Junio C Hamano.

test_description='Per branch config variables affects "git fetch".

'

. ./test-lib.sh

D=`pwd`

12 13 14 15 16
test_bundle_object_count () {
	git verify-pack -v "$1" >verify.out &&
	test "$2" = $(grep '^[0-9a-f]\{40\} ' verify.out | wc -l)
}

17 18 19 20 21 22 23 24
convert_bundle_to_pack () {
	while read x && test -n "$x"
	do
		:;
	done
	cat
}

25 26 27 28 29 30 31
test_expect_success setup '
	echo >file original &&
	git add file &&
	git commit -a -m original'

test_expect_success "clone and setup child repos" '
	git clone . one &&
32 33 34 35
	(
		cd one &&
		echo >file updated by one &&
		git commit -a -m "updated by one"
36
	) &&
37
	git clone . two &&
38 39 40 41 42
	(
		cd two &&
		git config branch.master.remote one &&
		git config remote.one.url ../one/.git/ &&
		git config remote.one.fetch refs/heads/master:refs/heads/one
43
	) &&
44
	git clone . three &&
45 46 47 48 49 50 51 52 53 54
	(
		cd three &&
		git config branch.master.remote two &&
		git config branch.master.merge refs/heads/one &&
		mkdir -p .git/remotes &&
		{
			echo "URL: ../two/.git/"
			echo "Pull: refs/heads/master:refs/heads/two"
			echo "Pull: refs/heads/one:refs/heads/one"
		} >.git/remotes/two
55
	) &&
56 57
	git clone . bundle &&
	git clone . seven
58 59 60 61 62 63 64 65 66 67 68 69 70 71
'

test_expect_success "fetch test" '
	cd "$D" &&
	echo >file updated by origin &&
	git commit -a -m "updated by origin" &&
	cd two &&
	git fetch &&
	test -f .git/refs/heads/one &&
	mine=`git rev-parse refs/heads/one` &&
	his=`cd ../one && git rev-parse refs/heads/master` &&
	test "z$mine" = "z$his"
'

72 73 74 75 76 77 78 79 80 81 82 83 84
test_expect_success "fetch test for-merge" '
	cd "$D" &&
	cd three &&
	git fetch &&
	test -f .git/refs/heads/two &&
	test -f .git/refs/heads/one &&
	master_in_two=`cd ../two && git rev-parse master` &&
	one_in_two=`cd ../two && git rev-parse one` &&
	{
		echo "$master_in_two	not-for-merge"
		echo "$one_in_two	"
	} >expected &&
	cut -f -2 .git/FETCH_HEAD >actual &&
85
	test_cmp expected actual'
86

87 88 89 90 91 92 93 94 95 96
test_expect_success 'fetch --prune on its own works as expected' '
	cd "$D" &&
	git clone . prune &&
	cd prune &&
	git fetch origin refs/heads/master:refs/remotes/origin/extrabranch &&

	git fetch --prune origin &&
	test_must_fail git rev-parse origin/extrabranch
'

97
test_expect_success 'fetch --prune with a branch name keeps branches' '
98 99 100 101 102 103 104 105 106
	cd "$D" &&
	git clone . prune-branch &&
	cd prune-branch &&
	git fetch origin refs/heads/master:refs/remotes/origin/extrabranch &&

	git fetch --prune origin master &&
	git rev-parse origin/extrabranch
'

107
test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
108 109 110 111 112 113 114 115
	cd "$D" &&
	git clone . prune-namespace &&
	cd prune-namespace &&

	git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
	git rev-parse origin/master
'

116
test_expect_success 'fetch --prune --tags does not delete the remote-tracking branches' '
117 118 119 120 121 122 123 124 125 126
	cd "$D" &&
	git clone . prune-tags &&
	cd prune-tags &&
	git fetch origin refs/heads/master:refs/tags/sometag &&

	git fetch --prune --tags origin &&
	git rev-parse origin/master &&
	test_must_fail git rev-parse somebranch
'

127
test_expect_success 'fetch --prune --tags with branch does not delete other remote-tracking branches' '
128 129 130 131 132 133 134 135 136
	cd "$D" &&
	git clone . prune-tags-branch &&
	cd prune-tags-branch &&
	git fetch origin refs/heads/master:refs/remotes/origin/extrabranch &&

	git fetch --prune --tags origin master &&
	git rev-parse origin/extrabranch
'

137 138 139 140 141 142 143 144 145 146 147 148
test_expect_success 'fetch tags when there is no tags' '

    cd "$D" &&

    mkdir notags &&
    cd notags &&
    git init &&

    git fetch -t ..

'

149 150 151 152 153 154 155 156
test_expect_success 'fetch following tags' '

	cd "$D" &&
	git tag -a -m 'annotated' anno HEAD &&
	git tag light HEAD &&

	mkdir four &&
	cd four &&
157
	git init &&
158 159 160 161 162 163 164

	git fetch .. :track &&
	git show-ref --verify refs/tags/anno &&
	git show-ref --verify refs/tags/light

'

J
Junio C Hamano 已提交
165
test_expect_success 'fetch must not resolve short tag name' '
166 167 168 169 170 171 172

	cd "$D" &&

	mkdir five &&
	cd five &&
	git init &&

173
	test_must_fail git fetch .. anno:five
174 175 176

'

J
Junio C Hamano 已提交
177
test_expect_success 'fetch must not resolve short remote name' '
178 179

	cd "$D" &&
J
Jonathan Nieder 已提交
180
	git update-ref refs/remotes/six/HEAD HEAD &&
181 182 183 184 185

	mkdir six &&
	cd six &&
	git init &&

186
	test_must_fail git fetch .. six:six
187 188 189

'

190 191 192 193 194 195 196
test_expect_success 'create bundle 1' '
	cd "$D" &&
	echo >file updated again by origin &&
	git commit -a -m "tip" &&
	git bundle create bundle1 master^..master
'

197 198 199 200 201 202 203
test_expect_success 'header of bundle looks right' '
	head -n 1 "$D"/bundle1 | grep "^#" &&
	head -n 2 "$D"/bundle1 | grep "^-[0-9a-f]\{40\} " &&
	head -n 3 "$D"/bundle1 | grep "^[0-9a-f]\{40\} " &&
	head -n 4 "$D"/bundle1 | grep "^$"
'

204 205 206 207 208
test_expect_success 'create bundle 2' '
	cd "$D" &&
	git bundle create bundle2 master~2..master
'

J
Junio C Hamano 已提交
209
test_expect_success 'unbundle 1' '
210 211
	cd "$D/bundle" &&
	git checkout -b some-branch &&
212
	test_must_fail git fetch "$D/bundle1" master:master
213 214
'

215

216 217
test_expect_success 'bundle 1 has only 3 files ' '
	cd "$D" &&
218
	convert_bundle_to_pack <bundle1 >bundle.pack &&
219
	git index-pack bundle.pack &&
220
	test_bundle_object_count bundle.pack 3
221 222
'

223 224 225 226 227 228
test_expect_success 'unbundle 2' '
	cd "$D/bundle" &&
	git fetch ../bundle2 master:master &&
	test "tip" = "$(git log -1 --pretty=oneline master | cut -b42-)"
'

229 230 231 232 233 234
test_expect_success 'bundle does not prerequisite objects' '
	cd "$D" &&
	touch file2 &&
	git add file2 &&
	git commit -m add.file2 file2 &&
	git bundle create bundle3 -1 HEAD &&
235
	convert_bundle_to_pack <bundle3 >bundle.pack &&
236
	git index-pack bundle.pack &&
237
	test_bundle_object_count bundle.pack 3
238 239
'

240 241 242 243 244 245 246 247
test_expect_success 'bundle should be able to create a full history' '

	cd "$D" &&
	git tag -a -m '1.0' v1.0 master &&
	git bundle create bundle4 v1.0

'

248 249
! rsync --help > /dev/null 2> /dev/null &&
say 'Skipping rsync tests because rsync was not found' || {
250 251 252
test_expect_success 'fetch via rsync' '
	git pack-refs &&
	mkdir rsynced &&
253 254 255 256 257 258
	(cd rsynced &&
	 git init --bare &&
	 git fetch "rsync:$(pwd)/../.git" master:refs/heads/master &&
	 git gc --prune &&
	 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
	 git fsck --full)
259 260 261
'

test_expect_success 'push via rsync' '
262 263
	mkdir rsynced2 &&
	(cd rsynced2 &&
264
	 git init) &&
265 266 267 268 269 270
	(cd rsynced &&
	 git push "rsync:$(pwd)/../rsynced2/.git" master) &&
	(cd rsynced2 &&
	 git gc --prune &&
	 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
	 git fsck --full)
271 272 273 274 275 276
'

test_expect_success 'push via rsync' '
	mkdir rsynced3 &&
	(cd rsynced3 &&
	 git init) &&
277 278 279 280
	git push --all "rsync:$(pwd)/rsynced3/.git" &&
	(cd rsynced3 &&
	 test $(git rev-parse master) = $(cd .. && git rev-parse master) &&
	 git fsck --full)
281 282 283
'
}

284 285 286 287 288 289 290 291
test_expect_success 'fetch with a non-applying branch.<name>.merge' '
	git config branch.master.remote yeti &&
	git config branch.master.merge refs/heads/bigfoot &&
	git config remote.blub.url one &&
	git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
	git fetch blub
'

292 293 294 295
# URL supplied to fetch does not match the url of the configured branch's remote
test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
	one_head=$(cd one && git rev-parse HEAD) &&
	this_head=$(git rev-parse HEAD) &&
296 297
	git update-ref -d FETCH_HEAD &&
	git fetch one &&
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
	test $this_head = "$(git rev-parse --verify HEAD)"
'

# URL supplied to fetch matches the url of the configured branch's remote and
# the merge spec matches the branch the remote HEAD points to
test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
	one_ref=$(cd one && git symbolic-ref HEAD) &&
	git config branch.master.remote blub &&
	git config branch.master.merge "$one_ref" &&
	git update-ref -d FETCH_HEAD &&
	git fetch one &&
	test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
	test $this_head = "$(git rev-parse --verify HEAD)"
'

# URL supplied to fetch matches the url of the configured branch's remote, but
# the merge spec does not match the branch the remote HEAD points to
test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
	git config branch.master.merge "${one_ref}_not" &&
	git update-ref -d FETCH_HEAD &&
	git fetch one &&
	test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
	test $this_head = "$(git rev-parse --verify HEAD)"
322 323
'

324 325
# the strange name is: a\!'b
test_expect_success 'quoting of a strangely named repo' '
326
	test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
327 328 329 330
	cat result &&
	grep "fatal: '\''a\\\\!'\''b'\''" result
'

331 332 333 334 335 336 337 338 339
test_expect_success 'bundle should record HEAD correctly' '

	cd "$D" &&
	git bundle create bundle5 HEAD master &&
	git bundle list-heads bundle5 >actual &&
	for h in HEAD refs/heads/master
	do
		echo "$(git rev-parse --verify $h) $h"
	done >expect &&
340
	test_cmp expect actual
341 342 343

'

344 345 346 347 348 349 350 351 352 353
test_expect_success 'explicit fetch should not update tracking' '

	cd "$D" &&
	git branch -f side &&
	(
		cd three &&
		o=$(git rev-parse --verify refs/remotes/origin/master) &&
		git fetch origin master &&
		n=$(git rev-parse --verify refs/remotes/origin/master) &&
		test "$o" = "$n" &&
354
		test_must_fail git rev-parse --verify refs/remotes/origin/side
355 356 357 358 359 360 361 362 363 364 365 366 367
	)
'

test_expect_success 'explicit pull should not update tracking' '

	cd "$D" &&
	git branch -f side &&
	(
		cd three &&
		o=$(git rev-parse --verify refs/remotes/origin/master) &&
		git pull origin master &&
		n=$(git rev-parse --verify refs/remotes/origin/master) &&
		test "$o" = "$n" &&
368
		test_must_fail git rev-parse --verify refs/remotes/origin/side
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	)
'

test_expect_success 'configured fetch updates tracking' '

	cd "$D" &&
	git branch -f side &&
	(
		cd three &&
		o=$(git rev-parse --verify refs/remotes/origin/master) &&
		git fetch origin &&
		n=$(git rev-parse --verify refs/remotes/origin/master) &&
		test "$o" != "$n" &&
		git rev-parse --verify refs/remotes/origin/side
	)
'

386 387 388 389 390 391 392
test_expect_success 'pushing nonexistent branch by mistake should not segv' '

	cd "$D" &&
	test_must_fail git push seven no:no

'

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
test_expect_success 'auto tag following fetches minimum' '

	cd "$D" &&
	git clone .git follow &&
	git checkout HEAD^0 &&
	(
		for i in 1 2 3 4 5 6 7
		do
			echo $i >>file &&
			git commit -m $i -a &&
			git tag -a -m $i excess-$i || exit 1
		done
	) &&
	git checkout master &&
	(
		cd follow &&
		git fetch
	)
'

413 414 415 416 417 418 419 420 421 422 423 424
test_expect_success 'refuse to fetch into the current branch' '

	test_must_fail git fetch . side:master

'

test_expect_success 'fetch into the current branch with --update-head-ok' '

	git fetch --update-head-ok . side:master

'

425 426 427 428 429 430 431
test_expect_success 'fetch --dry-run' '

	rm -f .git/FETCH_HEAD &&
	git fetch --dry-run . &&
	! test -f .git/FETCH_HEAD
'

432 433 434 435 436 437 438 439 440 441 442
test_expect_success "should be able to fetch with duplicate refspecs" '
        mkdir dups &&
        cd dups &&
        git init &&
        git config branch.master.remote three &&
        git config remote.three.url ../three/.git &&
        git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
        git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
        git fetch three
'

443
test_done