]> source.dussan.org Git - jgit.git/commitdiff
Refactor Repository.openObject to be Repository.open 92/992/1
authorShawn O. Pearce <spearce@spearce.org>
Mon, 28 Jun 2010 18:54:58 +0000 (11:54 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Mon, 28 Jun 2010 18:54:58 +0000 (11:54 -0700)
We drop the "Object" suffix, because its pretty clear here that
we want to open an object, given that we pass in AnyObjectId as
the main parameter.  We also fix the calling convention to throw
a MissingObjectException or IncorrectObjectTypeException, so that
callers don't have to do this error checking themselves.

Change-Id: I72c43353cea8372278b032f5086d52082c1eee39
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 files changed:
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java

index daa959f116b98d04112a561f0e46cd93d1291ab2..a179773d1511a1cfaa0e0ddd63a4d98eb90610b6 100644 (file)
@@ -563,7 +563,7 @@ public class TestRepository<R extends Repository> {
                        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<R extends Repository> {
                        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);
                }
index a5db453688e26981170ac4025fd2cdeb16eeb9dc..e1f78244b172a22a561e68568792322d10147ae3 100644 (file)
@@ -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());
        }
 }
 
index 63d26eacae61f1271e78e93b3ab98e19346eb1a8..c798950a2e8693d82ef8c9f5fd8c73a4c79054a6 100644 (file)
@@ -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);
index 8e7df41a8d31ea0f35cedfc5dfeada4d071b0ce1..c8f2aad7599be7ab584ca2aab89b3fcbe9d4ff48 100644 (file)
@@ -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);
 
index 70016f9ea11c8d5ea98a7689cfc55f03cc0aa879..b8bcca2e14eb2f31ca67fe303050dc4d5726576b 100644 (file)
@@ -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());
index 89b91558bace748ac3d4ed1763cc24bdcd6e3717..177a1d5cf7182a993a17ab33e4c9e2f607424a63 100644 (file)
@@ -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());
index cb1ce6f38217fc103850d2c7edb2552b8b2319f6..0bcdbcea418fa8e6bc8097c9140656ba6534ec1f 100644 (file)
@@ -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);
index b05942b02c51884f5de8055c6a38c9b67ff68039..b56966ff42fc01d7e13a5e20ce3f8b11d1ea06da 100644 (file)
@@ -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()));
        }
 }
index 3da91dd2a2d89ab0455d0af7b8478802478c7bc8..5bb3f62da34af3af4200a579f220f7a7447fbb66 100644 (file)
@@ -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)
index 929cd2d2eee9fc49395fb2b732006f055b3eceb5..0495d38a110ef823cb45a0a6211cf4ee863f1e1b 100644 (file)
@@ -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();
index c2d2beda9ae472bc80190126e6d950a907617777..a095663bf4edd41b7555156ee88767dbfaf121bc 100644 (file)
@@ -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.
+        * <p>
+        * 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();
                }
index e8961bdce22a13038bf7ff234df4b757a3ccc479..001b18c4c834984313830ce08ed89516d28f062c 100644 (file)
@@ -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.
index bab349e6fb71f55c5f3a2bbe1736bdc342f83ae6..a2d9f61e0a0399a2c92372f71e2d5a794ba3bb10 100644 (file)
@@ -221,41 +221,49 @@ public abstract class Repository {
        }
 
        /**
-        * @param id
-        *            SHA-1 of an object.
+        * Open an object from this repository.
+        * <p>
+        * 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.
+        * <p>
+        * 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);
        }
 
index 2aa3098f17ca7e163ad400d74d9bbb64c3aa8ee9..f3b5409917a18a4ef4deac6da46f8cadcd17221a 100644 (file)
@@ -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());
                }
        }