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