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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2015, Google 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.revwalk;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.IOException;
  47. import java.util.Set;
  48. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  49. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  50. import org.eclipse.jgit.junit.TestRepository;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Sets;
  53. import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
  54. import org.eclipse.jgit.revwalk.filter.NotRevFilter;
  55. import org.eclipse.jgit.revwalk.filter.ObjectFilter;
  56. import org.junit.After;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class ObjectWalkFilterTest {
  60. private TestRepository<InMemoryRepository> tr;
  61. private ObjectWalk rw;
  62. // 3 commits, 2 top-level trees, 4 subtrees, 3 blobs
  63. private static final int OBJECT_COUNT = 12;
  64. @Before
  65. public void setUp() throws Exception {
  66. tr = new TestRepository<>(new InMemoryRepository(
  67. new DfsRepositoryDescription("test")));
  68. rw = new ObjectWalk(tr.getRepository());
  69. rw.markStart(tr.branch("master").commit()
  70. .add("a/a", "1")
  71. .add("b/b", "2")
  72. .add("c/c", "3")
  73. .message("initial commit")
  74. .child()
  75. .rm("a/a")
  76. .add("a/A", "1")
  77. .message("capitalize a/a")
  78. .child()
  79. .rm("a/A")
  80. .add("a/a", "1")
  81. .message("make a/A lowercase again")
  82. .create());
  83. }
  84. @After
  85. public void tearDown() {
  86. rw.close();
  87. tr.getRepository().close();
  88. }
  89. private static class BlacklistObjectFilter extends ObjectFilter {
  90. final Set<AnyObjectId> badObjects;
  91. BlacklistObjectFilter(Set<AnyObjectId> badObjects) {
  92. this.badObjects = badObjects;
  93. }
  94. @Override
  95. public boolean include(ObjectWalk walker, AnyObjectId o) {
  96. return !badObjects.contains(o);
  97. }
  98. }
  99. private AnyObjectId resolve(String revstr) throws Exception {
  100. return tr.getRepository().resolve(revstr);
  101. }
  102. private int countObjects() throws IOException {
  103. int n = 0;
  104. while (rw.next() != null) {
  105. n++;
  106. }
  107. while (rw.nextObject() != null) {
  108. n++;
  109. }
  110. return n;
  111. }
  112. @Test
  113. public void testDefaultFilter() throws Exception {
  114. assertTrue("filter is ALL",
  115. rw.getObjectFilter() == ObjectFilter.ALL);
  116. assertEquals(OBJECT_COUNT, countObjects());
  117. }
  118. @Test
  119. public void testObjectFilterCanFilterOutBlob() throws Exception {
  120. AnyObjectId one = rw.parseAny(resolve("master:a/a"));
  121. AnyObjectId two = rw.parseAny(resolve("master:b/b"));
  122. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
  123. // 2 blobs filtered out
  124. assertEquals(OBJECT_COUNT - 2, countObjects());
  125. }
  126. @Test
  127. public void testFilteringCommitsHasNoEffect() throws Exception {
  128. AnyObjectId initial = rw.parseCommit(resolve("master^^"));
  129. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial)));
  130. assertEquals(OBJECT_COUNT, countObjects());
  131. }
  132. @Test
  133. public void testRevFilterAndObjectFilterCanCombine() throws Exception {
  134. AnyObjectId one = rw.parseAny(resolve("master:a/a"));
  135. AnyObjectId two = rw.parseAny(resolve("master:b/b"));
  136. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
  137. rw.setRevFilter(NotRevFilter.create(
  138. MessageRevFilter.create("capitalize")));
  139. // 2 blobs, one commit, two trees filtered out
  140. assertEquals(OBJECT_COUNT - 5, countObjects());
  141. }
  142. @Test
  143. public void testFilteringTreeFiltersSubtrees() throws Exception {
  144. AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:"));
  145. rw.setObjectFilter(new BlacklistObjectFilter(
  146. Sets.of(capitalizeTree)));
  147. // trees "master^:" and "master^:a" filtered out
  148. assertEquals(OBJECT_COUNT - 2, countObjects());
  149. }
  150. @Test
  151. public void testFilteringTreeFiltersReferencedBlobs() throws Exception {
  152. AnyObjectId a1 = rw.parseAny(resolve("master:a"));
  153. AnyObjectId a2 = rw.parseAny(resolve("master^:a"));
  154. rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2)));
  155. // 2 trees, one blob filtered out
  156. assertEquals(OBJECT_COUNT - 3, countObjects());
  157. }
  158. }