Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Delaigue2015-08-26 14:39:05 +0000
committerMatthias Sohn2015-08-30 21:24:13 +0000
commit99cdec0cd3cce94f4ceb69364c715dfa81ee0edb (patch)
tree5fd22f25b4c9c4d787f0bfd05cb48ee6563f77c3 /org.eclipse.egit.core.test
parent3a6a38c067ef807fe3898367c891029cf3611e17 (diff)
downloadegit-99cdec0cd3cce94f4ceb69364c715dfa81ee0edb.tar.gz
egit-99cdec0cd3cce94f4ceb69364c715dfa81ee0edb.tar.xz
egit-99cdec0cd3cce94f4ceb69364c715dfa81ee0edb.zip
Introduce a mergeStrategy extension point.
The new extension point can be used to implement and register custom merge strategies which can be used by EGit operations that require merge (merge, rebase, pull, revert, squash, stash, submodule update). Add a preference page to allow selection of the preferred merge strategy. Make sure that relevant operations use the preferred strategy. Bug: 418151 Change-Id: I4d455ce171cca13c780e72b8d27dd929b28c6b71 Signed-off-by: Laurent Delaigue <laurent.delaigue@obeo.fr> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.egit.core.test')
-rw-r--r--org.eclipse.egit.core.test/fragment.xml11
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/OursMergeStrategyExtensionFactory.java18
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/TheirsMergeStrategyExtensionFactory.java18
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/MergeWithPreferredStrategyTest.java151
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/PreferredMergeStrategyTest.java78
5 files changed, 276 insertions, 0 deletions
diff --git a/org.eclipse.egit.core.test/fragment.xml b/org.eclipse.egit.core.test/fragment.xml
index e1f93e1006..afd57f9c27 100644
--- a/org.eclipse.egit.core.test/fragment.xml
+++ b/org.eclipse.egit.core.test/fragment.xml
@@ -34,5 +34,16 @@
</adapter>
</factory>
</extension>
+ <extension
+ point="org.eclipse.egit.core.mergeStrategy">
+ <strategy
+ class="org.eclipse.egit.core.mergestrategy.OursMergeStrategyExtensionFactory"
+ label="Strategy OURS (used for tests)"/>
+ <strategy
+ class="org.eclipse.egit.core.mergestrategy.TheirsMergeStrategyExtensionFactory"
+ label="Strategy THEIRS with explicit name (used for tests)"
+ name="org.eclipse.egit.core.test.theirs">
+ </strategy>
+ </extension>
</fragment>
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/OursMergeStrategyExtensionFactory.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/OursMergeStrategyExtensionFactory.java
new file mode 100644
index 0000000000..4a10046ab7
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/OursMergeStrategyExtensionFactory.java
@@ -0,0 +1,18 @@
+package org.eclipse.egit.core.mergestrategy;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IExecutableExtensionFactory;
+import org.eclipse.jgit.merge.MergeStrategy;
+
+/**
+ * This is used to provide the "OURS" MergeStrategy as a registered
+ * MergeStrategy.
+ */
+public class OursMergeStrategyExtensionFactory implements
+ IExecutableExtensionFactory {
+
+ @Override
+ public Object create() throws CoreException {
+ return MergeStrategy.OURS;
+ }
+}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/TheirsMergeStrategyExtensionFactory.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/TheirsMergeStrategyExtensionFactory.java
new file mode 100644
index 0000000000..ebe0563048
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/mergestrategy/TheirsMergeStrategyExtensionFactory.java
@@ -0,0 +1,18 @@
+package org.eclipse.egit.core.mergestrategy;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IExecutableExtensionFactory;
+import org.eclipse.jgit.merge.MergeStrategy;
+
+/**
+ * This is used to provide the "OURS" MergeStrategy as a registered
+ * MergeStrategy.
+ */
+public class TheirsMergeStrategyExtensionFactory implements
+ IExecutableExtensionFactory {
+
+ @Override
+ public Object create() throws CoreException {
+ return MergeStrategy.THEIRS;
+ }
+}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/MergeWithPreferredStrategyTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/MergeWithPreferredStrategyTest.java
new file mode 100644
index 0000000000..866f5431e0
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/MergeWithPreferredStrategyTest.java
@@ -0,0 +1,151 @@
+/*******************************************************************************
+ * Copyright (C) 2015 Obeo and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.egit.core.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.util.Iterator;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.core.GitCorePreferences;
+import org.eclipse.egit.core.op.MergeOperation;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.LogCommand;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.merge.MergeStrategy;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * This makes sure that the preferred merge strategy chosen bu the user ni the
+ * preferences is actually used by MergeOperations to do its job.
+ */
+public class MergeWithPreferredStrategyTest extends GitTestCase {
+
+ private static final String MASTER = Constants.R_HEADS + Constants.MASTER;
+
+ private static final String SIDE = Constants.R_HEADS + "side";
+
+ private TestRepository testRepository;
+
+ private File file2;
+
+ private File file3;
+
+ /**
+ * Sets the preference "preferred merge strategy" to "ours", so that the
+ * MergeStrategy that will be used by any operation that needs a merge will
+ * be the {@link MergeStrategy#OURS}.
+ */
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).put(
+ GitCorePreferences.core_preferredMergeStrategy, "ours");
+ gitDir = new File(project.getProject().getLocationURI().getPath(),
+ Constants.DOT_GIT);
+ testRepository = new TestRepository(gitDir);
+ testRepository.connect(project.getProject());
+
+ File file1 = testRepository.createFile(project.getProject(), "file1-1");
+ testRepository.addAndCommit(project.getProject(), file1,
+ "master commit 1");
+ testRepository.createBranch(MASTER, SIDE);
+ file2 = testRepository.createFile(project.getProject(), "file2");
+ testRepository.appendFileContent(file2, "Content 2");
+ testRepository.addAndCommit(project.getProject(), file2,
+ "master commit 2");
+ testRepository.checkoutBranch(SIDE);
+ file3 = testRepository.createFile(project.getProject(), "file3");
+ testRepository.appendFileContent(file3, "Content 3");
+ testRepository.addAndCommit(project.getProject(), file3,
+ "side commit 3");
+ testRepository.checkoutBranch(MASTER);
+ }
+
+ /**
+ * Removes any preference about preferred merge strategy after any test.
+ */
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).remove(
+ GitCorePreferences.core_preferredMergeStrategy);
+ }
+
+ @Test
+ public void testMergeUsesPreferredStrategy() throws Exception {
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), SIDE);
+ operation.execute(new NullProgressMonitor());
+
+ // With the MergeStrategy OURS, new files from branch 'side' should be
+ // ignored and new files from branch 'master' should be present
+ assertEquals(4, countCommitsInHead());
+ assertTrue(testRepository.getIFile(project.getProject(), file2)
+ .exists());
+ assertFalse(testRepository.getIFile(project.getProject(), file3)
+ .exists());
+ }
+
+ @Test
+ public void testMergeUsesPreferredStrategyRegisteredWithExplicitName()
+ throws Exception {
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).put(
+ GitCorePreferences.core_preferredMergeStrategy,
+ "org.eclipse.egit.core.test.theirs");
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), SIDE);
+ operation.execute(new NullProgressMonitor());
+
+ // With the MergeStrategy THEIRS, new files from branch 'master' should
+ // be ignored and new files from branch 'side' should be present
+ assertEquals(4, countCommitsInHead());
+ assertFalse(testRepository.getIFile(project.getProject(), file2)
+ .exists());
+ assertTrue(testRepository.getIFile(project.getProject(), file3)
+ .exists());
+ }
+
+ @Test
+ public void testStrategyCanBeOverridden() throws Exception {
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), SIDE, "recursive");
+ operation.execute(new NullProgressMonitor());
+
+ // With the MergeStrategy RECURSIVE, new files from branch 'master' and
+ // 'side' should be present
+ assertEquals(4, countCommitsInHead());
+ assertTrue(testRepository.getIFile(project.getProject(), file2)
+ .exists());
+ assertTrue(
+ testRepository.getIFile(project.getProject(), file3).exists());
+ }
+
+ private int countCommitsInHead() throws GitAPIException {
+ try (Git git = new Git(testRepository.getRepository())) {
+ LogCommand log = git.log();
+ Iterable<RevCommit> commits = log.call();
+ int result = 0;
+ for (Iterator i = commits.iterator(); i.hasNext();) {
+ i.next();
+ result++;
+ }
+ return result;
+ }
+ }
+}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/PreferredMergeStrategyTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/PreferredMergeStrategyTest.java
new file mode 100644
index 0000000000..56c7be06c4
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/PreferredMergeStrategyTest.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * Copyright (C) 2015 Obeo and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.egit.core.test;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.core.GitCorePreferences;
+import org.eclipse.jgit.merge.MergeStrategy;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PreferredMergeStrategyTest {
+
+ private Activator a;
+
+ /**
+ * Removes any preference about preferred merge strategy before any test.
+ */
+ @Before
+ public void setUp() {
+ a = Activator.getDefault();
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).remove(
+ GitCorePreferences.core_preferredMergeStrategy);
+ }
+
+ /**
+ * Removes any preference about preferred merge strategy after any test.
+ */
+ @After
+ public void tearDown() {
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).remove(
+ GitCorePreferences.core_preferredMergeStrategy);
+ }
+
+ @Test
+ public void testGetDefaultPreferredMergeStrategy() {
+ assertNull(a.getPreferredMergeStrategy());
+ }
+
+ @Test
+ public void testGetPreferredMergeStrategyWhenNoPref() {
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).remove(
+ GitCorePreferences.core_preferredMergeStrategy);
+
+ assertNull(a.getPreferredMergeStrategy());
+ }
+
+ @Test
+ public void testGetPreferredMergeStrategyWhenInvalidPreference() {
+ // Using "invalid value" simulates a property set to a merge strategy
+ // that's no longer registered
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId())
+ .put(GitCorePreferences.core_preferredMergeStrategy,
+ "invalid value");
+
+ assertNull(a.getPreferredMergeStrategy());
+ }
+
+ @Test
+ public void testGetPreferredMergeStrategyWhenValidPreference() {
+ // Using "resolve" here because there's no need for more configuration
+ // in this test, it is a registered MergeStrategy
+ InstanceScope.INSTANCE.getNode(Activator.getPluginId()).put(
+ GitCorePreferences.core_preferredMergeStrategy, "resolve");
+
+ assertSame(MergeStrategy.RESOLVE, a.getPreferredMergeStrategy());
+ }
+}

Back to the top