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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. submodule_trash = submodule_db.getWorkTree();
  85. addRepoToClose(submodule_db);
  86. writeTrashFile("fileInRoot", "root");
  87. Git rootGit = Git.wrap(db);
  88. rootGit.add().addFilepattern("fileInRoot").call();
  89. rootGit.commit().setMessage("add submodule and root file").call();
  90. }
  91. @Theory
  92. public void testInitiallyClean(IgnoreSubmoduleMode mode)
  93. throws IOException {
  94. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  95. new FileTreeIterator(db));
  96. indexDiff.setIgnoreSubmoduleMode(mode);
  97. assertFalse(indexDiff.diff());
  98. }
  99. @Theory
  100. public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
  101. throws IOException {
  102. writeTrashFile("fileInRoot", "2");
  103. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  104. new FileTreeIterator(db));
  105. indexDiff.setIgnoreSubmoduleMode(mode);
  106. assertTrue(indexDiff.diff());
  107. }
  108. @Theory
  109. public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
  110. throws IOException {
  111. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  112. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  113. new FileTreeIterator(db));
  114. indexDiff.setIgnoreSubmoduleMode(mode);
  115. if (mode.equals(IgnoreSubmoduleMode.ALL)
  116. || mode.equals(IgnoreSubmoduleMode.DIRTY))
  117. assertFalse("diff should be false with mode=" + mode,
  118. indexDiff.diff());
  119. else
  120. assertTrue("diff should be true with mode=" + mode,
  121. indexDiff.diff());
  122. }
  123. @Theory
  124. public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
  125. throws IOException, GitAPIException {
  126. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  127. Git submoduleGit = Git.wrap(submodule_db);
  128. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  129. submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
  130. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  131. new FileTreeIterator(db));
  132. indexDiff.setIgnoreSubmoduleMode(mode);
  133. if (mode.equals(IgnoreSubmoduleMode.ALL))
  134. assertFalse("diff should be false with mode=" + mode,
  135. indexDiff.diff());
  136. else
  137. assertTrue("diff should be true with mode=" + mode,
  138. indexDiff.diff());
  139. }
  140. @Theory
  141. public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
  142. throws IOException, GitAPIException {
  143. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  144. Git submoduleGit = Git.wrap(submodule_db);
  145. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  146. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  147. new FileTreeIterator(db));
  148. indexDiff.setIgnoreSubmoduleMode(mode);
  149. if (mode.equals(IgnoreSubmoduleMode.ALL)
  150. || mode.equals(IgnoreSubmoduleMode.DIRTY))
  151. assertFalse("diff should be false with mode=" + mode,
  152. indexDiff.diff());
  153. else
  154. assertTrue("diff should be true with mode=" + mode,
  155. indexDiff.diff());
  156. }
  157. @Theory
  158. public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
  159. throws IOException, GitAPIException, NoWorkTreeException {
  160. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
  161. Git submoduleGit = Git.wrap(submodule_db);
  162. submoduleGit.add().addFilepattern("fileInSubmodule").call();
  163. JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
  164. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  165. new FileTreeIterator(db));
  166. indexDiff.setIgnoreSubmoduleMode(mode);
  167. if (mode.equals(IgnoreSubmoduleMode.ALL)
  168. || mode.equals(IgnoreSubmoduleMode.DIRTY))
  169. assertFalse("diff should be false with mode=" + mode,
  170. indexDiff.diff());
  171. else
  172. assertTrue("diff should be true with mode=" + mode,
  173. indexDiff.diff());
  174. }
  175. @Theory
  176. public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
  177. throws IOException {
  178. JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
  179. "2");
  180. IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
  181. new FileTreeIterator(db));
  182. indexDiff.setIgnoreSubmoduleMode(mode);
  183. if (mode.equals(IgnoreSubmoduleMode.ALL)
  184. || mode.equals(IgnoreSubmoduleMode.DIRTY)
  185. || mode.equals(IgnoreSubmoduleMode.UNTRACKED))
  186. assertFalse("diff should be false with mode=" + mode,
  187. indexDiff.diff());
  188. else
  189. assertTrue("diff should be true with mode=" + mode,
  190. indexDiff.diff());
  191. }
  192. }