From 44506732525ca5499ab913d9b3c8d61839c7aa46 Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:04:02 -0500 Subject: [PATCH 1/3] fix(commit): don't skip a parent when `iter_parents` filters out self `Commit.iter_parents()` runs `git rev-list -- ` 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 --- git/objects/commit.py | 6 ++++++ test/test_commit.py | 15 +++++++++++++++ tests/fixtures/commit.sh | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/git/objects/commit.py b/git/objects/commit.py index 185b08728..28ff19cb1 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -489,6 +489,12 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs skip = kwargs.get("skip", 1) if skip == 0: # skip ourselves skip = 1 + if paths or kwargs.keys() - {"skip", "max_count"}: + # rev-list leaves us out when we don't match the paths or filters, so there + # is nothing of ours to skip. + probe = {**kwargs, "skip": 0, "max_count": 1} + if next(self.iter_items(self.repo, self, paths, **probe), None) != self: + skip -= 1 kwargs["skip"] = skip return self.iter_items(self.repo, self, paths, **kwargs) diff --git a/test/test_commit.py b/test/test_commit.py index 8c09828e5..d56ebd60c 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -476,6 +476,21 @@ def test_iter_parents(self): self.assertEqual(first_parent, c.parents[0]) # END for each + def test_iter_parents_when_self_is_filtered_out(self): + with fixture_repo("commit.sh", "iter-parents") as repo: + tip, merge = repo.commit("B"), repo.commit("M") + a1, a2 = repo.commit("A1"), repo.commit("A2") + for skip in (0, 1): + self.assertEqual(list(tip.iter_parents(paths="a", skip=skip)), [a2, a1]) + self.assertEqual(list(tip.iter_parents(paths="a", skip=2)), [a1]) + self.assertEqual(list(tip.iter_parents(paths="a", max_count=1)), [a2]) + self.assertEqual(list(tip.iter_parents(paths="a", reverse=True)), [a1, a2]) + self.assertEqual(list(merge.iter_parents(paths="a", first_parent=True)), [a2, a1]) + self.assertEqual(list(merge.iter_parents(grep="^A")), [a2, a1]) + # Commits that do match keep the old meaning of skip. + self.assertEqual(list(a2.iter_parents(paths="a")), [a1]) + self.assertEqual(list(tip.iter_parents(paths="b")), []) + def test_name_rev(self): name_rev = self.rorepo.head.commit.name_rev assert isinstance(name_rev, str) diff --git a/tests/fixtures/commit.sh b/tests/fixtures/commit.sh index cad5d28d4..ec6de256e 100644 --- a/tests/fixtures/commit.sh +++ b/tests/fixtures/commit.sh @@ -33,6 +33,16 @@ case "$1" in git tag removed "$commit" git update-ref refs/heads/master "$commit" ;; + iter-parents) + # A merge whose other side and own commit both leave `a` alone. + echo 1 > a && git add a && git commit -m A1 && git tag A1 + echo 2 > a && git add a && git commit -m A2 && git tag A2 + git checkout -b side A1 + echo s > s && git add s && git commit -m S + git checkout master + git merge --no-ff -m M side && git tag M + echo x > b && git add b && git commit -m B && git tag B + ;; ambiguous) touch master git add master From 5a991b98affb07fc0ac5c74793fc01e00b731820 Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:03:57 -0500 Subject: [PATCH 2/3] fix(commit): accept `skip=None` and numeric strings in `iter_parents` 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 --- git/objects/commit.py | 4 ++-- test/test_commit.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/git/objects/commit.py b/git/objects/commit.py index 28ff19cb1..5c3fc7868 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -493,8 +493,8 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs # rev-list leaves us out when we don't match the paths or filters, so there # is nothing of ours to skip. probe = {**kwargs, "skip": 0, "max_count": 1} - if next(self.iter_items(self.repo, self, paths, **probe), None) != self: - skip -= 1 + if skip is not None and next(self.iter_items(self.repo, self, paths, **probe), None) != self: + skip = int(skip) - 1 kwargs["skip"] = skip return self.iter_items(self.repo, self, paths, **kwargs) diff --git a/test/test_commit.py b/test/test_commit.py index d56ebd60c..d0495217e 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -483,6 +483,8 @@ def test_iter_parents_when_self_is_filtered_out(self): for skip in (0, 1): self.assertEqual(list(tip.iter_parents(paths="a", skip=skip)), [a2, a1]) self.assertEqual(list(tip.iter_parents(paths="a", skip=2)), [a1]) + self.assertEqual(list(tip.iter_parents(paths="a", skip="2")), [a1]) + self.assertEqual(list(tip.iter_parents(paths="a", skip=None)), [a2, a1]) self.assertEqual(list(tip.iter_parents(paths="a", max_count=1)), [a2]) self.assertEqual(list(tip.iter_parents(paths="a", reverse=True)), [a1, a2]) self.assertEqual(list(merge.iter_parents(paths="a", first_parent=True)), [a2, a1]) From 58e336e1e376e209fd2d49c91389f83b6b244aa8 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 24 Nov 2026 06:19:47 +0100 Subject: [PATCH 3/3] review --- doc/gix-backend.md | 1 + git/objects/commit.py | 74 ++++++++++++++++++++++++++++++++++--------- test/test_commit.py | 25 ++++++++++++++- 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/doc/gix-backend.md b/doc/gix-backend.md index 909b8c707..021279979 100644 --- a/doc/gix-backend.md +++ b/doc/gix-backend.md @@ -992,6 +992,7 @@ identify the historical fixture where needed. | GIX-34 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, an author, committer or tagger line with fields after its time zone, such as `A 1112591163 +0200 1700000000 -0500`, decodes through `Commit.author()`/`committer()` and `Tag.tagger()` with the correct seconds but an offset of 0. Git reads only the first date and time zone after the last `>` (`split_ident_line`) and reports `1112591163 +0200` through `git show --format=%ad --date=raw` and `%(taggerdate:raw)`. `git fsck` reports `badTimezone` for such objects, but the default `transfer.fsckObjects=false` accepts them. The `trailing-date` read-only `commit.sh` fixture in `test_deserialization_reads_date_after_email` reproduces both object kinds. | Report the time zone Git reads for identity lines with trailing fields, or reject them consistently. `object_metadata()` selects CLI when the decoded signature does not reproduce the end of its raw header line. This adapter guard does not close the upstream compatibility bug. | | GIX-35 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `ModulesFile.from_bytes()` accepts `.gitmodules` lines that Git rejects with `bad config line`, so `git submodule update --init` stops while `git fsck` reports only `gitmodulesParse`. A line holding more than one name is read as several settings: after `url = ../good`, `x url = ../evil` makes `url()` return `../evil` with `submodule..x` true, and `[submodule "module"] x url = ../evil` does the same on the header line. `br#anch = main` is read as an implicit `br` followed by a comment. The `submodule-rejected-lines.sh` read-only fixture in `test_submodule_configuration_rejects_lines_git_rejects` reproduces all three. | Reject the lines Git rejects, at least in a Git-strict parsing mode, with a typed error. `submodule_config()` selects CLI unless every line is blank, a comment, a bare submodule header or a single `key` or `key = value` setting, so Git's parser reports the error. This adapter guard does not close the upstream compatibility bug. | | GIX-36 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `Tree.iter()` reports tree entry modes as stored, while Git canonicalizes them when it reads a tree (`canon_mode`): `git ls-tree` and checkout use `100755` for `100700`, `120000` for `120777` and `160000` for every mode whose type is not a regular file, symbolic link or directory. `TreeEntry.mode()` returned `0o100700` and `0o120777`, so `Blob.mode` did not match `Blob.executable_mode` or `Blob.link_mode` for entries Git checks out as an executable or a symbolic link. A `140000` entry, which Git reads as a gitlink, is reported as a tree with mode `0o40000`, so GitPython listed the contents of an object Git never checks out; other noncanonical gitlink modes made tree iteration raise `TypeError`. `git fsck` reports these trees only as `badFilemode` warnings, so default transfers accept them. The `tree-modes.sh` read-only fixture in `test_tree_entry_modes_match_git` reproduces all three for SHA-1 and SHA-256. | Report the modes Git reads, at least in a Git-compatible mode, or expose the raw mode bytes. `_ls_tree()` selects CLI unless every native mode is one Git reports and the tree's bytes re-encode to the same object ID through the in-memory object database, so Git lists the entries. This adapter guard does not close the upstream compatibility bug. | +| GIX-37 | Bug (open) | On macOS with Git 2.54.0 and GixPython 0.3.0, create a linear history `A <- B <- C` with `HEAD` at `C` and write `B`'s full SHA-1 followed by a space and newline to `.git/shallow`. `git rev-list --skip=1 HEAD` returns only `B` and stops at the shallow boundary. `Repository.rev_walk([repo.head_id()]).all()` instead raises a `gix.Error` when consumed: `gix-shallow` treats the trailing space as a 41st hexadecimal character. | Match Git's shallow-file parsing in Gitoxide. `Commit.iter_parents(first_parent=False)` keeps the existing CLI fallback in `_backend.history()` and returns `B` with or without `max_count=1`. An explicitly native `first_parent=True` walk still exposes the parsing mismatch, which also occurs through `Commit.iter_items(..., first_parent=True, skip=1)`. | GIX-14 also covers configured worktree symlink resolution. On macOS with GixPython 0.3.0, CPython 3.12.14 and Git 2.54.0, set a submodule's diff --git a/git/objects/commit.py b/git/objects/commit.py index 5c3fc7868..12464f27b 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -5,9 +5,10 @@ __all__ = ["Commit"] -from collections import defaultdict +from collections import defaultdict, deque import datetime from io import BytesIO +from itertools import chain, islice import logging import os from subprocess import Popen @@ -36,6 +37,7 @@ from typing import ( Any, + Deque, Dict, IO, Iterator, @@ -480,24 +482,66 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs contain at least one of the paths. :param kwargs: - All arguments allowed by :manpage:`git-rev-list(1)`. + All arguments allowed by :manpage:`git-rev-list(1)`. ``skip`` values of + 0 and 1 skip only ``self``; larger values also skip matching ancestors. :return: Iterator yielding :class:`Commit` objects which are parents of ``self`` """ - # skip ourselves - skip = kwargs.get("skip", 1) - if skip == 0: # skip ourselves - skip = 1 - if paths or kwargs.keys() - {"skip", "max_count"}: - # rev-list leaves us out when we don't match the paths or filters, so there - # is nothing of ours to skip. - probe = {**kwargs, "skip": 0, "max_count": 1} - if skip is not None and next(self.iter_items(self.repo, self, paths, **probe), None) != self: - skip = int(skip) - 1 - kwargs["skip"] = skip - - return self.iter_items(self.repo, self, paths, **kwargs) + skip = max(0, int(kwargs.pop("skip", None) or 1) - 1) + count_options = {"max_count", "max-count", "n"} + if not paths and kwargs.keys() <= count_options: + return self.iter_items(self.repo, self, paths, skip=skip + 1, **kwargs) + + # Keep Git's option order when resolving alternate spellings of max_count. + limits = {key: kwargs.pop(key) for key in tuple(kwargs) if key in count_options} + count_args = self.repo.git.transform_kwargs(**limits) if limits else [] + max_count = int(count_args[-1].rsplit("=", 1)[-1]) if count_args else -1 + if max_count == 0: + return self.iter_items(self.repo, self, paths, max_count=0, **kwargs) + + reverse_args = self.repo.git.transform_kwargs(reverse=kwargs["reverse"]) if "reverse" in kwargs else [] + reverse = bool(reverse_args) and all(arg == "--reverse" for arg in reverse_args) + if reverse and max_count < 0: + commits = self.iter_items(self.repo, self, paths, **kwargs) + + def skip_from_end() -> Iterator["Commit"]: + # Git applies skip before reversing. Hold back just the commits that + # might need to be skipped, including self if it is last. + tail: Deque[Commit] = deque() + for commit in commits: + tail.append(commit) + if len(tail) > skip + 1: + yield tail.popleft() + if len(tail) == skip + 1 and tail[-1] != self: + yield tail[0] + + return skip_from_end() + + if reverse: + del kwargs["reverse"] + if max_count > 0: + # Allow room for self even though filters may leave it out. + kwargs["max_count"] = skip + max_count + 1 + commits = self.iter_items(self.repo, self, paths, **kwargs) + + def skip_from_start() -> Iterator["Commit"]: + first = next(commits, None) + if first is None: + return + start = skip + (first == self) + stop = start + max_count if max_count > 0 else None + parents = islice(chain((first,), commits), start, stop) + if reverse: + yield from reversed(list(parents)) + else: + yield from parents + if stop is not None: + # Finish the walk, consuming the one extra result if self was filtered out. + for _ in commits: + pass + + return skip_from_start() @property def stats(self) -> Stats: diff --git a/test/test_commit.py b/test/test_commit.py index d0495217e..c2eacd89c 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -11,7 +11,7 @@ import os.path as osp import sys import time -from unittest.mock import Mock +from unittest.mock import Mock, patch from gitdb import IStream @@ -493,6 +493,29 @@ def test_iter_parents_when_self_is_filtered_out(self): self.assertEqual(list(a2.iter_parents(paths="a")), [a1]) self.assertEqual(list(tip.iter_parents(paths="b")), []) + def test_iter_parents_uses_one_walk(self): + with fixture_repo("commit.sh", "iter-parents") as repo: + tip, a1, a2 = repo.commit("B"), repo.commit("A1"), repo.commit("A2") + with patch.object(Commit, "iter_items", wraps=Commit.iter_items) as iter_items: + self.assertEqual(list(tip.iter_parents(paths="a", max_count=1)), [a2]) + self.assertEqual(iter_items.call_count, 1) + + self.assertEqual(list(tip.iter_parents(paths="a", max_count=0)), []) + self.assertEqual(list(tip.iter_parents(paths="a", n=1)), [a2]) + self.assertEqual(list(a2.iter_parents(paths="a", max_count=1)), [a1]) + self.assertEqual(list(a2.iter_parents(paths="a", skip=None)), [a1]) + + def test_iter_parents_reverses_after_applying_limits(self): + with fixture_repo("commit.sh", "iter-parents") as repo: + tip, a1, a2 = repo.commit("B"), repo.commit("A1"), repo.commit("A2") + self.assertEqual(list(tip.iter_parents(paths="a", reverse=True, max_count=1)), [a2]) + self.assertEqual(list(tip.iter_parents(paths="a", reverse=True, skip=2, max_count=1)), [a1]) + self.assertEqual(list(tip.iter_parents(paths="a", reverse=True, skip=2)), [a1]) + self.assertEqual(list(tip.iter_parents(paths="a", reverse=True, skip=3)), []) + self.assertEqual(list(a2.iter_parents(paths="a", reverse=True, max_count=1)), [a1]) + self.assertEqual(list(a2.iter_parents(paths="a", reverse=True)), [a1]) + self.assertEqual(list(a2.iter_parents(paths="a", reverse=True, skip=2)), []) + def test_name_rev(self): name_rev = self.rorepo.head.commit.name_rev assert isinstance(name_rev, str)