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
20 args' = if relative != null then {
21 stripLen = 1 + lib.length (lib.splitString "/" relative) + stripLen;
22 extraPrefix = lib.optionalString (extraPrefix != null) extraPrefix;
24 inherit stripLen extraPrefix;
27 inherit (args') stripLen extraPrefix;
29 lib.throwIfNot (excludes == [] || includes == [])
30 "fetchpatch: cannot use excludes and includes simultaneously"
33 tmpfile="$TMPDIR/patch"
35 if [ ! -s "$out" ]; then
36 echo "error: Fetched patch file '$out' is empty!" 1>&2
41 ${decode} < "$out" > "$tmpfile"
42 if [ $? -ne 0 ] || [ ! -s "$tmpfile" ]; then
43 echo 'Failed to decode patch with command "'${lib.escapeShellArg decode}'"' >&2
44 echo 'Fetched file was (limited to 128 bytes):' >&2
45 od -A x -t x1z -v -N 128 "$out" >&2
51 "${patchutils}/bin/lsdiff" \
52 ${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
54 | sort -u | sed -e 's/[*?]/\\&/g' \
56 "${patchutils}/bin/filterdiff" \
58 --strip=${toString stripLen} \
59 ${lib.optionalString (extraPrefix != null) ''
60 --addoldprefix=a/${lib.escapeShellArg extraPrefix} \
61 --addnewprefix=b/${lib.escapeShellArg extraPrefix} \
63 --clean "$out" > "$tmpfile"
65 if [ ! -s "$tmpfile" ]; then
66 echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
67 echo "Did you maybe fetch a HTML representation of a patch instead of a raw patch?" 1>&2
68 echo "Fetched file was:" 1>&2
73 ${patchutils}/bin/filterdiff \
75 ${builtins.toString (builtins.map (x: "-x ${lib.escapeShellArg x}") excludes)} \
76 ${builtins.toString (builtins.map (x: "-i ${lib.escapeShellArg x}") includes)} \
79 if [ ! -s "$out" ]; then
80 echo "error: Filtered patch '$out' is empty (while the original patch file was not)!" 1>&2
81 echo "Check your includes and excludes." 1>&2
82 echo "Normalized patch file was:" 1>&2
86 '' + lib.optionalString revert ''
87 ${patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
90 } // builtins.removeAttrs args [
91 "relative" "stripLen" "decode" "extraPrefix" "excludes" "includes" "revert"