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.

DiffCommandTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.io.OutputStream;
  51. import java.util.List;
  52. import org.eclipse.jgit.diff.DiffEntry;
  53. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.lib.RepositoryTestCase;
  57. import org.eclipse.jgit.revwalk.RevWalk;
  58. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  59. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  60. import org.eclipse.jgit.treewalk.filter.PathFilter;
  61. import org.junit.Test;
  62. public class DiffCommandTest extends RepositoryTestCase {
  63. @Test
  64. public void testDiffModified() throws Exception {
  65. write(new File(db.getWorkTree(), "test.txt"), "test");
  66. File folder = new File(db.getWorkTree(), "folder");
  67. folder.mkdir();
  68. write(new File(folder, "folder.txt"), "folder");
  69. Git git = new Git(db);
  70. git.add().addFilepattern(".").call();
  71. git.commit().setMessage("Initial commit").call();
  72. write(new File(folder, "folder.txt"), "folder change");
  73. OutputStream out = new ByteArrayOutputStream();
  74. List<DiffEntry> entries = git.diff().setOutputStream(out).call();
  75. assertEquals(1, entries.size());
  76. assertEquals(ChangeType.MODIFY, entries.get(0)
  77. .getChangeType());
  78. assertEquals("folder/folder.txt", entries.get(0)
  79. .getOldPath());
  80. assertEquals("folder/folder.txt", entries.get(0)
  81. .getNewPath());
  82. String actual = out.toString();
  83. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  84. + "index 0119635..95c4c65 100644\n"
  85. + "--- a/folder/folder.txt\n"
  86. + "+++ b/folder/folder.txt\n"
  87. + "@@ -1 +1 @@\n"
  88. + "-folder\n"
  89. + "\\ No newline at end of file\n"
  90. + "+folder change\n"
  91. + "\\ No newline at end of file\n";
  92. assertEquals(expected.toString(), actual);
  93. }
  94. @Test
  95. public void testDiffCached() throws Exception {
  96. write(new File(db.getWorkTree(), "test.txt"), "test");
  97. File folder = new File(db.getWorkTree(), "folder");
  98. folder.mkdir();
  99. Git git = new Git(db);
  100. git.add().addFilepattern(".").call();
  101. git.commit().setMessage("Initial commit").call();
  102. write(new File(folder, "folder.txt"), "folder");
  103. git.add().addFilepattern(".").call();
  104. OutputStream out = new ByteArrayOutputStream();
  105. List<DiffEntry> entries = git.diff().setOutputStream(out)
  106. .setCached(true).call();
  107. assertEquals(1, entries.size());
  108. assertEquals(ChangeType.ADD, entries.get(0)
  109. .getChangeType());
  110. assertEquals("/dev/null", entries.get(0)
  111. .getOldPath());
  112. assertEquals("folder/folder.txt", entries.get(0)
  113. .getNewPath());
  114. String actual = out.toString();
  115. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  116. + "new file mode 100644\n"
  117. + "index 0000000..0119635\n"
  118. + "--- /dev/null\n"
  119. + "+++ b/folder/folder.txt\n"
  120. + "@@ -0,0 +1 @@\n"
  121. + "+folder\n"
  122. + "\\ No newline at end of file\n";
  123. assertEquals(expected.toString(), actual);
  124. }
  125. @Test
  126. public void testDiffTwoCommits() throws Exception {
  127. write(new File(db.getWorkTree(), "test.txt"), "test");
  128. File folder = new File(db.getWorkTree(), "folder");
  129. folder.mkdir();
  130. write(new File(folder, "folder.txt"), "folder");
  131. Git git = new Git(db);
  132. git.add().addFilepattern(".").call();
  133. git.commit().setMessage("Initial commit").call();
  134. write(new File(folder, "folder.txt"), "folder change");
  135. git.add().addFilepattern(".").call();
  136. git.commit().setMessage("second commit").call();
  137. write(new File(folder, "folder.txt"), "second folder change");
  138. git.add().addFilepattern(".").call();
  139. git.commit().setMessage("third commit").call();
  140. // bad filter
  141. DiffCommand diff = git.diff().setShowNameAndStatusOnly(true)
  142. .setPathFilter(PathFilter.create("test.txt"))
  143. .setOldTree(getTreeIterator("HEAD^^"))
  144. .setNewTree(getTreeIterator("HEAD^"));
  145. List<DiffEntry> entries = diff.call();
  146. assertEquals(0, entries.size());
  147. // no filter, two commits
  148. OutputStream out = new ByteArrayOutputStream();
  149. diff = git.diff().setOutputStream(out)
  150. .setOldTree(getTreeIterator("HEAD^^"))
  151. .setNewTree(getTreeIterator("HEAD^"));
  152. entries = diff.call();
  153. assertEquals(1, entries.size());
  154. assertEquals(ChangeType.MODIFY, entries.get(0).getChangeType());
  155. assertEquals("folder/folder.txt", entries.get(0).getOldPath());
  156. assertEquals("folder/folder.txt", entries.get(0).getNewPath());
  157. String actual = out.toString();
  158. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  159. + "index 0119635..95c4c65 100644\n"
  160. + "--- a/folder/folder.txt\n"
  161. + "+++ b/folder/folder.txt\n"
  162. + "@@ -1 +1 @@\n"
  163. + "-folder\n"
  164. + "\\ No newline at end of file\n"
  165. + "+folder change\n"
  166. + "\\ No newline at end of file\n";
  167. assertEquals(expected.toString(), actual);
  168. }
  169. @Test
  170. public void testDiffWithPrefixes() throws Exception {
  171. write(new File(db.getWorkTree(), "test.txt"), "test");
  172. Git git = new Git(db);
  173. git.add().addFilepattern(".").call();
  174. git.commit().setMessage("Initial commit").call();
  175. write(new File(db.getWorkTree(), "test.txt"), "test change");
  176. OutputStream out = new ByteArrayOutputStream();
  177. git.diff().setOutputStream(out).setSourcePrefix("old/")
  178. .setDestinationPrefix("new/")
  179. .call();
  180. String actual = out.toString();
  181. String expected = "diff --git old/test.txt new/test.txt\n"
  182. + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n"
  183. + "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n"
  184. + "\\ No newline at end of file\n" + "+test change\n"
  185. + "\\ No newline at end of file\n";
  186. assertEquals(expected.toString(), actual);
  187. }
  188. @Test
  189. public void testDiffWithNegativeLineCount() throws Exception {
  190. write(new File(db.getWorkTree(), "test.txt"),
  191. "0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
  192. Git git = new Git(db);
  193. git.add().addFilepattern(".").call();
  194. git.commit().setMessage("Initial commit").call();
  195. write(new File(db.getWorkTree(), "test.txt"),
  196. "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9");
  197. OutputStream out = new ByteArrayOutputStream();
  198. git.diff().setOutputStream(out).setContextLines(1)
  199. .call();
  200. String actual = out.toString();
  201. String expected = "diff --git a/test.txt b/test.txt\n"
  202. + "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n"
  203. + "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n"
  204. + "+4a\n" + " 5\n";
  205. assertEquals(expected.toString(), actual);
  206. }
  207. @Test
  208. public void testNoOutputStreamSet() throws Exception {
  209. File file = writeTrashFile("test.txt", "a");
  210. assertTrue(file.setLastModified(file.lastModified() - 5000));
  211. Git git = new Git(db);
  212. git.add().addFilepattern(".").call();
  213. write(file, "b");
  214. List<DiffEntry> diffs = git.diff().call();
  215. assertNotNull(diffs);
  216. assertEquals(1, diffs.size());
  217. DiffEntry diff = diffs.get(0);
  218. assertEquals(ChangeType.MODIFY, diff.getChangeType());
  219. assertEquals("test.txt", diff.getOldPath());
  220. assertEquals("test.txt", diff.getNewPath());
  221. }
  222. private AbstractTreeIterator getTreeIterator(String name)
  223. throws IOException {
  224. final ObjectId id = db.resolve(name);
  225. if (id == null)
  226. throw new IllegalArgumentException(name);
  227. final CanonicalTreeParser p = new CanonicalTreeParser();
  228. final ObjectReader or = db.newObjectReader();
  229. try {
  230. p.reset(or, new RevWalk(db).parseTree(id));
  231. return p;
  232. } finally {
  233. or.release();
  234. }
  235. }
  236. }