chore: pre-commit hook - format all staged files, re-add then commit (#3078)

* chore: pre-commit hook - format all staged files, re-add then commit

* chore: pre-commit hook - formatted files logged green colour
This commit is contained in:
Joseph Goulden 2019-10-07 21:00:41 +01:00 committed by Antioch Peverell
parent a3f3fc25dc
commit 94b351beac

View file

@ -22,38 +22,29 @@ if [ $? != 0 ]; then
exit 1
fi
result=0
problem_files=()
printf "[pre_commit] rustfmt "
# first collect all the files that need reformatting
for file in $(git diff --name-only --cached); do
if [ ${file: -3} == ".rs" ]; then
# first collect all the files that need reformatting
rustfmt --check $file &>/dev/null
if [ $? != 0 ]; then
problem_files+=($file)
result=1
fi
fi
done
# now reformat all the files that need reformatting
for file in ${problem_files[@]}; do
rustfmt $file
done
# and let the user know what just happened (and which files were affected)
printf "\033[0;32mok\033[0m \n"
if [ $result != 0 ]; then
# printf "\033[0;31mrustfmt\033[0m \n"
printf "[pre_commit] the following files were rustfmt'd (not yet committed): \n"
for file in ${problem_files[@]}; do
printf "\033[0;31m $file\033[0m \n"
done
if [ ${#problem_files[@]} == 0 ]; then
# nothing to do
printf "[pre_commit] rustfmt \033[0;32mok\033[0m \n"
else
# reformat the files that need it and re-stage them.
printf "[pre_commit] the following files were rustfmt'd before commit: \n"
for file in ${problem_files[@]}; do
rustfmt $file
git add $file
printf "\033[0;32m $file\033[0m \n"
done
fi
exit 0
# to actually fail the build on rustfmt failure -
# exit $result