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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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.AnyObjectId;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.patch.FileHeader;
  57. import org.eclipse.jgit.patch.HunkHeader;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.treewalk.FileTreeIterator;
  60. import org.eclipse.jgit.treewalk.filter.PathFilter;
  61. import org.eclipse.jgit.util.FileUtils;
  62. import org.eclipse.jgit.util.RawParseUtils;
  63. import org.eclipse.jgit.util.io.DisabledOutputStream;
  64. import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
  65. import org.junit.After;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. public class DiffFormatterTest extends RepositoryTestCase {
  69. private static final String DIFF = "diff --git ";
  70. private static final String REGULAR_FILE = "100644";
  71. private static final String GITLINK = "160000";
  72. private static final String PATH_A = "src/a";
  73. private static final String PATH_B = "src/b";
  74. private DiffFormatter df;
  75. private TestRepository<Repository> testDb;
  76. @Override
  77. @Before
  78. public void setUp() throws Exception {
  79. super.setUp();
  80. testDb = new TestRepository<Repository>(db);
  81. df = new DiffFormatter(DisabledOutputStream.INSTANCE);
  82. df.setRepository(db);
  83. df.setAbbreviationLength(8);
  84. }
  85. @Override
  86. @After
  87. public void tearDown() throws Exception {
  88. if (df != null) {
  89. df.close();
  90. }
  91. super.tearDown();
  92. }
  93. @Test
  94. public void testCreateFileHeader_Add() throws Exception {
  95. ObjectId adId = blob("a\nd\n");
  96. DiffEntry ent = DiffEntry.add("FOO", adId);
  97. FileHeader fh = df.toFileHeader(ent);
  98. String diffHeader = "diff --git a/FOO b/FOO\n" //
  99. + "new file mode " + REGULAR_FILE + "\n"
  100. + "index "
  101. + ObjectId.zeroId().abbreviate(8).name()
  102. + ".."
  103. + adId.abbreviate(8).name() + "\n" //
  104. + "--- /dev/null\n"//
  105. + "+++ b/FOO\n";
  106. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  107. assertEquals(0, fh.getStartOffset());
  108. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  109. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  110. assertEquals(1, fh.getHunks().size());
  111. HunkHeader hh = fh.getHunks().get(0);
  112. assertEquals(1, hh.toEditList().size());
  113. EditList el = hh.toEditList();
  114. assertEquals(1, el.size());
  115. Edit e = el.get(0);
  116. assertEquals(0, e.getBeginA());
  117. assertEquals(0, e.getEndA());
  118. assertEquals(0, e.getBeginB());
  119. assertEquals(2, e.getEndB());
  120. assertEquals(Edit.Type.INSERT, e.getType());
  121. }
  122. @Test
  123. public void testCreateFileHeader_Delete() throws Exception {
  124. ObjectId adId = blob("a\nd\n");
  125. DiffEntry ent = DiffEntry.delete("FOO", adId);
  126. FileHeader fh = df.toFileHeader(ent);
  127. String diffHeader = "diff --git a/FOO b/FOO\n" //
  128. + "deleted file mode " + REGULAR_FILE + "\n"
  129. + "index "
  130. + adId.abbreviate(8).name()
  131. + ".."
  132. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  133. + "--- a/FOO\n"//
  134. + "+++ /dev/null\n";
  135. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  136. assertEquals(0, fh.getStartOffset());
  137. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  138. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  139. assertEquals(1, fh.getHunks().size());
  140. HunkHeader hh = fh.getHunks().get(0);
  141. assertEquals(1, hh.toEditList().size());
  142. EditList el = hh.toEditList();
  143. assertEquals(1, el.size());
  144. Edit e = el.get(0);
  145. assertEquals(0, e.getBeginA());
  146. assertEquals(2, e.getEndA());
  147. assertEquals(0, e.getBeginB());
  148. assertEquals(0, e.getEndB());
  149. assertEquals(Edit.Type.DELETE, e.getType());
  150. }
  151. @Test
  152. public void testCreateFileHeader_Modify() throws Exception {
  153. ObjectId adId = blob("a\nd\n");
  154. ObjectId abcdId = blob("a\nb\nc\nd\n");
  155. String diffHeader = makeDiffHeader(PATH_A, PATH_A, adId, abcdId);
  156. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  157. DiffEntry abcd = DiffEntry.add(PATH_A, abcdId);
  158. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  159. FileHeader fh = df.toFileHeader(mod);
  160. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  161. assertEquals(0, fh.getStartOffset());
  162. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  163. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  164. assertEquals(1, fh.getHunks().size());
  165. HunkHeader hh = fh.getHunks().get(0);
  166. assertEquals(1, hh.toEditList().size());
  167. EditList el = hh.toEditList();
  168. assertEquals(1, el.size());
  169. Edit e = el.get(0);
  170. assertEquals(1, e.getBeginA());
  171. assertEquals(1, e.getEndA());
  172. assertEquals(1, e.getBeginB());
  173. assertEquals(3, e.getEndB());
  174. assertEquals(Edit.Type.INSERT, e.getType());
  175. }
  176. @Test
  177. public void testCreateFileHeader_Binary() throws Exception {
  178. ObjectId adId = blob("a\nd\n");
  179. ObjectId binId = blob("a\nb\nc\n\0\0\0\0d\n");
  180. String diffHeader = makeDiffHeader(PATH_A, PATH_B, adId, binId)
  181. + "Binary files differ\n";
  182. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  183. DiffEntry abcd = DiffEntry.add(PATH_B, binId);
  184. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  185. FileHeader fh = df.toFileHeader(mod);
  186. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  187. assertEquals(FileHeader.PatchType.BINARY, fh.getPatchType());
  188. assertEquals(1, fh.getHunks().size());
  189. HunkHeader hh = fh.getHunks().get(0);
  190. assertEquals(0, hh.toEditList().size());
  191. }
  192. @Test
  193. public void testCreateFileHeader_GitLink() throws Exception {
  194. ObjectId aId = blob("a\n");
  195. ObjectId bId = blob("b\n");
  196. String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId,
  197. GITLINK, REGULAR_FILE);
  198. DiffEntry ad = DiffEntry.delete(PATH_A, aId);
  199. ad.oldMode = FileMode.GITLINK;
  200. DiffEntry abcd = DiffEntry.add(PATH_A, bId);
  201. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  202. FileHeader fh = df.toFileHeader(mod);
  203. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  204. assertEquals(1, fh.getHunks().size());
  205. HunkHeader hh = fh.getHunks().get(0);
  206. assertEquals(1, hh.toEditList().size());
  207. }
  208. @Test
  209. public void testCreateFileHeader_AddGitLink() throws Exception {
  210. ObjectId adId = blob("a\nd\n");
  211. DiffEntry ent = DiffEntry.add("FOO", adId);
  212. ent.newMode = FileMode.GITLINK;
  213. FileHeader fh = df.toFileHeader(ent);
  214. String diffHeader = "diff --git a/FOO b/FOO\n" //
  215. + "new file mode " + GITLINK + "\n"
  216. + "index "
  217. + ObjectId.zeroId().abbreviate(8).name()
  218. + ".."
  219. + adId.abbreviate(8).name() + "\n" //
  220. + "--- /dev/null\n"//
  221. + "+++ b/FOO\n";
  222. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  223. assertEquals(1, fh.getHunks().size());
  224. HunkHeader hh = fh.getHunks().get(0);
  225. EditList el = hh.toEditList();
  226. assertEquals(1, el.size());
  227. Edit e = el.get(0);
  228. assertEquals(0, e.getBeginA());
  229. assertEquals(0, e.getEndA());
  230. assertEquals(0, e.getBeginB());
  231. assertEquals(1, e.getEndB());
  232. assertEquals(Edit.Type.INSERT, e.getType());
  233. }
  234. @Test
  235. public void testCreateFileHeader_DeleteGitLink() throws Exception {
  236. ObjectId adId = blob("a\nd\n");
  237. DiffEntry ent = DiffEntry.delete("FOO", adId);
  238. ent.oldMode = FileMode.GITLINK;
  239. FileHeader fh = df.toFileHeader(ent);
  240. String diffHeader = "diff --git a/FOO b/FOO\n" //
  241. + "deleted file mode " + GITLINK + "\n"
  242. + "index "
  243. + adId.abbreviate(8).name()
  244. + ".."
  245. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  246. + "--- a/FOO\n"//
  247. + "+++ /dev/null\n";
  248. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  249. assertEquals(1, fh.getHunks().size());
  250. HunkHeader hh = fh.getHunks().get(0);
  251. EditList el = hh.toEditList();
  252. assertEquals(1, el.size());
  253. Edit e = el.get(0);
  254. assertEquals(0, e.getBeginA());
  255. assertEquals(1, e.getEndA());
  256. assertEquals(0, e.getBeginB());
  257. assertEquals(0, e.getEndB());
  258. assertEquals(Edit.Type.DELETE, e.getType());
  259. }
  260. @Test
  261. public void testCreateFileHeaderWithoutIndexLine() throws Exception {
  262. DiffEntry m = DiffEntry.modify(PATH_A);
  263. m.oldMode = FileMode.REGULAR_FILE;
  264. m.newMode = FileMode.EXECUTABLE_FILE;
  265. FileHeader fh = df.toFileHeader(m);
  266. String expected = DIFF + "a/src/a b/src/a\n" + //
  267. "old mode 100644\n" + //
  268. "new mode 100755\n";
  269. assertEquals(expected, fh.getScriptText());
  270. }
  271. @Test
  272. public void testCreateFileHeaderForRenameWithoutContentChange() throws Exception {
  273. DiffEntry a = DiffEntry.delete(PATH_A, ObjectId.zeroId());
  274. DiffEntry b = DiffEntry.add(PATH_B, ObjectId.zeroId());
  275. DiffEntry m = DiffEntry.pair(ChangeType.RENAME, a, b, 100);
  276. m.oldId = null;
  277. m.newId = null;
  278. FileHeader fh = df.toFileHeader(m);
  279. String expected = DIFF + "a/src/a b/src/b\n" + //
  280. "similarity index 100%\n" + //
  281. "rename from src/a\n" + //
  282. "rename to src/b\n";
  283. assertEquals(expected, fh.getScriptText());
  284. }
  285. @Test
  286. public void testCreateFileHeaderForRenameModeChange()
  287. throws Exception {
  288. DiffEntry a = DiffEntry.delete(PATH_A, ObjectId.zeroId());
  289. DiffEntry b = DiffEntry.add(PATH_B, ObjectId.zeroId());
  290. b.oldMode = FileMode.REGULAR_FILE;
  291. b.newMode = FileMode.EXECUTABLE_FILE;
  292. DiffEntry m = DiffEntry.pair(ChangeType.RENAME, a, b, 100);
  293. m.oldId = null;
  294. m.newId = null;
  295. FileHeader fh = df.toFileHeader(m);
  296. //@formatter:off
  297. String expected = DIFF + "a/src/a b/src/b\n" +
  298. "old mode 100644\n" +
  299. "new mode 100755\n" +
  300. "similarity index 100%\n" +
  301. "rename from src/a\n" +
  302. "rename to src/b\n";
  303. //@formatter:on
  304. assertEquals(expected, fh.getScriptText());
  305. }
  306. @Test
  307. public void testDiff() throws Exception {
  308. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  309. File folder = new File(db.getDirectory().getParent(), "folder");
  310. FileUtils.mkdir(folder);
  311. write(new File(folder, "folder.txt"), "folder");
  312. Git git = new Git(db);
  313. git.add().addFilepattern(".").call();
  314. git.commit().setMessage("Initial commit").call();
  315. write(new File(folder, "folder.txt"), "folder change");
  316. ByteArrayOutputStream os = new ByteArrayOutputStream();
  317. DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
  318. dfmt.setRepository(db);
  319. dfmt.setPathFilter(PathFilter.create("folder"));
  320. DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
  321. FileTreeIterator newTree = new FileTreeIterator(db);
  322. dfmt.format(oldTree, newTree);
  323. dfmt.flush();
  324. String actual = os.toString("UTF-8");
  325. String expected =
  326. "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  327. + "index 0119635..95c4c65 100644\n"
  328. + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
  329. + "@@ -1 +1 @@\n" + "-folder\n"
  330. + "\\ No newline at end of file\n" + "+folder change\n"
  331. + "\\ No newline at end of file\n";
  332. assertEquals(expected, actual);
  333. }
  334. @Test
  335. public void testDiffRootNullToTree() throws Exception {
  336. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  337. File folder = new File(db.getDirectory().getParent(), "folder");
  338. FileUtils.mkdir(folder);
  339. write(new File(folder, "folder.txt"), "folder");
  340. Git git = new Git(db);
  341. git.add().addFilepattern(".").call();
  342. RevCommit commit = git.commit().setMessage("Initial commit").call();
  343. write(new File(folder, "folder.txt"), "folder change");
  344. ByteArrayOutputStream os = new ByteArrayOutputStream();
  345. DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
  346. dfmt.setRepository(db);
  347. dfmt.setPathFilter(PathFilter.create("folder"));
  348. dfmt.format(null, commit.getTree().getId());
  349. dfmt.flush();
  350. String actual = os.toString("UTF-8");
  351. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  352. + "new file mode 100644\n"
  353. + "index 0000000..0119635\n"
  354. + "--- /dev/null\n"
  355. + "+++ b/folder/folder.txt\n"
  356. + "@@ -0,0 +1 @@\n"
  357. + "+folder\n"
  358. + "\\ No newline at end of file\n";
  359. assertEquals(expected, actual);
  360. }
  361. @Test
  362. public void testDiffRootTreeToNull() throws Exception {
  363. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  364. File folder = new File(db.getDirectory().getParent(), "folder");
  365. FileUtils.mkdir(folder);
  366. write(new File(folder, "folder.txt"), "folder");
  367. Git git = new Git(db);
  368. git.add().addFilepattern(".").call();
  369. RevCommit commit = git.commit().setMessage("Initial commit").call();
  370. write(new File(folder, "folder.txt"), "folder change");
  371. ByteArrayOutputStream os = new ByteArrayOutputStream();
  372. DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
  373. dfmt.setRepository(db);
  374. dfmt.setPathFilter(PathFilter.create("folder"));
  375. dfmt.format(commit.getTree().getId(), null);
  376. dfmt.flush();
  377. String actual = os.toString("UTF-8");
  378. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  379. + "deleted file mode 100644\n"
  380. + "index 0119635..0000000\n"
  381. + "--- a/folder/folder.txt\n"
  382. + "+++ /dev/null\n"
  383. + "@@ -1 +0,0 @@\n"
  384. + "-folder\n"
  385. + "\\ No newline at end of file\n";
  386. assertEquals(expected, actual);
  387. }
  388. @Test
  389. public void testDiffNullToNull() throws Exception {
  390. ByteArrayOutputStream os = new ByteArrayOutputStream();
  391. DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os));
  392. dfmt.setRepository(db);
  393. dfmt.format((AnyObjectId) null, null);
  394. dfmt.flush();
  395. String actual = os.toString("UTF-8");
  396. String expected = "";
  397. assertEquals(expected, actual);
  398. }
  399. private static String makeDiffHeader(String pathA, String pathB,
  400. ObjectId aId,
  401. ObjectId bId) {
  402. String a = aId.abbreviate(8).name();
  403. String b = bId.abbreviate(8).name();
  404. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  405. "index " + a + ".." + b + " " + REGULAR_FILE + "\n" + //
  406. "--- a/" + pathA + "\n" + //
  407. "+++ b/" + pathB + "\n";
  408. }
  409. private static String makeDiffHeaderModeChange(String pathA, String pathB,
  410. ObjectId aId, ObjectId bId, String modeA, String modeB) {
  411. String a = aId.abbreviate(8).name();
  412. String b = bId.abbreviate(8).name();
  413. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  414. "old mode " + modeA + "\n" + //
  415. "new mode " + modeB + "\n" + //
  416. "index " + a + ".." + b + "\n" + //
  417. "--- a/" + pathA + "\n" + //
  418. "+++ b/" + pathB + "\n";
  419. }
  420. private ObjectId blob(String content) throws Exception {
  421. return testDb.blob(content).copy();
  422. }
  423. }