Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2014-08-24 12:43:29 +0000
committerMatthias Sohn2014-12-12 23:14:15 +0000
commit8aa7103692227c281f2a3c046cf0bd0a2e8aec1f (patch)
tree5757188e8d56cfba7b0b1d3a8e22e7e1b3af8419
parent5346986a7e4037a10e69edc5cad813fd3fdaaae1 (diff)
downloadegit-8aa7103692227c281f2a3c046cf0bd0a2e8aec1f.tar.gz
egit-8aa7103692227c281f2a3c046cf0bd0a2e8aec1f.tar.xz
egit-8aa7103692227c281f2a3c046cf0bd0a2e8aec1f.zip
Support checking out stage with DiscardChangesOperation
For resolving conflicts, we want to be able to do `git checkout --ours` or `git checkout --theirs` for a path. Change-Id: Ia6d003993ad1016602ff960203b3526cd884bad2 Signed-off-by: Robin Stocker <robin@nibor.org> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java43
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/DiscardChangesOperation.java44
2 files changed, 86 insertions, 1 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java
index fa460622a6..34a0f56ba7 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java
@@ -19,12 +19,18 @@ import java.util.Arrays;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.op.DiscardChangesOperation;
+import org.eclipse.egit.core.op.DiscardChangesOperation.Stage;
import org.eclipse.egit.core.test.DualRepositoryTestCase;
import org.eclipse.egit.core.test.TestRepository;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.util.IO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -115,6 +121,43 @@ public class DiscardChangesOperationTest extends DualRepositoryTestCase {
}
@Test
+ public void testDiscardChangesWithStage() throws Exception {
+ Git git = Git.wrap(repository1.getRepository());
+ File file = new File(repository1.getRepository().getWorkTree(),
+ "conflict.txt");
+ repository1.appendFileContent(file, "base", false);
+ git.add().addFilepattern("conflict.txt").call();
+ git.commit().setMessage("commit").call();
+
+ git.checkout().setCreateBranch(true).setName("side").call();
+ repository1.appendFileContent(file, "side", false);
+ git.add().addFilepattern("conflict.txt").call();
+ RevCommit side = git.commit().setMessage("commit on side").call();
+
+ git.checkout().setName("master").call();
+ repository1.appendFileContent(file, "master", false);
+ git.add().addFilepattern("conflict.txt").call();
+ git.commit().setMessage("commit on master").call();
+
+ git.merge().include(side).call();
+
+ DirCache dirCache = repository1.getRepository().readDirCache();
+ assertEquals(1, dirCache.getEntry("conflict.txt").getStage());
+
+ IPath path = new Path(file.getAbsolutePath());
+ DiscardChangesOperation operation = new DiscardChangesOperation(
+ Arrays.asList(path));
+ operation.setStage(Stage.THEIRS);
+ operation.execute(new NullProgressMonitor());
+
+ DirCache dirCacheAfter = repository1.getRepository().readDirCache();
+ assertEquals("Expected index to be unmodified", 1, dirCacheAfter
+ .getEntry("conflict.txt").getStage());
+
+ assertEquals("side", new String(IO.readFully(file), "UTF-8"));
+ }
+
+ @Test
public void shouldWorkWhenProjectIsRootOfRepository() throws Exception {
IFile file = project2.getFile(new Path("file.txt"));
String contents = testUtils.slurpAndClose(file.getContents());
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/DiscardChangesOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/DiscardChangesOperation.java
index f40e59c44e..b5eeac661b 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/DiscardChangesOperation.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/DiscardChangesOperation.java
@@ -50,6 +50,32 @@ public class DiscardChangesOperation implements IEGitOperation {
private String revision;
+ private Stage stage;
+
+ /**
+ * The index stage to check out for conflicting files.
+ */
+ public enum Stage {
+ /**
+ * "base" stage
+ */
+ BASE(CheckoutCommand.Stage.BASE),
+ /**
+ * "ours" stage
+ */
+ OURS(CheckoutCommand.Stage.OURS),
+ /**
+ * "theirs" stage
+ */
+ THEIRS(CheckoutCommand.Stage.THEIRS);
+
+ private CheckoutCommand.Stage checkoutStage;
+
+ private Stage(CheckoutCommand.Stage checkoutStage) {
+ this.checkoutStage = checkoutStage;
+ }
+ }
+
/**
* Construct a {@link DiscardChangesOperation} object.
*
@@ -86,6 +112,19 @@ public class DiscardChangesOperation implements IEGitOperation {
.keySet());
}
+ /**
+ * Set the index stage to check out for conflicting files. Not compatible
+ * with revision.
+ *
+ * @param stage
+ */
+ public void setStage(Stage stage) {
+ if (revision != null)
+ throw new IllegalStateException(
+ "Either stage or revision can be set, but not both"); //$NON-NLS-1$
+ this.stage = stage;
+ }
+
/*
* (non-Javadoc)
*
@@ -151,7 +190,10 @@ public class DiscardChangesOperation implements IEGitOperation {
throws GitAPIException {
ResourceUtil.saveLocalHistory(repository);
CheckoutCommand checkoutCommand = new Git(repository).checkout();
- checkoutCommand.setStartPoint(this.revision);
+ if (revision != null)
+ checkoutCommand.setStartPoint(revision);
+ if (stage != null)
+ checkoutCommand.setStage(stage.checkoutStage);
if (paths.isEmpty() || paths.contains("")) //$NON-NLS-1$
checkoutCommand.setAllPaths(true);
else {

Back to the top