]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5703-upload-pack-ref-in-want.sh
repo-settings: rename the traditional default fetch.negotiationAlgorithm
[thirdparty/git.git] / t / t5703-upload-pack-ref-in-want.sh
1 #!/bin/sh
2
3 test_description='upload-pack ref-in-want'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 get_actual_refs () {
11 sed -n -e '/wanted-refs/,/0001/{
12 /wanted-refs/d
13 /0001/d
14 p
15 }' <out | test-tool pkt-line unpack >actual_refs
16 }
17
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
24 }
25
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
32 }
33
34 write_command () {
35 echo "command=$1"
36
37 if test "$(test_oid algo)" != sha1
38 then
39 echo "object-format=$(test_oid algo)"
40 fi
41 }
42
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.
45 #
46 # Examples:
47 #
48 # write_fetch_command <<-EOF
49 # want-ref refs/heads/main
50 # have $(git rev-parse a)
51 # EOF
52 #
53 # write_fetch_command <<-EOF
54 # want $(git rev-parse b)
55 # have $(git rev-parse a)
56 # EOF
57 #
58 write_fetch_command () {
59 write_command fetch &&
60 echo "0001" &&
61 echo "no-progress" &&
62 cat &&
63 echo "done" &&
64 echo "0000"
65 }
66
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
85 '
86
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 &&
91
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 &&
96
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
101 '
102
103 test_expect_success 'invalid want-ref line' '
104 write_fetch_command >pkt <<-EOF &&
105 want-ref refs/heads/non-existent
106 EOF
107
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
111 '
112
113 test_expect_success 'basic want-ref' '
114 oid=$(git rev-parse f) &&
115 cat >expected_refs <<-EOF &&
116 $oid refs/heads/main
117 EOF
118 git rev-parse f >expected_commits &&
119
120 write_fetch_command >pkt <<-EOF &&
121 want-ref refs/heads/main
122 have $(git rev-parse a)
123 EOF
124 test-tool pkt-line pack <pkt >in &&
125
126 test-tool serve-v2 --stateless-rpc >out <in &&
127 check_output
128 '
129
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
136 EOF
137 git rev-parse c d >expected_commits &&
138
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)
143 EOF
144 test-tool pkt-line pack <pkt >in &&
145
146 test-tool serve-v2 --stateless-rpc >out <in &&
147 check_output
148 '
149
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
154 EOF
155 git rev-parse e f >expected_commits &&
156
157 write_fetch_command >pkt <<-EOF &&
158 want-ref refs/heads/main
159 want $(git rev-parse e)
160 have $(git rev-parse a)
161 EOF
162 test-tool pkt-line pack <pkt >in &&
163
164 test-tool serve-v2 --stateless-rpc >out <in &&
165 check_output
166 '
167
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
172 EOF
173 >expected_commits &&
174
175 write_fetch_command >pkt <<-EOF &&
176 want-ref refs/heads/o/foo
177 have $(git rev-parse c)
178 EOF
179 test-tool pkt-line pack <pkt >in &&
180
181 test-tool serve-v2 --stateless-rpc >out <in &&
182 check_output
183 '
184
185 REPO="$(pwd)/repo"
186 LOCAL_PRISTINE="$(pwd)/local_pristine"
187
188 # $REPO
189 # c(o/foo) d(o/bar)
190 # \ /
191 # b e(baz) f(main)
192 # \__ | __/
193 # \ | /
194 # a
195 #
196 # $LOCAL_PRISTINE
197 # s32(side)
198 # |
199 # .
200 # .
201 # |
202 # a(main)
203 test_expect_success 'setup repos for fetching with ref-in-want tests' '
204 (
205 git init -b main "$REPO" &&
206 cd "$REPO" &&
207 test_commit a &&
208
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 &&
216
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
232 '
233
234 test_expect_success 'fetching with exact OID' '
235 test_when_finished "rm -f log" &&
236
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 &&
242
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
247 '
248
249 test_expect_success 'fetching multiple refs' '
250 test_when_finished "rm -f log" &&
251
252 rm -rf local &&
253 cp -r "$LOCAL_PRISTINE" local &&
254 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
255
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
261 '
262
263 test_expect_success 'fetching ref and exact OID' '
264 test_when_finished "rm -f log" &&
265
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 &&
271
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
277 '
278
279 test_expect_success 'fetching with wildcard that does not match any refs' '
280 test_when_finished "rm -f log" &&
281
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
286 '
287
288 test_expect_success 'fetching with wildcard that matches multiple refs' '
289 test_when_finished "rm -f log" &&
290
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* &&
294
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
300 '
301
302 REPO="$(pwd)/repo-ns"
303
304 test_expect_success 'setup namespaced repo' '
305 (
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
319 '
320
321 test_expect_success 'with namespace: want-ref is considered relative to namespace' '
322 wanted_ref=refs/heads/ns-yes &&
323
324 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
325 cat >expected_refs <<-EOF &&
326 $oid $wanted_ref
327 EOF
328 cat >expected_commits <<-EOF &&
329 $oid
330 $(git -C "$REPO" rev-parse a)
331 EOF
332
333 write_fetch_command >pkt <<-EOF &&
334 want-ref $wanted_ref
335 EOF
336 test-tool pkt-line pack <pkt >in &&
337
338 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
339 check_output
340 '
341
342 test_expect_success 'with namespace: want-ref outside namespace is unknown' '
343 wanted_ref=refs/heads/ns-no &&
344
345 write_fetch_command >pkt <<-EOF &&
346 want-ref $wanted_ref
347 EOF
348 test-tool pkt-line pack <pkt >in &&
349
350 test_must_fail env GIT_NAMESPACE=ns \
351 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
352 grep "unknown ref" out
353 '
354
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 &&
358
359 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
360 cat >expected_refs <<-EOF &&
361 $oid $wanted_ref
362 EOF
363 cat >expected_commits <<-EOF &&
364 $oid
365 $(git -C "$REPO" rev-parse a)
366 EOF
367
368 write_fetch_command >pkt <<-EOF &&
369 want-ref $wanted_ref
370 EOF
371 test-tool pkt-line pack <pkt >in &&
372
373 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
374 check_output
375 '
376
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 &&
380
381 write_fetch_command >pkt <<-EOF &&
382 want-ref $wanted_ref
383 EOF
384 test-tool pkt-line pack <pkt >in &&
385
386 test_must_fail env GIT_NAMESPACE=ns \
387 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
388 grep "unknown ref" out
389 '
390
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 &&
395
396 oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
397 cat >expected_refs <<-EOF &&
398 $oid $wanted_ref
399 EOF
400 cat >expected_commits <<-EOF &&
401 $oid
402 $(git -C "$REPO" rev-parse a)
403 EOF
404
405 write_fetch_command >pkt <<-EOF &&
406 want-ref $wanted_ref
407 EOF
408 test-tool pkt-line pack <pkt >in &&
409
410 GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
411 check_output
412 '
413
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 &&
417
418 oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
419 cat >expected_refs <<-EOF &&
420 $oid $wanted_ref
421 EOF
422 cat >expected_commits <<-EOF &&
423 $oid
424 $(git -C "$REPO" rev-parse a)
425 EOF
426
427 write_fetch_command >pkt <<-EOF &&
428 want-ref $wanted_ref
429 EOF
430 test-tool pkt-line pack <pkt >in &&
431
432 test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
433 check_output
434 '
435
436
437 . "$TEST_DIRECTORY"/lib-httpd.sh
438 start_httpd
439
440 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
441 LOCAL_PRISTINE="$(pwd)/local_pristine"
442
443 test_expect_success 'setup repos for change-while-negotiating test' '
444 (
445 git init -b main "$REPO" &&
446 cd "$REPO" &&
447 >.git/git-daemon-export-ok &&
448 test_commit m1 &&
449 git tag -d m1 &&
450
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 &&
458
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
468 '
469
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"
480 }
481
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
489 '
490
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 &&
497
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
501 '
502
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 &&
509
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
513 '
514
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 &&
521
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
525 '
526
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 &&
533
534 test_i18ngrep "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