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.

Commit.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.ByteArrayInputStream;
  47. import java.io.DataInputStream;
  48. import java.io.IOException;
  49. import java.nio.charset.Charset;
  50. import org.eclipse.jgit.errors.CorruptObjectException;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. /**
  53. * Instances of this class represent a Commit object. It represents a snapshot
  54. * in a Git repository, who created it and when.
  55. */
  56. public class Commit implements Treeish {
  57. private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
  58. private final Repository objdb;
  59. private ObjectId commitId;
  60. private ObjectId treeId;
  61. private ObjectId[] parentIds;
  62. private PersonIdent author;
  63. private PersonIdent committer;
  64. private String message;
  65. private Tree treeObj;
  66. private byte[] raw;
  67. private Charset encoding;
  68. /**
  69. * Create an empty commit object. More information must be fed to this
  70. * object to make it useful.
  71. *
  72. * @param db
  73. * The repository with which to associate it.
  74. */
  75. public Commit(final Repository db) {
  76. objdb = db;
  77. parentIds = EMPTY_OBJECTID_LIST;
  78. }
  79. /**
  80. * Create a commit associated with these parents and associate it with a
  81. * repository.
  82. *
  83. * @param db
  84. * The repository to which this commit object belongs
  85. * @param parentIds
  86. * Id's of the parent(s)
  87. */
  88. public Commit(final Repository db, final ObjectId[] parentIds) {
  89. objdb = db;
  90. this.parentIds = parentIds;
  91. }
  92. /**
  93. * Create a commit object with the specified id and data from and existing
  94. * commit object in a repository.
  95. *
  96. * @param db
  97. * The repository to which this commit object belongs
  98. * @param id
  99. * Commit id
  100. * @param raw
  101. * Raw commit object data
  102. */
  103. public Commit(final Repository db, final ObjectId id, final byte[] raw) {
  104. objdb = db;
  105. commitId = id;
  106. treeId = ObjectId.fromString(raw, 5);
  107. parentIds = new ObjectId[1];
  108. int np=0;
  109. int rawPtr = 46;
  110. for (;;) {
  111. if (raw[rawPtr] != 'p')
  112. break;
  113. if (np == 0) {
  114. parentIds[np++] = ObjectId.fromString(raw, rawPtr + 7);
  115. } else if (np == 1) {
  116. parentIds = new ObjectId[] { parentIds[0], ObjectId.fromString(raw, rawPtr + 7) };
  117. np++;
  118. } else {
  119. if (parentIds.length <= np) {
  120. ObjectId[] old = parentIds;
  121. parentIds = new ObjectId[parentIds.length+32];
  122. for (int i=0; i<np; ++i)
  123. parentIds[i] = old[i];
  124. }
  125. parentIds[np++] = ObjectId.fromString(raw, rawPtr + 7);
  126. }
  127. rawPtr += 48;
  128. }
  129. if (np != parentIds.length) {
  130. ObjectId[] old = parentIds;
  131. parentIds = new ObjectId[np];
  132. for (int i=0; i<np; ++i)
  133. parentIds[i] = old[i];
  134. } else
  135. if (np == 0)
  136. parentIds = EMPTY_OBJECTID_LIST;
  137. this.raw = raw;
  138. }
  139. /**
  140. * @return get repository for the commit
  141. */
  142. public Repository getRepository() {
  143. return objdb;
  144. }
  145. /**
  146. * @return The commit object id
  147. */
  148. public ObjectId getCommitId() {
  149. return commitId;
  150. }
  151. /**
  152. * Set the id of this object.
  153. *
  154. * @param id
  155. * the id that we calculated for this object.
  156. */
  157. public void setCommitId(final ObjectId id) {
  158. commitId = id;
  159. }
  160. public ObjectId getTreeId() {
  161. return treeId;
  162. }
  163. /**
  164. * Set the tree id for this commit object
  165. *
  166. * @param id
  167. */
  168. public void setTreeId(final ObjectId id) {
  169. if (treeId==null || !treeId.equals(id)) {
  170. treeObj = null;
  171. }
  172. treeId = id;
  173. }
  174. public Tree getTree() throws IOException {
  175. if (treeObj == null) {
  176. treeObj = objdb.mapTree(getTreeId());
  177. if (treeObj == null) {
  178. throw new MissingObjectException(getTreeId(),
  179. Constants.TYPE_TREE);
  180. }
  181. }
  182. return treeObj;
  183. }
  184. /**
  185. * Set the tree object for this commit
  186. * @see #setTreeId
  187. * @param t the Tree object
  188. */
  189. public void setTree(final Tree t) {
  190. treeId = t.getTreeId();
  191. treeObj = t;
  192. }
  193. /**
  194. * @return the author and authoring time for this commit
  195. */
  196. public PersonIdent getAuthor() {
  197. decode();
  198. return author;
  199. }
  200. /**
  201. * Set the author and authoring time for this commit
  202. * @param a
  203. */
  204. public void setAuthor(final PersonIdent a) {
  205. author = a;
  206. }
  207. /**
  208. * @return the committer and commit time for this object
  209. */
  210. public PersonIdent getCommitter() {
  211. decode();
  212. return committer;
  213. }
  214. /**
  215. * Set the committer and commit time for this object
  216. * @param c the committer information
  217. */
  218. public void setCommitter(final PersonIdent c) {
  219. committer = c;
  220. }
  221. /**
  222. * @return the object ids of this commit
  223. */
  224. public ObjectId[] getParentIds() {
  225. return parentIds;
  226. }
  227. /**
  228. * @return the commit message
  229. */
  230. public String getMessage() {
  231. decode();
  232. return message;
  233. }
  234. /**
  235. * Set the parents of this commit
  236. * @param parentIds
  237. */
  238. public void setParentIds(ObjectId[] parentIds) {
  239. this.parentIds = new ObjectId[parentIds.length];
  240. for (int i=0; i<parentIds.length; ++i)
  241. this.parentIds[i] = parentIds[i];
  242. }
  243. private void decode() {
  244. // FIXME: handle I/O errors
  245. if (raw != null) {
  246. try {
  247. DataInputStream br = new DataInputStream(new ByteArrayInputStream(raw));
  248. String n = br.readLine();
  249. if (n == null || !n.startsWith("tree ")) {
  250. throw new CorruptObjectException(commitId, "no tree");
  251. }
  252. while ((n = br.readLine()) != null && n.startsWith("parent ")) {
  253. // empty body
  254. }
  255. if (n == null || !n.startsWith("author ")) {
  256. throw new CorruptObjectException(commitId, "no author");
  257. }
  258. String rawAuthor = n.substring("author ".length());
  259. n = br.readLine();
  260. if (n == null || !n.startsWith("committer ")) {
  261. throw new CorruptObjectException(commitId, "no committer");
  262. }
  263. String rawCommitter = n.substring("committer ".length());
  264. n = br.readLine();
  265. if (n != null && n.startsWith( "encoding"))
  266. encoding = Charset.forName(n.substring("encoding ".length()));
  267. else
  268. if (n == null || !n.equals("")) {
  269. throw new CorruptObjectException(commitId,
  270. "malformed header:"+n);
  271. }
  272. byte[] readBuf = new byte[br.available()]; // in-memory stream so this is all bytes left
  273. br.read(readBuf);
  274. int msgstart = readBuf.length != 0 ? ( readBuf[0] == '\n' ? 1 : 0 ) : 0;
  275. // If encoding is not specified, the default for commit is UTF-8
  276. if (encoding == null) encoding = Constants.CHARSET;
  277. // TODO: this isn't reliable so we need to guess the encoding from the actual content
  278. author = new PersonIdent(new String(rawAuthor.getBytes(),encoding.name()));
  279. committer = new PersonIdent(new String(rawCommitter.getBytes(),encoding.name()));
  280. message = new String(readBuf,msgstart, readBuf.length-msgstart, encoding.name());
  281. } catch (IOException e) {
  282. e.printStackTrace();
  283. } finally {
  284. raw = null;
  285. }
  286. }
  287. }
  288. /**
  289. * Set the commit message
  290. *
  291. * @param m the commit message
  292. */
  293. public void setMessage(final String m) {
  294. message = m;
  295. }
  296. /**
  297. * Persist this commit object
  298. *
  299. * @throws IOException
  300. */
  301. public void commit() throws IOException {
  302. if (getCommitId() != null)
  303. throw new IllegalStateException("exists " + getCommitId());
  304. setCommitId(new ObjectWriter(objdb).writeCommit(this));
  305. }
  306. public String toString() {
  307. return "Commit[" + ObjectId.toString(getCommitId()) + " " + getAuthor() + "]";
  308. }
  309. /**
  310. * State the encoding for the commit information
  311. *
  312. * @param e
  313. * the encoding. See {@link Charset}
  314. */
  315. public void setEncoding(String e) {
  316. encoding = Charset.forName(e);
  317. }
  318. /**
  319. * State the encoding for the commit information
  320. *
  321. * @param e
  322. * the encoding. See {@link Charset}
  323. */
  324. public void setEncoding(Charset e) {
  325. encoding = e;
  326. }
  327. /**
  328. * @return the encoding used. See {@link Charset}
  329. */
  330. public String getEncoding() {
  331. if (encoding != null)
  332. return encoding.name();
  333. else
  334. return null;
  335. }
  336. }