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

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