Add extraRules to gitignoreFilterWith

This commit is contained in:
Robert Hensing 2022-07-21 13:24:03 +02:00
parent f2ea0f8ff1
commit fad6033fca
3 changed files with 31 additions and 10 deletions

View file

@ -14,16 +14,18 @@ for paths at or below this root path.
```nix
let
gitignore = (import (import ./nix/sources.nix)."gitignore.nix" { inherit lib; });
inherit (gitignore) gitignoreFilter;
inherit (gitignore) gitignoreFilterWith;
customerFilter = src:
let
# IMPORTANT: use a let binding like this to memoize info about the git directories.
srcIgnored = gitignoreFilter src;
srcIgnored = gitignoreFilterWith { basePath = src; extraRules = ''
*.xml
!i-need-this.xml
''; };
in
path: type:
srcIgnored path type
|| builtins.baseNameOf path == "i-need-this.xml";
srcIgnored path type && baseNameOf path != "just-an-example-of-custom-filter-code.out";
name = "example";
exampleSrc = ./.;

View file

@ -20,9 +20,12 @@ rec {
gitignoreFilter = basePath:
gitignoreFilterWith { inherit basePath; };
gitignoreFilterWith = { basePath }:
gitignoreFilterWith = { basePath, extraRules ? null, extraRulesWithContextDir ? [] }:
assert extraRules == null || builtins.typeOf extraRules == "string";
let
patternsBelowP = findPatternsTree basePath;
extraRules2 = extraRulesWithContextDir ++
lib.optional (extraRules != null) { contextDir = basePath; rules = extraRules; };
patternsBelowP = findPatternsTree extraRules2 basePath;
basePathStr = toString basePath;
in
path: type: let
@ -60,11 +63,11 @@ rec {
The patterns are mixed into the attrsets using the special key "/patterns".
Leaves are simply {}
*/
findPatternsTree = dir:
findPatternsTree = extraRules: dir:
let
listOfStartingPatterns = map ({contextDir, file, ...}:
parse-gitignore.gitignoreFilter (readFile file) contextDir
) (findAncestryGitignores dir);
listOfStartingPatterns = map ({contextDir, rules ? readFile file, file ? throw "gitignore.nix: A `file` or `rules` attribute is required in extraRulesWithContextDir items.", ...}:
parse-gitignore.gitignoreFilter rules contextDir
) (findAncestryGitignores dir ++ extraRules);
startingPatterns = builtins.foldl'
parse-gitignore.mergePattern
(defaultPatterns dir) # not the unit of merge but a set of defaults

View file

@ -22,4 +22,20 @@ in
subdir-9 = runner.makeTest { name = "subdir-9"; rootDir = testdata.sourceUnfiltered + "/test-tree"; subpath = "9-expected"; };
subdir-10 = runner.makeTest { name = "subdir-10"; rootDir = testdata.sourceUnfiltered + "/test-tree"; subpath = "10-subdir-ignoring-itself"; };
unit-tests =
let gitignoreNix = import ../default.nix { inherit (pkgs) lib; };
inherit (gitignoreNix) gitignoreFilterWith;
example = gitignoreFilterWith { basePath = ./.; extraRules = ''
*.foo
!*.bar
''; };
in
# Test that extraRules works:
assert example ./x.foo "regular" == false;
assert example ./x.bar "regular" == true;
assert example ./x.qux "regular" == true;
# End of test. (a drv to show a buildable attr when successful)
pkgs.emptyFile or null;
}