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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 org.eclipse.jgit.diff.DiffEntry.ChangeType;
  46. import org.eclipse.jgit.junit.TestRepository;
  47. import org.eclipse.jgit.lib.FileMode;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.RepositoryTestCase;
  50. import org.eclipse.jgit.patch.FileHeader;
  51. import org.eclipse.jgit.patch.HunkHeader;
  52. import org.eclipse.jgit.util.RawParseUtils;
  53. import org.eclipse.jgit.util.io.DisabledOutputStream;
  54. import org.junit.After;
  55. import org.junit.Before;
  56. import org.junit.Test;
  57. public class DiffFormatterTest extends RepositoryTestCase {
  58. private static final String DIFF = "diff --git ";
  59. private static final String REGULAR_FILE = "100644";
  60. private static final String GITLINK = "160000";
  61. private static final String PATH_A = "src/a";
  62. private static final String PATH_B = "src/b";
  63. private DiffFormatter df;
  64. private TestRepository testDb;
  65. @Override
  66. @Before
  67. public void setUp() throws Exception {
  68. super.setUp();
  69. testDb = new TestRepository(db);
  70. df = new DiffFormatter(DisabledOutputStream.INSTANCE);
  71. df.setRepository(db);
  72. df.setAbbreviationLength(8);
  73. }
  74. @Override
  75. @After
  76. public void tearDown() throws Exception {
  77. if (df != null)
  78. df.release();
  79. super.tearDown();
  80. }
  81. @Test
  82. public void testCreateFileHeader_Add() throws Exception {
  83. ObjectId adId = blob("a\nd\n");
  84. DiffEntry ent = DiffEntry.add("FOO", adId);
  85. FileHeader fh = df.toFileHeader(ent);
  86. String diffHeader = "diff --git a/FOO b/FOO\n" //
  87. + "new file mode " + REGULAR_FILE + "\n"
  88. + "index "
  89. + ObjectId.zeroId().abbreviate(8).name()
  90. + ".."
  91. + adId.abbreviate(8).name() + "\n" //
  92. + "--- /dev/null\n"//
  93. + "+++ b/FOO\n";
  94. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  95. assertEquals(0, fh.getStartOffset());
  96. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  97. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  98. assertEquals(1, fh.getHunks().size());
  99. HunkHeader hh = fh.getHunks().get(0);
  100. assertEquals(1, hh.toEditList().size());
  101. EditList el = hh.toEditList();
  102. assertEquals(1, el.size());
  103. Edit e = el.get(0);
  104. assertEquals(0, e.getBeginA());
  105. assertEquals(0, e.getEndA());
  106. assertEquals(0, e.getBeginB());
  107. assertEquals(2, e.getEndB());
  108. assertEquals(Edit.Type.INSERT, e.getType());
  109. }
  110. @Test
  111. public void testCreateFileHeader_Delete() throws Exception {
  112. ObjectId adId = blob("a\nd\n");
  113. DiffEntry ent = DiffEntry.delete("FOO", adId);
  114. FileHeader fh = df.toFileHeader(ent);
  115. String diffHeader = "diff --git a/FOO b/FOO\n" //
  116. + "deleted file mode " + REGULAR_FILE + "\n"
  117. + "index "
  118. + adId.abbreviate(8).name()
  119. + ".."
  120. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  121. + "--- a/FOO\n"//
  122. + "+++ /dev/null\n";
  123. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  124. assertEquals(0, fh.getStartOffset());
  125. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  126. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  127. assertEquals(1, fh.getHunks().size());
  128. HunkHeader hh = fh.getHunks().get(0);
  129. assertEquals(1, hh.toEditList().size());
  130. EditList el = hh.toEditList();
  131. assertEquals(1, el.size());
  132. Edit e = el.get(0);
  133. assertEquals(0, e.getBeginA());
  134. assertEquals(2, e.getEndA());
  135. assertEquals(0, e.getBeginB());
  136. assertEquals(0, e.getEndB());
  137. assertEquals(Edit.Type.DELETE, e.getType());
  138. }
  139. @Test
  140. public void testCreateFileHeader_Modify() throws Exception {
  141. ObjectId adId = blob("a\nd\n");
  142. ObjectId abcdId = blob("a\nb\nc\nd\n");
  143. String diffHeader = makeDiffHeader(PATH_A, PATH_A, adId, abcdId);
  144. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  145. DiffEntry abcd = DiffEntry.add(PATH_A, abcdId);
  146. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  147. FileHeader fh = df.toFileHeader(mod);
  148. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  149. assertEquals(0, fh.getStartOffset());
  150. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  151. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  152. assertEquals(1, fh.getHunks().size());
  153. HunkHeader hh = fh.getHunks().get(0);
  154. assertEquals(1, hh.toEditList().size());
  155. EditList el = hh.toEditList();
  156. assertEquals(1, el.size());
  157. Edit e = el.get(0);
  158. assertEquals(1, e.getBeginA());
  159. assertEquals(1, e.getEndA());
  160. assertEquals(1, e.getBeginB());
  161. assertEquals(3, e.getEndB());
  162. assertEquals(Edit.Type.INSERT, e.getType());
  163. }
  164. @Test
  165. public void testCreateFileHeader_Binary() throws Exception {
  166. ObjectId adId = blob("a\nd\n");
  167. ObjectId binId = blob("a\nb\nc\n\0\0\0\0d\n");
  168. String diffHeader = makeDiffHeader(PATH_A, PATH_B, adId, binId)
  169. + "Binary files differ\n";
  170. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  171. DiffEntry abcd = DiffEntry.add(PATH_B, binId);
  172. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  173. FileHeader fh = df.toFileHeader(mod);
  174. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  175. assertEquals(FileHeader.PatchType.BINARY, fh.getPatchType());
  176. assertEquals(1, fh.getHunks().size());
  177. HunkHeader hh = fh.getHunks().get(0);
  178. assertEquals(0, hh.toEditList().size());
  179. }
  180. @Test
  181. public void testCreateFileHeader_GitLink() throws Exception {
  182. ObjectId aId = blob("a\n");
  183. ObjectId bId = blob("b\n");
  184. String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId,
  185. GITLINK, REGULAR_FILE)
  186. + "-Subproject commit " + aId.name() + "\n";
  187. DiffEntry ad = DiffEntry.delete(PATH_A, aId);
  188. ad.oldMode = FileMode.GITLINK;
  189. DiffEntry abcd = DiffEntry.add(PATH_A, bId);
  190. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  191. FileHeader fh = df.toFileHeader(mod);
  192. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  193. assertEquals(1, fh.getHunks().size());
  194. HunkHeader hh = fh.getHunks().get(0);
  195. assertEquals(0, hh.toEditList().size());
  196. }
  197. private String makeDiffHeader(String pathA, String pathB, ObjectId aId,
  198. ObjectId bId) {
  199. String a = aId.abbreviate(8).name();
  200. String b = bId.abbreviate(8).name();
  201. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  202. "index " + a + ".." + b + " " + REGULAR_FILE + "\n" + //
  203. "--- a/" + pathA + "\n" + //
  204. "+++ b/" + pathB + "\n";
  205. }
  206. private String makeDiffHeaderModeChange(String pathA, String pathB,
  207. ObjectId aId, ObjectId bId, String modeA, String modeB) {
  208. String a = aId.abbreviate(8).name();
  209. String b = bId.abbreviate(8).name();
  210. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  211. "old mode " + modeA + "\n" + //
  212. "new mode " + modeB + "\n" + //
  213. "index " + a + ".." + b + "\n" + //
  214. "--- a/" + pathA + "\n" + //
  215. "+++ b/" + pathB + "\n";
  216. }
  217. private ObjectId blob(String content) throws Exception {
  218. return testDb.blob(content).copy();
  219. }
  220. }