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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2011, GEBIT Solutions 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.revwalk;
  11. import static org.junit.Assert.assertNull;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import org.eclipse.jgit.diff.DiffConfig;
  15. import org.eclipse.jgit.diff.DiffEntry;
  16. import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
  17. import org.eclipse.jgit.lib.Config;
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class RevWalkFollowFilterTest extends RevWalkTestCase {
  22. private static class DiffCollector extends RenameCallback {
  23. List<DiffEntry> diffs = new ArrayList<>();
  24. @Override
  25. public void renamed(DiffEntry diff) {
  26. diffs.add(diff);
  27. }
  28. }
  29. private DiffCollector diffCollector;
  30. @Before
  31. @Override
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. diffCollector = new DiffCollector();
  35. }
  36. protected FollowFilter follow(String followPath) {
  37. FollowFilter followFilter =
  38. FollowFilter.create(followPath, new Config().get(DiffConfig.KEY));
  39. followFilter.setRenameCallback(diffCollector);
  40. rw.setTreeFilter(followFilter);
  41. return followFilter;
  42. }
  43. @Test
  44. public void testNoRename() throws Exception {
  45. final RevCommit a = commit(tree(file("0", blob("0"))));
  46. follow("0");
  47. markStart(a);
  48. assertCommit(a, rw.next());
  49. assertNull(rw.next());
  50. assertNoRenames();
  51. }
  52. @Test
  53. public void testSingleRename() throws Exception {
  54. final RevCommit a = commit(tree(file("a", blob("A"))));
  55. // rename a to b
  56. CommitBuilder commitBuilder = commitBuilder().parent(a)
  57. .add("b", blob("A")).rm("a");
  58. RevCommit renameCommit = commitBuilder.create();
  59. follow("b");
  60. markStart(renameCommit);
  61. assertCommit(renameCommit, rw.next());
  62. assertCommit(a, rw.next());
  63. assertNull(rw.next());
  64. assertRenames("a->b");
  65. }
  66. @Test
  67. public void testMultiRename() throws Exception {
  68. final String contents = "A";
  69. final RevCommit a = commit(tree(file("a", blob(contents))));
  70. // rename a to b
  71. CommitBuilder commitBuilder = commitBuilder().parent(a)
  72. .add("b", blob(contents)).rm("a");
  73. RevCommit renameCommit1 = commitBuilder.create();
  74. // rename b to c
  75. commitBuilder = commitBuilder().parent(renameCommit1)
  76. .add("c", blob(contents)).rm("b");
  77. RevCommit renameCommit2 = commitBuilder.create();
  78. // rename c to a
  79. commitBuilder = commitBuilder().parent(renameCommit2)
  80. .add("a", blob(contents)).rm("c");
  81. RevCommit renameCommit3 = commitBuilder.create();
  82. follow("a");
  83. markStart(renameCommit3);
  84. assertCommit(renameCommit3, rw.next());
  85. assertCommit(renameCommit2, rw.next());
  86. assertCommit(renameCommit1, rw.next());
  87. assertCommit(a, rw.next());
  88. assertNull(rw.next());
  89. assertRenames("c->a", "b->c", "a->b");
  90. }
  91. /**
  92. * Assert which renames should have happened, in traversal order.
  93. *
  94. * @param expectedRenames
  95. * the rename specs, each one in the form "srcPath-&gt;destPath"
  96. */
  97. protected void assertRenames(String... expectedRenames) {
  98. Assert.assertEquals("Unexpected number of renames. Expected: " +
  99. expectedRenames.length + ", actual: " + diffCollector.diffs.size(),
  100. expectedRenames.length, diffCollector.diffs.size());
  101. for (int i = 0; i < expectedRenames.length; i++) {
  102. DiffEntry diff = diffCollector.diffs.get(i);
  103. Assert.assertNotNull(diff);
  104. String[] split = expectedRenames[i].split("->");
  105. Assert.assertNotNull(split);
  106. Assert.assertEquals(2, split.length);
  107. String src = split[0];
  108. String target = split[1];
  109. Assert.assertEquals(src, diff.getOldPath());
  110. Assert.assertEquals(target, diff.getNewPath());
  111. }
  112. }
  113. protected void assertNoRenames() {
  114. Assert.assertEquals("Found unexpected rename/copy diff", 0,
  115. diffCollector.diffs.size());
  116. }
  117. }