]> source.dussan.org Git - jgit.git/commitdiff
Throw AmbiguousObjectException during resolve if its ambiguous 03/1403/3
authorShawn O. Pearce <spearce@spearce.org>
Tue, 24 Aug 2010 23:26:40 +0000 (16:26 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 26 Aug 2010 00:07:12 +0000 (17:07 -0700)
Its wrong to return null if we are resolving an abbreviation and we
have proven it matches more than one object.  We know how to resolve
it if we had more nybbles, as there are two or more objects with the
same prefix.  Declare that to the caller quite clearly by giving them
an AmbiguousObjectException.

Change-Id: I01bb48e587e6d001b93da8575c2c81af3eda5a32
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/AbbreviationTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

index 96e36a2a6b2ad1cde3ef51577fe6ad755bff02ee..1a38a7deb9e3bf672d143d5dbea46149d203a709 100644 (file)
@@ -56,6 +56,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
+import org.eclipse.jgit.errors.AmbiguousObjectException;
 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
 import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.lib.AbbreviatedObjectId;
@@ -176,13 +177,25 @@ public class AbbreviationTest extends LocalDiskRepositoryTestCase {
 
                assertEquals(id.abbreviate(20), reader.abbreviate(id, 2));
 
-               Collection<ObjectId> matches = reader.resolve(id.abbreviate(8));
+               AbbreviatedObjectId abbrev8 = id.abbreviate(8);
+               Collection<ObjectId> matches = reader.resolve(abbrev8);
                assertNotNull(matches);
                assertEquals(objects.size(), matches.size());
                for (PackedObjectInfo info : objects)
                        assertTrue("contains " + info.name(), matches.contains(info));
 
-               assertNull("cannot resolve", db.resolve(id.abbreviate(8).name()));
+               try {
+                       db.resolve(abbrev8.name());
+                       fail("did not throw AmbiguousObjectException");
+               } catch (AmbiguousObjectException err) {
+                       assertEquals(abbrev8, err.getAbbreviatedObjectId());
+                       matches = err.getCandidates();
+                       assertNotNull(matches);
+                       assertEquals(objects.size(), matches.size());
+                       for (PackedObjectInfo info : objects)
+                               assertTrue("contains " + info.name(), matches.contains(info));
+               }
+
                assertEquals(id, db.resolve(id.abbreviate(20).name()));
        }
 
index b62f62b347f1985309aae42191e31d1a0ae680d8..078b293f41cb95e8c633ce5b8723f2921eae7167 100644 (file)
@@ -63,6 +63,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.eclipse.jgit.JGitText;
 import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.errors.AmbiguousObjectException;
 import org.eclipse.jgit.errors.CorruptObjectException;
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
@@ -393,10 +394,15 @@ public abstract class Repository {
         * @param revstr
         *            A git object references expression
         * @return an ObjectId or null if revstr can't be resolved to any ObjectId
+        * @throws AmbiguousObjectException
+        *             {@code revstr} contains an abbreviated ObjectId and this
+        *             repository contains more than one object which match to the
+        *             input abbreviation.
         * @throws IOException
         *             on serious errors
         */
-       public ObjectId resolve(final String revstr) throws IOException {
+       public ObjectId resolve(final String revstr)
+                       throws AmbiguousObjectException, IOException {
                RevWalk rw = new RevWalk(this);
                try {
                        return resolve(rw, revstr);
@@ -584,6 +590,8 @@ public abstract class Repository {
                                Collection<ObjectId> matches = reader.resolve(id);
                                if (matches.size() == 1)
                                        return matches.iterator().next();
+                               if (1 < matches.size())
+                                       throw new AmbiguousObjectException(id, matches);
                        } finally {
                                reader.release();
                        }