1 /*
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2006-2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org>
5 * and other copyright owners as documented in the project's IP log.
6 *
7 * This program and the accompanying materials are made available
8 * under the terms of the Eclipse Distribution License v1.0 which
9 * accompanies this distribution, is reproduced below, and is
10 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * - Neither the name of the Eclipse Foundation, Inc. nor the
27 * names of its contributors may be used to endorse or promote
28 * products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46 package org.eclipse.jgit.lib;
47
48 import java.io.ByteArrayOutputStream;
49 import java.io.IOException;
50 import java.io.OutputStreamWriter;
51 import java.io.UnsupportedEncodingException;
52 import java.nio.charset.Charset;
53 import java.util.List;
54
55 /**
56 * Mutable builder to construct a commit recording the state of a project.
57 *
58 * Applications should use this object when they need to manually construct a
59 * commit and want precise control over its fields. For a higher level interface
60 * see {@link org.eclipse.jgit.api.CommitCommand}.
61 *
62 * To read a commit object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
63 * and obtain a {@link org.eclipse.jgit.revwalk.RevCommit} instance by calling
64 * {@link org.eclipse.jgit.revwalk.RevWalk#parseCommit(AnyObjectId)}.
65 */
66 public class CommitBuilder {
67 private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
68
69 private static final byte[] htree = Constants.encodeASCII("tree"); //$NON-NLS-1$
70
71 private static final byte[] hparent = Constants.encodeASCII("parent"); //$NON-NLS-1$
72
73 private static final byte[] hauthor = Constants.encodeASCII("author"); //$NON-NLS-1$
74
75 private static final byte[] hcommitter = Constants.encodeASCII("committer"); //$NON-NLS-1$
76
77 private static final byte[] hencoding = Constants.encodeASCII("encoding"); //$NON-NLS-1$
78
79 private ObjectId treeId;
80
81 private ObjectId[] parentIds;
82
83 private PersonIdent author;
84
85 private PersonIdent committer;
86
87 private String message;
88
89 private Charset encoding;
90
91 /** Initialize an empty commit. */
92 public CommitBuilder() {
93 parentIds = EMPTY_OBJECTID_LIST;
94 encoding = Constants.CHARSET;
95 }
96
97 /** @return id of the root tree listing this commit's snapshot. */
98 public ObjectId getTreeId() {
99 return treeId;
100 }
101
102 /**
103 * Set the tree id for this commit object
104 *
105 * @param id
106 * the tree identity.
107 */
108 public void setTreeId(AnyObjectId id) {
109 treeId = id.copy();
110 }
111
112 /** @return the author of this commit (who wrote it). */
113 public PersonIdent getAuthor() {
114 return author;
115 }
116
117 /**
118 * Set the author (name, email address, and date) of who wrote the commit.
119 *
120 * @param newAuthor
121 * the new author. Should not be null.
122 */
123 public void setAuthor(PersonIdent newAuthor) {
124 author = newAuthor;
125 }
126
127 /** @return the committer and commit time for this object. */
128 public PersonIdent getCommitter() {
129 return committer;
130 }
131
132 /**
133 * Set the committer and commit time for this object
134 *
135 * @param newCommitter
136 * the committer information. Should not be null.
137 */
138 public void setCommitter(PersonIdent newCommitter) {
139 committer = newCommitter;
140 }
141
142 /** @return the ancestors of this commit. Never null. */
143 public ObjectId[] getParentIds() {
144 return parentIds;
145 }
146
147 /**
148 * Set the parent of this commit.
149 *
150 * @param newParent
151 * the single parent for the commit.
152 */
153 public void setParentId(AnyObjectId newParent) {
154 parentIds = new ObjectId[] { newParent.copy() };
155 }
156
157 /**
158 * Set the parents of this commit.
159 *
160 * @param parent1
161 * the first parent of this commit. Typically this is the current
162 * value of the {@code HEAD} reference and is thus the current
163 * branch's position in history.
164 * @param parent2
165 * the second parent of this merge commit. Usually this is the
166 * branch being merged into the current branch.
167 */
168 public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
169 parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
170 }
171
172 /**
173 * Set the parents of this commit.
174 *
175 * @param newParents
176 * the entire list of parents for this commit.
177 */
178 public void setParentIds(ObjectId... newParents) {
179 parentIds = new ObjectId[newParents.length];
180 for (int i = 0; i < newParents.length; i++)
181 parentIds[i] = newParents[i].copy();
182 }
183
184 /**
185 * Set the parents of this commit.
186 *
187 * @param newParents
188 * the entire list of parents for this commit.
189 */
190 public void setParentIds(List<? extends AnyObjectId> newParents) {
191 parentIds = new ObjectId[newParents.size()];
192 for (int i = 0; i < newParents.size(); i++)
193 parentIds[i] = newParents.get(i).copy();
194 }
195
196 /**
197 * Add a parent onto the end of the parent list.
198 *
199 * @param additionalParent
200 * new parent to add onto the end of the current parent list.
201 */
202 public void addParentId(AnyObjectId additionalParent) {
203 if (parentIds.length == 0) {
204 setParentId(additionalParent);
205 } else {
206 ObjectId[] newParents = new ObjectId[parentIds.length + 1];
207 System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
208 newParents[parentIds.length] = additionalParent.copy();
209 parentIds = newParents;
210 }
211 }
212
213 /** @return the complete commit message. */
214 public String getMessage() {
215 return message;
216 }
217
218 /**
219 * Set the commit message.
220 *
221 * @param newMessage
222 * the commit message. Should not be null.
223 */
224 public void setMessage(final String newMessage) {
225 message = newMessage;
226 }
227
228 /**
229 * Set the encoding for the commit information
230 *
231 * @param encodingName
232 * the encoding name. See {@link Charset#forName(String)}.
233 */
234 public void setEncoding(String encodingName) {
235 encoding = Charset.forName(encodingName);
236 }
237
238 /**
239 * Set the encoding for the commit information
240 *
241 * @param enc
242 * the encoding to use.
243 */
244 public void setEncoding(Charset enc) {
245 encoding = enc;
246 }
247
248 /** @return the encoding that should be used for the commit message text. */
249 public Charset getEncoding() {
250 return encoding;
251 }
252
253 /**
254 * Format this builder's state as a commit object.
255 *
256 * @return this object in the canonical commit format, suitable for storage
257 * in a repository.
258 * @throws UnsupportedEncodingException
259 * the encoding specified by {@link #getEncoding()} is not
260 * supported by this Java runtime.
261 */
262 public byte[] build() throws UnsupportedEncodingException {
263 ByteArrayOutputStream os = new ByteArrayOutputStream();
264 OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
265 try {
266 os.write(htree);
267 os.write(' ');
268 getTreeId().copyTo(os);
269 os.write('\n');
270
271 for (ObjectId p : getParentIds()) {
272 os.write(hparent);
273 os.write(' ');
274 p.copyTo(os);
275 os.write('\n');
276 }
277
278 os.write(hauthor);
279 os.write(' ');
280 w.write(getAuthor().toExternalString());
281 w.flush();
282 os.write('\n');
283
284 os.write(hcommitter);
285 os.write(' ');
286 w.write(getCommitter().toExternalString());
287 w.flush();
288 os.write('\n');
289
290 if (getEncoding() != Constants.CHARSET) {
291 os.write(hencoding);
292 os.write(' ');
293 os.write(Constants.encodeASCII(getEncoding().name()));
294 os.write('\n');
295 }
296
297 os.write('\n');
298
299 if (getMessage() != null) {
300 w.write(getMessage());
301 w.flush();
302 }
303 } catch (IOException err) {
304 // This should never occur, the only way to get it above is
305 // for the ByteArrayOutputStream to throw, but it doesn't.
306 //
307 throw new RuntimeException(err);
308 }
309 return os.toByteArray();
310 }
311
312 /**
313 * Format this builder's state as a commit object.
314 *
315 * @return this object in the canonical commit format, suitable for storage
316 * in a repository.
317 * @throws UnsupportedEncodingException
318 * the encoding specified by {@link #getEncoding()} is not
319 * supported by this Java runtime.
320 */
321 public byte[] toByteArray() throws UnsupportedEncodingException {
322 return build();
323 }
324
325 @SuppressWarnings("nls")
326 @Override
327 public String toString() {
328 StringBuilder r = new StringBuilder();
329 r.append("Commit");
330 r.append("={\n");
331
332 r.append("tree ");
333 r.append(treeId != null ? treeId.name() : "NOT_SET");
334 r.append("\n");
335
336 for (ObjectId p : parentIds) {
337 r.append("parent ");
338 r.append(p.name());
339 r.append("\n");
340 }
341
342 r.append("author ");
343 r.append(author != null ? author.toString() : "NOT_SET");
344 r.append("\n");
345
346 r.append("committer ");
347 r.append(committer != null ? committer.toString() : "NOT_SET");
348 r.append("\n");
349
350 if (encoding != null && encoding != Constants.CHARSET) {
351 r.append("encoding ");
352 r.append(encoding.name());
353 r.append("\n");
354 }
355
356 r.append("\n");
357 r.append(message != null ? message : "");
358 r.append("}");
359 return r.toString();
360 }
361 }