Skip to content

Commit 4450673

Browse files
committed
fix(commit): don't skip a parent when iter_parents filters out self
`Commit.iter_parents()` runs `git rev-list <self> -- <paths>` and forces `--skip=1` to drop the commit itself. That only works when rev-list lists the commit. With `paths` the commit doesn't touch, or a filter such as `grep` or `author` it doesn't match, rev-list never lists it, and the skip throws away the newest real parent instead: A1 (a) <- A2 (a) <- B (b) B.iter_parents(paths="a") -> [A1] # git rev-list B^ -- a: A2 A1 When `paths` or other rev-list options are given, ask rev-list for its first commit under the same options and only count the commit itself in `skip` when it is that commit. Calls without paths or filters keep the single rev-list call. `skip` keeps its existing meaning: 0 and 1 drop only the commit itself, larger values also drop parents. `test_iter_parents_when_self_is_filtered_out` covers paths, `skip`, `max_count`, `reverse`, `first_parent` on a merge and `grep`; it fails without this change under both the `cli` and `gix` backends. Agent-Model: claude-opus-5-5
1 parent 029964a commit 4450673

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎git/objects/commit.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs
489489
skip = kwargs.get("skip", 1)
490490
if skip == 0: # skip ourselves
491491
skip = 1
492+
if paths or kwargs.keys() - {"skip", "max_count"}:
493+
# rev-list leaves us out when we don't match the paths or filters, so there
494+
# is nothing of ours to skip.
495+
probe = {**kwargs, "skip": 0, "max_count": 1}
496+
if next(self.iter_items(self.repo, self, paths, **probe), None) != self:
497+
skip -= 1
492498
kwargs["skip"] = skip
493499

494500
return self.iter_items(self.repo, self, paths, **kwargs)

‎test/test_commit.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,21 @@ def test_iter_parents(self):
476476
self.assertEqual(first_parent, c.parents[0])
477477
# END for each
478478

479+
def test_iter_parents_when_self_is_filtered_out(self):
480+
with fixture_repo("commit.sh", "iter-parents") as repo:
481+
tip, merge = repo.commit("B"), repo.commit("M")
482+
a1, a2 = repo.commit("A1"), repo.commit("A2")
483+
for skip in (0, 1):
484+
self.assertEqual(list(tip.iter_parents(paths="a", skip=skip)), [a2, a1])
485+
self.assertEqual(list(tip.iter_parents(paths="a", skip=2)), [a1])
486+
self.assertEqual(list(tip.iter_parents(paths="a", max_count=1)), [a2])
487+
self.assertEqual(list(tip.iter_parents(paths="a", reverse=True)), [a1, a2])
488+
self.assertEqual(list(merge.iter_parents(paths="a", first_parent=True)), [a2, a1])
489+
self.assertEqual(list(merge.iter_parents(grep="^A")), [a2, a1])
490+
# Commits that do match keep the old meaning of skip.
491+
self.assertEqual(list(a2.iter_parents(paths="a")), [a1])
492+
self.assertEqual(list(tip.iter_parents(paths="b")), [])
493+
479494
def test_name_rev(self):
480495
name_rev = self.rorepo.head.commit.name_rev
481496
assert isinstance(name_rev, str)

‎tests/fixtures/commit.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ case "$1" in
3333
git tag removed "$commit"
3434
git update-ref refs/heads/master "$commit"
3535
;;
36+
iter-parents)
37+
# A merge whose other side and own commit both leave `a` alone.
38+
echo 1 > a && git add a && git commit -m A1 && git tag A1
39+
echo 2 > a && git add a && git commit -m A2 && git tag A2
40+
git checkout -b side A1
41+
echo s > s && git add s && git commit -m S
42+
git checkout master
43+
git merge --no-ff -m M side && git tag M
44+
echo x > b && git add b && git commit -m B && git tag B
45+
;;
3646
ambiguous)
3747
touch master
3848
git add master

0 commit comments

Comments
 (0)