Created
June 7, 2021 13:45
-
-
Save iarenaza/c13d7205d574d38dbd46dd4313219b05 to your computer and use it in GitHub Desktop.
lein-check-staged-cljx-files.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
echo "Running pre-commit hook, it may take a while..." | |
# Pre-commit scripts are run from the root directory of the git | |
# repository. In our case, Leiningen project root is actually in this | |
# subdirectory inside the git repository root directory. | |
LEIN_PROJECT_ROOT="app" | |
staged_files=$(mktemp) | |
temporary_dir=$(mktemp -d --tmpdir="${LEIN_PROJECT_ROOT}") | |
cljx_files=$(mktemp) | |
# Prepare clean up for normal or abnormal termination of the script | |
# shellcheck disable=SC2064 | |
trap "rm -rf ${staged_files} ${temporary_dir} ${cljx_files}" EXIT ERR | |
# Get path of files staged in the index, relative to repository root | |
# paths are separated by NUL (\0) character, to avoid problems | |
# with spaces, tags, etc in the names | |
git diff-index --cached --name-only -z HEAD > "${staged_files}" | |
# Exit early (successfully) if there are no staged files. | |
if [[ ! ( -s "${staged_files}" ) ]] | |
then | |
exit 0 | |
fi | |
# Checkout out a copy of them in a temporary directory, inside the | |
# Leiningen project root directory. | |
git checkout-index --prefix="${temporary_dir}"/ -z --stdin -- < "${staged_files}" | |
# Filter the ones having a .clj/.cljs/.cljc extension | |
# and print out the temporary filename prefixed with "../", as we'll | |
# refer to them later from the "app/" subdirectory | |
cljx_files_dir="../${temporary_dir}/" | |
# shellcheck disable=SC2016 | |
env cljx_files_dir="${cljx_files_dir}" awk < "${staged_files}" > "${cljx_files}" ' | |
BEGIN { FS= "\t"; RS = "\0" } | |
/\.clj[cs]*$/ {print ENVIRON["cljx_files_dir"] $1}' | |
# Now run `lein cljfmt check` and `clj-kondo` check on those files, if there are any | |
if [[ -s "${cljx_files}" ]] | |
then | |
# We need to run the tools from the root directory of the Leiningen project. | |
( | |
cd "${LEIN_PROJECT_ROOT}"; | |
echo "Running xargs lein update-in ':cljfmt' merge '{:ansi? false}' -- cljfmt check < ${cljx_files}"; | |
xargs lein update-in ':cljfmt' merge '{:ansi? false}' -- cljfmt check < "${cljx_files}"; | |
echo "Running clj-kondo --parallel --lint ${cljx_files_dir}"; | |
clj-kondo --parallel --lint "${cljx_files_dir}" | |
) | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment