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.

SubmoduleAddCommand.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  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 org.eclipse.jgit.api.errors.GitAPIException;
  48. import org.eclipse.jgit.api.errors.JGitInternalException;
  49. import org.eclipse.jgit.api.errors.NoFilepatternException;
  50. import org.eclipse.jgit.errors.ConfigInvalidException;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.internal.submodule.SubmoduleValidator;
  53. import org.eclipse.jgit.lib.ConfigConstants;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.NullProgressMonitor;
  56. import org.eclipse.jgit.lib.ProgressMonitor;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.lib.StoredConfig;
  59. import org.eclipse.jgit.storage.file.FileBasedConfig;
  60. import org.eclipse.jgit.submodule.SubmoduleWalk;
  61. import org.eclipse.jgit.treewalk.filter.PathFilter;
  62. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  63. /**
  64. * A class used to execute a submodule add command.
  65. *
  66. * This will clone the configured submodule, register the submodule in the
  67. * .gitmodules file and the repository config file, and also add the submodule
  68. * and .gitmodules file to the index.
  69. *
  70. * @see <a
  71. * href="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
  72. * >Git documentation about submodules</a>
  73. */
  74. public class SubmoduleAddCommand extends
  75. TransportCommand<SubmoduleAddCommand, Repository> {
  76. private String path;
  77. private String uri;
  78. private ProgressMonitor monitor;
  79. /**
  80. * @param repo
  81. */
  82. public SubmoduleAddCommand(final Repository repo) {
  83. super(repo);
  84. }
  85. /**
  86. * Set repository-relative path of submodule
  87. *
  88. * @param path
  89. * (with <code>/</code> as separator)
  90. * @return this command
  91. */
  92. public SubmoduleAddCommand setPath(final String path) {
  93. this.path = path;
  94. return this;
  95. }
  96. /**
  97. * Set URI to clone submodule from
  98. *
  99. * @param uri
  100. * @return this command
  101. */
  102. public SubmoduleAddCommand setURI(final String uri) {
  103. this.uri = uri;
  104. return this;
  105. }
  106. /**
  107. * The progress monitor associated with the clone operation. By default,
  108. * this is set to <code>NullProgressMonitor</code>
  109. *
  110. * @see NullProgressMonitor
  111. * @param monitor
  112. * @return this command
  113. */
  114. public SubmoduleAddCommand setProgressMonitor(final ProgressMonitor monitor) {
  115. this.monitor = monitor;
  116. return this;
  117. }
  118. /**
  119. * Is the configured already a submodule in the index?
  120. *
  121. * @return true if submodule exists in index, false otherwise
  122. * @throws IOException
  123. */
  124. protected boolean submoduleExists() throws IOException {
  125. TreeFilter filter = PathFilter.create(path);
  126. try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) {
  127. return w.setFilter(filter).next();
  128. }
  129. }
  130. /**
  131. * Executes the {@code SubmoduleAddCommand}
  132. *
  133. * The {@code Repository} instance returned by this command needs to be
  134. * closed by the caller to free resources held by the {@code Repository}
  135. * instance. It is recommended to call this method as soon as you don't need
  136. * a reference to this {@code Repository} instance anymore.
  137. *
  138. * @return the newly created {@link Repository}
  139. * @throws GitAPIException
  140. */
  141. @Override
  142. public Repository call() throws GitAPIException {
  143. checkCallable();
  144. if (path == null || path.length() == 0)
  145. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  146. if (uri == null || uri.length() == 0)
  147. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  148. try {
  149. SubmoduleValidator.assertValidSubmoduleName(path);
  150. SubmoduleValidator.assertValidSubmodulePath(path);
  151. SubmoduleValidator.assertValidSubmoduleUri(uri);
  152. } catch (SubmoduleValidator.SubmoduleValidationException e) {
  153. throw new IllegalArgumentException(e.getMessage());
  154. }
  155. try {
  156. if (submoduleExists())
  157. throw new JGitInternalException(MessageFormat.format(
  158. JGitText.get().submoduleExists, path));
  159. } catch (IOException e) {
  160. throw new JGitInternalException(e.getMessage(), e);
  161. }
  162. final String resolvedUri;
  163. try {
  164. resolvedUri = SubmoduleWalk.getSubmoduleRemoteUrl(repo, uri);
  165. } catch (IOException e) {
  166. throw new JGitInternalException(e.getMessage(), e);
  167. }
  168. // Clone submodule repository
  169. File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
  170. CloneCommand clone = Git.cloneRepository();
  171. configure(clone);
  172. clone.setDirectory(moduleDirectory);
  173. clone.setGitDir(new File(new File(repo.getDirectory(),
  174. Constants.MODULES), path));
  175. clone.setURI(resolvedUri);
  176. if (monitor != null)
  177. clone.setProgressMonitor(monitor);
  178. Repository subRepo = null;
  179. try (Git git = clone.call()) {
  180. subRepo = git.getRepository();
  181. subRepo.incrementOpen();
  182. }
  183. // Save submodule URL to parent repository's config
  184. StoredConfig config = repo.getConfig();
  185. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  186. ConfigConstants.CONFIG_KEY_URL, resolvedUri);
  187. try {
  188. config.save();
  189. } catch (IOException e) {
  190. throw new JGitInternalException(e.getMessage(), e);
  191. }
  192. // Save path and URL to parent repository's .gitmodules file
  193. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  194. repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
  195. try {
  196. modulesConfig.load();
  197. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  198. path, ConfigConstants.CONFIG_KEY_PATH, path);
  199. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  200. path, ConfigConstants.CONFIG_KEY_URL, uri);
  201. modulesConfig.save();
  202. } catch (IOException e) {
  203. throw new JGitInternalException(e.getMessage(), e);
  204. } catch (ConfigInvalidException e) {
  205. throw new JGitInternalException(e.getMessage(), e);
  206. }
  207. AddCommand add = new AddCommand(repo);
  208. // Add .gitmodules file to parent repository's index
  209. add.addFilepattern(Constants.DOT_GIT_MODULES);
  210. // Add submodule directory to parent repository's index
  211. add.addFilepattern(path);
  212. try {
  213. add.call();
  214. } catch (NoFilepatternException e) {
  215. throw new JGitInternalException(e.getMessage(), e);
  216. }
  217. return subRepo;
  218. }
  219. }