@@ -161,7 +161,7 @@ class Git(LazyMixin): | |||
| 161 | 161 | Set its value to 'full' to see details about the returned values. | |
| 162 | 162 | """ | |
| 163 | 163 | __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info", | |
| 164 | - "_git_options", "_environment") | ||
| 164 | + "_git_options", "_persistent_git_options", "_environment") | ||
| 165 | 165 | ||
| 166 | 166 | _excluded_ = ('cat_file_all', 'cat_file_header', '_version_info') | |
| 167 | 167 | ||
@@ -386,6 +386,7 @@ def __init__(self, working_dir=None): | |||
| 386 | 386 | super(Git, self).__init__() | |
| 387 | 387 | self._working_dir = working_dir | |
| 388 | 388 | self._git_options = () | |
| 389 | + self._persistent_git_options = [] | ||
| 389 | 390 | ||
| 390 | 391 | # Extra environment variables to pass to git commands | |
| 391 | 392 | self._environment = {} | |
@@ -402,6 +403,20 @@ def __getattr__(self, name): | |||
| 402 | 403 | return LazyMixin.__getattr__(self, name) | |
| 403 | 404 | return lambda *args, **kwargs: self._call_process(name, *args, **kwargs) | |
| 404 | 405 | ||
| 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 | + | ||
| 405 | 420 | def _set_cache_(self, attr): | |
| 406 | 421 | if attr == '_version_info': | |
| 407 | 422 | # 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): | |||
| 820 | 835 | ||
| 821 | 836 | call = [self.GIT_PYTHON_GIT_EXECUTABLE] | |
| 822 | 837 | ||
| 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 | ||
| 824 | 842 | # to avoid side_effects | |
| 825 | 843 | call.extend(self._git_options) | |
| 826 | 844 | self._git_options = () | |
@@ -160,6 +160,20 @@ def test_options_are_passed_to_git(self): | |||
| 160 | 160 | git_command_version = self.git.version() | |
| 161 | 161 | self.assertEquals(git_version, git_command_version) | |
| 162 | 162 | ||
| 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 | + | ||
| 163 | 177 | def test_single_char_git_options_are_passed_to_git(self): | |
| 164 | 178 | input_value = 'TestValue' | |
| 165 | 179 | output_value = self.git(c='user.name=%s' % input_value).config('--get', 'user.name') | |
0 commit comments