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
;
48 public Commit(final Repository db
) {
50 parentIds
= new ArrayList(2);
53 public Commit(final Repository db
, final ObjectId id
, final byte[] raw
) {
56 treeId
= ObjectId
.fromString(raw
, 5);
57 parentIds
= new ArrayList(2);
60 if (raw
[rawPtr
] != 'p')
62 parentIds
.add(ObjectId
.fromString(raw
, rawPtr
+ 7));
69 public ObjectId
getCommitId() {
73 public void setCommitId(final ObjectId id
) {
77 public ObjectId
getTreeId() {
81 public void setTreeId(final ObjectId id
) {
82 if (!treeId
.equals(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(),
99 public void setTree(final Tree t
) {
100 treeId
= t
.getTreeId();
104 public PersonIdent
getAuthor() {
109 public void setAuthor(final PersonIdent a
) {
113 public PersonIdent
getCommitter() {
118 public void setCommitter(final PersonIdent c
) {
122 public List
getParentIds() {
126 public String
getMessage() {
131 private void decode() {
132 // FIXME: handle I/O errors
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 ")) {
144 if (n
== null || !n
.startsWith("author ")) {
145 throw new CorruptObjectException(commitId
, "no author");
147 author
= new PersonIdent(n
.substring("author ".length()));
149 if (n
== null || !n
.startsWith("committer ")) {
150 throw new CorruptObjectException(commitId
, "no committer");
152 committer
= new PersonIdent(n
.substring("committer ".length()));
154 if (n
== null || !n
.equals("")) {
155 throw new CorruptObjectException(commitId
,
158 StringBuffer tempMessage
= new StringBuffer();
159 char[] readBuf
= new char[2048];
161 while ((readLen
= br
.read(readBuf
)) > 0) {
162 tempMessage
.append(readBuf
, 0, readLen
);
164 message
= tempMessage
.toString();
165 } catch (IOException e
) {
173 public void setMessage(final String 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() + "]";