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

Show submodule difference as a hunk Current DiffFormat behavior regarding submodules (aka git links) is incorrect. The "Subproject commit <sha1>" appears as part of the diff header, rather than as its own hunk. --> From JGit implementation diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin index b9d3ca8..ec6ed89 160000 --- a/plugins/cookbook-plugin +++ b/plugins/cookbook-plugin -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e --> From C Git 2.5.2 diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin index b9d3ca8..ec6ed89 160000 --- a/plugins/cookbook-plugin +++ b/plugins/cookbook-plugin @@ -1 +1 @@ -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e The current way of processing submodules results in no hunk header and includes the contents of the hunk as part of the headers. To fix this, we can't just have our writeGitLinkDiffText output the hunk header. We have to change the flow so that the raw text gets parsed as a diff. The easiest way to do this is to fake the RawText in the FormatResult when we have a GITLINK. It should be noted that it seems possible for there to be a difference between a GITLINK and a non-GITLINK, but I don't think this can happen in practice, so I don't think we need to worry too much about it. This patch also fixes up the test for GitLink headers, as the test was for the old behavior. My setup has 3 other failing tests which may or may not be the result of environmental changes. However, the same tests fail without this commit, so I do not believe they are related. Bug: 477759 Change-Id: If13f7b406904fad814416c93ed09ea47ef183337 Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
8 年之前
Show submodule difference as a hunk Current DiffFormat behavior regarding submodules (aka git links) is incorrect. The "Subproject commit <sha1>" appears as part of the diff header, rather than as its own hunk. --> From JGit implementation diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin index b9d3ca8..ec6ed89 160000 --- a/plugins/cookbook-plugin +++ b/plugins/cookbook-plugin -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e --> From C Git 2.5.2 diff --git a/plugins/cookbook-plugin b/plugins/cookbook-plugin index b9d3ca8..ec6ed89 160000 --- a/plugins/cookbook-plugin +++ b/plugins/cookbook-plugin @@ -1 +1 @@ -Subproject commit b9d3ca8a65030071e28be19296ba867ab424fbbf +Subproject commit ec6ed89c47ba7223f82d9cb512926a6c5081343e The current way of processing submodules results in no hunk header and includes the contents of the hunk as part of the headers. To fix this, we can't just have our writeGitLinkDiffText output the hunk header. We have to change the flow so that the raw text gets parsed as a diff. The easiest way to do this is to fake the RawText in the FormatResult when we have a GITLINK. It should be noted that it seems possible for there to be a difference between a GITLINK and a non-GITLINK, but I don't think this can happen in practice, so I don't think we need to worry too much about it. This patch also fixes up the test for GitLink headers, as the test was for the old behavior. My setup has 3 other failing tests which may or may not be the result of environmental changes. However, the same tests fail without this commit, so I do not believe they are related. Bug: 477759 Change-Id: If13f7b406904fad814416c93ed09ea47ef183337 Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
8 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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.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.RepositoryTestCase;
  52. import org.eclipse.jgit.junit.TestRepository;
  53. import org.eclipse.jgit.lib.AnyObjectId;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.patch.FileHeader;
  58. import org.eclipse.jgit.patch.HunkHeader;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.treewalk.FileTreeIterator;
  61. import org.eclipse.jgit.treewalk.filter.PathFilter;
  62. import org.eclipse.jgit.util.FileUtils;
  63. import org.eclipse.jgit.util.RawParseUtils;
  64. import org.eclipse.jgit.util.io.DisabledOutputStream;
  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<>(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. try (Git git = new Git(db);
  313. ByteArrayOutputStream os = new ByteArrayOutputStream();
  314. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  315. git.add().addFilepattern(".").call();
  316. git.commit().setMessage("Initial commit").call();
  317. write(new File(folder, "folder.txt"), "folder change");
  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. }
  335. @Test
  336. public void testDiffRootNullToTree() throws Exception {
  337. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  338. File folder = new File(db.getDirectory().getParent(), "folder");
  339. FileUtils.mkdir(folder);
  340. write(new File(folder, "folder.txt"), "folder");
  341. try (Git git = new Git(db);
  342. ByteArrayOutputStream os = new ByteArrayOutputStream();
  343. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  344. git.add().addFilepattern(".").call();
  345. RevCommit commit = git.commit().setMessage("Initial commit").call();
  346. write(new File(folder, "folder.txt"), "folder change");
  347. dfmt.setRepository(db);
  348. dfmt.setPathFilter(PathFilter.create("folder"));
  349. dfmt.format(null, commit.getTree().getId());
  350. dfmt.flush();
  351. String actual = os.toString("UTF-8");
  352. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  353. + "new file mode 100644\n"
  354. + "index 0000000..0119635\n"
  355. + "--- /dev/null\n"
  356. + "+++ b/folder/folder.txt\n"
  357. + "@@ -0,0 +1 @@\n"
  358. + "+folder\n"
  359. + "\\ No newline at end of file\n";
  360. assertEquals(expected, actual);
  361. }
  362. }
  363. @Test
  364. public void testDiffRootTreeToNull() throws Exception {
  365. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  366. File folder = new File(db.getDirectory().getParent(), "folder");
  367. FileUtils.mkdir(folder);
  368. write(new File(folder, "folder.txt"), "folder");
  369. try (Git git = new Git(db);
  370. ByteArrayOutputStream os = new ByteArrayOutputStream();
  371. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os));) {
  372. git.add().addFilepattern(".").call();
  373. RevCommit commit = git.commit().setMessage("Initial commit").call();
  374. write(new File(folder, "folder.txt"), "folder change");
  375. dfmt.setRepository(db);
  376. dfmt.setPathFilter(PathFilter.create("folder"));
  377. dfmt.format(commit.getTree().getId(), null);
  378. dfmt.flush();
  379. String actual = os.toString("UTF-8");
  380. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  381. + "deleted file mode 100644\n"
  382. + "index 0119635..0000000\n"
  383. + "--- a/folder/folder.txt\n"
  384. + "+++ /dev/null\n"
  385. + "@@ -1 +0,0 @@\n"
  386. + "-folder\n"
  387. + "\\ No newline at end of file\n";
  388. assertEquals(expected, actual);
  389. }
  390. }
  391. @Test
  392. public void testDiffNullToNull() throws Exception {
  393. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  394. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  395. dfmt.setRepository(db);
  396. dfmt.format((AnyObjectId) null, null);
  397. dfmt.flush();
  398. String actual = os.toString("UTF-8");
  399. String expected = "";
  400. assertEquals(expected, actual);
  401. }
  402. }
  403. private static String makeDiffHeader(String pathA, String pathB,
  404. ObjectId aId,
  405. ObjectId bId) {
  406. String a = aId.abbreviate(8).name();
  407. String b = bId.abbreviate(8).name();
  408. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  409. "index " + a + ".." + b + " " + REGULAR_FILE + "\n" + //
  410. "--- a/" + pathA + "\n" + //
  411. "+++ b/" + pathB + "\n";
  412. }
  413. private static String makeDiffHeaderModeChange(String pathA, String pathB,
  414. ObjectId aId, ObjectId bId, String modeA, String modeB) {
  415. String a = aId.abbreviate(8).name();
  416. String b = bId.abbreviate(8).name();
  417. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  418. "old mode " + modeA + "\n" + //
  419. "new mode " + modeB + "\n" + //
  420. "index " + a + ".." + b + "\n" + //
  421. "--- a/" + pathA + "\n" + //
  422. "+++ b/" + pathB + "\n";
  423. }
  424. private ObjectId blob(String content) throws Exception {
  425. return testDb.blob(content).copy();
  426. }
  427. }