rework hg commands

This commit is contained in:
Konstantin Schaper
2020-12-01 18:39:50 +01:00
parent 121e7bd638
commit 8e19fc641d
6 changed files with 77 additions and 98 deletions

View File

@@ -35,60 +35,28 @@ import sonia.scm.repository.Repository;
public class AbstractCommand
{
/**
* Constructs ...
*
* @param context
*
*/
protected final HgCommandContext context;
protected final Repository repository;
public AbstractCommand(HgCommandContext context)
{
this.context = context;
this.repository = context.getScmRepository();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public com.aragost.javahg.Repository open()
{
return context.open();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public HgCommandContext getContext()
{
return context;
}
/**
* Method description
*
*
* @return
*/
public Repository getRepository()
{
return repository;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private HgCommandContext context;
/** Field description */
private Repository repository;
}

View File

@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.spi;
import com.aragost.javahg.Changeset;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.ExecutionException;
import com.aragost.javahg.commands.PullCommand;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.work.WorkingCopy;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
public class AbstractWorkingCopyCommand extends AbstractCommand {
static final Pattern HG_MESSAGE_PATTERN = Pattern.compile(".*\\[SCM\\](?: Error:)? (.*)");
protected final HgWorkingCopyFactory workingCopyFactory;
public AbstractWorkingCopyCommand(HgCommandContext context, HgWorkingCopyFactory workingCopyFactory)
{
super(context);
this.workingCopyFactory = workingCopyFactory;
}
protected List<Changeset> pullChangesIntoCentralRepository(WorkingCopy<Repository, Repository> workingCopy, String branch) {
try {
com.aragost.javahg.commands.PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
workingCopyFactory.configure(pullCommand);
return pullCommand.execute(workingCopy.getDirectory().getAbsolutePath());
} catch (ExecutionException e) {
throw IntegrateChangesFromWorkdirException
.withPattern(HG_MESSAGE_PATTERN)
.forMessage(context.getScmRepository(), e.getMessage());
} catch (IOException e) {
throw new InternalRepositoryException(getRepository(),
String.format("Could not pull changes '%s' into central repository", branch),
e);
}
}
}

View File

@@ -43,15 +43,12 @@ import java.io.IOException;
* Mercurial implementation of the {@link BranchCommand}.
* Note that this creates an empty commit to "persist" the new branch.
*/
public class HgBranchCommand extends AbstractCommand implements BranchCommand {
public class HgBranchCommand extends AbstractWorkingCopyCommand implements BranchCommand {
private static final Logger LOG = LoggerFactory.getLogger(HgBranchCommand.class);
private final HgWorkingCopyFactory workingCopyFactory;
HgBranchCommand(HgCommandContext context, HgWorkingCopyFactory workingCopyFactory) {
super(context);
this.workingCopyFactory = workingCopyFactory;
super(context, workingCopyFactory);
}
@Override
@@ -103,15 +100,4 @@ public class HgBranchCommand extends AbstractCommand implements BranchCommand {
.execute();
}
private void pullChangesIntoCentralRepository(WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy, String branch) {
try {
PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
workingCopyFactory.configure(pullCommand);
pullCommand.execute(workingCopy.getDirectory().getAbsolutePath());
} catch (IOException e) {
throw new InternalRepositoryException(getRepository(),
String.format("Could not pull changes '%s' into central repository", branch),
e);
}
}
}

View File

@@ -46,17 +46,13 @@ import java.util.regex.Pattern;
import static sonia.scm.repository.spi.UserFormatter.getUserStringFor;
@SuppressWarnings("java:S3252") // it is ok for javahg classes to access static method of subtype
public class HgModifyCommand implements ModifyCommand {
public class HgModifyCommand extends AbstractWorkingCopyCommand implements ModifyCommand {
private static final Logger LOG = LoggerFactory.getLogger(HgModifyCommand.class);
static final Pattern HG_MESSAGE_PATTERN = Pattern.compile(".*\\[SCM\\](?: Error:)? (.*)");
private final HgCommandContext context;
private final HgWorkingCopyFactory workingCopyFactory;
public HgModifyCommand(HgCommandContext context, HgWorkingCopyFactory workingCopyFactory) {
this.context = context;
this.workingCopyFactory = workingCopyFactory;
super(context, workingCopyFactory);
}
@Override
@@ -115,7 +111,7 @@ public class HgModifyCommand implements ModifyCommand {
.user(getUserStringFor(request.getAuthor()))
.message(request.getCommitMessage()).execute();
List<Changeset> execute = pullModifyChangesToCentralRepository(request, workingCopy);
List<Changeset> execute = pullChangesIntoCentralRepository(workingCopy, request.getBranch());
String node = execute.get(0).getNode();
LOG.debug("successfully pulled changes from working copy, new node {}", node);
@@ -126,24 +122,7 @@ public class HgModifyCommand implements ModifyCommand {
}
}
private List<Changeset> pullModifyChangesToCentralRepository(ModifyCommandRequest request, WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy) {
LOG.trace("pull changes from working copy");
try {
com.aragost.javahg.commands.PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
workingCopyFactory.configure(pullCommand);
return pullCommand.execute(workingCopy.getDirectory().getAbsolutePath());
} catch (ExecutionException e) {
throw IntegrateChangesFromWorkdirException
.withPattern(HG_MESSAGE_PATTERN)
.forMessage(context.getScmRepository(), e.getMessage());
} catch (IOException e) {
throw new InternalRepositoryException(context.getScmRepository(),
String.format("Could not pull modify changes from working copy to central repository for branch %s", request.getBranch()),
e);
}
}
private String throwInternalRepositoryException(String message, Exception e) {
private void throwInternalRepositoryException(String message, Exception e) {
throw new InternalRepositoryException(context.getScmRepository(), message, e);
}
}

View File

@@ -25,28 +25,22 @@
package sonia.scm.repository.spi;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.PullCommand;
import com.google.common.base.Strings;
import org.apache.shiro.SecurityUtils;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Tag;
import sonia.scm.repository.api.TagCreateRequest;
import sonia.scm.repository.api.TagDeleteRequest;
import sonia.scm.repository.work.WorkingCopy;
import sonia.scm.user.User;
import java.io.IOException;
import static sonia.scm.repository.spi.UserFormatter.getUserStringFor;
public class HgTagCommand extends AbstractCommand implements TagCommand {
public class HgTagCommand extends AbstractWorkingCopyCommand implements TagCommand {
public static final String DEFAULT_BRANCH_NAME = "default";
private final HgWorkingCopyFactory workingCopyFactory;
public HgTagCommand(HgCommandContext context, HgWorkingCopyFactory workingCopyFactory) {
super(context);
this.workingCopyFactory = workingCopyFactory;
super(context, workingCopyFactory);
}
@Override
@@ -77,16 +71,4 @@ public class HgTagCommand extends AbstractCommand implements TagCommand {
pullChangesIntoCentralRepository(workingCopy, DEFAULT_BRANCH_NAME);
}
}
private void pullChangesIntoCentralRepository(WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy, String branch) {
try {
com.aragost.javahg.commands.PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
workingCopyFactory.configure(pullCommand);
pullCommand.execute(workingCopy.getDirectory().getAbsolutePath());
} catch (IOException e) {
throw new InternalRepositoryException(getRepository(),
String.format("Could not pull changes '%s' into central repository", branch),
e);
}
}
}

View File

@@ -184,14 +184,14 @@ public class HgModifyCommandTest extends AbstractHgCommandTestBase {
@Test
public void shouldExtractSimpleMessage() {
Matcher matcher = HgModifyCommand.HG_MESSAGE_PATTERN.matcher("[SCM] This is a simple message");
Matcher matcher = AbstractWorkingCopyCommand.HG_MESSAGE_PATTERN.matcher("[SCM] This is a simple message");
matcher.matches();
assertThat(matcher.group(1)).isEqualTo("This is a simple message");
}
@Test
public void shouldExtractErrorMessage() {
Matcher matcher = HgModifyCommand.HG_MESSAGE_PATTERN.matcher("[SCM] Error: This is an error message");
Matcher matcher = AbstractWorkingCopyCommand.HG_MESSAGE_PATTERN.matcher("[SCM] Error: This is an error message");
matcher.matches();
assertThat(matcher.group(1)).isEqualTo("This is an error message");
}