checkpatch.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env bash
  2. # tools/checkpatch.sh
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one or more
  5. # contributor license agreements. See the NOTICE file distributed with
  6. # this work for additional information regarding copyright ownership.
  7. # The ASF licenses this file to you under the Apache License, Version 2.0
  8. # (the "License"); you may not use this file except in compliance with
  9. # the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. TOOLDIR=$(dirname $0)
  20. check=check_patch
  21. fail=0
  22. range=0
  23. spell=0
  24. usage() {
  25. echo "USAGE: ${0} [options] [list|-]"
  26. echo ""
  27. echo "Options:"
  28. echo "-h"
  29. echo "-c spell check with codespell(install with: pip install codespell)"
  30. echo "-r range check only (coupled with -p or -g)"
  31. echo "-p <patch file names> (default)"
  32. echo "-g <commit list>"
  33. echo "-f <file list>"
  34. echo "- read standard input mainly used by git pre-commit hook as below:"
  35. echo " git diff --cached | ./tools/checkpatch.sh -"
  36. echo "Where a <commit list> is any syntax supported by git for specifying git revision, see GITREVISIONS(7)"
  37. echo "Where a <patch file names> is a space separated list of patch file names or wildcard. or *.patch"
  38. exit $@
  39. }
  40. check_file() {
  41. if ! $TOOLDIR/nxstyle $@ 2>&1; then
  42. fail=1
  43. fi
  44. if [ $spell != 0 ]; then
  45. if ! codespell -q 7 ${@: -1}; then
  46. fail=1
  47. fi
  48. fi
  49. }
  50. check_ranges() {
  51. while read; do
  52. if [[ $REPLY =~ ^(\+\+\+\ (b/)?([^[:blank:]]+).*)$ ]]; then
  53. if [ "$ranges" != "" ]; then
  54. if [ $range != 0 ]; then
  55. check_file $ranges $path
  56. else
  57. check_file $path
  58. fi
  59. fi
  60. path=$(realpath "${BASH_REMATCH[3]}")
  61. ranges=""
  62. elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+,[0-9]+)?\ @@.* ]]; then
  63. ranges+="-r ${BASH_REMATCH[2]} "
  64. fi
  65. done
  66. if [ "$ranges" != "" ]; then
  67. if [ $range != 0 ]; then
  68. check_file $ranges $path
  69. else
  70. check_file $path
  71. fi
  72. fi
  73. }
  74. check_patch() {
  75. if ! git apply --check $1; then
  76. fail=1
  77. else
  78. git apply $1
  79. diffs=`cat $1`
  80. check_ranges <<< "$diffs"
  81. git apply -R $1
  82. fi
  83. }
  84. check_commit() {
  85. diffs=`git diff $1`
  86. check_ranges <<< "$diffs"
  87. }
  88. make -C $TOOLDIR -f Makefile.host nxstyle 1>/dev/null
  89. if [ -z "$1" ]; then
  90. usage
  91. exit 0
  92. fi
  93. while [ ! -z "$1" ]; do
  94. case "$1" in
  95. - )
  96. check_ranges
  97. ;;
  98. -c )
  99. spell=1
  100. ;;
  101. -f )
  102. check=check_file
  103. ;;
  104. -g )
  105. check=check_commit
  106. ;;
  107. -h )
  108. usage 0
  109. ;;
  110. -p )
  111. check=check_patch
  112. ;;
  113. -r )
  114. range=1
  115. ;;
  116. -* )
  117. usage 1
  118. ;;
  119. * )
  120. break
  121. ;;
  122. esac
  123. shift
  124. done
  125. for arg in $@; do
  126. $check $arg
  127. done
  128. exit $fail