1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-07 01:26:13 +02:00

test-lib-functions: make packetize() more efficient

The packetize() function takes its input on stdin, and requires 4
separate sub-processes to format a simple string. We can do much better
by getting the length via the shell's "${#packet}" construct. The one
caveat is that the shell can't put a NUL into a variable, so we'll have
to continue to provide the stdin form for a few calls.

There are a few other cleanups here in the touched code:

 - the stdin form of packetize() had an extra stray "%s" when printing
   the packet

 - the converted calls in t5562 can be made simpler by redirecting
   output as a block, rather than repeated appending

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2020-03-27 04:03:00 -04:00 committed by Junio C Hamano
parent 274b9cc253
commit 88124ab263
2 changed files with 28 additions and 14 deletions

View File

@ -53,15 +53,20 @@ test_expect_success 'setup' '
test_commit c1 && test_commit c1 &&
hash_head=$(git rev-parse HEAD) && hash_head=$(git rev-parse HEAD) &&
hash_prev=$(git rev-parse HEAD~1) && hash_prev=$(git rev-parse HEAD~1) &&
printf "want %s" "$hash_head" | packetize >fetch_body && {
printf 0000 >>fetch_body && packetize "want $hash_head" &&
printf "have %s" "$hash_prev" | packetize >>fetch_body && printf 0000 &&
printf done | packetize >>fetch_body && packetize "have $hash_prev" &&
packetize "done"
} >fetch_body &&
test_copy_bytes 10 <fetch_body >fetch_body.trunc && test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) && hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
printf "%s %s refs/heads/newbranch\\0report-status\\n" "$ZERO_OID" "$hash_next" | packetize >push_body && {
printf 0000 >>push_body && printf "%s %s refs/heads/newbranch\\0report-status\\n" \
echo "$hash_next" | git pack-objects --stdout >>push_body && "$ZERO_OID" "$hash_next" | packetize &&
printf 0000 &&
echo "$hash_next" | git pack-objects --stdout
} >push_body &&
test_copy_bytes 10 <push_body >push_body.trunc && test_copy_bytes 10 <push_body >push_body.trunc &&
: >empty_body : >empty_body
' '

View File

@ -1362,14 +1362,23 @@ nongit () {
) )
} 7>&2 2>&4 } 7>&2 2>&4
# convert stdin to pktline representation; note that empty input becomes an # convert function arguments or stdin (if not arguments given) to pktline
# empty packet, not a flush packet (for that you can just print 0000 yourself). # representation. If multiple arguments are given, they are separated by
# whitespace and put in a single packet. Note that data containing NULs must be
# given on stdin, and that empty input becomes an empty packet, not a flush
# packet (for that you can just print 0000 yourself).
packetize() { packetize() {
cat >packetize.tmp && if test $# -gt 0
len=$(wc -c <packetize.tmp) && then
printf '%04x%s' "$(($len + 4))" && packet="$*"
cat packetize.tmp && printf '%04x%s' "$((4 + ${#packet}))" "$packet"
rm -f packetize.tmp else
cat >packetize.tmp &&
len=$(wc -c <packetize.tmp) &&
printf '%04x' "$(($len + 4))" &&
cat packetize.tmp &&
rm -f packetize.tmp
fi
} }
# Parse the input as a series of pktlines, writing the result to stdout. # Parse the input as a series of pktlines, writing the result to stdout.