Add method to retrieve branch name from repository

This commit is contained in:
René Pfeuffer
2018-09-28 10:14:04 +02:00
parent 487ca6bd6d
commit e35ec519cb
4 changed files with 66 additions and 12 deletions

View File

@@ -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
*

View File

@@ -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);
}
}

View File

@@ -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) {