Skip to content

Commit

Permalink
scm: Use Conventional Commits
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Feb 20, 2022
1 parent 72082b4 commit 2d6fbc0
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 68 deletions.
83 changes: 83 additions & 0 deletions devtools/check-git-commit-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2022 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

SOURCEDIR="${SCRIPTDIR}/commit-msg"
HOOKDIR="${SCRIPTDIR}/../.git/hooks"

if [ ! -f "${HOOKDIR}/commit-msg.sample" ];
then
echo "Cannot find ${HOOKDIR}/commit-msg.sample"
exit 0
fi

function listFiles() {
cd "${SOURCEDIR}" || die "Unable to enter ${SOURCEDIR}"
find . -type f | grep -F commit-msg | grep -F -v sample | sort
}

function calculateHashes() {
DIR=$1
cd "${DIR}" || die "Unable to enter ${DIR}"
listFiles | xargs md5sum
}

function calculateHash() {
DIR=$1
cd "${DIR}" || die "Unable to enter ${DIR}"
calculateHashes "${DIR}" | md5sum | cut -d' ' -f1
}

SCRIPTHASH=$(calculateHash "${SCRIPTDIR}/commit-msg" )
INSTALLEDHASH=$(calculateHash "${HOOKDIR}")

if [ "$SCRIPTHASH" == "$INSTALLEDHASH" ]
then
echo "Git hooks are in place."
else

#https://wiki.archlinux.org/index.php/Color_Bash_Prompt
# Reset
export Color_Off='\e[0m' # Text Reset

# High Intensity
export IRed='\e[0;91m' # Red
export IYellow='\e[0;93m' # Yellow
export IBlue='\e[0;94m' # Blue
export IWhite='\e[0;97m' # White

# Bold High Intensity
export BIRed='\e[1;91m' # Red
export BIBlue='\e[1;94m' # Blue

echo -e "${Color_Off}"
echo -e "${IWhite}[${BIRed}WARN${IWhite}] ${IYellow}/========================================================================\\"
echo -e "${IWhite}[${BIRed}WARN${IWhite}] ${IYellow}| ${BIRed}git commit hooks need to be updated !${IYellow} |"
echo -e "${IWhite}[${BIRed}WARN${IWhite}] ${IYellow}| ${BIRed}Use ${SCRIPTDIR}/commit-msg/install.sh to install them.${IYellow} |"
echo -e "${IWhite}[${BIRed}WARN${IWhite}] ${IYellow}\\========================================================================/"
echo -e "${Color_Off}"

calculateHashes "${SOURCEDIR}" > /tmp/checkCommitHooks_source_$$
calculateHashes "${HOOKDIR}" > /tmp/checkCommitHooks_hook_$$

echo "Difference between source(<) and hookdir(>)"
diff /tmp/checkCommitHooks_source_$$ /tmp/checkCommitHooks_hook_$$
rm /tmp/checkCommitHooks_source_$$ /tmp/checkCommitHooks_hook_$$
fi

54 changes: 54 additions & 0 deletions devtools/commit-msg/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2022 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Client side pre-commit hook to assist the committers to ensure the commit messages
# follow the chosen convention.

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

function die() {
#https://wiki.archlinux.org/index.php/Color_Bash_Prompt
# Reset
export Color_Off='\e[0m' # Text Reset

# High Intensity
export IRed='\e[0;91m' # Red
export IYellow='\e[0;93m' # Yellow
export IBlue='\e[0;94m' # Blue
export IWhite='\e[0;97m' # White

# Bold High Intensity
export BIRed='\e[1;91m' # Red
export BIBlue='\e[1;94m' # Blue

echo -e "${Color_Off}"
echo -e "${IWhite}[${BIRed}FATAL${IWhite}] ${IYellow}/========================================================================"
echo -e "${IWhite}[${BIRed}FATAL${IWhite}] ${IYellow}| ${BIRed} ---------->>> THE CHANGES WERE NOT COMMITTED ! <<<---------- ${IYellow}"
echo -e "${IWhite}[${BIRed}FATAL${IWhite}] ${IYellow}| ${BIRed} $@ ${IYellow}"
echo -e "${IWhite}[${BIRed}FATAL${IWhite}] ${IYellow}\\========================================================================"
echo -e "${Color_Off}"
exit 1
}

