Browse Source

Refactor Repository.openObject to be Repository.open

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>
stable-0.9
Shawn O. Pearce 15 years ago
parent
commit
acb7be2c5a
  1. 4
      org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
  2. 8
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java
  3. 9
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java
  4. 4
      org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/ConcurrentRepackTest.java
  5. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0004_PackReader.java
  6. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/WindowCacheGetTest.java
  7. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java
  8. 13
      org.eclipse.jgit/src/org/eclipse/jgit/lib/BlobBasedConfig.java
  9. 2
      org.eclipse.jgit/src/org/eclipse/jgit/lib/FileTreeEntry.java
  10. 4
      org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java
  11. 30
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectDatabase.java
  12. 7
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java
  13. 125
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
  14. 6
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java

4
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java

@ -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);
}

8
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());
}
}

9
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);

4
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);

2
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());

2
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());

10
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);

13
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()));
}
}

2
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)

4
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();

30
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.
* <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();
}

7
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.

125
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.
* <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);
}

6
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());
}
}

Loading…
Cancel
Save