|
5 | 5 | # pre-commit hook wrapper that runs 'spotless:apply' to format the Java sources of every Maven |
6 | 6 | # project under java/. |
7 | 7 | # |
8 | | -# Most cuvs contributors do not work on the Java client and do not have Maven installed. For them |
9 | | -# (running outside CI without Maven) this skips gracefully, so that 'pre-commit run --all-files' |
10 | | -# does not require every contributor to install Maven. In CI, Maven is expected to be available and |
11 | | -# its absence is treated as an error. |
| 8 | +# Most cuvs contributors do not work on the Java client and do not have Maven installed. For them, |
| 9 | +# running 'pre-commit run --all-files' matches every Java source file in the repo regardless of |
| 10 | +# whether they touched any of it, so this skips gracefully when Maven is missing and there are no |
| 11 | +# actual local changes to Java sources. In CI, and for anyone who has actually modified Java |
| 12 | +# sources locally, Maven is expected to be available and its absence is treated as an error. |
12 | 13 |
|
13 | 14 | set -euo pipefail |
14 | 15 |
|
| 16 | +# Keep these in sync with the spotless-fmt hook's 'files'/'exclude' entries in |
| 17 | +# .pre-commit-config.yaml. |
| 18 | +JAVA_SRC_PATTERN='^java/(cuvs-java|cuvs-lucene)/([^/]+/)?src/.*\.java$' |
| 19 | +JAVA_SRC_EXCLUDE='.*/panama/.*' |
| 20 | + |
| 21 | +java_sources_modified() { |
| 22 | + git status --porcelain --untracked-files=all -- java/cuvs-java java/cuvs-lucene | |
| 23 | + cut -c4- | |
| 24 | + grep -Ev "${JAVA_SRC_EXCLUDE}" | |
| 25 | + grep -Eq "${JAVA_SRC_PATTERN}" |
| 26 | +} |
| 27 | + |
15 | 28 | if ! command -v mvn >/dev/null 2>&1; then |
16 | 29 | if [ "${CI:-false}" = "true" ]; then |
17 | 30 | echo "spotless-fmt: 'mvn' is required in CI but was not found on PATH." >&2 |
18 | 31 | exit 1 |
19 | 32 | fi |
20 | | - echo "spotless-fmt: skipping Java formatting ('mvn' not installed and not running in CI)." >&2 |
| 33 | + if java_sources_modified; then |
| 34 | + echo "spotless-fmt: 'mvn' is required to format modified Java sources but was not found on PATH." >&2 |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + echo "spotless-fmt: 'mvn' was not found on PATH and no Java sources were modified, skipping Java formatting." >&2 |
21 | 38 | exit 0 |
22 | 39 | fi |
23 | 40 |
|
|
0 commit comments