You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommitBuilder.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. package org.eclipse.jgit.lib;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.IOException;
  49. import java.io.OutputStreamWriter;
  50. import java.io.UnsupportedEncodingException;
  51. import java.nio.charset.Charset;
  52. import java.util.List;
  53. /**
  54. * Mutable builder to construct a commit recording the state of a project.
  55. *
  56. * Applications should use this object when they need to manually construct a
  57. * commit and want precise control over its fields. For a higher level interface
  58. * see {@link org.eclipse.jgit.api.CommitCommand}.
  59. *
  60. * To read a commit object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
  61. * and obtain a {@link org.eclipse.jgit.revwalk.RevCommit} instance by calling
  62. * {@link org.eclipse.jgit.revwalk.RevWalk#parseCommit(AnyObjectId)}.
  63. */
  64. public class CommitBuilder {
  65. private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
  66. private static final byte[] htree = Constants.encodeASCII("tree"); //$NON-NLS-1$
  67. private static final byte[] hparent = Constants.encodeASCII("parent"); //$NON-NLS-1$
  68. private static final byte[] hauthor = Constants.encodeASCII("author"); //$NON-NLS-1$
  69. private static final byte[] hcommitter = Constants.encodeASCII("committer"); //$NON-NLS-1$
  70. private static final byte[] hencoding = Constants.encodeASCII("encoding"); //$NON-NLS-1$
  71. private ObjectId treeId;
  72. private ObjectId[] parentIds;
  73. private PersonIdent author;
  74. private PersonIdent committer;
  75. private String message;
  76. private Charset encoding;
  77. /**
  78. * Initialize an empty commit.
  79. */
  80. public CommitBuilder() {
  81. parentIds = EMPTY_OBJECTID_LIST;
  82. encoding = UTF_8;
  83. }
  84. /**
  85. * Get id of the root tree listing this commit's snapshot.
  86. *
  87. * @return id of the root tree listing this commit's snapshot.
  88. */
  89. public ObjectId getTreeId() {
  90. return treeId;
  91. }
  92. /**
  93. * Set the tree id for this commit object
  94. *
  95. * @param id
  96. * the tree identity.
  97. */
  98. public void setTreeId(AnyObjectId id) {
  99. treeId = id.copy();
  100. }
  101. /**
  102. * Get the author of this commit (who wrote it).
  103. *
  104. * @return the author of this commit (who wrote it).
  105. */
  106. public PersonIdent getAuthor() {
  107. return author;
  108. }
  109. /**
  110. * Set the author (name, email address, and date) of who wrote the commit.
  111. *
  112. * @param newAuthor
  113. * the new author. Should not be null.
  114. */
  115. public void setAuthor(PersonIdent newAuthor) {
  116. author = newAuthor;
  117. }
  118. /**
  119. * Get the committer and commit time for this object.
  120. *
  121. * @return the committer and commit time for this object.
  122. */
  123. public PersonIdent getCommitter() {
  124. return committer;
  125. }
  126. /**
  127. * Set the committer and commit time for this object
  128. *
  129. * @param newCommitter
  130. * the committer information. Should not be null.
  131. */
  132. public void setCommitter(PersonIdent newCommitter) {
  133. committer = newCommitter;
  134. }
  135. /**
  136. * Get the ancestors of this commit.
  137. *
  138. * @return the ancestors of this commit. Never null.
  139. */
  140. public ObjectId[] getParentIds() {
  141. return parentIds;
  142. }
  143. /**
  144. * Set the parent of this commit.
  145. *
  146. * @param newParent
  147. * the single parent for the commit.
  148. */
  149. public void setParentId(AnyObjectId newParent) {
  150. parentIds = new ObjectId[] { newParent.copy() };
  151. }
  152. /**
  153. * Set the parents of this commit.
  154. *
  155. * @param parent1
  156. * the first parent of this commit. Typically this is the current
  157. * value of the {@code HEAD} reference and is thus the current
  158. * branch's position in history.
  159. * @param parent2
  160. * the second parent of this merge commit. Usually this is the
  161. * branch being merged into the current branch.
  162. */
  163. public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
  164. parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
  165. }
  166. /**
  167. * Set the parents of this commit.
  168. *
  169. * @param newParents
  170. * the entire list of parents for this commit.
  171. */
  172. public void setParentIds(ObjectId... newParents) {
  173. parentIds = new ObjectId[newParents.length];
  174. for (int i = 0; i < newParents.length; i++)
  175. parentIds[i] = newParents[i].copy();
  176. }
  177. /**
  178. * Set the parents of this commit.
  179. *
  180. * @param newParents
  181. * the entire list of parents for this commit.
  182. */
  183. public void setParentIds(List<? extends AnyObjectId> newParents) {
  184. parentIds = new ObjectId[newParents.size()];
  185. for (int i = 0; i < newParents.size(); i++)
  186. parentIds[i] = newParents.get(i).copy();
  187. }
  188. /**
  189. * Add a parent onto the end of the parent list.
  190. *
  191. * @param additionalParent
  192. * new parent to add onto the end of the current parent list.
  193. */
  194. public void addParentId(AnyObjectId additionalParent) {
  195. if (parentIds.length == 0) {
  196. setParentId(additionalParent);
  197. } else {
  198. ObjectId[] newParents = new ObjectId[parentIds.length + 1];
  199. System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
  200. newParents[parentIds.length] = additionalParent.copy();
  201. parentIds = newParents;
  202. }
  203. }
  204. /**
  205. * Get the complete commit message.
  206. *
  207. * @return the complete commit message.
  208. */
  209. public String getMessage() {
  210. return message;
  211. }
  212. /**
  213. * Set the commit message.
  214. *
  215. * @param newMessage
  216. * the commit message. Should not be null.
  217. */
  218. public void setMessage(String newMessage) {
  219. message = newMessage;
  220. }
  221. /**
  222. * Set the encoding for the commit information
  223. *
  224. * @param encodingName
  225. * the encoding name. See
  226. * {@link java.nio.charset.Charset#forName(String)}.
  227. */
  228. public void setEncoding(String encodingName) {
  229. encoding = Charset.forName(encodingName);
  230. }
  231. /**
  232. * Set the encoding for the commit information
  233. *
  234. * @param enc
  235. * the encoding to use.
  236. */
  237. public void setEncoding(Charset enc) {
  238. encoding = enc;
  239. }
  240. /**
  241. * Get the encoding that should be used for the commit message text.
  242. *
  243. * @return the encoding that should be used for the commit message text.
  244. */
  245. public Charset getEncoding() {
  246. return encoding;
  247. }
  248. /**
  249. * Format this builder's state as a commit object.
  250. *
  251. * @return this object in the canonical commit format, suitable for storage
  252. * in a repository.
  253. * @throws java.io.UnsupportedEncodingException
  254. * the encoding specified by {@link #getEncoding()} is not
  255. * supported by this Java runtime.
  256. */
  257. public byte[] build() throws UnsupportedEncodingException {
  258. ByteArrayOutputStream os = new ByteArrayOutputStream();
  259. OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
  260. try {
  261. os.write(htree);
  262. os.write(' ');
  263. getTreeId().copyTo(os);
  264. os.write('\n');
  265. for (ObjectId p : getParentIds()) {
  266. os.write(hparent);
  267. os.write(' ');
  268. p.copyTo(os);
  269. os.write('\n');
  270. }
  271. os.write(hauthor);
  272. os.write(' ');
  273. w.write(getAuthor().toExternalString());
  274. w.flush();
  275. os.write('\n');
  276. os.write(hcommitter);
  277. os.write(' ');
  278. w.write(getCommitter().toExternalString());
  279. w.flush();
  280. os.write('\n');
  281. if (getEncoding() != UTF_8) {
  282. os.write(hencoding);
  283. os.write(' ');
  284. os.write(Constants.encodeASCII(getEncoding().name()));
  285. os.write('\n');
  286. }
  287. os.write('\n');
  288. if (getMessage() != null) {
  289. w.write(getMessage());
  290. w.flush();
  291. }
  292. } catch (IOException err) {
  293. // This should never occur, the only way to get it above is
  294. // for the ByteArrayOutputStream to throw, but it doesn't.
  295. //
  296. throw new RuntimeException(err);
  297. }
  298. return os.toByteArray();
  299. }
  300. /**
  301. * Format this builder's state as a commit object.
  302. *
  303. * @return this object in the canonical commit format, suitable for storage
  304. * in a repository.
  305. * @throws java.io.UnsupportedEncodingException
  306. * the encoding specified by {@link #getEncoding()} is not
  307. * supported by this Java runtime.
  308. */
  309. public byte[] toByteArray() throws UnsupportedEncodingException {
  310. return build();
  311. }
  312. /** {@inheritDoc} */
  313. @SuppressWarnings("nls")
  314. @Override
  315. public String toString() {
  316. StringBuilder r = new StringBuilder();
  317. r.append("Commit");
  318. r.append("={\n");
  319. r.append("tree ");
  320. r.append(treeId != null ? treeId.name() : "NOT_SET");
  321. r.append("\n");
  322. for (ObjectId p : parentIds) {
  323. r.append("parent ");
  324. r.append(p.name());
  325. r.append("\n");
  326. }
  327. r.append("author ");
  328. r.append(author != null ? author.toString() : "NOT_SET");
  329. r.append("\n");
  330. r.append("committer ");
  331. r.append(committer != null ? committer.toString() : "NOT_SET");
  332. r.append("\n");
  333. if (encoding != null && encoding != UTF_8) {
  334. r.append("encoding ");
  335. r.append(encoding.name());
  336. r.append("\n");
  337. }
  338. r.append("\n");
  339. r.append(message != null ? message : "");
  340. r.append("}");
  341. return r.toString();
  342. }
  343. }