Skip to content

Instantly share code, notes, and snippets.

@Dounm
Created January 4, 2021 10:13
Show Gist options
  • Save Dounm/7c1ed8a87756f93eb3567a8a88afca4c to your computer and use it in GitHub Desktop.
Save Dounm/7c1ed8a87756f93eb3567a8a88afca4c to your computer and use it in GitHub Desktop.
Git Hooks for formatting
#!/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