{"id":2958,"library":"gitignore-parser","title":"Gitignore Parser","description":"gitignore-parser is a spec-compliant Python library for parsing `.gitignore` files and matching paths against their rules. It provides accurate behavior according to the Git documentation for ignore rules. The current version is 0.1.13, and the library receives regular updates for bug fixes and minor improvements.","status":"active","version":"0.1.13","language":"en","source_language":"en","source_url":"https://github.com/mherrmann/gitignore_parser","tags":["filesystem","git","parser","gitignore"],"install":[{"cmd":"pip install gitignore-parser","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"parse_gitignore","correct":"from gitignore_parser import parse_gitignore"},{"note":"The primary interface is via parsing functions, not a direct class constructor. 'parse_gitignore_str' was added in v0.1.12.","wrong":"from gitignore_parser import GitignoreParser","symbol":"parse_gitignore_str","correct":"from gitignore_parser import parse_gitignore_str"}],"quickstart":{"code":"from pathlib import Path\nfrom gitignore_parser import parse_gitignore, parse_gitignore_str\n\n# Example 1: Parsing from a file\ngitignore_path = Path(\"temp_test.gitignore\")\ngitignore_path.write_text(\"\"\"\n# Ignore build artifacts\n/build/\n*.log\n!important.log\n\"\"\")\n\n# Create a parser instance from the .gitignore file\nparser_file = parse_gitignore(gitignore_path)\n\n# Test matching paths\nprint(f\"Is 'build/app.exe' ignored? {parser_file.match('build/app.exe')}\")\nprint(f\"Is 'temp.log' ignored? {parser_file.match('temp.log')}\")\nprint(f\"Is 'important.log' ignored? {parser_file.match('important.log')}\")\n\ngitignore_path.unlink() # Clean up the temporary file\n\n# Example 2: Parsing from a string (available from v0.1.12+)\ngitignore_content = \"\"\"\n# Ignore temporary files\n*.tmp\n/cache/\n\"\"\"\nparser_str = parse_gitignore_str(gitignore_content)\n\nprint(f\"Is 'file.tmp' ignored (from string)? {parser_str.match('file.tmp')}\")\nprint(f\"Is 'project/cache/data.json' ignored (from string)? {parser_str.match('project/cache/data.json')}\")","lang":"python","description":"This quickstart demonstrates how to create a gitignore parser instance from both a file path and a string, then use its `match()` method to check if specific file paths are ignored according to the rules."},"warnings":[{"fix":"Upgrade to version 0.1.8 or newer to ensure correct, spec-compliant parsing behavior. Review your `.gitignore` files if upgrading from very old versions to ensure rules still behave as intended after the fixes.","message":"Older versions (pre-0.1.8 / pre-0.1.7) had incorrect parsing behavior for certain complex patterns, such as implicit anchoring, multi-asterisks, leading exclamation marks, and slashes in ranges. This could lead to rules not matching as expected according to the gitignore specification.","severity":"gotcha","affected_versions":"<0.1.8"},{"fix":"If you need to parse rules from a string, upgrade to version 0.1.12 or newer. For older versions, you must write the rules to a temporary file and use `parse_gitignore()`.","message":"The `parse_gitignore_str()` function, which allows parsing gitignore rules directly from a string without needing a file, was introduced in version 0.1.12. Prior versions only supported parsing from a file path using `parse_gitignore()`.","severity":"gotcha","affected_versions":"<0.1.12"},{"fix":"Windows users should upgrade to version 0.1.13 or newer to ensure robust and correct parsing behavior across different file system structures and path formats.","message":"Version 0.1.13 fixed some edge cases related to path matching on Windows, which could lead to incorrect ignore decisions for certain patterns and paths on Windows systems.","severity":"gotcha","affected_versions":"<0.1.13"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}