@@ -12,6 +12,10 @@ fetchSubmodules=
1212builder=
1313branchName=$NIX_PREFETCH_GIT_BRANCH_NAME
1414
15+# ENV params
16+out=${out:-}
17+http_proxy=${http_proxy:-}
18+
1519# populated by clone_user_rev()
1620fullRev=
1721humanReadableRev=
@@ -64,7 +68,7 @@ for arg; do
6468 --builder) builder=true;;
6569 --help) usage; exit;;
6670 *)
67− argi=$(($argi + 1))
71+ argi=$((argi++))
6872 case $argi in
6973 1) url=$arg;;
7074 2) rev=$arg;;
@@ -76,7 +80,7 @@ for arg; do
7680 else
7781 case $argfun in
7882 set_*)
79− var=$(echo $argfun | sed 's,^set_,,')
83+ var=${argfun#set_}
8084 eval $var=$arg
8185 ;;
8286 esac
@@ -92,8 +96,8 @@ fi
9296init_remote(){
9397 local url=$1
9498 git init
95− git remote add origin $url
96− ( [ -n "$http_proxy" ] && git config http.proxy $http_proxy ) || true
99+ git remote add origin "$url"
100+ ( [ -n "$http_proxy" ] && git config http.proxy "$http_proxy" ) || true
97101}
98102
99103# Return the reference of an hash if it exists on the remote repository.
@@ -116,12 +120,13 @@ url_to_name(){
116120 local url=$1
117121 local ref=$2
118122 # basename removes the / and .git suffixes
119− local base=$(basename "$url" .git)
123+ local base
124+ base=$(basename "$url" .git)
120125
121126 if [[ $ref =~ ^[a-z0-9]+$ ]]; then
122127 echo "$base-${ref:0:7}"
123128 else
124− echo $base
129+ echo "$base"
125130 fi
126131}
127132
@@ -131,11 +136,11 @@ checkout_hash(){
131136 local ref="$2"
132137
133138 if test -z "$hash"; then
134− hash=$(hash_from_ref $ref)
139+ hash=$(hash_from_ref "$ref")
135140 fi
136141
137142 git fetch -t ${builder:+--progress} origin || return 1
138− git checkout -b $branchName $hash || return 1
143+ git checkout -b "$branchName" "$hash" || return 1
139144}
140145
141146# Fetch only a branch/tag and checkout it.
@@ -152,13 +157,13 @@ checkout_ref(){
152157 fi
153158
154159 if test -z "$ref"; then
155− ref=$(ref_from_hash $hash)
160+ ref=$(ref_from_hash "$hash")
156161 fi
157162
158163 if test -n "$ref"; then
159164 # --depth option is ignored on http repository.
160165 git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1
161− git checkout -b $branchName FETCH_HEAD || return 1
166+ git checkout -b "$branchName" FETCH_HEAD || return 1
162167 else
163168 return 1
164169 fi
@@ -171,27 +176,32 @@ init_submodules(){
171176
172177 # list submodule directories and their hashes
173178 git submodule status |
174− while read l; do
179+ while read -r l; do
180+ local hash
181+ local dir
182+ local name
183+ local url
184+
175185 # checkout each submodule
176− local hash=$(echo $l | awk '{print substr($1,2)}')
177− local dir=$(echo $l | awk '{print $2}')
178− local name=$(
186+ hash=$(echo "$l" | awk '{print substr($1,2)}')
187+ dir=$(echo "$l" | awk '{print $2}')
188+ name=$(
179189 git config -f .gitmodules --get-regexp submodule\..*\.path |
180190 sed -n "s,^\(.*\)\.path $dir\$,\\1,p")
181− local url=$(git config --get ${name}.url)
191+ url=$(git config --get "${name}.url")
182192
183193 clone "$dir" "$url" "$hash" ""
184194 done
185195}
186196
187197clone(){
188− local top=$(pwd)
198+ local top=$PWD
189199 local dir="$1"
190200 local url="$2"
191201 local hash="$3"
192202 local ref="$4"
193203
194− cd $dir
204+ cd "$dir"
195205
196206 # Initialize the repository.
197207 init_remote "$url"
@@ -208,9 +218,8 @@ clone(){
208218 init_submodules
209219 fi
210220
211− if [ -z "$builder" -a -f .topdeps ]; then
212− if tg help 2>&1 > /dev/null
213− then
221+ if [ -z "$builder" ] && [ -f .topdeps ]; then
222+ if tg help &>/dev/null; then
214223 echo "populating TopGit branches..."
215224 tg remote --populate origin
216225 else
@@ -219,7 +228,7 @@ clone(){
219228 fi
220229 fi
221230
222− cd $top
231+ cd "$top"
223232}
224233
225234# Remove all remote branches, remove tags not reachable from HEAD, do a full
@@ -236,14 +245,14 @@ make_deterministic_repo(){
236245 .git/refs/remotes/origin/HEAD .git/config
237246
238247 # Remove all remote branches.
239− git branch -r | while read branch; do
248+ git branch -r | while read -r branch; do
240249 git branch -rD "$branch" >&2
241250 done
242251
243252 # Remove tags not reachable from HEAD. If we're exactly on a tag, don't
244253 # delete it.
245254 maybe_tag=$(git tag --points-at HEAD)
246− git tag --contains HEAD | while read tag; do
255+ git tag --contains HEAD | while read -r tag; do
247256 if [ "$tag" != "$maybe_tag" ]; then
248257 git tag -d "$tag" >&2
249258 fi
@@ -270,7 +279,7 @@ _clone_user_rev() {
270279 HEAD|refs/*)
271280 clone "$dir" "$url" "" "$rev" 1>&2;;
272281 *)
273− if test -z "$(echo $rev | tr -d 0123456789abcdef)"; then
282+ if test -z "$(echo "$rev" | tr -d 0123456789abcdef)"; then
274283 clone "$dir" "$url" "$rev" "" 1>&2
275284 else
276285 echo 1>&2 "Bad commit hash or bad reference."
@@ -278,9 +287,9 @@ _clone_user_rev() {
278287 fi;;
279288 esac
280289
281− fullRev="$(cd $dir && (git rev-parse $rev 2> /dev/null || git rev-parse refs/heads/$branchName) | tail -n1)"
282− humanReadableRev="$(cd $dir && (git describe $fullRev 2> /dev/null || git describe --tags $fullRev 2> /dev/null || echo -- none --))"
283− commitDate="$(cd $dir && git show --no-patch --pretty=%ci $fullRev)"
290+ fullRev="$(cd "$dir" && (git rev-parse "$rev" 2> /dev/null || git rev-parse "refs/heads/$branchName") | tail -n1)"
291+ humanReadableRev="$(cd "$dir" && (git describe "$fullRev" 2> /dev/null || git describe --tags "$fullRev" 2> /dev/null || echo -- none --))"
292+ commitDate="$(cd "$dir" && git show --no-patch --pretty=%ci "$fullRev")"
284293
285294 # Allow doing additional processing before .git removal
286295 eval "$NIX_PREFETCH_GIT_CHECKOUT_HOOK"
@@ -288,7 +297,7 @@ _clone_user_rev() {
288297 echo "removing \`.git'..." >&2
289298 find "$dir" -name .git -print0 | xargs -0 rm -rf
290299 else
291− find "$dir" -name .git | while read gitdir; do
300+ find "$dir" -name .git | while read -r gitdir; do
292301 make_deterministic_repo "$(readlink -f "$gitdir/..")"
293302 done
294303 fi
@@ -299,6 +308,7 @@ clone_user_rev() {
299308 _clone_user_rev "$@"
300309 else
301310 errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")"
311+ # shellcheck disable=SC2064
302312 trap "rm -rf \"$errfile\"" EXIT
303313 _clone_user_rev "$@" 2> "$errfile" || (
304314 status="$?"
@@ -343,7 +353,7 @@ fi
343353
344354if test -n "$builder"; then
345355 test -n "$out" -a -n "$url" -a -n "$rev" || usage
346− mkdir -p $out
356+ mkdir -p "$out"
347357 clone_user_rev "$out" "$url" "$rev"
348358else
349359 if test -z "$hashType"; then
@@ -365,6 +375,7 @@ else
365375 if test -z "$finalPath"; then
366376
367377 tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")"
378+ # shellcheck disable=SC2064
368379 trap "rm -rf \"$tmpPath\"" EXIT
369380
370381 tmpFile="$tmpPath/$(url_to_name "$url" "$rev")"
@@ -374,7 +385,7 @@ else
374385 clone_user_rev "$tmpFile" "$url" "$rev"
375386
376387 # Compute the hash.
377− hash=$(nix-hash --type $hashType --base32 $tmpFile)
388+ hash=$(nix-hash --type $hashType --base32 "$tmpFile")
378389
379390 # Add the downloaded file to the Nix store.
380391 finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpFile")
@@ -389,6 +400,6 @@ else
389400 print_results "$hash"
390401
391402 if test -n "$PRINT_PATH"; then
392− echo $finalPath
403+ echo "$finalPath"
393404 fi
394405fi