Last active
October 23, 2019 08:09
-
-
Save iarenaza/3431a78e37d10bcb8136e79bc5a75e7c to your computer and use it in GitHub Desktop.
Run "lein cljfmt check" on .clj/.cljs/.cljc files staged for commit
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 | |
staged_files=$(mktemp) | |
file_mappings=$(mktemp) | |
temporary_files=$(mktemp) | |
cljx_files=$(mktemp) | |
# Prepare clean up for normal or abnormal termination of the script | |
trap "rm -f ${staged_files} ${file_mappings} ${temporary_files} ${cljx_files}" EXIT ERR | |
# Get path of files staged in the index, relative to repository root. | |
# Paths are separated by NUL (\0) characters, 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 temporary copy of them and get the list of | |
# temporary files to path mappings. One mapping per line, | |
# with NUL (\0) characters separating each mapping. | |
git checkout-index -z --temp --stdin -- < "${staged_files}" > "${file_mappings}" | |
# Get the list of all temporary files (to clean up later) | |
awk 'BEGIN { FS= "\t"; RS = "\0" } {print $1}' < "${file_mappings}" > "${temporary_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 | |
awk 'BEGIN { FS= "\t"; RS = "\0" } /\.clj[cs]*$/ {print "../" $1}' < "${file_mappings}" > "${cljx_files}" | |
# Now run lein cljfmt check on those files, if there are any | |
exit_code=0 | |
if [[ -s "${cljx_files}" ]] | |
then | |
if (cd app; xargs lein cljfmt check) < "${cljx_files}"; | |
then | |
exit_code=0 | |
else | |
exit_code="$?" | |
fi | |
fi | |
# Clean up the temporary copy of the staged files | |
if [[ -s "${temporary_files}" ]] | |
then | |
xargs rm < "${temporary_files}" | |
fi | |
exit "${exit_code}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment