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 NGit.Treewalk.Filter;
48
namespace NGit.Treewalk.Filter
50
/// <summary>Selects interesting tree entries during walking.</summary>
52
/// Selects interesting tree entries during walking.
54
/// This is an abstract interface. Applications may implement a subclass, or use
55
/// one of the predefined implementations already available within this package.
57
/// Unless specifically noted otherwise a TreeFilter implementation is not thread
58
/// safe and may not be shared by different TreeWalk instances at the same time.
59
/// This restriction allows TreeFilter implementations to cache state within
60
/// their instances during
61
/// <see cref="Include(NGit.Treewalk.TreeWalk)">Include(NGit.Treewalk.TreeWalk)</see>
62
/// if it is beneficial to
63
/// their implementation. Deep clones created by
64
/// <see cref="Clone()">Clone()</see>
66
/// construct a thread-safe copy of an existing filter.
68
/// <b>Path filters:</b>
70
/// <li>Matching pathname:
71
/// <see cref="PathFilter">PathFilter</see>
75
/// <b>Difference filters:</b>
77
/// <li>Only select differences:
78
/// <see cref="ANY_DIFF">ANY_DIFF</see>
82
/// <b>Boolean modifiers:</b>
85
/// <see cref="AndTreeFilter">AndTreeFilter</see>
88
/// <see cref="OrTreeFilter">OrTreeFilter</see>
91
/// <see cref="NotTreeFilter">NotTreeFilter</see>
95
public abstract class TreeFilter
97
/// <summary>Selects all tree entries.</summary>
98
/// <remarks>Selects all tree entries.</remarks>
99
public static readonly TreeFilter ALL = new TreeFilter.AllFilter();
101
private sealed class AllFilter : TreeFilter
103
public override bool Include(TreeWalk walker)
108
public override bool ShouldBeRecursive()
113
public override TreeFilter Clone()
118
public override string ToString()
124
/// <summary>Selects only tree entries which differ between at least 2 trees.</summary>
126
/// Selects only tree entries which differ between at least 2 trees.
128
/// This filter also prevents a TreeWalk from recursing into a subtree if all
129
/// parent trees have the identical subtree at the same path. This
130
/// dramatically improves walk performance as only the changed subtrees are
133
/// If this filter is applied to a walker with only one tree it behaves like
134
/// <see cref="ALL">ALL</see>
135
/// , or as though the walker was matching a virtual empty tree
136
/// against the single tree it was actually given. Applications may wish to
137
/// treat such a difference as "all names added".
140
/// <see cref="NGit.Treewalk.WorkingTreeIterator">NGit.Treewalk.WorkingTreeIterator</see>
142
/// <see cref="NGit.Dircache.DirCacheIterator">NGit.Dircache.DirCacheIterator</see>
143
/// applications should use
144
/// <see cref="IndexDiffFilter">IndexDiffFilter</see>
147
public static readonly TreeFilter ANY_DIFF = new TreeFilter.AnyDiffFilter();
149
private sealed class AnyDiffFilter : TreeFilter
151
public override bool Include(TreeWalk walker)
153
int n = walker.TreeCount;
156
// Assume they meant difference to empty tree.
159
int m = walker.GetRawMode(0);
160
for (int i = 1; i < n; i++)
162
if (walker.GetRawMode(i) != m || !walker.IdEqual(i, 0))
170
public override bool ShouldBeRecursive()
175
public override TreeFilter Clone()
180
public override string ToString()
186
/// <summary>Create a new filter that does the opposite of this filter.</summary>
187
/// <remarks>Create a new filter that does the opposite of this filter.</remarks>
188
/// <returns>a new filter that includes tree entries this filter rejects.</returns>
189
public virtual TreeFilter Negate()
191
return NotTreeFilter.Create(this);
194
/// <summary>Determine if the current entry is interesting to report.</summary>
196
/// Determine if the current entry is interesting to report.
198
/// This method is consulted for subtree entries even if
199
/// <see cref="NGit.Treewalk.TreeWalk.Recursive()">NGit.Treewalk.TreeWalk.Recursive()
201
/// is enabled. The consultation allows the
202
/// filter to bypass subtree recursion on a case-by-case basis, even when
203
/// recursion is enabled at the application level.
205
/// <param name="walker">the walker the filter needs to examine.</param>
207
/// true if the current entry should be seen by the application;
208
/// false to hide the entry.
210
/// <exception cref="NGit.Errors.MissingObjectException">
211
/// an object the filter needs to consult to determine its answer
212
/// does not exist in the Git repository the walker is operating
213
/// on. Filtering this current walker entry is impossible without
216
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">
217
/// an object the filter needed to consult was not of the
218
/// expected object type. This usually indicates a corrupt
219
/// repository, as an object link is referencing the wrong type.
221
/// <exception cref="System.IO.IOException">
222
/// a loose object or pack file could not be read to obtain data
223
/// necessary for the filter to make its decision.
225
public abstract bool Include(TreeWalk walker);
228
/// Does this tree filter require a recursive walk to match everything?
230
/// If this tree filter is matching on full entry path names and its pattern
231
/// is looking for a '/' then the filter would require a recursive TreeWalk
232
/// to accurately make its decisions.
235
/// Does this tree filter require a recursive walk to match everything?
237
/// If this tree filter is matching on full entry path names and its pattern
238
/// is looking for a '/' then the filter would require a recursive TreeWalk
239
/// to accurately make its decisions. The walker is not required to enable
240
/// recursive behavior for any particular filter, this is only a hint.
243
/// true if the filter would like to have the walker recurse into
244
/// subtrees to make sure it matches everything correctly; false if
245
/// the filter does not require entering subtrees.
247
public abstract bool ShouldBeRecursive();
249
/// <summary>Clone this tree filter, including its parameters.</summary>
251
/// Clone this tree filter, including its parameters.
253
/// This is a deep clone. If this filter embeds objects or other filters it
254
/// must also clone those, to ensure the instances do not share mutable data.
256
/// <returns>another copy of this filter, suitable for another thread.</returns>
257
public abstract TreeFilter Clone();
259
public override string ToString()
261
string n = GetType().FullName;
262
int lastDot = n.LastIndexOf('.');
265
n = Sharpen.Runtime.Substring(n, lastDot + 1);
267
return n.Replace('$', '.');