1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 14:32:42 +01:00
nix/tests/functional/git-hashing/simple-common.sh
John Ericson 13b44742a4 JSON for Hash now has to be Base16
Fix #14532.

As discussed on the call today:

1. We'll stick with `format = "base16"` and `hash = "<hash>"`, not do
   `base16 = "<hash>"`, in order to be forward compatible with
   supporting more versioning formats.

   The motivation we discussed for someday *possibly* doing this is
   making it easier to write very slap-dash lang2nix tools that create
   (not consume) derivations with dynamic derivations.

2. We will remove support for non-base16 (and make that the default, not
   base64) in `Hash`, so this is strictly forward contingency, *not*
   yet something we support. (And also not something we have concrete
   plans to start supporting.)
2025-11-10 16:43:54 -05:00

104 lines
2.5 KiB
Bash

# shellcheck shell=bash
source common.sh
# Assert is set
[[ ${hashAlgo+x} ]]
repo="$TEST_ROOT/scratch"
initRepo () {
git init "$repo" --object-format="$hashAlgo"
git -C "$repo" config user.email "you@example.com"
git -C "$repo" config user.name "Your Name"
}
# Compare Nix's and git's implementation of git hashing
try () {
local expected="$1"
local hash
hash=$(nix hash path --mode git --format base16 --algo "$hashAlgo" "$TEST_ROOT/hash-path")
[[ "$hash" == "$expected" ]]
git -C "$repo" rm -rf hash-path || true
cp -r "$TEST_ROOT/hash-path" "$repo/hash-path"
git -C "$repo" add hash-path
git -C "$repo" commit -m "x"
git -C "$repo" status
local hash2
hash2=$(git -C "$repo" rev-parse HEAD:hash-path)
[[ "$hash2" = "$expected" ]]
}
# Check Nix added object has matching git hash
try2 () {
local hashPath="$1"
local expected="$2"
local path
path=$(nix store add --mode git --hash-algo "$hashAlgo" "$repo/$hashPath")
git -C "$repo" add "$hashPath"
git -C "$repo" commit -m "x"
git -C "$repo" status
local hashFromGit
hashFromGit=$(git -C "$repo" rev-parse "HEAD:$hashPath")
[[ "$hashFromGit" == "$expected" ]]
nix path-info --json "$path" | jq -e \
--arg algo "$hashAlgo" \
--arg hash "$hashFromGit" \
'.[].ca == {
method: "git",
hash: {
algorithm: $algo,
format: "base16",
hash: $hash
},
}'
}
test0 () {
rm -rf "$TEST_ROOT/hash-path"
echo "Hello World" > "$TEST_ROOT/hash-path"
}
test1 () {
rm -rf "$TEST_ROOT/hash-path"
mkdir "$TEST_ROOT/hash-path"
echo "Hello World" > "$TEST_ROOT/hash-path/hello"
echo "Run Hello World" > "$TEST_ROOT/hash-path/executable"
chmod +x "$TEST_ROOT/hash-path/executable"
}
test2 () {
rm -rf "$repo/dummy1"
echo Hello World! > "$repo/dummy1"
}
test3 () {
rm -rf "$repo/dummy2"
mkdir -p "$repo/dummy2"
echo Hello World! > "$repo/dummy2/hello"
}
test4 () {
rm -rf "$repo/dummy3"
mkdir -p "$repo/dummy3"
mkdir -p "$repo/dummy3/dir"
touch "$repo/dummy3/dir/file"
echo Hello World! > "$repo/dummy3/dir/file"
touch "$repo/dummy3/dir/executable"
chmod +x "$repo/dummy3/dir/executable"
echo Run Hello World! > "$repo/dummy3/dir/executable"
}
test5 () {
rm -rf "$repo/dummy4"
mkdir -p "$repo/dummy4"
mkdir -p "$repo/dummy4/dir"
touch "$repo/dummy4/dir/file"
ln -s './hello/world.txt' "$repo/dummy4/dir/symlink"
}