Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SubmoduleUpdateCommand.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  86. * @param repo
  87. */
  88. public SubmoduleUpdateCommand(final Repository repo) {
  89. super(repo);
  90. paths = new ArrayList<String>();
  91. }
  92. /**
  93. * The progress monitor associated with the clone operation. By default,
  94. * this is set to <code>NullProgressMonitor</code>
  95. *
  96. * @see NullProgressMonitor
  97. * @param monitor
  98. * @return this command
  99. */
  100. public SubmoduleUpdateCommand setProgressMonitor(
  101. final ProgressMonitor monitor) {
  102. this.monitor = monitor;
  103. return this;
  104. }
  105. /**
  106. * Add repository-relative submodule path to initialize
  107. *
  108. * @param path
  109. * (with <code>/</code> as separator)
  110. * @return this command
  111. */
  112. public SubmoduleUpdateCommand addPath(final String path) {
  113. paths.add(path);
  114. return this;
  115. }
  116. /**
  117. * Execute the SubmoduleUpdateCommand command.
  118. *
  119. * @return a collection of updated submodule paths
  120. * @throws ConcurrentRefUpdateException
  121. * @throws CheckoutConflictException
  122. * @throws InvalidMergeHeadsException
  123. * @throws InvalidConfigurationException
  124. * @throws NoHeadException
  125. * @throws NoMessageException
  126. * @throws RefNotFoundException
  127. * @throws WrongRepositoryStateException
  128. * @throws GitAPIException
  129. */
  130. public Collection<String> call() throws InvalidConfigurationException,
  131. NoHeadException, ConcurrentRefUpdateException,
  132. CheckoutConflictException, InvalidMergeHeadsException,
  133. WrongRepositoryStateException, NoMessageException, NoHeadException,
  134. RefNotFoundException, GitAPIException {
  135. checkCallable();
  136. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
  137. if (!paths.isEmpty())
  138. generator.setFilter(PathFilterGroup.createFromStrings(paths));
  139. List<String> updated = new ArrayList<String>();
  140. while (generator.next()) {
  141. // Skip submodules not registered in .gitmodules file
  142. if (generator.getModulesPath() == null)
  143. continue;
  144. // Skip submodules not registered in parent repository's config
  145. String url = generator.getConfigUrl();
  146. if (url == null)
  147. continue;
  148. Repository submoduleRepo = generator.getRepository();
  149. // Clone repository is not present
  150. if (submoduleRepo == null) {
  151. CloneCommand clone = Git.cloneRepository();
  152. configure(clone);
  153. clone.setURI(url);
  154. clone.setDirectory(generator.getDirectory());
  155. clone.setGitDir(new File(new File(repo.getDirectory(),
  156. Constants.MODULES), generator.getPath()));
  157. if (monitor != null)
  158. clone.setProgressMonitor(monitor);
  159. submoduleRepo = clone.call().getRepository();
  160. }
  161. try (RevWalk walk = new RevWalk(submoduleRepo)) {
  162. RevCommit commit = walk
  163. .parseCommit(generator.getObjectId());
  164. String update = generator.getConfigUpdate();
  165. if (ConfigConstants.CONFIG_KEY_MERGE.equals(update)) {
  166. MergeCommand merge = new MergeCommand(submoduleRepo);
  167. merge.include(commit);
  168. merge.setProgressMonitor(monitor);
  169. merge.setStrategy(strategy);
  170. merge.call();
  171. } else if (ConfigConstants.CONFIG_KEY_REBASE.equals(update)) {
  172. RebaseCommand rebase = new RebaseCommand(submoduleRepo);
  173. rebase.setUpstream(commit);
  174. rebase.setProgressMonitor(monitor);
  175. rebase.setStrategy(strategy);
  176. rebase.call();
  177. } else {
  178. // Checkout commit referenced in parent repository's
  179. // index as a detached HEAD
  180. DirCacheCheckout co = new DirCacheCheckout(
  181. submoduleRepo, submoduleRepo.lockDirCache(),
  182. commit.getTree());
  183. co.setFailOnConflict(true);
  184. co.checkout();
  185. RefUpdate refUpdate = submoduleRepo.updateRef(
  186. Constants.HEAD, true);
  187. refUpdate.setNewObjectId(commit);
  188. refUpdate.forceUpdate();
  189. }
  190. } finally {
  191. submoduleRepo.close();
  192. }
  193. updated.add(generator.getPath());
  194. }
  195. return updated;
  196. } catch (IOException e) {
  197. throw new JGitInternalException(e.getMessage(), e);
  198. } catch (ConfigInvalidException e) {
  199. throw new InvalidConfigurationException(e.getMessage(), e);
  200. }
  201. }
  202. /**
  203. * @param strategy
  204. * The merge strategy to use during this update operation.
  205. * @return {@code this}
  206. * @since 3.4
  207. */
  208. public SubmoduleUpdateCommand setStrategy(MergeStrategy strategy) {
  209. this.strategy = strategy;
  210. return this;
  211. }
  212. }