Remove unnecessary interface

This commit is contained in:
René Pfeuffer
2019-03-29 10:36:55 +01:00
parent 1162536e21
commit ee219f2d59
5 changed files with 47 additions and 55 deletions

View File

@@ -15,15 +15,12 @@ public abstract class SimpleWorkdirFactory<R, C> implements WorkdirFactory<R, C>
private final File poolDirectory;
private final CloneProvider<R, C> cloneProvider;
public SimpleWorkdirFactory(CloneProvider<R, C> cloneProvider) {
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"), cloneProvider);
public SimpleWorkdirFactory() {
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"));
}
public SimpleWorkdirFactory(File poolDirectory, CloneProvider<R, C> cloneProvider) {
public SimpleWorkdirFactory(File poolDirectory) {
this.poolDirectory = poolDirectory;
this.cloneProvider = cloneProvider;
if (!poolDirectory.exists() && !poolDirectory.mkdirs()) {
throw new IllegalStateException("could not create pool directory " + poolDirectory);
}
@@ -33,17 +30,19 @@ public abstract class SimpleWorkdirFactory<R, C> implements WorkdirFactory<R, C>
public WorkingCopy<R> createWorkingCopy(C context) {
try {
File directory = createNewWorkdir();
ParentAndClone<R> parentAndClone = cloneProvider.cloneRepository(context, directory);
ParentAndClone<R> parentAndClone = cloneRepository(context, directory);
return new WorkingCopy<>(parentAndClone.getClone(), parentAndClone.getParent(), this::close, directory);
} catch (IOException e) {
throw new InternalRepositoryException(getRepository(context), "could not create temporary directory for clone of repository", e);
throw new InternalRepositoryException(getScmRepository(context), "could not clone repository in temporary directory", e);
}
}
protected abstract Repository getRepository(C context);
protected abstract Repository getScmRepository(C context);
protected abstract void closeRepository(R repository);
protected abstract ParentAndClone<R> cloneRepository(C context, File target) throws IOException;
private File createNewWorkdir() throws IOException {
return Files.createTempDirectory(poolDirectory.toPath(),"workdir").toFile();
}
@@ -56,11 +55,6 @@ public abstract class SimpleWorkdirFactory<R, C> implements WorkdirFactory<R, C>
}
}
@FunctionalInterface
protected interface CloneProvider<R, C> {
ParentAndClone<R> cloneRepository(C context, File target) throws IOException;
}
protected static class ParentAndClone<R> {
private final R parent;
private final R clone;

View File

@@ -13,31 +13,27 @@ import java.io.File;
public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, GitContext> implements GitWorkdirFactory {
public SimpleGitWorkdirFactory() {
super(new GitCloneProvider());
}
public SimpleGitWorkdirFactory(File poolDirectory) {
super(poolDirectory, new GitCloneProvider());
SimpleGitWorkdirFactory(File poolDirectory) {
super(poolDirectory);
}
private static class GitCloneProvider implements CloneProvider<Repository, GitContext> {
@Override
public ParentAndClone<Repository> cloneRepository(GitContext context, File target) {
try {
return new ParentAndClone<>(null, Git.cloneRepository()
.setURI(createScmTransportProtocolUri(context.getDirectory()))
.setDirectory(target)
.call()
.getRepository());
} catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not clone working copy of repository", e);
}
@Override
public ParentAndClone<Repository> cloneRepository(GitContext context, File target) {
try {
return new ParentAndClone<>(null, Git.cloneRepository()
.setURI(createScmTransportProtocolUri(context.getDirectory()))
.setDirectory(target)
.call()
.getRepository());
} catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not clone working copy of repository", e);
}
}
private String createScmTransportProtocolUri(File bareRepository) {
return ScmTransportProtocol.NAME + "://" + bareRepository.getAbsolutePath();
}
private String createScmTransportProtocolUri(File bareRepository) {
return ScmTransportProtocol.NAME + "://" + bareRepository.getAbsolutePath();
}
@Override
@@ -46,7 +42,7 @@ public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, Gi
}
@Override
protected sonia.scm.repository.Repository getRepository(GitContext context) {
protected sonia.scm.repository.Repository getScmRepository(GitContext context) {
return context.getRepository();
}
}

View File

@@ -160,6 +160,10 @@ public class HgCommandContext implements Closeable
return handler.getConfig();
}
public sonia.scm.repository.Repository getScmRepository() {
return scmRepository;
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -15,27 +15,19 @@ import java.util.function.BiConsumer;
public class SimpleHgWorkdirFactory extends SimpleWorkdirFactory<Repository, HgCommandContext> implements HgWorkdirFactory {
private final Provider<HgRepositoryEnvironmentBuilder> hgRepositoryEnvironmentBuilder;
@Inject
public SimpleHgWorkdirFactory(Provider<HgRepositoryEnvironmentBuilder> hgRepositoryEnvironmentBuilder) {
super(new HgCloneProvider(hgRepositoryEnvironmentBuilder));
this.hgRepositoryEnvironmentBuilder = hgRepositoryEnvironmentBuilder;
}
private static class HgCloneProvider implements CloneProvider<Repository, HgCommandContext> {
private final Provider<HgRepositoryEnvironmentBuilder> hgRepositoryEnvironmentBuilder;
private HgCloneProvider(Provider<HgRepositoryEnvironmentBuilder> hgRepositoryEnvironmentBuilder) {
this.hgRepositoryEnvironmentBuilder = hgRepositoryEnvironmentBuilder;
}
@Override
public ParentAndClone<Repository> cloneRepository(HgCommandContext context, File target) throws IOException {
BiConsumer<sonia.scm.repository.Repository, Map<String, String>> repositoryMapBiConsumer =
(repository, environment) -> hgRepositoryEnvironmentBuilder.get().buildFor(repository, null, environment);
Repository centralRepository = context.openWithSpecialEnvironment(repositoryMapBiConsumer);
CloneCommand.on(centralRepository).execute(target.getAbsolutePath());
return new ParentAndClone<>(centralRepository, Repository.open(target));
}
@Override
public ParentAndClone<Repository> cloneRepository(HgCommandContext context, File target) throws IOException {
BiConsumer<sonia.scm.repository.Repository, Map<String, String>> repositoryMapBiConsumer =
(repository, environment) -> hgRepositoryEnvironmentBuilder.get().buildFor(repository, null, environment);
Repository centralRepository = context.openWithSpecialEnvironment(repositoryMapBiConsumer);
CloneCommand.on(centralRepository).execute(target.getAbsolutePath());
return new ParentAndClone<>(centralRepository, Repository.open(target));
}
@Override
@@ -44,8 +36,8 @@ public class SimpleHgWorkdirFactory extends SimpleWorkdirFactory<Repository, HgC
}
@Override
protected sonia.scm.repository.Repository getRepository(HgCommandContext context) {
return null;
protected sonia.scm.repository.Repository getScmRepository(HgCommandContext context) {
return context.getScmRepository();
}
@Override

View File

@@ -1,5 +1,6 @@
package sonia.scm.repository.spi;
import com.aragost.javahg.commands.PullCommand;
import com.google.inject.util.Providers;
import org.assertj.core.api.Assertions;
import org.junit.Test;
@@ -19,7 +20,12 @@ public class HgBranchCommandTest extends AbstractHgCommandTestBase {
HgRepositoryEnvironmentBuilder hgRepositoryEnvironmentBuilder =
new HgRepositoryEnvironmentBuilder(handler, HgTestUtil.createHookManager());
SimpleHgWorkdirFactory workdirFactory = new SimpleHgWorkdirFactory(Providers.of(hgRepositoryEnvironmentBuilder));
SimpleHgWorkdirFactory workdirFactory = new SimpleHgWorkdirFactory(Providers.of(hgRepositoryEnvironmentBuilder)) {
@Override
public void configure(PullCommand pullCommand) {
// we do not want to configure http hooks in this unit test
}
};
BranchRequest branchRequest = new BranchRequest();
branchRequest.setNewBranch("new_branch");