TestRepository: Use try-with-resources where appropriate

Change-Id: I06f9534ab84278df37a140700fc2bed5ab667299
This commit is contained in:
Dave Borowitz 2015-03-11 11:39:59 -07:00
parent d1bda470d4
commit 6599111d92
1 changed files with 58 additions and 74 deletions

View File

@ -217,11 +217,9 @@ public RevBlob blob(final String content) throws Exception {
*/ */
public RevBlob blob(final byte[] content) throws Exception { public RevBlob blob(final byte[] content) throws Exception {
ObjectId id; ObjectId id;
try { try (ObjectInserter ins = inserter) {
id = inserter.insert(Constants.OBJ_BLOB, content); id = ins.insert(Constants.OBJ_BLOB, content);
inserter.flush(); ins.flush();
} finally {
inserter.release();
} }
return pool.lookupBlob(id); return pool.lookupBlob(id);
} }
@ -260,11 +258,9 @@ public RevTree tree(final DirCacheEntry... entries) throws Exception {
b.add(e); b.add(e);
b.finish(); b.finish();
ObjectId root; ObjectId root;
try { try (ObjectInserter ins = inserter) {
root = dc.writeTree(inserter); root = dc.writeTree(ins);
inserter.flush(); ins.flush();
} finally {
inserter.release();
} }
return pool.lookupTree(root); return pool.lookupTree(root);
} }
@ -281,18 +277,19 @@ public RevTree tree(final DirCacheEntry... entries) throws Exception {
*/ */
public RevObject get(final RevTree tree, final String path) public RevObject get(final RevTree tree, final String path)
throws Exception { throws Exception {
final TreeWalk tw = new TreeWalk(pool.getObjectReader()); try (TreeWalk tw = new TreeWalk(pool.getObjectReader())) {
tw.setFilter(PathFilterGroup.createFromStrings(Collections tw.setFilter(PathFilterGroup.createFromStrings(Collections
.singleton(path))); .singleton(path)));
tw.reset(tree); tw.reset(tree);
while (tw.next()) { while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) { if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree(); tw.enterSubtree();
continue; continue;
}
final ObjectId entid = tw.getObjectId(0);
final FileMode entmode = tw.getFileMode(0);
return pool.lookupAny(entid, entmode.getObjectType());
} }
final ObjectId entid = tw.getObjectId(0);
final FileMode entmode = tw.getFileMode(0);
return pool.lookupAny(entid, entmode.getObjectType());
} }
fail("Can't find " + path + " in tree " + tree.name()); fail("Can't find " + path + " in tree " + tree.name());
return null; // never reached. return null; // never reached.
@ -377,11 +374,9 @@ public RevCommit commit(final int secDelta, final RevTree tree,
c.setCommitter(new PersonIdent(committer, new Date(now))); c.setCommitter(new PersonIdent(committer, new Date(now)));
c.setMessage(""); c.setMessage("");
ObjectId id; ObjectId id;
try { try (ObjectInserter ins = inserter) {
id = inserter.insert(c); id = ins.insert(c);
inserter.flush(); ins.flush();
} finally {
inserter.release();
} }
return pool.lookupCommit(id); return pool.lookupCommit(id);
} }
@ -414,11 +409,9 @@ public RevTag tag(final String name, final RevObject dst) throws Exception {
t.setTagger(new PersonIdent(committer, new Date(now))); t.setTagger(new PersonIdent(committer, new Date(now)));
t.setMessage(""); t.setMessage("");
ObjectId id; ObjectId id;
try { try (ObjectInserter ins = inserter) {
id = inserter.insert(t); id = ins.insert(t);
inserter.flush(); ins.flush();
} finally {
inserter.release();
} }
return (RevTag) pool.lookupAny(id, Constants.OBJ_TAG); return (RevTag) pool.lookupAny(id, Constants.OBJ_TAG);
} }
@ -581,34 +574,35 @@ public ObjectId lightweightTag(String name, ObjectId obj) throws Exception {
*/ */
public void fsck(RevObject... tips) throws MissingObjectException, public void fsck(RevObject... tips) throws MissingObjectException,
IncorrectObjectTypeException, IOException { IncorrectObjectTypeException, IOException {
ObjectWalk ow = new ObjectWalk(db); try (ObjectWalk ow = new ObjectWalk(db)) {
if (tips.length != 0) { if (tips.length != 0) {
for (RevObject o : tips) for (RevObject o : tips)
ow.markStart(ow.parseAny(o)); ow.markStart(ow.parseAny(o));
} else { } else {
for (Ref r : db.getAllRefs().values()) for (Ref r : db.getAllRefs().values())
ow.markStart(ow.parseAny(r.getObjectId())); ow.markStart(ow.parseAny(r.getObjectId()));
} }
ObjectChecker oc = new ObjectChecker(); ObjectChecker oc = new ObjectChecker();
for (;;) { for (;;) {
final RevCommit o = ow.next(); final RevCommit o = ow.next();
if (o == null) if (o == null)
break; break;
final byte[] bin = db.open(o, o.getType()).getCachedBytes(); final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.checkCommit(bin); oc.checkCommit(bin);
assertHash(o, bin); assertHash(o, bin);
} }
for (;;) { for (;;) {
final RevObject o = ow.nextObject(); final RevObject o = ow.nextObject();
if (o == null) if (o == null)
break; break;
final byte[] bin = db.open(o, o.getType()).getCachedBytes(); final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.check(o.getType(), bin); oc.check(o.getType(), bin);
assertHash(o, bin); assertHash(o, bin);
}
} }
} }
@ -636,35 +630,27 @@ public void packAndPrune() throws Exception {
NullProgressMonitor m = NullProgressMonitor.INSTANCE; NullProgressMonitor m = NullProgressMonitor.INSTANCE;
final File pack, idx; final File pack, idx;
PackWriter pw = new PackWriter(db); try (PackWriter pw = new PackWriter(db)) {
try {
Set<ObjectId> all = new HashSet<ObjectId>(); Set<ObjectId> all = new HashSet<ObjectId>();
for (Ref r : db.getAllRefs().values()) for (Ref r : db.getAllRefs().values())
all.add(r.getObjectId()); all.add(r.getObjectId());
pw.preparePack(m, all, Collections.<ObjectId> emptySet()); pw.preparePack(m, all, Collections.<ObjectId> emptySet());
final ObjectId name = pw.computeName(); final ObjectId name = pw.computeName();
OutputStream out;
pack = nameFor(odb, name, ".pack"); pack = nameFor(odb, name, ".pack");
out = new SafeBufferedOutputStream(new FileOutputStream(pack)); try (OutputStream out =
try { new SafeBufferedOutputStream(new FileOutputStream(pack))) {
pw.writePack(m, m, out); pw.writePack(m, m, out);
} finally {
out.close();
} }
pack.setReadOnly(); pack.setReadOnly();
idx = nameFor(odb, name, ".idx"); idx = nameFor(odb, name, ".idx");
out = new SafeBufferedOutputStream(new FileOutputStream(idx)); try (OutputStream out =
try { new SafeBufferedOutputStream(new FileOutputStream(idx))) {
pw.writeIndex(out); pw.writeIndex(out);
} finally {
out.close();
} }
idx.setReadOnly(); idx.setReadOnly();
} finally {
pw.release();
} }
odb.openPack(pack); odb.openPack(pack);
@ -863,15 +849,13 @@ public RevCommit create() throws Exception {
c.setMessage(message); c.setMessage(message);
ObjectId commitId; ObjectId commitId;
try { try (ObjectInserter ins = inserter) {
if (topLevelTree != null) if (topLevelTree != null)
c.setTreeId(topLevelTree); c.setTreeId(topLevelTree);
else else
c.setTreeId(tree.writeTree(inserter)); c.setTreeId(tree.writeTree(ins));
commitId = inserter.insert(c); commitId = ins.insert(c);
inserter.flush(); ins.flush();
} finally {
inserter.release();
} }
self = pool.lookupCommit(commitId); self = pool.lookupCommit(commitId);