Search in sources :

Example 1 with MagicFile

use of com.google.gerrit.server.patch.MagicFile 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);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MagicFile(com.google.gerrit.server.patch.MagicFile) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FixReplacement(com.google.gerrit.entities.FixReplacement)

Aggregations

FixReplacement (com.google.gerrit.entities.FixReplacement)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 MagicFile (com.google.gerrit.server.patch.MagicFile)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1