2009-06-24 13:48:01 +01:00
|
|
|
#! /bin/sh -e
|
|
|
|
|
|
|
|
url=$1
|
|
|
|
rev=$2
|
|
|
|
expHash=$3
|
|
|
|
hashType=$NIX_HASH_ALGO
|
2011-08-20 15:29:57 +01:00
|
|
|
deepClone=$NIX_PREFETCH_GIT_DEEP_CLONE
|
|
|
|
leaveDotGit=$NIX_PREFETCH_GIT_LEAVE_DOT_GIT
|
|
|
|
builder=
|
|
|
|
|
|
|
|
if test -n "$deepClone"; then
|
|
|
|
deepClone=true
|
|
|
|
else
|
|
|
|
deepClone=false
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$leaveDotGit" != 1; then
|
|
|
|
leaveDotGit=
|
|
|
|
else
|
|
|
|
leaveDotGit=true
|
2009-06-24 13:48:01 +01:00
|
|
|
fi
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
|
|
|
|
argi=0
|
|
|
|
argfun=""
|
|
|
|
for arg; do
|
|
|
|
if test -z "$argfun"; then
|
|
|
|
case $arg in
|
|
|
|
--out) argfun=set_out;;
|
|
|
|
--url) argfun=set_url;;
|
|
|
|
--rev) argfun=set_rev;;
|
|
|
|
--hash) argfun=set_hashType;;
|
|
|
|
--deepClone) deepClone=true;;
|
|
|
|
--no-deepClone) deepClone=false;;
|
|
|
|
--leave-dotGit) leaveDotGit=true;;
|
|
|
|
--builder) builder=true;;
|
|
|
|
*)
|
|
|
|
argi=$(($argi + 1))
|
|
|
|
case $argi in
|
|
|
|
1) url=$arg;;
|
|
|
|
2) rev=$arg;;
|
|
|
|
3) expHash=$arg;;
|
|
|
|
*) exit 1;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
case $argfun in
|
|
|
|
set_*)
|
|
|
|
var=$(echo $argfun | sed 's,^set_,,')
|
|
|
|
eval $var=$arg
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
argfun=""
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2009-06-24 13:48:01 +01:00
|
|
|
if test -z "$url"; then
|
|
|
|
echo "syntax: nix-prefetch-git URL [REVISION [EXPECTED-HASH]]" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
init_remote(){
|
|
|
|
local url=$1;
|
|
|
|
git init;
|
|
|
|
git remote add origin $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return the reference of an hash if it exists on the remote repository.
|
|
|
|
ref_from_hash(){
|
|
|
|
local hash=$1;
|
|
|
|
git ls-remote origin | sed -n "\,$hash\t, { s,\(.*\)\t\(.*\),\2,; p; q}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return the hash of a reference if it exists on the remote repository.
|
|
|
|
hash_from_ref(){
|
|
|
|
local ref=$1
|
|
|
|
git ls-remote origin | sed -n "\,\t$ref, { s,\(.*\)\t\(.*\),\1,; p; q}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fetch everything and checkout the right sha1
|
|
|
|
checkout_hash(){
|
|
|
|
local hash="$1";
|
|
|
|
local ref="$2";
|
|
|
|
|
|
|
|
if test -z "$hash"; then
|
|
|
|
hash=$(hash_from_ref $ref);
|
|
|
|
fi;
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
git fetch ${builder:+--progress} origin || return 1
|
|
|
|
git checkout -b fetchgit $hash || return 1
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fetch only a branch/tag and checkout it.
|
|
|
|
checkout_ref(){
|
|
|
|
local hash="$1";
|
|
|
|
local ref="$2";
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
if "$deepClone"; then
|
2010-01-25 13:23:08 +00:00
|
|
|
# The caller explicitly asked for a deep clone. Deep clones
|
|
|
|
# allow "git describe" and similar tools to work. See
|
|
|
|
# http://thread.gmane.org/gmane.linux.distributions.nixos/3569
|
|
|
|
# for a discussion.
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
if test -z "$ref"; then
|
|
|
|
ref=$(ref_from_hash $hash);
|
|
|
|
fi;
|
|
|
|
|
|
|
|
if test -n "$ref"; then
|
|
|
|
# --depth option is ignored on http repository.
|
2011-08-20 15:29:57 +01:00
|
|
|
git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1
|
|
|
|
git checkout -b fetchgit FETCH_HEAD || return 1
|
2010-01-18 08:28:38 +00:00
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
fi;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update submodules
|
|
|
|
init_submodules(){
|
|
|
|
# Add urls into .git/config file
|
|
|
|
git submodule init
|
|
|
|
|
|
|
|
# list submodule directories and their hashes
|
|
|
|
git submodule status |
|
|
|
|
while read l; do
|
|
|
|
# checkout each submodule
|
|
|
|
local hash=$(echo $l | sed 's,^-\([0-9a-f]*\) \(.*\)$,\1,');
|
|
|
|
local dir=$(echo $l | sed 's,^-\([0-9a-f]*\) \(.*\)$,\2,');
|
|
|
|
local url=$(sed -n "\,$dir, { :loop; n; s,^.*url = ,,; T loop; p; q }" .git/config);
|
|
|
|
|
|
|
|
clone "$dir" "$url" "$hash" "";
|
|
|
|
done;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone(){
|
|
|
|
local top=$(pwd)
|
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
|
|
|
local hash="$3"
|
|
|
|
local ref="$4"
|
|
|
|
|
|
|
|
cd $dir;
|
|
|
|
|
|
|
|
# Initialize the repository.
|
|
|
|
init_remote "$url";
|
|
|
|
|
|
|
|
# Download data from the repository.
|
|
|
|
checkout_ref "$hash" "$ref" ||
|
|
|
|
checkout_hash "$hash" "$ref" || (
|
|
|
|
echo 1>&2 "Unable to checkout $hash$ref from $url.";
|
|
|
|
exit 1;
|
|
|
|
)
|
|
|
|
|
|
|
|
# Checkout linked sources.
|
|
|
|
init_submodules;
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
if [ -z "$builder" -a -f .topdeps ]; then
|
2011-03-14 22:00:20 +00:00
|
|
|
if tg help 2>&1 > /dev/null
|
|
|
|
then
|
|
|
|
echo "populating TopGit branches..."
|
|
|
|
tg remote --populate origin
|
|
|
|
else
|
|
|
|
echo "WARNING: would populate TopGit branches but TopGit is not available" >&2
|
|
|
|
echo "WARNING: install TopGit to fix the problem" >&2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
cd $top;
|
|
|
|
}
|
2009-06-24 13:48:01 +01:00
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
clone_user_rev() {
|
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
|
|
|
local rev="$3"
|
2009-06-24 13:48:01 +01:00
|
|
|
|
|
|
|
# Perform the checkout.
|
2010-01-18 08:28:38 +00:00
|
|
|
case "$rev" in
|
|
|
|
HEAD|refs/*)
|
2011-08-20 15:29:57 +01:00
|
|
|
clone "$dir" "$url" "" "$rev" 1>&2;;
|
2010-01-18 08:28:38 +00:00
|
|
|
[0-9a-f]*)
|
|
|
|
if test -z "$(echo $rev | tr -d 0123456789abcdef)"; then
|
2011-08-20 15:29:57 +01:00
|
|
|
clone "$dir" "$url" "$rev" "" 1>&2;
|
2010-01-18 08:28:38 +00:00
|
|
|
else
|
|
|
|
echo 1>&2 "Bad commit hash or bad reference.";
|
|
|
|
exit 1;
|
|
|
|
fi;;
|
|
|
|
"")
|
2011-08-20 15:29:57 +01:00
|
|
|
clone "$dir" "$url" "" "HEAD" 1>&2;;
|
2010-01-18 08:28:38 +00:00
|
|
|
esac
|
|
|
|
|
2009-09-08 10:35:21 +01:00
|
|
|
# Allow doing additional processing before .git removal
|
|
|
|
eval "$NIX_PREFETCH_GIT_CHECKOUT_HOOK"
|
2011-08-20 15:29:57 +01:00
|
|
|
if test -z "$leaveDotGit"; then
|
2009-11-18 19:10:24 +00:00
|
|
|
echo "removing \`.git'..." >&2
|
2011-08-20 15:29:57 +01:00
|
|
|
find $out -name .git\* | xargs rm -rf
|
2009-06-24 13:48:01 +01:00
|
|
|
fi
|
2011-08-20 15:29:57 +01:00
|
|
|
}
|
2009-06-24 13:48:01 +01:00
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
if test -n "$builder"; then
|
|
|
|
mkdir $out
|
|
|
|
clone_user_rev "$out" "$url" "$rev"
|
|
|
|
else
|
|
|
|
if test -z "$hashType"; then
|
|
|
|
hashType=sha256
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If the hash was given, a file with that hash may already be in the
|
|
|
|
# store.
|
2011-08-29 11:06:12 +01:00
|
|
|
if test -n "$expHash"; then
|
2011-08-20 15:29:57 +01:00
|
|
|
finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" git-export)
|
|
|
|
if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
|
|
|
|
finalPath=
|
|
|
|
fi
|
|
|
|
hash=$expHash
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If we don't know the hash or a path with that hash doesn't exist,
|
|
|
|
# download the file and add it to the store.
|
|
|
|
if test -z "$finalPath"; then
|
|
|
|
|
|
|
|
tmpPath=/tmp/git-checkout-tmp-$$
|
|
|
|
tmpFile=$tmpPath/git-export
|
|
|
|
mkdir $tmpPath $tmpFile
|
|
|
|
|
|
|
|
trap "rm -rf $tmpPath" EXIT
|
|
|
|
|
|
|
|
# Perform the checkout.
|
|
|
|
clone_user_rev "$tmpFile" "$url" "$rev"
|
|
|
|
|
|
|
|
# Compute the hash.
|
|
|
|
hash=$(nix-hash --type $hashType $hashFormat $tmpFile)
|
|
|
|
if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
|
|
|
|
|
|
|
|
# Add the downloaded file to the Nix store.
|
|
|
|
finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
|
|
|
|
|
|
|
|
if test -n "$expHash" -a "$expHash" != "$hash"; then
|
|
|
|
echo "hash mismatch for URL \`$url'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
|
|
|
|
|
|
|
|
echo $hash
|
|
|
|
|
|
|
|
if test -n "$PRINT_PATH"; then
|
|
|
|
echo $finalPath
|
|
|
|
fi
|
2011-08-29 11:06:12 +01:00
|
|
|
fi
|