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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit/CommitBuilder.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 System.Text;
 
47
using NGit;
 
48
using Sharpen;
 
49
 
 
50
namespace NGit
 
51
{
 
52
        /// <summary>Mutable builder to construct a commit recording the state of a project.</summary>
 
53
        /// <remarks>
 
54
        /// Mutable builder to construct a commit recording the state of a project.
 
55
        /// Applications should use this object when they need to manually construct a
 
56
        /// commit and want precise control over its fields. For a higher level interface
 
57
        /// see
 
58
        /// <see cref="NGit.Api.CommitCommand">NGit.Api.CommitCommand</see>
 
59
        /// .
 
60
        /// To read a commit object, construct a
 
61
        /// <see cref="NGit.Revwalk.RevWalk">NGit.Revwalk.RevWalk</see>
 
62
        /// and obtain a
 
63
        /// <see cref="NGit.Revwalk.RevCommit">NGit.Revwalk.RevCommit</see>
 
64
        /// instance by calling
 
65
        /// <see cref="NGit.Revwalk.RevWalk.ParseCommit(AnyObjectId)">NGit.Revwalk.RevWalk.ParseCommit(AnyObjectId)
 
66
        ///     </see>
 
67
        /// .
 
68
        /// </remarks>
 
69
        public class CommitBuilder
 
70
        {
 
71
                private static readonly ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
 
72
 
 
73
                private static readonly byte[] htree = Constants.EncodeASCII("tree");
 
74
 
 
75
                private static readonly byte[] hparent = Constants.EncodeASCII("parent");
 
76
 
 
77
                private static readonly byte[] hauthor = Constants.EncodeASCII("author");
 
78
 
 
79
                private static readonly byte[] hcommitter = Constants.EncodeASCII("committer");
 
80
 
 
81
                private static readonly byte[] hencoding = Constants.EncodeASCII("encoding");
 
82
 
 
83
                private ObjectId treeId;
 
84
 
 
85
                private ObjectId[] parentIds;
 
86
 
 
87
                private PersonIdent author;
 
88
 
 
89
                private PersonIdent committer;
 
90
 
 
91
                private string message;
 
92
 
 
93
                private System.Text.Encoding encoding;
 
94
 
 
95
                /// <summary>Initialize an empty commit.</summary>
 
96
                /// <remarks>Initialize an empty commit.</remarks>
 
97
                public CommitBuilder()
 
98
                {
 
99
                        parentIds = EMPTY_OBJECTID_LIST;
 
100
                        encoding = Constants.CHARSET;
 
101
                }
 
102
 
 
103
                /// <returns>id of the root tree listing this commit's snapshot.</returns>
 
104
                /// <summary>Set the tree id for this commit object</summary>
 
105
                /// <value>the tree identity.</value>
 
106
                public virtual ObjectId TreeId
 
107
                {
 
108
                        get
 
109
                        {
 
110
                                return treeId;
 
111
                        }
 
112
                        set
 
113
                        {
 
114
                                ObjectId id = value;
 
115
                                treeId = id.Copy();
 
116
                        }
 
117
                }
 
118
 
 
119
                /// <returns>the author of this commit (who wrote it).</returns>
 
120
                /// <summary>Set the author (name, email address, and date) of who wrote the commit.</summary>
 
121
                /// <remarks>Set the author (name, email address, and date) of who wrote the commit.</remarks>
 
122
                /// <value>the new author. Should not be null.</value>
 
123
                public virtual PersonIdent Author
 
124
                {
 
125
                        get
 
126
                        {
 
127
                                return author;
 
128
                        }
 
129
                        set
 
130
                        {
 
131
                                PersonIdent newAuthor = value;
 
132
                                author = newAuthor;
 
133
                        }
 
134
                }
 
135
 
 
136
                /// <returns>the committer and commit time for this object.</returns>
 
137
                /// <summary>Set the committer and commit time for this object</summary>
 
138
                /// <value>the committer information. Should not be null.</value>
 
139
                public virtual PersonIdent Committer
 
140
                {
 
141
                        get
 
142
                        {
 
143
                                return committer;
 
144
                        }
 
145
                        set
 
146
                        {
 
147
                                PersonIdent newCommitter = value;
 
148
                                committer = newCommitter;
 
149
                        }
 
150
                }
 
151
 
 
152
                /// <returns>the ancestors of this commit. Never null.</returns>
 
153
                public virtual ObjectId[] ParentIds
 
154
                {
 
155
                        get
 
156
                        {
 
157
                                return parentIds;
 
158
                        }
 
159
                }
 
160
 
 
161
                /// <summary>Set the parent of this commit.</summary>
 
162
                /// <remarks>Set the parent of this commit.</remarks>
 
163
                /// <param name="newParent">the single parent for the commit.</param>
 
164
                public virtual void SetParentId(AnyObjectId newParent)
 
165
                {
 
166
                        parentIds = new ObjectId[] { newParent.Copy() };
 
167
                }
 
168
 
 
169
                /// <summary>Set the parents of this commit.</summary>
 
170
                /// <remarks>Set the parents of this commit.</remarks>
 
171
                /// <param name="parent1">
 
172
                /// the first parent of this commit. Typically this is the current
 
173
                /// value of the
 
174
                /// <code>HEAD</code>
 
175
                /// reference and is thus the current
 
176
                /// branch's position in history.
 
177
                /// </param>
 
178
                /// <param name="parent2">
 
179
                /// the second parent of this merge commit. Usually this is the
 
180
                /// branch being merged into the current branch.
 
181
                /// </param>
 
182
                public virtual void SetParentIds(AnyObjectId parent1, AnyObjectId parent2)
 
183
                {
 
184
                        parentIds = new ObjectId[] { parent1.Copy(), parent2.Copy() };
 
185
                }
 
186
 
 
187
                /// <summary>Set the parents of this commit.</summary>
 
188
                /// <remarks>Set the parents of this commit.</remarks>
 
189
                /// <param name="newParents">the entire list of parents for this commit.</param>
 
190
                public virtual void SetParentIds(params ObjectId[] newParents)
 
191
                {
 
192
                        parentIds = new ObjectId[newParents.Length];
 
193
                        for (int i = 0; i < newParents.Length; i++)
 
194
                        {
 
195
                                parentIds[i] = newParents[i].Copy();
 
196
                        }
 
197
                }
 
198
 
 
199
                /// <summary>Set the parents of this commit.</summary>
 
200
                /// <remarks>Set the parents of this commit.</remarks>
 
201
                /// <param name="newParents">the entire list of parents for this commit.</param>
 
202
                public virtual void SetParentIds<_T0>(IList<_T0> newParents) where _T0:AnyObjectId
 
203
                {
 
204
                        parentIds = new ObjectId[newParents.Count];
 
205
                        for (int i = 0; i < newParents.Count; i++)
 
206
                        {
 
207
                                parentIds[i] = newParents[i].Copy();
 
208
                        }
 
209
                }
 
210
 
 
211
                /// <summary>Add a parent onto the end of the parent list.</summary>
 
212
                /// <remarks>Add a parent onto the end of the parent list.</remarks>
 
213
                /// <param name="additionalParent">new parent to add onto the end of the current parent list.
 
214
                ///     </param>
 
215
                public virtual void AddParentId(AnyObjectId additionalParent)
 
216
                {
 
217
                        if (parentIds.Length == 0)
 
218
                        {
 
219
                                SetParentId(additionalParent);
 
220
                        }
 
221
                        else
 
222
                        {
 
223
                                ObjectId[] newParents = new ObjectId[parentIds.Length + 1];
 
224
                                for (int i = 0; i < parentIds.Length; i++)
 
225
                                {
 
226
                                        newParents[i] = parentIds[i];
 
227
                                }
 
228
                                newParents[parentIds.Length] = additionalParent.Copy();
 
229
                                parentIds = newParents;
 
230
                        }
 
231
                }
 
232
 
 
233
                /// <returns>the complete commit message.</returns>
 
234
                /// <summary>Set the commit message.</summary>
 
235
                /// <remarks>Set the commit message.</remarks>
 
236
                /// <value>the commit message. Should not be null.</value>
 
237
                public virtual string Message
 
238
                {
 
239
                        get
 
240
                        {
 
241
                                return message;
 
242
                        }
 
243
                        set
 
244
                        {
 
245
                                string newMessage = value;
 
246
                                message = newMessage;
 
247
                        }
 
248
                }
 
249
 
 
250
                /// <summary>Set the encoding for the commit information</summary>
 
251
                /// <param name="encodingName">
 
252
                /// the encoding name. See
 
253
                /// <see cref="Sharpen.Extensions.GetEncoding(string)">Sharpen.Extensions.GetEncoding(string)
 
254
                ///     </see>
 
255
                /// .
 
256
                /// </param>
 
257
                public virtual void SetEncoding(string encodingName)
 
258
                {
 
259
                        encoding = Sharpen.Extensions.GetEncoding(encodingName);
 
260
                }
 
261
 
 
262
                /// <summary>Set the encoding for the commit information</summary>
 
263
                /// <param name="enc">the encoding to use.</param>
 
264
                public virtual void SetEncoding(System.Text.Encoding enc)
 
265
                {
 
266
                        encoding = enc;
 
267
                }
 
268
 
 
269
                /// <returns>the encoding that should be used for the commit message text.</returns>
 
270
                public virtual System.Text.Encoding Encoding
 
271
                {
 
272
                        get
 
273
                        {
 
274
                                return encoding;
 
275
                        }
 
276
                }
 
277
 
 
278
                /// <summary>Format this builder's state as a commit object.</summary>
 
279
                /// <remarks>Format this builder's state as a commit object.</remarks>
 
280
                /// <returns>
 
281
                /// this object in the canonical commit format, suitable for storage
 
282
                /// in a repository.
 
283
                /// </returns>
 
284
                /// <exception cref="Sharpen.UnsupportedEncodingException">
 
285
                /// the encoding specified by
 
286
                /// <see cref="Encoding()">Encoding()</see>
 
287
                /// is not
 
288
                /// supported by this Java runtime.
 
289
                /// </exception>
 
290
                public virtual byte[] Build()
 
291
                {
 
292
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
 
293
                        OutputStreamWriter w = new OutputStreamWriter(os, Encoding);
 
294
                        try
 
295
                        {
 
296
                                os.Write(htree);
 
297
                                os.Write(' ');
 
298
                                TreeId.CopyTo(os);
 
299
                                os.Write('\n');
 
300
                                foreach (ObjectId p in ParentIds)
 
301
                                {
 
302
                                        os.Write(hparent);
 
303
                                        os.Write(' ');
 
304
                                        p.CopyTo(os);
 
305
                                        os.Write('\n');
 
306
                                }
 
307
                                os.Write(hauthor);
 
308
                                os.Write(' ');
 
309
                                w.Write(Author.ToExternalString());
 
310
                                w.Flush();
 
311
                                os.Write('\n');
 
312
                                os.Write(hcommitter);
 
313
                                os.Write(' ');
 
314
                                w.Write(Committer.ToExternalString());
 
315
                                w.Flush();
 
316
                                os.Write('\n');
 
317
                                if (Encoding != Constants.CHARSET)
 
318
                                {
 
319
                                        os.Write(hencoding);
 
320
                                        os.Write(' ');
 
321
                                        os.Write(Constants.EncodeASCII(Encoding.Name()));
 
322
                                        os.Write('\n');
 
323
                                }
 
324
                                os.Write('\n');
 
325
                                if (Message != null)
 
326
                                {
 
327
                                        w.Write(Message);
 
328
                                        w.Flush();
 
329
                                }
 
330
                        }
 
331
                        catch (IOException err)
 
332
                        {
 
333
                                // This should never occur, the only way to get it above is
 
334
                                // for the ByteArrayOutputStream to throw, but it doesn't.
 
335
                                //
 
336
                                throw new RuntimeException(err);
 
337
                        }
 
338
                        return os.ToByteArray();
 
339
                }
 
340
 
 
341
                /// <summary>Format this builder's state as a commit object.</summary>
 
342
                /// <remarks>Format this builder's state as a commit object.</remarks>
 
343
                /// <returns>
 
344
                /// this object in the canonical commit format, suitable for storage
 
345
                /// in a repository.
 
346
                /// </returns>
 
347
                /// <exception cref="Sharpen.UnsupportedEncodingException">
 
348
                /// the encoding specified by
 
349
                /// <see cref="Encoding()">Encoding()</see>
 
350
                /// is not
 
351
                /// supported by this Java runtime.
 
352
                /// </exception>
 
353
                public virtual byte[] ToByteArray()
 
354
                {
 
355
                        return Build();
 
356
                }
 
357
 
 
358
                public override string ToString()
 
359
                {
 
360
                        StringBuilder r = new StringBuilder();
 
361
                        r.Append("Commit");
 
362
                        r.Append("={\n");
 
363
                        r.Append("tree ");
 
364
                        r.Append(treeId != null ? treeId.Name : "NOT_SET");
 
365
                        r.Append("\n");
 
366
                        foreach (ObjectId p in parentIds)
 
367
                        {
 
368
                                r.Append("parent ");
 
369
                                r.Append(p.Name);
 
370
                                r.Append("\n");
 
371
                        }
 
372
                        r.Append("author ");
 
373
                        r.Append(author != null ? author.ToString() : "NOT_SET");
 
374
                        r.Append("\n");
 
375
                        r.Append("committer ");
 
376
                        r.Append(committer != null ? committer.ToString() : "NOT_SET");
 
377
                        r.Append("\n");
 
378
                        if (encoding != null && encoding != Constants.CHARSET)
 
379
                        {
 
380
                                r.Append("encoding ");
 
381
                                r.Append(encoding.Name());
 
382
                                r.Append("\n");
 
383
                        }
 
384
                        r.Append("\n");
 
385
                        r.Append(message != null ? message : string.Empty);
 
386
                        r.Append("}");
 
387
                        return r.ToString();
 
388
                }
 
389
        }
 
390
}