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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 href=
  71. * "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. * Constructor for SubmoduleAddCommand.
  81. *
  82. * @param repo
  83. * a {@link org.eclipse.jgit.lib.Repository} object.
  84. */
  85. public SubmoduleAddCommand(Repository repo) {
  86. super(repo);
  87. }
  88. /**
  89. * Set repository-relative path of submodule
  90. *
  91. * @param path
  92. * (with <code>/</code> as separator)
  93. * @return this command
  94. */
  95. public SubmoduleAddCommand setPath(String path) {
  96. this.path = path;
  97. return this;
  98. }
  99. /**
  100. * Set URI to clone submodule from
  101. *
  102. * @param uri
  103. * a {@link java.lang.String} object.
  104. * @return this command
  105. */
  106. public SubmoduleAddCommand setURI(String uri) {
  107. this.uri = uri;
  108. return this;
  109. }
  110. /**
  111. * The progress monitor associated with the clone operation. By default,
  112. * this is set to <code>NullProgressMonitor</code>
  113. *
  114. * @see NullProgressMonitor
  115. * @param monitor
  116. * a {@link org.eclipse.jgit.lib.ProgressMonitor} object.
  117. * @return this command
  118. */
  119. public SubmoduleAddCommand setProgressMonitor(ProgressMonitor monitor) {
  120. this.monitor = monitor;
  121. return this;
  122. }
  123. /**
  124. * Is the configured already a submodule in the index?
  125. *
  126. * @return true if submodule exists in index, false otherwise
  127. * @throws java.io.IOException
  128. */
  129. protected boolean submoduleExists() throws IOException {
  130. TreeFilter filter = PathFilter.create(path);
  131. try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) {
  132. return w.setFilter(filter).next();
  133. }
  134. }
  135. /**
  136. * {@inheritDoc}
  137. * <p>
  138. * Executes the {@code SubmoduleAddCommand}
  139. *
  140. * The {@code Repository} instance returned by this command needs to be
  141. * closed by the caller to free resources held by the {@code Repository}
  142. * instance. It is recommended to call this method as soon as you don't need
  143. * a reference to this {@code Repository} instance anymore.
  144. */
  145. @Override
  146. public Repository call() throws GitAPIException {
  147. checkCallable();
  148. if (path == null || path.length() == 0)
  149. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  150. if (uri == null || uri.length() == 0)
  151. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  152. try {
  153. SubmoduleValidator.assertValidSubmoduleName(path);
  154. SubmoduleValidator.assertValidSubmodulePath(path);
  155. SubmoduleValidator.assertValidSubmoduleUri(uri);
  156. } catch (SubmoduleValidator.SubmoduleValidationException e) {
  157. throw new IllegalArgumentException(e.getMessage());
  158. }
  159. try {
  160. if (submoduleExists())
  161. throw new JGitInternalException(MessageFormat.format(
  162. JGitText.get().submoduleExists, path));
  163. } catch (IOException e) {
  164. throw new JGitInternalException(e.getMessage(), e);
  165. }
  166. final String resolvedUri;
  167. try {
  168. resolvedUri = SubmoduleWalk.getSubmoduleRemoteUrl(repo, uri);
  169. } catch (IOException e) {
  170. throw new JGitInternalException(e.getMessage(), e);
  171. }
  172. // Clone submodule repository
  173. File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
  174. CloneCommand clone = Git.cloneRepository();
  175. configure(clone);
  176. clone.setDirectory(moduleDirectory);
  177. clone.setGitDir(new File(new File(repo.getDirectory(),
  178. Constants.MODULES), path));
  179. clone.setURI(resolvedUri);
  180. if (monitor != null)
  181. clone.setProgressMonitor(monitor);
  182. Repository subRepo = null;
  183. try (Git git = clone.call()) {
  184. subRepo = git.getRepository();
  185. subRepo.incrementOpen();
  186. }
  187. // Save submodule URL to parent repository's config
  188. StoredConfig config = repo.getConfig();
  189. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  190. ConfigConstants.CONFIG_KEY_URL, resolvedUri);
  191. try {
  192. config.save();
  193. } catch (IOException e) {
  194. throw new JGitInternalException(e.getMessage(), e);
  195. }
  196. // Save path and URL to parent repository's .gitmodules file
  197. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  198. repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
  199. try {
  200. modulesConfig.load();
  201. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  202. path, ConfigConstants.CONFIG_KEY_PATH, path);
  203. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  204. path, ConfigConstants.CONFIG_KEY_URL, uri);
  205. modulesConfig.save();
  206. } catch (IOException e) {
  207. throw new JGitInternalException(e.getMessage(), e);
  208. } catch (ConfigInvalidException e) {
  209. throw new JGitInternalException(e.getMessage(), e);
  210. }
  211. AddCommand add = new AddCommand(repo);
  212. // Add .gitmodules file to parent repository's index
  213. add.addFilepattern(Constants.DOT_GIT_MODULES);
  214. // Add submodule directory to parent repository's index
  215. add.addFilepattern(path);
  216. try {
  217. add.call();
  218. } catch (NoFilepatternException e) {
  219. throw new JGitInternalException(e.getMessage(), e);
  220. }
  221. return subRepo;
  222. }
  223. }