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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Copyright (C) 2010, 2021 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.diff;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.BufferedOutputStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.eclipse.jgit.api.Git;
  21. import org.eclipse.jgit.api.Status;
  22. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  23. import org.eclipse.jgit.dircache.DirCacheIterator;
  24. import org.eclipse.jgit.junit.RepositoryTestCase;
  25. import org.eclipse.jgit.junit.TestRepository;
  26. import org.eclipse.jgit.lib.AnyObjectId;
  27. import org.eclipse.jgit.lib.ConfigConstants;
  28. import org.eclipse.jgit.lib.FileMode;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. import org.eclipse.jgit.lib.Repository;
  31. import org.eclipse.jgit.patch.FileHeader;
  32. import org.eclipse.jgit.patch.HunkHeader;
  33. import org.eclipse.jgit.revwalk.RevCommit;
  34. import org.eclipse.jgit.storage.file.FileBasedConfig;
  35. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  36. import org.eclipse.jgit.treewalk.FileTreeIterator;
  37. import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
  38. import org.eclipse.jgit.treewalk.filter.PathFilter;
  39. import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
  40. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  41. import org.eclipse.jgit.util.FileUtils;
  42. import org.eclipse.jgit.util.RawParseUtils;
  43. import org.eclipse.jgit.util.io.DisabledOutputStream;
  44. import org.junit.After;
  45. import org.junit.Before;
  46. import org.junit.Test;
  47. public class DiffFormatterTest extends RepositoryTestCase {
  48. private static final String DIFF = "diff --git ";
  49. private static final String REGULAR_FILE = "100644";
  50. private static final String GITLINK = "160000";
  51. private static final String PATH_A = "src/a";
  52. private static final String PATH_B = "src/b";
  53. private DiffFormatter df;
  54. private TestRepository<Repository> testDb;
  55. @Override
  56. @Before
  57. public void setUp() throws Exception {
  58. super.setUp();
  59. testDb = new TestRepository<>(db);
  60. df = new DiffFormatter(DisabledOutputStream.INSTANCE);
  61. df.setRepository(db);
  62. df.setAbbreviationLength(8);
  63. }
  64. @Override
  65. @After
  66. public void tearDown() throws Exception {
  67. if (df != null) {
  68. df.close();
  69. }
  70. super.tearDown();
  71. }
  72. @Test
  73. public void testDefaultRenameDetectorSettings() throws Exception {
  74. RenameDetector rd = df.getRenameDetector();
  75. assertNull(rd);
  76. df.setDetectRenames(true);
  77. rd = df.getRenameDetector();
  78. assertNotNull(rd);
  79. assertEquals(400, rd.getRenameLimit());
  80. assertEquals(60, rd.getRenameScore());
  81. }
  82. @Test
  83. public void testCreateFileHeader_Add() throws Exception {
  84. ObjectId adId = blob("a\nd\n");
  85. DiffEntry ent = DiffEntry.add("FOO", adId);
  86. FileHeader fh = df.toFileHeader(ent);
  87. String diffHeader = "diff --git a/FOO b/FOO\n" //
  88. + "new file mode " + REGULAR_FILE + "\n"
  89. + "index "
  90. + ObjectId.zeroId().abbreviate(8).name()
  91. + ".."
  92. + adId.abbreviate(8).name() + "\n" //
  93. + "--- /dev/null\n"//
  94. + "+++ b/FOO\n";
  95. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  96. assertEquals(0, fh.getStartOffset());
  97. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  98. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  99. assertEquals(1, fh.getHunks().size());
  100. HunkHeader hh = fh.getHunks().get(0);
  101. assertEquals(1, hh.toEditList().size());
  102. EditList el = hh.toEditList();
  103. assertEquals(1, el.size());
  104. Edit e = el.get(0);
  105. assertEquals(0, e.getBeginA());
  106. assertEquals(0, e.getEndA());
  107. assertEquals(0, e.getBeginB());
  108. assertEquals(2, e.getEndB());
  109. assertEquals(Edit.Type.INSERT, e.getType());
  110. }
  111. @Test
  112. public void testCreateFileHeader_Delete() throws Exception {
  113. ObjectId adId = blob("a\nd\n");
  114. DiffEntry ent = DiffEntry.delete("FOO", adId);
  115. FileHeader fh = df.toFileHeader(ent);
  116. String diffHeader = "diff --git a/FOO b/FOO\n" //
  117. + "deleted file mode " + REGULAR_FILE + "\n"
  118. + "index "
  119. + adId.abbreviate(8).name()
  120. + ".."
  121. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  122. + "--- a/FOO\n"//
  123. + "+++ /dev/null\n";
  124. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  125. assertEquals(0, fh.getStartOffset());
  126. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  127. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  128. assertEquals(1, fh.getHunks().size());
  129. HunkHeader hh = fh.getHunks().get(0);
  130. assertEquals(1, hh.toEditList().size());
  131. EditList el = hh.toEditList();
  132. assertEquals(1, el.size());
  133. Edit e = el.get(0);
  134. assertEquals(0, e.getBeginA());
  135. assertEquals(2, e.getEndA());
  136. assertEquals(0, e.getBeginB());
  137. assertEquals(0, e.getEndB());
  138. assertEquals(Edit.Type.DELETE, e.getType());
  139. }
  140. @Test
  141. public void testCreateFileHeader_Modify() throws Exception {
  142. ObjectId adId = blob("a\nd\n");
  143. ObjectId abcdId = blob("a\nb\nc\nd\n");
  144. String diffHeader = makeDiffHeader(PATH_A, PATH_A, adId, abcdId);
  145. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  146. DiffEntry abcd = DiffEntry.add(PATH_A, abcdId);
  147. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  148. FileHeader fh = df.toFileHeader(mod);
  149. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  150. assertEquals(0, fh.getStartOffset());
  151. assertEquals(fh.getBuffer().length, fh.getEndOffset());
  152. assertEquals(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  153. assertEquals(1, fh.getHunks().size());
  154. HunkHeader hh = fh.getHunks().get(0);
  155. assertEquals(1, hh.toEditList().size());
  156. EditList el = hh.toEditList();
  157. assertEquals(1, el.size());
  158. Edit e = el.get(0);
  159. assertEquals(1, e.getBeginA());
  160. assertEquals(1, e.getEndA());
  161. assertEquals(1, e.getBeginB());
  162. assertEquals(3, e.getEndB());
  163. assertEquals(Edit.Type.INSERT, e.getType());
  164. }
  165. @Test
  166. public void testCreateFileHeader_Binary() throws Exception {
  167. ObjectId adId = blob("a\nd\n");
  168. ObjectId binId = blob("a\nb\nc\n\0\0\0\0d\n");
  169. String diffHeader = makeDiffHeader(PATH_A, PATH_B, adId, binId)
  170. + "Binary files differ\n";
  171. DiffEntry ad = DiffEntry.delete(PATH_A, adId);
  172. DiffEntry abcd = DiffEntry.add(PATH_B, binId);
  173. DiffEntry mod = DiffEntry.pair(ChangeType.MODIFY, ad, abcd, 0);
  174. FileHeader fh = df.toFileHeader(mod);
  175. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  176. assertEquals(FileHeader.PatchType.BINARY, fh.getPatchType());
  177. assertEquals(1, fh.getHunks().size());
  178. HunkHeader hh = fh.getHunks().get(0);
  179. assertEquals(0, hh.toEditList().size());
  180. }
  181. @Test
  182. public void testCreateFileHeader_GitLink() throws Exception {
  183. ObjectId aId = blob("a\n");
  184. ObjectId bId = blob("b\n");
  185. String diffHeader = makeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId,
  186. GITLINK, REGULAR_FILE);
  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(1, hh.toEditList().size());
  196. }
  197. @Test
  198. public void testCreateFileHeader_AddGitLink() throws Exception {
  199. ObjectId adId = blob("a\nd\n");
  200. DiffEntry ent = DiffEntry.add("FOO", adId);
  201. ent.newMode = FileMode.GITLINK;
  202. FileHeader fh = df.toFileHeader(ent);
  203. String diffHeader = "diff --git a/FOO b/FOO\n" //
  204. + "new file mode " + GITLINK + "\n"
  205. + "index "
  206. + ObjectId.zeroId().abbreviate(8).name()
  207. + ".."
  208. + adId.abbreviate(8).name() + "\n" //
  209. + "--- /dev/null\n"//
  210. + "+++ b/FOO\n";
  211. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  212. assertEquals(1, fh.getHunks().size());
  213. HunkHeader hh = fh.getHunks().get(0);
  214. EditList el = hh.toEditList();
  215. assertEquals(1, el.size());
  216. Edit e = el.get(0);
  217. assertEquals(0, e.getBeginA());
  218. assertEquals(0, e.getEndA());
  219. assertEquals(0, e.getBeginB());
  220. assertEquals(1, e.getEndB());
  221. assertEquals(Edit.Type.INSERT, e.getType());
  222. }
  223. @Test
  224. public void testCreateFileHeader_DeleteGitLink() throws Exception {
  225. ObjectId adId = blob("a\nd\n");
  226. DiffEntry ent = DiffEntry.delete("FOO", adId);
  227. ent.oldMode = FileMode.GITLINK;
  228. FileHeader fh = df.toFileHeader(ent);
  229. String diffHeader = "diff --git a/FOO b/FOO\n" //
  230. + "deleted file mode " + GITLINK + "\n"
  231. + "index "
  232. + adId.abbreviate(8).name()
  233. + ".."
  234. + ObjectId.zeroId().abbreviate(8).name() + "\n" //
  235. + "--- a/FOO\n"//
  236. + "+++ /dev/null\n";
  237. assertEquals(diffHeader, RawParseUtils.decode(fh.getBuffer()));
  238. assertEquals(1, fh.getHunks().size());
  239. HunkHeader hh = fh.getHunks().get(0);
  240. EditList el = hh.toEditList();
  241. assertEquals(1, el.size());
  242. Edit e = el.get(0);
  243. assertEquals(0, e.getBeginA());
  244. assertEquals(1, e.getEndA());
  245. assertEquals(0, e.getBeginB());
  246. assertEquals(0, e.getEndB());
  247. assertEquals(Edit.Type.DELETE, e.getType());
  248. }
  249. @Test
  250. public void testCreateFileHeaderWithoutIndexLine() throws Exception {
  251. DiffEntry m = DiffEntry.modify(PATH_A);
  252. m.oldMode = FileMode.REGULAR_FILE;
  253. m.newMode = FileMode.EXECUTABLE_FILE;
  254. FileHeader fh = df.toFileHeader(m);
  255. String expected = DIFF + "a/src/a b/src/a\n" + //
  256. "old mode 100644\n" + //
  257. "new mode 100755\n";
  258. assertEquals(expected, fh.getScriptText());
  259. }
  260. @Test
  261. public void testCreateFileHeaderForRenameWithoutContentChange() throws Exception {
  262. DiffEntry a = DiffEntry.delete(PATH_A, ObjectId.zeroId());
  263. DiffEntry b = DiffEntry.add(PATH_B, ObjectId.zeroId());
  264. DiffEntry m = DiffEntry.pair(ChangeType.RENAME, a, b, 100);
  265. m.oldId = null;
  266. m.newId = null;
  267. FileHeader fh = df.toFileHeader(m);
  268. String expected = DIFF + "a/src/a b/src/b\n" + //
  269. "similarity index 100%\n" + //
  270. "rename from src/a\n" + //
  271. "rename to src/b\n";
  272. assertEquals(expected, fh.getScriptText());
  273. }
  274. @Test
  275. public void testCreateFileHeaderForRenameModeChange()
  276. throws Exception {
  277. DiffEntry a = DiffEntry.delete(PATH_A, ObjectId.zeroId());
  278. DiffEntry b = DiffEntry.add(PATH_B, ObjectId.zeroId());
  279. b.oldMode = FileMode.REGULAR_FILE;
  280. b.newMode = FileMode.EXECUTABLE_FILE;
  281. DiffEntry m = DiffEntry.pair(ChangeType.RENAME, a, b, 100);
  282. m.oldId = null;
  283. m.newId = null;
  284. FileHeader fh = df.toFileHeader(m);
  285. //@formatter:off
  286. String expected = DIFF + "a/src/a b/src/b\n" +
  287. "old mode 100644\n" +
  288. "new mode 100755\n" +
  289. "similarity index 100%\n" +
  290. "rename from src/a\n" +
  291. "rename to src/b\n";
  292. //@formatter:on
  293. assertEquals(expected, fh.getScriptText());
  294. }
  295. @Test
  296. public void testDiff() throws Exception {
  297. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  298. File folder = new File(db.getDirectory().getParent(), "folder");
  299. FileUtils.mkdir(folder);
  300. write(new File(folder, "folder.txt"), "folder");
  301. try (Git git = new Git(db);
  302. ByteArrayOutputStream os = new ByteArrayOutputStream();
  303. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  304. git.add().addFilepattern(".").call();
  305. git.commit().setMessage("Initial commit").call();
  306. write(new File(folder, "folder.txt"), "folder change");
  307. dfmt.setRepository(db);
  308. dfmt.setPathFilter(PathFilter.create("folder"));
  309. DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
  310. FileTreeIterator newTree = new FileTreeIterator(db);
  311. dfmt.format(oldTree, newTree);
  312. dfmt.flush();
  313. String actual = os.toString("UTF-8");
  314. String expected =
  315. "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  316. + "index 0119635..95c4c65 100644\n"
  317. + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
  318. + "@@ -1 +1 @@\n" + "-folder\n"
  319. + "\\ No newline at end of file\n" + "+folder change\n"
  320. + "\\ No newline at end of file\n";
  321. assertEquals(expected, actual);
  322. }
  323. }
  324. @Test
  325. public void testDiffRootNullToTree() throws Exception {
  326. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  327. File folder = new File(db.getDirectory().getParent(), "folder");
  328. FileUtils.mkdir(folder);
  329. write(new File(folder, "folder.txt"), "folder");
  330. try (Git git = new Git(db);
  331. ByteArrayOutputStream os = new ByteArrayOutputStream();
  332. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  333. git.add().addFilepattern(".").call();
  334. RevCommit commit = git.commit().setMessage("Initial commit").call();
  335. write(new File(folder, "folder.txt"), "folder change");
  336. dfmt.setRepository(db);
  337. dfmt.setPathFilter(PathFilter.create("folder"));
  338. dfmt.format(null, commit.getTree().getId());
  339. dfmt.flush();
  340. String actual = os.toString("UTF-8");
  341. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  342. + "new file mode 100644\n"
  343. + "index 0000000..0119635\n"
  344. + "--- /dev/null\n"
  345. + "+++ b/folder/folder.txt\n"
  346. + "@@ -0,0 +1 @@\n"
  347. + "+folder\n"
  348. + "\\ No newline at end of file\n";
  349. assertEquals(expected, actual);
  350. }
  351. }
  352. @Test
  353. public void testDiffRootTreeToNull() throws Exception {
  354. write(new File(db.getDirectory().getParent(), "test.txt"), "test");
  355. File folder = new File(db.getDirectory().getParent(), "folder");
  356. FileUtils.mkdir(folder);
  357. write(new File(folder, "folder.txt"), "folder");
  358. try (Git git = new Git(db);
  359. ByteArrayOutputStream os = new ByteArrayOutputStream();
  360. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os));) {
  361. git.add().addFilepattern(".").call();
  362. RevCommit commit = git.commit().setMessage("Initial commit").call();
  363. write(new File(folder, "folder.txt"), "folder change");
  364. dfmt.setRepository(db);
  365. dfmt.setPathFilter(PathFilter.create("folder"));
  366. dfmt.format(commit.getTree().getId(), null);
  367. dfmt.flush();
  368. String actual = os.toString("UTF-8");
  369. String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n"
  370. + "deleted file mode 100644\n"
  371. + "index 0119635..0000000\n"
  372. + "--- a/folder/folder.txt\n"
  373. + "+++ /dev/null\n"
  374. + "@@ -1 +0,0 @@\n"
  375. + "-folder\n"
  376. + "\\ No newline at end of file\n";
  377. assertEquals(expected, actual);
  378. }
  379. }
  380. @Test
  381. public void testDiffNullToNull() throws Exception {
  382. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  383. DiffFormatter dfmt = new DiffFormatter(new BufferedOutputStream(os))) {
  384. dfmt.setRepository(db);
  385. dfmt.format((AnyObjectId) null, null);
  386. dfmt.flush();
  387. String actual = os.toString("UTF-8");
  388. String expected = "";
  389. assertEquals(expected, actual);
  390. }
  391. }
  392. @Test
  393. public void testTrackedFileInIgnoredFolderUnchanged()
  394. throws Exception {
  395. commitFile("empty/empty/foo", "", "master");
  396. commitFile(".gitignore", "empty/*", "master");
  397. try (Git git = new Git(db)) {
  398. Status status = git.status().call();
  399. assertTrue(status.isClean());
  400. }
  401. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  402. DiffFormatter dfmt = new DiffFormatter(os)) {
  403. dfmt.setRepository(db);
  404. dfmt.format(new DirCacheIterator(db.readDirCache()),
  405. new FileTreeIterator(db));
  406. dfmt.flush();
  407. String actual = os.toString("UTF-8");
  408. assertEquals("", actual);
  409. }
  410. }
  411. @Test
  412. public void testFilter() throws Exception {
  413. RevCommit parent;
  414. RevCommit head;
  415. try (Git git = new Git(db)) {
  416. writeTrashFile("foo.txt", "foo\n");
  417. writeTrashFile("src/some.txt", "some\n");
  418. writeTrashFile("src/image.png", "image\n");
  419. writeTrashFile("src/test.pdf", "test\n");
  420. writeTrashFile("src/xyz.txt", "xyz\n");
  421. git.add().addFilepattern(".").call();
  422. parent = git.commit().setMessage("initial").call();
  423. writeTrashFile("foo.txt", "FOO\n");
  424. writeTrashFile("src/some.txt", "SOME\n");
  425. writeTrashFile("src/image.png", "IMAGE\n");
  426. writeTrashFile("src/test.pdf", "TEST\n");
  427. writeTrashFile("src/xyz.txt", "XYZ\n");
  428. git.add().addFilepattern(".").call();
  429. head = git.commit().setMessage("second").call();
  430. }
  431. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  432. DiffFormatter dfmt = new DiffFormatter(os)) {
  433. dfmt.setRepository(db);
  434. List<TreeFilter> skip = new ArrayList<>();
  435. skip.add(PathSuffixFilter.create(".png"));
  436. skip.add(PathSuffixFilter.create(".pdf"));
  437. dfmt.setPathFilter(OrTreeFilter.create(skip).negate());
  438. dfmt.format(
  439. new CanonicalTreeParser(null, db.newObjectReader(),
  440. parent.getTree()),
  441. new CanonicalTreeParser(null, db.newObjectReader(),
  442. head.getTree()));
  443. dfmt.flush();
  444. String actual = os.toString("UTF-8");
  445. String expected = "diff --git a/foo.txt b/foo.txt\n"
  446. + "index 257cc56..b7d6715 100644\n"
  447. + "--- a/foo.txt\n"
  448. + "+++ b/foo.txt\n"
  449. + "@@ -1 +1 @@\n"
  450. + "-foo\n"
  451. + "+FOO\n"
  452. + "diff --git a/src/some.txt b/src/some.txt\n"
  453. + "index 363ef61..76cea5f 100644\n"
  454. + "--- a/src/some.txt\n"
  455. + "+++ b/src/some.txt\n"
  456. + "@@ -1 +1 @@\n"
  457. + "-some\n"
  458. + "+SOME\n"
  459. + "diff --git a/src/xyz.txt b/src/xyz.txt\n"
  460. + "index cd470e6..d4e3ab0 100644\n"
  461. + "--- a/src/xyz.txt\n"
  462. + "+++ b/src/xyz.txt\n"
  463. + "@@ -1 +1 @@\n"
  464. + "-xyz\n"
  465. + "+XYZ\n";
  466. assertEquals(expected, actual);
  467. }
  468. }
  469. @Test
  470. public void testTrackedFileInIgnoredFolderChanged()
  471. throws Exception {
  472. String expectedDiff = "diff --git a/empty/empty/foo b/empty/empty/foo\n"
  473. + "index e69de29..5ea2ed4 100644\n" //
  474. + "--- a/empty/empty/foo\n" //
  475. + "+++ b/empty/empty/foo\n" //
  476. + "@@ -0,0 +1 @@\n" //
  477. + "+changed\n";
  478. commitFile("empty/empty/foo", "", "master");
  479. commitFile(".gitignore", "empty/*", "master");
  480. try (Git git = new Git(db)) {
  481. Status status = git.status().call();
  482. assertTrue(status.isClean());
  483. }
  484. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  485. DiffFormatter dfmt = new DiffFormatter(os)) {
  486. writeTrashFile("empty/empty/foo", "changed\n");
  487. dfmt.setRepository(db);
  488. dfmt.format(new DirCacheIterator(db.readDirCache()),
  489. new FileTreeIterator(db));
  490. dfmt.flush();
  491. String actual = os.toString("UTF-8");
  492. assertEquals(expectedDiff, actual);
  493. }
  494. }
  495. @Test
  496. public void testDiffAutoCrlfSmallFile() throws Exception {
  497. String content = "01234\r\n01234\r\n01234\r\n";
  498. String expectedDiff = "diff --git a/test.txt b/test.txt\n"
  499. + "index fe25983..a44a032 100644\n" //
  500. + "--- a/test.txt\n" //
  501. + "+++ b/test.txt\n" //
  502. + "@@ -1,3 +1,4 @@\n" //
  503. + " 01234\n" //
  504. + "+ABCD\n" //
  505. + " 01234\n" //
  506. + " 01234\n";
  507. doAutoCrLfTest(content, expectedDiff);
  508. }
  509. @Test
  510. public void testDiffAutoCrlfMediumFile() throws Exception {
  511. String content = mediumCrLfString();
  512. String expectedDiff = "diff --git a/test.txt b/test.txt\n"
  513. + "index 215c502..c10f08c 100644\n" //
  514. + "--- a/test.txt\n" //
  515. + "+++ b/test.txt\n" //
  516. + "@@ -1,4 +1,5 @@\n" //
  517. + " 01234567\n" //
  518. + "+ABCD\n" //
  519. + " 01234567\n" //
  520. + " 01234567\n" //
  521. + " 01234567\n";
  522. doAutoCrLfTest(content, expectedDiff);
  523. }
  524. @Test
  525. public void testDiffAutoCrlfLargeFile() throws Exception {
  526. String content = largeCrLfString();
  527. String expectedDiff = "diff --git a/test.txt b/test.txt\n"
  528. + "index 7014942..c0487a7 100644\n" //
  529. + "--- a/test.txt\n" //
  530. + "+++ b/test.txt\n" //
  531. + "@@ -1,4 +1,5 @@\n"
  532. + " 012345678901234567890123456789012345678901234567\n"
  533. + "+ABCD\n"
  534. + " 012345678901234567890123456789012345678901234567\n"
  535. + " 012345678901234567890123456789012345678901234567\n"
  536. + " 012345678901234567890123456789012345678901234567\n";
  537. doAutoCrLfTest(content, expectedDiff);
  538. }
  539. private void doAutoCrLfTest(String content, String expectedDiff)
  540. throws Exception {
  541. FileBasedConfig config = db.getConfig();
  542. config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  543. ConfigConstants.CONFIG_KEY_AUTOCRLF, "true");
  544. config.save();
  545. commitFile("test.txt", content, "master");
  546. // Insert a line into content
  547. int i = content.indexOf('\n');
  548. content = content.substring(0, i + 1) + "ABCD\r\n"
  549. + content.substring(i + 1);
  550. writeTrashFile("test.txt", content);
  551. // Create the patch
  552. try (ByteArrayOutputStream os = new ByteArrayOutputStream();
  553. DiffFormatter dfmt = new DiffFormatter(
  554. new BufferedOutputStream(os))) {
  555. dfmt.setRepository(db);
  556. dfmt.format(new DirCacheIterator(db.readDirCache()),
  557. new FileTreeIterator(db));
  558. dfmt.flush();
  559. String actual = os.toString("UTF-8");
  560. assertEquals(expectedDiff, actual);
  561. }
  562. }
  563. private static String largeCrLfString() {
  564. String line = "012345678901234567890123456789012345678901234567\r\n";
  565. StringBuilder builder = new StringBuilder(
  566. 2 * RawText.FIRST_FEW_BYTES);
  567. while (builder.length() < 2 * RawText.FIRST_FEW_BYTES) {
  568. builder.append(line);
  569. }
  570. return builder.toString();
  571. }
  572. private static String mediumCrLfString() {
  573. // Create a CR-LF string longer than RawText.FIRST_FEW_BYTES whose
  574. // canonical representation is shorter than RawText.FIRST_FEW_BYTES.
  575. String line = "01234567\r\n"; // 10 characters
  576. StringBuilder builder = new StringBuilder(
  577. RawText.FIRST_FEW_BYTES + line.length());
  578. while (builder.length() <= RawText.FIRST_FEW_BYTES) {
  579. builder.append(line);
  580. }
  581. return builder.toString();
  582. }
  583. private static String makeDiffHeader(String pathA, String pathB,
  584. ObjectId aId,
  585. ObjectId bId) {
  586. String a = aId.abbreviate(8).name();
  587. String b = bId.abbreviate(8).name();
  588. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  589. "index " + a + ".." + b + " " + REGULAR_FILE + "\n" + //
  590. "--- a/" + pathA + "\n" + //
  591. "+++ b/" + pathB + "\n";
  592. }
  593. private static String makeDiffHeaderModeChange(String pathA, String pathB,
  594. ObjectId aId, ObjectId bId, String modeA, String modeB) {
  595. String a = aId.abbreviate(8).name();
  596. String b = bId.abbreviate(8).name();
  597. return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + //
  598. "old mode " + modeA + "\n" + //
  599. "new mode " + modeB + "\n" + //
  600. "index " + a + ".." + b + "\n" + //
  601. "--- a/" + pathA + "\n" + //
  602. "+++ b/" + pathB + "\n";
  603. }
  604. private ObjectId blob(String content) throws Exception {
  605. return testDb.blob(content).copy();
  606. }
  607. }