You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

  1. #!/usr/bin/env bash
  2. rustfmt --version &>/dev/null
  3. if [ $? != 0 ]; then
  4. printf "[pre_commit] \033[0;31merror\033[0m: \"rustfmt\" not available. \n"
  5. printf "[pre_commit] \033[0;31merror\033[0m: rustfmt can be installed via - \n"
  6. printf "[pre_commit] $ rustup component add rustfmt \n"
  7. exit 1
  8. fi
  9. problem_files=()
  10. # collect ill-formatted files
  11. for file in $(git diff --name-only --cached); do
  12. if [ ${file: -3} == ".rs" ]; then
  13. rustfmt +stable --check $file &>/dev/null
  14. if [ $? != 0 ]; then
  15. problem_files+=($file)
  16. fi
  17. fi
  18. done
  19. if [ ${#problem_files[@]} == 0 ]; then
  20. # done
  21. printf "[pre_commit] rustfmt \033[0;32mok\033[0m \n"
  22. else
  23. # reformat the files that need it and re-stage them.
  24. printf "[pre_commit] the following files were rustfmt'd before commit: \n"
  25. for file in ${problem_files[@]}; do
  26. rustfmt +stable $file
  27. git add $file
  28. printf "\033[0;32m $file\033[0m \n"
  29. done
  30. fi
  31. exit 0