pre-commit 1.6 KB

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