Added force to push command for git and hg repos

Committed-by: Rene Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
Thomas Zerr
2023-09-14 20:25:59 +02:00
committed by René Pfeuffer
parent a608a0df80
commit b2472d85d0
16 changed files with 305 additions and 25 deletions

View File

@@ -48,5 +48,11 @@ public enum Feature
*
* @since 2.23.0
*/
MODIFICATIONS_BETWEEN_REVISIONS
MODIFICATIONS_BETWEEN_REVISIONS,
/**
* The repository supports pushing with a force flag.
*
* @since 2.47.0
*/
FORCE_PUSH
}

View File

@@ -28,6 +28,8 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.FeatureNotSupportedException;
import sonia.scm.repository.Feature;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.spi.PushCommand;
@@ -35,6 +37,7 @@ import sonia.scm.repository.spi.PushCommandRequest;
import java.io.IOException;
import java.net.URL;
import java.util.Set;
/**
* The push command push changes to a other repository.
@@ -48,9 +51,11 @@ public final class PushCommandBuilder {
private final PushCommand command;
private final PushCommandRequest request = new PushCommandRequest();
private final Set<Feature> supportedFeatures;
PushCommandBuilder(PushCommand command) {
PushCommandBuilder(PushCommand command, Set<Feature> supportedFeatures) {
this.command = command;
this.supportedFeatures = supportedFeatures;
}
/**
@@ -67,12 +72,22 @@ public final class PushCommandBuilder {
return this;
}
public PushCommandBuilder withForce(boolean force) {
if (!supportedFeatures.contains(Feature.FORCE_PUSH)) {
throw new FeatureNotSupportedException(Feature.FORCE_PUSH.name());
}
request.setForce(force);
return this;
}
/**
* Push all changes to the given remote repository.
*
* @param remoteRepository remote repository
* @return informations of the executed push command
* @throws IOException
* @throws PushFailedException when the push (maybe just partially) failed (since 2.47.0)
*/
public PushResponse push(Repository remoteRepository) throws IOException {
Subject subject = SecurityUtils.getSubject();
@@ -95,6 +110,7 @@ public final class PushCommandBuilder {
* @param url url of a remote repository
* @return informations of the executed push command
* @throws IOException
* @throws PushFailedException when the push (maybe just partially) failed (since 2.47.0)
* @since 1.43
*/
public PushResponse push(String url) throws IOException {

View File

@@ -0,0 +1,43 @@
/*
* 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.api;
import sonia.scm.ContextEntry;
import sonia.scm.ExceptionWithContext;
import sonia.scm.repository.Repository;
public class PushFailedException extends ExceptionWithContext {
public static final String CODE = "dnWjIroRhT";
public PushFailedException(Repository repository) {
super(ContextEntry.ContextBuilder.entity(repository).build(), "Failed to push");
}
@Override
public String getCode() {
return CODE;
}
}

View File

@@ -346,7 +346,7 @@ public final class RepositoryService implements Closeable {
public PushCommandBuilder getPushCommand() {
LOG.debug("create push command for repository {}", repository);
return new PushCommandBuilder(provider.getPushCommand());
return new PushCommandBuilder(provider.getPushCommand(), provider.getSupportedFeatures());
}
/**

View File

@@ -24,10 +24,23 @@
package sonia.scm.repository.spi;
import lombok.Getter;
import lombok.Setter;
/**
* Request object for {@link PushCommand}.
*
* @author Sebastian Sdorra
* @since 1.31
*/
public final class PushCommandRequest extends RemoteCommandRequest {}
@Getter
@Setter
public final class PushCommandRequest extends RemoteCommandRequest {
private boolean force = false;
@Override
public void reset() {
force = false;
super.reset();
}
}