summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorTomasz Zarna2013-01-21 06:55:38 (EST)
committer Tomasz Zarna2013-01-22 06:01:27 (EST)
commit0741a9849857c828cda7733d2d1bd8534ec68224 (patch)
tree1be79fabe9c62b691a80199728dbe62d7cabd02d
parent49297286b5e064a12f27226974c20f13a6e461d4 (diff)
downloadegit-0741a9849857c828cda7733d2d1bd8534ec68224.zip
egit-0741a9849857c828cda7733d2d1bd8534ec68224.tar.gz
egit-0741a9849857c828cda7733d2d1bd8534ec68224.tar.bz2
Support git --no-ff as an EGit preferencerefs/changes/00/9800/2
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java154
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java24
2 files changed, 173 insertions, 5 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java
new file mode 100644
index 0000000..6af4cd6
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java
@@ -0,0 +1,154 @@
+/*******************************************************************************
+ * Copyright (C) 2013, Tomasz Zarna <tomasz.zarna@gmail.com>
+ *
+ * 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.op;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.egit.core.op.MergeOperation;
+import org.eclipse.egit.core.test.GitTestCase;
+import org.eclipse.egit.core.test.TestRepository;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.LogCommand;
+import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.ConfigConstants;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MergeOperationTest 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 RevCommit secondCommit;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ 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);
+ testRepository.appendFileContent(file1, "file1-2");
+ secondCommit = testRepository.addAndCommit(project.getProject(), file1,
+ "master commit 2");
+ testRepository.checkoutBranch(SIDE);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ testRepository.dispose();
+ super.tearDown();
+ }
+
+ @Test
+ public void testMergeFF() throws Exception {
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), MASTER);
+ operation.execute(new NullProgressMonitor());
+
+ assertTrue(testRepository.getRepository().resolve(SIDE).equals(secondCommit));
+ assertEquals(2, countCommitsInHead());
+ }
+
+ @Test
+ public void testMergeoptionsNoFF() throws Exception {
+ setMergeOptions("side", FastForwardMode.NO_FF);
+
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), MASTER);
+ operation.execute(new NullProgressMonitor());
+
+ assertEquals(3, countCommitsInHead());
+ }
+
+ @Test
+ public void testMergeoptionsFFOnly() throws Exception {
+ setMergeOptions("side", FastForwardMode.FF_ONLY);
+ File file2 = testRepository.createFile(project.getProject(), "file2");
+ testRepository.appendFileContent(file2, "file2-1");
+ RevCommit commit = testRepository.addAndCommit(project.getProject(), file2,
+ "side commit 1");
+
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), MASTER);
+ operation.execute(new NullProgressMonitor());
+
+ assertTrue(testRepository.getRepository().resolve(SIDE).equals(commit));
+ }
+
+ private void setMergeOptions(String branch, FastForwardMode ffMode)
+ throws IOException {
+ StoredConfig config = testRepository.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
+ ConfigConstants.CONFIG_KEY_MERGEOPTIONS, ffMode);
+ config.save();
+ }
+
+ @Test
+ public void testMergeNoFF() throws Exception {
+ setMerge(FastForwardMode.NO_FF);
+
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), MASTER);
+ operation.execute(new NullProgressMonitor());
+
+ assertEquals(3, countCommitsInHead());
+ }
+
+ @Test
+ public void testMergeFFOnly() throws Exception {
+ setMerge(FastForwardMode.FF_ONLY);
+ File file2 = testRepository.createFile(project.getProject(), "file2");
+ testRepository.appendFileContent(file2, "file2-1");
+ RevCommit commit = testRepository.addAndCommit(project.getProject(), file2,
+ "side commit 1");
+
+ MergeOperation operation = new MergeOperation(
+ testRepository.getRepository(), MASTER);
+ operation.execute(new NullProgressMonitor());
+
+ assertTrue(testRepository.getRepository().resolve(SIDE).equals(commit));
+ }
+
+ private void setMerge(FastForwardMode ffMode) throws IOException {
+ StoredConfig config = testRepository.getRepository().getConfig();
+ config.setEnum(ConfigConstants.CONFIG_KEY_MERGE, null,
+ ConfigConstants.CONFIG_KEY_FF,
+ FastForwardMode.Merge.valueOf(ffMode));
+ config.save();
+ }
+
+ private int countCommitsInHead() throws GitAPIException {
+ LogCommand log = new Git(testRepository.getRepository()).log();
+ Iterable<RevCommit> commits = log.call();
+ int result = 0;
+ for (Iterator i = commits.iterator(); i.hasNext();) {
+ i.next();
+ result++;
+ }
+ return result;
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java
index 0a60886..06ac4a5 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2010 SAP AG.
- * Copyright (C) 2012, Tomasz Zarna <Tomasz.Zarna@pl.ibm.com>
+ * Copyright (C) 2012, 2013 Tomasz Zarna <tzarna@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -30,11 +30,13 @@ import org.eclipse.egit.core.CoreText;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand;
+import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoHeadException;
+import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
@@ -106,11 +108,14 @@ public class MergeOperation implements IEGitOperation {
mymonitor.worked(1);
MergeCommand merge;
try {
+ FastForwardMode ffmode = getFastForwardMode();
Ref ref = repository.getRef(refName);
if (ref != null)
- merge = git.merge().include(ref);
+ merge = git.merge().include(ref).setFastForward(ffmode);
else
- merge = git.merge().include(ObjectId.fromString(refName));
+ merge = git.merge()
+ .include(ObjectId.fromString(refName))
+ .setFastForward(ffmode);
} catch (IOException e) {
throw new TeamException(CoreText.MergeOperation_InternalError, e);
}
@@ -143,6 +148,17 @@ public class MergeOperation implements IEGitOperation {
ResourcesPlugin.getWorkspace().run(action, monitor);
}
+ private FastForwardMode getFastForwardMode() throws IOException {
+ FastForwardMode ffmode = FastForwardMode.valueOf(repository.getConfig()
+ .getEnum(ConfigConstants.CONFIG_KEY_MERGE, null,
+ ConfigConstants.CONFIG_KEY_FF,
+ FastForwardMode.Merge.TRUE));
+ ffmode = repository.getConfig().getEnum(
+ ConfigConstants.CONFIG_BRANCH_SECTION, repository.getBranch(),
+ ConfigConstants.CONFIG_KEY_MERGEOPTIONS, ffmode);
+ return ffmode;
+ }
+
/**
* @return the merge result, or <code>null</code> if this has not been
* executed or if an exception occurred
@@ -154,6 +170,4 @@ public class MergeOperation implements IEGitOperation {
public ISchedulingRule getSchedulingRule() {
return ResourcesPlugin.getWorkspace().getRoot();
}
-
-
}