aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/util/PathFragmentFilter.java
blob: 34f6cd2c957d915f9ca5877bde9e70717e289056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.util;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.Converter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Handles options that specify list of included/excluded directories.
 * Validates whether path is included in that filter.
 *
 * Excluded directories always take precedence over included ones (path depth
 * and order are not important).
 */
public class PathFragmentFilter implements Serializable {
  private final List<PathFragment> inclusions;
  private final List<PathFragment> exclusions;

  /**
   * Converts from a colon-separated list of of paths with optional '-' prefix into the
   * PathFragmentFilter:
   *   [-]path1[,[-]path2]...
   *
   * Order of paths is not important. Empty entries are ignored. '-' marks an excluded path.
   */
  public static class PathFragmentFilterConverter implements Converter<PathFragmentFilter> {

    @Override
    public PathFragmentFilter convert(String input) {
      List<PathFragment> inclusionList = new ArrayList<>();
      List<PathFragment> exclusionList = new ArrayList<>();

      for (String piece : Splitter.on(',').split(input)) {
        if (piece.length() > 1 && piece.startsWith("-")) {
          exclusionList.add(new PathFragment(piece.substring(1)));
        } else if (piece.length() > 0) {
          inclusionList.add(new PathFragment(piece));
        }
      }

      // TODO(bazel-team): (2010) Both lists could be optimized not to include unnecessary
      // entries - e.g.  entry 'a/b/c' is not needed if 'a/b' is also specified and 'a/b' on
      // inclusion list is not needed if 'a' or 'a/b' is on exclusion list.
      return new PathFragmentFilter(inclusionList, exclusionList);
    }

    @Override
    public String getTypeDescription() {
      return "a comma-separated list of paths with prefix '-' specifying excluded paths";
    }

  }

  /**
   * Creates new PathFragmentFilter using provided inclusion and exclusion path lists.
   */
  public PathFragmentFilter(List<PathFragment> inclusions, List<PathFragment> exclusions) {
    this.inclusions = ImmutableList.copyOf(inclusions);
    this.exclusions = ImmutableList.copyOf(exclusions);
  }

  /**
   * @return true iff path is included (it is not on the exclusion list and
   *         it is either on the inclusion list or inclusion list is empty).
   */
  public boolean isIncluded(PathFragment path) {
    for (PathFragment excludedPath : exclusions) {
      if (path.startsWith(excludedPath)) {
        return false;
      }
    }
    for (PathFragment includedPath : inclusions) {
      if (path.startsWith(includedPath)) {
        return true;
      }
    }
    return inclusions.isEmpty(); // If inclusion filter is not specified, path is included.
  }

  @Override
  public String toString() {
    List<String> list = Lists.newArrayListWithExpectedSize(inclusions.size() + exclusions.size());
    for (PathFragment path : inclusions) {
      list.add(path.getPathString());
    }
    for (PathFragment path : exclusions) {
      list.add("-" + path.getPathString());
    }
    return Joiner.on(',').join(list);
  }
}