Git 2.33.8
[git.git] / t / t5703-upload-pack-ref-in-want.sh
blob220098523a699451cbb3abaaa2850416561b981b
1 #!/bin/sh
3 test_description='upload-pack ref-in-want'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 get_actual_refs () {
11 sed -n -e '/wanted-refs/,/0001/{
12 /wanted-refs/d
13 /0001/d
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
26 check_output () {
27 get_actual_refs &&
28 test_cmp expected_refs actual_refs &&
29 get_actual_commits &&
30 sort expected_commits >sorted_commits &&
31 test_cmp sorted_commits actual_commits
34 write_command () {
35 echo "command=$1"
37 if test "$(test_oid algo)" != sha1
38 then
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.
46 # Examples:
48 # write_fetch_command <<-EOF
49 # want-ref refs/heads/main
50 # have $(git rev-parse a)
51 # EOF
53 # write_fetch_command <<-EOF
54 # want $(git rev-parse b)
55 # have $(git rev-parse a)
56 # EOF
58 write_fetch_command () {
59 write_command fetch &&
60 echo "0001" &&
61 echo "no-progress" &&
62 cat &&
63 echo "done" &&
64 echo "0000"
67 # c(o/foo) d(o/bar)
68 # \ /
69 # b e(baz) f(main)
70 # \__ | __/
71 # \ | /
72 # a
73 test_expect_success 'setup repository' '
74 test_commit a &&
75 git branch -M main &&
76 git checkout -b o/foo &&
77 test_commit b &&
78 test_commit c &&
79 git checkout -b o/bar b &&
80 test_commit d &&
81 git checkout -b baz a &&
82 test_commit e &&
83 git checkout main &&
84 test_commit f
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 &&
116 $oid refs/heads/main
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 &&
127 check_output
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 &&
147 check_output
150 test_expect_success 'mix want and want-ref' '
151 oid=$(git rev-parse f) &&
152 cat >expected_refs <<-EOF &&
153 $oid refs/heads/main
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 &&
165 check_output
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
173 >expected_commits &&
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 &&
182 check_output
185 REPO="$(pwd)/repo"
186 LOCAL_PRISTINE="$(pwd)/local_pristine"
188 # $REPO
189 # c(o/foo) d(o/bar)
190 # \ /
191 # b e(baz) f(main)
192 # \__ | __/
193 # \ | /
196 # $LOCAL_PRISTINE
197 # s32(side)
202 # a(main)
203 test_expect_success 'setup repos for fetching with ref-in-want tests' '
205 git init -b main "$REPO" &&
206 cd "$REPO" &&
207 test_commit a &&
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
218 git checkout main &&
219 cd "$REPO" &&
220 git checkout -b o/foo &&
221 test_commit b &&
222 test_commit c &&
223 git checkout -b o/bar b &&
224 test_commit d &&
225 git checkout -b baz a &&
226 test_commit e &&
227 git checkout main &&
228 test_commit f
229 ) &&
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" &&
237 rm -rf local &&
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 &&
246 grep "want $oid" log
249 test_expect_success 'fetching multiple refs' '
250 test_when_finished "rm -f log" &&
252 rm -rf local &&
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" &&
266 rm -rf local &&
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" &&
282 rm -rf local &&
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" &&
291 rm -rf local &&
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" &&
307 cd "$REPO" &&
308 test_commit a &&
309 test_commit b &&
310 git checkout a &&
311 test_commit c &&
312 git checkout a &&
313 test_commit d &&
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
317 ) &&
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 &&
326 $oid $wanted_ref
328 cat >expected_commits <<-EOF &&
329 $oid
330 $(git -C "$REPO" rev-parse a)
333 write_fetch_command >pkt <<-EOF &&
334 want-ref $wanted_ref
336 test-tool pkt-line pack <pkt >in &&
338 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
339 check_output
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 &&
346 want-ref $wanted_ref
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 &&
361 $oid $wanted_ref
363 cat >expected_commits <<-EOF &&
364 $oid
365 $(git -C "$REPO" rev-parse a)
368 write_fetch_command >pkt <<-EOF &&
369 want-ref $wanted_ref
371 test-tool pkt-line pack <pkt >in &&
373 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
374 check_output
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 &&
382 want-ref $wanted_ref
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 &&
398 $oid $wanted_ref
400 cat >expected_commits <<-EOF &&
401 $oid
402 $(git -C "$REPO" rev-parse a)
405 write_fetch_command >pkt <<-EOF &&
406 want-ref $wanted_ref
408 test-tool pkt-line pack <pkt >in &&
410 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
411 check_output
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 &&
420 $oid $wanted_ref
422 cat >expected_commits <<-EOF &&
423 $oid
424 $(git -C "$REPO" rev-parse a)
427 write_fetch_command >pkt <<-EOF &&
428 want-ref $wanted_ref
430 test-tool pkt-line pack <pkt >in &&
432 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
433 check_output
437 . "$TEST_DIRECTORY"/lib-httpd.sh
438 start_httpd
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" &&
446 cd "$REPO" &&
447 >.git/git-daemon-export-ok &&
448 test_commit m1 &&
449 git tag -d m1 &&
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
460 git checkout main &&
461 cd "$REPO" &&
462 test_commit m2 &&
463 test_commit m3 &&
464 git tag -d m2 m3
465 ) &&
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
470 inconsistency () {
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 &&
484 rm -rf local &&
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 &&
493 rm -rf local &&
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 &&
505 rm -rf local &&
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 &&
517 rm -rf local &&
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 &&
529 rm -rf local &&
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.
540 test_done