Browse Source

Replace manual peel loops with RevWalk.peel

Instead of peeling things by hand in application level code, defer
the peeling logic into RevWalk's new peel utility method.

Change-Id: Idabd10dc41502e782f6a2eeb56f09566b97775a8
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
stable-0.9
Shawn O. Pearce 15 years ago
parent
commit
47c07e1a0d
  1. 6
      org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java
  2. 35
      org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java
  3. 4
      org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
  4. 11
      org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

6
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java

@ -409,12 +409,8 @@ public class RefDirectory extends RefDatabase {
RevObject obj = rw.parseAny(leaf.getObjectId()); RevObject obj = rw.parseAny(leaf.getObjectId());
ObjectIdRef newLeaf; ObjectIdRef newLeaf;
if (obj instanceof RevTag) { if (obj instanceof RevTag) {
do {
obj = rw.parseAny(((RevTag) obj).getObject());
} while (obj instanceof RevTag);
newLeaf = new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf newLeaf = new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf
.getName(), leaf.getObjectId(), obj.copy()); .getName(), leaf.getObjectId(), rw.peel(obj).copy());
} else { } else {
newLeaf = new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf newLeaf = new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf
.getName(), leaf.getObjectId()); .getName(), leaf.getObjectId());

35
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java

@ -659,11 +659,7 @@ public class RevWalk implements Iterable<RevCommit> {
public RevCommit parseCommit(final AnyObjectId id) public RevCommit parseCommit(final AnyObjectId id)
throws MissingObjectException, IncorrectObjectTypeException, throws MissingObjectException, IncorrectObjectTypeException,
IOException { IOException {
RevObject c = parseAny(id); RevObject c = peel(parseAny(id));
while (c instanceof RevTag) {
c = ((RevTag) c).getObject();
parseHeaders(c);
}
if (!(c instanceof RevCommit)) if (!(c instanceof RevCommit))
throw new IncorrectObjectTypeException(id.toObjectId(), throw new IncorrectObjectTypeException(id.toObjectId(),
Constants.TYPE_COMMIT); Constants.TYPE_COMMIT);
@ -690,11 +686,7 @@ public class RevWalk implements Iterable<RevCommit> {
public RevTree parseTree(final AnyObjectId id) public RevTree parseTree(final AnyObjectId id)
throws MissingObjectException, IncorrectObjectTypeException, throws MissingObjectException, IncorrectObjectTypeException,
IOException { IOException {
RevObject c = parseAny(id); RevObject c = peel(parseAny(id));
while (c instanceof RevTag) {
c = ((RevTag) c).getObject();
parseHeaders(c);
}
final RevTree t; final RevTree t;
if (c instanceof RevCommit) if (c instanceof RevCommit)
@ -802,6 +794,29 @@ public class RevWalk implements Iterable<RevCommit> {
obj.parseBody(this); obj.parseBody(this);
} }
/**
* Peel back annotated tags until a non-tag object is found.
*
* @param obj
* the starting object.
* @return If {@code obj} is not an annotated tag, {@code obj}. Otherwise
* the first non-tag object that {@code obj} references. The
* returned object's headers have been parsed.
* @throws MissingObjectException
* a referenced object cannot be found.
* @throws IOException
* a pack file or loose object could not be read.
*/
public RevObject peel(RevObject obj) throws MissingObjectException,
IOException {
while (obj instanceof RevTag) {
parseHeaders(obj);
obj = ((RevTag) obj).getObject();
}
parseHeaders(obj);
return obj;
}
/** /**
* Create a new flag for application use during walking. * Create a new flag for application use during walking.
* <p> * <p>

4
org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java

@ -84,7 +84,6 @@ import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevFlag; import org.eclipse.jgit.revwalk.RevFlag;
import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand.Result; import org.eclipse.jgit.transport.ReceiveCommand.Result;
@ -818,8 +817,7 @@ public class ReceivePack {
ow.markUninteresting(o); ow.markUninteresting(o);
if (checkReferencedIsReachable && !baseObjects.isEmpty()) { if (checkReferencedIsReachable && !baseObjects.isEmpty()) {
while (o instanceof RevTag) o = ow.peel(o);
o = ((RevTag) o).getObject();
if (o instanceof RevCommit) if (o instanceof RevCommit)
o = ((RevCommit) o).getTree(); o = ((RevCommit) o).getTree();
if (o instanceof RevTree) if (o instanceof RevTree)

11
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

@ -56,6 +56,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.eclipse.jgit.JGitText; import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.PackProtocolException; import org.eclipse.jgit.errors.PackProtocolException;
import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@ -393,11 +394,15 @@ public class UploadPack {
} }
if (!o.has(ADVERTISED)) if (!o.has(ADVERTISED))
throw new PackProtocolException(MessageFormat.format(JGitText.get().notValid, id.name())); throw new PackProtocolException(MessageFormat.format(JGitText.get().notValid, id.name()));
try {
want(o); want(o);
} catch (IOException e) {
throw new PackProtocolException(MessageFormat.format(JGitText.get().notValid, id.name()), e);
}
} }
} }
private void want(RevObject o) { private void want(RevObject o) throws MissingObjectException, IOException {
if (!o.has(WANT)) { if (!o.has(WANT)) {
o.add(WANT); o.add(WANT);
wantAll.add(o); wantAll.add(o);
@ -406,9 +411,7 @@ public class UploadPack {
wantCommits.add((RevCommit) o); wantCommits.add((RevCommit) o);
else if (o instanceof RevTag) { else if (o instanceof RevTag) {
do { o = walk.peel(o);
o = ((RevTag) o).getObject();
} while (o instanceof RevTag);
if (o instanceof RevCommit) if (o instanceof RevCommit)
want(o); want(o);
} }

Loading…
Cancel
Save