Test breakage with zlib-ng
Hi everyone,
As some might have heard, there is a proposal for Fedora 40 to transition from zlib to zlib-ng[0]. Because of this, there has been a rebuild of all packages to ensure every package works under zlib-ng.
Git test suit has three breakages in t6300-for-each-ref.sh. To be precise, it is:
not ok 35 - basic atom: refs/heads/main objectsize:disk not ok 107 - basic atom: refs/tags/testtag objectsize:disk not ok 108 - basic atom: refs/tags/testtag *objectsize:disk
All of these tests are atomic, and they compare the result against $disklen. I discussed it with Tulio Magno Quites Machado Filho, who ran the tests and is owner of the proposal.
It seems like the compression of zlib-ng is shaving/adding some bytes to the actual output, which then fails the comparison.
Here is an example:
``` expecting success of 6300.35 'basic atom: refs/heads/main objectsize:disk': git for-each-ref --format="%($format)" "$ref" >actual && sanitize_pgp <actual >actual.clean && test_cmp expected actual.clean ++ git for-each-ref '--format=%(objectsize:disk)' refs/heads/main ++ sanitize_pgp ++ perl -ne ' /^-----END PGP/ and $in_pgp = 0; print unless $in_pgp; /^-----BEGIN PGP/ and $in_pgp = 1; ' ++ command /usr/bin/perl -ne ' /^-----END PGP/ and $in_pgp = 0; print unless $in_pgp; /^-----BEGIN PGP/ and $in_pgp = 1; ' ++ test_cmp expected actual.clean ++ test 2 -ne 2 ++ eval 'diff -u' '"$@"' +++ diff -u expected actual.clean --- expected 2023-12-06 21:06:07.808849497 +0000 +++ actual.clean 2023-12-06 21:06:07.812849541 +0000 @@ -1 +1 @@ -138 +137 error: last command exited with $?=1 not ok 35 - basic atom: refs/heads/main objectsize:disk ```
The whole build log can be found here[1].
I can easily patch these tests in Fedora to be compatible with zlib-ng only by not comparing to $disklen, but a concrete value, however I would like to have a universal solution that works with both zlib and zlib-ng. So if anyone has an idea on how to do it, please let me know. Thanks
[0]https://discussion.fedoraproject.org/t/f40-change-proposal-transitioning-to-zlib-ng-as-a-compatible-replacement-for-zlib-system-wide/95807 [1]https://download.copr.fedorainfracloud.org/results/tuliom/zlib-ng-compat-mpb/fedora-rawhide-x86_64/06729801-git/builder-live.log.gz
Cheers,
Ondřej Pohořelský
Am 12.12.23 um 15:16 schrieb Ondrej Pohorelsky:
Quoted 9 lines
Hi everyone,
As some might have heard, there is a proposal for Fedora 40 to transition from zlib to zlib-ng[0]. Because of this, there has been a rebuild of all packages to ensure every package works under zlib-ng.
Git test suit has three breakages in
t6300-for-each-ref.sh. To be precise, it is:
Quoted 7 lines
not ok 35 - basic atom: refs/heads/main objectsize:disk not ok 107 - basic atom: refs/tags/testtag objectsize:disk not ok 108 - basic atom: refs/tags/testtag *objectsize:disk
All of these tests are atomic, and they compare the result against $disklen.
Why do these three objects (HEAD commit of main, testtag and testtag target) have the same size? Half of the answer is that testtag points to the HEAD of main. But the other half is pure coincidence as far as I can see.
Quoted 4 lines
I can easily patch these tests in Fedora to be compatible with zlib-ng only by not comparing to $disklen, but a concrete value, however I would like to have a universal solution that works with both zlib and zlib-ng. So if anyone has an idea on how to do it, please let me know.
The test stores the expected values at the top, in the following lines, for the two possible repository formats:
test_expect_success setup ' test_oid_cache <<-EOF && disklen sha1:138 disklen sha256:154 EOF
So it's using hard-coded values already, which breaks when the compression rate changes.
We could set core.compression to 0 to take compression out of the picture.
Or we could get the sizes of the objects by checking their files, which would not require hard-coding anymore. Patch below.
--- >8 ---
Subject: [PATCH] t6300: avoid hard-coding object sizes
f4ee22b526 (ref-filter: add tests for objectsize:disk, 2018-12-24) hard-coded the expected object sizes. Coincidentally the size of commit and tag is the same with zlib at the default compression level.
1f5f8f3e85 (t6300: abstract away SHA-1-specific constants, 2020-02-22) encoded the sizes as a single value, which coincidentally also works with sha256.
Different compression libraries like zlib-ng may arrive at different values. Get them from the file system instead of hard-coding them to make switching the compression library (or changing the compression level) easier.
--- t/t6300-for-each-ref.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
t/t6300-for-each-ref.sh +9 -9
@@ -20,12 +20,13 @@ setdate_and_increment () { export GIT_COMMITTER_DATE GIT_AUTHOR_DATE } -test_expect_success setup ' - test_oid_cache <<-EOF && - disklen sha1:138 - disklen sha256:154 - EOF +test_object_file_size () { + oid=$(git rev-parse "$1") + path=".git/objects/$(test_oid_to_path $oid)" + test_file_size "$path" +} +test_expect_success setup ' # setup .mailmap cat >.mailmap <<-EOF && A Thor <[email protected]> A U Thor <[email protected]>
@@ -94,7 +95,6 @@ test_atom () { } hexlen=$(test_oid hexsz) -disklen=$(test_oid disklen) test_atom head refname refs/heads/main test_atom head refname: refs/heads/main
@@ -129,7 +129,7 @@ test_atom head push:strip=1 remotes/myfork/main test_atom head push:strip=-1 main test_atom head objecttype commit test_atom head objectsize $((131 + hexlen)) -test_atom head objectsize:disk $disklen +test_atom head objectsize:disk $(test_object_file_size refs/heads/main) test_atom head deltabase $ZERO_OID test_atom head objectname $(git rev-parse refs/heads/main) test_atom head objectname:short $(git rev-parse --short refs/heads/main)
@@ -203,8 +203,8 @@ test_atom tag upstream '' test_atom tag push '' test_atom tag objecttype tag test_atom tag objectsize $((114 + hexlen)) -test_atom tag objectsize:disk $disklen -test_atom tag '*objectsize:disk' $disklen +test_atom tag objectsize:disk $(test_object_file_size refs/tags/testtag) +test_atom tag '*objectsize:disk' $(test_object_file_size refs/heads/main) test_atom tag deltabase $ZERO_OID test_atom tag '*deltabase' $ZERO_OID test_atom tag objectname $(git rev-parse refs/tags/testtag)
--
2.43.0
Quoted 6 lines · René Scharfe
Subject: [PATCH] t6300: avoid hard-coding object sizes
f4ee22b526(ref-filter: add tests for objectsize:disk, 2018-12-24) hard-coded the expected object sizes. Coincidentally the size of commit and tag is the same with zlib at the default compression level.
Quoted 8 lines
1f5f8f3e85(t6300: abstract away SHA-1-specific constants, 2020-02-22) encoded the sizes as a single value, which coincidentally also works with sha256.Different compression libraries like zlib-ng may arrive at different values. Get them from the file system instead of hard-coding them to make switching the compression library (or changing the compression level) easier.
Yeah, this is definitely the right solution here. I'm surprised the hard-coded values didn't cause problems before now. ;)
The patch looks good to me, but a few small comments:
t/t6300-for-each-ref.sh setdate_and_increment () { · line 27
Quoted 5 lines
t/t6300-for-each-ref.sh +5
-0+test_object_file_size () { + oid=$(git rev-parse "$1") + path=".git/objects/$(test_oid_to_path $oid)" + test_file_size "$path" +}
Here we're assuming the objects are loose. I think that's probably OK (and certainly the test will notice if that changes).
We're covering the formatting code paths along with the underlying implementation that fills in object_info->disk_sizep for loose objects. Which I think is plenty for this particular script, which is about for-each-ref.
It would be nice to have coverage of the packed_object_info() code path, though. Back when it was added in a4ac106178 (cat-file: add %(objectsize:disk) format atom, 2013-07-10), I cowardly punted on this, writing:
This patch does not include any tests, as the exact numbers returned are volatile and subject to zlib and packing decisions. We cannot even reliably guarantee that the on-disk size is smaller than the object content (though in general this should be the case for non-trivial objects).
I don't think it's that big a deal, but I guess we could do something like:
prev=
git show-index <$pack_idx |
sort -n |
grep -A1 $oid |
while read ofs oid csum
do
test -n "$prev" && echo "$((ofs - prev))"
prev=$ofsdone
It feels a little redundant with what Git is doing under the hood, but at least is exercising the code (and we're using the idx directly, so we're confirming that the revindex is right).
Anyway, that is all way beyond the scope of your patch, but I wonder if it's worth doing on top.
t/t6300-for-each-ref.sh test_atom head push:strip=1 remotes/myfork/main · line 132
Quoted 6 lines
t/t6300-for-each-ref.sh +1
-1@@ -129,7 +129,7 @@ test_atom head push:strip=1 remotes/myfork/main test_atom head push:strip=-1 main test_atom head objecttype commit test_atom head objectsize $((131 + hexlen)) -test_atom head objectsize:disk $disklen +test_atom head objectsize:disk $(test_object_file_size refs/heads/main)
These test_object_file_size calls are happening outside of any test_expect_* block, so we'd miss failing exit codes (and also the helper is not &&-chained), and any stderr would leak to the output. That's probably OK in practice, though (if something goes wrong then the expected value output will be bogus and the test itself will fail).
-Peff
Am 12.12.23 um 21:01 schrieb Jeff King:
Quoted 56 lines · René Scharfe
Quoted 7 lines
Subject: [PATCH] t6300: avoid hard-coding object sizes
f4ee22b526(ref-filter: add tests for objectsize:disk, 2018-12-24) hard-coded the expected object sizes. Coincidentally the size of commit and tag is the same with zlib at the default compression level.Quoted 8 lines
1f5f8f3e85(t6300: abstract away SHA-1-specific constants, 2020-02-22) encoded the sizes as a single value, which coincidentally also works with sha256.Different compression libraries like zlib-ng may arrive at different values. Get them from the file system instead of hard-coding them to make switching the compression library (or changing the compression level) easier.
Yeah, this is definitely the right solution here. I'm surprised the hard-coded values didn't cause problems before now. ;)
The patch looks good to me, but a few small comments:
t/t6300-for-each-ref.sh setdate_and_increment () { · line 27
Quoted 5 lines
t/t6300-for-each-ref.sh +5
-0+test_object_file_size () { + oid=$(git rev-parse "$1") + path=".git/objects/$(test_oid_to_path $oid)" + test_file_size "$path" +}Here we're assuming the objects are loose. I think that's probably OK (and certainly the test will notice if that changes).
We're covering the formatting code paths along with the underlying implementation that fills in
object_info->disk_sizepfor loose objects. Which I think is plenty for this particular script, which is about for-each-ref.It would be nice to have coverage of the
packed_object_info()code path, though. Back when it was added ina4ac106178(cat-file: add %(objectsize:disk) format atom, 2013-07-10), I cowardly punted on this, writing:This patch does not include any tests, as the exact numbers returned are volatile and subject to zlib and packing decisions. We cannot even reliably guarantee that the on-disk size is smaller than the object content (though in general this should be the case for non-trivial objects).
I don't think it's that big a deal, but I guess we could do something like:
prev=
git show-index <$pack_idx |
sort -n |
grep -A1 $oid |
while read ofs oid csum
do
Quoted 7 lines
test -n "$prev" && echo "$((ofs - prev))" prev=$ofsdone
It feels a little redundant with what Git is doing under the hood, but at least is exercising the code (and we're using the idx directly, so we're confirming that the revindex is right).
A generic object size function based on both methods could live in the test lib and be used for e.g. cat-file tests as well. Getting such a function polished and library-worthy is probably more work than I naively imagine, however -- due to our shunning of pipes alone.
Quoted 15 lines
Anyway, that is all way beyond the scope of your patch, but I wonder if it's worth doing on top.
t/t6300-for-each-ref.sh test_atom head push:strip=1 remotes/myfork/main · line 132
Quoted 6 lines
t/t6300-for-each-ref.sh +1
-1@@ -129,7 +129,7 @@ test_atom head push:strip=1 remotes/myfork/main test_atom head push:strip=-1 main test_atom head objecttype commit test_atom head objectsize $((131 + hexlen)) -test_atom head objectsize:disk $disklen +test_atom head objectsize:disk $(test_object_file_size refs/heads/main)These
test_object_file_sizecalls are happening outside of any test_expect_* block, so we'd miss failing exit codes (and also the helper is not &&-chained), and any stderr would leak to the output. That's probably OK in practice, though (if something goes wrong then the expected value output will be bogus and the test itself will fail).
Right. We could also set variables during setup, though, to put readers' minds at rest.
René
[PATCH 2/1] test-lib-functions: add object size functions
Add test_object_size and its helpers test_loose_object_size and test_packed_object_size, which allow determining the size of a Git object using only the low-level Git commands rev-parse and show-index.
Use it in t6300 to replace the bare-bones function test_object_file_size as a motivating example. There it provides the expected output of the high-level Git command for-each-ref.
---
So how about this? I'm a bit nervous about all the rules about output descriptors and error propagation and whatnot in the test library, but this implementation seems simple enough and might be useful in more than one test. No idea how to add support for alternate object directories, but I doubt we'll ever need it.
--- t/t6300-for-each-ref.sh | 16 ++++++-------- t/test-lib-functions.sh | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-)
t/t6300-for-each-ref.sh +7 -9
@@ -20,12 +20,6 @@ setdate_and_increment () { export GIT_COMMITTER_DATE GIT_AUTHOR_DATE } -test_object_file_size () { - oid=$(git rev-parse "$1") - path=".git/objects/$(test_oid_to_path $oid)" - test_file_size "$path" -} - test_expect_success setup ' # setup .mailmap cat >.mailmap <<-EOF &&
@@ -40,7 +34,11 @@ test_expect_success setup ' git branch -M main && setdate_and_increment && git tag -a -m "Tagging at $datestamp" testtag && + testtag_oid=$(git rev-parse refs/tags/testtag) && + testtag_disksize=$(test_object_size $testtag_oid) && git update-ref refs/remotes/origin/main main && + commit_oid=$(git rev-parse refs/heads/main) && + commit_disksize=$(test_object_size $commit_oid) && git remote add origin nowhere && git config branch.main.remote origin && git config branch.main.merge refs/heads/main &&
@@ -129,7 +127,7 @@ test_atom head push:strip=1 remotes/myfork/main test_atom head push:strip=-1 main test_atom head objecttype commit test_atom head objectsize $((131 + hexlen)) -test_atom head objectsize:disk $(test_object_file_size refs/heads/main) +test_atom head objectsize:disk $commit_disksize test_atom head deltabase $ZERO_OID test_atom head objectname $(git rev-parse refs/heads/main) test_atom head objectname:short $(git rev-parse --short refs/heads/main)
@@ -203,8 +201,8 @@ test_atom tag upstream '' test_atom tag push '' test_atom tag objecttype tag test_atom tag objectsize $((114 + hexlen)) -test_atom tag objectsize:disk $(test_object_file_size refs/tags/testtag) -test_atom tag '*objectsize:disk' $(test_object_file_size refs/heads/main) +test_atom tag objectsize:disk $testtag_disksize +test_atom tag '*objectsize:disk' $commit_disksize test_atom tag deltabase $ZERO_OID test_atom tag '*deltabase' $ZERO_OID test_atom tag objectname $(git rev-parse refs/tags/testtag)
t/test-lib-functions.sh +47 -0
53 lines not on this page · raw message
--
2.43.0
Re: [PATCH 2/1] test-lib-functions: add object size functions
Quoted 7 lines · René Scharfe
Add
test_object_sizeand its helperstest_loose_object_sizeandtest_packed_object_size, which allow determining the size of a Git object using only the low-level Git commands rev-parse and show-index.Use it in t6300 to replace the bare-bones function
test_object_file_sizeas a motivating example. There it provides the expected output of the high-level Git command for-each-ref.
This adds a packed-object function, but I doubt anybody actually calls it. If we're going to do that, it's probably worth adding some tests for "cat-file --batch-check" or similar.
At which point I wonder if rather than having a function for a single object, we are better off just testing the result of:
git cat-file --batch-all-objects --unordered --batch-check='%(objectsize:disk)'
against a single post-processed "show-index" invocation.
Quoted 5 lines
So how about this? I'm a bit nervous about all the rules about output descriptors and error propagation and whatnot in the test library, but this implementation seems simple enough and might be useful in more than one test. No idea how to add support for alternate object directories, but I doubt we'll ever need it.
I'm not sure that we need to do anything special with output redirection. Shouldn't these functions just send errors to stderr as usual? If they are run inside a test_expect block, that goes to descriptor 4 (which is either /dev/null or the original stderr, depending on whether "-v" was used).
t/test-lib-functions.sh test_oid_to_path () { · line 1740
Quoted 5 lines
t/test-lib-functions.sh +5
-0+test_loose_object_size () { + test "$#" -ne 1 && BUG "1 param" + local path=$(test_oid_to_path "$1") + test_file_size "$(git rev-parse --git-path "objects/$path")" 2>&4 +}
OK. We lose the exit code from "rev-parse" but that is probably OK for our purposes.
t/test-lib-functions.sh test_oid_to_path () { · line 1748
Quoted 7 lines
t/test-lib-functions.sh +7
-0+test_packed_object_size () { + test "$#" -ne 2 && BUG "2 params" + local oid=$1 idx=$2 packsize rawsz end + + packsize=$(test_file_size "${idx%.idx}.pack") + rawsz=$(test_oid rawsz) + end=$(($packsize - $rawsz))
OK, this $end is the magic required for the final entry. Makes sense.
t/test-lib-functions.sh test_oid_to_path () { · line 1762
Quoted 13 lines
t/test-lib-functions.sh +13
-0+ git show-index <"$idx" | + awk -v oid="$oid" -v end="$end" ' + $2 == oid {start = $1} + {offsets[$1] = 1} + END { + if (!start || start >= end) + exit 1 + for (o in offsets) + if (start < o && o < end) + end = o + print end - start + } + ' && return 0
I was confused at first, because I didn't see any sorting happening. But if I understand correctly, you're just looking for the smallest "end" that comes after the start of the object we're looking for. Which I think works.
-Peff
Re: [PATCH 2/1] test-lib-functions: add object size functions
Am 14.12.23 um 21:59 schrieb Jeff King:
Quoted 6 lines · René Scharfe
Quoted 5 lines
Add
test_object_sizeand its helperstest_loose_object_sizeandtest_packed_object_size, which allow determining the size of a Git object using only the low-level Git commands rev-parse and show-index.
Quoted 7 lines
Quoted 3 lines
Use it in t6300 to replace the bare-bones function
test_object_file_sizeas a motivating example. There it provides the expected output of the high-level Git command for-each-ref.This adds a packed-object function, but I doubt anybody actually calls it. If we're going to do that, it's probably worth adding some tests for "cat-file --batch-check" or similar.
Yes, and I was assuming that someone else would be eager to add such tests. *ahem*
Quoted 6 lines
At which point I wonder if rather than having a function for a single object, we are better off just testing the result of:
git cat-file --batch-all-objects --unordered --batch-check='%(objectsize:disk)'against a single post-processed "show-index" invocation.
Sure, we might want to optimize for bulk-processing and possibly end up only checking the size of single objects in t6300, making new library functions unnecessary.
When dumping size information of multiple objects, it's probably a good idea to include "%(objectname)" as well in the format.
You'd need one show-index call for each .idx file. A simple test would only have a single one; a library function might need to handle multiple packs.
Quoted 11 lines
Quoted 5 lines
So how about this? I'm a bit nervous about all the rules about output descriptors and error propagation and whatnot in the test library, but this implementation seems simple enough and might be useful in more than one test. No idea how to add support for alternate object directories, but I doubt we'll ever need it.
I'm not sure that we need to do anything special with output redirection. Shouldn't these functions just send errors to stderr as usual? If they are run inside a
test_expectblock, that goes to descriptor 4 (which is either/dev/nullor the original stderr, depending on whether "-v" was used).
Good point. My bad excuse is that I copied the redirection to fd 4 from test_grep.
Quoted 10 lines
Quoted 10 lines
t/test-lib-functions.sh +10
-0+ git show-index <"$idx" | + awk -v oid="$oid" -v end="$end" ' + $2 == oid {start = $1} + {offsets[$1] = 1} + END { + if (!start || start >= end) + exit 1 + for (o in offsets) + if (start < o && o < end) + end = o
Quoted 8 lines
t/test-lib-functions.sh test_oid_to_path () { · line 1762
Quoted 3 lines
t/test-lib-functions.sh +3
-0+ print end - start + } + ' && return 0I was confused at first, because I didn't see any sorting happening. But if I understand correctly, you're just looking for the smallest "end" that comes after the start of the object we're looking for. Which I think works.
Yes, calculating the minimum offset suffices when handling a single object -- no sorting required. For bulk mode we'd better sort, of course:
git show-index <"$idx" |
sort -n |
awk -v end="$end" '
NR > 1 {print oid, $1 - start}
{start = $1; oid = $2}
END {print oid, end - start}
'No idea how to make such a thing robust against malformed or truncated output from show-index, but perhaps that's not necessary, depending on how the result is used.
René
PATCH 1/1 t1006: add tests for %(objectsize:disk)
Quoted 6 lines · René Scharfe
Quoted 3 lines
This adds a packed-object function, but I doubt anybody actually calls it. If we're going to do that, it's probably worth adding some tests for "cat-file --batch-check" or similar.
Yes, and I was assuming that someone else would be eager to add such tests. *ahem*
:P OK, here it is. This can be its own topic, or go on top of the rs/t6300-compressed-size-fix branch.
Quoted 10 lines
Quoted 6 lines
At which point I wonder if rather than having a function for a single object, we are better off just testing the result of:
git cat-file --batch-all-objects --unordered --batch-check='%(objectsize:disk)'against a single post-processed "show-index" invocation.
Sure, we might want to optimize for bulk-processing and possibly end up only checking the size of single objects in t6300, making new library functions unnecessary.
So yeah, I think the approach here makes library functions unnecessary (and I see you already asked Junio to drop your patch 2).
Quoted 2 lines
When dumping size information of multiple objects, it's probably a good idea to include "%(objectname)" as well in the format.
Yep, definitely.
-- >8 --
Subject: [PATCH] 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).
---
I stole a bit of your awk. You can tell because I'd have written it in perl. ;)
t/t1006-cat-file.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
t/t1006-cat-file.sh +34 -0
@@ -1100,6 +1100,40 @@ test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor cmp expect actual ' +test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' ' + # our state has both loose and packed objects, + # so find both for our expected output + { + find .git/objects/?? -type f | + awk -F/ "{ print \$0, \$3\$4 }" | + while read path oid + do + size=$(test_file_size "$path") && + echo "$oid $size" || + return 1 + done && + rawsz=$(test_oid rawsz) && + find .git/objects/pack -name "*.idx" | + while read idx + do + git show-index <"$idx" >idx.raw && + sort -n <idx.raw >idx.sorted && + packsz=$(test_file_size "${idx%.idx}.pack") && + end=$((packsz - rawsz)) && + awk -v end="$end" " + NR > 1 { print oid, \$1 - start } + { start = \$1; oid = \$2 } + END { print oid, end - start } + " idx.sorted || + return 1 + done + } >expect.raw && + sort <expect.raw >expect && + git cat-file --batch-all-objects \ + --batch-check="%(objectname) %(objectsize:disk)" >actual && + test_cmp expect actual +' + test_expect_success 'set up replacement object' ' orig=$(git rev-parse HEAD) && git cat-file commit $orig >orig &&
Signature · 1 line
2.43.0.430.gaf21263e5d
Re: [PATCH] t1006: add tests for %(objectsize:disk)
Am 21.12.23 um 10:47 schrieb Jeff King:
Quoted 11 lines · René Scharfe
Quoted 7 lines
Quoted 4 lines
This adds a packed-object function, but I doubt anybody actually calls it. If we're going to do that, it's probably worth adding some tests for "cat-file --batch-check" or similar.
Yes, and I was assuming that someone else would be eager to add such tests. *ahem*
:P OK, here it is. This can be its own topic, or go on top of the rs/t6300-compressed-size-fix branch.
Great, thank you!
Quoted 20 lines
-- >8 --
Subject: [PATCH] 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).
Quoted 5 lines
Helped-by René Scharfe <[email protected]>Signed-off-by Jeff King <[email protected]>---
I stole a bit of your awk. You can tell because I'd have written it in perl. ;)
I think we can do it even in shell, especially if...
Quoted 27 lines
t/t1006-cat-file.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)t/t1006-cat-file.sh +15
-0@@ -1100,6 +1100,40 @@ test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor cmp expect actual ' +test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' ' + # our state has both loose and packed objects, + # so find both for our expected output + { + find .git/objects/?? -type f | + awk -F/ "{ print \$0, \$3\$4 }" | + while read path oid + do + size=$(test_file_size "$path") && + echo "$oid $size" || + return 1 + done && + rawsz=$(test_oid rawsz) && + find .git/objects/pack -name "*.idx" | + while read idx
t/t1006-cat-file.sh test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor · line 1127
Quoted 10 lines
t/t1006-cat-file.sh +10
-0+ do + git show-index <"$idx" >idx.raw && + sort -n <idx.raw >idx.sorted && + packsz=$(test_file_size "${idx%.idx}.pack") && + end=$((packsz - rawsz)) && + awk -v end="$end" " + NR > 1 { print oid, \$1 - start } + { start = \$1; oid = \$2 } + END { print oid, end - start } + " idx.sorted ||
... we stop slicing the data against the grain. Let's reverse the order (sort -r), then we don't need to carry the oid forward:
sort -nr <idx.raw >idx.sorted &&
packsz=$(test_file_size "${idx%.idx}.pack") &&
end=$((packsz - rawsz)) &&
awk -v end="$end" "
{ print \$2, end - \$1; end = \$1 }
" idx.sorted ||And at that point it should be easy to use a shell loop instead of awk:
while read start oid rest do size=$((end - start)) && end=$start && echo "$oid $size" || return 1 done <idx.sorted
t/t1006-cat-file.sh test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor · line 1131
Quoted 4 lines
t/t1006-cat-file.sh +4
-0+ return 1 + done + } >expect.raw && + sort <expect.raw >expect &&
The reversal above becomes irrelevant with that line, so the result in expect stays the same.
Should we deduplicate here, like cat-file does (i.e. use "sort -u")? Having the same object in multiple places for whatever reason would not be a cause for reporting an error in this test, I would think.
t/t1006-cat-file.sh test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor · line 1139
Quoted 8 lines
t/t1006-cat-file.sh +5
-0+ git cat-file --batch-all-objects \ + --batch-check="%(objectname) %(objectsize:disk)" >actual && + test_cmp expect actual +' + test_expect_success 'set up replacement object' ' orig=$(git rev-parse HEAD) && git cat-file commit $orig >orig &&
One more thing: We can do the work of the first awk invocation in the already existing loop as well:
t/t1006-cat-file.sh test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor · line 1114
Quoted 12 lines
t/t1006-cat-file.sh +12
-0+test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' ' + # our state has both loose and packed objects, + # so find both for our expected output + { + find .git/objects/?? -type f | + awk -F/ "{ print \$0, \$3\$4 }" | + while read path oid + do + size=$(test_file_size "$path") && + echo "$oid $size" || + return 1 + done &&
... but the substitutions are a bit awkward:
find .git/objects/?? -type f |
while read path
do
basename=${path##*/} &&
dirname=${path%/$basename} &&
oid="${dirname#.git/objects/}${basename}" &&
size=$(test_file_size "$path") &&
echo "$oid $size" ||
return 1
done &&The avoided awk invocation might be worth the trouble, though.
René
Re: [PATCH] t1006: add tests for %(objectsize:disk)
Quoted 2 lines · René Scharfe
I think we can do it even in shell, especially if... [...]
Yeah, your conversion looks accurate. I do wonder if it is worth golfing further, though. If it were a process invocation per object, I'd definitely say the efficiency gain is worth it. But dropping one process from the whole test isn't that exciting either way.
Quoted 11 lines
(sort -r), then we don't need to carry the oid forward:
sort -nr <idx.raw >idx.sorted && packsz=$(test_file_size "${idx%.idx}.pack") && end=$((packsz - rawsz)) && awk -v end="$end" " { print \$2, end - \$1; end = \$1 } " idx.sorted ||And at that point it should be easy to use a shell loop instead of awk:
Quoted 7 lines
while read start oid rest do size=$((end - start)) && end=$start && echo "$oid $size" || return 1 done <idx.sorted
The one thing I do like is that we don't have to escape anything inside an awk program that is forced to use double-quotes. ;)
Quoted 3 lines
Should we deduplicate here, like cat-file does (i.e. use "sort -u")? Having the same object in multiple places for whatever reason would not be a cause for reporting an error in this test, I would think.
No, for the reasons I said in the commit message: if an object exists in multiple places the test is already potentially invalid, as Git does not promise which version it will use. So it might work racily, or it might work for now but be fragile. By not de-duplicating, we make sure the test's assumption holds.
Quoted 16 lines
One more thing: We can do the work of the first awk invocation in the already existing loop as well:
[...]
... but the substitutions are a bit awkward:find .git/objects/?? -type f | while read path do basename=${path##*/} && dirname=${path%/$basename} && oid="${dirname#.git/objects/}${basename}" && size=$(test_file_size "$path") && echo "$oid $size" || return 1 done &&
Quoted 1 line
The avoided awk invocation might be worth the trouble, though.
Yeah, I briefly considered whether it would be possible in pure shell, but didn't get very far before assuming it was going to be ugly. Thank you for confirming. ;)
Again, if we were doing one awk per object, I'd try to avoid it. But since we can cover all objects in a single pass, I think it's OK.
-Peff
Re: [PATCH] t1006: add tests for %(objectsize:disk)
Am 21.12.23 um 22:30 schrieb Jeff King:
Quoted 9 lines · René Scharfe
Quoted 3 lines
I think we can do it even in shell, especially if... [...]
Yeah, your conversion looks accurate. I do wonder if it is worth golfing further, though. If it were a process invocation per object, I'd definitely say the efficiency gain is worth it. But dropping one process from the whole test isn't that exciting either way.
Fair enough.
Quoted 20 lines
Quoted 12 lines
(sort -r), then we don't need to carry the oid forward:
sort -nr <idx.raw >idx.sorted && packsz=$(test_file_size "${idx%.idx}.pack") && end=$((packsz - rawsz)) && awk -v end="$end" " { print \$2, end - \$1; end = \$1 } " idx.sorted ||And at that point it should be easy to use a shell loop instead of awk:
Quoted 7 lines
while read start oid rest do size=$((end - start)) && end=$start && echo "$oid $size" || return 1 done <idx.sorted
Quoted 2 lines
The one thing I do like is that we don't have to escape anything inside an awk program that is forced to use double-quotes. ;)
For me it's processing the data in the "correct" order (descending, i.e. starting at the end, which we have to calculate first anyway based on the size).
Quoted 9 lines
Quoted 3 lines
Should we deduplicate here, like cat-file does (i.e. use "sort -u")? Having the same object in multiple places for whatever reason would not be a cause for reporting an error in this test, I would think.
No, for the reasons I said in the commit message: if an object exists in multiple places the test is already potentially invalid, as Git does not promise which version it will use. So it might work racily, or it might work for now but be fragile. By not de-duplicating, we make sure the test's assumption holds.
Oh, skipped that paragraph. Still I don't see how a duplicate object would necessarily invalidate t1006. The comment for the test "cat-file --batch-all-objects shows all objects" a few lines above indicates that it's picky about the provenance of objects, but it uses a separate repository. I can't infer the same requirement for the root repo, but we already established that I can't read.
Anyway, if someone finds a use for git repack without -d or git unpack-objects or whatever else causes duplicates in the root repository of t1006 then they can try to reverse your ban with concrete arguments.
René
PATCH v2 1/1 t1006: add tests for %(objectsize:disk)
Quoted 8 lines · René Scharfe
Quoted 8 lines
Quoted 7 lines
while read start oid rest do size=$((end - start)) && end=$start && echo "$oid $size" || return 1 done <idx.sorted
Quoted 6 lines
Quoted 2 lines
The one thing I do like is that we don't have to escape anything inside an awk program that is forced to use double-quotes. ;)
For me it's processing the data in the "correct" order (descending, i.e. starting at the end, which we have to calculate first anyway based on the size).
That was one thing that I thought made it more complicated. The obvious order to me is start-to-end in the pack. But I do agree that going in reverse order makes things much simpler, as we compute the size of each entry as we see it (and so there are fewer special cases).
So I'm convinced that it's worth switching. Here's a v2 with your suggestion.
-- >8 --
Subject: 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).
--- t/t1006-cat-file.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
t/t1006-cat-file.sh +36 -0
@@ -1100,6 +1100,42 @@ test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor cmp expect actual ' +test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' ' + # our state has both loose and packed objects, + # so find both for our expected output + { + find .git/objects/?? -type f | + awk -F/ "{ print \$0, \$3\$4 }" | + while read path oid + do + size=$(test_file_size "$path") && + echo "$oid $size" || + return 1 + done && + rawsz=$(test_oid rawsz) && + find .git/objects/pack -name "*.idx" | + while read idx + do + git show-index <"$idx" >idx.raw && + sort -nr <idx.raw >idx.sorted && + packsz=$(test_file_size "${idx%.idx}.pack") && + end=$((packsz - rawsz)) && + while read start oid rest + do + size=$((end - start)) && + end=$start && + echo "$oid $size" || + return 1 + done <idx.sorted || + return 1 + done + } >expect.raw && + sort <expect.raw >expect && + git cat-file --batch-all-objects \ + --batch-check="%(objectname) %(objectsize:disk)" >actual && + test_cmp expect actual +' + test_expect_success 'set up replacement object' ' orig=$(git rev-parse HEAD) && git cat-file commit $orig >orig &&
Signature · 1 line
2.43.0.448.g93112243fb
Re: [PATCH v2] t1006: add tests for %(objectsize:disk)
Am 23.12.23 um 11:09 schrieb Jeff King:
Quoted 42 lines
--- t/t1006-cat-file.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)t/t1006-cat-file.sh +29
-0@@ -1100,6 +1100,42 @@ test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor cmp expect actual ' +test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' ' + # our state has both loose and packed objects, + # so find both for our expected output + { + find .git/objects/?? -type f | + awk -F/ "{ print \$0, \$3\$4 }" | + while read path oid + do + size=$(test_file_size "$path") && + echo "$oid $size" || + return 1 + done && + rawsz=$(test_oid rawsz) && + find .git/objects/pack -name "*.idx" | + while read idx + do + git show-index <"$idx" >idx.raw && + sort -nr <idx.raw >idx.sorted && + packsz=$(test_file_size "${idx%.idx}.pack") && + end=$((packsz - rawsz)) && + while read start oid rest + do + size=$((end - start)) && + end=$start && + echo "$oid $size" || + return 1 + done <idx.sorted || + return 1 + done
t/t1006-cat-file.sh test_expect_success 'cat-file --batch="batman" with --batch-all-objects will wor · line 1141
Quoted 10 lines
t/t1006-cat-file.sh +7
-0+ } >expect.raw && + sort <expect.raw >expect && + git cat-file --batch-all-objects \ + --batch-check="%(objectname) %(objectsize:disk)" >actual && + test_cmp expect actual +' + test_expect_success 'set up replacement object' ' orig=$(git rev-parse HEAD) && git cat-file commit $orig >orig &&
Looks good to me.
René
Re: [PATCH] t1006: add tests for %(objectsize:disk)
In reply to René ScharfeQuoted 10 lines · René Scharfe
Quoted 9 lines
Quoted 3 lines
Should we deduplicate here, like cat-file does (i.e. use "sort -u")? Having the same object in multiple places for whatever reason would not be a cause for reporting an error in this test, I would think.
No, for the reasons I said in the commit message: if an object exists in multiple places the test is already potentially invalid, as Git does not promise which version it will use. So it might work racily, or it might work for now but be fragile. By not de-duplicating, we make sure the test's assumption holds.
Quoted 6 lines
Oh, skipped that paragraph. Still I don't see how a duplicate object would necessarily invalidate t1006. The comment for the test "cat-file --batch-all-objects shows all objects" a few lines above indicates that it's picky about the provenance of objects, but it uses a separate repository. I can't infer the same requirement for the root repo, but we already established that I can't read.
The cat-file documentation explicitly calls this situation out:
Note also that multiple copies of an object may be present in the object database; in this case, it is undefined which copy’s size or delta base will be reported.
So if t1006 were to grow such a duplicate object, what will happen? If we de-dup in the new test, then we might end up mentioning the same copy (and the test passes), or we might not (and the test fails). But much worse, the results might be racy (depending on how cat-file happens to decide which one to use). By no de-duping, then the test will reliably fail and the author can decide how to handle it then.
IOW it is about failing immediately and predictably rather than letting a future change to sneak a race or other accident-waiting-to-happen into t1006.
Quoted 4 lines
Anyway, if someone finds a use for git repack without -d or git unpack-objects or whatever else causes duplicates in the root repository of t1006 then they can try to reverse your ban with concrete arguments.
In the real world, the most common way to get a duplicate is to fetch or push into a repository, such that:
1. There are enough objects to retain the pack (100 by default)
2. There's a thin delta in the on-the-wire pack (i.e., a delta against a base that the sender knows the receiver has, but which isn't itself sent).
Then "index-pack --fix-thin" will complete the on-disk pack by storing a copy of the base object in it. And now we have it in two packs (and if it's a delta or loose in the original, the size will be different).
-Peff
Re: [PATCH] t1006: add tests for %(objectsize:disk)
Am 23.12.23 um 11:18 schrieb Jeff King:
Quoted 46 lines · René Scharfe
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Should we deduplicate here, like cat-file does (i.e. use "sort -u")? Having the same object in multiple places for whatever reason would not be a cause for reporting an error in this test, I would think.
No, for the reasons I said in the commit message: if an object exists in multiple places the test is already potentially invalid, as Git does not promise which version it will use. So it might work racily, or it might work for now but be fragile. By not de-duplicating, we make sure the test's assumption holds.
Quoted 6 lines
Oh, skipped that paragraph. Still I don't see how a duplicate object would necessarily invalidate t1006. The comment for the test "cat-file --batch-all-objects shows all objects" a few lines above indicates that it's picky about the provenance of objects, but it uses a separate repository. I can't infer the same requirement for the root repo, but we already established that I can't read.
The cat-file documentation explicitly calls this situation out:
Note also that multiple copies of an object may be present in the object database; in this case, it is undefined which copy’s size or delta base will be reported.
So if t1006 were to grow such a duplicate object, what will happen? If we de-dup in the new test, then we might end up mentioning the same copy (and the test passes), or we might not (and the test fails). But much worse, the results might be racy (depending on how cat-file happens to decide which one to use). By no de-duping, then the test will reliably fail and the author can decide how to handle it then.
IOW it is about failing immediately and predictably rather than letting a future change to sneak a race or other accident-waiting-to-happen into t1006.
Quoted 4 lines
Anyway, if someone finds a use for git repack without -d or git unpack-objects or whatever else causes duplicates in the root repository of t1006 then they can try to reverse your ban with concrete arguments.
In the real world, the most common way to get a duplicate is to fetch or push into a repository, such that:
1. There are enough objects to retain the pack (100 by default)
Quoted 7 lines
2. There's a thin delta in the on-the-wire pack (i.e., a delta against a base that the sender knows the receiver has, but which isn't itself sent).
Then "index-pack --fix-thin" will complete the on-disk pack by storing a copy of the base object in it. And now we have it in two packs (and if it's a delta or loose in the original, the size will be different).
I think I get it now. The size possibly being different is crucial. cat-file deduplicates based on object ID alone. sort -u in t1006 would deduplicate based on object ID and size, meaning that it would only remove duplicates of the same size. Emulating the deduplication of cat-file is also possible, but would introduce the race you mentioned.
However, even removing only same-size duplicates is unreliable because there is no guarantee that the same object has the same size in different packs. Adding a new object that is a better delta base would change the size.
So, deduplicating based on object ID and size is sound for any particular run, but sizes are not stable and thus we need to know if the tests do something that adds duplicates of any size.
René
Quoted 7 lines · René Scharfe
--- >8 ---
Subject: [PATCH] t6300: avoid hard-coding object sizes
f4ee22b526(ref-filter: add tests for objectsize:disk, 2018-12-24) hard-coded the expected object sizes. Coincidentally the size of commit and tag is the same with zlib at the default compression level.
Quoted 8 lines
1f5f8f3e85(t6300: abstract away SHA-1-specific constants, 2020-02-22) encoded the sizes as a single value, which coincidentally also works with sha256.Different compression libraries like zlib-ng may arrive at different values. Get them from the file system instead of hard-coding them to make switching the compression library (or changing the compression level) easier.
This definitely seems like the right thing to do. I was a bit lazy at the time and probably could have improved it then, but it's at least good that we're doing it now.
Signature · 2 lines
brian m. carlson (he/him or they/them) Toronto, Ontario, CA
Quoted 2 lines · René Scharfe
Or we could get the sizes of the objects by checking their files, which would not require hard-coding anymore. Patch below.
That was my first reaction to seeing the original report. It is a bit surprising that the necessary fix is so small and makes me wonder why we initially did the hardcoded values, which feels more work to initially write the test vector.
Looking good. Thanks.
Quoted 75 lines
--- >8 ---
Subject: [PATCH] t6300: avoid hard-coding object sizes
f4ee22b526(ref-filter: add tests for objectsize:disk, 2018-12-24) hard-coded the expected object sizes. Coincidentally the size of commit and tag is the same with zlib at the default compression level.
1f5f8f3e85(t6300: abstract away SHA-1-specific constants, 2020-02-22) encoded the sizes as a single value, which coincidentally also works with sha256.Different compression libraries like zlib-ng may arrive at different values. Get them from the file system instead of hard-coding them to make switching the compression library (or changing the compression level) easier.
Reported-by Ondrej Pohorelsky <[email protected]>Signed-off-by René Scharfe <[email protected]>--- t/t6300-for-each-ref.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)t/t6300-for-each-ref.sh +9
-10@@ -20,12 +20,13 @@ setdate_and_increment () { export GIT_COMMITTER_DATE GIT_AUTHOR_DATE } -test_expect_success setup ' - test_oid_cache <<-EOF && - disklen sha1:138 - disklen sha256:154 - EOF +test_object_file_size () { + oid=$(git rev-parse "$1") + path=".git/objects/$(test_oid_to_path $oid)" + test_file_size "$path" +} +test_expect_success setup ' # setup .mailmap cat >.mailmap <<-EOF && A Thor <[email protected]> A U Thor <[email protected]>@@ -94,7 +95,6 @@ test_atom () { } hexlen=$(test_oid hexsz) -disklen=$(test_oid disklen) test_atom head refname refs/heads/main test_atom head refname: refs/heads/main@@ -129,7 +129,7 @@ test_atom head push:strip=1 remotes/myfork/main test_atom head push:strip=-1 main test_atom head objecttype commit test_atom head objectsize $((131 + hexlen)) -test_atom head objectsize:disk $disklen +test_atom head objectsize:disk $(test_object_file_size refs/heads/main) test_atom head deltabase $ZERO_OID test_atom head objectname $(git rev-parse refs/heads/main) test_atom head objectname:short $(git rev-parse --short refs/heads/main)@@ -203,8 +203,8 @@ test_atom tag upstream '' test_atom tag push '' test_atom tag objecttype tag test_atom tag objectsize $((114 + hexlen)) -test_atom tag objectsize:disk $disklen -test_atom tag '*objectsize:disk' $disklen +test_atom tag objectsize:disk $(test_object_file_size refs/tags/testtag) +test_atom tag '*objectsize:disk' $(test_object_file_size refs/heads/main) test_atom tag deltabase $ZERO_OID test_atom tag '*deltabase' $ZERO_OID test_atom tag objectname $(git rev-parse refs/tags/testtag) --2.43.0