diff options
-rw-r--r-- | doc/source/changes.rst | 6 | ||||
-rw-r--r-- | git/cmd.py | 26 | ||||
-rw-r--r-- | git/test/test_git.py | 7 |
3 files changed, 33 insertions, 6 deletions
diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 131e0a5b..9bd09157 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -17,11 +17,15 @@ NEXT * **Blob** Type * Added mode constants to ease the manual creation of blobs - + * ### Module Changes ### * Removed rev_parse function from git.repo.fun - the respective functionality is available only through the repository's rev_parse method, which might in turn translate to any implementation. +* ### Git Cmd ### + + * Added ``version_info`` property to git command, returning a tuple of version numbers. + * ### Exceptions ### * There is a new common base for all exceptions git-python will throw, namely `GitPythonError`. @@ -5,7 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os, sys -from util import * +from util import LazyMixin from exc import GitCommandError from subprocess import ( @@ -26,7 +26,7 @@ __all__ = ('Git', ) def dashify(string): return string.replace('_', '-') -class Git(object): +class Git(LazyMixin): """ The Git class manages communication with the Git binary. @@ -41,7 +41,7 @@ class Git(object): of the command to stdout. Set its value to 'full' to see details about the returned values. """ - __slots__ = ("_working_dir", "cat_file_all", "cat_file_header") + __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info") # CONFIGURATION # The size in bytes read from stdout when copying git's output to another stream @@ -214,14 +214,30 @@ class Git(object): """A convenience method as it allows to call the command as if it was an object. :return: Callable object that will execute call _call_process with your arguments.""" - if name[:1] == '_': - raise AttributeError(name) + if name[0] == '_': + return LazyMixin.__getattr__(self, name) return lambda *args, **kwargs: self._call_process(name, *args, **kwargs) + def _set_cache_(self, attr): + if attr == '_version_info': + version_numbers = self._call_process('version').rpartition(' ')[2] + self._version_info = tuple(int(n) for n in version_numbers.split('.')) + else: + super(Git, self)._set_cache_(attr) + #END handle version info + + @property def working_dir(self): """:return: Git directory we are working on""" return self._working_dir + + @property + def version_info(self): + """:return: tuple(int, ...) tuple with integers representing the major, minor + and additional version numbers as parsed from git version. + This value is generated on demand and is cached""" + return self._version_info def execute(self, command, istream=None, diff --git a/git/test/test_git.py b/git/test/test_git.py index aba09c1d..b9a0b617 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -91,3 +91,10 @@ class TestGit(TestBase): hexsha, typename, size = self.git.get_object_header(hexsha) hexsha, typename_two, size_two, data = self.git.get_object_data(hexsha) assert typename == typename_two and size == size_two + + def test_version(self): + v = self.git.version_info + assert isinstance(v, tuple) + for n in v: + assert isinstance(n, int) + #END verify number types |