mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
implement deleteBranchCommand for git
This commit is contained in:
@@ -68,6 +68,10 @@ public final class BranchCommandBuilder {
|
||||
return command.branch(request);
|
||||
}
|
||||
|
||||
public void delete(String branchName) {
|
||||
command.delete(branchName);
|
||||
}
|
||||
|
||||
private BranchCommand command;
|
||||
private BranchRequest request = new BranchRequest();
|
||||
}
|
||||
|
||||
@@ -41,4 +41,6 @@ import sonia.scm.repository.api.BranchRequest;
|
||||
*/
|
||||
public interface BranchCommand {
|
||||
Branch branch(BranchRequest name);
|
||||
|
||||
void delete(String branchName);
|
||||
}
|
||||
|
||||
@@ -45,8 +45,11 @@ import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.BranchRequest;
|
||||
import sonia.scm.repository.util.WorkingCopy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
|
||||
public class GitBranchCommand extends AbstractGitCommand implements BranchCommand {
|
||||
|
||||
private final GitWorkdirFactory workdirFactory;
|
||||
@@ -73,6 +76,19 @@ public class GitBranchCommand extends AbstractGitCommand implements BranchComman
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String branchName) {
|
||||
try (Git gitRepo = new Git(context.open())) {
|
||||
gitRepo
|
||||
.branchDelete()
|
||||
.setBranchNames(branchName)
|
||||
.setForce(true)
|
||||
.call();
|
||||
} catch (GitAPIException | IOException ex) {
|
||||
throw new InternalRepositoryException(entity(context.getRepository()), String.format("Could not delete branch: %s", branchName));
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePushError(RemoteRefUpdate remoteRefUpdate, BranchRequest request, Repository repository) {
|
||||
if (remoteRefUpdate.getStatus() != RemoteRefUpdate.Status.OK) {
|
||||
// TODO handle failed remote update
|
||||
|
||||
@@ -4,12 +4,15 @@ import org.assertj.core.api.Assertions;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import sonia.scm.repository.Branch;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.api.BranchRequest;
|
||||
import sonia.scm.repository.util.WorkdirProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
||||
public class GitBranchCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@@ -51,6 +54,21 @@ public class GitBranchCommandTest extends AbstractGitCommandTestBase {
|
||||
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals("new_branch")).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteBranch() throws IOException {
|
||||
GitContext context = createContext();
|
||||
String branchToBeDeleted = "squash";
|
||||
new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory(new WorkdirProvider())).delete(branchToBeDeleted);
|
||||
Assertions.assertThat(readBranches(context)).filteredOn(b -> b.getName().equals(branchToBeDeleted)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowInternalRepositoryException() {
|
||||
GitContext context = createContext();
|
||||
String branchToBeDeleted = "master";
|
||||
assertThrows(InternalRepositoryException.class, () -> new GitBranchCommand(context, repository, new SimpleGitWorkdirFactory(new WorkdirProvider())).delete(branchToBeDeleted));
|
||||
}
|
||||
|
||||
private List<Branch> readBranches(GitContext context) throws IOException {
|
||||
return new GitBranchesCommand(context, repository).getBranches();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user