Skip to content

Commit

Permalink
OXDEV-7675: Add pre-commit hook to execute static analysis before commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid authored and RahatHameed committed Dec 13, 2023
1 parent 8febff8 commit d3ffe27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
}
},
"scripts": {
"post-install-cmd": "git-hooks/init.sh",
"post-update-cmd": "git-hooks/init.sh",
"phpcs": "phpcs --standard=tests/phpcs.xml",
"phpcbf": "phpcbf --standard=tests/phpcs.xml",

Expand Down
29 changes: 29 additions & 0 deletions git-hooks/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e

function setup_git_hooks()
{
echo "Initialising git hooks..."

MODULE_PATH=$(git rev-parse --show-toplevel)
PRE_COMMIT_HOOK_FILE_PATH=$MODULE_PATH/.git/hooks/pre-commit

echo "Add hook command to pre-commit file $PRE_COMMIT_HOOK_FILE_PATH"

if ! [ -f "${PRE_COMMIT_HOOK_FILE_PATH}" ];then
echo $'#!/bin/bash\n' >> $PRE_COMMIT_HOOK_FILE_PATH
fi

COMPOSER_STATIC_COMMAND="docker compose exec -T --workdir $MODULE_PATH php composer static"

if grep -q "$COMPOSER_STATIC_COMMAND" "$PRE_COMMIT_HOOK_FILE_PATH"; then
echo "Command has already been added"
return
fi

echo "$COMPOSER_STATIC_COMMAND" >> $PRE_COMMIT_HOOK_FILE_PATH
chmod +x $PRE_COMMIT_HOOK_FILE_PATH
}

setup_git_hooks

0 comments on commit d3ffe27

Please sign in to comment.