3 test_description
='upload-pack ref-in-want'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 sed -n -e '/wanted-refs/,/0001/{
15 }' <out | test-tool pkt-line unpack
>actual_refs
18 get_actual_commits
() {
19 test-tool pkt-line unpack-sideband
<out
>o.pack
&&
20 git index-pack o.pack
&&
21 git verify-pack
-v o.idx
>objs
&&
22 sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs
>objs.
sed &&
23 sort >actual_commits
<objs.
sed
28 test_cmp expected_refs actual_refs
&&
30 sort expected_commits
>sorted_commits
&&
31 test_cmp sorted_commits actual_commits
37 if test "$(test_oid algo)" != sha1
39 echo "object-format=$(test_oid algo)"
43 # Write a complete fetch command to stdout, suitable for use with `test-tool
44 # pkt-line`. "want-ref", "want", and "have" lines are read from stdin.
48 # write_fetch_command <<-EOF
49 # want-ref refs/heads/main
50 # have $(git rev-parse a)
53 # write_fetch_command <<-EOF
54 # want $(git rev-parse b)
55 # have $(git rev-parse a)
58 write_fetch_command
() {
59 write_command fetch
&&
73 test_expect_success
'setup repository' '
76 git checkout -b o/foo &&
79 git checkout -b o/bar b &&
81 git checkout -b baz a &&
87 test_expect_success
'config controls ref-in-want advertisement' '
88 test-tool serve-v2 --advertise-capabilities >out &&
89 perl -ne "/ref-in-want/ and print" out >out.filter &&
90 test_must_be_empty out.filter &&
92 git config uploadpack.allowRefInWant false &&
93 test-tool serve-v2 --advertise-capabilities >out &&
94 perl -ne "/ref-in-want/ and print" out >out.filter &&
95 test_must_be_empty out.filter &&
97 git config uploadpack.allowRefInWant true &&
98 test-tool serve-v2 --advertise-capabilities >out &&
99 perl -ne "/ref-in-want/ and print" out >out.filter &&
100 test_file_not_empty out.filter
103 test_expect_success
'invalid want-ref line' '
104 write_fetch_command >pkt <<-EOF &&
105 want-ref refs/heads/non-existent
108 test-tool pkt-line pack <pkt >in &&
109 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
110 grep "unknown ref" out
113 test_expect_success
'basic want-ref' '
114 oid=$(git rev-parse f) &&
115 cat >expected_refs <<-EOF &&
118 git rev-parse f >expected_commits &&
120 write_fetch_command >pkt <<-EOF &&
121 want-ref refs/heads/main
122 have $(git rev-parse a)
124 test-tool pkt-line pack <pkt >in &&
126 test-tool serve-v2 --stateless-rpc >out <in &&
130 test_expect_success
'multiple want-ref lines' '
131 oid_c=$(git rev-parse c) &&
132 oid_d=$(git rev-parse d) &&
133 cat >expected_refs <<-EOF &&
134 $oid_c refs/heads/o/foo
135 $oid_d refs/heads/o/bar
137 git rev-parse c d >expected_commits &&
139 write_fetch_command >pkt <<-EOF &&
140 want-ref refs/heads/o/foo
141 want-ref refs/heads/o/bar
142 have $(git rev-parse b)
144 test-tool pkt-line pack <pkt >in &&
146 test-tool serve-v2 --stateless-rpc >out <in &&
150 test_expect_success
'mix want and want-ref' '
151 oid=$(git rev-parse f) &&
152 cat >expected_refs <<-EOF &&
155 git rev-parse e f >expected_commits &&
157 write_fetch_command >pkt <<-EOF &&
158 want-ref refs/heads/main
159 want $(git rev-parse e)
160 have $(git rev-parse a)
162 test-tool pkt-line pack <pkt >in &&
164 test-tool serve-v2 --stateless-rpc >out <in &&
168 test_expect_success
'want-ref with ref we already have commit for' '
169 oid=$(git rev-parse c) &&
170 cat >expected_refs <<-EOF &&
171 $oid refs/heads/o/foo
175 write_fetch_command >pkt <<-EOF &&
176 want-ref refs/heads/o/foo
177 have $(git rev-parse c)
179 test-tool pkt-line pack <pkt >in &&
181 test-tool serve-v2 --stateless-rpc >out <in &&
186 LOCAL_PRISTINE
="$(pwd)/local_pristine"
203 test_expect_success
'setup repos for fetching with ref-in-want tests' '
205 git init -b main "$REPO" &&
209 # Local repo with many commits (so that negotiation will take
210 # more than 1 request/response pair)
211 rm -rf "$LOCAL_PRISTINE" &&
212 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
213 cd "$LOCAL_PRISTINE" &&
214 git checkout -b side &&
215 test_commit_bulk --id=s 33 &&
217 # Add novel commits to upstream
220 git checkout -b o/foo &&
223 git checkout -b o/bar b &&
225 git checkout -b baz a &&
230 git -C "$REPO" config uploadpack.allowRefInWant true &&
231 git -C "$LOCAL_PRISTINE" config protocol.version 2
234 test_expect_success
'fetching with exact OID' '
235 test_when_finished "rm -f log" &&
238 cp -r "$LOCAL_PRISTINE" local &&
239 oid=$(git -C "$REPO" rev-parse d) &&
240 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
241 "$oid":refs/heads/actual &&
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_i18ngrep "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_i18ngrep "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.