Skip to content

Commit a139889

Browse files
codexByron
authored andcommitted
Address unsafe option review feedback
Filter unsafe-option candidates so keyword arguments are considered only when GitPython would actually transform at least one value into a command-line option. This avoids raising UnsafeOptionError for inert values such as None, False, or empty repeated-option lists. Also replace the standalone string expression after Commit.unsafe_git_rev_options with a normal comment, matching the reviewer request and avoiding misleading class-body strings. Validation: - .venv/bin/python -m compileall -q git/cmd.py git/objects/commit.py test/test_git.py test/test_clone.py test/test_remote.py - git diff --check - Focused pytest/unittest execution could not run because the environment is missing pytest, ddt, and gitdb, and PyPI access failed while installing test-requirements.txt.
1 parent 0119404 commit a139889

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

git/cmd.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,10 @@ def _option_candidates(cls, args: Sequence[Any] = (), kwargs: Optional[Mapping[s
10341034
option for option in cls._unpack_args([arg for arg in args if arg is not None]) if option.startswith("-")
10351035
]
10361036
if kwargs:
1037-
options.extend(str(key) for key in kwargs)
1037+
for key, value in kwargs.items():
1038+
values = value if isinstance(value, (list, tuple)) else (value,)
1039+
if any(value is True or (value is not False and value is not None) for value in values):
1040+
options.append(str(key))
10381041
return options
10391042

10401043
AutoInterrupt: TypeAlias = _AutoInterrupt

git/objects/commit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable):
8686
# INVARIANTS
8787
default_encoding = "UTF-8"
8888

89+
# Options to :manpage:`git-rev-list(1)` that can overwrite files.
8990
unsafe_git_rev_options = [
90-
# This option can truncate or write arbitrary files before revision parsing.
9191
"--output",
9292
"-o",
9393
]
94-
"""Options to :manpage:`git-rev-list(1)` that can overwrite files."""
9594

9695
type: Literal["commit"] = "commit"
9796

test/test_git.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ def test_check_unsafe_options_allows_attached_safe_short_option_values(self):
201201

202202
Git.check_unsafe_options(options=["-oupstream", "-bcurrent"], unsafe_options=unsafe_options)
203203

204+
def test_option_candidates_ignore_untransformed_kwargs(self):
205+
options = Git._option_candidates(
206+
kwargs={
207+
"output": None,
208+
"upload_pack": False,
209+
"exec": [],
210+
"config": [None, False],
211+
"max_count": 1,
212+
}
213+
)
214+
215+
self.assertEqual(options, ["max_count"])
216+
204217
_shell_cases = (
205218
# value_in_call, value_from_class, expected_popen_arg
206219
(None, False, False),

0 commit comments

Comments
 (0)