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.

pre-commit 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # Copyright 2016 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
  5. # This file may not be copied, modified, or distributed except according to those terms.
  6. #
  7. # Pre-commit hook for the tarpc repository. To use this hook, copy it to .git/hooks in your
  8. # repository root.
  9. #
  10. # This precommit checks the following:
  11. # 1. All filenames are ascii
  12. # 2. There is no bad whitespace
  13. # 3. rustfmt is installed
  14. # 4. rustfmt is a noop on files that are in the index
  15. #
  16. # Options:
  17. #
  18. # - TARPC_SKIP_RUSTFMT, default = 0
  19. #
  20. # Set this to 1 to skip running rustfmt
  21. #
  22. # Note that these options are most useful for testing the hooks themselves. Use git commit
  23. # --no-verify to skip the pre-commit hook altogether.
  24. RED='\033[0;31m'
  25. GREEN='\033[0;32m'
  26. YELLOW='\033[0;33m'
  27. NC='\033[0m' # No Color
  28. PREFIX="${GREEN}[PRECOMMIT]${NC}"
  29. FAILURE="${RED}FAILED${NC}"
  30. WARNING="${RED}[WARNING]${NC}"
  31. SKIPPED="${YELLOW}SKIPPED${NC}"
  32. SUCCESS="${GREEN}ok${NC}"
  33. if git rev-parse --verify HEAD &>/dev/null
  34. then
  35. against=HEAD
  36. else
  37. # Initial commit: diff against an empty tree object
  38. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  39. fi
  40. FAILED=0
  41. printf "${PREFIX} Checking that all filenames are ascii ... "
  42. # Note that the use of brackets around a tr range is ok here, (it's
  43. # even required, for portability to Solaris 10's /usr/bin/tr), since
  44. # the square bracket bytes happen to fall in the designated range.
  45. if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
  46. then
  47. FAILED=1
  48. printf "${FAILURE}\n"
  49. else
  50. printf "${SUCCESS}\n"
  51. fi
  52. printf "${PREFIX} Checking for bad whitespace ... "
  53. git diff-index --check --cached $against -- &>/dev/null
  54. if [ "$?" != 0 ]; then
  55. FAILED=1
  56. printf "${FAILURE}\n"
  57. else
  58. printf "${SUCCESS}\n"
  59. fi
  60. printf "${PREFIX} Checking for shasum ... "
  61. command -v shasum &>/dev/null
  62. if [ $? == 0 ]; then
  63. printf "${SUCCESS}\n"
  64. else
  65. printf "${FAILURE}\n"
  66. exit 1
  67. fi
  68. printf "${PREFIX} Running clippy ... "
  69. command -v cargo +nightly clippy &>/dev/null
  70. if [ $? == 0 ]; then
  71. printf "${SUCCESS}\n"
  72. else
  73. printf "${FAILURE}\n"
  74. exit 1
  75. fi
  76. # Just check that running rustfmt doesn't do anything to the file. I do this instead of
  77. # modifying the file because I don't want to mess with the developer's index, which may
  78. # not only contain discrete files.
  79. printf "${PREFIX} Checking formatting ... "
  80. FMTRESULT=0
  81. diff=""
  82. for file in $(git diff --name-only --cached);
  83. do
  84. if [ ${file: -3} == ".rs" ]; then
  85. diff="$diff$(rustfmt $file)"
  86. fi
  87. done
  88. if grep --quiet "^Diff at line" <<< "$diff"; then
  89. FMTRESULT=1
  90. fi
  91. if [ "${TARPC_SKIP_RUSTFMT}" == 1 ]; then
  92. printf "${SKIPPED}\n"$?
  93. elif [ ${FMTRESULT} != 0 ]; then
  94. FAILED=1
  95. printf "${FAILURE}\n"
  96. echo "$diff" | sed 's/Using rustfmt config file.*$/d/'
  97. else
  98. printf "${SUCCESS}\n"
  99. fi
  100. exit ${FAILED}