diff options
author | Jeff King <peff@peff.net> | 2020-03-27 11:03:00 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-03-27 21:50:54 +0300 |
commit | 88124ab263670b4252be7c13d03754a127cee90e (patch) | |
tree | 3be57835c5a2d73dab7daa436aebd16fcf009042 /t/test-lib-functions.sh | |
parent | 274b9cc25322d9ee79aa8e6d4e86f0ffe5ced925 (diff) |
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>
Diffstat (limited to 't/test-lib-functions.sh')
-rw-r--r-- | t/test-lib-functions.sh | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 352c213d52..216918a58c 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -1362,14 +1362,23 @@ nongit () { ) } 7>&2 2>&4 -# convert stdin to pktline representation; note that empty input becomes an -# empty packet, not a flush packet (for that you can just print 0000 yourself). +# convert function arguments or stdin (if not arguments given) to pktline +# 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() { - cat >packetize.tmp && - len=$(wc -c <packetize.tmp) && - printf '%04x%s' "$(($len + 4))" && - cat packetize.tmp && - rm -f packetize.tmp + if test $# -gt 0 + then + packet="$*" + printf '%04x%s' "$((4 + ${#packet}))" "$packet" + 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. |