From 6289902bbc615da71d68af7a69d6732dc4ad0b2d Mon Sep 17 00:00:00 2001 From: Castedo Ellerman Date: Sat, 16 Nov 2024 10:30:14 -0500 Subject: [PATCH] fix incorrect empty message commit serialization * Fixes incorrect signature verification for commits with an empty message, as encoded by CGit (https://github.com/jelmer/dulwich/issues/1429) --- dulwich/objects.py | 2 +- tests/compat/test_porcelain.py | 25 ++++++++++++++++++++++++- tests/test_objects.py | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/dulwich/objects.py b/dulwich/objects.py index 48d3712f9..03426eda1 100644 --- a/dulwich/objects.py +++ b/dulwich/objects.py @@ -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 diff --git a/tests/compat/test_porcelain.py b/tests/compat/test_porcelain.py index f20d90016..003b9c43a 100644 --- a/tests/compat/test_porcelain.py +++ b/tests/compat/test_porcelain.py @@ -126,7 +126,6 @@ def test_verify(self): "commit", "--allow-empty", "-S" + PorcelainGpgTestCase.DEFAULT_KEY_ID, - "--allow-empty-message", "-m", "foo", ], @@ -139,3 +138,27 @@ def test_verify(self): 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": "joe@example.com", + }, + ) + commit = self.repo[b"HEAD"] + self.assertNotEqual(commit.gpgsig, None) + commit.verify() diff --git a/tests/test_objects.py b/tests/test_objects.py index 4be85dd3b..81f2190c7 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -1066,7 +1066,7 @@ def test_serialize_none_message(self): b"type blob\n" b"tag 0.1\n" b"tagger Jelmer Vernooij " - b"423423423 +0000\n" + b"423423423 +0000\n\n" ), x.as_raw_string(), )