mirror of
https://github.com/NixOS/nix.git
synced 2025-11-22 10:19:36 +01:00
Implement support for Git hashing with SHA-256
SHA-256 is Git's next hash algorithm. The world is still basically stuck on SHA-1 with git, but shouldn't be. We can at least do our part to get ready. On the C++ implementation side, only a little bit of generalization was needed, and that was fairly straight-forward. The tests (unit and system) were actually bigger, and care was taken to make sure they were all cover both algorithms equally.
This commit is contained in:
parent
7f4acb9f10
commit
d21e3f88ec
20 changed files with 350 additions and 181 deletions
44
src/libutil-tests/data/git/check-data.sh
Normal file → Executable file
44
src/libutil-tests/data/git/check-data.sh
Normal file → Executable file
|
|
@ -2,30 +2,34 @@
|
|||
|
||||
set -eu -o pipefail
|
||||
|
||||
export TEST_ROOT=$(realpath ${TMPDIR:-/tmp}/nix-test)/git-hashing/check-data
|
||||
mkdir -p $TEST_ROOT
|
||||
TEST_ROOT=$(realpath "${TMPDIR:-/tmp}/nix-test")/git-hashing/check-data
|
||||
export TEST_ROOT
|
||||
mkdir -p "$TEST_ROOT"
|
||||
|
||||
repo="$TEST_ROOT/scratch"
|
||||
git init "$repo"
|
||||
for hash in sha1 sha256; do
|
||||
repo="$TEST_ROOT/scratch-$hash"
|
||||
git init "$repo" --object-format="$hash"
|
||||
|
||||
git -C "$repo" config user.email "you@example.com"
|
||||
git -C "$repo" config user.name "Your Name"
|
||||
git -C "$repo" config user.email "you@example.com"
|
||||
git -C "$repo" config user.name "Your Name"
|
||||
|
||||
# `-w` to write for tree test
|
||||
freshlyAddedHash=$(git -C "$repo" hash-object -w -t blob --stdin < "./hello-world.bin")
|
||||
encodingHash=$(sha1sum -b < "./hello-world-blob.bin" | head -c 40)
|
||||
# `-w` to write for tree test
|
||||
freshlyAddedHash=$(git -C "$repo" hash-object -w -t blob --stdin < "./hello-world.bin")
|
||||
encodingHash=$("${hash}sum" -b < "./hello-world-blob.bin" | sed 's/ .*//')
|
||||
|
||||
# If the hashes match, then `hello-world-blob.bin` must be the encoding
|
||||
# of `hello-world.bin`.
|
||||
[[ "$encodingHash" == "$freshlyAddedHash" ]]
|
||||
# If the hashes match, then `hello-world-blob.bin` must be the encoding
|
||||
# of `hello-world.bin`.
|
||||
[[ "$encodingHash" == "$freshlyAddedHash" ]]
|
||||
|
||||
# Create empty directory object for tree test
|
||||
echo -n | git -C "$repo" hash-object -w -t tree --stdin
|
||||
# Create empty directory object for tree test
|
||||
echo -n | git -C "$repo" hash-object -w -t tree --stdin
|
||||
|
||||
# Relies on both child hashes already existing in the git store
|
||||
freshlyAddedHash=$(git -C "$repo" mktree < "./tree.txt")
|
||||
encodingHash=$(sha1sum -b < "./tree.bin" | head -c 40)
|
||||
# Relies on both child hashes already existing in the git store
|
||||
tree=tree-${hash}
|
||||
freshlyAddedHash=$(git -C "$repo" mktree < "${tree}.txt")
|
||||
encodingHash=$("${hash}sum" -b < "${tree}.bin" | sed 's/ .*//')
|
||||
|
||||
# If the hashes match, then `tree.bin` must be the encoding of the
|
||||
# directory denoted by `tree.txt` interpreted as git directory listing.
|
||||
[[ "$encodingHash" == "$freshlyAddedHash" ]]
|
||||
# If the hashes match, then `tree.bin` must be the encoding of the
|
||||
# directory denoted by `tree.txt` interpreted as git directory listing.
|
||||
[[ "$encodingHash" == "$freshlyAddedHash" ]]
|
||||
done
|
||||
|
|
|
|||
BIN
src/libutil-tests/data/git/tree-sha256.bin
Normal file
BIN
src/libutil-tests/data/git/tree-sha256.bin
Normal file
Binary file not shown.
4
src/libutil-tests/data/git/tree-sha256.txt
Normal file
4
src/libutil-tests/data/git/tree-sha256.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
100644 blob ce60f5ad78a08ac24872ef74d78b078f077be212e7a246893a1a5d957dfbc8b1 Foo
|
||||
100755 blob ce60f5ad78a08ac24872ef74d78b078f077be212e7a246893a1a5d957dfbc8b1 bAr
|
||||
040000 tree 6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 baZ
|
||||
120000 blob ce60f5ad78a08ac24872ef74d78b078f077be212e7a246893a1a5d957dfbc8b1 quuX
|
||||
Loading…
Add table
Add a link
Reference in a new issue