3 test_description='upload-pack ref-in-want'
8 sed -n -e '/wanted-refs/,/0001/{
12 }' <out | test-tool pkt-line unpack >actual_refs
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 grep commit objs | cut -d" " -f1 | sort >actual_commits
24 test_cmp expected_refs actual_refs &&
26 sort expected_commits >sorted_commits &&
27 test_cmp sorted_commits actual_commits
33 if test "$(test_oid algo)" != sha1
35 echo "object-format=$(test_oid algo)"
45 test_expect_success 'setup repository' '
47 git checkout -b o/foo &&
50 git checkout -b o/bar b &&
52 git checkout -b baz a &&
54 git checkout master &&
58 test_expect_success 'config controls ref-in-want advertisement' '
59 test-tool serve-v2 --advertise-capabilities >out &&
60 perl -ne "/ref-in-want/ and print" out >out.filter &&
61 test_must_be_empty out.filter &&
63 git config uploadpack.allowRefInWant false &&
64 test-tool serve-v2 --advertise-capabilities >out &&
65 perl -ne "/ref-in-want/ and print" out >out.filter &&
66 test_must_be_empty out.filter &&
68 git config uploadpack.allowRefInWant true &&
69 test-tool serve-v2 --advertise-capabilities >out &&
70 perl -ne "/ref-in-want/ and print" out >out.filter &&
71 test_file_not_empty out.filter
74 test_expect_success 'invalid want-ref line' '
75 test-tool pkt-line pack >in <<-EOF &&
76 $(write_command fetch)
79 want-ref refs/heads/non-existent
84 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
85 grep "unknown ref" out
88 test_expect_success 'basic want-ref' '
89 oid=$(git rev-parse f) &&
90 cat >expected_refs <<-EOF &&
91 $oid refs/heads/master
93 git rev-parse f >expected_commits &&
95 oid=$(git rev-parse a) &&
96 test-tool pkt-line pack >in <<-EOF &&
97 $(write_command fetch)
100 want-ref refs/heads/master
106 test-tool serve-v2 --stateless-rpc >out <in &&
110 test_expect_success 'multiple want-ref lines' '
111 oid_c=$(git rev-parse c) &&
112 oid_d=$(git rev-parse d) &&
113 cat >expected_refs <<-EOF &&
114 $oid_c refs/heads/o/foo
115 $oid_d refs/heads/o/bar
117 git rev-parse c d >expected_commits &&
119 oid=$(git rev-parse b) &&
120 test-tool pkt-line pack >in <<-EOF &&
121 $(write_command fetch)
124 want-ref refs/heads/o/foo
125 want-ref refs/heads/o/bar
131 test-tool serve-v2 --stateless-rpc >out <in &&
135 test_expect_success 'mix want and want-ref' '
136 oid=$(git rev-parse f) &&
137 cat >expected_refs <<-EOF &&
138 $oid refs/heads/master
140 git rev-parse e f >expected_commits &&
142 test-tool pkt-line pack >in <<-EOF &&
143 $(write_command fetch)
146 want-ref refs/heads/master
147 want $(git rev-parse e)
148 have $(git rev-parse a)
153 test-tool serve-v2 --stateless-rpc >out <in &&
157 test_expect_success 'want-ref with ref we already have commit for' '
158 oid=$(git rev-parse c) &&
159 cat >expected_refs <<-EOF &&
160 $oid refs/heads/o/foo
164 oid=$(git rev-parse c) &&
165 test-tool pkt-line pack >in <<-EOF &&
166 $(write_command fetch)
169 want-ref refs/heads/o/foo
175 test-tool serve-v2 --stateless-rpc >out <in &&
180 LOCAL_PRISTINE="$(pwd)/local_pristine"
197 test_expect_success 'setup repos for fetching with ref-in-want tests' '
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 &&
211 # Add novel commits to upstream
212 git checkout master &&
214 git checkout -b o/foo &&
217 git checkout -b o/bar b &&
219 git checkout -b baz a &&
221 git checkout master &&
224 git -C "$REPO" config uploadpack.allowRefInWant true &&
225 git -C "$LOCAL_PRISTINE" config protocol.version 2
228 test_expect_success 'fetching with exact OID' '
229 test_when_finished "rm -f log" &&
232 cp -r "$LOCAL_PRISTINE" local &&
233 oid=$(git -C "$REPO" rev-parse d) &&
234 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
235 "$oid":refs/heads/actual &&
237 git -C "$REPO" rev-parse "d" >expected &&
238 git -C local rev-parse refs/heads/actual >actual &&
239 test_cmp expected actual &&
243 test_expect_success 'fetching multiple refs' '
244 test_when_finished "rm -f log" &&
247 cp -r "$LOCAL_PRISTINE" local &&
248 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin master baz &&
250 git -C "$REPO" rev-parse "master" "baz" >expected &&
251 git -C local rev-parse refs/remotes/origin/master refs/remotes/origin/baz >actual &&
252 test_cmp expected actual &&
253 grep "want-ref refs/heads/master" log &&
254 grep "want-ref refs/heads/baz" log
257 test_expect_success 'fetching ref and exact OID' '
258 test_when_finished "rm -f log" &&
261 cp -r "$LOCAL_PRISTINE" local &&
262 oid=$(git -C "$REPO" rev-parse b) &&
263 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
264 master "$oid":refs/heads/actual &&
266 git -C "$REPO" rev-parse "master" "b" >expected &&
267 git -C local rev-parse refs/remotes/origin/master refs/heads/actual >actual &&
268 test_cmp expected actual &&
269 grep "want $oid" log &&
270 grep "want-ref refs/heads/master" log
273 test_expect_success 'fetching with wildcard that does not match any refs' '
274 test_when_finished "rm -f log" &&
277 cp -r "$LOCAL_PRISTINE" local &&
278 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
279 test_must_be_empty out
282 test_expect_success 'fetching with wildcard that matches multiple refs' '
283 test_when_finished "rm -f log" &&
286 cp -r "$LOCAL_PRISTINE" local &&
287 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
289 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
290 git -C local rev-parse "o/foo" "o/bar" >actual &&
291 test_cmp expected actual &&
292 grep "want-ref refs/heads/o/foo" log &&
293 grep "want-ref refs/heads/o/bar" log
296 . "$TEST_DIRECTORY"/lib-httpd.sh
299 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
300 LOCAL_PRISTINE="$(pwd)/local_pristine"
302 test_expect_success 'setup repos for change-while-negotiating test' '
306 >.git/git-daemon-export-ok &&
310 # Local repo with many commits (so that negotiation will take
311 # more than 1 request/response pair)
312 rm -rf "$LOCAL_PRISTINE" &&
313 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
314 cd "$LOCAL_PRISTINE" &&
315 git checkout -b side &&
316 test_commit_bulk --id=s 33 &&
318 # Add novel commits to upstream
319 git checkout master &&
325 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
326 git -C "$LOCAL_PRISTINE" config protocol.version 2
330 # Simulate that the server initially reports $2 as the ref
331 # corresponding to $1, and after that, $1 as the ref corresponding to
332 # $1. This corresponds to the real-life situation where the server's
333 # repository appears to change during negotiation, for example, when
334 # different servers in a load-balancing arrangement serve (stateless)
335 # RPCs during a single negotiation.
336 oid1=$(git -C "$REPO" rev-parse $1) &&
337 oid2=$(git -C "$REPO" rev-parse $2) &&
338 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
341 test_expect_success 'server is initially ahead - no ref in want' '
342 git -C "$REPO" config uploadpack.allowRefInWant false &&
344 cp -r "$LOCAL_PRISTINE" local &&
345 inconsistency master $(test_oid numeric) &&
346 test_must_fail git -C local fetch 2>err &&
347 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
350 test_expect_success 'server is initially ahead - ref in want' '
351 git -C "$REPO" config uploadpack.allowRefInWant true &&
353 cp -r "$LOCAL_PRISTINE" local &&
354 inconsistency master $(test_oid numeric) &&
355 git -C local fetch &&
357 git -C "$REPO" rev-parse --verify master >expected &&
358 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
359 test_cmp expected actual
362 test_expect_success 'server is initially behind - no ref in want' '
363 git -C "$REPO" config uploadpack.allowRefInWant false &&
365 cp -r "$LOCAL_PRISTINE" local &&
366 inconsistency master "master^" &&
367 git -C local fetch &&
369 git -C "$REPO" rev-parse --verify "master^" >expected &&
370 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
371 test_cmp expected actual
374 test_expect_success 'server is initially behind - ref in want' '
375 git -C "$REPO" config uploadpack.allowRefInWant true &&
377 cp -r "$LOCAL_PRISTINE" local &&
378 inconsistency master "master^" &&
379 git -C local fetch &&
381 git -C "$REPO" rev-parse --verify "master" >expected &&
382 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
383 test_cmp expected actual
386 test_expect_success 'server loses a ref - ref in want' '
387 git -C "$REPO" config uploadpack.allowRefInWant true &&
389 cp -r "$LOCAL_PRISTINE" local &&
390 echo "s/master/raster/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
391 test_must_fail git -C local fetch 2>err &&
393 test_i18ngrep "fatal: remote error: unknown ref refs/heads/raster" err
396 # DO NOT add non-httpd-specific tests here, because the last part of this
397 # test script is only executed when httpd is available and enabled.