Fix merge direction

This commit is contained in:
Rene Pfeuffer
2019-11-11 16:15:59 +01:00
parent cc7ef47153
commit 6b9aabd3ff
2 changed files with 20 additions and 22 deletions

View File

@@ -49,7 +49,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
@Override @Override
public MergeConflictResult computeConflicts(MergeCommandRequest request) { public MergeConflictResult computeConflicts(MergeCommandRequest request) {
return inClone(git -> new ConflictWorker(git, request), workdirFactory, request.getTargetBranch()); return inClone(git -> new ConflictWorker(git, request), workdirFactory, request.getBranchToMerge());
} }
private MergeCommandResult mergeWithStrategy(MergeCommandRequest request) { private MergeCommandResult mergeWithStrategy(MergeCommandRequest request) {
@@ -93,8 +93,8 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
} }
private class ConflictWorker extends GitCloneWorker<MergeConflictResult> { private class ConflictWorker extends GitCloneWorker<MergeConflictResult> {
private final String branchToMerge; private final String theirs;
private final String targetBranch; private final String ours;
private final CanonicalTreeParser treeParser; private final CanonicalTreeParser treeParser;
private final ObjectId treeId; private final ObjectId treeId;
private final ByteArrayOutputStream diffBuffer; private final ByteArrayOutputStream diffBuffer;
@@ -104,15 +104,15 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
private ConflictWorker(Git git, MergeCommandRequest request) { private ConflictWorker(Git git, MergeCommandRequest request) {
super(git, context, repository); super(git, context, repository);
branchToMerge = request.getBranchToMerge(); theirs = request.getTargetBranch();
targetBranch = request.getTargetBranch(); ours = request.getBranchToMerge();
treeParser = new CanonicalTreeParser(); treeParser = new CanonicalTreeParser();
diffBuffer = new ByteArrayOutputStream(); diffBuffer = new ByteArrayOutputStream();
try { try {
treeId = git.getRepository().resolve(request.getTargetBranch() + "^{tree}"); treeId = git.getRepository().resolve(ours + "^{tree}");
} catch (IOException e) { } catch (IOException e) {
throw notFound(entity("branch", request.getTargetBranch()).in(repository)); throw notFound(entity("branch", ours).in(repository));
} }
} }
@@ -154,15 +154,15 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
} }
private MergeResult doTemporaryMerge() throws IOException { private MergeResult doTemporaryMerge() throws IOException {
ObjectId sourceRevision = resolveRevision(branchToMerge); ObjectId sourceRevision = resolveRevision(theirs);
try { try {
return getClone().merge() return getClone().merge()
.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.NO_FF) .setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.NO_FF)
.setCommit(false) .setCommit(false)
.include(branchToMerge, sourceRevision) .include(theirs, sourceRevision)
.call(); .call();
} catch (GitAPIException e) { } catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not merge branch " + branchToMerge + " into " + targetBranch, e); throw new InternalRepositoryException(context.getRepository(), "could not merge branch " + theirs + " into " + ours, e);
} }
} }

View File

@@ -17,20 +17,18 @@ public class GitMergeCommand_Conflict_Test extends AbstractGitCommandTestBase {
static final String DIFF_HEADER = "diff --git a/Main.java b/Main.java"; static final String DIFF_HEADER = "diff --git a/Main.java b/Main.java";
static final String DIFF_FILE_CONFLICT = "--- a/Main.java\n" + static final String DIFF_FILE_CONFLICT = "--- a/Main.java\n" +
"+++ b/Main.java\n" + "+++ b/Main.java\n" +
"@@ -1,6 +1,13 @@\n" + "@@ -3,7 +3,11 @@\n" +
"+import java.util.Arrays;\n" +
"+\n" +
" class Main {\n" + " class Main {\n" +
" public static void main(String[] args) {\n" + " public static void main(String[] args) {\n" +
" System.out.println(\"Expect nothing more to happen.\");\n" + " System.out.println(\"Expect nothing more to happen.\");\n" +
"+<<<<<<< HEAD\n" + "+<<<<<<< HEAD\n" +
" System.out.println(\"This is for demonstration, only.\");\n" + " System.out.println(\"Parameters:\");\n" +
" Arrays.stream(args).map(arg -> \"- \" + arg).forEach(System.out::println);\n" +
"+=======\n" + "+=======\n" +
"+ System.out.println(\"Parameters:\");\n" + "+ System.out.println(\"This is for demonstration, only.\");\n" +
"+ Arrays.stream(args).map(arg -> \"- \" + arg).forEach(System.out::println);\n" + "+>>>>>>> integration\n" +
"+>>>>>>> feature/print_args\n" +
" }\n" + " }\n" +
" }\n"; " }";
@Rule @Rule
public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule(); public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule();
@@ -51,18 +49,18 @@ public class GitMergeCommand_Conflict_Test extends AbstractGitCommandTestBase {
} }
@Test @Test
public void diffBetweenTwoBranchesWithDeletedByThem() throws IOException { public void diffBetweenTwoBranchesWithDeletedByUs() throws IOException {
MergeConflictResult result = computeMergeConflictResult("feature/remove_class", "integration"); MergeConflictResult result = computeMergeConflictResult("feature/remove_class", "integration");
SingleMergeConflict conflict = result.getConflicts().get(0); SingleMergeConflict conflict = result.getConflicts().get(0);
assertThat(conflict.getType()).isEqualTo(DELETED_BY_THEM); assertThat(conflict.getType()).isEqualTo(DELETED_BY_US);
assertThat(conflict.getPath()).isEqualTo("Main.java"); assertThat(conflict.getPath()).isEqualTo("Main.java");
} }
@Test @Test
public void diffBetweenTwoBranchesWithDeletedByUs() throws IOException { public void diffBetweenTwoBranchesWithDeletedByThem() throws IOException {
MergeConflictResult result = computeMergeConflictResult("integration", "feature/remove_class"); MergeConflictResult result = computeMergeConflictResult("integration", "feature/remove_class");
SingleMergeConflict conflict = result.getConflicts().get(0); SingleMergeConflict conflict = result.getConflicts().get(0);
assertThat(conflict.getType()).isEqualTo(DELETED_BY_US); assertThat(conflict.getType()).isEqualTo(DELETED_BY_THEM);
assertThat(conflict.getPath()).isEqualTo("Main.java"); assertThat(conflict.getPath()).isEqualTo("Main.java");
} }