From e62a598aa29a5f8a65c4e97aca59823df2db8ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Mon, 7 Dec 2020 13:34:10 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 5 +++++ .../sonia/scm/repository/spi/GitModifyCommand.java | 12 +++++++++--- .../GitModifyCommand_withEmptyRepositoryTest.java | 13 +++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2d91d2de5..3772eab0e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitModifyCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitModifyCommand.java index ba6df0c1e8..355c84e4b5 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitModifyCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitModifyCommand.java @@ -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 store = gitRepositoryConfigStoreProvider .get(repository); diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitModifyCommand_withEmptyRepositoryTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitModifyCommand_withEmptyRepositoryTest.java index 7877f1a686..97ab9a9776 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitModifyCommand_withEmptyRepositoryTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitModifyCommand_withEmptyRepositoryTest.java @@ -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 branches = git.branchList().call(); + assertThat(branches).extracting("name").containsExactly("refs/heads/master"); + } + } + @Test public void shouldCreateCommitWithConfiguredDefaultBranch() throws IOException, GitAPIException { createContext().getGlobalConfig().setDefaultBranch("main");