Initial set of files.
[egit/egit-new.git] / src / org / spearce / jgit / lib / Commit.java
blob0544bc81bfbbd5719bd54fd74aacac7050ddd688
1 package org.spearce.jgit.lib;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.List;
9 public class Commit implements Treeish {
10 private final ObjectDatabase objdb;
12 private final ObjectId commitId;
14 private final ObjectId treeId;
16 private final List parentIds;
18 private final String author;
20 private final String committer;
22 private final String message;
24 private Tree treeObj;
26 public Commit(final ObjectDatabase db, final ObjectId id,
27 final BufferedReader br) throws IOException {
28 objdb = db;
29 commitId = id;
31 final ArrayList tempParents;
32 final StringBuffer tempMessage;
33 final char[] readBuf;
34 int readLen;
35 String line;
37 line = br.readLine();
38 if (line == null || !line.startsWith("tree ")) {
39 throw new CorruptObjectException("No tree found in commit " + id);
41 treeId = new ObjectId(line.substring("tree ".length()));
43 tempParents = new ArrayList(2);
44 for (;;) {
45 line = br.readLine();
46 if (line == null) {
47 throw new CorruptObjectException("Commit header corrupt " + id);
49 if (line.startsWith("parent ")) {
50 tempParents
51 .add(new ObjectId(line.substring("parent ".length())));
52 } else {
53 break;
56 parentIds = Collections.unmodifiableList(tempParents);
58 if (line == null || !line.startsWith("author ")) {
59 throw new CorruptObjectException("No author found in commit " + id);
61 author = line.substring("author ".length());
63 line = br.readLine();
64 if (line == null || !line.startsWith("committer ")) {
65 throw new CorruptObjectException("No committer found in commit "
66 + id);
68 committer = line.substring("committer ".length());
70 line = br.readLine();
71 if (line == null || !line.equals("")) {
72 throw new CorruptObjectException(
73 "No blank line after header in commit " + id);
76 tempMessage = new StringBuffer();
77 readBuf = new char[128];
78 while ((readLen = br.read(readBuf)) > 0) {
79 tempMessage.append(readBuf, 0, readLen);
81 message = tempMessage.toString();
84 public ObjectId getCommitId() {
85 return commitId;
88 public ObjectId getTreeId() {
89 return treeId;
92 public List getTreeEntries() throws IOException {
93 if (treeObj == null) {
94 treeObj = objdb.openTree(getTreeId());
96 return treeObj.getTreeEntries();
99 public String getAuthor() {
100 return author;
103 public String getCommitter() {
104 return committer;
107 public List getParentIds() {
108 return parentIds;
111 public String getMessage() {
112 return message;