Remove code duplication

This commit is contained in:
René Pfeuffer
2018-10-02 13:44:22 +02:00
parent 5a1f15b8c8
commit de6b94fa4c

View File

@@ -102,12 +102,22 @@ public class AbstractGitCommand
return commit;
}
protected ObjectId getDefaultBranch(Repository gitRepository) throws IOException {
Ref ref = getBranchOrDefault(gitRepository, null);
if (ref == null) {
return null;
} else {
return ref.getObjectId();
}
}
protected Ref getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
if ( Strings.isNullOrEmpty(requestedBranch) ) {
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
if (!Strings.isNullOrEmpty(defaultBranchName)) {
return GitUtil.getBranchId(gitRepository, defaultBranchName);
} else {
logger.trace("no default branch configured, use repository head as default");
Optional<Ref> repositoryHeadRef = GitUtil.getRepositoryHeadRef(gitRepository);
return repositoryHeadRef.orElse(null);
}
@@ -116,19 +126,6 @@ public class AbstractGitCommand
}
}
protected ObjectId getDefaultBranch(Repository gitRepository) throws IOException {
ObjectId head;
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
if (!Strings.isNullOrEmpty(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);
}
return head;
}
//~--- fields ---------------------------------------------------------------
/** Field description */