You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InitCommand.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.SystemReader;
  55. /**
  56. * Create an empty git repository or reinitalize an existing one
  57. *
  58. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  59. * >Git documentation about init</a>
  60. */
  61. public class InitCommand implements Callable<Git> {
  62. private File directory;
  63. private File gitDir;
  64. private boolean bare;
  65. /**
  66. * Executes the {@code Init} command.
  67. *
  68. * @return the newly created {@code Git} object with associated repository
  69. */
  70. public Git call() throws GitAPIException {
  71. try {
  72. RepositoryBuilder builder = new RepositoryBuilder();
  73. if (bare)
  74. builder.setBare();
  75. builder.readEnvironment();
  76. if (gitDir != null)
  77. builder.setGitDir(gitDir);
  78. else
  79. gitDir = builder.getGitDir();
  80. if (directory != null) {
  81. if (bare)
  82. builder.setGitDir(directory);
  83. else {
  84. builder.setWorkTree(directory);
  85. if (gitDir == null)
  86. builder.setGitDir(new File(directory, Constants.DOT_GIT));
  87. }
  88. } else if (builder.getGitDir() == null) {
  89. String dStr = SystemReader.getInstance()
  90. .getProperty("user.dir"); //$NON-NLS-1$
  91. if (dStr == null)
  92. dStr = "."; //$NON-NLS-1$
  93. File d = new File(dStr);
  94. if (!bare)
  95. d = new File(d, Constants.DOT_GIT);
  96. builder.setGitDir(d);
  97. } else {
  98. // directory was not set but gitDir was set
  99. if (!bare) {
  100. String dStr = SystemReader.getInstance().getProperty(
  101. "user.dir"); //$NON-NLS-1$
  102. if (dStr == null)
  103. dStr = "."; //$NON-NLS-1$
  104. builder.setWorkTree(new File(dStr));
  105. }
  106. }
  107. Repository repository = builder.build();
  108. if (!repository.getObjectDatabase().exists())
  109. repository.create(bare);
  110. return new Git(repository);
  111. } catch (IOException e) {
  112. throw new JGitInternalException(e.getMessage(), e);
  113. }
  114. }
  115. /**
  116. * The optional directory associated with the init operation. If no
  117. * directory is set, we'll use the current directory
  118. *
  119. * @param directory
  120. * the directory to init to
  121. * @return this instance
  122. * @throws IllegalStateException
  123. * if the combination of directory, gitDir and bare is illegal.
  124. * E.g. if for a non-bare repository directory and gitDir point
  125. * to the same directory of if for a bare repository both
  126. * directory and gitDir are specified
  127. */
  128. public InitCommand setDirectory(File directory)
  129. throws IllegalStateException {
  130. validateDirs(directory, gitDir, bare);
  131. this.directory = directory;
  132. return this;
  133. }
  134. /**
  135. * @param gitDir
  136. * the repository meta directory
  137. * @return this instance
  138. * @throws IllegalStateException
  139. * if the combination of directory, gitDir and bare is illegal.
  140. * E.g. if for a non-bare repository directory and gitDir point
  141. * to the same directory of if for a bare repository both
  142. * directory and gitDir are specified
  143. * @since 3.6
  144. */
  145. public InitCommand setGitDir(File gitDir)
  146. throws IllegalStateException {
  147. validateDirs(directory, gitDir, bare);
  148. this.gitDir = gitDir;
  149. return this;
  150. }
  151. private static void validateDirs(File directory, File gitDir, boolean bare)
  152. throws IllegalStateException {
  153. if (directory != null) {
  154. if (bare) {
  155. if (gitDir != null && !gitDir.equals(directory))
  156. throw new IllegalStateException(MessageFormat.format(
  157. JGitText.get().initFailedBareRepoDifferentDirs,
  158. gitDir, directory));
  159. } else {
  160. if (gitDir != null && gitDir.equals(directory))
  161. throw new IllegalStateException(MessageFormat.format(
  162. JGitText.get().initFailedNonBareRepoSameDirs,
  163. gitDir, directory));
  164. }
  165. }
  166. }
  167. /**
  168. * @param bare
  169. * whether the repository is bare or not
  170. * @throws IllegalStateException
  171. * if the combination of directory, gitDir and bare is illegal.
  172. * E.g. if for a non-bare repository directory and gitDir point
  173. * to the same directory of if for a bare repository both
  174. * directory and gitDir are specified
  175. * @return this instance
  176. */
  177. public InitCommand setBare(boolean bare) {
  178. validateDirs(directory, gitDir, bare);
  179. this.bare = bare;
  180. return this;
  181. }
  182. }