Skip to content

fetchpatch: add relative#165327

Merged
maralorn merged 5 commits intoNixOS:masterfrom
ncfavier:fetchpatch-relative
Mar 25, 2022
Merged

fetchpatch: add relative#165327
maralorn merged 5 commits intoNixOS:masterfrom
ncfavier:fetchpatch-relative

Conversation

@ncfavier
Copy link
Member

Allows restricting patches to a specific subdirectory, à la git diff --relative=subdir.

This cannot be done (cleanly) currently because the includes logic happens after stripLen is applied, so we can't match on subdir/*. This change adds a preIncludes argument to make this possible, as well as a relative meta-argument that takes the name of a subdirectory and sets preIncludes, stripLen and extraPrefix accordingly.

(I turned meta.broken into assertions as it seems cleaner (#43538 (review)).)

As a proof of concept, I changed every instance of fetchpatch { stripLen > 0; includes = [ ... ]; } in nixpkgs (checked by adding a builtins.trace and evaluating everything) to use relative instead. This changes some hashes because (besides the conversion to SRI) previously people would use stripLen to transform a/subdir/file into subdir/file, while with relative you get a/file instead.

There might be more applications in the haskell-updates branch, and in fact the whole motivation for this was #165014 (comment).

Example

fetchpatch {
  url = "https://github.com/lambdabot/lambdabot/pull/204.patch";
  relative = "lambdabot-core";
  sha256 = "...";
}

@github-actions github-actions bot added 6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) 6.topic: haskell General-purpose, statically typed, purely functional programming language 8.has: documentation This PR adds or changes documentation labels Mar 22, 2022
@ncfavier ncfavier force-pushed the fetchpatch-relative branch from 3185d3c to cfb6253 Compare March 22, 2022 21:16
@ofborg ofborg bot requested a review from 7c6f434c March 22, 2022 21:20
@ofborg ofborg bot added 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels Mar 22, 2022
@maralorn
Copy link
Member

This is so beautiful. I am not sure if we need all of those options, but relative is just amazing.

@maralorn
Copy link
Member

Do we have a way to test if this invalidates any other hashes in nixpkgs? (Although I assume a test like that would give us false positives because of unstable sources …)

@ncfavier
Copy link
Member Author

ncfavier commented Mar 22, 2022

Do we have a way to test if this invalidates any other hashes in nixpkgs?

Look at the source while assuming relative == null and preIncludes == []. The only difference is that now extraPrefix is escaped, which from a look at rg extraPrefix doesn't change anything. (Well, and lsdiff -p1 but that doesn't do anything if there are no includes.)

@ncfavier ncfavier requested a review from hjones2199 March 22, 2022 21:47
@maralorn
Copy link
Member

I just randomly invalidated a random hash. something called guitarix uses a fetchpatch with stripLen = 1.

Got this:

-p given without -i or -x; guessing that you meant --strip instead.
error: Normalized patch '/build/patch' is empty (while the fetched file was not)!
Did you maybe fetch a HTML representation of a patch instead of a raw patch?
Fetched file was:
From d8f003484c57d808682025dfb07a7a1fb848afdc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hubert=20Figui=C3=A8re?= <hub@figuiere.net>
Date: Sun, 25 Apr 2021 11:21:23 -0400
Subject: [PATCH] Issue #63 - Fix build on gcc 11

---
 trunk/src/headers/gx_system.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/trunk/src/headers/gx_system.h b/trunk/src/headers/gx_system.h
