FileRepositoryBuilder.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.storage.file;

  11. import java.io.File;
  12. import java.io.IOException;

  13. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  14. import org.eclipse.jgit.internal.storage.file.FileRepository;
  15. import org.eclipse.jgit.lib.BaseRepositoryBuilder;
  16. import org.eclipse.jgit.lib.Repository;

  17. /**
  18.  * Constructs a {@link org.eclipse.jgit.internal.storage.file.FileRepository}.
  19.  * <p>
  20.  * Applications must set one of {@link #setGitDir(File)} or
  21.  * {@link #setWorkTree(File)}, or use {@link #readEnvironment()} or
  22.  * {@link #findGitDir()} in order to configure the minimum property set
  23.  * necessary to open a repository.
  24.  * <p>
  25.  * Single repository applications trying to be compatible with other Git
  26.  * implementations are encouraged to use a model such as:
  27.  *
  28.  * <pre>
  29.  * new FileRepositoryBuilder() //
  30.  *      .setGitDir(gitDirArgument) // --git-dir if supplied, no-op if null
  31.  *      .readEnvironment() // scan environment GIT_* variables
  32.  *      .findGitDir() // scan up the file system tree
  33.  *      .build()
  34.  * </pre>
  35.  */
  36. public class FileRepositoryBuilder extends
  37.         BaseRepositoryBuilder<FileRepositoryBuilder, Repository> {
  38.     /**
  39.      * {@inheritDoc}
  40.      * <p>
  41.      * Create a repository matching the configuration in this builder.
  42.      * <p>
  43.      * If an option was not set, the build method will try to default the option
  44.      * based on other options. If insufficient information is available, an
  45.      * exception is thrown to the caller.
  46.      *
  47.      * @since 3.0
  48.      */
  49.     @Override
  50.     public Repository build() throws IOException {
  51.         FileRepository repo = new FileRepository(setup());
  52.         if (isMustExist() && !repo.getObjectDatabase().exists())
  53.             throw new RepositoryNotFoundException(getGitDir());
  54.         return repo;
  55.     }

  56.     /**
  57.      * Convenience factory method to construct a
  58.      * {@link org.eclipse.jgit.internal.storage.file.FileRepository}.
  59.      *
  60.      * @param gitDir
  61.      *            {@code GIT_DIR}, the repository meta directory.
  62.      * @return a repository matching this configuration.
  63.      * @throws java.io.IOException
  64.      *             the repository could not be accessed to configure the rest of
  65.      *             the builder's parameters.
  66.      * @since 3.0
  67.      */
  68.     public static Repository create(File gitDir) throws IOException {
  69.         return new FileRepositoryBuilder().setGitDir(gitDir).readEnvironment()
  70.                 .build();
  71.     }
  72. }