API
Functions
- gitmatch.compile(patterns: Iterable, ignorecase: bool = False) Gitignore [source]
Compile a collection of gitignore patterns into a
Gitignore
instance. Any invalid or empty patterns are discarded.Trailing newlines are stripped from the patterns before compiling, so you can compile a pre-existing
.gitignore
file by simply doing:with open("path/to/.gitignore") as fp: gi = gitmatch.compile(fp)
- Parameters
patterns – an iterable of gitignore patterns
ignorecase (bool) – Whether the patterns should match case-insensitively
- gitmatch.pattern2regex(pattern: AnyStr, ignorecase: bool = False) Optional[Regex] [source]
Convert a gitignore pattern to a regular expression and return a
Regex
object. If the pattern is empty or a comment, returnsNone
.- Parameters
pattern – a gitignore pattern
ignorecase (bool) – Whether the pattern should match case-insensitively
- Raises
InvalidPatternError – If the given pattern is invalid
Classes
Note
Although the Sphinx docs don’t show it, all of the gitmatch
classes are
generic in typing.AnyStr
; i.e., they should be written in type
annotations as Gitignore[AnyStr]
, Gitignore[str]
, or
Gitignore[bytes]
, as appropriate.
- class gitmatch.Gitignore[source]
A collection of compiled gitignore patterns
- match(path: Union[AnyStr, PathLike], is_dir: bool = False) Optional[Match] [source]
Test whether the given relative path matches the collection of patterns. If
is_dir
is true or ifpath
ends in a slash,path
is treated as a path to a directory; otherwise, it treated as a path to a file.If on Windows and
path
is not an instance ofpathlib.PurePosixPath
, or if on any OS andpath
is an instance ofpathlib.PureWindowsPath
, any backslashes inpath
will be converted to forward slashes before matching.If a match is found, a
Match
object is returned containing information about the matching pattern and the path or portion thereof that matched. TheMatch
object may be either “truthy” or “falsy” depending on whether the matching pattern is negative or not. If none of the patterns match the path,match()
returnsNone
. Hence, if you’re just interested in whether the patterns say the path should be gitignored, callbool()
on the result or use it in a boolean context like anif ... :
line.- Raises
InvalidPathError – If
path
is empty, is absolute, is not normalized (aside from an optional trailing slash), contains a NUL character, or starts with..
.
- class gitmatch.Pattern[source]
A compiled gitignore pattern
- match(path: AnyStr, is_dir: bool = False) bool [source]
Test whether the pattern matches the given path.
path
is assumed to be a relative, normalized,/
-separated path. Ifis_dir
is true, the path is assumed to refer to a directory; otherwise, it is assumed to refer to a file.Unlike
Gitignore.match()
, this method only testspath
itself, not any of its parent paths.
- pattern: AnyStr
The original gitignore pattern provided to
compile()
, with trailing spaces stripped
- regex: Pattern
A compiled regular expression pattern
- class gitmatch.Regex[source]
A gitignore pattern that has been converted to a regular expression
- pattern: AnyStr
The original gitignore pattern provided to
compile()
, with trailing spaces stripped
- regex: AnyStr
The regular expression equivalent of the pattern
Exceptions
- exception gitmatch.InvalidPathError[source]
Bases:
ValueError
Raised by
Gitignore.match()
when given an invalid path- msg
A description of the problem with the path
- path
The invalid path
- exception gitmatch.InvalidPatternError[source]
Bases:
ValueError
Raised by
pattern2regex()
when given an invalid pattern- pattern
The invalid pattern