Do not try to init non-git repos for git

The initialization to set the default branch in
git repositories should not be executed for
non-git repositories. Otherwise, this throws
exceptions in SVN repositories all the time
because the branch command is not supported.

Committed-by: Konstantin Schaper <konstantin.schaper@cloudogu.com>
This commit is contained in:
Rene Pfeuffer
2023-09-08 13:22:57 +02:00
parent 2c04b124f0
commit 8352cf349c
3 changed files with 113 additions and 92 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Exception in SVN repositories due to incorrect git initialization

View File

@@ -50,6 +50,7 @@ public class GitRepositoryConfigInitializer {
@Subscribe
public void initConfig(PostReceiveRepositoryHookEvent event) {
if (GitRepositoryHandler.TYPE_NAME.equals(event.getRepository().getType())) {
ConfigurationStore<GitRepositoryConfig> store = storeProvider.get(event.getRepository());
GitRepositoryConfig repositoryConfig = store.get();
if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) {
@@ -61,6 +62,7 @@ public class GitRepositoryConfigInitializer {
store.set(gitRepositoryConfig);
}
}
}
private String determineDefaultBranch(List<String> defaultBranchCandidates) {
String globalConfigDefaultBranch = repoHandler.getConfig().getDefaultBranch();

View File

@@ -65,10 +65,27 @@ class GitRepositoryConfigInitializerTest {
@InjectMocks
private GitRepositoryConfigInitializer initializer;
@Test
void shouldSkipNonGitRepositories() {
REPOSITORY.setType("svn");
initializer.initConfig(event);
verify(event, never()).getContext();
}
@BeforeEach
void initEvent() {
when(event.getRepository()).thenReturn(REPOSITORY);
}
@Nested
class ForGitRepositories {
@BeforeEach
void initMocks() {
when(storeProvider.get(REPOSITORY)).thenReturn(configStore);
when(event.getRepository()).thenReturn(REPOSITORY);
REPOSITORY.setType("git");
}
@Test
@@ -173,5 +190,5 @@ class GitRepositoryConfigInitializerTest {
when(hookContext.getBranchProvider()).thenReturn(branchProvider);
when(branchProvider.getCreatedOrModified()).thenReturn(branches);
}
}
}