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