Skip to content

Commit 7dcb079

Browse files
committed
Implemented GIT_PYTHON_GIT_EXECUTABLE including test and docs
1 parent 1ddf05a commit 7dcb079

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

doc/source/changes.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Changelog
77
* **git** command wrapper
88

99
* Added ``version_info`` property which returns a tuple of integers representing the installed git version.
10-
10+
11+
* Added GIT_PYTHON_GIT_EXECUTABLE environment variable, which can be used to set the desired git executable to be used. despite of what would be found in the path.
12+
1113
* **Blob** Type
1214

1315
* Added mode constants to ease the manual creation of blobs
@@ -21,7 +23,9 @@ Changelog
2123
* Configuration file parsing is more robust. It should now be able to handle everything that the git command can parse as well.
2224
* The progress parsing was updated to support git 1.7.0.3 and newer. Previously progress was not enabled for the git command or only worked with ssh in case of older git versions.
2325
* Parsing of tags was improved. Previously some parts of the name could not be parsed properly.
24-
* The rev-parse pure python implementation now handles branches correctly if they look like hexadecimal sha's.
26+
* The rev-parse pure python implementation now handles branches correctly if they look like hexadecimal sha's.
27+
* GIT_PYTHON_TRACE is now set on class level of the Git type, previously it was a module level global variable.
28+
* GIT_PYTHON_GIT_EXECUTABLE is a class level variable as well.
2529

2630

2731
0.3.1 Beta 2

doc/source/tutorial.rst

+14
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ The special notion ``git.command(flag=True)`` will create a flag without value l
427427

428428
If ``None`` is found in the arguments, it will be dropped silently. Lists and tuples passed as arguments will be unpacked recursively to individual arguments. Objects are converted to strings using the str(...) function.
429429

430+
Git Command Debugging and Customization
431+
***************************************
432+
433+
Using environment variables, you can further adjust the behaviour of the git command.
434+
435+
* **GIT_PYTHON_TRACE**
436+
437+
* If set to non-0, all executed git commands will be printed to stdout.
438+
* if set to *full*, the executed git command will be printed along with its output.
439+
440+
* **GIT_PYTHON_GIT_EXECUTABLE**
441+
442+
* If set, it should contain the full path to the git executable, e.g. *c:\\Program Files (x86)\\Git\\bin\\git.exe* on windows or */usr/bin/git* on linux.
443+
430444
And even more ...
431445
*****************
432446

git/cmd.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
PIPE
1818
)
1919

20-
# Enables debugging of GitPython's git commands
21-
GIT_PYTHON_TRACE = os.environ.get("GIT_PYTHON_TRACE", False)
22-
2320
execute_kwargs = ('istream', 'with_keep_cwd', 'with_extended_output',
2421
'with_exceptions', 'as_process',
2522
'output_stream' )
@@ -29,6 +26,7 @@
2926
def dashify(string):
3027
return string.replace('_', '-')
3128

29+
3230
class Git(LazyMixin):
3331
"""
3432
The Git class manages communication with the Git binary.
@@ -50,6 +48,13 @@ class Git(LazyMixin):
5048
# The size in bytes read from stdout when copying git's output to another stream
5149
max_chunk_size = 1024*64
5250

51+
# Enables debugging of GitPython's git commands
52+
GIT_PYTHON_TRACE = os.environ.get("GIT_PYTHON_TRACE", False)
53+
54+
# Provide the full path to the git executable. Otherwise it assumes git is in the path
55+
GIT_PYTHON_GIT_EXECUTABLE = os.environ.get("GIT_PYTHON_GIT_EXECUTABLE", 'git')
56+
57+
5358
class AutoInterrupt(object):
5459
"""Kill/Interrupt the stored process instance once this instance goes out of scope. It is
5560
used to prevent processes piling up in case iterators stop reading.
@@ -311,7 +316,7 @@ def execute(self, command,
311316
:note:
312317
If you add additional keyword arguments to the signature of this method,
313318
you must update the execute_kwargs tuple housed in this module."""
314-
if GIT_PYTHON_TRACE and not GIT_PYTHON_TRACE == 'full':
319+
if self.GIT_PYTHON_TRACE and not self.GIT_PYTHON_TRACE == 'full':
315320
print ' '.join(command)
316321

317322
# Allow the user to have the command executed in their working dir.
@@ -358,7 +363,7 @@ def execute(self, command,
358363
proc.stdout.close()
359364
proc.stderr.close()
360365

361-
if GIT_PYTHON_TRACE == 'full':
366+
if self.GIT_PYTHON_TRACE == 'full':
362367
cmdstr = " ".join(command)
363368
if stderr_value:
364369
print "%s -> %d; stdout: '%s'; stderr: '%s'" % (cmdstr, status, stdout_value, stderr_value)
@@ -445,7 +450,7 @@ def _call_process(self, method, *args, **kwargs):
445450
ext_args = self.__unpack_args([a for a in args if a is not None])
446451
args = opt_args + ext_args
447452

448-
call = ["git", dashify(method)]
453+
call = [self.GIT_PYTHON_GIT_EXECUTABLE, dashify(method)]
449454
call.extend(args)
450455

451456
return self.execute(call, **_kwargs)

git/test/test_git.py

+10
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ def test_version(self):
9898
for n in v:
9999
assert isinstance(n, int)
100100
#END verify number types
101+
102+
def test_cmd_override(self):
103+
prev_cmd = self.git.GIT_PYTHON_GIT_EXECUTABLE
104+
try:
105+
# set it to something that doens't exist, assure it raises
106+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = os.path.join("some", "path", "which", "doesn't", "exist", "gitbinary")
107+
self.failUnlessRaises(OSError, self.git.version)
108+
finally:
109+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd
110+
#END undo adjustment

0 commit comments

Comments
 (0)