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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Revwalk/PendingGenerator.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;
 
45
using NGit.Errors;
 
46
using NGit.Revwalk;
 
47
using NGit.Revwalk.Filter;
 
48
using Sharpen;
 
49
 
 
50
namespace NGit.Revwalk
 
51
{
 
52
        /// <summary>Default (and first pass) RevCommit Generator implementation for RevWalk.
 
53
        ///     </summary>
 
54
        /// <remarks>
 
55
        /// Default (and first pass) RevCommit Generator implementation for RevWalk.
 
56
        /// <p>
 
57
        /// This generator starts from a set of one or more commits and process them in
 
58
        /// descending (newest to oldest) commit time order. Commits automatically cause
 
59
        /// their parents to be enqueued for further processing, allowing the entire
 
60
        /// commit graph to be walked. A
 
61
        /// <see cref="NGit.Revwalk.Filter.RevFilter">NGit.Revwalk.Filter.RevFilter</see>
 
62
        /// may be used to select a subset
 
63
        /// of the commits and return them to the caller.
 
64
        /// </remarks>
 
65
        internal class PendingGenerator : Generator
 
66
        {
 
67
                private const int PARSED = RevWalk.PARSED;
 
68
 
 
69
                private const int SEEN = RevWalk.SEEN;
 
70
 
 
71
                private const int UNINTERESTING = RevWalk.UNINTERESTING;
 
72
 
 
73
                /// <summary>Number of additional commits to scan after we think we are done.</summary>
 
74
                /// <remarks>
 
75
                /// Number of additional commits to scan after we think we are done.
 
76
                /// <p>
 
77
                /// This small buffer of commits is scanned to ensure we didn't miss anything
 
78
                /// as a result of clock skew when the commits were made. We need to set our
 
79
                /// constant to 1 additional commit due to the use of a pre-increment
 
80
                /// operator when accessing the value.
 
81
                /// </remarks>
 
82
                internal const int OVER_SCAN = 5 + 1;
 
83
 
 
84
                /// <summary>
 
85
                /// A commit near the end of time, to initialize
 
86
                /// <see cref="last">last</see>
 
87
                /// with.
 
88
                /// </summary>
 
89
                private static readonly RevCommit INIT_LAST;
 
90
 
 
91
                static PendingGenerator()
 
92
                {
 
93
                        INIT_LAST = new RevCommit(ObjectId.ZeroId);
 
94
                        INIT_LAST.commitTime = int.MaxValue;
 
95
                }
 
96
 
 
97
                private readonly RevWalk walker;
 
98
 
 
99
                private readonly DateRevQueue pending;
 
100
 
 
101
                private readonly RevFilter filter;
 
102
 
 
103
                private readonly int output;
 
104
 
 
105
                /// <summary>
 
106
                /// Last commit produced to the caller from
 
107
                /// <see cref="Next()">Next()</see>
 
108
                /// .
 
109
                /// </summary>
 
110
                private RevCommit last = INIT_LAST;
 
111
 
 
112
                /// <summary>Number of commits we have remaining in our over-scan allotment.</summary>
 
113
                /// <remarks>
 
114
                /// Number of commits we have remaining in our over-scan allotment.
 
115
                /// <p>
 
116
                /// Only relevant if there are
 
117
                /// <see cref="UNINTERESTING">UNINTERESTING</see>
 
118
                /// commits in the
 
119
                /// <see cref="pending">pending</see>
 
120
                /// queue.
 
121
                /// </remarks>
 
122
                private int overScan = OVER_SCAN;
 
123
 
 
124
                internal bool canDispose;
 
125
 
 
126
                internal PendingGenerator(RevWalk w, DateRevQueue p, RevFilter f, int @out)
 
127
                {
 
128
                        walker = w;
 
129
                        pending = p;
 
130
                        filter = f;
 
131
                        output = @out;
 
132
                        canDispose = true;
 
133
                }
 
134
 
 
135
                internal override int OutputType()
 
136
                {
 
137
                        return output | SORT_COMMIT_TIME_DESC;
 
138
                }
 
139
 
 
140
                /// <exception cref="NGit.Errors.MissingObjectException"></exception>
 
141
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
 
142
                /// <exception cref="System.IO.IOException"></exception>
 
143
                internal override RevCommit Next()
 
144
                {
 
145
                        try
 
146
                        {
 
147
                                for (; ; )
 
148
                                {
 
149
                                        RevCommit c = pending.Next();
 
150
                                        if (c == null)
 
151
                                        {
 
152
                                                walker.reader.WalkAdviceEnd();
 
153
                                                return null;
 
154
                                        }
 
155
                                        bool produce;
 
156
                                        if ((c.flags & UNINTERESTING) != 0)
 
157
                                        {
 
158
                                                produce = false;
 
159
                                        }
 
160
                                        else
 
161
                                        {
 
162
                                                if (filter.RequiresCommitBody())
 
163
                                                {
 
164
                                                        c.ParseBody(walker);
 
165
                                                }
 
166
                                                produce = filter.Include(walker, c);
 
167
                                        }
 
168
                                        foreach (RevCommit p in c.parents)
 
169
                                        {
 
170
                                                if ((p.flags & SEEN) != 0)
 
171
                                                {
 
172
                                                        continue;
 
173
                                                }
 
174
                                                if ((p.flags & PARSED) == 0)
 
175
                                                {
 
176
                                                        p.ParseHeaders(walker);
 
177
                                                }
 
178
                                                p.flags |= SEEN;
 
179
                                                pending.Add(p);
 
180
                                        }
 
181
                                        walker.CarryFlagsImpl(c);
 
182
                                        if ((c.flags & UNINTERESTING) != 0)
 
183
                                        {
 
184
                                                if (pending.EverbodyHasFlag(UNINTERESTING))
 
185
                                                {
 
186
                                                        RevCommit n = pending.Peek();
 
187
                                                        if (n != null && n.commitTime >= last.commitTime)
 
188
                                                        {
 
189
                                                                // This is too close to call. The next commit we
 
190
                                                                // would pop is dated after the last one produced.
 
191
                                                                // We have to keep going to ensure that we carry
 
192
                                                                // flags as much as necessary.
 
193
                                                                //
 
194
                                                                overScan = OVER_SCAN;
 
195
                                                        }
 
196
                                                        else
 
197
                                                        {
 
198
                                                                if (--overScan == 0)
 
199
                                                                {
 
200
                                                                        throw StopWalkException.INSTANCE;
 
201
                                                                }
 
202
                                                        }
 
203
                                                }
 
204
                                                else
 
205
                                                {
 
206
                                                        overScan = OVER_SCAN;
 
207
                                                }
 
208
                                                if (canDispose)
 
209
                                                {
 
210
                                                        c.DisposeBody();
 
211
                                                }
 
212
                                                continue;
 
213
                                        }
 
214
                                        if (produce)
 
215
                                        {
 
216
                                                return last = c;
 
217
                                        }
 
218
                                        else
 
219
                                        {
 
220
                                                if (canDispose)
 
221
                                                {
 
222
                                                        c.DisposeBody();
 
223
                                                }
 
224
                                        }
 
225
                                }
 
226
                        }
 
227
                        catch (StopWalkException)
 
228
                        {
 
229
                                walker.reader.WalkAdviceEnd();
 
230
                                pending.Clear();
 
231
                                return null;
 
232
                        }
 
233
                }
 
234
        }
 
235
}