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.

BlameCommandTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (C) 2011, 2019 GitHub 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.api;
  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.File;
  16. import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
  17. import org.eclipse.jgit.api.ResetCommand.ResetType;
  18. import org.eclipse.jgit.blame.BlameResult;
  19. import org.eclipse.jgit.diff.RawTextComparator;
  20. import org.eclipse.jgit.junit.RepositoryTestCase;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  23. import org.eclipse.jgit.revwalk.RevCommit;
  24. import org.eclipse.jgit.storage.file.FileBasedConfig;
  25. import org.junit.Test;
  26. /**
  27. * Unit tests of {@link BlameCommand}
  28. */
  29. public class BlameCommandTest extends RepositoryTestCase {
  30. private static String join(String... lines) {
  31. StringBuilder joined = new StringBuilder();
  32. for (String line : lines)
  33. joined.append(line).append('\n');
  34. return joined.toString();
  35. }
  36. @Test
  37. public void testSingleRevision() throws Exception {
  38. try (Git git = new Git(db)) {
  39. String[] content = new String[] { "first", "second", "third" };
  40. writeTrashFile("file.txt", join(content));
  41. git.add().addFilepattern("file.txt").call();
  42. RevCommit commit = git.commit().setMessage("create file").call();
  43. BlameCommand command = new BlameCommand(db);
  44. command.setFilePath("file.txt");
  45. BlameResult lines = command.call();
  46. assertNotNull(lines);
  47. assertEquals(3, lines.getResultContents().size());
  48. for (int i = 0; i < 3; i++) {
  49. assertEquals(commit, lines.getSourceCommit(i));
  50. assertEquals(i, lines.getSourceLine(i));
  51. }
  52. }
  53. }
  54. @Test
  55. public void testTwoRevisions() throws Exception {
  56. try (Git git = new Git(db)) {
  57. String[] content1 = new String[] { "first", "second" };
  58. writeTrashFile("file.txt", join(content1));
  59. git.add().addFilepattern("file.txt").call();
  60. RevCommit commit1 = git.commit().setMessage("create file").call();
  61. String[] content2 = new String[] { "first", "second", "third" };
  62. writeTrashFile("file.txt", join(content2));
  63. git.add().addFilepattern("file.txt").call();
  64. RevCommit commit2 = git.commit().setMessage("create file").call();
  65. BlameCommand command = new BlameCommand(db);
  66. command.setFilePath("file.txt");
  67. BlameResult lines = command.call();
  68. assertEquals(3, lines.getResultContents().size());
  69. assertEquals(commit1, lines.getSourceCommit(0));
  70. assertEquals(0, lines.getSourceLine(0));
  71. assertEquals(commit1, lines.getSourceCommit(1));
  72. assertEquals(1, lines.getSourceLine(1));
  73. assertEquals(commit2, lines.getSourceCommit(2));
  74. assertEquals(2, lines.getSourceLine(2));
  75. }
  76. }
  77. @Test
  78. public void testRename() throws Exception {
  79. testRename("file1.txt", "file2.txt");
  80. }
  81. @Test
  82. public void testRenameInSubDir() throws Exception {
  83. testRename("subdir/file1.txt", "subdir/file2.txt");
  84. }
  85. @Test
  86. public void testMoveToOtherDir() throws Exception {
  87. testRename("subdir/file1.txt", "otherdir/file1.txt");
  88. }
  89. private void testRename(String sourcePath, String destPath)
  90. throws Exception {
  91. try (Git git = new Git(db)) {
  92. String[] content1 = new String[] { "a", "b", "c" };
  93. writeTrashFile(sourcePath, join(content1));
  94. git.add().addFilepattern(sourcePath).call();
  95. RevCommit commit1 = git.commit().setMessage("create file").call();
  96. writeTrashFile(destPath, join(content1));
  97. git.add().addFilepattern(destPath).call();
  98. git.rm().addFilepattern(sourcePath).call();
  99. git.commit().setMessage("moving file").call();
  100. String[] content2 = new String[] { "a", "b", "c2" };
  101. writeTrashFile(destPath, join(content2));
  102. git.add().addFilepattern(destPath).call();
  103. RevCommit commit3 = git.commit().setMessage("editing file").call();
  104. BlameCommand command = new BlameCommand(db);
  105. command.setFollowFileRenames(true);
  106. command.setFilePath(destPath);
  107. BlameResult lines = command.call();
  108. assertEquals(commit1, lines.getSourceCommit(0));
  109. assertEquals(0, lines.getSourceLine(0));
  110. assertEquals(sourcePath, lines.getSourcePath(0));
  111. assertEquals(commit1, lines.getSourceCommit(1));
  112. assertEquals(1, lines.getSourceLine(1));
  113. assertEquals(sourcePath, lines.getSourcePath(1));
  114. assertEquals(commit3, lines.getSourceCommit(2));
  115. assertEquals(2, lines.getSourceLine(2));
  116. assertEquals(destPath, lines.getSourcePath(2));
  117. }
  118. }
  119. @Test
  120. public void testTwoRenames() throws Exception {
  121. try (Git git = new Git(db)) {
  122. // Commit 1: Add file.txt
  123. String[] content1 = new String[] { "a" };
  124. writeTrashFile("file.txt", join(content1));
  125. git.add().addFilepattern("file.txt").call();
  126. RevCommit commit1 = git.commit().setMessage("create file").call();
  127. // Commit 2: Rename to file1.txt
  128. writeTrashFile("file1.txt", join(content1));
  129. git.add().addFilepattern("file1.txt").call();
  130. git.rm().addFilepattern("file.txt").call();
  131. git.commit().setMessage("moving file").call();
  132. // Commit 3: Edit file1.txt
  133. String[] content2 = new String[] { "a", "b" };
  134. writeTrashFile("file1.txt", join(content2));
  135. git.add().addFilepattern("file1.txt").call();
  136. RevCommit commit3 = git.commit().setMessage("editing file").call();
  137. // Commit 4: Rename to file2.txt
  138. writeTrashFile("file2.txt", join(content2));
  139. git.add().addFilepattern("file2.txt").call();
  140. git.rm().addFilepattern("file1.txt").call();
  141. git.commit().setMessage("moving file again").call();
  142. BlameCommand command = new BlameCommand(db);
  143. command.setFollowFileRenames(true);
  144. command.setFilePath("file2.txt");
  145. BlameResult lines = command.call();
  146. assertEquals(commit1, lines.getSourceCommit(0));
  147. assertEquals(0, lines.getSourceLine(0));
  148. assertEquals("file.txt", lines.getSourcePath(0));
  149. assertEquals(commit3, lines.getSourceCommit(1));
  150. assertEquals(1, lines.getSourceLine(1));
  151. assertEquals("file1.txt", lines.getSourcePath(1));
  152. }
  153. }
  154. @Test
  155. public void testDeleteTrailingLines() throws Exception {
  156. try (Git git = new Git(db)) {
  157. String[] content1 = new String[] { "a", "b", "c", "d" };
  158. String[] content2 = new String[] { "a", "b" };
  159. writeTrashFile("file.txt", join(content2));
  160. git.add().addFilepattern("file.txt").call();
  161. RevCommit commit1 = git.commit().setMessage("create file").call();
  162. writeTrashFile("file.txt", join(content1));
  163. git.add().addFilepattern("file.txt").call();
  164. git.commit().setMessage("edit file").call();
  165. writeTrashFile("file.txt", join(content2));
  166. git.add().addFilepattern("file.txt").call();
  167. git.commit().setMessage("edit file").call();
  168. BlameCommand command = new BlameCommand(db);
  169. command.setFilePath("file.txt");
  170. BlameResult lines = command.call();
  171. assertEquals(content2.length, lines.getResultContents().size());
  172. assertEquals(commit1, lines.getSourceCommit(0));
  173. assertEquals(commit1, lines.getSourceCommit(1));
  174. assertEquals(0, lines.getSourceLine(0));
  175. assertEquals(1, lines.getSourceLine(1));
  176. }
  177. }
  178. @Test
  179. public void testDeleteMiddleLines() throws Exception {
  180. try (Git git = new Git(db)) {
  181. String[] content1 = new String[] { "a", "b", "c", "d", "e" };
  182. String[] content2 = new String[] { "a", "c", "e" };
  183. writeTrashFile("file.txt", join(content2));
  184. git.add().addFilepattern("file.txt").call();
  185. RevCommit commit1 = git.commit().setMessage("edit file").call();
  186. writeTrashFile("file.txt", join(content1));
  187. git.add().addFilepattern("file.txt").call();
  188. git.commit().setMessage("edit file").call();
  189. writeTrashFile("file.txt", join(content2));
  190. git.add().addFilepattern("file.txt").call();
  191. git.commit().setMessage("edit file").call();
  192. BlameCommand command = new BlameCommand(db);
  193. command.setFilePath("file.txt");
  194. BlameResult lines = command.call();
  195. assertEquals(content2.length, lines.getResultContents().size());
  196. assertEquals(commit1, lines.getSourceCommit(0));
  197. assertEquals(0, lines.getSourceLine(0));
  198. assertEquals(commit1, lines.getSourceCommit(1));
  199. assertEquals(1, lines.getSourceLine(1));
  200. assertEquals(commit1, lines.getSourceCommit(2));
  201. assertEquals(2, lines.getSourceLine(2));
  202. }
  203. }
  204. @Test
  205. public void testEditAllLines() throws Exception {
  206. try (Git git = new Git(db)) {
  207. String[] content1 = new String[] { "a", "1" };
  208. String[] content2 = new String[] { "b", "2" };
  209. writeTrashFile("file.txt", join(content1));
  210. git.add().addFilepattern("file.txt").call();
  211. git.commit().setMessage("edit file").call();
  212. writeTrashFile("file.txt", join(content2));
  213. git.add().addFilepattern("file.txt").call();
  214. RevCommit commit2 = git.commit().setMessage("create file").call();
  215. BlameCommand command = new BlameCommand(db);
  216. command.setFilePath("file.txt");
  217. BlameResult lines = command.call();
  218. assertEquals(content2.length, lines.getResultContents().size());
  219. assertEquals(commit2, lines.getSourceCommit(0));
  220. assertEquals(commit2, lines.getSourceCommit(1));
  221. }
  222. }
  223. @Test
  224. public void testMiddleClearAllLines() throws Exception {
  225. try (Git git = new Git(db)) {
  226. String[] content1 = new String[] { "a", "b", "c" };
  227. writeTrashFile("file.txt", join(content1));
  228. git.add().addFilepattern("file.txt").call();
  229. git.commit().setMessage("edit file").call();
  230. writeTrashFile("file.txt", "");
  231. git.add().addFilepattern("file.txt").call();
  232. git.commit().setMessage("create file").call();
  233. writeTrashFile("file.txt", join(content1));
  234. git.add().addFilepattern("file.txt").call();
  235. RevCommit commit3 = git.commit().setMessage("edit file").call();
  236. BlameCommand command = new BlameCommand(db);
  237. command.setFilePath("file.txt");
  238. BlameResult lines = command.call();
  239. assertEquals(content1.length, lines.getResultContents().size());
  240. assertEquals(commit3, lines.getSourceCommit(0));
  241. assertEquals(commit3, lines.getSourceCommit(1));
  242. assertEquals(commit3, lines.getSourceCommit(2));
  243. }
  244. }
  245. @Test
  246. public void testCoreAutoCrlf1() throws Exception {
  247. testCoreAutoCrlf(AutoCRLF.INPUT, AutoCRLF.FALSE);
  248. }
  249. @Test
  250. public void testCoreAutoCrlf2() throws Exception {
  251. testCoreAutoCrlf(AutoCRLF.FALSE, AutoCRLF.FALSE);
  252. }
  253. @Test
  254. public void testCoreAutoCrlf3() throws Exception {
  255. testCoreAutoCrlf(AutoCRLF.INPUT, AutoCRLF.INPUT);
  256. }
  257. @Test
  258. public void testCoreAutoCrlf4() throws Exception {
  259. testCoreAutoCrlf(AutoCRLF.FALSE, AutoCRLF.INPUT);
  260. }
  261. @Test
  262. public void testCoreAutoCrlf5() throws Exception {
  263. testCoreAutoCrlf(AutoCRLF.INPUT, AutoCRLF.TRUE);
  264. }
  265. private void testCoreAutoCrlf(AutoCRLF modeForCommitting,
  266. AutoCRLF modeForReset) throws Exception {
  267. try (Git git = new Git(db)) {
  268. FileBasedConfig config = db.getConfig();
  269. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  270. ConfigConstants.CONFIG_KEY_AUTOCRLF, modeForCommitting);
  271. config.save();
  272. String joinedCrlf = "a\r\nb\r\nc\r\n";
  273. File trashFile = writeTrashFile("file.txt", joinedCrlf);
  274. git.add().addFilepattern("file.txt").call();
  275. RevCommit commit = git.commit().setMessage("create file").call();
  276. // re-create file from the repo
  277. trashFile.delete();
  278. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  279. ConfigConstants.CONFIG_KEY_AUTOCRLF, modeForReset);
  280. config.save();
  281. git.reset().setMode(ResetType.HARD).call();
  282. BlameCommand command = new BlameCommand(db);
  283. command.setFilePath("file.txt");
  284. BlameResult lines = command.call();
  285. assertEquals(3, lines.getResultContents().size());
  286. assertEquals(commit, lines.getSourceCommit(0));
  287. assertEquals(commit, lines.getSourceCommit(1));
  288. assertEquals(commit, lines.getSourceCommit(2));
  289. }
  290. }
  291. @Test
  292. public void testConflictingMerge1() throws Exception {
  293. try (Git git = new Git(db)) {
  294. RevCommit base = commitFile("file.txt", join("0", "1", "2", "3", "4"),
  295. "master");
  296. git.checkout().setName("side").setCreateBranch(true)
  297. .setStartPoint(base).call();
  298. RevCommit side = commitFile("file.txt",
  299. join("0", "1 side", "2", "3 on side", "4"), "side");
  300. commitFile("file.txt", join("0", "1", "2"), "master");
  301. checkoutBranch("refs/heads/master");
  302. git.merge().include(side).call();
  303. // The merge results in a conflict, which we resolve using mostly the
  304. // side branch contents. Especially the "4" survives.
  305. RevCommit merge = commitFile("file.txt",
  306. join("0", "1 side", "2", "3 resolved", "4"), "master");
  307. BlameCommand command = new BlameCommand(db);
  308. command.setFilePath("file.txt");
  309. BlameResult lines = command.call();
  310. assertEquals(5, lines.getResultContents().size());
  311. assertEquals(base, lines.getSourceCommit(0));
  312. assertEquals(side, lines.getSourceCommit(1));
  313. assertEquals(base, lines.getSourceCommit(2));
  314. assertEquals(merge, lines.getSourceCommit(3));
  315. assertEquals(base, lines.getSourceCommit(4));
  316. }
  317. }
  318. // this test inverts the order of the master and side commit and is
  319. // otherwise identical to testConflictingMerge1
  320. @Test
  321. public void testConflictingMerge2() throws Exception {
  322. try (Git git = new Git(db)) {
  323. RevCommit base = commitFile("file.txt", join("0", "1", "2", "3", "4"),
  324. "master");
  325. commitFile("file.txt", join("0", "1", "2"), "master");
  326. git.checkout().setName("side").setCreateBranch(true)
  327. .setStartPoint(base).call();
  328. RevCommit side = commitFile("file.txt",
  329. join("0", "1 side", "2", "3 on side", "4"), "side");
  330. checkoutBranch("refs/heads/master");
  331. git.merge().include(side).call();
  332. // The merge results in a conflict, which we resolve using mostly the
  333. // side branch contents. Especially the "4" survives.
  334. RevCommit merge = commitFile("file.txt",
  335. join("0", "1 side", "2", "3 resolved", "4"), "master");
  336. BlameCommand command = new BlameCommand(db);
  337. command.setFilePath("file.txt");
  338. BlameResult lines = command.call();
  339. assertEquals(5, lines.getResultContents().size());
  340. assertEquals(base, lines.getSourceCommit(0));
  341. assertEquals(side, lines.getSourceCommit(1));
  342. assertEquals(base, lines.getSourceCommit(2));
  343. assertEquals(merge, lines.getSourceCommit(3));
  344. assertEquals(base, lines.getSourceCommit(4));
  345. }
  346. }
  347. @Test
  348. public void testUnresolvedMergeConflict() throws Exception {
  349. try (Git git = new Git(db)) {
  350. RevCommit base = commitFile("file.txt", "Origin\n", "master");
  351. RevCommit master = commitFile("file.txt",
  352. "Change on master branch\n", "master");
  353. git.checkout().setName("side").setCreateBranch(true)
  354. .setStartPoint(base).call();
  355. RevCommit side = commitFile("file.txt",
  356. "Conflicting change on side\n", "side");
  357. checkoutBranch("refs/heads/master");
  358. MergeResult result = git.merge().include(side).call();
  359. // The merge results in a conflict, which we do not resolve
  360. assertTrue("Expected a conflict",
  361. result.getConflicts().containsKey("file.txt"));
  362. BlameCommand command = new BlameCommand(db);
  363. command.setFilePath("file.txt");
  364. BlameResult lines = command.call();
  365. assertEquals(5, lines.getResultContents().size());
  366. assertNull(lines.getSourceCommit(0));
  367. assertEquals(master, lines.getSourceCommit(1));
  368. assertNull(lines.getSourceCommit(2));
  369. assertEquals(side, lines.getSourceCommit(3));
  370. assertNull(lines.getSourceCommit(4));
  371. }
  372. }
  373. @Test
  374. public void testWhitespaceMerge() throws Exception {
  375. try (Git git = new Git(db)) {
  376. RevCommit base = commitFile("file.txt", join("0", "1", "2"), "master");
  377. RevCommit side = commitFile("file.txt", join("0", "1", " 2 side "),
  378. "side");
  379. checkoutBranch("refs/heads/master");
  380. git.merge().setFastForward(FastForwardMode.NO_FF).include(side).call();
  381. // change whitespace, so the merge content is not identical to side, but
  382. // is the same when ignoring whitespace
  383. writeTrashFile("file.txt", join("0", "1", "2 side"));
  384. RevCommit merge = git.commit().setAll(true).setMessage("merge")
  385. .setAmend(true)
  386. .call();
  387. BlameCommand command = new BlameCommand(db);
  388. command.setFilePath("file.txt")
  389. .setTextComparator(RawTextComparator.WS_IGNORE_ALL)
  390. .setStartCommit(merge.getId());
  391. BlameResult lines = command.call();
  392. assertEquals(3, lines.getResultContents().size());
  393. assertEquals(base, lines.getSourceCommit(0));
  394. assertEquals(base, lines.getSourceCommit(1));
  395. assertEquals(side, lines.getSourceCommit(2));
  396. }
  397. }
  398. @Test
  399. public void testBlameWithNulByteInHistory() throws Exception {
  400. try (Git git = new Git(db)) {
  401. String[] content1 = { "First line", "Another line" };
  402. writeTrashFile("file.txt", join(content1));
  403. git.add().addFilepattern("file.txt").call();
  404. RevCommit c1 = git.commit().setMessage("create file").call();
  405. String[] content2 = { "First line", "Second line with NUL >\000<",
  406. "Another line" };
  407. assertTrue("Content should contain a NUL byte",
  408. content2[1].indexOf(0) > 0);
  409. writeTrashFile("file.txt", join(content2));
  410. git.add().addFilepattern("file.txt").call();
  411. git.commit().setMessage("add line with NUL").call();
  412. String[] content3 = { "First line", "Second line with NUL >\000<",
  413. "Third line" };
  414. writeTrashFile("file.txt", join(content3));
  415. git.add().addFilepattern("file.txt").call();
  416. RevCommit c3 = git.commit().setMessage("change third line").call();
  417. String[] content4 = { "First line", "Second line with NUL >\\000<",
  418. "Third line" };
  419. assertTrue("Content should not contain a NUL byte",
  420. content4[1].indexOf(0) < 0);
  421. writeTrashFile("file.txt", join(content4));
  422. git.add().addFilepattern("file.txt").call();
  423. RevCommit c4 = git.commit().setMessage("fix NUL line").call();
  424. BlameResult lines = git.blame().setFilePath("file.txt").call();
  425. assertEquals(3, lines.getResultContents().size());
  426. assertEquals(c1, lines.getSourceCommit(0));
  427. assertEquals(c4, lines.getSourceCommit(1));
  428. assertEquals(c3, lines.getSourceCommit(2));
  429. }
  430. }
  431. @Test
  432. public void testBlameWithNulByteInTopRevision() throws Exception {
  433. try (Git git = new Git(db)) {
  434. String[] content1 = { "First line", "Another line" };
  435. writeTrashFile("file.txt", join(content1));
  436. git.add().addFilepattern("file.txt").call();
  437. RevCommit c1 = git.commit().setMessage("create file").call();
  438. String[] content2 = { "First line", "Second line with NUL >\000<",
  439. "Another line" };
  440. assertTrue("Content should contain a NUL byte",
  441. content2[1].indexOf(0) > 0);
  442. writeTrashFile("file.txt", join(content2));
  443. git.add().addFilepattern("file.txt").call();
  444. RevCommit c2 = git.commit().setMessage("add line with NUL").call();
  445. String[] content3 = { "First line", "Second line with NUL >\000<",
  446. "Third line" };
  447. writeTrashFile("file.txt", join(content3));
  448. git.add().addFilepattern("file.txt").call();
  449. RevCommit c3 = git.commit().setMessage("change third line").call();
  450. BlameResult lines = git.blame().setFilePath("file.txt").call();
  451. assertEquals(3, lines.getResultContents().size());
  452. assertEquals(c1, lines.getSourceCommit(0));
  453. assertEquals(c2, lines.getSourceCommit(1));
  454. assertEquals(c3, lines.getSourceCommit(2));
  455. }
  456. }
  457. }