From 30bcf673d0138198a657726550c67354c3de57b6 Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Fri, 20 Nov 2026 15:50:25 +0530 Subject: [PATCH] fix(commit): stop treating `---` as a patch divider for trailers `Commit._interpret_trailers`, which backs `Commit.trailers_list`, `Commit.trailers_dict` and the `trailers` argument of `Commit.create_from_tree()` and `IndexFile.commit()`, ran `git interpret-trailers` without `--no-divider`. In that mode interpret-trailers reads its input as a `git format-patch` e-mail: the first line starting with `---` followed by whitespace begins the patch, and only the text above it is searched for trailers or receives new ones. Git does not apply that rule to commit messages. `%(trailers)` in `pretty.c` sets `no_divider`, `git commit --trailer` runs `interpret-trailers --no-divider` (`amend_file_with_trailers()`), and `trailer_iterator_init()`, which the sign-off check in `sequencer.c` uses, sets it as well. Commit messages are chosen by whoever authored the commit, so GitPython and Git disagreed about the trailers of the same commit, on both backends: - Dependabot commits put a `---` line before their YAML metadata and sign off below it. `trailers_list` returned `[]` for them, including the Dependabot commits in this repository's history, while `git log --format=%(trailers)` reports `Signed-off-by: dependabot[bot] `. - A paragraph such as `Reviewed-by: Maintainer ` followed by `---` and more text was reported as a trailer, although Git treats it as body text and reports no trailers for that commit. - `create_from_tree()` with the message `Subject\n\nBody\n---\nNotes\n` and a `Signed-off-by` trailer inserted the trailer above the `---` line, where `git commit --trailer` appends it after `Notes`. Pass `--no-divider` in `_interpret_trailers`, so the parse and add paths treat their input as the commit message it is. Messages without such a `---` line are unaffected. `interpret_trailers` has no Gix handler, so the change covers both backends. Validation on macOS with CPython 3.13.17, Git 2.54.0 and GixPython 0.3.0: - `test_trailers_ignore_patch_divider` and `test_create_from_tree_appends_trailers_after_patch_divider` in `test/test_commit.py` fail before the change and pass after it on both backends. - 300 randomized messages built from trailer lines, `---` variants, comments and body text were committed and `trailers_list` was compared with `git log --format=%(trailers:only,unfold)` for the same commit. Before the change 52 disagreed on the CLI backend; after it none disagreed on either backend (300 messages each). - Full suite: 2207 passed, 155 skipped, 1 xfailed on the CLI backend; 2310 passed, 52 skipped, 1 xfailed on the Gix backend. - Ruff check and format (pre-commit), mypy, `basedpyright --warnings` and `git diff --check` pass. Agent-Model: claude-opus-5-5 --- doc/source/changes.rst | 5 +++++ git/objects/commit.py | 4 ++++ test/test_commit.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index f9e8a13b2..5efd23ff1 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -97,6 +97,11 @@ API changes subsequent and empty commits, including the Gix backend's filename quoting fallback. File keys contain the actual names, unquoting Git's C-style escapes and preserving undecodable bytes with ``surrogateescape``. +* ``Commit.trailers_list``, ``Commit.trailers_dict`` and the ``trailers`` argument + of ``Commit.create_from_tree()`` and ``IndexFile.commit()`` no longer treat a + ``---`` line as the end of the message, matching Git's ``%(trailers)`` and + ``git commit --trailer``. Trailers below such a line are reported, and added + trailers are placed at the end of the message. * Imports from a source checkout now read ``git.__version__`` from ``VERSION``. Built wheels and source distributions continue to carry their stamped release version. diff --git a/git/objects/commit.py b/git/objects/commit.py index 185b08728..8546d2bed 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -612,6 +612,10 @@ def _interpret_trailers( stream.seek(0) return repo.git._call_process_safe( "interpret_trailers", + # The input is a commit message, not a patch e-mail. Git's own commit + # trailer handling (`%(trailers)`, `git commit --trailer`) does not end + # the message at a `---` line either. + "--no-divider", *trailer_args, istream=stream, stdout_as_string=False, diff --git a/test/test_commit.py b/test/test_commit.py index 8c09828e5..909b52c1e 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -872,6 +872,21 @@ def test_trailers(self): assert commit.trailers_list == [(KEY_2, VALUE_2)] assert commit.trailers_dict == {KEY_2: [VALUE_2]} + def test_trailers_ignore_patch_divider(self): + """A ``---`` line does not end a commit message, as in Git's ``%(trailers)``.""" + commit = copy.copy(self.rorepo.commit("HEAD")) + # Dependabot writes its metadata after a `---` line and signs off below it. + commit.message = ( + "Bump x from 1 to 2\n\nBumps x.\n\n---\nupdated-dependencies:\n- dependency-name: x\n...\n\n" + "Signed-off-by: dependabot[bot] \n" + ) + assert commit.trailers_list == [("Signed-off-by", "dependabot[bot] ")] + + # A paragraph followed by `---` and more text is part of the body. + commit.message = "Subject\n\nReviewed-by: Maintainer \n---\nNot a trailer\n" + assert commit.trailers_list == [] + assert commit.trailers_dict == {} + def test_commit_co_authors(self): commit = copy.copy(self.rorepo.commit("dated")) commit.message = """Commit message @@ -950,6 +965,22 @@ def test_create_from_tree_with_trailers_list(self): "Issue": ["456"], } + def test_create_from_tree_appends_trailers_after_patch_divider(self): + """Trailers go at the end of a message containing ``---``, as with ``git commit --trailer``.""" + with fixture_repo("commit.sh", "tree", write=True) as rw_repo: + tree = rw_repo.tree((Path(rw_repo.working_tree_dir).parent / "tree-id").read_text().strip()) + + commit = Commit.create_from_tree( + rw_repo, + tree, + "Subject\n\nBody\n---\nNotes\n", + head=True, + trailers={"Signed-off-by": "Test User "}, + ) + + assert commit.message == "Subject\n\nBody\n---\nNotes\n\nSigned-off-by: Test User \n" + assert commit.trailers_dict == {"Signed-off-by": ["Test User "]} + def test_create_from_tree_with_non_utf8_trailers(self): """Test that trailer creation and parsing respect the configured commit encoding.""" with fixture_repo("commit.sh", "latin1", write=True) as rw_repo: