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

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