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.

SubmoduleDeinitTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.submodule;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.Collection;
  47. import org.eclipse.jgit.api.Git;
  48. import org.eclipse.jgit.api.SubmoduleDeinitCommand;
  49. import org.eclipse.jgit.api.SubmoduleDeinitResult;
  50. import org.eclipse.jgit.api.SubmoduleUpdateCommand;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheEditor;
  54. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  55. import org.eclipse.jgit.dircache.DirCacheEntry;
  56. import org.eclipse.jgit.junit.JGitTestUtil;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.ConfigConstants;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.lib.StoredConfig;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  64. import org.eclipse.jgit.storage.file.FileBasedConfig;
  65. import org.junit.Test;
  66. import static org.junit.Assert.assertEquals;
  67. import static org.junit.Assert.assertNotEquals;
  68. import static org.junit.Assert.assertNotNull;
  69. import static org.junit.Assert.assertTrue;
  70. /**
  71. * Unit tests of {@link SubmoduleDeinitCommand}
  72. */
  73. public class SubmoduleDeinitTest extends RepositoryTestCase {
  74. @Test
  75. public void repositoryWithNoSubmodules() throws GitAPIException {
  76. SubmoduleDeinitCommand command = new SubmoduleDeinitCommand(db);
  77. Collection<SubmoduleDeinitResult> modules = command.call();
  78. assertNotNull(modules);
  79. assertTrue(modules.isEmpty());
  80. }
  81. @Test
  82. public void alreadyClosedSubmodule() throws Exception {
  83. final String path = "sub";
  84. Git git = Git.wrap(db);
  85. commitSubmoduleCreation(path, git);
  86. SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub"));
  87. assertEquals(path, result.getPath());
  88. assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.ALREADY_DEINITIALIZED, result.getStatus());
  89. }
  90. @Test
  91. public void dirtySubmoduleBecauseUntracked() throws Exception {
  92. final String path = "sub";
  93. Git git = Git.wrap(db);
  94. commitSubmoduleCreation(path, git);
  95. Collection<String> updated = new SubmoduleUpdateCommand(db).addPath(path).setFetch(false).call();
  96. assertEquals(1, updated.size());
  97. File submoduleDir = assertSubmoduleIsInitialized();
  98. SubmoduleWalk generator;
  99. write(new File(submoduleDir, "untracked"), "untracked");
  100. SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub"));
  101. assertEquals(path, result.getPath());
  102. assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus());
  103. generator = SubmoduleWalk.forIndex(db);
  104. assertTrue(generator.next());
  105. assertTrue(submoduleDir.isDirectory());
  106. assertNotEquals(0, submoduleDir.list().length);
  107. }
  108. @Test
  109. public void dirtySubmoduleBecauseNewCommit() throws Exception {
  110. final String path = "sub";
  111. Git git = Git.wrap(db);
  112. commitSubmoduleCreation(path, git);
  113. Collection<String> updated = new SubmoduleUpdateCommand(db).addPath(path).setFetch(false).call();
  114. assertEquals(1, updated.size());
  115. File submoduleDir = assertSubmoduleIsInitialized();
  116. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  117. generator.next();
  118. //want to create a commit inside the repo...
  119. try (Repository submoduleLocalRepo = generator.getRepository()) {
  120. JGitTestUtil.writeTrashFile(submoduleLocalRepo, "file.txt",
  121. "new data");
  122. Git.wrap(submoduleLocalRepo).commit().setAll(true)
  123. .setMessage("local commit").call();
  124. }
  125. SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub"));
  126. assertEquals(path, result.getPath());
  127. assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus());
  128. generator = SubmoduleWalk.forIndex(db);
  129. assertTrue(generator.next());
  130. assertTrue(submoduleDir.isDirectory());
  131. assertNotEquals(0, submoduleDir.list().length);
  132. }
  133. private File assertSubmoduleIsInitialized() throws IOException {
  134. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  135. assertTrue(generator.next());
  136. File submoduleDir = new File(db.getWorkTree(), generator.getPath());
  137. assertTrue(submoduleDir.isDirectory());
  138. assertNotEquals(0, submoduleDir.list().length);
  139. return submoduleDir;
  140. }
  141. @Test
  142. public void dirtySubmoduleWithForce() throws Exception {
  143. final String path = "sub";
  144. Git git = Git.wrap(db);
  145. commitSubmoduleCreation(path, git);
  146. Collection<String> updated = new SubmoduleUpdateCommand(db).addPath(path).setFetch(false).call();
  147. assertEquals(1, updated.size());
  148. File submoduleDir = assertSubmoduleIsInitialized();
  149. write(new File(submoduleDir, "untracked"), "untracked");
  150. SubmoduleDeinitCommand command = new SubmoduleDeinitCommand(db).addPath("sub").setForce(true);
  151. SubmoduleDeinitResult result = runDeinit(command);
  152. assertEquals(path, result.getPath());
  153. assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.FORCED, result.getStatus());
  154. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  155. assertTrue(generator.next());
  156. assertTrue(submoduleDir.isDirectory());
  157. assertEquals(0, submoduleDir.list().length);
  158. }
  159. @Test
  160. public void cleanSubmodule() throws Exception {
  161. final String path = "sub";
  162. Git git = Git.wrap(db);
  163. commitSubmoduleCreation(path, git);
  164. Collection<String> updated = new SubmoduleUpdateCommand(db).addPath(path).setFetch(false).call();
  165. assertEquals(1, updated.size());
  166. File submoduleDir = assertSubmoduleIsInitialized();
  167. SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub"));
  168. assertEquals(path, result.getPath());
  169. assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.SUCCESS, result.getStatus());
  170. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  171. assertTrue(generator.next());
  172. assertTrue(submoduleDir.isDirectory());
  173. assertEquals(0, submoduleDir.list().length);
  174. }
  175. private SubmoduleDeinitResult runDeinit(SubmoduleDeinitCommand command) throws GitAPIException {
  176. Collection<SubmoduleDeinitResult> deinitialized = command.call();
  177. assertNotNull(deinitialized);
  178. assertEquals(1, deinitialized.size());
  179. return deinitialized.iterator().next();
  180. }
  181. private RevCommit commitSubmoduleCreation(String path, Git git) throws IOException, GitAPIException {
  182. writeTrashFile("file.txt", "content");
  183. git.add().addFilepattern("file.txt").call();
  184. final RevCommit commit = git.commit().setMessage("create file").call();
  185. DirCache cache = db.lockDirCache();
  186. DirCacheEditor editor = cache.editor();
  187. editor.add(new PathEdit(path) {
  188. @Override
  189. public void apply(DirCacheEntry ent) {
  190. ent.setFileMode(FileMode.GITLINK);
  191. ent.setObjectId(commit);
  192. }
  193. });
  194. editor.commit();
  195. StoredConfig config = db.getConfig();
  196. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  197. ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
  198. .toString());
  199. config.save();
  200. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  201. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  202. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  203. ConfigConstants.CONFIG_KEY_PATH, path);
  204. modulesConfig.save();
  205. new File(db.getWorkTree(), "sub").mkdir();
  206. git.add().addFilepattern(Constants.DOT_GIT_MODULES).call();
  207. git.commit().setMessage("create submodule").call();
  208. return commit;
  209. }
  210. }