Add parameter for parent of new branch

This commit is contained in:
René Pfeuffer
2019-03-28 16:15:31 +01:00
parent 81e493ddf0
commit 93cec3d282
11 changed files with 105 additions and 28 deletions

View File

@@ -42,6 +42,7 @@ import sonia.scm.repository.GitUtil;
import sonia.scm.repository.GitWorkdirFactory;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.BranchRequest;
import sonia.scm.repository.util.WorkingCopy;
import java.util.stream.StreamSupport;
@@ -56,19 +57,22 @@ public class GitBranchCommand extends AbstractGitCommand implements BranchComman
}
@Override
public Branch branch(String name) {
public Branch branch(BranchRequest request) {
try (WorkingCopy<org.eclipse.jgit.lib.Repository> workingCopy = workdirFactory.createWorkingCopy(context)) {
Git clone = new Git(workingCopy.get());
Ref ref = clone.branchCreate().setName(name).call();
Iterable<PushResult> call = clone.push().add(name).call();
if (request.getParentBranch() != null) {
clone.checkout().setName(request.getParentBranch());
}
Ref ref = clone.branchCreate().setName(request.getNewBranch()).call();
Iterable<PushResult> call = clone.push().add(request.getNewBranch()).call();
StreamSupport.stream(call.spliterator(), false)
.flatMap(pushResult -> pushResult.getRemoteUpdates().stream())
.filter(remoteRefUpdate -> remoteRefUpdate.getStatus() != RemoteRefUpdate.Status.OK)
.findFirst()
.ifPresent(this::handlePushError);
return Branch.normalBranch(name, GitUtil.getId(ref.getObjectId()));
return Branch.normalBranch(request.getNewBranch(), GitUtil.getId(ref.getObjectId()));
} catch (GitAPIException ex) {
throw new InternalRepositoryException(repository, "could not create branch " + name, ex);
throw new InternalRepositoryException(repository, "could not create branch " + request.getNewBranch(), ex);
}
}

View File

@@ -4,6 +4,7 @@ import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import sonia.scm.repository.Branch;
import sonia.scm.repository.api.BranchRequest;
import java.io.IOException;
import java.util.List;
@@ -20,7 +21,10 @@ public class GitBranchCommandTest extends AbstractGitCommandTestBase {
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals("new_branch")).isEmpty();
new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory()).branch("new_branch");
BranchRequest branchRequest = new BranchRequest();
branchRequest.setNewBranch("new_branch");
new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory()).branch(branchRequest);
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals("new_branch")).isNotEmpty();
}