2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
44
using System.Collections.Generic;
52
/// <summary>Mutable builder to construct a commit recording the state of a project.</summary>
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
58
/// <see cref="NGit.Api.CommitCommand">NGit.Api.CommitCommand</see>
60
/// To read a commit object, construct a
61
/// <see cref="NGit.Revwalk.RevWalk">NGit.Revwalk.RevWalk</see>
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)
69
public class CommitBuilder
71
private static readonly ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
73
private static readonly byte[] htree = Constants.EncodeASCII("tree");
75
private static readonly byte[] hparent = Constants.EncodeASCII("parent");
77
private static readonly byte[] hauthor = Constants.EncodeASCII("author");
79
private static readonly byte[] hcommitter = Constants.EncodeASCII("committer");
81
private static readonly byte[] hencoding = Constants.EncodeASCII("encoding");
83
private ObjectId treeId;
85
private ObjectId[] parentIds;
87
private PersonIdent author;
89
private PersonIdent committer;
91
private string message;
93
private System.Text.Encoding encoding;
95
/// <summary>Initialize an empty commit.</summary>
96
/// <remarks>Initialize an empty commit.</remarks>
97
public CommitBuilder()
99
parentIds = EMPTY_OBJECTID_LIST;
100
encoding = Constants.CHARSET;
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
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
131
PersonIdent newAuthor = value;
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
147
PersonIdent newCommitter = value;
148
committer = newCommitter;
152
/// <returns>the ancestors of this commit. Never null.</returns>
153
public virtual ObjectId[] ParentIds
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)
166
parentIds = new ObjectId[] { newParent.Copy() };
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
174
/// <code>HEAD</code>
175
/// reference and is thus the current
176
/// branch's position in history.
178
/// <param name="parent2">
179
/// the second parent of this merge commit. Usually this is the
180
/// branch being merged into the current branch.
182
public virtual void SetParentIds(AnyObjectId parent1, AnyObjectId parent2)
184
parentIds = new ObjectId[] { parent1.Copy(), parent2.Copy() };
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)
192
parentIds = new ObjectId[newParents.Length];
193
for (int i = 0; i < newParents.Length; i++)
195
parentIds[i] = newParents[i].Copy();
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
204
parentIds = new ObjectId[newParents.Count];
205
for (int i = 0; i < newParents.Count; i++)
207
parentIds[i] = newParents[i].Copy();
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.
215
public virtual void AddParentId(AnyObjectId additionalParent)
217
if (parentIds.Length == 0)
219
SetParentId(additionalParent);
223
ObjectId[] newParents = new ObjectId[parentIds.Length + 1];
224
for (int i = 0; i < parentIds.Length; i++)
226
newParents[i] = parentIds[i];
228
newParents[parentIds.Length] = additionalParent.Copy();
229
parentIds = newParents;
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
245
string newMessage = value;
246
message = newMessage;
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)
257
public virtual void SetEncoding(string encodingName)
259
encoding = Sharpen.Extensions.GetEncoding(encodingName);
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)
269
/// <returns>the encoding that should be used for the commit message text.</returns>
270
public virtual System.Text.Encoding Encoding
278
/// <summary>Format this builder's state as a commit object.</summary>
279
/// <remarks>Format this builder's state as a commit object.</remarks>
281
/// this object in the canonical commit format, suitable for storage
284
/// <exception cref="Sharpen.UnsupportedEncodingException">
285
/// the encoding specified by
286
/// <see cref="Encoding()">Encoding()</see>
288
/// supported by this Java runtime.
290
public virtual byte[] Build()
292
ByteArrayOutputStream os = new ByteArrayOutputStream();
293
OutputStreamWriter w = new OutputStreamWriter(os, Encoding);
300
foreach (ObjectId p in ParentIds)
309
w.Write(Author.ToExternalString());
312
os.Write(hcommitter);
314
w.Write(Committer.ToExternalString());
317
if (Encoding != Constants.CHARSET)
321
os.Write(Constants.EncodeASCII(Encoding.Name()));
331
catch (IOException err)
333
// This should never occur, the only way to get it above is
334
// for the ByteArrayOutputStream to throw, but it doesn't.
336
throw new RuntimeException(err);
338
return os.ToByteArray();
341
/// <summary>Format this builder's state as a commit object.</summary>
342
/// <remarks>Format this builder's state as a commit object.</remarks>
344
/// this object in the canonical commit format, suitable for storage
347
/// <exception cref="Sharpen.UnsupportedEncodingException">
348
/// the encoding specified by
349
/// <see cref="Encoding()">Encoding()</see>
351
/// supported by this Java runtime.
353
public virtual byte[] ToByteArray()
358
public override string ToString()
360
StringBuilder r = new StringBuilder();
364
r.Append(treeId != null ? treeId.Name : "NOT_SET");
366
foreach (ObjectId p in parentIds)
373
r.Append(author != null ? author.ToString() : "NOT_SET");
375
r.Append("committer ");
376
r.Append(committer != null ? committer.ToString() : "NOT_SET");
378
if (encoding != null && encoding != Constants.CHARSET)
380
r.Append("encoding ");
381
r.Append(encoding.Name());
385
r.Append(message != null ? message : string.Empty);