Merge pull request #112458 from thefloweringash/shebangs
This commit is contained in:
commit
bf5cc63b6c
@ -51,7 +51,7 @@ patchShebangs() {
|
|||||||
isScript "$f" || continue
|
isScript "$f" || continue
|
||||||
|
|
||||||
read -r oldInterpreterLine < "$f"
|
read -r oldInterpreterLine < "$f"
|
||||||
read -r oldPath arg0 args <<< "${oldInterpreterLine:3}"
|
read -r oldPath arg0 args <<< "${oldInterpreterLine:2}"
|
||||||
|
|
||||||
if [[ -z "$pathName" ]]; then
|
if [[ -z "$pathName" ]]; then
|
||||||
if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then
|
if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then
|
||||||
@ -88,17 +88,15 @@ patchShebangs() {
|
|||||||
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}
|
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}
|
||||||
|
|
||||||
if [[ -n "$oldPath" && "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]]; then
|
if [[ -n "$oldPath" && "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]]; then
|
||||||
if [[ -n "$newPath" ]] && [[ "$newPath" != "$oldPath" ]]; then
|
if [[ -n "$newPath" && "$newPath" != "$oldPath" ]]; then
|
||||||
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
|
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
|
||||||
# escape the escape chars so that sed doesn't interpret them
|
# escape the escape chars so that sed doesn't interpret them
|
||||||
escapedInterpreterLine=${newInterpreterLine//\\/\\\\}
|
escapedInterpreterLine=${newInterpreterLine//\\/\\\\}
|
||||||
|
|
||||||
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
|
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
|
||||||
timestamp=$(mktemp)
|
timestamp=$(stat --printf "%y" "$f")
|
||||||
touch -r "$f" "$timestamp"
|
|
||||||
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
|
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
|
||||||
touch -r "$timestamp" "$f"
|
touch --date "$timestamp" "$f"
|
||||||
rm "$timestamp"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done < <(find "$@" -type f -perm -0100 -print0)
|
done < <(find "$@" -type f -perm -0100 -print0)
|
||||||
|
@ -1,26 +1,70 @@
|
|||||||
{ lib, stdenv, runCommand }:
|
{ lib, stdenv, runCommand }:
|
||||||
|
|
||||||
let
|
let
|
||||||
bad-shebang = stdenv.mkDerivation {
|
tests = {
|
||||||
name = "bad-shebang";
|
bad-shebang = stdenv.mkDerivation {
|
||||||
dontUnpack = true;
|
name = "bad-shebang";
|
||||||
installPhase = ''
|
dontUnpack = true;
|
||||||
mkdir -p $out/bin
|
installPhase = ''
|
||||||
echo "#!/bin/sh" > $out/bin/test
|
mkdir -p $out/bin
|
||||||
echo "echo -n hello" >> $out/bin/test
|
echo "#!/bin/sh" > $out/bin/test
|
||||||
chmod +x $out/bin/test
|
echo "echo -n hello" >> $out/bin/test
|
||||||
'';
|
chmod +x $out/bin/test
|
||||||
|
'';
|
||||||
|
passthru = {
|
||||||
|
assertion = "grep -v '^#!/bin/sh' $out/bin/test > /dev/null";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
ignores-nix-store = stdenv.mkDerivation {
|
||||||
|
name = "ignores-nix-store";
|
||||||
|
dontUnpack = true;
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p $out/bin
|
||||||
|
echo "#!$NIX_STORE/path/to/sh" > $out/bin/test
|
||||||
|
echo "echo -n hello" >> $out/bin/test
|
||||||
|
chmod +x $out/bin/test
|
||||||
|
'';
|
||||||
|
passthru = {
|
||||||
|
assertion = "grep \"^#!$NIX_STORE/path/to/sh\" $out/bin/test > /dev/null";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
in runCommand "patch-shebangs-test" {
|
in runCommand "patch-shebangs-test" {
|
||||||
passthru = { inherit bad-shebang; };
|
passthru = { inherit (tests) bad-shebang ignores-nix-store; };
|
||||||
meta.platforms = lib.platforms.all;
|
meta.platforms = lib.platforms.all;
|
||||||
} ''
|
} ''
|
||||||
printf "checking whether patchShebangs works properly... ">&2
|
validate() {
|
||||||
if ! grep -q '^#!/bin/sh' ${bad-shebang}/bin/test; then
|
local name=$1
|
||||||
echo "yes" >&2
|
local testout=$2
|
||||||
touch $out
|
local assertion=$3
|
||||||
else
|
|
||||||
echo "no" >&2
|
echo -n "... $name: " >&2
|
||||||
|
|
||||||
|
local rc=0
|
||||||
|
(out=$testout eval "$assertion") || rc=1
|
||||||
|
|
||||||
|
if [ "$rc" -eq 0 ]; then
|
||||||
|
echo "yes" >&2
|
||||||
|
else
|
||||||
|
echo "no" >&2
|
||||||
|
fi
|
||||||
|
|
||||||
|
return "$rc"
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "checking whether patchShebangs works properly... ">&2
|
||||||
|
|
||||||
|
fail=
|
||||||
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: ''
|
||||||
|
validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1
|
||||||
|
'') tests)}
|
||||||
|
|
||||||
|
if [ "$fail" ]; then
|
||||||
|
echo "failed"
|
||||||
exit 1
|
exit 1
|
||||||
|
else
|
||||||
|
echo "succeeded"
|
||||||
|
touch $out
|
||||||
fi
|
fi
|
||||||
''
|
''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user