mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 11:35:57 +01:00
merge with default branch
This commit is contained in:
@@ -81,19 +81,19 @@ public class GitGcTask implements Runnable {
|
||||
{
|
||||
if (repository.isValid() && repository.isHealthy())
|
||||
{
|
||||
logger.info("start git gc for repository {}", repository.getName());
|
||||
logger.info("start git gc for repository {}", repository.getNamespaceAndName());
|
||||
Stopwatch sw = Stopwatch.createStarted();
|
||||
gc(repository);
|
||||
logger.debug("gc of repository {} has finished after {}", repository.getName(), sw.stop());
|
||||
logger.debug("gc of repository {} has finished after {}", repository.getNamespaceAndName(), sw.stop());
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.debug("skip non valid/healthy repository {}", repository.getName());
|
||||
logger.debug("skip non valid/healthy repository {}", repository.getNamespaceAndName());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.trace("skip non git repository {}", repository.getName());
|
||||
logger.trace("skip non git repository {}", repository.getNamespaceAndName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.MergeResult;
|
||||
import org.eclipse.jgit.api.Status;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.IndexDiff;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectReader;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.merge.ResolveMerger;
|
||||
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
|
||||
import org.eclipse.jgit.treewalk.filter.PathFilter;
|
||||
import sonia.scm.repository.GitWorkdirFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.api.MergeCommandResult;
|
||||
@@ -10,10 +19,13 @@ import sonia.scm.repository.api.MergeDryRunCommandResult;
|
||||
import sonia.scm.repository.api.MergeStrategy;
|
||||
import sonia.scm.repository.api.MergeStrategyNotSupportedException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.eclipse.jgit.merge.MergeStrategy.RECURSIVE;
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
import static sonia.scm.NotFoundException.notFound;
|
||||
|
||||
public class GitMergeCommand extends AbstractGitCommand implements MergeCommand {
|
||||
|
||||
@@ -35,6 +47,11 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
||||
return mergeWithStrategy(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MergeConflictResult computeConflicts(MergeCommandRequest request) {
|
||||
return inClone(git -> new ConflictWorker(git, request), workdirFactory, request.getTargetBranch());
|
||||
}
|
||||
|
||||
private MergeCommandResult mergeWithStrategy(MergeCommandRequest request) {
|
||||
switch(request.getMergeStrategy()) {
|
||||
case SQUASH:
|
||||
@@ -75,4 +92,91 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
||||
return STRATEGIES;
|
||||
}
|
||||
|
||||
private class ConflictWorker extends GitCloneWorker<MergeConflictResult> {
|
||||
private final String theirs;
|
||||
private final String ours;
|
||||
private final CanonicalTreeParser treeParser;
|
||||
private final ObjectId treeId;
|
||||
private final ByteArrayOutputStream diffBuffer;
|
||||
|
||||
private final MergeConflictResult result = new MergeConflictResult();
|
||||
|
||||
|
||||
private ConflictWorker(Git git, MergeCommandRequest request) {
|
||||
super(git, context, repository);
|
||||
theirs = request.getBranchToMerge();
|
||||
ours = request.getTargetBranch();
|
||||
|
||||
treeParser = new CanonicalTreeParser();
|
||||
diffBuffer = new ByteArrayOutputStream();
|
||||
try {
|
||||
treeId = git.getRepository().resolve(ours + "^{tree}");
|
||||
} catch (IOException e) {
|
||||
throw notFound(entity("branch", ours).in(repository));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
MergeConflictResult run() throws IOException {
|
||||
MergeResult mergeResult = doTemporaryMerge();
|
||||
if (mergeResult.getConflicts() != null) {
|
||||
getStatus().getConflictingStageState().forEach(this::computeConflict);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void computeConflict(String path, IndexDiff.StageState stageState) {
|
||||
switch (stageState) {
|
||||
case BOTH_MODIFIED:
|
||||
diffBuffer.reset();
|
||||
try (ObjectReader reader = getClone().getRepository().newObjectReader()) {
|
||||
treeParser.reset(reader, treeId);
|
||||
getClone()
|
||||
.diff()
|
||||
.setOldTree(treeParser)
|
||||
.setPathFilter(PathFilter.create(path))
|
||||
.setOutputStream(diffBuffer)
|
||||
.call();
|
||||
result.addBothModified(path, diffBuffer.toString());
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new InternalRepositoryException(repository, "could not calculate diff for path " + path, e);
|
||||
}
|
||||
break;
|
||||
case BOTH_ADDED:
|
||||
result.addAddedByBoth(path);
|
||||
break;
|
||||
case DELETED_BY_THEM:
|
||||
result.addDeletedByUs(path);
|
||||
break;
|
||||
case DELETED_BY_US:
|
||||
result.addDeletedByThem(path);
|
||||
break;
|
||||
default:
|
||||
throw new InternalRepositoryException(context.getRepository(), "unexpected conflict type: " + stageState);
|
||||
}
|
||||
}
|
||||
|
||||
private MergeResult doTemporaryMerge() throws IOException {
|
||||
ObjectId sourceRevision = resolveRevision(theirs);
|
||||
try {
|
||||
return getClone().merge()
|
||||
.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.NO_FF)
|
||||
.setCommit(false)
|
||||
.include(theirs, sourceRevision)
|
||||
.call();
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not merge branch " + theirs + " into " + ours, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Status getStatus() {
|
||||
Status status;
|
||||
try {
|
||||
status = getClone().status().call();
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not get status", e);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,20 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
|
||||
"+++ b/f.txt\n" +
|
||||
"@@ -0,0 +1 @@\n" +
|
||||
"+f\n";
|
||||
public static final String DIFF_FILE_PARTIAL_MERGE = "diff --git a/a.txt b/a.txt\n" +
|
||||
"index 7898192..8cd63ec 100644\n" +
|
||||
"--- a/a.txt\n" +
|
||||
"+++ b/a.txt\n" +
|
||||
"@@ -1 +1,2 @@\n" +
|
||||
" a\n" +
|
||||
"+change\n" +
|
||||
"diff --git a/b.txt b/b.txt\n" +
|
||||
"index 6178079..09ccdf0 100644\n" +
|
||||
"--- a/b.txt\n" +
|
||||
"+++ b/b.txt\n" +
|
||||
"@@ -1 +1,2 @@\n" +
|
||||
" b\n" +
|
||||
"+change\n";
|
||||
|
||||
@Test
|
||||
public void diffForOneRevisionShouldCreateDiff() throws IOException {
|
||||
@@ -91,4 +105,15 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
|
||||
gitDiffCommand.getDiffResult(diffCommandRequest).accept(output);
|
||||
assertEquals(DIFF_FILE_A_MULTIPLE_REVISIONS, output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffBetweenTwoBranchesWithMergedIntegrationBranchShouldCreateDiffOfAllIncomingChanges() throws IOException {
|
||||
GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext(), repository);
|
||||
DiffCommandRequest diffCommandRequest = new DiffCommandRequest();
|
||||
diffCommandRequest.setRevision("partially_merged");
|
||||
diffCommandRequest.setAncestorChangeset("master");
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
gitDiffCommand.getDiffResult(diffCommandRequest).accept(output);
|
||||
assertEquals(DIFF_FILE_PARTIAL_MERGE, output.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import sonia.scm.repository.spi.MergeConflictResult.SingleMergeConflict;
|
||||
import sonia.scm.repository.util.WorkdirProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static sonia.scm.repository.spi.MergeConflictResult.ConflictTypes.BOTH_MODIFIED;
|
||||
import static sonia.scm.repository.spi.MergeConflictResult.ConflictTypes.DELETED_BY_THEM;
|
||||
import static sonia.scm.repository.spi.MergeConflictResult.ConflictTypes.DELETED_BY_US;
|
||||
|
||||
public class GitMergeCommand_Conflict_Test extends AbstractGitCommandTestBase {
|
||||
|
||||
static final String DIFF_HEADER = "diff --git a/Main.java b/Main.java";
|
||||
static final String DIFF_FILE_CONFLICT = "--- a/Main.java\n" +
|
||||
"+++ b/Main.java\n" +
|
||||
"@@ -1,6 +1,13 @@\n" +
|
||||
"+import java.util.Arrays;\n" +
|
||||
"+\n" +
|
||||
" class Main {\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" System.out.println(\"Expect nothing more to happen.\");\n" +
|
||||
"+<<<<<<< HEAD\n" +
|
||||
" System.out.println(\"This is for demonstration, only.\");\n" +
|
||||
"+=======\n" +
|
||||
"+ System.out.println(\"Parameters:\");\n" +
|
||||
"+ Arrays.stream(args).map(arg -> \"- \" + arg).forEach(System.out::println);\n" +
|
||||
"+>>>>>>> feature/print_args\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
|
||||
@Rule
|
||||
public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule();
|
||||
|
||||
@Test
|
||||
public void diffBetweenTwoBranchesWithoutConflict() throws IOException {
|
||||
MergeConflictResult result = computeMergeConflictResult("feature/rename_variable", "integration");
|
||||
assertThat(result.getConflicts()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffBetweenTwoBranchesWithSimpleConflict() throws IOException {
|
||||
MergeConflictResult result = computeMergeConflictResult("feature/print_args", "integration");
|
||||
SingleMergeConflict conflict = result.getConflicts().get(0);
|
||||
assertThat(conflict.getType()).isEqualTo(BOTH_MODIFIED);
|
||||
assertThat(conflict.getPath()).isEqualTo("Main.java");
|
||||
assertThat(conflict.getDiff()).contains(DIFF_HEADER, DIFF_FILE_CONFLICT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffBetweenTwoBranchesWithDeletedByUs() throws IOException {
|
||||
MergeConflictResult result = computeMergeConflictResult("feature/remove_class", "integration");
|
||||
SingleMergeConflict conflict = result.getConflicts().get(0);
|
||||
assertThat(conflict.getType()).isEqualTo(DELETED_BY_US);
|
||||
assertThat(conflict.getPath()).isEqualTo("Main.java");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffBetweenTwoBranchesWithDeletedByThem() throws IOException {
|
||||
MergeConflictResult result = computeMergeConflictResult("integration", "feature/remove_class");
|
||||
SingleMergeConflict conflict = result.getConflicts().get(0);
|
||||
assertThat(conflict.getType()).isEqualTo(DELETED_BY_THEM);
|
||||
assertThat(conflict.getPath()).isEqualTo("Main.java");
|
||||
}
|
||||
|
||||
private MergeConflictResult computeMergeConflictResult(String branchToMerge, String targetBranch) {
|
||||
GitMergeCommand gitMergeCommand = new GitMergeCommand(createContext(), repository, new SimpleGitWorkdirFactory(new WorkdirProvider()));
|
||||
MergeCommandRequest mergeCommandRequest = new MergeCommandRequest();
|
||||
mergeCommandRequest.setBranchToMerge(branchToMerge);
|
||||
mergeCommandRequest.setTargetBranch(targetBranch);
|
||||
return gitMergeCommand.computeConflicts(mergeCommandRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getZippedRepositoryResource() {
|
||||
return "sonia/scm/repository/spi/scm-git-spi-merge-diff-test.zip";
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user