mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-05 20:00:55 +01:00
Make reclaimer and initializer top level classes
This commit is contained in:
@@ -57,7 +57,7 @@ public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
||||
if (existingWorkdir != null) {
|
||||
Stopwatch stopwatch = Stopwatch.createStarted();
|
||||
try {
|
||||
ParentAndClone<R, W> reclaimed = workingCopyContext.getReclaimer().reclaim(existingWorkdir);
|
||||
ParentAndClone<R, W> reclaimed = workingCopyContext.getReclaimer().reclaim(existingWorkdir, workingCopyContext.getRequestedBranch());
|
||||
LOG.debug("reclaimed workdir for {} in path {} in {}", workingCopyContext.getScmRepository().getNamespaceAndName(), existingWorkdir, stopwatch.stop());
|
||||
return reclaimed;
|
||||
} catch (SimpleWorkingCopyFactory.ReclaimFailedException e) {
|
||||
@@ -75,7 +75,8 @@ public class CachingAllWorkingCopyPool implements WorkingCopyPool {
|
||||
private <R, W> ParentAndClone<R, W> createNewWorkdir(WorkingCopyContext<R, W, ?> workingCopyContext) throws WorkingCopyFailedException {
|
||||
Stopwatch stopwatch = Stopwatch.createStarted();
|
||||
File newWorkdir = workdirProvider.createNewWorkdir();
|
||||
ParentAndClone<R, W> parentAndClone = workingCopyContext.getInitializer().initialize(newWorkdir);
|
||||
SimpleWorkingCopyFactory.WorkingCopyInitializer<R, W> initializer = workingCopyContext.getInitializer();
|
||||
ParentAndClone<R, W> parentAndClone = initializer.initialize(newWorkdir, workingCopyContext.getRequestedBranch());
|
||||
LOG.debug("initialized new workdir for {} in path {} in {}", workingCopyContext.getScmRepository().getNamespaceAndName(), newWorkdir, stopwatch.stop());
|
||||
return parentAndClone;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class NoneCachingWorkingCopyPool implements WorkingCopyPool {
|
||||
|
||||
@Override
|
||||
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(), context.getRequestedBranch());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -42,10 +42,10 @@ import java.util.function.Supplier;
|
||||
* implemented:
|
||||
*
|
||||
* <dl>
|
||||
* <dt>{@link #cloneRepository(C, File, String)}</dt>
|
||||
* <dt>{@link #getInitializer(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>
|
||||
* <dt>{@link #getReclaimer(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
|
||||
@@ -82,19 +82,16 @@ import java.util.function.Supplier;
|
||||
* │ │ │ reclaim │ │
|
||||
* │ │ │<─────────────────────────────────────────────│ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ reclaim │ │ │ │
|
||||
* │ │<────────────────────────────│ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ │ │ │
|
||||
* │ ╔══════╤════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╗
|
||||
* │ ║ ALT │ reclaim successful │ │ │ │ ║
|
||||
* │ ╟──────┘ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────>│ │ ║
|
||||
* │ ║ │ │─────────────────────────────────────────────>│ │ ║
|
||||
* │ ╠═══════════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╣
|
||||
* │ ║ [reclaim fails; create new] │ │ │ │ ║
|
||||
* │ ║ │ ReclaimFailedException │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────X│ │ ║
|
||||
* │ ║ │ ReclaimFailedException │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────>│ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ createNewWorkdir │ ║
|
||||
* │ ║ │ │ │ │─────────────────────────>│ ║
|
||||
@@ -105,16 +102,13 @@ import java.util.function.Supplier;
|
||||
* │ ║ │ │ │ initialize │ │ ║
|
||||
* │ ║ │ │ │<───────────────────────│ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ initialize │ │ │ ║
|
||||
* │ ║ │<──────────────────────────────────────────────────│ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │ │ │ │ │ ║
|
||||
* │ ║ │───────────────────────────────────────────────────────────────────────────>│ │ ║
|
||||
* │ ║ │ │ │───────────────────────>│ │ ║
|
||||
* │ ╚═══════════════════════╪═════════════════════════════╪═════════════════════╪════════════════════════╪══════════════════════════╪═════════════════╝
|
||||
* │ │ │ │ │ │
|
||||
* │ │ │ParentAndClone │ │ │
|
||||
* │ │<───────────────────────────────────────────────────────────────────────────│ │
|
||||
* │ │ │ │ │ │
|
||||
* │ │<───────────────────────────────────────────────────────────────────────────│ │ │
|
||||
* │ │ │ │ │ │ │
|
||||
* │ │ │ │ │ │ ┌───────────┐
|
||||
* │ │────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────> │WorkingCopy│
|
||||
* │ │ │ │ │ │ └─────┬─────┘
|
||||
@@ -148,17 +142,14 @@ import java.util.function.Supplier;
|
||||
* │ │ │ │ │ │ │
|
||||
* │ │ │ 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"/>
|
||||
* <img src="http://www.plantuml.com/plantuml/png/fPF1QiCm38RlVWgV0-a3U10wmZfss2ZTO6TZgqLCiOBjhCsUVeIcmKkPcx5RB7t_7qcsrY5g7xk6n3DRtFSrDursjgnUiMa97Z6-i_z0TwYvxJVQHFQzLD9uq16IbCZmMJDrjghPHJZ5l8tSWI6D3VYY67Kt14yE8sh2hyMI9BPb9dM051C7prqhttml8qj_BaVCD6KrrQakYAPumMNeZ84GzXs92IohIivi1520IRJNIE5k7BnqSCotSPgxgV5N6uq4zk5ae8t8xhhs8M3HRpr_eWK_3kq5ZcD2p82oci_isZEv1eNJjvZ2l_JlxBLLzYrjjGSNxvsjYTtoYowAF5hzs0sL5YfMgzLyTPAqiZoSRb66E43IQtvbHZn3B90dyNywxQ3bWCFvRvjbpYjV3nvMhC7Phg5l"/>
|
||||
* @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
|
||||
http://www.plantuml.com/plantuml/uml/fPF1QiCm38RlVWgV0-a3U10wmZfss2ZTO6TZgqLCiOBjhCsUVeIcmKkPcx5RB7t_7qcsrY5g7xk6n3DRtFSrDursjgnUiMa97Z6-i_z0TwYvxJVQHFQzLD9uq16IbCZmMJDrjghPHJZ5l8tSWI6D3VYY67Kt14yE8sh2hyMI9BPb9dM051C7prqhttml8qj_BaVCD6KrrQakYAPumMNeZ84GzXs92IohIivi1520IRJNIE5k7BnqSCotSPgxgV5N6uq4zk5ae8t8xhhs8M3HRpr_eWK_3kq5ZcD2p82oci_isZEv1eNJjvZ2l_JlxBLLzYrjjGSNxvsjYTtoYowAF5hzs0sL5YfMgzLyTPAqiZoSRb66E43IQtvbHZn3B90dyNywxQ3bWCFvRvjbpYjV3nvMhC7Phg5l
|
||||
@startuml
|
||||
ModifyCommand->SimpleGitWorkingCopyFactory : createWorkingCopy
|
||||
SimpleGitWorkingCopyFactory->SimpleGitWorkingCopyFactory:createContext
|
||||
@@ -167,18 +158,16 @@ SimpleGitWorkingCopyFactory-> Initializer**:create
|
||||
SimpleGitWorkingCopyFactory->WorkingCopyPool:getWorkingCopy
|
||||
group Try to reclaim
|
||||
WorkingCopyPool->Reclaimer:reclaim
|
||||
Reclaimer->SimpleGitWorkingCopyFactory:reclaim
|
||||
alt reclaim successful
|
||||
SimpleGitWorkingCopyFactory->> WorkingCopyPool
|
||||
Reclaimer->> WorkingCopyPool
|
||||
else reclaim fails; create new
|
||||
SimpleGitWorkingCopyFactory->x WorkingCopyPool:ReclaimFailedException
|
||||
WorkingCopyPool->WorkdirProvider:createNewWorkdir
|
||||
WorkdirProvider->>WorkingCopyPool
|
||||
WorkingCopyPool->Initializer:initialize
|
||||
Initializer->SimpleGitWorkingCopyFactory:initialize
|
||||
SimpleGitWorkingCopyFactory->> WorkingCopyPool
|
||||
Initializer->> WorkingCopyPool
|
||||
end
|
||||
WorkingCopyPool->>SimpleGitWorkingCopyFactory
|
||||
WorkingCopyPool->>SimpleGitWorkingCopyFactory:ParentAndClone
|
||||
SimpleGitWorkingCopyFactory->WorkingCopy**
|
||||
SimpleGitWorkingCopyFactory->>ModifyCommand: WorkingCopy
|
||||
...
|
||||
@@ -216,8 +205,8 @@ public abstract class SimpleWorkingCopyFactory<R, W, C extends Supplier<Reposito
|
||||
return new WorkingCopyContext<>(
|
||||
initialBranch,
|
||||
repositoryContext,
|
||||
newFolder -> cloneRepository(repositoryContext, newFolder, initialBranch),
|
||||
cachedFolder -> reclaimRepository(repositoryContext, cachedFolder, initialBranch)
|
||||
getInitializer(repositoryContext),
|
||||
getReclaimer(repositoryContext)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -251,17 +240,17 @@ public abstract class SimpleWorkingCopyFactory<R, W, C extends Supplier<Reposito
|
||||
|
||||
@FunctionalInterface
|
||||
public interface WorkingCopyInitializer<R, W> {
|
||||
WorkingCopyPool.ParentAndClone<R, W> initialize(File target) throws WorkingCopyFailedException;
|
||||
WorkingCopyPool.ParentAndClone<R, W> initialize(File target, String initialBranch) throws WorkingCopyFailedException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface WorkingCopyReclaimer<R, W> {
|
||||
WorkingCopyPool.ParentAndClone<R, W> reclaim(File target) throws ReclaimFailedException;
|
||||
WorkingCopyPool.ParentAndClone<R, W> reclaim(File target, String initialBranch) throws ReclaimFailedException;
|
||||
}
|
||||
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> cloneRepository(C context, File target, String initialBranch) throws WorkingCopyFailedException;
|
||||
protected abstract WorkingCopyInitializer<R, W> getInitializer(C context);
|
||||
|
||||
protected abstract WorkingCopyPool.ParentAndClone<R, W> reclaimRepository(C context, File target, String initialBranch) throws ReclaimFailedException;
|
||||
protected abstract WorkingCopyReclaimer<R, W> getReclaimer(C context);
|
||||
|
||||
@SuppressWarnings("squid:S00112")
|
||||
// We do allow implementations to throw arbitrary exceptions here, so that we can handle them in closeCentral
|
||||
|
||||
@@ -39,6 +39,7 @@ import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -66,9 +67,9 @@ class CachingAllWorkingCopyPoolTest {
|
||||
lenient().when(workingCopyContext.getInitializer()).thenReturn(initializer);
|
||||
lenient().when(workingCopyContext.getReclaimer()).thenReturn(reclaimer);
|
||||
|
||||
lenient().when(initializer.initialize(any()))
|
||||
lenient().when(initializer.initialize(any(), any()))
|
||||
.thenAnswer(invocationOnMock -> new WorkingCopyPool.ParentAndClone<>(null, null, invocationOnMock.getArgument(0, File.class)));
|
||||
lenient().when(reclaimer.reclaim(any()))
|
||||
lenient().when(reclaimer.reclaim(any(), any()))
|
||||
.thenAnswer(invocationOnMock -> new WorkingCopyPool.ParentAndClone<>(null, null, invocationOnMock.getArgument(0, File.class)));
|
||||
}
|
||||
|
||||
@@ -79,7 +80,7 @@ class CachingAllWorkingCopyPoolTest {
|
||||
|
||||
WorkingCopyPool.ParentAndClone<?, ?> workdir = cachingAllWorkingCopyPool.getWorkingCopy(workingCopyContext);
|
||||
|
||||
verify(initializer).initialize(temp.toFile());
|
||||
verify(initializer).initialize(temp.toFile(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,8 +92,8 @@ class CachingAllWorkingCopyPoolTest {
|
||||
cachingAllWorkingCopyPool.contextClosed(workingCopyContext, firstWorkdir.getDirectory());
|
||||
WorkingCopyPool.ParentAndClone<?, ?> secondWorkdir = cachingAllWorkingCopyPool.getWorkingCopy(workingCopyContext);
|
||||
|
||||
verify(initializer).initialize(temp.toFile());
|
||||
verify(reclaimer).reclaim(temp.toFile());
|
||||
verify(initializer).initialize(temp.toFile(), null);
|
||||
verify(reclaimer).reclaim(temp.toFile(), null);
|
||||
assertThat(secondWorkdir.getDirectory()).isEqualTo(temp.toFile());
|
||||
}
|
||||
|
||||
@@ -112,9 +113,9 @@ class CachingAllWorkingCopyPoolTest {
|
||||
cachingAllWorkingCopyPool.contextClosed(workingCopyContext, firstWorkdir.getDirectory());
|
||||
cachingAllWorkingCopyPool.contextClosed(workingCopyContext, secondWorkdir.getDirectory());
|
||||
|
||||
verify(reclaimer, never()).reclaim(any());
|
||||
verify(initializer).initialize(firstDirectory);
|
||||
verify(initializer).initialize(secondDirectory);
|
||||
verify(reclaimer, never()).reclaim(any(), any());
|
||||
verify(initializer).initialize(firstDirectory, null);
|
||||
verify(initializer).initialize(secondDirectory, null);
|
||||
assertThat(firstWorkdir.getDirectory()).isNotEqualTo(secondWorkdir.getDirectory());
|
||||
assertThat(firstWorkdir.getDirectory()).exists();
|
||||
assertThat(secondWorkdir.getDirectory()).doesNotExist();
|
||||
|
||||
@@ -63,7 +63,7 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
@Override
|
||||
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);
|
||||
return context.getInitializer().initialize(workdir, context.getRequestedBranch());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,8 +84,9 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WorkingCopyPool.ParentAndClone<Closeable, Closeable> reclaimRepository(Context context, File target, String initialBranch) {
|
||||
throw new UnsupportedOperationException();
|
||||
protected WorkingCopyReclaimer<
|
||||
Closeable, Closeable> getReclaimer(Context context) {
|
||||
return (target, initialBranch) -> {throw new UnsupportedOperationException();};
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,9 +95,11 @@ public class SimpleWorkingCopyFactoryTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WorkingCopyPool.ParentAndClone<Closeable, Closeable> cloneRepository(Context context, File target, String initialBranch) {
|
||||
initialBranchForLastCloneCall = initialBranch;
|
||||
return new WorkingCopyPool.ParentAndClone<>(parent, clone, target);
|
||||
protected WorkingCopyInitializer<Closeable, Closeable> getInitializer(Context context) {
|
||||
return (target, initialBranch) -> {
|
||||
initialBranchForLastCloneCall = initialBranch;
|
||||
return new WorkingCopyPool.ParentAndClone<>(parent, clone, target);
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -58,53 +58,57 @@ public class SimpleGitWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposi
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParentAndClone<Repository, Repository> cloneRepository(GitContext context, File target, String initialBranch) {
|
||||
LOG.trace("clone repository {}", context.getRepository().getId());
|
||||
long start = System.nanoTime();
|
||||
try {
|
||||
Repository clone = Git.cloneRepository()
|
||||
.setURI(createScmTransportProtocolUri(context.getDirectory()))
|
||||
.setDirectory(target)
|
||||
.setBranch(initialBranch)
|
||||
.call()
|
||||
.getRepository();
|
||||
public WorkingCopyInitializer<Repository, Repository> getInitializer(GitContext context) {
|
||||
return (target, initialBranch) -> {
|
||||
LOG.trace("clone repository {}", context.getRepository().getId());
|
||||
long start = System.nanoTime();
|
||||
try {
|
||||
Repository clone = Git.cloneRepository()
|
||||
.setURI(createScmTransportProtocolUri(context.getDirectory()))
|
||||
.setDirectory(target)
|
||||
.setBranch(initialBranch)
|
||||
.call()
|
||||
.getRepository();
|
||||
|
||||
Ref head = clone.exactRef(Constants.HEAD);
|
||||
Ref head = clone.exactRef(Constants.HEAD);
|
||||
|
||||
if (head == null || !head.isSymbolic() || (initialBranch != null && !head.getTarget().getName().endsWith(initialBranch))) {
|
||||
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
||||
if (head == null || !head.isSymbolic() || (initialBranch != null && !head.getTarget().getName().endsWith(initialBranch))) {
|
||||
throw notFound(entity("Branch", initialBranch).in(context.getRepository()));
|
||||
}
|
||||
|
||||
return new ParentAndClone<>(null, clone, target);
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not clone working copy of repository", e);
|
||||
} finally {
|
||||
long end = System.nanoTime();
|
||||
long duration = end - start;
|
||||
LOG.trace("took {} ns to clone repository {}", duration, context.getRepository().getId());
|
||||
}
|
||||
|
||||
return new ParentAndClone<>(null, clone, target);
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not clone working copy of repository", e);
|
||||
} finally {
|
||||
long end = System.nanoTime();
|
||||
long duration = end - start;
|
||||
LOG.trace("took {} ns to clone repository {}", duration, context.getRepository().getId());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ParentAndClone<Repository, Repository> reclaimRepository(GitContext context, File target, String initialBranch) throws ReclaimFailedException {
|
||||
LOG.trace("reclaim repository {}", context.getRepository().getId());
|
||||
long start = System.nanoTime();
|
||||
Repository repo = openTarget(target);
|
||||
try (Git git = Git.open(target)) {
|
||||
git.reset().setMode(ResetCommand.ResetType.HARD).call();
|
||||
git.clean().setForce(true).setCleanDirectories(true).call();
|
||||
git.fetch().call();
|
||||
git.checkout().setForced(true).setName("origin/" + initialBranch).call();
|
||||
git.branchDelete().setBranchNames(initialBranch).setForce(true).call();
|
||||
git.checkout().setName(initialBranch).setCreateBranch(true).call();
|
||||
return new ParentAndClone<>(null, repo, target);
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
} finally {
|
||||
long end = System.nanoTime();
|
||||
long duration = end - start;
|
||||
LOG.trace("took {} ns to reclaim repository {}\n", duration, context.getRepository().getId());
|
||||
}
|
||||
protected WorkingCopyReclaimer<Repository, Repository> getReclaimer(GitContext context) {
|
||||
return (target, initialBranch) -> {
|
||||
LOG.trace("reclaim repository {}", context.getRepository().getId());
|
||||
long start = System.nanoTime();
|
||||
Repository repo = openTarget(target);
|
||||
try (Git git = Git.open(target)) {
|
||||
git.reset().setMode(ResetCommand.ResetType.HARD).call();
|
||||
git.clean().setForce(true).setCleanDirectories(true).call();
|
||||
git.fetch().call();
|
||||
git.checkout().setForced(true).setName("origin/" + initialBranch).call();
|
||||
git.branchDelete().setBranchNames(initialBranch).setForce(true).call();
|
||||
git.checkout().setName(initialBranch).setCreateBranch(true).call();
|
||||
return new ParentAndClone<>(null, repo, target);
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
} finally {
|
||||
long end = System.nanoTime();
|
||||
long duration = end - start;
|
||||
LOG.trace("took {} ns to reclaim repository {}\n", duration, context.getRepository().getId());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Repository openTarget(File target) throws ReclaimFailedException {
|
||||
|
||||
@@ -142,7 +142,7 @@ public class SimpleGitWorkingCopyFactoryTest extends AbstractGitCommandTestBase
|
||||
SimpleGitWorkingCopyFactory factory = new SimpleGitWorkingCopyFactory(new NoneCachingWorkingCopyPool(workdirProvider));
|
||||
File workdir = createExistingClone(factory);
|
||||
|
||||
factory.reclaimRepository(createContext(), workdir, "master");
|
||||
factory.getReclaimer(createContext()).reclaim(workdir, "master");
|
||||
|
||||
assertBranchCheckedOutAndClean(workdir, "master");
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public class SimpleGitWorkingCopyFactoryTest extends AbstractGitCommandTestBase
|
||||
SimpleGitWorkingCopyFactory factory = new SimpleGitWorkingCopyFactory(new NoneCachingWorkingCopyPool(workdirProvider));
|
||||
File workdir = createExistingClone(factory);
|
||||
|
||||
factory.reclaimRepository(createContext(), workdir, "test-branch");
|
||||
factory.getReclaimer(createContext()).reclaim(workdir, "test-branch");
|
||||
|
||||
assertBranchCheckedOutAndClean(workdir, "test-branch");
|
||||
}
|
||||
@@ -163,7 +163,7 @@ public class SimpleGitWorkingCopyFactoryTest extends AbstractGitCommandTestBase
|
||||
File workdir = createExistingClone(factory);
|
||||
Git.open(workdir).rm().addFilepattern("a.txt").call();
|
||||
|
||||
factory.reclaimRepository(createContext(), workdir, "master");
|
||||
factory.getReclaimer(createContext()).reclaim(workdir, "master");
|
||||
|
||||
assertBranchCheckedOutAndClean(workdir, "master");
|
||||
}
|
||||
@@ -174,7 +174,7 @@ public class SimpleGitWorkingCopyFactoryTest extends AbstractGitCommandTestBase
|
||||
File workdir = createExistingClone(factory);
|
||||
Files.delete(workdir.toPath().resolve("a.txt"));
|
||||
|
||||
factory.reclaimRepository(createContext(), workdir, "master");
|
||||
factory.getReclaimer(createContext()).reclaim(workdir, "master");
|
||||
|
||||
assertBranchCheckedOutAndClean(workdir, "master");
|
||||
}
|
||||
@@ -187,7 +187,7 @@ public class SimpleGitWorkingCopyFactoryTest extends AbstractGitCommandTestBase
|
||||
Files.createDirectories(newDirectory);
|
||||
Files.createFile(newDirectory.resolve("newFile"));
|
||||
|
||||
factory.reclaimRepository(createContext(), workdir, "master");
|
||||
factory.getReclaimer(createContext()).reclaim(workdir, "master");
|
||||
|
||||
assertBranchCheckedOutAndClean(workdir, "master");
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ import sonia.scm.web.HgRepositoryEnvironmentBuilder;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.xml.stream.events.StartDocument;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
@@ -56,21 +57,23 @@ public class SimpleHgWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposit
|
||||
this.hgRepositoryEnvironmentBuilder = hgRepositoryEnvironmentBuilder;
|
||||
}
|
||||
@Override
|
||||
public ParentAndClone<Repository, Repository> cloneRepository(HgCommandContext context, File target, String initialBranch) throws WorkingCopyFailedException {
|
||||
Repository centralRepository = openCentral(context);
|
||||
CloneCommand cloneCommand = CloneCommandFlags.on(centralRepository);
|
||||
if (initialBranch != null) {
|
||||
cloneCommand.updaterev(initialBranch);
|
||||
}
|
||||
try {
|
||||
cloneCommand.execute(target.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
throw new WorkingCopyFailedException(e);
|
||||
}
|
||||
public WorkingCopyInitializer<Repository, Repository> getInitializer(HgCommandContext context) {
|
||||
return (target, initialBranch) -> {
|
||||
Repository centralRepository = openCentral(context);
|
||||
CloneCommand cloneCommand = CloneCommandFlags.on(centralRepository);
|
||||
if (initialBranch != null) {
|
||||
cloneCommand.updaterev(initialBranch);
|
||||
}
|
||||
try {
|
||||
cloneCommand.execute(target.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
throw new WorkingCopyFailedException(e);
|
||||
}
|
||||
|
||||
BaseRepository clone = Repository.open(target);
|
||||
BaseRepository clone = Repository.open(target);
|
||||
|
||||
return new ParentAndClone<>(centralRepository, clone, target);
|
||||
return new ParentAndClone<>(centralRepository, clone, target);
|
||||
};
|
||||
}
|
||||
|
||||
public Repository openCentral(HgCommandContext context) {
|
||||
@@ -80,18 +83,20 @@ public class SimpleHgWorkingCopyFactory extends SimpleWorkingCopyFactory<Reposit
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ParentAndClone<Repository, Repository> reclaimRepository(HgCommandContext context, File target, String initialBranch) throws ReclaimFailedException {
|
||||
Repository centralRepository = openCentral(context);
|
||||
try {
|
||||
BaseRepository clone = Repository.open(target);
|
||||
for (String unknown : StatusCommand.on(clone).execute().getUnknown()) {
|
||||
delete(clone.getDirectory(), unknown);
|
||||
protected WorkingCopyReclaimer<Repository, Repository> getReclaimer(HgCommandContext context) {
|
||||
return (target, initialBranch) -> {
|
||||
Repository centralRepository = openCentral(context);
|
||||
try {
|
||||
BaseRepository clone = Repository.open(target);
|
||||
for (String unknown : StatusCommand.on(clone).execute().getUnknown()) {
|
||||
delete(clone.getDirectory(), unknown);
|
||||
}
|
||||
UpdateCommand.on(clone).rev(initialBranch).clean().execute();
|
||||
return new ParentAndClone<>(centralRepository, clone, target);
|
||||
} catch (ExecutionException | IOException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
}
|
||||
UpdateCommand.on(clone).rev(initialBranch).clean().execute();
|
||||
return new ParentAndClone<>(centralRepository, clone, target);
|
||||
} catch (ExecutionException | IOException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void delete(File directory, String unknownFile) throws IOException {
|
||||
|
||||
@@ -50,41 +50,44 @@ public class SimpleSvnWorkingCopyFactory extends SimpleWorkingCopyFactory<File,
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ParentAndClone<File, File> cloneRepository(SvnContext context, File workingCopy, String initialBranch) {
|
||||
protected WorkingCopyInitializer getInitializer(SvnContext context) {
|
||||
return (workingCopy, initialBranch) -> {
|
||||
final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
|
||||
|
||||
final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
|
||||
SVNURL source;
|
||||
try {
|
||||
source = SVNURL.fromFile(context.getDirectory());
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "error creating svn url from central directory", ex);
|
||||
}
|
||||
|
||||
SVNURL source;
|
||||
try {
|
||||
source = SVNURL.fromFile(context.getDirectory());
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "error creating svn url from central directory", ex);
|
||||
}
|
||||
try {
|
||||
final SvnCheckout checkout = svnOperationFactory.createCheckout();
|
||||
checkout.setSingleTarget(SvnTarget.fromFile(workingCopy));
|
||||
checkout.setSource(SvnTarget.fromURL(source));
|
||||
checkout.run();
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "error running svn checkout", ex);
|
||||
} finally {
|
||||
svnOperationFactory.dispose();
|
||||
}
|
||||
|
||||
try {
|
||||
final SvnCheckout checkout = svnOperationFactory.createCheckout();
|
||||
checkout.setSingleTarget(SvnTarget.fromFile(workingCopy));
|
||||
checkout.setSource(SvnTarget.fromURL(source));
|
||||
checkout.run();
|
||||
} catch (SVNException ex) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "error running svn checkout", ex);
|
||||
} finally {
|
||||
svnOperationFactory.dispose();
|
||||
}
|
||||
|
||||
return new ParentAndClone<>(context.getDirectory(), workingCopy, workingCopy);
|
||||
return new ParentAndClone<>(context.getDirectory(), workingCopy, workingCopy);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ParentAndClone<File, File> reclaimRepository(SvnContext context, File target, String initialBranch) throws ReclaimFailedException {
|
||||
SVNClientManager clientManager = SVNClientManager.newInstance();
|
||||
try {
|
||||
clientManager.getWCClient().doCleanup(target);
|
||||
clientManager.getUpdateClient().doUpdate(target, SVNRevision.HEAD, SVNDepth.fromRecurse(true), false, false);
|
||||
} catch (SVNException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
}
|
||||
return new ParentAndClone<>(context.getDirectory(), target, target);
|
||||
protected WorkingCopyReclaimer<File, File> getReclaimer(SvnContext context) {
|
||||
return (target, initialBranch) -> {
|
||||
SVNClientManager clientManager = SVNClientManager.newInstance();
|
||||
try {
|
||||
clientManager.getWCClient().doCleanup(target);
|
||||
clientManager.getUpdateClient().doUpdate(target, SVNRevision.HEAD, SVNDepth.fromRecurse(true), false, false);
|
||||
} catch (SVNException e) {
|
||||
throw new ReclaimFailedException(e);
|
||||
}
|
||||
return new ParentAndClone<>(context.getDirectory(), target, target);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user