It works for rust and for C++. The difference is that for rust you need to specify rustfmt and for C++ clang-format -i.
- Add file
pre-committo the folder.githooksin your repo with the following text:
#!/bin/bash
exe=$(which rustfmt)
if [ -n "$exe" ]
then
# field separator to the new line
IFS=$'\n'
for line in $(git status -s)
do
# if added or modified
if [[ $line == A* || $line == M* ]]
then
if [[ $line == *.rs ]]
then
# format file
rustfmt $(pwd)/${line:3}
# add changes
git add $(pwd)/${line:3}
fi
fi
done
else
echo "rustfmt was not found"
fi- Run in your repo folder:
chmod +x .githooks/pre-commit
git config core.hooksPath .githooks