ObjectChecker: honor some git-core fsck.* options

Accept some of the same section keys that fsck does in git-core,
allowing repositories to skip over specific kinds of acceptable
broken objects, e.g.:

  [fsck]
    duplicateEntries = ignore
    zeroPaddedFilemode = ignore

The zeroPaddedFilemode = ignore is a synonym for the JGit specific
allowLeadingZeroFileMode = true. Only accept the JGit key if git-core
key was not specified.

Change-Id: Idaed9310e2a5ce5511670ead1aaea2b30aac903c
This commit is contained in:
Shawn Pearce 2015-12-29 18:21:19 -08:00
parent fa7ce0e0f3
commit ac41920a43
7 changed files with 600 additions and 205 deletions

View File

@ -53,7 +53,17 @@
import static org.eclipse.jgit.lib.Constants.OBJ_TREE; import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
import static org.eclipse.jgit.lib.Constants.encode; import static org.eclipse.jgit.lib.Constants.encode;
import static org.eclipse.jgit.lib.Constants.encodeASCII; import static org.eclipse.jgit.lib.Constants.encodeASCII;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.DUPLICATE_ENTRIES;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.EMPTY_NAME;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.FULL_PATHNAME;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTDOT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTGIT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.NULL_SHA1;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.TREE_NOT_SORTED;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -130,7 +140,7 @@ public void testCommitCorruptAuthor() throws CorruptObjectException {
b.append("committer <> 0 +0000\n"); b.append("committer <> 0 +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad date", OBJ_COMMIT, data);
checker.setAllowInvalidPersonIdent(true); checker.setAllowInvalidPersonIdent(true);
checker.checkCommit(data); checker.checkCommit(data);
@ -146,7 +156,7 @@ public void testCommitCorruptCommitter() throws CorruptObjectException {
b.append("committer b <b@c> <b@c> 0 +0000\n"); b.append("committer b <b@c> <b@c> 0 +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid committer", OBJ_COMMIT, data); assertCorrupt("bad date", OBJ_COMMIT, data);
checker.setAllowInvalidPersonIdent(true); checker.setAllowInvalidPersonIdent(true);
checker.checkCommit(data); checker.checkCommit(data);
@ -300,7 +310,6 @@ public void testInvalidCommitInvalidTree3() {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid tree", OBJ_COMMIT, data); assertCorrupt("invalid tree", OBJ_COMMIT, data);
assertSkipListRejects("invalid tree", OBJ_COMMIT, data);
} }
@Test @Test
@ -425,7 +434,7 @@ public void testInvalidCommitInvalidAuthor1()
b.append("author A. U. Thor <foo 1 +0000\n"); b.append("author A. U. Thor <foo 1 +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad email", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -439,7 +448,7 @@ public void testInvalidCommitInvalidAuthor2()
b.append("author A. U. Thor foo> 1 +0000\n"); b.append("author A. U. Thor foo> 1 +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("missing email", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -453,7 +462,7 @@ public void testInvalidCommitInvalidAuthor3()
b.append("author 1 +0000\n"); b.append("author 1 +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("missing email", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -467,7 +476,7 @@ public void testInvalidCommitInvalidAuthor4()
b.append("author a <b> +0000\n"); b.append("author a <b> +0000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad date", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -481,7 +490,7 @@ public void testInvalidCommitInvalidAuthor5()
b.append("author a <b>\n"); b.append("author a <b>\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad date", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -495,7 +504,7 @@ public void testInvalidCommitInvalidAuthor6()
b.append("author a <b> z"); b.append("author a <b> z");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad date", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -509,7 +518,7 @@ public void testInvalidCommitInvalidAuthor7()
b.append("author a <b> 1 z"); b.append("author a <b> 1 z");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid author", OBJ_COMMIT, data); assertCorrupt("bad time zone", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -524,7 +533,7 @@ public void testInvalidCommitInvalidCommitter()
b.append("committer a <"); b.append("committer a <");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid committer", OBJ_COMMIT, data); assertCorrupt("bad email", OBJ_COMMIT, data);
assertSkipListAccepts(OBJ_COMMIT, data); assertSkipListAccepts(OBJ_COMMIT, data);
} }
@ -686,7 +695,7 @@ public void testInvalidTagInvalidTaggerHeader1()
b.append("tagger \n"); b.append("tagger \n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid tagger", OBJ_TAG, data); assertCorrupt("missing email", OBJ_TAG, data);
checker.setAllowInvalidPersonIdent(true); checker.setAllowInvalidPersonIdent(true);
checker.checkTag(data); checker.checkTag(data);
@ -706,7 +715,7 @@ public void testInvalidTagInvalidTaggerHeader3()
b.append("tagger a < 1 +000\n"); b.append("tagger a < 1 +000\n");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid tagger", OBJ_TAG, data); assertCorrupt("bad email", OBJ_TAG, data);
assertSkipListAccepts(OBJ_TAG, data); assertSkipListAccepts(OBJ_TAG, data);
} }
@ -758,6 +767,17 @@ public void testValidTree6() throws CorruptObjectException {
checker.checkTree(encodeASCII(b.toString())); checker.checkTree(encodeASCII(b.toString()));
} }
@Test
public void testNullSha1InTreeEntry() throws CorruptObjectException {
byte[] data = concat(
encodeASCII("100644 A"), new byte[] { '\0' },
new byte[OBJECT_ID_LENGTH]);
assertCorrupt("entry points to null SHA-1", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(NULL_SHA1, true);
checker.checkTree(data);
}
@Test @Test
public void testValidPosixTree() throws CorruptObjectException { public void testValidPosixTree() throws CorruptObjectException {
checkOneName("a<b>c:d|e"); checkOneName("a<b>c:d|e");
@ -842,6 +862,9 @@ public void testAcceptTreeModeWithZero() throws CorruptObjectException {
checker.setAllowLeadingZeroFileMode(false); checker.setAllowLeadingZeroFileMode(false);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(ZERO_PADDED_FILEMODE, true);
checker.checkTree(data);
} }
@Test @Test
@ -905,39 +928,48 @@ public void testInvalidTreeModeMissingName() {
} }
@Test @Test
public void testInvalidTreeNameContainsSlash() { public void testInvalidTreeNameContainsSlash()
throws CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 a/b"); entry(b, "100644 a/b");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("name contains '/'", OBJ_TREE, data); assertCorrupt("name contains '/'", OBJ_TREE, data);
assertSkipListRejects("name contains '/'", OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(FULL_PATHNAME, true);
checker.checkTree(data);
} }
@Test @Test
public void testInvalidTreeNameIsEmpty() { public void testInvalidTreeNameIsEmpty() throws CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 "); entry(b, "100644 ");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("zero length name", OBJ_TREE, data); assertCorrupt("zero length name", OBJ_TREE, data);
assertSkipListRejects("zero length name", OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(EMPTY_NAME, true);
checker.checkTree(data);
} }
@Test @Test
public void testInvalidTreeNameIsDot() { public void testInvalidTreeNameIsDot() throws CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 ."); entry(b, "100644 .");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.'", OBJ_TREE, data); assertCorrupt("invalid name '.'", OBJ_TREE, data);
assertSkipListRejects("invalid name '.'", OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOT, true);
checker.checkTree(data);
} }
@Test @Test
public void testInvalidTreeNameIsDotDot() { public void testInvalidTreeNameIsDotDot() throws CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 .."); entry(b, "100644 ..");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '..'", OBJ_TREE, data); assertCorrupt("invalid name '..'", OBJ_TREE, data);
assertSkipListRejects("invalid name '..'", OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTDOT, true);
checker.checkTree(data);
} }
@Test @Test
@ -947,6 +979,8 @@ public void testInvalidTreeNameIsGit() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.git'", OBJ_TREE, data); assertCorrupt("invalid name '.git'", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -957,6 +991,8 @@ public void testInvalidTreeNameIsMixedCaseGit()
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.GiT'", OBJ_TREE, data); assertCorrupt("invalid name '.GiT'", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -974,6 +1010,8 @@ public void testInvalidTreeNameIsMacHFSGit() throws CorruptObjectException {
"invalid name '.gi\u200Ct' contains ignorable Unicode characters", "invalid name '.gi\u200Ct' contains ignorable Unicode characters",
OBJ_TREE, data); OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -992,6 +1030,8 @@ public void testInvalidTreeNameIsMacHFSGit2()
"invalid name '\u206B.git' contains ignorable Unicode characters", "invalid name '\u206B.git' contains ignorable Unicode characters",
OBJ_TREE, data); OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1010,12 +1050,22 @@ public void testInvalidTreeNameIsMacHFSGit3()
"invalid name '.git\uFEFF' contains ignorable Unicode characters", "invalid name '.git\uFEFF' contains ignorable Unicode characters",
OBJ_TREE, data); OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
private static byte[] concat(byte[] b1, byte[] b2) { private static byte[] concat(byte[]... b) {
byte[] data = new byte[b1.length + b2.length]; int n = 0;
System.arraycopy(b1, 0, data, 0, b1.length); for (byte[] a : b) {
System.arraycopy(b2, 0, data, b1.length, b2.length); n += a.length;
}
byte[] data = new byte[n];
n = 0;
for (byte[] a : b) {
System.arraycopy(a, 0, data, n, a.length);
n += a.length;
}
return data; return data;
} }
@ -1096,6 +1146,8 @@ public void testInvalidTreeNameIsDotGitDot() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.git.'", OBJ_TREE, data); assertCorrupt("invalid name '.git.'", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1114,6 +1166,8 @@ public void testInvalidTreeNameIsDotGitSpace()
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.git '", OBJ_TREE, data); assertCorrupt("invalid name '.git '", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1160,6 +1214,8 @@ public void testInvalidTreeNameIsDotGitDotSpace()
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.git. '", OBJ_TREE, data); assertCorrupt("invalid name '.git. '", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1170,6 +1226,8 @@ public void testInvalidTreeNameIsDotGitSpaceDot()
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name '.git . '", OBJ_TREE, data); assertCorrupt("invalid name '.git . '", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1179,6 +1237,8 @@ public void testInvalidTreeNameIsGITTilde1() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name 'GIT~1'", OBJ_TREE, data); assertCorrupt("invalid name 'GIT~1'", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1188,6 +1248,8 @@ public void testInvalidTreeNameIsGiTTilde1() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("invalid name 'GiT~1'", OBJ_TREE, data); assertCorrupt("invalid name 'GiT~1'", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(HAS_DOTGIT, true);
checker.checkTree(data);
} }
@Test @Test
@ -1222,8 +1284,22 @@ public void testInvalidTreeBadSorting1() throws CorruptObjectException {
entry(b, "100644 foobar"); entry(b, "100644 foobar");
entry(b, "100644 fooaaa"); entry(b, "100644 fooaaa");
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("incorrectly sorted", OBJ_TREE, data); assertCorrupt("incorrectly sorted", OBJ_TREE, data);
ObjectId id = idFor(OBJ_TREE, data);
try {
checker.check(id, OBJ_TREE, data);
fail("Did not throw CorruptObjectException");
} catch (CorruptObjectException e) {
assertSame(TREE_NOT_SORTED, e.getErrorType());
assertEquals("treeNotSorted: object " + id.name()
+ ": incorrectly sorted", e.getMessage());
}
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(TREE_NOT_SORTED, true);
checker.checkTree(data);
} }
@Test @Test
@ -1234,6 +1310,8 @@ public void testInvalidTreeBadSorting2() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("incorrectly sorted", OBJ_TREE, data); assertCorrupt("incorrectly sorted", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(TREE_NOT_SORTED, true);
checker.checkTree(data);
} }
@Test @Test
@ -1244,6 +1322,8 @@ public void testInvalidTreeBadSorting3() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("incorrectly sorted", OBJ_TREE, data); assertCorrupt("incorrectly sorted", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(TREE_NOT_SORTED, true);
checker.checkTree(data);
} }
@Test @Test
@ -1254,6 +1334,8 @@ public void testInvalidTreeDuplicateNames1() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
@ -1264,6 +1346,8 @@ public void testInvalidTreeDuplicateNames2() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
@ -1274,6 +1358,8 @@ public void testInvalidTreeDuplicateNames3() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
@ -1288,30 +1374,36 @@ public void testInvalidTreeDuplicateNames4() throws CorruptObjectException {
byte[] data = encodeASCII(b.toString()); byte[] data = encodeASCII(b.toString());
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
public void testInvalidTreeDuplicateNames5() public void testInvalidTreeDuplicateNames5()
throws UnsupportedEncodingException, CorruptObjectException { throws UnsupportedEncodingException, CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 a");
entry(b, "100644 A"); entry(b, "100644 A");
entry(b, "100644 a");
byte[] data = b.toString().getBytes("UTF-8"); byte[] data = b.toString().getBytes("UTF-8");
checker.setSafeForWindows(true); checker.setSafeForWindows(true);
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
public void testInvalidTreeDuplicateNames6() public void testInvalidTreeDuplicateNames6()
throws UnsupportedEncodingException, CorruptObjectException { throws UnsupportedEncodingException, CorruptObjectException {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
entry(b, "100644 a");
entry(b, "100644 A"); entry(b, "100644 A");
entry(b, "100644 a");
byte[] data = b.toString().getBytes("UTF-8"); byte[] data = b.toString().getBytes("UTF-8");
checker.setSafeForMacOS(true); checker.setSafeForMacOS(true);
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test
@ -1324,6 +1416,8 @@ public void testInvalidTreeDuplicateNames7()
checker.setSafeForMacOS(true); checker.setSafeForMacOS(true);
assertCorrupt("duplicate entry names", OBJ_TREE, data); assertCorrupt("duplicate entry names", OBJ_TREE, data);
assertSkipListAccepts(OBJ_TREE, data); assertSkipListAccepts(OBJ_TREE, data);
checker.setIgnore(DUPLICATE_ENTRIES, true);
checker.checkTree(data);
} }
@Test @Test

View File

@ -126,14 +126,15 @@ connectionFailed=connection failed
connectionTimeOut=Connection time out: {0} connectionTimeOut=Connection time out: {0}
contextMustBeNonNegative=context must be >= 0 contextMustBeNonNegative=context must be >= 0
corruptionDetectedReReadingAt=Corruption detected re-reading at {0} corruptionDetectedReReadingAt=Corruption detected re-reading at {0}
corruptObjectBadDate=bad date
corruptObjectBadEmail=bad email
corruptObjectBadStream=bad stream corruptObjectBadStream=bad stream
corruptObjectBadStreamCorruptHeader=bad stream, corrupt header corruptObjectBadStreamCorruptHeader=bad stream, corrupt header
corruptObjectBadTimezone=bad time zone
corruptObjectDuplicateEntryNames=duplicate entry names corruptObjectDuplicateEntryNames=duplicate entry names
corruptObjectGarbageAfterSize=garbage after size corruptObjectGarbageAfterSize=garbage after size
corruptObjectIncorrectLength=incorrect length corruptObjectIncorrectLength=incorrect length
corruptObjectIncorrectSorting=incorrectly sorted corruptObjectIncorrectSorting=incorrectly sorted
corruptObjectInvalidAuthor=invalid author
corruptObjectInvalidCommitter=invalid committer
corruptObjectInvalidEntryMode=invalid entry mode corruptObjectInvalidEntryMode=invalid entry mode
corruptObjectInvalidMode=invalid mode corruptObjectInvalidMode=invalid mode
corruptObjectInvalidModeChar=invalid mode character corruptObjectInvalidModeChar=invalid mode character
@ -152,11 +153,11 @@ corruptObjectInvalidNameNul=invalid name 'NUL'
corruptObjectInvalidNamePrn=invalid name 'PRN' corruptObjectInvalidNamePrn=invalid name 'PRN'
corruptObjectInvalidObject=invalid object corruptObjectInvalidObject=invalid object
corruptObjectInvalidParent=invalid parent corruptObjectInvalidParent=invalid parent
corruptObjectInvalidTagger=invalid tagger
corruptObjectInvalidTree=invalid tree corruptObjectInvalidTree=invalid tree
corruptObjectInvalidType=invalid type corruptObjectInvalidType=invalid type
corruptObjectInvalidType2=invalid type {0} corruptObjectInvalidType2=invalid type {0}
corruptObjectMalformedHeader=malformed header: {0} corruptObjectMalformedHeader=malformed header: {0}
corruptObjectMissingEmail=missing email
corruptObjectNameContainsByte=name contains byte 0x%x corruptObjectNameContainsByte=name contains byte 0x%x
corruptObjectNameContainsChar=name contains '%c' corruptObjectNameContainsChar=name contains '%c'
corruptObjectNameContainsNullByte=name contains byte 0x00 corruptObjectNameContainsNullByte=name contains byte 0x00
@ -182,6 +183,7 @@ corruptObjectPackfileChecksumIncorrect=Packfile checksum incorrect.
corruptObjectTruncatedInMode=truncated in mode corruptObjectTruncatedInMode=truncated in mode
corruptObjectTruncatedInName=truncated in name corruptObjectTruncatedInName=truncated in name
corruptObjectTruncatedInObjectId=truncated in object id corruptObjectTruncatedInObjectId=truncated in object id
corruptObjectZeroId=entry points to null SHA-1
couldNotCheckOutBecauseOfConflicts=Could not check out because of conflicts couldNotCheckOutBecauseOfConflicts=Could not check out because of conflicts
couldNotDeleteLockFileShouldNotHappen=Could not delete lock file. Should not happen couldNotDeleteLockFileShouldNotHappen=Could not delete lock file. Should not happen
couldNotDeleteTemporaryIndexFileShouldNotHappen=Could not delete temporary index file. Should not happen couldNotDeleteTemporaryIndexFileShouldNotHappen=Could not delete temporary index file. Should not happen
@ -433,6 +435,7 @@ noXMLParserAvailable=No XML parser available.
objectAtHasBadZlibStream=Object at {0} in {1} has bad zlib stream objectAtHasBadZlibStream=Object at {0} in {1} has bad zlib stream
objectAtPathDoesNotHaveId=Object at path "{0}" does not have an id assigned. All object ids must be assigned prior to writing a tree. objectAtPathDoesNotHaveId=Object at path "{0}" does not have an id assigned. All object ids must be assigned prior to writing a tree.
objectIsCorrupt=Object {0} is corrupt: {1} objectIsCorrupt=Object {0} is corrupt: {1}
objectIsCorrupt3={0}: object {1}: {2}
objectIsNotA=Object {0} is not a {1}. objectIsNotA=Object {0} is not a {1}.
objectNotFound=Object {0} not found. objectNotFound=Object {0} not found.
objectNotFoundIn=Object {0} not found in {1}. objectNotFoundIn=Object {0} not found in {1}.

View File

@ -49,8 +49,10 @@
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectChecker;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
/** /**
@ -59,15 +61,24 @@
public class CorruptObjectException extends IOException { public class CorruptObjectException extends IOException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private ObjectChecker.ErrorType errorType;
/** /**
* Construct a CorruptObjectException for reporting a problem specified * Report a specific error condition discovered in an object.
* object id
* *
* @param type
* type of error
* @param id * @param id
* identity of the bad object
* @param why * @param why
* description of the error.
* @since 4.2
*/ */
public CorruptObjectException(final AnyObjectId id, final String why) { public CorruptObjectException(ObjectChecker.ErrorType type, AnyObjectId id,
this(id.toObjectId(), why); String why) {
super(MessageFormat.format(JGitText.get().objectIsCorrupt3,
type.getMessageId(), id.name(), why));
this.errorType = type;
} }
/** /**
@ -77,7 +88,18 @@ public CorruptObjectException(final AnyObjectId id, final String why) {
* @param id * @param id
* @param why * @param why
*/ */
public CorruptObjectException(final ObjectId id, final String why) { public CorruptObjectException(AnyObjectId id, String why) {
super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
}
/**
* Construct a CorruptObjectException for reporting a problem specified
* object id
*
* @param id
* @param why
*/
public CorruptObjectException(ObjectId id, String why) {
super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why)); super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
} }
@ -87,7 +109,7 @@ public CorruptObjectException(final ObjectId id, final String why) {
* *
* @param why * @param why
*/ */
public CorruptObjectException(final String why) { public CorruptObjectException(String why) {
super(why); super(why);
} }
@ -105,4 +127,15 @@ public CorruptObjectException(String why, Throwable cause) {
super(why); super(why);
initCause(cause); initCause(cause);
} }
/**
* Specific error condition identified by {@link ObjectChecker}.
*
* @return error condition or null.
* @since 4.2
*/
@Nullable
public ObjectChecker.ErrorType getErrorType() {
return errorType;
}
} }

