]> source.dussan.org Git - jgit.git/commitdiff
Permit ObjectChecker to optionally accept leading '0' in trees 24/23224/3
authorShawn Pearce <spearce@spearce.org>
Wed, 12 Mar 2014 00:19:37 +0000 (17:19 -0700)
committerShawn Pearce <spearce@spearce.org>
Wed, 12 Mar 2014 22:43:20 +0000 (15:43 -0700)
The leading '0' is a broken mode that although incorrect in the
Git canonical tree format was created by a couple of libraries
frequently used on a popular Git hosting site. Some projects have
these modes stuck in their ancient history and cannot easily
repair the damage without a full history rewrite. Optionally permit
ObjectChecker to ignore them.

Bug: 307291
Change-Id: Ib921dfd77ce757e89280d1c00328a88430daef35

org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ObjectCheckerTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java

index 380defaa08ca6d3f8d87c9f4b355f9f92cac1235..4a4e349cfd956d9ee4f05d4951720104218c49cf 100644 (file)
@@ -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();
index bb67befae1c0365b9d618e09b06542aa22c800dc..14cb5289840d08ef86d0e98b1b5d19d883507541 100644 (file)
@@ -97,6 +97,25 @@ public class ObjectChecker {
 
        private final MutableInteger ptrout = new MutableInteger();
 
+       private boolean allowZeroMode;
+
+       /**
+        * Enable accepting leading zero mode in tree entries.
+        * <p>
+        * 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';