]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5703-upload-pack-ref-in-want.sh
Merge branch 'tl/doc-cli-options-first'
[thirdparty/git.git] / t / t5703-upload-pack-ref-in-want.sh
CommitLineData
516e2b76
BW
1#!/bin/sh
2
3test_description='upload-pack ref-in-want'
4
5. ./test-lib.sh
6
7get_actual_refs () {
8 sed -n -e '/wanted-refs/,/0001/{
9 /wanted-refs/d
10 /0001/d
11 p
8ea40cc5 12 }' <out | test-tool pkt-line unpack >actual_refs
516e2b76
BW
13}
14
15get_actual_commits () {
84370e36 16 test-tool pkt-line unpack-sideband <out >o.pack &&
516e2b76 17 git index-pack o.pack &&
763b47ba 18 git verify-pack -v o.idx >objs &&
9aebc470
ÆAB
19 sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs >objs.sed &&
20 sort >actual_commits <objs.sed
516e2b76
BW
21}
22
23check_output () {
24 get_actual_refs &&
25 test_cmp expected_refs actual_refs &&
26 get_actual_commits &&
763b47ba
DL
27 sort expected_commits >sorted_commits &&
28 test_cmp sorted_commits actual_commits
516e2b76
BW
29}
30
f7c6a3bf 31write_command () {
32 echo "command=$1"
33
34 if test "$(test_oid algo)" != sha1
35 then
36 echo "object-format=$(test_oid algo)"
37 fi
38}
39
bac01c64
KA
40# Write a complete fetch command to stdout, suitable for use with `test-tool
41# pkt-line`. "want-ref", "want", and "have" lines are read from stdin.
42#
43# Examples:
44#
45# write_fetch_command <<-EOF
46# want-ref refs/heads/main
47# have $(git rev-parse a)
48# EOF
49#
50# write_fetch_command <<-EOF
51# want $(git rev-parse b)
52# have $(git rev-parse a)
53# EOF
54#
55write_fetch_command () {
56 write_command fetch &&
57 echo "0001" &&
58 echo "no-progress" &&
59 cat &&
60 echo "done" &&
61 echo "0000"
62}
63
516e2b76
BW
64# c(o/foo) d(o/bar)
65# \ /
72dc1728 66# b e(baz) f(main)
516e2b76
BW
67# \__ | __/
68# \ | /
69# a
70test_expect_success 'setup repository' '
71 test_commit a &&
72dc1728 72 git branch -M main &&
516e2b76
BW
73 git checkout -b o/foo &&
74 test_commit b &&
75 test_commit c &&
76 git checkout -b o/bar b &&
77 test_commit d &&
78 git checkout -b baz a &&
79 test_commit e &&
72dc1728 80 git checkout main &&
516e2b76
BW
81 test_commit f
82'
83
84test_expect_success 'config controls ref-in-want advertisement' '
b7ce24d0 85 test-tool serve-v2 --advertise-capabilities >out &&
1eb73712
ĐTCD
86 perl -ne "/ref-in-want/ and print" out >out.filter &&
87 test_must_be_empty out.filter &&
516e2b76
BW
88
89 git config uploadpack.allowRefInWant false &&
b7ce24d0 90 test-tool serve-v2 --advertise-capabilities >out &&
1eb73712
ĐTCD
91 perl -ne "/ref-in-want/ and print" out >out.filter &&
92 test_must_be_empty out.filter &&
516e2b76
BW
93
94 git config uploadpack.allowRefInWant true &&
b7ce24d0 95 test-tool serve-v2 --advertise-capabilities >out &&
1eb73712
ĐTCD
96 perl -ne "/ref-in-want/ and print" out >out.filter &&
97 test_file_not_empty out.filter
516e2b76
BW
98'
99
100test_expect_success 'invalid want-ref line' '
bac01c64 101 write_fetch_command >pkt <<-EOF &&
516e2b76 102 want-ref refs/heads/non-existent
516e2b76
BW
103 EOF
104
bac01c64 105 test-tool pkt-line pack <pkt >in &&
b7ce24d0 106 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
516e2b76
BW
107 grep "unknown ref" out
108'
109
110test_expect_success 'basic want-ref' '
763b47ba 111 oid=$(git rev-parse f) &&
516e2b76 112 cat >expected_refs <<-EOF &&
72dc1728 113 $oid refs/heads/main
516e2b76 114 EOF
763b47ba 115 git rev-parse f >expected_commits &&
516e2b76 116
bac01c64 117 write_fetch_command >pkt <<-EOF &&
72dc1728 118 want-ref refs/heads/main
bac01c64 119 have $(git rev-parse a)
516e2b76 120 EOF
bac01c64 121 test-tool pkt-line pack <pkt >in &&
516e2b76 122
b7ce24d0 123 test-tool serve-v2 --stateless-rpc >out <in &&
516e2b76
BW
124 check_output
125'
126
127test_expect_success 'multiple want-ref lines' '
763b47ba
DL
128 oid_c=$(git rev-parse c) &&
129 oid_d=$(git rev-parse d) &&
516e2b76 130 cat >expected_refs <<-EOF &&
763b47ba
DL
131 $oid_c refs/heads/o/foo
132 $oid_d refs/heads/o/bar
516e2b76 133 EOF
763b47ba 134 git rev-parse c d >expected_commits &&
516e2b76 135
bac01c64 136 write_fetch_command >pkt <<-EOF &&
516e2b76
BW
137 want-ref refs/heads/o/foo
138 want-ref refs/heads/o/bar
bac01c64 139 have $(git rev-parse b)
516e2b76 140 EOF
bac01c64 141 test-tool pkt-line pack <pkt >in &&
516e2b76 142
b7ce24d0 143 test-tool serve-v2 --stateless-rpc >out <in &&
516e2b76
BW
144 check_output
145'
146
147test_expect_success 'mix want and want-ref' '
763b47ba 148 oid=$(git rev-parse f) &&
516e2b76 149 cat >expected_refs <<-EOF &&
72dc1728 150 $oid refs/heads/main
516e2b76 151 EOF
763b47ba 152 git rev-parse e f >expected_commits &&
516e2b76 153
bac01c64 154 write_fetch_command >pkt <<-EOF &&
72dc1728 155 want-ref refs/heads/main
516e2b76
BW
156 want $(git rev-parse e)
157 have $(git rev-parse a)
516e2b76 158 EOF
bac01c64 159 test-tool pkt-line pack <pkt >in &&
516e2b76 160
b7ce24d0 161 test-tool serve-v2 --stateless-rpc >out <in &&
516e2b76
BW
162 check_output
163'
164
165test_expect_success 'want-ref with ref we already have commit for' '
763b47ba 166 oid=$(git rev-parse c) &&
516e2b76 167 cat >expected_refs <<-EOF &&
763b47ba 168 $oid refs/heads/o/foo
516e2b76
BW
169 EOF
170 >expected_commits &&
171
bac01c64 172 write_fetch_command >pkt <<-EOF &&
516e2b76 173 want-ref refs/heads/o/foo
bac01c64 174 have $(git rev-parse c)
516e2b76 175 EOF
bac01c64 176 test-tool pkt-line pack <pkt >in &&
516e2b76 177
b7ce24d0 178 test-tool serve-v2 --stateless-rpc >out <in &&
516e2b76
BW
179 check_output
180'
181
73302051
BW
182REPO="$(pwd)/repo"
183LOCAL_PRISTINE="$(pwd)/local_pristine"
184
185# $REPO
186# c(o/foo) d(o/bar)
187# \ /
72dc1728 188# b e(baz) f(main)
73302051
BW
189# \__ | __/
190# \ | /
191# a
192#
193# $LOCAL_PRISTINE
194# s32(side)
195# |
196# .
197# .
198# |
72dc1728 199# a(main)
73302051
BW
200test_expect_success 'setup repos for fetching with ref-in-want tests' '
201 (
72dc1728 202 git init -b main "$REPO" &&
73302051
BW
203 cd "$REPO" &&
204 test_commit a &&
205
206 # Local repo with many commits (so that negotiation will take
207 # more than 1 request/response pair)
208 rm -rf "$LOCAL_PRISTINE" &&
209 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
210 cd "$LOCAL_PRISTINE" &&
211 git checkout -b side &&
ac093d55 212 test_commit_bulk --id=s 33 &&
73302051
BW
213
214 # Add novel commits to upstream
72dc1728 215 git checkout main &&
73302051
BW
216 cd "$REPO" &&
217 git checkout -b o/foo &&
218 test_commit b &&
219 test_commit c &&
220 git checkout -b o/bar b &&
221 test_commit d &&
222 git checkout -b baz a &&
223 test_commit e &&
72dc1728 224 git checkout main &&
73302051
BW
225 test_commit f
226 ) &&
227 git -C "$REPO" config uploadpack.allowRefInWant true &&
228 git -C "$LOCAL_PRISTINE" config protocol.version 2
229'
230
231test_expect_success 'fetching with exact OID' '
232 test_when_finished "rm -f log" &&
233
234 rm -rf local &&
235 cp -r "$LOCAL_PRISTINE" local &&
763b47ba 236 oid=$(git -C "$REPO" rev-parse d) &&
73302051 237 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
763b47ba 238 "$oid":refs/heads/actual &&
73302051
BW
239
240 git -C "$REPO" rev-parse "d" >expected &&
241 git -C local rev-parse refs/heads/actual >actual &&
242 test_cmp expected actual &&
763b47ba 243 grep "want $oid" log
73302051
BW
244'
245
246test_expect_success 'fetching multiple refs' '
247 test_when_finished "rm -f log" &&
248
249 rm -rf local &&
250 cp -r "$LOCAL_PRISTINE" local &&
72dc1728 251 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
73302051 252
72dc1728
JS
253 git -C "$REPO" rev-parse "main" "baz" >expected &&
254 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
73302051 255 test_cmp expected actual &&
72dc1728 256 grep "want-ref refs/heads/main" log &&
73302051
BW
257 grep "want-ref refs/heads/baz" log
258'
259
260test_expect_success 'fetching ref and exact OID' '
261 test_when_finished "rm -f log" &&
262
263 rm -rf local &&
264 cp -r "$LOCAL_PRISTINE" local &&
763b47ba 265 oid=$(git -C "$REPO" rev-parse b) &&
73302051 266 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
72dc1728 267 main "$oid":refs/heads/actual &&
73302051 268
72dc1728
JS
269 git -C "$REPO" rev-parse "main" "b" >expected &&
270 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
73302051 271 test_cmp expected actual &&
763b47ba 272 grep "want $oid" log &&
72dc1728 273 grep "want-ref refs/heads/main" log
73302051
BW
274'
275
276test_expect_success 'fetching with wildcard that does not match any refs' '
277 test_when_finished "rm -f log" &&
278
279 rm -rf local &&
280 cp -r "$LOCAL_PRISTINE" local &&
281 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
282 test_must_be_empty out
283'
284
285test_expect_success 'fetching with wildcard that matches multiple refs' '
286 test_when_finished "rm -f log" &&
287
288 rm -rf local &&
289 cp -r "$LOCAL_PRISTINE" local &&
290 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
291
292 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
293 git -C local rev-parse "o/foo" "o/bar" >actual &&
294 test_cmp expected actual &&
295 grep "want-ref refs/heads/o/foo" log &&
296 grep "want-ref refs/heads/o/bar" log
297'
298
39551406
KA
299REPO="$(pwd)/repo-ns"
300
301test_expect_success 'setup namespaced repo' '
302 (
303 git init -b main "$REPO" &&
304 cd "$REPO" &&
305 test_commit a &&
306 test_commit b &&
307 git checkout a &&
308 test_commit c &&
309 git checkout a &&
310 test_commit d &&
311 git update-ref refs/heads/ns-no b &&
312 git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
313 git update-ref refs/namespaces/ns/refs/heads/hidden d
314 ) &&
315 git -C "$REPO" config uploadpack.allowRefInWant true
316'
317
318test_expect_success 'with namespace: want-ref is considered relative to namespace' '
319 wanted_ref=refs/heads/ns-yes &&
320
321 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
322 cat >expected_refs <<-EOF &&
323 $oid $wanted_ref
324 EOF
325 cat >expected_commits <<-EOF &&
326 $oid
327 $(git -C "$REPO" rev-parse a)
328 EOF
329
330 write_fetch_command >pkt <<-EOF &&
331 want-ref $wanted_ref
332 EOF
333 test-tool pkt-line pack <pkt >in &&
334
335 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
336 check_output
337'
338
339test_expect_success 'with namespace: want-ref outside namespace is unknown' '
340 wanted_ref=refs/heads/ns-no &&
341
342 write_fetch_command >pkt <<-EOF &&
343 want-ref $wanted_ref
344 EOF
345 test-tool pkt-line pack <pkt >in &&
346
347 test_must_fail env GIT_NAMESPACE=ns \
348 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
349 grep "unknown ref" out
350'
351
352# Cross-check refs/heads/ns-no indeed exists
353test_expect_success 'without namespace: want-ref outside namespace succeeds' '
354 wanted_ref=refs/heads/ns-no &&
355
356 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
357 cat >expected_refs <<-EOF &&
358 $oid $wanted_ref
359 EOF
360 cat >expected_commits <<-EOF &&
361 $oid
362 $(git -C "$REPO" rev-parse a)
363 EOF
364
365 write_fetch_command >pkt <<-EOF &&
366 want-ref $wanted_ref
367 EOF
368 test-tool pkt-line pack <pkt >in &&
369
370 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
371 check_output
372'
373
374test_expect_success 'with namespace: hideRefs is matched, relative to namespace' '
375 wanted_ref=refs/heads/hidden &&
376 git -C "$REPO" config transfer.hideRefs $wanted_ref &&
377
378 write_fetch_command >pkt <<-EOF &&
379 want-ref $wanted_ref
380 EOF
381 test-tool pkt-line pack <pkt >in &&
382
383 test_must_fail env GIT_NAMESPACE=ns \
384 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
385 grep "unknown ref" out
386'
387
388# Cross-check refs/heads/hidden indeed exists
389test_expect_success 'with namespace: want-ref succeeds if hideRefs is removed' '
390 wanted_ref=refs/heads/hidden &&
391 git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
392
393 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
394 cat >expected_refs <<-EOF &&
395 $oid $wanted_ref
396 EOF
397 cat >expected_commits <<-EOF &&
398 $oid
399 $(git -C "$REPO" rev-parse a)
400 EOF
401
402 write_fetch_command >pkt <<-EOF &&
403 want-ref $wanted_ref
404 EOF
405 test-tool pkt-line pack <pkt >in &&
406
407 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
408 check_output
409'
410
411test_expect_success 'without namespace: relative hideRefs does not match' '
412 wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
413 git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
414
415 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
416 cat >expected_refs <<-EOF &&
417 $oid $wanted_ref
418 EOF
419 cat >expected_commits <<-EOF &&
420 $oid
421 $(git -C "$REPO" rev-parse a)
422 EOF
423
424 write_fetch_command >pkt <<-EOF &&
425 want-ref $wanted_ref
426 EOF
427 test-tool pkt-line pack <pkt >in &&
428
429 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
430 check_output
431'
432
433
7f005b0f
SG
434. "$TEST_DIRECTORY"/lib-httpd.sh
435start_httpd
436
437REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
438LOCAL_PRISTINE="$(pwd)/local_pristine"
439
440test_expect_success 'setup repos for change-while-negotiating test' '
441 (
72dc1728 442 git init -b main "$REPO" &&
7f005b0f
SG
443 cd "$REPO" &&
444 >.git/git-daemon-export-ok &&
445 test_commit m1 &&
446 git tag -d m1 &&
447
448 # Local repo with many commits (so that negotiation will take
449 # more than 1 request/response pair)
450 rm -rf "$LOCAL_PRISTINE" &&
451 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
452 cd "$LOCAL_PRISTINE" &&
453 git checkout -b side &&
454 test_commit_bulk --id=s 33 &&
455
456 # Add novel commits to upstream
72dc1728 457 git checkout main &&
7f005b0f
SG
458 cd "$REPO" &&
459 test_commit m2 &&
460 test_commit m3 &&
461 git tag -d m2 m3
462 ) &&
eafff6e4 463 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
7f005b0f
SG
464 git -C "$LOCAL_PRISTINE" config protocol.version 2
465'
466
467inconsistency () {
468 # Simulate that the server initially reports $2 as the ref
469 # corresponding to $1, and after that, $1 as the ref corresponding to
470 # $1. This corresponds to the real-life situation where the server's
471 # repository appears to change during negotiation, for example, when
472 # different servers in a load-balancing arrangement serve (stateless)
473 # RPCs during a single negotiation.
eacaa1c1
DL
474 oid1=$(git -C "$REPO" rev-parse $1) &&
475 oid2=$(git -C "$REPO" rev-parse $2) &&
eafff6e4 476 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
7f005b0f
SG
477}
478
479test_expect_success 'server is initially ahead - no ref in want' '
480 git -C "$REPO" config uploadpack.allowRefInWant false &&
481 rm -rf local &&
482 cp -r "$LOCAL_PRISTINE" local &&
72dc1728 483 inconsistency main $(test_oid numeric) &&
7f005b0f
SG
484 test_must_fail git -C local fetch 2>err &&
485 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
486'
487
488test_expect_success 'server is initially ahead - ref in want' '
489 git -C "$REPO" config uploadpack.allowRefInWant true &&
490 rm -rf local &&
491 cp -r "$LOCAL_PRISTINE" local &&
72dc1728 492 inconsistency main $(test_oid numeric) &&
7f005b0f
SG
493 git -C local fetch &&
494
72dc1728
JS
495 git -C "$REPO" rev-parse --verify main >expected &&
496 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
7f005b0f
SG
497 test_cmp expected actual
498'
499
500test_expect_success 'server is initially behind - no ref in want' '
501 git -C "$REPO" config uploadpack.allowRefInWant false &&
502 rm -rf local &&
503 cp -r "$LOCAL_PRISTINE" local &&
72dc1728 504 inconsistency main "main^" &&
7f005b0f
SG
505 git -C local fetch &&
506
72dc1728
JS
507 git -C "$REPO" rev-parse --verify "main^" >expected &&
508 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
7f005b0f
SG
509 test_cmp expected actual
510'
511
512test_expect_success 'server is initially behind - ref in want' '
513 git -C "$REPO" config uploadpack.allowRefInWant true &&
514 rm -rf local &&
515 cp -r "$LOCAL_PRISTINE" local &&
72dc1728 516 inconsistency main "main^" &&
7f005b0f
SG
517 git -C local fetch &&
518
72dc1728
JS
519 git -C "$REPO" rev-parse --verify "main" >expected &&
520 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
7f005b0f
SG
521 test_cmp expected actual
522'
523
72dc1728 524test_expect_success 'server loses a ref - ref in want' '
7f005b0f
SG
525 git -C "$REPO" config uploadpack.allowRefInWant true &&
526 rm -rf local &&
527 cp -r "$LOCAL_PRISTINE" local &&
97cf8d50 528 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
7f005b0f
SG
529 test_must_fail git -C local fetch 2>err &&
530
97cf8d50 531 test_i18ngrep "fatal: remote error: unknown ref refs/heads/rain" err
7f005b0f
SG
532'
533
534# DO NOT add non-httpd-specific tests here, because the last part of this
535# test script is only executed when httpd is available and enabled.
536
516e2b76 537test_done