summary refs log tree commit diff
path: root/pkgs/build-support/fetchpatch/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/build-support/fetchpatch/default.nix')
-rw-r--r--pkgs/build-support/fetchpatch/default.nix45
1 files changed, 30 insertions, 15 deletions
diff --git a/pkgs/build-support/fetchpatch/default.nix b/pkgs/build-support/fetchpatch/default.nix
index 6e25b2d6ecc..d46162c97ff 100644
--- a/pkgs/build-support/fetchpatch/default.nix
+++ b/pkgs/build-support/fetchpatch/default.nix
@@ -9,7 +9,8 @@ let
   # 0.3.4 would change hashes: https://github.com/NixOS/nixpkgs/issues/25154
   patchutils = buildPackages.patchutils_0_3_3;
 in
-{ stripLen ? 0
+{ relative ? null
+, stripLen ? 0
 , extraPrefix ? null
 , excludes ? []
 , includes ? []
@@ -17,7 +18,18 @@ in
 , postFetch ? ""
 , ...
 }@args:
-
+let
+  args' = if relative != null then {
+    stripLen = 1 + lib.length (lib.splitString "/" relative) + stripLen;
+    extraPrefix = if extraPrefix != null then extraPrefix else "";
+  } else {
+    inherit stripLen extraPrefix;
+  };
+in let
+  inherit (args') stripLen extraPrefix;
+in
+lib.throwIfNot (excludes == [] || includes == [])
+  "fetchpatch: cannot use excludes and includes simultaneously"
 fetchurl ({
   postFetch = ''
     tmpfile="$TMPDIR/patch"
@@ -27,17 +39,19 @@ fetchurl ({
       exit 1
     fi
 
-    "${patchutils}/bin/lsdiff" "$out" \
-      | sort -u | sed -e 's/[*?]/\\&/g' \
-      | xargs -I{} \
-        "${patchutils}/bin/filterdiff" \
-        --include={} \
-        --strip=${toString stripLen} \
-        ${lib.optionalString (extraPrefix != null) ''
-           --addoldprefix=a/${extraPrefix} \
-           --addnewprefix=b/${extraPrefix} \
-        ''} \
-        --clean "$out" > "$tmpfile"
+    "${patchutils}/bin/lsdiff" \
+      ${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
+      "$out" \
+    | sort -u | sed -e 's/[*?]/\\&/g' \
+    | xargs -I{} \
+      "${patchutils}/bin/filterdiff" \
+      --include={} \
+      --strip=${toString stripLen} \
+      ${lib.optionalString (extraPrefix != null) ''
+          --addoldprefix=a/${lib.escapeShellArg extraPrefix} \
+          --addnewprefix=b/${lib.escapeShellArg extraPrefix} \
+      ''} \
+      --clean "$out" > "$tmpfile"
 
     if [ ! -s "$tmpfile" ]; then
       echo "error: Normalized patch '$tmpfile' is empty (while the fetched file was not)!" 1>&2
@@ -64,5 +78,6 @@ fetchurl ({
     ${patchutils}/bin/interdiff "$out" /dev/null > "$tmpfile"
     mv "$tmpfile" "$out"
   '' + postFetch;
-  meta.broken = excludes != [] && includes != [];
-} // builtins.removeAttrs args ["stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"])
+} // builtins.removeAttrs args [
+  "relative" "stripLen" "extraPrefix" "excludes" "includes" "revert" "postFetch"
+])