index d334ecfc..88d97567 100644
--- a/trunk/src/headers/gx_system.h
+++ b/trunk/src/headers/gx_system.h
@@ -132,7 +132,7 @@ inline T *atomic_get(T*& p) {
 
 template <class T>
 inline bool atomic_compare_and_exchange(T **p, T *oldv, T *newv) {
-    return g_atomic_pointer_compare_and_exchange(reinterpret_cast<void* volatile*>(p), static_cast<void*>(oldv), newv);
+    return g_atomic_pointer_compare_and_exchange(reinterpret_cast<void**>(p), static_cast<void*>(oldv), newv);
 }

@ncfavier ncfavier force-pushed the fetchpatch-relative branch from cfb6253 to 6ff03ad Compare March 22, 2022 23:05
@ncfavier
Copy link
Member Author

(Well, and lsdiff -p1 but that doesn't do anything if there are no includes.)

-p given without -i or -x; guessing that you meant --strip instead.

🤦

Fixed now.

Copy link
Member

@maralorn maralorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have invalidated a 6 interesting looking hashes for different fetchpatches all over nixpkgs, with and without stripLen. They all seemed fine.

I think this change is really nice. But because this is a rather central piece of infrastructure I would prefer to have another pair of eyes on this.

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests in tests.fetchpatch aka pkgs/build-support/fetchpatch/tests.nix.

Testing fixed-output derivations is tricky without invalidateFetcherByDrvHash, because successes are cached, so we really need those test cases.

@sternenseemann
Copy link
Member

I'd drop preIncludes, it is unnecessarily confusing and usually not needed. If needs be you can either use includes and an appropriate -p flag in patchFlags or get the patch using multiple relative fetchpatches.

Allows restricting patches to a specific subdirectory, à la
`git diff --relative=subdir`.

This cannot be done (cleanly) currently because the `includes` logic
happens *after* `stripLen` is applied, so we can't match on `subdir/*`.

This change adds a `relative` argument that makes this possible by
filtering files before doing any processing, and setting `stripLen` and
`extraPrefix` accordingly.
@ncfavier ncfavier force-pushed the fetchpatch-relative branch from a363192 to 9aced64 Compare March 25, 2022 00:09
@ncfavier
Copy link
Member Author

I'd drop preIncludes, it is unnecessarily confusing and usually not needed. If needs be you can either use includes and an appropriate -p flag in patchFlags or get the patch using multiple relative fetchpatches.

Agreed, dropped it. I also realised there's no good reason to disallow using stripLen with relative, so that simplifies things quite a bit.

Please add tests in tests.fetchpatch aka pkgs/build-support/fetchpatch/tests.nix.

Added a simple relative test and a full test that uses everything. The produced patch looks "correct" (maybe it's overkill though).

@roberth
Copy link
Member

roberth commented Mar 25, 2022

@ofborg build tests.fetchpatch

@7c6f434c
Copy link
Member

Hm, it looks like you drop a few includes restrictions; is it intentional?

@ncfavier
Copy link
Member Author

What do you mean? The only restriction on includes is that it cannot be used with excludes. I moved that assertion up into a throwIfNot.

@7c6f434c
Copy link
Member

I look at the package changes. It looks like the goal was to move from stripLen`` to relativewhere it simplifies things, but commit messages contain no indication whyincludes` parameters were removed. Weere they checked to be redundant?

@ncfavier
Copy link
Member Author

ncfavier commented Mar 25, 2022

Oh, yes, I looked at each patch carefully. The only one whose includes wasn't redundant is the first CGAL patch, but I saw that the patch was only replacing NULL with nullptr (CGAL/cgal#3979) and the build still succeeded so I left the whole directory in. (Sorry, should have mentioned that)

We can drop `includes` since there's only one file in that directory.
We can drop `includes` since there's only one file in that directory.
The `gcc-12-prereq.patch` patch now includes the entire `CGAL_Core`
subdirectory, but the patch only fixes warnings so this is fine.
We can drop `includes` since there's only one file in that directory.
@ncfavier ncfavier force-pushed the fetchpatch-relative branch from 9aced64 to a6bc988 Compare March 25, 2022 09:48
@maralorn maralorn merged commit 0e0bb20 into NixOS:master Mar 25, 2022
@ncfavier ncfavier deleted the fetchpatch-relative branch March 25, 2022 15:33
@ncfavier ncfavier changed the title fetchpatch: add preIncludes and relative fetchpatch: add relative Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: fetch Fetchers (e.g. fetchgit, fetchsvn, ...) 6.topic: haskell General-purpose, statically typed, purely functional programming language 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants