mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-08 04:11:08 +03:00
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:
parent
a3f3fc25dc
commit
94b351beac
1 changed files with 12 additions and 21 deletions
|
@ -22,38 +22,29 @@ if [ $? != 0 ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
result=0
|
|
||||||
problem_files=()
|
problem_files=()
|
||||||
|
|
||||||
printf "[pre_commit] rustfmt "
|
# first collect all the files that need reformatting
|
||||||
|
|
||||||
for file in $(git diff --name-only --cached); do
|
for file in $(git diff --name-only --cached); do
|
||||||
if [ ${file: -3} == ".rs" ]; then
|
if [ ${file: -3} == ".rs" ]; then
|
||||||
# first collect all the files that need reformatting
|
|
||||||
rustfmt --check $file &>/dev/null
|
rustfmt --check $file &>/dev/null
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
problem_files+=($file)
|
problem_files+=($file)
|
||||||
result=1
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# now reformat all the files that need reformatting
|
if [ ${#problem_files[@]} == 0 ]; then
|
||||||
for file in ${problem_files[@]}; do
|
# nothing to do
|
||||||
rustfmt $file
|
printf "[pre_commit] rustfmt \033[0;32mok\033[0m \n"
|
||||||
done
|
else
|
||||||
|
# reformat the files that need it and re-stage them.
|
||||||
# and let the user know what just happened (and which files were affected)
|
printf "[pre_commit] the following files were rustfmt'd before commit: \n"
|
||||||
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
|
for file in ${problem_files[@]}; do
|
||||||
printf "\033[0;31m $file\033[0m \n"
|
rustfmt $file
|
||||||
|
git add $file
|
||||||
|
printf "\033[0;32m $file\033[0m \n"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
# to actually fail the build on rustfmt failure -
|
|
||||||
# exit $result
|
|
||||||
|
|
Loading…
Reference in a new issue