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

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