Extract ChainedReceiveCommands to its own class

Change-Id: I3224265a7570ac4feeebdfff7f326a37e894a23b
This commit is contained in:
Dave Borowitz
2016-02-18 15:17:35 -05:00
parent bebb1790a2
commit 56a55daf31
2 changed files with 82 additions and 34 deletions

View File

@@ -64,7 +64,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
@@ -256,39 +255,6 @@ public class BatchUpdate implements AutoCloseable {
public abstract Change createChange(Context ctx);
}
private static class ChainedReceiveCommands {
private final Map<String, ReceiveCommand> commands = new LinkedHashMap<>();
private boolean isEmpty() {
return commands.isEmpty();
}
private void add(ReceiveCommand cmd) {
checkArgument(!cmd.getOldId().equals(cmd.getNewId()),
"ref update is a no-op: %s", cmd);
ReceiveCommand old = commands.get(cmd.getRefName());
if (old == null) {
commands.put(cmd.getRefName(), cmd);
return;
}
checkArgument(old.getResult() == ReceiveCommand.Result.NOT_ATTEMPTED,
"cannot chain ref update %s after update %s with result %s",
cmd, old, old.getResult());
checkArgument(cmd.getOldId().equals(old.getNewId()),
"cannot chain ref update %s after update %s with different new ID",
cmd, old);
commands.put(cmd.getRefName(), new ReceiveCommand(
old.getOldId(), cmd.getNewId(), cmd.getRefName()));
}
private void addTo(BatchRefUpdate bru) {
checkState(!isEmpty(), "no commands to add");
for (ReceiveCommand cmd : commands.values()) {
bru.addCommand(cmd);
}
}
}
/**
* Interface for listening during batch update execution.
* <p>

View File

@@ -0,0 +1,82 @@
// Copyright (C) 2016 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.google.gerrit.server.git;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.transport.ReceiveCommand;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Collection of {@link ReceiveCommand}s that supports multiple updates per ref.
* <p>
* The underlying behavior of {@link BatchRefUpdate} is undefined (an
* implementations vary) when more than one command per ref is added. This class
* works around that limitation by allowing multiple updates per ref, as long as
* the previous new SHA-1 matches the next old SHA-1.
*/
public class ChainedReceiveCommands {
private final Map<String, ReceiveCommand> commands = new LinkedHashMap<>();
/** @return true if no commands have been added. */
public boolean isEmpty() {
return commands.isEmpty();
}
/**
* Add a command.
*
* @param cmd command to add. If a command has been previously added for the
* same ref, the new SHA-1 of the most recent previous command must match
* the old SHA-1 of this command.
*/
public void add(ReceiveCommand cmd) {
checkArgument(!cmd.getOldId().equals(cmd.getNewId()),
"ref update is a no-op: %s", cmd);
ReceiveCommand old = commands.get(cmd.getRefName());
if (old == null) {
commands.put(cmd.getRefName(), cmd);
return;
}
checkArgument(old.getResult() == ReceiveCommand.Result.NOT_ATTEMPTED,
"cannot chain ref update %s after update %s with result %s",
cmd, old, old.getResult());
checkArgument(cmd.getOldId().equals(old.getNewId()),
"cannot chain ref update %s after update %s with different new ID",
cmd, old);
commands.put(cmd.getRefName(), new ReceiveCommand(
old.getOldId(), cmd.getNewId(), cmd.getRefName()));
}
/**
* Add commands from this instance to a native JGit batch update.
* <p>
* Exactly one command per ref will be added to the update. The old SHA-1 will
* be the old SHA-1 of the first command added to this instance for that ref;
* the new SHA-1 will be the new SHA-1 of the last command.
*
* @param bru batch update
*/
public void addTo(BatchRefUpdate bru) {
checkState(!isEmpty(), "no commands to add");
for (ReceiveCommand cmd : commands.values()) {
bru.addCommand(cmd);
}
}
}