diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2011-02-12 18:44:39 -0800 |
---|---|---|
committer | Chris Aniszczyk <caniszczyk@gmail.com> | 2011-02-15 18:17:46 -0600 |
commit | a06be83045f8aa0584137849c412dd6b3de8d5fe (patch) | |
tree | 1fa55d3cf1d306d5b150875a42e8c411abe46383 /org.eclipse.jgit | |
parent | 6490090f14684b6026a7aa1ad4fd10c5611366b5 (diff) | |
download | jgit-a06be83045f8aa0584137849c412dd6b3de8d5fe.tar.gz jgit-a06be83045f8aa0584137849c412dd6b3de8d5fe.zip |
RepositoryBuilder: Allow callers to require repository exists
The setMustExist() method allows callers to require the repository
exists in order for build() to succeed. This is useful within a
RepositoryResolver where existence is required.
Change-Id: I6a1154551435cf0da6c2b4a7f4dce266abea5dff
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java | 27 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java | 6 |
2 files changed, 31 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java index f1e54ed1df..e4cecac777 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java @@ -63,6 +63,7 @@ import java.util.List; import org.eclipse.jgit.JGitText; import org.eclipse.jgit.errors.ConfigInvalidException; +import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.RepositoryCache.FileKey; import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.storage.file.FileRepository; @@ -102,6 +103,9 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re /** True only if the caller wants to force bare behavior. */ private boolean bare; + /** True if the caller requires the repository to exist. */ + private boolean mustExist; + /** Configuration file of target repository, lazily loaded if required. */ private Config config; @@ -248,6 +252,24 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re } /** + * Require the repository to exist before it can be opened. + * + * @param mustExist + * true if it must exist; false if it can be missing and created + * after being built. + * @return {@code this} (for chaining calls). + */ + public B setMustExist(boolean mustExist) { + this.mustExist = mustExist; + return self(); + } + + /** @return true if the repository must exist before being opened. */ + public boolean isMustExist() { + return mustExist; + } + + /** * Set the top level directory of the working files. * * @param workTree @@ -504,7 +526,10 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re */ @SuppressWarnings("unchecked") public R build() throws IOException { - return (R) new FileRepository(setup()); + R repo = (R) new FileRepository(setup()); + if (isMustExist() && !repo.getObjectDatabase().exists()) + throw new RepositoryNotFoundException(getGitDir()); + return repo; } /** Require either {@code gitDir} or {@code workTree} to be set. */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java index 31d3e99ad6..377fb04443 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java @@ -46,6 +46,7 @@ package org.eclipse.jgit.storage.file; import java.io.File; import java.io.IOException; +import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.BaseRepositoryBuilder; /** @@ -85,6 +86,9 @@ public class FileRepositoryBuilder extends */ @Override public FileRepository build() throws IOException { - return new FileRepository(setup()); + FileRepository repo = new FileRepository(setup()); + if (isMustExist() && !repo.getObjectDatabase().exists()) + throw new RepositoryNotFoundException(getGitDir()); + return repo; } } |