2014-05-17 05:27:29 +00:00
|
|
|
# This function downloads and normalizes a patch/diff file.
|
|
|
|
# This is primarily useful for dynamically generated patches,
|
|
|
|
# such as GitHub's or cgit's, where the non-significant content parts
|
|
|
|
# often change with updating of git or cgit.
|
|
|
|
# stripLen acts as the -p parameter when applying a patch.
|
|
|
|
|
2018-08-21 20:58:56 +00:00
|
|
|
{ lib, fetchurl, buildPackages }:
|
2019-04-13 21:12:55 +00:00
|
|
|
let
|
2019-05-12 13:16:45 +00:00
|
|
|
# 0.3.4 would change hashes: https://github.com/NixOS/nixpkgs/issues/25154
|
2019-04-13 21:12:55 +00:00
|
|
|
patchutils = buildPackages.patchutils_0_3_3;
|
|
|
|
in
|
2018-07-16 20:45:07 +00:00
|
|
|
{ stripLen ? 0, extraPrefix ? null, excludes ? [], includes ? [], revert ? false, ... }@args:
|
2014-05-17 05:27:29 +00:00
|
|
|
|
2020-02-14 11:56:51 +00:00
|
|
|
let
|
|
|
|
# Make base-64 encoded SRI hash filename-safe using RFC 4648 §5
|
|
|
|
tmpname = lib.replaceStrings [ "+" "/" "=" ] [ "-" "_" "" ] args.sha256;
|
|
|
|
in
|
2014-05-17 05:27:29 +00:00
|
|
|
fetchurl ({
|
|
|
|
postFetch = ''
|
2020-02-14 11:56:51 +00:00
|
|
|
tmpfile="$TMPDIR/${tmpname}"
|
2018-07-15 11:57:37 +00:00
|
|
|
if [ ! -s "$out" ]; then
|
|
|
|
echo "error: Fetched patch file '$out' is empty!" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-04-13 21:12:55 +00:00
|
|
|
"${patchutils}/bin/lsdiff" "$out" \
|
2014-05-17 05:27:29 +00:00
|
|
|
| sort -u | sed -e 's/[*?]/\\&/g' \
|
|
|
|
| xargs -I{} \
|
2019-04-13 21:12:55 +00:00
|
|
|
"${patchutils}/bin/filterdiff" \
|
2014-05-17 05:27:29 +00:00
|
|
|
--include={} \
|
|
|
|
--strip=${toString stripLen} \
|
2018-01-04 19:55:19 +00:00
|
|
|
${lib.optionalString (extraPrefix != null) ''
|
|
|
|
--addoldprefix=a/${extraPrefix} \
|
|
|
|
--addnewprefix=b/${extraPrefix} \
|
2016-07-29 01:54:31 +00:00
|
|
|
''} \
|
2014-05-17 05:27:29 +00:00
|
|
|
--clean "$out" > "$tmpfile"
|
2018-07-15 11:57:37 +00:00
|
|
|
if [ ! -s "$tmpfile" ]; then
|
|
|
|
echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
|
|
|
|
echo "Did you maybe fetch a HTML representation of a patch instead of a raw patch?" 1>&2
|
|
|
|
echo "Fetched file was:" 1>&2
|
|
|
|
cat "$out" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-04-13 21:12:55 +00:00
|
|
|
${patchutils}/bin/filterdiff \
|
2017-07-26 10:59:02 +00:00
|
|
|
-p1 \
|
2018-07-15 07:58:47 +00:00
|
|
|
${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
|
|
|
|
${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
|
2017-07-26 10:59:02 +00:00
|
|
|
"$tmpfile" > "$out"
|
2018-07-16 20:45:07 +00:00
|
|
|
|
2018-07-15 11:57:37 +00:00
|
|
|
if [ ! -s "$out" ]; then
|
|
|
|
echo "error: Filtered patch '$out$' is empty (while the original patch file was not)!" 1>&2
|
|
|
|
echo "Check your includes and excludes." 1>&2
|
|
|
|
echo "Normalizd patch file was:" 1>&2
|
|
|
|
cat "$tmpfile" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-07-16 20:45:07 +00:00
|
|
|
'' + lib.optionalString revert ''
|
2019-04-13 21:12:55 +00:00
|
|
|
${patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
|
2018-07-16 20:45:07 +00:00
|
|
|
mv "$tmpfile" "$out"
|
|
|
|
'' + (args.postFetch or "");
|
2018-06-05 02:29:22 +00:00
|
|
|
meta.broken = excludes != [] && includes != [];
|
2018-07-16 20:45:07 +00:00
|
|
|
} // builtins.removeAttrs args ["stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"])
|