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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.treewalk.FileTreeIterator;
  49. public class IndexDiffTest extends RepositoryTestCase {
  50. public void testAdded() throws IOException {
  51. GitIndex index = new GitIndex(db);
  52. writeTrashFile("file1", "file1");
  53. writeTrashFile("dir/subfile", "dir/subfile");
  54. Tree tree = new Tree(db);
  55. tree.setId(insertTree(tree));
  56. index.add(trash, new File(trash, "file1"));
  57. index.add(trash, new File(trash, "dir/subfile"));
  58. index.write();
  59. FileTreeIterator iterator = new FileTreeIterator(db);
  60. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  61. diff.diff();
  62. assertEquals(2, diff.getAdded().size());
  63. assertTrue(diff.getAdded().contains("file1"));
  64. assertTrue(diff.getAdded().contains("dir/subfile"));
  65. assertEquals(0, diff.getChanged().size());
  66. assertEquals(0, diff.getModified().size());
  67. assertEquals(0, diff.getRemoved().size());
  68. }
  69. public void testRemoved() throws IOException {
  70. writeTrashFile("file2", "file2");
  71. writeTrashFile("dir/file3", "dir/file3");
  72. Tree tree = new Tree(db);
  73. tree.addFile("file2");
  74. tree.addFile("dir/file3");
  75. assertEquals(2, tree.memberCount());
  76. tree.findBlobMember("file2").setId(ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
  77. Tree tree2 = (Tree) tree.findTreeMember("dir");
  78. tree2.findBlobMember("file3").setId(ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
  79. tree2.setId(insertTree(tree2));
  80. tree.setId(insertTree(tree));
  81. FileTreeIterator iterator = new FileTreeIterator(db);
  82. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  83. diff.diff();
  84. assertEquals(2, diff.getRemoved().size());
  85. assertTrue(diff.getRemoved().contains("file2"));
  86. assertTrue(diff.getRemoved().contains("dir/file3"));
  87. assertEquals(0, diff.getChanged().size());
  88. assertEquals(0, diff.getModified().size());
  89. assertEquals(0, diff.getAdded().size());
  90. }
  91. public void testModified() throws IOException {
  92. GitIndex index = new GitIndex(db);
  93. index.add(trash, writeTrashFile("file2", "file2"));
  94. index.add(trash, writeTrashFile("dir/file3", "dir/file3"));
  95. index.write();
  96. writeTrashFile("dir/file3", "changed");
  97. Tree tree = new Tree(db);
  98. tree.addFile("file2").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  99. tree.addFile("dir/file3").setId(ObjectId.fromString("0123456789012345678901234567890123456789"));
  100. assertEquals(2, tree.memberCount());
  101. Tree tree2 = (Tree) tree.findTreeMember("dir");
  102. tree2.setId(insertTree(tree2));
  103. tree.setId(insertTree(tree));
  104. FileTreeIterator iterator = new FileTreeIterator(db);
  105. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  106. diff.diff();
  107. assertEquals(2, diff.getChanged().size());
  108. assertTrue(diff.getChanged().contains("file2"));
  109. assertTrue(diff.getChanged().contains("dir/file3"));
  110. assertEquals(1, diff.getModified().size());
  111. assertTrue(diff.getModified().contains("dir/file3"));
  112. assertEquals(0, diff.getAdded().size());
  113. assertEquals(0, diff.getRemoved().size());
  114. assertEquals(0, diff.getMissing().size());
  115. }
  116. public void testUnchangedSimple() throws IOException {
  117. GitIndex index = new GitIndex(db);
  118. index.add(trash, writeTrashFile("a.b", "a.b"));
  119. index.add(trash, writeTrashFile("a.c", "a.c"));
  120. index.add(trash, writeTrashFile("a=c", "a=c"));
  121. index.add(trash, writeTrashFile("a=d", "a=d"));
  122. index.write();
  123. Tree tree = new Tree(db);
  124. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  125. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  126. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  127. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  128. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  129. tree.setId(insertTree(tree));
  130. FileTreeIterator iterator = new FileTreeIterator(db);
  131. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  132. diff.diff();
  133. assertEquals(0, diff.getChanged().size());
  134. assertEquals(0, diff.getAdded().size());
  135. assertEquals(0, diff.getRemoved().size());
  136. assertEquals(0, diff.getMissing().size());
  137. assertEquals(0, diff.getModified().size());
  138. }
  139. /**
  140. * This test has both files and directories that involve
  141. * the tricky ordering used by Git.
  142. *
  143. * @throws IOException
  144. */
  145. public void testUnchangedComplex() throws IOException {
  146. GitIndex index = new GitIndex(db);
  147. index.add(trash, writeTrashFile("a.b", "a.b"));
  148. index.add(trash, writeTrashFile("a.c", "a.c"));
  149. index.add(trash, writeTrashFile("a/b.b/b", "a/b.b/b"));
  150. index.add(trash, writeTrashFile("a/b", "a/b"));
  151. index.add(trash, writeTrashFile("a/c", "a/c"));
  152. index.add(trash, writeTrashFile("a=c", "a=c"));
  153. index.add(trash, writeTrashFile("a=d", "a=d"));
  154. index.write();
  155. Tree tree = new Tree(db);
  156. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  157. tree.addFile("a.b").setId(ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  158. tree.addFile("a.c").setId(ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  159. tree.addFile("a/b.b/b").setId(ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
  160. tree.addFile("a/b").setId(ObjectId.fromString("db89c972fc57862eae378f45b74aca228037d415"));
  161. tree.addFile("a/c").setId(ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
  162. tree.addFile("a=c").setId(ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  163. tree.addFile("a=d").setId(ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  164. Tree tree3 = (Tree) tree.findTreeMember("a/b.b");
  165. tree3.setId(insertTree(tree3));
  166. Tree tree2 = (Tree) tree.findTreeMember("a");
  167. tree2.setId(insertTree(tree2));
  168. tree.setId(insertTree(tree));
  169. FileTreeIterator iterator = new FileTreeIterator(db);
  170. IndexDiff diff = new IndexDiff(db, tree.getId(), iterator);
  171. diff.diff();
  172. assertEquals(0, diff.getChanged().size());
  173. assertEquals(0, diff.getAdded().size());
  174. assertEquals(0, diff.getRemoved().size());
  175. assertEquals(0, diff.getMissing().size());
  176. assertEquals(0, diff.getModified().size());
  177. }
  178. private ObjectId insertTree(Tree tree) throws IOException {
  179. ObjectInserter oi = db.newObjectInserter();
  180. try {
  181. ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
  182. oi.flush();
  183. return id;
  184. } finally {
  185. oi.release();
  186. }
  187. }
  188. }