From e54cd8fed2d1788618df64b319a30c7aed791191 Mon Sep 17 00:00:00 2001 From: Eric Brunson Date: Wed, 23 Apr 2014 11:13:54 -0600 Subject: [PATCH 1/3] add git command options Add __call__ method to Git object to allow passing git command options to the executable Change-Id: If1bc01008e66d3fd3811c15b56e58f38c95b9887 --- git/cmd.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 2d4aa7279..e57bb7610 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -42,7 +42,8 @@ class Git(LazyMixin): of the command to stdout. Set its value to 'full' to see details about the returned values. """ - __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info") + __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info", + "_git_options") # CONFIGURATION # The size in bytes read from stdout when copying git's output to another stream @@ -217,7 +218,8 @@ def __init__(self, working_dir=None): .git directory in case of bare repositories.""" super(Git, self).__init__() self._working_dir = working_dir - + self._git_options = () + # cached command slots self.cat_file_header = None self.cat_file_all = None @@ -417,6 +419,21 @@ def __unpack_args(cls, arg_list): # END for each arg return outlist + def __call__(self, **kwargs): + """Specify command line options to the git executable + for a subcommand call + + :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. + + ``Examples``:: + git(work_tree='/tmp').difftool()""" + self._git_options = self.transform_kwargs(**kwargs) + return self + def _call_process(self, method, *args, **kwargs): """Run the given git command with the specified arguments and return the result as a String @@ -455,7 +472,14 @@ def _call_process(self, method, *args, **kwargs): args = opt_args + ext_args def make_call(): - call = [self.GIT_PYTHON_GIT_EXECUTABLE, dashify(method)] + call = [self.GIT_PYTHON_GIT_EXECUTABLE] + + # add the git options, the reset to empty + # to avoid side_effects + call.extend(self._git_options) + self._git_options = () + + call.extend([dashify(method)]) call.extend(args) return call #END utility to recreate call after changes From f2df73b317a4e2834037be8b67084bee0b533bfe Mon Sep 17 00:00:00 2001 From: Eric Brunson Date: Wed, 23 Apr 2014 11:13:54 -0600 Subject: [PATCH 2/3] add git command options Add __call__ method to Git object to allow passing git command options to the executable requires flag to transform_kwargs add unit test Change-Id: If1bc01008e66d3fd3811c15b56e58f38c95b9887 --- git/cmd.py | 10 +++++++--- git/test/test_git.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index e57bb7610..b3274dd8f 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -388,7 +388,7 @@ def execute(self, command, else: return stdout_value - def transform_kwargs(self, **kwargs): + def transform_kwargs(self, split_single_char_options=False, **kwargs): """Transforms Python style kwargs into git command line options.""" args = list() for k, v in kwargs.items(): @@ -396,7 +396,10 @@ def transform_kwargs(self, **kwargs): if v is True: args.append("-%s" % k) elif type(v) is not bool: - args.append("-%s%s" % (k, v)) + if split_single_char_options: + args.extend(["-%s" % k, "%s" % v]) + else: + args.append("-%s%s" % (k, v)) else: if v is True: args.append("--%s" % dashify(k)) @@ -431,7 +434,8 @@ def __call__(self, **kwargs): ``Examples``:: git(work_tree='/tmp').difftool()""" - self._git_options = self.transform_kwargs(**kwargs) + self._git_options = self.transform_kwargs( + split_single_char_options=True, **kwargs) return self def _call_process(self, method, *args, **kwargs): diff --git a/git/test/test_git.py b/git/test/test_git.py index b61a0eea0..e39c95755 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -107,3 +107,15 @@ def test_cmd_override(self): finally: type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd #END undo adjustment + + def test_options_are_passed_to_git(self): + # This work because any command after git --version is ignored + git_version = self.git(version=True).NoOp() + git_command_version = self.git.version() + self.assertEquals(git_version, git_command_version) + + def test_single_char_git_options_are_passed_to_git(self): + input_value='TestValue' + output_value = self.git(c='user.name={}'.format(input_value)).config('--get', 'user.name') + self.assertEquals(input_value, output_value) + From f5ec638a77dd1cd38512bc9cf2ebae949e7a8812 Mon Sep 17 00:00:00 2001 From: Eric Brunson Date: Wed, 23 Apr 2014 11:13:54 -0600 Subject: [PATCH 3/3] add git command options Add __call__ method to Git object to allow passing git command options to the executable requires flag to transform_kwargs add unit tests Change-Id: If1bc01008e66d3fd3811c15b56e58f38c95b9887 --- git/test/test_git.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/git/test/test_git.py b/git/test/test_git.py index e39c95755..e67cb92b0 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -119,3 +119,5 @@ def test_single_char_git_options_are_passed_to_git(self): output_value = self.git(c='user.name={}'.format(input_value)).config('--get', 'user.name') self.assertEquals(input_value, output_value) + def test_change_to_transform_kwargs_does_not_break_command_options(self): + self.git.log(n=1)