Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGOV-554: Githook pre-commit config #60

Merged
merged 19 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions .githooks/commit-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
54 changes: 54 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
echo "Executing .git/hooks/pre-commit..."
VIOLATIONS=0

# Check backend
STANDARD="Drupal"
BIN="./vendor/bin"

echo
echo "Drupal Coder pre-commit hook – commit with the --no-verify option to skip the hook"
echo

# Check whether PHP_CodeSniffer can be found
if [ ! -f "$BIN/phpcs" ]
then
echo "Drupal Coder not found – is it installed? 'composer require drupal/coder'"
echo
exit 1
fi

# Run the sniffer
echo "Running Drupal Coder."
echo
PHPCS=("$BIN/phpcs" "--standard=$STANDARD" "--filter=gitstaged" "--encoding=utf-8" "-p" ".")
"${PHPCS[@]}"
VIOLATIONS=$((VIOLATIONS + $?))

# Check frontend
echo "Running Frontend Lints."
echo
cd src/frontend || exit 1
npm run lint-staged
# Add frontend violations to the total
VIOLATIONS=$((VIOLATIONS + $?))

# One or more violations detected
if [ $VIOLATIONS != 0 ]
then
echo "Please fix each violations detected."
echo
exit 1
fi

# Syntax OK
if [ $VIOLATIONS == 0 ]
then
echo "No violations detected"
echo
exit 0
fi

git update-index --again


3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ vendor

# testing
/coverage
/tests/cypress/screenshots

# production
/build
/db/
/keys

# misc
.DS_Store
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
}
],
"scripts": {
"phpcs": "phpcs --standard=phpcs.xml"
"phpcs": "phpcs --standard=phpcs.xml",
"post-install-cmd": [
"php -r \"copy('.githooks/pre-commit', '.git/hooks/pre-commit');\"",
"php -r \"chmod('.git/hooks/pre-commit', 0755);\""
]
},
"require": {
"composer/installers": "^2.0",
"cweagans/composer-patches": "^1.7",
"drupal/better_exposed_filters": "^7.0",
"drupal/cer": "^5.0@beta",
"drupal/coder": "^8.3",
"drupal/core-composer-scaffold": "^10.3",
"drupal/core-project-message": "^10.3",
"drupal/core-recommended": "^10.3",
Expand Down
Loading