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>
Jeff King committed
Mar 27, 2020 at 04:03 UTC
88124ab263670b4252be7c13d03754a127cee90e
2 files changed
+28
-14
t/t5562-http-backend-content-length.sh
+12
-7
@@ -53,15 +53,20 @@ test_expect_success 'setup' '
53
test_commit c1 &&
54
hash_head=$(git rev-parse HEAD) &&
55
hash_prev=$(git rev-parse HEAD~1) &&
56
- printf "want %s" "$hash_head" | packetize >fetch_body &&
57
- printf 0000 >>fetch_body &&
58
- printf "have %s" "$hash_prev" | packetize >>fetch_body &&
59
- printf done | packetize >>fetch_body &&
56
+ {
57
+ packetize "want $hash_head" &&
58
+ printf 0000 &&
59
+ packetize "have $hash_prev" &&
60
+ packetize "done"
61
+ } >fetch_body &&
62
test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
63
hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
62
- printf "%s %s refs/heads/newbranch\\0report-status\\n" "$ZERO_OID" "$hash_next" | packetize >push_body &&
63
- printf 0000 >>push_body &&
64
- echo "$hash_next" | git pack-objects --stdout >>push_body &&
64
+ {
65
+ printf "%s %s refs/heads/newbranch\\0report-status\\n" \
66
+ "$ZERO_OID" "$hash_next" | packetize &&
67
+ printf 0000 &&
68
+ echo "$hash_next" | git pack-objects --stdout
69
+ } >push_body &&
70
test_copy_bytes 10 <push_body >push_body.trunc &&
71
: >empty_body
72
'
t/test-lib-functions.sh
+16
-7
@@ -1362,14 +1362,23 @@ nongit () {
1362
)
1363
} 7>&2 2>&4
1364
1365
-# convert stdin to pktline representation; note that empty input becomes an
1366
-# empty packet, not a flush packet (for that you can just print 0000 yourself).
1365
+# convert function arguments or stdin (if not arguments given) to pktline
1366
+# representation. If multiple arguments are given, they are separated by
1367
+# whitespace and put in a single packet. Note that data containing NULs must be
1368
+# given on stdin, and that empty input becomes an empty packet, not a flush
1369
+# packet (for that you can just print 0000 yourself).
1370
packetize() {
1368
- cat >packetize.tmp &&
1369
- len=$(wc -c <packetize.tmp) &&
1370
- printf '%04x%s' "$(($len + 4))" &&
1371
- cat packetize.tmp &&
1372
- rm -f packetize.tmp
1371
+ if test $# -gt 0
1372
+ then
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
1381
+ fi
1382
}
1383
1384
# Parse the input as a series of pktlines, writing the result to stdout.