diff options
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; } } |