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 }:
8let
9 # 0.3.4 would change hashes: https://github.com/NixOS/nixpkgs/issues/25154
10 patchutils = buildPackages.patchutils_0_3_3;
11in
12{ stripLen ? 0, extraPrefix ? null, excludes ? [], includes ? [], revert ? false, ... }@args:
13
14fetchurl ({
15 postFetch = ''
16 tmpfile="$TMPDIR/${args.sha256}"
17 if [ ! -s "$out" ]; then
18 echo "error: Fetched patch file '$out' is empty!" 1>&2
19 exit 1
20 fi
21 "${patchutils}/bin/lsdiff" "$out" \
22 | sort -u | sed -e 's/[*?]/\\&/g' \
23 | xargs -I{} \
24 "${patchutils}/bin/filterdiff" \
25 --include={} \
26 --strip=${toString stripLen} \
27 ${lib.optionalString (extraPrefix != null) ''
28 --addoldprefix=a/${extraPrefix} \
29 --addnewprefix=b/${extraPrefix} \
30 ''} \
31 --clean "$out" > "$tmpfile"
32 if [ ! -s "$tmpfile" ]; then
33 echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
34 echo "Did you maybe fetch a HTML representation of a patch instead of a raw patch?" 1>&2
35 echo "Fetched file was:" 1>&2
36 cat "$out" 1>&2
37 exit 1
38 fi
39 ${patchutils}/bin/filterdiff \
40 -p1 \
41 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
42 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
43 "$tmpfile" > "$out"
44
45 if [ ! -s "$out" ]; then
46 echo "error: Filtered patch '$out$' is empty (while the original patch file was not)!" 1>&2
47 echo "Check your includes and excludes." 1>&2
48 echo "Normalizd patch file was:" 1>&2
49 cat "$tmpfile" 1>&2
50 exit 1
51 fi
52 '' + lib.optionalString revert ''
53 ${patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
54 mv "$tmpfile" "$out"
55 '' + (args.postFetch or "");
56 meta.broken = excludes != [] && includes != [];
57} // builtins.removeAttrs args ["stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"])