diff options
author | Thomas Wolf <twolf@apache.org> | 2024-01-10 18:36:50 +0100 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2024-01-28 20:40:20 +0100 |
commit | 906c2bebed0dc732a2fbd5b397466cf3522714f0 (patch) | |
tree | fa51e9267a6df563ff685c80dc0eeefcb19f6036 | |
parent | c701c01b49d92993f1c3df0a0e26a2dd68b8cec1 (diff) | |
download | jgit-906c2bebed0dc732a2fbd5b397466cf3522714f0.tar.gz jgit-906c2bebed0dc732a2fbd5b397466cf3522714f0.zip |
RebaseCommand: fix stopping on root commit conflicts
If rebasing runs into a conflict when applying a root commit from an
independent branch, there is no parent commit. Write an empty patch
file in that case like C git does.
Bug: jgit-6
Change-Id: I315313673d2abf29639d7d96c958d599961ba276
Signed-off-by: Thomas Wolf <twolf@apache.org>
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java | 34 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java | 14 |
2 files changed, 43 insertions, 5 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index 16f7cd1eb0..987c98e232 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -332,6 +332,40 @@ public class RebaseCommandTest extends RepositoryTestCase { } /** + * Create a commit A and an unrelated commit B creating the same file with + * different content. Then rebase A onto B. The rebase should stop with a + * conflict. + * + * @throws Exception + * on errors + */ + @Test + public void testRebaseNoMergeBaseConflict() throws Exception { + writeTrashFile(FILE1, FILE1); + git.add().addFilepattern(FILE1).call(); + RevCommit first = git.commit().setMessage("Add file").call(); + File file1 = new File(db.getWorkTree(), FILE1); + assertTrue(file1.exists()); + // Create an independent branch + git.checkout().setOrphan(true).setName("orphan").call(); + git.rm().addFilepattern(FILE1).call(); + assertFalse(file1.exists()); + writeTrashFile(FILE1, "something else"); + git.add().addFilepattern(FILE1).call(); + git.commit().setMessage("Orphan").call(); + checkoutBranch("refs/heads/master"); + assertEquals(first.getId(), db.resolve("HEAD")); + RebaseResult res = git.rebase().setUpstream("refs/heads/orphan").call(); + assertEquals(Status.STOPPED, res.getStatus()); + assertEquals(first, res.getCurrentCommit()); + checkFile(file1, "<<<<<<< Upstream, based on orphan\n" + + "something else\n" + + "=======\n" + + "file1\n" + + ">>>>>>> " + first.abbreviate(7).name() + " Add file\n"); + } + + /** * Create the following commits and then attempt to rebase topic onto * master. This will serialize the branches. * diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java index 0f7b8afe21..757aff89c3 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java @@ -1064,12 +1064,16 @@ public class RebaseCommand extends GitCommand<RebaseResult> { String authorScript = toAuthorScript(author); rebaseState.createFile(AUTHOR_SCRIPT, authorScript); rebaseState.createFile(MESSAGE, commitToPick.getFullMessage()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (DiffFormatter df = new DiffFormatter(bos)) { - df.setRepository(repo); - df.format(commitToPick.getParent(0), commitToPick); + if (commitToPick.getParentCount() > 0) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (DiffFormatter df = new DiffFormatter(bos)) { + df.setRepository(repo); + df.format(commitToPick.getParent(0), commitToPick); + } + rebaseState.createFile(PATCH, new String(bos.toByteArray(), UTF_8)); + } else { + rebaseState.createFile(PATCH, ""); //$NON-NLS-1$ } - rebaseState.createFile(PATCH, new String(bos.toByteArray(), UTF_8)); rebaseState.createFile(STOPPED_SHA, repo.newObjectReader() .abbreviate( |