2014-05-17 06:27:29 +01: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 21:58:56 +01:00
|
|
|
{ lib, fetchurl, buildPackages }:
|
2019-04-13 22:12:55 +01:00
|
|
|
let
|
2019-05-12 14:16:45 +01:00
|
|
|
# 0.3.4 would change hashes: https://github.com/NixOS/nixpkgs/issues/25154
|
2019-04-13 22:12:55 +01:00
|
|
|
patchutils = buildPackages.patchutils_0_3_3;
|
|
|
|
in
|
2018-07-16 21:45:07 +01:00
|
|
|
{ stripLen ? 0, extraPrefix ? null, excludes ? [], includes ? [], revert ? false, ... }@args:
|
2014-05-17 06:27:29 +01: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 06:27:29 +01:00
|
|
|
fetchurl ({
|
|
|
|
postFetch = ''
|
2020-02-14 11:56:51 +00:00
|
|
|
tmpfile="$TMPDIR/${tmpname}"
|
2018-07-15 12:57:37 +01:00
|
|
|
if [ ! -s "$out" ]; then
|
|
|
|
echo "error: Fetched patch file '$out' is empty!" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-04-13 22:12:55 +01:00
|
|
|
"${patchutils}/bin/lsdiff" "$out" \
|
2014-05-17 06:27:29 +01:00
|
|
|
| sort -u | sed -e 's/[*?]/\\&/g' \
|
|
|
|
| xargs -I{} \
|
2019-04-13 22:12:55 +01:00
|
|
|
"${patchutils}/bin/filterdiff" \
|
2014-05-17 06:27:29 +01: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 02:54:31 +01:00
|
|
|
''} \
|
2014-05-17 06:27:29 +01:00
|
|
|
--clean "$out" > "$tmpfile"
|
2018-07-15 12:57:37 +01: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 22:12:55 +01:00
|
|
|
${patchutils}/bin/filterdiff \
|
2017-07-26 11:59:02 +01:00
|
|
|
-p1 \
|
2018-07-15 08:58:47 +01:00
|
|
|
${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
|
|
|
|
${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
|
2017-07-26 11:59:02 +01:00
|
|
|
"$tmpfile" > "$out"
|
2018-07-16 21:45:07 +01:00
|
|
|
|
2018-07-15 12:57:37 +01: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 21:45:07 +01:00
|
|
|
'' + lib.optionalString revert ''
|
2019-04-13 22:12:55 +01:00
|
|
|
${patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
|
2018-07-16 21:45:07 +01:00
|
|
|
mv "$tmpfile" "$out"
|
|
|
|
'' + (args.postFetch or "");
|
2018-06-05 03:29:22 +01:00
|
|
|
meta.broken = excludes != [] && includes != [];
|
2018-07-16 21:45:07 +01:00
|
|
|
} // builtins.removeAttrs args ["stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"])
|