The eighth batch
[git/gitster.git] / t / t5703-upload-pack-ref-in-want.sh
blobf75fae52c83d8b2fb300a1a323b11806fc7c3ecc
1 #!/bin/sh
3 test_description='upload-pack ref-in-want'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 get_actual_refs () {
9 sed -n -e '/wanted-refs/,/0001/{
10 /wanted-refs/d
11 /0001/d
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
24 check_output () {
25 get_actual_refs &&
26 test_cmp expected_refs actual_refs &&
27 get_actual_commits &&
28 sort expected_commits >sorted_commits &&
29 test_cmp sorted_commits actual_commits
32 write_command () {
33 echo "command=$1"
35 if test "$(test_oid algo)" != sha1
36 then
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.
44 # Examples:
46 # write_fetch_command <<-EOF
47 # want-ref refs/heads/main
48 # have $(git rev-parse a)
49 # EOF
51 # write_fetch_command <<-EOF
52 # want $(git rev-parse b)
53 # have $(git rev-parse a)
54 # EOF
56 write_fetch_command () {
57 write_command fetch &&
58 echo "0001" &&
59 echo "no-progress" &&
60 cat &&
61 echo "done" &&
62 echo "0000"
65 # c(o/foo) d(o/bar)
66 # \ /
67 # b e(baz) f(main)
68 # \__ | __/
69 # \ | /
70 # a
71 test_expect_success 'setup repository' '
72 test_commit a &&
73 git branch -M main &&
74 git checkout -b o/foo &&
75 test_commit b &&
76 test_commit c &&
77 git checkout -b o/bar b &&
78 test_commit d &&
79 git checkout -b baz a &&
80 test_commit e &&
81 git checkout main &&
82 test_commit f
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 &&
114 $oid refs/heads/main
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 &&
125 check_output
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 &&
145 check_output
148 test_expect_success 'mix want and want-ref' '
149 oid=$(git rev-parse f) &&
150 cat >expected_refs <<-EOF &&
151 $oid refs/heads/main
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 &&
163 check_output
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
171 >expected_commits &&
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 &&
180 check_output
183 REPO="$(pwd)/repo"
184 LOCAL_PRISTINE="$(pwd)/local_pristine"
186 # $REPO
187 # c(o/foo) d(o/bar)
188 # \ /
189 # b e(baz) f(main)
190 # \__ | __/
191 # \ | /
194 # $LOCAL_PRISTINE
195 # s32(side)
200 # a(main)
201 test_expect_success 'setup repos for fetching with ref-in-want tests' '
203 git init -b main "$REPO" &&
204 cd "$REPO" &&
205 test_commit a &&
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
216 git checkout main &&
217 cd "$REPO" &&
218 git checkout -b o/foo &&
219 test_commit b &&
220 test_commit c &&
221 git checkout -b o/bar b &&
222 test_commit d &&
223 git checkout -b baz a &&
224 test_commit e &&
225 git checkout main &&
226 test_commit f
227 ) &&
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" &&
235 rm -rf local &&
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 &&
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_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 &&
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_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.
540 test_done