blob: 532d44ef2ee63aafaa6087de2f68f26ef0873a31 [file]
#!/bin/sh
set -e
# Run Git without inherited pathspec overrides, scoped to this invocation.
git_clean_pathspecs() (
unset GIT_LITERAL_PATHSPECS GIT_GLOB_PATHSPECS \
GIT_NOGLOB_PATHSPECS GIT_ICASE_PATHSPECS
git "$@"
)
REPO_ROOT_DIR="$(git rev-parse --show-toplevel)"
# Save staged Java paths as NUL-delimited records to preserve special filenames.
java_files=$(mktemp)
trap 'rm -f "$java_files"' 0
git_clean_pathspecs diff --cached --name-only -z --diff-filter=ACMR \
-- ':(icase,glob)**/*.java' >"$java_files"
# Skip formatting when there are no staged Java changes.
if [ ! -s "$java_files" ]; then
exit 0
fi
# Run Spotless silently on success and report Gradle output on failure.
if gradle_output=$("${REPO_ROOT_DIR}/gradlew" spotlessApply -Pspotless.ratchet.from=HEAD 2>&1); then
:
else
gradle_status=$?
printf '%s\n' "$gradle_output" >&2
exit "$gradle_status"
fi
# Restage every selected Java file
# NOTE: this includes any previously unstaged edits; this script assumes
# files are staged in their entirety
git_clean_pathspecs --literal-pathspecs add \
--pathspec-from-file="$java_files" --pathspec-file-nul