Skip to content

Commit f54a2a8

Browse files
committed
Merge branch 'jt/partial-clone-missing-ref-delta-base' into maint
"git fetch" into a lazy clone forgot to fetch base objects that are necessary to complete delta in a thin packfile, which has been corrected. * jt/partial-clone-missing-ref-delta-base: t5616: cover case of client having delta base t5616: use correct flag to check object is missing index-pack: prefetch missing REF_DELTA bases t5616: refactor packfile replacement
2 parents 9db52cf + 810e193 commit f54a2a8

File tree

2 files changed

+123
-15
lines changed

2 files changed

+123
-15
lines changed

builtin/index-pack.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "thread-utils.h"
1515
#include "packfile.h"
1616
#include "object-store.h"
17+
#include "fetch-object.h"
1718

1819
static const char index_pack_usage[] =
1920
"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
@@ -1351,6 +1352,25 @@ static void fix_unresolved_deltas(struct hashfile *f)
13511352
sorted_by_pos[i] = &ref_deltas[i];
13521353
QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
13531354

1355+
if (repository_format_partial_clone) {
1356+
/*
1357+
* Prefetch the delta bases.
1358+
*/
1359+
struct oid_array to_fetch = OID_ARRAY_INIT;
1360+
for (i = 0; i < nr_ref_deltas; i++) {
1361+
struct ref_delta_entry *d = sorted_by_pos[i];
1362+
if (!oid_object_info_extended(the_repository, &d->oid,
1363+
NULL,
1364+
OBJECT_INFO_FOR_PREFETCH))
1365+
continue;
1366+
oid_array_append(&to_fetch, &d->oid);
1367+
}
1368+
if (to_fetch.nr)
1369+
fetch_objects(repository_format_partial_clone,
1370+
to_fetch.oid, to_fetch.nr);
1371+
oid_array_clear(&to_fetch);
1372+
}
1373+
13541374
for (i = 0; i < nr_ref_deltas; i++) {
13551375
struct ref_delta_entry *d = sorted_by_pos[i];
13561376
enum object_type type;
@@ -1650,8 +1670,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16501670
int report_end_of_input = 0;
16511671

16521672
/*
1653-
* index-pack never needs to fetch missing objects, since it only
1654-
* accesses the repo to do hash collision checks
1673+
* index-pack never needs to fetch missing objects except when
1674+
* REF_DELTA bases are missing (which are explicitly handled). It only
1675+
* accesses the repo to do hash collision checks and to check which
1676+
* REF_DELTA bases need to be fetched.
16551677
*/
16561678
fetch_if_missing = 0;
16571679

t/t5616-partial-clone.sh

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,25 @@ test_expect_success 'fetch what is specified on CLI even if already promised' '
244244
. "$TEST_DIRECTORY"/lib-httpd.sh
245245
start_httpd
246246

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"
252266
}
253267

254268
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' '
270284
# Replace the existing packfile with the crafted one. The protocol
271285
# requires that the packfile be sent in sideband 1, hence the extra
272286
# \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 &&
277288
278289
# Use protocol v2 because the sed command looks for the "packfile"
279290
# section header.
@@ -313,10 +324,7 @@ test_expect_success 'when partial cloning, tolerate server not sending target of
313324
# Replace the existing packfile with the crafted one. The protocol
314325
# requires that the packfile be sent in sideband 1, hence the extra
315326
# \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 &&
320328
321329
# Use protocol v2 because the sed command looks for the "packfile"
322330
# section header.
@@ -331,4 +339,82 @@ test_expect_success 'when partial cloning, tolerate server not sending target of
331339
! test -e "$HTTPD_ROOT_PATH/one-time-sed"
332340
'
333341

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+
334420
test_done

0 commit comments

Comments
 (0)