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.
16 decode ? "cat", # custom command to decode patch e.g. base64 -d
22 nativeBuildInputs ? [ ],
27 if relative != null then
29 stripLen = 1 + lib.length (lib.splitString "/" relative) + stripLen;
30 extraPrefix = lib.optionalString (extraPrefix != null) extraPrefix;
34 inherit stripLen extraPrefix;
38 inherit (args') stripLen extraPrefix;
40 lib.throwIfNot (excludes == [ ] || includes == [ ])
41 "fetchpatch: cannot use excludes and includes simultaneously"
45 nativeBuildInputs = [ patchutils ] ++ nativeBuildInputs;
48 tmpfile="$TMPDIR/patch"
50 if [ ! -s "$out" ]; then
51 echo "error: Fetched patch file '$out' is empty!" 1>&2
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
67 ${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
69 | sort -u | sed -e 's/[*?]/\\&/g' \
73 --strip=${toString stripLen} \
75 lib.optionalString (extraPrefix != null) ''
76 --addoldprefix=a/${lib.escapeShellArg extraPrefix} \
77 --addnewprefix=b/${lib.escapeShellArg extraPrefix} \
80 --clean "$out" > "$tmpfile"
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
92 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
93 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
96 if [ ! -s "$out" ]; then
97 echo "error: Filtered patch '$out' is empty (while the original patch file was not)!" 1>&2
98 echo "Check your includes and excludes." 1>&2
99 echo "Normalized patch file was:" 1>&2
104 + lib.optionalString revert ''
105 interdiff "$out" /dev/null > "$tmpfile"
110 // builtins.removeAttrs args [