module documentation

Parse .gitattributes file.

Class GitAttributes A collection of gitattributes patterns that can match paths.
Class Pattern A single gitattributes pattern.
Function match_path Get attributes for a path by matching against patterns.
Function parse_git_attributes Parse a Git attributes string.
Function parse_gitattributes_file Parse a gitattributes file and return compiled patterns.
Function read_gitattributes Read .gitattributes from a directory.
Variable AttributeValue Undocumented
Function _parse_attr Parse a git attribute into its value.
Function _translate_pattern Translate a gitattributes pattern to a regular expression.
def match_path(patterns: Sequence[tuple[Pattern, Mapping[bytes, AttributeValue]]], path: bytes) -> dict[bytes, AttributeValue]:

Get attributes for a path by matching against patterns.

Parameters
patterns:Sequence[tuple[Pattern, Mapping[bytes, AttributeValue]]]List of (Pattern, attributes) tuples
path:bytesPath to match (relative to repository root)
Returns
dict[bytes, AttributeValue]Dictionary of attributes that apply to this path
def parse_git_attributes(f: IO[bytes]) -> Generator[tuple[bytes, Mapping[bytes, AttributeValue]], None, None]:

Parse a Git attributes string.

>>> from io import BytesIO
>>> list(parse_git_attributes(BytesIO(b'''*.tar.* filter=lfs diff=lfs merge=lfs -text
...
... # store signatures in Git
... *.tar.*.asc -filter -diff merge=binary -text
...
... # store .dsc verbatim
... *.dsc -filter !diff merge=binary !text
... '''))) #doctest: +NORMALIZE_WHITESPACE
[(b'*.tar.*', {'filter': 'lfs', 'diff': 'lfs', 'merge': 'lfs', 'text': False}),
 (b'*.tar.*.asc', {'filter': False, 'diff': False, 'merge': 'binary', 'text': False}),
 (b'*.dsc', {'filter': False, 'diff': None, 'merge': 'binary', 'text': None})]
Parameters
f:IO[bytes]File-like object to read bytes from
Returns
Generator[tuple[bytes, Mapping[bytes, AttributeValue]], None, None]List of patterns and corresponding patterns in the order or them being encountered
def parse_gitattributes_file(filename: str | bytes) -> list[tuple[Pattern, Mapping[bytes, AttributeValue]]]:

Parse a gitattributes file and return compiled patterns.

Parameters
filename:str | bytesPath to the .gitattributes file
Returns
list[tuple[Pattern, Mapping[bytes, AttributeValue]]]List of (Pattern, attributes) tuples
def read_gitattributes(path: str | bytes) -> list[tuple[Pattern, Mapping[bytes, AttributeValue]]]:

Read .gitattributes from a directory.

Parameters
path:str | bytesDirectory path to check for .gitattributes
Returns
list[tuple[Pattern, Mapping[bytes, AttributeValue]]]List of (Pattern, attributes) tuples
AttributeValue =

Undocumented

def _parse_attr(attr: bytes) -> tuple[bytes, AttributeValue]:

Parse a git attribute into its value.

>>> _parse_attr(b'attr')
(b'attr', True)
>>> _parse_attr(b'-attr')
(b'attr', False)
>>> _parse_attr(b'!attr')
(b'attr', None)
>>> _parse_attr(b'attr=text')
(b'attr', b'text')
def _translate_pattern(pattern: bytes) -> bytes:

Translate a gitattributes pattern to a regular expression.

Similar to gitignore patterns, but simpler as gitattributes doesn't support all the same features (e.g., no directory-only patterns with trailing /).