@ -45,13 +45,17 @@ package org.eclipse.jgit.internal.submodule;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH ;
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_KEY_URL ;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION ;
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 java.text.MessageFormat ;
import org.eclipse.jgit.errors.ConfigInvalidException ;
import org.eclipse.jgit.errors.ConfigInvalidException ;
import org.eclipse.jgit.internal.JGitText ;
import org.eclipse.jgit.internal.JGitText ;
import org.eclipse.jgit.lib.Config ;
import org.eclipse.jgit.lib.Config ;
import org.eclipse.jgit.lib.ObjectChecker ;
/ * *
/ * *
* Validations for the git submodule fields ( name , path , uri ) .
* Validations for the git submodule fields ( name , path , uri ) .
@ -66,15 +70,30 @@ public class SubmoduleValidator {
* /
* /
public static class SubmoduleValidationException extends Exception {
public static class SubmoduleValidationException extends Exception {
private static final long serialVersionUID = 1L ;
private final ObjectChecker . ErrorType fsckMessageId ;
/ * *
/ * *
* @param message
* @param message
* Description of the problem
* 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 ) ;
super ( message ) ;
this . fsckMessageId = fsckMessageId ;
}
}
private static final long serialVersionUID = 1L ;
/ * *
* @return the error identifier
* /
public ObjectChecker . ErrorType getFsckMessageId ( ) {
return fsckMessageId ;
}
}
}
/ * *
/ * *
@ -100,13 +119,15 @@ public class SubmoduleValidator {
// Since Path class is platform dependent, we manually check '/' and
// Since Path class is platform dependent, we manually check '/' and
// '\\' patterns here.
// '\\' patterns here.
throw new SubmoduleValidationException ( MessageFormat
throw new SubmoduleValidationException ( MessageFormat
. format ( JGitText . get ( ) . invalidNameContainsDotDot , name ) ) ;
. format ( JGitText . get ( ) . invalidNameContainsDotDot , name ) ,
GITMODULES_NAME ) ;
}
}
if ( name . startsWith ( "-" ) ) { //$NON-NLS-1$
if ( name . startsWith ( "-" ) ) { //$NON-NLS-1$
throw new SubmoduleValidationException (
throw new SubmoduleValidationException (
MessageFormat . format (
MessageFormat . format (
JGitText . get ( ) . submoduleNameInvalid , name ) ) ;
JGitText . get ( ) . submoduleNameInvalid , name ) ,
GITMODULES_NAME ) ;
}
}
}
}
@ -123,7 +144,8 @@ public class SubmoduleValidator {
if ( uri . startsWith ( "-" ) ) { //$NON-NLS-1$
if ( uri . startsWith ( "-" ) ) { //$NON-NLS-1$
throw new SubmoduleValidationException (
throw new SubmoduleValidationException (
MessageFormat . format (
MessageFormat . format (
JGitText . get ( ) . submoduleUrlInvalid , uri ) ) ;
JGitText . get ( ) . submoduleUrlInvalid , uri ) ,
GITMODULES_URL ) ;
}
}
}
}
@ -140,19 +162,22 @@ public class SubmoduleValidator {
if ( path . startsWith ( "-" ) ) { //$NON-NLS-1$
if ( path . startsWith ( "-" ) ) { //$NON-NLS-1$
throw new SubmoduleValidationException (
throw new SubmoduleValidationException (
MessageFormat . format (
MessageFormat . format (
JGitText . get ( ) . submodulePathInvalid , path ) ) ;
JGitText . get ( ) . submodulePathInvalid , path ) ,
GITMODULES_PATH ) ;
}
}
}
}
/ * *
/ * *
* Validate a . gitmodules file
*
* @param gitModulesContents
* @param gitModulesContents
* Contents of a . gitmodule file . They will be parsed internally .
* Contents of a . gitmodule file . They will be parsed internally .
* @throws IOException
* @throws SubmoduleValidationException
* If the contents
* if the contents don ' t look like a configuration file or field
* values are not valid
* /
* /
public static void assertValidGitModulesFile ( String gitModulesContents )
public static void assertValidGitModulesFile ( String gitModulesContents )
throws IOException {
throws SubmoduleValidationException {
// Validate .gitmodules file
Config c = new Config ( ) ;
Config c = new Config ( ) ;
try {
try {
c . fromText ( gitModulesContents ) ;
c . fromText ( gitModulesContents ) ;
@ -173,12 +198,9 @@ public class SubmoduleValidator {
}
}
}
}
} catch ( ConfigInvalidException e ) {
} catch ( ConfigInvalidException e ) {
throw new IOException (
throw new SubmoduleValidationException (
MessageFormat . format (
JGitText . get ( ) . invalidGitModules ,
JGitText . get ( ) . invalidGitModules ,
e ) ) ;
GITMODULES_PARSE ) ;
} catch ( SubmoduleValidationException e ) {
throw new IOException ( e . getMessage ( ) , e ) ;
}
}
}
}
}
}