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 10KB

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