Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 25 additions & 91 deletions pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
# it wants to stop the commit.
#
# Author : Jacques Raphanel
# Version: 1.9
# Version: 2.0
#

# File containing the commit message
# Current branch name
BRANCHNAME=$(git rev-parse --abbrev-ref HEAD)
# Predefined list of supported commit types
TYPELIST="chore|ci|fix|bugfix|hotfix|doc(s)?|experimental|build|feat(ure)?|refactor|revert|style|perf|release"
# Project root directory
ROOTDIR=$PWD

Expand All @@ -25,106 +23,53 @@ echo_err() {
printf "${COLOR_ERROR}%s${COLOR_RESET}\n" "$@" >&2
}

# Report invalid commit type error
report_invalid_type() {
echo_err "Invalid commit type detected from branch name !"
echo_err ""
echo_err "The list of supported commit types is:"
echo_err " => $(echo $TYPELIST | sed -e 's:|: :g')"
exit 1
}

# Report invalid JIRA ref error
report_invalid_jira_ref() {
echo_err "Invalid JIRA reference detected from branch name !"
echo_err ""
echo_err "Expected format: <type>/<subject>/<jira_ref>"
echo_err ""
echo_err "Examples:"
echo_err "no-ref"
echo_err "REW-374"
echo_err "DS-241"
exit 1
}


# Report invalid branch name format
report_invalid_format() {
echo_err "Invalid branch name!"
echo_err "Please rename your branch and try to commit again."
echo_err ""
echo_err "Format: <type>/<subject>/<jira_ref>"
echo_err "Format: <linear-ticket>-<scope>-<short-description> (all lower kebab-case)"
echo_err "Regex : ^eng-[0-9]+-[a-z0-9]+(-[a-z0-9]+)*\$"
echo_err ""
echo_err "where jira_ref can be no-ref if not associated to a jira ticket"
echo_err "or omitted in case of experimental or release branch type"
echo_err "Example: eng-145-extranet-front-document-preview"
exit 1
}

# Check that the branch name is following company's conventions
# Check that the branch name follows the company's Linear convention
check_branch_name() {
# Build the regular expression to validate the branch name
local COMMITRE="^(${TYPELIST})" # Branch type
COMMITRE="${COMMITRE}/[a-z].*" # Description/Subject
COMMITRE="${COMMITRE}/(no-ref|[A-Z]{2,6}-[0-9]+)\$" # JIRA reference
# Linear convention: <linear-ticket>-<scope>-<short-description>
# e.g. eng-145-extranet-front-document-preview
local BRANCHRE="^eng-[0-9]+-[a-z0-9]+(-[a-z0-9]+)*\$"

if echo $BRANCHNAME | grep -Eq "$COMMITRE" >& /dev/null; then
if echo "$BRANCHNAME" | grep -Eq "$BRANCHRE"; then
return
fi

# Apply specific logic in case of specific branches:
# Allow special-purpose branches that are not feature branches
case "$BRANCHNAME" in
HEAD)
# New project or rebase from specific Git Hash
# Detached HEAD (new project or rebase from a specific git hash)
return
;;
main|master|develop)
# main branch from github project
# Long-lived integration branches
return
;;
preprod|staging)
# specific branch for deployment purpose
# Deployment branches
return
;;
release/*)
# specific release branch
if echo $BRANCHNAME | grep -Eq "release/[0-9.]+x\$" >& /dev/null; then
return
fi
echo_err "release branch name must be formatted using 'release/MAJOR.MINOR.x' format"
exit 1
# Release / maintenance branches (e.g. release/1.2.x, release/extranet-legacy)
return
;;
experimental/*)
# Throwaway experimental / preprod branches
return
;;
esac

if [ `echo $BRANCHNAME | grep -c '/'` -eq 0 ]; then
report_invalid_format
fi

# Decompose the branch name into simple fields
local BRANCHTYPE=$(echo $BRANCHNAME | cut -d/ -f1)
local SUBJECT=$(echo $BRANCHNAME | cut -d/ -f2)
local JIRAISSUE=$(echo $BRANCHNAME | cut -d/ -f3)

# check if it is related to an unsupported type
COMMITRE="^(${TYPELIST})(/.*)?$"
if ! echo $BRANCHTYPE | grep -Eq "$COMMITRE"; then
report_invalid_type
fi

# ensure that subject is defined
if [ -z "${SUBJECT// /}" ]; then
report_invalid_format
fi

# double-check JIRA issue definition
if [ -z "${JIRAISSUE// /}" ]; then
if [ "$BRANCHTYPE" = "experimental" ]; then
# JIRA issue not required in case of experimental branch
return
fi
else
if ! echo $JIRAISSUE | grep -Eq "^(no-ref|[A-Z]{2,6}-[0-9]+)$"; then
report_invalid_jira_ref
fi
fi
report_invalid_format
}

# Execute php-cs-fixer with fix mode
Expand All @@ -134,37 +79,26 @@ run_pre_commit() {
exit $?
fi
files=$(git diff --cached --name-only --diff-filter=ACM)
if [ "$files" != "" ]; then
if [ "$files" != "" ]; then
git add $files
fi
}

# Validate the branch name
# Validate the branch name and run the project pre-commit checks
main() {
# Check if the branch message follow our conventions or not
# Check if the branch name follows our conventions or not
check_branch_name

if [ -r Makefile ]; then
if [ `grep -Ec '^pre-commit:' Makefile` -gt 0 ]; then
run_pre_commit
fi
fi

}

# Check if commit types are locally customized
if [ -r "$ROOTDIR/.github/conventions/commit_types.conf" ]; then
TYPELIST=$(sed -z 's:\n:|:g' "$ROOTDIR/.github/conventions/commit_types.conf")
if [ -z "$TYPELIST" ]; then
echo_err "Invalid commit_types.conf file detected"
exit 1
fi
fi

# Perform all required checks
main

exit 0

# vim:set tabstop=4 shiftwidth=4 expandtab autoindent: