Fix initialization bug if master set as default branch

Fixes issue https://github.com/scm-manager/scm-manager/issues/1467
where initialization fails when master is set as default branch.
This commit is contained in:
René Pfeuffer
2020-12-07 13:34:10 +01:00
committed by Sebastian Sdorra
parent f1934735fa
commit e62a598aa2
3 changed files with 27 additions and 3 deletions

View File

@@ -113,14 +113,20 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
String branch = StringUtils.isNotBlank(request.getBranch()) ? request.getBranch() : context.getGlobalConfig().getDefaultBranch();
if (StringUtils.isNotBlank(branch)) {
try {
getClone().checkout().setName(branch).setCreateBranch(true).call();
setBranchInConfig(branch);
} catch (GitAPIException e) {
createBranchIfNotThere(branch);
} catch (GitAPIException | IOException e) {
throw new InternalRepositoryException(repository, "could not create default branch for initial commit", e);
}
}
}
private void createBranchIfNotThere(String branch) throws IOException, GitAPIException {
if (!branch.equals(getClone().getRepository().getBranch())) {
getClone().checkout().setName(branch).setCreateBranch(true).call();
setBranchInConfig(branch);
}
}
private void setBranchInConfig(String branch) {
ConfigurationStore<GitRepositoryConfig> store = gitRepositoryConfigStoreProvider
.get(repository);