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 File poolDirectory;
private final CloneProvider<R, C> cloneProvider; public SimpleWorkdirFactory() {
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"));
public SimpleWorkdirFactory(CloneProvider<R, C> cloneProvider) {
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"), cloneProvider);
} }
public SimpleWorkdirFactory(File poolDirectory, CloneProvider<R, C> cloneProvider) { public SimpleWorkdirFactory(File poolDirectory) {
this.poolDirectory = poolDirectory; this.poolDirectory = poolDirectory;
this.cloneProvider = cloneProvider;
if (!poolDirectory.exists() && !poolDirectory.mkdirs()) { if (!poolDirectory.exists() && !poolDirectory.mkdirs()) {
throw new IllegalStateException("could not create pool directory " + poolDirectory); 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) { public WorkingCopy<R> createWorkingCopy(C context) {
try { try {
File directory = createNewWorkdir(); 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); return new WorkingCopy<>(parentAndClone.getClone(), parentAndClone.getParent(), this::close, directory);
} catch (IOException e) { } 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 void closeRepository(R repository);
protected abstract ParentAndClone<R> cloneRepository(C context, File target) throws IOException;
private File createNewWorkdir() throws IOException { private File createNewWorkdir() throws IOException {
return Files.createTempDirectory(poolDirectory.toPath(),"workdir").toFile(); 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> { protected static class ParentAndClone<R> {
private final R parent; private final R parent;
private final R clone; private final R clone;

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
import com.aragost.javahg.commands.PullCommand;
import com.google.inject.util.Providers; import com.google.inject.util.Providers;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
@@ -19,7 +20,12 @@ public class HgBranchCommandTest extends AbstractHgCommandTestBase {
HgRepositoryEnvironmentBuilder hgRepositoryEnvironmentBuilder = HgRepositoryEnvironmentBuilder hgRepositoryEnvironmentBuilder =
new HgRepositoryEnvironmentBuilder(handler, HgTestUtil.createHookManager()); 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 branchRequest = new BranchRequest();
branchRequest.setNewBranch("new_branch"); branchRequest.setNewBranch("new_branch");