From 5b4f92c8eea41f20b95f9e62a39b210400f4d2a9 Mon Sep 17 00:00:00 2001 From: Benjamin Poldrack Date: Tue, 18 Oct 2016 15:10:11 +0200 Subject: [PATCH 1/3] Allow for setting git options, that are persistent across subcommand calls --- git/cmd.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index f07573017..3d455546f 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -161,7 +161,7 @@ class Git(LazyMixin): Set its value to 'full' to see details about the returned values. """ __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info", - "_git_options", "_environment") + "_git_options", "_persistent_git_options", "_environment") _excluded_ = ('cat_file_all', 'cat_file_header', '_version_info') @@ -386,6 +386,7 @@ def __init__(self, working_dir=None): super(Git, self).__init__() self._working_dir = working_dir self._git_options = () + self._persistent_git_options = [] # Extra environment variables to pass to git commands self._environment = {} @@ -402,6 +403,20 @@ def __getattr__(self, name): return LazyMixin.__getattr__(self, name) return lambda *args, **kwargs: self._call_process(name, *args, **kwargs) + def set_persistent_git_options(self, **kwargs): + """Specify command line options to the git executable + for subsequent subcommand calls + + :param kwargs: + is a dict of keyword arguments. + these arguments are passed as in _call_process + but will be passed to the git command rather than + the subcommand. + """ + + self._persistent_git_options = self.transform_kwargs( + split_single_char_options=True, **kwargs) + def _set_cache_(self, attr): if attr == '_version_info': # 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): call = [self.GIT_PYTHON_GIT_EXECUTABLE] - # add the git options, the reset to empty + # add persistent git options + call.extend(self._persistent_git_options) + + # add the git options, then reset to empty # to avoid side_effects call.extend(self._git_options) self._git_options = () From f1b8d0c92e4b5797b95948bdb95bec7756f5189f Mon Sep 17 00:00:00 2001 From: Benjamin Poldrack Date: Tue, 18 Oct 2016 16:07:54 +0200 Subject: [PATCH 2/3] Add a test for persistent git options --- git/cmd.py | 2 +- git/test/test_git.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/git/cmd.py b/git/cmd.py index 3d455546f..1481ac81e 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -415,7 +415,7 @@ def set_persistent_git_options(self, **kwargs): """ self._persistent_git_options = self.transform_kwargs( - split_single_char_options=True, **kwargs) + split_single_char_options=True, **kwargs) def _set_cache_(self, attr): if attr == '_version_info': diff --git a/git/test/test_git.py b/git/test/test_git.py index bd8ebee2c..ef327c6df 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -160,6 +160,20 @@ def test_options_are_passed_to_git(self): git_command_version = self.git.version() self.assertEquals(git_version, git_command_version) + def test_persistent_options(self): + git_command_version = self.git.version() + # analog to test_options_are_passed_to_git + self.git.set_persistent_git_options(version=True) + git_version = self.git.NoOp() + self.assertEquals(git_version, git_command_version) + # subsequent calls keep this option: + git_version_2 = self.git.NoOp() + self.assertEquals(git_version_2, git_command_version) + + # reset to empty: + self.git.set_persistent_git_options() + self.assertRaises(GitCommandError, self.git.NoOp) + def test_single_char_git_options_are_passed_to_git(self): input_value = 'TestValue' output_value = self.git(c='user.name=%s' % input_value).config('--get', 'user.name') From bf8ce9464987c7b0dbe6acbc2cc2653e98ec739a Mon Sep 17 00:00:00 2001 From: Benjamin Poldrack Date: Wed, 19 Oct 2016 12:56:57 +0200 Subject: [PATCH 3/3] Fix flake8 error --- git/test/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/test/test_git.py b/git/test/test_git.py index ef327c6df..14c70e18f 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -172,7 +172,7 @@ def test_persistent_options(self): # reset to empty: self.git.set_persistent_git_options() - self.assertRaises(GitCommandError, self.git.NoOp) + self.assertRaises(GitCommandError, self.git.NoOp) def test_single_char_git_options_are_passed_to_git(self): input_value = 'TestValue'