mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 13:35:44 +01:00
Fix usage of default branch
This commit is contained in:
@@ -350,12 +350,11 @@ public final class GitUtil
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static ObjectId getBranchId(org.eclipse.jgit.lib.Repository repo,
|
||||
public static Ref getBranchId(org.eclipse.jgit.lib.Repository repo,
|
||||
String branchName)
|
||||
throws IOException
|
||||
{
|
||||
ObjectId branchId = null;
|
||||
|
||||
Ref ref = null;
|
||||
if (!branchName.startsWith(REF_HEAD))
|
||||
{
|
||||
branchName = PREFIX_HEADS.concat(branchName);
|
||||
@@ -365,24 +364,19 @@ public final class GitUtil
|
||||
|
||||
try
|
||||
{
|
||||
Ref ref = repo.findRef(branchName);
|
||||
ref = repo.findRef(branchName);
|
||||
|
||||
if (ref != null)
|
||||
{
|
||||
branchId = ref.getObjectId();
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
if (ref == null)
|
||||
{
|
||||
logger.warn("could not find branch for {}", branchName);
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.warn("error occured during resolve of branch id", ex);
|
||||
}
|
||||
|
||||
return branchId;
|
||||
return ref;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -528,7 +522,7 @@ public final class GitUtil
|
||||
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);
|
||||
return of(refHead.getTarget());
|
||||
}
|
||||
|
||||
Ref master = refs.get(REF_HEAD_PREFIX + REF_MASTER);
|
||||
|
||||
@@ -102,12 +102,17 @@ public class AbstractGitCommand
|
||||
return commit;
|
||||
}
|
||||
|
||||
protected BranchWithId getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
|
||||
protected Ref getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
|
||||
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
||||
Optional<Ref> repositoryHeadRef = GitUtil.getRepositoryHeadRef(gitRepository);
|
||||
return repositoryHeadRef.map(r -> new BranchWithId(GitUtil.getBranch(r), r.getObjectId())).orElse(null);
|
||||
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
return GitUtil.getBranchId(gitRepository, defaultBranchName);
|
||||
} else {
|
||||
Optional<Ref> repositoryHeadRef = GitUtil.getRepositoryHeadRef(gitRepository);
|
||||
return repositoryHeadRef.orElse(null);
|
||||
}
|
||||
} else {
|
||||
return new BranchWithId(requestedBranch, GitUtil.getBranchId(gitRepository, requestedBranch));
|
||||
return GitUtil.getBranchId(gitRepository, requestedBranch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +120,8 @@ public class AbstractGitCommand
|
||||
ObjectId head;
|
||||
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
head = GitUtil.getBranchId(gitRepository, defaultBranchName);
|
||||
Ref ref = GitUtil.getBranchId(gitRepository, defaultBranchName);
|
||||
head = ref == null? null: ref.getObjectId();
|
||||
} else {
|
||||
logger.trace("no default branch configured, use repository head as default");
|
||||
head = GitUtil.getRepositoryHead(gitRepository);
|
||||
@@ -130,10 +136,4 @@ public class AbstractGitCommand
|
||||
|
||||
/** Field description */
|
||||
protected sonia.scm.repository.Repository repository;
|
||||
|
||||
@Getter @AllArgsConstructor
|
||||
static class BranchWithId {
|
||||
private final String name;
|
||||
private final ObjectId objectId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.eclipse.jgit.errors.MissingObjectException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
@@ -207,7 +208,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
|
||||
}
|
||||
|
||||
BranchWithId branch = getBranchOrDefault(repository,request.getBranch());
|
||||
Ref branch = getBranchOrDefault(repository,request.getBranch());
|
||||
|
||||
if (branch != null) {
|
||||
if (startId != null) {
|
||||
@@ -235,7 +236,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
}
|
||||
|
||||
if (branch != null) {
|
||||
changesets = new ChangesetPagingResult(counter, changesetList, branch.getName());
|
||||
changesets = new ChangesetPagingResult(counter, changesetList, GitUtil.getBranch(branch.getName()));
|
||||
} else {
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
|
||||
@@ -83,11 +83,11 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
|
||||
result = createCommand().getChangesets(new LogCommandRequest());
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("test-branch", result.getBranchName());
|
||||
assertEquals(3, result.getTotal());
|
||||
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getChangesets().get(0).getId());
|
||||
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(1).getId());
|
||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(2).getId());
|
||||
assertEquals("test-branch", result.getBranchName());
|
||||
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user