1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
* Copyright (C) 2006, 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2006, 2020, Shawn O. Pearce <spearce@spearce.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.lib;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import org.eclipse.jgit.util.References;
/**
* Mutable builder to construct a commit recording the state of a project.
*
* Applications should use this object when they need to manually construct a
* commit and want precise control over its fields. For a higher level interface
* see {@link org.eclipse.jgit.api.CommitCommand}.
*
* To read a commit object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
* and obtain a {@link org.eclipse.jgit.revwalk.RevCommit} instance by calling
* {@link org.eclipse.jgit.revwalk.RevWalk#parseCommit(AnyObjectId)}.
*/
public class CommitBuilder extends ObjectBuilder {
private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
private static final byte[] htree = Constants.encodeASCII("tree"); //$NON-NLS-1$
private static final byte[] hparent = Constants.encodeASCII("parent"); //$NON-NLS-1$
private static final byte[] hauthor = Constants.encodeASCII("author"); //$NON-NLS-1$
private static final byte[] hcommitter = Constants.encodeASCII("committer"); //$NON-NLS-1$
private static final byte[] hgpgsig = Constants.encodeASCII("gpgsig"); //$NON-NLS-1$
private ObjectId treeId;
private ObjectId[] parentIds;
private PersonIdent committer;
/**
* Initialize an empty commit.
*/
public CommitBuilder() {
parentIds = EMPTY_OBJECTID_LIST;
}
/**
* Get id of the root tree listing this commit's snapshot.
*
* @return id of the root tree listing this commit's snapshot.
*/
public ObjectId getTreeId() {
return treeId;
}
/**
* Set the tree id for this commit object.
*
* @param id
* the tree identity.
*/
public void setTreeId(AnyObjectId id) {
treeId = id.copy();
}
/**
* Get the author of this commit (who wrote it).
*
* @return the author of this commit (who wrote it).
*/
@Override
public PersonIdent getAuthor() {
return super.getAuthor();
}
/**
* Set the author (name, email address, and date) of who wrote the commit.
*
* @param newAuthor
* the new author. Should not be null.
*/
@Override
public void setAuthor(PersonIdent newAuthor) {
super.setAuthor(newAuthor);
}
/**
* Get the committer and commit time for this object.
*
* @return the committer and commit time for this object.
*/
public PersonIdent getCommitter() {
return committer;
}
/**
* Set the committer and commit time for this object.
*
* @param newCommitter
* the committer information. Should not be null.
*/
public void setCommitter(PersonIdent newCommitter) {
committer = newCommitter;
}
/**
* Get the ancestors of this commit.
*
* @return the ancestors of this commit. Never null.
*/
public ObjectId[] getParentIds() {
return parentIds;
}
/**
* Set the parent of this commit.
*
* @param newParent
* the single parent for the commit.
*/
public void setParentId(AnyObjectId newParent) {
parentIds = new ObjectId[] { newParent.copy() };
}
/**
* Set the parents of this commit.
*
* @param parent1
* the first parent of this commit. Typically this is the current
* value of the {@code HEAD} reference and is thus the current
* branch's position in history.
* @param parent2
* the second parent of this merge commit. Usually this is the
* branch being merged into the current branch.
*/
public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
}
/**
* Set the parents of this commit.
*
* @param newParents
* the entire list of parents for this commit.
*/
public void setParentIds(ObjectId... newParents) {
parentIds = new ObjectId[newParents.length];
for (int i = 0; i < newParents.length; i++)
parentIds[i] = newParents[i].copy();
}
/**
* Set the parents of this commit.
*
* @param newParents
* the entire list of parents for this commit.
*/
public void setParentIds(List<? extends AnyObjectId> newParents) {
parentIds = new ObjectId[newParents.size()];
for (int i = 0; i < newParents.size(); i++)
parentIds[i] = newParents.get(i).copy();
}
/**
* Add a parent onto the end of the parent list.
*
* @param additionalParent
* new parent to add onto the end of the current parent list.
*/
public void addParentId(AnyObjectId additionalParent) {
if (parentIds.length == 0) {
setParentId(additionalParent);
} else {
ObjectId[] newParents = new ObjectId[parentIds.length + 1];
System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
newParents[parentIds.length] = additionalParent.copy();
parentIds = newParents;
}
}
/**
* Set the encoding for the commit information.
*
* @param encodingName
* the encoding name. See
* {@link java.nio.charset.Charset#forName(String)}.
* @deprecated use {@link #setEncoding(Charset)} instead.
*/
@Deprecated
public void setEncoding(String encodingName) {
setEncoding(Charset.forName(encodingName));
}
@Override
public byte[] build() throws UnsupportedEncodingException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
try {
os.write(htree);
os.write(' ');
getTreeId().copyTo(os);
os.write('\n');
for (ObjectId p : getParentIds()) {
os.write(hparent);
os.write(' ');
p.copyTo(os);
os.write('\n');
}
os.write(hauthor);
os.write(' ');
w.write(getAuthor().toExternalString());
w.flush();
os.write('\n');
os.write(hcommitter);
os.write(' ');
w.write(getCommitter().toExternalString());
w.flush();
os.write('\n');
GpgSignature signature = getGpgSignature();
if (signature != null) {
os.write(hgpgsig);
os.write(' ');
writeMultiLineHeader(signature.toExternalString(), os,
true);
os.write('\n');
}
writeEncoding(getEncoding(), os);
os.write('\n');
if (getMessage() != null) {
w.write(getMessage());
w.flush();
}
} catch (IOException err) {
// This should never occur, the only way to get it above is
// for the ByteArrayOutputStream to throw, but it doesn't.
//
throw new RuntimeException(err);
}
return os.toByteArray();
}
/**
* Format this builder's state as a commit object.
*
* @return this object in the canonical commit format, suitable for storage
* in a repository.
* @throws java.io.UnsupportedEncodingException
* the encoding specified by {@link #getEncoding()} is not
* supported by this Java runtime.
*/
public byte[] toByteArray() throws UnsupportedEncodingException {
return build();
}
/** {@inheritDoc} */
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder r = new StringBuilder();
r.append("Commit");
r.append("={\n");
r.append("tree ");
r.append(treeId != null ? treeId.name() : "NOT_SET");
r.append("\n");
for (ObjectId p : parentIds) {
r.append("parent ");
r.append(p.name());
r.append("\n");
}
r.append("author ");
r.append(getAuthor() != null ? getAuthor().toString() : "NOT_SET");
r.append("\n");
r.append("committer ");
r.append(committer != null ? committer.toString() : "NOT_SET");
r.append("\n");
r.append("gpgSignature ");
GpgSignature signature = getGpgSignature();
r.append(signature != null ? signature.toString()
: "NOT_SET");
r.append("\n");
Charset encoding = getEncoding();
if (!References.isSameObject(encoding, UTF_8)) {
r.append("encoding ");
r.append(encoding.name());
r.append("\n");
}
r.append("\n");
r.append(getMessage() != null ? getMessage() : "");
r.append("}");
return r.toString();
}
}
|