Browse Source

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>
stable-0.12
Shawn O. Pearce 14 years ago committed by Chris Aniszczyk
parent
commit
a06be83045
  1. 27
      org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
  2. 6
      org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileRepositoryBuilder.java

27
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;
@ -247,6 +251,24 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re
return bare;
}
/**
* 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.
*
@ -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. */

6
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;
}
}

Loading…
Cancel
Save