@@ -244,11 +244,25 @@ test_expect_success 'fetch what is specified on CLI even if already promised' '
244
244
. " $TEST_DIRECTORY " /lib-httpd.sh
245
245
start_httpd
246
246
247
- # Converts bytes into a form suitable for inclusion in a sed command. For
248
- # example, "printf 'ab\r\n' | hex_unpack" results in '\x61\x62\x0d\x0a'.
249
- sed_escape () {
250
- perl -e ' $/ = undef; $input = <>; print unpack("H2" x length($input), $input)' |
251
- sed ' s/\(..\)/\\x\1/g'
247
+ # Converts bytes into their hexadecimal representation. For example,
248
+ # "printf 'ab\r\n' | hex_unpack" results in '61620d0a'.
249
+ hex_unpack () {
250
+ perl -e ' $/ = undef; $input = <>; print unpack("H2" x length($input), $input)'
251
+ }
252
+
253
+ # Inserts $1 at the start of the string and every 2 characters thereafter.
254
+ intersperse () {
255
+ sed ' s/\(..\)/' $1 ' \1/g'
256
+ }
257
+
258
+ # Create a one-time-sed command to replace the existing packfile with $1.
259
+ replace_packfile () {
260
+ # The protocol requires that the packfile be sent in sideband 1, hence
261
+ # the extra \x01 byte at the beginning.
262
+ printf " 1,/packfile/!c %04x\\\\ x01%s0000" \
263
+ " $(( $(wc - c < $1 ) + 5 )) " \
264
+ " $( hex_unpack < $1 | intersperse ' \\x' ) " \
265
+ > " $HTTPD_ROOT_PATH /one-time-sed"
252
266
}
253
267
254
268
test_expect_success ' upon cloning, check that all refs point to objects' '
@@ -270,10 +284,7 @@ test_expect_success 'upon cloning, check that all refs point to objects' '
270
284
# Replace the existing packfile with the crafted one. The protocol
271
285
# requires that the packfile be sent in sideband 1, hence the extra
272
286
# \x01 byte at the beginning.
273
- printf "1,/packfile/!c %04x\\\\x01%s0000" \
274
- "$(($(wc -c <incomplete.pack) + 5))" \
275
- "$(sed_escape <incomplete.pack)" \
276
- >"$HTTPD_ROOT_PATH/one-time-sed" &&
287
+ replace_packfile incomplete.pack &&
277
288
278
289
# Use protocol v2 because the sed command looks for the "packfile"
279
290
# section header.
@@ -313,10 +324,7 @@ test_expect_success 'when partial cloning, tolerate server not sending target of
313
324
# Replace the existing packfile with the crafted one. The protocol
314
325
# requires that the packfile be sent in sideband 1, hence the extra
315
326
# \x01 byte at the beginning.
316
- printf "1,/packfile/!c %04x\\\\x01%s0000" \
317
- "$(($(wc -c <incomplete.pack) + 5))" \
318
- "$(sed_escape <incomplete.pack)" \
319
- >"$HTTPD_ROOT_PATH/one-time-sed" &&
327
+ replace_packfile incomplete.pack &&
320
328
321
329
# Use protocol v2 because the sed command looks for the "packfile"
322
330
# section header.
@@ -331,4 +339,82 @@ test_expect_success 'when partial cloning, tolerate server not sending target of
331
339
! test -e "$HTTPD_ROOT_PATH/one-time-sed"
332
340
'
333
341
342
+ test_expect_success ' tolerate server sending REF_DELTA against missing promisor objects' '
343
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
344
+ rm -rf "$SERVER" repo &&
345
+ test_create_repo "$SERVER" &&
346
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
347
+ test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
348
+
349
+ # Create a commit with 2 blobs to be used as delta bases.
350
+ for i in $(test_seq 10)
351
+ do
352
+ echo "this is a line" >>"$SERVER/foo.txt" &&
353
+ echo "this is another line" >>"$SERVER/have.txt"
354
+ done &&
355
+ git -C "$SERVER" add foo.txt have.txt &&
356
+ git -C "$SERVER" commit -m bar &&
357
+ git -C "$SERVER" rev-parse HEAD:foo.txt >deltabase_missing &&
358
+ git -C "$SERVER" rev-parse HEAD:have.txt >deltabase_have &&
359
+
360
+ # Clone. The client has deltabase_have but not deltabase_missing.
361
+ git -c protocol.version=2 clone --no-checkout \
362
+ --filter=blob:none $HTTPD_URL/one_time_sed/server repo &&
363
+ git -C repo hash-object -w -- "$SERVER/have.txt" &&
364
+
365
+ # Sanity check to ensure that the client does not have
366
+ # deltabase_missing.
367
+ git -C repo rev-list --objects --ignore-missing \
368
+ -- $(cat deltabase_missing) >objlist &&
369
+ test_line_count = 0 objlist &&
370
+
371
+ # Another commit. This commit will be fetched by the client.
372
+ echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/foo.txt" &&
373
+ echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/have.txt" &&
374
+ git -C "$SERVER" add foo.txt have.txt &&
375
+ git -C "$SERVER" commit -m baz &&
376
+
377
+ # Pack a thin pack containing, among other things, HEAD:foo.txt
378
+ # delta-ed against HEAD^:foo.txt and HEAD:have.txt delta-ed against
379
+ # HEAD^:have.txt.
380
+ printf "%s\n--not\n%s\n" \
381
+ $(git -C "$SERVER" rev-parse HEAD) \
382
+ $(git -C "$SERVER" rev-parse HEAD^) |
383
+ git -C "$SERVER" pack-objects --thin --stdout >thin.pack &&
384
+
385
+ # Ensure that the pack contains one delta against HEAD^:foo.txt. Since
386
+ # the delta contains at least 26 novel characters, the size cannot be
387
+ # contained in 4 bits, so the object header will take up 2 bytes. The
388
+ # most significant nybble of the first byte is 0b1111 (0b1 to indicate
389
+ # that the header continues, and 0b111 to indicate REF_DELTA), followed
390
+ # by any 3 nybbles, then the OID of the delta base.
391
+ printf "f.,..%s" $(intersperse "," <deltabase_missing) >want &&
392
+ hex_unpack <thin.pack | intersperse "," >have &&
393
+ grep $(cat want) have &&
394
+
395
+ # Ensure that the pack contains one delta against HEAD^:have.txt,
396
+ # similar to the above.
397
+ printf "f.,..%s" $(intersperse "," <deltabase_have) >want &&
398
+ hex_unpack <thin.pack | intersperse "," >have &&
399
+ grep $(cat want) have &&
400
+
401
+ replace_packfile thin.pack &&
402
+
403
+ # Use protocol v2 because the sed command looks for the "packfile"
404
+ # section header.
405
+ test_config -C "$SERVER" protocol.version 2 &&
406
+
407
+ # Fetch the thin pack and ensure that index-pack is able to handle the
408
+ # REF_DELTA object with a missing promisor delta base.
409
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C repo -c protocol.version=2 fetch &&
410
+
411
+ # Ensure that the missing delta base was directly fetched, but not the
412
+ # one that the client has.
413
+ grep "want $(cat deltabase_missing)" trace &&
414
+ ! grep "want $(cat deltabase_have)" trace &&
415
+
416
+ # Ensure that the one-time-sed script was used.
417
+ ! test -e "$HTTPD_ROOT_PATH/one-time-sed"
418
+ '
419
+
334
420
test_done
0 commit comments