import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION;
+import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_NAME;
+import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_PARSE;
+import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_PATH;
+import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_URL;
-import java.io.IOException;
import java.text.MessageFormat;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Config;
+import org.eclipse.jgit.lib.ObjectChecker;
/**
* Validations for the git submodule fields (name, path, uri).
*/
public static class SubmoduleValidationException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ private final ObjectChecker.ErrorType fsckMessageId;
+
/**
* @param message
* Description of the problem
+ * @param fsckMessageId
+ * Error identifier, following the git fsck fsck.<msg-id>
+ * format
*/
- public SubmoduleValidationException(String message) {
+ SubmoduleValidationException(String message,
+ ObjectChecker.ErrorType fsckMessageId) {
super(message);
+ this.fsckMessageId = fsckMessageId;
}
- private static final long serialVersionUID = 1L;
+
+ /**
+ * @return the error identifier
+ */
+ public ObjectChecker.ErrorType getFsckMessageId() {
+ return fsckMessageId;
+ }
}
/**
// Since Path class is platform dependent, we manually check '/' and
// '\\' patterns here.
throw new SubmoduleValidationException(MessageFormat
- .format(JGitText.get().invalidNameContainsDotDot, name));
+ .format(JGitText.get().invalidNameContainsDotDot, name),
+ GITMODULES_NAME);
}
if (name.startsWith("-")) { //$NON-NLS-1$
throw new SubmoduleValidationException(
MessageFormat.format(
- JGitText.get().submoduleNameInvalid, name));
+ JGitText.get().submoduleNameInvalid, name),
+ GITMODULES_NAME);
}
}
if (uri.startsWith("-")) { //$NON-NLS-1$
throw new SubmoduleValidationException(
MessageFormat.format(
- JGitText.get().submoduleUrlInvalid, uri));
+ JGitText.get().submoduleUrlInvalid, uri),
+ GITMODULES_URL);
}
}
if (path.startsWith("-")) { //$NON-NLS-1$
throw new SubmoduleValidationException(
MessageFormat.format(
- JGitText.get().submodulePathInvalid, path));
+ JGitText.get().submodulePathInvalid, path),
+ GITMODULES_PATH);
}
}
/**
+ * Validate a .gitmodules file
+ *
* @param gitModulesContents
* Contents of a .gitmodule file. They will be parsed internally.
- * @throws IOException
- * If the contents
+ * @throws SubmoduleValidationException
+ * if the contents don't look like a configuration file or field
+ * values are not valid
*/
public static void assertValidGitModulesFile(String gitModulesContents)
- throws IOException {
- // Validate .gitmodules file
+ throws SubmoduleValidationException {
Config c = new Config();
try {
c.fromText(gitModulesContents);
}
}
} catch (ConfigInvalidException e) {
- throw new IOException(
- MessageFormat.format(
- JGitText.get().invalidGitModules,
- e));
- } catch (SubmoduleValidationException e) {
- throw new IOException(e.getMessage(), e);
+ throw new SubmoduleValidationException(
+ JGitText.get().invalidGitModules,
+ GITMODULES_PARSE);
}
}
}
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.InvalidObjectIdException;
+import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.PackProtocolException;
import org.eclipse.jgit.errors.TooLargePackException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.file.PackLock;
import org.eclipse.jgit.internal.submodule.SubmoduleValidator;
+import org.eclipse.jgit.internal.submodule.SubmoduleValidator.SubmoduleValidationException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.Config;
AnyObjectId blobId = entry.getBlobId();
ObjectLoader blob = odb.open(blobId, Constants.OBJ_BLOB);
- SubmoduleValidator.assertValidGitModulesFile(
- new String(blob.getBytes(), UTF_8));
+ try {
+ SubmoduleValidator.assertValidGitModulesFile(
+ new String(blob.getBytes(), UTF_8));
+ } catch (LargeObjectException | SubmoduleValidationException e) {
+ throw new IOException(e);
+ }
}
}