]> source.dussan.org Git - jgit.git/commitdiff
Throw IncorrectObjectTypeException on bad type hints 88/988/1
authorShawn O. Pearce <spearce@spearce.org>
Mon, 28 Jun 2010 17:37:08 +0000 (10:37 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Mon, 28 Jun 2010 18:47:25 +0000 (11:47 -0700)
If the type hint isn't OBJ_ANY and it doesn't match the actual type
observed from the object store, define the reader to throw back an
IncorrectObjectTypeException.  This way the caller doesn't have to
perform this check itself before it evaluates the object data, and
we can simplify quite a few call sites.

Change-Id: I9f0dfa033857f439c94245361fcae515bc0a6533
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WindowCursor.java

index 29d2eb6f3be7fd4aced751392bca88d7cbd6b965..e8961bdce22a13038bf7ff234df4b757a3ccc479 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;
 import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
 
@@ -103,10 +104,13 @@ public abstract class ObjectReader {
         * @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
         */
        public abstract ObjectLoader openObject(AnyObjectId objectId, int typeHint)
-                       throws MissingObjectException, IOException;
+                       throws MissingObjectException, IncorrectObjectTypeException, IOException;
 
        /**
         * Release any resources used by this reader.
index d5c2fd79dd758cce26c1045e9e3b8482c79e4e91..0272201e22534fda8edc533ef705964b579e2f41 100644 (file)
@@ -48,6 +48,7 @@ import java.io.IOException;
 import java.util.zip.DataFormatException;
 import java.util.zip.Inflater;
 
+import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
 import org.eclipse.jgit.lib.AnyObjectId;
@@ -81,13 +82,16 @@ final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
        }
 
        public ObjectLoader openObject(AnyObjectId objectId, int typeHint)
-                       throws MissingObjectException, IOException {
+                       throws MissingObjectException, IncorrectObjectTypeException,
+                       IOException {
                final ObjectLoader ldr = db.openObject(this, objectId);
                if (ldr == null) {
                        if (typeHint == OBJ_ANY)
                                throw new MissingObjectException(objectId.copy(), "unknown");
                        throw new MissingObjectException(objectId.copy(), typeHint);
                }
+               if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
+                       throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
                return ldr;
        }