diff options
| author | Silvan Mosberger <contact@infinisil.com> | 2023-11-15 01:19:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-15 01:19:36 +0100 |
| commit | 7e533bab6df5dc1d065adafceda44a106e3796a4 (patch) | |
| tree | ec580f2bbfbeca182f93ccb7521f7676da74303e /lib/fileset/internal.nix | |
| parent | Merge pull request #267372 from cu1ch3n/update-abella (diff) | |
| parent | lib.fileset.fileFilter: Don't run predicate unnecessarily (diff) | |
| download | nixpkgs-7e533bab6df5dc1d065adafceda44a106e3796a4.tar.gz | |
Merge pull request #267381 from tweag/fileset.fileFilter-path
`fileset.fileFilter`: Don't run predicate unnecessarily
Diffstat (limited to 'lib/fileset/internal.nix')
| -rw-r--r-- | lib/fileset/internal.nix | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/lib/fileset/internal.nix b/lib/fileset/internal.nix index 717253f45715..b245caade1f5 100644 --- a/lib/fileset/internal.nix +++ b/lib/fileset/internal.nix @@ -786,29 +786,40 @@ rec { _differenceTree (path + "/${name}") lhsValue (rhs.${name} or null) ) (_directoryEntries path lhs); + # Filters all files in a file set based on a predicate + # Type: ({ name, type, ... } -> Bool) -> FileSet -> FileSet _fileFilter = predicate: fileset: let - recurse = path: tree: + # Check the predicate for a single file + # Type: String -> String -> filesetTree + fromFile = name: type: + if + predicate { + inherit name type; + # To ensure forwards compatibility with more arguments being added in the future, + # adding an attribute which can't be deconstructed :) + "lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you're using `{ name, file }:`, use `{ name, file, ... }:` instead." = null; + } + then + type + else + null; + + # Check the predicate for all files in a directory + # Type: Path -> filesetTree + fromDir = path: tree: mapAttrs (name: subtree: if isAttrs subtree || subtree == "directory" then - recurse (path + "/${name}") subtree - else if - predicate { - inherit name; - type = subtree; - # To ensure forwards compatibility with more arguments being added in the future, - # adding an attribute which can't be deconstructed :) - "lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you're using `{ name, file }:`, use `{ name, file, ... }:` instead." = null; - } - then - subtree - else + fromDir (path + "/${name}") subtree + else if subtree == null then null + else + fromFile name subtree ) (_directoryEntries path tree); in if fileset._internalIsEmptyWithoutBase then _emptyWithoutBase else _create fileset._internalBase - (recurse fileset._internalBase fileset._internalTree); + (fromDir fileset._internalBase fileset._internalTree); } |
