hercules-ci / gitignore.nix

Nix functions for filtering local git sources
The Unlicense
242 stars 19 forks source link

A filter that includes .git #38

Open infinisil opened 4 years ago

infinisil commented 4 years ago

Since I needed this myself, here is a gitignore filter that includes the .git directory itself (which is often needed for running git commands):

{ lib, gitignoreFilter }: path:
let
  ignoreFilter = gitignoreFilter path;
  dotGit = toString path + "/.git";
in lib.cleanSourceWith {
  src = path;
  filter = path: type: lib.hasPrefix dotGit path || ignoreFilter path type;
}

It might be worth adding a filter with this functionality to this library

roberth commented 4 years ago

I think we can generalize this to a function that isn't specific to gitignore.nix. Something like unionFilterAndSource: (origSrc -> path -> type -> bool) -> src -> src. It can even memoize your dotGit by let binding the partially applied filter function.

infinisil commented 4 years ago

I guess combining filters with cleanSourceWith is like an AND statement, whereas this would add OR's, so the boolean logic equivalent is Disjunctive normal form. Maybe this can be a guiding principle in a generalized interface, e.g.

filterDNF [
  [ (only ".git") ]
  [ gitignore (not "foo" ) ]
]

For a filter that includes all of .git unioned with all files that pass both the gitignore and are not in the folder foo (or: .git OR (gitignore AND (NOT "foo")))

roberth commented 4 years ago

Doesn't have to be normal form though and my thinking was that set terminology is more natural here, because at this level we're thinking about sets of files rather than bools.

roberth commented 2 years ago

See https://github.com/NixOS/nixpkgs/pull/112083

roberth commented 2 years ago

that set terminology is more natural here, because at this level we're thinking about sets of files rather than bools.

This is a good first intuition but not entirely accurate as illustrated by the example where the union of a and b/c necessarily includes b. This is just a theoretical observation at this point. https://github.com/NixOS/nixpkgs/pull/112083 takes care of it.

infinisil commented 8 months ago

my thinking was that set terminology is more natural here, because at this level we're thinking about sets of files rather than bools.

Just happen to come across this issue again, and indeed, I now also think of it as sets of files! :laughing: https://github.com/NixOS/nixpkgs/pull/222981