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.

SubmoduleUpdateCommand.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  3. * Copyright (C) 2016, Laurent Delaigue <laurent.delaigue@obeo.fr>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.List;
  50. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  51. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.InvalidConfigurationException;
  54. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.api.errors.NoHeadException;
  57. import org.eclipse.jgit.api.errors.NoMessageException;
  58. import org.eclipse.jgit.api.errors.RefNotFoundException;
  59. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  60. import org.eclipse.jgit.dircache.DirCacheCheckout;
  61. import org.eclipse.jgit.errors.ConfigInvalidException;
  62. import org.eclipse.jgit.lib.ConfigConstants;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.NullProgressMonitor;
  65. import org.eclipse.jgit.lib.ProgressMonitor;
  66. import org.eclipse.jgit.lib.RefUpdate;
  67. import org.eclipse.jgit.lib.Repository;
  68. import org.eclipse.jgit.merge.MergeStrategy;
  69. import org.eclipse.jgit.revwalk.RevCommit;
  70. import org.eclipse.jgit.revwalk.RevWalk;
  71. import org.eclipse.jgit.submodule.SubmoduleWalk;
  72. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  73. /**
  74. * A class used to execute a submodule update command.
  75. *
  76. * @see <a
  77. * href="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
  78. * >Git documentation about submodules</a>
  79. */
  80. public class SubmoduleUpdateCommand extends
  81. TransportCommand<SubmoduleUpdateCommand, Collection<String>> {
  82. private ProgressMonitor monitor;
  83. private final Collection<String> paths;
  84. private MergeStrategy strategy = MergeStrategy.RECURSIVE;
  85. private CloneCommand.Callback callback;
  86. private FetchCommand.Callback fetchCallback;
  87. private boolean fetch = false;
  88. /**
  89. * <p>
  90. * Constructor for SubmoduleUpdateCommand.
  91. * </p>
  92. *
  93. * @param repo
  94. * a {@link org.eclipse.jgit.lib.Repository} object.
  95. */
  96. public SubmoduleUpdateCommand(final Repository repo) {
  97. super(repo);
  98. paths = new ArrayList<>();
  99. }
  100. /**
  101. * The progress monitor associated with the clone operation. By default,
  102. * this is set to <code>NullProgressMonitor</code>
  103. *
  104. * @see NullProgressMonitor
  105. * @param monitor
  106. * a {@link org.eclipse.jgit.lib.ProgressMonitor} object.
  107. * @return this command
  108. */
  109. public SubmoduleUpdateCommand setProgressMonitor(
  110. final ProgressMonitor monitor) {
  111. this.monitor = monitor;
  112. return this;
  113. }
  114. /**
  115. * Whether to fetch the submodules before we update them. By default, this
  116. * is set to <code>false</code>
  117. *
  118. * @param fetch
  119. * whether to fetch the submodules before we update them
  120. * @return this command
  121. * @since 4.9
  122. */
  123. public SubmoduleUpdateCommand setFetch(final boolean fetch) {
  124. this.fetch = fetch;
  125. return this;
  126. }
  127. /**
  128. * Add repository-relative submodule path to initialize
  129. *
  130. * @param path
  131. * (with <code>/</code> as separator)
  132. * @return this command
  133. */
  134. public SubmoduleUpdateCommand addPath(final String path) {
  135. paths.add(path);
  136. return this;
  137. }
  138. /**
  139. * {@inheritDoc}
  140. *
  141. * Execute the SubmoduleUpdateCommand command.
  142. */
  143. @Override
  144. public Collection<String> call() throws InvalidConfigurationException,
  145. NoHeadException, ConcurrentRefUpdateException,
  146. CheckoutConflictException, InvalidMergeHeadsException,
  147. WrongRepositoryStateException, NoMessageException, NoHeadException,
  148. RefNotFoundException, GitAPIException {
  149. checkCallable();
  150. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
  151. if (!paths.isEmpty())
  152. generator.setFilter(PathFilterGroup.createFromStrings(paths));
  153. List<String> updated = new ArrayList<>();
  154. while (generator.next()) {
  155. // Skip submodules not registered in .gitmodules file
  156. if (generator.getModulesPath() == null)
  157. continue;
  158. // Skip submodules not registered in parent repository's config
  159. String url = generator.getConfigUrl();
  160. if (url == null)
  161. continue;
  162. Repository submoduleRepo = generator.getRepository();
  163. // Clone repository if not present
  164. if (submoduleRepo == null) {
  165. if (callback != null) {
  166. callback.cloningSubmodule(generator.getPath());
  167. }
  168. CloneCommand clone = Git.cloneRepository();
  169. configure(clone);
  170. clone.setURI(url);
  171. clone.setDirectory(generator.getDirectory());
  172. clone.setGitDir(new File(new File(repo.getDirectory(),
  173. Constants.MODULES), generator.getPath()));
  174. if (monitor != null)
  175. clone.setProgressMonitor(monitor);
  176. submoduleRepo = clone.call().getRepository();
  177. } else if (this.fetch) {
  178. if (fetchCallback != null) {
  179. fetchCallback.fetchingSubmodule(generator.getPath());
  180. }
  181. FetchCommand fetchCommand = Git.wrap(submoduleRepo).fetch();
  182. if (monitor != null) {
  183. fetchCommand.setProgressMonitor(monitor);
  184. }
  185. configure(fetchCommand);
  186. fetchCommand.call();
  187. }
  188. try (RevWalk walk = new RevWalk(submoduleRepo)) {
  189. RevCommit commit = walk
  190. .parseCommit(generator.getObjectId());
  191. String update = generator.getConfigUpdate();
  192. if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
  193. MergeCommand merge = new MergeCommand(submoduleRepo);
  194. merge.include(commit);
  195. merge.setProgressMonitor(monitor);
  196. merge.setStrategy(strategy);
  197. merge.call();
  198. } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
  199. RebaseCommand rebase = new RebaseCommand(submoduleRepo);
  200. rebase.setUpstream(commit);
  201. rebase.setProgressMonitor(monitor);
  202. rebase.setStrategy(strategy);
  203. rebase.call();
  204. } else {
  205. // Checkout commit referenced in parent repository's
  206. // index as a detached HEAD
  207. DirCacheCheckout co = new DirCacheCheckout(
  208. submoduleRepo, submoduleRepo.lockDirCache(),
  209. commit.getTree());
  210. co.setFailOnConflict(true);
  211. co.setProgressMonitor(monitor);
  212. co.checkout();
  213. RefUpdate refUpdate = submoduleRepo.updateRef(
  214. Constants.HEAD, true);
  215. refUpdate.setNewObjectId(commit);
  216. refUpdate.forceUpdate();
  217. if (callback != null) {
  218. callback.checkingOut(commit,
  219. generator.getPath());
  220. }
  221. }
  222. } finally {
  223. submoduleRepo.close();
  224. }
  225. updated.add(generator.getPath());
  226. }
  227. return updated;
  228. } catch (IOException e) {
  229. throw new JGitInternalException(e.getMessage(), e);
  230. } catch (ConfigInvalidException e) {
  231. throw new InvalidConfigurationException(e.getMessage(), e);
  232. }
  233. }
  234. /**
  235. * Setter for the field <code>strategy</code>.
  236. *
  237. * @param strategy
  238. * The merge strategy to use during this update operation.
  239. * @return {@code this}
  240. * @since 3.4
  241. */
  242. public SubmoduleUpdateCommand setStrategy(MergeStrategy strategy) {
  243. this.strategy = strategy;
  244. return this;
  245. }
  246. /**
  247. * Set status callback for submodule clone operation.
  248. *
  249. * @param callback
  250. * the callback
  251. * @return {@code this}
  252. * @since 4.8
  253. */
  254. public SubmoduleUpdateCommand setCallback(CloneCommand.Callback callback) {
  255. this.callback = callback;
  256. return this;
  257. }
  258. /**
  259. * Set status callback for submodule fetch operation.
  260. *
  261. * @param callback
  262. * the callback
  263. * @return {@code this}
  264. * @since 4.9
  265. */
  266. public SubmoduleUpdateCommand setFetchCallback(
  267. FetchCommand.Callback callback) {
  268. this.fetchCallback = callback;
  269. return this;
  270. }
  271. }