Merge changes If98b0b97,I7c9c09b4

* changes:
  Add convenience factory method for most used builder pattern
  Don't use internal type FileRepository in public API
This commit is contained in:
Shawn Pearce 2013-03-21 03:52:33 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit 9aee4e0a26
5 changed files with 47 additions and 27 deletions

View File

@ -54,7 +54,7 @@
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.RawParseUtils;
@ -135,14 +135,14 @@ private static ClassLoader cl() {
return JGitTestUtil.class.getClassLoader(); return JGitTestUtil.class.getClassLoader();
} }
public static File writeTrashFile(final FileRepository db, public static File writeTrashFile(final Repository db,
final String name, final String data) throws IOException { final String name, final String data) throws IOException {
File path = new File(db.getWorkTree(), name); File path = new File(db.getWorkTree(), name);
write(path, data); write(path, data);
return path; return path;
} }
public static File writeTrashFile(final FileRepository db, public static File writeTrashFile(final Repository db,
final String subdir, final String subdir,
final String name, final String data) throws IOException { final String name, final String data) throws IOException {
File path = new File(db.getWorkTree() + "/" + subdir, name); File path = new File(db.getWorkTree() + "/" + subdir, name);
@ -190,13 +190,13 @@ public static String read(final File file) throws IOException {
return new String(body, 0, body.length, "UTF-8"); return new String(body, 0, body.length, "UTF-8");
} }
public static String read(final FileRepository db, final String name) public static String read(final Repository db, final String name)
throws IOException { throws IOException {
File file = new File(db.getWorkTree(), name); File file = new File(db.getWorkTree(), name);
return read(file); return read(file);
} }
public static void deleteTrashFile(final FileRepository db, public static void deleteTrashFile(final Repository db,
final String name) throws IOException { final String name) throws IOException {
File path = new File(db.getWorkTree(), name); File path = new File(db.getWorkTree(), name);
FileUtils.delete(path); FileUtils.delete(path);

View File

@ -55,6 +55,7 @@
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
@ -126,7 +127,7 @@ public void absoluteGitDirRef() throws Exception {
builder.setWorkTree(dir); builder.setWorkTree(dir);
builder.setMustExist(true); builder.setMustExist(true);
FileRepository repo2 = builder.build(); Repository repo2 = builder.build();
assertEquals(repo1.getDirectory(), repo2.getDirectory()); assertEquals(repo1.getDirectory(), repo2.getDirectory());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
@ -145,7 +146,7 @@ public void relativeGitDirRef() throws Exception {
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir); builder.setWorkTree(dir);
builder.setMustExist(true); builder.setMustExist(true);
FileRepository repo2 = builder.build(); Repository repo2 = builder.build();
assertEquals(repo1.getDirectory(), repo2.getDirectory()); assertEquals(repo1.getDirectory(), repo2.getDirectory());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
@ -165,7 +166,7 @@ public void scanWithGitDirRef() throws Exception {
builder.findGitDir(dir); builder.findGitDir(dir);
assertEquals(repo1.getDirectory(), builder.getGitDir()); assertEquals(repo1.getDirectory(), builder.getGitDir());
builder.setMustExist(true); builder.setMustExist(true);
FileRepository repo2 = builder.build(); Repository repo2 = builder.build();
assertEquals(repo1.getDirectory(), repo2.getDirectory()); assertEquals(repo1.getDirectory(), repo2.getDirectory());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());

View File

@ -97,7 +97,8 @@ public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
@Test @Test
public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception { public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
File workdir = getFile("workdir", "repo"); File workdir = getFile("workdir", "repo");
FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build(); Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
.build();
assertFalse(repo.isBare()); assertFalse(repo.isBare());
assertWorkdirPath(repo, "workdir", "repo"); assertWorkdirPath(repo, "workdir", "repo");
assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT); assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
@ -107,7 +108,8 @@ public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly() public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
throws Exception { throws Exception {
File workdir = getFile("workdir", "repo"); File workdir = getFile("workdir", "repo");
FileRepository repo = new FileRepositoryBuilder().setWorkTree(workdir).build(); Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
.build();
assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT); assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
} }
@ -117,7 +119,7 @@ public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
File gitDir = getFile("workdir", "repoWithConfig"); File gitDir = getFile("workdir", "repoWithConfig");
File workTree = getFile("workdir", "treeRoot"); File workTree = getFile("workdir", "treeRoot");
setWorkTree(gitDir, workTree); setWorkTree(gitDir, workTree);
FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertFalse(repo.isBare()); assertFalse(repo.isBare());
assertWorkdirPath(repo, "workdir", "treeRoot"); assertWorkdirPath(repo, "workdir", "treeRoot");
assertGitdirPath(repo, "workdir", "repoWithConfig"); assertGitdirPath(repo, "workdir", "repoWithConfig");
@ -128,7 +130,7 @@ public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
throws Exception { throws Exception {
File gitDir = getFile("workdir", "repoWithConfig"); File gitDir = getFile("workdir", "repoWithConfig");
setBare(gitDir, true); setBare(gitDir, true);
FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertTrue(repo.isBare()); assertTrue(repo.isBare());
} }
@ -137,7 +139,7 @@ public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFals
throws Exception { throws Exception {
File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child"); File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
setBare(gitDir, false); setBare(gitDir, false);
FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue"); assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
} }
@ -146,7 +148,7 @@ public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
throws Exception { throws Exception {
File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child"); File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
setBare(gitDir, false); setBare(gitDir, false);
FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build(); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertFalse(repo.isBare()); assertFalse(repo.isBare());
assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse"); assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child"); assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");

View File

@ -140,8 +140,8 @@ public void test000_openrepo_default_gitDirSet() throws IOException {
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder().setGitDir(theDir) FileRepository r = (FileRepository) new FileRepositoryBuilder()
.build(); .setGitDir(theDir).build();
assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir, r.getDirectory());
assertEqualsPath(repo1Parent, r.getWorkTree()); assertEqualsPath(repo1Parent, r.getWorkTree());
assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
@ -165,8 +165,9 @@ public void test000_openrepo_default_gitDirAndWorkTreeSet()
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder().setGitDir(theDir) FileRepository r = (FileRepository) new FileRepositoryBuilder()
.setWorkTree(repo1Parent.getParentFile()).build(); .setGitDir(theDir).setWorkTree(repo1Parent.getParentFile())
.build();
assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir, r.getDirectory());
assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree()); assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree());
assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
@ -189,8 +190,8 @@ public void test000_openrepo_default_workDirSet() throws IOException {
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder().setWorkTree(repo1Parent) FileRepository r = (FileRepository) new FileRepositoryBuilder()
.build(); .setWorkTree(repo1Parent).build();
assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir, r.getDirectory());
assertEqualsPath(repo1Parent, r.getWorkTree()); assertEqualsPath(repo1Parent, r.getWorkTree());
assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
@ -218,8 +219,8 @@ public void test000_openrepo_default_absolute_workdirconfig()
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder().setGitDir(theDir) FileRepository r = (FileRepository) new FileRepositoryBuilder()
.build(); .setGitDir(theDir).build();
assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir, r.getDirectory());
assertEqualsPath(workdir, r.getWorkTree()); assertEqualsPath(workdir, r.getWorkTree());
assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
@ -247,8 +248,8 @@ public void test000_openrepo_default_relative_workdirconfig()
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder().setGitDir(theDir) FileRepository r = (FileRepository) new FileRepositoryBuilder()
.build(); .setGitDir(theDir).build();
assertEqualsPath(theDir, r.getDirectory()); assertEqualsPath(theDir, r.getDirectory());
assertEqualsPath(workdir, r.getWorkTree()); assertEqualsPath(workdir, r.getWorkTree());
assertEqualsPath(new File(theDir, "index"), r.getIndexFile()); assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
@ -275,7 +276,7 @@ public void test000_openrepo_alternate_index_file_and_objdirs()
repo1initial.close(); repo1initial.close();
File theDir = new File(repo1Parent, Constants.DOT_GIT); File theDir = new File(repo1Parent, Constants.DOT_GIT);
FileRepository r = new FileRepositoryBuilder() // FileRepository r = (FileRepository) new FileRepositoryBuilder() //
.setGitDir(theDir).setObjectDirectory(objDir) // .setGitDir(theDir).setObjectDirectory(objDir) //
.addAlternateObjectDirectory(altObjDir) // .addAlternateObjectDirectory(altObjDir) //
.setIndexFile(indexFile) // .setIndexFile(indexFile) //

