Skip to content
Open
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
4 changes: 2 additions & 2 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def from_rev_and_commit(cls, rev_and_commit: str) -> GitCommit:
>>> commit.parents
['def456', 'ghi789']
"""
rev, parents, title, author, author_email, *body_list = rev_and_commit.split(
"\n"
rev, parents, title, author, author_email, *body_list = (
rev_and_commit.splitlines()
)
Comment on lines +121 to 123
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we split rev, parents, title, author, author_email with "\n" only?

Copy link
Author

@MattBelle MattBelle Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my above comment I think that would technically work. My preference is we stick with my provided implementation (everything uses splitlines())

  • it still handles \n
  • is easier to read
  • splitting the output by line seems to be the intended goal of that line

Furthermore, I think there is minimal risk of it causing a bug since the parts that theoretically don't need the extra delimiter logic are all computer generated. That said, I'd be happy to piece out the logic and only apply the splitlines() call to the body if that's what you want.

return cls(
rev=rev.strip(),
Expand Down
40 changes: 23 additions & 17 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,21 @@ def test_get_filenames_in_commit_error(util: UtilFixture):
assert str(excinfo.value) == "fatal: bad object HEAD"


def test_git_commit_from_rev_and_commit():
# Test data with all fields populated
rev_and_commit = (
"abc123\n" # rev
"def456 ghi789\n" # parents
"feat: add new feature\n" # title
"John Doe\n" # author
"john@example.com\n" # author_email
"This is a detailed description\n" # body
"of the new feature\n"
"with multiple lines"
@pytest.mark.parametrize(
"linebreak", ["\n", "\r\n"], ids=["line_feed", "carriage_return"]
)
def test_git_commit_from_rev_and_commit(linebreak):
rev_and_commit = linebreak.join(
[
"abc123", # rev
"def456 ghi789", # parents
"feat: add new feature", # title
"John Doe", # author
"john@example.com", # author_email
"This is a detailed description", # body
"of the new feature",
"with multiple lines",
]
)

commit = git.GitCommit.from_rev_and_commit(rev_and_commit)
Expand All @@ -421,12 +425,14 @@ def test_git_commit_from_rev_and_commit():
assert commit.parents == ["def456", "ghi789"]

# Test with minimal data
minimal_commit = (
"abc123\n" # rev
"\n" # no parents
"feat: minimal commit\n" # title
"John Doe\n" # author
"john@example.com\n" # author_email
minimal_commit = linebreak.join(
[
"abc123", # rev
"", # no parents
"feat: minimal commit", # title
"John Doe", # author
"john@example.com", # author_email
]
)

commit = git.GitCommit.from_rev_and_commit(minimal_commit)
Expand Down