diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileRepositoryBuilderTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileRepositoryBuilderTest.java index 80e0f1a92..aed48aa5f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileRepositoryBuilderTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileRepositoryBuilderTest.java @@ -44,10 +44,14 @@ package org.eclipse.jgit.storage.file; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import java.io.File; +import java.io.IOException; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; +import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.util.FileUtils; import org.junit.Test; @@ -61,4 +65,47 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase { assertEquals(r.getDirectory(), new FileRepositoryBuilder() .findGitDir(d).getGitDir()); } + + @Test + public void emptyRepositoryFormatVersion() throws Exception { + FileRepository r = createWorkRepository(); + FileBasedConfig config = r.getConfig(); + config.setString(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, ""); + config.save(); + + new FileRepository(r.getDirectory()); + } + + @Test + public void invalidRepositoryFormatVersion() throws Exception { + FileRepository r = createWorkRepository(); + FileBasedConfig config = r.getConfig(); + config.setString(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); + config.save(); + + try { + new FileRepository(r.getDirectory()); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + assertNotNull(e.getMessage()); + } + } + + @Test + public void unknownRepositoryFormatVersion() throws Exception { + FileRepository r = createWorkRepository(); + FileBasedConfig config = r.getConfig(); + config.setLong(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 1); + config.save(); + + try { + new FileRepository(r.getDirectory()); + fail("IOException not thrown"); + } catch (IOException e) { + assertNotNull(e.getMessage()); + } + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_BasicTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_BasicTest.java index 37d28d2ab..2a9bb7a56 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_BasicTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_BasicTest.java @@ -370,9 +370,8 @@ public class T0003_BasicTest extends SampleDataRepositoryTestCase { try { new FileRepository(db.getDirectory()); fail("incorrectly opened a bad repository"); - } catch (IOException ioe) { - assertTrue(ioe.getMessage().indexOf("format") > 0); - assertTrue(ioe.getMessage().indexOf(badvers) > 0); + } catch (IllegalArgumentException ioe) { + assertNotNull(ioe.getMessage()); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepository.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepository.java index f1a8367ee..fa87b9f26 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepository.java @@ -181,14 +181,13 @@ public class FileRepository extends Repository { getFS()); if (objectDatabase.exists()) { - final String repositoryFormatVersion = getConfig().getString( + final long repositoryFormatVersion = getConfig().getLong( ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION); - if (!"0".equals(repositoryFormatVersion)) { + ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0); + if (repositoryFormatVersion > 0) throw new IOException(MessageFormat.format( JGitText.get().unknownRepositoryFormat2, - repositoryFormatVersion)); - } + Long.valueOf(repositoryFormatVersion))); } if (!isBare())