use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method shiftRangesBy.
private static FixReplacement shiftRangesBy(FixReplacement fixReplacement, int shiftedAmount) {
Range adjustedRange = new Range(fixReplacement.range);
adjustedRange.startLine += shiftedAmount;
adjustedRange.endLine += shiftedAmount;
return new FixReplacement(fixReplacement.path, adjustedRange, fixReplacement.replacement);
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method getNewCommitMessage.
private static String getNewCommitMessage(Repository repository, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws ResourceConflictException, IOException {
try (ObjectReader reader = repository.newObjectReader()) {
// In the magic /COMMIT_MSG file, the actual commit message is placed after some generated
// header lines. -> Need to find out to which actual line of the commit message a replacement
// refers.
MagicFile commitMessageFile = MagicFile.forCommitMessage(reader, patchSetCommitId);
int commitMessageStartLine = commitMessageFile.getStartLineOfModifiableContent();
// Line numbers are 1-based. -> Add 1 to not move first line.
// Move up for any additionally found lines.
int necessaryRangeShift = -commitMessageStartLine + 1;
ImmutableList<FixReplacement> adjustedReplacements = shiftRangesBy(fixReplacements, necessaryRangeShift);
if (referToNonPositiveLine(adjustedReplacements)) {
throw new ResourceConflictException(String.format("The header of the %s file cannot be modified.", Patch.COMMIT_MSG));
}
String commitMessage = commitMessageFile.modifiableContent();
return FixCalculator.getNewFileContent(commitMessage, adjustedReplacements);
}
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class PostReview method ensureFixReplacementsAreAddable.
private static void ensureFixReplacementsAreAddable(String commentPath, List<FixReplacementInfo> fixReplacementInfos) throws BadRequestException {
ensureReplacementsArePresent(commentPath, fixReplacementInfos);
for (FixReplacementInfo fixReplacementInfo : fixReplacementInfos) {
ensureReplacementPathIsSetAndNotPatchsetLevel(commentPath, fixReplacementInfo.path);
ensureRangeIsSet(commentPath, fixReplacementInfo.range);
ensureRangeIsValid(commentPath, fixReplacementInfo.range);
ensureReplacementStringIsSet(commentPath, fixReplacementInfo.replacement);
}
Map<String, List<FixReplacementInfo>> replacementsPerFilePath = fixReplacementInfos.stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
for (List<FixReplacementInfo> sameFileReplacements : replacementsPerFilePath.values()) {
ensureRangesDoNotOverlap(commentPath, sameFileReplacements);
}
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class GetFixPreview method apply.
@Override
public Response<Map<String, DiffInfo>> apply(FixResource resource) throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException, AuthException, IOException, InvalidChangeOperationException {
Map<String, DiffInfo> result = new HashMap<>();
PatchSet patchSet = resource.getRevisionResource().getPatchSet();
ChangeNotes notes = resource.getRevisionResource().getNotes();
Change change = notes.getChange();
ProjectState state = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
Map<String, List<FixReplacement>> fixReplacementsPerFilePath = resource.getFixReplacements().stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
try {
try (Repository git = repoManager.openRepository(notes.getProjectName())) {
for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
String fileName = entry.getKey();
DiffInfo diffInfo = getFixPreviewForSingleFile(git, patchSet, state, notes, fileName, ImmutableList.copyOf(entry.getValue()));
result.put(fileName, diffInfo);
}
}
} catch (NoSuchChangeException e) {
throw new ResourceNotFoundException(e.getMessage(), e);
} catch (LargeObjectException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
return Response.ok(result);
}
use of com.google.gerrit.entities.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsCanModifySeveralFilesInAnyOrder.
@Test
public void replacementsCanModifySeveralFilesInAnyOrder() throws Exception {
FixReplacement fixReplacement1 = new FixReplacement(filePath1, new Range(1, 1, 3, 2), "Modified content");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
FixReplacement fixReplacement2 = new FixReplacement(filePath2, new Range(2, 0, 3, 0), "First modification\n");
FixReplacement fixReplacement3 = new FixReplacement(filePath2, new Range(3, 0, 4, 0), "Second modification\n");
mockFileContent(filePath2, "1st line\n2nd line\n3rd line\n");
CommitModification commitModification = toCommitModification(fixReplacement3, fixReplacement1, fixReplacement2);
List<TreeModification> sortedTreeModifications = getSortedCopy(commitModification.treeModifications());
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().newContent().isEqualTo("FModified contentird line\n");
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().newContent().isEqualTo("1st line\nFirst modification\nSecond modification\n");
}
Aggregations