2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.treewalk
.filter
;
40 import java
.io
.UnsupportedEncodingException
;
42 import org
.spearce
.jgit
.lib
.Constants
;
43 import org
.spearce
.jgit
.treewalk
.TreeWalk
;
46 * Includes tree entries only if they match the configured path.
48 * Applications should use {@link PathFilterGroup} to connect these into a tree
49 * filter graph, as the group supports breaking out of traversal once it is
50 * known the path can never match.
52 public class PathFilter
extends TreeFilter
{
54 * Create a new tree filter for a user supplied path.
56 * Path strings are relative to the root of the repository. If the user's
57 * input should be assumed relative to a subdirectory of the repository the
58 * caller must prepend the subdirectory's path prior to creating the filter.
60 * Path strings use '/' to delimit directories on all platforms.
63 * the path to filter on. Must not be the empty string. All
64 * trailing '/' characters will be trimmed before string's length
65 * is checked or is used as part of the constructed filter.
66 * @return a new filter for the requested path.
67 * @throws IllegalArgumentException
68 * the path supplied was the empty string.
70 public static PathFilter
create(String path
) {
71 while (path
.endsWith("/"))
72 path
= path
.substring(0, path
.length() - 1);
73 if (path
.length() == 0)
74 throw new IllegalArgumentException("Empty path not permitted.");
75 return new PathFilter(path
);
82 private PathFilter(final String s
) {
85 pathRaw
= pathStr
.getBytes(Constants
.CHARACTER_ENCODING
);
86 } catch (UnsupportedEncodingException uee
) {
87 throw new RuntimeException("JVM doesn't support "
88 + Constants
.CHARACTER_ENCODING
89 + " which is required for path filtering.", uee
);
94 public boolean include(final TreeWalk walker
) {
95 return walker
.isPathPrefix(pathRaw
, pathRaw
.length
) == 0;
99 public boolean shouldBeRecursive() {
100 for (final byte b
: pathRaw
)
107 public TreeFilter
clone() {
111 public String
toString() {
112 return "PATH(\"" + pathStr
+ "\")";