View File

@ -185,14 +185,15 @@ public static JGitText get() {
/***/ public String connectionTimeOut; /***/ public String connectionTimeOut;
/***/ public String contextMustBeNonNegative; /***/ public String contextMustBeNonNegative;
/***/ public String corruptionDetectedReReadingAt; /***/ public String corruptionDetectedReReadingAt;
/***/ public String corruptObjectBadDate;
/***/ public String corruptObjectBadEmail;
/***/ public String corruptObjectBadStream; /***/ public String corruptObjectBadStream;
/***/ public String corruptObjectBadStreamCorruptHeader; /***/ public String corruptObjectBadStreamCorruptHeader;
/***/ public String corruptObjectBadTimezone;
/***/ public String corruptObjectDuplicateEntryNames; /***/ public String corruptObjectDuplicateEntryNames;
/***/ public String corruptObjectGarbageAfterSize; /***/ public String corruptObjectGarbageAfterSize;
/***/ public String corruptObjectIncorrectLength; /***/ public String corruptObjectIncorrectLength;
/***/ public String corruptObjectIncorrectSorting; /***/ public String corruptObjectIncorrectSorting;
/***/ public String corruptObjectInvalidAuthor;
/***/ public String corruptObjectInvalidCommitter;
/***/ public String corruptObjectInvalidEntryMode; /***/ public String corruptObjectInvalidEntryMode;
/***/ public String corruptObjectInvalidMode; /***/ public String corruptObjectInvalidMode;
/***/ public String corruptObjectInvalidModeChar; /***/ public String corruptObjectInvalidModeChar;
@ -211,11 +212,11 @@ public static JGitText get() {
/***/ public String corruptObjectInvalidNamePrn; /***/ public String corruptObjectInvalidNamePrn;
/***/ public String corruptObjectInvalidObject; /***/ public String corruptObjectInvalidObject;
/***/ public String corruptObjectInvalidParent; /***/ public String corruptObjectInvalidParent;
/***/ public String corruptObjectInvalidTagger;
/***/ public String corruptObjectInvalidTree; /***/ public String corruptObjectInvalidTree;
/***/ public String corruptObjectInvalidType; /***/ public String corruptObjectInvalidType;
/***/ public String corruptObjectInvalidType2; /***/ public String corruptObjectInvalidType2;
/***/ public String corruptObjectMalformedHeader; /***/ public String corruptObjectMalformedHeader;
/***/ public String corruptObjectMissingEmail;
/***/ public String corruptObjectNameContainsByte; /***/ public String corruptObjectNameContainsByte;
/***/ public String corruptObjectNameContainsChar; /***/ public String corruptObjectNameContainsChar;
/***/ public String corruptObjectNameContainsNullByte; /***/ public String corruptObjectNameContainsNullByte;
@ -241,6 +242,7 @@ public static JGitText get() {
/***/ public String corruptObjectTruncatedInMode; /***/ public String corruptObjectTruncatedInMode;
/***/ public String corruptObjectTruncatedInName; /***/ public String corruptObjectTruncatedInName;
/***/ public String corruptObjectTruncatedInObjectId; /***/ public String corruptObjectTruncatedInObjectId;
/***/ public String corruptObjectZeroId;
/***/ public String corruptPack; /***/ public String corruptPack;
/***/ public String couldNotCheckOutBecauseOfConflicts; /***/ public String couldNotCheckOutBecauseOfConflicts;
/***/ public String couldNotDeleteLockFileShouldNotHappen; /***/ public String couldNotDeleteLockFileShouldNotHappen;
@ -492,6 +494,7 @@ public static JGitText get() {
/***/ public String objectAtHasBadZlibStream; /***/ public String objectAtHasBadZlibStream;
/***/ public String objectAtPathDoesNotHaveId; /***/ public String objectAtPathDoesNotHaveId;
/***/ public String objectIsCorrupt; /***/ public String objectIsCorrupt;
/***/ public String objectIsCorrupt3;
/***/ public String objectIsNotA; /***/ public String objectIsNotA;
/***/ public String objectNotFound; /***/ public String objectNotFound;
/***/ public String objectNotFoundIn; /***/ public String objectNotFoundIn;

View File

@ -44,25 +44,56 @@
package org.eclipse.jgit.lib; package org.eclipse.jgit.lib;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
import static org.eclipse.jgit.lib.Constants.OBJ_BAD;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT; import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
import static org.eclipse.jgit.lib.Constants.OBJ_TAG; import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
import static org.eclipse.jgit.lib.Constants.OBJ_TREE; import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
import static org.eclipse.jgit.util.RawParseUtils.match; import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_DATE;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_EMAIL;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_OBJECT_SHA1;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_PARENT_SHA1;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_TIMEZONE;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_TREE_SHA1;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.BAD_UTF8;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.DUPLICATE_ENTRIES;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.EMPTY_NAME;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.FULL_PATHNAME;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTDOT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.HAS_DOTGIT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_AUTHOR;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_COMMITTER;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_EMAIL;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_OBJECT;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_SPACE_BEFORE_DATE;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TAG_ENTRY;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TREE;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.MISSING_TYPE_ENTRY;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.NULL_SHA1;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.TREE_NOT_SORTED;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.UNKNOWN_TYPE;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.WIN32_BAD_NAME;
import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE;
import static org.eclipse.jgit.util.RawParseUtils.nextLF; import static org.eclipse.jgit.util.RawParseUtils.nextLF;
import static org.eclipse.jgit.util.RawParseUtils.parseBase10; import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.text.Normalizer; import java.text.Normalizer;
import java.util.EnumSet;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.util.MutableInteger; import org.eclipse.jgit.util.MutableInteger;
import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.StringUtils;
/** /**
* Verifies that an object is formatted correctly. * Verifies that an object is formatted correctly.
@ -103,11 +134,65 @@ public class ObjectChecker {
/** Header "tagger " */ /** Header "tagger " */
public static final byte[] tagger = Constants.encodeASCII("tagger "); //$NON-NLS-1$ public static final byte[] tagger = Constants.encodeASCII("tagger "); //$NON-NLS-1$
private final MutableObjectId tempId = new MutableObjectId(); /**
private final MutableInteger ptrout = new MutableInteger(); * Potential issues identified by the checker.
*
* @since 4.2
*/
public enum ErrorType {
// @formatter:off
// These names match git-core so that fsck section keys also match.
/***/ NULL_SHA1,
/***/ DUPLICATE_ENTRIES,
/***/ TREE_NOT_SORTED,
/***/ ZERO_PADDED_FILEMODE,
/***/ EMPTY_NAME,
/***/ FULL_PATHNAME,
/***/ HAS_DOT,
/***/ HAS_DOTDOT,
/***/ HAS_DOTGIT,
/***/ BAD_OBJECT_SHA1,
/***/ BAD_PARENT_SHA1,
/***/ BAD_TREE_SHA1,
/***/ MISSING_AUTHOR,
/***/ MISSING_COMMITTER,
/***/ MISSING_OBJECT,
/***/ MISSING_TREE,
/***/ MISSING_TYPE_ENTRY,
/***/ MISSING_TAG_ENTRY,
/***/ BAD_DATE,
/***/ BAD_EMAIL,
/***/ BAD_TIMEZONE,
/***/ MISSING_EMAIL,
/***/ MISSING_SPACE_BEFORE_DATE,
/***/ UNKNOWN_TYPE,
// These are unique to JGit.
/***/ WIN32_BAD_NAME,
/***/ BAD_UTF8;
// @formatter:on
/** @return camelCaseVersion of the name. */
public String getMessageId() {
String n = name();
StringBuilder r = new StringBuilder(n.length());
for (int i = 0; i < n.length(); i++) {
char c = n.charAt(i);
if (c != '_') {
r.append(StringUtils.toLowerCase(c));
} else {
r.append(n.charAt(++i));
}
}
return r.toString();
}
}
private final MutableObjectId tempId = new MutableObjectId();
private final MutableInteger bufPtr = new MutableInteger();
private EnumSet<ErrorType> errors = EnumSet.allOf(ErrorType.class);
private ObjectIdSet skipList; private ObjectIdSet skipList;
private boolean allowZeroMode;
private boolean allowInvalidPersonIdent; private boolean allowInvalidPersonIdent;
private boolean windows; private boolean windows;
private boolean macosx; private boolean macosx;
@ -126,6 +211,42 @@ public ObjectChecker setSkipList(@Nullable ObjectIdSet objects) {
return this; return this;
} }
/**
* Configure error types to be ignored across all objects.
*
* @param ids
* error types to ignore. The caller's set is copied.
* @return {@code this}
* @since 4.2
*/
public ObjectChecker setIgnore(@Nullable Set<ErrorType> ids) {
errors = EnumSet.allOf(ErrorType.class);
if (ids != null) {
errors.removeAll(ids);
}
return this;
}
/**
* Add message type to be ignored across all objects.
*
* @param id
* error type to ignore.
* @param ignore
* true to ignore this error; false to treat the error as an
* error and throw.
* @return {@code this}
* @since 4.2
*/
public ObjectChecker setIgnore(ErrorType id, boolean ignore) {
if (ignore) {
errors.remove(id);
} else {
errors.add(id);
}
return this;
}
/** /**
* Enable accepting leading zero mode in tree entries. * Enable accepting leading zero mode in tree entries.
* <p> * <p>
@ -133,14 +254,15 @@ public ObjectChecker setSkipList(@Nullable ObjectIdSet objects) {
* tree entries. This is technically incorrect but gracefully allowed by * tree entries. This is technically incorrect but gracefully allowed by
* git-core. JGit rejects such trees by default, but may need to accept * git-core. JGit rejects such trees by default, but may need to accept
* them on broken histories. * them on broken histories.
* <p>
* Same as {@code setIgnore(ZERO_PADDED_FILEMODE, allow)}.
* *
* @param allow allow leading zero mode. * @param allow allow leading zero mode.
* @return {@code this}. * @return {@code this}.
* @since 3.4 * @since 3.4
*/ */
public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) { public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) {
allowZeroMode = allow; return setIgnore(ZERO_PADDED_FILEMODE, allow);
return this;
} }
/** /**
@ -238,50 +360,80 @@ public void check(@Nullable AnyObjectId id, int objType, byte[] raw)
checkBlob(raw); checkBlob(raw);
break; break;
default: default:
throw new CorruptObjectException(MessageFormat.format( report(UNKNOWN_TYPE, id, MessageFormat.format(
JGitText.get().corruptObjectInvalidType2, JGitText.get().corruptObjectInvalidType2,
Integer.valueOf(objType))); Integer.valueOf(objType)));
} }
} }
private int id(final byte[] raw, final int ptr) { private boolean checkId(byte[] raw) {
int p = bufPtr.value;
try { try {
tempId.fromString(raw, ptr); tempId.fromString(raw, p);
return ptr + Constants.OBJECT_ID_STRING_LENGTH;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return -1; bufPtr.value = nextLF(raw, p);
} return false;
} }
private int personIdent(byte[] raw, int ptr, @Nullable AnyObjectId id) { p += OBJECT_ID_STRING_LENGTH;
if (allowInvalidPersonIdent || skip(id)) if (raw[p] == '\n') {
return nextLF(raw, ptr); bufPtr.value = p + 1;
return true;
}
bufPtr.value = nextLF(raw, p);
return false;
}
final int emailB = nextLF(raw, ptr, '<'); private void checkPersonIdent(byte[] raw, @Nullable AnyObjectId id)
if (emailB == ptr || raw[emailB - 1] != '<') throws CorruptObjectException {
return -1; if (allowInvalidPersonIdent) {
bufPtr.value = nextLF(raw, bufPtr.value);
return;
}
final int emailB = nextLF(raw, bufPtr.value, '<');
if (emailB == bufPtr.value || raw[emailB - 1] != '<') {
report(MISSING_EMAIL, id, JGitText.get().corruptObjectMissingEmail);
bufPtr.value = nextLF(raw, bufPtr.value);
return;
}
final int emailE = nextLF(raw, emailB, '>'); final int emailE = nextLF(raw, emailB, '>');
if (emailE == emailB || raw[emailE - 1] != '>') if (emailE == emailB || raw[emailE - 1] != '>') {
return -1; report(BAD_EMAIL, id, JGitText.get().corruptObjectBadEmail);
if (emailE == raw.length || raw[emailE] != ' ') bufPtr.value = nextLF(raw, bufPtr.value);
return -1; return;
}
if (emailE == raw.length || raw[emailE] != ' ') {
report(MISSING_SPACE_BEFORE_DATE, id,
JGitText.get().corruptObjectBadDate);
bufPtr.value = nextLF(raw, bufPtr.value);
return;
}
parseBase10(raw, emailE + 1, ptrout); // when parseBase10(raw, emailE + 1, bufPtr); // when
ptr = ptrout.value; if (emailE + 1 == bufPtr.value || bufPtr.value == raw.length
if (emailE + 1 == ptr) || raw[bufPtr.value] != ' ') {
return -1; report(BAD_DATE, id, JGitText.get().corruptObjectBadDate);
if (ptr == raw.length || raw[ptr] != ' ') bufPtr.value = nextLF(raw, bufPtr.value);
return -1; return;
}
parseBase10(raw, ptr + 1, ptrout); // tz offset int p = bufPtr.value + 1;
if (ptr + 1 == ptrout.value) parseBase10(raw, p, bufPtr); // tz offset
return -1; if (p == bufPtr.value) {
report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
bufPtr.value = nextLF(raw, bufPtr.value);
return;
}
ptr = ptrout.value; p = bufPtr.value;
if (raw[ptr++] == '\n') if (raw[p] == '\n') {
return ptr; bufPtr.value = p + 1;
return -1; } else {
report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
bufPtr.value = nextLF(raw, p);
}
} }
/** /**
@ -309,41 +461,31 @@ public void checkCommit(byte[] raw) throws CorruptObjectException {
*/ */
public void checkCommit(@Nullable AnyObjectId id, byte[] raw) public void checkCommit(@Nullable AnyObjectId id, byte[] raw)
throws CorruptObjectException { throws CorruptObjectException {
int ptr = 0; bufPtr.value = 0;
if ((ptr = match(raw, ptr, tree)) < 0) if (!match(raw, tree)) {
throw new CorruptObjectException( report(MISSING_TREE, id, JGitText.get().corruptObjectNotreeHeader);
JGitText.get().corruptObjectNotreeHeader); } else if (!checkId(raw)) {
if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n') report(BAD_TREE_SHA1, id, JGitText.get().corruptObjectInvalidTree);
throw new CorruptObjectException( }
JGitText.get().corruptObjectInvalidTree);
while (match(raw, ptr, parent) >= 0) { while (match(raw, parent)) {
ptr += parent.length; if (!checkId(raw)) {
if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n') report(BAD_PARENT_SHA1, id,
throw new CorruptObjectException(
JGitText.get().corruptObjectInvalidParent); JGitText.get().corruptObjectInvalidParent);
} }
int p = match(raw, ptr, author);
if (p > ptr) {
if ((ptr = personIdent(raw, p, id)) < 0) {
throw new CorruptObjectException(
JGitText.get().corruptObjectInvalidAuthor);
}
} else if (!skip(id)) {
throw new CorruptObjectException(
JGitText.get().corruptObjectNoAuthor);
} }
p = match(raw, ptr, committer); if (match(raw, author)) {
if (p > ptr) { checkPersonIdent(raw, id);
if ((ptr = personIdent(raw, p, id)) < 0) { } else {
throw new CorruptObjectException( report(MISSING_AUTHOR, id, JGitText.get().corruptObjectNoAuthor);
JGitText.get().corruptObjectInvalidCommitter);
} }
} else if (!skip(id)) {
throw new CorruptObjectException( if (match(raw, committer)) {
checkPersonIdent(raw, id);
} else {
report(MISSING_COMMITTER, id,
JGitText.get().corruptObjectNoCommitter); JGitText.get().corruptObjectNoCommitter);
} }
} }
@ -373,30 +515,29 @@ public void checkTag(byte[] raw) throws CorruptObjectException {
*/ */
public void checkTag(@Nullable AnyObjectId id, byte[] raw) public void checkTag(@Nullable AnyObjectId id, byte[] raw)
throws CorruptObjectException { throws CorruptObjectException {
int ptr = 0; bufPtr.value = 0;
if (!match(raw, object)) {
if ((ptr = match(raw, ptr, object)) < 0) report(MISSING_OBJECT, id,
throw new CorruptObjectException(
JGitText.get().corruptObjectNoObjectHeader); JGitText.get().corruptObjectNoObjectHeader);
if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n') } else if (!checkId(raw)) {
throw new CorruptObjectException( report(BAD_OBJECT_SHA1, id,
JGitText.get().corruptObjectInvalidObject); JGitText.get().corruptObjectInvalidObject);
if ((ptr = match(raw, ptr, type)) < 0)
throw new CorruptObjectException(
JGitText.get().corruptObjectNoTypeHeader);
ptr = nextLF(raw, ptr);
if (match(raw, ptr, tag) < 0 && !skip(id))
throw new CorruptObjectException(
JGitText.get().corruptObjectNoTagHeader);
ptr = nextLF(raw, ptr);
if ((ptr = match(raw, ptr, tagger)) > 0) {
if ((ptr = personIdent(raw, ptr, id)) < 0) {
throw new CorruptObjectException(
JGitText.get().corruptObjectInvalidTagger);
} }
if (!match(raw, type)) {
report(MISSING_TYPE_ENTRY, id,
JGitText.get().corruptObjectNoTypeHeader);
}
bufPtr.value = nextLF(raw, bufPtr.value);
if (!match(raw, tag)) {
report(MISSING_TAG_ENTRY, id,
JGitText.get().corruptObjectNoTagHeader);
}
bufPtr.value = nextLF(raw, bufPtr.value);
if (match(raw, tagger)) {
checkPersonIdent(raw, id);
} }
} }
@ -485,82 +626,96 @@ public void checkTree(@Nullable AnyObjectId id, byte[] raw)
final int sz = raw.length; final int sz = raw.length;
int ptr = 0; int ptr = 0;
int lastNameB = 0, lastNameE = 0, lastMode = 0; int lastNameB = 0, lastNameE = 0, lastMode = 0;
boolean skip = skip(id); Set<String> normalized = windows || macosx
Set<String> normalized = !skip && (windows || macosx)
? new HashSet<String>() ? new HashSet<String>()
: null; : null;
while (ptr < sz) { while (ptr < sz) {
int thisMode = 0; int thisMode = 0;
for (;;) { for (;;) {
if (ptr == sz) if (ptr == sz) {
throw new CorruptObjectException( throw new CorruptObjectException(
JGitText.get().corruptObjectTruncatedInMode); JGitText.get().corruptObjectTruncatedInMode);
}
final byte c = raw[ptr++]; final byte c = raw[ptr++];
if (' ' == c) if (' ' == c)
break; break;
if (c < '0' || c > '7') if (c < '0' || c > '7') {
throw new CorruptObjectException( throw new CorruptObjectException(
JGitText.get().corruptObjectInvalidModeChar); JGitText.get().corruptObjectInvalidModeChar);
if (thisMode == 0 && c == '0' && !allowZeroMode && !skip) }
throw new CorruptObjectException( if (thisMode == 0 && c == '0') {
report(ZERO_PADDED_FILEMODE, id,
JGitText.get().corruptObjectInvalidModeStartsZero); JGitText.get().corruptObjectInvalidModeStartsZero);
}
thisMode <<= 3; thisMode <<= 3;
thisMode += c - '0'; thisMode += c - '0';
} }
if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD) if (FileMode.fromBits(thisMode).getObjectType() == OBJ_BAD) {
throw new CorruptObjectException(MessageFormat.format( throw new CorruptObjectException(MessageFormat.format(
JGitText.get().corruptObjectInvalidMode2, JGitText.get().corruptObjectInvalidMode2,
Integer.valueOf(thisMode))); Integer.valueOf(thisMode)));
}
final int thisNameB = ptr; final int thisNameB = ptr;
ptr = scanPathSegment(raw, ptr, sz); ptr = scanPathSegment(raw, ptr, sz, id);
if (ptr == sz || raw[ptr] != 0) if (ptr == sz || raw[ptr] != 0) {
throw new CorruptObjectException( throw new CorruptObjectException(
JGitText.get().corruptObjectTruncatedInName); JGitText.get().corruptObjectTruncatedInName);
checkPathSegment2(raw, thisNameB, ptr, skip); }
checkPathSegment2(raw, thisNameB, ptr, id);
if (normalized != null) { if (normalized != null) {
if (!normalized.add(normalize(raw, thisNameB, ptr))) if (!normalized.add(normalize(raw, thisNameB, ptr))) {
throw new CorruptObjectException( report(DUPLICATE_ENTRIES, id,
JGitText.get().corruptObjectDuplicateEntryNames); JGitText.get().corruptObjectDuplicateEntryNames);
} else if (!skip && duplicateName(raw, thisNameB, ptr)) }
throw new CorruptObjectException( } else if (duplicateName(raw, thisNameB, ptr)) {
report(DUPLICATE_ENTRIES, id,
JGitText.get().corruptObjectDuplicateEntryNames); JGitText.get().corruptObjectDuplicateEntryNames);
}
if (!skip && lastNameB != 0) { if (lastNameB != 0) {
final int cmp = pathCompare(raw, lastNameB, lastNameE, final int cmp = pathCompare(raw, lastNameB, lastNameE,
lastMode, thisNameB, ptr, thisMode); lastMode, thisNameB, ptr, thisMode);
if (cmp > 0) if (cmp > 0) {
throw new CorruptObjectException( report(TREE_NOT_SORTED, id,
JGitText.get().corruptObjectIncorrectSorting); JGitText.get().corruptObjectIncorrectSorting);
} }
}
lastNameB = thisNameB; lastNameB = thisNameB;
lastNameE = ptr; lastNameE = ptr;
lastMode = thisMode; lastMode = thisMode;
ptr += 1 + Constants.OBJECT_ID_LENGTH; ptr += 1 + OBJECT_ID_LENGTH;
if (ptr > sz) if (ptr > sz) {
throw new CorruptObjectException( throw new CorruptObjectException(
JGitText.get().corruptObjectTruncatedInObjectId); JGitText.get().corruptObjectTruncatedInObjectId);
} }
if (ObjectId.zeroId().compareTo(raw, ptr - OBJECT_ID_LENGTH) == 0) {
report(NULL_SHA1, id, JGitText.get().corruptObjectZeroId);
}
}
} }
private int scanPathSegment(byte[] raw, int ptr, int end) private int scanPathSegment(byte[] raw, int ptr, int end,
throws CorruptObjectException { @Nullable AnyObjectId id) throws CorruptObjectException {
for (; ptr < end; ptr++) { for (; ptr < end; ptr++) {
byte c = raw[ptr]; byte c = raw[ptr];
if (c == 0) if (c == 0) {
return ptr; return ptr;
if (c == '/') }
throw new CorruptObjectException( if (c == '/') {
report(FULL_PATHNAME, id,
JGitText.get().corruptObjectNameContainsSlash); JGitText.get().corruptObjectNameContainsSlash);
}
if (windows && isInvalidOnWindows(c)) { if (windows && isInvalidOnWindows(c)) {
if (c > 31) if (c > 31) {
throw new CorruptObjectException(String.format( throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectNameContainsChar, JGitText.get().corruptObjectNameContainsChar,
Byte.valueOf(c))); Byte.valueOf(c)));
}
throw new CorruptObjectException(String.format( throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectNameContainsByte, JGitText.get().corruptObjectNameContainsByte,
Integer.valueOf(c & 0xff))); Integer.valueOf(c & 0xff)));
@ -578,8 +733,15 @@ private ObjectId idFor(int objType, byte[] raw) {
return null; return null;
} }
private boolean skip(@Nullable AnyObjectId id) { private void report(@NonNull ErrorType err, @Nullable AnyObjectId id,
return skipList != null && id != null && skipList.contains(id); String why) throws CorruptObjectException {
if (errors.contains(err)
&& (id == null || skipList == null || !skipList.contains(id))) {
if (id != null) {
throw new CorruptObjectException(err, id, why);
}
throw new CorruptObjectException(why);
}
} }
/** /**
@ -632,75 +794,82 @@ public void checkPath(byte[] raw, int ptr, int end)
*/ */
public void checkPathSegment(byte[] raw, int ptr, int end) public void checkPathSegment(byte[] raw, int ptr, int end)
throws CorruptObjectException { throws CorruptObjectException {
int e = scanPathSegment(raw, ptr, end); int e = scanPathSegment(raw, ptr, end, null);
if (e < end && raw[e] == 0) if (e < end && raw[e] == 0)
throw new CorruptObjectException( throw new CorruptObjectException(
JGitText.get().corruptObjectNameContainsNullByte); JGitText.get().corruptObjectNameContainsNullByte);
checkPathSegment2(raw, ptr, end, false); checkPathSegment2(raw, ptr, end, null);
}
private void checkPathSegment2(byte[] raw, int ptr, int end,
@Nullable AnyObjectId id) throws CorruptObjectException {
if (ptr == end) {
report(EMPTY_NAME, id, JGitText.get().corruptObjectNameZeroLength);
return;
} }
private void checkPathSegment2(byte[] raw, int ptr, int end, boolean skip)
throws CorruptObjectException {
if (ptr == end)
throw new CorruptObjectException(
JGitText.get().corruptObjectNameZeroLength);
if (raw[ptr] == '.') { if (raw[ptr] == '.') {
switch (end - ptr) { switch (end - ptr) {
case 1: case 1:
throw new CorruptObjectException( report(HAS_DOT, id, JGitText.get().corruptObjectNameDot);
JGitText.get().corruptObjectNameDot); break;
case 2: case 2:
if (raw[ptr + 1] == '.') if (raw[ptr + 1] == '.') {
throw new CorruptObjectException( report(HAS_DOTDOT, id,
JGitText.get().corruptObjectNameDotDot); JGitText.get().corruptObjectNameDotDot);
}
break; break;
case 4: case 4:
if (!skip && isGit(raw, ptr + 1)) if (isGit(raw, ptr + 1)) {
throw new CorruptObjectException(String.format( report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidName, JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end))); RawParseUtils.decode(raw, ptr, end)));
}
break; break;
default: default:
if (!skip && end - ptr > 4 if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end)) {
&& isNormalizedGit(raw, ptr + 1, end)) report(HAS_DOTGIT, id, String.format(
throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectInvalidName, JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end))); RawParseUtils.decode(raw, ptr, end)));
} }
} else if (!skip && isGitTilde1(raw, ptr, end)) { }
throw new CorruptObjectException(String.format( } else if (isGitTilde1(raw, ptr, end)) {
report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidName, JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end))); RawParseUtils.decode(raw, ptr, end)));
} }
if (!skip) { if (macosx && isMacHFSGit(raw, ptr, end, id)) {
if (macosx && isMacHFSGit(raw, ptr, end)) report(HAS_DOTGIT, id, String.format(
throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectInvalidNameIgnorableUnicode, JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
RawParseUtils.decode(raw, ptr, end))); RawParseUtils.decode(raw, ptr, end)));
}
if (windows) { if (windows) {
// Windows ignores space and dot at end of file name. // Windows ignores space and dot at end of file name.
if (raw[end - 1] == ' ' || raw[end - 1] == '.') if (raw[end - 1] == ' ' || raw[end - 1] == '.') {
throw new CorruptObjectException(String.format( report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameEnd, JGitText.get().corruptObjectInvalidNameEnd,
Character.valueOf(((char) raw[end - 1])))); Character.valueOf(((char) raw[end - 1]))));
if (end - ptr >= 3) }
checkNotWindowsDevice(raw, ptr, end); if (end - ptr >= 3) {
checkNotWindowsDevice(raw, ptr, end, id);
} }
} }
} }
// Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters // Mac's HFS+ folds permutations of ".git" and Unicode ignorable characters
// to ".git" therefore we should prevent such names // to ".git" therefore we should prevent such names
private static boolean isMacHFSGit(byte[] raw, int ptr, int end) private boolean isMacHFSGit(byte[] raw, int ptr, int end,
throws CorruptObjectException { @Nullable AnyObjectId id) throws CorruptObjectException {
boolean ignorable = false; boolean ignorable = false;
byte[] git = new byte[] { '.', 'g', 'i', 't' }; byte[] git = new byte[] { '.', 'g', 'i', 't' };
int g = 0; int g = 0;
while (ptr < end) { while (ptr < end) {
switch (raw[ptr]) { switch (raw[ptr]) {
case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192 case (byte) 0xe2: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192
checkTruncatedIgnorableUTF8(raw, ptr, end); if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
return false;
}
switch (raw[ptr + 1]) { switch (raw[ptr + 1]) {
case (byte) 0x80: case (byte) 0x80:
switch (raw[ptr + 2]) { switch (raw[ptr + 2]) {
@ -737,7 +906,9 @@ private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
return false; return false;
} }
case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024 case (byte) 0xef: // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65024
checkTruncatedIgnorableUTF8(raw, ptr, end); if (!checkTruncatedIgnorableUTF8(raw, ptr, end, id)) {
return false;
}
// U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE // U+FEFF 0xefbbbf ZERO WIDTH NO-BREAK SPACE
if ((raw[ptr + 1] == (byte) 0xbb) if ((raw[ptr + 1] == (byte) 0xbb)
&& (raw[ptr + 2] == (byte) 0xbf)) { && (raw[ptr + 2] == (byte) 0xbf)) {
@ -758,12 +929,15 @@ private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
return false; return false;
} }
private static void checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end) private boolean checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end,
throws CorruptObjectException { @Nullable AnyObjectId id) throws CorruptObjectException {
if ((ptr + 2) >= end) if ((ptr + 2) >= end) {
throw new CorruptObjectException(MessageFormat.format( report(BAD_UTF8, id, MessageFormat.format(
JGitText.get().corruptObjectInvalidNameInvalidUtf8, JGitText.get().corruptObjectInvalidNameInvalidUtf8,
toHexString(raw, ptr, end))); toHexString(raw, ptr, end)));
return false;
}
return true;
} }
private static String toHexString(byte[] raw, int ptr, int end) { private static String toHexString(byte[] raw, int ptr, int end) {
@ -773,33 +947,36 @@ private static String toHexString(byte[] raw, int ptr, int end) {
return b.toString(); return b.toString();
} }
private static void checkNotWindowsDevice(byte[] raw, int ptr, int end) private void checkNotWindowsDevice(byte[] raw, int ptr, int end,
throws CorruptObjectException { @Nullable AnyObjectId id) throws CorruptObjectException {
switch (toLower(raw[ptr])) { switch (toLower(raw[ptr])) {
case 'a': // AUX case 'a': // AUX
if (end - ptr >= 3 if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'u' && toLower(raw[ptr + 1]) == 'u'
&& toLower(raw[ptr + 2]) == 'x' && toLower(raw[ptr + 2]) == 'x'
&& (end - ptr == 3 || raw[ptr + 3] == '.')) && (end - ptr == 3 || raw[ptr + 3] == '.')) {
throw new CorruptObjectException( report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameAux); JGitText.get().corruptObjectInvalidNameAux);
}
break; break;
case 'c': // CON, COM[1-9] case 'c': // CON, COM[1-9]
if (end - ptr >= 3 if (end - ptr >= 3
&& toLower(raw[ptr + 2]) == 'n' && toLower(raw[ptr + 2]) == 'n'
&& toLower(raw[ptr + 1]) == 'o' && toLower(raw[ptr + 1]) == 'o'
&& (end - ptr == 3 || raw[ptr + 3] == '.')) && (end - ptr == 3 || raw[ptr + 3] == '.')) {
throw new CorruptObjectException( report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameCon); JGitText.get().corruptObjectInvalidNameCon);
}
if (end - ptr >= 4 if (end - ptr >= 4
&& toLower(raw[ptr + 2]) == 'm' && toLower(raw[ptr + 2]) == 'm'
&& toLower(raw[ptr + 1]) == 'o' && toLower(raw[ptr + 1]) == 'o'
&& isPositiveDigit(raw[ptr + 3]) && isPositiveDigit(raw[ptr + 3])
&& (end - ptr == 4 || raw[ptr + 4] == '.')) && (end - ptr == 4 || raw[ptr + 4] == '.')) {
throw new CorruptObjectException(String.format( report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameCom, JGitText.get().corruptObjectInvalidNameCom,
Character.valueOf(((char) raw[ptr + 3])))); Character.valueOf(((char) raw[ptr + 3]))));
}
break; break;
case 'l': // LPT[1-9] case 'l': // LPT[1-9]
@ -807,28 +984,31 @@ && isPositiveDigit(raw[ptr + 3])
&& toLower(raw[ptr + 1]) == 'p' && toLower(raw[ptr + 1]) == 'p'
&& toLower(raw[ptr + 2]) == 't' && toLower(raw[ptr + 2]) == 't'
&& isPositiveDigit(raw[ptr + 3]) && isPositiveDigit(raw[ptr + 3])
&& (end - ptr == 4 || raw[ptr + 4] == '.')) && (end - ptr == 4 || raw[ptr + 4] == '.')) {
throw new CorruptObjectException(String.format( report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameLpt, JGitText.get().corruptObjectInvalidNameLpt,
Character.valueOf(((char) raw[ptr + 3])))); Character.valueOf(((char) raw[ptr + 3]))));
}
break; break;
case 'n': // NUL case 'n': // NUL
if (end - ptr >= 3 if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'u' && toLower(raw[ptr + 1]) == 'u'
&& toLower(raw[ptr + 2]) == 'l' && toLower(raw[ptr + 2]) == 'l'
&& (end - ptr == 3 || raw[ptr + 3] == '.')) && (end - ptr == 3 || raw[ptr + 3] == '.')) {
throw new CorruptObjectException( report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameNul); JGitText.get().corruptObjectInvalidNameNul);
}
break; break;
case 'p': // PRN case 'p': // PRN
if (end - ptr >= 3 if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'r' && toLower(raw[ptr + 1]) == 'r'
&& toLower(raw[ptr + 2]) == 'n' && toLower(raw[ptr + 2]) == 'n'
&& (end - ptr == 3 || raw[ptr + 3] == '.')) && (end - ptr == 3 || raw[ptr + 3] == '.')) {
throw new CorruptObjectException( report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNamePrn); JGitText.get().corruptObjectInvalidNamePrn);
}
break; break;
} }
} }
@ -881,6 +1061,15 @@ else if (raw[p] == ' ')
return false; return false;
} }
private boolean match(byte[] b, byte[] src) {
int r = RawParseUtils.match(b, bufPtr.value, src);
if (r < 0) {
return false;
}
bufPtr.value = r;
return true;
}
private static char toLower(byte b) { private static char toLower(byte b) {
if ('A' <= b && b <= 'Z') if ('A' <= b && b <= 'Z')
return (char) (b + ('a' - 'A')); return (char) (b + ('a' - 'A'));

View File

@ -1051,6 +1051,9 @@ private void verifySafeObject(final AnyObjectId id, final int type,
try { try {
objCheck.check(id, type, data); objCheck.check(id, type, data);
} catch (CorruptObjectException e) { } catch (CorruptObjectException e) {
if (e.getErrorType() != null) {
throw e;
}
throw new CorruptObjectException(MessageFormat.format( throw new CorruptObjectException(MessageFormat.format(
JGitText.get().invalidObject, JGitText.get().invalidObject,
Constants.typeString(type), Constants.typeString(type),

View File

@ -43,7 +43,11 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import static org.eclipse.jgit.util.StringUtils.equalsIgnoreCase;
import static org.eclipse.jgit.util.StringUtils.toLowerCase;
import java.io.File; import java.io.File;
import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -62,6 +66,8 @@
* parameters. * parameters.
*/ */
public class TransferConfig { public class TransferConfig {
private static final String FSCK = "fsck"; //$NON-NLS-1$
/** Key for {@link Config#get(SectionParser)}. */ /** Key for {@link Config#get(SectionParser)}. */
public static final Config.SectionParser<TransferConfig> KEY = new SectionParser<TransferConfig>() { public static final Config.SectionParser<TransferConfig> KEY = new SectionParser<TransferConfig>() {
public TransferConfig parse(final Config cfg) { public TransferConfig parse(final Config cfg) {
@ -69,10 +75,14 @@ public TransferConfig parse(final Config cfg) {
} }
}; };
enum FsckMode {
ERROR, WARN, IGNORE;
}
private final boolean fetchFsck; private final boolean fetchFsck;
private final boolean receiveFsck; private final boolean receiveFsck;
private final String fsckSkipList; private final String fsckSkipList;
private final boolean allowLeadingZeroFileMode; private final EnumSet<ObjectChecker.ErrorType> ignore;
private final boolean allowInvalidPersonIdent; private final boolean allowInvalidPersonIdent;
private final boolean safeForWindows; private final boolean safeForWindows;
private final boolean safeForMacOS; private final boolean safeForMacOS;
@ -88,14 +98,44 @@ public TransferConfig parse(final Config cfg) {
boolean fsck = rc.getBoolean("transfer", "fsckobjects", false); //$NON-NLS-1$ //$NON-NLS-2$ boolean fsck = rc.getBoolean("transfer", "fsckobjects", false); //$NON-NLS-1$ //$NON-NLS-2$
fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$ fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$
receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$ receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$
fsckSkipList = rc.getString("fsck", null, "skipList"); //$NON-NLS-1$ //$NON-NLS-2$ fsckSkipList = rc.getString(FSCK, null, "skipList"); //$NON-NLS-1$
allowLeadingZeroFileMode = rc.getBoolean("fsck", "allowLeadingZeroFileMode", false); //$NON-NLS-1$ //$NON-NLS-2$ allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent", false); //$NON-NLS-1$
allowInvalidPersonIdent = rc.getBoolean("fsck", "allowInvalidPersonIdent", false); //$NON-NLS-1$ //$NON-NLS-2$ safeForWindows = rc.getBoolean(FSCK, "safeForWindows", //$NON-NLS-1$
safeForWindows = rc.getBoolean("fsck", "safeForWindows", //$NON-NLS-1$ //$NON-NLS-2$
SystemReader.getInstance().isWindows()); SystemReader.getInstance().isWindows());
safeForMacOS = rc.getBoolean("fsck", "safeForMacOS", //$NON-NLS-1$ //$NON-NLS-2$ safeForMacOS = rc.getBoolean(FSCK, "safeForMacOS", //$NON-NLS-1$
SystemReader.getInstance().isMacOS()); SystemReader.getInstance().isMacOS());
ignore = EnumSet.noneOf(ObjectChecker.ErrorType.class);
EnumSet<ObjectChecker.ErrorType> set = EnumSet
.noneOf(ObjectChecker.ErrorType.class);
for (String key : rc.getNames(FSCK)) {
if (equalsIgnoreCase(key, "skipList") //$NON-NLS-1$
|| equalsIgnoreCase(key, "allowLeadingZeroFileMode") //$NON-NLS-1$
|| equalsIgnoreCase(key, "allowInvalidPersonIdent") //$NON-NLS-1$
|| equalsIgnoreCase(key, "safeForWindows") //$NON-NLS-1$
|| equalsIgnoreCase(key, "safeForMacOS")) { //$NON-NLS-1$
continue;
}
ObjectChecker.ErrorType id = FsckKeyNameHolder.parse(key);
if (id != null) {
switch (rc.getEnum(FSCK, null, key, FsckMode.ERROR)) {
case ERROR:
ignore.remove(id);
break;
case WARN:
case IGNORE:
ignore.add(id);
break;
}
set.add(id);
}
}
if (!set.contains(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE)
&& rc.getBoolean(FSCK, "allowLeadingZeroFileMode", false)) { //$NON-NLS-1$
ignore.add(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE);
}
allowTipSha1InWant = rc.getBoolean( allowTipSha1InWant = rc.getBoolean(
"uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$ "uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
allowReachableSha1InWant = rc.getBoolean( allowReachableSha1InWant = rc.getBoolean(
@ -128,7 +168,7 @@ private ObjectChecker newObjectChecker(boolean check) {
return null; return null;
} }
return new ObjectChecker() return new ObjectChecker()
.setAllowLeadingZeroFileMode(allowLeadingZeroFileMode) .setIgnore(ignore)
.setAllowInvalidPersonIdent(allowInvalidPersonIdent) .setAllowInvalidPersonIdent(allowInvalidPersonIdent)
.setSafeForWindows(safeForWindows) .setSafeForWindows(safeForWindows)
.setSafeForMacOS(safeForMacOS) .setSafeForMacOS(safeForMacOS)
@ -188,4 +228,34 @@ private boolean prefixMatch(String p, String s) {
} }
}; };
} }
static class FsckKeyNameHolder {
private static final Map<String, ObjectChecker.ErrorType> errors;
static {
errors = new HashMap<>();
for (ObjectChecker.ErrorType m : ObjectChecker.ErrorType.values()) {
errors.put(keyNameFor(m.name()), m);
}
}
@Nullable
static ObjectChecker.ErrorType parse(String key) {
return errors.get(toLowerCase(key));
}
private static String keyNameFor(String name) {
StringBuilder r = new StringBuilder(name.length());
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c != '_') {
r.append(c);
}
}
return toLowerCase(r.toString());
}
private FsckKeyNameHolder() {
}
}
} }