utf8: fix overflow when returning string width
[alt-git.git] / t / t5703-upload-pack-ref-in-want.sh
blobeab966985b7aab80afad18cb87e0f605b37d246b
1 #!/bin/sh
3 test_description='upload-pack ref-in-want'
5 . ./test-lib.sh
7 get_actual_refs () {
8 sed -n -e '/wanted-refs/,/0001/{
9 /wanted-refs/d
10 /0001/d
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
22 check_output () {
23 get_actual_refs &&
24 test_cmp expected_refs actual_refs &&
25 get_actual_commits &&
26 sort expected_commits >sorted_commits &&
27 test_cmp sorted_commits actual_commits
30 write_command () {
31 echo "command=$1"
33 if test "$(test_oid algo)" != sha1
34 then
35 echo "object-format=$(test_oid algo)"
39 # c(o/foo) d(o/bar)
40 # \ /
41 # b e(baz) f(main)
42 # \__ | __/
43 # \ | /
44 # a
45 test_expect_success 'setup repository' '
46 test_commit a &&
47 git branch -M main &&
48 git checkout -b o/foo &&
49 test_commit b &&
50 test_commit c &&
51 git checkout -b o/bar b &&
52 test_commit d &&
53 git checkout -b baz a &&
54 test_commit e &&
55 git checkout main &&
56 test_commit f
59 test_expect_success 'config controls ref-in-want advertisement' '
60 test-tool serve-v2 --advertise-capabilities >out &&
61 perl -ne "/ref-in-want/ and print" out >out.filter &&
62 test_must_be_empty out.filter &&
64 git config uploadpack.allowRefInWant false &&
65 test-tool serve-v2 --advertise-capabilities >out &&
66 perl -ne "/ref-in-want/ and print" out >out.filter &&
67 test_must_be_empty out.filter &&
69 git config uploadpack.allowRefInWant true &&
70 test-tool serve-v2 --advertise-capabilities >out &&
71 perl -ne "/ref-in-want/ and print" out >out.filter &&
72 test_file_not_empty out.filter
75 test_expect_success 'invalid want-ref line' '
76 test-tool pkt-line pack >in <<-EOF &&
77 $(write_command fetch)
78 0001
79 no-progress
80 want-ref refs/heads/non-existent
81 done
82 0000
83 EOF
85 test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
86 grep "unknown ref" out
89 test_expect_success 'basic want-ref' '
90 oid=$(git rev-parse f) &&
91 cat >expected_refs <<-EOF &&
92 $oid refs/heads/main
93 EOF
94 git rev-parse f >expected_commits &&
96 oid=$(git rev-parse a) &&
97 test-tool pkt-line pack >in <<-EOF &&
98 $(write_command fetch)
99 0001
100 no-progress
101 want-ref refs/heads/main
102 have $oid
103 done
104 0000
107 test-tool serve-v2 --stateless-rpc >out <in &&
108 check_output
111 test_expect_success 'multiple want-ref lines' '
112 oid_c=$(git rev-parse c) &&
113 oid_d=$(git rev-parse d) &&
114 cat >expected_refs <<-EOF &&
115 $oid_c refs/heads/o/foo
116 $oid_d refs/heads/o/bar
118 git rev-parse c d >expected_commits &&
120 oid=$(git rev-parse b) &&
121 test-tool pkt-line pack >in <<-EOF &&
122 $(write_command fetch)
123 0001
124 no-progress
125 want-ref refs/heads/o/foo
126 want-ref refs/heads/o/bar
127 have $oid
128 done
129 0000
132 test-tool serve-v2 --stateless-rpc >out <in &&
133 check_output
136 test_expect_success 'mix want and want-ref' '
137 oid=$(git rev-parse f) &&
138 cat >expected_refs <<-EOF &&
139 $oid refs/heads/main
141 git rev-parse e f >expected_commits &&
143 test-tool pkt-line pack >in <<-EOF &&
144 $(write_command fetch)
145 0001
146 no-progress
147 want-ref refs/heads/main
148 want $(git rev-parse e)
149 have $(git rev-parse a)
150 done
151 0000
154 test-tool serve-v2 --stateless-rpc >out <in &&
155 check_output
158 test_expect_success 'want-ref with ref we already have commit for' '
159 oid=$(git rev-parse c) &&
160 cat >expected_refs <<-EOF &&
161 $oid refs/heads/o/foo
163 >expected_commits &&
165 oid=$(git rev-parse c) &&
166 test-tool pkt-line pack >in <<-EOF &&
167 $(write_command fetch)
168 0001
169 no-progress
170 want-ref refs/heads/o/foo
171 have $oid
172 done
173 0000
176 test-tool serve-v2 --stateless-rpc >out <in &&
177 check_output
180 REPO="$(pwd)/repo"
181 LOCAL_PRISTINE="$(pwd)/local_pristine"
183 # $REPO
184 # c(o/foo) d(o/bar)
185 # \ /
186 # b e(baz) f(main)
187 # \__ | __/
188 # \ | /
191 # $LOCAL_PRISTINE
192 # s32(side)
197 # a(main)
198 test_expect_success 'setup repos for fetching with ref-in-want tests' '
200 git init -b main "$REPO" &&
201 cd "$REPO" &&
202 test_commit a &&
204 # Local repo with many commits (so that negotiation will take
205 # more than 1 request/response pair)
206 rm -rf "$LOCAL_PRISTINE" &&
207 git clone "file://$REPO" "$LOCAL_PRISTINE" &&
208 cd "$LOCAL_PRISTINE" &&
209 git checkout -b side &&
210 test_commit_bulk --id=s 33 &&
212 # Add novel commits to upstream
213 git checkout main &&
214 cd "$REPO" &&
215 git checkout -b o/foo &&
216 test_commit b &&
217 test_commit c &&
218 git checkout -b o/bar b &&
219 test_commit d &&
220 git checkout -b baz a &&
221 test_commit e &&
222 git checkout main &&
223 test_commit f
224 ) &&
225 git -C "$REPO" config uploadpack.allowRefInWant true &&
226 git -C "$LOCAL_PRISTINE" config protocol.version 2
229 test_expect_success 'fetching with exact OID' '
230 test_when_finished "rm -f log" &&
232 rm -rf local &&
233 cp -r "$LOCAL_PRISTINE" local &&
234 oid=$(git -C "$REPO" rev-parse d) &&
235 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
236 "$oid":refs/heads/actual &&
238 git -C "$REPO" rev-parse "d" >expected &&
239 git -C local rev-parse refs/heads/actual >actual &&
240 test_cmp expected actual &&
241 grep "want $oid" log
244 test_expect_success 'fetching multiple refs' '
245 test_when_finished "rm -f log" &&
247 rm -rf local &&
248 cp -r "$LOCAL_PRISTINE" local &&
249 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
251 git -C "$REPO" rev-parse "main" "baz" >expected &&
252 git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
253 test_cmp expected actual &&
254 grep "want-ref refs/heads/main" log &&
255 grep "want-ref refs/heads/baz" log
258 test_expect_success 'fetching ref and exact OID' '
259 test_when_finished "rm -f log" &&
261 rm -rf local &&
262 cp -r "$LOCAL_PRISTINE" local &&
263 oid=$(git -C "$REPO" rev-parse b) &&
264 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
265 main "$oid":refs/heads/actual &&
267 git -C "$REPO" rev-parse "main" "b" >expected &&
268 git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
269 test_cmp expected actual &&
270 grep "want $oid" log &&
271 grep "want-ref refs/heads/main" log
274 test_expect_success 'fetching with wildcard that does not match any refs' '
275 test_when_finished "rm -f log" &&
277 rm -rf local &&
278 cp -r "$LOCAL_PRISTINE" local &&
279 git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
280 test_must_be_empty out
283 test_expect_success 'fetching with wildcard that matches multiple refs' '
284 test_when_finished "rm -f log" &&
286 rm -rf local &&
287 cp -r "$LOCAL_PRISTINE" local &&
288 GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
290 git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
291 git -C local rev-parse "o/foo" "o/bar" >actual &&
292 test_cmp expected actual &&
293 grep "want-ref refs/heads/o/foo" log &&
294 grep "want-ref refs/heads/o/bar" log
297 . "$TEST_DIRECTORY"/lib-httpd.sh
298 start_httpd
300 REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
301 LOCAL_PRISTINE="$(pwd)/local_pristine"
303 test_expect_success 'setup repos for change-while-negotiating test' '
305 git init -b main "$REPO" &&
306 cd "$REPO" &&
307 >.git/git-daemon-export-ok &&
308 test_commit m1 &&
309 git tag -d m1 &&
311 # Local repo with many commits (so that negotiation will take
312 # more than 1 request/response pair)
313 rm -rf "$LOCAL_PRISTINE" &&
314 git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
315 cd "$LOCAL_PRISTINE" &&
316 git checkout -b side &&
317 test_commit_bulk --id=s 33 &&
319 # Add novel commits to upstream
320 git checkout main &&
321 cd "$REPO" &&
322 test_commit m2 &&
323 test_commit m3 &&
324 git tag -d m2 m3
325 ) &&
326 git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
327 git -C "$LOCAL_PRISTINE" config protocol.version 2
330 inconsistency () {
331 # Simulate that the server initially reports $2 as the ref
332 # corresponding to $1, and after that, $1 as the ref corresponding to
333 # $1. This corresponds to the real-life situation where the server's
334 # repository appears to change during negotiation, for example, when
335 # different servers in a load-balancing arrangement serve (stateless)
336 # RPCs during a single negotiation.
337 oid1=$(git -C "$REPO" rev-parse $1) &&
338 oid2=$(git -C "$REPO" rev-parse $2) &&
339 echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
342 test_expect_success 'server is initially ahead - no ref in want' '
343 git -C "$REPO" config uploadpack.allowRefInWant false &&
344 rm -rf local &&
345 cp -r "$LOCAL_PRISTINE" local &&
346 inconsistency main $(test_oid numeric) &&
347 test_must_fail git -C local fetch 2>err &&
348 test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
351 test_expect_success 'server is initially ahead - ref in want' '
352 git -C "$REPO" config uploadpack.allowRefInWant true &&
353 rm -rf local &&
354 cp -r "$LOCAL_PRISTINE" local &&
355 inconsistency main $(test_oid numeric) &&
356 git -C local fetch &&
358 git -C "$REPO" rev-parse --verify main >expected &&
359 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
360 test_cmp expected actual
363 test_expect_success 'server is initially behind - no ref in want' '
364 git -C "$REPO" config uploadpack.allowRefInWant false &&
365 rm -rf local &&
366 cp -r "$LOCAL_PRISTINE" local &&
367 inconsistency main "main^" &&
368 git -C local fetch &&
370 git -C "$REPO" rev-parse --verify "main^" >expected &&
371 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
372 test_cmp expected actual
375 test_expect_success 'server is initially behind - ref in want' '
376 git -C "$REPO" config uploadpack.allowRefInWant true &&
377 rm -rf local &&
378 cp -r "$LOCAL_PRISTINE" local &&
379 inconsistency main "main^" &&
380 git -C local fetch &&
382 git -C "$REPO" rev-parse --verify "main" >expected &&
383 git -C local rev-parse --verify refs/remotes/origin/main >actual &&
384 test_cmp expected actual
387 test_expect_success 'server loses a ref - ref in want' '
388 git -C "$REPO" config uploadpack.allowRefInWant true &&
389 rm -rf local &&
390 cp -r "$LOCAL_PRISTINE" local &&
391 echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
392 test_must_fail git -C local fetch 2>err &&
394 test_i18ngrep "fatal: remote error: unknown ref refs/heads/rain" err
397 # DO NOT add non-httpd-specific tests here, because the last part of this
398 # test script is only executed when httpd is available and enabled.
400 test_done