MCPcopy Create free account
hub / github.com/libgit2/pygit2 / get_attr

Method get_attr

pygit2/repository.py:1511–1575  ·  view source on GitHub ↗

Retrieve an attribute for a file by path. Returns: a boolean, `None` if the value is unspecified, or string with the value of the attribute. Parameters: path The path of the file to look up attributes for, relative to the workdir ro

(
        self,
        path: str | bytes | Path,
        name: str | bytes,
        flags: AttrCheck = AttrCheck.FILE_THEN_INDEX,
        commit: Oid | str | None = None,
    )

Source from the content-addressed store, hash-verified

1509 # Git attributes
1510 #
1511 def get_attr(
1512 self,
1513 path: str | bytes | Path,
1514 name: str | bytes,
1515 flags: AttrCheck = AttrCheck.FILE_THEN_INDEX,
1516 commit: Oid | str | None = None,
1517 ) -> bool | None | str:
1518 """
1519 Retrieve an attribute for a file by path.
1520
1521 Returns: a boolean, `None` if the value is unspecified, or string with
1522 the value of the attribute.
1523
1524 Parameters:
1525
1526 path
1527 The path of the file to look up attributes for, relative to the
1528 workdir root.
1529
1530 name
1531 The name of the attribute to look up.
1532
1533 flags
1534 A combination of enums.AttrCheck flags which determine the lookup order.
1535
1536 commit
1537 Optional id of commit to load attributes from when the
1538 `INCLUDE_COMMIT` flag is specified.
1539
1540 Examples::
1541
1542 >>> print(repo.get_attr('splash.bmp', 'binary'))
1543 True
1544 >>> print(repo.get_attr('splash.bmp', 'unknown-attr'))
1545 None
1546 >>> repo.get_attr('test.h', 'whitespace')
1547 'tab-in-indent,trailing-space'
1548 """
1549
1550 copts = ffi.new('git_attr_options *')
1551 copts.version = C.GIT_ATTR_OPTIONS_VERSION
1552 copts.flags = int(flags)
1553 if commit is not None:
1554 if not isinstance(commit, Oid):
1555 commit = Oid(hex=commit)
1556 ffi.buffer(ffi.addressof(copts, 'attr_commit_id'))[:] = commit.raw
1557
1558 cvalue = ffi.new('char **')
1559 err = C.git_attr_get_ext(
1560 cvalue, self._repo, copts, to_bytes(path), to_bytes(name)
1561 )
1562 check_error(err)
1563
1564 # Now let's see if we can figure out what the value is
1565 attr_kind = C.git_attr_value(cvalue[0])
1566 if attr_kind == C.GIT_ATTR_VALUE_UNSPECIFIED:
1567 return None
1568 elif attr_kind == C.GIT_ATTR_VALUE_TRUE:

Callers 2

test_no_attrFunction · 0.80
test_no_attr_aspathFunction · 0.80

Calls 3

to_bytesFunction · 0.85
check_errorFunction · 0.85
stringMethod · 0.80

Tested by 2

test_no_attrFunction · 0.64
test_no_attr_aspathFunction · 0.64