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