Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

SubmoduleAddCommand.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 name;
  77. private String path;
  78. private String uri;
  79. private ProgressMonitor monitor;
  80. /**
  81. * Constructor for SubmoduleAddCommand.
  82. *
  83. * @param repo
  84. * a {@link org.eclipse.jgit.lib.Repository} object.
  85. */
  86. public SubmoduleAddCommand(Repository repo) {
  87. super(repo);
  88. }
  89. /**
  90. * Set the submodule name
  91. *
  92. * @param name
  93. * @return this command
  94. * @since 5.1
  95. */
  96. public SubmoduleAddCommand setName(String name) {
  97. this.name = name;
  98. return this;
  99. }
  100. /**
  101. * Set repository-relative path of submodule
  102. *
  103. * @param path
  104. * (with <code>/</code> as separator)
  105. * @return this command
  106. */
  107. public SubmoduleAddCommand setPath(String path) {
  108. this.path = path;
  109. return this;
  110. }
  111. /**
  112. * Set URI to clone submodule from
  113. *
  114. * @param uri
  115. * a {@link java.lang.String} object.
  116. * @return this command
  117. */
  118. public SubmoduleAddCommand setURI(String uri) {
  119. this.uri = uri;
  120. return this;
  121. }
  122. /**
  123. * The progress monitor associated with the clone operation. By default,
  124. * this is set to <code>NullProgressMonitor</code>
  125. *
  126. * @see NullProgressMonitor
  127. * @param monitor
  128. * a {@link org.eclipse.jgit.lib.ProgressMonitor} object.
  129. * @return this command
  130. */
  131. public SubmoduleAddCommand setProgressMonitor(ProgressMonitor monitor) {
  132. this.monitor = monitor;
  133. return this;
  134. }
  135. /**
  136. * Is the configured already a submodule in the index?
  137. *
  138. * @return true if submodule exists in index, false otherwise
  139. * @throws java.io.IOException
  140. */
  141. protected boolean submoduleExists() throws IOException {
  142. TreeFilter filter = PathFilter.create(path);
  143. try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) {
  144. return w.setFilter(filter).next();
  145. }
  146. }
  147. /**
  148. * {@inheritDoc}
  149. * <p>
  150. * Executes the {@code SubmoduleAddCommand}
  151. *
  152. * The {@code Repository} instance returned by this command needs to be
  153. * closed by the caller to free resources held by the {@code Repository}
  154. * instance. It is recommended to call this method as soon as you don't need
  155. * a reference to this {@code Repository} instance anymore.
  156. */
  157. @Override
  158. public Repository call() throws GitAPIException {
  159. checkCallable();
  160. if (path == null || path.length() == 0)
  161. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  162. if (uri == null || uri.length() == 0)
  163. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  164. if (name == null || name.length() == 0) {
  165. // Use the path as the default.
  166. name = path;
  167. }
  168. if (name.contains("/../") || name.contains("\\..\\") //$NON-NLS-1$ //$NON-NLS-2$
  169. || name.startsWith("../") || name.startsWith("..\\") //$NON-NLS-1$ //$NON-NLS-2$
  170. || name.endsWith("/..") || name.endsWith("\\..")) { //$NON-NLS-1$ //$NON-NLS-2$
  171. // Submodule names are used to store the submodule repositories
  172. // under $GIT_DIR/modules. Having ".." in submodule names makes a
  173. // vulnerability (CVE-2018-11235
  174. // https://bugs.eclipse.org/bugs/show_bug.cgi?id=535027#c0)
  175. // Reject the names with them. The callers need to make sure the
  176. // names free from these. We don't automatically replace these
  177. // characters or canonicalize by regarding the name as a file path.
  178. // Since Path class is platform dependent, we manually check '/' and
  179. // '\\' patterns here.
  180. throw new IllegalArgumentException(MessageFormat
  181. .format(JGitText.get().invalidNameContainsDotDot, name));
  182. }
  183. try {
  184. SubmoduleValidator.assertValidSubmoduleName(name);
  185. SubmoduleValidator.assertValidSubmodulePath(path);
  186. SubmoduleValidator.assertValidSubmoduleUri(uri);
  187. } catch (SubmoduleValidator.SubmoduleValidationException e) {
  188. throw new IllegalArgumentException(e.getMessage());
  189. }
  190. try {
  191. if (submoduleExists())
  192. throw new JGitInternalException(MessageFormat.format(
  193. JGitText.get().submoduleExists, path));
  194. } catch (IOException e) {
  195. throw new JGitInternalException(e.getMessage(), e);
  196. }
  197. final String resolvedUri;
  198. try {
  199. resolvedUri = SubmoduleWalk.getSubmoduleRemoteUrl(repo, uri);
  200. } catch (IOException e) {
  201. throw new JGitInternalException(e.getMessage(), e);
  202. }
  203. // Clone submodule repository
  204. File moduleDirectory = SubmoduleWalk.getSubmoduleDirectory(repo, path);
  205. CloneCommand clone = Git.cloneRepository();
  206. configure(clone);
  207. clone.setDirectory(moduleDirectory);
  208. clone.setGitDir(new File(new File(repo.getDirectory(),
  209. Constants.MODULES), path));
  210. clone.setURI(resolvedUri);
  211. if (monitor != null)
  212. clone.setProgressMonitor(monitor);
  213. Repository subRepo = null;
  214. try (Git git = clone.call()) {
  215. subRepo = git.getRepository();
  216. subRepo.incrementOpen();
  217. }
  218. // Save submodule URL to parent repository's config
  219. StoredConfig config = repo.getConfig();
  220. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, name,
  221. ConfigConstants.CONFIG_KEY_URL, resolvedUri);
  222. try {
  223. config.save();
  224. } catch (IOException e) {
  225. throw new JGitInternalException(e.getMessage(), e);
  226. }
  227. // Save path and URL to parent repository's .gitmodules file
  228. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  229. repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS());
  230. try {
  231. modulesConfig.load();
  232. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  233. name, ConfigConstants.CONFIG_KEY_PATH, path);
  234. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  235. name, ConfigConstants.CONFIG_KEY_URL, uri);
  236. modulesConfig.save();
  237. } catch (IOException e) {
  238. throw new JGitInternalException(e.getMessage(), e);
  239. } catch (ConfigInvalidException e) {
  240. throw new JGitInternalException(e.getMessage(), e);
  241. }
  242. AddCommand add = new AddCommand(repo);
  243. // Add .gitmodules file to parent repository's index
  244. add.addFilepattern(Constants.DOT_GIT_MODULES);
  245. // Add submodule directory to parent repository's index
  246. add.addFilepattern(path);
  247. try {
  248. add.call();
  249. } catch (NoFilepatternException e) {
  250. throw new JGitInternalException(e.getMessage(), e);
  251. }
  252. return subRepo;
  253. }
  254. }