Skip to content

Commit 63a0bbe

Browse files
committed
Added version_info property to git command
1 parent 65f2dd0 commit 63a0bbe

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

doc/source/changes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ NEXT
1717
* **Blob** Type
1818

1919
* Added mode constants to ease the manual creation of blobs
20-
20+
2121
* ### Module Changes ###
2222

2323
* 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.
2424

25+
* ### Git Cmd ###
26+
27+
* Added ``version_info`` property to git command, returning a tuple of version numbers.
28+
2529
* ### Exceptions ###
2630

2731
* There is a new common base for all exceptions git-python will throw, namely `GitPythonError`.

git/cmd.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

77
import os, sys
8-
from util import *
8+
from util import LazyMixin
99
from exc import GitCommandError
1010

1111
from subprocess import (
@@ -26,7 +26,7 @@
2626
def dashify(string):
2727
return string.replace('_', '-')
2828

29-
class Git(object):
29+
class Git(LazyMixin):
3030
"""
3131
The Git class manages communication with the Git binary.
3232
@@ -41,7 +41,7 @@ class Git(object):
4141
of the command to stdout.
4242
Set its value to 'full' to see details about the returned values.
4343
"""
44-
__slots__ = ("_working_dir", "cat_file_all", "cat_file_header")
44+
__slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info")
4545

4646
# CONFIGURATION
4747
# The size in bytes read from stdout when copying git's output to another stream
@@ -214,14 +214,30 @@ def __getattr__(self, name):
214214
"""A convenience method as it allows to call the command as if it was
215215
an object.
216216
:return: Callable object that will execute call _call_process with your arguments."""
217-
if name[:1] == '_':
218-
raise AttributeError(name)
217+
if name[0] == '_':
218+
return LazyMixin.__getattr__(self, name)
219219
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
220220

221+
def _set_cache_(self, attr):
222+
if attr == '_version_info':
223+
version_numbers = self._call_process('version').rpartition(' ')[2]
224+
self._version_info = tuple(int(n) for n in version_numbers.split('.'))
225+
else:
226+
super(Git, self)._set_cache_(attr)
227+
#END handle version info
228+
229+
221230
@property
222231
def working_dir(self):
223232
""":return: Git directory we are working on"""
224233
return self._working_dir
234+
235+
@property
236+
def version_info(self):
237+
""":return: tuple(int, ...) tuple with integers representing the major, minor
238+
and additional version numbers as parsed from git version.
239+
This value is generated on demand and is cached"""
240+
return self._version_info
225241

226242
def execute(self, command,
227243
istream=None,

git/test/test_git.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ def test_persistent_cat_file_command(self):
9191
hexsha, typename, size = self.git.get_object_header(hexsha)
9292
hexsha, typename_two, size_two, data = self.git.get_object_data(hexsha)
9393
assert typename == typename_two and size == size_two
94+
95+
def test_version(self):
96+
v = self.git.version_info
97+
assert isinstance(v, tuple)
98+
for n in v:
99+
assert isinstance(n, int)
100+
#END verify number types

0 commit comments

Comments
 (0)