A compact Haskell library for reading .gitignore files and filtering paths.
The library should also work with other .gitignore-like formats, e.g. .dockerignore. The rules for parsing and filtering where taken from the Git documentation pages. Input paths must be OsPath.
Given the following .gitignore file:
# Lines starting with '#' are ignored.
# Empty lines are ignored as well.
/foo
/bar/
/baz/**
!/baz/special
*.py?
The library can be used as follows:
> import Ignore (parse, ignores)
> import qualified Data.Text.IO as TIO
> import System.OsPath (unsafeEncodeUtf)
> :t parse
parse :: Text -> Ignore
> :t ignores
ignores :: Ignore -> OsPath -> Bool -> Bool
> os = unsafeEncodeUtf
> text <- TIO.readFile ".gitignore"
> ign = parse text
> ignores ign (os "foo") False
True
> ignores ign (os "bar") True
True
> ignores ign (os "baz/a/b/c") False
True
> ignores ign (os "baz/special") False
False
> ignores ign (os "foo.py") False
False
> ignores ign (os "foo.pyc") False
True
The ignores' variant of ignores takes an OsPath that was already split (i.e. [OsPath]) instead of OsPath directly. This is useful for contexts where the user is building paths by segment, for example when recursing into a directory. Using ignores' in this case help avoid making calls to splitDirectories.