LCOV - code coverage report
Current view: top level - server/util - CommitMessageUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 28 89.3 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.util;
      16             : 
      17             : import static java.nio.charset.StandardCharsets.UTF_8;
      18             : 
      19             : import com.google.common.base.Strings;
      20             : import com.google.gerrit.common.Nullable;
      21             : import com.google.gerrit.entities.Change;
      22             : import com.google.gerrit.extensions.restapi.BadRequestException;
      23             : import java.security.NoSuchAlgorithmException;
      24             : import java.security.SecureRandom;
      25             : import java.util.Optional;
      26             : import java.util.regex.Matcher;
      27             : import java.util.regex.Pattern;
      28             : import org.eclipse.jgit.lib.Constants;
      29             : import org.eclipse.jgit.lib.ObjectId;
      30             : import org.eclipse.jgit.lib.ObjectInserter;
      31             : import org.eclipse.jgit.util.ChangeIdUtil;
      32             : 
      33             : /** Utility functions to manipulate commit messages. */
      34             : public class CommitMessageUtil {
      35             :   private static final SecureRandom rng;
      36          54 :   private static final Pattern changeIdFooterPattern =
      37          54 :       Pattern.compile("Change-Id: *(I[a-f0-9]{40})");
      38             : 
      39             :   static {
      40             :     try {
      41          54 :       rng = SecureRandom.getInstance("SHA1PRNG");
      42           0 :     } catch (NoSuchAlgorithmException e) {
      43           0 :       throw new IllegalStateException("Cannot create RNG for Change-Id generator", e);
      44          54 :     }
      45          54 :   }
      46             : 
      47             :   private CommitMessageUtil() {}
      48             : 
      49             :   /**
      50             :    * Checks for invalid (empty or containing \0) commit messages and appends a newline character to
      51             :    * the commit message.
      52             :    *
      53             :    * @throws BadRequestException if the commit message is null or empty
      54             :    * @return the trimmed message with a trailing newline character
      55             :    */
      56             :   public static String checkAndSanitizeCommitMessage(@Nullable String commitMessage)
      57             :       throws BadRequestException {
      58          30 :     String trimmed = Strings.nullToEmpty(commitMessage).trim();
      59          30 :     if (trimmed.isEmpty()) {
      60           2 :       throw new BadRequestException("Commit message cannot be null or empty");
      61             :     }
      62          29 :     if (trimmed.indexOf(0) >= 0) {
      63           1 :       throw new BadRequestException("Commit message cannot have NUL character");
      64             :     }
      65          29 :     trimmed = trimmed + "\n";
      66          29 :     return trimmed;
      67             :   }
      68             : 
      69             :   public static ObjectId generateChangeId() {
      70          50 :     byte[] rand = new byte[Constants.OBJECT_ID_STRING_LENGTH];
      71          50 :     rng.nextBytes(rand);
      72          50 :     String randomString = new String(rand, UTF_8);
      73             : 
      74          50 :     try (ObjectInserter f = new ObjectInserter.Formatter()) {
      75          50 :       return f.idFor(Constants.OBJ_COMMIT, Constants.encode(randomString));
      76             :     }
      77             :   }
      78             : 
      79             :   public static Change.Key generateKey() {
      80           9 :     return Change.key(getChangeIdFromObjectId(generateChangeId()));
      81             :   }
      82             : 
      83             :   public static String getChangeIdFromObjectId(ObjectId objectId) {
      84          10 :     return "I" + objectId.name();
      85             :   }
      86             : 
      87             :   /**
      88             :    * Return the value of Change-Id from the commit message footer.
      89             :    *
      90             :    * <p>The behaviour matches {@link org.eclipse.jgit.util.ChangeIdUtil}. If more than one matching
      91             :    * Change-Id footer is found, return the value of the last one.
      92             :    *
      93             :    * @param commitMessage commit message to get Change-Id from.
      94             :    * @return {@link Optional} value of Change-Id footer in the commit message.
      95             :    */
      96             :   public static Optional<String> getChangeIdFromCommitMessageFooter(String commitMessage) {
      97           7 :     int indexOfChangeId = ChangeIdUtil.indexOfChangeId(commitMessage, "\n");
      98           7 :     if (indexOfChangeId == -1) {
      99           6 :       return Optional.empty();
     100             :     }
     101           4 :     Matcher matcher = changeIdFooterPattern.matcher(commitMessage);
     102           4 :     if (matcher.find(indexOfChangeId)) {
     103           4 :       return Optional.of(matcher.group(1));
     104             :     }
     105           0 :     return Optional.empty();
     106             :   }
     107             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750