Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] <support@github.com>\n"
)
assert commit.trailers_list == [("Signed-off-by", "dependabot[bot] <support@github.com>")]

# A paragraph followed by `---` and more text is part of the body.
commit.message = "Subject\n\nReviewed-by: Maintainer <maintainer@example.com>\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
Expand Down Expand Up @@ -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 <test@test.com>"},
)

assert commit.message == "Subject\n\nBody\n---\nNotes\n\nSigned-off-by: Test User <test@test.com>\n"
assert commit.trailers_dict == {"Signed-off-by": ["Test User <test@test.com>"]}

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:
Expand Down
Loading