InitCommand.java

  1. /*
  2.  * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3.  * and other copyright owners as documented in the project's IP log.
  4.  *
  5.  * This program and the accompanying materials are made available
  6.  * under the terms of the Eclipse Distribution License v1.0 which
  7.  * accompanies this distribution, is reproduced below, and is
  8.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or
  13.  * without modification, are permitted provided that the following
  14.  * conditions are met:
  15.  *
  16.  * - Redistributions of source code must retain the above copyright
  17.  *   notice, this list of conditions and the following disclaimer.
  18.  *
  19.  * - Redistributions in binary form must reproduce the above
  20.  *   copyright notice, this list of conditions and the following
  21.  *   disclaimer in the documentation and/or other materials provided
  22.  *   with the distribution.
  23.  *
  24.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  25.  *   names of its contributors may be used to endorse or promote
  26.  *   products derived from this software without specific prior
  27.  *   written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  */
  43. package org.eclipse.jgit.api;

  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.text.MessageFormat;
  47. import java.util.concurrent.Callable;

  48. import org.eclipse.jgit.api.errors.GitAPIException;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.internal.JGitText;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.lib.RepositoryBuilder;
  54. import org.eclipse.jgit.util.FS;
  55. import org.eclipse.jgit.util.SystemReader;

  56. /**
  57.  * Create an empty git repository or reinitalize an existing one
  58.  *
  59.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  60.  *      >Git documentation about init</a>
  61.  */
  62. public class InitCommand implements Callable<Git> {
  63.     private File directory;

  64.     private File gitDir;

  65.     private boolean bare;

  66.     private FS fs;

  67.     /**
  68.      * {@inheritDoc}
  69.      * <p>
  70.      * Executes the {@code Init} command.
  71.      *
  72.      * @return a {@code Git} instance that owns the {@code Repository} that it
  73.      *         wraps.
  74.      */
  75.     @Override
  76.     public Git call() throws GitAPIException {
  77.         try {
  78.             RepositoryBuilder builder = new RepositoryBuilder();
  79.             if (bare)
  80.                 builder.setBare();
  81.             if (fs != null) {
  82.                 builder.setFS(fs);
  83.             }
  84.             builder.readEnvironment();
  85.             if (gitDir != null)
  86.                 builder.setGitDir(gitDir);
  87.             else
  88.                 gitDir = builder.getGitDir();
  89.             if (directory != null) {
  90.                 if (bare)
  91.                     builder.setGitDir(directory);
  92.                 else {
  93.                     builder.setWorkTree(directory);
  94.                     if (gitDir == null)
  95.                         builder.setGitDir(new File(directory, Constants.DOT_GIT));
  96.                 }
  97.             } else if (builder.getGitDir() == null) {
  98.                 String dStr = SystemReader.getInstance()
  99.                         .getProperty("user.dir"); //$NON-NLS-1$
  100.                 if (dStr == null)
  101.                     dStr = "."; //$NON-NLS-1$
  102.                 File d = new File(dStr);
  103.                 if (!bare)
  104.                     d = new File(d, Constants.DOT_GIT);
  105.                 builder.setGitDir(d);
  106.             } else {
  107.                 // directory was not set but gitDir was set
  108.                 if (!bare) {
  109.                     String dStr = SystemReader.getInstance().getProperty(
  110.                             "user.dir"); //$NON-NLS-1$
  111.                     if (dStr == null)
  112.                         dStr = "."; //$NON-NLS-1$
  113.                     builder.setWorkTree(new File(dStr));
  114.                 }
  115.             }
  116.             Repository repository = builder.build();
  117.             if (!repository.getObjectDatabase().exists())
  118.                 repository.create(bare);
  119.             return new Git(repository, true);
  120.         } catch (IOException e) {
  121.             throw new JGitInternalException(e.getMessage(), e);
  122.         }
  123.     }

  124.     /**
  125.      * The optional directory associated with the init operation. If no
  126.      * directory is set, we'll use the current directory
  127.      *
  128.      * @param directory
  129.      *            the directory to init to
  130.      * @return this instance
  131.      * @throws java.lang.IllegalStateException
  132.      *             if the combination of directory, gitDir and bare is illegal.
  133.      *             E.g. if for a non-bare repository directory and gitDir point
  134.      *             to the same directory of if for a bare repository both
  135.      *             directory and gitDir are specified
  136.      */
  137.     public InitCommand setDirectory(File directory)
  138.             throws IllegalStateException {
  139.         validateDirs(directory, gitDir, bare);
  140.         this.directory = directory;
  141.         return this;
  142.     }

  143.     /**
  144.      * Set the repository meta directory (.git)
  145.      *
  146.      * @param gitDir
  147.      *            the repository meta directory
  148.      * @return this instance
  149.      * @throws java.lang.IllegalStateException
  150.      *             if the combination of directory, gitDir and bare is illegal.
  151.      *             E.g. if for a non-bare repository directory and gitDir point
  152.      *             to the same directory of if for a bare repository both
  153.      *             directory and gitDir are specified
  154.      * @since 3.6
  155.      */
  156.     public InitCommand setGitDir(File gitDir)
  157.             throws IllegalStateException {
  158.         validateDirs(directory, gitDir, bare);
  159.         this.gitDir = gitDir;
  160.         return this;
  161.     }

  162.     private static void validateDirs(File directory, File gitDir, boolean bare)
  163.             throws IllegalStateException {
  164.         if (directory != null) {
  165.             if (bare) {
  166.                 if (gitDir != null && !gitDir.equals(directory))
  167.                     throw new IllegalStateException(MessageFormat.format(
  168.                             JGitText.get().initFailedBareRepoDifferentDirs,
  169.                             gitDir, directory));
  170.             } else {
  171.                 if (gitDir != null && gitDir.equals(directory))
  172.                     throw new IllegalStateException(MessageFormat.format(
  173.                             JGitText.get().initFailedNonBareRepoSameDirs,
  174.                             gitDir, directory));
  175.             }
  176.         }
  177.     }

  178.     /**
  179.      * Set whether the repository is bare or not
  180.      *
  181.      * @param bare
  182.      *            whether the repository is bare or not
  183.      * @throws java.lang.IllegalStateException
  184.      *             if the combination of directory, gitDir and bare is illegal.
  185.      *             E.g. if for a non-bare repository directory and gitDir point
  186.      *             to the same directory of if for a bare repository both
  187.      *             directory and gitDir are specified
  188.      * @return this instance
  189.      */
  190.     public InitCommand setBare(boolean bare) {
  191.         validateDirs(directory, gitDir, bare);
  192.         this.bare = bare;
  193.         return this;
  194.     }

  195.     /**
  196.      * Set the file system abstraction to be used for repositories created by
  197.      * this command.
  198.      *
  199.      * @param fs
  200.      *            the abstraction.
  201.      * @return {@code this} (for chaining calls).
  202.      * @since 4.10
  203.      */
  204.     public InitCommand setFs(FS fs) {
  205.         this.fs = fs;
  206.         return this;
  207.     }
  208. }