about summary refs log tree commit diff
path: root/pkgs/build-support
diff options
context:
space:
mode:
authorAndrew Childs <lorne@cons.org.nz>2019-01-25 00:32:39 +0900
committerAndrew Childs <lorne@cons.org.nz>2019-01-25 01:52:26 +0900
commit597d6d2bcdc15ce330f88459d23fb3cab206a147 (patch)
tree720d0d51a48ff7505dba76a94f97b2ddac65108a /pkgs/build-support
parentbe89ca5564979200609025f7c2e56ed120bc0e00 (diff)
downloadnixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar.gz
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar.bz2
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar.lz
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar.xz
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.tar.zst
nixlib-597d6d2bcdc15ce330f88459d23fb3cab206a147.zip
nix-prefetch-git: propagate errors under --quiet
Diffstat (limited to 'pkgs/build-support')
-rwxr-xr-xpkgs/build-support/fetchgit/nix-prefetch-git47
1 files changed, 32 insertions, 15 deletions
diff --git a/pkgs/build-support/fetchgit/nix-prefetch-git b/pkgs/build-support/fetchgit/nix-prefetch-git
index fa4e504c908f..0a9314d22a3c 100755
--- a/pkgs/build-support/fetchgit/nix-prefetch-git
+++ b/pkgs/build-support/fetchgit/nix-prefetch-git
@@ -270,7 +270,7 @@ make_deterministic_repo(){
 }
 
 
-_clone_user_rev() {
+clone_user_rev() {
     local dir="$1"
     local url="$2"
     local rev="${3:-HEAD}"
@@ -307,19 +307,29 @@ _clone_user_rev() {
     fi
 }
 
-clone_user_rev() {
-    if ! test -n "$QUIET"; then
-        _clone_user_rev "$@"
-    else
-        errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")"
-        # shellcheck disable=SC2064
-        trap "rm -rf \"$errfile\"" EXIT
-        _clone_user_rev "$@" 2> "$errfile" || (
-            status="$?"
-            cat "$errfile" >&2
-            exit "$status"
-        )
+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
     fi
+    rm -f "$errfile"
+}
+
+quiet_mode() {
+    errfile="$(mktemp "${TMPDIR:-/tmp}/git-checkout-err-XXXXXXXX")"
+    exit_handlers+=(quiet_exit_handler)
+    exec 3>&2 2>"$errfile"
 }
 
 json_escape() {
@@ -362,6 +372,14 @@ EOF
     fi
 }
 
+remove_tmpPath() {
+    rm -rf "$tmpPath"
+}
+
+if test -n "$QUIET"; then
+    quiet_mode
+fi
+
 if test -z "$branchName"; then
     branchName=fetchgit
 fi
@@ -390,8 +408,7 @@ else
     if test -z "$finalPath"; then
 
         tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/git-checkout-tmp-XXXXXXXX")"
-        # shellcheck disable=SC2064
-        trap "rm -rf \"$tmpPath\"" EXIT
+        exit_handlers+=(remove_tmpPath)
 
         tmpFile="$tmpPath/$(url_to_name "$url" "$rev")"
         mkdir -p "$tmpFile"