StashCreateCommand: Abort in case of unmerged paths

Bug: 391861
Change-Id: I5f8ffe072c08c8ca2ca6be6b6afa67c8e16a63b6
This commit is contained in:
Robin Stocker 2012-10-22 11:14:40 +02:00
parent 1ff82ca6d0
commit ad52ec5207
4 changed files with 61 additions and 0 deletions

View File

@ -52,6 +52,7 @@
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 void refLogIncludesCommitMessage() throws Exception {
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();
}
}

View File

@ -58,6 +58,8 @@
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 @@ protected File writeTrashFiles(boolean ensureDistinctTimestamps,
}
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);
}
}
}

View File

@ -52,12 +52,14 @@
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 RevCommit call() throws GitAPIException {
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;

View File

@ -446,6 +446,15 @@ public boolean isIntentToAdd() {
return (getExtendedFlags() & INTENT_TO_ADD) != 0;
}
/**
* 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.
*