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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Api/LogCommand.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.Collections.Generic;
 
45
using System.IO;
 
46
using NGit;
 
47
using NGit.Api;
 
48
using NGit.Api.Errors;
 
49
using NGit.Errors;
 
50
using NGit.Revwalk;
 
51
using NGit.Treewalk.Filter;
 
52
using Sharpen;
 
53
 
 
54
namespace NGit.Api
 
55
{
 
56
        /// <summary>
 
57
        /// A class used to execute a
 
58
        /// <code>Log</code>
 
59
        /// command. It has setters for all
 
60
        /// supported options and arguments of this command and a
 
61
        /// <see cref="Call()">Call()</see>
 
62
        /// method
 
63
        /// to finally execute the command. Each instance of this class should only be
 
64
        /// used for one invocation of the command (means: one call to
 
65
        /// <see cref="Call()">Call()</see>
 
66
        /// )
 
67
        /// <p>
 
68
        /// This is currently a very basic implementation which takes only one starting
 
69
        /// revision as option.
 
70
        /// TODO: add more options (revision ranges, sorting, ...)
 
71
        /// </summary>
 
72
        /// <seealso><a href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
 
73
        /// *      >Git documentation about Log</a></seealso>
 
74
        public class LogCommand : GitCommand<Iterable<RevCommit>>
 
75
        {
 
76
                private RevWalk walk;
 
77
 
 
78
                private bool startSpecified = false;
 
79
 
 
80
                private readonly IList<PathFilter> pathFilters = new AList<PathFilter>();
 
81
 
 
82
                /// <param name="repo"></param>
 
83
                protected internal LogCommand(Repository repo) : base(repo)
 
84
                {
 
85
                        walk = new RevWalk(repo);
 
86
                }
 
87
 
 
88
                /// <summary>
 
89
                /// Executes the
 
90
                /// <code>Log</code>
 
91
                /// command with all the options and parameters
 
92
                /// collected by the setter methods (e.g.
 
93
                /// <see cref="Add(NGit.AnyObjectId)">Add(NGit.AnyObjectId)</see>
 
94
                /// ,
 
95
                /// <see cref="Not(NGit.AnyObjectId)">Not(NGit.AnyObjectId)</see>
 
96
                /// , ..) of this class. Each instance of this class
 
97
                /// should only be used for one invocation of the command. Don't call this
 
98
                /// method twice on an instance.
 
99
                /// </summary>
 
100
                /// <returns>an iteration over RevCommits</returns>
 
101
                /// <exception cref="NGit.Api.Errors.NoHeadException"></exception>
 
102
                /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
 
103
                public override Iterable<RevCommit> Call()
 
104
                {
 
105
                        CheckCallable();
 
106
                        if (pathFilters.Count > 0)
 
107
                        {
 
108
                                walk.SetTreeFilter(PathFilterGroup.Create(pathFilters));
 
109
                        }
 
110
                        if (!startSpecified)
 
111
                        {
 
112
                                try
 
113
                                {
 
114
                                        ObjectId headId = repo.Resolve(Constants.HEAD);
 
115
                                        if (headId == null)
 
116
                                        {
 
117
                                                throw new NoHeadException(JGitText.Get().noHEADExistsAndNoExplicitStartingRevisionWasSpecified
 
118
                                                        );
 
119
                                        }
 
120
                                        Add(headId);
 
121
                                }
 
122
                                catch (IOException e)
 
123
                                {
 
124
                                        // all exceptions thrown by add() shouldn't occur and represent
 
125
                                        // severe low-level exception which are therefore wrapped
 
126
                                        throw new JGitInternalException(JGitText.Get().anExceptionOccurredWhileTryingToAddTheIdOfHEAD
 
127
                                                , e);
 
128
                                }
 
129
                        }
 
130
                        SetCallable(false);
 
131
                        return walk;
 
132
                }
 
133
 
 
134
                /// <summary>Mark a commit to start graph traversal from.</summary>
 
135
                /// <remarks>Mark a commit to start graph traversal from.</remarks>
 
136
                /// <seealso cref="NGit.Revwalk.RevWalk.MarkStart(NGit.Revwalk.RevCommit)">NGit.Revwalk.RevWalk.MarkStart(NGit.Revwalk.RevCommit)
 
137
                ///     </seealso>
 
138
                /// <param name="start"></param>
 
139
                /// <returns>
 
140
                /// 
 
141
                /// <code>this</code>
 
142
                /// </returns>
 
143
                /// <exception cref="NGit.Errors.MissingObjectException">
 
144
                /// the commit supplied is not available from the object
 
145
                /// database. This usually indicates the supplied commit is
 
146
                /// invalid, but the reference was constructed during an earlier
 
147
                /// invocation to
 
148
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
149
                ///     </see>
 
150
                /// .
 
151
                /// </exception>
 
152
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException">
 
153
                /// the object was not parsed yet and it was discovered during
 
154
                /// parsing that it is not actually a commit. This usually
 
155
                /// indicates the caller supplied a non-commit SHA-1 to
 
156
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
157
                ///     </see>
 
158
                /// .
 
159
                /// </exception>
 
160
                /// <exception cref="NGit.Api.Errors.JGitInternalException">
 
161
                /// a low-level exception of JGit has occurred. The original
 
162
                /// exception can be retrieved by calling
 
163
                /// <see cref="System.Exception.InnerException()">System.Exception.InnerException()</see>
 
164
                /// . Expect only
 
165
                /// <code>IOException's</code>
 
166
                /// to be wrapped. Subclasses of
 
167
                /// <see cref="System.IO.IOException">System.IO.IOException</see>
 
168
                /// (e.g.
 
169
                /// <see cref="NGit.Errors.MissingObjectException">NGit.Errors.MissingObjectException
 
170
                ///     </see>
 
171
                /// ) are
 
172
                /// typically not wrapped here but thrown as original exception
 
173
                /// </exception>
 
174
                public virtual NGit.Api.LogCommand Add(AnyObjectId start)
 
175
                {
 
176
                        return Add(true, start);
 
177
                }
 
178
 
 
179
                /// <summary>
 
180
                /// Same as
 
181
                /// <code>--not start</code>
 
182
                /// , or
 
183
                /// <code>^start</code>
 
184
                /// </summary>
 
185
                /// <param name="start"></param>
 
186
                /// <returns>
 
187
                /// 
 
188
                /// <code>this</code>
 
189
                /// </returns>
 
190
                /// <exception cref="NGit.Errors.MissingObjectException">
 
191
                /// the commit supplied is not available from the object
 
192
                /// database. This usually indicates the supplied commit is
 
193
                /// invalid, but the reference was constructed during an earlier
 
194
                /// invocation to
 
195
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
196
                ///     </see>
 
197
                /// .
 
198
                /// </exception>
 
199
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException">
 
200
                /// the object was not parsed yet and it was discovered during
 
201
                /// parsing that it is not actually a commit. This usually
 
202
                /// indicates the caller supplied a non-commit SHA-1 to
 
203
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
204
                ///     </see>
 
205
                /// .
 
206
                /// </exception>
 
207
                /// <exception cref="NGit.Api.Errors.JGitInternalException">
 
208
                /// a low-level exception of JGit has occurred. The original
 
209
                /// exception can be retrieved by calling
 
210
                /// <see cref="System.Exception.InnerException()">System.Exception.InnerException()</see>
 
211
                /// . Expect only
 
212
                /// <code>IOException's</code>
 
213
                /// to be wrapped. Subclasses of
 
214
                /// <see cref="System.IO.IOException">System.IO.IOException</see>
 
215
                /// (e.g.
 
216
                /// <see cref="NGit.Errors.MissingObjectException">NGit.Errors.MissingObjectException
 
217
                ///     </see>
 
218
                /// ) are
 
219
                /// typically not wrapped here but thrown as original exception
 
220
                /// </exception>
 
221
                public virtual NGit.Api.LogCommand Not(AnyObjectId start)
 
222
                {
 
223
                        return Add(false, start);
 
224
                }
 
225
 
 
226
                /// <summary>
 
227
                /// Adds the range
 
228
                /// <code>since..until</code>
 
229
                /// </summary>
 
230
                /// <param name="since"></param>
 
231
                /// <param name="until"></param>
 
232
                /// <returns>
 
233
                /// 
 
234
                /// <code>this</code>
 
235
                /// </returns>
 
236
                /// <exception cref="NGit.Errors.MissingObjectException">
 
237
                /// the commit supplied is not available from the object
 
238
                /// database. This usually indicates the supplied commit is
 
239
                /// invalid, but the reference was constructed during an earlier
 
240
                /// invocation to
 
241
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
242
                ///     </see>
 
243
                /// .
 
244
                /// </exception>
 
245
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException">
 
246
                /// the object was not parsed yet and it was discovered during
 
247
                /// parsing that it is not actually a commit. This usually
 
248
                /// indicates the caller supplied a non-commit SHA-1 to
 
249
                /// <see cref="NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)">NGit.Revwalk.RevWalk.LookupCommit(NGit.AnyObjectId)
 
250
                ///     </see>
 
251
                /// .
 
252
                /// </exception>
 
253
                /// <exception cref="NGit.Api.Errors.JGitInternalException">
 
254
                /// a low-level exception of JGit has occurred. The original
 
255
                /// exception can be retrieved by calling
 
256
                /// <see cref="System.Exception.InnerException()">System.Exception.InnerException()</see>
 
257
                /// . Expect only
 
258
                /// <code>IOException's</code>
 
259
                /// to be wrapped. Subclasses of
 
260
                /// <see cref="System.IO.IOException">System.IO.IOException</see>
 
261
                /// (e.g.
 
262
                /// <see cref="NGit.Errors.MissingObjectException">NGit.Errors.MissingObjectException
 
263
                ///     </see>
 
264
                /// ) are
 
265
                /// typically not wrapped here but thrown as original exception
 
266
                /// </exception>
 
267
                public virtual NGit.Api.LogCommand AddRange(AnyObjectId since, AnyObjectId until)
 
268
                {
 
269
                        return Not(since).Add(until);
 
270
                }
 
271
 
 
272
                /// <summary>Show only commits that affect any of the specified paths.</summary>
 
273
                /// <remarks>
 
274
                /// Show only commits that affect any of the specified paths. The path must
 
275
                /// either name a file or a directory exactly. Note that regex expressions or
 
276
                /// wildcards are not supported.
 
277
                /// </remarks>
 
278
                /// <param name="path">a path is relative to the top level of the repository</param>
 
279
                /// <returns>
 
280
                /// 
 
281
                /// <code>this</code>
 
282
                /// </returns>
 
283
                public virtual NGit.Api.LogCommand AddPath(string path)
 
284
                {
 
285
                        CheckCallable();
 
286
                        pathFilters.AddItem(PathFilter.Create(path));
 
287
                        return this;
 
288
                }
 
289
 
 
290
                /// <exception cref="NGit.Errors.MissingObjectException"></exception>
 
291
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
 
292
                /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
 
293
                private NGit.Api.LogCommand Add(bool include, AnyObjectId start)
 
294
                {
 
295
                        CheckCallable();
 
296
                        try
 
297
                        {
 
298
                                if (include)
 
299
                                {
 
300
                                        walk.MarkStart(walk.LookupCommit(start));
 
301
                                        startSpecified = true;
 
302
                                }
 
303
                                else
 
304
                                {
 
305
                                        walk.MarkUninteresting(walk.LookupCommit(start));
 
306
                                }
 
307
                                return this;
 
308
                        }
 
309
                        catch (MissingObjectException e)
 
310
                        {
 
311
                                throw;
 
312
                        }
 
313
                        catch (IncorrectObjectTypeException e)
 
314
                        {
 
315
                                throw;
 
316
                        }
 
317
                        catch (IOException e)
 
318
                        {
 
319
                                throw new JGitInternalException(MessageFormat.Format(JGitText.Get().exceptionOccurredDuringAddingOfOptionToALogCommand
 
320
                                        , start), e);
 
321
                        }
 
322
                }
 
323
        }
 
324
}