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{
8 lib,
9 fetchurl,
10 patchutils,
11}:
12
13{
14 relative ? null,
15 stripLen ? 0,
16 decode ? "cat", # custom command to decode patch e.g. base64 -d
17 extraPrefix ? null,
18 excludes ? [ ],
19 includes ? [ ],
20 revert ? false,
21 postFetch ? "",
22 nativeBuildInputs ? [ ],
23 ...
24}@args:
25let
26 args' =
27 if relative != null then
28 {
29 stripLen = 1 + lib.length (lib.splitString "/" relative) + stripLen;
30 extraPrefix = lib.optionalString (extraPrefix != null) extraPrefix;
31 }
32 else
33 {
34 inherit stripLen extraPrefix;
35 };
36in
37let
38 inherit (args') stripLen extraPrefix;
39in
40lib.throwIfNot (excludes == [ ] || includes == [ ])
41 "fetchpatch: cannot use excludes and includes simultaneously"
42 fetchurl
43 (
44 {
45 nativeBuildInputs = [ patchutils ] ++ nativeBuildInputs;
46 postFetch = ''
47 tmpfile="$TMPDIR/patch"
48
49 if [ ! -s "$out" ]; then
50 echo "error: Fetched patch file '$out' is empty!" 1>&2
51 exit 1
52 fi
53
54 set +e
55 ${decode} < "$out" > "$tmpfile"
56 if [ $? -ne 0 ] || [ ! -s "$tmpfile" ]; then
57 echo 'Failed to decode patch with command "'${lib.escapeShellArg decode}'"' >&2
58 echo 'Fetched file was (limited to 128 bytes):' >&2
59 od -A x -t x1z -v -N 128 "$out" >&2
60 exit 1
61 fi
62 set -e
63 mv "$tmpfile" "$out"
64
65 lsdiff \
66 ${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
67 "$out" \
68 | sort -u | sed -e 's/[*?]/\\&/g' \
69 | xargs -I{} --delimiter='\n' \
70 filterdiff \
71 --include={} \
72 --strip=${toString stripLen} \
73 ${
74 lib.optionalString (extraPrefix != null) ''
75 --addoldprefix=a/${lib.escapeShellArg extraPrefix} \
76 --addnewprefix=b/${lib.escapeShellArg extraPrefix} \
77 ''
78 } \
79 --clean "$out" > "$tmpfile"
80
81 if [ ! -s "$tmpfile" ]; then
82 echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
83 echo "Did you maybe fetch a HTML representation of a patch instead of a raw patch?" 1>&2
84 echo "Fetched file was:" 1>&2
85 cat "$out" 1>&2
86 exit 1
87 fi
88
89 filterdiff \
90 -p1 \
91 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
92 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
93 "$tmpfile" > "$out"
94
95 if [ ! -s "$out" ]; then
96 echo "error: Filtered patch '$out' is empty (while the original patch file was not)!" 1>&2
97 echo "Check your includes and excludes." 1>&2
98 echo "Normalized patch file was:" 1>&2
99 cat "$tmpfile" 1>&2
100 exit 1
101 fi
102 ''
103 + lib.optionalString revert ''
104 interdiff "$out" /dev/null > "$tmpfile"
105 mv "$tmpfile" "$out"
106 ''
107 + postFetch;
108 }
109 // builtins.removeAttrs args [
110 "relative"
111 "stripLen"
112 "decode"
113 "extraPrefix"
114 "excludes"
115 "includes"
116 "revert"
117 "postFetch"
118 "nativeBuildInputs"
119 ]
120 )