2020-03-26 04:35:40 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
|
|
|
set -e -o pipefail
|
2009-06-24 13:48:01 +01:00
|
|
|
|
2012-06-07 14:52:38 +01:00
|
|
|
url=
|
|
|
|
rev=
|
|
|
|
expHash=
|
2009-06-24 13:48:01 +01:00
|
|
|
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
|
2014-03-23 16:19:39 +00:00
|
|
|
fetchSubmodules=
|
2011-08-20 15:29:57 +01:00
|
|
|
builder=
|
2015-04-20 13:25:14 +01:00
|
|
|
branchName=$NIX_PREFETCH_GIT_BRANCH_NAME
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2016-03-18 19:42:52 +00:00
|
|
|
# ENV params
|
|
|
|
out=${out:-}
|
|
|
|
http_proxy=${http_proxy:-}
|
|
|
|
|
2015-12-13 00:27:49 +00:00
|
|
|
# populated by clone_user_rev()
|
|
|
|
fullRev=
|
|
|
|
humanReadableRev=
|
|
|
|
commitDate=
|
2016-06-30 04:57:28 +01:00
|
|
|
commitDateStrict8601=
|
2015-12-13 00:27:49 +00:00
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
if test -n "$deepClone"; then
|
2014-11-01 12:40:24 +00:00
|
|
|
deepClone=true
|
2011-08-20 15:29:57 +01:00
|
|
|
else
|
2020-02-14 05:05:43 +00:00
|
|
|
deepClone=
|
2011-08-20 15:29:57 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$leaveDotGit" != 1; then
|
2014-11-01 12:40:24 +00:00
|
|
|
leaveDotGit=
|
2011-08-20 15:29:57 +01:00
|
|
|
else
|
2014-11-01 12:40:24 +00:00
|
|
|
leaveDotGit=true
|
2009-06-24 13:48:01 +01:00
|
|
|
fi
|
|
|
|
|
2015-11-12 23:10:56 +00:00
|
|
|
usage(){
|
|
|
|
echo >&2 "syntax: nix-prefetch-git [options] [URL [REVISION [EXPECTED-HASH]]]
|
|
|
|
|
|
|
|
Options:
|
2020-03-26 04:35:40 +00:00
|
|
|
--out path Path where the output would be stored.
|
|
|
|
--url url Any url understood by 'git clone'.
|
|
|
|
--rev ref Any sha1 or references (such as refs/heads/master)
|
|
|
|
--hash h Expected hash.
|
|
|
|
--branch-name Branch name to check out into
|
|
|
|
--deepClone Clone the entire repository.
|
|
|
|
--no-deepClone Make a shallow clone of just the required ref.
|
|
|
|
--leave-dotGit Keep the .git directories.
|
2015-11-12 23:10:56 +00:00
|
|
|
--fetch-submodules Fetch submodules.
|
2020-03-26 04:35:40 +00:00
|
|
|
--builder Clone as fetchgit does, but url, rev, and out option are mandatory.
|
|
|
|
--quiet Only print the final json summary.
|
2015-11-12 23:10:56 +00:00
|
|
|
"
|
|
|
|
exit 1
|
|
|
|
}
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2020-02-14 20:27:47 +00:00
|
|
|
# some git commands print to stdout, which would contaminate our JSON output
|
|
|
|
clean_git(){
|
|
|
|
git "$@" >&2
|
|
|
|
}
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
argi=0
|
|
|
|
argfun=""
|
|
|
|
for arg; do
|
2014-11-01 12:40:24 +00: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;;
|
2015-04-20 13:25:14 +01:00
|
|
|
--branch-name) argfun=set_branchName;;
|
2014-11-01 12:40:24 +00:00
|
|
|
--deepClone) deepClone=true;;
|
2015-12-13 00:27:03 +00:00
|
|
|
--quiet) QUIET=true;;
|
2020-02-14 05:05:43 +00:00
|
|
|
--no-deepClone) deepClone=;;
|
2014-11-01 12:40:24 +00:00
|
|
|
--leave-dotGit) leaveDotGit=true;;
|
|
|
|
--fetch-submodules) fetchSubmodules=true;;
|
|
|
|
--builder) builder=true;;
|
2019-05-03 22:48:28 +01:00
|
|
|
-h|--help) usage; exit;;
|
2014-11-01 12:40:24 +00:00
|
|
|
*)
|
2016-06-15 21:54:35 +01:00
|
|
|
: $((++argi))
|
2014-11-01 12:40:24 +00:00
|
|
|
case $argi in
|
|
|
|
1) url=$arg;;
|
|
|
|
2) rev=$arg;;
|
|
|
|
3) expHash=$arg;;
|
|
|
|
*) exit 1;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
case $argfun in
|
|
|
|
set_*)
|
2016-03-18 19:42:52 +00:00
|
|
|
var=${argfun#set_}
|
2014-11-01 12:40:24 +00:00
|
|
|
eval $var=$arg
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
argfun=""
|
|
|
|
fi
|
2011-08-20 15:29:57 +01:00
|
|
|
done
|
|
|
|
|
2011-09-07 11:02:14 +01:00
|
|
|
if test -z "$url"; then
|
2014-11-01 12:40:24 +00:00
|
|
|
usage
|
2009-06-24 13:48:01 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
init_remote(){
|
2014-11-01 14:09:44 +00:00
|
|
|
local url=$1
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git init
|
|
|
|
clean_git remote add origin "$url"
|
|
|
|
( [ -n "$http_proxy" ] && clean_git config http.proxy "$http_proxy" ) || true
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return the reference of an hash if it exists on the remote repository.
|
|
|
|
ref_from_hash(){
|
2014-11-01 14:09:44 +00:00
|
|
|
local hash=$1
|
2010-01-18 08:28:38 +00: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}"
|
|
|
|
}
|
|
|
|
|
2016-01-24 14:49:08 +00:00
|
|
|
# Returns a name based on the url and reference
|
|
|
|
#
|
|
|
|
# This function needs to be in sync with nix's fetchgit implementation
|
|
|
|
# of urlToName() to re-use the same nix store paths.
|
|
|
|
url_to_name(){
|
|
|
|
local url=$1
|
|
|
|
local ref=$2
|
2016-03-18 19:42:52 +00:00
|
|
|
local base
|
2017-06-18 12:42:39 +01:00
|
|
|
base=$(basename "$url" .git | cut -d: -f2)
|
2016-01-24 14:49:08 +00:00
|
|
|
|
2016-03-18 19:47:19 +00:00
|
|
|
if [[ $ref =~ ^[a-z0-9]+$ ]]; then
|
2016-01-24 14:49:08 +00:00
|
|
|
echo "$base-${ref:0:7}"
|
|
|
|
else
|
2016-03-18 19:42:52 +00:00
|
|
|
echo "$base"
|
2016-01-24 14:49:08 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
# Fetch everything and checkout the right sha1
|
|
|
|
checkout_hash(){
|
2014-11-01 14:09:44 +00:00
|
|
|
local hash="$1"
|
|
|
|
local ref="$2"
|
2010-01-18 08:28:38 +00:00
|
|
|
|
|
|
|
if test -z "$hash"; then
|
2016-03-18 19:42:52 +00:00
|
|
|
hash=$(hash_from_ref "$ref")
|
2014-11-01 14:09:44 +00:00
|
|
|
fi
|
2010-01-18 08:28:38 +00:00
|
|
|
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git fetch -t ${builder:+--progress} origin || return 1
|
|
|
|
clean_git checkout -b "$branchName" "$hash" || return 1
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fetch only a branch/tag and checkout it.
|
|
|
|
checkout_ref(){
|
2014-11-01 14:09:44 +00:00
|
|
|
local hash="$1"
|
|
|
|
local ref="$2"
|
2010-01-18 08:28:38 +00:00
|
|
|
|
2020-02-14 05:05:43 +00:00
|
|
|
if [[ -n "$deepClone" ]]; then
|
2014-11-01 12:40:24 +00:00
|
|
|
# The caller explicitly asked for a deep clone. Deep clones
|
|
|
|
# allow "git describe" and similar tools to work. See
|
2018-11-30 01:19:05 +00:00
|
|
|
# https://marc.info/?l=nix-dev&m=139641582514772
|
2014-11-01 12:40:24 +00:00
|
|
|
# for a discussion.
|
|
|
|
return 1
|
2010-01-25 13:23:08 +00:00
|
|
|
fi
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
if test -z "$ref"; then
|
2016-03-18 19:42:52 +00:00
|
|
|
ref=$(ref_from_hash "$hash")
|
2014-11-01 14:09:44 +00:00
|
|
|
fi
|
2010-01-18 08:28:38 +00:00
|
|
|
|
|
|
|
if test -n "$ref"; then
|
|
|
|
# --depth option is ignored on http repository.
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1
|
|
|
|
clean_git checkout -b "$branchName" FETCH_HEAD || return 1
|
2010-01-18 08:28:38 +00:00
|
|
|
else
|
2014-11-01 14:09:44 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update submodules
|
|
|
|
init_submodules(){
|
|
|
|
# Add urls into .git/config file
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git submodule init
|
2010-01-18 08:28:38 +00:00
|
|
|
|
|
|
|
# list submodule directories and their hashes
|
|
|
|
git submodule status |
|
2016-03-18 19:42:52 +00:00
|
|
|
while read -r l; do
|
|
|
|
local hash
|
|
|
|
local dir
|
|
|
|
local name
|
|
|
|
local url
|
|
|
|
|
2010-01-18 08:28:38 +00:00
|
|
|
# checkout each submodule
|
2018-01-24 15:02:55 +00:00
|
|
|
hash=$(echo "$l" | awk '{print $1}' | tr -d '-')
|
2018-06-25 16:08:29 +01:00
|
|
|
dir=$(echo "$l" | sed -n 's/^.[0-9a-f]\+ \(.*[^)]*\)\( (.*)\)\?$/\1/p')
|
2016-03-18 19:42:52 +00:00
|
|
|
name=$(
|
2014-11-05 20:59:44 +00:00
|
|
|
git config -f .gitmodules --get-regexp submodule\..*\.path |
|
2013-11-18 05:54:40 +00:00
|
|
|
sed -n "s,^\(.*\)\.path $dir\$,\\1,p")
|
2016-03-18 19:42:52 +00:00
|
|
|
url=$(git config --get "${name}.url")
|
2014-01-10 05:11:22 +00:00
|
|
|
|
2014-11-01 14:09:44 +00:00
|
|
|
clone "$dir" "$url" "$hash" ""
|
|
|
|
done
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clone(){
|
2016-03-18 19:42:52 +00:00
|
|
|
local top=$PWD
|
2010-01-18 08:28:38 +00:00
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
|
|
|
local hash="$3"
|
|
|
|
local ref="$4"
|
|
|
|
|
2016-03-18 19:42:52 +00:00
|
|
|
cd "$dir"
|
2010-01-18 08:28:38 +00:00
|
|
|
|
|
|
|
# Initialize the repository.
|
2014-11-01 14:09:44 +00:00
|
|
|
init_remote "$url"
|
2010-01-18 08:28:38 +00:00
|
|
|
|
|
|
|
# Download data from the repository.
|
|
|
|
checkout_ref "$hash" "$ref" ||
|
|
|
|
checkout_hash "$hash" "$ref" || (
|
2014-11-01 14:09:44 +00:00
|
|
|
echo 1>&2 "Unable to checkout $hash$ref from $url."
|
|
|
|
exit 1
|
2010-01-18 08:28:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Checkout linked sources.
|
2014-03-23 16:19:39 +00:00
|
|
|
if test -n "$fetchSubmodules"; then
|
2014-11-01 14:09:44 +00:00
|
|
|
init_submodules
|
2014-03-23 16:19:39 +00:00
|
|
|
fi
|
2010-01-18 08:28:38 +00:00
|
|
|
|
2016-03-18 19:42:52 +00:00
|
|
|
if [ -z "$builder" ] && [ -f .topdeps ]; then
|
|
|
|
if tg help &>/dev/null; then
|
2014-11-01 12:40:24 +00:00
|
|
|
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-14 22:00:20 +00:00
|
|
|
fi
|
|
|
|
|
2016-03-18 19:42:52 +00:00
|
|
|
cd "$top"
|
2010-01-18 08:28:38 +00:00
|
|
|
}
|
2009-06-24 13:48:01 +01:00
|
|
|
|
2014-11-01 15:42:03 +00: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.
|
2016-03-18 19:42:52 +00:00
|
|
|
git branch -r | while read -r branch; do
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git branch -rD "$branch"
|
2014-11-01 15:42:03 +00:00
|
|
|
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)
|
2016-03-18 19:42:52 +00:00
|
|
|
git tag --contains HEAD | while read -r tag; do
|
2014-11-01 15:42:03 +00:00
|
|
|
if [ "$tag" != "$maybe_tag" ]; then
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git tag -d "$tag"
|
2014-11-01 15:42:03 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-10-10 15:59:37 +01:00
|
|
|
# Do a full repack. Must run single-threaded, or else we lose determinism.
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git config pack.threads 1
|
|
|
|
clean_git repack -A -d -f
|
2014-11-03 21:51:56 +00:00
|
|
|
rm -f .git/config
|
2014-11-01 15:42:03 +00:00
|
|
|
|
|
|
|
# Garbage collect unreferenced objects.
|
2019-08-31 00:16:50 +01:00
|
|
|
# Note: --keep-largest-pack prevents non-deterministic ordering of packs
|
|
|
|
# listed in .git/objects/info/packs by only using a single pack
|
2020-02-14 20:27:47 +00:00
|
|
|
clean_git gc --prune=all --keep-largest-pack
|
2014-11-01 15:42:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-24 15:32:39 +00:00
|
|
|
clone_user_rev() {
|
2011-08-20 15:29:57 +01:00
|
|
|
local dir="$1"
|
|
|
|
local url="$2"
|
2012-06-07 15:01:07 +01:00
|
|
|
local rev="${3:-HEAD}"
|
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;;
|
2012-06-07 15:01:07 +01:00
|
|
|
*)
|
2016-03-18 19:42:52 +00:00
|
|
|
if test -z "$(echo "$rev" | tr -d 0123456789abcdef)"; then
|
2014-11-01 14:09:44 +00:00
|
|
|
clone "$dir" "$url" "$rev" "" 1>&2
|
2010-01-18 08:28:38 +00:00
|
|
|
else
|
2017-06-26 13:56:00 +01:00
|
|
|
# if revision is not hexadecimal it might be a tag
|
|
|
|
clone "$dir" "$url" "" "refs/tags/$rev" 1>&2
|
2010-01-18 08:28:38 +00:00
|
|
|
fi;;
|
|
|
|
esac
|
|
|
|
|
2016-07-04 13:34:24 +01:00
|
|
|
pushd "$dir" >/dev/null
|
2016-06-30 04:57:28 +01:00
|
|
|
fullRev=$( (git rev-parse "$rev" 2>/dev/null || git rev-parse "refs/heads/$branchName") | tail -n1)
|
|
|
|
humanReadableRev=$(git describe "$fullRev" 2> /dev/null || git describe --tags "$fullRev" 2> /dev/null || echo -- none --)
|
2016-10-08 18:05:45 +01:00
|
|
|
commitDate=$(git show -1 --no-patch --pretty=%ci "$fullRev")
|
|
|
|
commitDateStrict8601=$(git show -1 --no-patch --pretty=%cI "$fullRev")
|
2016-07-04 13:34:24 +01:00
|
|
|
popd >/dev/null
|
2014-05-16 08:21:22 +01:00
|
|
|
|
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
|
2014-11-01 12:40:24 +00:00
|
|
|
echo "removing \`.git'..." >&2
|
2016-05-15 00:24:04 +01:00
|
|
|
find "$dir" -name .git -print0 | xargs -0 rm -rf
|
2014-07-27 18:13:00 +01:00
|
|
|
else
|
2016-03-18 19:42:52 +00:00
|
|
|
find "$dir" -name .git | while read -r gitdir; do
|
2014-11-01 15:42:03 +00:00
|
|
|
make_deterministic_repo "$(readlink -f "$gitdir/..")"
|
|
|
|
done
|
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
|
|
|
|
2019-01-24 15:32:39 +00:00
|
|
|
exit_handlers=()
|
|
|
|
|
|
|
|
run_exit_handlers() {
|
|
|
|
exit_status=$?
|
|
|
|
for handler in "${exit_handlers[@]}"; do
|
|
|
|
eval "$handler $exit_status"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
trap run_exit_handlers EXIT
|
|
|
|
|
|
|
|
quiet_exit_handler() {
|
|
|
|
exec 2>&3 3>&-
|
|
|
|
if [ $1 -ne 0 ]; then
|
|
|
|
cat "$errfile" >&2
|
2015-12-13 00:27:49 +00:00
|
|
|
fi
|
2019-01-24 15:32:39 +00:00
|
|
|
rm -f "$errfile"
|
|
|
|
}
|
|
|
|
|
|
|
|
quiet_mode() {
|
|
|
|
errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")"
|
|
|
|
exit_handlers+=(quiet_exit_handler)
|
|
|
|
exec 3>&2 2>"$errfile"
|
2015-12-13 00:27:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:07:20 +01:00
|
|
|
json_escape() {
|
|
|
|
local s="$1"
|
|
|
|
s="${s//\\/\\\\}" # \
|
|
|
|
s="${s//\"/\\\"}" # "
|
|
|
|
s="${s//^H/\\\b}" # \b (backspace)
|
|
|
|
s="${s//^L/\\\f}" # \f (form feed)
|
|
|
|
s="${s//
|
|
|
|
/\\\n}" # \n (newline)
|
|
|
|
s="${s//^M/\\\r}" # \r (carriage return)
|
|
|
|
s="${s// /\\t}" # \t (tab)
|
|
|
|
echo "$s"
|
|
|
|
}
|
2015-12-13 00:27:49 +00:00
|
|
|
|
|
|
|
print_results() {
|
|
|
|
hash="$1"
|
|
|
|
if ! test -n "$QUIET"; then
|
|
|
|
echo "" >&2
|
|
|
|
echo "git revision is $fullRev" >&2
|
|
|
|
if test -n "$finalPath"; then
|
|
|
|
echo "path is $finalPath" >&2
|
|
|
|
fi
|
|
|
|
echo "git human-readable version is $humanReadableRev" >&2
|
|
|
|
echo "Commit date is $commitDate" >&2
|
|
|
|
if test -n "$hash"; then
|
|
|
|
echo "hash is $hash" >&2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if test -n "$hash"; then
|
2016-10-08 18:07:20 +01:00
|
|
|
cat <<EOF
|
|
|
|
{
|
|
|
|
"url": "$(json_escape "$url")",
|
|
|
|
"rev": "$(json_escape "$fullRev")",
|
|
|
|
"date": "$(json_escape "$commitDateStrict8601")",
|
2020-04-18 20:13:07 +01:00
|
|
|
"path": "$(json_escape "$finalPath")",
|
2016-10-08 18:07:20 +01:00
|
|
|
"$(json_escape "$hashType")": "$(json_escape "$hash")",
|
2020-02-14 05:05:43 +00:00
|
|
|
"fetchSubmodules": $([[ -n "$fetchSubmodules" ]] && echo true || echo false),
|
|
|
|
"deepClone": $([[ -n "$deepClone" ]] && echo true || echo false),
|
|
|
|
"leaveDotGit": $([[ -n "$leaveDotGit" ]] && echo true || echo false)
|
2016-10-08 18:07:20 +01:00
|
|
|
}
|
|
|
|
EOF
|
2015-12-13 00:27:49 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:32:39 +00:00
|
|
|
remove_tmpPath() {
|
|
|
|
rm -rf "$tmpPath"
|
|
|
|
}
|
|
|
|
|
|
|
|
if test -n "$QUIET"; then
|
|
|
|
quiet_mode
|
|
|
|
fi
|
|
|
|
|
2015-04-20 13:25:14 +01:00
|
|
|
if test -z "$branchName"; then
|
|
|
|
branchName=fetchgit
|
|
|
|
fi
|
|
|
|
|
2011-08-20 15:29:57 +01:00
|
|
|
if test -n "$builder"; then
|
2014-11-01 12:40:24 +00:00
|
|
|
test -n "$out" -a -n "$url" -a -n "$rev" || usage
|
2016-03-18 19:42:52 +00:00
|
|
|
mkdir -p "$out"
|
2014-11-01 12:40:24 +00:00
|
|
|
clone_user_rev "$out" "$url" "$rev"
|
2011-08-20 15:29:57 +01:00
|
|
|
else
|
2014-11-01 12:40:24 +00:00
|
|
|
if test -z "$hashType"; then
|
|
|
|
hashType=sha256
|
|
|
|
fi
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
# If the hash was given, a file with that hash may already be in the
|
|
|
|
# store.
|
|
|
|
if test -n "$expHash"; then
|
2016-01-24 14:49:08 +00:00
|
|
|
finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" "$(url_to_name "$url" "$rev")")
|
2014-11-01 12:40:24 +00:00
|
|
|
if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
|
|
|
|
finalPath=
|
|
|
|
fi
|
|
|
|
hash=$expHash
|
|
|
|
fi
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00: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 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")"
|
2019-01-24 15:32:39 +00:00
|
|
|
exit_handlers+=(remove_tmpPath)
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2016-01-24 14:49:08 +00:00
|
|
|
tmpFile="$tmpPath/$(url_to_name "$url" "$rev")"
|
2016-04-23 00:51:49 +01:00
|
|
|
mkdir -p "$tmpFile"
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
# Perform the checkout.
|
|
|
|
clone_user_rev "$tmpFile" "$url" "$rev"
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
# Compute the hash.
|
2016-03-18 19:42:52 +00:00
|
|
|
hash=$(nix-hash --type $hashType --base32 "$tmpFile")
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
# Add the downloaded file to the Nix store.
|
2016-01-24 14:49:08 +00:00
|
|
|
finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpFile")
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
if test -n "$expHash" -a "$expHash" != "$hash"; then
|
2017-01-04 02:54:13 +00:00
|
|
|
echo "hash mismatch for URL \`$url'. Got \`$hash'; expected \`$expHash'." >&2
|
2014-11-01 12:40:24 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2015-12-13 00:27:49 +00:00
|
|
|
print_results "$hash"
|
2011-08-20 15:29:57 +01:00
|
|
|
|
2014-11-01 12:40:24 +00:00
|
|
|
if test -n "$PRINT_PATH"; then
|
2016-03-18 19:42:52 +00:00
|
|
|
echo "$finalPath"
|
2014-11-01 12:40:24 +00:00
|
|
|
fi
|
2011-08-29 11:06:12 +01:00
|
|
|
fi
|