Skip to content

Commit a3c89a5

Browse files
committed
Support repeated kwargs
Some Git command line options are allowed to be repeated multiple times. Examples of this are the -C flag which may occur more than once to "strengthen" its effect, or the -L flag on Git blames, to select multiple blocks of lines to blame. $ git diff -C -C HEAD~1 HEAD $ git blame -L 1-3 -L 12-18 HEAD -- somefile.py This patch supports passing a list/tuple as the value part for kwargs, so that the generated Git command contain the repeated options.
1 parent 8bbf1a3 commit a3c89a5

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

git/cmd.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -764,23 +764,31 @@ def custom_environment(self, **kwargs):
764764
finally:
765765
self.update_environment(**old_env)
766766

767+
def transform_kwarg(self, name, value, split_single_char_options):
768+
if len(name) == 1:
769+
if value is True:
770+
return ["-%s" % name]
771+
elif type(value) is not bool:
772+
if split_single_char_options:
773+
return ["-%s" % name, "%s" % value]
774+
else:
775+
return ["-%s%s" % (name, value)]
776+
else:
777+
if value is True:
778+
return ["--%s" % dashify(name)]
779+
elif type(value) is not bool:
780+
return ["--%s=%s" % (dashify(name), value)]
781+
return []
782+
767783
def transform_kwargs(self, split_single_char_options=True, **kwargs):
768784
"""Transforms Python style kwargs into git command line options."""
769785
args = list()
770786
for k, v in kwargs.items():
771-
if len(k) == 1:
772-
if v is True:
773-
args.append("-%s" % k)
774-
elif type(v) is not bool:
775-
if split_single_char_options:
776-
args.extend(["-%s" % k, "%s" % v])
777-
else:
778-
args.append("-%s%s" % (k, v))
787+
if isinstance(v, (list, tuple)):
788+
for value in v:
789+
args += self.transform_kwarg(k, value, split_single_char_options)
779790
else:
780-
if v is True:
781-
args.append("--%s" % dashify(k))
782-
elif type(v) is not bool:
783-
args.append("--%s=%s" % (dashify(k), v))
791+
args += self.transform_kwarg(k, v, split_single_char_options)
784792
return args
785793

786794
@classmethod

git/test/test_git.py

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def test_it_transforms_kwargs_into_git_command_arguments(self):
7070
assert_equal(["--max-count"], self.git.transform_kwargs(**{'max_count': True}))
7171
assert_equal(["--max-count=5"], self.git.transform_kwargs(**{'max_count': 5}))
7272

73+
# Multiple args are supported by using lists/tuples
74+
assert_equal(["-L", "1-3", "-L", "12-18"], self.git.transform_kwargs(**{'L': ('1-3', '12-18')}))
75+
assert_equal(["-C", "-C"], self.git.transform_kwargs(**{'C': [True, True]}))
76+
7377
# order is undefined
7478
res = self.git.transform_kwargs(**{'s': True, 't': True})
7579
assert ['-s', '-t'] == res or ['-t', '-s'] == res

0 commit comments

Comments
 (0)