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.

IndexDiffSubmoduleTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2014, Christian Halstrick <christian.halstrick@sap.com>
  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.lib;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.Set;
  50. import org.eclipse.jgit.api.CloneCommand;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.errors.NoWorkTreeException;
  54. import org.eclipse.jgit.internal.storage.file.FileRepository;
  55. import org.eclipse.jgit.junit.JGitTestUtil;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
  58. import org.eclipse.jgit.treewalk.FileTreeIterator;
  59. import org.junit.Before;
  60. import org.junit.experimental.theories.DataPoints;
  61. import org.junit.experimental.theories.Theories;
  62. import org.junit.experimental.theories.Theory;
  63. import org.junit.runner.RunWith;
  64. @RunWith(Theories.class)
  65. public class IndexDiffSubmoduleTest extends RepositoryTestCase {
  66. /** a submodule repository inside a root repository */
  67. protected FileRepository submodule_db;
  68. /** Working directory of the submodule repository */
  69. protected File submodule_trash;
  70. @DataPoints
  71. public static IgnoreSubmoduleMode allModes[] = IgnoreSubmoduleMode.values();
  72. @Override
  73. @Before
  74. public void setUp() throws Exception {
  75. super.setUp();
  76. FileRepository submoduleStandalone = createWorkRepository();
  77. JGitTestUtil.writeTrashFile(submoduleStandalone, "fileInSubmodule",
  78. "submodule");
  79. Git submoduleStandaloneGit = Git.wrap(submoduleStandalone);
  80. submoduleStandaloneGit.add().addFilepattern("fileInSubmodule").call();
  81. submoduleStandaloneGit.commit().setMessage("add file to submodule")
  82. .call();
  83. submodule_db = (FileRepository) Git.wrap(db).submoduleAdd()
  84. .setPath("modules/submodule")
  85. .setURI(submoduleStandalone.getDirectory().toURI().toString())
  86. .call();
  87. submodule_trash = submodule_db.getWorkTree();
  88. addRepoToClose(submodule_db);
  89. writeTrashFile("fileInRoot", "root");
  90. Git rootGit = Git.wrap(db);
  91. rootGit.add().addFilepattern("fileInRoot").call();
  92. rootGit.commit().setMessage("add submodule and root file").call();
  93. }
  94. @Theory
  95. public void testInitiallyClean(IgnoreSubmoduleMode mode)
  96. throws IOException {
  97. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  98. new FileTreeIterator(db));
  99. indexDiff.setIgnoreSubmoduleMode(mode);
  100. assertFalse(indexDiff.diff());
  101. }
  102. private Repository cloneWithoutCloningSubmodule() throws Exception {
  103. File directory = createTempDirectory(
  104. "testCloneWithoutCloningSubmodules");
  105. CloneCommand clone = Git.cloneRepository();
  106. clone.setDirectory(directory);
  107. clone.setCloneSubmodules(false);
  108. clone.setURI(db.getDirectory().toURI().toString());
  109. Git git2 = clone.call();
  110. addRepoToClose(git2.getRepository());
  111. return git2.getRepository();
  112. }
  113. @Theory
  114. public void testCleanAfterClone(IgnoreSubmoduleMode mode) throws Exception {
  115. Repository db2 = cloneWithoutCloningSubmodule();
  116. IndexDiff indexDiff = new IndexDiff(db2, Constants.HEAD,
  117. new FileTreeIterator(db2));
  118. indexDiff.setIgnoreSubmoduleMode(mode);
  119. assertFalse(indexDiff.diff());
  120. }
  121. @Theory
  122. public void testMissingIfDirectoryGone(IgnoreSubmoduleMode mode)
  123. throws Exception {
  124. recursiveDelete(submodule_trash);
  125. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  126. new FileTreeIterator(db));
  127. indexDiff.setIgnoreSubmoduleMode(mode);
  128. boolean hasChanges = indexDiff.diff();
  129. if (mode != IgnoreSubmoduleMode.ALL) {
  130. assertTrue(hasChanges);
  131. assertEquals("[modules/submodule]",
  132. indexDiff.getMissing().toString());
  133. } else {
  134. assertFalse(hasChanges);
  135. }
  136. }
  137. @Theory
  138. public void testSubmoduleReplacedByFile(IgnoreSubmoduleMode mode)
  139. throws Exception {
  140. recursiveDelete(submodule_trash);
  141. writeTrashFile("modules/submodule", "nonsense");
  142. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  143. new FileTreeIterator(db));
  144. indexDiff.setIgnoreSubmoduleMode(mode);
  145. assertTrue(indexDiff.diff());
  146. assertEquals("[]", indexDiff.getMissing().toString());
  147. assertEquals("[]", indexDiff.getUntracked().toString());
  148. assertEquals("[modules/submodule]", indexDiff.getModified().toString());
  149. }
  150. @Theory
  151. public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
  152. throws IOException {
  153. writeTrashFile("fileInRoot", "2");
  154. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  155. new FileTreeIterator(db));
  156. indexDiff.setIgnoreSubmoduleMode(mode);
  157. assertTrue(indexDiff.diff());
  158. }
  159. private void assertDiff(IndexDiff indexDiff, IgnoreSubmoduleMode mode,
  160. IgnoreSubmoduleMode... expectedEmptyModes) throws IOException {
  161. boolean diffResult = indexDiff.diff();
  162. Set<String> submodulePaths = indexDiff
  163. .getPathsWithIndexMode(FileMode.GITLINK);
  164. boolean emptyExpected = false;
  165. for (IgnoreSubmoduleMode empty : expectedEmptyModes) {
  166. if (mode.equals(empty)) {
  167. emptyExpected = true;
  168. break;
  169. }
  170. }
  171. if (emptyExpected) {
  172. assertFalse("diff should be false with mode=" + mode,
  173. diffResult);
  174. assertEquals("should have no paths with FileMode.GITLINK", 0,
  175. submodulePaths.size());
  176. } else {
  177. assertTrue("diff should be true with mode=" + mode,
  178. diffResult);
  179. assertTrue("submodule path should have FileMode.GITLINK",
  180. submodulePaths.contains("modules/submodule"));
  181. }
  182. }
  183. @Theory
  184. public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
  185. throws IOException {
  186. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  187. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  188. new FileTreeIterator(db));
  189. indexDiff.setIgnoreSubmoduleMode(mode);
  190. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  191. IgnoreSubmoduleMode.DIRTY);
  192. }
  193. @Theory
  194. public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
  195. throws IOException, GitAPIException {
  196. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  197. Git submoduleGit = Git.wrap(submodule_db);
  198. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  199. submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
  200. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  201. new FileTreeIterator(db));
  202. indexDiff.setIgnoreSubmoduleMode(mode);
  203. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL);
  204. }
  205. @Theory
  206. public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
  207. throws IOException, GitAPIException {
  208. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  209. Git submoduleGit = Git.wrap(submodule_db);
  210. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  211. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  212. new FileTreeIterator(db));
  213. indexDiff.setIgnoreSubmoduleMode(mode);
  214. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  215. IgnoreSubmoduleMode.DIRTY);
  216. }
  217. @Theory
  218. public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
  219. throws IOException, GitAPIException, NoWorkTreeException {
  220. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  221. Git submoduleGit = Git.wrap(submodule_db);
  222. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  223. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
  224. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  225. new FileTreeIterator(db));
  226. indexDiff.setIgnoreSubmoduleMode(mode);
  227. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  228. IgnoreSubmoduleMode.DIRTY);
  229. }
  230. @Theory
  231. public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
  232. throws IOException {
  233. JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
  234. "2");
  235. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  236. new FileTreeIterator(db));
  237. indexDiff.setIgnoreSubmoduleMode(mode);
  238. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  239. IgnoreSubmoduleMode.DIRTY, IgnoreSubmoduleMode.UNTRACKED);
  240. }
  241. }