From eefa7e425ed5272eb1c1f7cc2c5f229bd552df98 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 10 Aug 2026 14:38:41 +0200 Subject: [PATCH 01/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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/19] 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