Skip to content

Commit 07eaa1f

Browse files
committed
attr: support attr lookup by commit
1 parent 6d1f337 commit 07eaa1f

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

pygit2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
GIT_ATTR_CHECK_INDEX_THEN_FILE = C.GIT_ATTR_CHECK_INDEX_THEN_FILE
9595
GIT_ATTR_CHECK_INDEX_ONLY = C.GIT_ATTR_CHECK_INDEX_ONLY
9696
GIT_ATTR_CHECK_NO_SYSTEM = C.GIT_ATTR_CHECK_NO_SYSTEM
97+
GIT_ATTR_CHECK_INCLUDE_HEAD = C.GIT_ATTR_CHECK_INCLUDE_HEAD
98+
GIT_ATTR_CHECK_INCLUDE_COMMIT = C.GIT_ATTR_CHECK_INCLUDE_COMMIT
9799

98100
# GIT_FETCH_PRUNE
99101
GIT_FETCH_PRUNE_UNSPECIFIED = C.GIT_FETCH_PRUNE_UNSPECIFIED

pygit2/_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
# Order matters
5555
h_files = [
5656
'types.h',
57-
'attr.h',
5857
'oid.h',
58+
'attr.h',
5959
'blame.h',
6060
'buffer.h',
6161
'strarray.h',

pygit2/decl/attr.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#define GIT_ATTR_CHECK_FILE_THEN_INDEX 0
22
#define GIT_ATTR_CHECK_INDEX_THEN_FILE 1
33
#define GIT_ATTR_CHECK_INDEX_ONLY 2
4-
#define GIT_ATTR_CHECK_NO_SYSTEM 4
4+
#define GIT_ATTR_CHECK_NO_SYSTEM 4
5+
#define GIT_ATTR_CHECK_INCLUDE_HEAD 8
6+
#define GIT_ATTR_CHECK_INCLUDE_COMMIT 16
7+
8+
#define GIT_ATTR_OPTIONS_VERSION ...
59

610
typedef enum {
711
GIT_ATTR_VALUE_UNSPECIFIED = 0, /**< The attribute has been left unspecified */
@@ -10,10 +14,17 @@ typedef enum {
1014
GIT_ATTR_VALUE_STRING /**< This attribute has a value */
1115
} git_attr_value_t;
1216

13-
int git_attr_get(
17+
typedef struct {
18+
unsigned int version;
19+
unsigned int flags;
20+
git_oid *commit_id;
21+
git_oid attr_commit_id;
22+
} git_attr_options;
23+
24+
int git_attr_get_ext(
1425
const char **value_out,
1526
git_repository *repo,
16-
uint32_t flags,
27+
git_attr_options *opts,
1728
const char *path,
1829
const char *name);
1930

pygit2/repository.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ def ahead_behind(self, local, upstream):
13711371
#
13721372
# Git attributes
13731373
#
1374-
def get_attr(self, path, name, flags=0):
1374+
def get_attr(self, path, name, flags=0, commit=None):
13751375
"""
13761376
Retrieve an attribute for a file by path.
13771377
@@ -1391,6 +1391,10 @@ def get_attr(self, path, name, flags=0):
13911391
A combination of `GIT_ATTR_CHECK_` flags which determine the
13921392
lookup order.
13931393
1394+
commit
1395+
Optional id of commit to load attributes from when
1396+
`GIT_ATTR_CHECK_INCLUDE_COMMIT` is specified.
1397+
13941398
Examples::
13951399
13961400
>>> print(repo.get_attr('splash.bmp', 'binary'))
@@ -1401,8 +1405,16 @@ def get_attr(self, path, name, flags=0):
14011405
'tab-in-indent,trailing-space'
14021406
"""
14031407

1408+
copts = ffi.new('git_attr_options *')
1409+
copts.version = C.GIT_ATTR_OPTIONS_VERSION
1410+
copts.flags = flags
1411+
if commit is not None:
1412+
if not isinstance(commit, Oid):
1413+
commit = Oid(hex=commit)
1414+
ffi.buffer(ffi.addressof(copts, 'attr_commit_id'))[:] = commit.raw
1415+
14041416
cvalue = ffi.new('char **')
1405-
err = C.git_attr_get(cvalue, self._repo, flags, to_bytes(path), to_bytes(name))
1417+
err = C.git_attr_get_ext(cvalue, self._repo, copts, to_bytes(path), to_bytes(name))
14061418
check_error(err)
14071419

14081420
# Now let's see if we can figure out what the value is

0 commit comments

Comments
 (0)