3 test_description
='upload-pack ref-in-want'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 sed -n -e '/wanted-refs/,/0001/{
15 }' <out | test-tool pkt-line unpack
>actual_refs
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 grep commit objs | cut
-d" " -f1 |
sort >actual_commits
27 test_cmp expected_refs actual_refs
&&
29 sort expected_commits
>sorted_commits
&&
30 test_cmp sorted_commits actual_commits
36 if test "$(test_oid algo)" != sha1
38 echo "object-format=$(test_oid algo)"
48 test_expect_success
'setup repository' '
51 git checkout -b o/foo &&
54 git checkout -b o/bar b &&
56 git checkout -b baz a &&
62 test_expect_success
'config controls ref-in-want advertisement' '
63 test-tool serve-v2 --advertise-capabilities >out &&
64 perl -ne "/ref-in-want/ and print" out >out.filter &&
65 test_must_be_empty out.filter &&
67 git config uploadpack.allowRefInWant false &&
68 test-tool serve-v2 --advertise-capabilities >out &&
69 perl -ne "/ref-in-want/ and print" out >out.filter &&
70 test_must_be_empty out.filter &&
72 git config uploadpack.allowRefInWant true &&
73 test-tool serve-v2 --advertise-capabilities >out &&
74 perl -ne "/ref-in-want/ and print" out >out.filter &&
75 test_file_not_empty out.filter
78 test_expect_success
'invalid want-ref line' '
79 test-tool pkt-line pack >in <<-EOF &&
80 $(write_command fetch)
83 want-ref refs/heads/non-existent
88 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
89 grep "unknown ref" out
92 test_expect_success
'basic want-ref' '
93 oid=$(git rev-parse f) &&
94 cat >expected_refs <<-EOF &&
97 git rev-parse f >expected_commits &&
99 oid=$(git rev-parse a) &&
100 test-tool pkt-line pack >in <<-EOF &&
101 $(write_command fetch)
104 want-ref refs/heads/main
110 test-tool serve-v2 --stateless-rpc >out <in &&
114 test_expect_success
'multiple want-ref lines' '
115 oid_c=$(git rev-parse c) &&
116 oid_d=$(git rev-parse d) &&
117 cat >expected_refs <<-EOF &&
118 $oid_c refs/heads/o/foo
119 $oid_d refs/heads/o/bar
121 git rev-parse c d >expected_commits &&
123 oid=$(git rev-parse b) &&
124 test-tool pkt-line pack >in <<-EOF &&
125 $(write_command fetch)
128 want-ref refs/heads/o/foo
129 want-ref refs/heads/o/bar
135 test-tool serve-v2 --stateless-rpc >out <in &&
139 test_expect_success
'mix want and want-ref' '
140 oid=$(git rev-parse f) &&
141 cat >expected_refs <<-EOF &&
144 git rev-parse e f >expected_commits &&
146 test-tool pkt-line pack >in <<-EOF &&
147 $(write_command fetch)
150 want-ref refs/heads/main
151 want $(git rev-parse e)
152 have $(git rev-parse a)
157 test-tool serve-v2 --stateless-rpc >out <in &&
161 test_expect_success
'want-ref with ref we already have commit for' '
162 oid=$(git rev-parse c) &&
163 cat >expected_refs <<-EOF &&
164 $oid refs/heads/o/foo
168 oid=$(git rev-parse c) &&
169 test-tool pkt-line pack >in <<-EOF &&
170 $(write_command fetch)
173 want-ref refs/heads/o/foo
179 test-tool serve-v2 --stateless-rpc >out <in &&
184 LOCAL_PRISTINE
="$(pwd)/local_pristine"
201 test_expect_success
'setup repos for fetching with ref-in-want tests' '
203 git init -b main "$REPO" &&
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
218 git checkout -b o/foo &&
221 git checkout -b o/bar b &&
223 git checkout -b baz a &&
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" &&
236 cp -r "$LOCAL_PRISTINE" local &&
237 oid=$(git -C "$REPO" rev-parse d) &&
238 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
239 "$oid":refs/heads/actual &&
241 git -C "$REPO" rev-parse "d" >expected &&
242 git -C local rev-parse refs/heads/actual >actual &&
243 test_cmp expected actual &&
247 test_expect_success
'fetching multiple refs' '
248 test_when_finished "rm -f log" &&
251 cp -r "$LOCAL_PRISTINE" local &&
252 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
254 git -C "$REPO" rev-parse "main" "baz" >expected &&
255 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
256 test_cmp expected actual &&
257 grep "want-ref refs/heads/main" log &&
258 grep "want-ref refs/heads/baz" log
261 test_expect_success
'fetching ref and exact OID' '
262 test_when_finished "rm -f log" &&
265 cp -r "$LOCAL_PRISTINE" local &&
266 oid=$(git -C "$REPO" rev-parse b) &&
267 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
268 main "$oid":refs/heads/actual &&
270 git -C "$REPO" rev-parse "main" "b" >expected &&
271 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
272 test_cmp expected actual &&
273 grep "want $oid" log &&
274 grep "want-ref refs/heads/main" log
277 test_expect_success
'fetching with wildcard that does not match any refs' '
278 test_when_finished "rm -f log" &&
281 cp -r "$LOCAL_PRISTINE" local &&
282 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
283 test_must_be_empty out
286 test_expect_success
'fetching with wildcard that matches multiple refs' '
287 test_when_finished "rm -f log" &&
290 cp -r "$LOCAL_PRISTINE" local &&
291 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
293 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
294 git -C local rev-parse "o/foo" "o/bar" >actual &&
295 test_cmp expected actual &&
296 grep "want-ref refs/heads/o/foo" log &&
297 grep "want-ref refs/heads/o/bar" log
300 .
"$TEST_DIRECTORY"/lib-httpd.sh
303 REPO
="$HTTPD_DOCUMENT_ROOT_PATH/repo"
304 LOCAL_PRISTINE
="$(pwd)/local_pristine"
306 test_expect_success
'setup repos for change-while-negotiating test' '
308 git init -b main "$REPO" &&
310 >.git/git-daemon-export-ok &&
314 # Local repo with many commits (so that negotiation will take
315 # more than 1 request/response pair)
316 rm -rf "$LOCAL_PRISTINE" &&
317 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
318 cd "$LOCAL_PRISTINE" &&
319 git checkout -b side &&
320 test_commit_bulk --id=s 33 &&
322 # Add novel commits to upstream
329 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
330 git -C "$LOCAL_PRISTINE" config protocol.version 2
334 # Simulate that the server initially reports $2 as the ref
335 # corresponding to $1, and after that, $1 as the ref corresponding to
336 # $1. This corresponds to the real-life situation where the server's
337 # repository appears to change during negotiation, for example, when
338 # different servers in a load-balancing arrangement serve (stateless)
339 # RPCs during a single negotiation.
340 oid1
=$
(git
-C "$REPO" rev-parse
$1) &&
341 oid2
=$
(git
-C "$REPO" rev-parse
$2) &&
342 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
345 test_expect_success
'server is initially ahead - no ref in want' '
346 git -C "$REPO" config uploadpack.allowRefInWant false &&
348 cp -r "$LOCAL_PRISTINE" local &&
349 inconsistency main $(test_oid numeric) &&
350 test_must_fail git -C local fetch 2>err &&
351 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
354 test_expect_success
'server is initially ahead - ref in want' '
355 git -C "$REPO" config uploadpack.allowRefInWant true &&
357 cp -r "$LOCAL_PRISTINE" local &&
358 inconsistency main $(test_oid numeric) &&
359 git -C local fetch &&
361 git -C "$REPO" rev-parse --verify main >expected &&
362 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
363 test_cmp expected actual
366 test_expect_success
'server is initially behind - no ref in want' '
367 git -C "$REPO" config uploadpack.allowRefInWant false &&
369 cp -r "$LOCAL_PRISTINE" local &&
370 inconsistency main "main^" &&
371 git -C local fetch &&
373 git -C "$REPO" rev-parse --verify "main^" >expected &&
374 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
375 test_cmp expected actual
378 test_expect_success
'server is initially behind - ref in want' '
379 git -C "$REPO" config uploadpack.allowRefInWant true &&
381 cp -r "$LOCAL_PRISTINE" local &&
382 inconsistency main "main^" &&
383 git -C local fetch &&
385 git -C "$REPO" rev-parse --verify "main" >expected &&
386 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
387 test_cmp expected actual
390 test_expect_success
'server loses a ref - ref in want' '
391 git -C "$REPO" config uploadpack.allowRefInWant true &&
393 cp -r "$LOCAL_PRISTINE" local &&
394 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
395 test_must_fail git -C local fetch 2>err &&
397 test_i18ngrep "fatal: remote error: unknown ref refs/heads/rain" err
400 # DO NOT add non-httpd-specific tests here, because the last part of this
401 # test script is only executed when httpd is available and enabled.