Merge branch 'js/var-git-shell-path'
[git/debian.git] / t / t5703-upload-pack-ref-in-want.sh
blob191097171bcbd4d16f107430af4ee4bb4c14493a
1 #!/bin/sh
3 test_description='upload-pack ref-in-want'
5 . ./test-lib.sh
7 get_actual_refs () {
8 sed -n -e '/wanted-refs/,/0001/{
9 /wanted-refs/d
10 /0001/d
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
23 check_output () {
24 get_actual_refs &&
25 test_cmp expected_refs actual_refs &&
26 get_actual_commits &&
27 sort expected_commits >sorted_commits &&
28 test_cmp sorted_commits actual_commits
31 write_command () {
32 echo "command=$1"
34 if test "$(test_oid algo)" != sha1
35 then
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.
43 # Examples:
45 # write_fetch_command <<-EOF
46 # want-ref refs/heads/main
47 # have $(git rev-parse a)
48 # EOF
50 # write_fetch_command <<-EOF
51 # want $(git rev-parse b)
52 # have $(git rev-parse a)
53 # EOF
55 write_fetch_command () {
56 write_command fetch &&
57 echo "0001" &&
58 echo "no-progress" &&
59 cat &&
60 echo "done" &&
61 echo "0000"
64 # c(o/foo) d(o/bar)
65 # \ /
66 # b e(baz) f(main)
67 # \__ | __/
68 # \ | /
69 # a
70 test_expect_success 'setup repository' '
71 test_commit a &&
72 git branch -M main &&
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 &&
80 git checkout main &&
81 test_commit f
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 &&
113 $oid refs/heads/main
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 &&
124 check_output
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 &&
144 check_output
147 test_expect_success 'mix want and want-ref' '
148 oid=$(git rev-parse f) &&
149 cat >expected_refs <<-EOF &&
150 $oid refs/heads/main
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 &&
162 check_output
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
170 >expected_commits &&
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 &&
179 check_output
182 REPO="$(pwd)/repo"
183 LOCAL_PRISTINE="$(pwd)/local_pristine"
185 # $REPO
186 # c(o/foo) d(o/bar)
187 # \ /
188 # b e(baz) f(main)
189 # \__ | __/
190 # \ | /
193 # $LOCAL_PRISTINE
194 # s32(side)
199 # a(main)
200 test_expect_success 'setup repos for fetching with ref-in-want tests' '
202 git init -b main "$REPO" &&
203 cd "$REPO" &&
204 test_commit a &&
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
215 git checkout main &&
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 &&
224 git checkout main &&
225 test_commit f
226 ) &&
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 trace2" &&
234 rm -rf local &&
235 cp -r "$LOCAL_PRISTINE" local &&
236 oid=$(git -C "$REPO" rev-parse d) &&
237 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE2_EVENT="$(pwd)/trace2" \
238 git -C local fetch origin \
239 "$oid":refs/heads/actual &&
241 grep \"key\":\"total_rounds\",\"value\":\"2\" trace2 &&
242 git -C "$REPO" rev-parse "d" >expected &&
243 git -C local rev-parse refs/heads/actual >actual &&
244 test_cmp expected actual &&
245 grep "want $oid" log
248 test_expect_success 'fetching multiple refs' '
249 test_when_finished "rm -f log" &&
251 rm -rf local &&
252 cp -r "$LOCAL_PRISTINE" local &&
253 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
255 git -C "$REPO" rev-parse "main" "baz" >expected &&
256 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
257 test_cmp expected actual &&
258 grep "want-ref refs/heads/main" log &&
259 grep "want-ref refs/heads/baz" log
262 test_expect_success 'fetching ref and exact OID' '
263 test_when_finished "rm -f log" &&
265 rm -rf local &&
266 cp -r "$LOCAL_PRISTINE" local &&
267 oid=$(git -C "$REPO" rev-parse b) &&
268 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
269 main "$oid":refs/heads/actual &&
271 git -C "$REPO" rev-parse "main" "b" >expected &&
272 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
273 test_cmp expected actual &&
274 grep "want $oid" log &&
275 grep "want-ref refs/heads/main" log
278 test_expect_success 'fetching with wildcard that does not match any refs' '
279 test_when_finished "rm -f log" &&
281 rm -rf local &&
282 cp -r "$LOCAL_PRISTINE" local &&
283 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
284 test_must_be_empty out
287 test_expect_success 'fetching with wildcard that matches multiple refs' '
288 test_when_finished "rm -f log" &&
290 rm -rf local &&
291 cp -r "$LOCAL_PRISTINE" local &&
292 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
294 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
295 git -C local rev-parse "o/foo" "o/bar" >actual &&
296 test_cmp expected actual &&
297 grep "want-ref refs/heads/o/foo" log &&
298 grep "want-ref refs/heads/o/bar" log
301 REPO="$(pwd)/repo-ns"
303 test_expect_success 'setup namespaced repo' '
305 git init -b main "$REPO" &&
306 cd "$REPO" &&
307 test_commit a &&
308 test_commit b &&
309 git checkout a &&
310 test_commit c &&
311 git checkout a &&
312 test_commit d &&
313 git update-ref refs/heads/ns-no b &&
314 git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
315 git update-ref refs/namespaces/ns/refs/heads/hidden d
316 ) &&
317 git -C "$REPO" config uploadpack.allowRefInWant true
320 test_expect_success 'with namespace: want-ref is considered relative to namespace' '
321 wanted_ref=refs/heads/ns-yes &&
323 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
324 cat >expected_refs <<-EOF &&
325 $oid $wanted_ref
327 cat >expected_commits <<-EOF &&
328 $oid
329 $(git -C "$REPO" rev-parse a)
332 write_fetch_command >pkt <<-EOF &&
333 want-ref $wanted_ref
335 test-tool pkt-line pack <pkt >in &&
337 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
338 check_output
341 test_expect_success 'with namespace: want-ref outside namespace is unknown' '
342 wanted_ref=refs/heads/ns-no &&
344 write_fetch_command >pkt <<-EOF &&
345 want-ref $wanted_ref
347 test-tool pkt-line pack <pkt >in &&
349 test_must_fail env GIT_NAMESPACE=ns \
350 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
351 grep "unknown ref" out
354 # Cross-check refs/heads/ns-no indeed exists
355 test_expect_success 'without namespace: want-ref outside namespace succeeds' '
356 wanted_ref=refs/heads/ns-no &&
358 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
359 cat >expected_refs <<-EOF &&
360 $oid $wanted_ref
362 cat >expected_commits <<-EOF &&
363 $oid
364 $(git -C "$REPO" rev-parse a)
367 write_fetch_command >pkt <<-EOF &&
368 want-ref $wanted_ref
370 test-tool pkt-line pack <pkt >in &&
372 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
373 check_output
376 test_expect_success 'with namespace: hideRefs is matched, relative to namespace' '
377 wanted_ref=refs/heads/hidden &&
378 git -C "$REPO" config transfer.hideRefs $wanted_ref &&
380 write_fetch_command >pkt <<-EOF &&
381 want-ref $wanted_ref
383 test-tool pkt-line pack <pkt >in &&
385 test_must_fail env GIT_NAMESPACE=ns \
386 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
387 grep "unknown ref" out
390 # Cross-check refs/heads/hidden indeed exists
391 test_expect_success 'with namespace: want-ref succeeds if hideRefs is removed' '
392 wanted_ref=refs/heads/hidden &&
393 git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
395 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
396 cat >expected_refs <<-EOF &&
397 $oid $wanted_ref
399 cat >expected_commits <<-EOF &&
400 $oid
401 $(git -C "$REPO" rev-parse a)
404 write_fetch_command >pkt <<-EOF &&
405 want-ref $wanted_ref
407 test-tool pkt-line pack <pkt >in &&
409 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
410 check_output
413 test_expect_success 'without namespace: relative hideRefs does not match' '
414 wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
415 git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
417 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
418 cat >expected_refs <<-EOF &&
419 $oid $wanted_ref
421 cat >expected_commits <<-EOF &&
422 $oid
423 $(git -C "$REPO" rev-parse a)
426 write_fetch_command >pkt <<-EOF &&
427 want-ref $wanted_ref
429 test-tool pkt-line pack <pkt >in &&
431 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
432 check_output
436 . "$TEST_DIRECTORY"/lib-httpd.sh
437 start_httpd
439 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
440 LOCAL_PRISTINE="$(pwd)/local_pristine"
442 test_expect_success 'setup repos for change-while-negotiating test' '
444 git init -b main "$REPO" &&
445 cd "$REPO" &&
446 >.git/git-daemon-export-ok &&
447 test_commit m1 &&
448 git tag -d m1 &&
450 # Local repo with many commits (so that negotiation will take
451 # more than 1 request/response pair)
452 rm -rf "$LOCAL_PRISTINE" &&
453 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
454 cd "$LOCAL_PRISTINE" &&
455 git checkout -b side &&
456 test_commit_bulk --id=s 33 &&
458 # Add novel commits to upstream
459 git checkout main &&
460 cd "$REPO" &&
461 test_commit m2 &&
462 test_commit m3 &&
463 git tag -d m2 m3
464 ) &&
465 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
466 git -C "$LOCAL_PRISTINE" config protocol.version 2
469 inconsistency () {
470 # Simulate that the server initially reports $2 as the ref
471 # corresponding to $1, and after that, $1 as the ref corresponding to
472 # $1. This corresponds to the real-life situation where the server's
473 # repository appears to change during negotiation, for example, when
474 # different servers in a load-balancing arrangement serve (stateless)
475 # RPCs during a single negotiation.
476 oid1=$(git -C "$REPO" rev-parse $1) &&
477 oid2=$(git -C "$REPO" rev-parse $2) &&
478 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
481 test_expect_success 'server is initially ahead - no ref in want' '
482 git -C "$REPO" config uploadpack.allowRefInWant false &&
483 rm -rf local &&
484 cp -r "$LOCAL_PRISTINE" local &&
485 inconsistency main $(test_oid numeric) &&
486 test_must_fail git -C local fetch 2>err &&
487 test_grep "fatal: remote error: upload-pack: not our ref" err
490 test_expect_success 'server is initially ahead - ref in want' '
491 git -C "$REPO" config uploadpack.allowRefInWant true &&
492 rm -rf local &&
493 cp -r "$LOCAL_PRISTINE" local &&
494 inconsistency main $(test_oid numeric) &&
495 git -C local fetch &&
497 git -C "$REPO" rev-parse --verify main >expected &&
498 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
499 test_cmp expected actual
502 test_expect_success 'server is initially behind - no ref in want' '
503 git -C "$REPO" config uploadpack.allowRefInWant false &&
504 rm -rf local &&
505 cp -r "$LOCAL_PRISTINE" local &&
506 inconsistency main "main^" &&
507 git -C local fetch &&
509 git -C "$REPO" rev-parse --verify "main^" >expected &&
510 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
511 test_cmp expected actual
514 test_expect_success 'server is initially behind - ref in want' '
515 git -C "$REPO" config uploadpack.allowRefInWant true &&
516 rm -rf local &&
517 cp -r "$LOCAL_PRISTINE" local &&
518 inconsistency main "main^" &&
519 git -C local fetch &&
521 git -C "$REPO" rev-parse --verify "main" >expected &&
522 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
523 test_cmp expected actual
526 test_expect_success 'server loses a ref - ref in want' '
527 git -C "$REPO" config uploadpack.allowRefInWant true &&
528 rm -rf local &&
529 cp -r "$LOCAL_PRISTINE" local &&
530 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
531 test_must_fail git -C local fetch 2>err &&
533 test_grep "fatal: remote error: unknown ref refs/heads/rain" err
536 # DO NOT add non-httpd-specific tests here, because the last part of this
537 # test script is only executed when httpd is available and enabled.
539 test_done