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 5.4KB

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