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. 33COMMIT_TEXT=$(git log ${TRAVIS_COMMIT_RANGE} --pretty=format:"XXXNEWLINEXXX"%n%B) 34 35# Bail if there are none 36if [ -z "${COMMIT_TEXT}" ]; then 37 echo -e "${GREEN}No commit messgages to check for formatting.${NC}" 38 exit 0 39elif ! echo $TRAVIS_COMMIT_RANGE | grep -q "\.\.\."; then 40 echo -e "${GREEN}No commit messgages to check for formatting.${NC}" 41 exit 0 42fi 43 44# Process commit messages 45success=1 46current_line=0 47prevline="" 48 49# Process each line of the commit message output, resetting counter on separator 50printf %s "$COMMIT_TEXT" | while IFS='' read -r line; do 51 # echo "Count = $current_line <Line> = $line" 52 current_line=$((current_line+1)) 53 if [ "$line" = "XXXNEWLINEXXX" ]; then 54 current_line=0 55 fi 56 chars=${#line} 57 if [ $current_line -eq 1 ]; then 58 # Subject line should be 50 chars or less (but give some slack here) 59 if [ $chars -gt 54 ]; then 60 echo "The following subject line exceeds 50 characters in length." 61 echo " '$line'" 62 success=0 63 fi 64 i=$(($chars-1)) 65 last_char=${line:$i:1} 66 # Output error if last char of subject line is not alpha-numeric 67 if [[ ! $last_char =~ [0-9a-zA-Z] ]]; then 68 echo "For the following commit, the last character of the subject line must not be non-alphanumeric." 69 echo " '$line'" 70 success=0 71 fi 72 # Checking if subject line doesn't start with 'module: ' 73 prefix=$(echo $line | cut -f1 -d " ") 74 if [ "${prefix: -1}" != ":" ]; then 75 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'" 76 echo " '$line'" 77 success=0 78 fi 79 elif [ $current_line -eq 2 ]; then 80 # Commit message must have a blank line between subject and body 81 if [ $chars -ne 0 ]; then 82 echo "The following subject line must be followed by a blank line." 83 echo " '$prevline'" 84 success=0 85 fi 86 else 87 # Lines in a commit message body must be less than 72 characters in length (but give some slack) 88 if [ $chars -gt 76 ]; then 89 echo "The following commit message body line exceeds the 72 character limit." 90 echo "'$line\'" 91 success=0 92 fi 93 fi 94 prevline=$line 95done 96 97if [ $success -eq 1 ]; then 98 echo -e "${GREEN}All commit messages in pull request are properly formatted.${NC}" 99 exit 0 100else 101 exit 1 102fi 103