1# This function downloads and normalizes a patch/diff file.
2# This is primarily useful for dynamically generated patches,
3# such as GitHub's or cgit's, where the non-significant content parts
4# often change with updating of git or cgit.
5# stripLen acts as the -p parameter when applying a patch.
6
7{ lib, fetchurl, buildPackages }:
8{ stripLen ? 0, extraPrefix ? null, excludes ? [], includes ? [], revert ? false, ... }@args:
9
10fetchurl ({
11 postFetch = ''
12 tmpfile="$TMPDIR/${args.sha256}"
13 if [ ! -s "$out" ]; then
14 echo "error: Fetched patch file '$out' is empty!" 1>&2
15 exit 1
16 fi
17 "${buildPackages.patchutils}/bin/lsdiff" "$out" \
18 | sort -u | sed -e 's/[*?]/\\&/g' \
19 | xargs -I{} \
20 "${buildPackages.patchutils}/bin/filterdiff" \
21 --include={} \
22 --strip=${toString stripLen} \
23 ${lib.optionalString (extraPrefix != null) ''
24 --addoldprefix=a/${extraPrefix} \
25 --addnewprefix=b/${extraPrefix} \
26 ''} \
27 --clean "$out" > "$tmpfile"
28 if [ ! -s "$tmpfile" ]; then
29 echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
30 echo "Did you maybe fetch a HTML representation of a patch instead of a raw patch?" 1>&2
31 echo "Fetched file was:" 1>&2
32 cat "$out" 1>&2
33 exit 1
34 fi
35 ${buildPackages.patchutils}/bin/filterdiff \
36 -p1 \
37 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
38 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
39 "$tmpfile" > "$out"
40
41 if [ ! -s "$out" ]; then
42 echo "error: Filtered patch '$out$' is empty (while the original patch file was not)!" 1>&2
43 echo "Check your includes and excludes." 1>&2
44 echo "Normalizd patch file was:" 1>&2
45 cat "$tmpfile" 1>&2
46 exit 1
47 fi
48 '' + lib.optionalString revert ''
49 ${buildPackages.patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
50 mv "$tmpfile" "$out"
51 '' + (args.postFetch or "");
52 meta.broken = excludes != [] && includes != [];
53} // builtins.removeAttrs args ["stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"])