implement deleteBranchCommand for hg

This commit is contained in:
Eduard Heimbuch
2019-11-11 11:16:33 +01:00
parent 7cd2cb4ccb
commit 39d41a56c5
2 changed files with 47 additions and 4 deletions

View File

@@ -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<com.aragost.javahg.Repository, com.aragost.javahg.Repository> 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<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy) {
private void pullChangesIntoCentralRepository(WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> 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);
}
}

View File

@@ -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<Branch> readBranches() {
return new HgBranchesCommand(cmdContext, repository).getBranches();
}