mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-01 21:29:47 +01:00
Add method to retrieve branch name from repository
This commit is contained in:
@@ -499,6 +499,33 @@ public final class GitUtil
|
||||
return ref;
|
||||
}
|
||||
|
||||
public static String getRepositoryHeadBranchName(org.eclipse.jgit.lib.Repository repo) {
|
||||
Map<String, Ref> refs = repo.getAllRefs();
|
||||
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())) {
|
||||
return getBranch(e.getValue().getTarget());
|
||||
}
|
||||
} else if (key.startsWith(REF_HEAD_PREFIX)) {
|
||||
if (REF_MASTER.equals(key.substring(REF_HEAD_PREFIX.length()))) {
|
||||
return REF_MASTER;
|
||||
} else {
|
||||
lastHeadRef = e.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lastHeadRef == null) {
|
||||
return null;
|
||||
} else {
|
||||
return getBranch(lastHeadRef);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -648,7 +675,7 @@ public final class GitUtil
|
||||
|
||||
return tagName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -97,9 +97,9 @@ public class AbstractGitCommand
|
||||
return commit;
|
||||
}
|
||||
|
||||
protected String getBranchNameOrDefault(String requestedBranch) {
|
||||
protected String getBranchNameOrDefault(Repository gitRepository, String requestedBranch) {
|
||||
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
||||
return getDefaultBranchName();
|
||||
return getDefaultBranchName(gitRepository);
|
||||
} else {
|
||||
return requestedBranch;
|
||||
}
|
||||
@@ -115,12 +115,12 @@ public class AbstractGitCommand
|
||||
return head;
|
||||
}
|
||||
|
||||
protected String getDefaultBranchName() {
|
||||
protected String getDefaultBranchName(Repository gitRepository) {
|
||||
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
return defaultBranchName;
|
||||
} else {
|
||||
return null;
|
||||
return GitUtil.getRepositoryHeadBranchName(gitRepository);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -208,7 +208,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
}
|
||||
|
||||
ObjectId head = getBranchOrDefault(repository, request.getBranch());
|
||||
String branch = getBranchNameOrDefault(request.getBranch());
|
||||
String branch = getBranchNameOrDefault(repository,request.getBranch());
|
||||
|
||||
if (head != null) {
|
||||
if (startId != null) {
|
||||
|
||||
Reference in New Issue
Block a user