forgejo-lts: 7.0.10 -> 7.0.11
[NixPkgs.git] / pkgs / build-support / fetchhg / nix-prefetch-hg
blob94c6b1ec6945cca85fb159f400e3237f0583bb5e
1 #! /usr/bin/env bash
2 set -e
4 url=$1
5 rev=$2
6 expHash=$3
8 hashType="${NIX_HASH_ALGO:-sha256}"
9 hashFormat=${hashFormat:-"--base32"}
10 rev="${rev:-tip}"
12 LOG() {
13 echo "$@" >&2
16 die() {
17 LOG "$@"
18 exit 1
21 if [[ -z "$url" || "$url" == "--help" ]]; then
22 die "Usage: nix-prefetch-hg URL [rev [EXPECTED-HASH]]"
25 if [[ "${fetchSubrepos:-0}" == 1 ]]; then
26 subrepoClause=S
27 else
28 subrepoClause=
31 # If the hash was given, a file with that hash may already be in the
32 # store.
33 if [[ -n "$expHash" ]]; then
34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" hg-archive)
35 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
36 finalPath=
38 hash="$expHash"
42 # If we don't know the hash or a path with that hash doesn't exist,
43 # download the file and add it to the store.
44 if [[ -z "$finalPath" ]]; then
46 tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/hg-checkout-tmp-XXXXXXXX")"
47 cleanup() { x=$?; rm -rf "$tmpPath"; exit $x; }; trap cleanup EXIT
49 tmpArchive="$tmpPath/hg-archive"
51 # Perform the checkout.
52 if [[ "$url" != /* ]]; then
53 tmpClone="$tmpPath/hg-clone"
54 hg clone -q -y -U "$url" "$tmpClone" >&2
55 else
56 tmpClone=$url
58 hg archive -q$subrepoClause -y -r "$rev" --cwd "$tmpClone" "$tmpArchive"
59 rm -f "$tmpArchive/.hg_archival.txt"
61 LOG "hg revision is $(cd "$tmpClone"; hg id -r "$rev" -i)"
63 # Compute the hash.
64 hash=$(nix-hash --type "$hashType" "$hashFormat" "$tmpArchive")
65 if [[ -z "$QUIET" ]]; then LOG "hash is $hash"; fi
67 # Add the downloaded file to the Nix store.
68 finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpArchive")
70 if [[ -n "$expHash" && "$expHash" != "$hash" ]]; then
71 die "ERROR: hash mismatch for URL \`$url'"
77 if [[ -z "$QUIET" ]]; then LOG "path is $finalPath"; fi
79 echo "$hash"
81 if [[ -n "$PRINT_PATH" ]]; then
82 echo "$finalPath"