blob: d6d99a87d39cba2f8ca7495bba651a6f8bc41350 [file]
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.googlesource.gerrit.plugins.replication;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@RequiresCapability(StartReplicationCapability.START_REPLICATION)
@CommandMetaData(
name = "start",
description = "Start replication for specific project or all projects")
final class StartCommand extends SshCommand implements PushResultProcessing.SshOutputCommand {
@Option(name = "--all", usage = "push all known projects")
private boolean all;
@Option(name = "--url", metaVar = "SUBSTRING", usage = "substring URL must match (or * to match everything)")
private String urlMatch;
@Option(name = "--ref", metaVar = "RefName", usage = "ref to replicate")
private String refName = PushOne.ALL_REFS;
private final Set<String> remotesToConsider = new HashSet<>();
@Option(name = "--remote", metaVar = "REMOTE", usage = "name of remote to replicate to")
void addRemote(String remote) {
remotesToConsider.add(remote);
}
@Option(name = "--wait", usage = "wait for replication to finish before exiting")
private boolean wait;
@Option(name = "--now", usage = "start replication without waiting for replicationDelay")
private boolean now;
@Argument(index = 0, multiValued = true, metaVar = "PATTERN", usage = "project name pattern")
private List<String> projectPatterns = new ArrayList<>(2);
@Inject private ReplicationStarter replicationStarter;
private final Object lock = new Object();
@Override
protected void run() throws Failure {
if (all && !projectPatterns.isEmpty()) {
throw new UnloggedFailure(1, "error: cannot combine --all and PROJECT");
}
ReplicationFilter projectFilter =
all
? ReplicationFilter.all()
: new ReplicationFilter(projectPatterns, Collections.emptyList());
replicationStarter.start(urlMatch, refName, remotesToConsider, projectFilter, now, wait, this);
}
@Override
public void writeStdOutSync(String message) {
if (wait) {
synchronized (lock) {
stdout.println(message);
stdout.flush();
}
}
}
@Override
public void writeStdErrSync(String message) {
if (wait) {
synchronized (lock) {
stderr.println(message);
stderr.flush();
}
}
}
}