Skip to content

Commit f21d0ba

Browse files
authored
Merge pull request #2356 from Keerthana-64/trailers-no-divider
fix(commit): stop treating `---` as a patch divider for trailers
2 parents 029964a + 30bcf67 commit f21d0ba

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ API changes
9797
subsequent and empty commits, including the Gix backend's filename
9898
quoting fallback. File keys contain the actual names, unquoting Git's C-style
9999
escapes and preserving undecodable bytes with ``surrogateescape``.
100+
* ``Commit.trailers_list``, ``Commit.trailers_dict`` and the ``trailers`` argument
101+
of ``Commit.create_from_tree()`` and ``IndexFile.commit()`` no longer treat a
102+
``---`` line as the end of the message, matching Git's ``%(trailers)`` and
103+
``git commit --trailer``. Trailers below such a line are reported, and added
104+
trailers are placed at the end of the message.
100105
* Imports from a source checkout now read ``git.__version__`` from ``VERSION``.
101106
Built wheels and source distributions continue to carry their stamped release
102107
version.

‎git/objects/commit.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ def _interpret_trailers(
612612
stream.seek(0)
613613
return repo.git._call_process_safe(
614614
"interpret_trailers",
615+
# The input is a commit message, not a patch e-mail. Git's own commit
616+
# trailer handling (`%(trailers)`, `git commit --trailer`) does not end
617+
# the message at a `---` line either.
618+
"--no-divider",
615619
*trailer_args,
616620
istream=stream,
617621
stdout_as_string=False,

‎test/test_commit.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,21 @@ def test_trailers(self):
872872
assert commit.trailers_list == [(KEY_2, VALUE_2)]
873873
assert commit.trailers_dict == {KEY_2: [VALUE_2]}
874874

875+
def test_trailers_ignore_patch_divider(self):
876+
"""A ``---`` line does not end a commit message, as in Git's ``%(trailers)``."""
877+
commit = copy.copy(self.rorepo.commit("HEAD"))
878+
# Dependabot writes its metadata after a `---` line and signs off below it.
879+
commit.message = (
880+
"Bump x from 1 to 2\n\nBumps x.\n\n---\nupdated-dependencies:\n- dependency-name: x\n...\n\n"
881+
"Signed-off-by: dependabot[bot] <support@github.com>\n"
882+
)
883+
assert commit.trailers_list == [("Signed-off-by", "dependabot[bot] <support@github.com>")]
884+
885+
# A paragraph followed by `---` and more text is part of the body.
886+
commit.message = "Subject\n\nReviewed-by: Maintainer <maintainer@example.com>\n---\nNot a trailer\n"
887+
assert commit.trailers_list == []
888+
assert commit.trailers_dict == {}
889+
875890
def test_commit_co_authors(self):
876891
commit = copy.copy(self.rorepo.commit("dated"))
877892
commit.message = """Commit message
@@ -950,6 +965,22 @@ def test_create_from_tree_with_trailers_list(self):
950965
"Issue": ["456"],
951966
}
952967

968+
def test_create_from_tree_appends_trailers_after_patch_divider(self):
969+
"""Trailers go at the end of a message containing ``---``, as with ``git commit --trailer``."""
970+
with fixture_repo("commit.sh", "tree", write=True) as rw_repo:
971+
tree = rw_repo.tree((Path(rw_repo.working_tree_dir).parent / "tree-id").read_text().strip())
972+
973+
commit = Commit.create_from_tree(
974+
rw_repo,
975+
tree,
976+
"Subject\n\nBody\n---\nNotes\n",
977+
head=True,
978+
trailers={"Signed-off-by": "Test User <test@test.com>"},
979+
)
980+
981+
assert commit.message == "Subject\n\nBody\n---\nNotes\n\nSigned-off-by: Test User <test@test.com>\n"
982+
assert commit.trailers_dict == {"Signed-off-by": ["Test User <test@test.com>"]}
983+
953984
def test_create_from_tree_with_non_utf8_trailers(self):
954985
"""Test that trailer creation and parsing respect the configured commit encoding."""
955986
with fixture_repo("commit.sh", "latin1", write=True) as rw_repo:

0 commit comments

Comments
 (0)