forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-setup-pre-commit-hook
More file actions
executable file
·36 lines (32 loc) · 1.13 KB
/
git-setup-pre-commit-hook
File metadata and controls
executable file
·36 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
#
# Setup Git pre-commit hook.
#
# stop when composer.json file is not found
if [[ ! -f "composer.json" ]]; then
echo "unable to locate composer.json file."
exit 1
fi
# determine the phpcs binary location
if [[ -f "vendor/bin/phpcs" ]]; then
phpcs_bin="./vendor/bin/phpcs"
elif [[ -n $COMPOSER_BIN_DIR && -f "$COMPOSER_BIN_DIR/phpcs" ]]; then
phpcs_bin="./$COMPOSER_BIN_DIR/phpcs"
elif [[ -n $COMPOSER_VENDOR_DIR && -f "$COMPOSER_VENDOR_DIR/bin/phpcs" ]]; then
phpcs_bin="./$COMPOSER_VENDOR_DIR/bin/phpcs"
else
echo "pre-commit hook: unable to locate phpcs binary file."
exit
fi
if git --version &>/dev/null; then
pre_commit_file="$(git rev-parse --git-dir)/hooks/pre-commit"
else
pre_commit_file=".git/hooks/pre-commit"
fi
# create the pre-commit file or notify to add command if it already exists
if [[ ! -f "$pre_commit_file" ]]; then
cat utils/git-pre-commit-script | sed 's|{{PHPCS_BIN}}|'$phpcs_bin'|' > $pre_commit_file
chmod +x $pre_commit_file
elif ! grep -q "phpcs" $pre_commit_file; then
echo -e "hook file already exists: $pre_commit_file\n\nplease add the below command to your pre-commit file:\n\n$phpcs_bin"
fi