Dissolve gerrit-server top-level directory

Change-Id: I538512dfe0f1bea774c01fdd45fa410a45634011
This commit is contained in:
David Ostrovsky
2017-09-21 08:37:42 +02:00
committed by Dave Borowitz
parent 472396c797
commit 376a7bbb64
1549 changed files with 342 additions and 335 deletions

View File

@@ -0,0 +1,154 @@
// Copyright (C) 2017 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.fixes;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.gerrit.common.RawInputUtil;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.reviewdb.client.Comment;
import com.google.gerrit.reviewdb.client.FixReplacement;
import com.google.gerrit.server.change.FileContentUtil;
import com.google.gerrit.server.edit.tree.ChangeFileContentModification;
import com.google.gerrit.server.edit.tree.TreeModification;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
/** An interpreter for {@code FixReplacement}s. */
@Singleton
public class FixReplacementInterpreter {
private static final Comparator<FixReplacement> ASC_RANGE_FIX_REPLACEMENT_COMPARATOR =
Comparator.comparing(fixReplacement -> fixReplacement.range);
private final FileContentUtil fileContentUtil;
@Inject
public FixReplacementInterpreter(FileContentUtil fileContentUtil) {
this.fileContentUtil = fileContentUtil;
}
/**
* Transforms the given {@code FixReplacement}s into {@code TreeModification}s.
*
* @param repository the affected Git repository
* @param projectState the affected project
* @param patchSetCommitId the patch set which should be modified
* @param fixReplacements the replacements which should be applied
* @return a list of {@code TreeModification}s representing the given replacements
* @throws ResourceNotFoundException if a file to which one of the replacements refers doesn't
* exist
* @throws ResourceConflictException if the replacements can't be transformed into {@code
* TreeModification}s
*/
public List<TreeModification> toTreeModifications(
Repository repository,
ProjectState projectState,
ObjectId patchSetCommitId,
List<FixReplacement> fixReplacements)
throws ResourceNotFoundException, IOException, ResourceConflictException {
checkNotNull(fixReplacements, "Fix replacements must not be null");
Map<String, List<FixReplacement>> fixReplacementsPerFilePath =
fixReplacements
.stream()
.collect(Collectors.groupingBy(fixReplacement -> fixReplacement.path));
List<TreeModification> treeModifications = new ArrayList<>();
for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
TreeModification treeModification =
toTreeModification(
repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
treeModifications.add(treeModification);
}
return treeModifications;
}
private TreeModification toTreeModification(
Repository repository,
ProjectState projectState,
ObjectId patchSetCommitId,
String filePath,
List<FixReplacement> fixReplacements)
throws ResourceNotFoundException, IOException, ResourceConflictException {
String fileContent = getFileContent(repository, projectState, patchSetCommitId, filePath);
String newFileContent = getNewFileContent(fileContent, fixReplacements);
return new ChangeFileContentModification(filePath, RawInputUtil.create(newFileContent));
}
private String getFileContent(
Repository repository, ProjectState projectState, ObjectId patchSetCommitId, String filePath)
throws ResourceNotFoundException, IOException {
try (BinaryResult fileContent =
fileContentUtil.getContent(repository, projectState, patchSetCommitId, filePath)) {
return fileContent.asString();
}
}
private static String getNewFileContent(String fileContent, List<FixReplacement> fixReplacements)
throws ResourceConflictException {
List<FixReplacement> sortedReplacements = new ArrayList<>(fixReplacements);
sortedReplacements.sort(ASC_RANGE_FIX_REPLACEMENT_COMPARATOR);
LineIdentifier lineIdentifier = new LineIdentifier(fileContent);
StringModifier fileContentModifier = new StringModifier(fileContent);
for (FixReplacement fixReplacement : sortedReplacements) {
Comment.Range range = fixReplacement.range;
try {
int startLineIndex = lineIdentifier.getStartIndexOfLine(range.startLine);
int startLineLength = lineIdentifier.getLengthOfLine(range.startLine);
int endLineIndex = lineIdentifier.getStartIndexOfLine(range.endLine);
int endLineLength = lineIdentifier.getLengthOfLine(range.endLine);
if (range.startChar > startLineLength || range.endChar > endLineLength) {
throw new ResourceConflictException(
String.format(
"Range %s refers to a non-existent offset (start line length: %s,"
+ " end line length: %s)",
toString(range), startLineLength, endLineLength));
}
int startIndex = startLineIndex + range.startChar;
int endIndex = endLineIndex + range.endChar;
fileContentModifier.replace(startIndex, endIndex, fixReplacement.replacement);
} catch (StringIndexOutOfBoundsException e) {
// Most of the StringIndexOutOfBoundsException should never occur because we reject fix
// replacements for invalid ranges. However, we can't cover all cases for efficiency
// reasons. For instance, we don't determine the number of lines in a file. That's why we
// need to map this exception and thus provide a meaningful error.
throw new ResourceConflictException(
String.format("Cannot apply fix replacement for range %s", toString(range)), e);
}
}
return fileContentModifier.getResult();
}
private static String toString(Comment.Range range) {
return String.format(
"(%s:%s - %s:%s)", range.startLine, range.startChar, range.endLine, range.endChar);
}
}

