Merge "Refuse to checkout unmerged paths from index"

This commit is contained in:
Christian Halstrick 2012-09-24 05:08:36 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit 1d08015cad
2 changed files with 29 additions and 1 deletions

View File

@ -47,11 +47,13 @@
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.jgit.lib.RepositoryTestCase;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
@ -243,4 +245,23 @@ public void testCheckoutRepository() throws Exception {
assertEquals("1", read(test));
assertEquals("a", read(test2));
}
@Test(expected = JGitInternalException.class)
public void testCheckoutOfConflictingFileShouldThrow()
throws Exception {
// Setup
git.checkout().setCreateBranch(true).setName("conflict")
.setStartPoint(initialCommit).call();
writeTrashFile(FILE1, "Conflicting");
RevCommit conflict = git.commit().setAll(true)
.setMessage("Conflicting change").call();
git.checkout().setName("master").call();
git.merge().include(conflict).call();
assertEquals(RepositoryState.MERGING, db.getRepositoryState());
// Now check out the conflicting path
git.checkout().addPath(FILE1).call();
}
}

View File

@ -64,6 +64,7 @@
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
@ -284,7 +285,8 @@ protected CheckoutCommand checkoutPaths() throws IOException,
startWalk.setRecursive(true);
if (!checkoutAllPaths)
startWalk.setFilter(PathFilterGroup.createFromStrings(paths));
boolean checkoutIndex = startCommit == null && startPoint == null;
final boolean checkoutIndex = startCommit == null
&& startPoint == null;
if (!checkoutIndex)
startWalk.addTree(revWalk.parseCommit(getStartPoint())
.getTree());
@ -299,6 +301,11 @@ protected CheckoutCommand checkoutPaths() throws IOException,
final FileMode mode = startWalk.getFileMode(0);
editor.add(new PathEdit(startWalk.getPathString()) {
public void apply(DirCacheEntry ent) {
if (checkoutIndex
&& ent.getStage() > DirCacheEntry.STAGE_0) {
UnmergedPathException e = new UnmergedPathException(ent);
throw new JGitInternalException(e.getMessage(), e);
}
ent.setObjectId(blobId);
ent.setFileMode(mode);
File file = new File(workTree, ent.getPathString());