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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Treewalk.Filter/PathFilterGroup.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 System;
 
45
using System.Collections.Generic;
 
46
using System.Text;
 
47
using NGit;
 
48
using NGit.Errors;
 
49
using NGit.Treewalk;
 
50
using NGit.Treewalk.Filter;
 
51
using Sharpen;
 
52
 
 
53
namespace NGit.Treewalk.Filter
 
54
{
 
55
        /// <summary>Includes tree entries only if they match one or more configured paths.</summary>
 
56
        /// <remarks>
 
57
        /// Includes tree entries only if they match one or more configured paths.
 
58
        /// <p>
 
59
        /// Operates like
 
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.
 
65
        /// <p>
 
66
        /// This filter is effectively an OR group around paths, with the early abort
 
67
        /// feature described above.
 
68
        /// </remarks>
 
69
        public class PathFilterGroup
 
70
        {
 
71
                /// <summary>Create a collection of path filters from Java strings.</summary>
 
72
                /// <remarks>
 
73
                /// Create a collection of path filters from Java strings.
 
74
                /// <p>
 
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.
 
78
                /// <p>
 
79
                /// Path strings use '/' to delimit directories on all platforms.
 
80
                /// <p>
 
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.
 
84
                /// </remarks>
 
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)
 
88
                {
 
89
                        if (paths.IsEmpty())
 
90
                        {
 
91
                                throw new ArgumentException(JGitText.Get().atLeastOnePathIsRequired);
 
92
                        }
 
93
                        PathFilter[] p = new PathFilter[paths.Count];
 
94
                        int i = 0;
 
95
                        foreach (string s in paths)
 
96
                        {
 
97
                                p[i++] = PathFilter.Create(s);
 
98
                        }
 
99
                        return Create(p);
 
100
                }
 
101
 
 
102
                /// <summary>Create a collection of path filters.</summary>
 
103
                /// <remarks>
 
104
                /// Create a collection of path filters.
 
105
                /// <p>
 
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.
 
109
                /// </remarks>
 
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)
 
113
                {
 
114
                        if (paths.IsEmpty())
 
115
                        {
 
116
                                throw new ArgumentException(JGitText.Get().atLeastOnePathIsRequired);
 
117
                        }
 
118
                        PathFilter[] p = new PathFilter[paths.Count];
 
119
                        Sharpen.Collections.ToArray(paths, p);
 
120
                        return Create(p);
 
121
                }
 
122
 
 
123
                private static TreeFilter Create(PathFilter[] p)
 
124
                {
 
125
                        if (p.Length == 1)
 
126
                        {
 
127
                                return new PathFilterGroup.Single(p[0]);
 
128
                        }
 
129
                        return new PathFilterGroup.Group(p);
 
130
                }
 
131
 
 
132
                internal class Single : TreeFilter
 
133
                {
 
134
                        private readonly PathFilter path;
 
135
 
 
136
                        private readonly byte[] raw;
 
137
 
 
138
                        internal Single(PathFilter p)
 
139
                        {
 
140
                                path = p;
 
141
                                raw = path.pathRaw;
 
142
                        }
 
143
 
 
144
                        public override bool Include(TreeWalk walker)
 
145
                        {
 
146
                                int cmp = walker.IsPathPrefix(raw, raw.Length);
 
147
                                if (cmp > 0)
 
148
                                {
 
149
                                        throw StopWalkException.INSTANCE;
 
150
                                }
 
151
                                return cmp == 0;
 
152
                        }
 
153
 
 
154
                        public override bool ShouldBeRecursive()
 
155
                        {
 
156
                                return path.ShouldBeRecursive();
 
157
                        }
 
158
 
 
159
                        public override TreeFilter Clone()
 
160
                        {
 
161
                                return this;
 
162
                        }
 
163
 
 
164
                        public override string ToString()
 
165
                        {
 
166
                                return "FAST_" + path.ToString();
 
167
                        }
 
168
                }
 
169
 
 
170
                internal class Group : TreeFilter
 
171
                {
 
172
                        private sealed class _IComparer_153 : IComparer<PathFilter>
 
173
                        {
 
174
                                public _IComparer_153()
 
175
                                {
 
176
                                }
 
177
 
 
178
                                public int Compare(PathFilter o1, PathFilter o2)
 
179
                                {
 
180
                                        return Sharpen.Runtime.CompareOrdinal(o1.pathStr, o2.pathStr);
 
181
                                }
 
182
                        }
 
183
 
 
184
                        private static readonly IComparer<PathFilter> PATH_SORT = new _IComparer_153();
 
185
 
 
186
                        private readonly PathFilter[] paths;
 
187
 
 
188
                        internal Group(PathFilter[] p)
 
189
                        {
 
190
                                paths = p;
 
191
                                Arrays.Sort(paths, PATH_SORT);
 
192
                        }
 
193
 
 
194
                        public override bool Include(TreeWalk walker)
 
195
                        {
 
196
                                int n = paths.Length;
 
197
                                for (int i = 0; ; )
 
198
                                {
 
199
                                        byte[] r = paths[i].pathRaw;
 
200
                                        int cmp = walker.IsPathPrefix(r, r.Length);
 
201
                                        if (cmp == 0)
 
202
                                        {
 
203
                                                return true;
 
204
                                        }
 
205
                                        if (++i < n)
 
206
                                        {
 
207
                                                continue;
 
208
                                        }
 
209
                                        if (cmp > 0)
 
210
                                        {
 
211
                                                throw StopWalkException.INSTANCE;
 
212
                                        }
 
213
                                        return false;
 
214
                                }
 
215
                        }
 
216
 
 
217
                        public override bool ShouldBeRecursive()
 
218
                        {
 
219
                                foreach (PathFilter p in paths)
 
220
                                {
 
221
                                        if (p.ShouldBeRecursive())
 
222
                                        {
 
223
                                                return true;
 
224
                                        }
 
225
                                }
 
226
                                return false;
 
227
                        }
 
228
 
 
229
                        public override TreeFilter Clone()
 
230
                        {
 
231
                                return this;
 
232
                        }
 
233
 
 
234
                        public override string ToString()
 
235
                        {
 
236
                                StringBuilder r = new StringBuilder();
 
237
                                r.Append("FAST(");
 
238
                                for (int i = 0; i < paths.Length; i++)
 
239
                                {
 
240
                                        if (i > 0)
 
241
                                        {
 
242
                                                r.Append(" OR ");
 
243
                                        }
 
244
                                        r.Append(paths[i].ToString());
 
245
                                }
 
246
                                r.Append(")");
 
247
                                return r.ToString();
 
248
                        }
 
249
                }
 
250
        }
 
251
}