t1006: add tests for %(objectsize:disk)

Back when we added this placeholder in a4ac106178 (cat-file: add %(objectsize:disk) format atom, 2013-07-10), there were no tests, claiming "[...]the exact numbers returned are volatile and subject to zlib and packing decisions". But we can use a little shell hackery to get the expected numbers ourselves. To a certain degree this is just re-implementing what Git is doing under the hood, but it is still worth doing. It makes sure we exercise the %(objectsize:disk) code at all, and having the two implementations agree gives us more confidence. Note that our shell code assumes that no object appears twice (either in two packs, or as both loose and packed), as then the results really are undefined. That's OK for our purposes, and the test will notice if that assumption is violated (the shell version would produce duplicate lines that Git's output does not have). Helped-by: René Scharfe <l.s.r@web.de> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 21, 2023 at 04:47 UTC f546151228ea99cb9e68ec321ace7d2ee7c20af5
1 file changed +34
t/t1006-cat-file.sh
+34
@@ -1100,6 +1100,40 @@ test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor
1100 cmp expect actual
1101 '
1102
1103 +test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' '
1104 + # our state has both loose and packed objects,
1105 + # so find both for our expected output
1106 + {
1107 + find .git/objects/?? -type f |
1108 + awk -F/ "{ print \$0, \$3\$4 }" |
1109 + while read path oid
1110 + do
1111 + size=$(test_file_size "$path") &&
1112 + echo "$oid $size" ||
1113 + return 1
1114 + done &&
1115 + rawsz=$(test_oid rawsz) &&
1116 + find .git/objects/pack -name "*.idx" |
1117 + while read idx
1118 + do
1119 + git show-index <"$idx" >idx.raw &&
1120 + sort -n <idx.raw >idx.sorted &&
1121 + packsz=$(test_file_size "${idx%.idx}.pack") &&
1122 + end=$((packsz - rawsz)) &&
1123 + awk -v end="$end" "
1124 + NR > 1 { print oid, \$1 - start }
1125 + { start = \$1; oid = \$2 }
1126 + END { print oid, end - start }
1127 + " idx.sorted ||
1128 + return 1
1129 + done
1130 + } >expect.raw &&
1131 + sort <expect.raw >expect &&
1132 + git cat-file --batch-all-objects \
1133 + --batch-check="%(objectname) %(objectsize:disk)" >actual &&
1134 + test_cmp expect actual
1135 +'
1136 +
1137 test_expect_success 'set up replacement object' '
1138 orig=$(git rev-parse HEAD) &&
1139 git cat-file commit $orig >orig &&