From dd3846513bbc682b9c51b09d369687ab7a036a49 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Thu, 7 Jan 2021 17:10:45 +0100 Subject: [PATCH] Tag message must not include the signature Signatures on tags are just tacked onto the end of the message. Getting the message must not return the signature. Compare [1] and [2] in C git, which both drop a signature at the end of an object body. [1] https://github.com/git/git/blob/21bf933/builtin/tag.c#L173 [2] https://github.com/git/git/blob/21bf933/ref-filter.c#L1276 Change-Id: Ic8a1062b8bc77f2d7c138c3fe8a7fd13b1253f38 Signed-off-by: Thomas Wolf --- .../src/org/eclipse/jgit/pgm/Show.java | 15 ++++-- .../eclipse/jgit/revwalk/RevTagParseTest.java | 12 ++--- .../src/org/eclipse/jgit/revwalk/RevTag.java | 48 ++++++++++++++----- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java index 5f9551e529..1d43220ca8 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java @@ -41,6 +41,7 @@ import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.TreeFilter; +import org.eclipse.jgit.util.RawParseUtils; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; @@ -219,13 +220,17 @@ class Show extends TextBuiltin { } outw.println(); - final String[] lines = tag.getFullMessage().split("\n"); //$NON-NLS-1$ + String[] lines = tag.getFullMessage().split("\n"); //$NON-NLS-1$ for (String s : lines) { - outw.print(" "); //$NON-NLS-1$ - outw.print(s); - outw.println(); + outw.println(s); + } + byte[] rawSignature = tag.getRawGpgSignature(); + if (rawSignature != null) { + lines = RawParseUtils.decode(rawSignature).split("\n"); //$NON-NLS-1$ + for (String s : lines) { + outw.println(s); + } } - outw.println(); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java index edddc33a28..a3ba3d67b2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java @@ -136,7 +136,7 @@ public class RevTagParseTest extends RepositoryTestCase { + "\n" // + "iD8DBQBC0b9oF3Y\n" // + "-----END PGP SIGNATURE-----"; - final String message = "test\n\n" + fakeSignature + '\n'; + final String message = "test\n" + fakeSignature + '\n'; final StringBuilder body = new StringBuilder(); @@ -168,7 +168,7 @@ public class RevTagParseTest extends RepositoryTestCase { assertNotNull(c.getTagName()); assertEquals(name, c.getTagName()); assertEquals("test", c.getShortMessage()); - assertEquals(message, c.getFullMessage()); + assertEquals("test\n", c.getFullMessage()); assertEquals(fakeSignature + '\n', new String(c.getRawGpgSignature(), US_ASCII)); @@ -406,7 +406,7 @@ public class RevTagParseTest extends RepositoryTestCase { b.write("tag v1.0\n".getBytes(UTF_8)); b.write("tagger t 1218123387 +0700\n".getBytes(UTF_8)); b.write('\n'); - b.write("message\n\n".getBytes(UTF_8)); + b.write("message\n".getBytes(UTF_8)); b.write(signature.getBytes(US_ASCII)); b.write('\n'); @@ -417,7 +417,7 @@ public class RevTagParseTest extends RepositoryTestCase { assertEquals("t", t.getTaggerIdent().getName()); assertEquals("message", t.getShortMessage()); - assertEquals("message\n\n" + signature + '\n', t.getFullMessage()); + assertEquals("message\n", t.getFullMessage()); String gpgSig = new String(t.getRawGpgSignature(), UTF_8); assertEquals(signature + '\n', gpgSig); } @@ -452,7 +452,7 @@ public class RevTagParseTest extends RepositoryTestCase { assertEquals("t", t.getTaggerIdent().getName()); assertEquals("message", t.getShortMessage()); - assertEquals(message + signature + '\n', t.getFullMessage()); + assertEquals(message, t.getFullMessage()); String gpgSig = new String(t.getRawGpgSignature(), UTF_8); assertEquals(signature + '\n', gpgSig); } @@ -486,7 +486,7 @@ public class RevTagParseTest extends RepositoryTestCase { assertEquals("t", t.getTaggerIdent().getName()); assertEquals("message", t.getShortMessage()); - assertEquals(message + signature + '\n', t.getFullMessage()); + assertEquals(message, t.getFullMessage()); String gpgSig = new String(t.getRawGpgSignature(), UTF_8); assertEquals(signature + '\n', gpgSig); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java index 3bcdfafea7..3499136f92 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java @@ -202,24 +202,16 @@ public class RevTag extends RevObject { return -1; } - /** - * Parse the GPG signature from the raw buffer. - * - * @return contents of the GPG signature; {@code null} if the tag was not - * signed. - * @since 5.11 - */ - @Nullable - public final byte[] getRawGpgSignature() { + private int getSignatureStart() { byte[] raw = buffer; int msgB = RawParseUtils.tagMessage(raw, 0); if (msgB < 0) { - return null; + return msgB; } // Find the last signature start and return the rest int start = nextStart(hSignature, raw, msgB); if (start < 0) { - return null; + return start; } int next = RawParseUtils.nextLF(raw, start); while (next < raw.length) { @@ -230,6 +222,23 @@ public class RevTag extends RevObject { start = newStart; next = RawParseUtils.nextLF(raw, start); } + return start; + } + + /** + * Parse the GPG signature from the raw buffer. + * + * @return contents of the GPG signature; {@code null} if the tag was not + * signed. + * @since 5.11 + */ + @Nullable + public final byte[] getRawGpgSignature() { + byte[] raw = buffer; + int start = getSignatureStart(); + if (start < 0) { + return null; + } return Arrays.copyOfRange(raw, start, raw.length); } @@ -249,7 +258,12 @@ public class RevTag extends RevObject { if (msgB < 0) { return ""; //$NON-NLS-1$ } - return RawParseUtils.decode(guessEncoding(), raw, msgB, raw.length); + int signatureStart = getSignatureStart(); + int end = signatureStart < 0 ? raw.length : signatureStart; + if (end == msgB) { + return ""; //$NON-NLS-1$ + } + return RawParseUtils.decode(guessEncoding(), raw, msgB, end); } /** @@ -275,6 +289,16 @@ public class RevTag extends RevObject { } int msgE = RawParseUtils.endOfParagraph(raw, msgB); + int signatureStart = getSignatureStart(); + if (signatureStart >= msgB && msgE > signatureStart) { + msgE = signatureStart; + if (msgE > msgB) { + msgE--; + } + if (msgB == msgE) { + return ""; //$NON-NLS-1$ + } + } String str = RawParseUtils.decode(guessEncoding(), raw, msgB, msgE); if (RevCommit.hasLF(raw, msgB, msgE)) { str = StringUtils.replaceLineBreaksWithSpace(str); -- 2.39.5