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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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");
  66. private static final byte[] hparent = Constants.encodeASCII("parent");
  67. private static final byte[] hauthor = Constants.encodeASCII("author");
  68. private static final byte[] hcommitter = Constants.encodeASCII("committer");
  69. private static final byte[] hencoding = Constants.encodeASCII("encoding");
  70. private ObjectId commitId;
  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. /** Initialize an empty commit. */
  78. public CommitBuilder() {
  79. parentIds = EMPTY_OBJECTID_LIST;
  80. encoding = Constants.CHARSET;
  81. }
  82. /** @return this commit's object id. */
  83. public ObjectId getCommitId() {
  84. return commitId;
  85. }
  86. /**
  87. * Set the id of this commit object.
  88. *
  89. * @param id
  90. * the id that we calculated for this object.
  91. */
  92. public void setCommitId(final ObjectId id) {
  93. commitId = id;
  94. }
  95. /** @return id of the root tree listing this commit's snapshot. */
  96. public ObjectId getTreeId() {
  97. return treeId;
  98. }
  99. /**
  100. * Set the tree id for this commit object
  101. *
  102. * @param id
  103. * the tree identity.
  104. */
  105. public void setTreeId(AnyObjectId id) {
  106. treeId = id.copy();
  107. commitId = null;
  108. }
  109. /** @return the author of this commit (who wrote it). */
  110. public PersonIdent getAuthor() {
  111. return author;
  112. }
  113. /**
  114. * Set the author (name, email address, and date) of who wrote the commit.
  115. *
  116. * @param newAuthor
  117. * the new author. Should not be null.
  118. */
  119. public void setAuthor(PersonIdent newAuthor) {
  120. author = newAuthor;
  121. commitId = null;
  122. }
  123. /** @return the committer and commit time for this object. */
  124. public PersonIdent getCommitter() {
  125. return committer;
  126. }
  127. /**
  128. * Set the committer and commit time for this object
  129. *
  130. * @param newCommitter
  131. * the committer information. Should not be null.
  132. */
  133. public void setCommitter(PersonIdent newCommitter) {
  134. committer = newCommitter;
  135. commitId = null;
  136. }
  137. /** @return the ancestors of this commit. Never null. */
  138. public ObjectId[] getParentIds() {
  139. return parentIds;
  140. }
  141. /**
  142. * Set the parent of this commit.
  143. *
  144. * @param newParent
  145. * the single parent for the commit.
  146. */
  147. public void setParentId(AnyObjectId newParent) {
  148. parentIds = new ObjectId[] { newParent.copy() };
  149. commitId = null;
  150. }
  151. /**
  152. * Set the parents of this commit.
  153. *
  154. * @param parent1
  155. * the first parent of this commit. Typically this is the current
  156. * value of the {@code HEAD} reference and is thus the current
  157. * branch's position in history.
  158. * @param parent2
  159. * the second parent of this merge commit. Usually this is the
  160. * branch being merged into the current branch.
  161. */
  162. public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
  163. parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
  164. commitId = null;
  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. commitId = null;
  177. }
  178. /**
  179. * Set the parents of this commit.
  180. *
  181. * @param newParents
  182. * the entire list of parents for this commit.
  183. */
  184. public void setParentIds(List<? extends AnyObjectId> newParents) {
  185. parentIds = new ObjectId[newParents.size()];
  186. for (int i = 0; i < newParents.size(); i++)
  187. parentIds[i] = newParents.get(i).copy();
  188. commitId = null;
  189. }
  190. /**
  191. * Add a parent onto the end of the parent list.
  192. *
  193. * @param additionalParent
  194. * new parent to add onto the end of the current parent list.
  195. */
  196. public void addParentId(AnyObjectId additionalParent) {
  197. if (parentIds.length == 0) {
  198. setParentId(additionalParent);
  199. } else {
  200. ObjectId[] newParents = new ObjectId[parentIds.length + 1];
  201. for (int i = 0; i < parentIds.length; i++)
  202. newParents[i] = parentIds[i];
  203. newParents[parentIds.length] = additionalParent.copy();
  204. parentIds = newParents;
  205. commitId = null;
  206. }
  207. }
  208. /** @return the complete commit message. */
  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(final String newMessage) {
  219. message = newMessage;
  220. }
  221. /**
  222. * Set the encoding for the commit information
  223. *
  224. * @param encodingName
  225. * the encoding name. See {@link Charset#forName(String)}.
  226. */
  227. public void setEncoding(String encodingName) {
  228. encoding = Charset.forName(encodingName);
  229. }
  230. /**
  231. * Set the encoding for the commit information
  232. *
  233. * @param enc
  234. * the encoding to use.
  235. */
  236. public void setEncoding(Charset enc) {
  237. encoding = enc;
  238. }
  239. /** @return the encoding that should be used for the commit message text. */
  240. public Charset getEncoding() {
  241. return encoding;
  242. }
  243. /**
  244. * Format this builder's state as a commit object.
  245. *
  246. * As a side effect, {@link #getCommitId()} will be populated with the
  247. * proper ObjectId for the formatted content.
  248. *
  249. * @return this object in the canonical commit format, suitable for storage
  250. * in a repository.
  251. * @throws UnsupportedEncodingException
  252. * the encoding specified by {@link #getEncoding()} is not
  253. * supported by this Java runtime.
  254. */
  255. public byte[] format() throws UnsupportedEncodingException {
  256. return format(new ObjectInserter.Formatter());
  257. }
  258. /**
  259. * Format this builder's state as a commit object.
  260. *
  261. * As a side effect, {@link #getCommitId()} will be populated with the
  262. * proper ObjectId for the formatted content.
  263. *
  264. * @param oi
  265. * the inserter whose formatting support will be reused. The
  266. * inserter itself is not affected, and the commit is not
  267. * actually inserted into the repository.
  268. * @return this object in the canonical commit format, suitable for storage
  269. * in a repository.
  270. * @throws UnsupportedEncodingException
  271. * the encoding specified by {@link #getEncoding()} is not
  272. * supported by this Java runtime.
  273. */
  274. public byte[] format(ObjectInserter oi) throws UnsupportedEncodingException {
  275. ByteArrayOutputStream os = new ByteArrayOutputStream();
  276. OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
  277. try {
  278. os.write(htree);
  279. os.write(' ');
  280. getTreeId().copyTo(os);
  281. os.write('\n');
  282. for (ObjectId p : getParentIds()) {
  283. os.write(hparent);
  284. os.write(' ');
  285. p.copyTo(os);
  286. os.write('\n');
  287. }
  288. os.write(hauthor);
  289. os.write(' ');
  290. w.write(getAuthor().toExternalString());
  291. w.flush();
  292. os.write('\n');
  293. os.write(hcommitter);
  294. os.write(' ');
  295. w.write(getCommitter().toExternalString());
  296. w.flush();
  297. os.write('\n');
  298. if (getEncoding() != Constants.CHARSET) {
  299. os.write(hencoding);
  300. os.write(' ');
  301. os.write(Constants.encodeASCII(getEncoding().name()));
  302. os.write('\n');
  303. }
  304. os.write('\n');
  305. if (getMessage() != null) {
  306. w.write(getMessage());
  307. w.flush();
  308. }
  309. } catch (IOException err) {
  310. // This should never occur, the only way to get it above is
  311. // for the ByteArrayOutputStream to throw, but it doesn't.
  312. //
  313. throw new RuntimeException(err);
  314. }
  315. byte[] content = os.toByteArray();
  316. setCommitId(oi.idFor(Constants.OBJ_COMMIT, content));
  317. return content;
  318. }
  319. @Override
  320. public String toString() {
  321. StringBuilder r = new StringBuilder();
  322. r.append("Commit");
  323. if (commitId != null)
  324. r.append("[" + commitId.name() + "]");
  325. r.append("={\n");
  326. r.append("tree ");
  327. r.append(treeId != null ? treeId.name() : "NOT_SET");
  328. r.append("\n");
  329. for (ObjectId p : parentIds) {
  330. r.append("parent ");
  331. r.append(p.name());
  332. r.append("\n");
  333. }
  334. r.append("author ");
  335. r.append(author != null ? author.toString() : "NOT_SET");
  336. r.append("\n");
  337. r.append("committer ");
  338. r.append(committer != null ? committer.toString() : "NOT_SET");
  339. r.append("\n");
  340. if (encoding != null && encoding != Constants.CHARSET) {
  341. r.append("encoding ");
  342. r.append(encoding.name());
  343. r.append("\n");
  344. }
  345. r.append("\n");
  346. r.append(message != null ? message : "");
  347. r.append("}");
  348. return r.toString();
  349. }
  350. }