1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import static java.nio.charset.StandardCharsets.UTF_8;
16
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.OutputStreamWriter;
20 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.Charset;
22 import java.util.List;
23
24 import org.eclipse.jgit.util.References;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class CommitBuilder extends ObjectBuilder {
38 private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
39
40 private static final byte[] htree = Constants.encodeASCII("tree");
41
42 private static final byte[] hparent = Constants.encodeASCII("parent");
43
44 private static final byte[] hauthor = Constants.encodeASCII("author");
45
46 private static final byte[] hcommitter = Constants.encodeASCII("committer");
47
48 private static final byte[] hgpgsig = Constants.encodeASCII("gpgsig");
49
50 private ObjectId treeId;
51
52 private ObjectId[] parentIds;
53
54 private PersonIdent committer;
55
56
57
58
59 public CommitBuilder() {
60 parentIds = EMPTY_OBJECTID_LIST;
61 }
62
63
64
65
66
67
68 public ObjectId getTreeId() {
69 return treeId;
70 }
71
72
73
74
75
76
77
78 public void setTreeId(AnyObjectId id) {
79 treeId = id.copy();
80 }
81
82
83
84
85
86
87 @Override
88 public PersonIdent getAuthor() {
89 return super.getAuthor();
90 }
91
92
93
94
95
96
97
98 @Override
99 public void setAuthor(PersonIdent newAuthor) {
100 super.setAuthor(newAuthor);
101 }
102
103
104
105
106
107
108 public PersonIdent getCommitter() {
109 return committer;
110 }
111
112
113
114
115
116
117
118 public void setCommitter(PersonIdent newCommitter) {
119 committer = newCommitter;
120 }
121
122
123
124
125
126
127 public ObjectId[] getParentIds() {
128 return parentIds;
129 }
130
131
132
133
134
135
136
137 public void setParentId(AnyObjectId newParent) {
138 parentIds = new ObjectId[] { newParent.copy() };
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152 public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
153 parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
154 }
155
156
157
158
159
160
161
162 public void setParentIds(ObjectId... newParents) {
163 parentIds = new ObjectId[newParents.length];
164 for (int i = 0; i < newParents.length; i++)
165 parentIds[i] = newParents[i].copy();
166 }
167
168
169
170
171
172
173
174 public void setParentIds(List<? extends AnyObjectId> newParents) {
175 parentIds = new ObjectId[newParents.size()];
176 for (int i = 0; i < newParents.size(); i++)
177 parentIds[i] = newParents.get(i).copy();
178 }
179
180
181
182
183
184
185
186 public void addParentId(AnyObjectId additionalParent) {
187 if (parentIds.length == 0) {
188 setParentId(additionalParent);
189 } else {
190 ObjectId[] newParents = new ObjectId[parentIds.length + 1];
191 System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
192 newParents[parentIds.length] = additionalParent.copy();
193 parentIds = newParents;
194 }
195 }
196
197
198
199
200
201
202
203
204
205 @Deprecated
206 public void setEncoding(String encodingName) {
207 setEncoding(Charset.forName(encodingName));
208 }
209
210 @Override
211 public byte[] build() throws UnsupportedEncodingException {
212 ByteArrayOutputStream os = new ByteArrayOutputStream();
213 OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
214 try {
215 os.write(htree);
216 os.write(' ');
217 getTreeId().copyTo(os);
218 os.write('\n');
219
220 for (ObjectId p : getParentIds()) {
221 os.write(hparent);
222 os.write(' ');
223 p.copyTo(os);
224 os.write('\n');
225 }
226
227 os.write(hauthor);
228 os.write(' ');
229 w.write(getAuthor().toExternalString());
230 w.flush();
231 os.write('\n');
232
233 os.write(hcommitter);
234 os.write(' ');
235 w.write(getCommitter().toExternalString());
236 w.flush();
237 os.write('\n');
238
239 GpgSignature signature = getGpgSignature();
240 if (signature != null) {
241 os.write(hgpgsig);
242 os.write(' ');
243 writeMultiLineHeader(signature.toExternalString(), os,
244 true);
245 os.write('\n');
246 }
247
248 writeEncoding(getEncoding(), os);
249
250 os.write('\n');
251
252 if (getMessage() != null) {
253 w.write(getMessage());
254 w.flush();
255 }
256 } catch (IOException err) {
257
258
259
260 throw new RuntimeException(err);
261 }
262 return os.toByteArray();
263 }
264
265
266
267
268
269
270
271
272
273
274 public byte[] toByteArray() throws UnsupportedEncodingException {
275 return build();
276 }
277
278
279 @SuppressWarnings("nls")
280 @Override
281 public String toString() {
282 StringBuilder r = new StringBuilder();
283 r.append("Commit");
284 r.append("={\n");
285
286 r.append("tree ");
287 r.append(treeId != null ? treeId.name() : "NOT_SET");
288 r.append("\n");
289
290 for (ObjectId p : parentIds) {
291 r.append("parent ");
292 r.append(p.name());
293 r.append("\n");
294 }
295
296 r.append("author ");
297 r.append(getAuthor() != null ? getAuthor().toString() : "NOT_SET");
298 r.append("\n");
299
300 r.append("committer ");
301 r.append(committer != null ? committer.toString() : "NOT_SET");
302 r.append("\n");
303
304 r.append("gpgSignature ");
305 GpgSignature signature = getGpgSignature();
306 r.append(signature != null ? signature.toString()
307 : "NOT_SET");
308 r.append("\n");
309
310 Charset encoding = getEncoding();
311 if (!References.isSameObject(encoding, UTF_8)) {
312 r.append("encoding ");
313 r.append(encoding.name());
314 r.append("\n");
315 }
316
317 r.append("\n");
318 r.append(getMessage() != null ? getMessage() : "");
319 r.append("}");
320 return r.toString();
321 }
322 }