View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.treewalk.filter;
13  
14  import org.eclipse.jgit.internal.JGitText;
15  import org.eclipse.jgit.lib.Constants;
16  import org.eclipse.jgit.treewalk.TreeWalk;
17  
18  /**
19   * Includes tree entries only if they match the configured path.
20   * <p>
21   * Applications should use
22   * {@link org.eclipse.jgit.treewalk.filter.PathFilterGroup} to connect these
23   * into a tree filter graph, as the group supports breaking out of traversal
24   * once it is known the path can never match.
25   */
26  public class PathFilter extends TreeFilter {
27  	/**
28  	 * Create a new tree filter for a user supplied path.
29  	 * <p>
30  	 * Path strings are relative to the root of the repository. If the user's
31  	 * input should be assumed relative to a subdirectory of the repository the
32  	 * caller must prepend the subdirectory's path prior to creating the filter.
33  	 * <p>
34  	 * Path strings use '/' to delimit directories on all platforms.
35  	 *
36  	 * @param path
37  	 *            the path to filter on. Must not be the empty string. All
38  	 *            trailing '/' characters will be trimmed before string's length
39  	 *            is checked or is used as part of the constructed filter.
40  	 * @return a new filter for the requested path.
41  	 * @throws java.lang.IllegalArgumentException
42  	 *             the path supplied was the empty string.
43  	 */
44  	public static PathFilter create(String path) {
45  		while (path.endsWith("/")) //$NON-NLS-1$
46  			path = path.substring(0, path.length() - 1);
47  		if (path.length() == 0)
48  			throw new IllegalArgumentException(JGitText.get().emptyPathNotPermitted);
49  		return new PathFilter(path);
50  	}
51  
52  	final String pathStr;
53  
54  	final byte[] pathRaw;
55  
56  	private PathFilter(String s) {
57  		pathStr = s;
58  		pathRaw = Constants.encode(pathStr);
59  	}
60  
61  	/**
62  	 * Get the path this filter matches.
63  	 *
64  	 * @return the path this filter matches.
65  	 */
66  	public String getPath() {
67  		return pathStr;
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	public boolean include(TreeWalk walker) {
73  		return matchFilter(walker) <= 0;
74  	}
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	public int matchFilter(TreeWalk walker) {
79  		return walker.isPathMatch(pathRaw, pathRaw.length);
80  	}
81  
82  	/** {@inheritDoc} */
83  	@Override
84  	public boolean shouldBeRecursive() {
85  		for (byte b : pathRaw)
86  			if (b == '/')
87  				return true;
88  		return false;
89  	}
90  
91  	/** {@inheritDoc} */
92  	@Override
93  	public PathFilter clone() {
94  		return this;
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	@SuppressWarnings("nls")
100 	public String toString() {
101 		return "PATH(\"" + pathStr + "\")";
102 	}
103 
104 	/**
105 	 * Whether the path length of this filter matches the length of the current
106 	 * path of the supplied TreeWalk.
107 	 *
108 	 * @param walker
109 	 *            The walk to check against.
110 	 * @return {@code true} if the path length of this filter matches the length
111 	 *         of the current path of the supplied TreeWalk.
112 	 */
113 	public boolean isDone(TreeWalk walker) {
114 		return pathRaw.length == walker.getPathLength();
115 	}
116 }