diff options
author | Philipp Thun <philipp.thun@sap.com> | 2011-03-23 10:24:14 +0100 |
---|---|---|
committer | Mathias Kinzler <mathias.kinzler@sap.com> | 2011-03-23 10:24:14 +0100 |
commit | a21e508a318664dac23701ca29d5f0b64d36a76a (patch) | |
tree | 080712e1c619a0522a90d975b423074a2a0e1b63 /org.eclipse.jgit.test/tst/org/eclipse | |
parent | 770c733687d9f2f71f30822f9691427bf83b7577 (diff) | |
download | jgit-a21e508a318664dac23701ca29d5f0b64d36a76a.tar.gz jgit-a21e508a318664dac23701ca29d5f0b64d36a76a.zip |
Introduce CherryPickResult
In order to distinguish cherry-pick failures caused by conflicts vs.
'abnormal failures' (e.g. due to unstaged changes or a dirty
worktree), a CherryPickResult class is introduced and returned by
CherryPickCommand.call() instead of a RevCommit. This new class is
similar to MergeResult and RebaseResult. The CherryPickResult contains
all necessary information, e.g. paths causing the cherry-pick (a merge
called within, respectively) to fail. This allows callers to better
react on failures.
Change-Id: I5db57b9259e82ed118e4bf4ec94463efe68b8c1f
Signed-off-by: Philipp Thun <philipp.thun@sap.com>
Signed-off-by: Mathias Kinzler <mathias.kinzler@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java index 88bd6acfdf..1646c7b481 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java @@ -49,9 +49,12 @@ import java.io.File; import java.io.IOException; import java.util.Iterator; +import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.JGitInternalException; +import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.RepositoryTestCase; +import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Test; @@ -99,4 +102,74 @@ public class CherryPickCommandTest extends RepositoryTestCase { assertEquals("create a", history.next().getFullMessage()); assertFalse(history.hasNext()); } + + @Test + public void testCherryPickDirtyIndex() throws Exception { + Git git = new Git(db); + RevCommit sideCommit = prepareCherryPick(git); + + // modify and add file a + writeTrashFile("a", "a(modified)"); + git.add().addFilepattern("a").call(); + // do not commit + + doCherryPickAndCheckResult(git, sideCommit, + MergeFailureReason.DIRTY_INDEX); + } + + @Test + public void testCherryPickDirtyWorktree() throws Exception { + Git git = new Git(db); + RevCommit sideCommit = prepareCherryPick(git); + + // modify file a + writeTrashFile("a", "a(modified)"); + // do not add and commit + + doCherryPickAndCheckResult(git, sideCommit, + MergeFailureReason.DIRTY_WORKTREE); + } + + private RevCommit prepareCherryPick(final Git git) throws Exception { + // create, add and commit file a + writeTrashFile("a", "a"); + git.add().addFilepattern("a").call(); + RevCommit firstMasterCommit = git.commit().setMessage("first master") + .call(); + + // create and checkout side branch + createBranch(firstMasterCommit, "refs/heads/side"); + checkoutBranch("refs/heads/side"); + // modify, add and commit file a + writeTrashFile("a", "a(side)"); + git.add().addFilepattern("a").call(); + RevCommit sideCommit = git.commit().setMessage("side").call(); + + // checkout master branch + checkoutBranch("refs/heads/master"); + // modify, add and commit file a + writeTrashFile("a", "a(master)"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("second master").call(); + return sideCommit; + } + + private void doCherryPickAndCheckResult(final Git git, + final RevCommit sideCommit, final MergeFailureReason reason) + throws Exception { + // get current index state + String indexState = indexState(CONTENT); + + // cherry-pick + CherryPickResult result = git.cherryPick().include(sideCommit.getId()) + .call(); + assertEquals(CherryPickStatus.FAILED, result.getStatus()); + // staged file a causes DIRTY_INDEX + assertEquals(1, result.getFailingPaths().size()); + assertEquals(reason, result.getFailingPaths().get("a")); + assertEquals("a(modified)", read(new File(db.getWorkTree(), "a"))); + // index shall be unchanged + assertEquals(indexState, indexState(CONTENT)); + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + } } |