Repository navigation
fix(commit): don't skip a parent when iter_parents filters out self - #2355
Merged
Merged
Conversation
`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
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The skip adjustment introduces a TypeError for previously supported skip="2" and skip=None values when the probe excludes self, requiring normalization and regression coverage.
1 open issue
What changed in this PR
GitHub Copilot review: This PR fixes Commit.iter_parents() dropping a matching parent when paths or filters exclude the starting commit.
Changes:
- Probe filtered history before adjusting
skip, retaining the single-call path for unfiltered requests. - Add a merge-history fixture and regression coverage for paths, filters, ordering, and pagination options.
| File | Description |
|---|---|
| tests/fixtures/commit.sh | Adds tagged history with a merge and unrelated file changes. |
| test/test_commit.py | Tests filtered parent iteration and existing skip behavior. |
| git/objects/commit.py | Adjusts skip only when the filtered history includes the starting commit. |
Review effort: Balanced
Both reach rev-list unchanged on main (`None` is dropped, `"2"` becomes `--skip=2`), but the filtered-out path subtracted from them directly. Leave `None` alone, since there is nothing to skip, and convert the rest. Agent-Model: claude-opus-5-5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

I am a Claude (Opus 5.5) agent working through the contributor's account.
I'm still comparing GitPython's APIs with what Git itself returns. This time it's the history helpers in
git/objects/commit.py.Commit.iter_parents(paths=...)drops the newest matching parent whenever the commit itself doesn't touch those paths:iter_parentsrunsrev-list <self>and forces--skip=1to leave the commit itself out. rev-list only lists the commit when it matches the paths and filters. When it doesn't, the skip removes a real parent instead. A filter the commit doesn't match, such asauthor=orgrep=, has the same effect.When paths or other rev-list options are given, ask rev-list for its first commit under the same options. Only count the commit itself in
skipwhen it is that commit. Calls without paths or filters still make one rev-list call, andskipkeeps its current meaning.skip=Nonestill passes through unchanged and a numeric string such as"2"is converted.The probe costs one extra
rev-listprocess. When the commit doesn't match, it also costs a second walk back to the first match, which the real call repeats anyway (about 3 ms here forLICENSE, last changed 2506 commits back). I tried--no-walkto test only the commit itself. With--topo-orderit lists a merge that the normal walk prunes, so it can't stand in for the walk.Validation on Linux, CPython 3.14.4, Git 2.56.0:
test_iter_parents_when_self_is_filtered_out(new fixture case incommit.sh) covers paths,skip(includingNoneand"2"),max_count,reverse,first_parenton a merge andgrep. It fails without the change and passes with it under bothGIT_PYTHON_BACKEND=cliandgix.test/test_commit.pypasses (46) under both.author="A"on a three-commit repository where only the tip has another author:[A1]before,[A2, A1]after, both backends.Not run: mypy, other platforms and Python versions.