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