3 test_description
='upload-pack ref-in-want'
8 sed -n -e '/wanted-refs/,/0001/{
12 }' <out | test-tool pkt-line unpack
>actual_refs
15 get_actual_commits
() {
16 test-tool pkt-line unpack-sideband
<out
>o.pack
&&
17 git index-pack o.pack
&&
18 git verify-pack
-v o.idx
>objs
&&
19 sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs
>objs.
sed &&
20 sort >actual_commits
<objs.
sed
25 test_cmp expected_refs actual_refs
&&
27 sort expected_commits
>sorted_commits
&&
28 test_cmp sorted_commits actual_commits
34 if test "$(test_oid algo)" != sha1
36 echo "object-format=$(test_oid algo)"
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.
45 # write_fetch_command <<-EOF
46 # want-ref refs/heads/main
47 # have $(git rev-parse a)
50 # write_fetch_command <<-EOF
51 # want $(git rev-parse b)
52 # have $(git rev-parse a)
55 write_fetch_command
() {
56 write_command fetch
&&
70 test_expect_success
'setup repository' '
73 git checkout -b o/foo &&
76 git checkout -b o/bar b &&
78 git checkout -b baz a &&
84 test_expect_success
'config controls ref-in-want advertisement' '
85 test-tool serve-v2 --advertise-capabilities >out &&
86 perl -ne "/ref-in-want/ and print" out >out.filter &&
87 test_must_be_empty out.filter &&
89 git config uploadpack.allowRefInWant false &&
90 test-tool serve-v2 --advertise-capabilities >out &&
91 perl -ne "/ref-in-want/ and print" out >out.filter &&
92 test_must_be_empty out.filter &&
94 git config uploadpack.allowRefInWant true &&
95 test-tool serve-v2 --advertise-capabilities >out &&
96 perl -ne "/ref-in-want/ and print" out >out.filter &&
97 test_file_not_empty out.filter
100 test_expect_success
'invalid want-ref line' '
101 write_fetch_command >pkt <<-EOF &&
102 want-ref refs/heads/non-existent
105 test-tool pkt-line pack <pkt >in &&
106 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
107 grep "unknown ref" out
110 test_expect_success
'basic want-ref' '
111 oid=$(git rev-parse f) &&
112 cat >expected_refs <<-EOF &&
115 git rev-parse f >expected_commits &&
117 write_fetch_command >pkt <<-EOF &&
118 want-ref refs/heads/main
119 have $(git rev-parse a)
121 test-tool pkt-line pack <pkt >in &&
123 test-tool serve-v2 --stateless-rpc >out <in &&
127 test_expect_success
'multiple want-ref lines' '
128 oid_c=$(git rev-parse c) &&
129 oid_d=$(git rev-parse d) &&
130 cat >expected_refs <<-EOF &&
131 $oid_c refs/heads/o/foo
132 $oid_d refs/heads/o/bar
134 git rev-parse c d >expected_commits &&
136 write_fetch_command >pkt <<-EOF &&
137 want-ref refs/heads/o/foo
138 want-ref refs/heads/o/bar
139 have $(git rev-parse b)
141 test-tool pkt-line pack <pkt >in &&
143 test-tool serve-v2 --stateless-rpc >out <in &&
147 test_expect_success
'mix want and want-ref' '
148 oid=$(git rev-parse f) &&
149 cat >expected_refs <<-EOF &&
152 git rev-parse e f >expected_commits &&
154 write_fetch_command >pkt <<-EOF &&
155 want-ref refs/heads/main
156 want $(git rev-parse e)
157 have $(git rev-parse a)
159 test-tool pkt-line pack <pkt >in &&
161 test-tool serve-v2 --stateless-rpc >out <in &&
165 test_expect_success
'want-ref with ref we already have commit for' '
166 oid=$(git rev-parse c) &&
167 cat >expected_refs <<-EOF &&
168 $oid refs/heads/o/foo
172 write_fetch_command >pkt <<-EOF &&
173 want-ref refs/heads/o/foo
174 have $(git rev-parse c)
176 test-tool pkt-line pack <pkt >in &&
178 test-tool serve-v2 --stateless-rpc >out <in &&
183 LOCAL_PRISTINE
="$(pwd)/local_pristine"
200 test_expect_success
'setup repos for fetching with ref-in-want tests' '
202 git init -b main "$REPO" &&
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 &&
212 test_commit_bulk --id=s 33 &&
214 # Add novel commits to upstream
217 git checkout -b o/foo &&
220 git checkout -b o/bar b &&
222 git checkout -b baz a &&
227 git -C "$REPO" config uploadpack.allowRefInWant true &&
228 git -C "$LOCAL_PRISTINE" config protocol.version 2
231 test_expect_success
'fetching with exact OID' '
232 test_when_finished "rm -f log" &&
235 cp -r "$LOCAL_PRISTINE" local &&
236 oid=$(git -C "$REPO" rev-parse d) &&
237 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
238 "$oid":refs/heads/actual &&
240 git -C "$REPO" rev-parse "d" >expected &&
241 git -C local rev-parse refs/heads/actual >actual &&
242 test_cmp expected actual &&
246 test_expect_success
'fetching multiple refs' '
247 test_when_finished "rm -f log" &&
250 cp -r "$LOCAL_PRISTINE" local &&
251 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
253 git -C "$REPO" rev-parse "main" "baz" >expected &&
254 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
255 test_cmp expected actual &&
256 grep "want-ref refs/heads/main" log &&
257 grep "want-ref refs/heads/baz" log
260 test_expect_success
'fetching ref and exact OID' '
261 test_when_finished "rm -f log" &&
264 cp -r "$LOCAL_PRISTINE" local &&
265 oid=$(git -C "$REPO" rev-parse b) &&
266 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
267 main "$oid":refs/heads/actual &&
269 git -C "$REPO" rev-parse "main" "b" >expected &&
270 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
271 test_cmp expected actual &&
272 grep "want $oid" log &&
273 grep "want-ref refs/heads/main" log
276 test_expect_success
'fetching with wildcard that does not match any refs' '
277 test_when_finished "rm -f log" &&
280 cp -r "$LOCAL_PRISTINE" local &&
281 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
282 test_must_be_empty out
285 test_expect_success
'fetching with wildcard that matches multiple refs' '
286 test_when_finished "rm -f log" &&
289 cp -r "$LOCAL_PRISTINE" local &&
290 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
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
299 REPO
="$(pwd)/repo-ns"
301 test_expect_success
'setup namespaced repo' '
303 git init -b main "$REPO" &&
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
315 git -C "$REPO" config uploadpack.allowRefInWant true
318 test_expect_success
'with namespace: want-ref is considered relative to namespace' '
319 wanted_ref=refs/heads/ns-yes &&
321 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
322 cat >expected_refs <<-EOF &&
325 cat >expected_commits <<-EOF &&
327 $(git -C "$REPO" rev-parse a)
330 write_fetch_command >pkt <<-EOF &&
333 test-tool pkt-line pack <pkt >in &&
335 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
339 test_expect_success
'with namespace: want-ref outside namespace is unknown' '
340 wanted_ref=refs/heads/ns-no &&
342 write_fetch_command >pkt <<-EOF &&
345 test-tool pkt-line pack <pkt >in &&
347 test_must_fail env GIT_NAMESPACE=ns \
348 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
349 grep "unknown ref" out
352 # Cross-check refs/heads/ns-no indeed exists
353 test_expect_success
'without namespace: want-ref outside namespace succeeds' '
354 wanted_ref=refs/heads/ns-no &&
356 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
357 cat >expected_refs <<-EOF &&
360 cat >expected_commits <<-EOF &&
362 $(git -C "$REPO" rev-parse a)
365 write_fetch_command >pkt <<-EOF &&
368 test-tool pkt-line pack <pkt >in &&
370 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
374 test_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 &&
378 write_fetch_command >pkt <<-EOF &&
381 test-tool pkt-line pack <pkt >in &&
383 test_must_fail env GIT_NAMESPACE=ns \
384 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
385 grep "unknown ref" out
388 # Cross-check refs/heads/hidden indeed exists
389 test_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 &&
393 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
394 cat >expected_refs <<-EOF &&
397 cat >expected_commits <<-EOF &&
399 $(git -C "$REPO" rev-parse a)
402 write_fetch_command >pkt <<-EOF &&
405 test-tool pkt-line pack <pkt >in &&
407 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
411 test_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 &&
415 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
416 cat >expected_refs <<-EOF &&
419 cat >expected_commits <<-EOF &&
421 $(git -C "$REPO" rev-parse a)
424 write_fetch_command >pkt <<-EOF &&
427 test-tool pkt-line pack <pkt >in &&
429 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
434 .
"$TEST_DIRECTORY"/lib-httpd.sh
437 REPO
="$HTTPD_DOCUMENT_ROOT_PATH/repo"
438 LOCAL_PRISTINE
="$(pwd)/local_pristine"
440 test_expect_success
'setup repos for change-while-negotiating test' '
442 git init -b main "$REPO" &&
444 >.git/git-daemon-export-ok &&
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 &&
456 # Add novel commits to upstream
463 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
464 git -C "$LOCAL_PRISTINE" config protocol.version 2
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.
474 oid1
=$
(git
-C "$REPO" rev-parse
$1) &&
475 oid2
=$
(git
-C "$REPO" rev-parse
$2) &&
476 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
479 test_expect_success
'server is initially ahead - no ref in want' '
480 git -C "$REPO" config uploadpack.allowRefInWant false &&
482 cp -r "$LOCAL_PRISTINE" local &&
483 inconsistency main $(test_oid numeric) &&
484 test_must_fail git -C local fetch 2>err &&
485 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
488 test_expect_success
'server is initially ahead - ref in want' '
489 git -C "$REPO" config uploadpack.allowRefInWant true &&
491 cp -r "$LOCAL_PRISTINE" local &&
492 inconsistency main $(test_oid numeric) &&
493 git -C local fetch &&
495 git -C "$REPO" rev-parse --verify main >expected &&
496 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
497 test_cmp expected actual
500 test_expect_success
'server is initially behind - no ref in want' '
501 git -C "$REPO" config uploadpack.allowRefInWant false &&
503 cp -r "$LOCAL_PRISTINE" local &&
504 inconsistency main "main^" &&
505 git -C local fetch &&
507 git -C "$REPO" rev-parse --verify "main^" >expected &&
508 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
509 test_cmp expected actual
512 test_expect_success
'server is initially behind - ref in want' '
513 git -C "$REPO" config uploadpack.allowRefInWant true &&
515 cp -r "$LOCAL_PRISTINE" local &&
516 inconsistency main "main^" &&
517 git -C local fetch &&
519 git -C "$REPO" rev-parse --verify "main" >expected &&
520 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
521 test_cmp expected actual
524 test_expect_success
'server loses a ref - ref in want' '
525 git -C "$REPO" config uploadpack.allowRefInWant true &&
527 cp -r "$LOCAL_PRISTINE" local &&
528 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
529 test_must_fail git -C local fetch 2>err &&
531 test_i18ngrep "fatal: remote error: unknown ref refs/heads/rain" err
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.