Initial set of files.
[egit/egit-new.git] / src / org / spearce / jgit / lib / ObjectDatabase.java
blob41677b82e65e60c0caccf0b0fa24077fec306388
1 package org.spearce.jgit.lib;
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.io.InputStream;
11 public class ObjectDatabase {
12 private static final String[] refSearchPaths = { "", "refs/", "refs/tags/",
13 "refs/heads/", };
15 private final File db;
17 private final File objectsDir;
19 public ObjectDatabase(final File d) {
20 db = d.getAbsoluteFile();
21 objectsDir = new File(db, "objects");
24 private InputStream openObjectStream(final ObjectId objectId)
25 throws IOException {
26 try {
27 final String n = objectId.toString();
28 return new FileInputStream(new File(new File(objectsDir, n
29 .substring(0, 2)), n.substring(2)));
30 } catch (FileNotFoundException fnfe) {
31 return null;
35 public ObjectReader openBlob(final ObjectId id) throws IOException {
36 final InputStream fis = openObjectStream(id);
37 if (fis == null) {
38 return null;
41 try {
42 final ObjectReader or = new ObjectReader(id, fis);
43 if ("blob".equals(or.getType())) {
44 return or;
45 } else {
46 throw new CorruptObjectException("Not a blob " + id);
48 } catch (IOException ioe) {
49 fis.close();
50 throw ioe;
54 public Commit openCommit(final ObjectId id) throws IOException {
55 final InputStream fis = openObjectStream(id);
56 if (fis == null) {
57 return null;
60 try {
61 final ObjectReader or = new ObjectReader(id, fis);
62 try {
63 if ("commit".equals(or.getType())) {
64 return new Commit(this, id, or.getBufferedReader());
65 } else {
66 throw new CorruptObjectException("Not a commit: " + id);
68 } finally {
69 or.close();
71 } catch (IOException ioe) {
72 fis.close();
73 throw ioe;
77 public Tree openTree(final ObjectId id) throws IOException {
78 final InputStream fis = openObjectStream(id);
79 if (fis == null) {
80 return null;
83 try {
84 final ObjectReader or = new ObjectReader(id, fis);
85 try {
86 if ("commit".equals(or.getType())) {
87 return openTree(new Commit(this, id, or.getBufferedReader())
88 .getTreeId());
89 } else if ("tree".equals(or.getType())) {
90 return new Tree(this, id, or.getInputStream());
91 } else {
92 throw new CorruptObjectException("Not a tree-ish: " + id);
94 } finally {
95 or.close();
97 } catch (IOException ioe) {
98 fis.close();
99 throw ioe;
103 private ObjectId readRef(final String name) throws IOException {
104 final File f = new File(db, name);
105 if (!f.isFile()) {
106 return null;
108 final BufferedReader fr = new BufferedReader(new FileReader(f));
109 try {
110 final String line = fr.readLine();
111 if (line == null || line.length() == 0) {
112 return null;
114 if (line.startsWith("ref: ")) {
115 return readRef(line.substring("ref: ".length()));
117 if (ObjectId.isId(line)) {
118 return new ObjectId(line);
120 throw new IOException("Not a ref: " + name + ": " + line);
121 } finally {
122 fr.close();
126 public ObjectId resolveRevision(final String r) throws IOException {
127 ObjectId id = null;
129 if (ObjectId.isId(r)) {
130 id = new ObjectId(r);
132 if (id == null) {
133 for (int k = 0; k < refSearchPaths.length; k++) {
134 id = readRef(refSearchPaths[k] + r);
135 if (id != null) {
136 break;
140 return id;
143 public String toString() {
144 return "ObjectDatabase[" + db + "]";