aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorDavid Turner <dturner@twosigma.com>2018-06-14 20:05:38 -0400
committerMatthias Sohn <matthias.sohn@sap.com>2018-09-04 20:13:16 +0200
commit559c68cb012b971bf506d12aaf0a0f0504c9780b (patch)
treee3e8b5e4f7b6a4e8b3c70c8e583917d12e474201 /org.eclipse.jgit
parent30c6c7542190c149e2aee792f992a312a5fc5793 (diff)
downloadjgit-559c68cb012b971bf506d12aaf0a0f0504c9780b.tar.gz
jgit-559c68cb012b971bf506d12aaf0a0f0504c9780b.zip
Parse signature of GPG-signed commits
In order to support GPG-signed commits, add some methods which will allow GPG signatures to be parsed out of RevCommit objects. Later, we can add code to verify the signatures. Change-Id: Ifcf6b3ac79115c15d3ec4b4eaed07315534d09ac Signed-off-by: David Turner <dturner@twosigma.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java29
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java56
2 files changed, 85 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
index 5bd5dd6368..86ecd8eaee 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
@@ -51,6 +51,7 @@ import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -390,6 +391,34 @@ public class RevCommit extends RevObject {
}
/**
+ * Parse the gpg signature from the raw buffer.
+ * <p>
+ * This method parses and returns the raw content of the gpgsig lines. This
+ * method is fairly expensive and produces a new byte[] instance on each
+ * invocation. Callers should invoke this method only if they are certain
+ * they will need, and should cache the return value for as long as
+ * necessary to use all information from it.
+ * <p>
+ * RevFilter implementations should try to use
+ * {@link org.eclipse.jgit.util.RawParseUtils} to scan the
+ * {@link #getRawBuffer()} instead, as this will allow faster evaluation of
+ * commits.
+ *
+ * @return contents of the gpg signature; null if the commit was not signed.
+ * @since 5.1
+ */
+ public final @Nullable byte[] getRawGpgSignature() {
+ final byte[] raw = buffer;
+ final byte[] header = {'g', 'p', 'g', 's', 'i', 'g'};
+ final int start = RawParseUtils.headerStart(header, raw, 0);
+ if (start < 0) {
+ return null;
+ }
+ final int end = RawParseUtils.headerEnd(raw, start);
+ return Arrays.copyOfRange(raw, start, end);
+ }
+
+ /**
* Parse the author identity from the raw buffer.
* <p>
* This method parses and returns the content of the author line, after
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
index c22c8de5d1..28f406a49e 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java
@@ -549,6 +549,62 @@ public final class RawParseUtils {
}
/**
+ * Locate the end of the header. Note that headers may be
+ * more than one line long.
+ * @param b
+ * buffer to scan.
+ * @param ptr
+ * position within buffer to start looking for the end-of-header.
+ * @return new position just after the header. This is either
+ * b.length, or the index of the header's terminating newline.
+ * @since 5.1
+ */
+ public static final int headerEnd(final byte[] b, int ptr) {
+ final int sz = b.length;
+ while (ptr < sz) {
+ final byte c = b[ptr++];
+ if (c == '\n' && (ptr == sz || b[ptr] != ' ')) {
+ return ptr - 1;
+ }
+ }
+ return ptr - 1;
+ }
+
+ /**
+ * Find the start of the contents of a given header.
+ *
+ * @param b
+ * buffer to scan.
+ * @param headerName
+ * header to search for
+ * @param ptr
+ * position within buffer to start looking for header at.
+ * @return new position at the start of the header's contents, -1 for
+ * not found
+ * @since 5.1
+ */
+ public static final int headerStart(byte[] headerName, byte[] b, int ptr) {
+ // Start by advancing to just past a LF or buffer start
+ if (ptr != 0) {
+ ptr = nextLF(b, ptr - 1);
+ }
+ while (ptr < b.length - (headerName.length + 1)) {
+ boolean found = true;
+ for (int i = 0; i < headerName.length; i++) {
+ if (headerName[i] != b[ptr++]) {
+ found = false;
+ break;
+ }
+ }
+ if (found && b[ptr++] == ' ') {
+ return ptr;
+ }
+ ptr = nextLF(b, ptr);
+ }
+ return -1;
+ }
+
+ /**
* Locate the first position before a given character.
*
* @param b