aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2016-01-03 10:27:36 -0500
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2016-01-03 10:27:37 -0500
commit91a3f1747460e71ef310d9d989b3deea1226336a (patch)
treee589c9177a27e2597384c1046d1f5681fb3bef7a /org.eclipse.jgit
parent63bb0bcd4af3e70afe13173b4e3b9173fcb8eaea (diff)
parentac41920a430941b9385889d19e0fc2764ba23474 (diff)
downloadjgit-91a3f1747460e71ef310d9d989b3deea1226336a.tar.gz
jgit-91a3f1747460e71ef310d9d989b3deea1226336a.zip
Merge changes Idaed9310,I62bd7c0b
* changes: ObjectChecker: honor some git-core fsck.* options ObjectChecker: allow some objects to skip errors
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties9
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/errors/CorruptObjectException.java41
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java9
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LazyObjectIdSetFile.java106
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java599
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java5
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java97
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java7
8 files changed, 705 insertions, 168 deletions
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
index 840059d34f..992e10bad6 100644
--- a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
+++ b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
@@ -126,14 +126,15 @@ connectionFailed=connection failed
connectionTimeOut=Connection time out: {0}
contextMustBeNonNegative=context must be >= 0
corruptionDetectedReReadingAt=Corruption detected re-reading at {0}
+corruptObjectBadDate=bad date
+corruptObjectBadEmail=bad email
corruptObjectBadStream=bad stream
corruptObjectBadStreamCorruptHeader=bad stream, corrupt header
+corruptObjectBadTimezone=bad time zone
corruptObjectDuplicateEntryNames=duplicate entry names
corruptObjectGarbageAfterSize=garbage after size
corruptObjectIncorrectLength=incorrect length
corruptObjectIncorrectSorting=incorrectly sorted
-corruptObjectInvalidAuthor=invalid author
-corruptObjectInvalidCommitter=invalid committer
corruptObjectInvalidEntryMode=invalid entry mode
corruptObjectInvalidMode=invalid mode
corruptObjectInvalidModeChar=invalid mode character
@@ -152,11 +153,11 @@ corruptObjectInvalidNameNul=invalid name 'NUL'
corruptObjectInvalidNamePrn=invalid name 'PRN'
corruptObjectInvalidObject=invalid object
corruptObjectInvalidParent=invalid parent
-corruptObjectInvalidTagger=invalid tagger
corruptObjectInvalidTree=invalid tree
corruptObjectInvalidType=invalid type
corruptObjectInvalidType2=invalid type {0}
corruptObjectMalformedHeader=malformed header: {0}
+corruptObjectMissingEmail=missing email
corruptObjectNameContainsByte=name contains byte 0x%x
corruptObjectNameContainsChar=name contains '%c'
corruptObjectNameContainsNullByte=name contains byte 0x00
@@ -182,6 +183,7 @@ corruptObjectPackfileChecksumIncorrect=Packfile checksum incorrect.
corruptObjectTruncatedInMode=truncated in mode
corruptObjectTruncatedInName=truncated in name
corruptObjectTruncatedInObjectId=truncated in object id
+corruptObjectZeroId=entry points to null SHA-1
couldNotCheckOutBecauseOfConflicts=Could not check out because of conflicts
couldNotDeleteLockFileShouldNotHappen=Could not delete lock 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
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}
+objectIsCorrupt3={0}: object {1}: {2}
objectIsNotA=Object {0} is not a {1}.
objectNotFound=Object {0} not found.
objectNotFoundIn=Object {0} not found in {1}.
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/errors/CorruptObjectException.java b/org.eclipse.jgit/src/org/eclipse/jgit/errors/CorruptObjectException.java
index c6ea093750..e4db40b889 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/errors/CorruptObjectException.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/errors/CorruptObjectException.java
@@ -49,8 +49,10 @@ package org.eclipse.jgit.errors;
import java.io.IOException;
import java.text.MessageFormat;
+import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.ObjectChecker;
import org.eclipse.jgit.lib.ObjectId;
/**
@@ -59,6 +61,26 @@ import org.eclipse.jgit.lib.ObjectId;
public class CorruptObjectException extends IOException {
private static final long serialVersionUID = 1L;
+ private ObjectChecker.ErrorType errorType;
+
+ /**
+ * Report a specific error condition discovered in an object.
+ *
+ * @param type
+ * type of error
+ * @param id
+ * identity of the bad object
+ * @param why
+ * description of the error.
+ * @since 4.2
+ */
+ public CorruptObjectException(ObjectChecker.ErrorType type, AnyObjectId id,
+ String why) {
+ super(MessageFormat.format(JGitText.get().objectIsCorrupt3,
+ type.getMessageId(), id.name(), why));
+ this.errorType = type;
+ }
+
/**
* Construct a CorruptObjectException for reporting a problem specified
* object id
@@ -66,8 +88,8 @@ public class CorruptObjectException extends IOException {
* @param id
* @param why
*/
- public CorruptObjectException(final AnyObjectId id, final String why) {
- this(id.toObjectId(), why);
+ public CorruptObjectException(AnyObjectId id, String why) {
+ super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
}
/**
@@ -77,7 +99,7 @@ public class CorruptObjectException extends IOException {
* @param id
* @param why
*/
- public CorruptObjectException(final ObjectId id, final String why) {
+ public CorruptObjectException(ObjectId id, String why) {
super(MessageFormat.format(JGitText.get().objectIsCorrupt, id.name(), why));
}
@@ -87,7 +109,7 @@ public class CorruptObjectException extends IOException {
*
* @param why
*/
- public CorruptObjectException(final String why) {
+ public CorruptObjectException(String why) {
super(why);
}
@@ -105,4 +127,15 @@ public class CorruptObjectException extends IOException {
super(why);
initCause(cause);
}
+
+ /**
+ * Specific error condition identified by {@link ObjectChecker}.
+ *
+ * @return error condition or null.
+ * @since 4.2
+ */
+ @Nullable
+ public ObjectChecker.ErrorType getErrorType() {
+ return errorType;
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
index c476f1773e..7740a2bb80 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
@@ -185,14 +185,15 @@ public class JGitText extends TranslationBundle {
/***/ public String connectionTimeOut;
/***/ public String contextMustBeNonNegative;
/***/ public String corruptionDetectedReReadingAt;
+ /***/ public String corruptObjectBadDate;
+ /***/ public String corruptObjectBadEmail;
/***/ public String corruptObjectBadStream;
/***/ public String corruptObjectBadStreamCorruptHeader;
+ /***/ public String corruptObjectBadTimezone;
/***/ public String corruptObjectDuplicateEntryNames;
/***/ public String corruptObjectGarbageAfterSize;
/***/ public String corruptObjectIncorrectLength;
/***/ public String corruptObjectIncorrectSorting;
- /***/ public String corruptObjectInvalidAuthor;
- /***/ public String corruptObjectInvalidCommitter;
/***/ public String corruptObjectInvalidEntryMode;
/***/ public String corruptObjectInvalidMode;
/***/ public String corruptObjectInvalidModeChar;
@@ -211,11 +212,11 @@ public class JGitText extends TranslationBundle {
/***/ public String corruptObjectInvalidNamePrn;
/***/ public String corruptObjectInvalidObject;
/***/ public String corruptObjectInvalidParent;
- /***/ public String corruptObjectInvalidTagger;
/***/ public String corruptObjectInvalidTree;
/***/ public String corruptObjectInvalidType;
/***/ public String corruptObjectInvalidType2;
/***/ public String corruptObjectMalformedHeader;
+ /***/ public String corruptObjectMissingEmail;
/***/ public String corruptObjectNameContainsByte;
/***/ public String corruptObjectNameContainsChar;
/***/ public String corruptObjectNameContainsNullByte;
@@ -241,6 +242,7 @@ public class JGitText extends TranslationBundle {
/***/ public String corruptObjectTruncatedInMode;
/***/ public String corruptObjectTruncatedInName;
/***/ public String corruptObjectTruncatedInObjectId;
+ /***/ public String corruptObjectZeroId;
/***/ public String corruptPack;
/***/ public String couldNotCheckOutBecauseOfConflicts;
/***/ public String couldNotDeleteLockFileShouldNotHappen;
@@ -492,6 +494,7 @@ public class JGitText extends TranslationBundle {
/***/ public String objectAtHasBadZlibStream;
/***/ public String objectAtPathDoesNotHaveId;
/***/ public String objectIsCorrupt;
+ /***/ public String objectIsCorrupt3;
/***/ public String objectIsNotA;
/***/ public String objectNotFound;
/***/ public String objectNotFoundIn;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LazyObjectIdSetFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LazyObjectIdSetFile.java
new file mode 100644
index 0000000000..1e2617c0e3
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/LazyObjectIdSetFile.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2015, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.internal.storage.file;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.MutableObjectId;
+import org.eclipse.jgit.lib.ObjectIdOwnerMap;
+import org.eclipse.jgit.lib.ObjectIdSet;
+
+/** Lazily loads a set of ObjectIds, one per line. */
+public class LazyObjectIdSetFile implements ObjectIdSet {
+ private final File src;
+ private ObjectIdOwnerMap<Entry> set;
+
+ /**
+ * Create a new lazy set from a file.
+ *
+ * @param src
+ * the source file.
+ */
+ public LazyObjectIdSetFile(File src) {
+ this.src = src;
+ }
+
+ @Override
+ public boolean contains(AnyObjectId objectId) {
+ if (set == null) {
+ set = load();
+ }
+ return set.contains(objectId);
+ }
+
+ private ObjectIdOwnerMap<Entry> load() {
+ ObjectIdOwnerMap<Entry> r = new ObjectIdOwnerMap<>();
+ try (FileInputStream fin = new FileInputStream(src);
+ Reader rin = new InputStreamReader(fin, UTF_8);
+ BufferedReader br = new BufferedReader(rin)) {
+ MutableObjectId id = new MutableObjectId();
+ for (String line; (line = br.readLine()) != null;) {
+ id.fromString(line);
+ if (!r.contains(id)) {
+ r.add(new Entry(id));
+ }
+ }
+ } catch (IOException e) {
+ // Ignore IO errors accessing the lazy set.
+ }
+ return r;
+ }
+
+ static class Entry extends ObjectIdOwnerMap.Entry {
+ Entry(AnyObjectId id) {
+ super(id);
+ }
+ }
+}
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 855d9d7509..858a0385d0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java
@@ -44,20 +44,56 @@
package org.eclipse.jgit.lib;
-import static org.eclipse.jgit.util.RawParseUtils.match;
+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_COMMIT;
+import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
+import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
+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.parseBase10;
import java.text.MessageFormat;
import java.text.Normalizer;
+import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
+import org.eclipse.jgit.annotations.NonNull;
+import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.util.MutableInteger;
import org.eclipse.jgit.util.RawParseUtils;
+import org.eclipse.jgit.util.StringUtils;
/**
* Verifies that an object is formatted correctly.
@@ -98,31 +134,135 @@ public class ObjectChecker {
/** Header "tagger " */
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 boolean allowZeroMode;
+ private final MutableObjectId tempId = new MutableObjectId();
+ private final MutableInteger bufPtr = new MutableInteger();
+ private EnumSet<ErrorType> errors = EnumSet.allOf(ErrorType.class);
+ private ObjectIdSet skipList;
private boolean allowInvalidPersonIdent;
private boolean windows;
private boolean macosx;
/**
+ * Enable accepting specific malformed (but not horribly broken) objects.
+ *
+ * @param objects
+ * collection of object names known to be broken in a non-fatal
+ * way that should be ignored by the checker.
+ * @return {@code this}
+ * @since 4.2
+ */
+ public ObjectChecker setSkipList(@Nullable ObjectIdSet objects) {
+ skipList = objects;
+ 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.
* <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.
+ * <p>
+ * Same as {@code setIgnore(ZERO_PADDED_FILEMODE, allow)}.
*
* @param allow allow leading zero mode.
* @return {@code this}.
* @since 3.4
*/
public ObjectChecker setAllowLeadingZeroFileMode(boolean allow) {
- allowZeroMode = allow;
- return this;
+ return setIgnore(ZERO_PADDED_FILEMODE, allow);
}
/**
@@ -183,62 +323,117 @@ public class ObjectChecker {
* @throws CorruptObjectException
* if an error is identified.
*/
- public void check(final int objType, final byte[] raw)
+ public void check(int objType, byte[] raw)
+ throws CorruptObjectException {
+ check(idFor(objType, raw), objType, raw);
+ }
+
+ /**
+ * Check an object for parsing errors.
+ *
+ * @param id
+ * identify of the object being checked.
+ * @param objType
+ * type of the object. Must be a valid object type code in
+ * {@link Constants}.
+ * @param raw
+ * the raw data which comprises the object. This should be in the
+ * canonical format (that is the format used to generate the
+ * ObjectId of the object). The array is never modified.
+ * @throws CorruptObjectException
+ * if an error is identified.
+ * @since 4.2
+ */
+ public void check(@Nullable AnyObjectId id, int objType, byte[] raw)
throws CorruptObjectException {
switch (objType) {
- case Constants.OBJ_COMMIT:
- checkCommit(raw);
+ case OBJ_COMMIT:
+ checkCommit(id, raw);
break;
- case Constants.OBJ_TAG:
- checkTag(raw);
+ case OBJ_TAG:
+ checkTag(id, raw);
break;
- case Constants.OBJ_TREE:
- checkTree(raw);
+ case OBJ_TREE:
+ checkTree(id, raw);
break;
- case Constants.OBJ_BLOB:
+ case OBJ_BLOB:
checkBlob(raw);
break;
default:
- throw new CorruptObjectException(MessageFormat.format(
+ report(UNKNOWN_TYPE, id, MessageFormat.format(
JGitText.get().corruptObjectInvalidType2,
Integer.valueOf(objType)));
}
}
- private int id(final byte[] raw, final int ptr) {
+ private boolean checkId(byte[] raw) {
+ int p = bufPtr.value;
try {
- tempId.fromString(raw, ptr);
- return ptr + Constants.OBJECT_ID_STRING_LENGTH;
+ tempId.fromString(raw, p);
} catch (IllegalArgumentException e) {
- return -1;
+ bufPtr.value = nextLF(raw, p);
+ return false;
+ }
+
+ p += OBJECT_ID_STRING_LENGTH;
+ if (raw[p] == '\n') {
+ bufPtr.value = p + 1;
+ return true;
}
+ bufPtr.value = nextLF(raw, p);
+ return false;
}
- private int personIdent(final byte[] raw, int ptr) {
- if (allowInvalidPersonIdent)
- return nextLF(raw, ptr) - 1;
+ private void checkPersonIdent(byte[] raw, @Nullable AnyObjectId id)
+ throws CorruptObjectException {
+ if (allowInvalidPersonIdent) {
+ bufPtr.value = nextLF(raw, bufPtr.value);
+ return;
+ }
- final int emailB = nextLF(raw, ptr, '<');
- if (emailB == ptr || raw[emailB - 1] != '<')
- return -1;
+ 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, '>');
- if (emailE == emailB || raw[emailE - 1] != '>')
- return -1;
- if (emailE == raw.length || raw[emailE] != ' ')
- return -1;
+ if (emailE == emailB || raw[emailE - 1] != '>') {
+ report(BAD_EMAIL, id, JGitText.get().corruptObjectBadEmail);
+ bufPtr.value = nextLF(raw, bufPtr.value);
+ 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
- ptr = ptrout.value;
- if (emailE + 1 == ptr)
- return -1;
- if (ptr == raw.length || raw[ptr] != ' ')
- return -1;
+ parseBase10(raw, emailE + 1, bufPtr); // when
+ if (emailE + 1 == bufPtr.value || bufPtr.value == raw.length
+ || raw[bufPtr.value] != ' ') {
+ report(BAD_DATE, id, JGitText.get().corruptObjectBadDate);
+ bufPtr.value = nextLF(raw, bufPtr.value);
+ return;
+ }
+
+ int p = bufPtr.value + 1;
+ parseBase10(raw, p, bufPtr); // tz offset
+ if (p == bufPtr.value) {
+ report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
+ bufPtr.value = nextLF(raw, bufPtr.value);
+ return;
+ }
- parseBase10(raw, ptr + 1, ptrout); // tz offset
- if (ptr + 1 == ptrout.value)
- return -1;
- return ptrout.value;
+ p = bufPtr.value;
+ if (raw[p] == '\n') {
+ bufPtr.value = p + 1;
+ } else {
+ report(BAD_TIMEZONE, id, JGitText.get().corruptObjectBadTimezone);
+ bufPtr.value = nextLF(raw, p);
+ }
}
/**
@@ -249,36 +444,50 @@ public class ObjectChecker {
* @throws CorruptObjectException
* if any error was detected.
*/
- public void checkCommit(final byte[] raw) throws CorruptObjectException {
- int ptr = 0;
+ public void checkCommit(byte[] raw) throws CorruptObjectException {
+ checkCommit(idFor(OBJ_COMMIT, raw), raw);
+ }
- if ((ptr = match(raw, ptr, tree)) < 0)
- throw new CorruptObjectException(
- JGitText.get().corruptObjectNotreeHeader);
- if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
- JGitText.get().corruptObjectInvalidTree);
+ /**
+ * Check a commit for errors.
+ *
+ * @param id
+ * identity of the object being checked.
+ * @param raw
+ * the commit data. The array is never modified.
+ * @throws CorruptObjectException
+ * if any error was detected.
+ * @since 4.2
+ */
+ public void checkCommit(@Nullable AnyObjectId id, byte[] raw)
+ throws CorruptObjectException {
+ bufPtr.value = 0;
- while (match(raw, ptr, parent) >= 0) {
- ptr += parent.length;
- if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
+ if (!match(raw, tree)) {
+ report(MISSING_TREE, id, JGitText.get().corruptObjectNotreeHeader);
+ } else if (!checkId(raw)) {
+ report(BAD_TREE_SHA1, id, JGitText.get().corruptObjectInvalidTree);
+ }
+
+ while (match(raw, parent)) {
+ if (!checkId(raw)) {
+ report(BAD_PARENT_SHA1, id,
JGitText.get().corruptObjectInvalidParent);
+ }
}
- if ((ptr = match(raw, ptr, author)) < 0)
- throw new CorruptObjectException(
- JGitText.get().corruptObjectNoAuthor);
- if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
- JGitText.get().corruptObjectInvalidAuthor);
+ if (match(raw, author)) {
+ checkPersonIdent(raw, id);
+ } else {
+ report(MISSING_AUTHOR, id, JGitText.get().corruptObjectNoAuthor);
+ }
- if ((ptr = match(raw, ptr, committer)) < 0)
- throw new CorruptObjectException(
+ if (match(raw, committer)) {
+ checkPersonIdent(raw, id);
+ } else {
+ report(MISSING_COMMITTER, id,
JGitText.get().corruptObjectNoCommitter);
- if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
- JGitText.get().corruptObjectInvalidCommitter);
+ }
}
/**
@@ -289,30 +498,46 @@ public class ObjectChecker {
* @throws CorruptObjectException
* if any error was detected.
*/
- public void checkTag(final byte[] raw) throws CorruptObjectException {
- int ptr = 0;
+ public void checkTag(byte[] raw) throws CorruptObjectException {
+ checkTag(idFor(OBJ_TAG, raw), raw);
+ }
- if ((ptr = match(raw, ptr, object)) < 0)
- throw new CorruptObjectException(
+ /**
+ * Check an annotated tag for errors.
+ *
+ * @param id
+ * identity of the object being checked.
+ * @param raw
+ * the tag data. The array is never modified.
+ * @throws CorruptObjectException
+ * if any error was detected.
+ * @since 4.2
+ */
+ public void checkTag(@Nullable AnyObjectId id, byte[] raw)
+ throws CorruptObjectException {
+ bufPtr.value = 0;
+ if (!match(raw, object)) {
+ report(MISSING_OBJECT, id,
JGitText.get().corruptObjectNoObjectHeader);
- if ((ptr = id(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
+ } else if (!checkId(raw)) {
+ report(BAD_OBJECT_SHA1, id,
JGitText.get().corruptObjectInvalidObject);
+ }
- if ((ptr = match(raw, ptr, type)) < 0)
- throw new CorruptObjectException(
+ if (!match(raw, type)) {
+ report(MISSING_TYPE_ENTRY, id,
JGitText.get().corruptObjectNoTypeHeader);
- ptr = nextLF(raw, ptr);
+ }
+ bufPtr.value = nextLF(raw, bufPtr.value);
- if ((ptr = match(raw, ptr, tag)) < 0)
- throw new CorruptObjectException(
+ if (!match(raw, tag)) {
+ report(MISSING_TAG_ENTRY, id,
JGitText.get().corruptObjectNoTagHeader);
- ptr = nextLF(raw, ptr);
+ }
+ bufPtr.value = nextLF(raw, bufPtr.value);
- if ((ptr = match(raw, ptr, tagger)) > 0) {
- if ((ptr = personIdent(raw, ptr)) < 0 || raw[ptr++] != '\n')
- throw new CorruptObjectException(
- JGitText.get().corruptObjectInvalidTagger);
+ if (match(raw, tagger)) {
+ checkPersonIdent(raw, id);
}
}
@@ -381,7 +606,23 @@ public class ObjectChecker {
* @throws CorruptObjectException
* if any error was detected.
*/
- public void checkTree(final byte[] raw) throws CorruptObjectException {
+ public void checkTree(byte[] raw) throws CorruptObjectException {
+ checkTree(idFor(OBJ_TREE, raw), raw);
+ }
+
+ /**
+ * Check a canonical formatted tree for errors.
+ *
+ * @param id
+ * identity of the object being checked.
+ * @param raw
+ * the raw tree data. The array is never modified.
+ * @throws CorruptObjectException
+ * if any error was detected.
+ * @since 4.2
+ */
+ public void checkTree(@Nullable AnyObjectId id, byte[] raw)
+ throws CorruptObjectException {
final int sz = raw.length;
int ptr = 0;
int lastNameB = 0, lastNameE = 0, lastMode = 0;
@@ -392,74 +633,89 @@ public class ObjectChecker {
while (ptr < sz) {
int thisMode = 0;
for (;;) {
- if (ptr == sz)
+ if (ptr == sz) {
throw new CorruptObjectException(
JGitText.get().corruptObjectTruncatedInMode);
+ }
final byte c = raw[ptr++];
if (' ' == c)
break;
- if (c < '0' || c > '7')
+ if (c < '0' || c > '7') {
throw new CorruptObjectException(
JGitText.get().corruptObjectInvalidModeChar);
- if (thisMode == 0 && c == '0' && !allowZeroMode)
- throw new CorruptObjectException(
+ }
+ if (thisMode == 0 && c == '0') {
+ report(ZERO_PADDED_FILEMODE, id,
JGitText.get().corruptObjectInvalidModeStartsZero);
+ }
thisMode <<= 3;
thisMode += c - '0';
}
- if (FileMode.fromBits(thisMode).getObjectType() == Constants.OBJ_BAD)
+ if (FileMode.fromBits(thisMode).getObjectType() == OBJ_BAD) {
throw new CorruptObjectException(MessageFormat.format(
JGitText.get().corruptObjectInvalidMode2,
Integer.valueOf(thisMode)));
+ }
final int thisNameB = ptr;
- ptr = scanPathSegment(raw, ptr, sz);
- if (ptr == sz || raw[ptr] != 0)
+ ptr = scanPathSegment(raw, ptr, sz, id);
+ if (ptr == sz || raw[ptr] != 0) {
throw new CorruptObjectException(
JGitText.get().corruptObjectTruncatedInName);
- checkPathSegment2(raw, thisNameB, ptr);
+ }
+ checkPathSegment2(raw, thisNameB, ptr, id);
if (normalized != null) {
- if (!normalized.add(normalize(raw, thisNameB, ptr)))
- throw new CorruptObjectException(
+ if (!normalized.add(normalize(raw, thisNameB, ptr))) {
+ report(DUPLICATE_ENTRIES, id,
JGitText.get().corruptObjectDuplicateEntryNames);
- } else if (duplicateName(raw, thisNameB, ptr))
- throw new CorruptObjectException(
+ }
+ } else if (duplicateName(raw, thisNameB, ptr)) {
+ report(DUPLICATE_ENTRIES, id,
JGitText.get().corruptObjectDuplicateEntryNames);
+ }
if (lastNameB != 0) {
final int cmp = pathCompare(raw, lastNameB, lastNameE,
lastMode, thisNameB, ptr, thisMode);
- if (cmp > 0)
- throw new CorruptObjectException(
+ if (cmp > 0) {
+ report(TREE_NOT_SORTED, id,
JGitText.get().corruptObjectIncorrectSorting);
+ }
}
lastNameB = thisNameB;
lastNameE = ptr;
lastMode = thisMode;
- ptr += 1 + Constants.OBJECT_ID_LENGTH;
- if (ptr > sz)
+ ptr += 1 + OBJECT_ID_LENGTH;
+ if (ptr > sz) {
throw new CorruptObjectException(
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)
- throws CorruptObjectException {
+ private int scanPathSegment(byte[] raw, int ptr, int end,
+ @Nullable AnyObjectId id) throws CorruptObjectException {
for (; ptr < end; ptr++) {
byte c = raw[ptr];
- if (c == 0)
+ if (c == 0) {
return ptr;
- if (c == '/')
- throw new CorruptObjectException(
+ }
+ if (c == '/') {
+ report(FULL_PATHNAME, id,
JGitText.get().corruptObjectNameContainsSlash);
+ }
if (windows && isInvalidOnWindows(c)) {
- if (c > 31)
+ if (c > 31) {
throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectNameContainsChar,
Byte.valueOf(c)));
+ }
throw new CorruptObjectException(String.format(
JGitText.get().corruptObjectNameContainsByte,
Integer.valueOf(c & 0xff)));
@@ -468,6 +724,26 @@ public class ObjectChecker {
return ptr;
}
+ @SuppressWarnings("resource")
+ @Nullable
+ private ObjectId idFor(int objType, byte[] raw) {
+ if (skipList != null) {
+ return new ObjectInserter.Formatter().idFor(objType, raw);
+ }
+ return null;
+ }
+
+ private void report(@NonNull ErrorType err, @Nullable AnyObjectId 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);
+ }
+ }
+
/**
* Check tree path entry for validity.
* <p>
@@ -518,73 +794,82 @@ public class ObjectChecker {
*/
public void checkPathSegment(byte[] raw, int ptr, int end)
throws CorruptObjectException {
- int e = scanPathSegment(raw, ptr, end);
+ int e = scanPathSegment(raw, ptr, end, null);
if (e < end && raw[e] == 0)
throw new CorruptObjectException(
JGitText.get().corruptObjectNameContainsNullByte);
- checkPathSegment2(raw, ptr, end);
+ checkPathSegment2(raw, ptr, end, null);
}
- private void checkPathSegment2(byte[] raw, int ptr, int end)
- throws CorruptObjectException {
- if (ptr == end)
- throw new CorruptObjectException(
- JGitText.get().corruptObjectNameZeroLength);
+ 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;
+ }
+
if (raw[ptr] == '.') {
switch (end - ptr) {
case 1:
- throw new CorruptObjectException(
- JGitText.get().corruptObjectNameDot);
+ report(HAS_DOT, id, JGitText.get().corruptObjectNameDot);
+ break;
case 2:
- if (raw[ptr + 1] == '.')
- throw new CorruptObjectException(
+ if (raw[ptr + 1] == '.') {
+ report(HAS_DOTDOT, id,
JGitText.get().corruptObjectNameDotDot);
+ }
break;
case 4:
- if (isGit(raw, ptr + 1))
- throw new CorruptObjectException(String.format(
+ if (isGit(raw, ptr + 1)) {
+ report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end)));
+ }
break;
default:
- if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end))
- throw new CorruptObjectException(String.format(
+ if (end - ptr > 4 && isNormalizedGit(raw, ptr + 1, end)) {
+ report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end)));
+ }
}
} else if (isGitTilde1(raw, ptr, end)) {
- throw new CorruptObjectException(String.format(
+ report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidName,
RawParseUtils.decode(raw, ptr, end)));
}
-
- if (macosx && isMacHFSGit(raw, ptr, end))
- throw new CorruptObjectException(String.format(
+ if (macosx && isMacHFSGit(raw, ptr, end, id)) {
+ report(HAS_DOTGIT, id, String.format(
JGitText.get().corruptObjectInvalidNameIgnorableUnicode,
RawParseUtils.decode(raw, ptr, end)));
+ }
if (windows) {
// Windows ignores space and dot at end of file name.
- if (raw[end - 1] == ' ' || raw[end - 1] == '.')
- throw new CorruptObjectException(String.format(
+ if (raw[end - 1] == ' ' || raw[end - 1] == '.') {
+ report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameEnd,
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
// to ".git" therefore we should prevent such names
- private static boolean isMacHFSGit(byte[] raw, int ptr, int end)
- throws CorruptObjectException {
+ private boolean isMacHFSGit(byte[] raw, int ptr, int end,
+ @Nullable AnyObjectId id) throws CorruptObjectException {
boolean ignorable = false;
byte[] git = new byte[] { '.', 'g', 'i', 't' };
int g = 0;
while (ptr < end) {
switch (raw[ptr]) {
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]) {
case (byte) 0x80:
switch (raw[ptr + 2]) {
@@ -621,7 +906,9 @@ public class ObjectChecker {
return false;
}
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
if ((raw[ptr + 1] == (byte) 0xbb)
&& (raw[ptr + 2] == (byte) 0xbf)) {
@@ -642,12 +929,15 @@ public class ObjectChecker {
return false;
}
- private static void checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end)
- throws CorruptObjectException {
- if ((ptr + 2) >= end)
- throw new CorruptObjectException(MessageFormat.format(
+ private boolean checkTruncatedIgnorableUTF8(byte[] raw, int ptr, int end,
+ @Nullable AnyObjectId id) throws CorruptObjectException {
+ if ((ptr + 2) >= end) {
+ report(BAD_UTF8, id, MessageFormat.format(
JGitText.get().corruptObjectInvalidNameInvalidUtf8,
toHexString(raw, ptr, end)));
+ return false;
+ }
+ return true;
}
private static String toHexString(byte[] raw, int ptr, int end) {
@@ -657,33 +947,36 @@ public class ObjectChecker {
return b.toString();
}
- private static void checkNotWindowsDevice(byte[] raw, int ptr, int end)
- throws CorruptObjectException {
+ private void checkNotWindowsDevice(byte[] raw, int ptr, int end,
+ @Nullable AnyObjectId id) throws CorruptObjectException {
switch (toLower(raw[ptr])) {
case 'a': // AUX
if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'u'
&& toLower(raw[ptr + 2]) == 'x'
- && (end - ptr == 3 || raw[ptr + 3] == '.'))
- throw new CorruptObjectException(
+ && (end - ptr == 3 || raw[ptr + 3] == '.')) {
+ report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameAux);
+ }
break;
case 'c': // CON, COM[1-9]
if (end - ptr >= 3
&& toLower(raw[ptr + 2]) == 'n'
&& toLower(raw[ptr + 1]) == 'o'
- && (end - ptr == 3 || raw[ptr + 3] == '.'))
- throw new CorruptObjectException(
+ && (end - ptr == 3 || raw[ptr + 3] == '.')) {
+ report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameCon);
+ }
if (end - ptr >= 4
&& toLower(raw[ptr + 2]) == 'm'
&& toLower(raw[ptr + 1]) == 'o'
&& isPositiveDigit(raw[ptr + 3])
- && (end - ptr == 4 || raw[ptr + 4] == '.'))
- throw new CorruptObjectException(String.format(
+ && (end - ptr == 4 || raw[ptr + 4] == '.')) {
+ report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameCom,
Character.valueOf(((char) raw[ptr + 3]))));
+ }
break;
case 'l': // LPT[1-9]
@@ -691,28 +984,31 @@ public class ObjectChecker {
&& toLower(raw[ptr + 1]) == 'p'
&& toLower(raw[ptr + 2]) == 't'
&& isPositiveDigit(raw[ptr + 3])
- && (end - ptr == 4 || raw[ptr + 4] == '.'))
- throw new CorruptObjectException(String.format(
+ && (end - ptr == 4 || raw[ptr + 4] == '.')) {
+ report(WIN32_BAD_NAME, id, String.format(
JGitText.get().corruptObjectInvalidNameLpt,
Character.valueOf(((char) raw[ptr + 3]))));
+ }
break;
case 'n': // NUL
if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'u'
&& toLower(raw[ptr + 2]) == 'l'
- && (end - ptr == 3 || raw[ptr + 3] == '.'))
- throw new CorruptObjectException(
+ && (end - ptr == 3 || raw[ptr + 3] == '.')) {
+ report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNameNul);
+ }
break;
case 'p': // PRN
if (end - ptr >= 3
&& toLower(raw[ptr + 1]) == 'r'
&& toLower(raw[ptr + 2]) == 'n'
- && (end - ptr == 3 || raw[ptr + 3] == '.'))
- throw new CorruptObjectException(
+ && (end - ptr == 3 || raw[ptr + 3] == '.')) {
+ report(WIN32_BAD_NAME, id,
JGitText.get().corruptObjectInvalidNamePrn);
+ }
break;
}
}
@@ -765,6 +1061,15 @@ public class ObjectChecker {
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) {
if ('A' <= b && b <= 'Z')
return (char) (b + ('a' - 'A'));
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java
index 6e5fc9f009..b96fe885e1 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java
@@ -1049,8 +1049,11 @@ public abstract class PackParser {
final byte[] data) throws IOException {
if (objCheck != null) {
try {
- objCheck.check(type, data);
+ objCheck.check(id, type, data);
} catch (CorruptObjectException e) {
+ if (e.getErrorType() != null) {
+ throw e;
+ }
throw new CorruptObjectException(MessageFormat.format(
JGitText.get().invalidObject,
Constants.typeString(type),
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java
index f9b74c84e5..72c9c8b93e 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java
@@ -43,13 +43,20 @@
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.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jgit.annotations.Nullable;
+import org.eclipse.jgit.internal.storage.file.LazyObjectIdSetFile;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Config.SectionParser;
import org.eclipse.jgit.lib.ObjectChecker;
+import org.eclipse.jgit.lib.ObjectIdSet;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.SystemReader;
@@ -59,6 +66,8 @@ import org.eclipse.jgit.util.SystemReader;
* parameters.
*/
public class TransferConfig {
+ private static final String FSCK = "fsck"; //$NON-NLS-1$
+
/** Key for {@link Config#get(SectionParser)}. */
public static final Config.SectionParser<TransferConfig> KEY = new SectionParser<TransferConfig>() {
public TransferConfig parse(final Config cfg) {
@@ -66,9 +75,14 @@ public class TransferConfig {
}
};
+ enum FsckMode {
+ ERROR, WARN, IGNORE;
+ }
+
private final boolean fetchFsck;
private final boolean receiveFsck;
- private final boolean allowLeadingZeroFileMode;
+ private final String fsckSkipList;
+ private final EnumSet<ObjectChecker.ErrorType> ignore;
private final boolean allowInvalidPersonIdent;
private final boolean safeForWindows;
private final boolean safeForMacOS;
@@ -84,13 +98,44 @@ public class TransferConfig {
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$
receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$
- allowLeadingZeroFileMode = rc.getBoolean("fsck", "allowLeadingZeroFileMode", false); //$NON-NLS-1$ //$NON-NLS-2$
- allowInvalidPersonIdent = rc.getBoolean("fsck", "allowInvalidPersonIdent", false); //$NON-NLS-1$ //$NON-NLS-2$
- safeForWindows = rc.getBoolean("fsck", "safeForWindows", //$NON-NLS-1$ //$NON-NLS-2$
+ fsckSkipList = rc.getString(FSCK, null, "skipList"); //$NON-NLS-1$
+ allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent", false); //$NON-NLS-1$
+ safeForWindows = rc.getBoolean(FSCK, "safeForWindows", //$NON-NLS-1$
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());
+ 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(
"uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
allowReachableSha1InWant = rc.getBoolean(
@@ -123,10 +168,18 @@ public class TransferConfig {
return null;
}
return new ObjectChecker()
- .setAllowLeadingZeroFileMode(allowLeadingZeroFileMode)
+ .setIgnore(ignore)
.setAllowInvalidPersonIdent(allowInvalidPersonIdent)
.setSafeForWindows(safeForWindows)
- .setSafeForMacOS(safeForMacOS);
+ .setSafeForMacOS(safeForMacOS)
+ .setSkipList(skipList());
+ }
+
+ private ObjectIdSet skipList() {
+ if (fsckSkipList != null && !fsckSkipList.isEmpty()) {
+ return new LazyObjectIdSetFile(new File(fsckSkipList));
+ }
+ return null;
}
/**
@@ -175,4 +228,34 @@ public class TransferConfig {
}
};
}
+
+ 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() {
+ }
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java
index dfc3ee4c30..17edfdc4fb 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java
@@ -637,10 +637,11 @@ class WalkFetchConnection extends BaseFetchConnection {
final byte[] raw = uol.getCachedBytes();
if (objCheck != null) {
try {
- objCheck.check(type, raw);
+ objCheck.check(id, type, raw);
} catch (CorruptObjectException e) {
- throw new TransportException(MessageFormat.format(JGitText.get().transportExceptionInvalid
- , Constants.typeString(type), id.name(), e.getMessage()));
+ throw new TransportException(MessageFormat.format(
+ JGitText.get().transportExceptionInvalid,
+ Constants.typeString(type), id.name(), e.getMessage()));
}
}