Avoid top-level with ...; in pkgs/build-support/nix-gitignore/default.nix

We also renamed `filter` (as a name of a parameter) to `predicate` following the naming suggestion in code review. It's better!

Since it's not part of an attrset, the name can change with no impact to semantics, since it can't be observed with `builtins.functionArgs`.

```
$ nix-repl
Nix 2.21.0
Type :? for help.
nix-repl> f = x: y: z: (x + y + z)

nix-repl> builtins.functionArgs f
{ }

nix-repl> :doc builtins.functionArgs
Synopsis: builtins.functionArgs f

    Return a set containing the names of the formal arguments expected by the function f. The value of each attribute is a Boolean denoting whether the corresponding argument has a default value. For instance, functionArgs ({ x, y ?
    123}: ...) = { x = false; y = true; }.

    "Formal argument" here refers to the attributes pattern-matched by the function. Plain lambdas are not included, e.g. functionArgs (x: ...) = { }.
```
This commit is contained in:
Philip Taron 2024-03-14 09:58:31 -07:00 committed by Valentin Gagarin
parent 869e556606
commit 997e54a4fb

View File

@ -7,9 +7,32 @@
# - zero or more directories. For example, "a/**/b" matches "a/b", # - zero or more directories. For example, "a/**/b" matches "a/b",
# - "a/x/b", "a/x/y/b" and so on. # - "a/x/b", "a/x/y/b" and so on.
with builtins;
let let
inherit (builtins) filterSource;
inherit (lib)
concatStringsSep
elemAt
filter
head
isList
length
optionals
optionalString
pathExists
readFile
removePrefix
replaceStrings
stringLength
sub
substring
toList
trace
;
inherit (lib.strings) match split typeOf;
debug = a: trace a a; debug = a: trace a a;
last = l: elemAt l ((length l) - 1); last = l: elemAt l ((length l) - 1);
in rec { in rec {
@ -17,7 +40,7 @@ in rec {
filterPattern = patterns: root: filterPattern = patterns: root:
(name: _type: (name: _type:
let let
relPath = lib.removePrefix ((toString root) + "/") name; relPath = removePrefix ((toString root) + "/") name;
matches = pair: (match (head pair) relPath) != null; matches = pair: (match (head pair) relPath) != null;
matched = map (pair: [(matches pair) (last pair)]) patterns; matched = map (pair: [(matches pair) (last pair)]) patterns;
in in
@ -45,7 +68,7 @@ in rec {
escs = "\\*?"; escs = "\\*?";
splitString = splitString =
let recurse = str : [(substring 0 1 str)] ++ let recurse = str : [(substring 0 1 str)] ++
(lib.optionals (str != "") (recurse (substring 1 (stringLength(str)) str) )); (optionals (str != "") (recurse (substring 1 (stringLength(str)) str) ));
in str : recurse str; in str : recurse str;
chars = s: filter (c: c != "" && !isList c) (splitString s); chars = s: filter (c: c != "" && !isList c) (splitString s);
escape = s: map (c: "\\" + c) (chars s); escape = s: map (c: "\\" + c) (chars s);
@ -66,7 +89,7 @@ in rec {
handleSlashPrefix = l: handleSlashPrefix = l:
let let
split = (match "^(/?)(.*)" l); split = (match "^(/?)(.*)" l);
findSlash = l: lib.optionalString ((match ".+/.+" l) == null) l; findSlash = l: optionalString ((match ".+/.+" l) == null) l;
hasSlash = mapAroundCharclass findSlash l != l; hasSlash = mapAroundCharclass findSlash l != l;
in in
(if (elemAt split 0) == "/" || hasSlash (if (elemAt split 0) == "/" || hasSlash
@ -94,12 +117,12 @@ in rec {
gitignoreCompileIgnore = file_str_patterns: root: gitignoreCompileIgnore = file_str_patterns: root:
let let
onPath = f: a: if typeOf a == "path" then f a else a; onPath = f: a: if typeOf a == "path" then f a else a;
str_patterns = map (onPath readFile) (lib.toList file_str_patterns); str_patterns = map (onPath readFile) (toList file_str_patterns);
in concatStringsSep "\n" str_patterns; in concatStringsSep "\n" str_patterns;
gitignoreFilterPure = filter: patterns: root: name: type: gitignoreFilterPure = predicate: patterns: root: name: type:
gitignoreFilter (gitignoreCompileIgnore patterns root) root name type gitignoreFilter (gitignoreCompileIgnore patterns root) root name type
&& filter name type; && predicate name type;
# This is a very hacky way of programming this! # This is a very hacky way of programming this!
# A better way would be to reuse existing filtering by making multiple gitignore functions per each root. # A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
@ -145,23 +168,23 @@ in rec {
''); '');
withGitignoreFile = patterns: root: withGitignoreFile = patterns: root:
lib.toList patterns ++ [ ".git" ] ++ [(root + "/.gitignore")]; toList patterns ++ [ ".git" ] ++ [(root + "/.gitignore")];
withRecursiveGitignoreFile = patterns: root: withRecursiveGitignoreFile = patterns: root:
lib.toList patterns ++ [ ".git" ] ++ [(compileRecursiveGitignore root)]; toList patterns ++ [ ".git" ] ++ [(compileRecursiveGitignore root)];
# filterSource derivatives # filterSource derivatives
gitignoreFilterSourcePure = filter: patterns: root: gitignoreFilterSourcePure = predicate: patterns: root:
filterSource (gitignoreFilterPure filter patterns root) root; filterSource (gitignoreFilterPure predicate patterns root) root;
gitignoreFilterSource = filter: patterns: root: gitignoreFilterSource = predicate: patterns: root:
gitignoreFilterSourcePure filter (withGitignoreFile patterns root) root; gitignoreFilterSourcePure predicate (withGitignoreFile patterns root) root;
gitignoreFilterRecursiveSource = filter: patterns: root: gitignoreFilterRecursiveSource = predicate: patterns: root:
gitignoreFilterSourcePure filter (withRecursiveGitignoreFile patterns root) root; gitignoreFilterSourcePure predicate (withRecursiveGitignoreFile patterns root) root;
# "Filter"-less alternatives # "Predicate"-less alternatives
gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true); gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
gitignoreSource = patterns: let type = typeOf patterns; in gitignoreSource = patterns: let type = typeOf patterns; in