Skip to content

Commit 74b81bf

Browse files
committed
Add support for querying attributes
Expose a method in the repository which allows querying an attribute for a file and converts the result to the python equivalent.
1 parent e461198 commit 74b81bf

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

pygit2/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
GIT_REPOSITORY_INIT_SHARED_GROUP = C.GIT_REPOSITORY_INIT_SHARED_GROUP
6666
GIT_REPOSITORY_INIT_SHARED_ALL = C.GIT_REPOSITORY_INIT_SHARED_ALL
6767

68+
# GIT_ATTR_CHECK_*
69+
GIT_ATTR_CHECK_FILE_THEN_INDEX = C.GIT_ATTR_CHECK_FILE_THEN_INDEX
70+
GIT_ATTR_CHECK_INDEX_THEN_FILE = C.GIT_ATTR_CHECK_INDEX_THEN_FILE
71+
GIT_ATTR_CHECK_INDEX_ONLY = C.GIT_ATTR_CHECK_INDEX_ONLY
72+
GIT_ATTR_CHECK_NO_SYSTEM = C.GIT_ATTR_CHECK_NO_SYSTEM
73+
6874

6975
def init_repository(path, bare=False,
7076
flags=GIT_REPOSITORY_INIT_MKPATH,

pygit2/decl.h

+15
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,18 @@ int git_merge_commits(git_index **out, git_repository *repo, const git_commit *o
702702
int git_merge_trees(git_index **out, git_repository *repo, const git_tree *ancestor_tree, const git_tree *our_tree, const git_tree *their_tree, const git_merge_options *opts);
703703
int git_merge_file_from_index(git_merge_file_result *out, git_repository *repo, const git_index_entry *ancestor, const git_index_entry *ours, const git_index_entry *theirs, const git_merge_file_options *opts);
704704
void git_merge_file_result_free(git_merge_file_result *result);
705+
706+
#define GIT_ATTR_CHECK_FILE_THEN_INDEX ...
707+
#define GIT_ATTR_CHECK_INDEX_THEN_FILE ...
708+
#define GIT_ATTR_CHECK_INDEX_ONLY ...
709+
#define GIT_ATTR_CHECK_NO_SYSTEM ...
710+
711+
typedef enum {
712+
GIT_ATTR_UNSPECIFIED_T = 0,
713+
GIT_ATTR_TRUE_T,
714+
GIT_ATTR_FALSE_T,
715+
GIT_ATTR_VALUE_T,
716+
} git_attr_t;
717+
718+
int git_attr_get(const char **value_out, git_repository *repo, uint32_t flags, const char *path, const char *name);
719+
git_attr_t git_attr_value(const char *attr);

pygit2/repository.py

+38
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,41 @@ def ahead_behind(self, local, upstream):
764764
check_error(err)
765765

766766
return int(ahead[0]), int(behind[0])
767+
768+
#
769+
# Git attributes
770+
#
771+
def get_attr(self, path, name, flags=0):
772+
"""Retrieve an attribute for a file by path
773+
774+
Arguments
775+
776+
path
777+
The path of the file to look up attributes for, relative to the
778+
workdir root
779+
name
780+
The name of the attribute to look up
781+
flags
782+
A combination of GIT_ATTR_CHECK_ flags which determine the
783+
lookup order
784+
785+
Returns either a boolean, None (if the value is unspecified) or string
786+
with the value of the attribute.
787+
"""
788+
789+
cvalue = ffi.new('char **')
790+
err = C.git_attr_get(cvalue, self._repo, flags, to_bytes(path), to_bytes(name))
791+
check_error(err)
792+
793+
# Now let's see if we can figure out what the value is
794+
attr_kind = C.git_attr_value(cvalue[0])
795+
if attr_kind == C.GIT_ATTR_UNSPECIFIED_T:
796+
return None
797+
elif attr_kind == C.GIT_ATTR_TRUE_T:
798+
return True
799+
elif attr_kind == C.GIT_ATTR_FALSE_T:
800+
return False
801+
elif attr_kind == C.GIT_ATTR_VALUE_T:
802+
return ffi.string(cvalue[0]).decode('utf-8')
803+
804+
assert False, "the attribute value from libgit2 is invalid"

test/test_attributes.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Copyright 2010-2014 The pygit2 contributors
4+
#
5+
# This file is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License, version 2,
7+
# as published by the Free Software Foundation.
8+
#
9+
# In addition to the permissions in the GNU General Public License,
10+
# the authors give you unlimited permission to link the compiled
11+
# version of this file into combinations with other programs,
12+
# and to distribute those combinations without any restriction
13+
# coming from the use of this file. (The General Public License
14+
# restrictions do apply in other respects; for example, they cover
15+
# modification of the file, and distribution when not linked into
16+
# a combined executable.)
17+
#
18+
# This file is distributed in the hope that it will be useful, but
19+
# WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
# General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with this program; see the file COPYING. If not, write to
25+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26+
# Boston, MA 02110-1301, USA.
27+
28+
# Import from the future
29+
from __future__ import absolute_import
30+
from __future__ import unicode_literals, print_function
31+
32+
# Import from the Standard Library
33+
import binascii
34+
import unittest
35+
import tempfile
36+
import os
37+
from os.path import join, realpath
38+
import sys
39+
40+
# Import from pygit2
41+
from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT
42+
from pygit2 import init_repository, clone_repository, discover_repository
43+
from pygit2 import Oid, Reference, hashfile
44+
import pygit2
45+
from . import utils
46+
47+
try:
48+
import __pypy__
49+
except ImportError:
50+
__pypy__ = None
51+
52+
class RepositorySignatureTest(utils.RepoTestCase):
53+
54+
def test_no_attr(self):
55+
self.assertIsNone(self.repo.get_attr('file', 'foo'))
56+
57+
with open(join(self.repo.workdir, '.gitattributes'), 'w+') as f:
58+
print('*.py text\n', file=f)
59+
print('*.jpg -text\n', file=f)
60+
print('*.sh eol=lf\n', file=f)
61+
62+
self.assertIsNone(self.repo.get_attr('file.py', 'foo'))
63+
self.assertTrue(self.repo.get_attr('file.py', 'text'))
64+
self.assertFalse(self.repo.get_attr('file.jpg', 'text'))
65+
self.assertEqual("lf", self.repo.get_attr('file.sh', 'eol'))

0 commit comments

Comments
 (0)