~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Treewalk.Filter/TreeFilter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using NGit.Treewalk;
 
45
using NGit.Treewalk.Filter;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit.Treewalk.Filter
 
49
{
 
50
        /// <summary>Selects interesting tree entries during walking.</summary>
 
51
        /// <remarks>
 
52
        /// Selects interesting tree entries during walking.
 
53
        /// <p>
 
54
        /// This is an abstract interface. Applications may implement a subclass, or use
 
55
        /// one of the predefined implementations already available within this package.
 
56
        /// <p>
 
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>
 
65
        /// may be used to
 
66
        /// construct a thread-safe copy of an existing filter.
 
67
        /// <p>
 
68
        /// <b>Path filters:</b>
 
69
        /// <ul>
 
70
        /// <li>Matching pathname:
 
71
        /// <see cref="PathFilter">PathFilter</see>
 
72
        /// </li>
 
73
        /// </ul>
 
74
        /// <p>
 
75
        /// <b>Difference filters:</b>
 
76
        /// <ul>
 
77
        /// <li>Only select differences:
 
78
        /// <see cref="ANY_DIFF">ANY_DIFF</see>
 
79
        /// .</li>
 
80
        /// </ul>
 
81
        /// <p>
 
82
        /// <b>Boolean modifiers:</b>
 
83
        /// <ul>
 
84
        /// <li>AND:
 
85
        /// <see cref="AndTreeFilter">AndTreeFilter</see>
 
86
        /// </li>
 
87
        /// <li>OR:
 
88
        /// <see cref="OrTreeFilter">OrTreeFilter</see>
 
89
        /// </li>
 
90
        /// <li>NOT:
 
91
        /// <see cref="NotTreeFilter">NotTreeFilter</see>
 
92
        /// </li>
 
93
        /// </ul>
 
94
        /// </remarks>
 
95
        public abstract class TreeFilter
 
96
        {
 
97
                /// <summary>Selects all tree entries.</summary>
 
98
                /// <remarks>Selects all tree entries.</remarks>
 
99
                public static readonly TreeFilter ALL = new TreeFilter.AllFilter();
 
100
 
 
101
                private sealed class AllFilter : TreeFilter
 
102
                {
 
103
                        public override bool Include(TreeWalk walker)
 
104
                        {
 
105
                                return true;
 
106
                        }
 
107
 
 
108
                        public override bool ShouldBeRecursive()
 
109
                        {
 
110
                                return false;
 
111
                        }
 
112
 
 
113
                        public override TreeFilter Clone()
 
114
                        {
 
115
                                return this;
 
116
                        }
 
117
 
 
118
                        public override string ToString()
 
119
                        {
 
120
                                return "ALL";
 
121
                        }
 
122
                }
 
123
 
 
124
                /// <summary>Selects only tree entries which differ between at least 2 trees.</summary>
 
125
                /// <remarks>
 
126
                /// Selects only tree entries which differ between at least 2 trees.
 
127
                /// <p>
 
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
 
131
                /// entered into.
 
132
                /// <p>
 
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".
 
138
                /// <p>
 
139
                /// When comparing
 
140
                /// <see cref="NGit.Treewalk.WorkingTreeIterator">NGit.Treewalk.WorkingTreeIterator</see>
 
141
                /// and
 
142
                /// <see cref="NGit.Dircache.DirCacheIterator">NGit.Dircache.DirCacheIterator</see>
 
143
                /// applications should use
 
144
                /// <see cref="IndexDiffFilter">IndexDiffFilter</see>
 
145
                /// .
 
146
                /// </remarks>
 
147
                public static readonly TreeFilter ANY_DIFF = new TreeFilter.AnyDiffFilter();
 
148
 
 
149
                private sealed class AnyDiffFilter : TreeFilter
 
150
                {
 
151
                        public override bool Include(TreeWalk walker)
 
152
                        {
 
153
                                int n = walker.TreeCount;
 
154
                                if (n == 1)
 
155
                                {
 
156
                                        // Assume they meant difference to empty tree.
 
157
                                        return true;
 
158
                                }
 
159
                                int m = walker.GetRawMode(0);
 
160
                                for (int i = 1; i < n; i++)
 
161
                                {
 
162
                                        if (walker.GetRawMode(i) != m || !walker.IdEqual(i, 0))
 
163
                                        {
 
164
                                                return true;
 
165
                                        }
 
166
                                }
 
167
                                return false;
 
168
                        }
 
169
 
 
170
                        public override bool ShouldBeRecursive()
 
171
                        {
 
172
                                return false;
 
173
                        }
 
174
 
 
175
                        public override TreeFilter Clone()
 
176
                        {
 
177
                                return this;
 
178
                        }
 
179
 
 
180
                        public override string ToString()
 
181
                        {
 
182
                                return "ANY_DIFF";
 
183
                        }
 
184
                }
 
185
 
 
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()
 
190
                {
 
191
                        return NotTreeFilter.Create(this);
 
192
                }
 
193
 
 
194
                /// <summary>Determine if the current entry is interesting to report.</summary>
 
195
                /// <remarks>
 
196
                /// Determine if the current entry is interesting to report.
 
197
                /// <p>
 
198
                /// This method is consulted for subtree entries even if
 
199
                /// <see cref="NGit.Treewalk.TreeWalk.Recursive()">NGit.Treewalk.TreeWalk.Recursive()
 
200
                ///     </see>
 
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.
 
204
                /// </remarks>
 
205
                /// <param name="walker">the walker the filter needs to examine.</param>
 
206
                /// <returns>
 
207
                /// true if the current entry should be seen by the application;
 
208
                /// false to hide the entry.
 
209
                /// </returns>
 
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
 
214
                /// the object.
 
215
                /// </exception>
 
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.
 
220
                /// </exception>
 
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.
 
224
                /// </exception>
 
225
                public abstract bool Include(TreeWalk walker);
 
226
 
 
227
                /// <summary>
 
228
                /// Does this tree filter require a recursive walk to match everything?
 
229
                /// <p>
 
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.
 
233
                /// </summary>
 
234
                /// <remarks>
 
235
                /// Does this tree filter require a recursive walk to match everything?
 
236
                /// <p>
 
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.
 
241
                /// </remarks>
 
242
                /// <returns>
 
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.
 
246
                /// </returns>
 
247
                public abstract bool ShouldBeRecursive();
 
248
 
 
249
                /// <summary>Clone this tree filter, including its parameters.</summary>
 
250
                /// <remarks>
 
251
                /// Clone this tree filter, including its parameters.
 
252
                /// <p>
 
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.
 
255
                /// </remarks>
 
256
                /// <returns>another copy of this filter, suitable for another thread.</returns>
 
257
                public abstract TreeFilter Clone();
 
258
 
 
259
                public override string ToString()
 
260
                {
 
261
                        string n = GetType().FullName;
 
262
                        int lastDot = n.LastIndexOf('.');
 
263
                        if (lastDot >= 0)
 
264
                        {
 
265
                                n = Sharpen.Runtime.Substring(n, lastDot + 1);
 
266
                        }
 
267
                        return n.Replace('$', '.');
 
268
                }
 
269
        }
 
270
}