Skip to content

Repository files navigation

gitignorefile

BlackLinuxOS XWindowsPyPI

A spec-compliant .gitignore parser for Python.

Installation

pip3 install gitignorefile

Usage

gitignorefile.parse()

Parses single .gitignore file. Suppose /home/michael/project/.gitignore contains the following:

__pycache__/
*.py[cod]

Then:

importgitignorefilematches=gitignorefile.parse("/home/michael/project/.gitignore")
matches("/home/michael/project/main.py") # Falsematches("/home/michael/project/main.pyc") # Truematches("/home/michael/project/dir/main.pyc") # Truematches("/home/michael/project/__pycache__") # True

gitignorefile.ignore()

shutil.copytree() ignore function which checks if file is ignored by any .gitignore in the directory tree.

Example:

importshutilimportgitignorefileshutil.copytree("/source", "/destination", ignore=gitignorefile.ignore())

gitignorefile.ignored()

Checks if file is ignored by any .gitignore in the directory tree.

importgitignorefilegitignorefile.ignored("/home/michael/project/main.py") # False

gitignorefile.Cache

Caches .gitignore rules discovered in the directory tree.

importgitignorefilematches=gitignorefile.Cache()
matches("/home/michael/project/main.py") # Falsematches("/home/michael/project/main.pyc") # Truematches("/home/michael/project/dir/main.pyc") # Truematches("/home/michael/project/__pycache__") # True

Custom ignore file sources

You could override files, that will be used to fetch ignore rules. Default value is [".gitignore", ".git/info/exclude"].

importgitignorefilematches=gitignorefile.Cache(ignore_names=[".mylovelytoolignore"])
matches("/home/michael/project/main.py") # Falsematches("/home/michael/project/main.pyc") # Truematches("/home/michael/project/dir/main.pyc") # Truematches("/home/michael/project/__pycache__") # True

Credits

About

A spec-compliant `.gitignore` parser for Python

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages