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.
7 { lib, fetchurl, patchutils }:
11 , decode ? "cat" # custom command to decode patch e.g. base64 -d
17 , nativeBuildInputs ? []
21 args' = if relative != null then {
22 stripLen = 1 + lib.length (lib.splitString "/" relative) + stripLen;
23 extraPrefix = lib.optionalString (extraPrefix != null) extraPrefix;
25 inherit stripLen extraPrefix;
28 inherit (args') stripLen extraPrefix;
30 lib.throwIfNot (excludes == [] || includes == [])
31 "fetchpatch: cannot use excludes and includes simultaneously"
33 nativeBuildInputs = [ patchutils ] ++ nativeBuildInputs;
35 tmpfile="$TMPDIR/patch"
37 if [ ! -s "$out" ]; then
38 echo "error: Fetched patch file '$out' is empty!" 1>&2
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
54 ${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
56 | sort -u | sed -e 's/[*?]/\\&/g' \
60 --strip=${toString stripLen} \
61 ${lib.optionalString (extraPrefix != null) ''
62 --addoldprefix=a/${lib.escapeShellArg extraPrefix} \
63 --addnewprefix=b/${lib.escapeShellArg extraPrefix} \
65 --clean "$out" > "$tmpfile"
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
77 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
78 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
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
88 '' + lib.optionalString revert ''
89 interdiff "$out" /dev/null > "$tmpfile"
92 } // builtins.removeAttrs args [
93 "relative" "stripLen" "decode" "extraPrefix" "excludes" "includes" "revert"
94 "postFetch" "nativeBuildInputs"