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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.internal.JGitText;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.lib.RepositoryBuilder;
  22. import org.eclipse.jgit.util.FS;
  23. import org.eclipse.jgit.util.SystemReader;
  24. /**
  25. * Create an empty git repository or reinitalize an existing one
  26. *
  27. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  28. * >Git documentation about init</a>
  29. */
  30. public class InitCommand implements Callable<Git> {
  31. private File directory;
  32. private File gitDir;
  33. private boolean bare;
  34. private FS fs;
  35. private String initialBranch = Constants.MASTER;
  36. /**
  37. * {@inheritDoc}
  38. * <p>
  39. * Executes the {@code Init} command.
  40. *
  41. * @return a {@code Git} instance that owns the {@code Repository} that it
  42. * wraps.
  43. */
  44. @Override
  45. public Git call() throws GitAPIException {
  46. try {
  47. RepositoryBuilder builder = new RepositoryBuilder();
  48. if (bare)
  49. builder.setBare();
  50. if (fs != null) {
  51. builder.setFS(fs);
  52. }
  53. builder.readEnvironment();
  54. if (gitDir != null)
  55. builder.setGitDir(gitDir);
  56. else
  57. gitDir = builder.getGitDir();
  58. if (directory != null) {
  59. if (bare)
  60. builder.setGitDir(directory);
  61. else {
  62. builder.setWorkTree(directory);
  63. if (gitDir == null)
  64. builder.setGitDir(new File(directory, Constants.DOT_GIT));
  65. }
  66. } else if (builder.getGitDir() == null) {
  67. String dStr = SystemReader.getInstance()
  68. .getProperty("user.dir"); //$NON-NLS-1$
  69. if (dStr == null)
  70. dStr = "."; //$NON-NLS-1$
  71. File d = new File(dStr);
  72. if (!bare)
  73. d = new File(d, Constants.DOT_GIT);
  74. builder.setGitDir(d);
  75. } else {
  76. // directory was not set but gitDir was set
  77. if (!bare) {
  78. String dStr = SystemReader.getInstance().getProperty(
  79. "user.dir"); //$NON-NLS-1$
  80. if (dStr == null)
  81. dStr = "."; //$NON-NLS-1$
  82. builder.setWorkTree(new File(dStr));
  83. }
  84. }
  85. builder.setInitialBranch(initialBranch);
  86. Repository repository = builder.build();
  87. if (!repository.getObjectDatabase().exists())
  88. repository.create(bare);
  89. return new Git(repository, true);
  90. } catch (IOException e) {
  91. throw new JGitInternalException(e.getMessage(), e);
  92. }
  93. }
  94. /**
  95. * The optional directory associated with the init operation. If no
  96. * directory is set, we'll use the current directory
  97. *
  98. * @param directory
  99. * the directory to init to
  100. * @return this instance
  101. * @throws java.lang.IllegalStateException
  102. * if the combination of directory, gitDir and bare is illegal.
  103. * E.g. if for a non-bare repository directory and gitDir point
  104. * to the same directory of if for a bare repository both
  105. * directory and gitDir are specified
  106. */
  107. public InitCommand setDirectory(File directory)
  108. throws IllegalStateException {
  109. validateDirs(directory, gitDir, bare);
  110. this.directory = directory;
  111. return this;
  112. }
  113. /**
  114. * Set the repository meta directory (.git)
  115. *
  116. * @param gitDir
  117. * the repository meta directory
  118. * @return this instance
  119. * @throws java.lang.IllegalStateException
  120. * if the combination of directory, gitDir and bare is illegal.
  121. * E.g. if for a non-bare repository directory and gitDir point
  122. * to the same directory of if for a bare repository both
  123. * directory and gitDir are specified
  124. * @since 3.6
  125. */
  126. public InitCommand setGitDir(File gitDir)
  127. throws IllegalStateException {
  128. validateDirs(directory, gitDir, bare);
  129. this.gitDir = gitDir;
  130. return this;
  131. }
  132. private static void validateDirs(File directory, File gitDir, boolean bare)
  133. throws IllegalStateException {
  134. if (directory != null) {
  135. if (bare) {
  136. if (gitDir != null && !gitDir.equals(directory))
  137. throw new IllegalStateException(MessageFormat.format(
  138. JGitText.get().initFailedBareRepoDifferentDirs,
  139. gitDir, directory));
  140. } else {
  141. if (gitDir != null && gitDir.equals(directory))
  142. throw new IllegalStateException(MessageFormat.format(
  143. JGitText.get().initFailedNonBareRepoSameDirs,
  144. gitDir, directory));
  145. }
  146. }
  147. }
  148. /**
  149. * Set whether the repository is bare or not
  150. *
  151. * @param bare
  152. * whether the repository is bare or not
  153. * @throws java.lang.IllegalStateException
  154. * if the combination of directory, gitDir and bare is illegal.
  155. * E.g. if for a non-bare repository directory and gitDir point
  156. * to the same directory of if for a bare repository both
  157. * directory and gitDir are specified
  158. * @return this instance
  159. */
  160. public InitCommand setBare(boolean bare) {
  161. validateDirs(directory, gitDir, bare);
  162. this.bare = bare;
  163. return this;
  164. }
  165. /**
  166. * Set the file system abstraction to be used for repositories created by
  167. * this command.
  168. *
  169. * @param fs
  170. * the abstraction.
  171. * @return {@code this} (for chaining calls).
  172. * @since 4.10
  173. */
  174. public InitCommand setFs(FS fs) {
  175. this.fs = fs;
  176. return this;
  177. }
  178. /**
  179. * Set the initial branch of the new repository. If not specified
  180. * ({@code null} or empty), fall back to the default name (currently
  181. * master).
  182. *
  183. * @param branch
  184. * initial branch name of the new repository
  185. * @return {@code this}
  186. * @throws InvalidRefNameException
  187. * if the branch name is not valid
  188. *
  189. * @since 5.11
  190. */
  191. public InitCommand setInitialBranch(String branch)
  192. throws InvalidRefNameException {
  193. this.initialBranch = branch;
  194. return this;
  195. }
  196. }