for check in "${SCRIPTDIR}"/commit-msg.d/*sh; do
. ${check}
done

echo "=========================="

exit 0
21 changes: 21 additions & 0 deletions devtools/commit-msg/commit-msg.d/do-not-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2022 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Fail if there is a DONOTCOMMIT in one of the files to be committed
git-diff-index -p -M --cached HEAD -- | grep '^+' | grep -F DONOTCOMMIT && die "Blocking commit because string DONOTCOMMIT detected in patch: \n$(git diff --cached | fgrep -B2 -A2 DONOTCOMMIT)\n"
28 changes: 28 additions & 0 deletions devtools/commit-msg/commit-msg.d/one-line-msg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2022 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

COMMIT_MSG_FILE=$1

COMMIT_LINES=$(cat "${COMMIT_MSG_FILE}" | grep . | grep -v '^#' | wc -l)
if [ "${COMMIT_LINES}" -eq 1 ];
then
echo "Single line commit message: Ok"
else
echo "Single line commit message: Fail: Found ${COMMIT_LINES} lines"
exit 1
fi
21 changes: 21 additions & 0 deletions devtools/commit-msg/commit-msg.d/sailr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"enabled": true,
"revert": true,
"length": {
"min": 1,
"max": 52
},
"types": [
"build",
"scm",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"style",
"test",
"chore"
]
}
103 changes: 103 additions & 0 deletions devtools/commit-msg/commit-msg.d/sailr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash

release_tag=master
sailr_repo="https://github.com/craicoverflow/sailr/tree/$release_tag"

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

# checks that jq is usable
function check_jq_exists_and_executable {
if ! [ -x "$(command -v jq)" ]; then
echo -e "\`commit-msg\` hook failed. Please install jq."
exit 1
fi
}

# check if the config file exists
# if it doesnt we dont need to run the hook
function check_sailr_config {
if [[ ! -f "$CONFIG" ]]; then
echo -e "Sailr config file is missing. To set one see $sailr_repo#usage"
exit 0
fi
}

function set_config {
local_config="$SCRIPTDIR/sailr.json"

if [ -f "$local_config" ]; then
CONFIG=$local_config
elif [ -n "$SAILR_CONFIG" ]; then
CONFIG=$SAILR_CONFIG
fi
}

# set values from config file to variables
function set_config_values() {
enabled=$(jq -r .enabled "$CONFIG")

if [[ ! $enabled ]]; then
exit 0
fi

revert=$(jq -r .revert "$CONFIG")
types=($(jq -r '.types[]' "$CONFIG"))
min_length=$(jq -r .length.min "$CONFIG")
max_length=$(jq -r .length.max "$CONFIG")
}

# build the regex pattern based on the config file
function build_regex() {
set_config_values

regexp="^[.0-9]+$|"

if $revert; then
regexp="${regexp}^([Rr]elease|[Rr]evert|[Mm]erge):? )?.*$|^("
fi

for type in "${types[@]}"
do
regexp="${regexp}$type|"
done

regexp="${regexp%|})(\(.+\))?: "

regexp="${regexp}.{$min_length,$max_length}$"
}


# Print out a standard error message which explains
# how the commit message should be structured
function print_error() {
commit_message=$1
regular_expression=$2
echo -e "\n\e[31m[Invalid Commit Message]"
echo -e "------------------------\033[0m\e[0m"
echo -e "Valid types : \e[36m${types[@]}\033[0m"
echo -e "Max length (first line): \e[36m$max_length\033[0m"
echo -e "Min length (first line): \e[36m$min_length\033[0m"
echo -e "\e[37mRegex : \e[33m$regular_expression\033[0m\n"
echo -e "\e[37mCommit message : \e[33m\"$commit_message\"\033[0m"
echo -e "\e[37mCommit message length : \e[33m$(echo $commit_message | wc -c)\033[0m\n"
}

set_config

# check if the repo has a sailr config file
check_sailr_config

# make sure jq is installed
check_jq_exists_and_executable

# get the first line of the commit message
INPUT_FILE=$1
START_LINE=$(head -n1 $INPUT_FILE)

build_regex

if [[ ! $START_LINE =~ $regexp ]]; then
# commit message is invalid according to config - block commit
print_error "$START_LINE" "$regexp"
exit 1
fi
31 changes: 31 additions & 0 deletions devtools/commit-msg/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# Yet Another UserAgent Analyzer
# Copyright (C) 2013-2022 Niels Basjes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

HOOKDIR="${SCRIPTDIR}/../../.git/hooks"

if [ ! -f "${HOOKDIR}/commit-msg.sample" ];
then
echo "Cannot find ${HOOKDIR}/commit-msg.sample"
exit 1
fi

cp -a commit-msg* ${HOOKDIR}/
chmod 755 ${HOOKDIR}/commit-msg
echo "Installation completed"
Loading

0 comments on commit 2d6fbc0

Please sign in to comment.