InitCommand.java

  1. /*
  2.  * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> 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.api;

  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.text.MessageFormat;
  14. import java.util.concurrent.Callable;

  15. import org.eclipse.jgit.api.errors.GitAPIException;
  16. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  17. import org.eclipse.jgit.api.errors.JGitInternalException;
  18. import org.eclipse.jgit.errors.ConfigInvalidException;
  19. import org.eclipse.jgit.internal.JGitText;
  20. import org.eclipse.jgit.lib.ConfigConstants;
  21. import org.eclipse.jgit.lib.Constants;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.lib.RepositoryBuilder;
  24. import org.eclipse.jgit.util.FS;
  25. import org.eclipse.jgit.util.StringUtils;
  26. import org.eclipse.jgit.util.SystemReader;

  27. /**
  28.  * Create an empty git repository or reinitalize an existing one
  29.  *
  30.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  31.  *      >Git documentation about init</a>
  32.  */
  33. public class InitCommand implements Callable<Git> {
  34.     private File directory;

  35.     private File gitDir;

  36.     private boolean bare;

  37.     private FS fs;

  38.     private String initialBranch;

  39.     /**
  40.      * {@inheritDoc}
  41.      * <p>
  42.      * Executes the {@code Init} command.
  43.      *
  44.      * @return a {@code Git} instance that owns the {@code Repository} that it
  45.      *         wraps.
  46.      */
  47.     @Override
  48.     public Git call() throws GitAPIException {
  49.         try {
  50.             RepositoryBuilder builder = new RepositoryBuilder();
  51.             if (bare)
  52.                 builder.setBare();
  53.             if (fs != null) {
  54.                 builder.setFS(fs);
  55.             }
  56.             builder.readEnvironment();
  57.             if (gitDir != null)
  58.                 builder.setGitDir(gitDir);
  59.             else
  60.                 gitDir = builder.getGitDir();
  61.             if (directory != null) {
  62.                 if (bare)
  63.                     builder.setGitDir(directory);
  64.                 else {
  65.                     builder.setWorkTree(directory);
  66.                     if (gitDir == null)
  67.                         builder.setGitDir(new File(directory, Constants.DOT_GIT));
  68.                 }
  69.             } else if (builder.getGitDir() == null) {
  70.                 String dStr = SystemReader.getInstance()
  71.                         .getProperty("user.dir"); //$NON-NLS-1$
  72.                 if (dStr == null)
  73.                     dStr = "."; //$NON-NLS-1$
  74.                 File d = new File(dStr);
  75.                 if (!bare)
  76.                     d = new File(d, Constants.DOT_GIT);
  77.                 builder.setGitDir(d);
  78.             } else {
  79.                 // directory was not set but gitDir was set
  80.                 if (!bare) {
  81.                     String dStr = SystemReader.getInstance().getProperty(
  82.                             "user.dir"); //$NON-NLS-1$
  83.                     if (dStr == null)
  84.                         dStr = "."; //$NON-NLS-1$
  85.                     builder.setWorkTree(new File(dStr));
  86.                 }
  87.             }
  88.             builder.setInitialBranch(StringUtils.isEmptyOrNull(initialBranch)
  89.                     ? SystemReader.getInstance().getUserConfig().getString(
  90.                             ConfigConstants.CONFIG_INIT_SECTION, null,
  91.                             ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH)
  92.                     : initialBranch);
  93.             Repository repository = builder.build();
  94.             if (!repository.getObjectDatabase().exists())
  95.                 repository.create(bare);
  96.             return new Git(repository, true);
  97.         } catch (IOException | ConfigInvalidException e) {
  98.             throw new JGitInternalException(e.getMessage(), e);
  99.         }
  100.     }

  101.     /**
  102.      * The optional directory associated with the init operation. If no
  103.      * directory is set, we'll use the current directory
  104.      *
  105.      * @param directory
  106.      *            the directory to init to
  107.      * @return this instance
  108.      * @throws java.lang.IllegalStateException
  109.      *             if the combination of directory, gitDir and bare is illegal.
  110.      *             E.g. if for a non-bare repository directory and gitDir point
  111.      *             to the same directory of if for a bare repository both
  112.      *             directory and gitDir are specified
  113.      */
  114.     public InitCommand setDirectory(File directory)
  115.             throws IllegalStateException {
  116.         validateDirs(directory, gitDir, bare);
  117.         this.directory = directory;
  118.         return this;
  119.     }

  120.     /**
  121.      * Set the repository meta directory (.git)
  122.      *
  123.      * @param gitDir
  124.      *            the repository meta directory
  125.      * @return this instance
  126.      * @throws java.lang.IllegalStateException
  127.      *             if the combination of directory, gitDir and bare is illegal.
  128.      *             E.g. if for a non-bare repository directory and gitDir point
  129.      *             to the same directory of if for a bare repository both
  130.      *             directory and gitDir are specified
  131.      * @since 3.6
  132.      */
  133.     public InitCommand setGitDir(File gitDir)
  134.             throws IllegalStateException {
  135.         validateDirs(directory, gitDir, bare);
  136.         this.gitDir = gitDir;
  137.         return this;
  138.     }

  139.     private static void validateDirs(File directory, File gitDir, boolean bare)
  140.             throws IllegalStateException {
  141.         if (directory != null) {
  142.             if (bare) {
  143.                 if (gitDir != null && !gitDir.equals(directory))
  144.                     throw new IllegalStateException(MessageFormat.format(
  145.                             JGitText.get().initFailedBareRepoDifferentDirs,
  146.                             gitDir, directory));
  147.             } else {
  148.                 if (gitDir != null && gitDir.equals(directory))
  149.                     throw new IllegalStateException(MessageFormat.format(
  150.                             JGitText.get().initFailedNonBareRepoSameDirs,
  151.                             gitDir, directory));
  152.             }
  153.         }
  154.     }

  155.     /**
  156.      * Set whether the repository is bare or not
  157.      *
  158.      * @param bare
  159.      *            whether the repository is bare or not
  160.      * @throws java.lang.IllegalStateException
  161.      *             if the combination of directory, gitDir and bare is illegal.
  162.      *             E.g. if for a non-bare repository directory and gitDir point
  163.      *             to the same directory of if for a bare repository both
  164.      *             directory and gitDir are specified
  165.      * @return this instance
  166.      */
  167.     public InitCommand setBare(boolean bare) {
  168.         validateDirs(directory, gitDir, bare);
  169.         this.bare = bare;
  170.         return this;
  171.     }

  172.     /**
  173.      * Set the file system abstraction to be used for repositories created by
  174.      * this command.
  175.      *
  176.      * @param fs
  177.      *            the abstraction.
  178.      * @return {@code this} (for chaining calls).
  179.      * @since 4.10
  180.      */
  181.     public InitCommand setFs(FS fs) {
  182.         this.fs = fs;
  183.         return this;
  184.     }

  185.     /**
  186.      * Set the initial branch of the new repository. If not specified
  187.      * ({@code null} or empty), fall back to the default name (currently
  188.      * master).
  189.      *
  190.      * @param branch
  191.      *            initial branch name of the new repository
  192.      * @return {@code this}
  193.      * @throws InvalidRefNameException
  194.      *             if the branch name is not valid
  195.      *
  196.      * @since 5.11
  197.      */
  198.     public InitCommand setInitialBranch(String branch)
  199.             throws InvalidRefNameException {
  200.         this.initialBranch = branch;
  201.         return this;
  202.     }
  203. }