diff options
author | 2016-06-06 17:45:48 +0000 | |
---|---|---|
committer | 2016-06-07 07:48:01 +0000 | |
commit | 7853e042aa484a957d952d848e79ff65e7156550 (patch) | |
tree | 63cc6735908eff80e5fa2b38ac7eecde0cdb1811 /src/main/java/com/google/devtools | |
parent | 146ecf9883883a97cf9c25b0b8e478fc3e5e2c22 (diff) |
Remove the dead UnixGlob#Builder#addExcludes and move some existing tests around so that we still have good test coverage for 'excludes' in globs.
--
MOS_MIGRATED_REVID=124152499
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java | 147 |
1 files changed, 30 insertions, 117 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java b/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java index 42120c4afe..3d28f028fe 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java @@ -36,7 +36,6 @@ import com.google.devtools.build.lib.profiler.ProfilerTask; import com.google.devtools.build.lib.util.Preconditions; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -58,7 +57,6 @@ public final class UnixGlob { private UnixGlob() {} private static List<Path> globInternal(Path base, Collection<String> patterns, - Collection<String> excludePatterns, boolean excludeDirectories, Predicate<Path> dirPred, boolean checkForInterruption, @@ -68,11 +66,10 @@ public final class UnixGlob { GlobVisitor visitor = (threadPool == null) ? new GlobVisitor(checkForInterruption) : new GlobVisitor(threadPool, checkForInterruption); - return visitor.glob(base, patterns, excludePatterns, excludeDirectories, dirPred, syscalls); + return visitor.glob(base, patterns, excludeDirectories, dirPred, syscalls); } private static Future<List<Path>> globAsyncInternal(Path base, Collection<String> patterns, - Collection<String> excludePatterns, boolean excludeDirectories, Predicate<Path> dirPred, FilesystemCalls syscalls, @@ -81,7 +78,7 @@ public final class UnixGlob { Preconditions.checkNotNull(threadPool, "%s %s", base, patterns); try { return new GlobVisitor(threadPool, checkForInterruption) - .globAsync(base, patterns, excludePatterns, excludeDirectories, dirPred, syscalls); + .globAsync(base, patterns, excludeDirectories, dirPred, syscalls); } catch (IOException e) { // We are evaluating asynchronously, so no exceptions should be thrown until the future is // retrieved. @@ -142,54 +139,6 @@ public final class UnixGlob { return null; } - private static boolean excludedOnMatch(Path path, List<String[]> excludePatterns, - int idx, Cache<String, Pattern> cache) { - for (String[] excludePattern : excludePatterns) { - String text = path.getBaseName(); - if (idx == excludePattern.length - && matches(excludePattern[idx - 1], text, cache)) { - return true; - } - } - return false; - } - - /** - * Returns the exclude patterns in {@code excludePatterns} which could - * apply to the children of {@code base} - * - * @param idx index into {@code excludePatterns} for the part of the pattern - * which might match {@code base} - */ - private static List<String[]> getRelevantExcludes( - final Path base, List<String[]> excludePatterns, final int idx, - final Cache<String, Pattern> cache) { - if (excludePatterns.isEmpty()) { - return excludePatterns; - } - List<String[]> list = new ArrayList<>(); - for (String[] patterns : excludePatterns) { - if (excludePatternMatches(patterns, idx, base, cache)) { - list.add(patterns); - } - } - return list; - } - - /** - * @param patterns a list of patterns - * @param idx index into {@code patterns} - */ - private static boolean excludePatternMatches(String[] patterns, int idx, - Path base, - Cache<String, Pattern> cache) { - if (idx == 0) { - return true; - } - String text = base.getBaseName(); - return patterns.length > idx && matches(patterns[idx - 1], text, cache); - } - /** * Calls {@link #matches(String, String, Cache) matches(pattern, str, null)} */ @@ -337,7 +286,6 @@ public final class UnixGlob { public static class Builder { private Path base; private List<String> patterns; - private List<String> excludes; private boolean excludeDirectories; private Predicate<Path> pathFilter; private ThreadPoolExecutor threadPool; @@ -350,7 +298,6 @@ public final class UnixGlob { public Builder(Path base) { this.base = base; this.patterns = Lists.newArrayList(); - this.excludes = Lists.newArrayList(); this.excludeDirectories = false; this.pathFilter = Predicates.alwaysTrue(); } @@ -396,26 +343,6 @@ public final class UnixGlob { } /** - * Adds patterns to exclude from the results to the glob builder. - * - * <p>For a description of the syntax of the patterns, see {@link UnixGlob}. - */ - public Builder addExcludes(String... excludes) { - Collections.addAll(this.excludes, excludes); - return this; - } - - /** - * Adds patterns to exclude from the results to the glob builder. - * - * <p>For a description of the syntax of the patterns, see {@link UnixGlob}. - */ - public Builder addExcludes(Collection<String> excludes) { - this.excludes.addAll(excludes); - return this; - } - - /** * If set to true, directories are not returned in the glob result. */ public Builder setExcludeDirectories(boolean excludeDirectories) { @@ -449,7 +376,7 @@ public final class UnixGlob { */ public List<Path> glob() throws IOException { try { - return globInternal(base, patterns, excludes, excludeDirectories, pathFilter, false, + return globInternal(base, patterns, excludeDirectories, pathFilter, false, syscalls.get(), threadPool); } catch (InterruptedException e) { // cannot happen, since we told globInternal not to throw @@ -463,7 +390,7 @@ public final class UnixGlob { * @throws InterruptedException if the thread is interrupted. */ public List<Path> globInterruptible() throws IOException, InterruptedException { - return globInternal(base, patterns, excludes, excludeDirectories, pathFilter, true, + return globInternal(base, patterns, excludeDirectories, pathFilter, true, syscalls.get(), threadPool); } @@ -474,7 +401,7 @@ public final class UnixGlob { * @param checkForInterrupt if the returned future may throw InterruptedException. */ public Future<List<Path>> globAsync(boolean checkForInterrupt) { - return globAsyncInternal(base, patterns, excludes, excludeDirectories, pathFilter, + return globAsyncInternal(base, patterns, excludeDirectories, pathFilter, syscalls.get(), checkForInterrupt, threadPool); } } @@ -554,9 +481,8 @@ public final class UnixGlob { /** * Performs wildcard globbing: returns the sorted list of filenames that match any of - * {@code patterns} relative to {@code base}, but which do not match {@code excludePatterns}. - * Directories are traversed if and only if they match {@code dirPred}. The predicate is also - * called for the root of the traversal. + * {@code patterns} relative to {@code base}. Directories are traversed if and only if they + * match {@code dirPred}. The predicate is also called for the root of the traversal. * * <p>Patterns may include "*" and "?", but not "[a-z]". * @@ -564,19 +490,16 @@ public final class UnixGlob { * used as a complete path segment it matches the filenames in * subdirectories recursively. * - * @throws IllegalArgumentException if any glob or exclude pattern - * {@linkplain #checkPatternForError(String) contains errors} or if - * any exclude pattern segment contains <code>**</code> or if any - * include pattern segment contains <code>**</code> but not equal to - * it. + * @throws IllegalArgumentException if any glob pattern + * {@linkplain #checkPatternForError(String) contains errors} or if any include pattern + * segment contains <code>**</code> but not equal to it. */ public List<Path> glob(Path base, Collection<String> patterns, - Collection<String> excludePatterns, boolean excludeDirectories, - Predicate<Path> dirPred, FilesystemCalls syscalls) + boolean excludeDirectories, Predicate<Path> dirPred, + FilesystemCalls syscalls) throws IOException, InterruptedException { try { - return globAsync(base, patterns, excludePatterns, excludeDirectories, - dirPred, syscalls).get(); + return globAsync(base, patterns, excludeDirectories, dirPred, syscalls).get(); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfPossible(cause, IOException.class); @@ -585,8 +508,8 @@ public final class UnixGlob { } public Future<List<Path>> globAsync(Path base, Collection<String> patterns, - Collection<String> excludePatterns, boolean excludeDirectories, - Predicate<Path> dirPred, FilesystemCalls syscalls) throws IOException { + boolean excludeDirectories, Predicate<Path> dirPred, FilesystemCalls syscalls) + throws IOException { FileStatus baseStat = syscalls.statNullable(base, Symlinks.FOLLOW); if (baseStat == null || patterns.isEmpty()) { @@ -594,7 +517,6 @@ public final class UnixGlob { } List<String[]> splitPatterns = checkAndSplitPatterns(patterns); - List<String[]> splitExcludes = checkAndSplitPatterns(excludePatterns); // We do a dumb loop, even though it will likely duplicate work // (e.g., readdir calls). In order to optimize, we would need @@ -603,8 +525,8 @@ public final class UnixGlob { pendingOps.incrementAndGet(); try { for (String[] splitPattern : splitPatterns) { - queueGlob(base, baseStat.isDirectory(), splitPattern, 0, excludeDirectories, - splitExcludes, 0, results, cache, dirPred, syscalls); + queueGlob(base, baseStat.isDirectory(), splitPattern, 0, excludeDirectories, results, + cache, dirPred, syscalls); } } finally { decrementAndCheckDone(); @@ -616,8 +538,6 @@ public final class UnixGlob { private void queueGlob(final Path base, final boolean baseIsDir, final String[] patternParts, final int idx, final boolean excludeDirectories, - final List<String[]> excludePatterns, - final int excludeIdx, final Collection<Path> results, final Cache<String, Pattern> cache, final Predicate<Path> dirPred, final FilesystemCalls syscalls) throws IOException { enqueue(new Runnable() { @@ -625,8 +545,8 @@ public final class UnixGlob { public void run() { Profiler.instance().startTask(ProfilerTask.VFS_GLOB, this); try { - reallyGlob(base, baseIsDir, patternParts, idx, excludeDirectories, - excludePatterns, excludeIdx, results, cache, dirPred, syscalls); + reallyGlob(base, baseIsDir, patternParts, idx, excludeDirectories, results, cache, + dirPred, syscalls); } catch (IOException e) { failure.set(e); } finally { @@ -637,10 +557,9 @@ public final class UnixGlob { @Override public String toString() { return String.format( - "%s glob(include=[%s], exclude=[%s], exclude_directories=%s)", + "%s glob(include=[%s], exclude_directories=%s)", base.getPathString(), "\"" + Joiner.on("\", \"").join(patternParts) + "\"", - "\"" + Joiner.on("\", \"").join(excludePatterns) + "\"", excludeDirectories); } }); @@ -697,8 +616,6 @@ public final class UnixGlob { */ private void reallyGlob(Path base, boolean baseIsDir, String[] patternParts, int idx, boolean excludeDirectories, - List<String[]> excludePatterns, - int excludeIdx, Collection<Path> results, Cache<String, Pattern> cache, Predicate<Path> dirPred, FilesystemCalls syscalls) throws IOException { @@ -707,8 +624,7 @@ public final class UnixGlob { } if (idx == patternParts.length) { // Base case. - if (!(excludeDirectories && baseIsDir) && - !excludedOnMatch(base, excludePatterns, excludeIdx, cache)) { + if (!(excludeDirectories && baseIsDir)) { results.add(base); } @@ -720,15 +636,13 @@ public final class UnixGlob { return; } - List<String[]> relevantExcludes - = getRelevantExcludes(base, excludePatterns, excludeIdx, cache); final String pattern = patternParts[idx]; // ** is special: it can match nothing at all. // For example, x/** matches x, **/y matches y, and x/**/y matches x/y. if ("**".equals(pattern)) { - queueGlob(base, baseIsDir, patternParts, idx + 1, excludeDirectories, - excludePatterns, excludeIdx, results, cache, dirPred, syscalls); + queueGlob(base, baseIsDir, patternParts, idx + 1, excludeDirectories, results, cache, + dirPred, syscalls); } if (!pattern.contains("*") && !pattern.contains("?")) { @@ -742,8 +656,8 @@ public final class UnixGlob { boolean childIsDir = status.isDirectory(); - queueGlob(child, childIsDir, patternParts, idx + 1, excludeDirectories, - relevantExcludes, excludeIdx + 1, results, cache, dirPred, syscalls); + queueGlob(child, childIsDir, patternParts, idx + 1, excludeDirectories, results, cache, + dirPred, syscalls); return; } @@ -762,19 +676,18 @@ public final class UnixGlob { if ("**".equals(pattern)) { // Recurse without shifting the pattern. if (childIsDir) { - queueGlob(child, childIsDir, patternParts, idx, excludeDirectories, - relevantExcludes, excludeIdx + 1, results, cache, dirPred, syscalls); + queueGlob(child, childIsDir, patternParts, idx, excludeDirectories, results, cache, + dirPred, syscalls); } } if (matches(pattern, text, cache)) { // Recurse and consume one segment of the pattern. if (childIsDir) { - queueGlob(child, childIsDir, patternParts, idx + 1, excludeDirectories, - relevantExcludes, excludeIdx + 1, results, cache, dirPred, syscalls); + queueGlob(child, childIsDir, patternParts, idx + 1, excludeDirectories, results, cache, + dirPred, syscalls); } else { // Instead of using an async call, just repeat the base case above. - if (idx + 1 == patternParts.length && - !excludedOnMatch(child, relevantExcludes, excludeIdx + 1, cache)) { + if (idx + 1 == patternParts.length) { results.add(child); } } |