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.

DiffFormatterTest.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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.diff;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.BufferedOutputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.File;
  48. import org.eclipse.jgit.api.Git;
  49. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  50. import org.eclipse.jgit.dircache.DirCacheIterator;
  51. import org.eclipse.jgit.junit.TestRepository;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.RepositoryTestCase;
  55. import org.eclipse.jgit.patch.FileHeader;
  56. import org.eclipse.jgit.patch.HunkHeader;
  57. import org.eclipse.jgit.treewalk.FileTreeIterator;
  58. import org.eclipse.jgit.treewalk.filter.PathFilter;
  59. import org.eclipse.jgit.util.RawParseUtils;
  60. import org.eclipse.jgit.util.io.DisabledOutputStream;
  61. import org.junit.After;
  62. import org.junit.Before;
  63. import org.junit.Test;
  64. public class DiffFormatterTest extends RepositoryTestCase {
  65. private static final String DIFF = "diff --git ";
  66. private static final String REGULAR_FILE = "100644";
  67. private static final String GITLINK = "160000";
  68. private static final String PATH_A = "src/a";
  69. private static final String PATH_B = "src/b";
  70. private DiffFormatter df;
  71. private TestRepository testDb;
  72. @Override
  73. @Before
  74. public void setUp() throws Exception {
  75. super.setUp();
  76. testDb = new TestRepository(db);
  77. df = new DiffFormatter(DisabledOutputStream.INSTANCE);
  78. df.setRepository(db);
  79. df.setAbbreviationLength(8);
  80. }
  81. @Override
  82. @After
  83. public void tearDown() throws Exception {
  84. if (df != null)
  85. df.release();
  86. super.tearDown();
  87. }
  88. @Test
  89. public void testCreateFileHeader_Add() throws Exception {
  90. ObjectId adId = blob("a\nd\n");
  91. DiffEntry ent = DiffEntry.add("FOO", adId);
  92. FileHeader fh = df.toFileHeader(ent);
  93. String diffHeader = "diff --git a/FOO b/FOO\n" //
  94. + "new file mode " + REGULAR_FILE + "\n"
  95. + "index "
  96. + ObjectId.zeroId().abbreviate(8).name()
  97. + ".."
  98. + adId.abbreviate(8).name() + "\n" //
  99. + "--- /dev/null\n"//
  100. + "+++ b/FOO\n";
  101. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  102. assertEquals(0, fh.getStartOffset());
  103. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  104. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  105. assertEquals(1, fh.getHunks().size());
  106. HunkHeader hh = fh.getHunks().get(0);
  107. assertEquals(1, hh.toEditList().size());
  108. EditList el = hh.toEditList();
  109. assertEquals(1, el.size());
  110. Edit e = el.get(0);
  111. assertEquals(0, e.getBeginA());
  112. assertEquals(0, e.getEndA());
  113. assertEquals(0, e.getBeginB());
  114. assertEquals(2, e.getEndB());
  115. assertEquals(Edit.Type.INSERT, e.getType());
  116. }
  117. @Test
  118. public void testCreateFileHeader_Delete() throws Exception {
  119. ObjectId adId = blob("a\nd\n");
  120. DiffEntry ent = DiffEntry.delete("FOO", adId);
  121. FileHeader fh = df.toFileHeader(ent);
  122. String diffHeader = "diff --git a/FOO b/FOO\n" //
  123. + "deleted file mode " + REGULAR_FILE + "\n"
  124. + "index "
  125. + adId.abbreviate(8).name()
  126. + ".."
  127. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  128. + "--- a/FOO\n"//
  129. + "+++ /dev/null\n";
  130. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  131. assertEquals(0, fh.getStartOffset());
  132. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  133. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  134. assertEquals(1, fh.getHunks().size());
  135. HunkHeader hh = fh.getHunks().get(0);
  136. assertEquals(1, hh.toEditList().size());
  137. EditList el = hh.toEditList();
  138. assertEquals(1, el.size());
  139. Edit e = el.get(0);
  140. assertEquals(0, e.getBeginA());
  141. assertEquals(2, e.getEndA());
  142. assertEquals(0, e.getBeginB());
  143. assertEquals(0, e.getEndB());
  144. assertEquals(Edit.Type.DELETE, e.getType());
  145. }
  146. @Test
  147. public void testCreateFileHeader_Modify() throws Exception {
  148. ObjectId adId = blob("a\nd\n");
  149. ObjectId abcdId = blob("a\nb\nc\nd\n");
  150. String diffHeader = makeDiffHeader(PATH_A, PATH_A, adId, abcdId);
  151. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  152. DiffEntry abcd = DiffEntry.add(PATH_A, abcdId);
  153. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  154. FileHeader fh = df.toFileHeader(mod);
  155. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  156. assertEquals(0, fh.getStartOffset());
  157. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  158. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  159. assertEquals(1, fh.getHunks().size());
  160. HunkHeader hh = fh.getHunks().get(0);
  161. assertEquals(1, hh.toEditList().size());
  162. EditList el = hh.toEditList();
  163. assertEquals(1, el.size());
  164. Edit e = el.get(0);
  165. assertEquals(1, e.getBeginA());
  166. assertEquals(1, e.getEndA());
  167. assertEquals(1, e.getBeginB());
  168. assertEquals(3, e.getEndB());
  169. assertEquals(Edit.Type.INSERT, e.getType());
  170. }
  171. @Test
  172. public void testCreateFileHeader_Binary() throws Exception {
  173. ObjectId adId = blob("a\nd\n");
  174. ObjectId binId = blob("a\nb\nc\n\0\0\0\0d\n");
  175. String diffHeader = makeDiffHeader(PATH_A, PATH_B, adId, binId)
  176. + "Binary files differ\n";
  177. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  178. DiffEntry abcd = DiffEntry.add(PATH_B, binId);
  179. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  180. FileHeader fh = df.toFileHeader(mod);
  181. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  182. assertEquals(FileHeader.PatchType.BINARY, fh.getPatchType());
  183. assertEquals(1, fh.getHunks().size());
  184. HunkHeader hh = fh.getHunks().get(0);
  185. assertEquals(0, hh.toEditList().size());
  186. }
  187. @Test
  188. public void testCreateFileHeader_GitLink() throws Exception {
  189. ObjectId aId = blob("a\n");
  190. ObjectId bId = blob("b\n");
  191. String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId,
  192. GITLINK, REGULAR_FILE)
  193. + "-Subproject commit " + aId.name() + "\n";
  194. DiffEntry ad = DiffEntry.delete(PATH_A, aId);
  195. ad.oldMode = FileMode.GITLINK;
  196. DiffEntry abcd = DiffEntry.add(PATH_A, bId);
  197. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  198. FileHeader fh = df.toFileHeader(mod);
  199. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  200. assertEquals(1, fh.getHunks().size());
  201. HunkHeader hh = fh.getHunks().get(0);
  202. assertEquals(0, hh.toEditList().size());
  203. }
  204. @Test
  205. public void testDiff() throws Exception {
  206. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  207. File folder = new File(db.getDirectory().getParent(), "folder");
  208. folder.mkdir();
  209. write(new File(folder, "folder.txt"), "folder");
  210. Git git = new Git(db);
  211. git.add().addFilepattern(".").call();
  212. git.commit().setMessage("Initial commit").call();
  213. write(new File(folder, "folder.txt"), "folder change");
  214. ByteArrayOutputStream os = new ByteArrayOutputStream();
  215. DiffFormatter df = new DiffFormatter(new BufferedOutputStream(os));
  216. df.setRepository(db);
  217. df.setPathFilter(PathFilter.create("folder"));
  218. DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
  219. FileTreeIterator newTree = new FileTreeIterator(db);
  220. df.format(oldTree, newTree);
  221. df.flush();
  222. String actual = os.toString();
  223. String expected =
  224. "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  225. + "index 0119635..95c4c65 100644\n"
  226. + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
  227. + "@@ -1 +1 @@\n" + "-folder\n"
  228. + "\\ No newline at end of file\n" + "+folder change\n"
  229. + "\\ No newline at end of file\n";
  230. assertEquals(expected.toString(), actual);
  231. }
  232. private String makeDiffHeader(String pathA, String pathB, ObjectId aId,
  233. ObjectId bId) {
  234. String a = aId.abbreviate(8).name();
  235. String b = bId.abbreviate(8).name();
  236. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  237. "index " + a + ".." + b + " " + REGULAR_FILE + "\n" + //
  238. "--- a/" + pathA + "\n" + //
  239. "+++ b/" + pathB + "\n";
  240. }
  241. private String makeDiffHeaderModeChange(String pathA, String pathB,
  242. ObjectId aId, ObjectId bId, String modeA, String modeB) {
  243. String a = aId.abbreviate(8).name();
  244. String b = bId.abbreviate(8).name();
  245. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  246. "old mode " + modeA + "\n" + //
  247. "new mode " + modeB + "\n" + //
  248. "index " + a + ".." + b + "\n" + //
  249. "--- a/" + pathA + "\n" + //
  250. "+++ b/" + pathB + "\n";
  251. }
  252. private ObjectId blob(String content) throws Exception {
  253. return testDb.blob(content).copy();
  254. }
  255. }