Python Respect the .gitignore
Waylon Walker
Posted on January 17, 2022
Many tools such as ripgrep respect the .gitignore
file in the directory it's searching in. This helps make it incredibly faster and generally more intuitive for the user as it just searches files that are part of thier project and not things like their virtual environments, node modules, or compiled builds.
Editors like vscode often do not include files that are .gitignored in
their search either.
pathspec
is a pattern matching library that implements git's wildmatch
pattern so that you can ignore files included in your .gitignore
patterns. You might want this to help make your libraries more performant, or more intuitive for you users.
import pathspec from pathlib import Path
markdown_files = Path().glob('**/*.md') if (Path(".gitignore").exists():
lines = Path(".gitignore").read_text().splitlines()
spec = pathspec.PathSpec.from_lines("gitwildmatch", lines)
markdown_files = [
file for file in markdown_files if not spec.match_file(str(file))
]
pathspec
home page
This is my til series, I try to post daily in this series on things that I learn or teach someone. All my tils are posted in this feed on my website.
Posted on January 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 1, 2023