From 179db9c479fb48b721f623f34d89c47b402e88f6 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 24 Dec 2008 18:10:59 -0800 Subject: [PATCH] Allow TreeWalk callers to pass a MutableObjectId to get the current id This avoids a memory allocation associated with getting the entry object for each name. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/treewalk/TreeWalk.java | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java index 26544b52..9121683d 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java @@ -46,6 +46,7 @@ import org.spearce.jgit.errors.MissingObjectException; import org.spearce.jgit.errors.StopWalkException; import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.FileMode; +import org.spearce.jgit.lib.MutableObjectId; import org.spearce.jgit.lib.ObjectId; import org.spearce.jgit.lib.Repository; import org.spearce.jgit.revwalk.RevTree; @@ -513,7 +514,8 @@ public class TreeWalk { *

* Using this method to compare ObjectId values between trees of this walker * is very inefficient. Applications should try to use - * {@link #idEqual(int, int)} whenever possible. + * {@link #idEqual(int, int)} or {@link #getObjectId(MutableObjectId, int)} + * whenever possible. *

* Every tree supplies an object id, even if the tree does not contain the * current entry. In the latter case {@link ObjectId#zeroId()} is returned. @@ -521,6 +523,7 @@ public class TreeWalk { * @param nth * tree to obtain the object identifier from. * @return object identifier for the current tree entry. + * @see #getObjectId(MutableObjectId, int) * @see #idEqual(int, int) */ public ObjectId getObjectId(final int nth) { @@ -530,6 +533,29 @@ public class TreeWalk { } /** + * Obtain the ObjectId for the current entry. + *

+ * Every tree supplies an object id, even if the tree does not contain the + * current entry. In the latter case {@link ObjectId#zeroId()} is supplied. + *

+ * Applications should try to use {@link #idEqual(int, int)} when possible + * as it avoids conversion overheads. + * + * @param out + * buffer to copy the object id into. + * @param nth + * tree to obtain the object identifier from. + * @see #idEqual(int, int) + */ + public void getObjectId(final MutableObjectId out, final int nth) { + final AbstractTreeIterator t = trees[nth]; + if (t.matches == currentHead) + out.fromRaw(t.idBuffer(), t.idOffset()); + else + out.clear(); + } + + /** * Compare two tree's current ObjectId values for equality. * * @param nthA -- 2.11.4.GIT