From 39d41a56c54fd3cdf1499825ce7a577e2da6d496 Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Mon, 11 Nov 2019 11:16:33 +0100 Subject: [PATCH] implement deleteBranchCommand for hg --- .../scm/repository/spi/HgBranchCommand.java | 33 ++++++++++++++++--- .../repository/spi/HgBranchCommandTest.java | 18 ++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchCommand.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchCommand.java index e41dbf96da..eca8019329 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchCommand.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBranchCommand.java @@ -36,7 +36,9 @@ import com.aragost.javahg.commands.PullCommand; import org.apache.shiro.SecurityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.ContextEntry; import sonia.scm.repository.Branch; +import sonia.scm.repository.InternalRepositoryException; import sonia.scm.repository.Repository; import sonia.scm.repository.api.BranchRequest; import sonia.scm.repository.util.WorkingCopy; @@ -67,23 +69,46 @@ public class HgBranchCommand extends AbstractCommand implements BranchCommand { LOG.debug("Created new branch '{}' in repository {} with changeset {}", request.getNewBranch(), getRepository().getNamespaceAndName(), emptyChangeset.getNode()); - pullNewBranchIntoCentralRepository(request, workingCopy); + pullChangesIntoCentralRepository(workingCopy, request.getNewBranch()); return Branch.normalBranch(request.getNewBranch(), emptyChangeset.getNode()); } } + @Override + public void delete(String branchName) { + try (WorkingCopy workingCopy = workdirFactory.createWorkingCopy(getContext(), branchName)) { + User currentUser = SecurityUtils.getSubject().getPrincipals().oneByType(User.class); + + LOG.debug("Closing branch '{}' in repository {}", branchName, getRepository().getNamespaceAndName()); + + com.aragost.javahg.commands.CommitCommand + .on(workingCopy.getWorkingRepository()) + .user(getFormattedUser(currentUser)) + .message(String.format("Close branch: %s", branchName)) + .closeBranch() + .execute(); + pullChangesIntoCentralRepository(workingCopy, branchName); + } catch (Exception ex) { + throw new InternalRepositoryException(ContextEntry.ContextBuilder.entity(getContext().getScmRepository()), String.format("Could not close branch: %s", branchName)); + } + } + + private String getFormattedUser(User currentUser) { + return String.format("%s <%s>", currentUser.getDisplayName(), currentUser.getMail()); + } + private Changeset createNewBranchWithEmptyCommit(BranchRequest request, com.aragost.javahg.Repository repository) { com.aragost.javahg.commands.BranchCommand.on(repository).set(request.getNewBranch()); User currentUser = SecurityUtils.getSubject().getPrincipals().oneByType(User.class); return CommitCommand .on(repository) - .user(String.format("%s <%s>", currentUser.getDisplayName(), currentUser.getMail())) + .user(getFormattedUser(currentUser)) .message("Create new branch " + request.getNewBranch()) .execute(); } - private void pullNewBranchIntoCentralRepository(BranchRequest request, WorkingCopy workingCopy) { + private void pullChangesIntoCentralRepository(WorkingCopy workingCopy, String branch) { try { PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository()); workdirFactory.configure(pullCommand); @@ -91,7 +116,7 @@ public class HgBranchCommand extends AbstractCommand implements BranchCommand { } catch (Exception e) { // TODO handle failed update throw new IntegrateChangesFromWorkdirException(getRepository(), - String.format("Could not pull new branch '%s' into central repository", request.getNewBranch()), + String.format("Could not pull changes '%s' into central repository", branch), e); } } diff --git a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBranchCommandTest.java b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBranchCommandTest.java index 882dab05d0..8ecbb3dd97 100644 --- a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBranchCommandTest.java +++ b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBranchCommandTest.java @@ -6,6 +6,7 @@ import org.junit.Before; import org.junit.Test; import sonia.scm.repository.Branch; import sonia.scm.repository.HgTestUtil; +import sonia.scm.repository.InternalRepositoryException; import sonia.scm.repository.api.BranchRequest; import sonia.scm.repository.util.WorkdirProvider; import sonia.scm.web.HgRepositoryEnvironmentBuilder; @@ -13,6 +14,7 @@ import sonia.scm.web.HgRepositoryEnvironmentBuilder; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HgBranchCommandTest extends AbstractHgCommandTestBase { @@ -54,6 +56,22 @@ public class HgBranchCommandTest extends AbstractHgCommandTestBase { assertThat(cmdContext.open().changeset(newBranch.getRevision()).getParent1().getBranch()).isEqualTo("test-branch"); } + @Test + public void shouldCloseBranch() { + String branchToBeClosed = "test-branch"; + + new HgBranchCommand(cmdContext, repository, workdirFactory).delete(branchToBeClosed); + assertThat(readBranches()).filteredOn(b -> b.getName().equals("test-branch")).isEmpty(); + } + + @Test + public void shouldThrowInternalRepositoryException() { + String branchToBeClosed = "default"; + + new HgBranchCommand(cmdContext, repository, workdirFactory).delete(branchToBeClosed); + assertThrows(InternalRepositoryException.class, () -> new HgBranchCommand(cmdContext, repository, workdirFactory).delete(branchToBeClosed)); + } + private List readBranches() { return new HgBranchesCommand(cmdContext, repository).getBranches(); }