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.

RevWalkFollowFilterTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2011, GEBIT Solutions
  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.revwalk;
  44. import static org.junit.Assert.assertNull;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import org.eclipse.jgit.diff.DiffConfig;
  48. import org.eclipse.jgit.diff.DiffEntry;
  49. import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
  50. import org.eclipse.jgit.lib.Config;
  51. import org.junit.Assert;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class RevWalkFollowFilterTest extends RevWalkTestCase {
  55. private static class DiffCollector extends RenameCallback {
  56. List<DiffEntry> diffs = new ArrayList<>();
  57. @Override
  58. public void renamed(DiffEntry diff) {
  59. diffs.add(diff);
  60. }
  61. }
  62. private DiffCollector diffCollector;
  63. @Before
  64. @Override
  65. public void setUp() throws Exception {
  66. super.setUp();
  67. diffCollector = new DiffCollector();
  68. }
  69. protected FollowFilter follow(final String followPath) {
  70. FollowFilter followFilter =
  71. FollowFilter.create(followPath, new Config().get(DiffConfig.KEY));
  72. followFilter.setRenameCallback(diffCollector);
  73. rw.setTreeFilter(followFilter);
  74. return followFilter;
  75. }
  76. @Test
  77. public void testNoRename() throws Exception {
  78. final RevCommit a = commit(tree(file("0", blob("0"))));
  79. follow("0");
  80. markStart(a);
  81. assertCommit(a, rw.next());
  82. assertNull(rw.next());
  83. assertNoRenames();
  84. }
  85. @Test
  86. public void testSingleRename() throws Exception {
  87. final RevCommit a = commit(tree(file("a", blob("A"))));
  88. // rename a to b
  89. CommitBuilder commitBuilder = commitBuilder().parent(a)
  90. .add("b", blob("A")).rm("a");
  91. RevCommit renameCommit = commitBuilder.create();
  92. follow("b");
  93. markStart(renameCommit);
  94. assertCommit(renameCommit, rw.next());
  95. assertCommit(a, rw.next());
  96. assertNull(rw.next());
  97. assertRenames("a->b");
  98. }
  99. @Test
  100. public void testMultiRename() throws Exception {
  101. final String contents = "A";
  102. final RevCommit a = commit(tree(file("a", blob(contents))));
  103. // rename a to b
  104. CommitBuilder commitBuilder = commitBuilder().parent(a)
  105. .add("b", blob(contents)).rm("a");
  106. RevCommit renameCommit1 = commitBuilder.create();
  107. // rename b to c
  108. commitBuilder = commitBuilder().parent(renameCommit1)
  109. .add("c", blob(contents)).rm("b");
  110. RevCommit renameCommit2 = commitBuilder.create();
  111. // rename c to a
  112. commitBuilder = commitBuilder().parent(renameCommit2)
  113. .add("a", blob(contents)).rm("c");
  114. RevCommit renameCommit3 = commitBuilder.create();
  115. follow("a");
  116. markStart(renameCommit3);
  117. assertCommit(renameCommit3, rw.next());
  118. assertCommit(renameCommit2, rw.next());
  119. assertCommit(renameCommit1, rw.next());
  120. assertCommit(a, rw.next());
  121. assertNull(rw.next());
  122. assertRenames("c->a", "b->c", "a->b");
  123. }
  124. /**
  125. * Assert which renames should have happened, in traversal order.
  126. *
  127. * @param expectedRenames
  128. * the rename specs, each one in the form "srcPath-&gt;destPath"
  129. */
  130. protected void assertRenames(String... expectedRenames) {
  131. Assert.assertEquals("Unexpected number of renames. Expected: " +
  132. expectedRenames.length + ", actual: " + diffCollector.diffs.size(),
  133. expectedRenames.length, diffCollector.diffs.size());
  134. for (int i = 0; i < expectedRenames.length; i++) {
  135. DiffEntry diff = diffCollector.diffs.get(i);
  136. Assert.assertNotNull(diff);
  137. String[] split = expectedRenames[i].split("->");
  138. Assert.assertNotNull(split);
  139. Assert.assertEquals(2, split.length);
  140. String src = split[0];
  141. String target = split[1];
  142. Assert.assertEquals(src, diff.getOldPath());
  143. Assert.assertEquals(target, diff.getNewPath());
  144. }
  145. }
  146. protected void assertNoRenames() {
  147. Assert.assertEquals("Found unexpected rename/copy diff", 0,
  148. diffCollector.diffs.size());
  149. }
  150. }