Fix double /trunk folder bug

With this change, every repository initializer gets its own modification command and therefore its own commit. This allows each initializer to decide on its own, if he want to use the default path of the repository.
This commit is contained in:
Thomas Zerr
2024-04-30 15:28:08 +02:00
parent cdbf86b8c9
commit 6fbde760d4
6 changed files with 59 additions and 10 deletions

View File

@@ -0,0 +1,2 @@
- type: changed
description: Each repository initializer step is now executed with his own commit

View File

@@ -59,6 +59,14 @@ public interface RepositoryContentInitializer {
*/
CreateFile create(String path);
/**
* create new file which will be included in initial repository commit.
*
* @param path full path of the file to be created
* @param useDefaultPath Wether the default path of the repository should be prefixed to the specified path. For example "/trunk", if it is a SVN repository.
*/
CreateFile createWithDefaultPath(String path, boolean useDefaultPath);
/**
* Returns the context entry with the given key and unmarshalls it to the given type.
* It no entry with the given key is available an empty optional is returned.

View File

@@ -157,6 +157,10 @@ public class ModifyCommandBuilder {
}
}
public boolean isEmpty() {
return request.isEmpty();
}
/**
* Set the commit message for the new commit.
* @return This builder instance.

View File

@@ -65,6 +65,10 @@ public class ModifyCommandRequest implements Resetable, Validateable, CommandWit
this.requests.add(request);
}
public boolean isEmpty() {
return requests.isEmpty();
}
public void setAuthor(Person author) {
this.author = author;
}

View File

@@ -60,18 +60,19 @@ public class RepositoryInitializer {
public void initialize(Repository repository, Map<String, JsonNode> contextEntries) {
try (RepositoryService service = serviceFactory.create(repository)) {
ModifyCommandBuilder modifyCommandBuilder = service.getModifyCommand();
InitializerContextImpl initializerContext = new InitializerContextImpl(repository, modifyCommandBuilder, contextEntries);
for (RepositoryContentInitializer initializer : contentInitializers) {
ModifyCommandBuilder modifyCommandBuilder = service.getModifyCommand();
InitializerContextImpl initializerContext = new InitializerContextImpl(repository, modifyCommandBuilder, contextEntries);
initializer.initialize(initializerContext);
if (!modifyCommandBuilder.isEmpty()) {
modifyCommandBuilder.setCommitMessage("initialize repository");
String revision = modifyCommandBuilder.execute();
LOG.info("initialized repository {} as revision {}", repository, revision);
}
}
modifyCommandBuilder.setCommitMessage("initialize repository");
String revision = modifyCommandBuilder.execute();
LOG.info("initialized repository {} as revision {}", repository, revision);
} catch (IOException e) {
throw new InternalRepositoryException(repository, "failed to initialize repository", e);
}
@@ -109,6 +110,11 @@ public class RepositoryInitializer {
public RepositoryContentInitializer.CreateFile create(String path) {
return new CreateFileImpl(this, builder.useDefaultPath(true).createFile(path).setOverwrite(true));
}
@Override
public RepositoryContentInitializer.CreateFile createWithDefaultPath(String path, boolean useDefaultPath) {
return new CreateFileImpl(this, builder.useDefaultPath(useDefaultPath).createFile(path).setOverwrite(true));
}
}
private static class CreateFileImpl implements RepositoryContentInitializer.CreateFile {

View File

@@ -79,6 +79,23 @@ class RepositoryInitializerTest {
when(repositoryService.getModifyCommand()).thenReturn(modifyCommand);
}
@Test
void shouldNotCommitIfInitializerDidNotMakeAnyChanges() {
when(modifyCommand.isEmpty()).thenReturn(true);
Set<RepositoryContentInitializer> repositoryContentInitializers = ImmutableSet.of(
new NoOpInitializer()
);
RepositoryInitializer initializer = new RepositoryInitializer(repositoryServiceFactory, repositoryContentInitializers);
initializer.initialize(repository, Collections.emptyMap());
verify(modifyCommand, never()).setCommitMessage("initialize repository");
verify(modifyCommand, never()).execute();
verify(repositoryService).close();
}
@Test
void shouldCallRepositoryContentInitializer() throws IOException {
ModifyCommandBuilder.WithOverwriteFlagContentLoader readmeContentLoader = mockContentLoader("README.md");
@@ -95,8 +112,8 @@ class RepositoryInitializerTest {
verifyFileCreation(readmeContentLoader, "# HeartOfGold");
verifyFileCreation(licenseContentLoader, "MIT");
verify(modifyCommand).setCommitMessage("initialize repository");
verify(modifyCommand).execute();
verify(modifyCommand, times(2)).setCommitMessage("initialize repository");
verify(modifyCommand, times(2)).execute();
verify(repositoryService).close();
}
@@ -257,6 +274,14 @@ class RepositoryInitializerTest {
}
}
@Priority(3)
private static class NoOpInitializer implements RepositoryContentInitializer {
@Override
public void initialize(InitializerContext context) {
}
}
private static class StreamingContentInitializer implements RepositoryContentInitializer {
@Override