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