Update repositories in reclaim

This commit is contained in:
René Pfeuffer
2020-04-13 14:39:02 +02:00
parent 5b034f8d02
commit 7e89a0c1c0
4 changed files with 51 additions and 4 deletions

View File

@@ -86,7 +86,7 @@ public abstract class SimpleWorkdirFactory<R, W, C> implements WorkdirFactory<R,
protected abstract ParentAndClone<R, W> cloneRepository(C context, File target, String initialBranch) throws IOException; protected abstract ParentAndClone<R, W> cloneRepository(C context, File target, String initialBranch) throws IOException;
protected abstract ParentAndClone<R, W> reclaimRepository(C context, File target, String initialBranch) throws IOException; protected abstract ParentAndClone<R, W> reclaimRepository(C context, File target, String initialBranch) throws IOException, ReclaimFailedException;
private void closeCentral(R repository) { private void closeCentral(R repository) {
try { try {
@@ -129,5 +129,11 @@ public abstract class SimpleWorkdirFactory<R, W, C> implements WorkdirFactory<R,
} }
public static class ReclaimFailedException extends Exception { public static class ReclaimFailedException extends Exception {
public ReclaimFailedException() {
}
public ReclaimFailedException(Throwable cause) {
super(cause);
}
} }
} }

View File

@@ -25,11 +25,14 @@
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ScmTransportProtocol; import org.eclipse.jgit.transport.ScmTransportProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil; import sonia.scm.repository.GitUtil;
import sonia.scm.repository.GitWorkdirFactory; import sonia.scm.repository.GitWorkdirFactory;
import sonia.scm.repository.InternalRepositoryException; import sonia.scm.repository.InternalRepositoryException;
@@ -46,6 +49,8 @@ import static sonia.scm.NotFoundException.notFound;
public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, Repository, GitContext> implements GitWorkdirFactory { public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, Repository, GitContext> implements GitWorkdirFactory {
private static final Logger LOG = LoggerFactory.getLogger(SimpleGitWorkdirFactory.class);
@Inject @Inject
public SimpleGitWorkdirFactory(CacheSupportingWorkdirProvider workdirProvider) { public SimpleGitWorkdirFactory(CacheSupportingWorkdirProvider workdirProvider) {
super(workdirProvider); super(workdirProvider);
@@ -53,6 +58,8 @@ public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, Re
@Override @Override
public ParentAndClone<Repository, Repository> cloneRepository(GitContext context, File target, String initialBranch) { public ParentAndClone<Repository, Repository> cloneRepository(GitContext context, File target, String initialBranch) {
LOG.trace("clone repository {}", context.getRepository().getId());
long start = System.nanoTime();
try { try {
Repository clone = Git.cloneRepository() Repository clone = Git.cloneRepository()
.setURI(createScmTransportProtocolUri(context.getDirectory())) .setURI(createScmTransportProtocolUri(context.getDirectory()))
@@ -70,12 +77,33 @@ public class SimpleGitWorkdirFactory extends SimpleWorkdirFactory<Repository, Re
return new ParentAndClone<>(null, clone, target); return new ParentAndClone<>(null, clone, target);
} catch (GitAPIException | IOException e) { } catch (GitAPIException | IOException e) {
throw new InternalRepositoryException(context.getRepository(), "could not clone working copy of repository", 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 @Override
protected ParentAndClone<Repository, Repository> reclaimRepository(GitContext context, File target, String initialBranch) throws IOException { protected ParentAndClone<Repository, Repository> reclaimRepository(GitContext context, File target, String initialBranch) throws IOException, ReclaimFailedException {
return new ParentAndClone<>(null, GitUtil.open(target), target); LOG.trace("reclaim repository {}", context.getRepository().getId());
long start = System.nanoTime();
Repository repo = GitUtil.open(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.branchDelete().setBranchNames(initialBranch).setForce(true).call();
git.checkout().setForced(true).setName("origin/" + initialBranch).call();
git.checkout().setName(initialBranch).setCreateBranch(true).call();
return new ParentAndClone<>(null, repo, target);
} catch (GitAPIException 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 String createScmTransportProtocolUri(File bareRepository) { private String createScmTransportProtocolUri(File bareRepository) {

View File

@@ -28,6 +28,7 @@ import com.aragost.javahg.BaseRepository;
import com.aragost.javahg.Repository; import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.CloneCommand; import com.aragost.javahg.commands.CloneCommand;
import com.aragost.javahg.commands.PullCommand; import com.aragost.javahg.commands.PullCommand;
import com.aragost.javahg.commands.UpdateCommand;
import com.aragost.javahg.commands.flags.CloneCommandFlags; import com.aragost.javahg.commands.flags.CloneCommandFlags;
import sonia.scm.repository.util.CacheSupportingWorkdirProvider; import sonia.scm.repository.util.CacheSupportingWorkdirProvider;
import sonia.scm.repository.util.SimpleWorkdirFactory; import sonia.scm.repository.util.SimpleWorkdirFactory;
@@ -73,6 +74,7 @@ public class SimpleHgWorkdirFactory extends SimpleWorkdirFactory<Repository, Rep
protected ParentAndClone<Repository, Repository> reclaimRepository(HgCommandContext context, File target, String initialBranch) throws IOException { protected ParentAndClone<Repository, Repository> reclaimRepository(HgCommandContext context, File target, String initialBranch) throws IOException {
Repository centralRepository = openCentral(context); Repository centralRepository = openCentral(context);
BaseRepository clone = Repository.open(target); BaseRepository clone = Repository.open(target);
UpdateCommand.on(clone).rev(initialBranch).execute();
return new ParentAndClone<>(centralRepository, clone, target); return new ParentAndClone<>(centralRepository, clone, target);
} }

View File

@@ -24,8 +24,12 @@
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc2.SvnCheckout; import org.tmatesoft.svn.core.wc2.SvnCheckout;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory; import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnTarget; import org.tmatesoft.svn.core.wc2.SvnTarget;
@@ -78,7 +82,14 @@ public class SimpleSvnWorkDirFactory extends SimpleWorkdirFactory<File, File, Sv
} }
@Override @Override
protected ParentAndClone<File, File> reclaimRepository(SvnContext context, File target, String initialBranch) throws IOException { 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); return new ParentAndClone<>(context.getDirectory(), target, target);
} }