mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
Remove unnecessary HEAD resolving
This commit is contained in:
@@ -66,6 +66,7 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static java.util.Optional.empty;
|
||||||
import static java.util.Optional.of;
|
import static java.util.Optional.of;
|
||||||
import static java.util.Optional.ofNullable;
|
import static java.util.Optional.ofNullable;
|
||||||
|
|
||||||
@@ -507,35 +508,13 @@ public final class GitUtil
|
|||||||
return getRepositoryHeadRef(repo).map(GitUtil::getBranch).orElse(null);
|
return getRepositoryHeadRef(repo).map(GitUtil::getBranch).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ObjectId getRepositoryHead(org.eclipse.jgit.lib.Repository repo)
|
public static ObjectId getRepositoryHead(org.eclipse.jgit.lib.Repository repo) {
|
||||||
throws IOException
|
return getRepositoryHeadRef(repo).map(Ref::getObjectId).orElse(null);
|
||||||
{
|
|
||||||
Optional<Ref> headRef = getRepositoryHeadRef(repo);
|
|
||||||
|
|
||||||
String head = headRef.map(GitUtil::getBranch).orElse(null);
|
|
||||||
ObjectId id = headRef.map(Ref::getObjectId).orElse(repo.resolve(Constants.HEAD));
|
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
|
||||||
{
|
|
||||||
if ((head != null) && (id != null))
|
|
||||||
{
|
|
||||||
logger.debug("use {}:{} as repository head", head, id.name());
|
|
||||||
}
|
|
||||||
else if (id != null)
|
|
||||||
{
|
|
||||||
logger.debug("use {} as repository head", id.name());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.warn("could not find repository head");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<Ref> getRepositoryHeadRef(org.eclipse.jgit.lib.Repository repo) {
|
public static Optional<Ref> getRepositoryHeadRef(org.eclipse.jgit.lib.Repository repo) {
|
||||||
Map<String, Ref> refs = repo.getAllRefs();
|
Map<String, Ref> refs = repo.getAllRefs();
|
||||||
|
Optional<Ref> foundRef = empty();
|
||||||
Ref lastHeadRef = null;
|
Ref lastHeadRef = null;
|
||||||
|
|
||||||
for (Map.Entry<String, Ref> e : refs.entrySet()) {
|
for (Map.Entry<String, Ref> e : refs.entrySet()) {
|
||||||
@@ -543,18 +522,33 @@ public final class GitUtil
|
|||||||
|
|
||||||
if (REF_HEAD.equals(key)) {
|
if (REF_HEAD.equals(key)) {
|
||||||
if (e.getValue().isSymbolic() && isBranch(e.getValue().getTarget().getName())) {
|
if (e.getValue().isSymbolic() && isBranch(e.getValue().getTarget().getName())) {
|
||||||
return of(e.getValue().getTarget());
|
foundRef = of(e.getValue().getTarget());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if (key.startsWith(REF_HEAD_PREFIX)) {
|
} else if (key.startsWith(REF_HEAD_PREFIX) && REF_MASTER.equals(key.substring(REF_HEAD_PREFIX.length()))) {
|
||||||
if (REF_MASTER.equals(key.substring(REF_HEAD_PREFIX.length()))) {
|
foundRef = of(e.getValue());
|
||||||
return of(e.getValue());
|
break;
|
||||||
} else {
|
} else {
|
||||||
lastHeadRef = e.getValue();
|
lastHeadRef = e.getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!foundRef.isPresent()) {
|
||||||
|
foundRef = ofNullable(lastHeadRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ofNullable(lastHeadRef);
|
if (foundRef.isPresent()) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("use {}:{} as repository head for directory {}",
|
||||||
|
foundRef.map(GitUtil::getBranch).orElse(null),
|
||||||
|
foundRef.map(Ref::getObjectId).orElse(null).name(),
|
||||||
|
repo.getDirectory());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn("could not find repository head in directory {}", repo.getDirectory());
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user