aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/api
diff options
context:
space:
mode:
authorThomas Wolf <twolf@apache.org>2023-04-23 21:31:40 +0200
committerThomas Wolf <twolf@apache.org>2023-04-29 13:24:32 +0200
commit8bc13fb79d3ff44f09b67a1effda7155b47391ae (patch)
tree494e1ec52f21325470dfa8b71dc4a3cbe1c01f97 /org.eclipse.jgit.test/tst/org/eclipse/jgit/api
parent3ed4cdda6b99f812258ca50bd77447a28d9d4596 (diff)
downloadjgit-8bc13fb79d3ff44f09b67a1effda7155b47391ae.tar.gz
jgit-8bc13fb79d3ff44f09b67a1effda7155b47391ae.zip
Support cherry-picking a root commit
Handle the case of the commit to be picked not having any parents. Since JGit implements cherry-pick as a 3-way-merge between the commit to be picked and the target commit, using the parent of the picked commit as merge base, this is super simple: just don't set a base tree. The merger will not find any merge base and will supply an empty tree iterator for the base. Bug: 581832 Change-Id: I88985f1b1723db5b35ce58bf228bc48d23d6fca3 Signed-off-by: Thomas Wolf <twolf@apache.org>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/api')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CherryPickCommandTest.java102
1 files changed, 67 insertions, 35 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 0d38197d9a..301d6be662 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
@@ -100,41 +100,73 @@ public class CherryPickCommandTest extends RepositoryTestCase {
}
}
- @Test
- public void testSequentialCherryPick() throws IOException, JGitInternalException,
- GitAPIException {
- try (Git git = new Git(db)) {
- writeTrashFile("a", "first line\nsec. line\nthird line\n");
- git.add().addFilepattern("a").call();
- RevCommit firstCommit = git.commit().setMessage("create a").call();
-
- writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
- git.add().addFilepattern("a").call();
- RevCommit enlargingA = git.commit().setMessage("enlarged a").call();
-
- writeTrashFile("a",
- "first line\nsecond line\nthird line\nfourth line\n");
- git.add().addFilepattern("a").call();
- RevCommit fixingA = git.commit().setMessage("fixed a").call();
-
- git.branchCreate().setName("side").setStartPoint(firstCommit).call();
- checkoutBranch("refs/heads/side");
-
- writeTrashFile("b", "nothing to do with a");
- git.add().addFilepattern("b").call();
- git.commit().setMessage("create b").call();
-
- CherryPickResult result = git.cherryPick().include(enlargingA).include(fixingA).call();
- assertEquals(CherryPickResult.CherryPickStatus.OK, result.getStatus());
-
- Iterator<RevCommit> history = git.log().call().iterator();
- assertEquals("fixed a", history.next().getFullMessage());
- assertEquals("enlarged a", history.next().getFullMessage());
- assertEquals("create b", history.next().getFullMessage());
- assertEquals("create a", history.next().getFullMessage());
- assertFalse(history.hasNext());
- }
- }
+ @Test
+ public void testSequentialCherryPick()
+ throws IOException, JGitInternalException, GitAPIException {
+ try (Git git = new Git(db)) {
+ writeTrashFile("a", "first line\nsec. line\nthird line\n");
+ git.add().addFilepattern("a").call();
+ RevCommit firstCommit = git.commit().setMessage("create a").call();
+
+ writeTrashFile("a",
+ "first line\nsec. line\nthird line\nfourth line\n");
+ git.add().addFilepattern("a").call();
+ RevCommit enlargingA = git.commit().setMessage("enlarged a").call();
+
+ writeTrashFile("a",
+ "first line\nsecond line\nthird line\nfourth line\n");
+ git.add().addFilepattern("a").call();
+ RevCommit fixingA = git.commit().setMessage("fixed a").call();
+
+ git.branchCreate().setName("side").setStartPoint(firstCommit)
+ .call();
+ checkoutBranch("refs/heads/side");
+
+ writeTrashFile("b", "nothing to do with a");
+ git.add().addFilepattern("b").call();
+ git.commit().setMessage("create b").call();
+
+ CherryPickResult result = git.cherryPick().include(enlargingA)
+ .include(fixingA).call();
+ assertEquals(CherryPickResult.CherryPickStatus.OK,
+ result.getStatus());
+
+ Iterator<RevCommit> history = git.log().call().iterator();
+ assertEquals("fixed a", history.next().getFullMessage());
+ assertEquals("enlarged a", history.next().getFullMessage());
+ assertEquals("create b", history.next().getFullMessage());
+ assertEquals("create a", history.next().getFullMessage());
+ assertFalse(history.hasNext());
+ }
+ }
+
+ @Test
+ public void testRootCherryPick()
+ throws IOException, JGitInternalException, GitAPIException {
+ try (Git git = new Git(db)) {
+ writeTrashFile("a", "a");
+ writeTrashFile("b", "b");
+ git.add().addFilepattern("a").addFilepattern("b").call();
+ RevCommit firstCommit = git.commit().setMessage("Create a and b")
+ .call();
+
+ git.checkout().setOrphan(true).setName("orphan").call();
+ git.rm().addFilepattern("a").addFilepattern("b").call();
+ writeTrashFile("a", "a");
+ git.add().addFilepattern("a").call();
+ git.commit().setMessage("Orphan a").call();
+
+ CherryPickResult result = git.cherryPick().include(firstCommit)
+ .call();
+ assertEquals(CherryPickResult.CherryPickStatus.OK,
+ result.getStatus());
+
+ Iterator<RevCommit> history = git.log().call().iterator();
+ assertEquals("Create a and b", history.next().getFullMessage());
+ assertEquals("Orphan a", history.next().getFullMessage());
+ assertFalse(history.hasNext());
+ }
+ }
@Test
public void testCherryPickDirtyIndex() throws Exception {