mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-01 19:15:52 +01:00
Document SimpleGitWorkingCopyFactory
This commit is contained in:
@@ -28,12 +28,14 @@ import com.google.common.base.Stopwatch;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
||||
|
||||
@@ -49,7 +51,7 @@ public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, W, C> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> workingCopyContext) {
|
||||
public <R, W, C extends Supplier<Repository>> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> workingCopyContext) {
|
||||
String id = workingCopyContext.getScmRepository().getId();
|
||||
File existingWorkdir = workdirs.remove(id);
|
||||
if (existingWorkdir != null) {
|
||||
|
||||
@@ -24,10 +24,12 @@
|
||||
|
||||
package sonia.scm.repository.work;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
||||
|
||||
@@ -39,7 +41,7 @@ public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, W, C> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException {
|
||||
public <R, W, C extends Supplier<Repository>> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException {
|
||||
return context.getInitializer().initialize(workdirProvider.createNewWorkdir());
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,166 @@ import sonia.scm.repository.Repository;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import java.io.File;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class SimpleWorkingCopyFactory<R, W, C> implements WorkingCopyFactory<R, W, C>, ServletContextListener {
|
||||
/**
|
||||
* This class is responsible to govern the creation, the reuse and the destruction
|
||||
* of working copies. For every repository type there has to be an implementation
|
||||
* of this class to provide the repository specific logic to create, initialize,
|
||||
* reclaim and clean up working copies. To do this, the following methods have to be
|
||||
* implemented:
|
||||
*
|
||||
* <dl>
|
||||
* <dt>{@link #cloneRepository(C, File, String)}</dt>
|
||||
* <dd>Creates a new clone of the repository for the given context in the given
|
||||
* directory with the given branch checked out (if branches are supported).</dd>
|
||||
* <dt>{@link #reclaimRepository(C, File, String)}</dt>
|
||||
* <dd>Reclaim the working directory with a already checked out clone of the
|
||||
* repository given in the context, so that the directory is not modified in
|
||||
* respect to the repository and the given branch is checked out (if branches
|
||||
* are supported).</dd>
|
||||
* <dt>{@link #closeWorkingCopy(W)}</dt>
|
||||
* <dd>Closes resources allocated for the working copy, so that the directory can
|
||||
* be put to the cache. Will be called at the end of the operation.</dd>
|
||||
* <dt>{@link #closeRepository(R)}</dt>
|
||||
* <dd>Closes resources allocated for the central repository.</dd>
|
||||
* </dl>
|
||||
* <pre>
|
||||
* ┌─────────────┐ ┌───────────────────────────┐ ┌───────────────┐ ┌───────────────┐
|
||||
* │ModifyCommand│ │SimpleGitWorkingCopyFactory│ │WorkingCopyPool│ │WorkdirProvider│
|
||||
* └──────┬──────┘ └─────────────┬─────────────┘ └───────┬───────┘ └───────┬───────┘
|
||||
* │ createWorkingCopy │ │ │
|
||||
* │──────────────────────────────>│ │ │
|
||||
* │ │ │ │
|
||||
* │ ────┐ │ │
|
||||
* │ │ createContext │ │
|
||||
* │ <───┘ │ │
|
||||
* │ │ │ │
|
||||
* │ │ create ┌─────────┐ │ │
|
||||
* │ │──────────────────────> │Reclaimer│ │ │
|
||||
* │ │ └────┬────┘ │ │
|
||||
* │ │ create │ ┌───────────┐ │ │
|
||||
* │ │───────────────────────────────────────────> │Initializer│ │ │
|
||||
* │ │ │ └─────┬─────┘ │ │
|
||||
* │ │ │getWorkingCopy │ │ │
|
||||
* │ │───────────────────────────────────────────────────────────────────────────>│ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ reclaim │ │
|
||||
* │ │ │<─────────────────────────────────────────────│ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ reclaim │ │ │ │
|
||||
* │ │<────────────────────────────│ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ ╔══════╤════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╗
|
||||
* │ ║ ALT │ reclaim successful │ │ │ │ ║
|
||||
* │ ╟──────┘ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────>│ │ ║
|
||||
* │ ╠═══════════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╣
|
||||
* │ ║ [reclaim fails; create new] │ │ │ │ ║
|
||||
* │ ║ │ ReclaimFailedException │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────X│ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ createNewWorkdir │ ║
|
||||
* │ ║ │ │ │ │─────────────────────────>│ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │<─────────────────────────│ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ initialize │ │ ║
|
||||
* │ ║ │ │ │<───────────────────────│ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ initialize │ │ │ ║
|
||||
* │ ║ │<──────────────────────────────────────────────────│ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────>│ │ ║
|
||||
* │ ╚═══════════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╝
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │<───────────────────────────────────────────────────────────────────────────│ │ │
|
||||
* │ │ │ │ │ │ │
|
||||
* │ │ │ │ │ │ ┌───────────┐
|
||||
* │ │────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────> │WorkingCopy│
|
||||
* │ │ │ │ │ │ └─────┬─────┘
|
||||
* │ WorkingCopy │ │ │ │ │ │
|
||||
* │<──────────────────────────────│ │ │ │ │ │
|
||||
* │ │ │ │ │ │ │
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* │ │ │ doWork │ │ │ │
|
||||
* │───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────>│
|
||||
* │ │ │ │ │ │ │
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* . . . . . . .
|
||||
* │ │ │ close │ │ │ │
|
||||
* │───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────>│
|
||||
* │ │ │ │ │ │ │
|
||||
* │ │ │ │ close │ │ │
|
||||
* │ │<───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│
|
||||
* │ │ │ │ │ │ │
|
||||
* │ ────┐ │ │ │ │ │
|
||||
* │ │ closeWorkingCopy │ │ │ │ │
|
||||
* │ <───┘ │ │ │ │ │
|
||||
* │ │ │ │ │ │ │
|
||||
* │ ────┐ │ │ │ │ │
|
||||
* │ │ closeRepository │ │ │ │ │
|
||||
* │ <───┘ │ │ │ │ │
|
||||
* │ │ │ │ │ │ │
|
||||
* │ │ │ contextClosed │ │ │ │
|
||||
* │ │───────────────────────────────────────────────────────────────────────────>│ │ │
|
||||
* ┌──────┴──────┐ ┌─────────────┴─────────────┐ ┌────┴────┐ ┌─────┴─────┐ ┌───────┴───────┐ ┌───────┴───────┐ ┌─────┴─────┐
|
||||
* │ModifyCommand│ │SimpleGitWorkingCopyFactory│ │Reclaimer│ │Initializer│ │WorkingCopyPool│ │WorkdirProvider│ │WorkingCopy│
|
||||
* └─────────────┘ └───────────────────────────┘ └─────────┘ └───────────┘ └───────────────┘ └───────────────┘ └───────────┘
|
||||
* </pre>
|
||||
* <img src="http://www.plantuml.com/plantuml/png/fLH1QiCm4Bph5JeRaWUeO0AcAJsqXBJGiv0sOQcaDPBSn7rzQw8OLG4beJThTsOyE_9w5QBqSR26lP36tTYHjTBfHVk6jZVmXF63_2UwVKVzk9OgaX-vuCg3Z92rM0LV5HCdgOvSX6Eio_0jA2FHWc-QnBj2U7OOKHhylfcIrJP4HkmXXx6zfw7dxtxaaRW_sM45Pz7EFiKPCbUgOSnmQU9PdOT1AGXXDvYwyvONFXYO0BFCJg893-dck8D3NUrOgeaqmlfN1_JZKK4VaTpby5D1ezzu-a8DFgN-2eVKO0LWqPQXNsfczoXmVcPPkPfhnhaxGwSlxTJUTVFdrJHNlFpPI94xOylbibpOh2qqJQThA6KeG3vSDSC4ho1ke6VrVofjeQU0q_c-VLQd7qqtQMYs0gS7Qtu0"/>
|
||||
* @param <R> Type of central repository location
|
||||
* @param <W> Type of working copy for repository
|
||||
* @param <C> Type of repository context
|
||||
*/
|
||||
/*
|
||||
http://www.plantuml.com/plantuml/uml/fLH1QiCm4Bph5JeRaWUeO0AcAJsqXBJGiv0sOQcaDPBSn7rzQw8OLG4beJThTsOyE_9w5QBqSR26lP36tTYHjTBfHVk6jZVmXF63_2UwVKVzk9OgaX-vuCg3Z92rM0LV5HCdgOvSX6Eio_0jA2FHWc-QnBj2U7OOKHhylfcIrJP4HkmXXx6zfw7dxtxaaRW_sM45Pz7EFiKPCbUgOSnmQU9PdOT1AGXXDvYwyvONFXYO0BFCJg893-dck8D3NUrOgeaqmlfN1_JZKK4VaTpby5D1ezzu-a8DFgN-2eVKO0LWqPQXNsfczoXmVcPPkPfhnhaxGwSlxTJUTVFdrJHNlFpPI94xOylbibpOh2qqJQThA6KeG3vSDSC4ho1ke6VrVofjeQU0q_c-VLQd7qqtQMYs0gS7Qtu0
|
||||
@startuml
|
||||
ModifyCommand->SimpleGitWorkingCopyFactory : createWorkingCopy
|
||||
SimpleGitWorkingCopyFactory->SimpleGitWorkingCopyFactory:createContext
|
||||
SimpleGitWorkingCopyFactory-> Reclaimer**:create
|
||||
SimpleGitWorkingCopyFactory-> Initializer**:create
|
||||
SimpleGitWorkingCopyFactory->WorkingCopyPool:getWorkingCopy
|
||||
group Try to reclaim
|
||||
WorkingCopyPool->Reclaimer:reclaim
|
||||
Reclaimer->SimpleGitWorkingCopyFactory:reclaim
|
||||
alt reclaim successful
|
||||
SimpleGitWorkingCopyFactory->> WorkingCopyPool
|
||||
else reclaim fails; create new
|
||||
SimpleGitWorkingCopyFactory->x WorkingCopyPool:ReclaimFailedException
|
||||
WorkingCopyPool->WorkdirProvider:createNewWorkdir
|
||||
WorkdirProvider->>WorkingCopyPool
|
||||
WorkingCopyPool->Initializer:initialize
|
||||
Initializer->SimpleGitWorkingCopyFactory:initialize
|
||||
SimpleGitWorkingCopyFactory->> WorkingCopyPool
|
||||
end
|
||||
WorkingCopyPool->>SimpleGitWorkingCopyFactory
|
||||
SimpleGitWorkingCopyFactory->WorkingCopy**
|
||||
SimpleGitWorkingCopyFactory->>ModifyCommand: WorkingCopy
|
||||
...
|
||||
ModifyCommand->WorkingCopy:doWork
|
||||
...
|
||||
ModifyCommand->WorkingCopy:close
|
||||
WorkingCopy->SimpleGitWorkingCopyFactory:close
|
||||
SimpleGitWorkingCopyFactory->SimpleGitWorkingCopyFactory:closeWorkingCopy
|
||||
SimpleGitWorkingCopyFactory->SimpleGitWorkingCopyFactory:closeRepository
|
||||
SimpleGitWorkingCopyFactory->WorkingCopyPool:contextClosed
|
||||
@enduml
|
||||
*/
|
||||
public abstract class SimpleWorkingCopyFactory<R, W, C extends Supplier<Repository>> implements WorkingCopyFactory<R, W, C>, ServletContextListener {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SimpleWorkingCopyFactory.class);
|
||||
|
||||
@@ -50,13 +208,12 @@ public abstract class SimpleWorkingCopyFactory<R, W, C> implements WorkingCopyFa
|
||||
WorkingCopyPool.ParentAndClone<R, W> parentAndClone = workingCopyPool.getWorkingCopy(workingCopyContext);
|
||||
return new WorkingCopy<>(parentAndClone.getClone(), parentAndClone.getParent(), () -> this.close(workingCopyContext, parentAndClone), parentAndClone.getDirectory());
|
||||
} catch (WorkingCopyFailedException e) {
|
||||
throw new InternalRepositoryException(getScmRepository(repositoryContext), "could not create working copy for repository in temporary directory", e);
|
||||
throw new InternalRepositoryException(repositoryContext.get(), "could not create working copy for repository in temporary directory", e);
|
||||
}
|
||||
}
|
||||
|
||||
private WorkingCopyContext<R, W, C> createWorkingCopyContext(C repositoryContext, String initialBranch) {
|
||||
return new WorkingCopyContext<>(
|
||||
getScmRepository(repositoryContext),
|
||||
initialBranch,
|
||||
repositoryContext,
|
||||
newFolder -> cloneRepository(repositoryContext, newFolder, initialBranch),
|
||||
@@ -65,16 +222,16 @@ public abstract class SimpleWorkingCopyFactory<R, W, C> implements WorkingCopyFa
|
||||
}
|
||||
|
||||
private void close(WorkingCopyContext<R, W, C> workingCopyContext, WorkingCopyPool.ParentAndClone<R, W> parentAndClone) {
|
||||
try {
|
||||
closeWorkingCopy(parentAndClone.getClone());
|
||||
} catch (Exception e) {
|
||||
LOG.warn("could not close clone for {} in directory {}", workingCopyContext.getScmRepository(), parentAndClone.getDirectory(), e);
|
||||
}
|
||||
try {
|
||||
closeRepository(parentAndClone.getParent());
|
||||
} catch (Exception e) {
|
||||
LOG.warn("could not close central repository for {}", workingCopyContext.getScmRepository(), e);
|
||||
}
|
||||
try {
|
||||
closeWorkingCopyInternal(parentAndClone.getClone());
|
||||
} catch (Exception e) {
|
||||
LOG.warn("could not close clone for {} in directory {}", workingCopyContext.getScmRepository(), parentAndClone.getDirectory(), e);
|
||||
}
|
||||
try {
|
||||
workingCopyPool.contextClosed(workingCopyContext, parentAndClone.getDirectory());
|
||||
} catch (Exception e) {
|
||||
@@ -102,18 +259,17 @@ public abstract class SimpleWorkingCopyFactory<R, W, C> implements WorkingCopyFa
|
||||
WorkingCopyPool.ParentAndClone<R, W> reclaim(File target) throws ReclaimFailedException;
|
||||
}
|
||||
|
||||
protected abstract Repository getScmRepository(C context);
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> cloneRepository(C context, File target, String initialBranch) throws WorkingCopyFailedException;
|
||||
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> reclaimRepository(C context, File target, String initialBranch) throws ReclaimFailedException;
|
||||
|
||||
@SuppressWarnings("squid:S00112")
|
||||
// We do allow implementations to throw arbitrary exceptions here, so that we can handle them in closeCentral
|
||||
protected abstract void closeRepository(R repository) throws Exception;
|
||||
|
||||
@SuppressWarnings("squid:S00112")
|
||||
// We do allow implementations to throw arbitrary exceptions here, so that we can handle them in closeWorkingCopy
|
||||
protected abstract void closeWorkingCopyInternal(W workingCopy) throws Exception;
|
||||
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> cloneRepository(C context, File target, String initialBranch) throws WorkingCopyFailedException;
|
||||
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> reclaimRepository(C context, File target, String initialBranch) throws ReclaimFailedException;
|
||||
protected abstract void closeWorkingCopy(W workingCopy) throws Exception;
|
||||
|
||||
public static class ReclaimFailedException extends Exception {
|
||||
public ReclaimFailedException(String message) {
|
||||
|
||||
@@ -26,15 +26,15 @@ package sonia.scm.repository.work;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
public class WorkingCopyContext<R, W, C> {
|
||||
private final Repository scmRepository;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class WorkingCopyContext<R, W, C extends Supplier<Repository>> {
|
||||
private final String requestedBranch;
|
||||
private final C context;
|
||||
private final SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer;
|
||||
private final SimpleWorkingCopyFactory.WorkingCopyReclaimer<R, W> reclaimer;
|
||||
|
||||
public WorkingCopyContext(Repository scmRepository, String requestedBranch, C context, SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer, SimpleWorkingCopyFactory.WorkingCopyReclaimer<R, W> reclaimer) {
|
||||
this.scmRepository = scmRepository;
|
||||
public WorkingCopyContext(String requestedBranch, C context, SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer, SimpleWorkingCopyFactory.WorkingCopyReclaimer<R, W> reclaimer) {
|
||||
this.requestedBranch = requestedBranch;
|
||||
this.context = context;
|
||||
this.initializer = initializer;
|
||||
@@ -42,7 +42,7 @@ public class WorkingCopyContext<R, W, C> {
|
||||
}
|
||||
|
||||
public Repository getScmRepository() {
|
||||
return scmRepository;
|
||||
return context.get();
|
||||
}
|
||||
|
||||
public String getRequestedBranch() {
|
||||
|
||||
@@ -24,10 +24,13 @@
|
||||
|
||||
package sonia.scm.repository.work;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface WorkingCopyPool {
|
||||
<R, W, C> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException;
|
||||
<R, W, C extends Supplier<Repository>> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException;
|
||||
|
||||
void contextClosed(WorkingCopyContext<?, ?, ?> workingCopyContext, File workdir);
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ import sonia.scm.repository.Repository;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -54,7 +55,7 @@ class CachingAllWorkingCopyPoolTest {
|
||||
CachingAllWorkingCopyPool cachingAllWorkingCopyPool;
|
||||
|
||||
@Mock
|
||||
WorkingCopyContext<Object, Path, Path> workingCopyContext;
|
||||
WorkingCopyContext<Object, Path, Supplier<Repository>> workingCopyContext;
|
||||
@Mock
|
||||
SimpleWorkingCopyFactory.WorkingCopyInitializer<Object, Path> initializer;
|
||||
@Mock
|
||||
|
||||
@@ -34,6 +34,7 @@ import sonia.scm.util.IOUtil;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -60,7 +61,7 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
WorkdirProvider workdirProvider = new WorkdirProvider(temporaryFolder.newFolder());
|
||||
WorkingCopyPool configurableTestWorkingCopyPool = new WorkingCopyPool() {
|
||||
@Override
|
||||
public <R, W, C> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException {
|
||||
public <R, W, C extends Supplier<Repository>> ParentAndClone<R, W> getWorkingCopy(WorkingCopyContext<R, W, C> context) throws WorkingCopyFailedException {
|
||||
workdir = workdirProvider.createNewWorkdir();
|
||||
return context.getInitializer().initialize(workdir);
|
||||
}
|
||||
@@ -77,11 +78,6 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
}
|
||||
};
|
||||
simpleWorkingCopyFactory = new SimpleWorkingCopyFactory<Closeable, Closeable, Context>(configurableTestWorkingCopyPool) {
|
||||
@Override
|
||||
protected Repository getScmRepository(Context context) {
|
||||
return REPOSITORY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeRepository(Closeable repository) throws IOException {
|
||||
repository.close();
|
||||
@@ -93,7 +89,7 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeWorkingCopyInternal(Closeable workingCopy) throws Exception {
|
||||
protected void closeWorkingCopy(Closeable workingCopy) throws Exception {
|
||||
workingCopy.close();
|
||||
}
|
||||
|
||||
@@ -155,5 +151,10 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static class Context {}
|
||||
private static class Context implements Supplier<Repository> {
|
||||
@Override
|
||||
public Repository get() {
|
||||
return REPOSITORY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +39,13 @@ import sonia.scm.repository.Repository;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitContext implements Closeable
|
||||
public class GitContext implements Closeable, Supplier<Repository>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -108,6 +109,11 @@ public class GitContext implements Closeable
|
||||
return repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository get() {
|
||||
return getRepository();
|
||||
}
|
||||
|
||||
File getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
@@ -133,14 +133,9 @@ public class SimpleGitWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposi
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeWorkingCopyInternal(Repository workingCopy) throws Exception {
|
||||
protected void closeWorkingCopy(Repository workingCopy) throws Exception {
|
||||
if (workingCopy != null) {
|
||||
workingCopy.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected sonia.scm.repository.Repository getScmRepository(GitContext context) {
|
||||
return context.getRepository();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,12 +42,13 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class HgCommandContext implements Closeable
|
||||
public class HgCommandContext implements Closeable, Supplier<sonia.scm.repository.Repository>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -155,6 +156,11 @@ public class HgCommandContext implements Closeable
|
||||
return scmRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public sonia.scm.repository.Repository get() {
|
||||
return getScmRepository();
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -104,15 +104,10 @@ public class SimpleHgWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposit
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeWorkingCopyInternal(Repository workingCopy) throws Exception {
|
||||
protected void closeWorkingCopy(Repository workingCopy) throws Exception {
|
||||
workingCopy.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected sonia.scm.repository.Repository getScmRepository(HgCommandContext context) {
|
||||
return context.getScmRepository();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(PullCommand pullCommand) {
|
||||
pullCommand.cmdAppend("--config", "hooks.changegroup.scm=python:scmhooks.postHook");
|
||||
|
||||
@@ -49,11 +49,6 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
||||
super(workingCopyPool);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Repository getScmRepository(SvnContext context) {
|
||||
return context.getRepository();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ParentAndClone<File, File> cloneRepository(SvnContext context, File workingCopy, String initialBranch) {
|
||||
|
||||
@@ -63,7 +58,7 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
||||
try {
|
||||
source = SVNURL.fromFile(context.getDirectory());
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(getScmRepository(context), "error creating svn url from central directory", ex);
|
||||
throw new InternalRepositoryException(context.getRepository(), "error creating svn url from central directory", ex);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -72,7 +67,7 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
||||
checkout.setSource(SvnTarget.fromURL(source));
|
||||
checkout.run();
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(getScmRepository(context), "error running svn checkout", ex);
|
||||
throw new InternalRepositoryException(context.getRepository(), "error running svn checkout", ex);
|
||||
} finally {
|
||||
svnOperationFactory.dispose();
|
||||
}
|
||||
@@ -97,6 +92,6 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeWorkingCopyInternal(File workingCopy) {
|
||||
protected void closeWorkingCopy(File workingCopy) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +41,13 @@ import sonia.scm.repository.SvnUtil;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SvnContext implements Closeable {
|
||||
public class SvnContext implements Closeable, Supplier<Repository> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SvnContext.class);
|
||||
|
||||
@@ -64,6 +65,11 @@ public class SvnContext implements Closeable {
|
||||
return repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository get() {
|
||||
return getRepository();
|
||||
}
|
||||
|
||||
public File getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public class SimpleSvnWorkingCopyFactoryTest extends AbstractSvnCommandTestBase
|
||||
@Test
|
||||
public void shouldReturnRepository() {
|
||||
SimpleSvnWorkingCopyFactory factory = new SimpleSvnWorkingCopyFactory(new NoneCachingWorkingCopyPool(workdirProvider));
|
||||
Repository scmRepository = factory.getScmRepository(createContext());
|
||||
Repository scmRepository = createContext().getRepository();
|
||||
assertThat(scmRepository).isSameAs(repository);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user