From b3c6c7199d5a41372dc5f76c79e4751d128b4bd9 Mon Sep 17 00:00:00 2001 From: Castedo Ellerman Date: Thu, 21 Nov 2024 16:37:53 +0000 Subject: [PATCH] add Tag.raw_without_sig method and unittest --- dulwich/objects.py | 12 +++++++++++- tests/test_objects.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dulwich/objects.py b/dulwich/objects.py index ee87b56d3..31ffe1e2a 100644 --- a/dulwich/objects.py +++ b/dulwich/objects.py @@ -917,6 +917,16 @@ def sign(self, keyid: Optional[str] = None) -> None: self.as_raw_string(), mode=gpg.constants.sig.mode.DETACH ) + def raw_without_sig(self) -> bytes: + """Return raw string serialization without the GPG/SSH signature. + + self.signature is a signature for the returned raw byte string serialization. + """ + ret = self.as_raw_string() + if self._signature: + ret = ret[: -len(self._signature)] + return ret + def verify(self, keyids: Optional[Iterable[str]] = None) -> None: """Verify GPG signature for this tag (if it is signed). @@ -938,7 +948,7 @@ def verify(self, keyids: Optional[Iterable[str]] = None) -> None: with gpg.Context() as ctx: data, result = ctx.verify( - self.as_raw_string()[: -len(self._signature)], + self.raw_without_sig(), signature=self._signature, ) if keyids: diff --git a/tests/test_objects.py b/tests/test_objects.py index 45b675123..14d6bbb3e 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -218,6 +218,7 @@ def test_read_tag_from_file(self) -> None: b"=ql7y\n" b"-----END PGP SIGNATURE-----\n", ) + self.assertEqual(t.raw_without_sig() + t.signature, bytes(t)) def test_read_commit_from_file(self) -> None: sha = b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e" @@ -1238,6 +1239,23 @@ def test_tree_copy_after_update(self) -> None: self.assertNotIn(shas[0], shas[1:]) self.assertEqual(shas[1], shas[2]) + def test_tag_withough_sig(self) -> None: + x = Tag() + x.set_raw_string(self.make_tag_text()) + self.assertEqual(bytes(x), x.raw_without_sig() + x.signature) + self.assertEqual( + b"""\ +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.4.7 (GNU/Linux) + +iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql +OK2XeQOiEeXtT76rV4t2WR4= +=ivrA +-----END PGP SIGNATURE----- +""", + x.signature, + ) + class CheckTests(TestCase): def test_check_hexsha(self) -> None: