Skip to content

Commit 385d1bf

Browse files
jonathantanmygitster
authored andcommitted
t5616: refactor packfile replacement
A subsequent patch will perform the same packfile replacement that is already done twice, so refactor it into its own function. Also, the same subsequent patch will use, in another way, part of the packfile replacement functionality, so extract those out too. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab15ad1 commit 385d1bf

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

t/t5616-partial-clone.sh

Lines changed: 21 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.

0 commit comments

Comments
 (0)