lol

nix-prefetch-hg: Various bash style improvements, fixes #9511

authored by

Benjamin Staffin and committed by
Rok Garbas
fc85f1be ef4ea6d6

+34 -34
+34 -34
pkgs/build-support/fetchhg/nix-prefetch-hg
··· 5 5 rev=$2 6 6 expHash=$3 7 7 8 - hashType=$NIX_HASH_ALGO 9 - if test -z "$hashType"; then 10 - hashType=sha256 11 - fi 12 - if test -z "$hashFormat"; then 13 - hashFormat=--base32 14 - fi 8 + hashType="${NIX_HASH_ALGO:-sha256}" 9 + hashFormat=${hashFormat:-"--base32"} 10 + rev="${rev:-tip}" 11 + 12 + LOG() { 13 + echo "$@" >&2 14 + } 15 15 16 - if test -z "$url"; then 17 - echo "syntax: nix-prefetch-hg URL [rev [EXPECTED-HASH]]" >&2 18 - exit 1 16 + die() { 17 + LOG "$@" 18 + exit 1 19 + } 20 + 21 + if [[ -z "$url" || "$url" == "--help" ]]; then 22 + die "Usage: nix-prefetch-hg URL [rev [EXPECTED-HASH]]" 19 23 fi 20 24 21 - if test "$fetchSubrepos" == 1; then 25 + if [[ "${fetchSubrepos:-0}" == 1 ]]; then 22 26 subrepoClause=S 23 27 else 24 28 subrepoClause= 25 29 fi 26 30 27 - test -n "$rev" || rev="tip" 28 - 29 - 30 31 # If the hash was given, a file with that hash may already be in the 31 32 # store. 32 - if test -n "$expHash"; then 33 + if [[ -n "$expHash" ]]; then 33 34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" hg-archive) 34 35 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then 35 36 finalPath= 36 37 fi 37 - hash=$expHash 38 + hash="$expHash" 38 39 fi 39 40 40 41 41 42 # If we don't know the hash or a path with that hash doesn't exist, 42 43 # download the file and add it to the store. 43 - if test -z "$finalPath"; then 44 + if [[ -z "$finalPath" ]]; then 44 45 45 46 tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/hg-checkout-tmp-XXXXXXXX")" 46 - trap "rm -rf \"$tmpPath\"" EXIT 47 + cleanup() { x=$?; rm -rf "$tmpPath"; exit $x; }; trap cleanup EXIT 47 48 48 49 tmpArchive="$tmpPath/hg-archive" 49 50 50 51 # Perform the checkout. 51 - if [[ $url != /* ]]; then 52 - tmpClone=$tmpPath/hg-clone 53 - hg clone -q -y -U "$url" $tmpClone >&2 52 + if [[ "$url" != /* ]]; then 53 + tmpClone="$tmpPath/hg-clone" 54 + hg clone -q -y -U "$url" "$tmpClone" >&2 54 55 else 55 56 tmpClone=$url 56 57 fi 57 - hg archive -q$subrepoClause -y -r "$rev" --cwd $tmpClone $tmpArchive 58 - rm -f $tmpArchive/.hg_archival.txt 58 + hg archive -q$subrepoClause -y -r "$rev" --cwd "$tmpClone" "$tmpArchive" 59 + rm -f "$tmpArchive/.hg_archival.txt" 59 60 60 - echo "hg revision is $(cd $tmpClone; hg id -r "$rev" -i)" 61 + LOG "hg revision is $(cd "$tmpClone"; hg id -r "$rev" -i)" 61 62 62 63 # Compute the hash. 63 - hash=$(nix-hash --type $hashType $hashFormat $tmpArchive) 64 - if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi 64 + hash=$(nix-hash --type "$hashType" "$hashFormat" "$tmpArchive") 65 + if [[ -z "$QUIET" ]]; then LOG "hash is $hash"; fi 65 66 66 67 # Add the downloaded file to the Nix store. 67 - finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpArchive) 68 + finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpArchive") 68 69 69 - if test -n "$expHash" -a "$expHash" != "$hash"; then 70 - echo "hash mismatch for URL \`$url'" 71 - exit 1 70 + if [[ -n "$expHash" && "$expHash" != "$hash" ]]; then 71 + die "ERROR: hash mismatch for URL \`$url'" 72 72 fi 73 73 74 74 75 75 fi 76 76 77 - if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi 77 + if [[ -z "$QUIET" ]]; then LOG "path is $finalPath"; fi 78 78 79 - echo $hash 79 + echo "$hash" 80 80 81 - if test -n "$PRINT_PATH"; then 82 - echo $finalPath 81 + if [[ -n "$PRINT_PATH" ]]; then 82 + echo "$finalPath" 83 83 fi