]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5516-fetch-push.sh
The sixth batch
[thirdparty/git.git] / t / t5516-fetch-push.sh
CommitLineData
bcdb34f7
JH
1#!/bin/sh
2
2ead7a67
RR
3test_description='Basic fetch/push functionality.
4
5This test checks the following functionality:
6
7* command-line syntax
8* refspecs
9* fast-forward detection, and overriding it
10* configuration
11* hooks
12* --porcelain output format
13* hiderefs
72549dfd 14* reflogs
2ead7a67 15'
bcdb34f7
JH
16
17. ./test-lib.sh
18
bf45242b 19D=$(pwd)
bcdb34f7
JH
20
21mk_empty () {
2e433b78
JK
22 repo_name="$1"
23 rm -fr "$repo_name" &&
24 mkdir "$repo_name" &&
bcdb34f7 25 (
2e433b78 26 cd "$repo_name" &&
586e4ce2 27 git init &&
acd2a45b 28 git config receive.denyCurrentBranch warn &&
586e4ce2 29 mv .git/hooks .git/hooks-disabled
bcdb34f7
JH
30 )
31}
32
6125796f 33mk_test () {
2e433b78
JK
34 repo_name="$1"
35 shift
36
37 mk_empty "$repo_name" &&
6125796f
JH
38 (
39 for ref in "$@"
40 do
2e433b78 41 git push "$repo_name" $the_first_commit:refs/$ref ||
5bd81c73 42 exit
6125796f 43 done &&
2e433b78 44 cd "$repo_name" &&
6125796f
JH
45 for ref in "$@"
46 do
848575d8
JN
47 echo "$the_first_commit" >expect &&
48 git show-ref -s --verify refs/$ref >actual &&
49 test_cmp expect actual ||
50 exit
6125796f
JH
51 done &&
52 git fsck --full
53 )
54}
55
160b81ed 56mk_test_with_hooks() {
2e433b78 57 repo_name=$1
160b81ed
PYH
58 mk_test "$@" &&
59 (
2e433b78 60 cd "$repo_name" &&
160b81ed
PYH
61 mkdir .git/hooks &&
62 cd .git/hooks &&
63
64 cat >pre-receive <<-'EOF' &&
65 #!/bin/sh
66 cat - >>pre-receive.actual
67 EOF
68
69 cat >update <<-'EOF' &&
70 #!/bin/sh
71 printf "%s %s %s\n" "$@" >>update.actual
72 EOF
73
74 cat >post-receive <<-'EOF' &&
75 #!/bin/sh
76 cat - >>post-receive.actual
77 EOF
78
79 cat >post-update <<-'EOF' &&
80 #!/bin/sh
81 for ref in "$@"
82 do
83 printf "%s\n" "$ref" >>post-update.actual
84 done
85 EOF
86
87 chmod +x pre-receive update post-receive post-update
88 )
89}
90
b2dc968e 91mk_child() {
2e433b78
JK
92 rm -rf "$2" &&
93 git clone "$1" "$2"
b2dc968e
JK
94}
95
6125796f 96check_push_result () {
cfb482b6 97 test $# -ge 3 ||
165293af 98 BUG "check_push_result requires at least 3 parameters"
cfb482b6 99
2e433b78
JK
100 repo_name="$1"
101 shift
102
6125796f 103 (
2e433b78 104 cd "$repo_name" &&
848575d8
JN
105 echo "$1" >expect &&
106 shift &&
6125796f
JH
107 for ref in "$@"
108 do
848575d8
JN
109 git show-ref -s --verify refs/$ref >actual &&
110 test_cmp expect actual ||
111 exit
6125796f
JH
112 done &&
113 git fsck --full
114 )
115}
116
bcdb34f7
JH
117test_expect_success setup '
118
d4785cd1 119 >path1 &&
bcdb34f7
JH
120 git add path1 &&
121 test_tick &&
122 git commit -a -m repo &&
6125796f
JH
123 the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
124
d4785cd1 125 >path2 &&
6125796f
JH
126 git add path2 &&
127 test_tick &&
128 git commit -a -m second &&
bcdb34f7
JH
129 the_commit=$(git show-ref -s --verify refs/heads/master)
130
131'
132
133test_expect_success 'fetch without wildcard' '
2e433b78 134 mk_empty testrepo &&
bcdb34f7
JH
135 (
136 cd testrepo &&
137 git fetch .. refs/heads/master:refs/remotes/origin/master &&
138
848575d8
JN
139 echo "$the_commit commit refs/remotes/origin/master" >expect &&
140 git for-each-ref refs/remotes/origin >actual &&
141 test_cmp expect actual
bcdb34f7
JH
142 )
143'
144
145test_expect_success 'fetch with wildcard' '
2e433b78 146 mk_empty testrepo &&
bcdb34f7
JH
147 (
148 cd testrepo &&
149 git config remote.up.url .. &&
150 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
151 git fetch up &&
152
848575d8
JN
153 echo "$the_commit commit refs/remotes/origin/master" >expect &&
154 git for-each-ref refs/remotes/origin >actual &&
155 test_cmp expect actual
bcdb34f7
JH
156 )
157'
158
55029ae4 159test_expect_success 'fetch with insteadOf' '
2e433b78 160 mk_empty testrepo &&
55029ae4 161 (
60e3aba9 162 TRASH=$(pwd)/ &&
55029ae4 163 cd testrepo &&
f69e836f 164 git config "url.$TRASH.insteadOf" trash/ &&
55029ae4
DB
165 git config remote.up.url trash/. &&
166 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
167 git fetch up &&
168
848575d8
JN
169 echo "$the_commit commit refs/remotes/origin/master" >expect &&
170 git for-each-ref refs/remotes/origin >actual &&
171 test_cmp expect actual
55029ae4
DB
172 )
173'
174
1c2eafb8 175test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
2e433b78 176 mk_empty testrepo &&
1c2eafb8
JT
177 (
178 TRASH=$(pwd)/ &&
179 cd testrepo &&
180 git config "url.trash/.pushInsteadOf" "$TRASH" &&
181 git config remote.up.url "$TRASH." &&
182 git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
183 git fetch up &&
184
848575d8
JN
185 echo "$the_commit commit refs/remotes/origin/master" >expect &&
186 git for-each-ref refs/remotes/origin >actual &&
187 test_cmp expect actual
1c2eafb8
JT
188 )
189'
190
bcdb34f7 191test_expect_success 'push without wildcard' '
2e433b78 192 mk_empty testrepo &&
bcdb34f7
JH
193
194 git push testrepo refs/heads/master:refs/remotes/origin/master &&
195 (
196 cd testrepo &&
848575d8
JN
197 echo "$the_commit commit refs/remotes/origin/master" >expect &&
198 git for-each-ref refs/remotes/origin >actual &&
199 test_cmp expect actual
bcdb34f7
JH
200 )
201'
202
203test_expect_success 'push with wildcard' '
2e433b78 204 mk_empty testrepo &&
bcdb34f7
JH
205
206 git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
207 (
208 cd testrepo &&
848575d8
JN
209 echo "$the_commit commit refs/remotes/origin/master" >expect &&
210 git for-each-ref refs/remotes/origin >actual &&
211 test_cmp expect actual
bcdb34f7
JH
212 )
213'
214
55029ae4 215test_expect_success 'push with insteadOf' '
2e433b78 216 mk_empty testrepo &&
f69e836f 217 TRASH="$(pwd)/" &&
3c695523 218 test_config "url.$TRASH.insteadOf" trash/ &&
55029ae4
DB
219 git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
220 (
221 cd testrepo &&
848575d8
JN
222 echo "$the_commit commit refs/remotes/origin/master" >expect &&
223 git for-each-ref refs/remotes/origin >actual &&
224 test_cmp expect actual
55029ae4
DB
225 )
226'
227
1c2eafb8 228test_expect_success 'push with pushInsteadOf' '
2e433b78 229 mk_empty testrepo &&
1c2eafb8 230 TRASH="$(pwd)/" &&
3c695523 231 test_config "url.$TRASH.pushInsteadOf" trash/ &&
1c2eafb8
JT
232 git push trash/testrepo refs/heads/master:refs/remotes/origin/master &&
233 (
234 cd testrepo &&
848575d8
JN
235 echo "$the_commit commit refs/remotes/origin/master" >expect &&
236 git for-each-ref refs/remotes/origin >actual &&
237 test_cmp expect actual
1c2eafb8
JT
238 )
239'
240
241test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
2e433b78 242 mk_empty testrepo &&
41ae34d1 243 test_config "url.trash2/.pushInsteadOf" testrepo/ &&
eb32c66e 244 test_config "url.trash3/.pushInsteadOf" trash/wrong &&
3c695523 245 test_config remote.r.url trash/wrong &&
41ae34d1 246 test_config remote.r.pushurl "testrepo/" &&
1c2eafb8
JT
247 git push r refs/heads/master:refs/remotes/origin/master &&
248 (
249 cd testrepo &&
848575d8
JN
250 echo "$the_commit commit refs/remotes/origin/master" >expect &&
251 git for-each-ref refs/remotes/origin >actual &&
252 test_cmp expect actual
1c2eafb8
JT
253 )
254'
255
6125796f
JH
256test_expect_success 'push with matching heads' '
257
2e433b78 258 mk_test testrepo heads/master &&
0a42ac03 259 git push testrepo : &&
2e433b78 260 check_push_result testrepo $the_commit heads/master
6125796f
JH
261
262'
263
a83619d6
PB
264test_expect_success 'push with matching heads on the command line' '
265
2e433b78 266 mk_test testrepo heads/master &&
a83619d6 267 git push testrepo : &&
2e433b78 268 check_push_result testrepo $the_commit heads/master
a83619d6
PB
269
270'
271
272test_expect_success 'failed (non-fast-forward) push with matching heads' '
273
2e433b78 274 mk_test testrepo heads/master &&
a83619d6
PB
275 git push testrepo : &&
276 git commit --amend -massaged &&
d492b31c 277 test_must_fail git push testrepo &&
2e433b78 278 check_push_result testrepo $the_commit heads/master &&
a83619d6
PB
279 git reset --hard $the_commit
280
281'
282
283test_expect_success 'push --force with matching heads' '
284
2e433b78 285 mk_test testrepo heads/master &&
a83619d6
PB
286 git push testrepo : &&
287 git commit --amend -massaged &&
0a42ac03 288 git push --force testrepo : &&
2e433b78 289 ! check_push_result testrepo $the_commit heads/master &&
a83619d6
PB
290 git reset --hard $the_commit
291
292'
293
294test_expect_success 'push with matching heads and forced update' '
295
2e433b78 296 mk_test testrepo heads/master &&
a83619d6
PB
297 git push testrepo : &&
298 git commit --amend -massaged &&
299 git push testrepo +: &&
2e433b78 300 ! check_push_result testrepo $the_commit heads/master &&
a83619d6
PB
301 git reset --hard $the_commit
302
303'
304
6125796f
JH
305test_expect_success 'push with no ambiguity (1)' '
306
2e433b78 307 mk_test testrepo heads/master &&
6125796f 308 git push testrepo master:master &&
2e433b78 309 check_push_result testrepo $the_commit heads/master
6125796f
JH
310
311'
312
313test_expect_success 'push with no ambiguity (2)' '
314
2e433b78 315 mk_test testrepo remotes/origin/master &&
ae36bdcf 316 git push testrepo master:origin/master &&
2e433b78 317 check_push_result testrepo $the_commit remotes/origin/master
6125796f
JH
318
319'
320
ae36bdcf
SP
321test_expect_success 'push with colon-less refspec, no ambiguity' '
322
2e433b78 323 mk_test testrepo heads/master heads/t/master &&
ae36bdcf
SP
324 git branch -f t/master master &&
325 git push testrepo master &&
2e433b78
JK
326 check_push_result testrepo $the_commit heads/master &&
327 check_push_result testrepo $the_first_commit heads/t/master
ae36bdcf
SP
328
329'
330
6125796f
JH
331test_expect_success 'push with weak ambiguity (1)' '
332
2e433b78 333 mk_test testrepo heads/master remotes/origin/master &&
6125796f 334 git push testrepo master:master &&
2e433b78
JK
335 check_push_result testrepo $the_commit heads/master &&
336 check_push_result testrepo $the_first_commit remotes/origin/master
6125796f
JH
337
338'
339
340test_expect_success 'push with weak ambiguity (2)' '
341
2e433b78 342 mk_test testrepo heads/master remotes/origin/master remotes/another/master &&
6125796f 343 git push testrepo master:master &&
2e433b78
JK
344 check_push_result testrepo $the_commit heads/master &&
345 check_push_result testrepo $the_first_commit remotes/origin/master remotes/another/master
6125796f
JH
346
347'
348
3ef6a1fe 349test_expect_success 'push with ambiguity' '
6125796f 350
2e433b78 351 mk_test testrepo heads/frotz tags/frotz &&
5bd81c73 352 test_must_fail git push testrepo master:frotz &&
2e433b78 353 check_push_result testrepo $the_first_commit heads/frotz tags/frotz
1ed10b88
JH
354
355'
356
357test_expect_success 'push with colon-less refspec (1)' '
358
2e433b78 359 mk_test testrepo heads/frotz tags/frotz &&
1ed10b88
JH
360 git branch -f frotz master &&
361 git push testrepo frotz &&
2e433b78
JK
362 check_push_result testrepo $the_commit heads/frotz &&
363 check_push_result testrepo $the_first_commit tags/frotz
1ed10b88
JH
364
365'
366
367test_expect_success 'push with colon-less refspec (2)' '
368
2e433b78 369 mk_test testrepo heads/frotz tags/frotz &&
1ed10b88
JH
370 if git show-ref --verify -q refs/heads/frotz
371 then
372 git branch -D frotz
373 fi &&
374 git tag -f frotz &&
dbfeddb1 375 git push -f testrepo frotz &&
2e433b78
JK
376 check_push_result testrepo $the_commit tags/frotz &&
377 check_push_result testrepo $the_first_commit heads/frotz
1ed10b88
JH
378
379'
380
381test_expect_success 'push with colon-less refspec (3)' '
382
2e433b78 383 mk_test testrepo &&
1ed10b88
JH
384 if git show-ref --verify -q refs/tags/frotz
385 then
386 git tag -d frotz
387 fi &&
388 git branch -f frotz master &&
389 git push testrepo frotz &&
2e433b78 390 check_push_result testrepo $the_commit heads/frotz &&
9a3c6f7b 391 test 1 = $( cd testrepo && git show-ref | wc -l )
1ed10b88
JH
392'
393
394test_expect_success 'push with colon-less refspec (4)' '
395
2e433b78 396 mk_test testrepo &&
1ed10b88
JH
397 if git show-ref --verify -q refs/heads/frotz
398 then
399 git branch -D frotz
400 fi &&
401 git tag -f frotz &&
402 git push testrepo frotz &&
2e433b78 403 check_push_result testrepo $the_commit tags/frotz &&
9a3c6f7b 404 test 1 = $( cd testrepo && git show-ref | wc -l )
1ed10b88 405
6125796f
JH
406'
407
7be8b3ba 408test_expect_success 'push head with non-existent, incomplete dest' '
f8aae120 409
2e433b78 410 mk_test testrepo &&
f8aae120 411 git push testrepo master:branch &&
2e433b78 412 check_push_result testrepo $the_commit heads/branch
f8aae120
JK
413
414'
415
7be8b3ba 416test_expect_success 'push tag with non-existent, incomplete dest' '
f8aae120 417
2e433b78 418 mk_test testrepo &&
f8aae120
JK
419 git tag -f v1.0 &&
420 git push testrepo v1.0:tag &&
2e433b78 421 check_push_result testrepo $the_commit tags/tag
f8aae120
JK
422
423'
424
7be8b3ba 425test_expect_success 'push sha1 with non-existent, incomplete dest' '
f8aae120 426
2e433b78 427 mk_test testrepo &&
bf45242b 428 test_must_fail git push testrepo $(git rev-parse master):foo
f8aae120
JK
429
430'
431
7be8b3ba 432test_expect_success 'push ref expression with non-existent, incomplete dest' '
f8aae120 433
2e433b78 434 mk_test testrepo &&
f8aae120
JK
435 test_must_fail git push testrepo master^:branch
436
437'
438
47d996a2
SP
439test_expect_success 'push with HEAD' '
440
2e433b78 441 mk_test testrepo heads/master &&
47d996a2
SP
442 git checkout master &&
443 git push testrepo HEAD &&
2e433b78 444 check_push_result testrepo $the_commit heads/master
47d996a2
SP
445
446'
447
448test_expect_success 'push with HEAD nonexisting at remote' '
449
2e433b78 450 mk_test testrepo heads/master &&
47d996a2
SP
451 git checkout -b local master &&
452 git push testrepo HEAD &&
2e433b78 453 check_push_result testrepo $the_commit heads/local
47d996a2
SP
454'
455
9f0ea7e8
DB
456test_expect_success 'push with +HEAD' '
457
2e433b78 458 mk_test testrepo heads/master &&
9f0ea7e8
DB
459 git checkout master &&
460 git branch -D local &&
461 git checkout -b local &&
462 git push testrepo master local &&
2e433b78
JK
463 check_push_result testrepo $the_commit heads/master &&
464 check_push_result testrepo $the_commit heads/local &&
9f0ea7e8
DB
465
466 # Without force rewinding should fail
467 git reset --hard HEAD^ &&
d492b31c 468 test_must_fail git push testrepo HEAD &&
2e433b78 469 check_push_result testrepo $the_commit heads/local &&
9f0ea7e8
DB
470
471 # With force rewinding should succeed
472 git push testrepo +HEAD &&
2e433b78 473 check_push_result testrepo $the_first_commit heads/local
9f0ea7e8
DB
474
475'
476
7be8b3ba 477test_expect_success 'push HEAD with non-existent, incomplete dest' '
f8aae120 478
2e433b78 479 mk_test testrepo &&
f8aae120
JK
480 git checkout master &&
481 git push testrepo HEAD:branch &&
2e433b78 482 check_push_result testrepo $the_commit heads/branch
f8aae120
JK
483
484'
485
9f0ea7e8
DB
486test_expect_success 'push with config remote.*.push = HEAD' '
487
2e433b78 488 mk_test testrepo heads/local &&
9f0ea7e8
DB
489 git checkout master &&
490 git branch -f local $the_commit &&
491 (
492 cd testrepo &&
493 git checkout local &&
494 git reset --hard $the_first_commit
495 ) &&
3c695523
JN
496 test_config remote.there.url testrepo &&
497 test_config remote.there.push HEAD &&
498 test_config branch.master.remote there &&
9f0ea7e8 499 git push &&
2e433b78
JK
500 check_push_result testrepo $the_commit heads/master &&
501 check_push_result testrepo $the_first_commit heads/local
9f0ea7e8
DB
502'
503
224c2171
RR
504test_expect_success 'push with remote.pushdefault' '
505 mk_test up_repo heads/master &&
506 mk_test down_repo heads/master &&
507 test_config remote.up.url up_repo &&
508 test_config remote.down.url down_repo &&
509 test_config branch.master.remote up &&
510 test_config remote.pushdefault down &&
54a3c673 511 test_config push.default matching &&
224c2171
RR
512 git push &&
513 check_push_result up_repo $the_first_commit heads/master &&
514 check_push_result down_repo $the_commit heads/master
9f0ea7e8
DB
515'
516
e1ca4241
MG
517test_expect_success 'push with config remote.*.pushurl' '
518
2e433b78 519 mk_test testrepo heads/master &&
e1ca4241 520 git checkout master &&
3c695523
JN
521 test_config remote.there.url test2repo &&
522 test_config remote.there.pushurl testrepo &&
0a42ac03 523 git push there : &&
2e433b78 524 check_push_result testrepo $the_commit heads/master
e1ca4241
MG
525'
526
9f765ce6
RR
527test_expect_success 'push with config branch.*.pushremote' '
528 mk_test up_repo heads/master &&
529 mk_test side_repo heads/master &&
530 mk_test down_repo heads/master &&
531 test_config remote.up.url up_repo &&
532 test_config remote.pushdefault side_repo &&
533 test_config remote.down.url down_repo &&
534 test_config branch.master.remote up &&
535 test_config branch.master.pushremote down &&
54a3c673 536 test_config push.default matching &&
9f765ce6
RR
537 git push &&
538 check_push_result up_repo $the_first_commit heads/master &&
539 check_push_result side_repo $the_first_commit heads/master &&
540 check_push_result down_repo $the_commit heads/master
e1ca4241
MG
541'
542
98b406f3
JK
543test_expect_success 'branch.*.pushremote config order is irrelevant' '
544 mk_test one_repo heads/master &&
545 mk_test two_repo heads/master &&
546 test_config remote.one.url one_repo &&
547 test_config remote.two.url two_repo &&
548 test_config branch.master.pushremote two_repo &&
549 test_config remote.pushdefault one_repo &&
550 test_config push.default matching &&
551 git push &&
552 check_push_result one_repo $the_first_commit heads/master &&
553 check_push_result two_repo $the_commit heads/master
554'
555
11f2441f
BE
556test_expect_success 'push with dry-run' '
557
2e433b78 558 mk_test testrepo heads/master &&
cfb482b6 559 old_commit=$(git -C testrepo show-ref -s --verify refs/heads/master) &&
0a42ac03 560 git push --dry-run testrepo : &&
2e433b78 561 check_push_result testrepo $old_commit heads/master
11f2441f
BE
562'
563
28391a80
JS
564test_expect_success 'push updates local refs' '
565
2e433b78
JK
566 mk_test testrepo heads/master &&
567 mk_child testrepo child &&
d4785cd1
JS
568 (
569 cd child &&
b2dc968e 570 git pull .. master &&
28391a80 571 git push &&
d4785cd1
JS
572 test $(git rev-parse master) = \
573 $(git rev-parse remotes/origin/master)
574 )
28391a80
JS
575
576'
577
16ed2f48
CB
578test_expect_success 'push updates up-to-date local refs' '
579
2e433b78
JK
580 mk_test testrepo heads/master &&
581 mk_child testrepo child1 &&
582 mk_child testrepo child2 &&
b2dc968e 583 (cd child1 && git pull .. master && git push) &&
d4785cd1
JS
584 (
585 cd child2 &&
16ed2f48
CB
586 git pull ../child1 master &&
587 git push &&
d4785cd1
JS
588 test $(git rev-parse master) = \
589 $(git rev-parse remotes/origin/master)
590 )
16ed2f48
CB
591
592'
593
594test_expect_success 'push preserves up-to-date packed refs' '
595
2e433b78
JK
596 mk_test testrepo heads/master &&
597 mk_child testrepo child &&
d4785cd1
JS
598 (
599 cd child &&
16ed2f48 600 git push &&
d4785cd1
JS
601 ! test -f .git/refs/remotes/origin/master
602 )
16ed2f48
CB
603
604'
605
28391a80
JS
606test_expect_success 'push does not update local refs on failure' '
607
2e433b78
JK
608 mk_test testrepo heads/master &&
609 mk_child testrepo child &&
b2dc968e 610 mkdir testrepo/.git/hooks &&
fc012c28 611 echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
b2dc968e 612 chmod +x testrepo/.git/hooks/pre-receive &&
d4785cd1
JS
613 (
614 cd child &&
f6b82970 615 git pull .. master &&
d492b31c 616 test_must_fail git push &&
28391a80 617 test $(git rev-parse master) != \
d4785cd1
JS
618 $(git rev-parse remotes/origin/master)
619 )
28391a80
JS
620
621'
622
623test_expect_success 'allow deleting an invalid remote ref' '
624
2e433b78 625 mk_test testrepo heads/master &&
28391a80
JS
626 rm -f testrepo/.git/objects/??/* &&
627 git push testrepo :refs/heads/master &&
d492b31c 628 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
28391a80
JS
629
630'
631
160b81ed 632test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
2e433b78 633 mk_test_with_hooks testrepo heads/master heads/next &&
160b81ed
PYH
634 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
635 newmaster=$(git show-ref -s --verify refs/heads/master) &&
636 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
8125a58b 637 newnext=$ZERO_OID &&
160b81ed
PYH
638 git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
639 (
640 cd testrepo/.git &&
641 cat >pre-receive.expect <<-EOF &&
642 $orgmaster $newmaster refs/heads/master
643 $orgnext $newnext refs/heads/next
644 EOF
645
646 cat >update.expect <<-EOF &&
647 refs/heads/master $orgmaster $newmaster
648 refs/heads/next $orgnext $newnext
649 EOF
650
651 cat >post-receive.expect <<-EOF &&
652 $orgmaster $newmaster refs/heads/master
653 $orgnext $newnext refs/heads/next
654 EOF
655
656 cat >post-update.expect <<-EOF &&
657 refs/heads/master
658 refs/heads/next
659 EOF
660
661 test_cmp pre-receive.expect pre-receive.actual &&
662 test_cmp update.expect update.actual &&
663 test_cmp post-receive.expect post-receive.actual &&
664 test_cmp post-update.expect post-update.actual
665 )
666'
667
668test_expect_success 'deleting dangling ref triggers hooks with correct args' '
2e433b78 669 mk_test_with_hooks testrepo heads/master &&
160b81ed
PYH
670 rm -f testrepo/.git/objects/??/* &&
671 git push testrepo :refs/heads/master &&
672 (
673 cd testrepo/.git &&
674 cat >pre-receive.expect <<-EOF &&
8125a58b 675 $ZERO_OID $ZERO_OID refs/heads/master
160b81ed
PYH
676 EOF
677
678 cat >update.expect <<-EOF &&
8125a58b 679 refs/heads/master $ZERO_OID $ZERO_OID
160b81ed
PYH
680 EOF
681
682 cat >post-receive.expect <<-EOF &&
8125a58b 683 $ZERO_OID $ZERO_OID refs/heads/master
160b81ed
PYH
684 EOF
685
686 cat >post-update.expect <<-EOF &&
687 refs/heads/master
688 EOF
689
690 test_cmp pre-receive.expect pre-receive.actual &&
691 test_cmp update.expect update.actual &&
692 test_cmp post-receive.expect post-receive.actual &&
693 test_cmp post-update.expect post-update.actual
694 )
695'
696
697test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
2e433b78 698 mk_test_with_hooks testrepo heads/master &&
160b81ed
PYH
699 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
700 newmaster=$(git show-ref -s --verify refs/heads/master) &&
701 git push testrepo master :refs/heads/nonexistent &&
702 (
703 cd testrepo/.git &&
704 cat >pre-receive.expect <<-EOF &&
705 $orgmaster $newmaster refs/heads/master
8125a58b 706 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
707 EOF
708
709 cat >update.expect <<-EOF &&
710 refs/heads/master $orgmaster $newmaster
8125a58b 711 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
712 EOF
713
714 cat >post-receive.expect <<-EOF &&
715 $orgmaster $newmaster refs/heads/master
716 EOF
717
718 cat >post-update.expect <<-EOF &&
719 refs/heads/master
720 EOF
721
722 test_cmp pre-receive.expect pre-receive.actual &&
723 test_cmp update.expect update.actual &&
724 test_cmp post-receive.expect post-receive.actual &&
725 test_cmp post-update.expect post-update.actual
726 )
727'
728
729test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
2e433b78 730 mk_test_with_hooks testrepo heads/master &&
160b81ed
PYH
731 git push testrepo :refs/heads/nonexistent &&
732 (
733 cd testrepo/.git &&
734 cat >pre-receive.expect <<-EOF &&
8125a58b 735 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
736 EOF
737
738 cat >update.expect <<-EOF &&
8125a58b 739 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
740 EOF
741
742 test_cmp pre-receive.expect pre-receive.actual &&
743 test_cmp update.expect update.actual &&
744 test_path_is_missing post-receive.actual &&
745 test_path_is_missing post-update.actual
746 )
747'
748
749test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
2e433b78 750 mk_test_with_hooks testrepo heads/master heads/next heads/pu &&
160b81ed
PYH
751 orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
752 newmaster=$(git show-ref -s --verify refs/heads/master) &&
753 orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
8125a58b 754 newnext=$ZERO_OID &&
160b81ed
PYH
755 orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
756 newpu=$(git show-ref -s --verify refs/heads/master) &&
757 git push testrepo refs/heads/master:refs/heads/master \
758 refs/heads/master:refs/heads/pu :refs/heads/next \
759 :refs/heads/nonexistent &&
760 (
761 cd testrepo/.git &&
762 cat >pre-receive.expect <<-EOF &&
763 $orgmaster $newmaster refs/heads/master
764 $orgnext $newnext refs/heads/next
765 $orgpu $newpu refs/heads/pu
8125a58b 766 $ZERO_OID $ZERO_OID refs/heads/nonexistent
160b81ed
PYH
767 EOF
768
769 cat >update.expect <<-EOF &&
770 refs/heads/master $orgmaster $newmaster
771 refs/heads/next $orgnext $newnext
772 refs/heads/pu $orgpu $newpu
8125a58b 773 refs/heads/nonexistent $ZERO_OID $ZERO_OID
160b81ed
PYH
774 EOF
775
776 cat >post-receive.expect <<-EOF &&
777 $orgmaster $newmaster refs/heads/master
778 $orgnext $newnext refs/heads/next
779 $orgpu $newpu refs/heads/pu
780 EOF
781
782 cat >post-update.expect <<-EOF &&
783 refs/heads/master
784 refs/heads/next
785 refs/heads/pu
786 EOF
787
788 test_cmp pre-receive.expect pre-receive.actual &&
789 test_cmp update.expect update.actual &&
790 test_cmp post-receive.expect post-receive.actual &&
791 test_cmp post-update.expect post-update.actual
792 )
793'
794
f517f1f2 795test_expect_success 'allow deleting a ref using --delete' '
2e433b78 796 mk_test testrepo heads/master &&
f517f1f2
JK
797 (cd testrepo && git config receive.denyDeleteCurrent warn) &&
798 git push testrepo --delete master &&
799 (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
800'
801
802test_expect_success 'allow deleting a tag using --delete' '
2e433b78 803 mk_test testrepo heads/master &&
f517f1f2
JK
804 git tag -a -m dummy_message deltag heads/master &&
805 git push testrepo --tags &&
806 (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
807 git push testrepo --delete tag deltag &&
808 (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
809'
810
811test_expect_success 'push --delete without args aborts' '
2e433b78 812 mk_test testrepo heads/master &&
f517f1f2
JK
813 test_must_fail git push testrepo --delete
814'
815
816test_expect_success 'push --delete refuses src:dest refspecs' '
2e433b78 817 mk_test testrepo heads/master &&
f517f1f2
JK
818 test_must_fail git push testrepo --delete master:foo
819'
820
986e8239 821test_expect_success 'warn on push to HEAD of non-bare repository' '
2e433b78 822 mk_test testrepo heads/master &&
d4785cd1
JS
823 (
824 cd testrepo &&
986e8239 825 git checkout master &&
d4785cd1
JS
826 git config receive.denyCurrentBranch warn
827 ) &&
986e8239 828 git push testrepo master 2>stderr &&
3d95d92b 829 grep "warning: updating the current branch" stderr
986e8239
JK
830'
831
832test_expect_success 'deny push to HEAD of non-bare repository' '
2e433b78 833 mk_test testrepo heads/master &&
d4785cd1
JS
834 (
835 cd testrepo &&
986e8239 836 git checkout master &&
d4785cd1
JS
837 git config receive.denyCurrentBranch true
838 ) &&
986e8239
JK
839 test_must_fail git push testrepo master
840'
841
842test_expect_success 'allow push to HEAD of bare repository (bare)' '
2e433b78 843 mk_test testrepo heads/master &&
d4785cd1
JS
844 (
845 cd testrepo &&
986e8239
JK
846 git checkout master &&
847 git config receive.denyCurrentBranch true &&
d4785cd1
JS
848 git config core.bare true
849 ) &&
986e8239 850 git push testrepo master 2>stderr &&
3d95d92b 851 ! grep "warning: updating the current branch" stderr
986e8239
JK
852'
853
854test_expect_success 'allow push to HEAD of non-bare repository (config)' '
2e433b78 855 mk_test testrepo heads/master &&
d4785cd1
JS
856 (
857 cd testrepo &&
986e8239
JK
858 git checkout master &&
859 git config receive.denyCurrentBranch false
860 ) &&
861 git push testrepo master 2>stderr &&
3d95d92b 862 ! grep "warning: updating the current branch" stderr
986e8239
JK
863'
864
18afe101 865test_expect_success 'fetch with branches' '
2e433b78 866 mk_empty testrepo &&
18afe101
MK
867 git branch second $the_first_commit &&
868 git checkout second &&
869 echo ".." > testrepo/.git/branches/branch1 &&
d4785cd1
JS
870 (
871 cd testrepo &&
18afe101 872 git fetch branch1 &&
848575d8
JN
873 echo "$the_commit commit refs/heads/branch1" >expect &&
874 git for-each-ref refs/heads >actual &&
875 test_cmp expect actual
18afe101
MK
876 ) &&
877 git checkout master
878'
879
880test_expect_success 'fetch with branches containing #' '
2e433b78 881 mk_empty testrepo &&
18afe101 882 echo "..#second" > testrepo/.git/branches/branch2 &&
d4785cd1
JS
883 (
884 cd testrepo &&
18afe101 885 git fetch branch2 &&
848575d8
JN
886 echo "$the_first_commit commit refs/heads/branch2" >expect &&
887 git for-each-ref refs/heads >actual &&
888 test_cmp expect actual
18afe101
MK
889 ) &&
890 git checkout master
891'
892
893test_expect_success 'push with branches' '
2e433b78 894 mk_empty testrepo &&
18afe101
MK
895 git checkout second &&
896 echo "testrepo" > .git/branches/branch1 &&
897 git push branch1 &&
d4785cd1
JS
898 (
899 cd testrepo &&
848575d8
JN
900 echo "$the_first_commit commit refs/heads/master" >expect &&
901 git for-each-ref refs/heads >actual &&
902 test_cmp expect actual
18afe101
MK
903 )
904'
905
906test_expect_success 'push with branches containing #' '
2e433b78 907 mk_empty testrepo &&
18afe101
MK
908 echo "testrepo#branch3" > .git/branches/branch2 &&
909 git push branch2 &&
d4785cd1
JS
910 (
911 cd testrepo &&
848575d8
JN
912 echo "$the_first_commit commit refs/heads/branch3" >expect &&
913 git for-each-ref refs/heads >actual &&
914 test_cmp expect actual
18afe101
MK
915 ) &&
916 git checkout master
917'
918
da3efdb1 919test_expect_success 'push into aliased refs (consistent)' '
2e433b78
JK
920 mk_test testrepo heads/master &&
921 mk_child testrepo child1 &&
922 mk_child testrepo child2 &&
da3efdb1
JS
923 (
924 cd child1 &&
925 git branch foo &&
51b85471 926 git symbolic-ref refs/heads/bar refs/heads/foo &&
da3efdb1
JS
927 git config receive.denyCurrentBranch false
928 ) &&
929 (
930 cd child2 &&
931 >path2 &&
932 git add path2 &&
933 test_tick &&
934 git commit -a -m child2 &&
935 git branch foo &&
936 git branch bar &&
937 git push ../child1 foo bar
938 )
939'
940
941test_expect_success 'push into aliased refs (inconsistent)' '
2e433b78
JK
942 mk_test testrepo heads/master &&
943 mk_child testrepo child1 &&
944 mk_child testrepo child2 &&
da3efdb1
JS
945 (
946 cd child1 &&
947 git branch foo &&
51b85471 948 git symbolic-ref refs/heads/bar refs/heads/foo &&
da3efdb1
JS
949 git config receive.denyCurrentBranch false
950 ) &&
951 (
952 cd child2 &&
953 >path2 &&
954 git add path2 &&
955 test_tick &&
956 git commit -a -m child2 &&
957 git branch foo &&
958 >path3 &&
959 git add path3 &&
960 test_tick &&
961 git commit -a -m child2 &&
962 git branch bar &&
963 test_must_fail git push ../child1 foo bar 2>stderr &&
964 grep "refusing inconsistent update" stderr
965 )
966'
967
380efb65
ÆAB
968test_force_push_tag () {
969 tag_type_description=$1
970 tag_args=$2
971
f08fb8df 972 test_expect_success "force pushing required to update $tag_type_description" "
380efb65
ÆAB
973 mk_test testrepo heads/master &&
974 mk_child testrepo child1 &&
975 mk_child testrepo child2 &&
976 (
977 cd child1 &&
978 git tag testTag &&
979 git push ../child2 testTag &&
980 >file1 &&
981 git add file1 &&
982 git commit -m 'file1' &&
983 git tag $tag_args testTag &&
984 test_must_fail git push ../child2 testTag &&
985 git push --force ../child2 testTag &&
986 git tag $tag_args testTag HEAD~ &&
987 test_must_fail git push ../child2 testTag &&
988 git push --force ../child2 testTag &&
989
990 # Clobbering without + in refspec needs --force
991 git tag -f testTag &&
992 test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
993 git push --force ../child2 'refs/tags/*:refs/tags/*' &&
994
995 # Clobbering with + in refspec does not need --force
996 git tag -f testTag HEAD~ &&
997 git push ../child2 '+refs/tags/*:refs/tags/*' &&
998
999 # Clobbering with --no-force still obeys + in refspec
1000 git tag -f testTag &&
1001 git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
1002
1003 # Clobbering with/without --force and 'tag <name>' format
1004 git tag -f testTag HEAD~ &&
1005 test_must_fail git push ../child2 tag testTag &&
1006 git push --force ../child2 tag testTag
1007 )
1008 "
1009}
1010
1011test_force_push_tag "lightweight tag" "-f"
253b3d4f 1012test_force_push_tag "annotated tag" "-f -a -m'tag message'"
dbfeddb1 1013
6b0b0677
ÆAB
1014test_force_fetch_tag () {
1015 tag_type_description=$1
1016 tag_args=$2
1017
0bc8d71b 1018 test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
6b0b0677
ÆAB
1019 mk_test testrepo heads/master &&
1020 mk_child testrepo child1 &&
1021 mk_child testrepo child2 &&
1022 (
1023 cd testrepo &&
1024 git tag testTag &&
1025 git -C ../child1 fetch origin tag testTag &&
1026 >file1 &&
1027 git add file1 &&
1028 git commit -m 'file1' &&
1029 git tag $tag_args testTag &&
0bc8d71b
ÆAB
1030 test_must_fail git -C ../child1 fetch origin tag testTag &&
1031 git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
6b0b0677
ÆAB
1032 )
1033 "
1034}
1035
1036test_force_fetch_tag "lightweight tag" "-f"
1037test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
dbfeddb1 1038
fbe4f447 1039test_expect_success 'push --porcelain' '
2e433b78 1040 mk_empty testrepo &&
fbe4f447
LA
1041 echo >.git/foo "To testrepo" &&
1042 echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" &&
1043 echo >>.git/foo "Done" &&
1044 git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master &&
1045 (
1046 cd testrepo &&
848575d8
JN
1047 echo "$the_commit commit refs/remotes/origin/master" >expect &&
1048 git for-each-ref refs/remotes/origin >actual &&
1049 test_cmp expect actual
fbe4f447 1050 ) &&
c296134d 1051 test_cmp .git/foo .git/bar
fbe4f447
LA
1052'
1053
1054test_expect_success 'push --porcelain bad url' '
2e433b78 1055 mk_empty testrepo &&
fbe4f447 1056 test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
c7cf9566 1057 ! grep -q Done .git/bar
fbe4f447
LA
1058'
1059
1060test_expect_success 'push --porcelain rejected' '
2e433b78 1061 mk_empty testrepo &&
fbe4f447
LA
1062 git push testrepo refs/heads/master:refs/remotes/origin/master &&
1063 (cd testrepo &&
51b85471 1064 git reset --hard origin/master^ &&
fbe4f447
LA
1065 git config receive.denyCurrentBranch true) &&
1066
1067 echo >.git/foo "To testrepo" &&
1068 echo >>.git/foo "! refs/heads/master:refs/heads/master [remote rejected] (branch is currently checked out)" &&
7dcbeaa0 1069 echo >>.git/foo "Done" &&
fbe4f447
LA
1070
1071 test_must_fail git push >.git/bar --porcelain testrepo refs/heads/master:refs/heads/master &&
c296134d 1072 test_cmp .git/foo .git/bar
fbe4f447
LA
1073'
1074
1075test_expect_success 'push --porcelain --dry-run rejected' '
2e433b78 1076 mk_empty testrepo &&
fbe4f447
LA
1077 git push testrepo refs/heads/master:refs/remotes/origin/master &&
1078 (cd testrepo &&
51b85471 1079 git reset --hard origin/master &&
fbe4f447
LA
1080 git config receive.denyCurrentBranch true) &&
1081
1082 echo >.git/foo "To testrepo" &&
1083 echo >>.git/foo "! refs/heads/master^:refs/heads/master [rejected] (non-fast-forward)" &&
1084 echo >>.git/foo "Done" &&
1085
1086 test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/master^:refs/heads/master &&
c296134d 1087 test_cmp .git/foo .git/bar
fbe4f447
LA
1088'
1089
6ddba5e2 1090test_expect_success 'push --prune' '
2e433b78 1091 mk_test testrepo heads/master heads/second heads/foo heads/bar &&
0a42ac03 1092 git push --prune testrepo : &&
2e433b78
JK
1093 check_push_result testrepo $the_commit heads/master &&
1094 check_push_result testrepo $the_first_commit heads/second &&
1095 ! check_push_result testrepo $the_first_commit heads/foo heads/bar
6ddba5e2
FC
1096'
1097
1098test_expect_success 'push --prune refspec' '
2e433b78 1099 mk_test testrepo tmp/master tmp/second tmp/foo tmp/bar &&
6ddba5e2 1100 git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
2e433b78
JK
1101 check_push_result testrepo $the_commit tmp/master &&
1102 check_push_result testrepo $the_first_commit tmp/second &&
1103 ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
6ddba5e2
FC
1104'
1105
daebaa78
JH
1106for configsection in transfer receive
1107do
1108 test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
2e433b78 1109 mk_test testrepo heads/master hidden/one hidden/two hidden/three &&
daebaa78
JH
1110 (
1111 cd testrepo &&
1112 git config $configsection.hiderefs refs/hidden
1113 ) &&
1114
1115 # push to unhidden ref succeeds normally
1116 git push testrepo master:refs/heads/master &&
2e433b78 1117 check_push_result testrepo $the_commit heads/master &&
daebaa78
JH
1118
1119 # push to update a hidden ref should fail
1120 test_must_fail git push testrepo master:refs/hidden/one &&
2e433b78 1121 check_push_result testrepo $the_first_commit hidden/one &&
daebaa78
JH
1122
1123 # push to delete a hidden ref should fail
1124 test_must_fail git push testrepo :refs/hidden/two &&
2e433b78 1125 check_push_result testrepo $the_first_commit hidden/two &&
daebaa78
JH
1126
1127 # idempotent push to update a hidden ref should fail
1128 test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
2e433b78 1129 check_push_result testrepo $the_first_commit hidden/three
daebaa78
JH
1130 '
1131done
1132
6e7b66ee 1133test_expect_success 'fetch exact SHA1' '
2e433b78 1134 mk_test testrepo heads/master hidden/one &&
6e7b66ee
JH
1135 git push testrepo master:refs/hidden/one &&
1136 (
1137 cd testrepo &&
1138 git config transfer.hiderefs refs/hidden
1139 ) &&
2e433b78 1140 check_push_result testrepo $the_commit hidden/one &&
6e7b66ee 1141
2e433b78 1142 mk_child testrepo child &&
6e7b66ee
JH
1143 (
1144 cd child &&
1145
1146 # make sure $the_commit does not exist here
1147 git repack -a -d &&
1148 git prune &&
1149 test_must_fail git cat-file -t $the_commit &&
1150
ab0c5f50
JT
1151 # Some protocol versions (e.g. 2) support fetching
1152 # unadvertised objects, so restrict this test to v0.
1153
6e7b66ee 1154 # fetching the hidden object should fail by default
8a1b0978 1155 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1156 git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
d56583de 1157 test_i18ngrep "Server does not allow request for unadvertised object" err &&
6e7b66ee
JH
1158 test_must_fail git rev-parse --verify refs/heads/copy &&
1159
1160 # the server side can allow it to succeed
1161 (
1162 cd ../testrepo &&
1163 git config uploadpack.allowtipsha1inwant true
1164 ) &&
1165
c3c17bf1
JK
1166 git fetch -v ../testrepo $the_commit:refs/heads/copy master:refs/heads/extra &&
1167 cat >expect <<-EOF &&
1168 $the_commit
1169 $the_first_commit
1170 EOF
1171 {
1172 git rev-parse --verify refs/heads/copy &&
1173 git rev-parse --verify refs/heads/extra
1174 } >actual &&
1175 test_cmp expect actual
6e7b66ee
JH
1176 )
1177'
1178
6c301adb
JN
1179test_expect_success 'fetch exact SHA1 in protocol v2' '
1180 mk_test testrepo heads/master hidden/one &&
1181 git push testrepo master:refs/hidden/one &&
1182 git -C testrepo config transfer.hiderefs refs/hidden &&
1183 check_push_result testrepo $the_commit hidden/one &&
1184
1185 mk_child testrepo child &&
1186 git -C child config protocol.version 2 &&
1187
1188 # make sure $the_commit does not exist here
1189 git -C child repack -a -d &&
1190 git -C child prune &&
1191 test_must_fail git -C child cat-file -t $the_commit &&
1192
1193 # fetching the hidden object succeeds by default
1194 # NEEDSWORK: should this match the v0 behavior instead?
1195 git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
1196'
1197
68ee6289
FM
1198for configallowtipsha1inwant in true false
1199do
1200 test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
1201 mk_empty testrepo &&
1202 (
1203 cd testrepo &&
1204 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1205 git commit --allow-empty -m foo &&
1206 git commit --allow-empty -m bar
1207 ) &&
1208 SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1209 mk_empty shallow &&
1210 (
1211 cd shallow &&
ab0c5f50
JT
1212 # Some protocol versions (e.g. 2) support fetching
1213 # unadvertised objects, so restrict this test to v0.
8a1b0978 1214 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1215 git fetch --depth=1 ../testrepo/.git $SHA1 &&
68ee6289
FM
1216 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1217 git fetch --depth=1 ../testrepo/.git $SHA1 &&
1218 git cat-file commit $SHA1
1219 )
1220 '
1221
1222 test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
1223 mk_empty testrepo &&
1224 (
1225 cd testrepo &&
1226 git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
1227 git commit --allow-empty -m foo &&
1228 git commit --allow-empty -m bar &&
1229 git commit --allow-empty -m xyz
1230 ) &&
1231 SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
1232 SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
1233 SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
1234 (
1235 cd testrepo &&
1236 git reset --hard $SHA1_2 &&
1237 git cat-file commit $SHA1_1 &&
1238 git cat-file commit $SHA1_3
1239 ) &&
1240 mk_empty shallow &&
1241 (
1242 cd shallow &&
ab0c5f50
JT
1243 # Some protocol versions (e.g. 2) support fetching
1244 # unadvertised objects, so restrict this test to v0.
8a1b0978 1245 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1246 git fetch ../testrepo/.git $SHA1_3 &&
8a1b0978 1247 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1248 git fetch ../testrepo/.git $SHA1_1 &&
68ee6289
FM
1249 git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
1250 git fetch ../testrepo/.git $SHA1_1 &&
1251 git cat-file commit $SHA1_1 &&
1252 test_must_fail git cat-file commit $SHA1_2 &&
1253 git fetch ../testrepo/.git $SHA1_2 &&
1254 git cat-file commit $SHA1_2 &&
8a1b0978 1255 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
57a6b932 1256 git fetch ../testrepo/.git $SHA1_3 2>err &&
533ddba4 1257 test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
68ee6289
FM
1258 )
1259 '
1260done
1261
c2aba155 1262test_expect_success 'fetch follows tags by default' '
2e433b78 1263 mk_test testrepo heads/master &&
c2aba155
JH
1264 rm -fr src dst &&
1265 git init src &&
1266 (
1267 cd src &&
1268 git pull ../testrepo master &&
1269 git tag -m "annotated" tag &&
1270 git for-each-ref >tmp1 &&
1271 (
1272 cat tmp1
1273 sed -n "s|refs/heads/master$|refs/remotes/origin/master|p" tmp1
1274 ) |
1275 sort -k 3 >../expect
1276 ) &&
1277 git init dst &&
1278 (
1279 cd dst &&
1280 git remote add origin ../src &&
1281 git config branch.master.remote origin &&
1282 git config branch.master.merge refs/heads/master &&
1283 git pull &&
1284 git for-each-ref >../actual
1285 ) &&
1286 test_cmp expect actual
1287'
1288
34066f06
JK
1289test_expect_success 'peeled advertisements are not considered ref tips' '
1290 mk_empty testrepo &&
1291 git -C testrepo commit --allow-empty -m one &&
1292 git -C testrepo commit --allow-empty -m two &&
1293 git -C testrepo tag -m foo mytag HEAD^ &&
1294 oid=$(git -C testrepo rev-parse mytag^{commit}) &&
8a1b0978 1295 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
34066f06
JK
1296 git fetch testrepo $oid 2>err &&
1297 test_i18ngrep "Server does not allow request for unadvertised object" err
1298'
1299
ca02465b
JH
1300test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
1301 mk_test testrepo heads/master &&
1302 rm -fr src dst &&
1303 git init src &&
1304 git init --bare dst &&
1305 (
1306 cd src &&
1307 git pull ../testrepo master &&
1308 git branch next &&
1309 git config remote.dst.url ../dst &&
1310 git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
1311 git push dst master &&
1312 git show-ref refs/heads/master |
1313 sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
1314 ) &&
1315 (
1316 cd dst &&
1317 test_must_fail git show-ref refs/heads/next &&
1318 test_must_fail git show-ref refs/heads/master &&
1319 git show-ref refs/remotes/src/master >actual
1320 ) &&
1321 test_cmp dst/expect dst/actual
1322'
1323
1324test_expect_success 'with no remote.$name.push, it is not used as refmap' '
1325 mk_test testrepo heads/master &&
1326 rm -fr src dst &&
1327 git init src &&
1328 git init --bare dst &&
1329 (
1330 cd src &&
1331 git pull ../testrepo master &&
1332 git branch next &&
1333 git config remote.dst.url ../dst &&
fc9261ca 1334 git config push.default matching &&
ca02465b
JH
1335 git push dst master &&
1336 git show-ref refs/heads/master >../dst/expect
1337 ) &&
1338 (
1339 cd dst &&
1340 test_must_fail git show-ref refs/heads/next &&
1341 git show-ref refs/heads/master >actual
1342 ) &&
1343 test_cmp dst/expect dst/actual
1344'
1345
fc9261ca
JH
1346test_expect_success 'with no remote.$name.push, upstream mapping is used' '
1347 mk_test testrepo heads/master &&
1348 rm -fr src dst &&
1349 git init src &&
1350 git init --bare dst &&
1351 (
1352 cd src &&
1353 git pull ../testrepo master &&
1354 git branch next &&
1355 git config remote.dst.url ../dst &&
1356 git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
1357 git config push.default upstream &&
1358
1359 git config branch.master.merge refs/heads/trunk &&
1360 git config branch.master.remote dst &&
1361
1362 git push dst master &&
1363 git show-ref refs/heads/master |
1364 sed -e "s|refs/heads/master|refs/heads/trunk|" >../dst/expect
1365 ) &&
1366 (
1367 cd dst &&
1368 test_must_fail git show-ref refs/heads/master &&
1369 test_must_fail git show-ref refs/heads/next &&
1370 git show-ref refs/heads/trunk >actual
1371 ) &&
1372 test_cmp dst/expect dst/actual
1373'
1374
c2aba155 1375test_expect_success 'push does not follow tags by default' '
2e433b78 1376 mk_test testrepo heads/master &&
c2aba155
JH
1377 rm -fr src dst &&
1378 git init src &&
1379 git init --bare dst &&
1380 (
1381 cd src &&
1382 git pull ../testrepo master &&
1383 git tag -m "annotated" tag &&
1384 git checkout -b another &&
1385 git commit --allow-empty -m "future commit" &&
1386 git tag -m "future" future &&
1387 git checkout master &&
1388 git for-each-ref refs/heads/master >../expect &&
1389 git push ../dst master
1390 ) &&
1391 (
1392 cd dst &&
1393 git for-each-ref >../actual
1394 ) &&
1395 test_cmp expect actual
1396'
1397
f6188dcc 1398test_expect_success 'push --follow-tags only pushes relevant tags' '
2e433b78 1399 mk_test testrepo heads/master &&
c2aba155
JH
1400 rm -fr src dst &&
1401 git init src &&
1402 git init --bare dst &&
1403 (
1404 cd src &&
1405 git pull ../testrepo master &&
1406 git tag -m "annotated" tag &&
1407 git checkout -b another &&
1408 git commit --allow-empty -m "future commit" &&
1409 git tag -m "future" future &&
1410 git checkout master &&
51b85471 1411 git for-each-ref refs/heads/master refs/tags/tag >../expect &&
f6188dcc 1412 git push --follow-tags ../dst master
c2aba155
JH
1413 ) &&
1414 (
1415 cd dst &&
1416 git for-each-ref >../actual
1417 ) &&
1418 test_cmp expect actual
1419'
1420
f7c815c3
NTND
1421test_expect_success 'push --no-thin must produce non-thin pack' '
1422 cat >>path1 <<\EOF &&
1423keep base version of path1 big enough, compared to the new changes
1424later, in order to pass size heuristics in
1425builtin/pack-objects.c:try_delta()
1426EOF
1427 git commit -am initial &&
1428 git init no-thin &&
1429 git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
1430 git push no-thin/.git refs/heads/master:refs/heads/foo &&
1431 echo modified >> path1 &&
1432 git commit -am modified &&
1433 git repack -adf &&
1434 rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
1435 git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/master:refs/heads/foo
1436'
1437
458a7e50
JK
1438test_expect_success 'pushing a tag pushes the tagged object' '
1439 rm -rf dst.git &&
1440 blob=$(echo unreferenced | git hash-object -w --stdin) &&
1441 git tag -m foo tag-of-blob $blob &&
1442 git init --bare dst.git &&
1443 git push dst.git tag-of-blob &&
1444 # the receiving index-pack should have noticed
1445 # any problems, but we double check
1446 echo unreferenced >expect &&
1447 git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
1448 test_cmp expect actual
1449'
1450
72549dfd
JK
1451test_expect_success 'push into bare respects core.logallrefupdates' '
1452 rm -rf dst.git &&
1453 git init --bare dst.git &&
1454 git -C dst.git config core.logallrefupdates true &&
1455
1456 # double push to test both with and without
1457 # the actual pack transfer
1458 git push dst.git master:one &&
1459 echo "one@{0} push" >expect &&
1460 git -C dst.git log -g --format="%gd %gs" one >actual &&
1461 test_cmp expect actual &&
1462
1463 git push dst.git master:two &&
1464 echo "two@{0} push" >expect &&
1465 git -C dst.git log -g --format="%gd %gs" two >actual &&
1466 test_cmp expect actual
1467'
1468
1469test_expect_success 'fetch into bare respects core.logallrefupdates' '
1470 rm -rf dst.git &&
1471 git init --bare dst.git &&
1472 (
1473 cd dst.git &&
1474 git config core.logallrefupdates true &&
1475
1476 # as above, we double-fetch to test both
1477 # with and without pack transfer
1478 git fetch .. master:one &&
1479 echo "one@{0} fetch .. master:one: storing head" >expect &&
1480 git log -g --format="%gd %gs" one >actual &&
1481 test_cmp expect actual &&
1482
1483 git fetch .. master:two &&
1484 echo "two@{0} fetch .. master:two: storing head" >expect &&
1485 git log -g --format="%gd %gs" two >actual &&
1486 test_cmp expect actual
1487 )
1488'
1489
1404bcbb
JS
1490test_expect_success 'receive.denyCurrentBranch = updateInstead' '
1491 git push testrepo master &&
4d7a5cea
JH
1492 (
1493 cd testrepo &&
1404bcbb
JS
1494 git reset --hard &&
1495 git config receive.denyCurrentBranch updateInstead
1496 ) &&
1497 test_commit third path2 &&
4d7a5cea
JH
1498
1499 # Try pushing into a repository with pristine working tree
1404bcbb 1500 git push testrepo master &&
4d7a5cea
JH
1501 (
1502 cd testrepo &&
1503 git update-index -q --refresh &&
1504 git diff-files --quiet -- &&
1505 git diff-index --quiet --cached HEAD -- &&
1506 test third = "$(cat path2)" &&
1507 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1508 ) &&
1509
1510 # Try pushing into a repository with working tree needing a refresh
1511 (
1512 cd testrepo &&
1513 git reset --hard HEAD^ &&
1514 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
0e496492 1515 test-tool chmtime +100 path1
4d7a5cea
JH
1516 ) &&
1517 git push testrepo master &&
1518 (
1519 cd testrepo &&
1404bcbb
JS
1520 git update-index -q --refresh &&
1521 git diff-files --quiet -- &&
1522 git diff-index --quiet --cached HEAD -- &&
4d7a5cea
JH
1523 test_cmp ../path1 path1 &&
1524 test third = "$(cat path2)" &&
1525 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1404bcbb 1526 ) &&
4d7a5cea
JH
1527
1528 # Update what is to be pushed
1404bcbb 1529 test_commit fourth path2 &&
4d7a5cea
JH
1530
1531 # Try pushing into a repository with a dirty working tree
1532 # (1) the working tree updated
1533 (
1534 cd testrepo &&
1535 echo changed >path1
1536 ) &&
1404bcbb 1537 test_must_fail git push testrepo master &&
4d7a5cea
JH
1538 (
1539 cd testrepo &&
1540 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1541 git diff --quiet --cached &&
1542 test changed = "$(cat path1)"
1543 ) &&
1544
1545 # (2) the index updated
1546 (
1547 cd testrepo &&
1548 echo changed >path1 &&
1549 git add path1
1550 ) &&
1551 test_must_fail git push testrepo master &&
1552 (
1553 cd testrepo &&
1554 test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
1555 git diff --quiet &&
1556 test changed = "$(cat path1)"
1557 ) &&
1558
1559 # Introduce a new file in the update
1560 test_commit fifth path3 &&
1561
1562 # (3) the working tree has an untracked file that would interfere
1563 (
1564 cd testrepo &&
1565 git reset --hard &&
1566 echo changed >path3
1567 ) &&
1568 test_must_fail git push testrepo master &&
1569 (
1570 cd testrepo &&
1571 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1572 git diff --quiet &&
1573 git diff --quiet --cached &&
1574 test changed = "$(cat path3)"
1575 ) &&
1576
1577 # (4) the target changes to what gets pushed but it still is a change
1578 (
1579 cd testrepo &&
1580 git reset --hard &&
1581 echo fifth >path3 &&
1582 git add path3
1583 ) &&
1584 test_must_fail git push testrepo master &&
1585 (
1586 cd testrepo &&
1587 test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
1404bcbb 1588 git diff --quiet &&
4d7a5cea 1589 test fifth = "$(cat path3)"
1a51b524 1590 ) &&
4d7a5cea 1591
1a51b524
JH
1592 # (5) push into void
1593 rm -fr void &&
1594 git init void &&
1595 (
1596 cd void &&
1597 git config receive.denyCurrentBranch updateInstead
1598 ) &&
1599 git push void master &&
1600 (
1601 cd void &&
1602 test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1603 git diff --quiet &&
1604 git diff --cached --quiet
b072a25f
JH
1605 ) &&
1606
1607 # (6) updateInstead intervened by fast-forward check
1608 test_must_fail git push void master^:master &&
1609 test $(git -C void rev-parse HEAD) = $(git rev-parse master) &&
1610 git -C void diff --quiet &&
1611 git -C void diff --cached --quiet
1404bcbb
JS
1612'
1613
08553319
JH
1614test_expect_success 'updateInstead with push-to-checkout hook' '
1615 rm -fr testrepo &&
1616 git init testrepo &&
1617 (
1618 cd testrepo &&
1619 git pull .. master &&
1620 git reset --hard HEAD^^ &&
1621 git tag initial &&
1622 git config receive.denyCurrentBranch updateInstead &&
1623 write_script .git/hooks/push-to-checkout <<-\EOF
1624 echo >&2 updating from $(git rev-parse HEAD)
1625 echo >&2 updating to "$1"
1626
1627 git update-index -q --refresh &&
1628 git read-tree -u -m HEAD "$1" || {
1629 status=$?
1630 echo >&2 read-tree failed
1631 exit $status
1632 }
1633 EOF
1634 ) &&
1635
1636 # Try pushing into a pristine
1637 git push testrepo master &&
1638 (
1639 cd testrepo &&
1640 git diff --quiet &&
1641 git diff HEAD --quiet &&
1642 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1643 ) &&
1644
1645 # Try pushing into a repository with conflicting change
1646 (
1647 cd testrepo &&
1648 git reset --hard initial &&
1649 echo conflicting >path2
1650 ) &&
1651 test_must_fail git push testrepo master &&
1652 (
1653 cd testrepo &&
1654 test $(git rev-parse initial) = $(git rev-parse HEAD) &&
1655 test conflicting = "$(cat path2)" &&
1656 git diff-index --quiet --cached HEAD
1657 ) &&
1658
1659 # Try pushing into a repository with unrelated change
1660 (
1661 cd testrepo &&
1662 git reset --hard initial &&
1663 echo unrelated >path1 &&
1664 echo irrelevant >path5 &&
1665 git add path5
1666 ) &&
1667 git push testrepo master &&
1668 (
1669 cd testrepo &&
1670 test "$(cat path1)" = unrelated &&
1671 test "$(cat path5)" = irrelevant &&
1672 test "$(git diff --name-only --cached HEAD)" = path5 &&
1673 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1a51b524
JH
1674 ) &&
1675
1676 # push into void
1677 rm -fr void &&
1678 git init void &&
1679 (
1680 cd void &&
1681 git config receive.denyCurrentBranch updateInstead &&
1682 write_script .git/hooks/push-to-checkout <<-\EOF
1683 if git rev-parse --quiet --verify HEAD
1684 then
1685 has_head=yes
1686 echo >&2 updating from $(git rev-parse HEAD)
1687 else
1688 has_head=no
1689 echo >&2 pushing into void
1690 fi
1691 echo >&2 updating to "$1"
1692
1693 git update-index -q --refresh &&
1694 case "$has_head" in
1695 yes)
1696 git read-tree -u -m HEAD "$1" ;;
1697 no)
1698 git read-tree -u -m "$1" ;;
1699 esac || {
1700 status=$?
1701 echo >&2 read-tree failed
1702 exit $status
1703 }
1704 EOF
1705 ) &&
1706
1707 git push void master &&
1708 (
1709 cd void &&
1710 git diff --quiet &&
1711 git diff --cached --quiet &&
1712 test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
08553319
JH
1713 )
1714'
1715
4ef34648
HV
1716test_expect_success 'denyCurrentBranch and worktrees' '
1717 git worktree add new-wt &&
1718 git clone . cloned &&
1719 test_commit -C cloned first &&
1720 test_config receive.denyCurrentBranch refuse &&
1721 test_must_fail git -C cloned push origin HEAD:new-wt &&
1722 test_config receive.denyCurrentBranch updateInstead &&
1723 git -C cloned push origin HEAD:new-wt &&
1724 test_must_fail git -C cloned push --delete origin new-wt
1725'
1726
bcdb34f7 1727test_done