Raw
1 #!/bin/sh
2
3 test_description='upload-pack ref-in-want'
4
5 . ./test-lib.sh
6
7 get_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
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
21 }
22
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
29 }
30
31 write_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 #
55 write_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
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
82 '
83
84 test_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
97 test_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 grep "unknown ref" out
105 '
106
107 test_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
124 test_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
144 test_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
162 test_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
179 REPO="$(pwd)/repo"
180 LOCAL_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)
197 test_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
228 test_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 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 grep "want $oid" log
243 '
244
245 test_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 grep "want-ref refs/heads/main" log &&
256 grep "want-ref refs/heads/baz" log
257 '
258
259 test_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 grep "want $oid" log &&
272 grep "want-ref refs/heads/main" log
273 '
274
275 test_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
284 test_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 grep "want-ref refs/heads/o/foo" log &&
295 grep "want-ref refs/heads/o/bar" log
296 '
297
298 REPO="$(pwd)/repo-ns"
299
300 test_expect_success 'setup namespaced repo' '
301 (
302 git init -b main "$REPO" &&
303 cd "$REPO" &&
304 test_commit a &&
305 test_commit b &&
306 git checkout a &&
307 test_commit c &&
308 git checkout a &&
309 test_commit d &&
310 git update-ref refs/heads/ns-no b &&
311 git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
312 git update-ref refs/namespaces/ns/refs/heads/hidden d
313 ) &&
314 git -C "$REPO" config uploadpack.allowRefInWant true
315 '
316
317 test_expect_success 'with namespace: want-ref is considered relative to namespace' '
318 wanted_ref=refs/heads/ns-yes &&
319
320 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
321 cat >expected_refs <<-EOF &&
322 $oid $wanted_ref
323 EOF
324 cat >expected_commits <<-EOF &&
325 $oid
326 $(git -C "$REPO" rev-parse a)
327 EOF
328
329 write_fetch_command >pkt <<-EOF &&
330 want-ref $wanted_ref
331 EOF
332 test-tool pkt-line pack <pkt >in &&
333
334 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
335 check_output
336 '
337
338 test_expect_success 'with namespace: want-ref outside namespace is unknown' '
339 wanted_ref=refs/heads/ns-no &&
340
341 write_fetch_command >pkt <<-EOF &&
342 want-ref $wanted_ref
343 EOF
344 test-tool pkt-line pack <pkt >in &&
345
346 test_must_fail env GIT_NAMESPACE=ns \
347 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
348 grep "unknown ref" out
349 '
350
351 # Cross-check refs/heads/ns-no indeed exists
352 test_expect_success 'without namespace: want-ref outside namespace succeeds' '
353 wanted_ref=refs/heads/ns-no &&
354
355 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
356 cat >expected_refs <<-EOF &&
357 $oid $wanted_ref
358 EOF
359 cat >expected_commits <<-EOF &&
360 $oid
361 $(git -C "$REPO" rev-parse a)
362 EOF
363
364 write_fetch_command >pkt <<-EOF &&
365 want-ref $wanted_ref
366 EOF
367 test-tool pkt-line pack <pkt >in &&
368
369 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
370 check_output
371 '
372
373 test_expect_success 'with namespace: hideRefs is matched, relative to namespace' '
374 wanted_ref=refs/heads/hidden &&
375 git -C "$REPO" config transfer.hideRefs $wanted_ref &&
376
377 write_fetch_command >pkt <<-EOF &&
378 want-ref $wanted_ref
379 EOF
380 test-tool pkt-line pack <pkt >in &&
381
382 test_must_fail env GIT_NAMESPACE=ns \
383 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
384 grep "unknown ref" out
385 '
386
387 # Cross-check refs/heads/hidden indeed exists
388 test_expect_success 'with namespace: want-ref succeeds if hideRefs is removed' '
389 wanted_ref=refs/heads/hidden &&
390 git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
391
392 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
393 cat >expected_refs <<-EOF &&
394 $oid $wanted_ref
395 EOF
396 cat >expected_commits <<-EOF &&
397 $oid
398 $(git -C "$REPO" rev-parse a)
399 EOF
400
401 write_fetch_command >pkt <<-EOF &&
402 want-ref $wanted_ref
403 EOF
404 test-tool pkt-line pack <pkt >in &&
405
406 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
407 check_output
408 '
409
410 test_expect_success 'without namespace: relative hideRefs does not match' '
411 wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
412 git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
413
414 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
415 cat >expected_refs <<-EOF &&
416 $oid $wanted_ref
417 EOF
418 cat >expected_commits <<-EOF &&
419 $oid
420 $(git -C "$REPO" rev-parse a)
421 EOF
422
423 write_fetch_command >pkt <<-EOF &&
424 want-ref $wanted_ref
425 EOF
426 test-tool pkt-line pack <pkt >in &&
427
428 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
429 check_output
430 '
431
432
433 . "$TEST_DIRECTORY"/lib-httpd.sh
434 start_httpd
435
436 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
437 LOCAL_PRISTINE="$(pwd)/local_pristine"
438
439 test_expect_success 'setup repos for change-while-negotiating test' '
440 (
441 git init -b main "$REPO" &&
442 cd "$REPO" &&
443 >.git/git-daemon-export-ok &&
444 test_commit m1 &&
445 git tag -d m1 &&
446
447 # Local repo with many commits (so that negotiation will take
448 # more than 1 request/response pair)
449 rm -rf "$LOCAL_PRISTINE" &&
450 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
451 cd "$LOCAL_PRISTINE" &&
452 git checkout -b side &&
453 test_commit_bulk --id=s 33 &&
454
455 # Add novel commits to upstream
456 git checkout main &&
457 cd "$REPO" &&
458 test_commit m2 &&
459 test_commit m3 &&
460 git tag -d m2 m3
461 ) &&
462 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_script/repo" &&
463 git -C "$LOCAL_PRISTINE" config protocol.version 2
464 '
465
466 inconsistency () {
467 # Simulate that the server initially reports $2 as the ref
468 # corresponding to $1, and after that, $1 as the ref corresponding to
469 # $1. This corresponds to the real-life situation where the server's
470 # repository appears to change during negotiation, for example, when
471 # different servers in a load-balancing arrangement serve (stateless)
472 # RPCs during a single negotiation.
473 oid1=$(git -C "$REPO" rev-parse $1) &&
474 oid2=$(git -C "$REPO" rev-parse $2) &&
475 write_script "$HTTPD_ROOT_PATH/one-time-script" <<-EOF
476 sed "s/$oid1/$oid2/" "\$1"
477 EOF
478 }
479
480 test_expect_success PERL_TEST_HELPERS 'server is initially ahead - no ref in want' '
481 git -C "$REPO" config uploadpack.allowRefInWant false &&
482 rm -rf local &&
483 cp -r "$LOCAL_PRISTINE" local &&
484 inconsistency main $(test_oid numeric) &&
485 test_must_fail git -C local fetch 2>err &&
486 test_grep "fatal: remote error: upload-pack: not our ref" err
487 '
488
489 test_expect_success PERL_TEST_HELPERS 'server is initially ahead - ref in want' '
490 git -C "$REPO" config uploadpack.allowRefInWant true &&
491 rm -rf local &&
492 cp -r "$LOCAL_PRISTINE" local &&
493 inconsistency main $(test_oid numeric) &&
494 git -C local fetch &&
495
496 git -C "$REPO" rev-parse --verify main >expected &&
497 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
498 test_cmp expected actual
499 '
500
501 test_expect_success PERL_TEST_HELPERS 'server is initially behind - no ref in want' '
502 git -C "$REPO" config uploadpack.allowRefInWant false &&
503 rm -rf local &&
504 cp -r "$LOCAL_PRISTINE" local &&
505 inconsistency main "main^" &&
506 git -C local fetch &&
507
508 git -C "$REPO" rev-parse --verify "main^" >expected &&
509 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
510 test_cmp expected actual
511 '
512
513 test_expect_success PERL_TEST_HELPERS 'server is initially behind - ref in want' '
514 git -C "$REPO" config uploadpack.allowRefInWant true &&
515 rm -rf local &&
516 cp -r "$LOCAL_PRISTINE" local &&
517 inconsistency main "main^" &&
518 git -C local fetch &&
519
520 git -C "$REPO" rev-parse --verify "main" >expected &&
521 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
522 test_cmp expected actual
523 '
524
525 test_expect_success PERL_TEST_HELPERS 'server loses a ref - ref in want' '
526 git -C "$REPO" config uploadpack.allowRefInWant true &&
527 rm -rf local &&
528 cp -r "$LOCAL_PRISTINE" local &&
529 write_script "$HTTPD_ROOT_PATH/one-time-script" <<-\EOF &&
530 sed "s/main/rain/" "$1"
531 EOF
532 test_must_fail git -C local fetch 2>err &&
533
534 test_grep "fatal: remote error: unknown ref refs/heads/rain" err
535 '
536
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.
539
540 test_done