dol.gitignore¶
Read .gitignore lines by git’s rules, as a key filter over a store’s keys.
A store that copies a project folder (a sync, an export, a push to S3) has to keep
local the same files git keeps out of version control. gitignore_matcher() turns
.gitignore lines into a key -> bool predicate that composes with
filt_iter(). It reads a deliberate subset of the format and refuses
the rest (ValueError) rather than misread it: a misread line could copy what
should stay local.
>>> from dol import gitignore_matcher
>>> ignored = gitignore_matcher(["*.lock", "cache/"])
>>> ignored("deep/x.lock"), ignored("cache/blob"), ignored("scene.md")
(True, True, False)
Functions
|
|
- dol.gitignore.gitignore_matcher(lines, *, dirs=None)[source]¶
key -> bool: whether any oflines(.gitignorelines) covers the relative POSIX pathkey, by git’s rules.A line with a
/before its end is anchored at the root; one without matches a name at any depth; a trailing/matches folders only; wildcards never cross a/; a matched folder covers everything under it; case counts (as git’s does withcore.ignorecase=false). What is not read as git does raisesValueErrorinstead of being misread:!,#,**,\,[^,[:, surrounding spaces, empty lines, and akeythat is not a plain relative POSIX path (a backslash,./,..: on Windows, give aFilesstore’s keys as POSIX paths first).?and[...]take a byte, as in git (?does not match the one characteré), and never/; an unterminated, empty,/-holding or reversed[...], and//, are refused too.- Parameters:
dirs (
Callable[[str],bool] |None) –dirs(key)says whetherkeyitself is a folder. A key matcher cannot know that, and a trailing-/line matches only folders: so withoutdirsa key is taken as a file (cache/does not match the keycache, only what is under it). Asked at most once per call, and only when a folders-only line needs it.
- Return type:
>>> local = gitignore_matcher(["artifacts/shot_cache/", "notes.txt", "artifacts/v/*.lock"]) >>> local("artifacts/shot_cache/blobs/x"), local("deep/notes.txt"), local("artifacts/audio/a.wav") (True, True, False) >>> local("artifacts/v/a.json.lock"), local("artifacts/v/sub/a.lock") (True, False) >>> gitignore_matcher(["cache/"])("cache"), gitignore_matcher(["a/*/c"])("a/b/c/d") (False, True) >>> gitignore_matcher(["cache/"], dirs={"cache"}.__contains__)("cache") True