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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Git;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.errors.NoWorkTreeException;
  53. import org.eclipse.jgit.internal.storage.file.FileRepository;
  54. import org.eclipse.jgit.junit.JGitTestUtil;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
  57. import org.eclipse.jgit.treewalk.FileTreeIterator;
  58. import org.junit.Before;
  59. import org.junit.experimental.theories.DataPoints;
  60. import org.junit.experimental.theories.Theories;
  61. import org.junit.experimental.theories.Theory;
  62. import org.junit.runner.RunWith;
  63. @RunWith(Theories.class)
  64. public class IndexDiffSubmoduleTest extends RepositoryTestCase {
  65. /** a submodule repository inside a root repository */
  66. protected FileRepository submodule_db;
  67. /** Working directory of the submodule repository */
  68. protected File submodule_trash;
  69. @DataPoints
  70. public static IgnoreSubmoduleMode allModes[] = IgnoreSubmoduleMode.values();
  71. @Override
  72. @Before
  73. public void setUp() throws Exception {
  74. super.setUp();
  75. FileRepository submoduleStandalone = createWorkRepository();
  76. JGitTestUtil.writeTrashFile(submoduleStandalone, "fileInSubmodule",
  77. "submodule");
  78. Git submoduleStandaloneGit = Git.wrap(submoduleStandalone);
  79. submoduleStandaloneGit.add().addFilepattern("fileInSubmodule").call();
  80. submoduleStandaloneGit.commit().setMessage("add file to submodule")
  81. .call();
  82. submodule_db = (FileRepository) Git.wrap(db).submoduleAdd()
  83. .setPath("modules/submodule")
  84. .setURI(submoduleStandalone.getDirectory().toURI().toString())
  85. .call();
  86. submodule_trash = submodule_db.getWorkTree();
  87. addRepoToClose(submodule_db);
  88. writeTrashFile("fileInRoot", "root");
  89. Git rootGit = Git.wrap(db);
  90. rootGit.add().addFilepattern("fileInRoot").call();
  91. rootGit.commit().setMessage("add submodule and root file").call();
  92. }
  93. @Theory
  94. public void testInitiallyClean(IgnoreSubmoduleMode mode)
  95. throws IOException {
  96. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  97. new FileTreeIterator(db));
  98. indexDiff.setIgnoreSubmoduleMode(mode);
  99. assertFalse(indexDiff.diff());
  100. }
  101. @Theory
  102. public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
  103. throws IOException {
  104. writeTrashFile("fileInRoot", "2");
  105. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  106. new FileTreeIterator(db));
  107. indexDiff.setIgnoreSubmoduleMode(mode);
  108. assertTrue(indexDiff.diff());
  109. }
  110. private void assertDiff(IndexDiff indexDiff, IgnoreSubmoduleMode mode,
  111. IgnoreSubmoduleMode... expectedEmptyModes) throws IOException {
  112. boolean diffResult = indexDiff.diff();
  113. Set<String> submodulePaths = indexDiff
  114. .getPathsWithIndexMode(FileMode.GITLINK);
  115. boolean emptyExpected = false;
  116. for (IgnoreSubmoduleMode empty : expectedEmptyModes) {
  117. if (mode.equals(empty)) {
  118. emptyExpected = true;
  119. break;
  120. }
  121. }
  122. if (emptyExpected) {
  123. assertFalse("diff should be false with mode=" + mode,
  124. diffResult);
  125. assertEquals("should have no paths with FileMode.GITLINK", 0,
  126. submodulePaths.size());
  127. } else {
  128. assertTrue("diff should be true with mode=" + mode,
  129. diffResult);
  130. assertTrue("submodule path should have FileMode.GITLINK",
  131. submodulePaths.contains("modules/submodule"));
  132. }
  133. }
  134. @Theory
  135. public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
  136. throws IOException {
  137. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  138. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  139. new FileTreeIterator(db));
  140. indexDiff.setIgnoreSubmoduleMode(mode);
  141. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  142. IgnoreSubmoduleMode.DIRTY);
  143. }
  144. @Theory
  145. public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
  146. throws IOException, GitAPIException {
  147. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  148. Git submoduleGit = Git.wrap(submodule_db);
  149. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  150. submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
  151. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  152. new FileTreeIterator(db));
  153. indexDiff.setIgnoreSubmoduleMode(mode);
  154. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL);
  155. }
  156. @Theory
  157. public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
  158. throws IOException, GitAPIException {
  159. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  160. Git submoduleGit = Git.wrap(submodule_db);
  161. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  162. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  163. new FileTreeIterator(db));
  164. indexDiff.setIgnoreSubmoduleMode(mode);
  165. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  166. IgnoreSubmoduleMode.DIRTY);
  167. }
  168. @Theory
  169. public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
  170. throws IOException, GitAPIException, NoWorkTreeException {
  171. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  172. Git submoduleGit = Git.wrap(submodule_db);
  173. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  174. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
  175. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  176. new FileTreeIterator(db));
  177. indexDiff.setIgnoreSubmoduleMode(mode);
  178. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  179. IgnoreSubmoduleMode.DIRTY);
  180. }
  181. @Theory
  182. public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
  183. throws IOException {
  184. JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
  185. "2");
  186. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  187. new FileTreeIterator(db));
  188. indexDiff.setIgnoreSubmoduleMode(mode);
  189. assertDiff(indexDiff, mode, IgnoreSubmoduleMode.ALL,
  190. IgnoreSubmoduleMode.DIRTY, IgnoreSubmoduleMode.UNTRACKED);
  191. }
  192. }