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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.lib.ConfigConstants;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.NullProgressMonitor;
  55. import org.eclipse.jgit.lib.ProgressMonitor;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.lib.StoredConfig;
  58. import org.eclipse.jgit.storage.file.FileBasedConfig;
  59. import org.eclipse.jgit.submodule.SubmoduleWalk;
  60. import org.eclipse.jgit.treewalk.filter.PathFilter;
  61. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  62. /**
  63. * A class used to execute a submodule add command.
  64. *
  65. * This will clone the configured submodule, register the submodule in the
  66. * .gitmodules file and the repository config file, and also add the submodule
  67. * and .gitmodules file to the index.
  68. *
  69. * @see <a
  70. * href="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
  71. * >Git documentation about submodules</a>
  72. */
  73. public class SubmoduleAddCommand extends
  74. TransportCommand<SubmoduleAddCommand, Repository> {
  75. private String path;
  76. private String uri;
  77. private ProgressMonitor monitor;
  78. /**
  79. * @param repo
  80. */
  81. public SubmoduleAddCommand(final Repository repo) {
  82. super(repo);
  83. }
  84. /**
  85. * Set repository-relative path of submodule
  86. *
  87. * @param path
  88. * @return this command
  89. */
  90. public SubmoduleAddCommand setPath(final String path) {
  91. this.path = path;
  92. return this;
  93. }
  94. /**
  95. * Set URI to clone submodule from
  96. *
  97. * @param uri
  98. * @return this command
  99. */
  100. public SubmoduleAddCommand setURI(final String uri) {
  101. this.uri = uri;
  102. return this;
  103. }
  104. /**
  105. * The progress monitor associated with the clone operation. By default,
  106. * this is set to <code>NullProgressMonitor</code>
  107. *
  108. * @see NullProgressMonitor
  109. * @param monitor
  110. * @return this command
  111. */
  112. public SubmoduleAddCommand setProgressMonitor(final ProgressMonitor monitor) {
  113. this.monitor = monitor;
  114. return this;
  115. }
  116. /**
  117. * Is the configured already a submodule in the index?
  118. *
  119. * @return true if submodule exists in index, false otherwise
  120. * @throws IOException
  121. */
  122. protected boolean submoduleExists() throws IOException {
  123. TreeFilter filter = PathFilter.create(path);
  124. return SubmoduleWalk.forIndex(repo).setFilter(filter).next();
  125. }
  126. public Repository call() throws GitAPIException {
  127. checkCallable();
  128. if (path == null || path.length() == 0)
  129. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  130. if (uri == null || uri.length() == 0)
  131. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  132. try {
  133. if (submoduleExists())
  134. throw new JGitInternalException(MessageFormat.format(
  135. JGitText.get().submoduleExists, path));
  136. } catch (IOException e) {
  137. throw new JGitInternalException(e.getMessage(), e);
  138. }
  139. final String resolvedUri;
  140. try {
  141. resolvedUri = SubmoduleWalk.getSubmoduleRemoteUrl(repo, uri);
  142. } catch (IOException e) {
  143. throw new JGitInternalException(e.getMessage(), e);
  144. }
  145. // Clone submodule repository
  146. File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
  147. CloneCommand clone = Git.cloneRepository();
  148. configure(clone);
  149. clone.setDirectory(moduleDirectory);
  150. clone.setURI(resolvedUri);
  151. if (monitor != null)
  152. clone.setProgressMonitor(monitor);
  153. Repository subRepo = clone.call().getRepository();
  154. // Save submodule URL to parent repository's config
  155. StoredConfig config = repo.getConfig();
  156. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  157. ConfigConstants.CONFIG_KEY_URL, resolvedUri);
  158. try {
  159. config.save();
  160. } catch (IOException e) {
  161. throw new JGitInternalException(e.getMessage(), e);
  162. }
  163. // Save path and URL to parent repository's .gitmodules file
  164. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  165. repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
  166. try {
  167. modulesConfig.load();
  168. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  169. path, ConfigConstants.CONFIG_KEY_PATH, path);
  170. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  171. path, ConfigConstants.CONFIG_KEY_URL, uri);
  172. modulesConfig.save();
  173. } catch (IOException e) {
  174. throw new JGitInternalException(e.getMessage(), e);
  175. } catch (ConfigInvalidException e) {
  176. throw new JGitInternalException(e.getMessage(), e);
  177. }
  178. AddCommand add = new AddCommand(repo);
  179. // Add .gitmodules file to parent repository's index
  180. add.addFilepattern(Constants.DOT_GIT_MODULES);
  181. // Add submodule directory to parent repository's index
  182. add.addFilepattern(path);
  183. try {
  184. add.call();
  185. } catch (NoFilepatternException e) {
  186. throw new JGitInternalException(e.getMessage(), e);
  187. }
  188. return subRepo;
  189. }
  190. }