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