Skip to content

Commit

Permalink
fix incorrect empty message commit serialization
Browse files Browse the repository at this point in the history
* Fixes incorrect signature verification for commits with an empty message,
  as encoded by CGit (#1429)
  • Loading branch information
castedo committed Nov 16, 2024
1 parent cb5e1c3 commit 95c545d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dulwich/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,8 @@ def _format_message(headers, body):
yield git_line(field, lines[0])
for line in lines[1:]:
yield b" " + line + b"\n"
yield b"\n" # There must be a new line after the headers
if body:
yield b"\n" # There must be a new line after the headers
yield body


Expand Down
71 changes: 71 additions & 0 deletions tests/compat/test_porcelain.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,74 @@ def test_verify(self):
tag = self.repo[b"refs/tags/verifyme"]
self.assertNotEqual(tag.signature, None)
tag.verify()


@skipIf(
platform.python_implementation() == "PyPy" or sys.platform == "win32",
"gpgme not easily available or supported on Windows and PyPy",
)
class CommitCreateSignTestCase(PorcelainGpgTestCase, CompatTestCase):
def test_sign(self):
# Test that dulwich signatures can be verified by CGit
cfg = self.repo.get_config()
cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
self.import_default_key()

porcelain.commit(
self.repo.path,
b"messy message messiah",
b"foo <[email protected]>",
signoff=True,
)

run_git_or_fail(
[f"--git-dir={self.repo.controldir()}", "verify-commit", "-v", "HEAD"],
env={"GNUPGHOME": os.environ["GNUPGHOME"]},
)

def test_verify(self):
# Test that CGit signatures can be verified by dulwich
self.import_default_key()

run_git_or_fail(
[
f"--git-dir={self.repo.controldir()}",
"commit",
"--allow-empty",
"-S" + PorcelainGpgTestCase.DEFAULT_KEY_ID,
"-m",
"foo",
],
env={
"GNUPGHOME": os.environ["GNUPGHOME"],
"GIT_COMMITTER_NAME": "Joe Example",
"GIT_COMMITTER_EMAIL": "[email protected]",
},
)
commit = self.repo[b"HEAD"]
self.assertNotEqual(commit.gpgsig, None)
commit.verify()

def test_verify_with_empty_message(self):
# Test that CGit signatures can be verified by dulwich
self.import_default_key()

run_git_or_fail(
[
f"--git-dir={self.repo.controldir()}",
"commit",
"--allow-empty",
"-S" + PorcelainGpgTestCase.DEFAULT_KEY_ID,
"--allow-empty-message",
"-m",
"",
],
env={
"GNUPGHOME": os.environ["GNUPGHOME"],
"GIT_COMMITTER_NAME": "Joe Example",
"GIT_COMMITTER_EMAIL": "[email protected]",
},
)
commit = self.repo[b"HEAD"]
self.assertNotEqual(commit.gpgsig, None)
commit.verify()
2 changes: 1 addition & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ def test_serialize_none_message(self):
b"type blob\n"
b"tag 0.1\n"
b"tagger Jelmer Vernooij <[email protected]> "
b"423423423 +0000\n"
b"423423423 +0000\n\n"
),
x.as_raw_string(),
)
Expand Down

0 comments on commit 95c545d

Please sign in to comment.