summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorTomasz Zarna2012-11-18 16:40:04 (EST)
committer Tomasz Zarna2013-01-08 15:52:43 (EST)
commitacb7fee2faa7b181ded810c6e5dc5371b428c473 (patch)
tree8b42f12e2bd8a92058519556d9d35a79539482d1
parent66626798f7929578847a42c77123ce0c689cfcef (diff)
downloadegit-acb7fee2faa7b181ded810c6e5dc5371b428c473.zip
egit-acb7fee2faa7b181ded810c6e5dc5371b428c473.tar.gz
egit-acb7fee2faa7b181ded810c6e5dc5371b428c473.tar.bz2
Support git --no-ff as an EGit preferencerefs/changes/54/8754/7
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java155
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/MergeOperation.java21
2 files changed, 173 insertions, 3 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..595aec7
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/MergeOperationTest.java
@@ -0,0 +1,155 @@
+/*******************************************************************************
+ * Copyright (C) 2012, 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,
+ FastForwardMode.MergeOptions.valueOf(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;
+ }
+}
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 045818b..ca7b32b 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
@@ -31,11 +31,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;
@@ -107,11 +109,12 @@ public class MergeOperation implements IEGitOperation {
mymonitor.worked(1);
MergeCommand merge;
try {
+ FastForwardMode ffmode = getFFmode();
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);
}
@@ -153,6 +156,18 @@ public class MergeOperation implements IEGitOperation {
ResourcesPlugin.getWorkspace().run(action, monitor);
}
+ private FastForwardMode getFFmode() throws IOException {
+ FastForwardMode ffmode = FastForwardMode.valueOf(repository.getConfig()
+ .getEnum(ConfigConstants.CONFIG_KEY_MERGE, null,
+ ConfigConstants.CONFIG_KEY_FF,
+ FastForwardMode.Merge.TRUE));
+ ffmode = FastForwardMode.valueOf(repository.getConfig().getEnum(
+ ConfigConstants.CONFIG_BRANCH_SECTION, repository.getBranch(),
+ ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
+ FastForwardMode.MergeOptions.valueOf(ffmode)));
+ return ffmode;
+ }
+
/**
* @return the merge result, or <code>null</code> if this has not been
* executed or if an exception occurred