Skip to content

Commit 5fcbd4e

Browse files
committed
Run the directory's checks before the commit, not after
Plugin Check is what the release gates on, and today it caught three things after they were pushed: an escaping sniff that cannot see through a variable, a translators comment one line too far from its string, and a deliberately versionless script. Each cost a red run and a fix commit. It turns out the tool runs locally in about a second against a WordPress install with the plugin-check plugin active, so the hook now runs it when one is at hand. Errors block the commit and warnings do not, which is the same line the workflow draws - what passes here passes there. Point CAPTCHAAPI_WP_PATH at an install or keep one beside the repo as ../wp-test; without either, the step is skipped like phpcs above it, so a fresh clone is unaffected.
1 parent 079fa18 commit 5fcbd4e

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

.githooks/pre-commit

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,42 @@ if command -v phpcs >/dev/null 2>&1 && [ -n "$staged_php" ]; then
7575
echo "$staged_php" | tr '\n' ' ' | xargs phpcs
7676
echo "✓ phpcs passed"
7777
fi
78+
79+
# ── 4. The directory's own checks, when a WordPress install is at hand ───────
80+
# The same tool the release workflow gates on, run against a local install with
81+
# the plugin-check plugin active. It takes about a second and catches the class
82+
# of thing that is invisible until CI says no: an escaping sniff that cannot see
83+
# through a variable, a translators comment one line too far from its string.
84+
#
85+
# Point CAPTCHAAPI_WP_PATH at an install, or keep one beside this repo as
86+
# ../wp-test. Skip a run with SKIP_PLUGIN_CHECK=1.
87+
#
88+
# Like phpcs above, this reads the working tree rather than the index - it needs
89+
# real files on disk to load the plugin.
90+
wp_path="${CAPTCHAAPI_WP_PATH:-$(git rev-parse --show-toplevel)/../wp-test}"
91+
92+
if [ -z "${SKIP_PLUGIN_CHECK:-}" ] && [ -d "$wp_path" ]; then
93+
if command -v wp >/dev/null 2>&1; then
94+
wp_cli=(wp)
95+
elif [ -f "$wp_path/wp-cli.phar" ]; then
96+
wp_cli=(php "$wp_path/wp-cli.phar")
97+
else
98+
wp_cli=()
99+
fi
100+
101+
if [ ${#wp_cli[@]} -gt 0 ]; then
102+
echo "▶ plugin check"
103+
check_output=$(cd "$wp_path" && "${wp_cli[@]}" plugin check captchaapi --format=csv \
104+
--exclude-directories=.github,.githooks,.wordpress-org,.claude,.idea,languages \
105+
--exclude-files=.gitignore,.distignore,.DS_Store,README.md 2>/dev/null || true)
106+
107+
# Errors block a release, warnings do not - the same line the workflow draws.
108+
if printf '%s' "$check_output" | grep -q ',ERROR,'; then
109+
echo "✗ plugin check found errors:" >&2
110+
printf '%s' "$check_output" | grep -B 4 ',ERROR,' >&2
111+
exit 1
112+
fi
113+
114+
echo "✓ plugin check passed"
115+
fi
116+
fi

0 commit comments

Comments
 (0)