Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-07-18 06:00:52 -0400
committerMatthias Sohn2019-07-22 18:21:42 -0400
commit94da2cca5ab3a4ea25cfe269e66129cd32f50286 (patch)
treea9f1e7ba43637bf3be7857bf42967031857517d7
parent667dc0c5175637e32b4b9f41c37006e7f827beeb (diff)
downloadegit-94da2cca5ab3a4ea25cfe269e66129cd32f50286.tar.gz
egit-94da2cca5ab3a4ea25cfe269e66129cd32f50286.tar.xz
egit-94da2cca5ab3a4ea25cfe269e66129cd32f50286.zip
Simplify RepositoryUtils.mapCommitToRef()
Don't re-resolve a known tag ref; just ask it directly for its object id. Also factor out and simplify duplicate code for finding a local or remote branch for a commit id. Bug: 549178 Change-Id: Id8ea90e323c43ecad8ec677a2c2f2b3b6f473ce9 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java62
1 files changed, 25 insertions, 37 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java
index a4382e8b7..15a3964dd 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java
@@ -24,7 +24,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
-import java.util.TreeSet;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
@@ -285,7 +284,11 @@ public class RepositoryUtil {
List<Ref> tags = repository.getRefDatabase().getRefsByPrefix(
Constants.R_TAGS);
for (Ref tagRef : tags) {
- RevObject any = rw.parseAny(repository.resolve(tagRef.getName()));
+ ObjectId id = tagRef.getLeaf().getObjectId();
+ if (id == null) {
+ continue;
+ }
+ RevObject any = rw.parseAny(id);
if (any instanceof RevTag) {
RevTag tag = (RevTag) any;
if (tag.getObject().name().equals(commitId)) {
@@ -327,11 +330,10 @@ public class RepositoryUtil {
}
// if we don't have time stamps, we sort
if (cacheValue == null) {
- String compareString = ""; //$NON-NLS-1$
for (String tagName : tagMap.keySet()) {
- if (tagName.compareTo(compareString) >= 0) {
+ if (cacheValue == null
+ || cacheValue.compareTo(tagName) < 0) {
cacheValue = tagName;
- compareString = tagName;
}
}
}
@@ -339,47 +341,19 @@ public class RepositoryUtil {
if (cacheValue == null) {
// we didnt't find a tag, so let's look for local branches
- Set<String> branchNames = new TreeSet<>();
- // put this into a sorted set
try {
- List<Ref> remoteBranches = repository.getRefDatabase()
- .getRefsByPrefix(Constants.R_HEADS);
- for (Ref branch : remoteBranches) {
- ObjectId objectId = branch.getObjectId();
- if (objectId != null
- && objectId.name().equals(commitId)) {
- branchNames.add(branch.getName());
- }
- }
+ cacheValue = lastRefNameForCommitId(repository,
+ Constants.R_HEADS, commitId);
} catch (IOException e) {
// ignore here
}
- if (!branchNames.isEmpty()) {
- // get the last (sorted) entry
- cacheValue = branchNames.toArray(new String[branchNames
- .size()])[branchNames.size() - 1];
- }
}
if (cacheValue == null) {
// last try: remote branches
- Set<String> branchNames = new TreeSet<>();
- // put this into a sorted set
try {
- List<Ref> remoteBranches = repository.getRefDatabase()
- .getRefsByPrefix(Constants.R_REMOTES);
- for (Ref branch : remoteBranches) {
- ObjectId objectId = branch.getObjectId();
- if (objectId != null
- && objectId.name().equals(commitId)) {
- branchNames.add(branch.getName());
- }
- }
- if (!branchNames.isEmpty()) {
- // get the last (sorted) entry
- cacheValue = branchNames.toArray(new String[branchNames
- .size()])[branchNames.size() - 1];
- }
+ cacheValue = lastRefNameForCommitId(repository,
+ Constants.R_REMOTES, commitId);
} catch (IOException e) {
// ignore here
}
@@ -389,6 +363,20 @@ public class RepositoryUtil {
}
}
+ private String lastRefNameForCommitId(Repository repository,
+ String refPrefix, String commitId) throws IOException {
+ String result = null;
+ for (Ref ref : repository.getRefDatabase().getRefsByPrefix(refPrefix)) {
+ ObjectId objectId = ref.getObjectId();
+ if (objectId != null && objectId.name().equals(commitId)) {
+ if (result == null || result.compareTo(ref.getName()) < 0) {
+ result = ref.getName();
+ }
+ }
+ }
+ return result;
+ }
+
/**
* Return a cached UI "name" for a Repository
* <p>

Back to the top