null
- * if the stream was not parseable as LfsPointer
+ * @return an {@link org.eclipse.jgit.lfs.LfsPointer} or {@code null} if the
+ * stream was not parseable as LfsPointer
* @throws java.io.IOException
*/
@Nullable
public static LfsPointer parseLfsPointer(InputStream in)
throws IOException {
+ if (in.markSupported()) {
+ return parse(in);
+ }
+ // Fallback; note that while parse() resets its input stream, that won't
+ // reset "in".
+ return parse(new BufferedInputStream(in));
+ }
+
+ @Nullable
+ private static LfsPointer parse(InputStream in)
+ throws IOException {
+ if (!in.markSupported()) {
+ // No translation; internal error
+ throw new IllegalArgumentException(
+ "LFS pointer parsing needs InputStream.markSupported() == true"); //$NON-NLS-1$
+ }
+ // Try reading only a short block first.
+ in.mark(SIZE_THRESHOLD);
+ byte[] preamble = new byte[SIZE_THRESHOLD];
+ int length = IO.readFully(in, preamble, 0);
+ if (length < preamble.length || in.read() < 0) {
+ // We have the whole file. Try to parse a pointer from it.
+ try (BufferedReader r = new BufferedReader(new InputStreamReader(
+ new ByteArrayInputStream(preamble, 0, length), UTF_8))) {
+ LfsPointer ptr = parse(r);
+ if (ptr == null) {
+ in.reset();
+ }
+ return ptr;
+ }
+ }
+ // Longer than SIZE_THRESHOLD: expect "version" to be the first line.
+ boolean hasVersion = checkVersion(preamble);
+ in.reset();
+ if (!hasVersion) {
+ return null;
+ }
+ in.mark(FULL_SIZE_THRESHOLD);
+ byte[] fullPointer = new byte[FULL_SIZE_THRESHOLD];
+ length = IO.readFully(in, fullPointer, 0);
+ if (length == fullPointer.length && in.read() >= 0) {
+ in.reset();
+ return null; // Too long.
+ }
+ try (BufferedReader r = new BufferedReader(new InputStreamReader(
+ new ByteArrayInputStream(fullPointer, 0, length), UTF_8))) {
+ LfsPointer ptr = parse(r);
+ if (ptr == null) {
+ in.reset();
+ }
+ return ptr;
+ }
+ }
+
+ private static LfsPointer parse(BufferedReader r) throws IOException {
boolean versionLine = false;
LongObjectId id = null;
long sz = -1;
-
// This parsing is a bit too general if we go by the spec at
// https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
- // Comment lines are not mentioned in the spec, and the "version" line
- // MUST be the first.
- try (BufferedReader br = new BufferedReader(
- new InputStreamReader(in, UTF_8))) {
- for (String s = br.readLine(); s != null; s = br.readLine()) {
- if (s.startsWith("#") || s.length() == 0) { //$NON-NLS-1$
- continue;
- } else if (s.startsWith("version")) { //$NON-NLS-1$
- if (versionLine || s.length() < 8 || s.charAt(7) != ' ') {
- return null; // Not a LFS pointer
- }
- String rest = s.substring(8).trim();
- versionLine = VERSION.equals(rest)
- || VERSION_LEGACY.equals(rest);
- if (!versionLine) {
- return null; // Not a LFS pointer
- }
- } else {
- try {
- if (s.startsWith("oid sha256:")) { //$NON-NLS-1$
- if (id != null) {
- return null; // Not a LFS pointer
- }
- id = LongObjectId
- .fromString(s.substring(11).trim());
- } else if (s.startsWith("size")) { //$NON-NLS-1$
- if (sz > 0 || s.length() < 5
- || s.charAt(4) != ' ') {
- return null; // Not a LFS pointer
- }
- sz = Long.parseLong(s.substring(5).trim());
+ // Comment lines are not mentioned in the spec, the "version" line
+ // MUST be the first, and keys are ordered alphabetically.
+ for (String s = r.readLine(); s != null; s = r.readLine()) {
+ if (s.startsWith("#") || s.length() == 0) { //$NON-NLS-1$
+ continue;
+ } else if (s.startsWith("version")) { //$NON-NLS-1$
+ if (versionLine || !checkVersionLine(s)) {
+ return null; // Not a LFS pointer
+ }
+ versionLine = true;
+ } else {
+ try {
+ if (s.startsWith("oid sha256:")) { //$NON-NLS-1$
+ if (id != null) {
+ return null; // Not a LFS pointer
}
- } catch (RuntimeException e) {
- // We could not parse the line. If we have a version
- // already, this is a corrupt LFS pointer. Otherwise it
- // is just not an LFS pointer.
- if (versionLine) {
- throw e;
+ id = LongObjectId.fromString(s.substring(11).trim());
+ } else if (s.startsWith("size")) { //$NON-NLS-1$
+ if (sz > 0 || s.length() < 5 || s.charAt(4) != ' ') {
+ return null; // Not a LFS pointer
}
- return null;
+ sz = Long.parseLong(s.substring(5).trim());
}
+ } catch (RuntimeException e) {
+ // We could not parse the line. If we have a version
+ // already, this is a corrupt LFS pointer. Otherwise it
+ // is just not an LFS pointer.
+ if (versionLine) {
+ throw e;
+ }
+ return null;
}
}
if (versionLine && id != null && sz > -1) {
@@ -182,6 +240,30 @@ public static LfsPointer parseLfsPointer(InputStream in)
return null;
}
+ private static boolean checkVersion(byte[] data) {
+ // According to the spec at
+ // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
+ // it MUST always be the first line.
+ try (BufferedReader r = new BufferedReader(
+ new InputStreamReader(new ByteArrayInputStream(data), UTF_8))) {
+ String s = r.readLine();
+ if (s != null && s.startsWith("version")) { //$NON-NLS-1$
+ return checkVersionLine(s);
+ }
+ } catch (IOException e) {
+ // Doesn't occur, we're reading from a byte array!
+ }
+ return false;
+ }
+
+ private static boolean checkVersionLine(String s) {
+ if (s.length() < 8 || s.charAt(7) != ' ') {
+ return false; // Not a valid LFS pointer version line
+ }
+ String rest = s.substring(8).trim();
+ return VERSION.equals(rest) || VERSION_LEGACY.equals(rest);
+ }
+
/** {@inheritDoc} */
@Override
public String toString() {
diff --git a/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java b/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java
index 2f80d5b9a..341188756 100644
--- a/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java
+++ b/org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016, Christian Halstrick
* Internally calculates the hash and maintains {@link FileSnapshot}s to
@@ -333,7 +340,7 @@ public void write(URL url) throws IOException, InterruptedException {
path);
// reread new changes if necessary
Set