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

@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Initialization of new git repository with master set as default branch ([#1467](https://github.com/scm-manager/scm-manager/issues/1467) and [#1470](https://github.com/scm-manager/scm-manager/pull/1470))
## [2.11.0] - 2020-12-04
### Added

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

View File

@@ -38,6 +38,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("java:S5976") // using parameterized tests in this class is not useful because we would miss the descriptions
public class GitModifyCommand_withEmptyRepositoryTest extends GitModifyCommandTestBase {
@Test
@@ -70,6 +71,18 @@ public class GitModifyCommand_withEmptyRepositoryTest extends GitModifyCommandTe
}
}
@Test
public void shouldCreateCommitOnMasterIfSetExplicitly() throws IOException, GitAPIException {
createContext().getGlobalConfig().setDefaultBranch("master");
executeModifyCommand();
try (Git git = new Git(createContext().open())) {
List<Ref> branches = git.branchList().call();
assertThat(branches).extracting("name").containsExactly("refs/heads/master");
}
}
@Test
public void shouldCreateCommitWithConfiguredDefaultBranch() throws IOException, GitAPIException {
createContext().getGlobalConfig().setDefaultBranch("main");