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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 java.io.File;
  47. import java.io.IOException;
  48. import org.eclipse.jgit.api.Git;
  49. import org.eclipse.jgit.dircache.DirCache;
  50. import org.eclipse.jgit.dircache.DirCacheEditor;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.treewalk.FileTreeIterator;
  53. public class IndexDiffTest extends RepositoryTestCase {
  54. public void testAdded() throws IOException {
  55. GitIndex index = new GitIndex(db);
  56. writeTrashFile("file1", "file1");
  57. writeTrashFile("dir/subfile", "dir/subfile");
  58. Tree tree = new Tree(db);
  59. tree.setId(insertTree(tree));
  60. index.add(trash, new File(trash, "file1"));
  61. index.add(trash, new File(trash, "dir/subfile"));
  62. index.write();
  63. FileTreeIterator iterator = new FileTreeIterator(db);
  64. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  65. diff.diff();
  66. assertEquals(2, diff.getAdded().size());
  67. assertTrue(diff.getAdded().contains("file1"));
  68. assertTrue(diff.getAdded().contains("dir/subfile"));
  69. assertEquals(0, diff.getChanged().size());
  70. assertEquals(0, diff.getModified().size());
  71. assertEquals(0, diff.getRemoved().size());
  72. }
  73. public void testRemoved() throws IOException {
  74. writeTrashFile("file2", "file2");
  75. writeTrashFile("dir/file3", "dir/file3");
  76. Tree tree = new Tree(db);
  77. tree.addFile("file2");
  78. tree.addFile("dir/file3");
  79. assertEquals(2, tree.memberCount());
  80. tree.findBlobMember("file2").setId(ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
  81. Tree tree2 = (Tree) tree.findTreeMember("dir");
  82. tree2.findBlobMember("file3").setId(ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
  83. tree2.setId(insertTree(tree2));
  84. tree.setId(insertTree(tree));
  85. FileTreeIterator iterator = new FileTreeIterator(db);
  86. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  87. diff.diff();
  88. assertEquals(2, diff.getRemoved().size());
  89. assertTrue(diff.getRemoved().contains("file2"));
  90. assertTrue(diff.getRemoved().contains("dir/file3"));
  91. assertEquals(0, diff.getChanged().size());
  92. assertEquals(0, diff.getModified().size());
  93. assertEquals(0, diff.getAdded().size());
  94. }
  95. public void testModified() throws IOException {
  96. GitIndex index = new GitIndex(db);
  97. index.add(trash, writeTrashFile("file2", "file2"));
  98. index.add(trash, writeTrashFile("dir/file3", "dir/file3"));
  99. index.write();
  100. writeTrashFile("dir/file3", "changed");
  101. Tree tree = new Tree(db);
  102. tree.addFile("file2").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  103. tree.addFile("dir/file3").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  104. assertEquals(2, tree.memberCount());
  105. Tree tree2 = (Tree) tree.findTreeMember("dir");
  106. tree2.setId(insertTree(tree2));
  107. tree.setId(insertTree(tree));
  108. FileTreeIterator iterator = new FileTreeIterator(db);
  109. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  110. diff.diff();
  111. assertEquals(2, diff.getChanged().size());
  112. assertTrue(diff.getChanged().contains("file2"));
  113. assertTrue(diff.getChanged().contains("dir/file3"));
  114. assertEquals(1, diff.getModified().size());
  115. assertTrue(diff.getModified().contains("dir/file3"));
  116. assertEquals(0, diff.getAdded().size());
  117. assertEquals(0, diff.getRemoved().size());
  118. assertEquals(0, diff.getMissing().size());
  119. }
  120. public void testUnchangedSimple() throws IOException {
  121. GitIndex index = new GitIndex(db);
  122. index.add(trash, writeTrashFile("a.b", "a.b"));
  123. index.add(trash, writeTrashFile("a.c", "a.c"));
  124. index.add(trash, writeTrashFile("a=c", "a=c"));
  125. index.add(trash, writeTrashFile("a=d", "a=d"));
  126. index.write();
  127. Tree tree = new Tree(db);
  128. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  129. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  130. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  131. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  132. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  133. tree.setId(insertTree(tree));
  134. FileTreeIterator iterator = new FileTreeIterator(db);
  135. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  136. diff.diff();
  137. assertEquals(0, diff.getChanged().size());
  138. assertEquals(0, diff.getAdded().size());
  139. assertEquals(0, diff.getRemoved().size());
  140. assertEquals(0, diff.getMissing().size());
  141. assertEquals(0, diff.getModified().size());
  142. }
  143. /**
  144. * This test has both files and directories that involve
  145. * the tricky ordering used by Git.
  146. *
  147. * @throws IOException
  148. */
  149. public void testUnchangedComplex() throws IOException {
  150. GitIndex index = new GitIndex(db);
  151. index.add(trash, writeTrashFile("a.b", "a.b"));
  152. index.add(trash, writeTrashFile("a.c", "a.c"));
  153. index.add(trash, writeTrashFile("a/b.b/b", "a/b.b/b"));
  154. index.add(trash, writeTrashFile("a/b", "a/b"));
  155. index.add(trash, writeTrashFile("a/c", "a/c"));
  156. index.add(trash, writeTrashFile("a=c", "a=c"));
  157. index.add(trash, writeTrashFile("a=d", "a=d"));
  158. index.write();
  159. Tree tree = new Tree(db);
  160. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  161. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  162. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  163. tree.addFile("a/b.b/b").setId(ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
  164. tree.addFile("a/b").setId(ObjectId.fromString("db89c972fc57862eae378f45b74aca228037d415"));
  165. tree.addFile("a/c").setId(ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
  166. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  167. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  168. Tree tree3 = (Tree) tree.findTreeMember("a/b.b");
  169. tree3.setId(insertTree(tree3));
  170. Tree tree2 = (Tree) tree.findTreeMember("a");
  171. tree2.setId(insertTree(tree2));
  172. tree.setId(insertTree(tree));
  173. FileTreeIterator iterator = new FileTreeIterator(db);
  174. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  175. diff.diff();
  176. assertEquals(0, diff.getChanged().size());
  177. assertEquals(0, diff.getAdded().size());
  178. assertEquals(0, diff.getRemoved().size());
  179. assertEquals(0, diff.getMissing().size());
  180. assertEquals(0, diff.getModified().size());
  181. }
  182. private ObjectId insertTree(Tree tree) throws IOException {
  183. ObjectInserter oi = db.newObjectInserter();
  184. try {
  185. ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
  186. oi.flush();
  187. return id;
  188. } finally {
  189. oi.release();
  190. }
  191. }
  192. /**
  193. * A file is removed from the index but stays in the working directory. It
  194. * is checked if IndexDiff detects this file as removed and untracked.
  195. *
  196. * @throws Exception
  197. */
  198. public void testRemovedUntracked() throws Exception{
  199. Git git = new Git(db);
  200. String path = "file";
  201. writeTrashFile(path, "content");
  202. git.add().addFilepattern(path).call();
  203. git.commit().setMessage("commit").call();
  204. removeFromIndex(path);
  205. FileTreeIterator iterator = new FileTreeIterator(db);
  206. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  207. diff.diff();
  208. assertTrue(diff.getRemoved().contains(path));
  209. assertTrue(diff.getUntracked().contains(path));
  210. }
  211. public void testAssumeUnchanged() throws Exception {
  212. Git git = new Git(db);
  213. String path = "file";
  214. writeTrashFile(path, "content");
  215. git.add().addFilepattern(path).call();
  216. String path2 = "file2";
  217. writeTrashFile(path2, "content");
  218. git.add().addFilepattern(path2).call();
  219. git.commit().setMessage("commit").call();
  220. assumeUnchanged(path2);
  221. writeTrashFile(path, "more content");
  222. writeTrashFile(path2, "more content");
  223. FileTreeIterator iterator = new FileTreeIterator(db);
  224. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  225. diff.diff();
  226. assertEquals(1, diff.getAssumeUnchanged().size());
  227. assertEquals(2, diff.getModified().size());
  228. assertEquals(0, diff.getChanged().size());
  229. git.add().addFilepattern(".").call();
  230. iterator = new FileTreeIterator(db);
  231. diff = new IndexDiff(db, Constants.HEAD, iterator);
  232. diff.diff();
  233. assertEquals(1, diff.getAssumeUnchanged().size());
  234. assertEquals(1, diff.getModified().size());
  235. assertEquals(1, diff.getChanged().size());
  236. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  237. assertTrue(diff.getModified().contains("file2"));
  238. assertTrue(diff.getChanged().contains("file"));
  239. }
  240. private void removeFromIndex(String path) throws IOException {
  241. final DirCache dirc = db.lockDirCache();
  242. final DirCacheEditor edit = dirc.editor();
  243. edit.add(new DirCacheEditor.DeletePath(path));
  244. if (!edit.commit())
  245. throw new IOException("could not commit");
  246. }
  247. private void assumeUnchanged(String path) throws IOException {
  248. final DirCache dirc = db.lockDirCache();
  249. final DirCacheEntry ent = dirc.getEntry(path);
  250. if (ent != null)
  251. ent.setAssumeValid(true);
  252. dirc.write();
  253. if (!dirc.commit())
  254. throw new IOException("could not commit");
  255. }
  256. }