Browse Source

Cherry-Pick: Support --no-commit

Change-Id: Ic4c313a8971a5aa15745518c49e2587a1a0fe8d4
Signed-off-by: Konrad Kügler <swamblumat-eclipsebugs@yahoo.de>
stable-3.5
Konrad Kügler 11 years ago
parent
commit
0f679dbda5
  1. 48
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java
  2. 33
      org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java

48
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java

@ -76,6 +76,18 @@ public class CherryPickCommandTest extends RepositoryTestCase {
@Test @Test
public void testCherryPick() throws IOException, JGitInternalException, public void testCherryPick() throws IOException, JGitInternalException,
GitAPIException { GitAPIException {
doTestCherryPick(false);
}
@Test
public void testCherryPickNoCommit() throws IOException,
JGitInternalException, GitAPIException {
doTestCherryPick(true);
}
private void doTestCherryPick(boolean noCommit) throws IOException,
JGitInternalException,
GitAPIException {
Git git = new Git(db); Git git = new Git(db);
writeTrashFile("a", "first line\nsec. line\nthird line\n"); writeTrashFile("a", "first line\nsec. line\nthird line\n");
@ -102,13 +114,16 @@ public class CherryPickCommandTest extends RepositoryTestCase {
git.add().addFilepattern("a").call(); git.add().addFilepattern("a").call();
git.commit().setMessage("enhanced a").call(); git.commit().setMessage("enhanced a").call();
git.cherryPick().include(fixingA).call(); CherryPickResult pickResult = git.cherryPick().include(fixingA)
.setNoCommit(noCommit).call();
assertEquals(CherryPickStatus.OK, pickResult.getStatus());
assertFalse(new File(db.getWorkTree(), "b").exists()); assertFalse(new File(db.getWorkTree(), "b").exists());
checkFile(new File(db.getWorkTree(), "a"), checkFile(new File(db.getWorkTree(), "a"),
"first line\nsecond line\nthird line\nfeature++\n"); "first line\nsecond line\nthird line\nfeature++\n");
Iterator<RevCommit> history = git.log().call().iterator(); Iterator<RevCommit> history = git.log().call().iterator();
assertEquals("fixed a", history.next().getFullMessage()); if (!noCommit)
assertEquals("fixed a", history.next().getFullMessage());
assertEquals("enhanced a", history.next().getFullMessage()); assertEquals("enhanced a", history.next().getFullMessage());
assertEquals("create a", history.next().getFullMessage()); assertEquals("create a", history.next().getFullMessage());
assertFalse(history.hasNext()); assertFalse(history.hasNext());
@ -205,6 +220,35 @@ public class CherryPickCommandTest extends RepositoryTestCase {
assertEquals(RepositoryState.SAFE, db.getRepositoryState()); assertEquals(RepositoryState.SAFE, db.getRepositoryState());
} }
@Test
public void testCherryPickConflictResolutionNoCOmmit() throws Exception {
Git git = new Git(db);
RevCommit sideCommit = prepareCherryPick(git);
CherryPickResult result = git.cherryPick().include(sideCommit.getId())
.setNoCommit(true).call();
assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
assertTrue(db.readDirCache().hasUnmergedPaths());
String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
assertEquals(expected, read("a"));
assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
.exists());
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
// Resolve
writeTrashFile("a", "a");
git.add().addFilepattern("a").call();
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
git.commit().setOnly("a").setMessage("resolve").call();
assertEquals(RepositoryState.SAFE, db.getRepositoryState());
}
@Test @Test
public void testCherryPickConflictReset() throws Exception { public void testCherryPickConflictReset() throws Exception {
Git git = new Git(db); Git git = new Git(db);

33
org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java

@ -93,6 +93,8 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
private Integer mainlineParentNumber; private Integer mainlineParentNumber;
private boolean noCommit = false;
/** /**
* @param repo * @param repo
*/ */
@ -162,11 +164,12 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
merger.getResultTreeId()); merger.getResultTreeId());
dco.setFailOnConflict(true); dco.setFailOnConflict(true);
dco.checkout(); dco.checkout();
newHead = new Git(getRepository()).commit() if (!noCommit)
.setMessage(srcCommit.getFullMessage()) newHead = new Git(getRepository()).commit()
.setReflogComment(reflogPrefix + " " //$NON-NLS-1$ .setMessage(srcCommit.getFullMessage())
+ srcCommit.getShortMessage()) .setReflogComment(reflogPrefix + " " //$NON-NLS-1$
.setAuthor(srcCommit.getAuthorIdent()).call(); + srcCommit.getShortMessage())
.setAuthor(srcCommit.getAuthorIdent()).call();
cherryPickedRefs.add(src); cherryPickedRefs.add(src);
} else { } else {
if (merger.failed()) if (merger.failed())
@ -178,7 +181,8 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
.formatWithConflicts(srcCommit.getFullMessage(), .formatWithConflicts(srcCommit.getFullMessage(),
merger.getUnmergedPaths()); merger.getUnmergedPaths());
repo.writeCherryPickHead(srcCommit.getId()); if (!noCommit)
repo.writeCherryPickHead(srcCommit.getId());
repo.writeMergeCommitMsg(message); repo.writeMergeCommitMsg(message);
return CherryPickResult.CONFLICT; return CherryPickResult.CONFLICT;
@ -303,6 +307,23 @@ public class CherryPickCommand extends GitCommand<CherryPickResult> {
return this; return this;
} }
/**
* Allows cherry-picking changes without committing them.
* <p>
* NOTE: The behavior of cherry-pick is undefined if you pick multiple
* commits or if HEAD does not match the index state before cherry-picking.
*
* @param noCommit
* true to cherry-pick without committing, false to commit after
* each pick (default)
* @return {@code this}
* @since 3.5
*/
public CherryPickCommand setNoCommit(boolean noCommit) {
this.noCommit = noCommit;
return this;
}
private String calculateOurName(Ref headRef) { private String calculateOurName(Ref headRef) {
if (ourCommitName != null) if (ourCommitName != null)
return ourCommitName; return ourCommitName;

Loading…
Cancel
Save