diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBranchCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBranchCommand.java index a0ea403feb..9796182b04 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBranchCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBranchCommand.java @@ -61,7 +61,7 @@ public class GitBranchCommand extends AbstractGitCommand implements BranchComman try (WorkingCopy workingCopy = workdirFactory.createWorkingCopy(context)) { Git clone = new Git(workingCopy.getWorkingRepository()); if (request.getParentBranch() != null) { - clone.checkout().setName(request.getParentBranch()); + clone.checkout().setName("origin/" + request.getParentBranch()).call(); } Ref ref = clone.branchCreate().setName(request.getNewBranch()).call(); Iterable call = clone.push().add(request.getNewBranch()).call(); diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBranchCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBranchCommandTest.java index 364306c033..e429aa5a42 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBranchCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitBranchCommandTest.java @@ -15,6 +15,27 @@ public class GitBranchCommandTest extends AbstractGitCommandTestBase { @Rule public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule(); + @Test + public void shouldCreateBranchWithDefinedSourceBranch() throws IOException { + GitContext context = createContext(); + + Branch source = findBranch(context, "mergeable"); + + BranchRequest branchRequest = new BranchRequest(); + branchRequest.setParentBranch(source.getName()); + branchRequest.setNewBranch("new_branch"); + + new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory()).branch(branchRequest); + + Branch newBranch = findBranch(context, "new_branch"); + Assertions.assertThat(newBranch.getRevision()).isEqualTo(source.getRevision()); + } + + private Branch findBranch(GitContext context, String name) throws IOException { + List branches = readBranches(context); + return branches.stream().filter(b -> name.equals(b.getName())).findFirst().get(); + } + @Test public void shouldCreateBranch() throws IOException { GitContext context = createContext();