mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 03:25:56 +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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.repository.InternalRepositoryException;
|
import sonia.scm.repository.InternalRepositoryException;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
||||||
|
|
||||||
@@ -49,7 +51,7 @@ public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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();
|
String id = workingCopyContext.getScmRepository().getId();
|
||||||
File existingWorkdir = workdirs.remove(id);
|
File existingWorkdir = workdirs.remove(id);
|
||||||
if (existingWorkdir != null) {
|
if (existingWorkdir != null) {
|
||||||
|
|||||||
@@ -24,10 +24,12 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.work;
|
package sonia.scm.repository.work;
|
||||||
|
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
||||||
|
|
||||||
@@ -39,7 +41,7 @@ public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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());
|
return context.getInitializer().initialize(workdirProvider.createNewWorkdir());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,166 @@ import sonia.scm.repository.Repository;
|
|||||||
import javax.servlet.ServletContextEvent;
|
import javax.servlet.ServletContextEvent;
|
||||||
import javax.servlet.ServletContextListener;
|
import javax.servlet.ServletContextListener;
|
||||||
import java.io.File;
|
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);
|
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);
|
WorkingCopyPool.ParentAndClone<R, W> parentAndClone = workingCopyPool.getWorkingCopy(workingCopyContext);
|
||||||
return new WorkingCopy<>(parentAndClone.getClone(), parentAndClone.getParent(), () -> this.close(workingCopyContext, parentAndClone), parentAndClone.getDirectory());
|
return new WorkingCopy<>(parentAndClone.getClone(), parentAndClone.getParent(), () -> this.close(workingCopyContext, parentAndClone), parentAndClone.getDirectory());
|
||||||
} catch (WorkingCopyFailedException e) {
|
} 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) {
|
private WorkingCopyContext<R, W, C> createWorkingCopyContext(C repositoryContext, String initialBranch) {
|
||||||
return new WorkingCopyContext<>(
|
return new WorkingCopyContext<>(
|
||||||
getScmRepository(repositoryContext),
|
|
||||||
initialBranch,
|
initialBranch,
|
||||||
repositoryContext,
|
repositoryContext,
|
||||||
newFolder -> cloneRepository(repositoryContext, newFolder, initialBranch),
|
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) {
|
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 {
|
try {
|
||||||
closeRepository(parentAndClone.getParent());
|
closeRepository(parentAndClone.getParent());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.warn("could not close central repository for {}", workingCopyContext.getScmRepository(), 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 {
|
try {
|
||||||
workingCopyPool.contextClosed(workingCopyContext, parentAndClone.getDirectory());
|
workingCopyPool.contextClosed(workingCopyContext, parentAndClone.getDirectory());
|
||||||
} catch (Exception e) {
|
} 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;
|
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")
|
@SuppressWarnings("squid:S00112")
|
||||||
// We do allow implementations to throw arbitrary exceptions here, so that we can handle them in closeCentral
|
// 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;
|
protected abstract void closeRepository(R repository) throws Exception;
|
||||||
|
|
||||||
@SuppressWarnings("squid:S00112")
|
@SuppressWarnings("squid:S00112")
|
||||||
// We do allow implementations to throw arbitrary exceptions here, so that we can handle them in closeWorkingCopy
|
// 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 void closeWorkingCopy(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;
|
|
||||||
|
|
||||||
public static class ReclaimFailedException extends Exception {
|
public static class ReclaimFailedException extends Exception {
|
||||||
public ReclaimFailedException(String message) {
|
public ReclaimFailedException(String message) {
|
||||||
|
|||||||
@@ -26,15 +26,15 @@ package sonia.scm.repository.work;
|
|||||||
|
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
|
|
||||||
public class WorkingCopyContext<R, W, C> {
|
import java.util.function.Supplier;
|
||||||
private final Repository scmRepository;
|
|
||||||
|
public class WorkingCopyContext<R, W, C extends Supplier<Repository>> {
|
||||||
private final String requestedBranch;
|
private final String requestedBranch;
|
||||||
private final C context;
|
private final C context;
|
||||||
private final SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer;
|
private final SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer;
|
||||||
private final SimpleWorkingCopyFactory.WorkingCopyReclaimer<R, W> reclaimer;
|
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) {
|
public WorkingCopyContext(String requestedBranch, C context, SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer, SimpleWorkingCopyFactory.WorkingCopyReclaimer<R, W> reclaimer) {
|
||||||
this.scmRepository = scmRepository;
|
|
||||||
this.requestedBranch = requestedBranch;
|
this.requestedBranch = requestedBranch;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.initializer = initializer;
|
this.initializer = initializer;
|
||||||
@@ -42,7 +42,7 @@ public class WorkingCopyContext<R, W, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Repository getScmRepository() {
|
public Repository getScmRepository() {
|
||||||
return scmRepository;
|
return context.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRequestedBranch() {
|
public String getRequestedBranch() {
|
||||||
|
|||||||
@@ -24,10 +24,13 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.work;
|
package sonia.scm.repository.work;
|
||||||
|
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public interface WorkingCopyPool {
|
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);
|
void contextClosed(WorkingCopyContext<?, ?, ?> workingCopyContext, File workdir);
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import sonia.scm.repository.Repository;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
@@ -54,7 +55,7 @@ class CachingAllWorkingCopyPoolTest {
|
|||||||
CachingAllWorkingCopyPool cachingAllWorkingCopyPool;
|
CachingAllWorkingCopyPool cachingAllWorkingCopyPool;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
WorkingCopyContext<Object, Path, Path> workingCopyContext;
|
WorkingCopyContext<Object, Path, Supplier<Repository>> workingCopyContext;
|
||||||
@Mock
|
@Mock
|
||||||
SimpleWorkingCopyFactory.WorkingCopyInitializer<Object, Path> initializer;
|
SimpleWorkingCopyFactory.WorkingCopyInitializer<Object, Path> initializer;
|
||||||
@Mock
|
@Mock
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import sonia.scm.util.IOUtil;
|
|||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@@ -60,7 +61,7 @@ public class SimpleWorkingCopyFactoryTest {
|
|||||||
WorkdirProvider workdirProvider = new WorkdirProvider(temporaryFolder.newFolder());
|
WorkdirProvider workdirProvider = new WorkdirProvider(temporaryFolder.newFolder());
|
||||||
WorkingCopyPool configurableTestWorkingCopyPool = new WorkingCopyPool() {
|
WorkingCopyPool configurableTestWorkingCopyPool = new WorkingCopyPool() {
|
||||||
@Override
|
@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();
|
workdir = workdirProvider.createNewWorkdir();
|
||||||
return context.getInitializer().initialize(workdir);
|
return context.getInitializer().initialize(workdir);
|
||||||
}
|
}
|
||||||
@@ -77,11 +78,6 @@ public class SimpleWorkingCopyFactoryTest {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
simpleWorkingCopyFactory = new SimpleWorkingCopyFactory<Closeable, Closeable, Context>(configurableTestWorkingCopyPool) {
|
simpleWorkingCopyFactory = new SimpleWorkingCopyFactory<Closeable, Closeable, Context>(configurableTestWorkingCopyPool) {
|
||||||
@Override
|
|
||||||
protected Repository getScmRepository(Context context) {
|
|
||||||
return REPOSITORY;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeRepository(Closeable repository) throws IOException {
|
protected void closeRepository(Closeable repository) throws IOException {
|
||||||
repository.close();
|
repository.close();
|
||||||
@@ -93,7 +89,7 @@ public class SimpleWorkingCopyFactoryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeWorkingCopyInternal(Closeable workingCopy) throws Exception {
|
protected void closeWorkingCopy(Closeable workingCopy) throws Exception {
|
||||||
workingCopy.close();
|
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.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @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;
|
return repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Repository get() {
|
||||||
|
return getRepository();
|
||||||
|
}
|
||||||
|
|
||||||
File getDirectory() {
|
File getDirectory() {
|
||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,14 +133,9 @@ public class SimpleGitWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeWorkingCopyInternal(Repository workingCopy) throws Exception {
|
protected void closeWorkingCopy(Repository workingCopy) throws Exception {
|
||||||
if (workingCopy != null) {
|
if (workingCopy != null) {
|
||||||
workingCopy.close();
|
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.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class HgCommandContext implements Closeable
|
public class HgCommandContext implements Closeable, Supplier<sonia.scm.repository.Repository>
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -155,6 +156,11 @@ public class HgCommandContext implements Closeable
|
|||||||
return scmRepository;
|
return scmRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public sonia.scm.repository.Repository get() {
|
||||||
|
return getScmRepository();
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
@@ -104,15 +104,10 @@ public class SimpleHgWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposit
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeWorkingCopyInternal(Repository workingCopy) throws Exception {
|
protected void closeWorkingCopy(Repository workingCopy) throws Exception {
|
||||||
workingCopy.close();
|
workingCopy.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected sonia.scm.repository.Repository getScmRepository(HgCommandContext context) {
|
|
||||||
return context.getScmRepository();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(PullCommand pullCommand) {
|
public void configure(PullCommand pullCommand) {
|
||||||
pullCommand.cmdAppend("--config", "hooks.changegroup.scm=python:scmhooks.postHook");
|
pullCommand.cmdAppend("--config", "hooks.changegroup.scm=python:scmhooks.postHook");
|
||||||
|
|||||||
@@ -49,11 +49,6 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
|||||||
super(workingCopyPool);
|
super(workingCopyPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Repository getScmRepository(SvnContext context) {
|
|
||||||
return context.getRepository();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ParentAndClone<File, File> cloneRepository(SvnContext context, File workingCopy, String initialBranch) {
|
protected ParentAndClone<File, File> cloneRepository(SvnContext context, File workingCopy, String initialBranch) {
|
||||||
|
|
||||||
@@ -63,7 +58,7 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
|||||||
try {
|
try {
|
||||||
source = SVNURL.fromFile(context.getDirectory());
|
source = SVNURL.fromFile(context.getDirectory());
|
||||||
} catch (SVNException ex) {
|
} 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 {
|
try {
|
||||||
@@ -72,7 +67,7 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
|||||||
checkout.setSource(SvnTarget.fromURL(source));
|
checkout.setSource(SvnTarget.fromURL(source));
|
||||||
checkout.run();
|
checkout.run();
|
||||||
} catch (SVNException ex) {
|
} catch (SVNException ex) {
|
||||||
throw new InternalRepositoryException(getScmRepository(context), "error running svn checkout", ex);
|
throw new InternalRepositoryException(context.getRepository(), "error running svn checkout", ex);
|
||||||
} finally {
|
} finally {
|
||||||
svnOperationFactory.dispose();
|
svnOperationFactory.dispose();
|
||||||
}
|
}
|
||||||
@@ -97,6 +92,6 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class SvnContext implements Closeable {
|
public class SvnContext implements Closeable, Supplier<Repository> {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(SvnContext.class);
|
private static final Logger LOG = LoggerFactory.getLogger(SvnContext.class);
|
||||||
|
|
||||||
@@ -64,6 +65,11 @@ public class SvnContext implements Closeable {
|
|||||||
return repository;
|
return repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Repository get() {
|
||||||
|
return getRepository();
|
||||||
|
}
|
||||||
|
|
||||||
public File getDirectory() {
|
public File getDirectory() {
|
||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public class SimpleSvnWorkingCopyFactoryTest extends AbstractSvnCommandTestBase
|
|||||||
@Test
|
@Test
|
||||||
public void shouldReturnRepository() {
|
public void shouldReturnRepository() {
|
||||||
SimpleSvnWorkingCopyFactory factory = new SimpleSvnWorkingCopyFactory(new NoneCachingWorkingCopyPool(workdirProvider));
|
SimpleSvnWorkingCopyFactory factory = new SimpleSvnWorkingCopyFactory(new NoneCachingWorkingCopyPool(workdirProvider));
|
||||||
Repository scmRepository = factory.getScmRepository(createContext());
|
Repository scmRepository = createContext().getRepository();
|
||||||
assertThat(scmRepository).isSameAs(repository);
|
assertThat(scmRepository).isSameAs(repository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user