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.

SubmoduleDeinitCommand.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2017, Two Sigma Open Source
  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 static org.eclipse.jgit.util.FileUtils.RECURSIVE;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.Collection;
  50. import java.util.Collections;
  51. import java.util.List;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.api.errors.NoHeadException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.lib.ConfigConstants;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.lib.StoredConfig;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.revwalk.RevTree;
  63. import org.eclipse.jgit.revwalk.RevWalk;
  64. import org.eclipse.jgit.submodule.SubmoduleWalk;
  65. import org.eclipse.jgit.treewalk.filter.PathFilter;
  66. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  67. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  68. import org.eclipse.jgit.util.FileUtils;
  69. /**
  70. * A class used to execute a submodule deinit command.
  71. * <p>
  72. * This will remove the module(s) from the working tree, but won't affect
  73. * .git/modules.
  74. *
  75. * @since 4.10
  76. * @see <a href=
  77. * "http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
  78. * >Git documentation about submodules</a>
  79. */
  80. public class SubmoduleDeinitCommand
  81. extends GitCommand<Collection<SubmoduleDeinitResult>> {
  82. private final Collection<String> paths;
  83. private boolean force;
  84. /**
  85. * Constructor of SubmoduleDeinitCommand
  86. *
  87. * @param repo
  88. */
  89. public SubmoduleDeinitCommand(Repository repo) {
  90. super(repo);
  91. paths = new ArrayList<>();
  92. }
  93. /**
  94. * {@inheritDoc}
  95. * <p>
  96. *
  97. * @return the set of repositories successfully deinitialized.
  98. * @throws NoSuchSubmoduleException
  99. * if any of the submodules which we might want to deinitialize
  100. * don't exist
  101. */
  102. @Override
  103. public Collection<SubmoduleDeinitResult> call() throws GitAPIException {
  104. checkCallable();
  105. try {
  106. if (paths.isEmpty()) {
  107. return Collections.emptyList();
  108. }
  109. for (String path : paths) {
  110. if (!submoduleExists(path)) {
  111. throw new NoSuchSubmoduleException(path);
  112. }
  113. }
  114. List<SubmoduleDeinitResult> results = new ArrayList<>(paths.size());
  115. try (RevWalk revWalk = new RevWalk(repo);
  116. SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
  117. generator.setFilter(PathFilterGroup.createFromStrings(paths));
  118. StoredConfig config = repo.getConfig();
  119. while (generator.next()) {
  120. String path = generator.getPath();
  121. String name = generator.getModuleName();
  122. SubmoduleDeinitStatus status = checkDirty(revWalk, path);
  123. switch (status) {
  124. case SUCCESS:
  125. deinit(path);
  126. break;
  127. case ALREADY_DEINITIALIZED:
  128. break;
  129. case DIRTY:
  130. if (force) {
  131. deinit(path);
  132. status = SubmoduleDeinitStatus.FORCED;
  133. }
  134. break;
  135. default:
  136. throw new JGitInternalException(MessageFormat.format(
  137. JGitText.get().unexpectedSubmoduleStatus,
  138. status));
  139. }
  140. config.unsetSection(
  141. ConfigConstants.CONFIG_SUBMODULE_SECTION, name);
  142. results.add(new SubmoduleDeinitResult(path, status));
  143. }
  144. }
  145. return results;
  146. } catch (IOException e) {
  147. throw new JGitInternalException(e.getMessage(), e);
  148. }
  149. }
  150. /**
  151. * Recursively delete the *contents* of path, but leave path as an empty
  152. * directory
  153. *
  154. * @param path
  155. * the path to clean
  156. * @throws IOException
  157. */
  158. private void deinit(String path) throws IOException {
  159. File dir = new File(repo.getWorkTree(), path);
  160. if (!dir.isDirectory()) {
  161. throw new JGitInternalException(MessageFormat.format(
  162. JGitText.get().expectedDirectoryNotSubmodule, path));
  163. }
  164. final File[] ls = dir.listFiles();
  165. if (ls != null) {
  166. for (int i = 0; i < ls.length; i++) {
  167. FileUtils.delete(ls[i], RECURSIVE);
  168. }
  169. }
  170. }
  171. /**
  172. * Check if a submodule is dirty. A submodule is dirty if there are local
  173. * changes to the submodule relative to its HEAD, including untracked files.
  174. * It is also dirty if the HEAD of the submodule does not match the value in
  175. * the parent repo's index or HEAD.
  176. *
  177. * @param revWalk
  178. * @param path
  179. * @return status of the command
  180. * @throws GitAPIException
  181. * @throws IOException
  182. */
  183. private SubmoduleDeinitStatus checkDirty(RevWalk revWalk, String path)
  184. throws GitAPIException, IOException {
  185. Ref head = repo.exactRef("HEAD"); //$NON-NLS-1$
  186. if (head == null) {
  187. throw new NoHeadException(
  188. JGitText.get().invalidRepositoryStateNoHead);
  189. }
  190. RevCommit headCommit = revWalk.parseCommit(head.getObjectId());
  191. RevTree tree = headCommit.getTree();
  192. ObjectId submoduleHead;
  193. try (SubmoduleWalk w = SubmoduleWalk.forPath(repo, tree, path)) {
  194. submoduleHead = w.getHead();
  195. if (submoduleHead == null) {
  196. // The submodule is not checked out.
  197. return SubmoduleDeinitStatus.ALREADY_DEINITIALIZED;
  198. }
  199. if (!submoduleHead.equals(w.getObjectId())) {
  200. // The submodule's current HEAD doesn't match the value in the
  201. // outer repo's HEAD.
  202. return SubmoduleDeinitStatus.DIRTY;
  203. }
  204. }
  205. try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) {
  206. if (!w.next()) {
  207. // The submodule does not exist in the index (shouldn't happen
  208. // since we check this earlier)
  209. return SubmoduleDeinitStatus.DIRTY;
  210. }
  211. if (!submoduleHead.equals(w.getObjectId())) {
  212. // The submodule's current HEAD doesn't match the value in the
  213. // outer repo's index.
  214. return SubmoduleDeinitStatus.DIRTY;
  215. }
  216. try (Repository submoduleRepo = w.getRepository()) {
  217. Status status = Git.wrap(submoduleRepo).status().call();
  218. return status.isClean() ? SubmoduleDeinitStatus.SUCCESS
  219. : SubmoduleDeinitStatus.DIRTY;
  220. }
  221. }
  222. }
  223. /**
  224. * Check if this path is a submodule by checking the index, which is what
  225. * git submodule deinit checks.
  226. *
  227. * @param path
  228. * path of the submodule
  229. *
  230. * @return {@code true} if path exists and is a submodule in index,
  231. * {@code false} otherwise
  232. * @throws IOException
  233. */
  234. private boolean submoduleExists(String path) throws IOException {
  235. TreeFilter filter = PathFilter.create(path);
  236. try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) {
  237. return w.setFilter(filter).next();
  238. }
  239. }
  240. /**
  241. * Add repository-relative submodule path to deinitialize
  242. *
  243. * @param path
  244. * (with <code>/</code> as separator)
  245. * @return this command
  246. */
  247. public SubmoduleDeinitCommand addPath(String path) {
  248. paths.add(path);
  249. return this;
  250. }
  251. /**
  252. * If {@code true}, call() will deinitialize modules with local changes;
  253. * else it will refuse to do so.
  254. *
  255. * @param force
  256. * @return {@code this}
  257. */
  258. public SubmoduleDeinitCommand setForce(boolean force) {
  259. this.force = force;
  260. return this;
  261. }
  262. /**
  263. * The user tried to deinitialize a submodule that doesn't exist in the
  264. * index.
  265. */
  266. public static class NoSuchSubmoduleException extends GitAPIException {
  267. private static final long serialVersionUID = 1L;
  268. /**
  269. * Constructor of NoSuchSubmoduleException
  270. *
  271. * @param path
  272. * path of non-existing submodule
  273. */
  274. public NoSuchSubmoduleException(String path) {
  275. super(MessageFormat.format(JGitText.get().noSuchSubmodule, path));
  276. }
  277. }
  278. /**
  279. * The effect of a submodule deinit command for a given path
  280. */
  281. public enum SubmoduleDeinitStatus {
  282. /**
  283. * The submodule was not initialized in the first place
  284. */
  285. ALREADY_DEINITIALIZED,
  286. /**
  287. * The submodule was deinitialized
  288. */
  289. SUCCESS,
  290. /**
  291. * The submodule had local changes, but was deinitialized successfully
  292. */
  293. FORCED,
  294. /**
  295. * The submodule had local changes and force was false
  296. */
  297. DIRTY,
  298. }
  299. }