]> source.dussan.org Git - jgit.git/commitdiff
PathFilterGroup: implement getPathsBestEffort() 17/1184117/2
authorXing Huang <xingkhuang@google.com>
Fri, 15 Mar 2024 20:19:15 +0000 (15:19 -0500)
committerXing Huang <xingkhuang@google.com>
Mon, 18 Mar 2024 15:51:11 +0000 (10:51 -0500)
getPathsBestEffort() is a method in the TreeFilter class
to retrieve file paths specified by the caller. PathFilterGroup do not
propagate the paths of their subfilters as it does not implement the
getPathsBestEffort() method, resulting in the caller only getting an
empty list of paths.

Override getPathsBestEffort() in PathFilterGroup to propagate subfilter
values.

Signed-off-by: Xing Huang <xingkhuang@google.com>
Change-Id: I76bf08795360abc0874a7c258636d4f37da35060

org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/ByteArraySet.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/PathFilterGroup.java

index 32bd40312f19836a58db26039d017a18e8430620..1bb4939c8586372b8883d4ad034cef6d1ba69e00 100644 (file)
@@ -11,6 +11,7 @@
 package org.eclipse.jgit.treewalk.filter;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -21,7 +22,9 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheEditor;
@@ -30,6 +33,7 @@ import org.eclipse.jgit.dircache.DirCacheIterator;
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.errors.StopWalkException;
+import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.FileMode;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.Sets;
@@ -142,6 +146,29 @@ public class PathFilterGroupTest {
                assertFalse(longPathFilter.include(fakeWalk("a-nothing-in-common")));
        }
 
+       @Test
+       public void testGetPathsBestEffort() {
+               String[] paths = { "path1", "path2", "path3" };
+               Set<byte[]> expected = Arrays.stream(paths).map(Constants::encode)
+                               .collect(Collectors.toSet());
+               TreeFilter pathFilterGroup = PathFilterGroup.createFromStrings(paths);
+               Optional<Set<byte[]>> bestEffortPaths = pathFilterGroup
+                               .getPathsBestEffort();
+               assertTrue(bestEffortPaths.isPresent());
+               Set<byte[]> actual = bestEffortPaths.get();
+               assertEquals(expected.size(), actual.size());
+               for (byte[] actualPath : actual) {
+                       boolean findMatch = false;
+                       for (byte[] expectedPath : expected) {
+                               if (Arrays.equals(actualPath, expectedPath)) {
+                                       findMatch = true;
+                                       break;
+                               }
+                       }
+                       assertTrue(findMatch);
+               }
+       }
+
        @Test
        public void testStopWalk() throws MissingObjectException,
                        IncorrectObjectTypeException, IOException {
index c94160144ebf001e1437166c642e80d09f2c6318..bcf79a285d3bbbf9ed6edc7186ce96879864ef4f 100644 (file)
@@ -15,6 +15,10 @@ package org.eclipse.jgit.treewalk.filter;
 
 import org.eclipse.jgit.util.RawParseUtils;
 
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+
 /**
  * Specialized set for byte arrays, interpreted as strings for use in
  * {@link PathFilterGroup.Group}. Most methods assume the hash is already know
@@ -291,4 +295,8 @@ class ByteArraySet {
                return ret;
        }
 
+       Set<byte[]> toSet() {
+               return Arrays.stream(toArray()).collect(Collectors.toSet());
+       }
+
 }
index 59855572f235d69bba891a90cd45641ed4249dc8..4c0604ad56914ece963f7a1e4e5bd14e84c549ec 100644 (file)
@@ -12,6 +12,8 @@
 package org.eclipse.jgit.treewalk.filter;
 
 import java.util.Collection;
+import java.util.Optional;
+import java.util.Set;
 
 import org.eclipse.jgit.errors.StopWalkException;
 import org.eclipse.jgit.internal.JGitText;
@@ -231,6 +233,15 @@ public class PathFilterGroup {
                        return !prefixes.isEmpty();
                }
 
+               @Override
+               public Optional<Set<byte[]>> getPathsBestEffort() {
+                       Set<byte[]> result = fullpaths.toSet();
+                       if (result.isEmpty()) {
+                               return Optional.empty();
+                       }
+                       return Optional.of(result);
+               }
+
                @Override
                public TreeFilter clone() {
                        return this;