Adapt to stricter Eclipse error checking
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / Commit.java
blobfaf91b1af635401970e2b32e6b5ba94127abffba
1 /*
2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.BufferedReader;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.spearce.jgit.errors.CorruptObjectException;
27 import org.spearce.jgit.errors.MissingObjectException;
29 public class Commit implements Treeish {
30 private final Repository objdb;
32 private ObjectId commitId;
34 private ObjectId treeId;
36 private List parentIds;
38 private PersonIdent author;
40 private PersonIdent committer;
42 private String message;
44 private Tree treeObj;
46 private byte[] raw;
48 public Commit(final Repository db) {
49 objdb = db;
50 parentIds = new ArrayList(2);
53 public Commit(final Repository db, final ObjectId id, final byte[] raw) {
54 objdb = db;
55 commitId = id;
56 treeId = ObjectId.fromString(raw, 5);
57 parentIds = new ArrayList(2);
58 int rawPtr = 46;
59 for (;;) {
60 if (raw[rawPtr] != 'p')
61 break;
62 parentIds.add(ObjectId.fromString(raw, rawPtr + 7));
63 rawPtr += 48;
66 this.raw = raw;
69 public ObjectId getCommitId() {
70 return commitId;
73 public void setCommitId(final ObjectId id) {
74 commitId = id;
77 public ObjectId getTreeId() {
78 return treeId;
81 public void setTreeId(final ObjectId id) {
82 if (!treeId.equals(id)) {
83 treeObj = null;
85 treeId = id;
88 public Tree getTree() throws IOException {
89 if (treeObj == null) {
90 treeObj = objdb.mapTree(getTreeId());
91 if (treeObj == null) {
92 throw new MissingObjectException(getTreeId(),
93 Constants.TYPE_TREE);
96 return treeObj;
99 public void setTree(final Tree t) {
100 treeId = t.getTreeId();
101 treeObj = t;
104 public PersonIdent getAuthor() {
105 decode();
106 return author;
109 public void setAuthor(final PersonIdent a) {
110 author = a;
113 public PersonIdent getCommitter() {
114 decode();
115 return committer;
118 public void setCommitter(final PersonIdent c) {
119 committer = c;
122 public List getParentIds() {
123 return parentIds;
126 public String getMessage() {
127 decode();
128 return message;
131 private void decode() {
132 // FIXME: handle I/O errors
133 if (raw != null) {
134 try {
135 BufferedReader br = new BufferedReader(new InputStreamReader(
136 new ByteArrayInputStream(raw)));
137 String n = br.readLine();
138 if (n == null || !n.startsWith("tree ")) {
139 throw new CorruptObjectException(commitId, "no tree");
141 while ((n = br.readLine()) != null && n.startsWith("parent ")) {
142 // empty body
144 if (n == null || !n.startsWith("author ")) {
145 throw new CorruptObjectException(commitId, "no author");
147 author = new PersonIdent(n.substring("author ".length()));
148 n = br.readLine();
149 if (n == null || !n.startsWith("committer ")) {
150 throw new CorruptObjectException(commitId, "no committer");
152 committer = new PersonIdent(n.substring("committer ".length()));
153 n = br.readLine();
154 if (n == null || !n.equals("")) {
155 throw new CorruptObjectException(commitId,
156 "malformed header");
158 StringBuffer tempMessage = new StringBuffer();
159 char[] readBuf = new char[2048];
160 int readLen;
161 while ((readLen = br.read(readBuf)) > 0) {
162 tempMessage.append(readBuf, 0, readLen);
164 message = tempMessage.toString();
165 } catch (IOException e) {
166 e.printStackTrace();
167 } finally {
168 raw = null;
173 public void setMessage(final String m) {
174 message = m;
177 public void commit() throws IOException {
178 if (getCommitId() != null)
179 throw new IllegalStateException("exists " + getCommitId());
180 setCommitId(new ObjectWriter(objdb).writeCommit(this));
183 public String toString() {
184 return "Commit[" + getCommitId() + " " + getAuthor() + "]";