2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
using System.Collections.Generic;
50
using NGit.Treewalk.Filter;
53
namespace NGit.Treewalk.Filter
55
/// <summary>Includes tree entries only if they match one or more configured paths.</summary>
57
/// Includes tree entries only if they match one or more configured paths.
60
/// <see cref="PathFilter">PathFilter</see>
61
/// but causes the walk to abort as soon as the
62
/// tree can no longer match any of the paths within the group. This may bypass
63
/// the boolean logic of a higher level AND or OR group, but does improve
64
/// performance for the common case of examining one or more modified paths.
66
/// This filter is effectively an OR group around paths, with the early abort
67
/// feature described above.
69
public class PathFilterGroup
71
/// <summary>Create a collection of path filters from Java strings.</summary>
73
/// Create a collection of path filters from Java strings.
75
/// Path strings are relative to the root of the repository. If the user's
76
/// input should be assumed relative to a subdirectory of the repository the
77
/// caller must prepend the subdirectory's path prior to creating the filter.
79
/// Path strings use '/' to delimit directories on all platforms.
81
/// Paths may appear in any order within the collection. Sorting may be done
82
/// internally when the group is constructed if doing so will improve path
83
/// matching performance.
85
/// <param name="paths">the paths to test against. Must have at least one entry.</param>
86
/// <returns>a new filter for the list of paths supplied.</returns>
87
public static TreeFilter CreateFromStrings(ICollection<string> paths)
91
throw new ArgumentException(JGitText.Get().atLeastOnePathIsRequired);
93
PathFilter[] p = new PathFilter[paths.Count];
95
foreach (string s in paths)
97
p[i++] = PathFilter.Create(s);
102
/// <summary>Create a collection of path filters.</summary>
104
/// Create a collection of path filters.
106
/// Paths may appear in any order within the collection. Sorting may be done
107
/// internally when the group is constructed if doing so will improve path
108
/// matching performance.
110
/// <param name="paths">the paths to test against. Must have at least one entry.</param>
111
/// <returns>a new filter for the list of paths supplied.</returns>
112
public static TreeFilter Create(ICollection<PathFilter> paths)
116
throw new ArgumentException(JGitText.Get().atLeastOnePathIsRequired);
118
PathFilter[] p = new PathFilter[paths.Count];
119
Sharpen.Collections.ToArray(paths, p);
123
private static TreeFilter Create(PathFilter[] p)
127
return new PathFilterGroup.Single(p[0]);
129
return new PathFilterGroup.Group(p);
132
internal class Single : TreeFilter
134
private readonly PathFilter path;
136
private readonly byte[] raw;
138
internal Single(PathFilter p)
144
public override bool Include(TreeWalk walker)
146
int cmp = walker.IsPathPrefix(raw, raw.Length);
149
throw StopWalkException.INSTANCE;
154
public override bool ShouldBeRecursive()
156
return path.ShouldBeRecursive();
159
public override TreeFilter Clone()
164
public override string ToString()
166
return "FAST_" + path.ToString();
170
internal class Group : TreeFilter
172
private sealed class _IComparer_153 : IComparer<PathFilter>
174
public _IComparer_153()
178
public int Compare(PathFilter o1, PathFilter o2)
180
return Sharpen.Runtime.CompareOrdinal(o1.pathStr, o2.pathStr);
184
private static readonly IComparer<PathFilter> PATH_SORT = new _IComparer_153();
186
private readonly PathFilter[] paths;
188
internal Group(PathFilter[] p)
191
Arrays.Sort(paths, PATH_SORT);
194
public override bool Include(TreeWalk walker)
196
int n = paths.Length;
199
byte[] r = paths[i].pathRaw;
200
int cmp = walker.IsPathPrefix(r, r.Length);
211
throw StopWalkException.INSTANCE;
217
public override bool ShouldBeRecursive()
219
foreach (PathFilter p in paths)
221
if (p.ShouldBeRecursive())
229
public override TreeFilter Clone()
234
public override string ToString()
236
StringBuilder r = new StringBuilder();
238
for (int i = 0; i < paths.Length; i++)
244
r.Append(paths[i].ToString());