mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-24 07:20:49 +01:00
Added force to push command for git and hg repos
Committed-by: Rene Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
committed by
René Pfeuffer
parent
a608a0df80
commit
b2472d85d0
@@ -32,19 +32,26 @@ import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.transport.PushResult;
|
||||
import org.eclipse.jgit.transport.RemoteRefUpdate;
|
||||
import org.eclipse.jgit.transport.ScmTransportProtocol;
|
||||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.api.PushFailedException;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
|
||||
|
||||
private static final List<RemoteRefUpdate.Status> REJECTED_STATUSES = List.of(
|
||||
RemoteRefUpdate.Status.REJECTED_NODELETE,
|
||||
RemoteRefUpdate.Status.REJECTED_NONFASTFORWARD,
|
||||
RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED,
|
||||
RemoteRefUpdate.Status.REJECTED_OTHER_REASON
|
||||
);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
|
||||
|
||||
protected GitRepositoryHandler handler;
|
||||
@@ -54,7 +61,7 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
protected long push(Repository source, String remoteUrl, String username, String password) {
|
||||
protected long push(Repository source, String remoteUrl, String username, String password, boolean force) {
|
||||
Git git = Git.wrap(source);
|
||||
org.eclipse.jgit.api.PushCommand push = git.push();
|
||||
|
||||
@@ -63,19 +70,23 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
|
||||
}
|
||||
push.setPushAll().setPushTags();
|
||||
push.setRemote(remoteUrl);
|
||||
push.setForce(force);
|
||||
|
||||
long counter = -1;
|
||||
long counter;
|
||||
|
||||
try {
|
||||
Iterable<PushResult> results = push.call();
|
||||
|
||||
if (results != null) {
|
||||
counter = 0;
|
||||
|
||||
for (PushResult result : results) {
|
||||
counter += count(git, result);
|
||||
}
|
||||
if (hasPushFailed(results)) {
|
||||
throw new PushFailedException(repository);
|
||||
}
|
||||
|
||||
counter = 0;
|
||||
|
||||
for (PushResult result : results) {
|
||||
counter += count(git, result);
|
||||
}
|
||||
} catch (PushFailedException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw new InternalRepositoryException(repository, "could not execute push/pull command", ex);
|
||||
}
|
||||
@@ -83,6 +94,22 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
|
||||
return counter;
|
||||
}
|
||||
|
||||
private boolean hasPushFailed(Iterable<PushResult> pushResults) {
|
||||
if (pushResults == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (PushResult nextResult : pushResults) {
|
||||
for(RemoteRefUpdate remoteUpdate : nextResult.getRemoteUpdates()) {
|
||||
if(REJECTED_STATUSES.contains(remoteUpdate.getStatus())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected String getRemoteUrl(RemoteCommandRequest request) {
|
||||
String url;
|
||||
sonia.scm.repository.Repository remRepo = request.getRemoteRepository();
|
||||
|
||||
@@ -166,7 +166,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
|
||||
try (Git git = Git.open(sourceDirectory)) {
|
||||
source = git.getRepository();
|
||||
response = new PullResponse(push(source, getRemoteUrl(targetDirectory), username, password));
|
||||
response = new PullResponse(push(source, getRemoteUrl(targetDirectory), username, password, false));
|
||||
} finally {
|
||||
GitUtil.close(source);
|
||||
}
|
||||
|
||||
@@ -49,6 +49,6 @@ public class GitPushCommand extends AbstractGitPushOrPullCommand implements Push
|
||||
|
||||
LOG.debug("push changes from {} to {}", repository, remoteUrl);
|
||||
|
||||
return new PushResponse(push(open(), remoteUrl, request.getUsername(), request.getPassword()));
|
||||
return new PushResponse(push(open(), remoteUrl, request.getUsername(), request.getPassword(), request.isForce()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,8 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider {
|
||||
|
||||
protected static final Set<Feature> FEATURES = EnumSet.of(
|
||||
Feature.INCOMING_REVISION,
|
||||
Feature.MODIFICATIONS_BETWEEN_REVISIONS
|
||||
Feature.MODIFICATIONS_BETWEEN_REVISIONS,
|
||||
Feature.FORCE_PUSH
|
||||
);
|
||||
|
||||
private final GitContext context;
|
||||
|
||||
Reference in New Issue
Block a user