xref: /aosp_15_r20/external/vulkan-validation-layers/scripts/check_commit_message_format.sh (revision b7893ccf7851cd6a48cc5a1e965257d8a5cdcc70)
1#!/bin/bash
2# Copyright (c) 2018 Valve Corporation
3# Copyright (c) 2018 LunarG, Inc.
4
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Checks commit messages against project standards in CONTRIBUTING.md document
18# Script to determine if commit messages in Pull Request are properly formatted.
19# Exits with non 0 exit code if reformatting is needed.
20
21# Disable subshells
22shopt -s lastpipe
23
24RED='\033[0;31m'
25GREEN='\033[0;32m'
26NC='\033[0m' # No Color
27
28# TRAVIS_COMMIT_RANGE contains range of commits for this PR
29
30# Get user-supplied commit message text for applicable commits and insert
31# a unique separator string identifier. The git command returns ONLY the
32# subject line and body for each of the commits.
33TRAVIS_COMMIT_RANGE="${TRAVIS_COMMIT_RANGE/.../..}"
34COMMIT_TEXT=$(git log ${TRAVIS_COMMIT_RANGE} --pretty=format:"XXXNEWLINEXXX"%n%B)
35
36# Bail if there are none
37if [ -z "${COMMIT_TEXT}" ]; then
38  echo -e "${GREEN}No commit messgages to check for formatting.${NC}"
39  exit 0
40elif ! echo $TRAVIS_COMMIT_RANGE | grep -q "\.\.\."; then
41  echo -e "${GREEN}No commit messgages to check for formatting.${NC}"
42  exit 0
43fi
44
45# Process commit messages
46success=1
47current_line=0
48prevline=""
49
50# Process each line of the commit message output, resetting counter on separator
51printf %s "$COMMIT_TEXT" | while IFS='' read -r line; do
52  # echo "Count = $current_line <Line> = $line"
53  current_line=$((current_line+1))
54  if [ "$line" = "XXXNEWLINEXXX" ]; then
55    current_line=0
56  fi
57  chars=${#line}
58  if [ $current_line -eq 1 ]; then
59    # Subject line should be 64 chars or less
60    if [ $chars -gt 64 ]; then
61      echo "The following subject line exceeds 64 characters in length."
62      echo "     '$line'"
63      success=0
64    fi
65    i=$(($chars-1))
66    last_char=${line:$i:1}
67    # Output error if last char of subject line is not alpha-numeric
68    if [[ $last_char =~ [.,] ]]; then
69      echo "For the following commit, the last character of the subject line must not be a period or comma."
70      echo "     '$line'"
71      success=0
72    fi
73    # Checking if subject line doesn't start with 'module: '
74    prefix=$(echo $line | cut -f1 -d " ")
75    if [ "${prefix: -1}" != ":" ]; then
76      echo "The following subject line must start with a single word specifying the functional area of the change, followed by a colon and space. I.e., 'layers: Subject line here'"
77      echo "     '$line'"
78      success=0
79    fi
80    # Check if first character after the colon is lower-case
81    subject=$(echo $line | cut -f2 -d " ")
82    firstchar=$(echo ${subject} | cut -c 1)
83    if [[ "${firstchar}" =~ [a-z] ]]; then
84      echo "The first word of the subject line after the ':' character must be capitalized."
85      echo "     '$line'"
86      success=0
87    fi
88  elif [ $current_line -eq 2 ]; then
89    # Commit message must have a blank line between subject and body
90    if [ $chars -ne 0 ]; then
91      echo "The following subject line must be followed by a blank line."
92      echo "     '$prevline'"
93      success=0
94    fi
95  else
96    # Lines in a commit message body must be less than 72 characters in length (but give some slack)
97    if [ $chars -gt 76 ]; then
98      echo "The following commit message body line exceeds the 72 character limit."
99      echo "'$line\'"
100      success=0
101    fi
102  fi
103  prevline=$line
104done
105
106if [ $success -eq 1 ]; then
107  echo -e "${GREEN}All commit messages in pull request are properly formatted.${NC}"
108  exit 0
109else
110  exit 1
111fi
112