Created
January 4, 2021 10:13
-
-
Save Dounm/7c1ed8a87756f93eb3567a8a88afca4c to your computer and use it in GitHub Desktop.
Git Hooks for formatting
This file contains hidden or 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 | |
clang_format=clang-format | |
autopep=autopep8 | |
for FILE in $(git diff --cached --name-only) | |
do | |
if [[ $FILE == *.py ]]; then | |
if ! command -v $autopep &> /dev/null; then | |
echo "Cannot find autopep8 for formatting while there are py diffs" | |
exit -1 | |
fi | |
$autopep $FILE -i --max-line-length 100 | |
elif [[ $FILE =~ .*\.(cpp|h|hpp|cxx|c|cc|cu|cuh)$ ]]; then | |
$clang_format -i $FILE -style=file | |
fi | |
done | |
for FILE in $(git diff --name-only) | |
do | |
echo "Formatting $FILE" | |
git add $FILE | |
done | |
echo | |
echo "Format done!" | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment