2009-06-24 16:48:01 +04:00
|
|
|
#! /bin/sh -e
|
|
|
|
|
2012-06-07 17:52:38 +04:00
|
|
|
url=
|
|
|
|
rev=
|
|
|
|
expHash=
|
2009-06-24 16:48:01 +04:00
|
|
|
hashType=$NIX_HASH_ALGO
|
2011-08-20 18:29:57 +04:00
|
|
|
deepClone=$NIX_PREFETCH_GIT_DEEP_CLONE
|
|
|
|
leaveDotGit=$NIX_PREFETCH_GIT_LEAVE_DOT_GIT
|
2014-03-23 20:19:39 +04:00
|
|
|
fetchSubmodules=
|
2011-08-20 18:29:57 +04:00
|
|
|
builder=
|
|
|
|
|
|
|
|
if test -n "$deepClone"; then
|
2014-11-01 15:40:24 +03:00
|
|
|
deepClone=true
|
2011-08-20 18:29:57 +04:00
|
|
|
else
|
2014-11-01 15:40:24 +03:00
|
|
|
deepClone=false
|
2011-08-20 18:29:57 +04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$leaveDotGit" != 1; then
|
2014-11-01 15:40:24 +03:00
|
|
|
leaveDotGit=
|
2011-08-20 18:29:57 +04:00
|
|
|
else
|
2014-11-01 15:40:24 +03:00
|
|
|
leaveDotGit=true
|
2009-06-24 16:48:01 +04:00
|
|
|
fi
|
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
|
|
|
|
argi=0
|
|
|
|
argfun=""
|
|
|
|
for arg; do
|
2014-11-01 15:40:24 +03:00
|
|
|
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;;
|
|
|
|
--fetch-submodules) fetchSubmodules=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
|
2011-08-20 18:29:57 +04:00
|
|
|
done
|
|
|
|
|
2011-09-07 14:02:14 +04:00
|
|
|
usage(){
|
|
|
|
echo >&2 "syntax: nix-prefetch-git [options] [URL [REVISION [EXPECTED-HASH]]]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
--out path Path where the output would be stored.
|
|
|
|
--url url Any url understand by 'git clone'.
|
|
|
|
--rev ref Any sha1 or references (such as refs/heads/master)
|
|
|
|
--hash h Expected hash.
|
|
|
|
--deepClone Clone submodules recursively.
|
|
|
|
--no-deepClone Do not clone submodules.
|
|
|
|
--leave-dotGit Keep the .git directories.
|
2014-03-23 20:19:39 +04:00
|
|
|
--fetch-submodules Fetch submodules.
|
2011-09-07 14:02:14 +04:00
|
|
|
--builder Clone as fetchgit does, but url, rev, and out option are mandatory.
|
|
|
|
"
|
2009-06-24 16:48:01 +04:00
|
|
|
exit 1
|
2011-09-07 14:02:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if test -z "$url"; then
|
2014-11-01 15:40:24 +03:00
|
|
|
usage
|
2009-06-24 16:48:01 +04:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2010-01-18 11:28:38 +03:00
|
|
|
init_remote(){
|
2014-11-01 17:09:44 +03:00
|
|
|
local url=$1
|
|
|
|
git init
|
|
|
|
git remote add origin $url
|
2014-11-10 08:53:24 +03:00
|
|
|
( [ -n "$http_proxy" ] && git config http.proxy $http_proxy ) || true
|
2010-01-18 11:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return the reference of an hash if it exists on the remote repository.
|
|
|
|
ref_from_hash(){
|
2014-11-01 17:09:44 +03:00
|
|
|
local hash=$1
|
2010-01-18 11:28:38 +03:00
|
|
|
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(){
|
2014-11-01 17:09:44 +03:00
|
|
|
local hash="$1"
|
|
|
|
local ref="$2"
|
2010-01-18 11:28:38 +03:00
|
|
|
|
|
|
|
if test -z "$hash"; then
|
2014-11-01 17:09:44 +03:00
|
|
|
hash=$(hash_from_ref $ref)
|
|
|
|
fi
|
2010-01-18 11:28:38 +03:00
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
git fetch ${builder:+--progress} origin || return 1
|
|
|
|
git checkout -b fetchgit $hash || return 1
|
2010-01-18 11:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fetch only a branch/tag and checkout it.
|
|
|
|
checkout_ref(){
|
2014-11-01 17:09:44 +03:00
|
|
|
local hash="$1"
|
|
|
|
local ref="$2"
|
2010-01-18 11:28:38 +03:00
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
if "$deepClone"; then
|
2014-11-01 15:40:24 +03: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
|
2010-01-25 16:23:08 +03:00
|
|
|
fi
|
|
|
|
|
2010-01-18 11:28:38 +03:00
|
|
|
if test -z "$ref"; then
|
2014-11-01 17:09:44 +03:00
|
|
|
ref=$(ref_from_hash $hash)
|
|
|
|
fi
|
2010-01-18 11:28:38 +03:00
|
|
|
|
|
|
|
if test -n "$ref"; then
|
|
|
|
# --depth option is ignored on http repository.
|
2011-08-20 18:29:57 +04:00
|
|
|
git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1
|
|
|
|
git checkout -b fetchgit FETCH_HEAD || return 1
|
2010-01-18 11:28:38 +03:00
|
|
|
else
|
2014-11-01 17:09:44 +03:00
|
|
|
return 1
|
|
|
|
fi
|
2010-01-18 11:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
2014-11-01 17:09:44 +03:00
|
|
|
local hash=$(echo $l | awk '{print substr($1,2)}')
|
|
|
|
local dir=$(echo $l | awk '{print $2}')
|
2013-11-18 09:54:40 +04:00
|
|
|
local name=$(
|
|
|
|
git config -f .gitmodules --get-regexp submodule\.[^.]*\.path |
|
|
|
|
sed -n "s,^\(.*\)\.path $dir\$,\\1,p")
|
2014-11-01 17:09:44 +03:00
|
|
|
local url=$(git config -f .gitmodules --get ${name}.url)
|
2014-01-10 09:11:22 +04:00
|
|
|
|
|
|
|
# Get Absolute URL if we have a relative URL
|
|
|
|
if ! echo "$url" | grep '^[a-zA-Z]\+://' >/dev/null 2>&1; then
|
2014-11-01 15:40:24 +03:00
|
|
|
url="$(git config --get remote.origin.url)/$url"
|
2014-01-10 09:11:22 +04:00
|
|
|
fi
|
|
|
|
|
2014-11-01 17:09:44 +03:00
|
|
|
clone "$dir" "$url" "$hash" ""
|
|
|
|
done
|
2010-01-18 11:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clone(){
|
|
|
|
local top=$(pwd)
|
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
|
|
|
local hash="$3"
|
|
|
|
local ref="$4"
|
|
|
|
|
2014-11-01 17:09:44 +03:00
|
|
|
cd $dir
|
2010-01-18 11:28:38 +03:00
|
|
|
|
|
|
|
# Initialize the repository.
|
2014-11-01 17:09:44 +03:00
|
|
|
init_remote "$url"
|
2010-01-18 11:28:38 +03:00
|
|
|
|
|
|
|
# Download data from the repository.
|
|
|
|
checkout_ref "$hash" "$ref" ||
|
|
|
|
checkout_hash "$hash" "$ref" || (
|
2014-11-01 17:09:44 +03:00
|
|
|
echo 1>&2 "Unable to checkout $hash$ref from $url."
|
|
|
|
exit 1
|
2010-01-18 11:28:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# Checkout linked sources.
|
2014-03-23 20:19:39 +04:00
|
|
|
if test -n "$fetchSubmodules"; then
|
2014-11-01 17:09:44 +03:00
|
|
|
init_submodules
|
2014-03-23 20:19:39 +04:00
|
|
|
fi
|
2010-01-18 11:28:38 +03:00
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
if [ -z "$builder" -a -f .topdeps ]; then
|
2014-11-01 15:40:24 +03: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
|
2011-03-15 01:00:20 +03:00
|
|
|
fi
|
|
|
|
|
2014-11-01 17:09:44 +03:00
|
|
|
cd $top
|
2010-01-18 11:28:38 +03:00
|
|
|
}
|
2009-06-24 16:48:01 +04:00
|
|
|
|
2014-11-01 18:42:03 +03:00
|
|
|
# Remove all remote branches, remove tags not reachable from HEAD, do a full
|
|
|
|
# repack and then garbage collect unreferenced objects.
|
|
|
|
make_deterministic_repo(){
|
|
|
|
local repo="$1"
|
|
|
|
|
|
|
|
# run in sub-shell to not touch current working directory
|
|
|
|
(
|
|
|
|
cd "$repo"
|
|
|
|
# Remove files that contain timestamps or otherwise have non-deterministic
|
|
|
|
# properties.
|
|
|
|
rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD \
|
|
|
|
.git/refs/remotes/origin/HEAD .git/config
|
|
|
|
|
|
|
|
# Remove all remote branches.
|
|
|
|
git branch -r | while read branch; do
|
|
|
|
git branch -rD "$branch" >&2
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove tags not reachable from HEAD. If we're exactly on a tag, don't
|
|
|
|
# delete it.
|
|
|
|
maybe_tag=$(git tag --points-at HEAD)
|
|
|
|
git tag --contains HEAD | while read tag; do
|
|
|
|
if [ "$tag" != "$maybe_tag" ]; then
|
|
|
|
git tag -d "$tag" >&2
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-11-04 00:51:56 +03:00
|
|
|
# Do a full repack. Must run single-threaded, or else we loose determinism.
|
|
|
|
git config pack.threads 1
|
2014-11-01 18:42:03 +03:00
|
|
|
git repack -A -d -f
|
2014-11-04 00:51:56 +03:00
|
|
|
rm -f .git/config
|
2014-11-01 18:42:03 +03:00
|
|
|
|
|
|
|
# Garbage collect unreferenced objects.
|
|
|
|
git gc --prune=all
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
clone_user_rev() {
|
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
2012-06-07 18:01:07 +04:00
|
|
|
local rev="${3:-HEAD}"
|
2009-06-24 16:48:01 +04:00
|
|
|
|
|
|
|
# Perform the checkout.
|
2010-01-18 11:28:38 +03:00
|
|
|
case "$rev" in
|
|
|
|
HEAD|refs/*)
|
2011-08-20 18:29:57 +04:00
|
|
|
clone "$dir" "$url" "" "$rev" 1>&2;;
|
2012-06-07 18:01:07 +04:00
|
|
|
*)
|
2010-01-18 11:28:38 +03:00
|
|
|
if test -z "$(echo $rev | tr -d 0123456789abcdef)"; then
|
2014-11-01 17:09:44 +03:00
|
|
|
clone "$dir" "$url" "$rev" "" 1>&2
|
2010-01-18 11:28:38 +03:00
|
|
|
else
|
2014-11-01 17:09:44 +03:00
|
|
|
echo 1>&2 "Bad commit hash or bad reference."
|
|
|
|
exit 1
|
2010-01-18 11:28:38 +03:00
|
|
|
fi;;
|
|
|
|
esac
|
|
|
|
|
2014-06-25 02:12:10 +04:00
|
|
|
local full_revision=$(cd $dir && (git rev-parse $rev 2> /dev/null || git rev-parse refs/heads/fetchgit) | tail -n1)
|
|
|
|
echo "git revision is $full_revision"
|
2014-08-18 01:00:03 +04:00
|
|
|
echo "git human-readable version is $(cd $dir && (git describe $full_revision 2> /dev/null || git describe --tags $full_revision 2> /dev/null || echo -- none --))" >&2
|
2014-05-16 11:21:22 +04:00
|
|
|
|
2009-09-08 13:35:21 +04:00
|
|
|
# Allow doing additional processing before .git removal
|
|
|
|
eval "$NIX_PREFETCH_GIT_CHECKOUT_HOOK"
|
2011-08-20 18:29:57 +04:00
|
|
|
if test -z "$leaveDotGit"; then
|
2014-11-01 15:40:24 +03:00
|
|
|
echo "removing \`.git'..." >&2
|
2011-09-07 14:02:17 +04:00
|
|
|
find $dir -name .git\* | xargs rm -rf
|
2014-07-27 21:13:00 +04:00
|
|
|
else
|
2014-11-01 18:42:03 +03:00
|
|
|
find $dir -name .git | while read gitdir; do
|
|
|
|
make_deterministic_repo "$(readlink -f "$gitdir/..")"
|
|
|
|
done
|
2009-06-24 16:48:01 +04:00
|
|
|
fi
|
2011-08-20 18:29:57 +04:00
|
|
|
}
|
2009-06-24 16:48:01 +04:00
|
|
|
|
2011-08-20 18:29:57 +04:00
|
|
|
if test -n "$builder"; then
|
2014-11-01 15:40:24 +03:00
|
|
|
test -n "$out" -a -n "$url" -a -n "$rev" || usage
|
|
|
|
mkdir $out
|
|
|
|
clone_user_rev "$out" "$url" "$rev"
|
2011-08-20 18:29:57 +04:00
|
|
|
else
|
2014-11-01 15:40:24 +03:00
|
|
|
if test -z "$hashType"; then
|
|
|
|
hashType=sha256
|
|
|
|
fi
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
# If the hash was given, a file with that hash may already be in the
|
|
|
|
# store.
|
|
|
|
if test -n "$expHash"; then
|
|
|
|
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
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
# 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
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")"
|
|
|
|
trap "rm -rf \"$tmpPath\"" EXIT
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
tmpFile="$tmpPath/git-export"
|
|
|
|
mkdir "$tmpFile"
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
# Perform the checkout.
|
|
|
|
clone_user_rev "$tmpFile" "$url" "$rev"
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
# Compute the hash.
|
|
|
|
hash=$(nix-hash --type $hashType $hashFormat $tmpFile)
|
|
|
|
if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
# Add the downloaded file to the Nix store.
|
|
|
|
finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
if test -n "$expHash" -a "$expHash" != "$hash"; then
|
|
|
|
echo "hash mismatch for URL \`$url'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
echo $hash
|
2011-08-20 18:29:57 +04:00
|
|
|
|
2014-11-01 15:40:24 +03:00
|
|
|
if test -n "$PRINT_PATH"; then
|
|
|
|
echo $finalPath
|
|
|
|
fi
|
2011-08-29 14:06:12 +04:00
|
|
|
fi
|