Merge pull request #14031 from zimbatm/nix-prefetch-git-shellcheck

nix-prefetch-git: shellcheck fixes
This commit is contained in:
zimbatm 2016-06-12 13:48:28 +01:00 committed by GitHub
commit a06bf119c9

View file

@ -12,6 +12,10 @@ fetchSubmodules=
builder= builder=
branchName=$NIX_PREFETCH_GIT_BRANCH_NAME branchName=$NIX_PREFETCH_GIT_BRANCH_NAME
# ENV params
out=${out:-}
http_proxy=${http_proxy:-}
# populated by clone_user_rev() # populated by clone_user_rev()
fullRev= fullRev=
humanReadableRev= humanReadableRev=
@ -64,7 +68,7 @@ for arg; do
--builder) builder=true;; --builder) builder=true;;
--help) usage; exit;; --help) usage; exit;;
*) *)
argi=$(($argi + 1)) argi=$((argi++))
case $argi in case $argi in
1) url=$arg;; 1) url=$arg;;
2) rev=$arg;; 2) rev=$arg;;
@ -76,7 +80,7 @@ for arg; do
else else
case $argfun in case $argfun in
set_*) set_*)
var=$(echo $argfun | sed 's,^set_,,') var=${argfun#set_}
eval $var=$arg eval $var=$arg
;; ;;
esac esac
@ -92,8 +96,8 @@ fi
init_remote(){ init_remote(){
local url=$1 local url=$1
git init git init
git remote add origin $url git remote add origin "$url"
( [ -n "$http_proxy" ] && git config http.proxy $http_proxy ) || true ( [ -n "$http_proxy" ] && git config http.proxy "$http_proxy" ) || true
} }
# Return the reference of an hash if it exists on the remote repository. # Return the reference of an hash if it exists on the remote repository.
@ -116,12 +120,13 @@ url_to_name(){
local url=$1 local url=$1
local ref=$2 local ref=$2
# basename removes the / and .git suffixes # basename removes the / and .git suffixes
local base=$(basename "$url" .git) local base
base=$(basename "$url" .git)
if [[ $ref =~ ^[a-z0-9]+$ ]]; then if [[ $ref =~ ^[a-z0-9]+$ ]]; then
echo "$base-${ref:0:7}" echo "$base-${ref:0:7}"
else else
echo $base echo "$base"
fi fi
} }
@ -131,11 +136,11 @@ checkout_hash(){
local ref="$2" local ref="$2"
if test -z "$hash"; then if test -z "$hash"; then
hash=$(hash_from_ref $ref) hash=$(hash_from_ref "$ref")
fi fi
git fetch -t ${builder:+--progress} origin || return 1 git fetch -t ${builder:+--progress} origin || return 1
git checkout -b $branchName $hash || return 1 git checkout -b "$branchName" "$hash" || return 1
} }
# Fetch only a branch/tag and checkout it. # Fetch only a branch/tag and checkout it.
@ -152,13 +157,13 @@ checkout_ref(){
fi fi
if test -z "$ref"; then if test -z "$ref"; then
ref=$(ref_from_hash $hash) ref=$(ref_from_hash "$hash")
fi fi
if test -n "$ref"; then if test -n "$ref"; then
# --depth option is ignored on http repository. # --depth option is ignored on http repository.
git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1 git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1
git checkout -b $branchName FETCH_HEAD || return 1 git checkout -b "$branchName" FETCH_HEAD || return 1
else else
return 1 return 1
fi fi
@ -171,27 +176,32 @@ init_submodules(){
# list submodule directories and their hashes # list submodule directories and their hashes
git submodule status | git submodule status |
while read l; do while read -r l; do
local hash
local dir
local name
local url
# checkout each submodule # checkout each submodule
local hash=$(echo $l | awk '{print substr($1,2)}') hash=$(echo "$l" | awk '{print substr($1,2)}')
local dir=$(echo $l | awk '{print $2}') dir=$(echo "$l" | awk '{print $2}')
local name=$( name=$(
git config -f .gitmodules --get-regexp submodule\..*\.path | git config -f .gitmodules --get-regexp submodule\..*\.path |
sed -n "s,^\(.*\)\.path $dir\$,\\1,p") sed -n "s,^\(.*\)\.path $dir\$,\\1,p")
local url=$(git config --get ${name}.url) url=$(git config --get "${name}.url")
clone "$dir" "$url" "$hash" "" clone "$dir" "$url" "$hash" ""
done done
} }
clone(){ clone(){
local top=$(pwd) local top=$PWD
local dir="$1" local dir="$1"
local url="$2" local url="$2"
local hash="$3" local hash="$3"
local ref="$4" local ref="$4"
cd $dir cd "$dir"
# Initialize the repository. # Initialize the repository.
init_remote "$url" init_remote "$url"
@ -208,9 +218,8 @@ clone(){
init_submodules init_submodules
fi fi
if [ -z "$builder" -a -f .topdeps ]; then if [ -z "$builder" ] && [ -f .topdeps ]; then
if tg help 2>&1 > /dev/null if tg help &>/dev/null; then
then
echo "populating TopGit branches..." echo "populating TopGit branches..."
tg remote --populate origin tg remote --populate origin
else else
@ -219,7 +228,7 @@ clone(){
fi fi
fi fi
cd $top cd "$top"
} }
# Remove all remote branches, remove tags not reachable from HEAD, do a full # Remove all remote branches, remove tags not reachable from HEAD, do a full
@ -236,14 +245,14 @@ make_deterministic_repo(){
.git/refs/remotes/origin/HEAD .git/config .git/refs/remotes/origin/HEAD .git/config
# Remove all remote branches. # Remove all remote branches.
git branch -r | while read branch; do git branch -r | while read -r branch; do
git branch -rD "$branch" >&2 git branch -rD "$branch" >&2
done done
# Remove tags not reachable from HEAD. If we're exactly on a tag, don't # Remove tags not reachable from HEAD. If we're exactly on a tag, don't
# delete it. # delete it.
maybe_tag=$(git tag --points-at HEAD) maybe_tag=$(git tag --points-at HEAD)
git tag --contains HEAD | while read tag; do git tag --contains HEAD | while read -r tag; do
if [ "$tag" != "$maybe_tag" ]; then if [ "$tag" != "$maybe_tag" ]; then
git tag -d "$tag" >&2 git tag -d "$tag" >&2
fi fi
@ -270,7 +279,7 @@ _clone_user_rev() {
HEAD|refs/*) HEAD|refs/*)
clone "$dir" "$url" "" "$rev" 1>&2;; clone "$dir" "$url" "" "$rev" 1>&2;;
*) *)
if test -z "$(echo $rev | tr -d 0123456789abcdef)"; then if test -z "$(echo "$rev" | tr -d 0123456789abcdef)"; then
clone "$dir" "$url" "$rev" "" 1>&2 clone "$dir" "$url" "$rev" "" 1>&2
else else
echo 1>&2 "Bad commit hash or bad reference." echo 1>&2 "Bad commit hash or bad reference."
@ -278,9 +287,9 @@ _clone_user_rev() {
fi;; fi;;
esac esac
fullRev="$(cd $dir && (git rev-parse $rev 2> /dev/null || git rev-parse refs/heads/$branchName) | tail -n1)" fullRev="$(cd "$dir" && (git rev-parse "$rev" 2> /dev/null || git rev-parse "refs/heads/$branchName") | tail -n1)"
humanReadableRev="$(cd $dir && (git describe $fullRev 2> /dev/null || git describe --tags $fullRev 2> /dev/null || echo -- none --))" humanReadableRev="$(cd "$dir" && (git describe "$fullRev" 2> /dev/null || git describe --tags "$fullRev" 2> /dev/null || echo -- none --))"
commitDate="$(cd $dir && git show --no-patch --pretty=%ci $fullRev)" commitDate="$(cd "$dir" && git show --no-patch --pretty=%ci "$fullRev")"
# Allow doing additional processing before .git removal # Allow doing additional processing before .git removal
eval "$NIX_PREFETCH_GIT_CHECKOUT_HOOK" eval "$NIX_PREFETCH_GIT_CHECKOUT_HOOK"
@ -288,7 +297,7 @@ _clone_user_rev() {
echo "removing \`.git'..." >&2 echo "removing \`.git'..." >&2
find "$dir" -name .git -print0 | xargs -0 rm -rf find "$dir" -name .git -print0 | xargs -0 rm -rf
else else
find "$dir" -name .git | while read gitdir; do find "$dir" -name .git | while read -r gitdir; do
make_deterministic_repo "$(readlink -f "$gitdir/..")" make_deterministic_repo "$(readlink -f "$gitdir/..")"
done done
fi fi
@ -299,6 +308,7 @@ clone_user_rev() {
_clone_user_rev "$@" _clone_user_rev "$@"
else else
errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")" errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf \"$errfile\"" EXIT trap "rm -rf \"$errfile\"" EXIT
_clone_user_rev "$@" 2> "$errfile" || ( _clone_user_rev "$@" 2> "$errfile" || (
status="$?" status="$?"
@ -343,7 +353,7 @@ fi
if test -n "$builder"; then if test -n "$builder"; then
test -n "$out" -a -n "$url" -a -n "$rev" || usage test -n "$out" -a -n "$url" -a -n "$rev" || usage
mkdir -p $out mkdir -p "$out"
clone_user_rev "$out" "$url" "$rev" clone_user_rev "$out" "$url" "$rev"
else else
if test -z "$hashType"; then if test -z "$hashType"; then
@ -365,6 +375,7 @@ else
if test -z "$finalPath"; then if test -z "$finalPath"; then
tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")" tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf \"$tmpPath\"" EXIT trap "rm -rf \"$tmpPath\"" EXIT
tmpFile="$tmpPath/$(url_to_name "$url" "$rev")" tmpFile="$tmpPath/$(url_to_name "$url" "$rev")"
@ -374,7 +385,7 @@ else
clone_user_rev "$tmpFile" "$url" "$rev" clone_user_rev "$tmpFile" "$url" "$rev"
# Compute the hash. # Compute the hash.
hash=$(nix-hash --type $hashType --base32 $tmpFile) hash=$(nix-hash --type $hashType --base32 "$tmpFile")
# Add the downloaded file to the Nix store. # Add the downloaded file to the Nix store.
finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpFile") finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpFile")
@ -389,6 +400,6 @@ else
print_results "$hash" print_results "$hash"
if test -n "$PRINT_PATH"; then if test -n "$PRINT_PATH"; then
echo $finalPath echo "$finalPath"
fi fi
fi fi