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: