2015-10-03 13:16:42 +01:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
set -e
|
2010-01-27 12:12:35 +00:00
|
|
|
|
|
|
|
url=$1
|
|
|
|
rev=$2
|
|
|
|
expHash=$3
|
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
hashType="${NIX_HASH_ALGO:-sha256}"
|
|
|
|
hashFormat=${hashFormat:-"--base32"}
|
|
|
|
rev="${rev:-tip}"
|
|
|
|
|
|
|
|
LOG() {
|
|
|
|
echo "$@" >&2
|
|
|
|
}
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
die() {
|
|
|
|
LOG "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ -z "$url" || "$url" == "--help" ]]; then
|
|
|
|
die "Usage: nix-prefetch-hg URL [rev [EXPECTED-HASH]]"
|
2010-01-27 12:12:35 +00:00
|
|
|
fi
|
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ "${fetchSubrepos:-0}" == 1 ]]; then
|
2015-04-01 15:02:07 +01:00
|
|
|
subrepoClause=S
|
|
|
|
else
|
|
|
|
subrepoClause=
|
|
|
|
fi
|
|
|
|
|
2010-01-27 12:12:35 +00:00
|
|
|
# If the hash was given, a file with that hash may already be in the
|
|
|
|
# store.
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ -n "$expHash" ]]; then
|
2010-01-27 12:12:35 +00:00
|
|
|
finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" hg-archive)
|
|
|
|
if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
|
|
|
|
finalPath=
|
|
|
|
fi
|
2015-08-29 04:28:32 +01:00
|
|
|
hash="$expHash"
|
2010-01-27 12:12:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# If we don't know the hash or a path with that hash doesn't exist,
|
|
|
|
# download the file and add it to the store.
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ -z "$finalPath" ]]; then
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2014-08-27 09:48:26 +01:00
|
|
|
tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/hg-checkout-tmp-XXXXXXXX")"
|
2015-08-29 04:28:32 +01:00
|
|
|
cleanup() { x=$?; rm -rf "$tmpPath"; exit $x; }; trap cleanup EXIT
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2014-08-25 14:00:55 +01:00
|
|
|
tmpArchive="$tmpPath/hg-archive"
|
2010-01-27 12:12:35 +00:00
|
|
|
|
|
|
|
# Perform the checkout.
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ "$url" != /* ]]; then
|
|
|
|
tmpClone="$tmpPath/hg-clone"
|
|
|
|
hg clone -q -y -U "$url" "$tmpClone" >&2
|
2011-04-11 13:24:57 +01:00
|
|
|
else
|
|
|
|
tmpClone=$url
|
|
|
|
fi
|
2015-08-29 04:28:32 +01:00
|
|
|
hg archive -q$subrepoClause -y -r "$rev" --cwd "$tmpClone" "$tmpArchive"
|
|
|
|
rm -f "$tmpArchive/.hg_archival.txt"
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
LOG "hg revision is $(cd "$tmpClone"; hg id -r "$rev" -i)"
|
2010-01-27 12:12:35 +00:00
|
|
|
|
|
|
|
# Compute the hash.
|
2015-08-29 04:28:32 +01:00
|
|
|
hash=$(nix-hash --type "$hashType" "$hashFormat" "$tmpArchive")
|
|
|
|
if [[ -z "$QUIET" ]]; then LOG "hash is $hash"; fi
|
2010-01-27 12:12:35 +00:00
|
|
|
|
|
|
|
# Add the downloaded file to the Nix store.
|
2015-08-29 04:28:32 +01:00
|
|
|
finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpArchive")
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ -n "$expHash" && "$expHash" != "$hash" ]]; then
|
|
|
|
die "ERROR: hash mismatch for URL \`$url'"
|
2010-01-27 12:12:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ -z "$QUIET" ]]; then LOG "path is $finalPath"; fi
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
echo "$hash"
|
2010-01-27 12:12:35 +00:00
|
|
|
|
2015-08-29 04:28:32 +01:00
|
|
|
if [[ -n "$PRINT_PATH" ]]; then
|
|
|
|
echo "$finalPath"
|
2010-01-27 12:12:35 +00:00
|
|
|
fi
|