View File

@@ -0,0 +1,110 @@
// Copyright (C) 2017 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.fixes;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* An identifier of lines in a string. Lines are sequences of characters which are separated by any
* Unicode linebreak sequence as defined by the regular expression {@code \R}. If data for several
* lines is requested, calls which are ordered according to ascending line numbers are the most
* efficient.
*/
class LineIdentifier {
private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("\\R");
private final Matcher lineSeparatorMatcher;
private int nextLineNumber;
private int nextLineStartIndex;
private int currentLineStartIndex;
private int currentLineEndIndex;
LineIdentifier(String string) {
checkNotNull(string);
lineSeparatorMatcher = LINE_SEPARATOR_PATTERN.matcher(string);
reset();
}
/**
* Returns the start index of the indicated line within the given string. Start indices are
* zero-based while line numbers are one-based.
*
* <p><b>Note:</b> Requesting data for several lines is more efficient if those calls occur with
* increasing line number.
*
* @param lineNumber the line whose start index should be determined
* @return the start index of the line
* @throws StringIndexOutOfBoundsException if the line number is negative, zero or greater than
* the identified number of lines
*/
public int getStartIndexOfLine(int lineNumber) {
findLine(lineNumber);
return currentLineStartIndex;
}
/**
* Returns the length of the indicated line in the given string. The character(s) used to separate
* lines aren't included in the count. Line numbers are one-based.
*
* <p><b>Note:</b> Requesting data for several lines is more efficient if those calls occur with
* increasing line number.
*
* @param lineNumber the line whose length should be determined
* @return the length of the line
* @throws StringIndexOutOfBoundsException if the line number is negative, zero or greater than
* the identified number of lines
*/
public int getLengthOfLine(int lineNumber) {
findLine(lineNumber);
return currentLineEndIndex - currentLineStartIndex;
}
private void findLine(int targetLineNumber) {
if (targetLineNumber <= 0) {
throw new StringIndexOutOfBoundsException("Line number must be positive");
}
if (targetLineNumber < nextLineNumber) {
reset();
}
while (nextLineNumber < targetLineNumber + 1 && lineSeparatorMatcher.find()) {
currentLineStartIndex = nextLineStartIndex;
currentLineEndIndex = lineSeparatorMatcher.start();
nextLineStartIndex = lineSeparatorMatcher.end();
nextLineNumber++;
}
// End of string
if (nextLineNumber == targetLineNumber) {
currentLineStartIndex = nextLineStartIndex;
currentLineEndIndex = lineSeparatorMatcher.regionEnd();
}
if (nextLineNumber < targetLineNumber) {
throw new StringIndexOutOfBoundsException(
String.format("Line %d isn't available", targetLineNumber));
}
}
private void reset() {
nextLineNumber = 1;
nextLineStartIndex = 0;
currentLineStartIndex = 0;
currentLineEndIndex = 0;
lineSeparatorMatcher.reset();
}
}

View File

@@ -0,0 +1,77 @@
// Copyright (C) 2017 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.fixes;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A modifier of a string. It allows to replace multiple parts of a string by indicating those parts
* with indices based on the unmodified string. There is one limitation though: Replacements which
* affect lower indices of the string must be specified before replacements for higher indices.
*/
class StringModifier {
private final StringBuilder stringBuilder;
private int characterShift = 0;
private int previousEndOffset = Integer.MIN_VALUE;
StringModifier(String string) {
checkNotNull(string, "string must not be null");
stringBuilder = new StringBuilder(string);
}
/**
* Replaces part of the string with another content. When called multiple times, the calls must be
* ordered according to increasing start indices. Overlapping replacement regions aren't
* supported.
*
* @param startIndex the beginning index in the unmodified string (inclusive)
* @param endIndex the ending index in the unmodified string (exclusive)
* @param replacement the string which should be used instead of the original content
* @throws StringIndexOutOfBoundsException if the start index is smaller than the end index of a
* previous call of this method
*/
public void replace(int startIndex, int endIndex, String replacement) {
checkNotNull(replacement, "replacement string must not be null");
if (previousEndOffset > startIndex) {
throw new StringIndexOutOfBoundsException(
String.format(
"Not supported to replace the content starting at index %s after previous "
+ "replacement which ended at index %s",
startIndex, previousEndOffset));
}
int shiftedStartIndex = startIndex + characterShift;
int shiftedEndIndex = endIndex + characterShift;
if (shiftedEndIndex > stringBuilder.length()) {
throw new StringIndexOutOfBoundsException(
String.format("end %s > length %s", shiftedEndIndex, stringBuilder.length()));
}
stringBuilder.replace(shiftedStartIndex, shiftedEndIndex, replacement);
int replacedContentLength = endIndex - startIndex;
characterShift += replacement.length() - replacedContentLength;
previousEndOffset = endIndex;
}
/**
* Returns the modified string including all specified replacements.
*
* @return the modified string
*/
public String getResult() {
return stringBuilder.toString();
}
}