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.

ObjectWalkFilterTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2015, Google Inc. 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.assertEquals;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.IOException;
  14. import java.util.Set;
  15. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  16. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  17. import org.eclipse.jgit.junit.TestRepository;
  18. import org.eclipse.jgit.lib.AnyObjectId;
  19. import org.eclipse.jgit.lib.Sets;
  20. import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
  21. import org.eclipse.jgit.revwalk.filter.NotRevFilter;
  22. import org.eclipse.jgit.revwalk.filter.ObjectFilter;
  23. import org.junit.After;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. public class ObjectWalkFilterTest {
  27. private TestRepository<InMemoryRepository> tr;
  28. private ObjectWalk rw;
  29. // 3 commits, 2 top-level trees, 4 subtrees, 3 blobs
  30. private static final int OBJECT_COUNT = 12;
  31. @Before
  32. public void setUp() throws Exception {
  33. tr = new TestRepository<>(new InMemoryRepository(
  34. new DfsRepositoryDescription("test")));
  35. rw = new ObjectWalk(tr.getRepository());
  36. rw.markStart(tr.branch("master").commit()
  37. .add("a/a", "1")
  38. .add("b/b", "2")
  39. .add("c/c", "3")
  40. .message("initial commit")
  41. .child()
  42. .rm("a/a")
  43. .add("a/A", "1")
  44. .message("capitalize a/a")
  45. .child()
  46. .rm("a/A")
  47. .add("a/a", "1")
  48. .message("make a/A lowercase again")
  49. .create());
  50. }
  51. @After
  52. public void tearDown() {
  53. rw.close();
  54. tr.getRepository().close();
  55. }
  56. private static class BlacklistObjectFilter extends ObjectFilter {
  57. final Set<AnyObjectId> badObjects;
  58. BlacklistObjectFilter(Set<AnyObjectId> badObjects) {
  59. this.badObjects = badObjects;
  60. }
  61. @Override
  62. public boolean include(ObjectWalk walker, AnyObjectId o) {
  63. return !badObjects.contains(o);
  64. }
  65. }
  66. private AnyObjectId resolve(String revstr) throws Exception {
  67. return tr.getRepository().resolve(revstr);
  68. }
  69. private int countObjects() throws IOException {
  70. int n = 0;
  71. while (rw.next() != null) {
  72. n++;
  73. }
  74. while (rw.nextObject() != null) {
  75. n++;
  76. }
  77. return n;
  78. }
  79. @Test
  80. public void testDefaultFilter() throws Exception {
  81. assertTrue("filter is ALL",
  82. rw.getObjectFilter() == ObjectFilter.ALL);
  83. assertEquals(OBJECT_COUNT, countObjects());
  84. }
  85. @Test
  86. public void testObjectFilterCanFilterOutBlob() throws Exception {
  87. AnyObjectId one = rw.parseAny(resolve("master:a/a"));
  88. AnyObjectId two = rw.parseAny(resolve("master:b/b"));
  89. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
  90. // 2 blobs filtered out
  91. assertEquals(OBJECT_COUNT - 2, countObjects());
  92. }
  93. @Test
  94. public void testFilteringCommitsHasNoEffect() throws Exception {
  95. AnyObjectId initial = rw.parseCommit(resolve("master^^"));
  96. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial)));
  97. assertEquals(OBJECT_COUNT, countObjects());
  98. }
  99. @Test
  100. public void testRevFilterAndObjectFilterCanCombine() throws Exception {
  101. AnyObjectId one = rw.parseAny(resolve("master:a/a"));
  102. AnyObjectId two = rw.parseAny(resolve("master:b/b"));
  103. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
  104. rw.setRevFilter(NotRevFilter.create(
  105. MessageRevFilter.create("capitalize")));
  106. // 2 blobs, one commit, two trees filtered out
  107. assertEquals(OBJECT_COUNT - 5, countObjects());
  108. }
  109. @Test
  110. public void testFilteringTreeFiltersSubtrees() throws Exception {
  111. AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:"));
  112. rw.setObjectFilter(new BlacklistObjectFilter(
  113. Sets.of(capitalizeTree)));
  114. // trees "master^:" and "master^:a" filtered out
  115. assertEquals(OBJECT_COUNT - 2, countObjects());
  116. }
  117. @Test
  118. public void testFilteringTreeFiltersReferencedBlobs() throws Exception {
  119. AnyObjectId a1 = rw.parseAny(resolve("master:a"));
  120. AnyObjectId a2 = rw.parseAny(resolve("master^:a"));
  121. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2)));
  122. // 2 trees, one blob filtered out
  123. assertEquals(OBJECT_COUNT - 3, countObjects());
  124. }
  125. }