* @param workTree The worktree of the repository or {@code null}
* @param gitDir The GIT_DIR of the repository or {@code null}
*/
public JGitRepository(File workTree, File gitDir)
throws GitRepositoryException {
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.readEnvironment();
if (gitDir == null && workTree == null) {
throw new GitRepositoryException("Neither worktree nor GIT_DIR is set.");
} else {
if (workTree != null && !workTree.exists()) {
throw new GitRepositoryException("The worktree " + workTree + " does not exist");
}
if (gitDir != null && !gitDir.exists()) {
throw new GitRepositoryException("The GIT_DIR " + gitDir + " does not exist");
}
}
repositoryBuilder.setWorkTree(workTree);
if (gitDir != null) {
repositoryBuilder.setGitDir(gitDir);
} else {
repositoryBuilder.findGitDir(workTree);
if (repositoryBuilder.getGitDir() == null) {
throw new GitRepositoryException(workTree + " is not inside a Git repository. Please specify the GIT_DIR separately.");
}
}
try {
this.repository = repositoryBuilder.build();
} catch (IOException e) {
throw new GitRepositoryException("Could not initialize repository", e);
}
this.commitCache = new HashMap<ObjectId, RevCommit>();