interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t5703-upload-pack-ref-in-want.sh

558 lines · 14877 bytes Raw Blame History
1#!/bin/sh
2
3test_description='upload-pack ref-in-want'
4
5. ./test-lib.sh
6
7get_actual_refs () {
8 sed -n -e '/wanted-refs/,/0001/{
9 /wanted-refs/d
10 /0001/d
11 p
12 }' <out | test-tool pkt-line unpack >actual_refs
13}
14
15get_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
21}
22
23check_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
29}
30
31write_command () {
32 echo "command=$1"
33
34 if test "$(test_oid algo)" != sha1
35 then
36 echo "object-format=$(test_oid algo)"
37 fi
38}
39
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.
42#
43# Examples:
44#
45# write_fetch_command <<-EOF
46# want-ref refs/heads/main
47# have $(git rev-parse a)
48# EOF
49#
50# write_fetch_command <<-EOF
51# want $(git rev-parse b)
52# have $(git rev-parse a)
53# EOF
54#
55write_fetch_command () {
56 write_command fetch &&
57 echo "0001" &&
58 echo "no-progress" &&
59 cat &&
60 echo "done" &&
61 echo "0000"
62}
63
64# c(o/foo) d(o/bar)
65# \ /
66# b e(baz) f(main)
67# \__ | __/
68# \ | /
69# a
70test_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
82'
83
84test_expect_success 'config controls ref-in-want advertisement' '
85 test-tool serve-v2 --advertise-capabilities >out &&
86 test_grep ! "ref-in-want" out &&
87
88 git config uploadpack.allowRefInWant false &&
89 test-tool serve-v2 --advertise-capabilities >out &&
90 test_grep ! "ref-in-want" out &&
91
92 git config uploadpack.allowRefInWant true &&
93 test-tool serve-v2 --advertise-capabilities >out &&
94 test_grep "ref-in-want" out
95'
96
97test_expect_success 'invalid want-ref line' '
98 write_fetch_command >pkt <<-EOF &&
99 want-ref refs/heads/non-existent
100 EOF
101
102 test-tool pkt-line pack <pkt >in &&
103 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
104 test_grep "unknown ref" out
105'
106
107test_expect_success 'basic want-ref' '
108 oid=$(git rev-parse f) &&
109 cat >expected_refs <<-EOF &&
110 $oid refs/heads/main
111 EOF
112 git rev-parse f >expected_commits &&
113
114 write_fetch_command >pkt <<-EOF &&
115 want-ref refs/heads/main
116 have $(git rev-parse a)
117 EOF
118 test-tool pkt-line pack <pkt >in &&
119
120 test-tool serve-v2 --stateless-rpc >out <in &&
121 check_output
122'
123
124test_expect_success 'multiple want-ref lines' '
125 oid_c=$(git rev-parse c) &&
126 oid_d=$(git rev-parse d) &&
127 cat >expected_refs <<-EOF &&
128 $oid_c refs/heads/o/foo
129 $oid_d refs/heads/o/bar
130 EOF
131 git rev-parse c d >expected_commits &&
132
133 write_fetch_command >pkt <<-EOF &&
134 want-ref refs/heads/o/foo
135 want-ref refs/heads/o/bar
136 have $(git rev-parse b)
137 EOF
138 test-tool pkt-line pack <pkt >in &&
139
140 test-tool serve-v2 --stateless-rpc >out <in &&
141 check_output
142'
143
144test_expect_success 'mix want and want-ref' '
145 oid=$(git rev-parse f) &&
146 cat >expected_refs <<-EOF &&
147 $oid refs/heads/main
148 EOF
149 git rev-parse e f >expected_commits &&
150
151 write_fetch_command >pkt <<-EOF &&
152 want-ref refs/heads/main
153 want $(git rev-parse e)
154 have $(git rev-parse a)
155 EOF
156 test-tool pkt-line pack <pkt >in &&
157
158 test-tool serve-v2 --stateless-rpc >out <in &&
159 check_output
160'
161
162test_expect_success 'want-ref with ref we already have commit for' '
163 oid=$(git rev-parse c) &&
164 cat >expected_refs <<-EOF &&
165 $oid refs/heads/o/foo
166 EOF
167 >expected_commits &&
168
169 write_fetch_command >pkt <<-EOF &&
170 want-ref refs/heads/o/foo
171 have $(git rev-parse c)
172 EOF
173 test-tool pkt-line pack <pkt >in &&
174
175 test-tool serve-v2 --stateless-rpc >out <in &&
176 check_output
177'
178
179REPO="$(pwd)/repo"
180LOCAL_PRISTINE="$(pwd)/local_pristine"
181
182# $REPO
183# c(o/foo) d(o/bar)
184# \ /
185# b e(baz) f(main)
186# \__ | __/
187# \ | /
188# a
189#
190# $LOCAL_PRISTINE
191# s32(side)
192# |
193# .
194# .
195# |
196# a(main)
197test_expect_success 'setup repos for fetching with ref-in-want tests' '
198 (
199 git init -b main "$REPO" &&
200 cd "$REPO" &&
201 test_commit a &&
202
203 # Local repo with many commits (so that negotiation will take
204 # more than 1 request/response pair)
205 rm -rf "$LOCAL_PRISTINE" &&
206 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
207 cd "$LOCAL_PRISTINE" &&
208 git checkout -b side &&
209 test_commit_bulk --id=s 33 &&
210
211 # Add novel commits to upstream
212 git checkout main &&
213 cd "$REPO" &&
214 git checkout -b o/foo &&
215 test_commit b &&
216 test_commit c &&
217 git checkout -b o/bar b &&
218 test_commit d &&
219 git checkout -b baz a &&
220 test_commit e &&
221 git checkout main &&
222 test_commit f
223 ) &&
224 git -C "$REPO" config uploadpack.allowRefInWant true &&
225 git -C "$LOCAL_PRISTINE" config protocol.version 2
226'
227
228test_expect_success 'fetching with exact OID' '
229 test_when_finished "rm -f log trace2" &&
230
231 rm -rf local &&
232 cp -r "$LOCAL_PRISTINE" local &&
233 oid=$(git -C "$REPO" rev-parse d) &&
234 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE2_EVENT="$(pwd)/trace2" \
235 git -C local fetch origin \
236 "$oid":refs/heads/actual &&
237
238 test_grep \"key\":\"total_rounds\",\"value\":\"2\" trace2 &&
239 git -C "$REPO" rev-parse "d" >expected &&
240 git -C local rev-parse refs/heads/actual >actual &&
241 test_cmp expected actual &&
242 test_grep "want $oid" log
243'
244
245test_expect_success 'fetching multiple refs' '
246 test_when_finished "rm -f log" &&
247
248 rm -rf local &&
249 cp -r "$LOCAL_PRISTINE" local &&
250 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
251
252 git -C "$REPO" rev-parse "main" "baz" >expected &&
253 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
254 test_cmp expected actual &&
255 test_grep "want-ref refs/heads/main" log &&
256 test_grep "want-ref refs/heads/baz" log
257'
258
259test_expect_success 'fetching ref and exact OID' '
260 test_when_finished "rm -f log" &&
261
262 rm -rf local &&
263 cp -r "$LOCAL_PRISTINE" local &&
264 oid=$(git -C "$REPO" rev-parse b) &&
265 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
266 main "$oid":refs/heads/actual &&
267
268 git -C "$REPO" rev-parse "main" "b" >expected &&
269 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
270 test_cmp expected actual &&
271 test_grep "want $oid" log &&
272 test_grep "want-ref refs/heads/main" log
273'
274
275test_expect_success 'fetching with wildcard that does not match any refs' '
276 test_when_finished "rm -f log" &&
277
278 rm -rf local &&
279 cp -r "$LOCAL_PRISTINE" local &&
280 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
281 test_must_be_empty out
282'
283
284test_expect_success 'fetching with wildcard that matches multiple refs' '
285 test_when_finished "rm -f log" &&
286
287 rm -rf local &&
288 cp -r "$LOCAL_PRISTINE" local &&
289 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
290
291 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
292 git -C local rev-parse "o/foo" "o/bar" >actual &&
293 test_cmp expected actual &&
294 test_grep "want-ref refs/heads/o/foo" log &&
295 test_grep "want-ref refs/heads/o/bar" log
296'
297
298test_expect_success 'shallow clone with ref-in-want' '
299 rm -rf local &&
300 GIT_TEST_PROTOCOL_VERSION=2 git clone --depth=1 "file://$REPO" local &&
301 git -C "$REPO" rev-parse main >expected &&
302 git -C local rev-parse refs/remotes/origin/main >actual &&
303 test_cmp expected actual &&
304 git -C local log --oneline refs/remotes/origin/main >log &&
305 test_line_count = 1 log
306'
307
308test_expect_success 'incremental shallow fetch with ref-in-want' '
309 rm -rf local &&
310 GIT_TEST_PROTOCOL_VERSION=2 git clone --depth=1 "file://$REPO" local &&
311 GIT_TEST_PROTOCOL_VERSION=2 git -C local fetch --depth=2 origin main &&
312 git -C local log --oneline refs/remotes/origin/main >log &&
313 test_line_count = 2 log
314'
315
316REPO="$(pwd)/repo-ns"
317
318test_expect_success 'setup namespaced repo' '
319 (
320 git init -b main "$REPO" &&
321 cd "$REPO" &&
322 test_commit a &&
323 test_commit b &&
324 git checkout a &&
325 test_commit c &&
326 git checkout a &&
327 test_commit d &&
328 git update-ref refs/heads/ns-no b &&
329 git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
330 git update-ref refs/namespaces/ns/refs/heads/hidden d
331 ) &&
332 git -C "$REPO" config uploadpack.allowRefInWant true
333'
334
335test_expect_success 'with namespace: want-ref is considered relative to namespace' '
336 wanted_ref=refs/heads/ns-yes &&
337
338 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
339 cat >expected_refs <<-EOF &&
340 $oid $wanted_ref
341 EOF
342 cat >expected_commits <<-EOF &&
343 $oid
344 $(git -C "$REPO" rev-parse a)
345 EOF
346
347 write_fetch_command >pkt <<-EOF &&
348 want-ref $wanted_ref
349 EOF
350 test-tool pkt-line pack <pkt >in &&
351
352 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
353 check_output
354'
355
356test_expect_success 'with namespace: want-ref outside namespace is unknown' '
357 wanted_ref=refs/heads/ns-no &&
358
359 write_fetch_command >pkt <<-EOF &&
360 want-ref $wanted_ref
361 EOF
362 test-tool pkt-line pack <pkt >in &&
363
364 test_must_fail env GIT_NAMESPACE=ns \
365 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
366 test_grep "unknown ref" out
367'
368
369# Cross-check refs/heads/ns-no indeed exists
370test_expect_success 'without namespace: want-ref outside namespace succeeds' '
371 wanted_ref=refs/heads/ns-no &&
372
373 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
374 cat >expected_refs <<-EOF &&
375 $oid $wanted_ref
376 EOF
377 cat >expected_commits <<-EOF &&
378 $oid
379 $(git -C "$REPO" rev-parse a)
380 EOF
381
382 write_fetch_command >pkt <<-EOF &&
383 want-ref $wanted_ref
384 EOF
385 test-tool pkt-line pack <pkt >in &&
386
387 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
388 check_output
389'
390
391test_expect_success 'with namespace: hideRefs is matched, relative to namespace' '
392 wanted_ref=refs/heads/hidden &&
393 git -C "$REPO" config transfer.hideRefs $wanted_ref &&
394
395 write_fetch_command >pkt <<-EOF &&
396 want-ref $wanted_ref
397 EOF
398 test-tool pkt-line pack <pkt >in &&
399
400 test_must_fail env GIT_NAMESPACE=ns \
401 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
402 test_grep "unknown ref" out
403'
404
405# Cross-check refs/heads/hidden indeed exists
406test_expect_success 'with namespace: want-ref succeeds if hideRefs is removed' '
407 wanted_ref=refs/heads/hidden &&
408 git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
409
410 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
411 cat >expected_refs <<-EOF &&
412 $oid $wanted_ref
413 EOF
414 cat >expected_commits <<-EOF &&
415 $oid
416 $(git -C "$REPO" rev-parse a)
417 EOF
418
419 write_fetch_command >pkt <<-EOF &&
420 want-ref $wanted_ref
421 EOF
422 test-tool pkt-line pack <pkt >in &&
423
424 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
425 check_output
426'
427
428test_expect_success 'without namespace: relative hideRefs does not match' '
429 wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
430 git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
431
432 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
433 cat >expected_refs <<-EOF &&
434 $oid $wanted_ref
435 EOF
436 cat >expected_commits <<-EOF &&
437 $oid
438 $(git -C "$REPO" rev-parse a)
439 EOF
440
441 write_fetch_command >pkt <<-EOF &&
442 want-ref $wanted_ref
443 EOF
444 test-tool pkt-line pack <pkt >in &&
445
446 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
447 check_output
448'
449
450
451. "$TEST_DIRECTORY"/lib-httpd.sh
452start_httpd
453
454REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
455LOCAL_PRISTINE="$(pwd)/local_pristine"
456
457test_expect_success 'setup repos for change-while-negotiating test' '
458 (
459 git init -b main "$REPO" &&
460 cd "$REPO" &&
461 >.git/git-daemon-export-ok &&
462 test_commit m1 &&
463 git tag -d m1 &&
464
465 # Local repo with many commits (so that negotiation will take
466 # more than 1 request/response pair)
467 rm -rf "$LOCAL_PRISTINE" &&
468 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
469 cd "$LOCAL_PRISTINE" &&
470 git checkout -b side &&
471 test_commit_bulk --id=s 33 &&
472
473 # Add novel commits to upstream
474 git checkout main &&
475 cd "$REPO" &&
476 test_commit m2 &&
477 test_commit m3 &&
478 git tag -d m2 m3
479 ) &&
480 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_script/repo" &&
481 git -C "$LOCAL_PRISTINE" config protocol.version 2
482'
483
484inconsistency () {
485 # Simulate that the server initially reports $2 as the ref
486 # corresponding to $1, and after that, $1 as the ref corresponding to
487 # $1. This corresponds to the real-life situation where the server's
488 # repository appears to change during negotiation, for example, when
489 # different servers in a load-balancing arrangement serve (stateless)
490 # RPCs during a single negotiation.
491 oid1=$(git -C "$REPO" rev-parse $1) &&
492 oid2=$(git -C "$REPO" rev-parse $2) &&
493 write_script "$HTTPD_ROOT_PATH/one-time-script" <<-EOF
494 sed "s/$oid1/$oid2/" "\$1"
495 EOF
496}
497
498test_expect_success PERL_TEST_HELPERS 'server is initially ahead - no ref in want' '
499 git -C "$REPO" config uploadpack.allowRefInWant false &&
500 rm -rf local &&
501 cp -r "$LOCAL_PRISTINE" local &&
502 inconsistency main $(test_oid numeric) &&
503 test_must_fail git -C local fetch 2>err &&
504 test_grep "fatal: remote error: upload-pack: not our ref" err
505'
506
507test_expect_success PERL_TEST_HELPERS 'server is initially ahead - ref in want' '
508 git -C "$REPO" config uploadpack.allowRefInWant true &&
509 rm -rf local &&
510 cp -r "$LOCAL_PRISTINE" local &&
511 inconsistency main $(test_oid numeric) &&
512 git -C local fetch &&
513
514 git -C "$REPO" rev-parse --verify main >expected &&
515 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
516 test_cmp expected actual
517'
518
519test_expect_success PERL_TEST_HELPERS 'server is initially behind - no ref in want' '
520 git -C "$REPO" config uploadpack.allowRefInWant false &&
521 rm -rf local &&
522 cp -r "$LOCAL_PRISTINE" local &&
523 inconsistency main "main^" &&
524 git -C local fetch &&
525
526 git -C "$REPO" rev-parse --verify "main^" >expected &&
527 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
528 test_cmp expected actual
529'
530
531test_expect_success PERL_TEST_HELPERS 'server is initially behind - ref in want' '
532 git -C "$REPO" config uploadpack.allowRefInWant true &&
533 rm -rf local &&
534 cp -r "$LOCAL_PRISTINE" local &&
535 inconsistency main "main^" &&
536 git -C local fetch &&
537
538 git -C "$REPO" rev-parse --verify "main" >expected &&
539 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
540 test_cmp expected actual
541'
542
543test_expect_success PERL_TEST_HELPERS 'server loses a ref - ref in want' '
544 git -C "$REPO" config uploadpack.allowRefInWant true &&
545 rm -rf local &&
546 cp -r "$LOCAL_PRISTINE" local &&
547 write_script "$HTTPD_ROOT_PATH/one-time-script" <<-\EOF &&
548 sed "s/main/rain/" "$1"
549 EOF
550 test_must_fail git -C local fetch 2>err &&
551
552 test_grep "fatal: remote error: unknown ref refs/heads/rain" err
553'
554
555# DO NOT add non-httpd-specific tests here, because the last part of this
556# test script is only executed when httpd is available and enabled.
557
558test_done