diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index daa959f11..a179773d1 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -563,7 +563,7 @@ public class TestRepository { if (o == null) break; - final byte[] bin = db.openObject(o).getCachedBytes(); + final byte[] bin = db.open(o, o.getType()).getCachedBytes(); oc.checkCommit(bin); assertHash(o, bin); } @@ -573,7 +573,7 @@ public class TestRepository { if (o == null) break; - final byte[] bin = db.openObject(o).getCachedBytes(); + final byte[] bin = db.open(o, o.getType()).getCachedBytes(); oc.check(o.getType(), bin); assertHash(o, bin); } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java index a5db45368..e1f78244b 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java @@ -50,12 +50,10 @@ import java.io.PrintStream; import java.util.ArrayList; import java.util.List; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.Option; - import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.MyersDiff; import org.eclipse.jgit.diff.RawText; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler; @@ -63,6 +61,8 @@ import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.AndTreeFilter; import org.eclipse.jgit.treewalk.filter.TreeFilter; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; @Command(common = true, usage = "usage_ShowDiffs") class Diff extends TextBuiltin { @@ -125,7 +125,7 @@ class Diff extends TextBuiltin { private RawText getRawText(ObjectId id) throws IOException { if (id.equals(ObjectId.zeroId())) return new RawText(new byte[] { }); - return new RawText(db.openBlob(id).getCachedBytes()); + return new RawText(db.open(id, Constants.OBJ_BLOB).getCachedBytes()); } } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java index 63d26eaca..c798950a2 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java @@ -49,13 +49,12 @@ package org.eclipse.jgit.pgm; import java.text.MessageFormat; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.Option; -import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.PersonIdent; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; @Command(common = true, usage = "usage_CreateATag") class Tag extends TextBuiltin { @@ -86,9 +85,7 @@ class Tag extends TextBuiltin { , tagName.substring(Constants.R_TAGS.length()))); } - final ObjectLoader ldr = db.openObject(object); - if (ldr == null) - throw new MissingObjectException(object, "any"); + final ObjectLoader ldr = db.open(object); org.eclipse.jgit.lib.Tag tag = new org.eclipse.jgit.lib.Tag(db); tag.setObjId(object); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java index 8e7df41a8..c8f2aad75 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java @@ -158,7 +158,7 @@ public class ConcurrentRepackTest extends RepositoryTestCase { final File[] out1 = pack(eden, o1); assertEquals(o1.name(), parse(o1).name()); - final ObjectLoader load1 = db.openBlob(o1); + final ObjectLoader load1 = db.open(o1, Constants.OBJ_BLOB); assertNotNull(load1); final RevObject o2 = writeBlob(eden, "o2"); @@ -173,7 +173,7 @@ public class ConcurrentRepackTest extends RepositoryTestCase { // earlier still resolve the object, even though its underlying // pack is gone, but the object still exists. // - final ObjectLoader load2 = db.openBlob(o1); + final ObjectLoader load2 = db.open(o1, Constants.OBJ_BLOB); assertNotNull(load2); assertNotSame(load1, load2); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java index 70016f9ea..b8bcca2e1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java @@ -79,7 +79,7 @@ public class T0004_PackReader extends SampleDataRepositoryTestCase { final ObjectLoader or; id = ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"); - or = db.openObject(id); + or = db.open(id); assertNotNull(or); assertTrue(or instanceof PackedObjectLoader); assertEquals(Constants.OBJ_BLOB, or.getType()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java index 89b91558b..177a1d5cf 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java @@ -126,7 +126,7 @@ public class WindowCacheGetTest extends SampleDataRepositoryTestCase { private void doCacheTests() throws IOException { for (final TestObject o : toLoad) { - final ObjectLoader or = db.openObject(o.id); + final ObjectLoader or = db.open(o.id, o.type); assertNotNull(or); assertTrue(or instanceof PackedObjectLoader); assertEquals(o.type, or.getType()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java index cb1ce6f38..0bcdbcea4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java @@ -299,8 +299,8 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { // final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); packHeader(pack, 2); - copy(pack, src.openObject(N)); - copy(pack,src.openObject(s.parseBody(N).getTree())); + copy(pack, src.open(N)); + copy(pack,src.open(s.parseBody(N).getTree())); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); @@ -341,8 +341,8 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { // final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); packHeader(pack, 2); - copy(pack, src.openObject(N)); - copy(pack,src.openObject(s.parseBody(N).getTree())); + copy(pack, src.open(N)); + copy(pack,src.open(s.parseBody(N).getTree())); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); @@ -381,7 +381,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { // final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); packHeader(pack, 1); - copy(pack, src.openObject(N)); + copy(pack, src.open(N)); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java index b05942b02..b56966ff4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java @@ -91,10 +91,8 @@ public class BlobBasedConfig extends Config { public BlobBasedConfig(Config base, final Repository r, final ObjectId objectId) throws IOException, ConfigInvalidException { super(base); - final ObjectLoader loader = r.openBlob(objectId); - if (loader == null) - throw new IOException(MessageFormat.format(JGitText.get().blobNotFound, objectId)); - fromText(RawParseUtils.decode(loader.getBytes())); + ObjectLoader loader = r.open(objectId, Constants.OBJ_BLOB); + fromText(RawParseUtils.decode(loader.getCachedBytes())); } /** @@ -122,10 +120,7 @@ public class BlobBasedConfig extends Config { if (tree == null) throw new FileNotFoundException(MessageFormat.format(JGitText.get().entryNotFoundByPath, path)); final ObjectId blobId = tree.getObjectId(0); - final ObjectLoader loader = tree.getRepository().openBlob(blobId); - if (loader == null) - throw new IOException(MessageFormat.format(JGitText.get().blobNotFoundForPath - , blobId, path)); - fromText(RawParseUtils.decode(loader.getBytes())); + ObjectLoader loader = r.open(blobId,Constants.OBJ_BLOB); + fromText(RawParseUtils.decode(loader.getCachedBytes())); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java index 3da91dd2a..5bb3f62da 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java @@ -93,7 +93,7 @@ public class FileTreeEntry extends TreeEntry { * @throws IOException */ public ObjectLoader openReader() throws IOException { - return getRepository().openBlob(getId()); + return getRepository().open(getId(), Constants.OBJ_BLOB); } public void accept(final TreeVisitor tv, final int flags) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java index 929cd2d2e..0495d38a1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java @@ -433,7 +433,7 @@ public class GitIndex { uid = -1; gid = -1; try { - size = (int) db.openBlob(f.getId()).getSize(); + size = (int) db.open(f.getId(), Constants.OBJ_BLOB).getSize(); } catch (IOException e) { e.printStackTrace(); size = -1; @@ -873,7 +873,7 @@ public class GitIndex { * @throws IOException */ public void checkoutEntry(File wd, Entry e) throws IOException { - ObjectLoader ol = db.openBlob(e.sha1); + ObjectLoader ol = db.open(e.sha1, Constants.OBJ_BLOB); byte[] bytes = ol.getBytes(); File file = new File(wd, e.getName()); file.delete(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java index c2d2beda9..a095663bf 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java @@ -45,6 +45,7 @@ package org.eclipse.jgit.lib; import java.io.IOException; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; /** @@ -143,9 +144,36 @@ public abstract class ObjectDatabase { */ public ObjectLoader openObject(final AnyObjectId objectId) throws IOException { + return openObject(objectId, ObjectReader.OBJ_ANY); + } + + /** + * Open an object from this database. + *

+ * This is a one-shot call interface which may be faster than allocating a + * {@link #newReader()} to perform the lookup. + * + * @param objectId + * identity of the object to open. + * @param typeHint + * hint about the type of object being requested; + * {@link ObjectReader#OBJ_ANY} if the object type is not known, + * or does not matter to the caller. + * @return a {@link ObjectLoader} for accessing the object. + * @throws MissingObjectException + * the object does not exist. + * @throws IncorrectObjectTypeException + * typeHint was not OBJ_ANY, and the object's actual type does + * not match typeHint. + * @throws IOException + * the object store cannot be accessed. + */ + public ObjectLoader openObject(AnyObjectId objectId, int typeHint) + throws MissingObjectException, IncorrectObjectTypeException, + IOException { final ObjectReader or = newReader(); try { - return or.openObject(objectId); + return or.openObject(objectId, typeHint); } finally { or.release(); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java index e8961bdce..001b18c4c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java @@ -86,6 +86,7 @@ public abstract class ObjectReader { * @throws MissingObjectException * the object does not exist. * @throws IOException + * the object store cannot be accessed. */ public ObjectLoader openObject(AnyObjectId objectId) throws MissingObjectException, IOException { @@ -97,7 +98,7 @@ public abstract class ObjectReader { * * @param objectId * identity of the object to open. - *@param typeHint + * @param typeHint * hint about the type of object being requested; * {@link #OBJ_ANY} if the object type is not known, or does not * matter to the caller. @@ -108,9 +109,11 @@ public abstract class ObjectReader { * typeHint was not OBJ_ANY, and the object's actual type does * not match typeHint. * @throws IOException + * the object store cannot be accessed. */ public abstract ObjectLoader openObject(AnyObjectId objectId, int typeHint) - throws MissingObjectException, IncorrectObjectTypeException, IOException; + throws MissingObjectException, IncorrectObjectTypeException, + IOException; /** * Release any resources used by this reader. diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index bab349e6f..a2d9f61e0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -221,41 +221,49 @@ public abstract class Repository { } /** - * @param id - * SHA-1 of an object. + * Open an object from this repository. + *

+ * This is a one-shot call interface which may be faster than allocating a + * {@link #newObjectReader()} to perform the lookup. * - * @return a {@link ObjectLoader} for accessing the data of the named - * object, or null if the object does not exist. - * @throws IOException - */ - public ObjectLoader openObject(final AnyObjectId id) - throws IOException { - try { - return getObjectDatabase().openObject(id); - } catch (MissingObjectException notFound) { - // Legacy API, return null - return null; - } - } - - /** - * @param id - * SHA'1 of a blob - * @return an {@link ObjectLoader} for accessing the data of a named blob + * @param objectId + * identity of the object to open. + * @return a {@link ObjectLoader} for accessing the object. + * @throws MissingObjectException + * the object does not exist. * @throws IOException + * the object store cannot be accessed. */ - public ObjectLoader openBlob(final ObjectId id) throws IOException { - return openObject(id); + public ObjectLoader open(final AnyObjectId objectId) + throws MissingObjectException, IOException { + return getObjectDatabase().openObject(objectId); } /** - * @param id - * SHA'1 of a tree - * @return an {@link ObjectLoader} for accessing the data of a named tree + * Open an object from this repository. + *

+ * This is a one-shot call interface which may be faster than allocating a + * {@link #newObjectReader()} to perform the lookup. + * + * @param objectId + * identity of the object to open. + * @param typeHint + * hint about the type of object being requested; + * {@link ObjectReader#OBJ_ANY} if the object type is not known, + * or does not matter to the caller. + * @return a {@link ObjectLoader} for accessing the object. + * @throws MissingObjectException + * the object does not exist. + * @throws IncorrectObjectTypeException + * typeHint was not OBJ_ANY, and the object's actual type does + * not match typeHint. * @throws IOException + * the object store cannot be accessed. */ - public ObjectLoader openTree(final ObjectId id) throws IOException { - return openObject(id); + public ObjectLoader open(AnyObjectId objectId, int typeHint) + throws MissingObjectException, IncorrectObjectTypeException, + IOException { + return getObjectDatabase().openObject(objectId, typeHint); } /** @@ -284,19 +292,22 @@ public abstract class Repository { * @throws IOException */ public Object mapObject(final ObjectId id, final String refName) throws IOException { - final ObjectLoader or = openObject(id); - if (or == null) + final ObjectLoader or; + try { + or = open(id); + } catch (MissingObjectException notFound) { return null; - final byte[] raw = or.getBytes(); + } + final byte[] raw = or.getCachedBytes(); switch (or.getType()) { case Constants.OBJ_TREE: - return makeTree(id, raw); + return new Tree(this, id, raw); case Constants.OBJ_COMMIT: - return makeCommit(id, raw); + return new Commit(this, id, raw); case Constants.OBJ_TAG: - return makeTag(id, refName, raw); + return new Tag(this, id, refName, raw); case Constants.OBJ_BLOB: return raw; @@ -314,18 +325,13 @@ public abstract class Repository { * @throws IOException for I/O error or unexpected object type. */ public Commit mapCommit(final ObjectId id) throws IOException { - final ObjectLoader or = openObject(id); - if (or == null) + final ObjectLoader or; + try { + or = open(id, Constants.OBJ_COMMIT); + } catch (MissingObjectException notFound) { return null; - final byte[] raw = or.getBytes(); - if (Constants.OBJ_COMMIT == or.getType()) - return new Commit(this, id, raw); - throw new IncorrectObjectTypeException(id, Constants.TYPE_COMMIT); - } - - private Commit makeCommit(final ObjectId id, final byte[] raw) { - Commit ret = new Commit(this, id, raw); - return ret; + } + return new Commit(this, id, or.getCachedBytes()); } /** @@ -351,10 +357,13 @@ public abstract class Repository { * @throws IOException for I/O error or unexpected object type. */ public Tree mapTree(final ObjectId id) throws IOException { - final ObjectLoader or = openObject(id); - if (or == null) + final ObjectLoader or; + try { + or = open(id); + } catch (MissingObjectException notFound) { return null; - final byte[] raw = or.getBytes(); + } + final byte[] raw = or.getCachedBytes(); switch (or.getType()) { case Constants.OBJ_TREE: return new Tree(this, id, raw); @@ -367,16 +376,6 @@ public abstract class Repository { } } - private Tree makeTree(final ObjectId id, final byte[] raw) throws IOException { - Tree ret = new Tree(this, id, raw); - return ret; - } - - private Tag makeTag(final ObjectId id, final String refName, final byte[] raw) { - Tag ret = new Tag(this, id, refName, raw); - return ret; - } - /** * Access a tag by symbolic name. * @@ -397,12 +396,14 @@ public abstract class Repository { * @throws IOException for I/O error or unexpected object type. */ public Tag mapTag(final String refName, final ObjectId id) throws IOException { - final ObjectLoader or = openObject(id); - if (or == null) + final ObjectLoader or; + try { + or = open(id); + } catch (MissingObjectException notFound) { return null; - final byte[] raw = or.getBytes(); - if (Constants.OBJ_TAG == or.getType()) - return new Tag(this, id, refName, raw); + } + if (or.getType() == Constants.OBJ_TAG) + return new Tag(this, id, refName, or.getCachedBytes()); return new Tag(this, id, refName, null); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java index 2aa3098f1..f3b540991 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java @@ -530,10 +530,8 @@ public class Tree extends TreeEntry implements Treeish { private void ensureLoaded() throws IOException, MissingObjectException { if (!isLoaded()) { - final ObjectLoader or = db.openTree(getId()); - if (or == null) - throw new MissingObjectException(getId(), Constants.TYPE_TREE); - readTree(or.getBytes()); + ObjectLoader ldr = db.open(getId(), Constants.OBJ_TREE); + readTree(ldr.getCachedBytes()); } }