From: Robin Rosenberg Date: Fri, 07 Nov 2008 22:07:42 GMT Subject: [EGIT PATCH 5/7] Add a method to get refs by object Id Message-ID: <1226095664-13759-6-git-send-email-robin.rosenberg@dewire.com> In-Reply-To: <1226095664-13759-5-git-send-email-robin.rosenberg@dewire.com> Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/lib/Repository.java | 34 ++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java index 4d6e6fd..82a7454 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java @@ -47,10 +47,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Vector; import org.spearce.jgit.errors.IncorrectObjectTypeException; @@ -952,6 +955,37 @@ public Ref peel(final Ref ref) { } /** + * @return a map with all objects referenced by a peeled ref. + */ + public Map> getAllRefsByPeeledObjectId() { + Map allRefs = getAllRefs(); + Map> ret = new HashMap>(allRefs.size()); + for (Ref ref : allRefs.values()) { + if (ref == null) + continue; + if (!ref.isPeeled()) { + ref = peel(ref); + allRefs.put(ref.getOrigName(), ref); + } + AnyObjectId target = ref.getPeeledObjectId(); + if (target == null) + target = ref.getObjectId(); + // We assume most Sets here are singletons + Set oset = ret.put(target, Collections.singleton(ref)); + if (oset != null) { + // that was not the case (rare) + if (oset.size() == 1) { + // Was a read-only singleton, we must copy to a new Set + oset = new HashSet(oset); + } + ret.put(target, oset); + oset.add(ref); + } + } + return ret; + } + + /** * @return true if HEAD points to a StGit patch. */ public boolean isStGitMode() { -- 1.6.0.3.578.g6a50