Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1#! /bin/sh -e
2
3cvsRoot=$1
4module=$2
5tag=$3
6expHash=$4
7
8hashType=$NIX_HASH_ALGO
9if test -z "$hashType"; then
10 hashType=sha256
11fi
12
13if test -z "$cvsRoot"; then
14 echo "syntax: nix-prefetch-cvs CVSROOT MODULE [TAG [HASH]]" >&2
15 exit 1
16elif test -z "$module"; then
17 echo "syntax: nix-prefetch-cvs CVSROOT MODULE [TAG [HASH]]" >&2
18 exit 1
19fi
20
21
22mkTempDir() {
23 # nix>=2.20 rejects adding symlinked paths to the store, so use realpath
24 # to resolve to a physical path. https://github.com/NixOS/nix/issues/11941
25 tmpPath="$(realpath "$(mktemp -d --tmpdir nix-prefetch-csv-XXXXXXXX)")"
26 trap removeTempDir EXIT
27}
28
29removeTempDir() {
30 rm -rf "$tmpPath"
31}
32
33
34# If the hash was given, a file with that hash may already be in the
35# store.
36if test -n "$expHash"; then
37 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" cvs-export)
38 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
39 finalPath=
40 fi
41 hash=$expHash
42fi
43
44
45# If we don't know the hash or a path with that hash doesn't exist,
46# download the file and add it to the store.
47if test -z "$finalPath"; then
48
49 mkTempDir
50 tmpFile=$tmpPath/cvs-export
51 #mkdir $tmpPath
52
53 # Perform the checkout.
54 if test -z "$tag"; then
55 args=(-D "now")
56 elif test "$USE_DATE" = "1"; then
57 args=(-D "$tag")
58 else
59 args=(-r "$tag")
60 fi
61 (cd "$tmpPath" && cvs -f -z0 -d $cvsRoot export "${args[*]}" -d cvs-export $module >&2)
62
63 # Compute the hash.
64 hash=$(nix-hash --type $hashType ${hashFormat:-"--sri"} $tmpFile)
65 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
66
67 # Add the downloaded file to the Nix store.
68 finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
69
70 if test -n "$expHash" -a "$expHash" != "$hash"; then
71 echo "hash mismatch for CVS root \`$cvsRoot'"
72 exit 1
73 fi
74fi
75
76if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
77
78echo $hash
79
80if test -n "$PRINT_PATH"; then
81 echo $finalPath
82fi