lol
1#! /bin/sh -e
2
3url=$1
4rev=$2
5expHash=$3
6
7hashType=$NIX_HASH_ALGO
8if test -z "$hashType"; then
9 hashType=sha256
10fi
11if test -z "$hashFormat"; then
12 hashFormat=--base32
13fi
14
15if test -z "$url"; then
16 echo "syntax: nix-prefetch-bzr URL [REVISION [EXPECTED-HASH]]" >&2
17 exit 1
18fi
19
20revarg="-r $rev"
21test -n "$rev" || revarg=""
22
23repoName=$(echo $url | sed '
24 s,.*/\([^/]\+\)/trunk/*$,\1,;t
25 s,.*/\([^/]\+\)/branches/\([^/]\+\)/*$,\1-\2,;t
26 s,.*/\([^/]\+\)/tags/\([^/]\+\)/*$,\1-\2,;t
27 s,.*/\([^/]\+\)/*$,\1,;t
28')
29dstFile="bzr-export"
30
31# If the hash was given, a file with that hash may already be in the
32# store.
33if test -n "$expHash"; then
34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" $dstFile)
35 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
36 finalPath=
37 fi
38 hash=$expHash
39fi
40
41
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.
44if test -z "$finalPath"; then
45 tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/bzr-checkout-tmp-XXXXXXXX")"
46 trap "rm -rf \"$tmpPath\"" EXIT
47
48 tmpFile="$tmpPath/$dstFile"
49
50 # Perform the checkout.
51 bzr -Ossl.cert_reqs=none export $revarg --format=dir "$tmpFile" "$url"
52
53 echo "bzr revision is $(bzr revno $revarg "$url")"
54
55 # Compute the hash.
56 hash=$(nix-hash --type $hashType $hashFormat $tmpFile)
57 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
58
59 # Add the downloaded file to the Nix store.
60 finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
61
62 if test -n "$expHash" -a "$expHash" != "$hash"; then
63 echo "hash mismatch for URL \`$url'"
64 exit 1
65 fi
66fi
67
68if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
69
70echo $hash
71
72if test -n "$PRINT_PATH"; then
73 echo $finalPath
74fi