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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * (with <code>/</code> as separator)
  89. * @return this command
  90. */
  91. public SubmoduleAddCommand setPath(final String path) {
  92. this.path = path;
  93. return this;
  94. }
  95. /**
  96. * Set URI to clone submodule from
  97. *
  98. * @param uri
  99. * @return this command
  100. */
  101. public SubmoduleAddCommand setURI(final String uri) {
  102. this.uri = uri;
  103. return this;
  104. }
  105. /**
  106. * The progress monitor associated with the clone operation. By default,
  107. * this is set to <code>NullProgressMonitor</code>
  108. *
  109. * @see NullProgressMonitor
  110. * @param monitor
  111. * @return this command
  112. */
  113. public SubmoduleAddCommand setProgressMonitor(final ProgressMonitor monitor) {
  114. this.monitor = monitor;
  115. return this;
  116. }
  117. /**
  118. * Is the configured already a submodule in the index?
  119. *
  120. * @return true if submodule exists in index, false otherwise
  121. * @throws IOException
  122. */
  123. protected boolean submoduleExists() throws IOException {
  124. TreeFilter filter = PathFilter.create(path);
  125. return SubmoduleWalk.forIndex(repo).setFilter(filter).next();
  126. }
  127. public Repository call() throws GitAPIException {
  128. checkCallable();
  129. if (path == null || path.length() == 0)
  130. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  131. if (uri == null || uri.length() == 0)
  132. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  133. try {
  134. if (submoduleExists())
  135. throw new JGitInternalException(MessageFormat.format(
  136. JGitText.get().submoduleExists, path));
  137. } catch (IOException e) {
  138. throw new JGitInternalException(e.getMessage(), e);
  139. }
  140. final String resolvedUri;
  141. try {
  142. resolvedUri = SubmoduleWalk.getSubmoduleRemoteUrl(repo, uri);
  143. } catch (IOException e) {
  144. throw new JGitInternalException(e.getMessage(), e);
  145. }
  146. // Clone submodule repository
  147. File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
  148. CloneCommand clone = Git.cloneRepository();
  149. configure(clone);
  150. clone.setDirectory(moduleDirectory);
  151. clone.setURI(resolvedUri);
  152. if (monitor != null)
  153. clone.setProgressMonitor(monitor);
  154. Repository subRepo = clone.call().getRepository();
  155. // Save submodule URL to parent repository's config
  156. StoredConfig config = repo.getConfig();
  157. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  158. ConfigConstants.CONFIG_KEY_URL, resolvedUri);
  159. try {
  160. config.save();
  161. } catch (IOException e) {
  162. throw new JGitInternalException(e.getMessage(), e);
  163. }
  164. // Save path and URL to parent repository's .gitmodules file
  165. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  166. repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
  167. try {
  168. modulesConfig.load();
  169. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  170. path, ConfigConstants.CONFIG_KEY_PATH, path);
  171. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  172. path, ConfigConstants.CONFIG_KEY_URL, uri);
  173. modulesConfig.save();
  174. } catch (IOException e) {
  175. throw new JGitInternalException(e.getMessage(), e);
  176. } catch (ConfigInvalidException e) {
  177. throw new JGitInternalException(e.getMessage(), e);
  178. }
  179. AddCommand add = new AddCommand(repo);
  180. // Add .gitmodules file to parent repository's index
  181. add.addFilepattern(Constants.DOT_GIT_MODULES);
  182. // Add submodule directory to parent repository's index
  183. add.addFilepattern(path);
  184. try {
  185. add.call();
  186. } catch (NoFilepatternException e) {
  187. throw new JGitInternalException(e.getMessage(), e);
  188. }
  189. return subRepo;
  190. }
  191. }