diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2011-06-07 11:47:14 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2011-06-07 11:47:14 +0200 |
commit | 63a0bbe14d0b5b3a29f9647f4089604d8436458e (patch) | |
tree | b732fee35c6cc61d04227e1b49eeebfa21954977 /git | |
parent | 65f2dd0ab990adbe1a1470905090391ab5f2ce4e (diff) | |
download | gitpython-63a0bbe14d0b5b3a29f9647f4089604d8436458e.tar.gz |
Added version_info property to git command
Diffstat (limited to 'git')
-rw-r--r-- | git/cmd.py | 26 | ||||
-rw-r--r-- | git/test/test_git.py | 7 |
2 files changed, 28 insertions, 5 deletions
@@ -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 |