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.

BlameGeneratorTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.blame;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import org.eclipse.jgit.api.Git;
  48. import org.eclipse.jgit.blame.BlameGenerator;
  49. import org.eclipse.jgit.blame.BlameResult;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.junit.Test;
  54. /** Unit tests of {@link BlameGenerator}. */
  55. public class BlameGeneratorTest extends RepositoryTestCase {
  56. @Test
  57. public void testBoundLineDelete() throws Exception {
  58. try (Git git = new Git(db)) {
  59. String[] content1 = new String[] { "first", "second" };
  60. writeTrashFile("file.txt", join(content1));
  61. git.add().addFilepattern("file.txt").call();
  62. RevCommit c1 = git.commit().setMessage("create file").call();
  63. String[] content2 = new String[] { "third", "first", "second" };
  64. writeTrashFile("file.txt", join(content2));
  65. git.add().addFilepattern("file.txt").call();
  66. RevCommit c2 = git.commit().setMessage("create file").call();
  67. try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
  68. generator.push(null, db.resolve(Constants.HEAD));
  69. assertEquals(3, generator.getResultContents().size());
  70. assertTrue(generator.next());
  71. assertEquals(c2, generator.getSourceCommit());
  72. assertEquals(1, generator.getRegionLength());
  73. assertEquals(0, generator.getResultStart());
  74. assertEquals(1, generator.getResultEnd());
  75. assertEquals(0, generator.getSourceStart());
  76. assertEquals(1, generator.getSourceEnd());
  77. assertEquals("file.txt", generator.getSourcePath());
  78. assertTrue(generator.next());
  79. assertEquals(c1, generator.getSourceCommit());
  80. assertEquals(2, generator.getRegionLength());
  81. assertEquals(1, generator.getResultStart());
  82. assertEquals(3, generator.getResultEnd());
  83. assertEquals(0, generator.getSourceStart());
  84. assertEquals(2, generator.getSourceEnd());
  85. assertEquals("file.txt", generator.getSourcePath());
  86. assertFalse(generator.next());
  87. }
  88. }
  89. }
  90. @Test
  91. public void testRenamedBoundLineDelete() throws Exception {
  92. try (Git git = new Git(db)) {
  93. final String FILENAME_1 = "subdir/file1.txt";
  94. final String FILENAME_2 = "subdir/file2.txt";
  95. String[] content1 = new String[] { "first", "second" };
  96. writeTrashFile(FILENAME_1, join(content1));
  97. git.add().addFilepattern(FILENAME_1).call();
  98. RevCommit c1 = git.commit().setMessage("create file1").call();
  99. // rename it
  100. writeTrashFile(FILENAME_2, join(content1));
  101. git.add().addFilepattern(FILENAME_2).call();
  102. deleteTrashFile(FILENAME_1);
  103. git.rm().addFilepattern(FILENAME_1).call();
  104. git.commit().setMessage("rename file1.txt to file2.txt").call();
  105. // and change the new file
  106. String[] content2 = new String[] { "third", "first", "second" };
  107. writeTrashFile(FILENAME_2, join(content2));
  108. git.add().addFilepattern(FILENAME_2).call();
  109. RevCommit c2 = git.commit().setMessage("change file2").call();
  110. try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
  111. generator.push(null, db.resolve(Constants.HEAD));
  112. assertEquals(3, generator.getResultContents().size());
  113. assertTrue(generator.next());
  114. assertEquals(c2, generator.getSourceCommit());
  115. assertEquals(1, generator.getRegionLength());
  116. assertEquals(0, generator.getResultStart());
  117. assertEquals(1, generator.getResultEnd());
  118. assertEquals(0, generator.getSourceStart());
  119. assertEquals(1, generator.getSourceEnd());
  120. assertEquals(FILENAME_2, generator.getSourcePath());
  121. assertTrue(generator.next());
  122. assertEquals(c1, generator.getSourceCommit());
  123. assertEquals(2, generator.getRegionLength());
  124. assertEquals(1, generator.getResultStart());
  125. assertEquals(3, generator.getResultEnd());
  126. assertEquals(0, generator.getSourceStart());
  127. assertEquals(2, generator.getSourceEnd());
  128. assertEquals(FILENAME_1, generator.getSourcePath());
  129. assertFalse(generator.next());
  130. }
  131. // and test again with other BlameGenerator API:
  132. try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
  133. generator.push(null, db.resolve(Constants.HEAD));
  134. BlameResult result = generator.computeBlameResult();
  135. assertEquals(3, result.getResultContents().size());
  136. assertEquals(c2, result.getSourceCommit(0));
  137. assertEquals(FILENAME_2, result.getSourcePath(0));
  138. assertEquals(c1, result.getSourceCommit(1));
  139. assertEquals(FILENAME_1, result.getSourcePath(1));
  140. assertEquals(c1, result.getSourceCommit(2));
  141. assertEquals(FILENAME_1, result.getSourcePath(2));
  142. }
  143. }
  144. }
  145. @Test
  146. public void testLinesAllDeletedShortenedWalk() throws Exception {
  147. try (Git git = new Git(db)) {
  148. String[] content1 = new String[] { "first", "second", "third" };
  149. writeTrashFile("file.txt", join(content1));
  150. git.add().addFilepattern("file.txt").call();
  151. git.commit().setMessage("create file").call();
  152. String[] content2 = new String[] { "" };
  153. writeTrashFile("file.txt", join(content2));
  154. git.add().addFilepattern("file.txt").call();
  155. git.commit().setMessage("create file").call();
  156. writeTrashFile("file.txt", join(content1));
  157. git.add().addFilepattern("file.txt").call();
  158. RevCommit c3 = git.commit().setMessage("create file").call();
  159. try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
  160. generator.push(null, db.resolve(Constants.HEAD));
  161. assertEquals(3, generator.getResultContents().size());
  162. assertTrue(generator.next());
  163. assertEquals(c3, generator.getSourceCommit());
  164. assertEquals(0, generator.getResultStart());
  165. assertEquals(3, generator.getResultEnd());
  166. assertFalse(generator.next());
  167. }
  168. }
  169. }
  170. private static String join(String... lines) {
  171. StringBuilder joined = new StringBuilder();
  172. for (String line : lines)
  173. joined.append(line).append('\n');
  174. return joined.toString();
  175. }
  176. }