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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 org.eclipse.jgit.blame.BlameResult;
  47. import org.eclipse.jgit.lib.RepositoryTestCase;
  48. import org.eclipse.jgit.revwalk.RevCommit;
  49. import org.junit.Test;
  50. /**
  51. * Unit tests of {@link BlameCommand}
  52. */
  53. public class BlameCommandTest extends RepositoryTestCase {
  54. private String join(String... lines) {
  55. StringBuilder joined = new StringBuilder();
  56. for (String line : lines)
  57. joined.append(line).append('\n');
  58. return joined.toString();
  59. }
  60. @Test
  61. public void testSingleRevision() throws Exception {
  62. Git git = new Git(db);
  63. String[] content = new String[] { "first", "second", "third" };
  64. writeTrashFile("file.txt", join(content));
  65. git.add().addFilepattern("file.txt").call();
  66. RevCommit commit = git.commit().setMessage("create file").call();
  67. BlameCommand command = new BlameCommand(db);
  68. command.setFilePath("file.txt");
  69. BlameResult lines = command.call();
  70. assertNotNull(lines);
  71. assertEquals(3, lines.getResultContents().size());
  72. for (int i = 0; i < 3; i++) {
  73. assertEquals(commit, lines.getSourceCommit(i));
  74. assertEquals(i, lines.getSourceLine(i));
  75. }
  76. }
  77. @Test
  78. public void testTwoRevisions() throws Exception {
  79. Git git = new Git(db);
  80. String[] content1 = new String[] { "first", "second" };
  81. writeTrashFile("file.txt", join(content1));
  82. git.add().addFilepattern("file.txt").call();
  83. RevCommit commit1 = git.commit().setMessage("create file").call();
  84. String[] content2 = new String[] { "first", "second", "third" };
  85. writeTrashFile("file.txt", join(content2));
  86. git.add().addFilepattern("file.txt").call();
  87. RevCommit commit2 = git.commit().setMessage("create file").call();
  88. BlameCommand command = new BlameCommand(db);
  89. command.setFilePath("file.txt");
  90. BlameResult lines = command.call();
  91. assertEquals(3, lines.getResultContents().size());
  92. assertEquals(commit1, lines.getSourceCommit(0));
  93. assertEquals(0, lines.getSourceLine(0));
  94. assertEquals(commit1, lines.getSourceCommit(1));
  95. assertEquals(1, lines.getSourceLine(1));
  96. assertEquals(commit2, lines.getSourceCommit(2));
  97. assertEquals(2, lines.getSourceLine(2));
  98. }
  99. @Test
  100. public void testRename() throws Exception {
  101. testRename("file1.txt", "file2.txt");
  102. }
  103. @Test
  104. public void testRenameInSubDir() throws Exception {
  105. testRename("subdir/file1.txt", "subdir/file2.txt");
  106. }
  107. @Test
  108. public void testMoveToOtherDir() throws Exception {
  109. testRename("subdir/file1.txt", "otherdir/file1.txt");
  110. }
  111. private void testRename(final String sourcePath, final String destPath)
  112. throws Exception {
  113. Git git = new Git(db);
  114. String[] content1 = new String[] { "a", "b", "c" };
  115. writeTrashFile(sourcePath, join(content1));
  116. git.add().addFilepattern(sourcePath).call();
  117. RevCommit commit1 = git.commit().setMessage("create file").call();
  118. writeTrashFile(destPath, join(content1));
  119. git.add().addFilepattern(destPath).call();
  120. git.rm().addFilepattern(sourcePath).call();
  121. git.commit().setMessage("moving file").call();
  122. String[] content2 = new String[] { "a", "b", "c2" };
  123. writeTrashFile(destPath, join(content2));
  124. git.add().addFilepattern(destPath).call();
  125. RevCommit commit3 = git.commit().setMessage("editing file").call();
  126. BlameCommand command = new BlameCommand(db);
  127. command.setFollowFileRenames(true);
  128. command.setFilePath(destPath);
  129. BlameResult lines = command.call();
  130. assertEquals(commit1, lines.getSourceCommit(0));
  131. assertEquals(0, lines.getSourceLine(0));
  132. assertEquals(sourcePath, lines.getSourcePath(0));
  133. assertEquals(commit1, lines.getSourceCommit(1));
  134. assertEquals(1, lines.getSourceLine(1));
  135. assertEquals(sourcePath, lines.getSourcePath(1));
  136. assertEquals(commit3, lines.getSourceCommit(2));
  137. assertEquals(2, lines.getSourceLine(2));
  138. assertEquals(destPath, lines.getSourcePath(2));
  139. }
  140. @Test
  141. public void testTwoRenames() throws Exception {
  142. Git git = new Git(db);
  143. // Commit 1: Add file.txt
  144. String[] content1 = new String[] { "a" };
  145. writeTrashFile("file.txt", join(content1));
  146. git.add().addFilepattern("file.txt").call();
  147. RevCommit commit1 = git.commit().setMessage("create file").call();
  148. // Commit 2: Rename to file1.txt
  149. writeTrashFile("file1.txt", join(content1));
  150. git.add().addFilepattern("file1.txt").call();
  151. git.rm().addFilepattern("file.txt").call();
  152. git.commit().setMessage("moving file").call();
  153. // Commit 3: Edit file1.txt
  154. String[] content2 = new String[] { "a", "b" };
  155. writeTrashFile("file1.txt", join(content2));
  156. git.add().addFilepattern("file1.txt").call();
  157. RevCommit commit3 = git.commit().setMessage("editing file").call();
  158. // Commit 4: Rename to file2.txt
  159. writeTrashFile("file2.txt", join(content2));
  160. git.add().addFilepattern("file2.txt").call();
  161. git.rm().addFilepattern("file1.txt").call();
  162. git.commit().setMessage("moving file again").call();
  163. BlameCommand command = new BlameCommand(db);
  164. command.setFollowFileRenames(true);
  165. command.setFilePath("file2.txt");
  166. BlameResult lines = command.call();
  167. assertEquals(commit1, lines.getSourceCommit(0));
  168. assertEquals(0, lines.getSourceLine(0));
  169. assertEquals("file.txt", lines.getSourcePath(0));
  170. assertEquals(commit3, lines.getSourceCommit(1));
  171. assertEquals(1, lines.getSourceLine(1));
  172. assertEquals("file1.txt", lines.getSourcePath(1));
  173. }
  174. @Test
  175. public void testDeleteTrailingLines() throws Exception {
  176. Git git = new Git(db);
  177. String[] content1 = new String[] { "a", "b", "c", "d" };
  178. String[] content2 = new String[] { "a", "b" };
  179. writeTrashFile("file.txt", join(content2));
  180. git.add().addFilepattern("file.txt").call();
  181. RevCommit commit1 = git.commit().setMessage("create file").call();
  182. writeTrashFile("file.txt", join(content1));
  183. git.add().addFilepattern("file.txt").call();
  184. git.commit().setMessage("edit file").call();
  185. writeTrashFile("file.txt", join(content2));
  186. git.add().addFilepattern("file.txt").call();
  187. git.commit().setMessage("edit file").call();
  188. BlameCommand command = new BlameCommand(db);
  189. command.setFilePath("file.txt");
  190. BlameResult lines = command.call();
  191. assertEquals(content2.length, lines.getResultContents().size());
  192. assertEquals(commit1, lines.getSourceCommit(0));
  193. assertEquals(commit1, lines.getSourceCommit(1));
  194. assertEquals(0, lines.getSourceLine(0));
  195. assertEquals(1, lines.getSourceLine(1));
  196. }
  197. @Test
  198. public void testDeleteMiddleLines() throws Exception {
  199. Git git = new Git(db);
  200. String[] content1 = new String[] { "a", "b", "c", "d", "e" };
  201. String[] content2 = new String[] { "a", "c", "e" };
  202. writeTrashFile("file.txt", join(content2));
  203. git.add().addFilepattern("file.txt").call();
  204. RevCommit commit1 = git.commit().setMessage("edit file").call();
  205. writeTrashFile("file.txt", join(content1));
  206. git.add().addFilepattern("file.txt").call();
  207. git.commit().setMessage("edit file").call();
  208. writeTrashFile("file.txt", join(content2));
  209. git.add().addFilepattern("file.txt").call();
  210. git.commit().setMessage("edit file").call();
  211. BlameCommand command = new BlameCommand(db);
  212. command.setFilePath("file.txt");
  213. BlameResult lines = command.call();
  214. assertEquals(content2.length, lines.getResultContents().size());
  215. assertEquals(commit1, lines.getSourceCommit(0));
  216. assertEquals(0, lines.getSourceLine(0));
  217. assertEquals(commit1, lines.getSourceCommit(1));
  218. assertEquals(1, lines.getSourceLine(1));
  219. assertEquals(commit1, lines.getSourceCommit(2));
  220. assertEquals(2, lines.getSourceLine(2));
  221. }
  222. @Test
  223. public void testEditAllLines() throws Exception {
  224. Git git = new Git(db);
  225. String[] content1 = new String[] { "a", "1" };
  226. String[] content2 = new String[] { "b", "2" };
  227. writeTrashFile("file.txt", join(content1));
  228. git.add().addFilepattern("file.txt").call();
  229. git.commit().setMessage("edit file").call();
  230. writeTrashFile("file.txt", join(content2));
  231. git.add().addFilepattern("file.txt").call();
  232. RevCommit commit2 = git.commit().setMessage("create file").call();
  233. BlameCommand command = new BlameCommand(db);
  234. command.setFilePath("file.txt");
  235. BlameResult lines = command.call();
  236. assertEquals(content2.length, lines.getResultContents().size());
  237. assertEquals(commit2, lines.getSourceCommit(0));
  238. assertEquals(commit2, lines.getSourceCommit(1));
  239. }
  240. @Test
  241. public void testMiddleClearAllLines() throws Exception {
  242. Git git = new Git(db);
  243. String[] content1 = new String[] { "a", "b", "c" };
  244. writeTrashFile("file.txt", join(content1));
  245. git.add().addFilepattern("file.txt").call();
  246. git.commit().setMessage("edit file").call();
  247. writeTrashFile("file.txt", "");
  248. git.add().addFilepattern("file.txt").call();
  249. git.commit().setMessage("create file").call();
  250. writeTrashFile("file.txt", join(content1));
  251. git.add().addFilepattern("file.txt").call();
  252. RevCommit commit3 = git.commit().setMessage("edit file").call();
  253. BlameCommand command = new BlameCommand(db);
  254. command.setFilePath("file.txt");
  255. BlameResult lines = command.call();
  256. assertEquals(content1.length, lines.getResultContents().size());
  257. assertEquals(commit3, lines.getSourceCommit(0));
  258. assertEquals(commit3, lines.getSourceCommit(1));
  259. assertEquals(commit3, lines.getSourceCommit(2));
  260. }
  261. }