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 185b08728..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,18 +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 - kwargs["skip"] = skip + 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 self.iter_items(self.repo, self, paths, **kwargs) + return skip_from_start() @property def stats(self) -> Stats: diff --git a/test/test_commit.py b/test/test_commit.py index 8c09828e5..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 @@ -476,6 +476,46 @@ 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", 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]) + 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_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) 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