pre-commit 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. # Auto-tidy staged Perl before it lands.
  3. #
  4. # A file qualifies three ways:
  5. # - a .pm, .pl or .t extension
  6. # - a .PL extension, which is how Makefile.PL gets covered
  7. # - executable with perl on its shebang line, which is how the extensionless
  8. # scripts in bin/ get covered
  9. #
  10. # .t is in there because only one of the seven test files happens to be +x,
  11. # so the shebang rule alone would have tidied that one and let the rest drift.
  12. # Redirect output to stderr.
  13. exec 1>&2
  14. if ! command -v perltidy >/dev/null 2>&1
  15. then
  16. echo "pre-commit: perltidy is not on PATH, skipping the tidy pass."
  17. exit 0
  18. fi
  19. # ACMR so that a file deleted in this commit is not handed to perltidy, which
  20. # would fail on a path that is no longer there.
  21. staged=$(git diff --cached --name-only --diff-filter=ACMR)
  22. to_tidy=''
  23. for file in $staged
  24. do
  25. test -f "$file" || continue
  26. case "$file" in
  27. *.pm|*.pl|*.PL|*.t)
  28. to_tidy="$to_tidy $file"
  29. continue
  30. ;;
  31. esac
  32. if test -x "$file" && head -n 1 "$file" | grep -Eq '^#!.*perl'
  33. then
  34. to_tidy="$to_tidy $file"
  35. fi
  36. done
  37. # -n and quoted, unlike the version this replaces: bare [ $to_tidy ] is a
  38. # syntax error the moment there is more than one file, so the hook quietly
  39. # stopped tidying in exactly the commits that needed it most.
  40. if [ -n "$to_tidy" ]
  41. then
  42. echo "Auto-tidying perl changes..."
  43. # -bext='/' makes perltidy delete the backup it would otherwise leave
  44. # sitting next to the file as <name>.bak.
  45. perltidy -b -bext='/' $to_tidy || {
  46. echo "pre-commit: perltidy failed, leaving the commit alone."
  47. exit 1
  48. }
  49. git update-index --add $to_tidy
  50. echo "Done."
  51. fi