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.

IndexDiffTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.dircache.DirCacheEditor;
  53. import org.eclipse.jgit.dircache.DirCacheEntry;
  54. import org.eclipse.jgit.treewalk.FileTreeIterator;
  55. import org.junit.Test;
  56. public class IndexDiffTest extends RepositoryTestCase {
  57. @Test
  58. public void testAdded() throws IOException {
  59. GitIndex index = new GitIndex(db);
  60. writeTrashFile("file1", "file1");
  61. writeTrashFile("dir/subfile", "dir/subfile");
  62. Tree tree = new Tree(db);
  63. tree.setId(insertTree(tree));
  64. index.add(trash, new File(trash, "file1"));
  65. index.add(trash, new File(trash, "dir/subfile"));
  66. index.write();
  67. FileTreeIterator iterator = new FileTreeIterator(db);
  68. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  69. diff.diff();
  70. assertEquals(2, diff.getAdded().size());
  71. assertTrue(diff.getAdded().contains("file1"));
  72. assertTrue(diff.getAdded().contains("dir/subfile"));
  73. assertEquals(0, diff.getChanged().size());
  74. assertEquals(0, diff.getModified().size());
  75. assertEquals(0, diff.getRemoved().size());
  76. }
  77. @Test
  78. public void testRemoved() throws IOException {
  79. writeTrashFile("file2", "file2");
  80. writeTrashFile("dir/file3", "dir/file3");
  81. Tree tree = new Tree(db);
  82. tree.addFile("file2");
  83. tree.addFile("dir/file3");
  84. assertEquals(2, tree.memberCount());
  85. tree.findBlobMember("file2").setId(ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
  86. Tree tree2 = (Tree) tree.findTreeMember("dir");
  87. tree2.findBlobMember("file3").setId(ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
  88. tree2.setId(insertTree(tree2));
  89. tree.setId(insertTree(tree));
  90. FileTreeIterator iterator = new FileTreeIterator(db);
  91. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  92. diff.diff();
  93. assertEquals(2, diff.getRemoved().size());
  94. assertTrue(diff.getRemoved().contains("file2"));
  95. assertTrue(diff.getRemoved().contains("dir/file3"));
  96. assertEquals(0, diff.getChanged().size());
  97. assertEquals(0, diff.getModified().size());
  98. assertEquals(0, diff.getAdded().size());
  99. }
  100. @Test
  101. public void testModified() throws IOException {
  102. GitIndex index = new GitIndex(db);
  103. index.add(trash, writeTrashFile("file2", "file2"));
  104. index.add(trash, writeTrashFile("dir/file3", "dir/file3"));
  105. index.write();
  106. writeTrashFile("dir/file3", "changed");
  107. Tree tree = new Tree(db);
  108. tree.addFile("file2").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  109. tree.addFile("dir/file3").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  110. assertEquals(2, tree.memberCount());
  111. Tree tree2 = (Tree) tree.findTreeMember("dir");
  112. tree2.setId(insertTree(tree2));
  113. tree.setId(insertTree(tree));
  114. FileTreeIterator iterator = new FileTreeIterator(db);
  115. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  116. diff.diff();
  117. assertEquals(2, diff.getChanged().size());
  118. assertTrue(diff.getChanged().contains("file2"));
  119. assertTrue(diff.getChanged().contains("dir/file3"));
  120. assertEquals(1, diff.getModified().size());
  121. assertTrue(diff.getModified().contains("dir/file3"));
  122. assertEquals(0, diff.getAdded().size());
  123. assertEquals(0, diff.getRemoved().size());
  124. assertEquals(0, diff.getMissing().size());
  125. }
  126. @Test
  127. public void testUnchangedSimple() throws IOException {
  128. GitIndex index = new GitIndex(db);
  129. index.add(trash, writeTrashFile("a.b", "a.b"));
  130. index.add(trash, writeTrashFile("a.c", "a.c"));
  131. index.add(trash, writeTrashFile("a=c", "a=c"));
  132. index.add(trash, writeTrashFile("a=d", "a=d"));
  133. index.write();
  134. Tree tree = new Tree(db);
  135. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  136. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  137. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  138. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  139. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  140. tree.setId(insertTree(tree));
  141. FileTreeIterator iterator = new FileTreeIterator(db);
  142. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  143. diff.diff();
  144. assertEquals(0, diff.getChanged().size());
  145. assertEquals(0, diff.getAdded().size());
  146. assertEquals(0, diff.getRemoved().size());
  147. assertEquals(0, diff.getMissing().size());
  148. assertEquals(0, diff.getModified().size());
  149. }
  150. /**
  151. * This test has both files and directories that involve
  152. * the tricky ordering used by Git.
  153. *
  154. * @throws IOException
  155. */
  156. @Test
  157. public void testUnchangedComplex() throws IOException {
  158. GitIndex index = new GitIndex(db);
  159. index.add(trash, writeTrashFile("a.b", "a.b"));
  160. index.add(trash, writeTrashFile("a.c", "a.c"));
  161. index.add(trash, writeTrashFile("a/b.b/b", "a/b.b/b"));
  162. index.add(trash, writeTrashFile("a/b", "a/b"));
  163. index.add(trash, writeTrashFile("a/c", "a/c"));
  164. index.add(trash, writeTrashFile("a=c", "a=c"));
  165. index.add(trash, writeTrashFile("a=d", "a=d"));
  166. index.write();
  167. Tree tree = new Tree(db);
  168. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  169. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  170. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  171. tree.addFile("a/b.b/b").setId(ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
  172. tree.addFile("a/b").setId(ObjectId.fromString("db89c972fc57862eae378f45b74aca228037d415"));
  173. tree.addFile("a/c").setId(ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
  174. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  175. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  176. Tree tree3 = (Tree) tree.findTreeMember("a/b.b");
  177. tree3.setId(insertTree(tree3));
  178. Tree tree2 = (Tree) tree.findTreeMember("a");
  179. tree2.setId(insertTree(tree2));
  180. tree.setId(insertTree(tree));
  181. FileTreeIterator iterator = new FileTreeIterator(db);
  182. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  183. diff.diff();
  184. assertEquals(0, diff.getChanged().size());
  185. assertEquals(0, diff.getAdded().size());
  186. assertEquals(0, diff.getRemoved().size());
  187. assertEquals(0, diff.getMissing().size());
  188. assertEquals(0, diff.getModified().size());
  189. }
  190. private ObjectId insertTree(Tree tree) throws IOException {
  191. ObjectInserter oi = db.newObjectInserter();
  192. try {
  193. ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
  194. oi.flush();
  195. return id;
  196. } finally {
  197. oi.release();
  198. }
  199. }
  200. /**
  201. * A file is removed from the index but stays in the working directory. It
  202. * is checked if IndexDiff detects this file as removed and untracked.
  203. *
  204. * @throws Exception
  205. */
  206. @Test
  207. public void testRemovedUntracked() throws Exception{
  208. Git git = new Git(db);
  209. String path = "file";
  210. writeTrashFile(path, "content");
  211. git.add().addFilepattern(path).call();
  212. git.commit().setMessage("commit").call();
  213. removeFromIndex(path);
  214. FileTreeIterator iterator = new FileTreeIterator(db);
  215. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  216. diff.diff();
  217. assertTrue(diff.getRemoved().contains(path));
  218. assertTrue(diff.getUntracked().contains(path));
  219. }
  220. @Test
  221. public void testAssumeUnchanged() throws Exception {
  222. Git git = new Git(db);
  223. String path = "file";
  224. writeTrashFile(path, "content");
  225. git.add().addFilepattern(path).call();
  226. String path2 = "file2";
  227. writeTrashFile(path2, "content");
  228. git.add().addFilepattern(path2).call();
  229. git.commit().setMessage("commit").call();
  230. assumeUnchanged(path2);
  231. writeTrashFile(path, "more content");
  232. writeTrashFile(path2, "more content");
  233. FileTreeIterator iterator = new FileTreeIterator(db);
  234. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  235. diff.diff();
  236. assertEquals(1, diff.getAssumeUnchanged().size());
  237. assertEquals(1, diff.getModified().size());
  238. assertEquals(0, diff.getChanged().size());
  239. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  240. assertTrue(diff.getModified().contains("file"));
  241. git.add().addFilepattern(".").call();
  242. iterator = new FileTreeIterator(db);
  243. diff = new IndexDiff(db, Constants.HEAD, iterator);
  244. diff.diff();
  245. assertEquals(1, diff.getAssumeUnchanged().size());
  246. assertEquals(0, diff.getModified().size());
  247. assertEquals(1, diff.getChanged().size());
  248. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  249. assertTrue(diff.getChanged().contains("file"));
  250. }
  251. private void removeFromIndex(String path) throws IOException {
  252. final DirCache dirc = db.lockDirCache();
  253. final DirCacheEditor edit = dirc.editor();
  254. edit.add(new DirCacheEditor.DeletePath(path));
  255. if (!edit.commit())
  256. throw new IOException("could not commit");
  257. }
  258. private void assumeUnchanged(String path) throws IOException {
  259. final DirCache dirc = db.lockDirCache();
  260. final DirCacheEntry ent = dirc.getEntry(path);
  261. if (ent != null)
  262. ent.setAssumeValid(true);
  263. dirc.write();
  264. if (!dirc.commit())
  265. throw new IOException("could not commit");
  266. }
  267. }