summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/fetchgit/nix-prefetch-git
diff options
context:
space:
mode:
authorJörg Thalheim <joerg@higgsboson.tk>2016-10-08 19:07:20 +0200
committerJörg Thalheim <joerg@higgsboson.tk>2016-12-05 12:32:14 +0100
commit954d9953941d6c94abb5d2d549bd18486d0b27ab (patch)
tree469cd2f64a3155d8de535e6bca1201f2160fa494 /pkgs/build-support/fetchgit/nix-prefetch-git
parent7d44426ccd2cbef148fd4afada6568b04b952bf0 (diff)
nix-prefetch-git: escape string fields properly
json requires certain characters to be escaped in strings.
Diffstat (limited to 'pkgs/build-support/fetchgit/nix-prefetch-git')
-rwxr-xr-xpkgs/build-support/fetchgit/nix-prefetch-git32
1 files changed, 21 insertions, 11 deletions
diff --git a/pkgs/build-support/fetchgit/nix-prefetch-git b/pkgs/build-support/fetchgit/nix-prefetch-git
index 705d84c648bf..54945d203afb 100755
--- a/pkgs/build-support/fetchgit/nix-prefetch-git
+++ b/pkgs/build-support/fetchgit/nix-prefetch-git
@@ -322,6 +322,18 @@ clone_user_rev() {
fi
}
+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"
+}
print_results() {
hash="$1"
@@ -338,17 +350,15 @@ print_results() {
fi
fi
if test -n "$hash"; then
- echo "{"
- echo " \"url\": \"$url\","
- echo " \"rev\": \"$fullRev\","
- echo " \"date\": \"$commitDateStrict8601\","
- echo -n " \"$hashType\": \"$hash\""
- if test -n "$fetchSubmodules"; then
- echo ","
- echo -n " \"fetchSubmodules\": true"
- fi
- echo ""
- echo "}"
+ cat <<EOF
+{
+ "url": "$(json_escape "$url")",
+ "rev": "$(json_escape "$fullRev")",
+ "date": "$(json_escape "$commitDateStrict8601")",
+ "$(json_escape "$hashType")": "$(json_escape "$hash")",
+ "fetchSubmodules": $([[ -n "fetchSubmodules" ]] && echo true || echo false)
+}
+EOF
fi
}