Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DiffFormatterTest.java 12KB

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