From fad6033fca96a90be36cb04f2779e0bbf7158af5 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 21 Jul 2022 13:24:03 +0200 Subject: [PATCH] Add extraRules to gitignoreFilterWith --- docs/gitignoreFilter.md | 10 ++++++---- find-files.nix | 15 +++++++++------ tests/default.nix | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/gitignoreFilter.md b/docs/gitignoreFilter.md index dbe1435..09089b0 100644 --- a/docs/gitignoreFilter.md +++ b/docs/gitignoreFilter.md @@ -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 = ./.; diff --git a/find-files.nix b/find-files.nix index 23e9177..5d7c560 100644 --- a/find-files.nix +++ b/find-files.nix @@ -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 diff --git a/tests/default.nix b/tests/default.nix index 4eef480..a02acfa 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -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; } \ No newline at end of file