mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 05:55:44 +01:00
Cleanup computation of default branch
This commit is contained in:
@@ -39,6 +39,8 @@ import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.eclipse.jgit.api.FetchCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
@@ -66,9 +68,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
import static java.util.Optional.ofNullable;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -504,40 +504,12 @@ public final class GitUtil
|
||||
return ref;
|
||||
}
|
||||
|
||||
public static String getRepositoryHeadBranchName(org.eclipse.jgit.lib.Repository repo) {
|
||||
return getRepositoryHeadRef(repo).map(GitUtil::getBranch).orElse(null);
|
||||
}
|
||||
|
||||
public static ObjectId getRepositoryHead(org.eclipse.jgit.lib.Repository repo) {
|
||||
return getRepositoryHeadRef(repo).map(Ref::getObjectId).orElse(null);
|
||||
}
|
||||
|
||||
public static Optional<Ref> getRepositoryHeadRef(org.eclipse.jgit.lib.Repository repo) {
|
||||
Map<String, Ref> refs = repo.getAllRefs();
|
||||
Optional<Ref> foundRef = empty();
|
||||
Ref lastHeadRef = null;
|
||||
|
||||
for (Map.Entry<String, Ref> e : refs.entrySet()) {
|
||||
String key = e.getKey();
|
||||
|
||||
if (REF_HEAD.equals(key)) {
|
||||
if (e.getValue().isSymbolic() && isBranch(e.getValue().getTarget().getName())) {
|
||||
foundRef = of(e.getValue().getTarget());
|
||||
break;
|
||||
}
|
||||
} else if (key.startsWith(REF_HEAD_PREFIX)) {
|
||||
if (REF_MASTER.equals(key.substring(REF_HEAD_PREFIX.length()))) {
|
||||
foundRef = of(e.getValue());
|
||||
break;
|
||||
} else {
|
||||
lastHeadRef = e.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundRef.isPresent()) {
|
||||
foundRef = ofNullable(lastHeadRef);
|
||||
}
|
||||
Optional<Ref> foundRef = findMostAppropriateHead(repo.getAllRefs());
|
||||
|
||||
if (foundRef.isPresent()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -553,6 +525,29 @@ public final class GitUtil
|
||||
return foundRef;
|
||||
}
|
||||
|
||||
private static Optional<Ref> findMostAppropriateHead(Map<String, Ref> refs) {
|
||||
Ref refHead = refs.get(REF_HEAD);
|
||||
if (refHead != null && refHead.isSymbolic() && isBranch(refHead.getTarget().getName())) {
|
||||
return of(refHead);
|
||||
}
|
||||
|
||||
Ref master = refs.get(REF_HEAD_PREFIX + REF_MASTER);
|
||||
if (master != null) {
|
||||
return of(master);
|
||||
}
|
||||
|
||||
Ref defaultBranch = refs.get(REF_HEAD_PREFIX + "default");
|
||||
if (defaultBranch != null) {
|
||||
return of(defaultBranch);
|
||||
}
|
||||
|
||||
return refs.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e.getKey().startsWith(REF_HEAD_PREFIX))
|
||||
.map(Map.Entry::getValue)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -35,11 +35,16 @@ package sonia.scm.repository.spi;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -97,30 +102,12 @@ public class AbstractGitCommand
|
||||
return commit;
|
||||
}
|
||||
|
||||
protected String getBranchNameOrDefault(Repository gitRepository, String requestedBranch) {
|
||||
protected BranchWithId getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
|
||||
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
||||
return getDefaultBranchName(gitRepository);
|
||||
Optional<Ref> repositoryHeadRef = GitUtil.getRepositoryHeadRef(gitRepository);
|
||||
return repositoryHeadRef.map(r -> new BranchWithId(GitUtil.getBranch(r), r.getObjectId())).orElse(null);
|
||||
} else {
|
||||
return requestedBranch;
|
||||
}
|
||||
}
|
||||
|
||||
protected ObjectId getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
|
||||
ObjectId head;
|
||||
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
||||
head = getDefaultBranch(gitRepository);
|
||||
} else {
|
||||
head = GitUtil.getBranchId(gitRepository, requestedBranch);
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
protected String getDefaultBranchName(Repository gitRepository) {
|
||||
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
return defaultBranchName;
|
||||
} else {
|
||||
return GitUtil.getRepositoryHeadBranchName(gitRepository);
|
||||
return new BranchWithId(requestedBranch, GitUtil.getBranchId(gitRepository, requestedBranch));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,4 +130,10 @@ public class AbstractGitCommand
|
||||
|
||||
/** Field description */
|
||||
protected sonia.scm.repository.Repository repository;
|
||||
|
||||
@Getter @AllArgsConstructor
|
||||
static class BranchWithId {
|
||||
private final String name;
|
||||
private final ObjectId objectId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,14 +207,13 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
|
||||
}
|
||||
|
||||
ObjectId head = getBranchOrDefault(repository, request.getBranch());
|
||||
String branch = getBranchNameOrDefault(repository,request.getBranch());
|
||||
BranchWithId branch = getBranchOrDefault(repository,request.getBranch());
|
||||
|
||||
if (head != null) {
|
||||
if (branch != null) {
|
||||
if (startId != null) {
|
||||
revWalk.markStart(revWalk.lookupCommit(startId));
|
||||
} else {
|
||||
revWalk.markStart(revWalk.lookupCommit(head));
|
||||
revWalk.markStart(revWalk.lookupCommit(branch.getObjectId()));
|
||||
}
|
||||
|
||||
Iterator<RevCommit> iterator = revWalk.iterator();
|
||||
@@ -236,7 +235,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
}
|
||||
|
||||
if (branch != null) {
|
||||
changesets = new ChangesetPagingResult(counter, changesetList, branch);
|
||||
changesets = new ChangesetPagingResult(counter, changesetList, branch.getName());
|
||||
} else {
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user