Merge topic 'testrepo'

* changes:
  TestRepository: Allow custom author/committer per-commit
  TestRepository: Use try-with-resources where appropriate
This commit is contained in:
Shawn Pearce 2015-03-12 13:11:20 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit c3b8615ce8
1 changed files with 90 additions and 83 deletions

View File

@ -106,9 +106,9 @@
* type of Repository the test data is stored on.
*/
public class TestRepository<R extends Repository> {
private static final PersonIdent author;
private static final PersonIdent defaultAuthor;
private static final PersonIdent committer;
private static final PersonIdent defaultCommitter;
static {
final MockSystemReader m = new MockSystemReader();
@ -117,11 +117,11 @@ public class TestRepository<R extends Repository> {
final String an = "J. Author";
final String ae = "jauthor@example.com";
author = new PersonIdent(an, ae, now, tz);
defaultAuthor = new PersonIdent(an, ae, now, tz);
final String cn = "J. Committer";
final String ce = "jcommitter@example.com";
committer = new PersonIdent(cn, ce, now, tz);
defaultCommitter = new PersonIdent(cn, ce, now, tz);
}
private final R db;
@ -191,8 +191,8 @@ public void tick(final int secDelta) {
* the commit builder to store.
*/
public void setAuthorAndCommitter(org.eclipse.jgit.lib.CommitBuilder c) {
c.setAuthor(new PersonIdent(author, new Date(now)));
c.setCommitter(new PersonIdent(committer, new Date(now)));
c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
}
/**
@ -217,11 +217,9 @@ public RevBlob blob(final String content) throws Exception {
*/
public RevBlob blob(final byte[] content) throws Exception {
ObjectId id;
try {
id = inserter.insert(Constants.OBJ_BLOB, content);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(Constants.OBJ_BLOB, content);
ins.flush();
}
return pool.lookupBlob(id);
}
@ -260,11 +258,9 @@ public RevTree tree(final DirCacheEntry... entries) throws Exception {
b.add(e);
b.finish();
ObjectId root;
try {
root = dc.writeTree(inserter);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
root = dc.writeTree(ins);
ins.flush();
}
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)
throws Exception {
final TreeWalk tw = new TreeWalk(pool.getObjectReader());
tw.setFilter(PathFilterGroup.createFromStrings(Collections
.singleton(path)));
tw.reset(tree);
while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree();
continue;
try (TreeWalk tw = new TreeWalk(pool.getObjectReader())) {
tw.setFilter(PathFilterGroup.createFromStrings(Collections
.singleton(path)));
tw.reset(tree);
while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree();
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());
return null; // never reached.
@ -373,15 +370,13 @@ public RevCommit commit(final int secDelta, final RevTree tree,
c = new org.eclipse.jgit.lib.CommitBuilder();
c.setTreeId(tree);
c.setParentIds(parents);
c.setAuthor(new PersonIdent(author, new Date(now)));
c.setCommitter(new PersonIdent(committer, new Date(now)));
c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
c.setMessage("");
ObjectId id;
try {
id = inserter.insert(c);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(c);
ins.flush();
}
return pool.lookupCommit(id);
}
@ -411,14 +406,12 @@ public RevTag tag(final String name, final RevObject dst) throws Exception {
final TagBuilder t = new TagBuilder();
t.setObjectId(dst);
t.setTag(name);
t.setTagger(new PersonIdent(committer, new Date(now)));
t.setTagger(new PersonIdent(defaultCommitter, new Date(now)));
t.setMessage("");
ObjectId id;
try {
id = inserter.insert(t);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(t);
ins.flush();
}
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,
IncorrectObjectTypeException, IOException {
ObjectWalk ow = new ObjectWalk(db);
if (tips.length != 0) {
for (RevObject o : tips)
ow.markStart(ow.parseAny(o));
} else {
for (Ref r : db.getAllRefs().values())
ow.markStart(ow.parseAny(r.getObjectId()));
}
try (ObjectWalk ow = new ObjectWalk(db)) {
if (tips.length != 0) {
for (RevObject o : tips)
ow.markStart(ow.parseAny(o));
} else {
for (Ref r : db.getAllRefs().values())
ow.markStart(ow.parseAny(r.getObjectId()));
}
ObjectChecker oc = new ObjectChecker();
for (;;) {
final RevCommit o = ow.next();
if (o == null)
break;
ObjectChecker oc = new ObjectChecker();
for (;;) {
final RevCommit o = ow.next();
if (o == null)
break;
final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.checkCommit(bin);
assertHash(o, bin);
}
final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.checkCommit(bin);
assertHash(o, bin);
}
for (;;) {
final RevObject o = ow.nextObject();
if (o == null)
break;
for (;;) {
final RevObject o = ow.nextObject();
if (o == null)
break;
final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.check(o.getType(), bin);
assertHash(o, bin);
final byte[] bin = db.open(o, o.getType()).getCachedBytes();
oc.check(o.getType(), bin);
assertHash(o, bin);
}
}
}
@ -636,35 +630,27 @@ public void packAndPrune() throws Exception {
NullProgressMonitor m = NullProgressMonitor.INSTANCE;
final File pack, idx;
PackWriter pw = new PackWriter(db);
try {
try (PackWriter pw = new PackWriter(db)) {
Set<ObjectId> all = new HashSet<ObjectId>();
for (Ref r : db.getAllRefs().values())
all.add(r.getObjectId());
pw.preparePack(m, all, Collections.<ObjectId> emptySet());
final ObjectId name = pw.computeName();
OutputStream out;
pack = nameFor(odb, name, ".pack");
out = new SafeBufferedOutputStream(new FileOutputStream(pack));
try {
try (OutputStream out =
new SafeBufferedOutputStream(new FileOutputStream(pack))) {
pw.writePack(m, m, out);
} finally {
out.close();
}
pack.setReadOnly();
idx = nameFor(odb, name, ".idx");
out = new SafeBufferedOutputStream(new FileOutputStream(idx));
try {
try (OutputStream out =
new SafeBufferedOutputStream(new FileOutputStream(idx))) {
pw.writeIndex(out);
} finally {
out.close();
}
idx.setReadOnly();
} finally {
pw.release();
}
odb.openPack(pack);
@ -760,6 +746,9 @@ public class CommitBuilder {
private RevCommit self;
private PersonIdent author;
private PersonIdent committer;
CommitBuilder() {
branch = null;
}
@ -851,6 +840,22 @@ public CommitBuilder tick(int secs) {
return this;
}
public CommitBuilder ident(PersonIdent ident) {
author = ident;
committer = ident;
return this;
}
public CommitBuilder author(PersonIdent a) {
author = a;
return this;
}
public CommitBuilder committer(PersonIdent c) {
committer = c;
return this;
}
public RevCommit create() throws Exception {
if (self == null) {
TestRepository.this.tick(tick);
@ -860,18 +865,20 @@ public RevCommit create() throws Exception {
c = new org.eclipse.jgit.lib.CommitBuilder();
c.setParentIds(parents);
setAuthorAndCommitter(c);
if (author != null)
c.setAuthor(author);
if (committer != null)
c.setCommitter(committer);
c.setMessage(message);
ObjectId commitId;
try {
try (ObjectInserter ins = inserter) {
if (topLevelTree != null)
c.setTreeId(topLevelTree);
else
c.setTreeId(tree.writeTree(inserter));
commitId = inserter.insert(c);
inserter.flush();
} finally {
inserter.release();
c.setTreeId(tree.writeTree(ins));
commitId = ins.insert(c);
ins.flush();
}
self = pool.lookupCommit(commitId);