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 sed -n -e '/packfile/,/0000/{
19 }' <out | test-tool pkt-line unpack-sideband >o.pack &&
20 git index-pack o.pack &&
21 git verify-pack -v o.idx | grep commit | cut -c-40 | sort >actual_commits
26 test_cmp expected_refs actual_refs &&
28 test_cmp expected_commits actual_commits
37 test_expect_success 'setup repository' '
39 git checkout -b o/foo &&
42 git checkout -b o/bar b &&
44 git checkout -b baz a &&
46 git checkout master &&
50 test_expect_success 'config controls ref-in-want advertisement' '
51 test-tool serve-v2 --advertise-capabilities >out &&
52 ! grep -a ref-in-want out &&
54 git config uploadpack.allowRefInWant false &&
55 test-tool serve-v2 --advertise-capabilities >out &&
56 ! grep -a ref-in-want out &&
58 git config uploadpack.allowRefInWant true &&
59 test-tool serve-v2 --advertise-capabilities >out &&
60 grep -a ref-in-want out
63 test_expect_success 'invalid want-ref line' '
64 test-tool pkt-line pack >in <<-EOF &&
68 want-ref refs/heads/non-existent
73 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
74 grep "unknown ref" out
77 test_expect_success 'basic want-ref' '
78 cat >expected_refs <<-EOF &&
79 $(git rev-parse f) refs/heads/master
81 git rev-parse f | sort >expected_commits &&
83 test-tool pkt-line pack >in <<-EOF &&
87 want-ref refs/heads/master
88 have $(git rev-parse a)
93 test-tool serve-v2 --stateless-rpc >out <in &&
97 test_expect_success 'multiple want-ref lines' '
98 cat >expected_refs <<-EOF &&
99 $(git rev-parse c) refs/heads/o/foo
100 $(git rev-parse d) refs/heads/o/bar
102 git rev-parse c d | sort >expected_commits &&
104 test-tool pkt-line pack >in <<-EOF &&
108 want-ref refs/heads/o/foo
109 want-ref refs/heads/o/bar
110 have $(git rev-parse b)
115 test-tool serve-v2 --stateless-rpc >out <in &&
119 test_expect_success 'mix want and want-ref' '
120 cat >expected_refs <<-EOF &&
121 $(git rev-parse f) refs/heads/master
123 git rev-parse e f | sort >expected_commits &&
125 test-tool pkt-line pack >in <<-EOF &&
129 want-ref refs/heads/master
130 want $(git rev-parse e)
131 have $(git rev-parse a)
136 test-tool serve-v2 --stateless-rpc >out <in &&
140 test_expect_success 'want-ref with ref we already have commit for' '
141 cat >expected_refs <<-EOF &&
142 $(git rev-parse c) refs/heads/o/foo
146 test-tool pkt-line pack >in <<-EOF &&
150 want-ref refs/heads/o/foo
151 have $(git rev-parse c)
156 test-tool serve-v2 --stateless-rpc >out <in &&
160 . "$TEST_DIRECTORY"/lib-httpd.sh
163 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
164 LOCAL_PRISTINE="$(pwd)/local_pristine"
166 test_expect_success 'setup repos for change-while-negotiating test' '
170 >.git/git-daemon-export-ok &&
174 # Local repo with many commits (so that negotiation will take
175 # more than 1 request/response pair)
176 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
177 cd "$LOCAL_PRISTINE" &&
178 git checkout -b side &&
179 test_commit_bulk --id=s 33 &&
181 # Add novel commits to upstream
182 git checkout master &&
188 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_sed/repo" &&
189 git -C "$LOCAL_PRISTINE" config protocol.version 2
193 # Simulate that the server initially reports $2 as the ref
194 # corresponding to $1, and after that, $1 as the ref corresponding to
195 # $1. This corresponds to the real-life situation where the server's
196 # repository appears to change during negotiation, for example, when
197 # different servers in a load-balancing arrangement serve (stateless)
198 # RPCs during a single negotiation.
200 $(git -C "$REPO" rev-parse $1 | tr -d "\n") \
201 $(git -C "$REPO" rev-parse $2 | tr -d "\n") \
202 >"$HTTPD_ROOT_PATH/one-time-sed"
205 test_expect_success 'server is initially ahead - no ref in want' '
206 git -C "$REPO" config uploadpack.allowRefInWant false &&
208 cp -r "$LOCAL_PRISTINE" local &&
209 inconsistency master 1234567890123456789012345678901234567890 &&
210 test_must_fail git -C local fetch 2>err &&
211 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
214 test_expect_success 'server is initially ahead - ref in want' '
215 git -C "$REPO" config uploadpack.allowRefInWant true &&
217 cp -r "$LOCAL_PRISTINE" local &&
218 inconsistency master 1234567890123456789012345678901234567890 &&
219 git -C local fetch &&
221 git -C "$REPO" rev-parse --verify master >expected &&
222 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
223 test_cmp expected actual
226 test_expect_success 'server is initially behind - no ref in want' '
227 git -C "$REPO" config uploadpack.allowRefInWant false &&
229 cp -r "$LOCAL_PRISTINE" local &&
230 inconsistency master "master^" &&
231 git -C local fetch &&
233 git -C "$REPO" rev-parse --verify "master^" >expected &&
234 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
235 test_cmp expected actual
238 test_expect_success 'server is initially behind - ref in want' '
239 git -C "$REPO" config uploadpack.allowRefInWant true &&
241 cp -r "$LOCAL_PRISTINE" local &&
242 inconsistency master "master^" &&
243 git -C local fetch &&
245 git -C "$REPO" rev-parse --verify "master" >expected &&
246 git -C local rev-parse --verify refs/remotes/origin/master >actual &&
247 test_cmp expected actual
250 test_expect_success 'server loses a ref - ref in want' '
251 git -C "$REPO" config uploadpack.allowRefInWant true &&
253 cp -r "$LOCAL_PRISTINE" local &&
254 echo "s/master/raster/" >"$HTTPD_ROOT_PATH/one-time-sed" &&
255 test_must_fail git -C local fetch 2>err &&
257 test_i18ngrep "fatal: remote error: unknown ref refs/heads/raster" err
261 LOCAL_PRISTINE="$(pwd)/local_pristine"
278 test_expect_success 'setup repos for fetching with ref-in-want tests' '
284 # Local repo with many commits (so that negotiation will take
285 # more than 1 request/response pair)
286 rm -rf "$LOCAL_PRISTINE" &&
287 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
288 cd "$LOCAL_PRISTINE" &&
289 git checkout -b side &&
290 test_commit_bulk --id=s 33 &&
292 # Add novel commits to upstream
293 git checkout master &&
295 git checkout -b o/foo &&
298 git checkout -b o/bar b &&
300 git checkout -b baz a &&
302 git checkout master &&
305 git -C "$REPO" config uploadpack.allowRefInWant true &&
306 git -C "$LOCAL_PRISTINE" config protocol.version 2
309 test_expect_success 'fetching with exact OID' '
310 test_when_finished "rm -f log" &&
313 cp -r "$LOCAL_PRISTINE" local &&
314 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
315 $(git -C "$REPO" rev-parse d):refs/heads/actual &&
317 git -C "$REPO" rev-parse "d" >expected &&
318 git -C local rev-parse refs/heads/actual >actual &&
319 test_cmp expected actual &&
320 grep "want $(git -C "$REPO" rev-parse d)" log
323 test_expect_success 'fetching multiple refs' '
324 test_when_finished "rm -f log" &&
327 cp -r "$LOCAL_PRISTINE" local &&
328 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin master baz &&
330 git -C "$REPO" rev-parse "master" "baz" >expected &&
331 git -C local rev-parse refs/remotes/origin/master refs/remotes/origin/baz >actual &&
332 test_cmp expected actual &&
333 grep "want-ref refs/heads/master" log &&
334 grep "want-ref refs/heads/baz" log
337 test_expect_success 'fetching ref and exact OID' '
338 test_when_finished "rm -f log" &&
341 cp -r "$LOCAL_PRISTINE" local &&
342 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
343 master $(git -C "$REPO" rev-parse b):refs/heads/actual &&
345 git -C "$REPO" rev-parse "master" "b" >expected &&
346 git -C local rev-parse refs/remotes/origin/master refs/heads/actual >actual &&
347 test_cmp expected actual &&
348 grep "want $(git -C "$REPO" rev-parse b)" log &&
349 grep "want-ref refs/heads/master" log
352 test_expect_success 'fetching with wildcard that does not match any refs' '
353 test_when_finished "rm -f log" &&
356 cp -r "$LOCAL_PRISTINE" local &&
357 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
358 test_must_be_empty out
361 test_expect_success 'fetching with wildcard that matches multiple refs' '
362 test_when_finished "rm -f log" &&
365 cp -r "$LOCAL_PRISTINE" local &&
366 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
368 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
369 git -C local rev-parse "o/foo" "o/bar" >actual &&
370 test_cmp expected actual &&
371 grep "want-ref refs/heads/o/foo" log &&
372 grep "want-ref refs/heads/o/bar" log