Skip to content

Commit d3e9438

Browse files
authored
Merge pull request #2184 from gitpython-developers/fix-output-arg
fix: reject unsafe output options in Commit.count
2 parents be5031d + 38553b6 commit d3e9438

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

git/objects/commit.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,21 @@ def summary(self) -> Union[str, bytes]:
269269
else:
270270
return self.message.split(b"\n", 1)[0]
271271

272-
def count(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs: Any) -> int:
272+
def count(
273+
self,
274+
paths: Union[PathLike, Sequence[PathLike]] = "",
275+
allow_unsafe_options: bool = False,
276+
**kwargs: Any,
277+
) -> int:
273278
"""Count the number of commits reachable from this commit.
274279
275280
:param paths:
276281
An optional path or a list of paths restricting the return value to commits
277282
actually containing the paths.
278283
284+
:param allow_unsafe_options:
285+
Allow unsafe options, like ``--output``.
286+
279287
:param kwargs:
280288
Additional options to be passed to :manpage:`git-rev-list(1)`. They must not
281289
alter the output style of the command, or parsing will yield incorrect
@@ -284,6 +292,11 @@ def count(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs: Any)
284292
:return:
285293
An int defining the number of reachable commits
286294
"""
295+
if not allow_unsafe_options:
296+
Git.check_unsafe_options(
297+
options=Git._option_candidates([], kwargs), unsafe_options=self.unsafe_git_rev_options
298+
)
299+
287300
# Yes, it makes a difference whether empty paths are given or not in our case as
288301
# the empty paths version will ignore merge commits for some reason.
289302
if paths:

test/test_commit.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ def test_iter_items_rejects_unsafe_options(self):
301301
with self.assertRaises(UnsafeOptionError):
302302
list(Commit.iter_items(self.rorepo, "HEAD", output=marker))
303303

304+
def test_count_rejects_unsafe_options(self):
305+
with tempfile.NamedTemporaryFile() as marker:
306+
with self.assertRaises(UnsafeOptionError):
307+
self.rorepo.head.commit.count(output=marker.name)
308+
304309
def test_rev_list_bisect_all(self):
305310
"""
306311
'git rev-list --bisect-all' returns additional information

0 commit comments

Comments
 (0)