Skip to content

Commit aab7dc2

Browse files
authored
Merge pull request #536 from bpoldrack/nf-persistent-git-options
Allow for setting git generic options, persisted across subcommand calls.
2 parents 9e4a454 + bf8ce94 commit aab7dc2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

‎git/cmd.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class Git(LazyMixin):
161161
Set its value to 'full' to see details about the returned values.
162162
"""
163163
__slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info",
164-
"_git_options", "_environment")
164+
"_git_options", "_persistent_git_options", "_environment")
165165

166166
_excluded_ = ('cat_file_all', 'cat_file_header', '_version_info')
167167

@@ -386,6 +386,7 @@ def __init__(self, working_dir=None):
386386
super(Git, self).__init__()
387387
self._working_dir = working_dir
388388
self._git_options = ()
389+
self._persistent_git_options = []
389390

390391
# Extra environment variables to pass to git commands
391392
self._environment = {}
@@ -402,6 +403,20 @@ def __getattr__(self, name):
402403
return LazyMixin.__getattr__(self, name)
403404
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
404405

406+
def set_persistent_git_options(self, **kwargs):
407+
"""Specify command line options to the git executable
408+
for subsequent subcommand calls
409+
410+
:param kwargs:
411+
is a dict of keyword arguments.
412+
these arguments are passed as in _call_process
413+
but will be passed to the git command rather than
414+
the subcommand.
415+
"""
416+
417+
self._persistent_git_options = self.transform_kwargs(
418+
split_single_char_options=True, **kwargs)
419+
405420
def _set_cache_(self, attr):
406421
if attr == '_version_info':
407422
# We only use the first 4 numbers, as everthing else could be strings in fact (on windows)
@@ -820,7 +835,10 @@ def _call_process(self, method, *args, **kwargs):
820835

821836
call = [self.GIT_PYTHON_GIT_EXECUTABLE]
822837

823-
# add the git options, the reset to empty
838+
# add persistent git options
839+
call.extend(self._persistent_git_options)
840+
841+
# add the git options, then reset to empty
824842
# to avoid side_effects
825843
call.extend(self._git_options)
826844
self._git_options = ()

‎git/test/test_git.py

+14
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ def test_options_are_passed_to_git(self):
160160
git_command_version = self.git.version()
161161
self.assertEquals(git_version, git_command_version)
162162

163+
def test_persistent_options(self):
164+
git_command_version = self.git.version()
165+
# analog to test_options_are_passed_to_git
166+
self.git.set_persistent_git_options(version=True)
167+
git_version = self.git.NoOp()
168+
self.assertEquals(git_version, git_command_version)
169+
# subsequent calls keep this option:
170+
git_version_2 = self.git.NoOp()
171+
self.assertEquals(git_version_2, git_command_version)
172+
173+
# reset to empty:
174+
self.git.set_persistent_git_options()
175+
self.assertRaises(GitCommandError, self.git.NoOp)
176+
163177
def test_single_char_git_options_are_passed_to_git(self):
164178
input_value = 'TestValue'
165179
output_value = self.git(c='user.name=%s' % input_value).config('--get', 'user.name')

0 commit comments

Comments
 (0)