3 test_description
='upload-pack ref-in-want'
5 TEST_PASSES_SANITIZE_LEAK
=true
9 sed -n -e '/wanted-refs/,/0001/{
13 }' <out | test-tool pkt-line unpack
>actual_refs
16 get_actual_commits
() {
17 test-tool pkt-line unpack-sideband
<out
>o.pack
&&
18 git index-pack o.pack
&&
19 git verify-pack
-v o.idx
>objs
&&
20 sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs
>objs.
sed &&
21 sort >actual_commits
<objs.
sed
26 test_cmp expected_refs actual_refs
&&
28 sort expected_commits
>sorted_commits
&&
29 test_cmp sorted_commits actual_commits
35 if test "$(test_oid algo)" != sha1
37 echo "object-format=$(test_oid algo)"
41 # Write a complete fetch command to stdout, suitable for use with `test-tool
42 # pkt-line`. "want-ref", "want", and "have" lines are read from stdin.
46 # write_fetch_command <<-EOF
47 # want-ref refs/heads/main
48 # have $(git rev-parse a)
51 # write_fetch_command <<-EOF
52 # want $(git rev-parse b)
53 # have $(git rev-parse a)
56 write_fetch_command
() {
57 write_command fetch
&&
71 test_expect_success
'setup repository' '
74 git checkout -b o/foo &&
77 git checkout -b o/bar b &&
79 git checkout -b baz a &&
85 test_expect_success
'config controls ref-in-want advertisement' '
86 test-tool serve-v2 --advertise-capabilities >out &&
87 perl -ne "/ref-in-want/ and print" out >out.filter &&
88 test_must_be_empty out.filter &&
90 git config uploadpack.allowRefInWant false &&
91 test-tool serve-v2 --advertise-capabilities >out &&
92 perl -ne "/ref-in-want/ and print" out >out.filter &&
93 test_must_be_empty out.filter &&
95 git config uploadpack.allowRefInWant true &&
96 test-tool serve-v2 --advertise-capabilities >out &&
97 perl -ne "/ref-in-want/ and print" out >out.filter &&
98 test_file_not_empty out.filter
101 test_expect_success
'invalid want-ref line' '
102 write_fetch_command >pkt <<-EOF &&
103 want-ref refs/heads/non-existent
106 test-tool pkt-line pack <pkt >in &&
107 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
108 grep "unknown ref" out
111 test_expect_success
'basic want-ref' '
112 oid=$(git rev-parse f) &&
113 cat >expected_refs <<-EOF &&
116 git rev-parse f >expected_commits &&
118 write_fetch_command >pkt <<-EOF &&
119 want-ref refs/heads/main
120 have $(git rev-parse a)
122 test-tool pkt-line pack <pkt >in &&
124 test-tool serve-v2 --stateless-rpc >out <in &&
128 test_expect_success
'multiple want-ref lines' '
129 oid_c=$(git rev-parse c) &&
130 oid_d=$(git rev-parse d) &&
131 cat >expected_refs <<-EOF &&
132 $oid_c refs/heads/o/foo
133 $oid_d refs/heads/o/bar
135 git rev-parse c d >expected_commits &&
137 write_fetch_command >pkt <<-EOF &&
138 want-ref refs/heads/o/foo
139 want-ref refs/heads/o/bar
140 have $(git rev-parse b)
142 test-tool pkt-line pack <pkt >in &&
144 test-tool serve-v2 --stateless-rpc >out <in &&
148 test_expect_success
'mix want and want-ref' '
149 oid=$(git rev-parse f) &&
150 cat >expected_refs <<-EOF &&
153 git rev-parse e f >expected_commits &&
155 write_fetch_command >pkt <<-EOF &&
156 want-ref refs/heads/main
157 want $(git rev-parse e)
158 have $(git rev-parse a)
160 test-tool pkt-line pack <pkt >in &&
162 test-tool serve-v2 --stateless-rpc >out <in &&
166 test_expect_success
'want-ref with ref we already have commit for' '
167 oid=$(git rev-parse c) &&
168 cat >expected_refs <<-EOF &&
169 $oid refs/heads/o/foo
173 write_fetch_command >pkt <<-EOF &&
174 want-ref refs/heads/o/foo
175 have $(git rev-parse c)
177 test-tool pkt-line pack <pkt >in &&
179 test-tool serve-v2 --stateless-rpc >out <in &&
184 LOCAL_PRISTINE
="$(pwd)/local_pristine"
201 test_expect_success
'setup repos for fetching with ref-in-want tests' '
203 git init -b main "$REPO" &&
207 # Local repo with many commits (so that negotiation will take
208 # more than 1 request/response pair)
209 rm -rf "$LOCAL_PRISTINE" &&
210 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
211 cd "$LOCAL_PRISTINE" &&
212 git checkout -b side &&
213 test_commit_bulk --id=s 33 &&
215 # Add novel commits to upstream
218 git checkout -b o/foo &&
221 git checkout -b o/bar b &&
223 git checkout -b baz a &&
228 git -C "$REPO" config uploadpack.allowRefInWant true &&
229 git -C "$LOCAL_PRISTINE" config protocol.version 2
232 test_expect_success
'fetching with exact OID' '
233 test_when_finished "rm -f log trace2" &&
236 cp -r "$LOCAL_PRISTINE" local &&
237 oid=$(git -C "$REPO" rev-parse d) &&
238 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE2_EVENT="$(pwd)/trace2" \
239 git -C local fetch origin \
240 "$oid":refs/heads/actual &&
242 grep \"key\":\"total_rounds\",\"value\":\"2\" trace2 &&
243 git -C "$REPO" rev-parse "d" >expected &&
244 git -C local rev-parse refs/heads/actual >actual &&
245 test_cmp expected actual &&
249 test_expect_success
'fetching multiple refs' '
250 test_when_finished "rm -f log" &&
253 cp -r "$LOCAL_PRISTINE" local &&
254 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
256 git -C "$REPO" rev-parse "main" "baz" >expected &&
257 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
258 test_cmp expected actual &&
259 grep "want-ref refs/heads/main" log &&
260 grep "want-ref refs/heads/baz" log
263 test_expect_success
'fetching ref and exact OID' '
264 test_when_finished "rm -f log" &&
267 cp -r "$LOCAL_PRISTINE" local &&
268 oid=$(git -C "$REPO" rev-parse b) &&
269 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
270 main "$oid":refs/heads/actual &&
272 git -C "$REPO" rev-parse "main" "b" >expected &&
273 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
274 test_cmp expected actual &&
275 grep "want $oid" log &&
276 grep "want-ref refs/heads/main" log
279 test_expect_success
'fetching with wildcard that does not match any refs' '
280 test_when_finished "rm -f log" &&
283 cp -r "$LOCAL_PRISTINE" local &&
284 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
285 test_must_be_empty out
288 test_expect_success
'fetching with wildcard that matches multiple refs' '
289 test_when_finished "rm -f log" &&
292 cp -r "$LOCAL_PRISTINE" local &&
293 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
295 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
296 git -C local rev-parse "o/foo" "o/bar" >actual &&
297 test_cmp expected actual &&
298 grep "want-ref refs/heads/o/foo" log &&
299 grep "want-ref refs/heads/o/bar" log
302 REPO
="$(pwd)/repo-ns"
304 test_expect_success
'setup namespaced repo' '
306 git init -b main "$REPO" &&
314 git update-ref refs/heads/ns-no b &&
315 git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
316 git update-ref refs/namespaces/ns/refs/heads/hidden d
318 git -C "$REPO" config uploadpack.allowRefInWant true
321 test_expect_success
'with namespace: want-ref is considered relative to namespace' '
322 wanted_ref=refs/heads/ns-yes &&
324 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
325 cat >expected_refs <<-EOF &&
328 cat >expected_commits <<-EOF &&
330 $(git -C "$REPO" rev-parse a)
333 write_fetch_command >pkt <<-EOF &&
336 test-tool pkt-line pack <pkt >in &&
338 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
342 test_expect_success
'with namespace: want-ref outside namespace is unknown' '
343 wanted_ref=refs/heads/ns-no &&
345 write_fetch_command >pkt <<-EOF &&
348 test-tool pkt-line pack <pkt >in &&
350 test_must_fail env GIT_NAMESPACE=ns \
351 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
352 grep "unknown ref" out
355 # Cross-check refs/heads/ns-no indeed exists
356 test_expect_success
'without namespace: want-ref outside namespace succeeds' '
357 wanted_ref=refs/heads/ns-no &&
359 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
360 cat >expected_refs <<-EOF &&
363 cat >expected_commits <<-EOF &&
365 $(git -C "$REPO" rev-parse a)
368 write_fetch_command >pkt <<-EOF &&
371 test-tool pkt-line pack <pkt >in &&
373 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
377 test_expect_success
'with namespace: hideRefs is matched, relative to namespace' '
378 wanted_ref=refs/heads/hidden &&
379 git -C "$REPO" config transfer.hideRefs $wanted_ref &&
381 write_fetch_command >pkt <<-EOF &&
384 test-tool pkt-line pack <pkt >in &&
386 test_must_fail env GIT_NAMESPACE=ns \
387 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
388 grep "unknown ref" out
391 # Cross-check refs/heads/hidden indeed exists
392 test_expect_success
'with namespace: want-ref succeeds if hideRefs is removed' '
393 wanted_ref=refs/heads/hidden &&
394 git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
396 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
397 cat >expected_refs <<-EOF &&
400 cat >expected_commits <<-EOF &&
402 $(git -C "$REPO" rev-parse a)
405 write_fetch_command >pkt <<-EOF &&
408 test-tool pkt-line pack <pkt >in &&
410 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
414 test_expect_success
'without namespace: relative hideRefs does not match' '
415 wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
416 git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
418 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
419 cat >expected_refs <<-EOF &&
422 cat >expected_commits <<-EOF &&
424 $(git -C "$REPO" rev-parse a)
427 write_fetch_command >pkt <<-EOF &&
430 test-tool pkt-line pack <pkt >in &&
432 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
437 .
"$TEST_DIRECTORY"/lib-httpd.sh
440 REPO
="$HTTPD_DOCUMENT_ROOT_PATH/repo"
441 LOCAL_PRISTINE
="$(pwd)/local_pristine"
443 test_expect_success
'setup repos for change-while-negotiating test' '
445 git init -b main "$REPO" &&
447 >.git/git-daemon-export-ok &&
451 # Local repo with many commits (so that negotiation will take
452 # more than 1 request/response pair)
453 rm -rf "$LOCAL_PRISTINE" &&
454 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
455 cd "$LOCAL_PRISTINE" &&
456 git checkout -b side &&
457 test_commit_bulk --id=s 33 &&
459 # Add novel commits to upstream
466 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
467 git -C "$LOCAL_PRISTINE" config protocol.version 2
471 # Simulate that the server initially reports $2 as the ref
472 # corresponding to $1, and after that, $1 as the ref corresponding to
473 # $1. This corresponds to the real-life situation where the server's
474 # repository appears to change during negotiation, for example, when
475 # different servers in a load-balancing arrangement serve (stateless)
476 # RPCs during a single negotiation.
477 oid1
=$
(git
-C "$REPO" rev-parse
$1) &&
478 oid2
=$
(git
-C "$REPO" rev-parse
$2) &&
479 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
482 test_expect_success
'server is initially ahead - no ref in want' '
483 git -C "$REPO" config uploadpack.allowRefInWant false &&
485 cp -r "$LOCAL_PRISTINE" local &&
486 inconsistency main $(test_oid numeric) &&
487 test_must_fail git -C local fetch 2>err &&
488 test_grep "fatal: remote error: upload-pack: not our ref" err
491 test_expect_success
'server is initially ahead - ref in want' '
492 git -C "$REPO" config uploadpack.allowRefInWant true &&
494 cp -r "$LOCAL_PRISTINE" local &&
495 inconsistency main $(test_oid numeric) &&
496 git -C local fetch &&
498 git -C "$REPO" rev-parse --verify main >expected &&
499 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
500 test_cmp expected actual
503 test_expect_success
'server is initially behind - no ref in want' '
504 git -C "$REPO" config uploadpack.allowRefInWant false &&
506 cp -r "$LOCAL_PRISTINE" local &&
507 inconsistency main "main^" &&
508 git -C local fetch &&
510 git -C "$REPO" rev-parse --verify "main^" >expected &&
511 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
512 test_cmp expected actual
515 test_expect_success
'server is initially behind - ref in want' '
516 git -C "$REPO" config uploadpack.allowRefInWant true &&
518 cp -r "$LOCAL_PRISTINE" local &&
519 inconsistency main "main^" &&
520 git -C local fetch &&
522 git -C "$REPO" rev-parse --verify "main" >expected &&
523 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
524 test_cmp expected actual
527 test_expect_success
'server loses a ref - ref in want' '
528 git -C "$REPO" config uploadpack.allowRefInWant true &&
530 cp -r "$LOCAL_PRISTINE" local &&
531 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
532 test_must_fail git -C local fetch 2>err &&
534 test_grep "fatal: remote error: unknown ref refs/heads/rain" err
537 # DO NOT add non-httpd-specific tests here, because the last part of this
538 # test script is only executed when httpd is available and enabled.