Skip to content

Commit 6ce54d9

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 944dba3 commit 6ce54d9

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
@@ -995,7 +995,10 @@ def _option_candidates(cls, args: Sequence[Any] = (), kwargs: Optional[Mapping[s
995995
option for option in cls._unpack_args([arg for arg in args if arg is not None]) if option.startswith("-")
996996
]
997997
if kwargs:
998-
options.extend(str(key) for key in kwargs)
998+
for key, value in kwargs.items():
999+
values = value if isinstance(value, (list, tuple)) else (value,)
1000+
if any(value is True or (value is not False and value is not None) for value in values):
1001+
options.append(str(key))
9991002
return options
10001003

10011004
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
@@ -173,6 +173,19 @@ def test_check_unsafe_options_normalizes_kwargs(self):
173173
with self.assertRaises(UnsafeOptionError):
174174
Git.check_unsafe_options(options=options, unsafe_options=unsafe_options)
175175

176+
def test_option_candidates_ignore_untransformed_kwargs(self):
177+
options = Git._option_candidates(
178+
kwargs={
179+
"output": None,
180+
"upload_pack": False,
181+
"exec": [],
182+
"config": [None, False],
183+
"max_count": 1,
184+
}
185+
)
186+
187+
self.assertEqual(options, ["max_count"])
188+
176189
_shell_cases = (
177190
# value_in_call, value_from_class, expected_popen_arg
178191
(None, False, False),

0 commit comments

Comments
 (0)