summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorRobin Stocker2012-10-22 05:14:40 (EDT)
committer Robin Stocker2012-10-22 05:14:40 (EDT)
commitad52ec5207f5c7ee1736e3a77bc45ad61baf3b71 (patch)
tree9a7f0e76c890fae4a899de38af199ef1436e7437
parent1ff82ca6d0137eee1ecbbf20c96390c872691f7c (diff)
downloadjgit-ad52ec5207f5c7ee1736e3a77bc45ad61baf3b71.zip
jgit-ad52ec5207f5c7ee1736e3a77bc45ad61baf3b71.tar.gz
jgit-ad52ec5207f5c7ee1736e3a77bc45ad61baf3b71.tar.bz2
StashCreateCommand: Abort in case of unmerged pathsrefs/changes/97/8297/2
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java11
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java35
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java9
4 files changed, 61 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java
index 2b01b60..d481390 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java
@@ -52,6 +52,7 @@ import java.io.File;
import java.io.IOException;
import java.util.List;
+import org.eclipse.jgit.api.errors.UnmergedPathsException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
@@ -417,4 +418,14 @@ public class StashCreateCommandTest extends RepositoryTestCase {
assertEquals(who, entry.getWho());
assertEquals(stashed.getFullMessage(), entry.getComment());
}
+
+ @Test(expected = UnmergedPathsException.class)
+ public void unmergedPathsShouldCauseException() throws Exception {
+ commitFile("file.txt", "master", "base");
+ RevCommit side = commitFile("file.txt", "side", "side");
+ commitFile("file.txt", "master", "master");
+ git.merge().include(side).call();
+
+ git.stashCreate().call();
+ }
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
index dd03427..60b9462 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
@@ -58,6 +58,8 @@ import java.io.Reader;
import java.util.Map;
import java.util.TreeSet;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.dircache.DirCacheCheckout;
@@ -436,4 +438,37 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
}
return f;
}
+
+ /**
+ * Commit a file with the specified contents on the specified branch,
+ * creating the branch if it didn't exist before.
+ * <p>
+ * It switches back to the original branch after the commit if there was
+ * one.
+ *
+ * @param filename
+ * @param contents
+ * @param branch
+ * @return the created commit
+ */
+ protected RevCommit commitFile(String filename, String contents, String branch) {
+ try {
+ Git git = new Git(db);
+ String originalBranch = git.getRepository().getFullBranch();
+ if (git.getRepository().getRef(branch) == null)
+ git.branchCreate().setName(branch).call();
+ git.checkout().setName(branch).call();
+ writeTrashFile(filename, contents);
+ git.add().addFilepattern(filename).call();
+ RevCommit commit = git.commit()
+ .setMessage(branch + ": " + filename).call();
+ if (originalBranch != null)
+ git.checkout().setName(originalBranch).call();
+ return commit;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ } catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
index 4c7ae6a..375dee0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
@@ -52,12 +52,14 @@ import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.NoHeadException;
+import org.eclipse.jgit.api.errors.UnmergedPathsException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEditor;
import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator;
+import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
@@ -247,6 +249,10 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
WorkingTreeIterator wtIter = treeWalk.getTree(2,
WorkingTreeIterator.class);
if (headIter != null && indexIter != null && wtIter != null) {
+ if (!indexIter.getDirCacheEntry().isMerged())
+ throw new UnmergedPathsException(
+ new UnmergedPathException(
+ indexIter.getDirCacheEntry()));
if (wtIter.idEqual(indexIter)
|| wtIter.idEqual(headIter))
continue;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java
index 00af065..0b269b0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java
@@ -447,6 +447,15 @@ public class DirCacheEntry {
}
/**
+ * Returns whether this entry is in the fully-merged stage (0).
+ *
+ * @return true if this entry is merged
+ */
+ public boolean isMerged() {
+ return getStage() == STAGE_0;
+ }
+
+ /**
* Obtain the raw {@link FileMode} bits for this entry.
*
* @return mode bits for the entry.