diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectCheckerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectCheckerTest.java index 380defaa0..4a4e349cf 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectCheckerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectCheckerTest.java @@ -1108,6 +1108,14 @@ public class ObjectCheckerTest { checker.checkTree(data); } + @Test + public void testAcceptTreeModeWithZero() throws CorruptObjectException { + StringBuilder b = new StringBuilder(); + entry(b, "040000 a"); + checker.setAllowLeadingZeroFileMode(true); + checker.checkTree(Constants.encodeASCII(b.toString())); + } + @Test public void testInvalidTreeModeStartsWithZero1() { final StringBuilder b = new StringBuilder(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java index bb67befae..14cb52898 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java @@ -97,6 +97,25 @@ public class ObjectChecker { private final MutableInteger ptrout = new MutableInteger(); + private boolean allowZeroMode; + + /** + * Enable accepting leading zero mode in tree entries. + *
+ * Some broken Git libraries generated leading zeros in the mode part of + * tree entries. This is technically incorrect but gracefully allowed by + * git-core. JGit rejects such trees by default, but may need to accept + * them on broken histories. + * + * @param allow allow leading zero mode. + * @return {@code this}. + * @since 3.4 + */ + public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) { + allowZeroMode = allow; + return this; + } + /** * Check an object for parsing errors. * @@ -308,7 +327,7 @@ public class ObjectChecker { break; if (c < '0' || c > '7') throw new CorruptObjectException("invalid mode character"); - if (thisMode == 0 && c == '0') + if (thisMode == 0 && c == '0' && !allowZeroMode) throw new CorruptObjectException("mode starts with '0'"); thisMode <<= 3; thisMode += c - '0';