python3Packages.instructor: 1.6.4 -> 1.72 (#378975)
[NixPkgs.git] / pkgs / build-support / fetchsvn / nix-prefetch-svn
blob1164b484373aba60174c6d352d0c5492cbee1914
1 #! /bin/sh -e
3 url=$1
4 rev=$2
5 expHash=$3
7 hashType=$NIX_HASH_ALGO
8 if test -z "$hashType"; then
9 hashType=sha256
11 if test -z "$hashFormat"; then
12 hashFormat=--base32
15 if test -z "$url"; then
16 echo "syntax: nix-prefetch-svn URL [REVISION [EXPECTED-HASH]]" >&2
17 exit 1
20 test -n "$rev" || rev="HEAD"
22 repoName=$(echo $url | sed '
23 s,.*/\([^/]\+\)/trunk/*$,\1,;t
24 s,.*/\([^/]\+\)/branches/\([^/]\+\)/*$,\1-\2,;t
25 s,.*/\([^/]\+\)/tags/\([^/]\+\)/*$,\1-\2,;t
26 s,.*/\([^/]\+\)/*$,\1,;t
28 dstFile=$repoName-r$rev
30 # If the hash was given, a file with that hash may already be in the
31 # store.
32 if test -n "$expHash"; then
33 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" $dstFile)
34 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
35 finalPath=
37 hash=$expHash
41 # If we don't know the hash or a path with that hash doesn't exist,
42 # download the file and add it to the store.
43 if test -z "$finalPath"; then
44 # nix>=2.20 rejects adding symlinked paths to the store, so use realpath
45 # to resolve to a physical path. https://github.com/NixOS/nix/issues/11941
46 tmpPath="$(realpath "$(mktemp -d --tmpdir svn-checkout-tmp-XXXXXXXX)")"
47 trap "rm -rf \"$tmpPath\"" EXIT
49 tmpFile="$tmpPath/$dstFile"
51 # Perform the checkout.
52 if test "$NIX_PREFETCH_SVN_LEAVE_DOT_SVN" != 1
53 then
54 command="export"
55 else
56 command="checkout"
59 echo p | svn "$command" --quiet -r "$rev" "$url" "$tmpFile" >&2
60 echo "svn revision is $(svn info -r "$rev" "$url" | grep "Revision: " | cut -d' ' -f2)"
62 # Compute the hash.
63 hash=$(nix-hash --type $hashType $hashFormat $tmpFile)
64 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
66 # Add the downloaded file to the Nix store.
67 finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
69 if test -n "$expHash" -a "$expHash" != "$hash"; then
70 echo "hash mismatch for URL \`$url'"
71 exit 1
75 if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
77 echo $hash
79 if test -n "$PRINT_PATH"; then
80 echo $finalPath