View File

@ -49,6 +49,7 @@
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.BaseRepositoryBuilder; import org.eclipse.jgit.lib.BaseRepositoryBuilder;
import org.eclipse.jgit.lib.Repository;
/** /**
* Constructs a {@link FileRepository}. * Constructs a {@link FileRepository}.
@ -70,7 +71,7 @@
* </pre> * </pre>
*/ */
public class FileRepositoryBuilder extends public class FileRepositoryBuilder extends
BaseRepositoryBuilder<FileRepositoryBuilder, FileRepository> { BaseRepositoryBuilder<FileRepositoryBuilder, Repository> {
/** /**
* Create a repository matching the configuration in this builder. * Create a repository matching the configuration in this builder.
* <p> * <p>
@ -86,10 +87,25 @@ public class FileRepositoryBuilder extends
* the builder's parameters. * the builder's parameters.
*/ */
@Override @Override
public FileRepository build() throws IOException { public Repository build() throws IOException {
FileRepository repo = new FileRepository(setup()); FileRepository repo = new FileRepository(setup());
if (isMustExist() && !repo.getObjectDatabase().exists()) if (isMustExist() && !repo.getObjectDatabase().exists())
throw new RepositoryNotFoundException(getGitDir()); throw new RepositoryNotFoundException(getGitDir());
return repo; return repo;
} }
/**
* Convenience factory method to construct a {@link FileRepository}.
*
* @param gitDir
* {@code GIT_DIR}, the repository meta directory.
* @return a repository matching this configuration.
* @throws IOException
* the repository could not be accessed to configure the rest of
* the builder's parameters.
*/
public static Repository create(File gitDir) throws IOException {
return new FileRepositoryBuilder().setGitDir(gitDir).readEnvironment()
.build();
}
} }