@matheusbsbs / matheus-bagliano / commits / cacae4329f
Fork 0

test-lib-functions: simplify packetize() stdin code

The code path in packetize() for reading stdin needs to handle NUL bytes, so we can't rely on shell variables. However, the current code takes a whopping 4 processes and uses a temporary file. We can do this much more simply and efficiently by using a single perl invocation (and we already rely on perl in the matching depacketize() function). We'll keep the non-stdin code path as it is, since that uses zero extra processes. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 29, 2020 at 11:02 UTC cacae4329fa4779127f4944e7807512e7b9e8cac
1 file changed +4 -5
t/test-lib-functions.sh
+4 -5
@@ -1373,11 +1373,10 @@ packetize() {
1373 packet="$*"
1374 printf '%04x%s' "$((4 + ${#packet}))" "$packet"
1375 else
1376 - cat >packetize.tmp &&
1377 - len=$(wc -c <packetize.tmp) &&
1378 - printf '%04x' "$(($len + 4))" &&
1379 - cat packetize.tmp &&
1380 - rm -f packetize.tmp
1376 + perl -e '
1377 + my $packet = do { local $/; <STDIN> };
1378 + printf "%04x%s", 4 + length($packet), $packet;
1379 + '
1380 fi
1381 }
1382