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.3KB

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