mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
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:
2
gradle/changelog/git_initialization.yaml
Normal file
2
gradle/changelog/git_initialization.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
- type: fixed
|
||||||
|
description: Exception in SVN repositories due to incorrect git initialization
|
||||||
@@ -50,15 +50,17 @@ public class GitRepositoryConfigInitializer {
|
|||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void initConfig(PostReceiveRepositoryHookEvent event) {
|
public void initConfig(PostReceiveRepositoryHookEvent event) {
|
||||||
ConfigurationStore<GitRepositoryConfig> store = storeProvider.get(event.getRepository());
|
if (GitRepositoryHandler.TYPE_NAME.equals(event.getRepository().getType())) {
|
||||||
GitRepositoryConfig repositoryConfig = store.get();
|
ConfigurationStore<GitRepositoryConfig> store = storeProvider.get(event.getRepository());
|
||||||
if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) {
|
GitRepositoryConfig repositoryConfig = store.get();
|
||||||
List<String> defaultBranchCandidates = event.getContext().getBranchProvider().getCreatedOrModified();
|
if (repositoryConfig == null || Strings.isNullOrEmpty(repositoryConfig.getDefaultBranch())) {
|
||||||
|
List<String> defaultBranchCandidates = event.getContext().getBranchProvider().getCreatedOrModified();
|
||||||
|
|
||||||
String defaultBranch = determineDefaultBranch(defaultBranchCandidates);
|
String defaultBranch = determineDefaultBranch(defaultBranchCandidates);
|
||||||
|
|
||||||
GitRepositoryConfig gitRepositoryConfig = new GitRepositoryConfig(defaultBranch);
|
GitRepositoryConfig gitRepositoryConfig = new GitRepositoryConfig(defaultBranch);
|
||||||
store.set(gitRepositoryConfig);
|
store.set(gitRepositoryConfig);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,113 +65,130 @@ class GitRepositoryConfigInitializerTest {
|
|||||||
@InjectMocks
|
@InjectMocks
|
||||||
private GitRepositoryConfigInitializer initializer;
|
private GitRepositoryConfigInitializer initializer;
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void initMocks() {
|
|
||||||
when(storeProvider.get(REPOSITORY)).thenReturn(configStore);
|
|
||||||
when(event.getRepository()).thenReturn(REPOSITORY);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDoNothingIfDefaultBranchAlreadySet() {
|
void shouldSkipNonGitRepositories() {
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig("any"));
|
REPOSITORY.setType("svn");
|
||||||
|
|
||||||
initializer.initConfig(event);
|
initializer.initConfig(event);
|
||||||
|
|
||||||
verify(event, never()).getContext();
|
verify(event, never()).getContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void initEvent() {
|
||||||
|
when(event.getRepository()).thenReturn(REPOSITORY);
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class WithGlobalConfig {
|
class ForGitRepositories {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void initRepoHandler() {
|
void initMocks() {
|
||||||
GitConfig gitConfig = new GitConfig();
|
when(storeProvider.get(REPOSITORY)).thenReturn(configStore);
|
||||||
gitConfig.setDefaultBranch("global_default");
|
REPOSITORY.setType("git");
|
||||||
when(repoHandler.getConfig()).thenReturn(gitConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldSetDefaultBranchIfNoGitConfigYet() {
|
void shouldDoNothingIfDefaultBranchAlreadySet() {
|
||||||
when(configStore.get()).thenReturn(null);
|
when(configStore.get()).thenReturn(new GitRepositoryConfig("any"));
|
||||||
initEvent(List.of("main"));
|
|
||||||
|
|
||||||
initializer.initConfig(event);
|
initializer.initConfig(event);
|
||||||
|
|
||||||
verify(event).getContext();
|
verify(event, never()).getContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Nested
|
||||||
void shouldDetermineAndApplyDefaultBranch_GlobalDefault() {
|
class WithGlobalConfig {
|
||||||
initEvent(List.of("global_default", "main", "master"));
|
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
|
||||||
|
|
||||||
initializer.initConfig(event);
|
@BeforeEach
|
||||||
|
void initRepoHandler() {
|
||||||
|
GitConfig gitConfig = new GitConfig();
|
||||||
|
gitConfig.setDefaultBranch("global_default");
|
||||||
|
when(repoHandler.getConfig()).thenReturn(gitConfig);
|
||||||
|
}
|
||||||
|
|
||||||
verify(configStore).set(argThat(arg -> {
|
@Test
|
||||||
assertThat(arg.getDefaultBranch()).isEqualTo("global_default");
|
void shouldSetDefaultBranchIfNoGitConfigYet() {
|
||||||
return true;
|
when(configStore.get()).thenReturn(null);
|
||||||
}));
|
initEvent(List.of("main"));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(event).getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldDetermineAndApplyDefaultBranch_GlobalDefault() {
|
||||||
|
initEvent(List.of("global_default", "main", "master"));
|
||||||
|
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(configStore).set(argThat(arg -> {
|
||||||
|
assertThat(arg.getDefaultBranch()).isEqualTo("global_default");
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldDetermineAndApplyDefaultBranch_Main() {
|
||||||
|
initEvent(List.of("master", "main"));
|
||||||
|
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(configStore).set(argThat(arg -> {
|
||||||
|
assertThat(arg.getDefaultBranch()).isEqualTo("main");
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldDetermineAndApplyDefaultBranch_Master() {
|
||||||
|
initEvent(List.of("develop", "master"));
|
||||||
|
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(configStore).set(argThat(arg -> {
|
||||||
|
assertThat(arg.getDefaultBranch()).isEqualTo("master");
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldDetermineAndApplyDefaultBranch_BestMatch() {
|
||||||
|
initEvent(List.of("test/123", "trillian", "dent"));
|
||||||
|
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(configStore).set(argThat(arg -> {
|
||||||
|
assertThat(arg.getDefaultBranch()).isEqualTo("dent");
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldDetermineAndApplyDefaultBranch_AnyFallback() {
|
||||||
|
initEvent(List.of("test/123", "x/y/z"));
|
||||||
|
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
||||||
|
|
||||||
|
initializer.initConfig(event);
|
||||||
|
|
||||||
|
verify(configStore).set(argThat(arg -> {
|
||||||
|
assertThat(arg.getDefaultBranch()).isEqualTo("test/123");
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private void initEvent(List<String> branches) {
|
||||||
void shouldDetermineAndApplyDefaultBranch_Main() {
|
HookContext hookContext = mock(HookContext.class);
|
||||||
initEvent(List.of("master", "main"));
|
when(event.getContext()).thenReturn(hookContext);
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
HookBranchProvider branchProvider = mock(HookBranchProvider.class);
|
||||||
|
when(hookContext.getBranchProvider()).thenReturn(branchProvider);
|
||||||
initializer.initConfig(event);
|
when(branchProvider.getCreatedOrModified()).thenReturn(branches);
|
||||||
|
|
||||||
verify(configStore).set(argThat(arg -> {
|
|
||||||
assertThat(arg.getDefaultBranch()).isEqualTo("main");
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldDetermineAndApplyDefaultBranch_Master() {
|
|
||||||
initEvent(List.of("develop", "master"));
|
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
|
||||||
|
|
||||||
initializer.initConfig(event);
|
|
||||||
|
|
||||||
verify(configStore).set(argThat(arg -> {
|
|
||||||
assertThat(arg.getDefaultBranch()).isEqualTo("master");
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldDetermineAndApplyDefaultBranch_BestMatch() {
|
|
||||||
initEvent(List.of("test/123", "trillian", "dent"));
|
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
|
||||||
|
|
||||||
initializer.initConfig(event);
|
|
||||||
|
|
||||||
verify(configStore).set(argThat(arg -> {
|
|
||||||
assertThat(arg.getDefaultBranch()).isEqualTo("dent");
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldDetermineAndApplyDefaultBranch_AnyFallback() {
|
|
||||||
initEvent(List.of("test/123", "x/y/z"));
|
|
||||||
when(configStore.get()).thenReturn(new GitRepositoryConfig(null));
|
|
||||||
|
|
||||||
initializer.initConfig(event);
|
|
||||||
|
|
||||||
verify(configStore).set(argThat(arg -> {
|
|
||||||
assertThat(arg.getDefaultBranch()).isEqualTo("test/123");
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initEvent(List<String> branches) {
|
|
||||||
HookContext hookContext = mock(HookContext.class);
|
|
||||||
when(event.getContext()).thenReturn(hookContext);
|
|
||||||
HookBranchProvider branchProvider = mock(HookBranchProvider.class);
|
|
||||||
when(hookContext.getBranchProvider()).thenReturn(branchProvider);
|
|
||||||
when(branchProvider.getCreatedOrModified()).thenReturn(branches);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user