mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-01 21:29:47 +01:00
Implement first steps for merge
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class CloseableWrapper<C> implements AutoCloseable {
|
||||
|
||||
private final C wrapped;
|
||||
private final Consumer<C> cleanup;
|
||||
|
||||
public CloseableWrapper(C wrapped, Consumer<C> cleanup) {
|
||||
this.wrapped = wrapped;
|
||||
this.cleanup = cleanup;
|
||||
}
|
||||
|
||||
public C get() { return wrapped; }
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
cleanup.accept(wrapped);
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,8 @@ public class GitRepositoryHandler
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
private final Scheduler scheduler;
|
||||
|
||||
private final GitWorkdirPool workdirPool;
|
||||
|
||||
private Task task;
|
||||
|
||||
@@ -104,10 +106,11 @@ public class GitRepositoryHandler
|
||||
* @param scheduler
|
||||
*/
|
||||
@Inject
|
||||
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler)
|
||||
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler, GitWorkdirPool workdirPool)
|
||||
{
|
||||
super(storeFactory, fileSystem);
|
||||
this.scheduler = scheduler;
|
||||
this.workdirPool = workdirPool;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -235,4 +238,8 @@ public class GitRepositoryHandler
|
||||
{
|
||||
return new File(directory, DIRECTORY_REFS).exists();
|
||||
}
|
||||
|
||||
public GitWorkdirPool getWorkdirPool() {
|
||||
return workdirPool;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
/**
|
||||
* Config:
|
||||
*
|
||||
* 1. Overall and absolute maximum of temp work directories
|
||||
* 2. Maximum number of temp work directories pooled overall
|
||||
* 3. Maximum number of temp work directories pooled for one master repository
|
||||
*/
|
||||
public class GitWorkdirPool {
|
||||
|
||||
private final Random random = new Random();
|
||||
private final File poolDirectory;
|
||||
|
||||
public GitWorkdirPool() {
|
||||
this(new File(System.getProperty("java.io.tmpdir")));
|
||||
}
|
||||
|
||||
public GitWorkdirPool(File poolDirectory) {
|
||||
this.poolDirectory = poolDirectory;
|
||||
}
|
||||
|
||||
public CloseableWrapper<Repository> getWorkingCopy(File bareRepository) {
|
||||
try {
|
||||
Git clone = cloneRepository(bareRepository, new File(poolDirectory, Long.toString(random.nextLong())));
|
||||
return new CloseableWrapper<>(clone.getRepository(), r -> clone.close());
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException("could not clone working copy of repository", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected Git cloneRepository(File bareRepository, File target) throws GitAPIException {
|
||||
return Git.cloneRepository()
|
||||
.setURI(bareRepository.getAbsolutePath())
|
||||
.setDirectory(target)
|
||||
.call();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.eclipse.jgit.merge.MergeStrategy;
|
||||
import org.eclipse.jgit.merge.ResolveMerger;
|
||||
import sonia.scm.repository.CloseableWrapper;
|
||||
import sonia.scm.repository.GitWorkdirPool;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GitMergeCommand extends AbstractGitCommand implements MergeCommand {
|
||||
|
||||
private final GitWorkdirPool workdirPool;
|
||||
|
||||
GitMergeCommand(GitContext context, Repository repository, GitWorkdirPool workdirPool) {
|
||||
super(context, repository);
|
||||
this.workdirPool = workdirPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean merge(MergeCommandRequest request) {
|
||||
try (CloseableWrapper<org.eclipse.jgit.lib.Repository> workingCopy = workdirPool.getWorkingCopy(context.open().getDirectory())) {
|
||||
org.eclipse.jgit.lib.Repository repository = workingCopy.get();
|
||||
ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE.newMerger(repository);
|
||||
boolean mergeResult = merger.merge(repository.resolve(request.getBranchToMerge()), repository.resolve(request.getTargetBranch()));
|
||||
if (mergeResult) {
|
||||
// TODO push and verify push was successful
|
||||
}
|
||||
return mergeResult;
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,6 +245,12 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider
|
||||
public MergeDryRunCommand getMergeDryRunCommand() {
|
||||
return new GitMergeDryRunCommand(context, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MergeCommand getMergeCommand() {
|
||||
return new GitMergeCommand(context, repository, handler.getWorkdirPool());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
Reference in New Issue
Block a user