Last active
July 15, 2021 07:43
-
-
Save fousa/bf38ab0d3e0a0cd5ce1752c5c4932038 to your computer and use it in GitHub Desktop.
pre-commit formatting
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
#!/bin/bash | |
# Check if the stashed files with swiftformat before commiting. | |
# | |
# Installation: | |
# - Run the following command from your git root: | |
# `curl https://gist.githubusercontent.com/fousa/bf38ab0d3e0a0cd5ce1752c5c4932038/raw/ce9d02c9038e9f9b4b542a65f16fb94fb2ab352a/pre-commit > .git/hooks/pre-commit` | |
# - Set hook as executable: | |
# `chmod +x .git/hooks/pre-commit` | |
# Fetch the list of staged files. | |
FILELIST=$(git diff --name-only --cached --diff-filter=d) | |
if [ "$FILELIST" == '' ]; then | |
# When no staged files are available we return immediately. | |
echo "No staged files found that were added or updated." | |
exit 0 | |
fi | |
echo "Check formatting for the staged files..." | |
# Save the files to a text file. This is required by the `swiftformat` command. | |
git diff --name-only --cached --diff-filter=d > .staged-filelist | |
# Check the formatting for the given files. | |
RESULT="Pods/SwiftFormat/CommandLineTool/swiftformat --filelist .staged-filelist --lint --config .swiftformat ." | |
# Execute the swifformat command and save the result. | |
$RESULT | |
RESULT_EXIT_STATUS=$? | |
# Remove the staged file list. | |
rm -rf .staged-filelist | |
# Return the exit status from the swiftlint command. | |
exit $RESULT_EXIT_STATUS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment