From eefa7e425ed5272eb1c1f7cc2c5f229bd552df98 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 10 Aug 2026 14:38:41 +0200 Subject: [PATCH 01/92] Preserve Git config value semantics Decode Git-supported quoted value escapes directly instead of routing UTF-8 text through Python unicode_escape. This preserves newlines, quotes, backslashes, and non-ASCII text when an unrelated config update rewrites existing values. Quote values containing Git comment delimiters (# and ;) or leading/trailing whitespace so Git does not truncate or trim their data. Escape LF, tab, backspace, quote, and backslash, while rejecting carriage returns and NULs before opening the destination. Regression coverage round-trips these values through both GitPython and git config and verifies unsafe control characters cannot alter the original file. Behavior follows Git config.c parse_value() and write_pair(). Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- git/config.py | 29 +++++++++++---- test/test_config.py | 88 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 102 insertions(+), 15 deletions(-) diff --git a/git/config.py b/git/config.py index 6f26e58fc..e7f64f7b5 100644 --- a/git/config.py +++ b/git/config.py @@ -462,7 +462,8 @@ def string_decode(v: str) -> str: v = v[:-1] # END cut trailing escapes to prevent decode error - return v.encode(defenc).decode("unicode_escape") + escapes = {"b": "\b", "n": "\n", "r": "\r", "t": "\t", '"': '"', "\\": "\\"} + return re.sub(r"\\(.)", lambda match: escapes.get(match.group(1), match.group(0)), v) # END string_decode @@ -517,10 +518,12 @@ def string_decode(v: str) -> str: # Opens quoting and does not close: appears to start multi-line quoting. is_multi_line = True optval = string_decode(optval[1:]) - elif optval.find("\\", 1, -1) == -1 and optval.find('"', 1, -1) == -1: - # Opens and closes quoting. Single line, and all we need is quote removal. - optval = optval[1:-1] - # TODO: Handle other quoted content, especially well-formed backslash escapes. + elif re.search(r'(?:^|[^\\])(?:\\\\)*"', optval[1:-1]): + # Preserve malformed values containing unescaped quotes. + pass + else: + # Opens and closes quoting. + optval = string_decode(optval[1:-1]) # Preserves multiple values for duplicate optnames. cursect.add(optname, optval) @@ -706,7 +709,7 @@ def write_section(name: str, section_dict: _OMD) -> None: for v in values: value = self._value_to_string(v) - if any(char in value for char in '\n\t\b\\"'): + if any(char in value for char in '\n\t\b\\"#;') or value[:1].isspace() or value[-1:].isspace(): value = value.replace("\\", "\\\\").replace('"', '\\"') value = '"%s\\\n"' % value.replace("\n", "\\n").replace("\t", "\\t").replace("\b", "\\b") fp.write(("\t%s = %s\n" % (key, value)).encode(defenc)) @@ -768,6 +771,20 @@ def write(self) -> None: return # END stop if we have include files + sections: List[_OMD] = [self._defaults] + section: _OMD + stored_section: _OMD + values: List[Any] + raw_value: Any + for _, stored_section in self._sections.items(): + sections.append(stored_section) + for section in sections: + for key, values in section.items_all(): + if key != "__name__": + for raw_value in values: + if "\r" in self._value_to_string(raw_value) or "\x00" in self._value_to_string(raw_value): + raise ValueError("Git config values must not contain CR or NUL") + fp = self._file_or_files # We have a physical file on disk, so get a lock. diff --git a/test/test_config.py b/test/test_config.py index d664fdb6f..28bb12043 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -14,6 +14,7 @@ import pytest from git import GitConfigParser +from git.compat import defenc from git.config import _OMD, cp from git.util import cwd, rmfile from test.lib import SkipTest, TestCase, fixture_path, with_rw_directory @@ -170,7 +171,16 @@ def test_rewriting_multiline_value_does_not_create_option(self, rw_dir): @with_rw_directory def test_writer_escapes_special_characters_without_newline(self, rw_dir): config_path = osp.join(rw_dir, "config") - values = {"tab": "\tvalue\t", "backspace": "a\bb", "quote": 'a"b', "backslash": "a\\qb"} + values = { + "tab": "\tvalue\t", + "backspace": "a\bb", + "quote": 'a"b', + "backslash": "a\\qb", + "hash": "value#fragment", + "semicolon": "value;fragment", + "leading": " value", + "trailing": "value ", + } with GitConfigParser(config_path, read_only=False) as git_config: for key, value in values.items(): @@ -185,11 +195,72 @@ def test_writer_escapes_special_characters_without_newline(self, rw_dir): stdout=subprocess.PIPE, check=True, ).stdout, - value.encode() + b"\n", + value.encode(defenc) + b"\n", ) with open(config_path, "rb") as config_file: self.assertNotIn(b"\x08", config_file.read()) + @with_rw_directory + def test_writer_preserves_escaped_and_non_ascii_values_safely(self, rw_dir): + config_path = osp.join(rw_dir, "config") + with open(config_path, "wb") as config_file: + config_file.write( + ( + '[section]\nnewline = "first\\nsecond"\nquote = "a\\"b"\nbackslash = "a\\\\b"\n' + 'unicode = "café\\\\path"\n' + ).encode(defenc) + ) + + with GitConfigParser(config_path, read_only=False) as config: + config.set_value("unrelated", "key", "value") + + expected = { + "newline": "first\nsecond", + "quote": 'a"b', + "backslash": "a\\b", + "unicode": "café\\path", + } + with GitConfigParser(config_path, read_only=True) as config: + for key, value in expected.items(): + self.assertEqual( + config.get_value("section", key), + value, + "GitPython should preserve values when rewriting unrelated entries", + ) + self.assertEqual( + subprocess.run( + ["git", "config", "--file", config_path, "--get", "section.%s" % key], + stdout=subprocess.PIPE, + check=True, + ).stdout, + value.encode(defenc) + b"\n", + "git should read rewritten values with the same semantics", + ) + + with open(config_path, "rb") as config_file: + contents = config_file.read() + self.assertNotIn(b"\r", contents, "the writer should never emit carriage returns") + self.assertNotIn(b"\x00", contents, "the writer should never emit NUL bytes") + + for name, value in (("return", b"first\\rsecond"), ("nul", b"first\x00second")): + unsafe_path = osp.join(rw_dir, "%s-config" % name) + unsafe_contents = b'[section]\nvalue = "' + value + b'"\n' + with open(unsafe_path, "wb") as config_file: + config_file.write(unsafe_contents) + with self.assertRaisesRegex( + ValueError, + "CR or NUL", + msg="unsafe existing values should abort rewrites", + ): + with GitConfigParser(unsafe_path, read_only=False) as config: + config.set_value("unrelated", "key", "value") + with open(unsafe_path, "rb") as config_file: + self.assertEqual( + config_file.read(), + unsafe_contents, + "rejected rewrites should leave the original file unchanged", + ) + @with_rw_directory def test_set_value_rejects_config_injection(self, rw_dir): config_path = osp.join(rw_dir, "config") @@ -745,15 +816,14 @@ def test_config_with_quotes_with_whitespace_outside_value(self): self.assertEqual(cr.get("init", "defaultBranch"), "trunk") def test_config_with_quotes_containing_escapes(self): - """For now just suppress quote removal. But it would be good to interpret most of these.""" + """Interpret Git's quoted escapes without changing malformed values.""" cr = GitConfigParser(fixture_path("git_config_with_quotes_escapes"), read_only=True) - # These can eventually be supported by substituting the represented character. - self.assertEqual(cr.get("custom", "hasnewline"), R'"first\nsecond"') - self.assertEqual(cr.get("custom", "hasbackslash"), R'"foo\\bar"') - self.assertEqual(cr.get("custom", "hasquote"), R'"ab\"cd"') - self.assertEqual(cr.get("custom", "hastrailingbackslash"), R'"word\\"') - self.assertEqual(cr.get("custom", "hasunrecognized"), R'"p\qrs"') + self.assertEqual(cr.get("custom", "hasnewline"), "first\nsecond") + self.assertEqual(cr.get("custom", "hasbackslash"), R"foo\bar") + self.assertEqual(cr.get("custom", "hasquote"), 'ab"cd') + self.assertEqual(cr.get("custom", "hastrailingbackslash"), "word\\") + self.assertEqual(cr.get("custom", "hasunrecognized"), R"p\qrs") # It is less obvious whether and what to eventually do with this. self.assertEqual(cr.get("custom", "hasunescapedquotes"), '"ab"cd"e"') From 9a92677171dfcca5e1a9bcecfd07aaceabeddee4 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 11 Aug 2026 14:15:10 +0200 Subject: [PATCH 02/92] fix: decode quoted diff paths in one pass GHSA-v6xg-m7rh-r365 (closed) reports that quoted patch paths can crash or silently change when an escaped literal backslash precedes digits. Add regression coverage distinguishing literal backslashes from real octal byte escapes, then decode Git's C-style quoting sequentially so one escape cannot be reinterpreted by a later pass. Match Git baseline cf5497b14c5a24f10c13f7e0ee85cb95af13ea6a quote.c::unquote_c_style by accepting octal bytes only when all three digits are valid and the first is 0 through 3. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- git/diff.py | 39 +++++++++++++++++++++++++++++---------- test/test_diff.py | 6 ++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/git/diff.py b/git/diff.py index d1963b84f..f89f3126f 100644 --- a/git/diff.py +++ b/git/diff.py @@ -95,14 +95,35 @@ class DiffConstants(enum.Enum): :const:`git.INDEX` and :const:`Diffable.INDEX`, as well as :const:`Diffable.Index`. """ -_octal_byte_re = re.compile(rb"\\([0-9]{3})") - -def _octal_repl(matchobj: Match) -> bytes: - value = matchobj.group(1) - value = int(value, 8) - value = bytes(bytearray((value,))) - return value +def _unquote_path(path: bytes) -> bytes: + result = bytearray() + escapes = { + ord("a"): 7, + ord("b"): 8, + ord("f"): 12, + ord("n"): 10, + ord("r"): 13, + ord("t"): 9, + ord("v"): 11, + } + i = 0 + while i < len(path): + if path[i] != ord("\\") or i + 1 == len(path): + result.append(path[i]) + i += 1 + continue + if path[i + 1] in b"0123" and i + 3 < len(path) and all(c in b"01234567" for c in path[i + 2 : i + 4]): + result.append(int(path[i + 1 : i + 4], 8)) + i += 4 + continue + escaped = path[i + 1] + if escaped in escapes or escaped in b'\\"': + result.append(escapes.get(escaped, escaped)) + else: + result.extend(path[i : i + 2]) + i += 2 + return bytes(result) def decode_path(path: bytes, has_ab_prefix: bool = True) -> Optional[bytes]: @@ -110,9 +131,7 @@ def decode_path(path: bytes, has_ab_prefix: bool = True) -> Optional[bytes]: return None if path.startswith(b'"') and path.endswith(b'"'): - path = path[1:-1].replace(b"\\n", b"\n").replace(b"\\t", b"\t").replace(b'\\"', b'"').replace(b"\\\\", b"\\") - - path = _octal_byte_re.sub(_octal_repl, path) + path = _unquote_path(path[1:-1]) if has_ab_prefix: assert path.startswith(b"a/") or path.startswith(b"b/") diff --git a/test/test_diff.py b/test/test_diff.py index d5e14f3de..92f3876c7 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -14,6 +14,7 @@ from git import NULL_TREE, Diff, DiffIndex, Diffable, GitCommandError, Repo, Submodule from git.cmd import Git +from git.diff import decode_path from git.exc import UnsafeOptionError from test.lib import StringProcessAdapter, TestBase, fixture, with_rw_directory @@ -324,6 +325,11 @@ def test_diff_patch_format(self): Diff._index_from_patch_format(self.rorepo, diff_proc) # END for each fixture + def test_decode_path_distinguishes_escaped_backslashes_from_octal_bytes(self): + self.assertEqual(decode_path(b'"foo\\\\899bar"', False), b"foo\\899bar") + self.assertEqual(decode_path(b'"foo\\\\123bar"', False), b"foo\\123bar") + self.assertEqual(decode_path(b'"foo\\123bar"', False), b"fooSbar") + def test_diff_with_spaces(self): data = StringProcessAdapter(fixture("diff_file_with_spaces")) diff_index = Diff._index_from_patch_format(self.rorepo, data) From 751473a5f3221d6f989291cbebcc404353fd3ba8 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 11 Aug 2026 14:18:25 +0200 Subject: [PATCH 03/92] fix: parse actor identities without regular expressions GHSA-g5vv-9gxw-82hx reports quadratic backtracking when an actor identity contains a long unterminated email delimiter. Add a regression that exercises a 20,000-character malformed identity, then replace both actor regexes with direct delimiter scans following Git's first-opening, first-closing delimiter behavior. Keep GitPython's whole-string fallback when either delimiter is absent. Reference Git baseline cf5497b14c5a24f10c13f7e0ee85cb95 ident.c::split_ident_line and its invalid-committer cases in t/t9300-fast-import.sh. Also reference gix-actor's signature decoder and lenient identity tests. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- doc/source/changes.rst | 13 +++++++++++++ git/util.py | 24 ++++++++---------------- test/test_actor.py | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 1a1b8fa12..bd6c471ff 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,19 @@ Changelog ========= +3.1.60 +====== + +Security fixes for + +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-g5vv-9gxw-82hx + +If you can, also try and provide feedback on the upcoming v4 branch +https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. + +See the following for all changes. +https://github.com/gitpython-developers/GitPython/releases/tag/3.1.60 + 3.1.59 ====== diff --git a/git/util.py b/git/util.py index 02f57c132..b0593feea 100644 --- a/git/util.py +++ b/git/util.py @@ -858,10 +858,6 @@ class Actor: committers and authors or anything with a name and an email as mentioned in the git log entries.""" - # PRECOMPILED REGEX - name_only_regex = re.compile(r"<(.*)>") - name_email_regex = re.compile(r"(.*) <(.*?)>") - # ENVIRONMENT VARIABLES # These are read when creating new commits. env_author_name = "GIT_AUTHOR_NAME" @@ -906,18 +902,14 @@ def _from_string(cls, string: str) -> "Actor": :return: :class:`Actor` """ - m = cls.name_email_regex.search(string) - if m: - name, email = m.groups() - return Actor(name, email) - else: - m = cls.name_only_regex.search(string) - if m: - return Actor(m.group(1), None) - # Assume the best and use the whole string as name. - return Actor(string, None) - # END special case name - # END handle name/email matching + line = string.partition("\n")[0] + left_bracket = line.find("<") + right_bracket = line.find(">", left_bracket + 1) + if left_bracket >= 0 and right_bracket >= 0: + return Actor(line[:left_bracket].rstrip(), line[left_bracket + 1 : right_bracket]) + + # Assume the best and use the whole string as name. + return Actor(string, None) @classmethod def _main_actor( diff --git a/test/test_actor.py b/test/test_actor.py index 5e6635709..baf6545f1 100644 --- a/test/test_actor.py +++ b/test/test_actor.py @@ -27,6 +27,26 @@ def test_from_string_should_handle_just_name(self): self.assertEqual("Michael Trier", a.name) self.assertEqual(None, a.email) + def test_from_string_handles_unterminated_email_without_regex_backtracking(self): + value = "A" * 20_000 + " \n y "), Actor("x", "a")) + + def test_from_string_uses_git_delimiters(self): + for value, expected in ( + ("Name ", Actor("Name", "e>", Actor("Name", "email")), + ("Name", Actor("Name", "email")), + (" <>", Actor("", "")), + ("Name ", Actor("Name email>", None)), + ): + self.assertEqual(Actor._from_string(value), expected) + def test_should_display_representation(self): a = Actor._from_string("Michael Trier ") self.assertEqual('">', repr(a)) From 09f2cf383a992d1af697364569e9f384748c1bd8 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 17 Aug 2026 06:15:12 +0200 Subject: [PATCH 04/92] fix: require opt-in for no-index diffs Treat --no-index as an unsafe diff option because it changes path operands from repository pathspecs to arbitrary filesystem paths. This addresses GHSA-whh4-5q6c-9v3x without exposing advisory reproduction details. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- doc/source/changes.rst | 1 + git/diff.py | 4 ++-- git/repo/base.py | 2 ++ test/test_diff.py | 11 +++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index bd6c471ff..34e727bb2 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -8,6 +8,7 @@ Changelog Security fixes for * https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-g5vv-9gxw-82hx +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-whh4-5q6c-9v3x If you can, also try and provide feedback on the upcoming v4 branch https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. diff --git a/git/diff.py b/git/diff.py index f89f3126f..192c099d0 100644 --- a/git/diff.py +++ b/git/diff.py @@ -239,8 +239,8 @@ def diff( to be read and diffed. :param allow_unsafe_options: - If ``True``, allow options such as ``--output`` and ``-O`` that can write to - or read from arbitrary filesystem paths. + If ``True``, allow options such as ``--output``, ``--no-index``, and ``-O`` + that can write to or read from arbitrary filesystem paths. :param kwargs: Additional arguments passed to :manpage:`git-diff(1)`, such as ``R=True`` to diff --git a/git/repo/base.py b/git/repo/base.py index d0ec00ec9..890461959 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -209,6 +209,8 @@ class Repo: ] unsafe_git_diff_options = unsafe_git_revision_options + [ + # Treats path operands as arbitrary filesystem paths. + "--no-index", # Reads caller-controlled order patterns from an arbitrary file. "-O", "--orderfile", diff --git a/test/test_diff.py b/test/test_diff.py index 92f3876c7..9cfbffd17 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -412,6 +412,17 @@ def test_diff_rejects_unsafe_output_options(self): commit.diff(output=allowed_target, allow_unsafe_options=True) self.assertTrue(osp.isfile(allowed_target)) + def test_diff_rejects_no_index(self): + calls = ( + lambda: self.rorepo.head.commit.diff(no_index=True), + lambda: self.rorepo.head.commit.diff(other="--no-index"), + lambda: self.rorepo.index.diff(None, no_index=True), + lambda: self.rorepo.index.diff("--no-index"), + ) + for call in calls: + with self.assertRaises(UnsafeOptionError): + call() + def test_diff_interface(self): """Test a few variations of the main diff routine.""" assertion_map = {} From c7cf4d13b1ed0a2e70f2a1f3c6b4fc6c2652cf0b Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 25 Aug 2026 04:57:29 +0200 Subject: [PATCH 05/92] fix: prefer `.git` during repository discovery GitPython considered worktree administration and bare-repository signatures before a worktree's real .git entry. Align discovery with Git so .git files and directories win, malformed .git files stop discovery, and candidate git directories validate HEAD plus commondir-backed object and ref storage. This addresses GHSA-239g-whfq-7xj9. Regression coverage compares ambiguous layouts with git rev-parse and rejects invalid HEAD/.git metadata. Git baseline: 15c6308cf7ad276b306aa5b3ababfbdebfb1a917; setup.c setup_git_directory_gently_1(), is_git_directory(), and validate_headref(). Repository validation can use GIT_COMMON_DIR for refs and objects. Preserve the same value on Repo so later config, ref, and object access uses the directory that made discovery succeed. Capture GIT_COMMON_DIR before the first repository config read so bare-state detection uses the same metadata location as discovery. Resolve relative environment values immediately so later working-directory changes cannot retarget the Repo. Review feedback: relative GIT_COMMON_DIR left Git subprocesses resolving GIT_DIR and GIT_COMMON_DIR from a different working directory; malformed commondir data, empty GIT_OBJECT_DIRECTORY, and the linked-worktree signature were also handled inconsistently. Pin the repository environment to resolved paths, reject invalid metadata without consulting the process working directory, and restore HEAD-based linked-worktree detection. Keep the loose HEAD and dangling .git behavior because both match Git setup.c at baseline 15c6308cf7ad276b306aa5b3ababfbdebfb1a917. Review feedback identified that chained or self-referential .git pointers recurse, filesystem-encoded metadata can fail text decoding, and a relative GIT_DIR is not retained for later Git commands. Parse one regular, size-bounded Gitfile exactly once, decode Gitfile and commondir paths with the filesystem codec, and retain the resolved GIT_DIR for subprocesses. This rejects cycles like Git instead of recursing and keeps commands stable after working-directory changes. Review feedback noted that a dangling commondir symlink was treated as absent, allowing local objects and refs to validate the repository. Distinguish a truly missing commondir from a dangling symlink. This follows Git's get_common_dir_noenv(), whose file_exists check uses lstat before attempting to read the entry. Python 3.9 on Windows raised UnicodeDecodeError while the repository-discovery regression test parsed invalid commondir bytes, causing the Python package test (windows, 3.9) check to fail. Treat UnicodeError like an unreadable metadata file in both commondir and gitfile parsing. Invalid bytes now make discovery reject the candidate repository, matching Git's behavior. Review feedback noted that GIT_OBJECT_DIRECTORY made discovery succeed without becoming the Repo ODB root, while a relative value could later be resolved from the Git wrapper's different working directory. Resolve the environment value against the construction directory, use it as the ODB root, and preserve the absolute value for later Git commands. The regression moves the only object store outside the git directory and verifies access through both GitDB and git cat-file after the original environment and current directory are restored. Review feedback identified four setup mismatches: non-missing .git stat failures could fall through to another repository, explicit GIT_DIR could be redirected through a nested .git entry, Gitfile reads were not bounded to the stat-reported size, and alternates ignored GIT_OBJECT_DIRECTORY. Match Git setup.c by bypassing discovery for an environment-selected GIT_DIR, stopping discovery when Gitfile stat fails for reasons other than a missing path, and reading exactly the previously observed Gitfile size. Resolve alternates below the active ODB root so custom object stores remain internally consistent. The commit review noted that treating every present GIT_DIR as explicit broke two documented cases: an empty GIT_DIR must fall back to current-directory discovery, while an empty Repo path must still use a nonempty GIT_DIR. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- doc/source/changes.rst | 1 + git/repo/base.py | 111 +++++++++++++---------- git/repo/fun.py | 110 +++++++++++++++-------- test/test_repo.py | 200 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 333 insertions(+), 89 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 34e727bb2..dcdc6d0c5 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -9,6 +9,7 @@ Security fixes for * https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-g5vv-9gxw-82hx * https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-whh4-5q6c-9v3x +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-239g-whfq-7xj9 If you can, also try and provide feedback on the upcoming v4 branch https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. diff --git a/git/repo/base.py b/git/repo/base.py index 890461959..4ae48e17b 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -45,7 +45,6 @@ from .fun import ( find_submodule_git_dir, - find_worktree_git_dir, is_git_dir, rev_parse, touch, @@ -234,6 +233,10 @@ def __init__( ) -> None: R"""Create a new :class:`Repo` instance. + .. note:: + Repositories using reftable may be opened, but GitPython's direct reference + access does not support reftable. + :param path: The path to either the worktree directory or the .git directory itself:: @@ -268,7 +271,11 @@ def __init__( :class:`Repo` """ - epath = path or os.getenv("GIT_DIR") + git_dir_env = os.getenv("GIT_DIR") + object_dir_env = os.getenv("GIT_OBJECT_DIRECTORY") + if object_dir_env is not None: + object_dir_env = osp.abspath(object_dir_env) + epath = path or git_dir_env if not epath: epath = os.getcwd() epath = os.fspath(epath) @@ -290,37 +297,48 @@ def __init__( raise NoSuchPathError(epath) # Walk up the path to find the `.git` dir. - curpath = epath - git_dir = None + curpath = os.fspath(epath) if epath is not None else "" + git_dir: Optional[str] = None + explicit_git_dir = not path and bool(git_dir_env) while curpath: # ABOUT osp.NORMPATH # It's important to normalize the paths, as submodules will otherwise # initialize their repo instances with paths that depend on path-portions # that will not exist after being removed. It's just cleaner. - if ( - osp.isfile(osp.join(curpath, "gitdir")) - and osp.isfile(osp.join(curpath, "commondir")) - and osp.isfile(osp.join(curpath, "HEAD")) - ): - git_dir = curpath - - if "GIT_WORK_TREE" in os.environ: - self._working_tree_dir = os.getenv("GIT_WORK_TREE") - else: - # Linked worktree administrative directories store the path to the - # worktree's .git file in their gitdir file (without "gitdir: " prefix). - with open(osp.join(git_dir, "gitdir")) as fp: - worktree_gitfile = fp.read().strip() + if not explicit_git_dir: + dotgit = osp.join(curpath, ".git") + try: + sm_gitpath = find_submodule_git_dir(dotgit) + except OSError: + break + if sm_gitpath is not None: + # Worktrees can use relative paths as of Git 2.48, so join to curpath. + git_dir = osp.normpath(osp.join(curpath, os.fspath(sm_gitpath))) + self._working_tree_dir = curpath + break + + # Like Git, do not fall back to a bare repository or parent directory when + # a non-directory .git entry exists but is not a valid gitfile. + if osp.exists(dotgit) and not osp.isdir(dotgit): + break - if not osp.isabs(worktree_gitfile): - worktree_gitfile = osp.normpath(osp.join(git_dir, worktree_gitfile)) + if is_git_dir(curpath): + git_dir = curpath + if osp.isfile(osp.join(curpath, "gitdir")) and osp.isfile(osp.join(curpath, "commondir")): + if "GIT_WORK_TREE" in os.environ: + self._working_tree_dir = os.getenv("GIT_WORK_TREE") + else: + # Linked worktree administrative directories store the path to + # the worktree's .git file in gitdir (without a "gitdir: " prefix). + with open(osp.join(git_dir, "gitdir")) as fp: + worktree_gitfile = fp.read().strip() - self._working_tree_dir = osp.dirname(worktree_gitfile) + if not osp.isabs(worktree_gitfile): + worktree_gitfile = osp.normpath(osp.join(git_dir, worktree_gitfile)) - break + self._working_tree_dir = osp.dirname(worktree_gitfile) + break - if is_git_dir(curpath): - git_dir = curpath # from man git-config : core.worktree # Set the path to the root of the working tree. If GIT_COMMON_DIR # environment variable is set, core.worktree is ignored and not used for @@ -340,22 +358,7 @@ def __init__( self._working_tree_dir = os.getenv("GIT_WORK_TREE") break - dotgit = osp.join(curpath, ".git") - sm_gitpath = find_submodule_git_dir(dotgit) - if sm_gitpath is not None: - git_dir = osp.normpath(sm_gitpath) - - sm_gitpath = find_submodule_git_dir(dotgit) - if sm_gitpath is None: - sm_gitpath = find_worktree_git_dir(dotgit) - - if sm_gitpath is not None: - # worktrees can use relative paths as of Git 2.48, so we join to curpath - git_dir = osp.normpath(osp.join(curpath, sm_gitpath)) - self._working_tree_dir = curpath - break - - if not search_parent_directories: + if explicit_git_dir or not search_parent_directories: break curpath, tail = osp.split(curpath) if not tail: @@ -366,6 +369,16 @@ def __init__( raise InvalidGitRepositoryError(epath) self.git_dir = git_dir + common_dir_env = os.getenv("GIT_COMMON_DIR") + if common_dir_env is not None: + self._common_dir = osp.abspath(common_dir_env) + else: + try: + common_dir = os.fsdecode((Path(self.git_dir) / "commondir").read_bytes()).rstrip("\r\n") + self._common_dir = osp.join(self.git_dir, common_dir) + except OSError: + self._common_dir = "" + self._bare = False try: self._bare = self.config_reader("repository").getboolean("core", "bare") @@ -373,12 +386,6 @@ def __init__( # Let's not assume the option exists, although it should. pass - try: - common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip() - self._common_dir = osp.join(self.git_dir, common_dir) - except OSError: - self._common_dir = "" - # Adjust the working directory in case we are actually bare - we didn't know # that in the first place. if self._bare: @@ -387,9 +394,15 @@ def __init__( self.working_dir: PathLike = self._working_tree_dir or self.common_dir self.git = self.GitCommandWrapperType(self.working_dir) + if common_dir_env is not None: + self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir)) + elif git_dir_env is not None: + self.git.update_environment(GIT_DIR=os.fspath(self.git_dir)) + if object_dir_env is not None: + self.git.update_environment(GIT_OBJECT_DIRECTORY=object_dir_env) # Special handling, in special times. - rootpath = osp.join(self.common_dir, "objects") + rootpath = object_dir_env if object_dir_env is not None else osp.join(self.common_dir, "objects") if issubclass(odbt, GitCmdObjectDB): self.odb = odbt(rootpath, self.git) else: @@ -990,7 +1003,7 @@ def _get_alternates(self) -> List[str]: :return: List of strings being pathnames of alternates """ - alternates_path = osp.join(self.common_dir, "objects", "info", "alternates") + alternates_path = osp.join(self.odb.root_path(), "info", "alternates") if osp.exists(alternates_path): with open(alternates_path, "rb") as f: @@ -1011,7 +1024,7 @@ def _set_alternates(self, alts: List[str]) -> None: The method does not check for the existence of the paths in `alts`, as the caller is responsible. """ - alternates_path = osp.join(self.common_dir, "objects", "info", "alternates") + alternates_path = osp.join(self.odb.root_path(), "info", "alternates") if not alts: if osp.isfile(alternates_path): os.remove(alternates_path) diff --git a/git/repo/fun.py b/git/repo/fun.py index 66e7eba69..eb0d8075a 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -60,22 +60,61 @@ def touch(filename: str) -> str: def is_git_dir(d: PathLike) -> bool: """This is taken from the git setup.c:is_git_directory function. + .. note:: + This function recognizes repositories using reftable through their + compatibility files, but GitPython's direct reference access does not support + reftable. + :raise git.exc.WorkTreeRepositoryUnsupported: If it sees a worktree directory. It's quite hacky to do that here, but at least clearly indicates that we don't support it. There is the unlikely danger to throw if we see directories which just look like a worktree dir, but are none. """ if osp.isdir(d): - if (osp.isdir(osp.join(d, "objects")) or "GIT_OBJECT_DIRECTORY" in os.environ) and osp.isdir( - osp.join(d, "refs") - ): - headref = osp.join(d, "HEAD") - return osp.isfile(headref) or (osp.islink(headref) and os.readlink(headref).startswith("refs")) - elif ( - osp.isfile(osp.join(d, "gitdir")) - and osp.isfile(osp.join(d, "commondir")) - and osp.isfile(osp.join(d, "gitfile")) - ): + headref = osp.join(d, "HEAD") + if osp.islink(headref): + try: + valid_head = os.readlink(headref).startswith("refs/") + except OSError: + valid_head = False + else: + try: + with open(headref, "rb") as fp: + head = fp.read(256) + except OSError: + valid_head = False + else: + valid_head = (head.startswith(b"ref:") and head[4:].lstrip().startswith(b"refs/")) or bool( + re.match(rb"(?:[0-9A-Fa-f]{64}|[0-9A-Fa-f]{40})", head) + ) + + common_dir = os.getenv("GIT_COMMON_DIR") + if common_dir == "": + return False + if common_dir is None: + common_dir_file = Path(d) / "commondir" + try: + common_dir = os.fsdecode(common_dir_file.read_bytes()).rstrip("\r\n") + except FileNotFoundError: + if osp.lexists(common_dir_file): + return False + common_dir = os.fspath(d) + except (OSError, UnicodeError): + return False + else: + if not common_dir: + return False + try: + common_dir = osp.realpath(osp.join(d, common_dir)) + except (OSError, ValueError): + return False + + object_dir = os.getenv("GIT_OBJECT_DIRECTORY") + if object_dir is None: + object_dir = osp.join(common_dir, "objects") + if valid_head and osp.isdir(object_dir) and osp.isdir(osp.join(common_dir, "refs")): + return True + if osp.isfile(osp.join(d, "gitdir")) and osp.isfile(osp.join(d, "commondir")) and osp.isfile(headref): raise WorkTreeRepositoryUnsupported(d) return False @@ -84,19 +123,20 @@ def find_worktree_git_dir(dotgit: PathLike) -> Optional[str]: """Search for a gitdir for this worktree.""" try: statbuf = os.stat(dotgit) - except OSError: + except (FileNotFoundError, NotADirectoryError): return None - if not stat.S_ISREG(statbuf.st_mode): + if not stat.S_ISREG(statbuf.st_mode) or statbuf.st_size > (1 << 20): return None try: - lines = Path(dotgit).read_text().splitlines() - for key, value in [line.strip().split(": ") for line in lines]: - if key == "gitdir": - return value - except ValueError: - pass - return None + with open(dotgit, "rb") as fp: + content_bytes = fp.read(statbuf.st_size) + if len(content_bytes) != statbuf.st_size: + return None + content = os.fsdecode(content_bytes).rstrip("\r\n") + except (OSError, UnicodeError): + return None + return content[8:] if len(content) >= 9 and content.startswith("gitdir: ") else None def find_submodule_git_dir(d: PathLike) -> Optional[PathLike]: @@ -104,26 +144,18 @@ def find_submodule_git_dir(d: PathLike) -> Optional[PathLike]: if is_git_dir(d): return d - try: - with open(d) as fp: - content = fp.read().rstrip() - except IOError: - # It's probably not a file. - pass - else: - if content.startswith("gitdir: "): - path = content[8:] - - if Git.is_cygwin(): - # Cygwin creates submodules prefixed with `/cygdrive/...`. - # Cygwin git understands Cygwin paths much better than Windows ones. - # Also the Cygwin tests are assuming Cygwin paths. - path = cygpath(path) - if not osp.isabs(path): - path = osp.normpath(osp.join(osp.dirname(d), path)) - return find_submodule_git_dir(path) - # END handle exception - return None + path = find_worktree_git_dir(d) + if path is None: + return None + + if Git.is_cygwin(): + # Cygwin creates submodules prefixed with `/cygdrive/...`. + # Cygwin git understands Cygwin paths much better than Windows ones. + # Also the Cygwin tests are assuming Cygwin paths. + path = cygpath(path) + if not osp.isabs(path): + path = osp.normpath(osp.join(osp.dirname(d), path)) + return path if is_git_dir(path) else None def short_to_long(odb: "GitCmdObjectDB", hexsha: str) -> Optional[bytes]: diff --git a/test/test_repo.py b/test/test_repo.py index 1dfec951a..12e572f52 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -40,7 +40,8 @@ from git.exc import UnsafeOptionError from git.exc import UnsafeProtocolError from git.exc import BadObject -from git.repo.fun import touch +from git.exc import WorkTreeRepositoryUnsupported +from git.repo.fun import find_worktree_git_dir, touch from git.util import bin_to_hex, cwd, cygpath, join_path_native, rmfile, rmtree from test.lib import TestBase, fixture, requires_symlinks, with_rw_directory, with_rw_repo, PathLikeMock @@ -122,6 +123,178 @@ def test_new_should_raise_on_non_existent_path(self): nonexistent = osp.join(tdir, "foobar") self.assertRaises(NoSuchPathError, Repo, nonexistent) + def test_repo_discovery_prefers_dotgit(self): + layouts = { + "linked-worktree": { + "gitdir": ".git\n", + "commondir": ".git\n", + "HEAD": "ref: refs/heads/main\n", + }, + "bare": {"objects": None, "refs": None, "HEAD": "ref: refs/heads/main\n"}, + } + + with tempfile.TemporaryDirectory() as tdir: + for name, entries in layouts.items(): + path = Path(tdir) / name + Repo.init(path).close() + for entry, contents in entries.items(): + item = path / entry + if contents is None: + item.mkdir() + else: + item.write_text(contents) + + with self.subTest(layout=name): + expected_git_dir = Git(path).rev_parse("--absolute-git-dir") + assert osp.samefile(Repo(path).git_dir, expected_git_dir) + + def test_repo_discovery_honors_explicit_git_dir(self): + with tempfile.TemporaryDirectory() as tdir: + git_dir = Path(tdir) / "repo.git" + Repo.init(git_dir, bare=True).close() + Repo.init(git_dir / ".git", bare=True).close() + + with mock.patch.dict(os.environ, {"GIT_DIR": os.fspath(git_dir)}): + for path in (None, ""): + with Repo(path) as repo, self.subTest(path=path): + assert osp.samefile(repo.git_dir, git_dir) + + worktree = Path(tdir) / "worktree" + Repo.init(worktree).close() + with cwd(worktree), mock.patch.dict(os.environ, {"GIT_DIR": ""}): + with Repo() as repo: + assert osp.samefile(repo.git_dir, worktree / ".git") + + def test_repo_discovery_rejects_invalid_metadata(self): + with tempfile.TemporaryDirectory() as tdir: + path = Path(tdir) + (path / "objects").mkdir() + (path / "refs").mkdir() + (path / "HEAD").write_text("not a ref") + + with self.subTest(metadata="HEAD"): + self.assertRaises(InvalidGitRepositoryError, Repo, path) + + (path / "HEAD").write_text("ref: refs/heads/main\n") + + for contents in (b"", b"\xff"): + (path / "commondir").write_bytes(contents) + with cwd(path), self.subTest(metadata="commondir", contents=contents): + self.assertRaises(InvalidGitRepositoryError, Repo, path) + + (path / "gitdir").write_text("../worktree/.git\n") + (path / "commondir").write_text("missing\n") + with self.subTest(metadata="linked-worktree"): + self.assertRaises(WorkTreeRepositoryUnsupported, Repo, path) + + (path / "gitdir").unlink() + (path / "commondir").unlink() + for variable in ("GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY"): + with mock.patch.dict(os.environ, {variable: ""}), self.subTest(metadata=variable): + self.assertRaises(InvalidGitRepositoryError, Repo, path) + + for contents in (b"not a gitfile", b"gitdir: \n", b"gitdir: .git\n", b"\xff"): + (path / ".git").write_bytes(contents) + with self.subTest(metadata=".git", contents=contents): + self.assertRaises(InvalidGitRepositoryError, Repo, path) + + @requires_symlinks + def test_repo_discovery_rejects_dangling_commondir(self): + with tempfile.TemporaryDirectory() as tdir: + path = Path(tdir) + (path / "objects").mkdir() + (path / "refs").mkdir() + (path / "HEAD").write_text("ref: refs/heads/main\n") + (path / "commondir").symlink_to("missing") + + self.assertRaises(InvalidGitRepositoryError, Repo, path) + + @requires_symlinks + def test_repo_discovery_rejects_dotgit_stat_errors(self): + with tempfile.TemporaryDirectory() as tdir: + path = Path(tdir) + Repo.init(path).close() + child = path / "child" + child.mkdir() + (child / ".git").symlink_to(".git") + + self.assertRaises(InvalidGitRepositoryError, Repo, child, search_parent_directories=True) + + def test_gitfile_read_is_bounded(self): + with tempfile.TemporaryDirectory() as tdir: + dotgit = Path(tdir) / ".git" + content = b"gitdir: target\n" + dotgit.write_bytes(content) + reader = mock.mock_open(read_data=b"") + + with mock.patch("builtins.open", reader): + assert find_worktree_git_dir(dotgit) is None + + reader().read.assert_called_once_with(len(content)) + + def test_repo_discovery_uses_storage_environment(self): + with tempfile.TemporaryDirectory() as tdir: + git_dir = Path(tdir) / "git" + common_dir = Path(tdir) / "common" + git_dir.mkdir() + common_dir.mkdir() + (git_dir / "HEAD").write_text("ref: refs/heads/main\n") + (common_dir / "objects").mkdir() + (common_dir / "refs").mkdir() + (common_dir / "config").write_text("[core]\n\tbare = true\n") + + with cwd(tdir): + with mock.patch.dict(os.environ, {"GIT_DIR": "git", "GIT_COMMON_DIR": "common"}): + repo = Repo() + + assert osp.samefile(repo.common_dir, common_dir) + assert osp.samefile(repo.odb.root_path(), common_dir / "objects") + assert repo.bare + assert osp.samefile(repo.git.rev_parse("--absolute-git-dir"), git_dir) + assert osp.samefile(repo.git.rev_parse("--git-common-dir"), common_dir) + + (git_dir / "commondir").write_text("../common\n") + environment = dict(os.environ) + environment["GIT_DIR"] = "git" + environment.pop("GIT_COMMON_DIR", None) + with cwd(tdir), mock.patch.dict(os.environ, environment, clear=True): + repo = Repo() + + assert osp.samefile(repo.git.rev_parse("--absolute-git-dir"), git_dir) + + if sys.platform.startswith("linux"): + byte_common_dir = Path(tdir) / os.fsdecode(b"common-\xff") + byte_common_dir.mkdir() + (byte_common_dir / "objects").mkdir() + (byte_common_dir / "refs").mkdir() + (git_dir / "commondir").write_bytes(b"../common-\xff\n") + + assert osp.samefile(Repo(git_dir).common_dir, byte_common_dir) + + @with_rw_directory + def test_repo_discovery_preserves_object_directory(self, tdir): + git_dir = Path(tdir) / "git" + payload = b"custom object database" + payload_file = Path(tdir) / "payload" + payload_file.write_bytes(payload) + + source_repo = Repo.init(git_dir, bare=True) + blob_hexsha = source_repo.git.hash_object("-w", payload_file) + source_repo.close() + object_dir = Path(tdir) / "objects" + (git_dir / "objects").rename(object_dir) + + with cwd(tdir), mock.patch.dict(os.environ, {"GIT_DIR": "git", "GIT_OBJECT_DIRECTORY": "objects"}): + repo = Repo(odbt=GitDB) + + with repo: + assert osp.samefile(repo.odb.root_path(), object_dir) + assert repo.odb.has_object(bytes.fromhex(blob_hexsha)) + assert repo.git.cat_file("blob", blob_hexsha) == payload.decode() + repo.alternates = ["other/location"] + assert repo.alternates == ["other/location"] + assert (object_dir / "info" / "alternates").is_file() + @with_rw_repo("0.3.2.1") def test_repo_creation_from_different_paths(self, rw_repo): r_from_gitdir = Repo(rw_repo.git_dir) @@ -389,6 +562,7 @@ def test_alternates_use_common_dir(self, rw_dir): os.makedirs(osp.join(common_dir, "objects", "info")) os.makedirs(osp.join(git_dir, "objects", "info")) repo = mock.Mock(common_dir=common_dir, git_dir=git_dir) + repo.odb.root_path.return_value = osp.join(common_dir, "objects") alts = ["other/location", "this/location"] Repo._set_alternates(repo, alts) @@ -1147,6 +1321,30 @@ def test_empty_repo_reftable_active_branch(self, rw_dir): lambda: repo.active_branch, ) + @with_rw_directory + def test_reftable_repo_opens_but_direct_refs_are_unsupported(self, rw_dir): + git = Git(rw_dir) + try: + git.init(ref_format="reftable") + except GitCommandError as err: + if err.status == 129: + pytest.skip("git init --ref-format is not supported by this git version") + raise + + git.update_environment( + GIT_AUTHOR_NAME="Test Author", + GIT_AUTHOR_EMAIL="author@example.com", + GIT_COMMITTER_NAME="Test Committer", + GIT_COMMITTER_EMAIL="committer@example.com", + ) + git.commit(allow_empty=True, message="initial commit") + expected_head = git.rev_parse("HEAD") + + repo = Repo(rw_dir) + assert repo.git.rev_parse("HEAD") == expected_head + assert repo.head.reference.name == ".invalid" + assert not repo.heads + @with_rw_directory def test_active_branch_raises_type_error_when_head_is_detached(self, rw_dir): repo = Repo.init(rw_dir) From 56636c3614aff1555fe716d3804ca59e653934ea Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 25 Aug 2026 20:30:55 +0200 Subject: [PATCH 06/92] prepare new release --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index e5c812e68..17f8e2fbb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.59 +3.1.60 From db1a2cfe2d9b6dd88fe34195badaffd89e24610b Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Thu, 27 Aug 2026 15:35:34 +0200 Subject: [PATCH 07/92] fix: wait for git daemon to listen before connecting Properly wait instead of hardcoding a short sleep. Fixes #1676 --- test/lib/helper.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/lib/helper.py b/test/lib/helper.py index 58923eaef..4135fe5dd 100644 --- a/test/lib/helper.py +++ b/test/lib/helper.py @@ -32,6 +32,7 @@ import os import os.path as osp from stat import S_ISLNK, ST_MODE +import socket import subprocess import sys import tempfile @@ -218,8 +219,15 @@ def git_daemon_launched(base_path, ip, port): base_path=base_path, as_process=True, ) - # Yes, I know... fortunately, this is always going to work if sleep time is just large enough. - time.sleep(1.0 if sys.platform == "win32" else 0.5) + + # Wait until git daemon listens for connections. + for _attempt in range(1, 30): + try: + socket.create_connection((ip, port), timeout=30).close() + break + except ConnectionRefusedError: + time.sleep(0.5) + except Exception as ex: msg = textwrap.dedent( """ From bc63b027e2fc737e2ec0222bfa764e1aa76a427f Mon Sep 17 00:00:00 2001 From: Byron Date: Fri, 28 Aug 2026 08:37:50 +0200 Subject: [PATCH 08/92] fix: restore Actor.name_email_regex (#2220) GitPython 3.1.60 removed the Actor.name_email_regex class attribute while replacing internal actor parsing, which broke downstream consumers such as python-semantic-release. Bring back the historical compiled pattern for downstream compatibility, but emit DeprecationWarning whenever the class member is accessed. The warning explains its quadratic behavior on long malformed strings and points callers to the public Actor(name, email) constructor when fields are separate, or direct string parsing when they are not. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 # 252a84ac Add public Actor.from_string constructor (#2220) Add public Actor.from_string constructor (#2220) Promote the existing actor identity parser to Actor.from_string while retaining _from_string as a compatibility alias. Point the name_email_regex deprecation warning to the new public constructor or Actor(name, email). Validation: - actor-related test/test_actor.py and test/test_util.py cases: 12 passed - ruff check and format checks passed - basedpyright git/util.py: no errors Co-authored-by: Codex GPT-5 # dd2181d8 Address review feedback about Actor subclasses Address review feedback about Actor subclasses Review feedback: Actor.from_string() should preserve the class on which the public classmethod is called instead of always returning a base Actor. Construct through cls on both parsing paths and cover subclass construction directly. Validation: - actor-related test/test_actor.py and test/test_util.py cases: 13 passed - ruff check and format checks passed - basedpyright git/util.py: no errors # fcf1ecb1 Address review feedback about constructor typing Address review feedback about constructor typing Review feedback: Actor.from_string() constructs subclasses at runtime but its Actor return annotation prevents type checkers from preserving subclass-specific members. Use the project existing Python 3.7-compatible bound TypeVar pattern so the declared return type follows cls. Validation: - actor-related test/test_actor.py and test/test_util.py cases: 13 passed - ruff check and format checks passed - basedpyright git/util.py: no errors Co-authored-by: Codex GPT-5 --- git/util.py | 26 +++++++++++++++++++++++--- test/test_actor.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/git/util.py b/git/util.py index b0593feea..a80e667c7 100644 --- a/git/util.py +++ b/git/util.py @@ -80,6 +80,7 @@ Sequence, Tuple, TYPE_CHECKING, + Type, TypeVar, Union, cast, @@ -108,6 +109,7 @@ T_IterableObj = TypeVar("T_IterableObj", bound=Union["IterableObj", "Has_id_attribute"], covariant=True) # So IterableList[Head] is subtype of IterableList[IterableObj]. +T_Actor = TypeVar("T_Actor", bound="Actor") _logger = logging.getLogger(__name__) @@ -853,11 +855,27 @@ def update(self, *args: Any, **kwargs: Any) -> None: self._callable(*args, **kwargs) +class _DeprecatedActorNameEmailRegex: + _pattern = re.compile(r"(.*) <(.*?)>") + + def __get__(self, _instance: Any, _owner: Any) -> Pattern[str]: + warnings.warn( + "Actor.name_email_regex is deprecated and will be removed in GitPython 4.0.0 because searching long " + "malformed strings with it can take quadratic time. Use Actor.from_string() to parse actor identities, " + "or Actor(name, email) when the fields are already separate.", + DeprecationWarning, + stacklevel=2, + ) + return self._pattern + + class Actor: """Actors hold information about a person acting on the repository. They can be committers and authors or anything with a name and an email as mentioned in the git log entries.""" + name_email_regex = _DeprecatedActorNameEmailRegex() + # ENVIRONMENT VARIABLES # These are read when creating new commits. env_author_name = "GIT_AUTHOR_NAME" @@ -891,7 +909,7 @@ def __repr__(self) -> str: return '">' % (self.name, self.email) @classmethod - def _from_string(cls, string: str) -> "Actor": + def from_string(cls: Type[T_Actor], string: str) -> T_Actor: """Create an :class:`Actor` from a string. :param string: @@ -906,10 +924,12 @@ def _from_string(cls, string: str) -> "Actor": left_bracket = line.find("<") right_bracket = line.find(">", left_bracket + 1) if left_bracket >= 0 and right_bracket >= 0: - return Actor(line[:left_bracket].rstrip(), line[left_bracket + 1 : right_bracket]) + return cls(line[:left_bracket].rstrip(), line[left_bracket + 1 : right_bracket]) # Assume the best and use the whole string as name. - return Actor(string, None) + return cls(string, None) + + _from_string = from_string @classmethod def _main_actor( diff --git a/test/test_actor.py b/test/test_actor.py index baf6545f1..68afb80d3 100644 --- a/test/test_actor.py +++ b/test/test_actor.py @@ -3,14 +3,15 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ -from git import Actor +from unittest import mock +from git import Actor from test.lib import TestBase class TestActor(TestBase): def test_from_string_should_separate_name_and_email(self): - a = Actor._from_string("Michael Trier ") + a = Actor.from_string("Michael Trier ") self.assertEqual("Michael Trier", a.name) self.assertEqual("mtrier@example.com", a.email) @@ -23,18 +24,34 @@ def test_from_string_should_separate_name_and_email(self): assert len(m) == 1 def test_from_string_should_handle_just_name(self): - a = Actor._from_string("Michael Trier") + a = Actor.from_string("Michael Trier") self.assertEqual("Michael Trier", a.name) self.assertEqual(None, a.email) + def test_from_string_constructs_subclass(self): + class DerivedActor(Actor): + pass + + self.assertIsInstance(DerivedActor.from_string("name "), DerivedActor) + def test_from_string_handles_unterminated_email_without_regex_backtracking(self): value = "A" * 20_000 + " ") + + message = str(context.warning) + self.assertIn("Actor.from_string()", message) + self.assertIn("Actor(name, email)", message) + assert match is not None + self.assertEqual(match.groups(), ("Michael Trier", "mtrier@example.com")) + def test_from_string_does_not_parse_across_lines(self): - self.assertEqual(Actor._from_string("x \n y "), Actor("x", "a")) + self.assertEqual(Actor.from_string("x \n y "), Actor("x", "a")) def test_from_string_uses_git_delimiters(self): for value, expected in ( @@ -45,12 +62,12 @@ def test_from_string_uses_git_delimiters(self): ("Name ", Actor("Name email>", None)), ): - self.assertEqual(Actor._from_string(value), expected) + self.assertEqual(Actor.from_string(value), expected) def test_should_display_representation(self): - a = Actor._from_string("Michael Trier ") + a = Actor.from_string("Michael Trier ") self.assertEqual('">', repr(a)) def test_str_should_alias_name(self): - a = Actor._from_string("Michael Trier ") + a = Actor.from_string("Michael Trier ") self.assertEqual(a.name, str(a)) From 5346d1b587a790bac4dc4f100a28fae9db0fd7c8 Mon Sep 17 00:00:00 2001 From: Byron Date: Fri, 28 Aug 2026 13:00:28 +0200 Subject: [PATCH 09/92] prepare next release --- VERSION | 2 +- doc/source/changes.rst | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 17f8e2fbb..c29b32b56 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.60 +3.1.61 diff --git a/doc/source/changes.rst b/doc/source/changes.rst index dcdc6d0c5..6b06dd5bf 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,18 @@ Changelog ========= +3.1.61 +====== + +A fixup release to avoid accidental removal of public class regex on Actor. +It's now deprecated instead. + +If you can, also try and provide feedback on the upcoming v4 branch +https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. + +See the following for all changes. +https://github.com/gitpython-developers/GitPython/releases/tag/3.1.61 + 3.1.60 ====== From 180b1ffab5fdcb52a674fb6461ca42d9788b3a07 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 1 Sep 2026 09:33:11 +0200 Subject: [PATCH 10/92] fix: keep bare-repository worktrees non-bare (#2223) Opening a linked worktree created from a bare repository read `core.bare` from the common repository and discarded the worktree path. Keep a discovered linked worktree non-bare when its administrative directory has a `commondir` marker. The regression compares Git rev-parse behavior and verifies Repo.bare and working_tree_dir. Git baseline: 0bd5a6920d7c4238e0d90ddc0e7e08866e84a0f1; environment.c:is_bare_repository() and setup.c:check_repository_format_gently(). Git 2.50.1 reports the linked checkout as non-bare. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- git/repo/base.py | 4 ++++ test/test_repo.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/git/repo/base.py b/git/repo/base.py index 4ae48e17b..7039d0320 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -386,6 +386,10 @@ def __init__( # Let's not assume the option exists, although it should. pass + # A linked worktree is not bare even when its main repository is. + if self._bare and self._working_tree_dir and osp.isfile(osp.join(self.git_dir, "commondir")): + self._bare = False + # Adjust the working directory in case we are actually bare - we didn't know # that in the first place. if self._bare: diff --git a/test/test_repo.py b/test/test_repo.py index 12e572f52..ad1fa68ab 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1459,6 +1459,23 @@ def test_git_work_tree_dotgit(self, rw_dir, use_relative_paths=False): self.assertIsInstance(repo.heads["aaaaaaaa"], Head) + @with_rw_directory + def test_git_work_tree_from_bare_repo(self, rw_dir): + if Git().version_info[:3] < (2, 5, 1): + pytest.skip("worktree feature unsupported, needs git 2.5.1 or later") + + bare_repo = self.rorepo.clone(join_path_native(rw_dir, "bare_repo"), bare=True) + worktree_path = join_path_native(rw_dir, "worktree_repo") + if Git.is_cygwin(): + worktree_path = cygpath(worktree_path) + bare_repo.git.worktree("add", "--detach", worktree_path) + + repo = Repo(worktree_path) + + assert Git(worktree_path).rev_parse("--is-bare-repository") == "false" + assert not repo.bare + assert osp.samefile(repo.working_tree_dir, worktree_path) + def test_git_work_tree_dotgit_relative(self): """Check that we find .git as a worktree file containing a relative path and find the worktree based on it.""" From 0e8c23c56c816b7fae8d2417606e9d6f7b37cc87 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 1 Sep 2026 10:14:10 +0200 Subject: [PATCH 11/92] ci: restore Cygwin virtual environments on Python 3.9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cygwin patches ensurepip to load setuptools and pip wheels from `/usr/share/python-wheels`. The rolling python-pip-wheel package advanced to pip 26.2.1, which requires Python 3.10 or newer, so every Python 3.9 venv creation failed—including the installation test's nested virtual environment. Keep the single Cygwin lane on Python 3.9 because the regular matrix already covers Python 3.12 and newer. Install Cygwin's setuptools wheel, download pip 26.0.1 from its immutable PyPI URL into ensurepip's shared wheel directory, verify its SHA-256 digest, and avoid upgrading pip. This fixes all venv creation without relying on a version that may disappear from rolling Cygwin mirrors. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- .github/workflows/cygwin-test.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cygwin-test.yml b/.github/workflows/cygwin-test.yml index b8150e93d..e14856e7e 100644 --- a/.github/workflows/cygwin-test.yml +++ b/.github/workflows/cygwin-test.yml @@ -45,7 +45,7 @@ jobs: - name: Install Cygwin uses: cygwin/cygwin-install-action@v6 with: - packages: git python39 python-pip-wheel python-setuptools-wheel python-wheel-wheel + packages: curl git python39 python-setuptools-wheel add-to-path: false # No need to change $PATH outside the Cygwin environment. - name: Arrange for verbose output @@ -73,12 +73,15 @@ jobs: - name: Set up virtual environment run: | + pip_wheel=/usr/share/python-wheels/pip-26.0.1-py3-none-any.whl + curl -fsSLo "$pip_wheel" https://files.pythonhosted.org/packages/de/f0/c81e05b613866b76d2d1066490adf1a3dbc4ee9d9c839961c3fc8a6997af/pip-26.0.1-py3-none-any.whl + python3.9 -c 'import hashlib, pathlib, sys; assert hashlib.sha256(pathlib.Path(sys.argv[1]).read_bytes()).hexdigest() == sys.argv[2]' "$pip_wheel" bdb1b08f4274833d62c1aa29e20907365a2ceb950410df15fc9521bad440122b python3.9 -m venv .venv echo 'BASH_ENV=.venv/bin/activate' >>"$GITHUB_ENV" - name: Update PyPA packages run: | - python -m pip install -U pip 'setuptools; python_version<"3.12"' wheel + python -m pip install -U 'setuptools; python_version<"3.12"' wheel - name: Install project and test dependencies run: | From 1ed0ebc2f2e74d979cdc367a4864a7731fdcc093 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 1 Sep 2026 11:33:19 +0200 Subject: [PATCH 12/92] Reject submodule checkout paths outside the repository GHSA-59cr-6r3x-644w identifies that submodule update paths could reach filesystem operations without the containment check already used by add and move. Add a regression that proves update rejects a parent-directory checkout path before cloning, and override Submodule.abspath to apply the shared _to_relative_path guard for every filesystem consumer. Git baseline: git.git read-cache.c verify_path_internal() rejects invalid index paths, covered for parent traversal by t/t9300-fast-import.sh. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- doc/source/changes.rst | 13 ++++++++++++ git/objects/submodule/base.py | 12 +++++++++++ test/test_submodule.py | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 6b06dd5bf..20bfaeae6 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,19 @@ Changelog ========= +3.1.62 +====== + +Security fixes for + +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-59cr-6r3x-644w + +If you can, also try and provide feedback on the upcoming v4 branch +https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. + +See the following for all changes. +https://github.com/gitpython-developers/GitPython/releases/tag/3.1.62 + 3.1.61 ====== diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 39e912321..563b20a18 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -414,6 +414,18 @@ def _to_relative_path(cls, parent_repo: "Repo", path: PathLike) -> PathLike: return path + @property + def abspath(self) -> PathLike: + root = self.repo.working_tree_dir + if root is None: + return super().abspath + path = root + for component in os.fspath(self._to_relative_path(self.repo, self.path)).split("/"): + path = join_path_native(path, component) + if osp.islink(path): + raise ValueError("Submodule checkout path %r contains a symbolic link" % self.path) + return path + @classmethod def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_abspath: PathLike) -> None: """Write a ``.git`` file containing a (preferably) relative path to the actual diff --git a/test/test_submodule.py b/test/test_submodule.py index d545cc8d5..ca9078aac 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -1369,6 +1369,46 @@ class Repo: osp.join(Repo.working_tree_dir + "-other", "module"), ) + @with_rw_directory + def test_update_rejects_checkout_path_outside_parent(self, rwdir): + parent = git.Repo.init(osp.join(rwdir, "parent")) + submodule = Submodule( + parent, + Submodule.NULL_BIN_SHA, + name="module", + path=osp.join("..", "outside"), + url="unused", + ) + + with mock.patch.object(Submodule, "_clone_repo", side_effect=AssertionError("clone attempted")): + with pytest.raises(ValueError, match="is not in repository"): + submodule.update(init=True) + + @with_rw_directory + def test_update_rejects_checkout_path_through_symlink(self, rwdir): + parent = git.Repo.init(osp.join(rwdir, "parent")) + os.mkdir(osp.join(parent.working_tree_dir, "target")) + os.symlink("target", osp.join(parent.working_tree_dir, "link")) + submodule = Submodule( + parent, + Submodule.NULL_BIN_SHA, + name="module", + path=osp.join("link", "module"), + url="unused", + ) + + with mock.patch.object(Submodule, "_clone_repo", side_effect=AssertionError("clone attempted")): + with pytest.raises(ValueError, match="contains a symbolic link"): + submodule.update(init=True) + + @with_rw_directory + def test_update_rejects_checkout_path_at_parent_root(self, rwdir): + parent = git.Repo.init(osp.join(rwdir, "parent")) + submodule = Submodule(parent, Submodule.NULL_BIN_SHA, name="module", path=".", url="unused") + + with pytest.raises(ValueError, match="must not be the repository root"): + submodule.update(init=True) + @skipUnless(sys.platform == "win32", "Specifically for Windows.") @with_rw_directory def test_to_relative_path_windows_path_kinds(self, rwdir): From 23d0e9269c84573129acd1f90dbedf639e889c8f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:39:08 +0000 Subject: [PATCH 13/92] build(deps): bump https://github.com/astral-sh/ruff-pre-commit Bumps the pre-commit group with 1 update: [https://github.com/astral-sh/ruff-pre-commit](https://github.com/astral-sh/ruff-pre-commit). Updates `https://github.com/astral-sh/ruff-pre-commit` from v0.16.0 to 0.16.5 - [Release notes](https://github.com/astral-sh/ruff-pre-commit/releases) - [Commits](https://github.com/astral-sh/ruff-pre-commit/compare/v0.16.0...v0.16.5) --- updated-dependencies: - dependency-name: https://github.com/astral-sh/ruff-pre-commit dependency-version: 0.16.5 dependency-type: direct:production dependency-group: pre-commit ... Signed-off-by: dependabot[bot] --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 75a29d6db..dfa5fc91e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: exclude: ^test/fixtures/ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.16.0 + rev: v0.16.5 hooks: - id: ruff-check args: ["--fix"] From 075a664f3da8564c44f2b74536914b9ce9fb4cc1 Mon Sep 17 00:00:00 2001 From: NK Date: Fri, 4 Sep 2026 02:36:33 +0530 Subject: [PATCH 14/92] fix: join backslash line continuations when reading config values git config joins an unquoted value that ends in a backslash with the next line: the backslash and the newline are removed and the next line is appended verbatim, so 'k = line1\' followed by ' line2' reads back as 'line1 line2'. GitPython only implemented multi-line values for quoted strings; for unquoted values the continuation line was silently dropped, truncating whatever was stored in the config. Read the continuation inside _read and join it, chaining across lines that themselves end in a backslash. An even number of trailing backslashes is an escaped one, so the value ends there; a single backslash right at end-of-file is dropped, matching git. Verified against git itself for every case covered by the new tests. --- git/config.py | 21 ++++++++++++++++++++- test/test_config.py | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/git/config.py b/git/config.py index e7f64f7b5..f30a220ae 100644 --- a/git/config.py +++ b/git/config.py @@ -513,7 +513,26 @@ def string_decode(v: str) -> str: if len(optval) < 2 or optval[0] != '"': # Does not open quoting. - pass + # A value ending in an odd number of backslashes + # continues on the next line, exactly as git does: the + # final backslash and the newline are removed and the + # next line is appended verbatim (leading whitespace + # included). An even number means the last backslash + # is escaped and the value ends there. + while True: + trailing = len(optval) - len(optval.rstrip("\\")) + if trailing % 2 == 0: + break + continuation = fp.readline() + if not continuation: + # Backslash at end of file: git drops it. + optval = optval[:-1] + break + lineno = lineno + 1 + joined = continuation.decode(defenc) + while joined.endswith("\n") or joined.endswith("\r"): + joined = joined[:-1] + optval = optval[:-1] + joined elif optval[-1] != '"': # Opens quoting and does not close: appears to start multi-line quoting. is_multi_line = True diff --git a/test/test_config.py b/test/test_config.py index 28bb12043..b8d5b9363 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -143,6 +143,26 @@ def test_multi_line_config(self): ) self.assertEqual(len(config.sections()), 23) + def test_backslash_line_continuation(self): + """An unquoted value ending in a backslash continues on the next line, + exactly as git config parses it: the final backslash and the newline + are removed and the next line is appended verbatim.""" + cases = [ + (b"[a]\n\tk = line1\\\n line2\n", "line1 line2"), + (b"[a]\n\tk = one\\\n two\\\n three\n", "one two three"), + (b"[a]\n\tk = val\\\\\n next\n", "val\\\\"), + (b"[a]\n\tk = end\\\n", "end"), + (b"[alias]\n\tco = checkout \\\n\t\t-v\n", "checkout \t\t-v"), + ] + for content, expected in cases: + config_file = io.BytesIO(content) + config_file.name = "backslash_continuation.config" + config = GitConfigParser(config_file) + config.read() + section = "alias" if b"[alias]" in content else "a" + key = "co" if section == "alias" else "k" + self.assertEqual(config.get_value(section, key), expected) + def test_config_value_with_trailing_new_line(self): config_content = b'[section-header]\nkey:"value\n"' config_file = io.BytesIO(config_content) From 60dd9467a73140e00b0d1b0857df5176660b3832 Mon Sep 17 00:00:00 2001 From: Byron Date: Fri, 4 Sep 2026 06:20:07 +0200 Subject: [PATCH 15/92] fix: ignore continuation markers in config comments Git treats # and ; outside quotes as the start of a comment. A trailing backslash in that ignored text therefore cannot continue the value. The continuation loop counted trailing slashes without lexical context, so it consumed the next option and a later writable flush silently dropped that setting. - Track quote and escape state while checking the accumulated value. - Stop continuation scanning at an unquoted comment. - Cover preservation of the following option across a writable flush. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- git/config.py | 16 ++++++++++++++-- test/test_config.py | 13 +++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/git/config.py b/git/config.py index f30a220ae..283ee297b 100644 --- a/git/config.py +++ b/git/config.py @@ -467,6 +467,19 @@ def string_decode(v: str) -> str: # END string_decode + def is_line_continuation(value: str) -> bool: + quoted = escaped = False + for char in value: + if escaped: + escaped = False + elif char == "\\": + escaped = True + elif char == '"': + quoted = not quoted + elif char in "#;" and not quoted: + return False + return escaped + while True: # We assume to read binary! line = fp.readline().decode(defenc) @@ -520,8 +533,7 @@ def string_decode(v: str) -> str: # included). An even number means the last backslash # is escaped and the value ends there. while True: - trailing = len(optval) - len(optval.rstrip("\\")) - if trailing % 2 == 0: + if not is_line_continuation(optval): break continuation = fp.readline() if not continuation: diff --git a/test/test_config.py b/test/test_config.py index b8d5b9363..df60da086 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -163,6 +163,19 @@ def test_backslash_line_continuation(self): key = "co" if section == "alias" else "k" self.assertEqual(config.get_value(section, key), expected) + @with_rw_directory + def test_comment_backslash_does_not_continue_value(self, rw_dir): + config_path = osp.join(rw_dir, "config") + with open(config_path, "wb") as config_file: + config_file.write(b"[a]\n\tk = one\\\n two ; ignored \\\n\tx = two\n") + + with GitConfigParser(config_path, read_only=False) as config: + self.assertEqual(config.get_value("a", "x"), "two") + config.set_value("a", "added", "three") + + with GitConfigParser(config_path) as config: + self.assertEqual(config.get_value("a", "x"), "two") + def test_config_value_with_trailing_new_line(self): config_content = b'[section-header]\nkey:"value\n"' config_file = io.BytesIO(config_content) From a15f7910d1d9a169b5a0c7adf14ce9bf4ede35d0 Mon Sep 17 00:00:00 2001 From: Byron Date: Fri, 4 Sep 2026 06:22:28 +0200 Subject: [PATCH 16/92] fix: parse joined config values as a whole Git removes each backslash-newline pair before parsing the resulting logical value. Appending physical lines after processing only the first line left trailing whitespace, comments, quotes, and recognized escapes literal. That divergence returned values unlike git config and could preserve comment text as configuration data. - Accumulate continuation text before parsing it. - Apply whitespace, comment, quote, and escape handling once to the complete value. - Cover each affected syntax form with Git-compatible expectations. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 --- git/config.py | 36 +++++++++++++++++++++++++++++++++--- test/test_config.py | 7 ++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/git/config.py b/git/config.py index 283ee297b..aef881d2e 100644 --- a/git/config.py +++ b/git/config.py @@ -480,6 +480,32 @@ def is_line_continuation(value: str) -> bool: return False return escaped + def parse_value(value: str) -> str: + parsed: List[str] = [] + whitespace: List[str] = [] + quoted = escaped = False + escapes = {"b": "\b", "n": "\n", "t": "\t", '"': '"', "\\": "\\"} + for char in value: + if escaped: + parsed.append(escapes.get(char, "\\" + char)) + escaped = False + continue + if char.isspace() and not quoted: + if parsed: + whitespace.append(char) + continue + if char in "#;" and not quoted: + break + parsed.extend(whitespace) + whitespace.clear() + if char == "\\": + escaped = True + elif char == '"': + quoted = not quoted + else: + parsed.append(char) + return "".join(parsed) + while True: # We assume to read binary! line = fp.readline().decode(defenc) @@ -529,9 +555,10 @@ def is_line_continuation(value: str) -> bool: # A value ending in an odd number of backslashes # continues on the next line, exactly as git does: the # final backslash and the newline are removed and the - # next line is appended verbatim (leading whitespace - # included). An even number means the last backslash - # is escaped and the value ends there. + # next line is appended before the complete value is + # parsed. An even number means the last backslash is + # escaped and the value ends there. + continued = False while True: if not is_line_continuation(optval): break @@ -545,6 +572,9 @@ def is_line_continuation(value: str) -> bool: while joined.endswith("\n") or joined.endswith("\r"): joined = joined[:-1] optval = optval[:-1] + joined + continued = True + if continued: + optval = parse_value(optval) elif optval[-1] != '"': # Opens quoting and does not close: appears to start multi-line quoting. is_multi_line = True diff --git a/test/test_config.py b/test/test_config.py index df60da086..3107d8074 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -146,10 +146,14 @@ def test_multi_line_config(self): def test_backslash_line_continuation(self): """An unquoted value ending in a backslash continues on the next line, exactly as git config parses it: the final backslash and the newline - are removed and the next line is appended verbatim.""" + are removed before the complete logical value is parsed.""" cases = [ (b"[a]\n\tk = line1\\\n line2\n", "line1 line2"), (b"[a]\n\tk = one\\\n two\\\n three\n", "one two three"), + (b"[a]\n\tk = one\\\n two \n", "one two"), + (b"[a]\n\tk = one\\\n two ; ignored\n", "one two"), + (b'[a]\n\tk = one\\\n "two"\n', "one two"), + (b"[a]\n\tk = one\\\n two\\tthree\n", "one two\tthree"), (b"[a]\n\tk = val\\\\\n next\n", "val\\\\"), (b"[a]\n\tk = end\\\n", "end"), (b"[alias]\n\tco = checkout \\\n\t\t-v\n", "checkout \t\t-v"), @@ -170,6 +174,7 @@ def test_comment_backslash_does_not_continue_value(self, rw_dir): config_file.write(b"[a]\n\tk = one\\\n two ; ignored \\\n\tx = two\n") with GitConfigParser(config_path, read_only=False) as config: + self.assertEqual(config.get_value("a", "k"), "one two") self.assertEqual(config.get_value("a", "x"), "two") config.set_value("a", "added", "three") From b7548979693b3f7c8d13b8da2b7c3ecef07327da Mon Sep 17 00:00:00 2001 From: Christian Vuerings Date: Sun, 6 Sep 2026 23:28:29 +0000 Subject: [PATCH 17/92] test: cover subdirectory discovery and pathspec commands in bare-repo worktrees (#2223) --- test/test_repo.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_repo.py b/test/test_repo.py index ad1fa68ab..b7b5718ff 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1476,6 +1476,17 @@ def test_git_work_tree_from_bare_repo(self, rw_dir): assert not repo.bare assert osp.samefile(repo.working_tree_dir, worktree_path) + # Discovering from a subdirectory (as tools such as mkdocs plugins do) and then + # running a command with an absolute pathspec must use the worktree, not the + # bare common dir, as the working directory; 3.1.61 failed here with + # "is outside repository at '/.'" (#2223). + subdir = osp.join(worktree_path, "git", "repo") + sub_repo = Repo(subdir, search_parent_directories=True) + assert not sub_repo.bare + assert osp.samefile(sub_repo.working_dir, worktree_path) + expected = repo.git.log("-n", "1", "--format=%H", "--", "git/repo") + assert sub_repo.git.log("-n", "1", "--format=%H", "--", subdir) == expected + def test_git_work_tree_dotgit_relative(self): """Check that we find .git as a worktree file containing a relative path and find the worktree based on it.""" From 10ec385a725fee70def3d9bb7b52208e75174068 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 7 Sep 2026 04:44:56 +0200 Subject: [PATCH 18/92] get better commit messages from agents --- AGENTS.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 AGENTS.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..ee4ef9d95 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,14 @@ +# Commit messages + +Every commit must have a descriptive title and a substantive body. Title-only +commit messages are not acceptable. + +The body must explain the problem or motivation, what changed, and why the +chosen approach addresses it. Include relevant behavior before and after the +change, design decisions, limitations, and validation results. Scale the detail +to the change; do not add boilerplate or claim checks that were not run. + +Commit messages must stand on their own. Put the information needed to understand +and review the change in the commit body, even when it also appears in a pull +request description. PR and issue links may provide additional context, but must +not substitute for that explanation. From db47516dac750c5968b8f6d388b18712c355df50 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 7 Sep 2026 04:54:54 +0200 Subject: [PATCH 19/92] prepare new release --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index c29b32b56..4eb2ee669 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.61 +3.1.62 From 5fd795a4981d02c1cd9050e41793b3dc64ff2438 Mon Sep 17 00:00:00 2001 From: Roshan Ramani Date: Mon, 7 Sep 2026 12:10:23 +0530 Subject: [PATCH 20/92] Read git's yes/no and on/off booleans in get_value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git accepts yes/no and on/off for a boolean as well as true/false (git_parse_maybe_bool_text in parse.c), and ConfigParser.getboolean on this class already accepted all of them. _string_to_value handled only true/false, under a comment claiming to "try boolean values as git uses them", so the two accessors disagreed about the same file: value get_value() getboolean() git config --type=bool yes 'yes' True true no 'no' False false on 'on' True true off 'off' False false Returning them as strings was worse than merely inexact. "no" and "off" are non-empty, so a caller testing the result of get_value got True for a value git reads as false — the inversion is silent, since nothing raises. _string_to_value now recognises the same spellings getboolean does. A value that is not a boolean is untouched, so "meld" is still returned as a string, and numeric values keep their existing behavior. Not changed here: get_value also diverges on numeric bases and suffixes ("0x10" and "1k" come back as strings, "010" as 10 where git reads octal 8). Those change the value rather than its type and are worth their own commit. Validation: test_get_value_reads_git_boolean_spellings covers the ten spellings and asserts the two accessors agree; it fails on the previous revision. test/test_config.py passes (40 passed, 2 skipped), ruff check and ruff format are clean. test/test_repo.py errors on this clone because init-tests-after-clone.sh has not been run, unchanged by this commit. --- git/config.py | 11 ++++++++--- test/test_config.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/git/config.py b/git/config.py index aef881d2e..bb7fda19f 100644 --- a/git/config.py +++ b/git/config.py @@ -956,11 +956,16 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: continue # END for each numeric type - # Try boolean values as git uses them. + # Try boolean values as git uses them. git accepts yes/no and on/off as + # well as true/false (git_parse_maybe_bool_text in parse.c), and so does + # ConfigParser.getboolean on this class, so only get_value lagged behind. + # Leaving them as strings was worse than merely inexact: "no" and "off" + # are non-empty, so a caller testing the result got True for a value git + # reads as false. vl = valuestr.lower() - if vl == "false": + if vl in ("false", "no", "off"): return False - if vl == "true": + if vl in ("true", "yes", "on"): return True if not isinstance(valuestr, str): diff --git a/test/test_config.py b/test/test_config.py index 3107d8074..3031721de 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -167,6 +167,38 @@ def test_backslash_line_continuation(self): key = "co" if section == "alias" else "k" self.assertEqual(config.get_value(section, key), expected) + def test_get_value_reads_git_boolean_spellings(self): + # git accepts yes/no and on/off as well as true/false, and getboolean on + # this class already did. get_value returned them as strings, so "no" and + # "off" arrived as non-empty (truthy) values for a caller testing them. + cases = [ + (b"true", True), + (b"TRUE", True), + (b"yes", True), + (b"Yes", True), + (b"on", True), + (b"On", True), + (b"false", False), + (b"no", False), + (b"off", False), + (b"Off", False), + ] + for raw, expected in cases: + config_file = io.BytesIO(b"[core]\n\tflag = " + raw + b"\n") + config_file.name = "boolean_spellings.config" + config = GitConfigParser(config_file) + config.read() + self.assertIs(config.get_value("core", "flag"), expected, raw.decode()) + # The two accessors must not disagree about the same value. + self.assertIs(config.getboolean("core", "flag"), expected, raw.decode()) + + # A value that is not a boolean at all still comes back untouched. + config_file = io.BytesIO(b"[core]\n\tflag = meld\n") + config_file.name = "boolean_spellings.config" + config = GitConfigParser(config_file) + config.read() + self.assertEqual(config.get_value("core", "flag"), "meld") + @with_rw_directory def test_comment_backslash_does_not_continue_value(self, rw_dir): config_path = osp.join(rw_dir, "config") From 297cd26412e63c1f94c3ef8bdcf27c54956328ab Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 7 Sep 2026 11:26:45 +0200 Subject: [PATCH 21/92] review --- git/config.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/git/config.py b/git/config.py index bb7fda19f..0da90bab2 100644 --- a/git/config.py +++ b/git/config.py @@ -10,36 +10,34 @@ import abc import configparser as cp import fnmatch -from functools import wraps import inspect -from io import BufferedReader, IOBase import logging import os import os.path as osp import re import sys - -from git.compat import defenc, force_text -from git.util import LockFile +from functools import wraps +from io import BufferedReader, IOBase # typing------------------------------------------------------- - from typing import ( + IO, + TYPE_CHECKING, Any, Callable, + Dict, Generic, - IO, List, - Dict, Sequence, - TYPE_CHECKING, Tuple, TypeVar, Union, cast, ) -from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, assert_never, _T +from git.compat import defenc, force_text +from git.types import _T, ConfigLevels_Tup, Lit_config_levels, PathLike, assert_never +from git.util import LockFile if TYPE_CHECKING: from io import BytesIO @@ -956,12 +954,7 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: continue # END for each numeric type - # Try boolean values as git uses them. git accepts yes/no and on/off as - # well as true/false (git_parse_maybe_bool_text in parse.c), and so does - # ConfigParser.getboolean on this class, so only get_value lagged behind. - # Leaving them as strings was worse than merely inexact: "no" and "off" - # are non-empty, so a caller testing the result got True for a value git - # reads as false. + # Try boolean values as git uses them. vl = valuestr.lower() if vl in ("false", "no", "off"): return False From 6bcb25b80071cfc59c72f2bbb03318a87f769a39 Mon Sep 17 00:00:00 2001 From: Byron Date: Wed, 9 Sep 2026 18:43:25 +0200 Subject: [PATCH 22/92] Clarify detached HEAD access and attach writable test fixtures (#2230) Reading head.reference or active_branch raises TypeError when HEAD points directly to a commit, but the public documentation did not clearly explain how to access that commit. Document head.commit.hexsha for attached and detached HEADs, explain the reference setter/getter asymmetry, and clarify that active_branch requires an attached HEAD. Preserve the exception type and existing message prefix while adding a hint to use .commit or .object. Writable test fixtures assumed cloning produced an attached HEAD, so their branch access could fail when the source checkout was detached. Have with_rw_repo create and attach master at the requested revision when its clone is detached, retaining the clone's branch and tracking configuration otherwise. Explicitly attach the temporary bare remote to its own master branch before cloning it for remote tests. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- doc/source/tutorial.rst | 8 +++++++- git/refs/symbolic.py | 37 +++++++++++++++++++++++++++++++------ git/repo/base.py | 6 +++++- test/lib/helper.py | 11 +++++++++-- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/doc/source/tutorial.rst b/doc/source/tutorial.rst index d095d3be3..1edfa4e11 100644 --- a/doc/source/tutorial.rst +++ b/doc/source/tutorial.rst @@ -78,6 +78,12 @@ Query relevant repository paths ... :class:`Heads ` Heads are branches in git-speak. :class:`References ` are pointers to a specific commit or to other references. Heads and :class:`Tags ` are a kind of references. GitPython allows you to query them rather intuitively. +To obtain the current commit ID, use ``repo.head.commit.hexsha``. This works both +on a branch and with a detached HEAD, provided HEAD resolves to an existing commit. +When ``repo.head.is_detached`` is true, HEAD points directly to a commit and there +is no active branch: reading ``repo.head.reference`` or ``repo.active_branch`` +raises :exc:`TypeError`. The branch examples below assume an attached HEAD. + .. literalinclude:: ../../test/test_docs.py :language: python :dedent: 8 @@ -152,7 +158,7 @@ Examining References :start-after: # [2-test_references_and_objects] :end-before: # ![2-test_references_and_objects] -A :class:`symbolic reference ` is a special case of a reference as it points to another reference instead of a commit. +A :class:`symbolic reference ` can point to another reference. When detached, it points directly to a commit instead. Reading its ``commit`` property resolves the commit in either state. Assigning a commit to ``reference`` detaches it; reading ``reference`` then raises :exc:`TypeError`. .. literalinclude:: ../../test/test_docs.py :language: python diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 824d0c46c..7c3eac508 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -59,12 +59,14 @@ def _git_dir(repo: "Repo", path: Union[PathLike, None]) -> PathLike: class SymbolicReference: - """Special case of a reference that is symbolic. + """A reference that can point to another reference or be detached. - This does not point to a specific commit, but to another - :class:`~git.refs.head.Head`, which itself specifies a commit. + An attached :class:`~git.refs.head.HEAD` usually points to a + :class:`~git.refs.head.Head`, which itself specifies a commit. A detached + :class:`~git.refs.head.HEAD` points directly to a commit instead. - A typical example for a symbolic reference is :class:`~git.refs.head.HEAD`. + Use :attr:`commit` to access the commit in either case, and :attr:`reference` + to access the target reference when attached. """ __slots__ = ("repo", "path") @@ -416,7 +418,15 @@ def set_object( @property def commit(self) -> "Commit": - """Query or set commits directly""" + """The commit this reference resolves to, whether detached or symbolic. + + For example, ``repo.head.commit.hexsha`` returns the current commit ID + both on a branch and with a detached HEAD. HEAD must resolve to an + existing commit; an unborn branch in an empty repository has none. + + Assigning updates the commit without changing whether this reference + is detached. + """ return self._get_commit() @commit.setter @@ -443,7 +453,10 @@ def _get_reference(self) -> "Reference": """ sha, target_ref_path = self._get_ref_info(self.repo, self.path) if target_ref_path is None: - raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha)) + raise TypeError( + "%s is a detached symbolic reference as it points to %r. " + "Use .commit or .object to access the target directly." % (self, sha) + ) return cast("Reference", self.from_path(self.repo, target_ref_path)) def set_reference( @@ -531,6 +544,18 @@ def set_reference( # Aliased reference @property def reference(self) -> "Reference": + """The reference we point to, available only when not detached. + + Check :attr:`is_detached` before reading this property if a target + reference is required. To access the target commit or object in either + state, use :attr:`commit` or :attr:`object` instead. + + Assigning a reference keeps this reference symbolic. Assigning a git + object or revision string detaches it; reading this property then raises. + + :raise TypeError: + If this reference is detached when reading the property. + """ return self._get_reference() @reference.setter diff --git a/git/repo/base.py b/git/repo/base.py index 7039d0320..f326266d6 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -1150,7 +1150,11 @@ def ignored(self, *paths: PathLike) -> List[str]: @property def active_branch(self) -> Head: - """The name of the currently active branch. + """The currently active branch. + + Check ``repo.head.is_detached`` before accessing this property if HEAD + may be detached. To access the current commit in either state, use + ``repo.head.commit`` instead. :raise TypeError: If HEAD is detached. diff --git a/test/lib/helper.py b/test/lib/helper.py index 4135fe5dd..b4399ca62 100644 --- a/test/lib/helper.py +++ b/test/lib/helper.py @@ -139,7 +139,8 @@ def wrapper(self, *args, **kwargs): def with_rw_repo(working_tree_ref, bare=False): """Same as with_bare_repo, but clones the rorepo as non-bare repository, checking - out the working tree at the given working_tree_ref. + out the working tree at the given working_tree_ref with an attached HEAD, + regardless of the source repository's HEAD state. This repository type is more costly due to the working copy checkout. @@ -158,7 +159,12 @@ def repo_creator(self): repo_dir = tempfile.mktemp(prefix="%sbare_%s" % (prefix, func.__name__)) rw_repo = self.rorepo.clone(repo_dir, shared=True, bare=bare, n=True) - rw_repo.head.commit = rw_repo.commit(working_tree_ref) + if rw_repo.head.is_detached: + rw_repo.head.reference = rw_repo.create_head( + "master", working_tree_ref, force=True, logmsg="Create test branch" + ) + else: + rw_repo.head.commit = rw_repo.commit(working_tree_ref) if not bare: rw_repo.head.reference.checkout() # END handle checkout @@ -294,6 +300,7 @@ def remote_repo_creator(self): rw_repo_dir = tempfile.mktemp(prefix="daemon_cloned_repo-%s-" % func.__name__) rw_daemon_repo = self.rorepo.clone(rw_daemon_repo_dir, shared=True, bare=True) + rw_daemon_repo.head.reference = rw_daemon_repo.create_head("master", force=True) # Recursive alternates info? rw_repo = rw_daemon_repo.clone(rw_repo_dir, shared=True, bare=False, n=True) try: From 24b6f95d4b90ab1483fb601c736ac6e7c059cd61 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 04:59:45 +0200 Subject: [PATCH 23/92] fix: Reject submodule move destinations through intermediate symlinks Also, sloppy review of the tests which are assumpted to not make things worse. Submodule.move() checked lexical containment but did not validate intermediate destination components before filesystem and repository updates. GHSA-gq48-pqfc-9p58 identifies the resulting checkout-path boundary violation. The new regression failed before the fix because move() returned successfully. Share the existing abspath component walk with move() and validate the normalized destination before any mutation, including configuration-only and module-only calls. Preserve the no-op early return and existing final-component symlink handling; abspath still rejects every symlink component. This addresses pre-existing links, not concurrent directory replacement races. The Git reference checkout at 1630431f326e15fcde608827b5ff38422528eb59 uses has_symlink_leading_path() in builtin/mv.c and tests rejection without index changes in t/t7001-mv.sh. The fix follows that intermediate-component rule while retaining GitPython leaf-link compatibility. Validation: the 30 new parameterized cases pass, covering relative and absolute destinations and link targets, internal and dangling links, all move flag combinations, unchanged repository state after rejection, ordinary and no-op moves, and leaf-link compatibility. The complete test/test_submodule.py suite passes: 75 passed, 3 skipped, 1 xfailed. Test-process commit.gpgsign=false avoids sandbox GPG failures. Ruff lint and format checks, mypy (45 source files), and git diff --check pass. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- doc/source/changes.rst | 13 ++++ git/objects/submodule/base.py | 20 ++++-- test/test_submodule.py | 125 ++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 5 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 20bfaeae6..4803790b9 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,19 @@ Changelog ========= +3.1.63 +====== + +Security fixes for + +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-gq48-pqfc-9p58 + +If you can, also try and provide feedback on the upcoming v4 branch +https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. + +See the following for all changes. +https://github.com/gitpython-developers/GitPython/releases/tag/3.1.63 + 3.1.62 ====== diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 563b20a18..5314ccf62 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -419,11 +419,20 @@ def abspath(self) -> PathLike: root = self.repo.working_tree_dir if root is None: return super().abspath - path = root - for component in os.fspath(self._to_relative_path(self.repo, self.path)).split("/"): + return self._checkout_abspath(self._to_relative_path(self.repo, self.path)) + + def _checkout_abspath(self, relative_path: PathLike, allow_final_symlink: bool = False) -> PathLike: + """Check a checkout path already normalized by :meth:`_to_relative_path`.""" + path = self.repo.working_tree_dir + if path is None: + raise NotADirectoryError("Submodules require a working tree") + components = os.fspath(relative_path).split("/") + for index, component in enumerate(components): path = join_path_native(path, component) + if allow_final_symlink and index == len(components) - 1: + break if osp.islink(path): - raise ValueError("Submodule checkout path %r contains a symbolic link" % self.path) + raise ValueError("Submodule checkout path %r contains a symbolic link" % relative_path) return path @classmethod @@ -1039,7 +1048,8 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = self :raise ValueError: - If the module path existed and was not empty, or was a file. + If the module path existed and was not empty, was a file, or had a + symbolic link in an intermediate component. :note: Currently the method is not atomic, and it could leave the repository in an @@ -1057,7 +1067,7 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = return self # END handle no change - module_checkout_abspath = join_path_native(str(self.repo.working_tree_dir), module_checkout_path) + module_checkout_abspath = self._checkout_abspath(module_checkout_path, allow_final_symlink=True) if osp.isfile(module_checkout_abspath): raise ValueError("Cannot move repository onto a file: %s" % module_checkout_abspath) # END handle target files diff --git a/test/test_submodule.py b/test/test_submodule.py index ca9078aac..0d0275f21 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -51,6 +51,131 @@ def _patch_git_config(name, value): yield +@pytest.fixture +def movable_submodule(tmp_path): + """Create a committed local submodule whose logical name stays fixed when moved.""" + with git.Repo.init(tmp_path / "source") as source, git.Repo.init(tmp_path / "parent") as parent: + (tmp_path / "source" / "file").write_text("content", encoding="utf-8") + source.index.add(["file"]) + source.index.commit("Create source") + with _patch_git_config("protocol.file.allow", "always"): + submodule = parent.create_submodule("logical-name", "module", source.working_tree_dir) + parent.index.commit("Create submodule") + # Release clone handles before Windows moves the checkout. + submodule.module().close() + yield submodule + + +def _move_snapshot(submodule): + """Capture index, configuration, and path state to detect side effects of rejected moves.""" + parent = submodule.repo + with submodule.module() as module: + config = Path(module.git_dir, "config").read_bytes() + return ( + Path(parent.index.path).read_bytes(), + Path(parent.working_tree_dir, ".gitmodules").read_bytes(), + Path(submodule.abspath, ".git").read_bytes(), + config, + submodule.path, + ) + + +@pytest.mark.parametrize("target_kind", ["relative", "absolute", "internal", "dangling"]) +@pytest.mark.parametrize("configuration,module", [(True, True), (False, True), (True, False)]) +@pytest.mark.parametrize("absolute_path", [False, True]) +def test_move_rejects_intermediate_symlink( + movable_submodule, tmp_path, target_kind, configuration, module, absolute_path +): + """Reject intermediate symlinks before changing repository state or their targets. + + Cover relative and absolute destinations in every move mode, including links + within the repository and dangling links, which must also be rejected. + """ + submodule = movable_submodule + parent = submodule.repo + root = Path(parent.working_tree_dir) + target = root / "target" if target_kind == "internal" else tmp_path / "outside" + if target_kind != "dangling": + target.mkdir() + (root / "nested").mkdir() + link = root / "nested" / "link" + link.symlink_to( + target if target_kind == "absolute" else os.path.relpath(target, link.parent), target_is_directory=True + ) + parent.index.add(["nested/link"]) + parent.index.commit("Record layout") + tree = parent.git.write_tree() + before = _move_snapshot(submodule) + destination = root / "nested/link/new/moved" if absolute_path else "nested/link/new/moved" + + with pytest.raises(ValueError, match="contains a symbolic link"): + submodule.move(destination, configuration=configuration, module=module) + + assert _move_snapshot(submodule) == before + assert parent.git.write_tree() == tree + assert Path(submodule.abspath, "file").read_text(encoding="utf-8") == "content" + if target_kind == "dangling": + assert not target.exists() + else: + assert list(target.iterdir()) == [] + + +@pytest.mark.parametrize("absolute_path", [False, True]) +def test_move_normal_destination(movable_submodule, absolute_path): + """Allow ordinary relative and absolute moves, and make a repeated move a no-op.""" + submodule = movable_submodule + root = Path(submodule.repo.working_tree_dir) + destination = root / "nested/moved" if absolute_path else "nested/moved" + assert submodule.move(destination) is submodule + assert Path(submodule.abspath, "file").read_text(encoding="utf-8") == "content" + assert not (root / "module").exists() + assert submodule.path == "nested/moved" + submodule.repo.git.write_tree() + before = _move_snapshot(submodule) + assert submodule.move(destination) is submodule + assert _move_snapshot(submodule) == before + + +@pytest.mark.parametrize("kind", ["empty", "nonempty", "file", "dangling"]) +def test_move_leaf_symlink_compatibility(movable_submodule, tmp_path, kind): + """Preserve leaf-symlink replacement without modifying the external target. + + Moving onto a link to an empty directory replaces the link; nonempty, file, + and dangling targets fail without changing repository state. Direct checkout + path access must still reject every leaf symlink. + """ + submodule = movable_submodule + root = Path(submodule.repo.working_tree_dir) + target = tmp_path / "outside" + if kind in ("empty", "nonempty"): + target.mkdir() + if kind == "nonempty": + (target / "keep").write_text("keep", encoding="utf-8") + if kind == "file": + target.write_text("keep", encoding="utf-8") + destination = root / "destination" + destination.symlink_to(target, target_is_directory=kind != "file") + with pytest.raises(ValueError, match="contains a symbolic link"): + Submodule(submodule.repo, Submodule.NULL_BIN_SHA, name="unused", path="destination").abspath + before = _move_snapshot(submodule) + if kind == "empty": + assert submodule.move("destination") is submodule + assert not destination.is_symlink() + assert (destination / "file").is_file() + assert list(target.iterdir()) == [] + else: + with pytest.raises(OSError if kind == "dangling" else ValueError): + submodule.move("destination") + assert _move_snapshot(submodule) == before + assert destination.is_symlink() + if kind == "nonempty": + assert (target / "keep").read_text(encoding="utf-8") == "keep" + elif kind == "file": + assert target.read_text(encoding="utf-8") == "keep" + else: + assert not target.exists() + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" From 43a43cd800200f75ae84957f37b0e8b27c52f403 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 06:52:24 +0200 Subject: [PATCH 24/92] fix: Validate submodule checkout and metadata paths before mutation A bit of a sloppy review, rubber-stamping the tests based on the assumption that they are validating it's conforming to Git, probably also while increasing coverage. Submodule.add() could clone through a checkout symlink after module_exists() swallowed the validation error. Metadata paths had a similar gap: locally planted symlinks under .git/modules could redirect cloning, reconnecting, renaming, updating, or removing a submodule. Some failures were detected only after changing configuration or moving or removing checkout directories. Reuse the checkout component check for metadata paths and validate checkout paths in the shared clone helper, including legacy embedded repositories. Check .gitfiles, submodule configuration files, and the actual repository path named by a gitfile, which can differ from .git/modules/. Reject symlinked .gitmodules files as well. Preflight move and rename sources and destinations before mutation, including the implicit metadata rename when a default-named submodule moves. Keep module_exists()'s boolean contract and the existing supported replacement of a leaf symlink during a move. Add 56 regression cases covering checkout and metadata links, dangling links, redirected gitfiles, legacy clone layouts, and rejected operations preserving external targets, configuration, the index, and an empty move destination. The initial 36 cases reproduced failures before the fix. These checks reject existing symlinks; they do not prevent concurrent filesystem replacement between validation and use. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/objects/submodule/base.py | 49 +++++++--- test/test_submodule.py | 165 ++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+), 14 deletions(-) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 5314ccf62..461b3068d 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -255,7 +255,7 @@ def _config_parser( # END handle parent_commit fp_module: Union[str, BytesIO] if not repo.bare and parent_matches_head and repo.working_tree_dir: - fp_module = osp.join(repo.working_tree_dir, cls.k_modules_file) + fp_module = cls._checked_abspath(repo.working_tree_dir, cls.k_modules_file) else: assert parent_commit is not None, "need valid parent_commit in bare repositories" try: @@ -322,7 +322,7 @@ def _module_abspath(cls, parent_repo: "Repo", path: PathLike, name: str) -> Path if cls._need_gitfile_submodules(parent_repo.git): return osp.join(parent_repo.git_dir, "modules", name) if parent_repo.working_tree_dir: - return osp.join(parent_repo.working_tree_dir, path) + return cls._checked_abspath(parent_repo.working_tree_dir, cls._to_relative_path(parent_repo, path)) raise NotADirectoryError() @classmethod @@ -361,8 +361,11 @@ def _clone_repo( :param kwargs: Additional arguments given to :manpage:`git-clone(1)`. """ + path = cls._to_relative_path(repo, path) + if repo.working_tree_dir is None: + raise NotADirectoryError("Submodules require a working tree") + module_checkout_path = cls._checked_abspath(repo.working_tree_dir, path) module_abspath = cls._module_abspath(repo, path, name) - module_checkout_path = module_abspath if cls._need_gitfile_submodules(repo.git): if not allow_unsafe_options: Git.check_unsafe_options(Git._option_candidates([], kwargs), repo.unsafe_git_clone_options) @@ -377,7 +380,6 @@ def _clone_repo( module_abspath_dir = osp.dirname(module_abspath) if not osp.isdir(module_abspath_dir): os.makedirs(module_abspath_dir) - module_checkout_path = osp.join(repo.working_tree_dir, path) # type: ignore[arg-type] if url.startswith("../"): remote_name = cast("RemoteReference", repo.active_branch.tracking_branch()).remote_name @@ -423,16 +425,23 @@ def abspath(self) -> PathLike: def _checkout_abspath(self, relative_path: PathLike, allow_final_symlink: bool = False) -> PathLike: """Check a checkout path already normalized by :meth:`_to_relative_path`.""" - path = self.repo.working_tree_dir - if path is None: + return self._checked_abspath(self.repo.working_tree_dir, relative_path, allow_final_symlink) + + @classmethod + def _checked_abspath( + cls, root: Union[PathLike, None], relative_path: PathLike, allow_final_symlink: bool = False + ) -> str: + """Reject symlinks below a trusted root before accessing submodule paths.""" + if root is None: raise NotADirectoryError("Submodules require a working tree") - components = os.fspath(relative_path).split("/") + path = os.fspath(root) + components = to_native_path_linux(relative_path).split("/") for index, component in enumerate(components): - path = join_path_native(path, component) + path = os.fspath(join_path_native(path, component)) if allow_final_symlink and index == len(components) - 1: break if osp.islink(path): - raise ValueError("Submodule checkout path %r contains a symbolic link" % relative_path) + raise ValueError("Submodule path %r contains a symbolic link" % relative_path) return path @classmethod @@ -458,14 +467,18 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab :param module_abspath: Absolute path to the bare repository. """ + # Git resolves metadata symlinks before interpreting core.worktree. + module_abspath = osp.realpath(module_abspath) + working_tree_dir = osp.realpath(working_tree_dir) git_file = osp.join(working_tree_dir, ".git") + module_config = osp.join(module_abspath, "config") rela_path = osp.relpath(module_abspath, start=working_tree_dir) if sys.platform == "win32" and osp.isfile(git_file): os.remove(git_file) with open(git_file, "wb") as fp: fp.write(("gitdir: %s" % rela_path).encode(defenc)) - with GitConfigParser(osp.join(module_abspath, "config"), read_only=False, merge_includes=False) as writer: + with GitConfigParser(module_config, read_only=False, merge_includes=False) as writer: writer.set_value( "core", "worktree", @@ -576,6 +589,8 @@ def add( name, url="invalid-temporary", ) + cls._checked_abspath(repo.working_tree_dir, cls.k_modules_file) + sm._checkout_abspath(path) if sm.exists(): # Reretrieve submodule from tree. try: @@ -1067,6 +1082,10 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = return self # END handle no change + if configuration: + self._checked_abspath(self.repo.working_tree_dir, self.k_modules_file) + # Validate the source before removing the destination. + cur_path = self.abspath module_checkout_abspath = self._checkout_abspath(module_checkout_path, allow_final_symlink=True) if osp.isfile(module_checkout_abspath): raise ValueError("Cannot move repository onto a file: %s" % module_checkout_abspath) @@ -1099,7 +1118,6 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = # END handle module # Move the module into place if possible. - cur_path = self.abspath renamed_module = False if module and osp.exists(cur_path): os.renames(cur_path, module_checkout_abspath) @@ -1201,6 +1219,8 @@ def remove( # END handle parameters self._validated_name(self.name) + if configuration: + self._checked_abspath(self.repo.working_tree_dir, self.k_modules_file) # Recursively remove children of this submodule. nc = 0 for csm in self.children(): @@ -1219,7 +1239,7 @@ def remove( ################################ if module and self.module_exists(): mod = self.module() - git_dir = mod.git_dir + git_dir = osp.realpath(mod.git_dir) if force: # Take the fast lane and just delete everything in our module path. # TODO: If we run into permission problems, we have a highly @@ -1460,6 +1480,9 @@ def rename(self, new_name: str) -> "Submodule": self._validated_name(self.name) self._validated_name(new_name) + destination_module_abspath = self._module_abspath(self.repo, self.path, new_name) + mod = self.module() + self._checked_abspath(self.repo.working_tree_dir, self.k_modules_file) # .git/config with self.repo.config_writer() as pw: @@ -1476,9 +1499,7 @@ def rename(self, new_name: str) -> "Submodule": self._name = new_name # .git/modules - mod = self.module() if mod.has_separate_working_tree(): - destination_module_abspath = self._module_abspath(self.repo, self.path, new_name) source_dir = mod.git_dir # Let's be sure the submodule name is not so obviously tied to a directory. if str(destination_module_abspath).startswith(str(mod.git_dir)): diff --git a/test/test_submodule.py b/test/test_submodule.py index 0d0275f21..1fcf3ae23 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -176,6 +176,171 @@ def test_move_leaf_symlink_compatibility(movable_submodule, tmp_path, kind): assert not target.exists() +@pytest.mark.parametrize("leaf", [False, True]) +@pytest.mark.parametrize("dangling", [False, True]) +@pytest.mark.parametrize("operation", ["add", "clone"]) +@pytest.mark.parametrize("gitfile", [False, True]) +def test_clone_rejects_checkout_symlinks(movable_submodule, tmp_path, leaf, dangling, operation, gitfile): + """Reject checkout symlinks before add or clone creates metadata or touches the target. + + Cover leaf and intermediate links, including dangling targets, with both + embedded and separate Git directories. + """ + sm = movable_submodule + root = Path(sm.repo.working_tree_dir) + target = tmp_path / "outside" + if not dangling: + target.mkdir() + (root / "link").symlink_to(target, target_is_directory=True) + path = "link" if leaf else "link/new/module" + before = _move_snapshot(sm) + with mock.patch.object(Submodule, "_need_gitfile_submodules", return_value=gitfile): + with pytest.raises(ValueError, match="contains a symbolic link"): + if operation == "add": + Submodule.add(sm.repo, "new", path, sm.url) + else: + Submodule._clone_repo(sm.repo, sm.url, path, "new") + assert _move_snapshot(sm) == before + assert not (Path(sm.repo.git_dir) / "modules/new").exists() + assert not target.exists() if dangling else list(target.iterdir()) == [] + + +@pytest.mark.parametrize("link_kind", ["gitmodules", "checkout"]) +@pytest.mark.parametrize("operation", ["update", "move", "rename", "remove"]) +def test_submodule_rejects_checkout_and_gitmodules_symlinks(movable_submodule, tmp_path, link_kind, operation): + """Reject operations on symlinked checkouts or .gitmodules without side effects. + + Update, move, rename, and forced removal must preserve the external target, + repository configuration, index, checkout, and any existing move destination. + """ + sm = movable_submodule + sm.rename("nested/module") + root = Path(sm.repo.working_tree_dir) + path = root / (".gitmodules" if link_kind == "gitmodules" else "module") + target = tmp_path / "outside" + path.rename(target) + path.symlink_to(target, target_is_directory=target.is_dir()) + before = ( + {p.relative_to(target): p.read_bytes() for p in target.rglob("*") if p.is_file()} + if target.is_dir() + else target.read_bytes() + ) + config = Path(sm.repo.git_dir, "config").read_bytes() + index = Path(sm.repo.index.path).read_bytes() + gitmodules = (root / ".gitmodules").read_bytes() + (root / "moved").mkdir() + with pytest.raises(ValueError, match="contains a symbolic link"): + if operation == "update": + sm.update() + elif operation == "move": + sm.move("moved") + elif operation == "rename": + sm.rename("renamed") + else: + sm.remove(force=True) + after = ( + {p.relative_to(target): p.read_bytes() for p in target.rglob("*") if p.is_file()} + if target.is_dir() + else target.read_bytes() + ) + assert after == before + assert Path(sm.repo.git_dir, "config").read_bytes() == config + assert Path(sm.repo.index.path).read_bytes() == index + assert (root / ".gitmodules").read_bytes() == gitmodules + assert (root / "module/file").read_text() == "content" + assert path.is_symlink() + assert (root / "moved").is_dir() + + +def test_submodule_allows_symlink_above_worktree(movable_submodule, tmp_path): + """Allow adding and moving submodules when the parent is opened through a symlink.""" + sm = movable_submodule + alias = tmp_path / "alias" + alias.symlink_to(sm.repo.working_tree_dir, target_is_directory=True) + with git.Repo(alias) as parent: + added = Submodule.add(parent, "new", "new", sm.url) + added.move("moved") + with added.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(added.abspath).resolve() + assert Path(added.abspath, "file").read_text() == "content" + + +@pytest.mark.parametrize("operation", ["add", "reconnect", "rename"]) +def test_submodule_allows_metadata_destination_symlinks(movable_submodule, tmp_path, operation): + """Allow linked metadata destinations while keeping the checkout correctly connected. + + Adding, reconnecting after deinit, and renaming may store metadata outside the + parent repository through a symlink under .git/modules, preserving that link. + """ + sm = movable_submodule + root = Path(sm.repo.working_tree_dir) + outside = tmp_path / "outside" + outside.mkdir() + link = Path(sm.repo.git_dir) / "modules/link" + link.symlink_to(outside, target_is_directory=True) + if operation == "rename": + sm.rename("link/new") + else: + sm = Submodule.add(sm.repo, "link/new", "new", sm.url) + if operation == "reconnect": + sm.repo.index.commit("Add linked metadata submodule") + sm.repo.git.submodule("deinit", "--force", "new") + sm.update(init=True) + assert link.is_symlink() + assert (outside / "new/HEAD").is_file() + with sm.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sm.abspath).resolve() + assert Path(sm.abspath, "file").read_text() == "content" + assert (root / ".gitmodules").is_file() + + +@pytest.mark.parametrize("kind", ["modules", "intermediate", "leaf", "gitfile", "config", "alias"]) +@pytest.mark.parametrize("operation", ["update", "move", "rename", "remove"]) +def test_submodule_allows_existing_metadata_symlinks(movable_submodule, tmp_path, kind, operation): + """Keep submodule operations compatible with existing symlinks in Git metadata. + + Cover linked metadata directories, gitfiles, configs, and internal aliases. + Update, move, and rename must retain a usable checkout; forced removal must + still remove it. + """ + sm = movable_submodule + sm.rename("nested/module") + root = Path(sm.repo.working_tree_dir) + modules = Path(sm.repo.git_dir) / "modules" + paths = { + "modules": modules, + "intermediate": modules / "nested", + "leaf": modules / "nested/module", + "gitfile": root / "module/.git", + "config": modules / "nested/module/config", + "alias": modules / "alias", + } + link = paths[kind] + target = tmp_path / "outside" + if kind == "alias": + target = modules / "nested" + (root / "module/.git").write_text("gitdir: ../.git/modules/alias/module") + else: + link.rename(target) + link.symlink_to(target, target_is_directory=target.is_dir()) + # Relocating metadata changes the base of a relative core.worktree setting. + sm.repo.git.config("--file", str(modules / "nested/module/config"), "core.worktree", str(root / "module")) + assert sm.module_exists() + if operation == "remove": + sm.remove(force=True) + assert not (root / "module").exists() + return + if operation == "update": + sm.update() + elif operation == "move": + sm.move("moved") + else: + sm.rename("renamed") + with sm.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sm.abspath).resolve() + assert Path(sm.abspath, "file").read_text() == "content" + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" From 2bfd829f37f4bb810e3c4b409baeeb2be34544b8 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 08:46:28 +0200 Subject: [PATCH 25/92] fix: Preserve submodule paths and release checkout handles on Windows A quick rubber-stamp, admittedly. V4 will probably review all tests and make it more proper, if there can be such a thing in python anyway. Both Windows failures came from os.renames() pruning a directory symlink above the source after the rename succeeded. Unlike POSIX, Windows rmdir() can remove a directory symlink even when its target is nonempty. Moving a checkout through a worktree alias therefore deleted the alias and broke configuration updates and rollback. Renaming metadata through a linked .git/modules directory deleted that link and broke config.lock creation. Route checkout moves, rollback, and metadata renames through one helper. It creates destination parents and renames the source, then prunes empty source parents only until it reaches a symlink or a directory it cannot remove. This keeps ordinary empty-directory cleanup while preserving parent links and their targets, including targets that become empty. Leaf symlinks continue to move as links rather than moving their targets. Exercise Windows directory-symlink removal semantics on POSIX in the existing compatibility tests, and use native behavior on Windows. Both reported failures reproduced locally before the fix. Strengthen assertions that worktree and metadata parent aliases survive, their targets remain directories, and leaf metadata symlinks move without moving their targets. A further Windows run exposed a separate sharing violation during the checkout move. Submodule.add() read HEAD through its temporary Repo but left that Repo's persistent cat-file processes open. Those processes can hold the checkout as their current directory and prevent its rename. Close the owned Repo with a context manager when reading HEAD, including on read failure, instead of waiting for garbage collection. Add a regression that observes the real cat-file processes started for the new checkout and requires them to have exited before add() returns. It failed before the fix and now passes, along with the immediate move. The remaining metadata failures also reproduce with Python 3.7's Windows path semantics: ntpath.realpath is an alias of abspath and does not resolve symlinks. Relative core.worktree values were calculated from the metadata alias instead of the repository directory Git actually opens. This broke add/reconnect HEAD reads and made moves and renames point at nonexistent worktrees. The SHA/dubious-ownership message was a secondary read failure. Use pathlib.Path.resolve(), which resolves Windows symlinks on Python 3.7, for both endpoints of gitfile/config rewrites and for metadata removal. Run metadata and worktree-alias tests with native and simulated Windows 3.7 realpath behavior. The simulation reproduced all eight reported failures plus a leaf-symlink removal failure before this change. Reference: https://github.com/python/cpython/blob/3.7/Lib/ntpath.py and https://github.com/python/cpython/blob/3.7/Lib/pathlib.py. Validation: 183 passed, 3 skipped, and 1 expected failure across the submodule and diff suites plus the commit-message hook success test on macOS. Ruff lint and formatting, mypy for the changed module, and git diff --check passed. Native Windows validation remains for CI. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/objects/submodule/base.py | 34 +++++++++++++---- test/test_submodule.py | 72 +++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 11 deletions(-) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 461b3068d..2b21fc145 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -9,6 +9,7 @@ import ntpath import os import os.path as osp +from pathlib import Path import shlex import stat import sys @@ -444,6 +445,20 @@ def _checked_abspath( raise ValueError("Submodule path %r contains a symbolic link" % relative_path) return path + @staticmethod + def _renames(source: PathLike, destination: PathLike) -> None: + os.makedirs(osp.dirname(destination), exist_ok=True) + os.rename(source, destination) + # Match renames() cleanup, but stop before directory symlinks: Windows + # rmdir() removes the link even when its target is nonempty. + parent = osp.dirname(source) + while parent and not osp.islink(parent): + try: + os.rmdir(parent) + except OSError: + break + parent = osp.dirname(parent) + @classmethod def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_abspath: PathLike) -> None: """Write a ``.git`` file containing a (preferably) relative path to the actual @@ -468,8 +483,9 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab Absolute path to the bare repository. """ # Git resolves metadata symlinks before interpreting core.worktree. - module_abspath = osp.realpath(module_abspath) - working_tree_dir = osp.realpath(working_tree_dir) + # Path.resolve() also handles Windows symlinks on Python 3.7. + module_abspath = str(Path(module_abspath).resolve()) + working_tree_dir = str(Path(working_tree_dir).resolve()) git_file = osp.join(working_tree_dir, ".git") module_config = osp.join(module_abspath, "config") rela_path = osp.relpath(module_abspath, start=working_tree_dir) @@ -685,7 +701,9 @@ def add( # We deliberately assume that our head matches our index! if mrepo: - sm.binsha = mrepo.head.commit.binsha + # Release cat-file processes before callers move the checkout on Windows. + with mrepo: + sm.binsha = mrepo.head.commit.binsha index.add([sm], write=True) return sm @@ -1120,7 +1138,7 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = # Move the module into place if possible. renamed_module = False if module and osp.exists(cur_path): - os.renames(cur_path, module_checkout_abspath) + self._renames(cur_path, module_checkout_abspath) renamed_module = True if osp.isfile(osp.join(module_checkout_abspath, ".git")): @@ -1151,7 +1169,7 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool = # END handle configuration flag except Exception: if renamed_module: - os.renames(module_checkout_abspath, cur_path) + self._renames(module_checkout_abspath, cur_path) # END undo module renaming raise # END handle undo rename @@ -1239,7 +1257,7 @@ def remove( ################################ if module and self.module_exists(): mod = self.module() - git_dir = osp.realpath(mod.git_dir) + git_dir = str(Path(mod.git_dir).resolve()) if force: # Take the fast lane and just delete everything in our module path. # TODO: If we run into permission problems, we have a highly @@ -1504,10 +1522,10 @@ def rename(self, new_name: str) -> "Submodule": # Let's be sure the submodule name is not so obviously tied to a directory. if str(destination_module_abspath).startswith(str(mod.git_dir)): tmp_dir = self._module_abspath(self.repo, self.path, str(uuid.uuid4())) - os.renames(source_dir, tmp_dir) + self._renames(source_dir, tmp_dir) source_dir = tmp_dir # END handle self-containment - os.renames(source_dir, destination_module_abspath) + self._renames(source_dir, destination_module_abspath) if mod.working_tree_dir: self._write_git_file_and_module_config(mod.working_tree_dir, destination_module_abspath) # END move separate git repository diff --git a/test/test_submodule.py b/test/test_submodule.py index 1fcf3ae23..e64416847 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -252,7 +252,53 @@ def test_submodule_rejects_checkout_and_gitmodules_symlinks(movable_submodule, t assert (root / "moved").is_dir() -def test_submodule_allows_symlink_above_worktree(movable_submodule, tmp_path): +def test_add_closes_checkout_processes(movable_submodule, monkeypatch): + """Adding a submodule must not leave a child process holding its checkout open.""" + sm = movable_submodule + checkout = Path(sm.repo.working_tree_dir, "new") + execute = Git.execute + processes = [] + + def capture_process(self, command, *args, **kwargs): + result = execute(self, command, *args, **kwargs) + if ( + kwargs.get("as_process") + and "cat-file" in command + and Path(self.working_dir).resolve() == checkout.resolve() + ): + processes.append(result.proc) + return result + + monkeypatch.setattr(Git, "execute", capture_process) + try: + added = Submodule.add(sm.repo, "new", "new", sm.url) + assert processes, "The HEAD read must exercise a persistent cat-file process" + assert all(process.poll() is not None for process in processes) + added.move("moved") + finally: + for process in processes: + if process.poll() is None: + process.terminate() + process.wait() + + +@pytest.fixture +def windows_directory_symlink_removal(monkeypatch): + """Exercise Windows rmdir semantics on POSIX, where rmdir rejects symlinks.""" + if sys.platform != "win32": + original_rmdir = os.rmdir + + def rmdir(path, *args, **kwargs): + if osp.islink(path): + return os.unlink(path, *args, **kwargs) + return original_rmdir(path, *args, **kwargs) + + monkeypatch.setattr(os, "rmdir", rmdir) + + +def test_submodule_allows_symlink_above_worktree( + movable_submodule, tmp_path, windows_directory_symlink_removal, metadata_realpath +): """Allow adding and moving submodules when the parent is opened through a symlink.""" sm = movable_submodule alias = tmp_path / "alias" @@ -260,13 +306,25 @@ def test_submodule_allows_symlink_above_worktree(movable_submodule, tmp_path): with git.Repo(alias) as parent: added = Submodule.add(parent, "new", "new", sm.url) added.move("moved") + assert alias.is_symlink() with added.module() as module: assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(added.abspath).resolve() assert Path(added.abspath, "file").read_text() == "content" +@pytest.fixture(params=[False, True], ids=["native-realpath", "windows37-realpath"]) +def metadata_realpath(request): + """Model Python 3.7 on Windows without altering pathlib's own resolver.""" + if request.param: + with mock.patch("git.objects.submodule.base.osp", wraps=osp) as paths: + paths.realpath.side_effect = osp.abspath + yield + else: + yield + + @pytest.mark.parametrize("operation", ["add", "reconnect", "rename"]) -def test_submodule_allows_metadata_destination_symlinks(movable_submodule, tmp_path, operation): +def test_submodule_allows_metadata_destination_symlinks(movable_submodule, tmp_path, operation, metadata_realpath): """Allow linked metadata destinations while keeping the checkout correctly connected. Adding, reconnecting after deinit, and renaming may store metadata outside the @@ -296,7 +354,9 @@ def test_submodule_allows_metadata_destination_symlinks(movable_submodule, tmp_p @pytest.mark.parametrize("kind", ["modules", "intermediate", "leaf", "gitfile", "config", "alias"]) @pytest.mark.parametrize("operation", ["update", "move", "rename", "remove"]) -def test_submodule_allows_existing_metadata_symlinks(movable_submodule, tmp_path, kind, operation): +def test_submodule_allows_existing_metadata_symlinks( + movable_submodule, tmp_path, kind, operation, windows_directory_symlink_removal, metadata_realpath +): """Keep submodule operations compatible with existing symlinks in Git metadata. Cover linked metadata directories, gitfiles, configs, and internal aliases. @@ -336,6 +396,12 @@ def test_submodule_allows_existing_metadata_symlinks(movable_submodule, tmp_path sm.move("moved") else: sm.rename("renamed") + if kind == "modules" or (operation == "rename" and kind in ("intermediate", "alias")): + assert link.is_symlink() + assert link.is_dir() + if operation == "rename" and kind == "leaf": + assert (modules / "renamed").is_symlink() + assert target.is_dir() with sm.module() as module: assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sm.abspath).resolve() assert Path(sm.abspath, "file").read_text() == "content" From 415c211705882fadae6be0a299d86e1e4a3c5b03 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 08:19:17 +0000 Subject: [PATCH 26/92] test: Simplify submodule path simulation and verify metadata removal rubber stamp The Python 3.7 Windows path simulation wrapped the entire os.path module in Mock. On Windows, all six simulated removal cases hit sharing violations that the default error handling converted into skipped tests. Use a SimpleNamespace copy of the path module and replace only realpath with abspath. Patch only the submodule module's osp binding so pathlib keeps its own resolver and other path operations remain ordinary calls. This retains Python 3.7 compatibility and allows the removal cases to run successfully without Windows permission-error suppression. Before removing a submodule, resolve and verify its metadata directory, then assert that removal deletes it as well as the checkout. Checking only the checkout could miss metadata left behind through a directory symlink. Validation: all 57 focused submodule compatibility and process-cleanup tests passed on Windows with Python 3.10 and HIDE_WINDOWS_KNOWN_ERRORS=0. Ruff 0.16.5 lint and formatting checks and git diff --check passed. Native Python 3.7 was unavailable; its realpath behavior is simulated. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/objects/submodule/base.py | 6 +++++ test/test_submodule.py | 50 ++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 2b21fc145..57dbf81f3 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -1226,6 +1226,12 @@ def remove( Doesn't work atomically, as failure to remove any part of the submodule will leave an inconsistent state. + :note: + Metadata-directory aliases under ``.git/modules`` are retained. A link + directly to the deleted repository becomes dangling; adding or initializing + the submodule again recreates its target. Linked parent directories remain + available to sibling submodules. + :raise git.exc.InvalidGitRepositoryError: Thrown if the repository cannot be deleted. diff --git a/test/test_submodule.py b/test/test_submodule.py index e64416847..5ca855bd4 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -9,6 +9,7 @@ import shutil import sys import tempfile +from types import SimpleNamespace from unittest import mock, skipUnless import pytest @@ -316,8 +317,9 @@ def test_submodule_allows_symlink_above_worktree( def metadata_realpath(request): """Model Python 3.7 on Windows without altering pathlib's own resolver.""" if request.param: - with mock.patch("git.objects.submodule.base.osp", wraps=osp) as paths: - paths.realpath.side_effect = osp.abspath + paths = SimpleNamespace(**vars(osp)) + paths.realpath = osp.abspath + with mock.patch("git.objects.submodule.base.osp", paths): yield else: yield @@ -361,7 +363,7 @@ def test_submodule_allows_existing_metadata_symlinks( Cover linked metadata directories, gitfiles, configs, and internal aliases. Update, move, and rename must retain a usable checkout; forced removal must - still remove it. + still remove both the checkout and the resolved metadata directory. """ sm = movable_submodule sm.rename("nested/module") @@ -387,8 +389,20 @@ def test_submodule_allows_existing_metadata_symlinks( sm.repo.git.config("--file", str(modules / "nested/module/config"), "core.worktree", str(root / "module")) assert sm.module_exists() if operation == "remove": + metadata_dir = (modules / "nested/module").resolve() + assert metadata_dir.is_dir() + url = sm.url sm.remove(force=True) assert not (root / "module").exists() + assert not metadata_dir.exists() + if kind in ("modules", "intermediate", "alias"): + assert link.is_symlink() and link.is_dir() + replacement = Submodule.add(sm.repo, "nested/module", "module", url) + if kind == "leaf": + assert link.is_symlink() and link.is_dir() + assert target.is_dir() + with replacement.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == (root / "module").resolve() return if operation == "update": sm.update() @@ -407,6 +421,36 @@ def test_submodule_allows_existing_metadata_symlinks( assert Path(sm.abspath, "file").read_text() == "content" +@pytest.mark.parametrize("kind", ["modules", "intermediate", "leaf"]) +def test_remove_linked_metadata_keeps_siblings_and_can_reinitialize( + movable_submodule, tmp_path, kind, metadata_realpath +): + sm = movable_submodule + sm.rename("nested/module") + sibling = Submodule.add(sm.repo, "nested/sibling", "sibling", sm.url) + sm.repo.index.commit("Add sibling") + modules = Path(sm.repo.git_dir) / "modules" + link = {"modules": modules, "intermediate": modules / "nested", "leaf": modules / "nested/module"}[kind] + target = tmp_path / "outside" + link.rename(target) + link.symlink_to(target, target_is_directory=True) + for child in (sm, sibling): + sm.repo.git.config("--file", str(modules / child.name / "config"), "core.worktree", str(child.abspath)) + + sm.remove(force=True, configuration=False) + + assert link.is_symlink() + assert link.exists() == (kind != "leaf") + with sibling.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sibling.abspath).resolve() + assert Path(sibling.abspath, "file").read_text() == "content" + sm.update(init=True) + assert link.is_symlink() and link.is_dir() + assert Path(sm.abspath, "file").read_text() == "content" + with sm.module() as module: + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sm.abspath).resolve() + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" From 958002b4cc06647407d39eb78c65d9dc68d1bee9 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 11:28:46 +0200 Subject: [PATCH 27/92] test: Disable automatic maintenance in remote timeout tests Ubuntu CI reported FileNotFoundError for maintenance.lock while running test_timeout_funcs. The test performs normal pull/fetch calls before its forced timeouts, and Git can launch detached automatic maintenance from those operations. Maintenance can then race with the fixture's recursive removal of the temporary repository, removing a lock file after cleanup has enumerated it. Disable maintenance.auto in this test's temporary repository and set gc.auto to zero for older Git versions that use automatic garbage collection. This removes background housekeeping unrelated to the timeout assertions without weakening repository cleanup or changing library behavior. Use mock.patch.object for the global forced termination status so an assertion failure cannot leak the override into subsequent tests. Validation: Git Trace2 recorded three detached maintenance launches in the original test and none with the fix. The timeout test then passed 20 consecutive runs locally on macOS. Ruff lint and formatting checks and git diff --check passed. The original Ubuntu cleanup exception was not reproduced locally; tracing verified removal of the suspected race source. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- test/test_remote.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/test_remote.py b/test/test_remote.py index e1793214c..0212ffab8 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -1090,14 +1090,16 @@ def test_fetch_unsafe_branch_name(self, rw_repo, remote_repo): class TestTimeouts(TestBase): @with_rw_repo("HEAD", bare=False) def test_timeout_funcs(self, repo): + # Maintenance may outlive a timed-out fetch and race with fixture cleanup. + with repo.config_writer() as config: + config.set_value("maintenance", "auto", False) + config.set_value("gc", "auto", 0) # Older Git versions use auto-gc. + # Force error code to prevent a race condition if the python thread is slow. - default = Git.AutoInterrupt._status_code_if_terminate - Git.AutoInterrupt._status_code_if_terminate = -15 - for function in ["pull", "fetch"]: # Can't get push to time out. - f = getattr(repo.remotes.origin, function) - assert f is not None # Make sure these functions exist. - _ = f() # Make sure the function runs. - with pytest.raises(GitCommandError, match="kill_after_timeout=0 s"): - f(kill_after_timeout=0) - - Git.AutoInterrupt._status_code_if_terminate = default + with mock.patch.object(Git.AutoInterrupt, "_status_code_if_terminate", -15): + for function in ["pull", "fetch"]: # Can't get push to time out. + f = getattr(repo.remotes.origin, function) + assert f is not None # Make sure these functions exist. + _ = f() # Make sure the function runs. + with pytest.raises(GitCommandError, match="kill_after_timeout=0 s"): + f(kill_after_timeout=0) From e9e271bdc0982c262cd47ca4858533eb8c60e8bd Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 09:56:03 +0000 Subject: [PATCH 28/92] fix: Clone into dangling submodule metadata symlink targets on Windows rubber stamp Removing a submodule retains its metadata alias but deletes the target. Adding the same submodule again then passes a dangling directory symlink to git clone --separate-git-dir. Git for Windows fails while copying its template files through that alias. Both native-realpath and simulated Windows 3.7 remove-leaf cases reproduced this failure locally. When the metadata destination is a leaf symlink, pass its target to Git and leave the alias intact. Resolve relative targets against the link's parent, create missing target parents through the existing clone setup, and let Git create the repository directory itself. Precreating that directory is insufficient because Git rejects an existing separate git repository destination. Read the link explicitly because Python 3.7 on Windows cannot resolve a dangling link with Path.resolve(). Normalize the Windows namespace prefix returned by newer os.readlink implementations, including UNC targets, and use forward slashes before passing the path through Git's URL logic. The shared clone helper covers add() and initialization through update(). Add regression coverage for direct cloning through absolute and relative dangling metadata links with missing target parents. Verify that the link and its stored target are retained, metadata is created at the target, and the resulting checkout works under both realpath modes. Validation: 61 focused tests passed on Windows/Python 3.10 with HIDE_WINDOWS_KNOWN_ERRORS=0, including both reported failures. Ruff 0.16.5 lint and formatting checks and git diff --check passed. Native Python 3.7 and UNC network shares were not available for execution. A broader run also exposed sharing violations in sibling-reinitialization tests before cloning; a representative case also failed with the unchanged HEAD clone helper loaded in memory. That separate removal issue is not changed here. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/objects/submodule/base.py | 12 ++++++++++++ test/test_submodule.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 57dbf81f3..8308e4459 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -377,6 +377,18 @@ def _clone_repo( repo.unsafe_git_clone_options, ) allow_unsafe_options = True + if osp.islink(module_abspath): + # Clone into the target while retaining the metadata alias. Git for + # Windows cannot initialize through a dangling directory symlink. + # Read the link explicitly for Python 3.7, and remove the Windows + # namespace prefix returned by newer Python versions for Git. + target = os.readlink(module_abspath) + if sys.platform == "win32": + if target.startswith("\\\\?\\UNC\\"): + target = "\\\\" + target[8:] + elif target.startswith("\\\\?\\"): + target = target[4:] + module_abspath = to_native_path_linux(osp.join(osp.dirname(module_abspath), target)) kwargs["separate_git_dir"] = module_abspath module_abspath_dir = osp.dirname(module_abspath) if not osp.isdir(module_abspath_dir): diff --git a/test/test_submodule.py b/test/test_submodule.py index 5ca855bd4..da391d5cb 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -451,6 +451,25 @@ def test_remove_linked_metadata_keeps_siblings_and_can_reinitialize( assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(sm.abspath).resolve() +@pytest.mark.parametrize("relative_target", [False, True], ids=["absolute-target", "relative-target"]) +def test_add_to_dangling_metadata_symlink(movable_submodule, tmp_path, metadata_realpath, relative_target): + sm = movable_submodule + link = Path(sm.repo.git_dir) / "modules/new" + target = tmp_path / "missing" / "metadata" + link.symlink_to(osp.relpath(target, link.parent) if relative_target else target, target_is_directory=True) + link_target = os.readlink(link) + + added = Submodule.add(sm.repo, "new", "new", sm.url) + + assert link.is_symlink() and link.is_dir() + assert os.readlink(link) == link_target + assert (target / "HEAD").is_file() + with added.module() as module: + assert Path(module.git_dir).resolve() == target.resolve() + assert Path(module.git.rev_parse("--show-toplevel")).resolve() == Path(added.abspath).resolve() + assert Path(added.abspath, "file").read_text() == "content" + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" From 2aca1778cd8b884b8dd39c638e366072e99425ee Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 10:24:34 +0000 Subject: [PATCH 29/92] test: Pass strings to readlink for Windows Python 3.7 rubber stamp, just get this through CI OMG The dangling-metadata regression passed pathlib.Path objects to os.readlink() when capturing and checking the symlink target. Windows Python 3.7 requires a string argument, so all four parameter combinations failed with TypeError before exercising the clone fix. Convert the path to str at both calls. The production clone helper already passes a string and needs no change. Keep the target-preservation assertions and the absolute/relative and realpath-mode coverage intact. Validation: reproduced all four TypeErrors on Windows/Python 3.10 with an in-memory readlink wrapper enforcing the Python 3.7 string requirement. After the conversions, all four cases passed with the same wrapper and Windows permission-error suppression disabled. Ruff 0.16.5 lint and formatting checks and git diff --check passed. Native Python 3.7 was not available locally. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- test/test_submodule.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_submodule.py b/test/test_submodule.py index da391d5cb..f5070c665 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -457,12 +457,12 @@ def test_add_to_dangling_metadata_symlink(movable_submodule, tmp_path, metadata_ link = Path(sm.repo.git_dir) / "modules/new" target = tmp_path / "missing" / "metadata" link.symlink_to(osp.relpath(target, link.parent) if relative_target else target, target_is_directory=True) - link_target = os.readlink(link) + link_target = os.readlink(str(link)) added = Submodule.add(sm.repo, "new", "new", sm.url) assert link.is_symlink() and link.is_dir() - assert os.readlink(link) == link_target + assert os.readlink(str(link)) == link_target assert (target / "HEAD").is_file() with added.module() as module: assert Path(module.git_dir).resolve() == target.resolve() From 8da65080849a56a2fc9f3e7817bbf0524e8397d5 Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 10 Sep 2026 08:07:55 +0200 Subject: [PATCH 30/92] fix: validate checkout positional reference options Head.checkout checked keyword options but omitted the serialized reference from its existing unsafe-option validation (GHSA-23mf-xhv8-69c2). Reference names can originate in a cloned repository, so callers could reach behavior that normally requires explicit opt-in without supplying any checkout options themselves. Pass self through the shared option-candidate helper, matching the argument actually sent to Git. This applies the existing policy to direct and cloned references, including abbreviated option spellings, while retaining allow_unsafe_options=True and ordinary checkout semantics. A leading -- separator would instead make the reference a pathspec and break branch switching. Add direct-reference regression coverage and a local clone regression using synthetic file content; the latter also verifies explicit opt-in. Both regression tests failed before the guard change. All 30 tests in test/test_refs.py pass with Python 3.12.14 and Apple Git 2.50.1; git diff --check passes. Git behavior reference: local git/git checkout at 1630431f326e15fcde608827b5ff38422528eb59, builtin/checkout.c checkout_main pathspec_from_file handling, which parses file contents as pathspecs. No Git source was copied. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- doc/source/changes.rst | 1 + git/refs/head.py | 2 +- test/test_refs.py | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 4803790b9..df003c7d3 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -8,6 +8,7 @@ Changelog Security fixes for * https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-gq48-pqfc-9p58 +* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-23mf-xhv8-69c2 If you can, also try and provide feedback on the upcoming v4 branch https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome. diff --git a/git/refs/head.py b/git/refs/head.py index 7f563374e..5dd4a5a0a 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -283,7 +283,7 @@ def checkout( """ if not allow_unsafe_options: Git.check_unsafe_options( - options=Git._option_candidates([], kwargs), + options=Git._option_candidates([self], kwargs), unsafe_options=Git.unsafe_git_pathspec_from_file_options, ) kwargs["f"] = force diff --git a/test/test_refs.py b/test/test_refs.py index 32c8fbe34..ae3687c30 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -289,6 +289,25 @@ def test_head_checkout_rejects_pathspec_from_file(self, rw_repo): pathspec_file_nul=True, **{option_name: str(pathspecs)}, ) + for option_name in ("--pathspec-from-file", "--pathspec-from"): + branch = Head(rw_repo, f"refs/heads/{option_name}={pathspecs}") + with self.assertRaises(UnsafeOptionError): + branch.checkout() + + def test_cloned_head_checkout_rejects_pathspec_from_file(self): + with tempfile.TemporaryDirectory() as tdir: + base_dir = Path(tdir) + with self._repo_with_initial_commit(base_dir) as source: + branch = source.create_head("--pathspec-from-file=pathspecs") + source.head.reference = branch + with Repo.clone_from(source.working_tree_dir, base_dir / "clone") as cloned: + (base_dir / "clone" / "pathspecs").write_text("unmatched-private-content\n", encoding="utf-8") + assert cloned.active_branch.name == branch.name + with self.assertRaises(UnsafeOptionError): + cloned.active_branch.checkout() + with self.assertRaises(GitCommandError) as error: + cloned.active_branch.checkout(allow_unsafe_options=True) + assert "unmatched-private-content" in str(error.exception) @with_rw_repo("HEAD") def test_head_reset_rejects_pathspec_from_file(self, rw_repo): From fa931374b841fd01f11114b42ca0fdb4d87633dc Mon Sep 17 00:00:00 2001 From: Mingyang Wu <129849514+aprylewu@users.noreply.github.com> Date: Sat, 12 Sep 2026 23:02:32 +0800 Subject: [PATCH 31/92] Fix timeout child-process lookup on non-GNU systems Git.execute used ps --ppid to find direct children before enforcing kill_after_timeout. On macOS this option is rejected: the parent is killed, but a child can continue running and hold captured output pipes open. Use pgrep -P for the child lookup, with POSIX ps PID/PPID output as a fallback when pgrep is absent. Filter the fallback by the original parent PID and reap the lookup subprocess in both paths. Keep the existing parent-first SIGKILL order, direct-child scope, and Windows guard, and update the documented command requirements. Systems without either lookup facility and the existing PID-reuse race remain limitations. Add real-process regressions for native pgrep and the ps fallback, plus a test that excludes unrelated processes and grandchildren from the fallback. Both real-process cases failed on the original code on macOS. The command module now passes 105 tests with 1 skip on macOS 27.0 / Python 3.13.5. Ruff check and format, codespell, mypy (45 files), basedpyright, and diff whitespace checks pass. Linux and Cygwin were not run locally; Cygwin's default ps lacks the required options, so the real-process cases skip it. Fixes #1756 Signed-off-by: Mingyang Wu <129849514+aprylewu@users.noreply.github.com> --- git/cmd.py | 30 ++++++++++++++++--------- test/test_git.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 193dfd4f6..9de2f6e8a 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -1303,9 +1303,9 @@ def execute( carefully considered, due to the following limitations: 1. This feature is not supported at all on Windows. - 2. Effectiveness may vary by operating system. ``ps --ppid`` is used to - enumerate child processes, which is available on most GNU/Linux systems - but not most others. + 2. Enumerating child processes requires ``pgrep -P``, or a ``ps`` command + supporting the POSIX ``-A`` and ``-o`` options if ``pgrep`` is not + installed. Effectiveness may vary on systems without these commands. 3. Deeper descendants do not receive signals, though they may sometimes terminate as a consequence of their parent processes being killed. 4. `kill_after_timeout` uses ``SIGKILL``, which can have negative side @@ -1465,14 +1465,24 @@ def kill_process(pid: int) -> None: This callback implementation would be ineffective and unsafe on Windows. """ - p = Popen(["ps", "--ppid", str(pid)], stdout=PIPE) child_pids = [] - if p.stdout is not None: - for line in p.stdout: - if len(line.split()) > 0: - local_pid = (line.split())[0] - if local_pid.isdigit(): - child_pids.append(int(local_pid)) + try: + p = Popen(["pgrep", "-P", str(pid)], stdout=PIPE) + except FileNotFoundError: + # POSIX ps does not support selecting by parent PID. + with Popen(["ps", "-A", "-o", "pid=", "-o", "ppid="], stdout=PIPE) as p: + if p.stdout is not None: + for line in p.stdout: + fields = line.split() + if len(fields) == 2 and all(field.isdigit() for field in fields): + if int(fields[1]) == pid: + child_pids.append(int(fields[0])) + else: + with p: + if p.stdout is not None: + for line in p.stdout: + if line.strip().isdigit(): + child_pids.append(int(line)) try: os.kill(pid, signal.SIGKILL) for child_pid in child_pids: diff --git a/test/test_git.py b/test/test_git.py index a88d980fb..b19652363 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -14,6 +14,7 @@ import pickle import re import shutil +import signal import subprocess import sys import tempfile @@ -332,6 +333,63 @@ def test_it_honors_kill_after_timeout_with_output_stream(self): self.assertEqual(output_stream.getvalue(), b"started\n") self.assertIn("Timeout: the command", stderr) + @skipUnless( + sys.platform not in ("win32", "cygwin"), + "child process lookup requires pgrep or POSIX ps", + ) + @ddt.data(False, True) + def test_timeout_kills_direct_child(self, without_pgrep): + with tempfile.TemporaryDirectory() as directory: + marker = Path(directory, "child-survived") + child_code = ( + "import pathlib, sys, time; time.sleep(2); " + "pathlib.Path(sys.argv[1]).write_text('survived', encoding='utf-8')" + ) + parent_code = ( + "import subprocess, sys, time; " + "subprocess.Popen([sys.executable, '-c', sys.argv[1], sys.argv[2]]); " + "time.sleep(30)" + ) + popen = cmd.Popen + + def portable_popen(args, **kwargs): + if without_pgrep and args[0] == "pgrep": + raise FileNotFoundError("pgrep is not installed") + return popen(args, **kwargs) + + with mock.patch.object(cmd, "Popen", side_effect=portable_popen): + status, _, stderr = self.git.execute( + [sys.executable, "-c", parent_code, child_code, str(marker)], + kill_after_timeout=1, + with_exceptions=False, + with_extended_output=True, + ) + + self.assertNotEqual(status, 0) + self.assertIn("Timeout: the command", stderr) + self.assertFalse(marker.exists(), "the direct child survived the timeout") + + @skipUnless(sys.platform != "win32", "kill_after_timeout is not supported on Windows") + def test_timeout_ps_fallback_selects_only_direct_children(self): + process = mock.MagicMock() + process.pid = 1234 + process.communicate.return_value = (b"", b"") + process.returncode = -signal.SIGKILL + ps = mock.MagicMock() + ps.__enter__.return_value = ps + ps.stdout = io.BytesIO(b"PID PPID\n 321 1\n 5678 1234\n 9012 5678\n\n") + + with contextlib.ExitStack() as stack: + stack.enter_context(mock.patch.object(cmd, "safer_popen", return_value=process)) + stack.enter_context(mock.patch.object(cmd, "Popen", side_effect=[FileNotFoundError, ps])) + kill = stack.enter_context(mock.patch.object(cmd.os, "kill")) + timer = stack.enter_context(mock.patch.object(cmd.threading, "Timer")) + # Run the timeout callback synchronously, with no real processes or signals. + timer.return_value.start.side_effect = lambda: timer.call_args.args[1](1234) + self.git.execute(["git", "version"], kill_after_timeout=1, with_exceptions=False) + + self.assertEqual(kill.call_args_list, [mock.call(1234, signal.SIGKILL), mock.call(5678, signal.SIGKILL)]) + def test_it_executes_git_without_stdout_redirect(self): returncode, stdout, stderr = self.git.execute( ["git", "version"], From d66edad65ef7c6a42a3040c4c5287e53a16b6b68 Mon Sep 17 00:00:00 2001 From: Byron Date: Sun, 13 Sep 2026 04:44:16 +0200 Subject: [PATCH 32/92] Correct public API typing and add portable runtime checks Several public annotations rejected supported inputs or lost the relationship between input options and return types. Describe Git.execute process, text, bytes, and extended-output results with overloads, accept stdin file descriptors, and account for absent stdout. Correct remote-removal, object, database, blame, and index-entry types, preserve entry subclasses and the supported tuple shapes, and accept streams with only the required read or write methods. Normalize absent previous stderr before appending process errors in AutoInterrupt.wait. Add runtime and static regressions for these interfaces, include them in mypy and basedpyright, make required imports explicit, and reduce the basedpyright baseline to the remaining diagnostics. Keep mock available to typecheck the Python 3.7 import branches. Make the output-type regression emit its payload without a newline. Its original print call produced CRLF on Windows; Git.execute strips the final LF as documented, leaving a carriage return that failed the test in all 11 Windows jobs. Omitting the newline keeps the same text, bytes, and tuple assertions independent of platform newline translation. Validation on macOS with Python 3.12.14: all four test/test_typing.py tests pass. Forcing the child stdout to translate newlines to CRLF reproduces the old failure and passes with the corrected command. Mypy passes for 46 source files; basedpyright --warnings reports no errors or warnings; Ruff lint and format checks for test/test_typing.py and git diff --check pass. Native Windows validation is delegated to the PR CI matrix. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- .basedpyright/baseline.json | 1464 +++++++++++---------------------- git/cmd.py | 112 ++- git/index/base.py | 2 +- git/index/fun.py | 6 +- git/index/typ.py | 23 +- git/objects/base.py | 4 +- git/objects/commit.py | 2 +- git/objects/fun.py | 6 +- git/objects/submodule/base.py | 4 +- git/objects/tag.py | 4 +- git/refs/reference.py | 2 +- git/remote.py | 16 +- git/repo/base.py | 13 +- git/repo/fun.py | 19 +- git/types.py | 16 + git/util.py | 5 +- pyproject.toml | 4 +- test-requirements.txt | 2 +- test/test_git.py | 26 +- test/test_typing.py | 91 ++ 20 files changed, 772 insertions(+), 1049 deletions(-) create mode 100644 test/test_typing.py diff --git a/.basedpyright/baseline.json b/.basedpyright/baseline.json index 14bf1024e..79d35235e 100644 --- a/.basedpyright/baseline.json +++ b/.basedpyright/baseline.json @@ -1,966 +1,502 @@ { - "files": { - "./git/cmd.py": [ - { - "code": "reportGeneralTypeIssues", - "range": { - "startColumn": 13, - "endColumn": 32, - "lineCount": 1 - } - }, - { - "code": "reportOptionalOperand", - "range": { - "startColumn": 27, - "endColumn": 35, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 28, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 28, - "endColumn": 40, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 19, - "endColumn": 68, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 19, - "endColumn": 31, - "lineCount": 1 - } - } - ], - "./git/config.py": [ - { - "code": "reportGeneralTypeIssues", - "range": { - "startColumn": 11, - "endColumn": 26, - "lineCount": 1 - } - }, - { - "code": "reportInvalidTypeVarUse", - "range": { - "startColumn": 43, - "endColumn": 45, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 41, - "endColumn": 48, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 32, - "endColumn": 39, - "lineCount": 1 - } - }, - { - "code": "reportCallIssue", - "range": { - "startColumn": 25, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 30, - "endColumn": 39, - "lineCount": 1 - } - } - ], - "./git/db.py": [ - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 12, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 61, - "endColumn": 79, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 14, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 70, - "endColumn": 88, - "lineCount": 1 - } - } - ], - "./git/index/base.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 30, - "endColumn": 36, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 28, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportAssignmentType", - "range": { - "startColumn": 38, - "endColumn": 76, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 45, - "endColumn": 53, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 60, - "endColumn": 63, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 56, - "endColumn": 60, - "lineCount": 1 - } - }, - { - "code": "reportSelfClsParameterName", - "range": { - "startColumn": 30, - "endColumn": 33, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 51, - "endColumn": 57, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 55, - "endColumn": 61, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 12, - "lineCount": 1 - } - }, - { - "code": "reportAssignmentType", - "range": { - "startColumn": 20, - "endColumn": 46, - "lineCount": 1 - } - } - ], - "./git/index/fun.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 8, - "endColumn": 36, - "lineCount": 1 - } - } - ], - "./git/index/typ.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 37, - "endColumn": 46, - "lineCount": 1 - } - } - ], - "./git/objects/base.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 20, - "endColumn": 27, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 29, - "endColumn": 36, - "lineCount": 1 - } - } - ], - "./git/objects/blob.py": [ - { - "code": "reportIncompatibleVariableOverride", - "range": { - "startColumn": 4, - "endColumn": 8, - "lineCount": 1 - } - } - ], - "./git/objects/commit.py": [ - { - "code": "reportIncompatibleVariableOverride", - "range": { - "startColumn": 4, - "endColumn": 8, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 20, - "endColumn": 33, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 20, - "endColumn": 30, - "lineCount": 1 - } - } - ], - "./git/objects/submodule/base.py": [ - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 19, - "endColumn": 24, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 19, - "endColumn": 24, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 23, - "endColumn": 25, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 23, - "endColumn": 25, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 41, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 52, - "endColumn": 84, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 20, - "endColumn": 40, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 35, - "endColumn": 42, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 20, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 20, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 19, - "endColumn": 33, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 19, - "endColumn": 33, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 27, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 27, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 19, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 19, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 18, - "endColumn": 20, - "lineCount": 1 - } - } - ], - "./git/objects/tag.py": [ - { - "code": "reportIncompatibleVariableOverride", - "range": { - "startColumn": 4, - "endColumn": 8, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 72, - "endColumn": 78, - "lineCount": 1 - } - } - ], - "./git/objects/tree.py": [ - { - "code": "reportIncompatibleVariableOverride", - "range": { - "startColumn": 4, - "endColumn": 8, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 19, - "endColumn": 83, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 54, - "lineCount": 1 - } - } - ], - "./git/objects/util.py": [ - { - "code": "reportAssignmentType", - "range": { - "startColumn": 23, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 22, - "endColumn": 26, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 30, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 54, - "lineCount": 1 - } - } - ], - "./git/refs/log.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 30, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 17, - "endColumn": 22, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 28, - "endColumn": 30, - "lineCount": 1 - } - } - ], - "./git/refs/reference.py": [ - { - "code": "reportInvalidTypeVarUse", - "range": { - "startColumn": 22, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleVariableOverride", - "range": { - "startColumn": 13, - "endColumn": 17, - "lineCount": 1 - } - } - ], - "./git/refs/symbolic.py": [ - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 15, - "endColumn": 20, - "lineCount": 1 - } - } - ], - "./git/refs/tag.py": [ - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 14, - "lineCount": 1 - } - }, - { - "code": "reportIncompatibleMethodOverride", - "range": { - "startColumn": 8, - "endColumn": 14, - "lineCount": 1 - } - } - ], - "./git/remote.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 25, - "endColumn": 36, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 29, - "endColumn": 40, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 26, - "endColumn": 38, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 26, - "endColumn": 38, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 26, - "endColumn": 38, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 26, - "endColumn": 38, - "lineCount": 1 - } - } - ], - "./git/repo/base.py": [ - { - "code": "reportRedeclaration", - "range": { - "startColumn": 4, - "endColumn": 15, - "lineCount": 1 - } - }, - { - "code": "reportAttributeAccessIssue", - "range": { - "startColumn": 18, - "endColumn": 22, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 35, - "endColumn": 41, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 51, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 28, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 16, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 21, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 34, - "endColumn": 44, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 65, - "endColumn": 79, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 82, - "endColumn": 102, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 50, - "endColumn": 69, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 68, - "endColumn": 85, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 88, - "endColumn": 111, - "lineCount": 1 - } - }, - { - "code": "reportTypedDictNotRequiredAccess", - "range": { - "startColumn": 51, - "endColumn": 73, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 12, - "endColumn": 26, - "lineCount": 1 - } - }, - { - "code": "reportAssignmentType", - "range": { - "startColumn": 22, - "endColumn": 38, - "lineCount": 1 - } - } - ], - "./git/repo/fun.py": [ - { - "code": "reportArgumentType", - "range": { - "startColumn": 25, - "endColumn": 33, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 35, - "endColumn": 43, - "lineCount": 1 - } - }, - { - "code": "reportAssignmentType", - "range": { - "startColumn": 18, - "endColumn": 28, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 11, - "endColumn": 14, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 24, - "endColumn": 27, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 11, - "endColumn": 14, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 11, - "endColumn": 20, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 25, - "endColumn": 28, - "lineCount": 1 - } - }, - { - "code": "reportArgumentType", - "range": { - "startColumn": 24, - "endColumn": 27, - "lineCount": 1 - } - } - ], - "./test/deprecation/test_basic.py": [ - { - "code": "reportUnusedExpression", - "range": { - "startColumn": 12, - "endColumn": 62, - "lineCount": 1 - } - } - ] - } + "files": { + "./git/config.py": [ + { + "code": "reportGeneralTypeIssues", + "range": { + "startColumn": 11, + "endColumn": 26, + "lineCount": 1 + } + }, + { + "code": "reportInvalidTypeVarUse", + "range": { + "startColumn": 43, + "endColumn": 45, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 41, + "endColumn": 48, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 32, + "endColumn": 39, + "lineCount": 1 + } + }, + { + "code": "reportCallIssue", + "range": { + "startColumn": 25, + "endColumn": 46, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 30, + "endColumn": 39, + "lineCount": 1 + } + } + ], + "./git/db.py": [ + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 12, + "lineCount": 1 + } + }, + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 14, + "lineCount": 1 + } + } + ], + "./git/index/base.py": [ + { + "code": "reportArgumentType", + "range": { + "startColumn": 30, + "endColumn": 36, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 28, + "endColumn": 34, + "lineCount": 1 + } + }, + { + "code": "reportAssignmentType", + "range": { + "startColumn": 38, + "endColumn": 76, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 51, + "endColumn": 57, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 55, + "endColumn": 61, + "lineCount": 1 + } + }, + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 12, + "lineCount": 1 + } + }, + { + "code": "reportAssignmentType", + "range": { + "startColumn": 20, + "endColumn": 46, + "lineCount": 1 + } + } + ], + "./git/objects/blob.py": [ + { + "code": "reportIncompatibleVariableOverride", + "range": { + "startColumn": 4, + "endColumn": 8, + "lineCount": 1 + } + } + ], + "./git/objects/commit.py": [ + { + "code": "reportIncompatibleVariableOverride", + "range": { + "startColumn": 4, + "endColumn": 8, + "lineCount": 1 + } + }, + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 31, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 20, + "endColumn": 33, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 20, + "endColumn": 30, + "lineCount": 1 + } + } + ], + "./git/objects/submodule/base.py": [ + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 19, + "endColumn": 24, + "lineCount": 1 + } + }, + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 19, + "endColumn": 24, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 23, + "endColumn": 25, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 23, + "endColumn": 25, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 52, + "endColumn": 84, + "lineCount": 1 + } + } + ], + "./git/objects/tag.py": [ + { + "code": "reportIncompatibleVariableOverride", + "range": { + "startColumn": 4, + "endColumn": 8, + "lineCount": 1 + } + } + ], + "./git/objects/tree.py": [ + { + "code": "reportIncompatibleVariableOverride", + "range": { + "startColumn": 4, + "endColumn": 8, + "lineCount": 1 + } + }, + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 31, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 19, + "endColumn": 83, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 15, + "endColumn": 54, + "lineCount": 1 + } + } + ], + "./git/objects/util.py": [ + { + "code": "reportAssignmentType", + "range": { + "startColumn": 23, + "endColumn": 34, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 22, + "endColumn": 26, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 30, + "endColumn": 34, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 15, + "endColumn": 54, + "lineCount": 1 + } + } + ], + "./git/refs/log.py": [ + { + "code": "reportArgumentType", + "range": { + "startColumn": 30, + "endColumn": 34, + "lineCount": 1 + } + }, + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 17, + "endColumn": 22, + "lineCount": 1 + } + }, + { + "code": "reportArgumentType", + "range": { + "startColumn": 28, + "endColumn": 30, + "lineCount": 1 + } + } + ], + "./git/refs/reference.py": [ + { + "code": "reportIncompatibleVariableOverride", + "range": { + "startColumn": 13, + "endColumn": 17, + "lineCount": 1 + } + } + ], + "./git/refs/symbolic.py": [ + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 15, + "endColumn": 20, + "lineCount": 1 + } + } + ], + "./git/refs/tag.py": [ + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 14, + "lineCount": 1 + } + }, + { + "code": "reportIncompatibleMethodOverride", + "range": { + "startColumn": 8, + "endColumn": 14, + "lineCount": 1 + } + } + ], + "./git/remote.py": [ + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 26, + "endColumn": 38, + "lineCount": 1 + } + }, + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 26, + "endColumn": 38, + "lineCount": 1 + } + }, + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 26, + "endColumn": 38, + "lineCount": 1 + } + }, + { + "code": "reportAttributeAccessIssue", + "range": { + "startColumn": 26, + "endColumn": 38, + "lineCount": 1 + } + } + ], + "./git/repo/base.py": [ + { + "code": "reportReturnType", + "range": { + "startColumn": 15, + "endColumn": 46, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 15, + "endColumn": 51, + "lineCount": 1 + } + }, + { + "code": "reportReturnType", + "range": { + "startColumn": 15, + "endColumn": 28, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 21, + "endColumn": 31, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 34, + "endColumn": 44, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 65, + "endColumn": 79, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 82, + "endColumn": 102, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 50, + "endColumn": 69, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 68, + "endColumn": 85, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 88, + "endColumn": 111, + "lineCount": 1 + } + }, + { + "code": "reportTypedDictNotRequiredAccess", + "range": { + "startColumn": 51, + "endColumn": 73, + "lineCount": 1 + } + } + ], + "./git/repo/fun.py": [ + { + "code": "reportReturnType", + "range": { + "startColumn": 11, + "endColumn": 20, + "lineCount": 1 + } + } + ], + "./test/deprecation/test_basic.py": [ + { + "code": "reportUnusedExpression", + "range": { + "startColumn": 12, + "endColumn": 62, + "lineCount": 1 + } + } + ] + } } diff --git a/git/cmd.py b/git/cmd.py index 9de2f6e8a..3d7446906 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -100,7 +100,7 @@ def handle_process_output( - process: "Git.AutoInterrupt" | Popen, + process: Union["Git.AutoInterrupt", Popen], stdout_handler: Union[ None, Callable[[AnyStr], None], @@ -395,9 +395,7 @@ def wait(self, stderr: Union[None, str, bytes] = b"") -> int: :raise git.exc.GitCommandError: If the return status is not 0. """ - if stderr is None: - stderr_b = b"" - stderr_b = force_bytes(data=stderr, encoding="utf-8") + stderr_b = force_bytes(data=stderr, encoding="utf-8") or b"" status: Union[int, None] if self.proc is not None: status = self.proc.wait() @@ -1180,52 +1178,112 @@ def version_info(self) -> Tuple[int, ...]: def execute( self, command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, *, as_process: Literal[True], + **subprocess_kwargs: Any, ) -> "AutoInterrupt": ... @overload def execute( self, command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, *, as_process: Literal[False] = False, - stdout_as_string: Literal[True], - ) -> Union[str, Tuple[int, str, str]]: ... + with_extended_output: Literal[False] = False, + stdout_as_string: Literal[True] = True, + with_stdout: Literal[True] = True, + **subprocess_kwargs: Any, + ) -> str: ... @overload def execute( self, command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, *, as_process: Literal[False] = False, - stdout_as_string: Literal[False] = False, - ) -> Union[bytes, Tuple[int, bytes, str]]: ... + with_extended_output: Literal[False] = False, + stdout_as_string: Literal[False], + universal_newlines: Literal[False] = False, + with_stdout: Literal[True] = True, + **subprocess_kwargs: Any, + ) -> bytes: ... @overload def execute( self, command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, *, - with_extended_output: Literal[False], - as_process: Literal[False], - stdout_as_string: Literal[True], - ) -> str: ... + as_process: Literal[False] = False, + with_extended_output: Literal[True], + stdout_as_string: Literal[True] = True, + with_stdout: Literal[True] = True, + **subprocess_kwargs: Any, + ) -> Tuple[int, str, str]: ... @overload def execute( self, command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, *, - with_extended_output: Literal[False], - as_process: Literal[False], + as_process: Literal[False] = False, + with_extended_output: Literal[True], stdout_as_string: Literal[False], - ) -> bytes: ... + universal_newlines: Literal[False] = False, + with_stdout: Literal[True] = True, + **subprocess_kwargs: Any, + ) -> Tuple[int, bytes, str]: ... + + @overload + def execute( + self, + command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, + *, + as_process: Literal[False] = False, + with_extended_output: Literal[True], + **subprocess_kwargs: Any, + ) -> Tuple[int, Union[str, bytes, None], str]: ... + + @overload + def execute( + self, + command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, + *, + as_process: Literal[False] = False, + with_extended_output: Literal[False] = False, + **subprocess_kwargs: Any, + ) -> Union[str, bytes, None]: ... + + @overload + def execute( + self, + command: Union[str, Sequence[Any]], + istream: Union[None, int, BinaryIO] = None, + with_extended_output: bool = False, + with_exceptions: bool = True, + as_process: bool = False, + output_stream: Union[None, BinaryIO] = None, + stdout_as_string: bool = True, + kill_after_timeout: Union[None, float] = None, + with_stdout: bool = True, + universal_newlines: bool = False, + shell: Union[None, bool] = None, + env: Union[None, Mapping[str, str]] = None, + max_chunk_size: int = io.DEFAULT_BUFFER_SIZE, + strip_newline_in_stdout: bool = True, + **subprocess_kwargs: Any, + ) -> Union[None, str, bytes, Tuple[int, Union[str, bytes, None], str], AutoInterrupt]: ... def execute( self, command: Union[str, Sequence[Any]], - istream: Union[None, BinaryIO] = None, + istream: Union[None, int, BinaryIO] = None, with_extended_output: bool = False, with_exceptions: bool = True, as_process: bool = False, @@ -1239,7 +1297,7 @@ def execute( max_chunk_size: int = io.DEFAULT_BUFFER_SIZE, strip_newline_in_stdout: bool = True, **subprocess_kwargs: Any, - ) -> Union[str, bytes, Tuple[int, Union[str, bytes], str], AutoInterrupt]: + ) -> Union[None, str, bytes, Tuple[int, Union[str, bytes, None], str], AutoInterrupt]: R"""Handle executing the command, and consume and return the returned information (stdout). @@ -1503,7 +1561,7 @@ def make_timeout_error() -> Union[str, bytes]: err = f'Timeout: the command "{" ".join(redacted_command)}" did not complete in {timeout:g} secs.' return err if universal_newlines else err.encode(defenc) - def communicate() -> Tuple[AnyStr, AnyStr]: + def communicate() -> Tuple[Union[str, bytes, None], Union[str, bytes, None]]: assert watchdog is not None assert kill_check is not None watchdog.start() @@ -1523,8 +1581,8 @@ def communicate() -> Tuple[AnyStr, AnyStr]: # Wait for the process to return. status = 0 - stdout_value: Union[str, bytes] = b"" - stderr_value: Union[str, bytes] = b"" + stdout_value: Union[str, bytes, None] = b"" + stderr_value: Union[str, bytes, None] = b"" newline = "\n" if universal_newlines else b"\n" try: if output_stream is None: @@ -1566,7 +1624,7 @@ def communicate() -> Tuple[AnyStr, AnyStr]: if self.GIT_PYTHON_TRACE == "full": cmdstr = " ".join(redacted_command) - def as_text(stdout_value: Union[bytes, str]) -> str: + def as_text(stdout_value: Union[bytes, str, None]) -> str: return not output_stream and safe_decode(stdout_value) or "" # END as_text @@ -1591,6 +1649,8 @@ def as_text(stdout_value: Union[bytes, str]) -> str: if isinstance(stdout_value, bytes) and stdout_as_string: # Could also be output_stream. stdout_value = safe_decode(stdout_value) + # stderr is always captured through PIPE. + assert stderr_value is not None # Allow access to the command's status code. if with_extended_output: return (status, stdout_value, safe_decode(stderr_value)) @@ -1829,7 +1889,7 @@ def _parse_object_header(self, header_line: str) -> Tuple[str, str, int]: raise ValueError("Failed to parse header: %r" % header_line) return (tokens[0], tokens[1], int(tokens[2])) - def _prepare_ref(self, ref: AnyStr) -> bytes: + def _prepare_ref(self, ref: object) -> bytes: # Required for command to separate refs on stdin, as bytes. if isinstance(ref, bytes): # Assume 40 bytes hexsha - bin-to-ascii for some reason returns bytes, not text. @@ -1856,7 +1916,7 @@ def _get_persistent_cmd(self, attr_name: str, cmd_name: str, *args: Any, **kwarg cmd = cast("Git.AutoInterrupt", cmd) return cmd - def __get_object_header(self, cmd: "Git.AutoInterrupt", ref: AnyStr) -> Tuple[str, str, int]: + def __get_object_header(self, cmd: "Git.AutoInterrupt", ref: Union[str, bytes]) -> Tuple[str, str, int]: if cmd.stdin and cmd.stdout: cmd.stdin.write(self._prepare_ref(ref)) cmd.stdin.flush() @@ -1864,7 +1924,7 @@ def __get_object_header(self, cmd: "Git.AutoInterrupt", ref: AnyStr) -> Tuple[st else: raise ValueError("cmd stdin was empty") - def get_object_header(self, ref: str) -> Tuple[str, str, int]: + def get_object_header(self, ref: Union[str, bytes]) -> Tuple[str, str, int]: """Use this method to quickly examine the type and size of the object behind the given ref. @@ -1878,7 +1938,7 @@ def get_object_header(self, ref: str) -> Tuple[str, str, int]: cmd = self._get_persistent_cmd("cat_file_header", "cat_file", batch_check=True) return self.__get_object_header(cmd, ref) - def get_object_data(self, ref: str) -> Tuple[str, str, int, bytes]: + def get_object_data(self, ref: Union[str, bytes]) -> Tuple[str, str, int, bytes]: """Similar to :meth:`get_object_header`, but returns object data as well. :return: @@ -1892,7 +1952,7 @@ def get_object_data(self, ref: str) -> Tuple[str, str, int, bytes]: del stream return (hexsha, typename, size, data) - def stream_object_data(self, ref: str) -> Tuple[str, str, int, "Git.CatFileContentStream"]: + def stream_object_data(self, ref: Union[str, bytes]) -> Tuple[str, str, int, "Git.CatFileContentStream"]: """Similar to :meth:`get_object_data`, but returns the data as a stream. :return: diff --git a/git/index/base.py b/git/index/base.py index a3c915242..560fc5e2c 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -1235,7 +1235,7 @@ def _read_commit_editmsg(self) -> str: def _commit_editmsg_filepath(self) -> str: return osp.join(self.repo.common_dir, "COMMIT_EDITMSG") - def _flush_stdin_and_wait(cls, proc: "Popen[bytes]", ignore_stdout: bool = False) -> bytes: + def _flush_stdin_and_wait(self, proc: "Popen[bytes]", ignore_stdout: bool = False) -> bytes: stdin_IO = proc.stdin if stdin_IO: stdin_IO.flush() diff --git a/git/index/fun.py b/git/index/fun.py index 886e10de9..45c18ace4 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -46,7 +46,7 @@ from git.types import PathLike if TYPE_CHECKING: - from git.db import GitCmdObjectDB + from gitdb.db.base import ObjectDBR, ObjectDBW from git.objects.tree import TreeCacheTup from .base import IndexFile @@ -412,7 +412,7 @@ def read_cache( def write_tree_from_cache( - entries: List[IndexEntry], odb: "GitCmdObjectDB", sl: slice, si: int = 0 + entries: List[IndexEntry], odb: "ObjectDBW", sl: slice, si: int = 0 ) -> Tuple[bytes, List["TreeCacheTup"]]: R"""Create a tree from the given sorted list of entries and put the respective trees into the given object database. @@ -484,7 +484,7 @@ def _tree_entry_to_baseindexentry(tree_entry: "TreeCacheTup", stage: int) -> Bas return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) -def aggressive_tree_merge(odb: "GitCmdObjectDB", tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]: +def aggressive_tree_merge(odb: "ObjectDBR", tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]: R""" :return: List of :class:`~git.index.typ.BaseIndexEntry`\s representing the aggressive diff --git a/git/index/typ.py b/git/index/typ.py index 927633a9f..78f7c8ea5 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -9,12 +9,13 @@ from pathlib import Path from git.objects import Blob +from git.objects.base import IndexObject from .util import pack, unpack # typing ---------------------------------------------------------------------- -from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast +from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Type, TypeVar, Union, cast from git.types import PathLike @@ -22,6 +23,7 @@ from git.repo import Repo StageType = int +_T_IndexEntry = TypeVar("_T_IndexEntry", bound="BaseIndexEntry") # --------------------------------------------------------------------------------- @@ -104,15 +106,20 @@ class BaseIndexEntry(BaseIndexEntryHelper): """ def __new__( - cls, + cls: Type[_T_IndexEntry], inp_tuple: Union[ Tuple[int, bytes, int, PathLike], + Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int], Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int, int], ], - ) -> "BaseIndexEntry": + ) -> _T_IndexEntry: """Override ``__new__`` to allow construction from a tuple for backwards compatibility.""" - return super().__new__(cls, *inp_tuple) + if len(inp_tuple) == 4: + return BaseIndexEntryHelper.__new__(cls, *inp_tuple) + if len(inp_tuple) == 11: + return BaseIndexEntryHelper.__new__(cls, *inp_tuple) + return BaseIndexEntryHelper.__new__(cls, *inp_tuple) def __str__(self) -> str: return "%o %s %i\t%s" % (self.mode, self.hexsha, self.stage, self.path) @@ -148,7 +155,7 @@ def intent_to_add(self) -> bool: return (self.extended_flags & CE_EXT_INTENT_TO_ADD) > 0 @classmethod - def from_blob(cls, blob: Blob, stage: int = 0) -> "BaseIndexEntry": + def from_blob(cls, blob: IndexObject, stage: int = 0) -> "BaseIndexEntry": """:return: Fully equipped BaseIndexEntry at the given stage""" return cls((blob.mode, blob.binsha, stage << CE_STAGESHIFT, blob.path)) @@ -192,10 +199,10 @@ def from_base(cls, base: "BaseIndexEntry") -> "IndexEntry": Instance of type :class:`BaseIndexEntry`. """ time = pack(">LL", 0, 0) - return IndexEntry((base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0)) # type: ignore[arg-type] + return IndexEntry((base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0)) @classmethod - def from_blob(cls, blob: Blob, stage: int = 0) -> "IndexEntry": + def from_blob(cls, blob: IndexObject, stage: int = 0) -> "IndexEntry": """:return: Minimal entry resembling the given blob object""" time = pack(">LL", 0, 0) return IndexEntry( @@ -211,5 +218,5 @@ def from_blob(cls, blob: Blob, stage: int = 0) -> "IndexEntry": 0, 0, blob.size, - ) # type: ignore[arg-type] + ) ) diff --git a/git/objects/base.py b/git/objects/base.py index faf600c6b..1188ec0c9 100644 --- a/git/objects/base.py +++ b/git/objects/base.py @@ -18,7 +18,7 @@ from typing import Any, TYPE_CHECKING, Union -from git.types import AnyGitObject, GitObjectTypeString, PathLike +from git.types import AnyGitObject, GitObjectTypeString, PathLike, SupportsWrite if TYPE_CHECKING: from gitdb.base import OStream @@ -200,7 +200,7 @@ def data_stream(self) -> "OStream": """ return self.repo.odb.stream(self.binsha) - def stream_data(self, ostream: "OStream") -> "Object": + def stream_data(self, ostream: SupportsWrite[bytes]) -> "Object": """Write our data directly to the given output stream. :param ostream: diff --git a/git/objects/commit.py b/git/objects/commit.py index 45843eac2..0348d3299 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -502,7 +502,7 @@ def _interpret_trailers( ) -> str: message_bytes = message if isinstance(message, bytes) else message.encode(encoding, errors="strict") cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers", *trailer_args] - proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload] + proc: Git.AutoInterrupt = repo.git.execute( cmd, as_process=True, istream=PIPE, diff --git a/git/objects/fun.py b/git/objects/fun.py index ad5fbd59b..7ef229990 100644 --- a/git/objects/fun.py +++ b/git/objects/fun.py @@ -30,7 +30,7 @@ if TYPE_CHECKING: from _typeshed import ReadableBuffer - from git import GitCmdObjectDB + from gitdb.db.base import ObjectDBR EntryTup = Tuple[bytes, int, str] # Same as TreeCacheTup in tree.py. EntryTupOrNone = Union[EntryTup, None] @@ -166,7 +166,7 @@ def _to_full_path(item: EntryTupOrNone, path_prefix: str) -> EntryTupOrNone: def traverse_trees_recursive( - odb: "GitCmdObjectDB", tree_shas: Sequence[Union[bytes, None]], path_prefix: str + odb: "ObjectDBR", tree_shas: Sequence[Union[bytes, None]], path_prefix: str ) -> List[Tuple[EntryTupOrNone, ...]]: """ :return: @@ -253,7 +253,7 @@ def traverse_trees_recursive( return out -def traverse_tree_recursive(odb: "GitCmdObjectDB", tree_sha: bytes, path_prefix: str) -> List[EntryTup]: +def traverse_tree_recursive(odb: "ObjectDBR", tree_sha: bytes, path_prefix: str) -> List[EntryTup]: """ :return: List of entries of the tree pointed to by the binary `tree_sha`. diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 8308e4459..ba281c499 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -14,7 +14,7 @@ import stat import sys import uuid -import urllib +import urllib.parse import git from git.cmd import Git @@ -1769,7 +1769,7 @@ def iter_items( # END handle critical error # Make sure we are looking at a submodule object. - if type(sm) is not git.objects.submodule.base.Submodule: + if type(sm) is not Submodule: continue # Fill in remaining info - saves time as it doesn't have to be parsed again. diff --git a/git/objects/tag.py b/git/objects/tag.py index 88671d316..18b4a9ca4 100644 --- a/git/objects/tag.py +++ b/git/objects/tag.py @@ -23,6 +23,8 @@ from typing import List, TYPE_CHECKING, Union +from git.types import AnyGitObject + if sys.version_info >= (3, 8): from typing import Literal else: @@ -61,7 +63,7 @@ def __init__( self, repo: "Repo", binsha: bytes, - object: Union[None, base.Object] = None, + object: Union[None, AnyGitObject] = None, tag: Union[None, str] = None, tagger: Union[None, Actor] = None, tagged_date: Union[int, None] = None, diff --git a/git/refs/reference.py b/git/refs/reference.py index 0c4327225..7d6c62cf5 100644 --- a/git/refs/reference.py +++ b/git/refs/reference.py @@ -26,7 +26,7 @@ def require_remote_ref_path(func: Callable[..., _T]) -> Callable[..., _T]: """A decorator raising :exc:`ValueError` if we are not a valid remote, based on the path.""" - def wrapper(self: T_References, *args: Any) -> _T: + def wrapper(self: SymbolicReference, *args: Any) -> _T: if not self.is_remote(): raise ValueError("ref path does not point to a remote reference: %s" % self.path) return func(self, *args) diff --git a/git/remote.py b/git/remote.py index e2d5cbc1d..2ddf11af0 100644 --- a/git/remote.py +++ b/git/remote.py @@ -38,6 +38,7 @@ Sequence, TYPE_CHECKING, Type, + TypeVar, Union, cast, overload, @@ -50,6 +51,8 @@ from git.objects.submodule.base import UpdateProgress from git.repo.base import Repo +_T_RemoteName = TypeVar("_T_RemoteName", bound=Union[str, "Remote"]) + flagKeyLiteral = Literal[" ", "!", "+", "-", "*", "=", "t", "?"] # ------------------------------------------------------------- @@ -820,19 +823,20 @@ def add(cls, repo: "Repo", name: str, url: str, **kwargs: Any) -> "Remote": return cls.create(repo, name, url, **kwargs) @classmethod - def remove(cls, repo: "Repo", name: str) -> str: + def remove(cls, repo: "Repo", name: _T_RemoteName) -> _T_RemoteName: """Remove the remote with the given name. :return: The passed remote name to remove """ repo.git.remote("rm", name) - if isinstance(name, cls): - name._clear_cache() + remote = name + if isinstance(remote, cls): + remote._clear_cache() return name @classmethod - def rm(cls, repo: "Repo", name: str) -> str: + def rm(cls, repo: "Repo", name: _T_RemoteName) -> _T_RemoteName: """Alias of remove. Remove the remote with the given name. @@ -901,7 +905,7 @@ def _get_fetch_info_from_stderr( kill_after_timeout=kill_after_timeout, ) - stderr_text = progress.error_lines and "\n".join(progress.error_lines) or "" + stderr_text = "\n".join(progress.error_lines) proc.wait(stderr=stderr_text) if stderr_text: _logger.warning("Error lines received while fetching: %s", stderr_text) @@ -973,7 +977,7 @@ def stdout_handler(line: str) -> None: decode_streams=False, kill_after_timeout=kill_after_timeout, ) - stderr_text = progress.error_lines and "\n".join(progress.error_lines) or "" + stderr_text = "\n".join(progress.error_lines) try: proc.wait(stderr=stderr_text) except Exception as e: diff --git a/git/repo/base.py b/git/repo/base.py index f326266d6..29922e118 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -18,6 +18,7 @@ import warnings import gitdb +import gitdb.util from gitdb.db.loose import LooseObjectDB from gitdb.exc import BadObject @@ -33,7 +34,7 @@ from git.index import IndexFile from git.objects import Submodule, RootModule, Commit from git.refs import HEAD, Head, Reference, TagReference -from git.remote import Remote, add_progress, to_progress_instance +from git.remote import Remote, _T_RemoteName, add_progress, to_progress_instance from git.util import ( Actor, cygpath, @@ -95,7 +96,7 @@ class BlameEntry(NamedTuple): - commit: Dict[str, Commit] + commit: Commit linenos: range orig_path: Optional[str] orig_linenos: range @@ -396,7 +397,7 @@ def __init__( self._working_tree_dir = None # END working dir handling - self.working_dir: PathLike = self._working_tree_dir or self.common_dir + self.working_dir = self._working_tree_dir or self.common_dir self.git = self.GitCommandWrapperType(self.working_dir) if common_dir_env is not None: self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir)) @@ -718,7 +719,7 @@ def create_remote(self, name: str, url: str, **kwargs: Any) -> Remote: """ return Remote.create(self, name, url, **kwargs) - def delete_remote(self, remote: "Remote") -> str: + def delete_remote(self, remote: _T_RemoteName) -> _T_RemoteName: """Delete the given remote.""" return Remote.remove(self, remote) @@ -1504,7 +1505,7 @@ def _clone( git: "Git", url: PathLike, path: PathLike, - odb_default_type: Type[GitCmdObjectDB], + odb_default_type: Type[LooseObjectDB], progress: Union["RemoteProgress", "UpdateProgress", Callable[..., "RemoteProgress"], None] = None, multi_options: Optional[List[str]] = None, allow_unsafe_protocols: bool = False, @@ -1711,7 +1712,7 @@ def clone_from( def archive( self, ostream: Union[TextIO, BinaryIO], - treeish: Optional[str] = None, + treeish: Union[str, Commit, None] = None, prefix: Optional[str] = None, allow_unsafe_options: bool = False, allow_unsafe_protocols: bool = False, diff --git a/git/repo/fun.py b/git/repo/fun.py index eb0d8075a..a565054bc 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -40,11 +40,10 @@ from git.types import AnyGitObject, Literal, PathLike if TYPE_CHECKING: - from git.db import GitCmdObjectDB + from gitdb.db import CompoundDB, LooseObjectDB from git.objects import Commit from git.refs.reference import Reference from git.refs.log import RefLog, RefLogEntry - from git.refs.tag import Tag from .base import Repo @@ -158,7 +157,7 @@ def find_submodule_git_dir(d: PathLike) -> Optional[PathLike]: return path if is_git_dir(path) else None -def short_to_long(odb: "GitCmdObjectDB", hexsha: str) -> Optional[bytes]: +def short_to_long(odb: Union["CompoundDB", "LooseObjectDB"], hexsha: str) -> Optional[bytes]: """ :return: Long hexadecimal sha1 from the given less than 40 byte hexsha, or ``None`` if no @@ -261,7 +260,7 @@ def name_to_object(repo: "Repo", name: str, return_ref: bool = False) -> Union[A return Object.new_from_sha(repo, hex_to_bin(hexsha)) -def deref_tag(tag: "Tag") -> AnyGitObject: +def deref_tag(tag: AnyGitObject) -> AnyGitObject: """Recursively dereference a tag and return the resulting object.""" while True: try: @@ -272,7 +271,7 @@ def deref_tag(tag: "Tag") -> AnyGitObject: return tag -def to_commit(obj: Object) -> "Commit": +def to_commit(obj: AnyGitObject) -> "Commit": """Convert the given object to a commit if possible and return it.""" if obj.type == "tag": obj = deref_tag(obj) @@ -521,7 +520,7 @@ def _find_commit_by_message( if rev is None: commits = _all_ref_commits(repo) else: - commits = _reachable_commits([to_commit(cast(Object, rev))]) + commits = _reachable_commits([to_commit(rev)]) # END handle starting point for commit in commits: @@ -541,7 +540,7 @@ def _all_ref_commits(repo: "Repo") -> Iterator["Commit"]: starts = [] for ref in repo.references: try: - starts.append(to_commit(cast(Object, ref.object))) + starts.append(to_commit(ref.object)) except (BadName, ValueError): pass # END skip refs that do not point to commits @@ -589,7 +588,7 @@ def _index_lookup(repo: "Repo", spec: str) -> AnyGitObject: def _tree_lookup(obj: AnyGitObject, path: str) -> AnyGitObject: if obj.type != "tree": - obj = to_commit(cast(Object, obj)).tree + obj = to_commit(obj).tree # END get tree if not path: return obj @@ -604,9 +603,9 @@ def _peel(obj: AnyGitObject, output_type: str, repo: "Repo", rev: str) -> AnyGit if output_type == "object": return obj if output_type == "commit": - return to_commit(cast(Object, obj)) + return to_commit(obj) if output_type == "tree": - return to_commit(cast(Object, obj)).tree if obj.type != "tree" else obj + return to_commit(obj).tree if obj.type != "tree" else obj if output_type == "blob": obj = deref_tag(obj) if obj.type == "tag" else obj if obj.type == output_type: diff --git a/git/types.py b/git/types.py index 100fff43f..31d40bf3b 100644 --- a/git/types.py +++ b/git/types.py @@ -48,6 +48,22 @@ _T = TypeVar("_T") """Type variable used internally in GitPython.""" +_T_Stream_co = TypeVar("_T_Stream_co", str, bytes, covariant=True) +_T_Stream_contra = TypeVar("_T_Stream_contra", str, bytes, contravariant=True) + + +class SupportsRead(Protocol[_T_Stream_co]): + """A stream supporting reads, without requiring the full IO interface.""" + + def read(self, __size: int = -1) -> _T_Stream_co: ... + + +class SupportsWrite(Protocol[_T_Stream_contra]): + """A stream supporting writes, including writers that return None.""" + + def write(self, __data: _T_Stream_contra) -> object: ... + + AnyGitObject = Union["Commit", "Tree", "TagObject", "Blob"] """Union of the :class:`~git.objects.base.Object`-based types that represent actual git object types. diff --git a/git/util.py b/git/util.py index a80e667c7..f72e3d7c1 100644 --- a/git/util.py +++ b/git/util.py @@ -68,7 +68,6 @@ from typing import ( Any, AnyStr, - BinaryIO, Callable, Dict, Generator, @@ -101,6 +100,8 @@ PathLike, Protocol, SupportsIndex, + SupportsRead, + SupportsWrite, Total_TD, runtime_checkable, ) @@ -253,7 +254,7 @@ def rmfile(path: PathLike) -> None: os.remove(path) -def stream_copy(source: BinaryIO, destination: BinaryIO, chunk_size: int = 512 * 1024) -> int: +def stream_copy(source: SupportsRead[AnyStr], destination: SupportsWrite[AnyStr], chunk_size: int = 512 * 1024) -> int: """Copy all data from the `source` stream into the `destination` stream in chunks of size `chunk_size`. diff --git a/pyproject.toml b/pyproject.toml index b7c437bf3..fbcde3611 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ testpaths = "test" # Space separated list of paths from root e.g test tests doc # filterwarnings ignore::WarningType # ignores those warnings [tool.mypy] -files = ["git/", "test/deprecation/"] +files = ["git/", "test/deprecation/", "test/test_typing.py"] disallow_untyped_defs = true no_implicit_optional = true warn_redundant_casts = true @@ -40,6 +40,8 @@ pythonVersion = "3.7" include = [ "git", "test/deprecation", + "test/test_typing.py", + "test/test_git.py", ] extraPaths = [ "gitdb", diff --git a/test-requirements.txt b/test-requirements.txt index e2443825c..0fabac809 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,7 +1,7 @@ coverage[toml] basedpyright==1.39.9 ; python_version >= "3.9" and sys_platform != "cygwin" ddt >= 1.1.1, != 1.4.3 -mock ; python_version < "3.8" +mock # Also needed to typecheck the Python 3.7 import branches. mypy==1.18.2 ; python_version >= "3.9" # pin mypy version to avoid new errors pre-commit pytest >= 7.3.1 diff --git a/test/test_git.py b/test/test_git.py index b19652363..14fc7dfc3 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -137,24 +137,24 @@ def test_it_raises_errors(self): self.assertRaises(GitCommandError, self.git.this_does_not_exist) def test_it_transforms_kwargs_into_git_command_arguments(self): - self.assertEqual(["-s"], self.git.transform_kwargs(**{"s": True})) - self.assertEqual(["-s", "5"], self.git.transform_kwargs(**{"s": 5})) - self.assertEqual([], self.git.transform_kwargs(**{"s": None})) + self.assertEqual(["-s"], self.git.transform_kwargs(s=True)) + self.assertEqual(["-s", "5"], self.git.transform_kwargs(s=5)) + self.assertEqual([], self.git.transform_kwargs(s=None)) - self.assertEqual(["--max-count"], self.git.transform_kwargs(**{"max_count": True})) - self.assertEqual(["--max-count=5"], self.git.transform_kwargs(**{"max_count": 5})) - self.assertEqual(["--max-count=0"], self.git.transform_kwargs(**{"max_count": 0})) - self.assertEqual([], self.git.transform_kwargs(**{"max_count": None})) + self.assertEqual(["--max-count"], self.git.transform_kwargs(max_count=True)) + self.assertEqual(["--max-count=5"], self.git.transform_kwargs(max_count=5)) + self.assertEqual(["--max-count=0"], self.git.transform_kwargs(max_count=0)) + self.assertEqual([], self.git.transform_kwargs(max_count=None)) # Multiple args are supported by using lists/tuples. self.assertEqual( ["-L", "1-3", "-L", "12-18"], - self.git.transform_kwargs(**{"L": ("1-3", "12-18")}), + self.git.transform_kwargs(L=("1-3", "12-18")), ) - self.assertEqual(["-C", "-C"], self.git.transform_kwargs(**{"C": [True, True, None, False]})) + self.assertEqual(["-C", "-C"], self.git.transform_kwargs(C=[True, True, None, False])) # Order is undefined. - res = self.git.transform_kwargs(**{"s": True, "t": True}) + res = self.git.transform_kwargs(s=True, t=True) self.assertEqual({"-s", "-t"}, set(res)) def test_check_unsafe_options_normalizes_kwargs(self): @@ -221,7 +221,9 @@ def test_option_candidates_include_falsey_non_boolean_values(self): candidates = Git._option_candidates(kwargs=kwargs) self.assertEqual(candidates, ["--pathspec-from-file"]) - self.assertEqual(self.git.transform_kwargs(**kwargs), ["--pathspec-from-file=0"]) + self.assertEqual( + self.git.transform_kwargs(split_single_char_options=True, **kwargs), ["--pathspec-from-file=0"] + ) with self.assertRaises(UnsafeOptionError): Git.check_unsafe_options( options=candidates, @@ -634,6 +636,7 @@ def test_initial_refresh_from_bad_git_path_env_warn(self, case): with mock.patch.dict(os.environ, env_vars): with self.assertLogs(cmd.__name__, logging.CRITICAL) as ctx: refresh() + assert ctx is not None self.assertEqual(len(ctx.records), 1) message = ctx.records[0].getMessage() self.assertRegex(message, r"\ABad git executable.\n") @@ -753,6 +756,7 @@ def test_refresh_with_good_absolute_git_path_arg(self): def test_refresh_with_good_relative_git_path_arg(self): """Good relative path arg is resolved to absolute path and set.""" absolute_path = shutil.which("git") + assert absolute_path is not None dirname, basename = osp.split(absolute_path) with cwd(dirname): diff --git a/test/test_typing.py b/test/test_typing.py new file mode 100644 index 000000000..101c05520 --- /dev/null +++ b/test/test_typing.py @@ -0,0 +1,91 @@ +# This module is part of GitPython and is released under the +# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ + +"""Runtime and static checks for public APIs with related input/output types.""" + +from io import BytesIO, StringIO +import subprocess +import sys +from typing import List, Tuple, TYPE_CHECKING + +import pytest + +from git import Git +from git.exc import GitCommandError +from git.index.typ import BaseIndexEntry, IndexEntry +from git.util import stream_copy + + +def test_index_entry_constructor_shapes() -> None: + class DerivedEntry(IndexEntry): + pass + + short = (0o100644, b"\0" * 20, 0, "file") + full = short + (b"\0" * 8, b"\0" * 8, 1, 2, 3, 4, 5) + entries: List[IndexEntry] = [IndexEntry(short), IndexEntry(full), IndexEntry(full + (0x4000,))] + derived: DerivedEntry = DerivedEntry(short) + + assert all(type(entry) is IndexEntry for entry in entries) + assert [entry.size for entry in entries] == [0, 5, 5] + assert [entry.skip_worktree for entry in entries] == [False, False, True] + assert type(derived) is DerivedEntry + assert IndexEntry.from_base(BaseIndexEntry(short)) == entries[0] + + +def test_stream_copy_minimal_writer() -> None: + class Writer: + def __init__(self) -> None: + self.data = b"" + + def write(self, data: bytes) -> None: + self.data += data + + writer = Writer() + assert stream_copy(BytesIO(b"payload"), writer, chunk_size=3) == 7 + assert writer.data == b"payload" + text = StringIO() + assert stream_copy(StringIO("payload"), text, chunk_size=3) == 7 + assert text.getvalue() == "payload" + + +def test_process_wait_with_no_previous_stderr() -> None: + process = Git().execute( + [sys.executable, "-c", "import sys; sys.stderr.write('failure'); sys.exit(1)"], + as_process=True, + istream=subprocess.DEVNULL, + shell=False, + ) + with pytest.raises(GitCommandError, match="failure"): + process.wait(stderr=None) + + +def test_execute_output_types() -> None: + git = Git() + command = [sys.executable, "-c", "print('payload', end='')"] + text: str = git.execute(command, with_exceptions=False, shell=False) + binary: bytes = git.execute(command, stdout_as_string=False, shell=False) + extended_text: Tuple[int, str, str] = git.execute(command, with_extended_output=True, shell=False) + extended_binary: Tuple[int, bytes, str] = git.execute( + command, with_extended_output=True, stdout_as_string=False, shell=False + ) + assert text == "payload" + assert binary == b"payload" + assert extended_text == (0, text, "") + assert extended_binary == (0, binary, "") + + +if TYPE_CHECKING: + from git import Remote, Repo + from git.repo.base import BlameEntry + from git.objects import Commit, Submodule + + repo = Repo() + remote = Remote(repo, "origin") + removed_names: List[str] = [Remote.remove(repo, "origin"), Remote.rm(repo, "origin"), repo.delete_remote("origin")] + removed_remotes: List[Remote] = [Remote.remove(repo, remote), Remote.rm(repo, remote), repo.delete_remote(remote)] + blame = BlameEntry(repo.head.commit, range(1), "file", range(1)) + commit: Commit = blame.commit + submodule_entry: IndexEntry = IndexEntry.from_blob(Submodule(repo, b"\0" * 20)) + Git().get_object_header(b"HEAD") + Git().get_object_data(b"HEAD") + Git().stream_object_data(b"HEAD") From 22296682d66fb0b2a76e549670d0ec6ef7447ab1 Mon Sep 17 00:00:00 2001 From: Byron Date: Sun, 13 Sep 2026 11:01:15 +0200 Subject: [PATCH 33/92] fix: preserve implicit boolean config keys (#2237) This was mostly a rubber-stamp, knowing the the whole implementation is quite a hack that is held together with ductape. Ideally, it will just work well enough at some point, to gain time for v4 to be made. GitConfigParser discarded keys written without an assignment. Reading such an option raised NoOptionError, and editing an unrelated setting silently removed it. Git treats a bare key as true but an explicitly empty value as false, so representing both as an empty string would lose their meaning. Store bare entries as None, following RawConfigParser's allow_no_value representation. Raw get/items access preserves the distinction, while get_value/get_values return an empty string as requested. Convert None to true and an empty string to false in getboolean, retaining the standard boolean spellings. Write None entries without an equals sign and exclude them from string validation and include-path expansion. The existing ordered multi-dict preserves repeated bare and assigned entries together. Update the regression that expected bare color.ui to disappear, and add a Git-backed read-modify-write test covering trailing whitespace, EOF without a newline, quoted and unquoted empty values, repeated keys, and a valueless non-path option in an include section. Both regressions failed before the fix. Compare Git's NUL-delimited listing before and after an unrelated edit and check the resulting repeated values with --type=bool --get-all. Git reference: checkout 1630431f326e15fcde608827b5ff38422528eb59, t/t1300-config.sh tests for novalue.variable and emptyvalue.variable, and parse.c:git_parse_maybe_bool_text. Runtime comparison used Git 2.50.1 (Apple Git-155). Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/config.py | 51 ++++++++++++++++++++++++++++++++---------- test/test_config.py | 54 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/git/config.py b/git/config.py index 0da90bab2..d6f2706f1 100644 --- a/git/config.py +++ b/git/config.py @@ -45,7 +45,7 @@ from git.repo.base import Repo T_ConfigParser = TypeVar("T_ConfigParser", bound="GitConfigParser") -T_OMD_value = TypeVar("T_OMD_value", str, bytes, int, float, bool) +T_OMD_value = TypeVar("T_OMD_value", str, bytes, int, float, bool, None) if sys.version_info[:3] < (3, 7, 2): # typing.Ordereddict not added until Python 3.7.2. @@ -291,6 +291,12 @@ class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder): :note: If used as a context manager, this will release the locked file. + + :note: + Options without a value are stored as ``None`` and written without ``=``. + :meth:`get_value` and :meth:`get_values` return an empty string for them, + while :meth:`getboolean` returns ``True``. An explicit empty value is + stored as an empty string and reads as ``False`` with :meth:`getboolean`. """ # { Configuration @@ -348,7 +354,7 @@ def __init__( Reference to repository to use if ``[includeIf]`` sections are found in configuration files. """ - cp.RawConfigParser.__init__(self, dict_type=_OMD) + cp.RawConfigParser.__init__(self, dict_type=_OMD, allow_no_value=True) self._dict: Callable[..., _OMD] self._defaults: _OMD self._sections: _OMD @@ -587,8 +593,12 @@ def parse_value(value: str) -> str: # Preserves multiple values for duplicate optnames. cursect.add(optname, optval) else: - # Check if it's an option with no value - it's just ignored by git. - if not self.OPTVALUEONLY.match(line): + # A valueless option is an implicit boolean true, not an empty value. + mo = self.OPTVALUEONLY.match(line) + if mo: + optname = self.optionxform(mo.group("option").rstrip()) + cursect.add(optname, None) + else: if not e: e = cp.ParsingError(fpname) e.append(lineno, repr(line)) @@ -625,6 +635,7 @@ def _all_items(section: str) -> List[Tuple[str, str]]: for key, values in self._sections[section].items_all() if key != "__name__" for value in values + if value is not None ] paths = [] @@ -760,13 +771,16 @@ def _write(self, fp: IO) -> None: def write_section(name: str, section_dict: _OMD) -> None: fp.write(("[%s]\n" % name).encode(defenc)) - values: Sequence[str] # Runtime only gets str in tests, but should be whatever _OMD stores. - v: str + values: List[Any] + v: Any for key, values in section_dict.items_all(): if key == "__name__": continue for v in values: + if v is None: + fp.write(("\t%s\n" % key).encode(defenc)) + continue value = self._value_to_string(v) if any(char in value for char in '\n\t\b\\"#;') or value[:1].isspace() or value[-1:].isspace(): value = value.replace("\\", "\\\\").replace('"', '\\"') @@ -783,11 +797,11 @@ def write_section(name: str, section_dict: _OMD) -> None: for name, value in self._sections.items(): write_section(name, value) - def items(self, section_name: str) -> List[Tuple[str, str]]: # type: ignore[override] + def items(self, section_name: str) -> List[Tuple[str, Union[str, None]]]: # type: ignore[override] """:return: list((option, value), ...) pairs of all items in the given section""" return [(k, v) for k, v in super().items(section_name) if k != "__name__"] - def items_all(self, section_name: str) -> List[Tuple[str, List[str]]]: + def items_all(self, section_name: str) -> List[Tuple[str, List[Union[str, None]]]]: """:return: list((option, [values...]), ...) pairs of all items in the given section""" rv = _OMD(self._defaults) @@ -841,6 +855,8 @@ def write(self) -> None: for key, values in section.items_all(): if key != "__name__": for raw_value in values: + if raw_value is None: + continue if "\r" in self._value_to_string(raw_value) or "\x00" in self._value_to_string(raw_value): raise ValueError("Git config values must not contain CR or NUL") @@ -877,7 +893,6 @@ def read_only(self) -> bool: """:return: ``True`` if this instance may change the configuration file""" return self._read_only - # FIXME: Figure out if default or return type can really include bool. def get_value( self, section: str, @@ -894,7 +909,7 @@ def get_value( did not exist. :return: - A properly typed value, either int, float or string + A properly typed value, either int, float, string or bool :raise TypeError: In case the value could not be understood. @@ -925,7 +940,7 @@ def get_values( in case the option did not exist. :return: - A list of properly typed values, either int, float or string + A list of properly typed values, either int, float, string or bool :raise TypeError: In case the value could not be understood. @@ -941,7 +956,19 @@ def get_values( return [self._string_to_value(valuestr) for valuestr in lst] - def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: + def _convert_to_boolean(self, value: Union[str, None]) -> bool: + if value is None: + return True + if value == "": + return False + try: + return self.BOOLEAN_STATES[value.lower()] + except KeyError: + raise ValueError("Not a boolean: %s" % value) from None + + def _string_to_value(self, valuestr: Union[str, None]) -> Union[int, float, str, bool]: + if valuestr is None: + return "" types = (int, float) for numtype in types: try: diff --git a/test/test_config.py b/test/test_config.py index 3031721de..2910f3e6c 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -864,8 +864,58 @@ def test_empty_config_value(self): assert cr.get_value("core", "filemode"), "Should read keys with values" - with self.assertRaises(cp.NoOptionError): - cr.get_value("color", "ui") + self.assertTrue(cr.has_option("color", "ui")) + self.assertIsNone(cr.get("color", "ui")) + self.assertEqual(cr.get_value("color", "ui"), "") + self.assertIs(cr.getboolean("color", "ui"), True) + + @with_rw_directory + def test_implicit_boolean_round_trip(self, rw_dir): + config_path = osp.join(rw_dir, "config") + with open(config_path, "wb") as config_file: + config_file.write( + b"[include]\n" + b"\toptional\n" + b"[flag]\n" + b"\timplicit\n" + b"\ttrailing-space \n" + b"\ttrailing-tab\t\n" + b"\tempty =\n" + b'\tquoted = ""\n' + b"\tmultiple = false\n" + b"\tmultiple\n" + b"\tmultiple =\n" + b"\tmultiple" + ) + git_config = ["git", "config", "--file", config_path] + original = subprocess.check_output(git_config + ["--null", "--list"]) + + with GitConfigParser(config_path, read_only=False) as config: + for option in ("implicit", "trailing-space", "trailing-tab"): + self.assertIsNone(config.get("flag", option)) + self.assertEqual(config.get_value("flag", option), "") + self.assertIs(config.getboolean("flag", option), True) + for option in ("empty", "quoted"): + self.assertEqual(config.get("flag", option), "") + self.assertEqual(config.get_value("flag", option), "") + self.assertIs(config.getboolean("flag", option), False) + self.assertEqual(config.get_values("flag", "multiple"), [False, "", "", ""]) + self.assertIsNone(dict(config.items("flag"))["multiple"]) + self.assertEqual(dict(config.items_all("flag"))["multiple"], ["false", None, "", None]) + config.set_value("other", "value", "updated") + + self.assertEqual( + subprocess.check_output(git_config + ["--null", "--list"]), + original + b"other.value\nupdated\0", + ) + self.assertEqual( + subprocess.check_output(git_config + ["--type=bool", "--get-all", "flag.multiple"]), + b"false\ntrue\nfalse\ntrue\n", + ) + with GitConfigParser(config_path) as config: + self.assertIs(config.getboolean("flag", "implicit"), True) + self.assertIs(config.getboolean("flag", "empty"), False) + self.assertEqual(dict(config.items_all("flag"))["multiple"], ["false", None, "", None]) def test_config_with_quotes(self): cr = GitConfigParser(fixture_path("git_config_with_quotes"), read_only=True) From 2d4f0683341cb77e75a8656484d3c09a441f9d22 Mon Sep 17 00:00:00 2001 From: Byron Date: Sun, 13 Sep 2026 18:04:41 +0200 Subject: [PATCH 34/92] fix: reject comments after implicit boolean config keys Note that this is just a fixup, on a huge hack which is the native git-config parsing. Let's just hope this holds up until v4. GitConfigParser accepted entries such as "enabled # comment" and "enabled ; comment" even though Git rejects them. The comment became part of the option name, and an equals sign or colon inside the comment could make the entry look like an assignment. Silently stripping the comment would also accept configuration that Git considers invalid. Exclude both comment markers from the shared option-name expression and require a full-line match for valueless options. The assignment pattern cannot cross a comment marker, and the valueless fallback cannot accept just the valid-looking prefix. Such lines now raise the existing ParsingError during reading or an attempted edit. Ordinary bare keys retain their implicit true value and round-trip behavior. Add six regression cases covering both markers, spaces, tabs, adjacent comments, and assignment delimiters inside comments. Compare rejection with git config, check both getboolean and an unrelated edit raise ParsingError, and verify that the failed edit leaves the original bytes untouched. All six cases failed before full-line matching was added. Git reference: checkout 1630431f326e15fcde608827b5ff38422528eb59, config.c:get_value. Without an assignment, that parser requires the line to end after the key and optional whitespace. Runtime comparisons used Git 2.50.1 (Apple Git-155), which rejected all six inputs with exit status 128. Validation on Python 3.12.14: 42 configuration tests and six regression subtests passed, with two existing skips. Ruff lint and formatting and git diff --check passed. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 --- git/config.py | 4 ++-- test/test_config.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/git/config.py b/git/config.py index d6f2706f1..cb130579a 100644 --- a/git/config.py +++ b/git/config.py @@ -310,7 +310,7 @@ class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder): re_comment = re.compile(r"^\s*[#;]") # } END configuration - optvalueonly_source = r"\s*(?P ` with the email in the final angle brackets, so the name ends at the last `` <`` and the line ends at `>`. This reproduces the previous results exactly (verified against the old pattern over ~1.8M fuzzed inputs, including the existing `test_commit_co_authors` cases) while running in linear time: the same crafted input drops from seconds to microseconds. Add a CPU-time regression test that fails on the old code and passes here, and confirm a well-formed trailer on a later line is still parsed. --- git/objects/commit.py | 24 ++++++++++++++++-------- test/test_commit.py | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/git/objects/commit.py b/git/objects/commit.py index 5b73a3432..771f87976 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -10,7 +10,6 @@ from io import BytesIO import logging import os -import re from subprocess import Popen, PIPE from time import altzone, daylight, localtime, time, timezone import warnings @@ -964,12 +963,21 @@ def co_authors(self) -> List[Actor]: co_authors = [] if self.message: - results = re.findall( - r"^Co-authored-by: (.*) <(.*?)>$", - str(self.message), - re.MULTILINE, - ) - for author in results: - co_authors.append(Actor(*author)) + # Scan line by line instead of matching `(.*) <(.*?)>` across the whole + # message. On a single trailer line that repeats " <" without ever closing + # a ">", greedy backtracking over each " <" made the regex run in O(n^2) + # time, so a large (fully attacker-controlled) commit message could stall + # any caller of this property. A trailer is "Co-authored-by: " + # with the email in the final angle brackets, so the name ends at the last + # " <" and the line ends at ">". + prefix = "Co-authored-by: " + for line in str(self.message).split("\n"): + if not line.startswith(prefix) or not line.endswith(">"): + continue + identity = line[len(prefix) :] + separator = identity.rfind(" <") + if separator == -1: + continue + co_authors.append(Actor(identity[:separator], identity[separator + 2 : -1])) return co_authors diff --git a/test/test_commit.py b/test/test_commit.py index 431269b29..b5baf0e33 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -590,6 +590,23 @@ def test_commit_co_authors(self): Actor("test_user_3", "test_user_3@github.com"), ] + def test_commit_co_authors_bounds_malformed_trailer(self): + """A malformed trailer line must not make co_authors run in quadratic time.""" + commit = copy.copy(self.rorepo.commit("4251bd5")) + # An unterminated trailer repeating " <" without a closing ">". The old + # `(.*) <(.*?)>` regex backtracked over every " <" (O(n^2)); the crafted line + # is fully attacker-controlled through the commit message. + commit.message = ( + "Subject\n\nCo-authored-by: " + ("a <" * 20_000) + "\nCo-authored-by: Real Name " + ) + start = time.process_time() + result = commit.co_authors + elapsed = time.process_time() - start + # Leave ample CPU time for slow runners, but catch quadratic backtracking. + self.assertLess(elapsed, 1.0) + # The malformed line yields nothing; the well-formed trailer still parses. + assert result == [Actor("Real Name", "real@example.com")] + @with_rw_directory def test_create_from_tree_with_trailers_dict(self, rw_dir): """Test that create_from_tree supports adding trailers via a dict.""" From 0b64501a081de8018983c3f464c16b36ddd64353 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 20:52:49 +0200 Subject: [PATCH 57/92] refactor(db): deprecate the pure-Python `GitDB` backend rubber-stamp, after checking diff quickly. The pure-Python `GitDB` backend has performance and security issues in object reading and resolution, including unbounded delta processing and incorrect object selection or reconstruction. `GitCmdObjectDB` is already the default and delegates these operations to Git. Emit `DeprecationWarning` from `Repo.__init__` when `odbt` selects `GitDB` or a subclass, directing callers to `GitCmdObjectDB`. Repository opening, initialization, and cloning all construct a `Repo` through this path. Keep the existing backend behavior available for compatibility and retain the `gitdb` dependency needed for shared types and utilities. Deprecation does not repair or disable the legacy parsers. Update backend-selection docstrings, the tutorial, and the changelog with migration guidance. Add regression checks that the deprecated backend and its subclasses warn at the direct caller's location while continuing to read objects, and that default, explicit, and subclassed `GitCmdObjectDB` use remains free of deprecation warnings. CI follow-up: the new deprecation test exposed that backend annotations accepted only `LooseObjectDB` descendants, excluding the supported legacy `GitDB`. Include both types in construction, initialization, and clone helpers. `basedpyright --warnings` and `mypy --python-version=3.12` pass; 16 focused deprecation/database tests and Ruff checks pass. --- doc/source/changes.rst | 9 +++++++++ doc/source/tutorial.rst | 31 ++++++++++++++++++------------- git/repo/base.py | 24 ++++++++++++++++++------ test/deprecation/test_basic.py | 27 +++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index cb5cb9d85..87a1d6619 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -2,6 +2,15 @@ Changelog ========= +Unreleased +========== + +* Deprecate the pure-Python ``GitDB`` object database backend due to security and + performance issues. Selecting it or a subclass through ``odbt`` now emits a + ``DeprecationWarning``. Remove ``odbt=GitDB`` to use ``GitCmdObjectDB``, the + existing default, or select ``odbt=GitCmdObjectDB`` explicitly. The ``gitdb`` + package remains a dependency for shared types and utilities. + 3.2.1 ===== diff --git a/doc/source/tutorial.rst b/doc/source/tutorial.rst index 1edfa4e11..a1ac1eace 100644 --- a/doc/source/tutorial.rst +++ b/doc/source/tutorial.rst @@ -513,24 +513,29 @@ Object Databases The type of the database determines certain performance characteristics, such as the quantity of objects that can be read per second, the resource usage when reading large data files, as well as the average memory footprint of your application. -GitDB -===== -The GitDB is a pure-python implementation of the git object database. It is the default database to use in GitPython 0.3. It uses less memory when handling huge files, but will be 2 to 5 times slower when extracting large quantities of small objects from densely packed repositories:: +GitCmdObjectDB +============== +``GitCmdObjectDB`` is the default and recommended backend. It reads objects and +resolves abbreviated object IDs through persistent ``git cat-file`` processes:: - repo = Repo("path/to/repo", odbt=GitDB) + repo = Repo("path/to/repo") + # Equivalent explicit selection: + repo = Repo("path/to/repo", odbt=GitCmdObjectDB) +GitDB +===== .. warning:: - ``GitDB`` may fail or become extremely slow when traversing trees in - repositories with very large commits (thousands of changed files in a - single commit). If you encounter ``RecursionError`` or excessive - slowness during tree traversal, switch to ``GitCmdObjectDB`` instead. + The pure-Python ``GitDB`` backend is deprecated due to security and performance + issues. Its object parsers can exhaust resources or return incorrect object + data when processing untrusted repositories. Do not use it for untrusted data. +Selecting ``odbt=GitDB`` (including a subclass) emits a ``DeprecationWarning``. +To migrate, remove ``odbt=GitDB`` or replace it with ``odbt=GitCmdObjectDB`` when +opening, initializing, or cloning a repository. The deprecated backend remains +available for compatibility; deprecation does not fix its parsing issues. -GitCmdObjectDB -============== -The git command database uses persistent git-cat-file instances to read repository information. These operate very fast under all conditions, but will consume additional memory for the process itself. When extracting large files, memory usage will be much higher than ``GitDB``:: - - repo = Repo("path/to/repo", odbt=GitCmdObjectDB) +The ``gitdb`` package remains a dependency because GitPython still uses its shared +types and utilities. Git Command Debugging and Customization *************************************** diff --git a/git/repo/base.py b/git/repo/base.py index b58ec5094..175d7cd1b 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -228,7 +228,7 @@ class Repo: def __init__( self, path: Optional[PathLike] = None, - odbt: Type[LooseObjectDB] = GitCmdObjectDB, + odbt: Type[Union[LooseObjectDB, gitdb.GitDB]] = GitCmdObjectDB, search_parent_directories: bool = False, expand_vars: bool = True, ) -> None: @@ -255,7 +255,9 @@ def __init__( :param odbt: Object DataBase type - a type which is constructed by providing the directory containing the database objects, i.e. ``.git/objects``. It will be - used to access all object data. + used to access all object data. The pure-Python ``GitDB`` backend is + deprecated due to security and performance issues. Use the default + :class:`~git.db.GitCmdObjectDB` instead. :param search_parent_directories: If ``True``, all parent directories will be searched for a valid repo as @@ -411,6 +413,13 @@ def __init__( if issubclass(odbt, GitCmdObjectDB): self.odb = odbt(rootpath, self.git) else: + if issubclass(odbt, gitdb.GitDB): + warnings.warn( + "GitDB is deprecated as a GitPython backend due to security and performance issues. " + "Use the default GitCmdObjectDB backend instead.", + DeprecationWarning, + stacklevel=2, + ) self.odb = odbt(rootpath) def __enter__(self) -> "Repo": @@ -1456,7 +1465,7 @@ def init( cls, path: Union[PathLike, None] = None, mkdir: bool = True, - odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, + odbt: Type[Union[LooseObjectDB, gitdb.GitDB]] = GitCmdObjectDB, expand_vars: bool = True, allow_unsafe_options: bool = False, **kwargs: Any, @@ -1476,7 +1485,8 @@ def init( :param odbt: Object DataBase type - a type which is constructed by providing the directory containing the database objects, i.e. ``.git/objects``. It will be - used to access all object data. + used to access all object data. The pure-Python ``GitDB`` backend is + deprecated; use the default :class:`~git.db.GitCmdObjectDB` instead. :param expand_vars: If specified, environment variables will not be escaped. This can lead to @@ -1515,7 +1525,7 @@ def _clone( git: "Git", url: PathLike, path: PathLike, - odb_default_type: Type[LooseObjectDB], + odb_default_type: Type[Union[LooseObjectDB, gitdb.GitDB]], progress: Union["RemoteProgress", "UpdateProgress", Callable[..., "RemoteProgress"], None] = None, multi_options: Optional[List[str]] = None, allow_unsafe_protocols: bool = False, @@ -1639,7 +1649,9 @@ def clone( :param kwargs: * ``odbt`` = ObjectDatabase Type, allowing to determine the object database - implementation used by the returned :class:`Repo` instance. + implementation used by the returned :class:`Repo` instance. The + pure-Python ``GitDB`` backend is deprecated; use the default + :class:`~git.db.GitCmdObjectDB` instead. * All remaining keyword arguments are given to the :manpage:`git-clone(1)` command. diff --git a/test/deprecation/test_basic.py b/test/deprecation/test_basic.py index 3bf0287c7..b905c4d23 100644 --- a/test/deprecation/test_basic.py +++ b/test/deprecation/test_basic.py @@ -17,6 +17,7 @@ import pytest +from git.db import GitCmdObjectDB, GitDB from git.diff import NULL_TREE from git.objects.util import Traversable from git.repo import Repo @@ -60,6 +61,32 @@ def diffs(commit: "Commit") -> Generator["DiffIndex", None, None]: yield commit.diff(NULL_TREE) +def test_gitdb_backend_warns(commit: "Commit") -> None: + class DerivedGitDB(GitDB): + pass + + for backend in (GitDB, DerivedGitDB): + with pytest.deprecated_call(match="GitDB.*deprecated.*GitCmdObjectDB") as caught: + with Repo(commit.repo.working_dir, odbt=backend) as repo: + assert type(repo.odb) is backend + assert repo.head.commit.message == commit.message + assert len(caught) == 1 + assert caught[0].filename == __file__ + + +def test_gitcmdobjectdb_backend_does_not_warn(commit: "Commit") -> None: + class DerivedGitCmdObjectDB(GitCmdObjectDB): + pass + + with assert_no_deprecation_warning(): + with Repo(commit.repo.working_dir) as repo: + assert type(repo.odb) is GitCmdObjectDB + for backend in (GitCmdObjectDB, DerivedGitCmdObjectDB): + with Repo(commit.repo.working_dir, odbt=backend) as repo: + assert type(repo.odb) is backend + assert repo.head.commit.message == commit.message + + def test_diff_renamed_warns(diff: "Diff") -> None: """The deprecated Diff.renamed property issues a deprecation warning.""" with pytest.deprecated_call(): From 3ca35bf483803f1fa29ba675c34c955f61fd00ac Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:15:09 +0200 Subject: [PATCH 58/92] fix(repo): delegate revision message searches to Git rubber-stamp, after checking diff quickly. Python evaluated user-supplied `:/pattern` and `^{/pattern}` searches with a backtracking regex engine and walked/deserialized history itself. A short expression could occupy a worker indefinitely on an ordinary repository (finding 21500). Delegate matching and traversal to `git rev-parse`, using its extended regular expressions, and delete the Python regex/history implementation. Construct fixed-prefix operands so they cannot become options, preserving support for Git versions without `--end-of-options`. Invalid expressions now raise `BadName`, like searches without a match. Resolve revisions before peeling objects in `Repo.commit()` and `Repo.tree()`; appending a peel suffix previously changed top-level search patterns. Add bounded subprocess regressions, native-Git parity cases, and checks that history is not deserialized in Python. Validation: 27 focused/related tests and Ruff checks passed. A 56-case comparison preserved previously successful ordinary revision resolutions. Native behavior was checked against Git and its local source at `d38352cd43`. --- .basedpyright/baseline.json | 16 ----- git/repo/base.py | 6 +- git/repo/fun.py | 111 ++++++--------------------------- test/test_rev_parse.py | 120 ++++++++++++++++++++++++++++++++++-- 4 files changed, 137 insertions(+), 116 deletions(-) diff --git a/.basedpyright/baseline.json b/.basedpyright/baseline.json index 1912255d9..f51f7ce13 100644 --- a/.basedpyright/baseline.json +++ b/.basedpyright/baseline.json @@ -373,22 +373,6 @@ } ], "./git/repo/base.py": [ - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 46, - "lineCount": 1 - } - }, - { - "code": "reportReturnType", - "range": { - "startColumn": 15, - "endColumn": 51, - "lineCount": 1 - } - }, { "code": "reportReturnType", "range": { diff --git a/git/repo/base.py b/git/repo/base.py index 175d7cd1b..3941ce609 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -48,6 +48,7 @@ find_submodule_git_dir, is_git_dir, rev_parse, + to_commit, touch, ) @@ -821,7 +822,7 @@ def commit(self, rev: Union[str, Commit_ish, None] = None) -> Commit: """ if rev is None: return self.head.commit - return self.rev_parse(str(rev) + "^0") + return to_commit(self.rev_parse(str(rev))) def iter_trees(self, *args: Any, **kwargs: Any) -> Iterator["Tree"]: """:return: Iterator yielding :class:`~git.objects.tree.Tree` objects @@ -851,7 +852,8 @@ def tree(self, rev: Union[Tree_ish, str, None] = None) -> "Tree": """ if rev is None: return self.head.commit.tree - return self.rev_parse(str(rev) + "^{tree}") + obj = self.rev_parse(str(rev)) + return obj if obj.type == "tree" else to_commit(obj).tree def iter_commits( self, diff --git a/git/repo/fun.py b/git/repo/fun.py index 817e2f862..611783a15 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -27,7 +27,7 @@ from gitdb.exc import BadName, BadObject from git.cmd import Git -from git.exc import WorkTreeRepositoryUnsupported +from git.exc import GitCommandError, WorkTreeRepositoryUnsupported from git.objects import Object from git.objects.util import parse_date from git.refs import SymbolicReference @@ -35,7 +35,7 @@ # Typing ---------------------------------------------------------------------- -from typing import Iterator, Optional, TYPE_CHECKING, Tuple, Union, cast, overload +from typing import Optional, TYPE_CHECKING, Union, cast, overload from git.types import AnyGitObject, Literal, PathLike @@ -472,100 +472,23 @@ def _find_closing_brace(rev: str, start: int) -> int: raise ValueError("Missing closing brace to define type in %s" % rev) -def _parse_search(pattern: str) -> Tuple[str, bool]: +def _find_commit_by_message(repo: "Repo", rev: Optional[AnyGitObject], pattern: str) -> AnyGitObject: if not pattern: raise ValueError("Revision search requires a pattern") - # END handle empty pattern - - if pattern.startswith("!-"): - return pattern[2:], True - if pattern.startswith("!!"): - return pattern[1:], False - if pattern.startswith("!"): + if pattern.startswith("!") and not pattern.startswith(("!-", "!!")): raise ValueError("Need one character after /!, typically -") - return pattern, False - - -def _unescape_braced_regex(pattern: str) -> str: - out = [] - idx = 0 - while idx < len(pattern): - char = pattern[idx] - if char == "\\" and idx + 1 < len(pattern): - next_char = pattern[idx + 1] - if next_char in "{}\\": - out.append(next_char) - else: - out.append(char) - out.append(next_char) - # END handle escaped char - idx += 2 - continue - # END handle backslash - out.append(char) - idx += 1 - # END for each char - return "".join(out) - -def _find_commit_by_message( - repo: "Repo", rev: Optional[AnyGitObject], pattern: str, braced: bool = False -) -> AnyGitObject: - pattern, negated = _parse_search(_unescape_braced_regex(pattern) if braced else pattern) - try: - regex = re.compile(pattern) - except re.error as e: - raise ValueError("Invalid commit message regex %r" % pattern) from e - # END handle invalid regex - if rev is None: - commits = _all_ref_commits(repo) - else: - commits = _reachable_commits([to_commit(rev)]) - # END handle starting point - - for commit in commits: - message = commit.message - if isinstance(message, bytes): - message = message.decode(commit.encoding, "replace") - # END handle bytes message - matches = regex.search(message or "") is not None - if matches != negated: - return commit - # END found commit - # END for each commit - raise BadName("No commit found matching message pattern %r" % pattern) - - -def _all_ref_commits(repo: "Repo") -> Iterator["Commit"]: - starts = [] - for ref in repo.references: - try: - starts.append(to_commit(ref.object)) - except (BadName, ValueError): - pass - # END skip refs that do not point to commits - # END for each ref + # Git's native regular expressions avoid Python's exponential backtracking, + # and its history walk does not deserialize every visited commit in Python. + search = ":/" + pattern if rev is None else "%s^{/%s}" % (to_commit(rev).hexsha, pattern) try: - starts.append(repo.head.commit) - except ValueError: - pass - # END handle unborn head - return _reachable_commits(starts) - - -def _reachable_commits(starts: list["Commit"]) -> Iterator["Commit"]: - seen = set() - pending = starts[:] - while pending: - pending.sort(key=lambda commit: commit.committed_date, reverse=True) - commit = pending.pop(0) - if commit.binsha in seen: - continue - # END skip seen commit - seen.add(commit.binsha) - yield commit - pending.extend(commit.parents) - # END while commits remain + # The fixed prefix prevents options without requiring Git 2.30's + # --end-of-options support in rev-parse. + hexsha = repo.git.rev_parse("--verify", search) + except GitCommandError as e: + # Git does not distinguish invalid regexes from searches with no match. + raise BadName("No commit found matching message pattern %r" % pattern) from e + return _object_from_hexsha(repo, hexsha) def _index_lookup(repo: "Repo", spec: str) -> AnyGitObject: @@ -597,7 +520,7 @@ def _tree_lookup(obj: AnyGitObject, path: str) -> AnyGitObject: def _peel(obj: AnyGitObject, output_type: str, repo: "Repo", rev: str) -> AnyGitObject: if output_type.startswith("/"): - return _find_commit_by_message(repo, obj, output_type[1:], braced=True) + return _find_commit_by_message(repo, obj, output_type[1:]) if output_type == "": return deref_tag(obj) if obj.type == "tag" else obj if output_type == "object": @@ -654,6 +577,10 @@ def rev_parse(repo: "Repo", rev: str) -> AnyGitObject: :manpage:`git-rev-parse(1)`-compatible revision specification as string. Please see :manpage:`git-rev-parse(1)` for details. + Commit message searches use Git's native extended regular expressions. + Invalid search expressions and searches without a match both raise + :exc:`~gitdb.exc.BadName`. + :raise gitdb.exc.BadObject: If the given revision could not be found. diff --git a/test/test_rev_parse.py b/test/test_rev_parse.py index b00347668..15ee8514e 100644 --- a/test/test_rev_parse.py +++ b/test/test_rev_parse.py @@ -3,15 +3,16 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ +import subprocess +import sys from pathlib import Path import pytest - -from git import Repo -from git.refs import RemoteReference -from git.refs import SymbolicReference from gitdb.exc import BadName +from git import Actor, Commit, Repo +from git.refs import RemoteReference, SymbolicReference + def _write(repo, path, content): full_path = Path(repo.working_tree_dir) / path @@ -140,6 +141,109 @@ def test_rev_parse_commit_message_search(rev_parse_repo): assert repo.rev_parse("HEAD^{/!-release}") == merge +@pytest.mark.parametrize( + "revision", + [ + ":/release[[:space:]]candidate", + "HEAD^{/release{1}}", + "ann^{/root}", + ":/!-release", + "HEAD^{/!-release}", + "HEAD^{/release}^0", + "HEAD^{/release}^{tree}", + ], +) +def test_rev_parse_commit_message_search_matches_git(rev_parse_repo, revision): + repo = rev_parse_repo["repo"] + expected = repo.git.rev_parse("--verify", revision) + assert repo.rev_parse(revision).hexsha == expected + + +@pytest.mark.parametrize("revision", [":/release", "HEAD^{/release}"]) +def test_commit_and_tree_message_search(rev_parse_repo, revision): + repo = rev_parse_repo["repo"] + release = rev_parse_repo["release"] + assert repo.commit(revision) == release + assert repo.tree(revision) == release.tree + + +def test_rev_parse_commit_message_literal_bang_and_option(rev_parse_repo): + repo = rev_parse_repo["repo"] + commit = repo.index.commit("!urgent --all") + assert repo.rev_parse(":/!!urgent") == commit + assert repo.rev_parse("HEAD^{/!!urgent}") == commit + assert repo.rev_parse(":/--all") == commit + assert repo.rev_parse("HEAD^{/--all}") == commit + + +def test_rev_parse_commit_message_escaped_braces(rev_parse_repo): + repo = rev_parse_repo["repo"] + commit = repo.index.commit("literal{3}") + repo.index.commit("literalll") + assert repo.rev_parse(r"HEAD^{/literal\{3\}}") == commit + + +def test_revision_message_search_does_not_backtrack_in_python(tmp_path): + with Repo.init(tmp_path) as repo: + actor = Actor("GitPython Tests", "gitpython@example.com") + repo.index.commit("hello " + "b" * 80, author=actor, committer=actor) + + # Run the adversarial expression in a killable process: Python's regex engine + # can otherwise hold the GIL indefinitely, including against a short message. + subprocess.run( + [ + sys.executable, + "-c", + """ +import sys +from git import Repo +from gitdb.exc import BadName + +with Repo(sys.argv[1]) as repo: + for resolve in (repo.rev_parse, repo.commit, repo.tree): + for revision in (':/(.+)+ZZZ', 'HEAD^{/(.+)+ZZZ}'): + try: + resolve(revision) + except BadName: + pass + else: + raise AssertionError('Unexpected match: ' + revision) +""", + str(tmp_path), + ], + check=True, + capture_output=True, + timeout=5, + ) + + +def test_revision_message_search_does_not_deserialize_history(tmp_path, monkeypatch): + with Repo.init(tmp_path) as repo: + actor = Actor("GitPython Tests", "gitpython@example.com") + commit = repo.index.commit("needle", author=actor, committer=actor) + + def reject_deserialization(*args, **kwargs): + raise AssertionError("Message search must not deserialize commits in Python") + + monkeypatch.setattr(Commit, "_deserialize", reject_deserialization) + assert repo.rev_parse(":/needle") == commit + assert repo.rev_parse("HEAD^{/needle}") == commit + + +def test_commit_and_tree_resolve_before_peeling(rev_parse_repo): + repo = rev_parse_repo["repo"] + root = rev_parse_repo["root"] + merge = rev_parse_repo["merge"] + assert repo.commit("ann") == root + assert repo.tree("ann") == root.tree + assert repo.tree("HEAD:dir") == merge.tree["dir"] + assert repo.tree("HEAD^{tree}") == merge.tree + with pytest.raises(ValueError): + repo.commit("HEAD^{tree}") + with pytest.raises(ValueError): + repo.tree("HEAD:README.md") + + def test_rev_parse_rejects_invalid_object_specs(rev_parse_repo): repo = rev_parse_repo["repo"] @@ -147,10 +251,14 @@ def test_rev_parse_rejects_invalid_object_specs(rev_parse_repo): repo.rev_parse(":") with pytest.raises(ValueError): repo.rev_parse(":/") - with pytest.raises(ValueError): + with pytest.raises(BadName): repo.rev_parse(":/[") - with pytest.raises(ValueError): + with pytest.raises(BadName): repo.rev_parse("HEAD^{/[}") + with pytest.raises(ValueError): + repo.rev_parse(":/!reserved") + with pytest.raises(ValueError): + repo.rev_parse("HEAD^{/!reserved}") with pytest.raises(ValueError): repo.rev_parse("@{-0}") with pytest.raises(ValueError): From c99207e7355bf6f30a42838956d34138d63a49db Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:15:10 +0200 Subject: [PATCH 59/92] fix(refs): bound symbolic reference resolution and updates rubber-stamp, after checking diff quickly. Cyclic loose symbolic refs could keep `dereference_recursive()` reading forever. Related updates and construction of custom symbolic refs could recurse without a useful limit (finding 21496). Bound reads and preflight updates to five references, including the terminal reference, matching Git's `SYMREF_MAXDEPTH`. Determine detachment from the current ref's contents without recursively constructing targets. Reuse validated ref paths for reads to reject escaping symlinks. Preserve public update signatures and subclass dispatch, including HEAD reflog updates and creation of unborn terminal references. The write preflight handles static chains; it does not introduce a transaction for concurrent external repository changes. --- git/refs/symbolic.py | 41 ++++++++--- test/test_refs.py | 168 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 193 insertions(+), 16 deletions(-) diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 7c3eac508..f625b90c7 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -76,6 +76,8 @@ class SymbolicReference: _common_path_default = "" _remote_common_path_default = "refs/remotes" _id_attribute_ = "name" + # Match Git's SYMREF_MAXDEPTH, counting the terminal reference as well. + _max_symref_depth = 5 def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None: self.repo = repo @@ -191,13 +193,18 @@ def dereference_recursive(cls, repo: "Repo", ref_path: Union[PathLike, None]) -> :param repo: The repository containing the reference at `ref_path`. + + :raise ValueError: + If the reference is missing, invalid, or exceeds Git's limit of five + references in a symbolic reference chain (including the terminal ref). """ - while True: + for _ in range(cls._max_symref_depth): hexsha, ref_path = cls._get_ref_info(repo, ref_path) if hexsha is not None: return hexsha # END recursive dereferencing + raise ValueError("Too many levels of symbolic references at %r" % ref_path) @staticmethod def _check_ref_name_valid(ref_path: PathLike) -> None: @@ -258,13 +265,13 @@ def _get_ref_info_helper( * *sha* is of the file at rela_path points to if available, or ``None``. * *target_ref_path* is the reference we point to, or ``None``. """ - if ref_path: - cls._check_ref_name_valid(ref_path) + if ref_path is None: + raise ValueError("Reference at %r does not exist" % ref_path) + ref_file = cls._get_validated_ref_path(repo, ref_path) tokens: Union[None, List[str], Tuple[str, str]] = None - repodir = _git_dir(repo, ref_path) try: - with open(os.path.join(repodir, ref_path), "rt", encoding="UTF-8") as fp: # type: ignore[arg-type] + with open(ref_file, "rt", encoding="UTF-8") as fp: value = fp.read().rstrip() # Don't only split on spaces, but on whitespace, which allows to parse lines like: # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo @@ -398,7 +405,23 @@ def set_object( :return: self + + :raise ValueError: + If the symbolic reference chain exceeds Git's limit of five references. """ + # Validate before recursing through the public method, preserving subclass + # dispatch and Reference's HEAD reflog updates. + ref_path: Union[PathLike, None] = self.path + for _ in range(self._max_symref_depth): + try: + hexsha, ref_path = self._get_ref_info(self.repo, ref_path) + except ValueError: + break # Allow creating an unborn terminal reference. + if hexsha is not None: + break + else: + raise ValueError("Too many levels of symbolic references at %r" % ref_path) + if isinstance(object, SymbolicReference): object = object.object # @ReservedAssignment # END resolve references @@ -584,11 +607,9 @@ def is_detached(self) -> bool: ``True`` if we are a detached reference, hence we point to a specific commit instead to another reference. """ - try: - self.ref # noqa: B018 - return False - except TypeError: - return True + # Inspect only this ref. Constructing its target would recursively inspect + # symbolic references with nonstandard names through from_path(). + return self._get_ref_info(self.repo, self.path)[1] is None def log(self) -> "RefLog": """ diff --git a/test/test_refs.py b/test/test_refs.py index ae3687c30..2edb7b766 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -4,20 +4,23 @@ # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ import contextlib -from itertools import chain import os.path as osp -from pathlib import Path import tempfile +import unittest +from itertools import chain +from pathlib import Path +from unittest.mock import patch from gitdb.exc import BadName +import git.refs as refs from git import ( Commit, GitCommandError, GitConfigParser, Head, - RefLog, Reference, + RefLog, RemoteReference, Repo, SymbolicReference, @@ -25,10 +28,8 @@ ) from git.exc import UnsafeOptionError from git.objects.tag import TagObject -import git.refs as refs from git.util import Actor, rmtree - -from test.lib import TestBase, requires_symlinks, with_rw_repo, PathLikeMock +from test.lib import PathLikeMock, TestBase, requires_symlinks, with_rw_repo class TestRefs(TestBase): @@ -912,3 +913,158 @@ def test_validity_ref_names(self): # Valid reference name should not raise. check_ref("valid/ref/name") + + +class TestSymbolicReferenceSecurity(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.repo = Repo.init(Path(self.tmp.name) / "repo") + self.addCleanup(self.repo.close) + actor = Actor("Reference Test", "reference@example.invalid") + self.commit = self.repo.index.commit("initial", author=actor, committer=actor) + self.next_commit = self.repo.index.commit("next", author=actor, committer=actor, head=False) + + def write_ref(self, path, value): + file = Path(self.repo.git_dir) / path + file.parent.mkdir(parents=True, exist_ok=True) + file.write_text(value + "\n", encoding="utf-8") + + def chain(self, count, terminal=None): + paths = ["HEAD"] + [f"refs/heads/link-{i}" for i in range(1, count)] + for path, target in zip(paths, paths[1:]): + self.write_ref(path, "ref: " + target) + self.write_ref(paths[-1], self.commit.hexsha if terminal is None else terminal) + return paths + + @contextlib.contextmanager + def bounded_reads(self): + # Make the regression fail deterministically instead of hanging on a cycle. + read_ref = SymbolicReference._get_ref_info + reads = 0 + + def read(repo, path): + nonlocal reads + reads += 1 + if reads > 100: + self.fail("Reference traversal exceeded its read budget") + return read_ref(repo, path) + + with patch.object(SymbolicReference, "_get_ref_info", side_effect=read): + yield + + def test_cycles_fail_in_all_object_read_entry_points(self): + for target in ("refs/heads/a", "refs/heads/b"): + self.write_ref("HEAD", "ref: refs/heads/a") + self.write_ref("refs/heads/a", "ref: " + target) + self.write_ref("refs/heads/b", "ref: refs/heads/a") + self.write_ref("refs/tags/loop", "ref: refs/heads/a") + for read in ( + lambda: SymbolicReference.dereference_recursive(self.repo, "HEAD"), + lambda: self.repo.head.commit, + lambda: self.repo.head.object, + lambda: Head(self.repo, "refs/heads/a").commit, + lambda: TagReference(self.repo, "refs/tags/loop").object, + ): + with self.subTest(target=target, read=read), self.bounded_reads(): + with self.assertRaisesRegex(ValueError, "symbolic reference"): + read() + for read in (lambda: self.repo.commit("HEAD"), lambda: self.repo.rev_parse("a")): + with self.subTest(target=target, read=read), self.bounded_reads(): + with self.assertRaises(BadName): + read() + with self.bounded_reads(): + self.assertFalse(self.repo.head.is_valid()) + + def test_depth_limit_matches_git_including_terminal_reference(self): + for count in range(1, 7): + self.chain(count) + with self.subTest(count=count), self.bounded_reads(): + if count <= 5: + self.assertEqual(self.repo.head.commit, self.commit) + self.assertEqual(self.repo.git.rev_parse("--verify", "HEAD"), self.commit.hexsha) + else: + with self.assertRaises(ValueError): + self.repo.head.commit + with self.assertRaises(GitCommandError): + self.repo.git.rev_parse("--verify", "HEAD") + + def test_packed_terminal_reference_resolves(self): + paths = self.chain(5) + (Path(self.repo.git_dir) / paths[-1]).unlink() + self.write_ref("packed-refs", f"{self.commit.hexsha} {paths[-1]}") + self.assertEqual(self.repo.head.commit, self.commit) + + def test_writes_reject_cycles_and_excessive_depth_without_side_effects(self): + for terminal in ("ref: refs/heads/link-1", self.commit.hexsha): + paths = self.chain(6, terminal) + before = {path: (Path(self.repo.git_dir) / path).read_bytes() for path in paths} + head_log = Path(self.repo.git_dir) / "logs" / "HEAD" + old_log = head_log.read_bytes() + for logmsg in (None, "must not write"): + with self.subTest(terminal=terminal, logmsg=logmsg), self.bounded_reads(): + with self.assertRaisesRegex(ValueError, "symbolic reference"): + self.repo.head.set_object(self.next_commit, logmsg) + self.assertEqual(before, {path: (Path(self.repo.git_dir) / path).read_bytes() for path in paths}) + self.assertEqual(head_log.read_bytes(), old_log) + + def test_valid_chain_updates_preserve_symbols_and_head_reflog(self): + paths = self.chain(5) + self.repo.head.set_object(self.next_commit, "updated through chain") + self.assertEqual(self.repo.head.commit, self.next_commit) + for path, target in zip(paths, paths[1:]): + self.assertEqual((Path(self.repo.git_dir) / path).read_text(), "ref: " + target + "\n") + for path in ("HEAD", paths[-1]): + entry = SymbolicReference(self.repo, path).log()[-1] + self.assertEqual(entry.oldhexsha, self.commit.hexsha) + self.assertEqual(entry.newhexsha, self.next_commit.hexsha) + self.assertEqual(entry.message, "updated through chain") + + def test_writes_create_unborn_terminal_reference(self): + paths = self.chain(5) + (Path(self.repo.git_dir) / paths[-1]).unlink() + self.repo.head.set_object(self.next_commit) + self.assertEqual(self.repo.head.commit, self.next_commit) + self.assertEqual(self.repo.head.reference.path, paths[1]) + + def test_set_object_preserves_subclass_override_signature_and_dispatch(self): + calls = [] + + class CustomReference(Reference): + @classmethod + def from_path(cls, repo, path): + return cls(repo, path, check_path=False) + + def set_object(self, object, logmsg=None): + calls.append(self.path) + return super().set_object(object, logmsg) + + paths = self.chain(5) + ref = CustomReference(self.repo, "HEAD", check_path=False) + self.assertIs(ref.set_object(self.next_commit, "custom reference"), ref) + self.assertEqual(calls, paths) + self.assertEqual(self.repo.head.commit, self.next_commit) + self.assertEqual(self.repo.head.log()[-1].message, "custom reference") + + def test_custom_symbolic_reference_names_do_not_recurse_during_construction(self): + self.write_ref("CUSTOM_A", "ref: CUSTOM_B") + self.write_ref("CUSTOM_B", "ref: CUSTOM_A") + with self.bounded_reads(): + ref = SymbolicReference.from_path(self.repo, "CUSTOM_A") + self.assertEqual(ref.reference.path, "CUSTOM_B") + self.assertFalse(ref.is_detached) + self.assertFalse(ref.is_valid()) + with self.assertRaises(ValueError): + ref.set_object(self.next_commit) + + def test_reference_reads_reject_symlinks_outside_repository(self): + outside = Path(self.tmp.name) / "outside" + outside.write_text(self.commit.hexsha + "\n", encoding="utf-8") + path = Path(self.repo.git_dir) / "refs" / "heads" / "external" + try: + path.symlink_to(outside) + except OSError: + self.skipTest("Symlinks are unavailable") + self.write_ref("HEAD", "ref: refs/heads/external") + with self.assertRaisesRegex(ValueError, "escapes the repository"): + self.repo.head.commit From 9e8c85edec50df797aa1f85f97ef01f8809b16c5 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:15:10 +0200 Subject: [PATCH 60/92] fix(cmd): reject the alternate `ext://` helper selector rubber-stamp, after checking diff quickly. The shared unsafe-protocol check rejected forced `::` selectors, but Git also selects `git-remote-ext` for `ext://` URLs. That helper treats the URL as a command path, leaving a second spelling outside the guard. This is a sibling of finding 21492, whose original missing `ls_remote` check was already fixed by `9ad28cc4`. Extend the existing anchored classifier to reject `ext://` too. Preserve native case sensitivity, ordinary IPv6 URLs, forced-helper checks, and the explicit `allow_unsafe_protocols` opt-in across all callers. --- git/cmd.py | 10 ++++++---- test/test_command_guards.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 02e3b157a..321208344 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -646,7 +646,8 @@ class Git(metaclass=_GitMeta): ) # Match Git's leading transport selector, including an empty helper name. - re_unsafe_protocol = re.compile(r"([A-Za-z0-9][A-Za-z0-9+.-]*|)::") + # Git also selects the command-executing ext helper for an ext:// URL. + re_unsafe_protocol = re.compile(r"([A-Za-z0-9][A-Za-z0-9+.-]*|)::|ext://") unsafe_git_ls_remote_options = [ # This option allows arbitrary command execution in git-ls-remote. @@ -946,7 +947,8 @@ def check_unsafe_protocols(cls, url: str) -> None: Apart from the usual protocols (http, git, ssh), Git allows "remote helpers" that have the form ``::
``. One of these helpers (``ext::``) - can be used to invoke any arbitrary command. + can be used to invoke any arbitrary command. Git also selects that helper + for ``ext://`` URLs and interprets the URL as a command path. See: @@ -955,9 +957,9 @@ def check_unsafe_protocols(cls, url: str) -> None: """ match = cls.re_unsafe_protocol.match(url) if match: - protocol = match.group(1) + protocol = match.group(0) raise UnsafeProtocolError( - f"The `{protocol}::` protocol looks suspicious, use `allow_unsafe_protocols=True` to allow it." + f"The `{protocol}` protocol looks suspicious, use `allow_unsafe_protocols=True` to allow it." ) @classmethod diff --git a/test/test_command_guards.py b/test/test_command_guards.py index 1782231cb..e53b66844 100644 --- a/test/test_command_guards.py +++ b/test/test_command_guards.py @@ -36,6 +36,29 @@ def test_ls_remote_rejects_unsafe_protocols(args, kwargs, allow_unsafe_options): run.assert_not_called() +@pytest.mark.parametrize("through_repo", (False, True)) +@pytest.mark.parametrize("remote", ("ext::helper", "ext://helper")) +def test_ls_remote_protocol_guard_at_both_entry_points(tmp_path, through_repo, remote): + with Repo.init(tmp_path) as repo: + command = repo.git if through_repo else Git() + with mock.patch.object(Git, "execute", return_value="refs") as execute: + for allow_unsafe_options in (False, True): + with pytest.raises(UnsafeProtocolError): + command.ls_remote(remote, allow_unsafe_options=allow_unsafe_options) + execute.assert_not_called() + + url = "ssh://git@[2001:db8::1]/repo.git" + assert command.ls_remote(url) == "refs" + execute.assert_called_once_with([Git.GIT_PYTHON_GIT_EXECUTABLE, "ls-remote", url]) + execute.reset_mock() + + with pytest.raises(UnsafeOptionError): + command.ls_remote(remote, upload_pack="helper", allow_unsafe_protocols=True) + execute.assert_not_called() + assert command.ls_remote(remote, allow_unsafe_protocols=True) == "refs" + execute.assert_called_once_with([Git.GIT_PYTHON_GIT_EXECUTABLE, "ls-remote", remote]) + + @pytest.mark.parametrize( "args, kwargs", [ From 428d20501b3c5cf3d128b71584f6325b9f243841 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:15:11 +0200 Subject: [PATCH 61/92] fix(repo): validate rendered archive remote options rubber-stamp, after checking diff quickly. `Repo.archive()` checked only the original `remote` keyword. Git accepts abbreviated long options, while GitPython renders sequence values as repeated options and permits a value embedded in the keyword name. Those spellings could bypass unsafe-protocol validation (finding 21492's broader URL-validation class). Inspect the arguments produced by the existing `Git.transform_kwargs()` and validate every emitted `--remote` value, including its abbreviations, with the shared protocol checker. Preserve safe repeated IPv6 URLs and keep unsafe-option and unsafe-protocol opt-ins independent. --- git/repo/base.py | 10 ++++--- test/test_repo.py | 66 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/git/repo/base.py b/git/repo/base.py index 3941ce609..c39523344 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -1778,9 +1778,13 @@ def archive( treeish = self.head.commit if prefix and "prefix" not in kwargs: kwargs["prefix"] = prefix - remote = kwargs.get("remote") - if not allow_unsafe_protocols and remote is not None: - Git.check_unsafe_protocols(str(remote)) + if not allow_unsafe_protocols: + # Check the emitted URL, including repeated values and Git's long-option + # abbreviations, rather than only the untransformed `remote` keyword. + for arg in self.git.transform_kwargs(**kwargs): + option, separator, remote = arg.partition("=") + if separator and option.startswith("--r") and "--remote".startswith(option): + Git.check_unsafe_protocols(remote) if not allow_unsafe_options: Git.check_unsafe_options( options=Git._option_candidates([], kwargs), diff --git a/test/test_repo.py b/test/test_repo.py index b2740dfae..ad05ff97f 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -6,7 +6,6 @@ import gc import glob import io -from io import BytesIO import itertools import os import os.path as osp @@ -14,8 +13,9 @@ import pickle import sys import tempfile -from unittest import mock +from io import BytesIO from pathlib import Path +from unittest import mock import pytest @@ -37,14 +37,10 @@ Submodule, Tree, ) -from git.exc import UnsafeOptionError -from git.exc import UnsafeProtocolError -from git.exc import BadObject -from git.exc import WorkTreeRepositoryUnsupported +from git.exc import BadObject, UnsafeOptionError, UnsafeProtocolError, WorkTreeRepositoryUnsupported from git.repo.fun import find_worktree_git_dir, touch from git.util import bin_to_hex, cwd, cygpath, join_path_native, rmfile, rmtree - -from test.lib import TestBase, fixture, requires_symlinks, with_rw_directory, with_rw_repo, PathLikeMock +from test.lib import PathLikeMock, TestBase, fixture, requires_symlinks, with_rw_directory, with_rw_repo def iter_flatten(lol): @@ -1672,3 +1668,57 @@ def test_ignored_raises_error_w_symlink(self): with pytest.raises(GitCommandError): temp_repo.ignored(tmp_dir / "symlink/file.txt") + + +@pytest.mark.parametrize("allow_unsafe_options", (False, True)) +@pytest.mark.parametrize( + "kwargs", + ( + {"rem": "ext::helper"}, + {"re": "ext::helper"}, + {"remo": "ext::helper"}, + {"remot": "ext::helper"}, # codespell:ignore remot + {"remote": ["https://example.com/repo", "ext::helper"]}, + {"remote": (None, False, "ext::helper")}, + {"remote=ext::helper": True}, + {"r=ext::helper": True}, + {"remote": "ext://helper"}, + ), +) +def test_archive_protocol_guard_checks_emitted_remote_options(tmp_path, kwargs, allow_unsafe_options): + with Repo.init(tmp_path) as repo, mock.patch.object(Git, "execute") as execute: + with pytest.raises(UnsafeProtocolError): + repo.archive(BytesIO(), "HEAD", allow_unsafe_options=allow_unsafe_options, **kwargs) + execute.assert_not_called() + + +def test_archive_preserves_safe_repeated_remote_options(tmp_path): + urls = ["https://[::1]/repo.git", "ssh://git@[2001:db8::1]/repo.git"] + with Repo.init(tmp_path) as repo, mock.patch.object(Git, "execute") as execute: + output = BytesIO() + repo.archive(output, "HEAD", rem=urls) + execute.assert_called_once_with( + [Git.GIT_PYTHON_GIT_EXECUTABLE, "archive", *(f"--rem={url}" for url in urls), "--", "HEAD"], + output_stream=output, + ) + + +def test_archive_protocol_and_option_opt_ins_are_independent(tmp_path): + with Repo.init(tmp_path) as repo, mock.patch.object(Git, "execute") as execute: + with pytest.raises(UnsafeOptionError): + repo.archive(BytesIO(), "HEAD", rem=["ext::helper"], exec="helper", allow_unsafe_protocols=True) + execute.assert_not_called() + + output = BytesIO() + repo.archive( + output, + "HEAD", + rem=["ext::helper"], + exec="helper", + allow_unsafe_options=True, + allow_unsafe_protocols=True, + ) + execute.assert_called_once_with( + [Git.GIT_PYTHON_GIT_EXECUTABLE, "archive", "--rem=ext::helper", "--exec=helper", "--", "HEAD"], + output_stream=output, + ) From a75aedf3dccc83b190b673f114d70f83a5fbeb27 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:16:39 +0200 Subject: [PATCH 62/92] fix(index): validate paths at native index boundaries rubber-stamp, after checking diff quickly. Native tree merging and index serialization could install paths that Git's own index writers reject. A subsequent checkout trusts that index and can write outside the working tree or into Git metadata (finding 21506 and the shared write-side cause of finding 21501). Validate complete paths when importing tree entries, reading or writing index entries, and building trees from the index. Reject traversal, absolute/drive paths, embedded NULs, and Git metadata aliases on NTFS/HFS while retaining ordinary POSIX filename characters. Validate before creating trees, and preserve index writer rollback on failure. Refresh the on-disk index before checkout, including when entries were cached. Parse full sentinel-length names and check their terminators and padding so validation sees the same paths as Git. Reject unsupported mandatory extensions such as split indexes: validating only their inline entries would overlook paths from external shared indexes. QA review follow-up: reject unsupported index versions with an explicit exception, preserving `AssertionError` for compatibility. Python's `-O` mode removed the former assertion, allowing version 4 prefix-compressed paths to be parsed with version 2/3 rules before native checkout. Validation must never accept a format whose paths Git interprets differently. Optimized subprocess regressions for unsupported versions fail before this fix and pass afterward. All 76 selected index tests pass, with two platform skips; locked Basedpyright, mypy, Codespell, and Ruff checks pass. --- git/index/base.py | 3 + git/index/fun.py | 48 +++++++++++--- git/util.py | 28 ++++++++ test/test_index.py | 155 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 209 insertions(+), 25 deletions(-) diff --git a/git/index/base.py b/git/index/base.py index c8aa9dd43..11995edf0 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -1390,6 +1390,9 @@ def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLik # END stderr handler + # Read and validate the index before Git trusts its paths for checkout. + self._delete_entries_cache() + self.entries # noqa: B018 if paths is None: args.append("--all") kwargs["as_process"] = 1 diff --git a/git/index/fun.py b/git/index/fun.py index 45c18ace4..929038f1f 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -34,7 +34,7 @@ traverse_trees_recursive, tree_to_stream, ) -from git.util import IndexFileSHA1Writer, finalize_process +from git.util import IndexFileSHA1Writer, finalize_process, _validate_repo_path from .typ import CE_EXTENDED, BaseIndexEntry, IndexEntry, CE_NAMEMASK, CE_STAGESHIFT from .util import pack, unpack @@ -279,13 +279,13 @@ def write_cache( # Body for entry in entries: + _validate_repo_path(entry.path) beginoffset = tell() write(entry.ctime_bytes) # ctime write(entry.mtime_bytes) # mtime - path_str = str(entry.path) + path_str = os.fspath(entry.path) path: bytes = force_bytes(path_str, encoding=defenc) - plen = len(path) & CE_NAMEMASK # Path length - assert plen == len(path), "Path %s too long to fit into index" % entry.path + plen = min(len(path), CE_NAMEMASK) # Longer names use a sentinel. flags = plen | (entry.flags & CE_NAMEMASK_INV) # Clear possible previous values. if entry.extended_flags: flags |= CE_EXTENDED @@ -325,7 +325,8 @@ def read_header(stream: IO[bytes]) -> Tuple[int, int]: unpacked = cast(Tuple[int, int], unpack(">LL", stream.read(4 * 2))) version, num_entries = unpacked - assert version in (1, 2, 3), "Unsupported git index version %i, only 1, 2, and 3 are supported" % version + if version not in (1, 2, 3): + raise AssertionError("Unsupported git index version %i, only 1, 2, and 3 are supported" % version) return version, num_entries @@ -382,10 +383,23 @@ def read_cache( if flags & CE_EXTENDED: extended_flags = unpack(">H", read(2))[0] path_size = flags & CE_NAMEMASK - path = read(path_size).decode(defenc) - - real_size = (tell() - beginoffset + 8) & ~7 - read((beginoffset + real_size) - tell()) + path_bytes = bytearray(read(path_size)) + if len(path_bytes) != path_size: + raise ValueError("Truncated index entry path") + terminator = read(1) + if path_size == CE_NAMEMASK: + while terminator and terminator != b"\0": + path_bytes.extend(terminator) + terminator = read(1) + if terminator != b"\0": + raise ValueError("Unterminated index entry path") + path = path_bytes.decode(defenc) + _validate_repo_path(path) + + real_size = (tell() - beginoffset + 7) & ~7 + padding_size = beginoffset + real_size - tell() + if read(padding_size) != b"\0" * padding_size: + raise ValueError("Invalid index entry padding") entry = IndexEntry((mode, sha, flags, path, ctime, mtime, dev, ino, uid, gid, size, extended_flags)) # entry_key would be the method to use, but we save the effort. entries[(path, entry.stage)] = entry @@ -408,6 +422,18 @@ def read_cache( # Truncate the sha in the end as we will dynamically create it anyway. extension_data = extension_data[:-20] + offset = 0 + while offset < len(extension_data): + header = extension_data[offset : offset + 8] + if len(header) != 8: + raise ValueError("Truncated index extension header") + signature, size = unpack(">4sL", header) + if not b"A" <= signature[:1] <= b"Z": + raise ValueError("Unsupported mandatory index extension %r" % signature) + offset += 8 + size + if offset > len(extension_data): + raise ValueError("Truncated index extension %r" % signature) + return (version, entries, extension_data, content_sha) @@ -434,6 +460,9 @@ def write_tree_from_cache( A tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name. """ + if si == 0: + for entry in entries[sl]: + _validate_repo_path(entry.path) tree_items: List["TreeCacheTup"] = [] ci = sl.start @@ -481,6 +510,7 @@ def write_tree_from_cache( def _tree_entry_to_baseindexentry(tree_entry: "TreeCacheTup", stage: int) -> BaseIndexEntry: + _validate_repo_path(tree_entry[2]) return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) diff --git a/git/util.py b/git/util.py index 431fb5d2a..6f1f38401 100644 --- a/git/util.py +++ b/git/util.py @@ -34,6 +34,7 @@ from functools import wraps import getpass import logging +import ntpath import os import os.path as osp from pathlib import Path @@ -380,6 +381,33 @@ def _to_relative_path(root: PathLike, path: PathLike) -> str: return relative_path +_HFS_IGNORABLES = str.maketrans( + "", "", "\u200c\u200d\u200e\u200f\u202a\u202b\u202c\u202d\u202e\u206a\u206b\u206c\u206d\u206e\u206f\ufeff" +) + + +def _validate_repo_path(path: PathLike) -> None: + """Reject unsafe tree/index paths without normalizing away their components. + + Protect Git metadata aliases on NTFS and HFS even when writing on another + platform. Other POSIX filename characters, including newlines, remain valid. + """ + name = os.fspath(path) + if not name or "\0" in name or ntpath.splitdrive(name)[0] or name.startswith("/"): + raise ValueError("Invalid repository path %r" % name) + if os.name == "nt" and "\\" in name: + raise ValueError("Index paths must use '/' separators: %r" % name) + for component in name.split("/"): + if component in ("", ".", ".."): + raise ValueError("Invalid repository path %r" % name) + # NTFS recognizes backslashes and alternate data streams in metadata names. + for part in component.split("\\"): + ntfs_name = part.split(":", 1)[0].rstrip(" .").lower() + hfs_name = part.translate(_HFS_IGNORABLES).lower() + if ntfs_name in (".git", "git~1") or hfs_name == ".git": + raise ValueError("Repository path aliases Git metadata: %r" % name) + + def assure_directory_exists(path: PathLike, is_file: bool = False) -> bool: """Make sure that the directory pointed to by path exists. diff --git a/test/test_index.py b/test/test_index.py index 302951402..343cc19e7 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -4,26 +4,28 @@ # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ import contextlib -from dataclasses import dataclass -from io import BytesIO import logging import os import os.path as osp -from pathlib import Path import re import shutil -from stat import S_ISLNK, ST_MODE +import struct import subprocess import sys import tempfile +from dataclasses import dataclass +from hashlib import sha1 +from io import BytesIO +from itertools import product +from pathlib import Path +from stat import S_ISLNK, ST_MODE from unittest import mock -from gitdb.base import IStream - import ddt import pytest +from gitdb.base import IStream -from git import BlobFilter, Diff, Git, IndexFile, NULL_TREE, Object, Repo, Tree +from git import NULL_TREE, BlobFilter, Diff, Git, IndexFile, Object, Repo, Tree from git.diff import NULL_TREE_SHA from git.exc import ( CheckoutError, @@ -33,13 +35,12 @@ UnmergedEntriesError, UnsafeOptionError, ) -from git.index.fun import _git_for_windows_bash, _which_from_path, hook_path, run_commit_hook +from git.index.fun import _git_for_windows_bash, _which_from_path, hook_path, read_cache, run_commit_hook, write_cache from git.index.typ import BaseIndexEntry, IndexEntry from git.index.util import TemporaryFileSwap from git.objects import Blob from git.util import Actor, cwd, hex_to_bin, rmtree - -from test.lib import TestBase, VirtualEnvironment, fixture, fixture_path, with_rw_directory, with_rw_repo, PathLikeMock +from test.lib import PathLikeMock, TestBase, VirtualEnvironment, fixture, fixture_path, with_rw_directory, with_rw_repo from test.lib.helper import symlinks_supported, xfail_if_raises HOOKS_SHEBANG = "#!/usr/bin/env sh\n" @@ -188,6 +189,15 @@ def _make_hook(git_dir, name, content, make_exec=True): return hp +def _raw_index(path): + """Build an index without using the writer under test.""" + name = path.encode("utf-8") + entry = struct.pack(">10L20sH", 0, 0, 0, 0, 0, 0, 0o100644, 0, 0, 0, b"a" * 20, min(len(name), 0xFFF)) + name + entry += b"\0" * (8 - len(entry) % 8) + data = b"DIRC" + struct.pack(">LL", 2, 1) + entry + return data + sha1(data).digest() + + @ddt.ddt class TestIndex(TestBase): @with_rw_repo("HEAD") @@ -303,6 +313,54 @@ def test_index_file_base(self): self.assertEqual(fp.read(), fixture("index_merge")) os.remove(tmpfile) + @ddt.data( + "", + ".", + "..", + "../outside", + "a/../outside", + "/absolute", + "C:relative", + "a//b", + "a/./b", + "a/", + "nul\0name", + ".git/config", + "a/.GiT/hooks/hook", + "git~1/config", + ".git. /config", + ".git:stream", + ".g\u200cit/config", + "a\\.git\\config", + ) + def test_index_reader_and_writer_reject_unsafe_paths(self, path): + with pytest.raises(ValueError): + read_cache(BytesIO(_raw_index(path))) + entry = IndexEntry((0o100644, b"a" * 20, 0, path)) + with pytest.raises(ValueError): + write_cache([entry], BytesIO()) + + def test_valid_unusual_index_names_round_trip(self): + names = ["a b", "a\nb", "a\tb", "name:value", "dir/.gitignore", "café"] + if os.name != "nt": + names.append("a\\b") + entries = [IndexEntry((0o100644, b"a" * 20, 0, name)) for name in sorted(names)] + stream = BytesIO() + write_cache(entries, stream) + stream.seek(0) + assert [entry.path for entry in read_cache(stream)[1].values()] == sorted(names) + + def test_long_index_names_are_fully_validated(self): + prefix = "a/" + "nested/" * 650 + with pytest.raises(ValueError): + read_cache(BytesIO(_raw_index(prefix + "../outside"))) + name = prefix + "file" + assert next(iter(read_cache(BytesIO(_raw_index(name)))[1])) == (name, 0) + stream = BytesIO() + write_cache([IndexEntry((0o100644, b"a" * 20, 0, name))], stream) + stream.seek(0) + assert next(iter(read_cache(stream)[1])) == (name, 0) + def _cmp_tree_index(self, tree, index): # Fail unless both objects contain the same paths and blobs. if isinstance(tree, str): @@ -637,6 +695,30 @@ def _count_existing(self, repo, files): # END num existing helper + @ddt.data("write", "write_tree", "checkout", "cached_checkout") + @with_rw_directory + def test_index_boundaries_reject_injected_entries_before_side_effects(self, rw_dir, operation): + tmp_path = Path(rw_dir) / "repo" + with Repo.init(tmp_path) as repo: + index_path = Path(repo.index.path) + if operation == "checkout": + index_path.write_bytes(_raw_index("../outside")) + else: + repo.index.write() + before = index_path.read_bytes() + index = repo.index + if operation == "cached_checkout": + assert not index.entries + index_path.write_bytes(_raw_index("../outside")) + before = index_path.read_bytes() + operation = "checkout" + elif operation != "checkout": + index.entries[("../outside", 0)] = IndexEntry((0o100644, b"a" * 20, 0, "../outside")) + with pytest.raises(ValueError): + getattr(index, operation)() + assert index_path.read_bytes() == before + assert not (tmp_path.parent / "outside").exists() + @with_rw_repo("0.1.6") def test_index_mutation(self, rw_repo): with xfail_if_raises( @@ -1035,6 +1117,20 @@ def test_index_new(self): assert isinstance(index, IndexFile) # END for each arg tuple + @ddt.data(*product(("../outside", ".git/hooks/pre-commit", "a/.GIT/config"), (1, 2, 3))) + @ddt.unpack + @with_rw_directory + def test_native_tree_merge_rejects_unsafe_paths(self, rw_dir, path, tree_count): + tmp_path = Path(rw_dir) + with Repo.init(tmp_path) as repo: + blob = repo.odb.store(IStream("blob", 4, BytesIO(b"data"))).binsha + data = b"100755 " + path.encode() + b"\0" + blob + tree = repo.odb.store(IStream("tree", len(data), BytesIO(data))).binsha + empty = repo.odb.store(IStream("tree", 0, BytesIO())).binsha + with pytest.raises(ValueError): + IndexFile.new(repo, *([empty] * (tree_count - 1) + [tree])) + assert not (tmp_path / ".git" / "index").exists() + @with_rw_repo("HEAD", bare=True) def test_index_bare_add(self, rw_bare_repo): # Something is wrong after cloning to a bare repo, reading the property @@ -1433,27 +1529,27 @@ def test_commit_msg_hook_fail(self, rw_repo): @with_rw_repo("HEAD") def test_index_add_pathlib(self, rw_repo): - git_dir = Path(rw_repo.git_dir) + worktree = Path(rw_repo.working_tree_dir) - file = git_dir / "file.txt" + file = worktree / "file.txt" file.touch() rw_repo.index.add(file) @with_rw_repo("HEAD") def test_index_add_pathlike(self, rw_repo): - git_dir = Path(rw_repo.git_dir) + worktree = Path(rw_repo.working_tree_dir) - file = git_dir / "file.txt" + file = worktree / "file.txt" file.touch() rw_repo.index.add(PathLikeMock(str(file))) @with_rw_repo("HEAD") def test_index_add_non_normalized_path(self, rw_repo): - git_dir = Path(rw_repo.git_dir) + worktree = Path(rw_repo.working_tree_dir) - file = git_dir / "file.txt" + file = worktree / "file.txt" file.touch() non_normalized_path = file.as_posix() if os.name != "nt": @@ -1461,6 +1557,33 @@ def test_index_add_non_normalized_path(self, rw_repo): rw_repo.index.add(non_normalized_path) + @ddt.data(0, 4, 5) + def test_unsupported_index_versions_fail_even_with_optimization(self, version): + data = b"DIRC" + struct.pack(">LL", version, 0) + data += sha1(data).digest() + with pytest.raises(AssertionError, match="Unsupported git index version"): + read_cache(BytesIO(data)) + code = """ +from io import BytesIO +import sys +from git.index.fun import read_cache +try: + read_cache(BytesIO(sys.stdin.buffer.read())) +except AssertionError as error: + if "Unsupported git index version" not in str(error): + raise +else: + raise SystemExit("Unsupported index version was accepted") +""" + result = subprocess.run([sys.executable, "-O", "-c", code], input=data, capture_output=True, timeout=10) + assert result.returncode == 0, result.stderr.decode() + + @ddt.data(b"link", b"sdir") + def test_unsupported_mandatory_index_extensions_fail_closed(self, signature): + data = b"DIRC" + struct.pack(">LL", 2, 0) + signature + struct.pack(">L", 0) + with pytest.raises(ValueError, match="extension"): + read_cache(BytesIO(data + sha1(data).digest())) + def test_index_file_v3(self): index = IndexFile(self.rorepo, fixture_path("index_extended_flags")) assert index.entries From d1576c4bba3d962b165bab51bda195c8aad5816b Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:26:35 +0200 Subject: [PATCH 63/92] fix(tree): validate entry names and record boundaries rubber-stamp, after checking diff quickly. `TreeModifier.add()` checked only for slashes in names. Embedded NULs could change serialized record boundaries, while dot components and Git metadata aliases produced unsafe tree paths (finding 21507). Reuse the repository path validator for single-component tree names and check both the public modifier and shared serializer. Validate object ID lengths and object modes at serialization, including entries inserted through unchecked or direct-cache APIs. Validate incoming records in `tree_entries_from_data()` as well: use bounded delimiter searches, require octal modes and complete 20-byte object IDs, and reject invalid names before exposing entries. Preserve `bytearray` inputs, immutable returned object IDs, undecodable filenames, and valid whitespace, Unicode, and POSIX backslash and colon names. Check drive/root restrictions on complete paths, not isolated tree components. Update the existing decoding fixture to contain a complete object ID. --- git/objects/fun.py | 72 ++++++++++++++++++++------------------------- git/objects/tree.py | 5 ++-- test/test_fun.py | 27 +++++++++++++---- test/test_tree.py | 33 +++++++++++++++++++-- 4 files changed, 86 insertions(+), 51 deletions(-) diff --git a/git/objects/fun.py b/git/objects/fun.py index 7ef229990..ff1bc8483 100644 --- a/git/objects/fun.py +++ b/git/objects/fun.py @@ -13,6 +13,7 @@ from stat import S_ISDIR from git.compat import safe_decode, defenc +from git.util import _validate_repo_path # typing ---------------------------------------------- @@ -38,6 +39,13 @@ # --------------------------------------------------- +def _validate_tree_entry_name(name: str) -> None: + if "/" in name: + raise ValueError("Tree entry names must not contain '/' characters") + # A tree name is a component, not a rooted path; a colon cannot select a drive. + _validate_repo_path("tree/" + name) + + def tree_to_stream(entries: Sequence[EntryTup], write: Callable[["ReadableBuffer"], Union[int, None]]) -> None: """Write the given list of entries into a stream using its ``write`` method. @@ -51,6 +59,10 @@ def tree_to_stream(entries: Sequence[EntryTup], write: Callable[["ReadableBuffer bit_mask = 7 # 3 bits set. for binsha, mode, name in entries: + if len(binsha) != 20: + raise ValueError("Tree entry object IDs must be exactly 20 bytes") + if mode >> 12 not in (4, 8, 10, 14): + raise ValueError("Invalid tree entry mode") mode_str = b"" for i in range(6): mode_str = bytes([((mode >> (i * 3)) & bit_mask) + ord_zero]) + mode_str @@ -70,13 +82,13 @@ def tree_to_stream(entries: Sequence[EntryTup], write: Callable[["ReadableBuffer name_bytes = name.encode(defenc) else: name_bytes = name # type: ignore[unreachable] # check runtime types - is always str? + _validate_tree_entry_name(safe_decode(name_bytes)) write(b"".join((mode_str, b" ", name_bytes, b"\0", binsha))) # END for each item def tree_entries_from_data(data: bytes) -> List[EntryTup]: - """Read the binary representation of a tree and returns tuples of - :class:`~git.objects.tree.Tree` items. + """Read complete tree records, rejecting invalid names and truncated fields. :param data: Data block with tree data (as bytes). @@ -84,45 +96,25 @@ def tree_entries_from_data(data: bytes) -> List[EntryTup]: :return: list(tuple(binsha, mode, tree_relative_path), ...) """ - ord_zero = ord("0") - space_ord = ord(" ") - len_data = len(data) - i = 0 out = [] - while i < len_data: - mode = 0 - - # Read Mode - # Some git versions truncate the leading 0, some don't. - # The type will be extracted from the mode later. - while data[i] != space_ord: - # Move existing mode integer up one level being 3 bits and add the actual - # ordinal value of the character. - mode = (mode << 3) + (data[i] - ord_zero) - i += 1 - # END while reading mode - - # Byte is space now, skip it. - i += 1 - - # Parse name, it is NULL separated. - - ns = i - while data[i] != 0: - i += 1 - # END while not reached NULL - - # Default encoding for strings in git is UTF-8. - # Only use the respective unicode object if the byte stream was encoded. - name_bytes = data[ns:i] - name = safe_decode(bytes(name_bytes)) - - # Byte is NULL, get next 20. - i += 1 - sha = bytes(data[i : i + 20]) - i = i + 20 - out.append((sha, mode, name)) - # END for each byte in data stream + offset = 0 + while offset < len(data): + mode_end = data.find(b" ", offset) + if mode_end < 0: + raise ValueError("Unterminated tree entry mode") + mode_bytes = data[offset:mode_end] + if not mode_bytes or mode_bytes.strip(b"01234567"): + raise ValueError("Invalid tree entry mode") + mode = int(mode_bytes, 8) + if mode >> 12 not in (4, 8, 10, 14): + raise ValueError("Invalid tree entry mode") + name_end = data.find(b"\0", mode_end + 1) + if name_end < 0 or name_end + 21 > len(data): + raise ValueError("Truncated tree entry") + name = safe_decode(bytes(data[mode_end + 1 : name_end])) + _validate_tree_entry_name(name) + offset = name_end + 21 + out.append((bytes(data[name_end + 1 : offset]), mode, name)) return out diff --git a/git/objects/tree.py b/git/objects/tree.py index 33ce0f907..d2a5859df 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -13,7 +13,7 @@ from . import util from .base import IndexObjUnion, IndexObject from .blob import Blob -from .fun import tree_entries_from_data, tree_to_stream +from .fun import tree_entries_from_data, tree_to_stream, _validate_tree_entry_name from .submodule.base import Submodule # typing ------------------------------------------------- @@ -110,8 +110,7 @@ def add(self, sha: bytes, mode: int, name: str, force: bool = False) -> "TreeMod :return: self """ - if "/" in name: - raise ValueError("Name must not contain '/' characters") + _validate_tree_entry_name(name) if (mode >> 12) not in Tree._map_id_to_type: raise ValueError("Invalid object type according to mode %o" % mode) diff --git a/test/test_fun.py b/test/test_fun.py index 8d594915a..3a4abe110 100644 --- a/test/test_fun.py +++ b/test/test_fun.py @@ -1,11 +1,12 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ +import os.path as osp from io import BytesIO -from stat import S_IFDIR, S_IFLNK, S_IFREG, S_IXUSR from os import stat -import os.path as osp +from stat import S_IFDIR, S_IFLNK, S_IFREG, S_IXUSR +import ddt from gitdb.base import IStream from gitdb.typ import str_tree_type @@ -20,10 +21,10 @@ ) from git.repo.fun import find_worktree_git_dir from git.util import bin_to_hex, cygpath, join_path_native - from test.lib import TestBase, with_rw_directory, with_rw_repo +@ddt.ddt class TestFun(TestBase): def _assert_index_entries(self, entries, trees): index = IndexFile.from_tree(self.rorepo, *[self.rorepo.tree(bin_to_hex(t).decode("ascii")) for t in trees]) @@ -304,9 +305,25 @@ def test_linked_worktree_traversal(self, rw_dir): self.assertTrue(statbuf.st_mode & S_IFDIR) def test_tree_entries_from_data_with_failing_name_decode(self): - r = tree_entries_from_data(b"100644 \x9f\0aaa") - assert r == [(b"aaa", 33188, "\udc9f")], r + r = tree_entries_from_data(b"100644 \x9f\0" + b"a" * 20) + assert r == [(b"a" * 20, 33188, "\udc9f")], r def test_tree_entries_from_bytearray(self): r = tree_entries_from_data(bytearray(b"100644 name\0abcdefghijklmnopqrst")) assert r == [(b"abcdefghijklmnopqrst", 33188, "name")], r + assert isinstance(r[0][0], bytes) + + @ddt.data(b"", b".", b"..", b".git", b"a/b") + def test_tree_reader_rejects_paths_that_cannot_be_tree_components(self, name): + with self.assertRaises(ValueError): + tree_entries_from_data(b"100644 " + name + b"\0" + b"a" * 20) + + @ddt.data(b"100644", b"100644 missing-nul", b"100644 name\0short", b"xyz name\0" + b"a" * 20) + def test_malformed_tree_records_fail_cleanly(self, data): + with self.assertRaises(ValueError): + tree_entries_from_data(data) + + @ddt.data(b"", b"a" * 19, b"a" * 20 + b"100644 extra\0" + b"b" * 20) + def test_tree_serializer_rejects_wrong_length_object_ids(self, sha): + with self.assertRaises(ValueError): + tree_to_stream([(sha, 0o100644, "file")], BytesIO().write) diff --git a/test/test_tree.py b/test/test_tree.py index 629fd4d32..803f1f381 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -3,21 +3,26 @@ # This module is part of GitPython and is released under the # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ -from io import BytesIO +import os import os.path as osp -from pathlib import Path import subprocess +from io import BytesIO +from pathlib import Path +import ddt import pytest from git.objects import Blob, Tree +from git.objects.fun import tree_entries_from_data, tree_to_stream +from git.objects.tree import TreeModifier from git.repo import Repo from git.util import cwd - from test.lib import TestBase, with_rw_directory + from .lib.helper import PathLikeMock, with_rw_repo +@ddt.ddt class TestTree(TestBase): def test_serializable(self): # Tree at the given commit contains a submodule as well. @@ -47,6 +52,19 @@ def test_serializable(self): testtree._deserialize(stream) # END for each item in tree + def test_valid_unusual_tree_names_round_trip(self): + names = ["a b", "a\nb", "a\tb", "café", ".gitignore"] + if os.name != "nt": + names.extend(["a\\b", "a:b", "C:relative"]) + cache = [] + modifier = TreeModifier(cache) + for name in names: + modifier.add(b"a" * 20, 0o100644, name) + modifier.set_done() + data = BytesIO() + tree_to_stream(cache, data.write) + assert tree_entries_from_data(data.getvalue()) == cache + @with_rw_directory def _get_git_ordered_files(self, rw_dir): """Get files as git orders them, to compare in test_tree_modifier_ordering.""" @@ -111,6 +129,15 @@ def names_in_mod_cache(): mod.set_done() assert names_in_mod_cache() == git_file_names_in_order, "set_done() performs git-sorting" + @ddt.data("", ".", "..", ".git", ".GIT", "git~1", ".git. ", ".g\u200cit", "a/b", "a\0b") + def test_tree_names_are_checked_at_construction_and_serialization(self, name): + cache = [] + with pytest.raises(ValueError): + TreeModifier(cache).add(b"a" * 20, 0o100644, name) + assert not cache + with pytest.raises(ValueError): + tree_to_stream([(b"a" * 20, 0o100644, name)], BytesIO().write) + def test_traverse(self): root = self.rorepo.tree("0.1.6") num_recursive = 0 From f2dfa9fee5ff9689a93c7a337b9d7b793f7385fc Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 29 Sep 2026 21:29:15 +0200 Subject: [PATCH 64/92] fix(index): confine staging reads to worktree files rubber-stamp, after checking diff quickly. `IndexFile.add()` could read through intermediate directory symlinks and store files outside the working tree. Null-SHA `Blob` and `BaseIndexEntry` inputs reached the same reader, while unsafe entry and rewritten paths could enter the in-memory index with `write=False` (finding 21501). Validate supplied object paths and all final entry paths with the shared repository path checker. Confine filesystem inputs in `_store_path()` and reject intermediate symlinks or redirected directories before opening files. Preserve final symlinks as links, including when rewriting paths, and reject special files. Use `O_NOFOLLOW` and `O_NONBLOCK` where available and check the opened regular file with `fstat()`. Skip Git metadata during directory expansion and stage directory symlinks themselves. Validate complete relative directory paths so nested POSIX colon names are not mistaken for drive-prefixed paths. Parent-directory checks handle the existing filesystem layout; they do not make staging atomic against concurrent directory replacement. The open flags protect final-component substitution on supporting platforms. Tests cover path/glob/object inputs, inside and outside symlink targets, rewriters, in-memory index validation, metadata exclusion, directory symlinks, special files, and nested colon names. Validation: 129 combined tree/index tests passed, with two platform skips; Ruff checks and formatting checks passed. --- git/index/base.py | 54 +++++++++++++++++++---- test/test_index.py | 104 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 8 deletions(-) diff --git a/git/index/base.py b/git/index/base.py index 11995edf0..c9d99840d 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -14,7 +14,7 @@ from io import BytesIO import os import os.path as osp -from stat import S_ISLNK +from stat import S_ISLNK, S_ISREG import subprocess import sys import tempfile @@ -36,6 +36,7 @@ file_contents_ro, _is_path_rooted, _to_relative_path, + _validate_repo_path, to_native_path_linux, unbare_repo, to_bin_sha, @@ -476,7 +477,17 @@ def raise_exc(e: Exception) -> NoReturn: continue # END glob handling try: - for root, _dirs, files in os.walk(abs_path, onerror=raise_exc): + for root, dirs, files in os.walk(abs_path, onerror=raise_exc): + for dirname in dirs[:]: + directory = osp.join(root, dirname) + try: + _validate_repo_path(to_native_path_linux(osp.relpath(directory, r))) + except ValueError: + dirs.remove(dirname) + continue + if osp.islink(directory): + dirs.remove(dirname) + yield osp.relpath(directory, r) for rela_file in files: # Add relative paths only. yield osp.join(root.replace(rs, ""), rela_file) @@ -717,6 +728,8 @@ def _preprocess_add_items( else: raise TypeError("Invalid Type: %r" % item) # END for each item + for entry in entries: + _validate_repo_path(entry.path) return paths, entries def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry: @@ -726,20 +739,43 @@ def _store_path(self, filepath: PathLike, fprogress: Callable) -> BaseIndexEntry This needs the :func:`~git.index.util.git_working_dir` decorator active! This must be ensured in the calling code. """ - st = os.lstat(filepath) # Handles non-symlinks as well. - + filepath = self._to_relative_path(filepath) + _validate_repo_path(filepath) + parent = osp.realpath(self.repo.working_dir) + for component in os.fspath(filepath).split("/")[:-1]: + parent = osp.join(parent, component) + if osp.islink(parent) or osp.normcase(osp.realpath(parent)) != osp.normcase(osp.abspath(parent)): + raise ValueError("Cannot stage a path beyond a symbolic link: %r" % filepath) + st = os.lstat(filepath) + if not S_ISLNK(st.st_mode) and not S_ISREG(st.st_mode): + raise ValueError("Can only stage a regular file or symbolic link: %r" % filepath) + + stream_size = st.st_size if S_ISLNK(st.st_mode): # readlink is a string, but we need bytes. + target = force_bytes(os.readlink(filepath), encoding=defenc) + stream_size = len(target) + def open_stream() -> BinaryIO: - return BytesIO(force_bytes(os.readlink(filepath), encoding=defenc)) + return BytesIO(target) else: def open_stream() -> BinaryIO: - return open(filepath, "rb") + # Do not follow a final symlink or block on a FIFO substituted + # between lstat and open on platforms supporting these flags. + def opener(path: str, flags: int) -> int: + return os.open(path, flags | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) + + return open(filepath, "rb", opener=opener) with open_stream() as stream: + if not S_ISLNK(st.st_mode): + st = os.fstat(stream.fileno()) + if not S_ISREG(st.st_mode): + raise ValueError("Can only stage a regular file: %r" % filepath) + stream_size = st.st_size fprogress(filepath, False, filepath) - istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream)) + istream = self.repo.odb.store(IStream(Blob.type, stream_size, stream)) fprogress(filepath, True, filepath) return BaseIndexEntry( ( @@ -777,7 +813,7 @@ def _entries_for_paths( blob = Blob( self.repo, Blob.NULL_BIN_SHA, - stat_mode_to_index_mode(os.stat(abspath).st_mode), + stat_mode_to_index_mode(os.lstat(abspath).st_mode), to_native_path_linux(gitrelative_path), ) # TODO: variable undefined @@ -989,6 +1025,8 @@ def handle_null_entries(self: "IndexFile") -> None: # FINALIZE # Add the new entries to this instance. + for entry in entries_added: + _validate_repo_path(entry.path) for entry in entries_added: self.entries[(entry.path, 0)] = IndexEntry.from_base(entry) diff --git a/test/test_index.py b/test/test_index.py index 343cc19e7..8c33fe29d 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -1180,6 +1180,110 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir): r.index.add([fp]) r.index.commit("Added [.exe") + @ddt.data(*product(("path", "glob", "blob", "entry"), (False, True))) + @ddt.unpack + @with_rw_directory + def test_staging_never_reads_through_a_directory_symlink(self, rw_dir, kind, outside): + tmp_path = Path(rw_dir) + with Repo.init(tmp_path / "repo") as repo: + root = tmp_path / "repo" + target = (tmp_path if outside else root) / "target" + target.mkdir() + (target / "secret").write_text("private data") + try: + (root / "link").symlink_to(target, target_is_directory=True) + except OSError: + pytest.skip("Symlinks unavailable") + item = "link/secret" + if kind == "glob": + item = "link/*" + elif kind == "blob": + item = Blob(repo, Blob.NULL_BIN_SHA, 0o100644, item) + elif kind == "entry": + item = BaseIndexEntry((0o100644, Blob.NULL_BIN_SHA, 0, item)) + with mock.patch.object(repo.odb, "store", wraps=repo.odb.store) as store: + with pytest.raises(ValueError, match="symbolic link"): + repo.index.add([item], write=False) + store.assert_not_called() + + @ddt.data("blob", "entry", "rewriter") + @with_rw_directory + def test_staging_rejects_unsafe_object_paths_even_without_writing(self, rw_dir, kind): + with Repo.init(rw_dir) as repo: + entry = BaseIndexEntry((0o100644, b"a" * 20, 0, "../outside")) + item = Blob(repo, entry.binsha, entry.mode, entry.path) if kind == "blob" else entry + kwargs = {} + if kind == "rewriter": + item = BaseIndexEntry((0o100644, b"a" * 20, 0, "safe")) + kwargs["path_rewriter"] = lambda entry: "../outside" + index = repo.index + with pytest.raises(ValueError): + index.add([item], write=False, **kwargs) + assert not index.entries + + @with_rw_directory + def test_staging_root_preserves_symlinks_and_skips_git_metadata(self, rw_dir): + tmp_path = Path(rw_dir) + root = tmp_path / "repo" + (tmp_path / "outside").mkdir() + with Repo.init(root) as repo: + (root / "file").write_text("contents") + try: + (root / "link").symlink_to("../outside", target_is_directory=True) + except OSError: + pytest.skip("Symlinks unavailable") + entries = repo.index.add(["."]) + assert {entry.path for entry in entries} == {"file", "link"} + link = repo.index.entries[("link", 0)] + assert link.mode == 0o120000 + assert repo.odb.stream(link.binsha).read() == os.fsencode(os.readlink(root / "link")) + + @with_rw_directory + def test_staging_symlink_measures_encoded_target_instead_of_stat_size(self, rw_dir): + tmp_path = Path(rw_dir) + with Repo.init(tmp_path) as repo: + link = tmp_path / "link" + try: + link.symlink_to("../café", target_is_directory=True) + except OSError: + pytest.skip("Symlinks unavailable") + target = os.fsencode(os.readlink(link)) + original_lstat = os.lstat + + def zero_size_for_symlinks(*args, **kwargs): + result = original_lstat(*args, **kwargs) + if S_ISLNK(result.st_mode): + fields = list(result) + fields[6] = 0 + return os.stat_result(fields) + return result + + with mock.patch("os.lstat", side_effect=zero_size_for_symlinks): + (entry,) = repo.index.add(["link"]) + assert entry.mode == 0o120000 + assert repo.odb.stream(entry.binsha).read() == target + + @pytest.mark.skipif(os.name == "nt", reason="Colons are not valid Windows filenames") + @with_rw_directory + def test_staging_nested_colon_directory(self, rw_dir): + tmp_path = Path(rw_dir) + with Repo.init(tmp_path) as repo: + directory = tmp_path / "nested" / "a:b" + directory.mkdir(parents=True) + (directory / "file").write_text("contents") + assert [entry.path for entry in repo.index.add(["nested"])] == ["nested/a:b/file"] + assert repo.index.write_tree()["nested/a:b/file"].data_stream.read() == b"contents" + + @pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFOs unavailable") + @with_rw_directory + def test_staging_special_files_fails_before_opening(self, rw_dir): + tmp_path = Path(rw_dir) + with Repo.init(tmp_path) as repo: + os.mkfifo(tmp_path / "fifo") + with mock.patch("builtins.open", side_effect=AssertionError("must not open a FIFO")): + with pytest.raises(ValueError, match="regular file"): + repo.index.add(["fifo"], write=False) + def test__to_relative_path_at_root(self): root = osp.abspath(os.sep) From cc02e452bdd5bda631d0039f1b42a09856f4bb63 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 30 Sep 2026 09:39:16 +0200 Subject: [PATCH 65/92] ci: remove `codespell` checks and configuration Reason: it triggered quite often, is an external dependency that isn't crucial, and... spelling issues aren't a problem that GitPython really needs to solve. rubber-stamp, after checking diff quickly. Remove `codespell` from the project's pre-commit checks so it no longer runs locally or in the shared CI lint workflow. This also removes the need to maintain spelling exceptions for intentional test inputs. Delete its hook-specific `tomli` dependency, the entire `[tool.codespell]` configuration and word exception list, and the obsolete inline suppression on the `remot` archive-option regression case. --- .pre-commit-config.yaml | 7 ------- pyproject.toml | 6 ------ test/test_repo.py | 2 +- 3 files changed, 1 insertion(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dfa5fc91e..45115f60c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,6 @@ exclude: ^(?:gitdb|smmap)/ repos: -- repo: https://github.com/codespell-project/codespell - rev: v2.4.3 - hooks: - - id: codespell - additional_dependencies: [tomli] - exclude: ^test/fixtures/ - - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.16.5 hooks: diff --git a/pyproject.toml b/pyproject.toml index d5b0e6b94..4b8258fcb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -153,9 +153,3 @@ lint.unfixable = [ "fuzzing/fuzz-targets/**" = [ "E402", # environment setup must happen before the `git` module is imported, thus cannot happen at top of file ] - - -[tool.codespell] -ignore-words-list="afile,assertIn,doesnt,gud,uptodate" -#count = true -quiet-level = 3 diff --git a/test/test_repo.py b/test/test_repo.py index ad05ff97f..47bf92b95 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1677,7 +1677,7 @@ def test_ignored_raises_error_w_symlink(self): {"rem": "ext::helper"}, {"re": "ext::helper"}, {"remo": "ext::helper"}, - {"remot": "ext::helper"}, # codespell:ignore remot + {"remot": "ext::helper"}, {"remote": ["https://example.com/repo", "ext::helper"]}, {"remote": (None, False, "ext::helper")}, {"remote=ext::helper": True}, From 223572343678368bac4a3a2bcb5d8cb59a95dc5e Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Wed, 30 Sep 2026 13:12:41 +0530 Subject: [PATCH 66/92] fix(config): bound option-line parsing `GitConfigParser.OPTCRE` matched an option line with `(?P