diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2010-06-24 09:07:53 -0700 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2010-06-25 17:58:40 -0700 |
commit | 532421d98925f23ddaa63c8d5f22be24879a6385 (patch) | |
tree | f22425b408d55019bacab3a20adae68e390528bd /org.eclipse.jgit.test/tst | |
parent | 8f46ee4870d0a49fcce1d0cb2b8e2aee9aaafee1 (diff) | |
download | jgit-532421d98925f23ddaa63c8d5f22be24879a6385.tar.gz jgit-532421d98925f23ddaa63c8d5f22be24879a6385.zip |
Refactor repository construction to builder class
The new FileRepositoryBuilder class helps applications to construct
a properly configured FileRepository, with properties assumed based
upon the standard Git rules for the local filesystem.
To better support simple command line applications, environment
variable handling and repository searching was moved into this
builder class.
The change gets rid of the ever-growing FileRepository constructor
variants, and the multitude of java.io.File typed parameters,
by using simple named setter methods.
Change-Id: I17e8e0392ad1dbf6a90a7eb49a6d809388d27e4c
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositorySetupWorkDirTest.java | 52 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java | 22 |
2 files changed, 39 insertions, 35 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositorySetupWorkDirTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositorySetupWorkDirTest.java index 070713d7b3..187d3ae9da 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositorySetupWorkDirTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositorySetupWorkDirTest.java @@ -47,6 +47,7 @@ package org.eclipse.jgit.lib; import java.io.File; import java.io.IOException; +import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; /** @@ -78,7 +79,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception { File workdir = getFile("workdir", "repo"); - Repository repo = new FileRepository(null, workdir); + FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build(); assertFalse(repo.isBare()); assertWorkdirPath(repo, "workdir", "repo"); assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT); @@ -87,7 +88,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly() throws Exception { File workdir = getFile("workdir", "repo"); - Repository repo = new FileRepository(null, workdir); + FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build(); assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT); } @@ -96,7 +97,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { File gitDir = getFile("workdir", "repoWithConfig"); File workTree = getFile("workdir", "treeRoot"); setWorkTree(gitDir, workTree); - Repository repo = new FileRepository(gitDir, null); + FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); assertFalse(repo.isBare()); assertWorkdirPath(repo, "workdir", "treeRoot"); assertGitdirPath(repo, "workdir", "repoWithConfig"); @@ -106,7 +107,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { throws Exception { File gitDir = getFile("workdir", "repoWithConfig"); setBare(gitDir, true); - Repository repo = new FileRepository(gitDir, null); + FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); assertTrue(repo.isBare()); } @@ -114,7 +115,7 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { throws Exception { File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child"); setBare(gitDir, false); - Repository repo = new FileRepository(gitDir, null); + FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue"); } @@ -122,21 +123,12 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { throws Exception { File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child"); setBare(gitDir, false); - Repository repo = new FileRepository(gitDir, null); + FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); assertFalse(repo.isBare()); assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse"); assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child"); } - public void testNotBare_MakeBareUnbareBySetWorkdir() throws Exception { - File gitDir = getFile("gitDir"); - Repository repo = new FileRepository(gitDir); - repo.setWorkDir(getFile("workingDir")); - assertFalse(repo.isBare()); - assertWorkdirPath(repo, "workingDir"); - assertGitdirPath(repo, "gitDir"); - } - public void testExceptionThrown_BareRepoGetWorkDir() throws Exception { File gitDir = getFile("workdir"); try { @@ -176,20 +168,28 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase { return result; } - private void setBare(File gitDir, boolean bare) throws IOException { - FileRepository repo = new FileRepository(gitDir, null); - repo.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + private void setBare(File gitDir, boolean bare) throws IOException, + ConfigInvalidException { + FileBasedConfig cfg = configFor(gitDir); + cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_BARE, bare); - repo.getConfig().save(); + cfg.save(); + } + + private void setWorkTree(File gitDir, File workTree) throws IOException, + ConfigInvalidException { + String path = workTree.getAbsolutePath(); + FileBasedConfig cfg = configFor(gitDir); + cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_WORKTREE, path); + cfg.save(); } - private void setWorkTree(File gitDir, File workTree) throws IOException { - FileRepository repo = new FileRepository(gitDir, null); - repo.getConfig() - .setString(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_WORKTREE, - workTree.getAbsolutePath()); - repo.getConfig().save(); + private FileBasedConfig configFor(File gitDir) throws IOException, + ConfigInvalidException { + FileBasedConfig cfg = new FileBasedConfig(new File(gitDir, "config")); + cfg.load(); + return cfg; } private void assertGitdirPath(Repository repo, String... expected) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java index c770d605d0..8dd5ae2bd0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java @@ -80,7 +80,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { public void test000_openRepoBadArgs() throws IOException { try { - new FileRepository(null, null); + new FileRepositoryBuilder().build(); fail("Must pass either GIT_DIR or GIT_WORK_TREE"); } catch (IllegalArgumentException e) { assertEquals( @@ -102,7 +102,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(theDir, null); + FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(repo1Parent, r.getWorkDir()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); @@ -122,7 +122,8 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(theDir, repo1Parent.getParentFile()); + FileRepository r = new FileRepositoryBuilder().setGitDir(theDir) + .setWorkTree(repo1Parent.getParentFile()).build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(repo1Parent.getParentFile(), r.getWorkDir()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); @@ -142,7 +143,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(null, repo1Parent); + FileRepository r = new FileRepositoryBuilder().setWorkTree(repo1Parent).build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(repo1Parent, r.getWorkDir()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); @@ -167,7 +168,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(theDir, null); + FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(workdir, r.getWorkDir()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); @@ -192,7 +193,7 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(theDir, null); + FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(workdir, r.getWorkDir()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); @@ -210,14 +211,17 @@ public class T0003_Basic extends SampleDataRepositoryTestCase { File repo1Parent = new File(trash.getParentFile(), "r1"); File indexFile = new File(trash, "idx"); File objDir = new File(trash, "../obj"); - File[] altObjDirs = new File[] { db.getObjectsDirectory() }; + File altObjDir = db.getObjectsDirectory(); Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT)); repo1initial.create(); repo1initial.close(); File theDir = new File(repo1Parent, Constants.DOT_GIT); - Repository r = new FileRepository(theDir, null, objDir, altObjDirs, - indexFile); + FileRepository r = new FileRepositoryBuilder() // + .setGitDir(theDir).setObjectDirectory(objDir) // + .addAlternateObjectDirectory(altObjDir) // + .setIndexFile(indexFile) // + .build(); assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir.getParentFile(), r.getWorkDir()); assertEqualsPath(indexFile, r.getIndexFile()); |