shellcheck.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. add_plugin shellcheck
  17. SHELLCHECK_TIMER=0
  18. SHELLCHECK=${SHELLCHECK:-$(which shellcheck)}
  19. SHELLCHECK_SPECIFICFILES=""
  20. # if it ends in an explicit .sh, then this is shell code.
  21. # if it doesn't have an extension, we assume it is shell code too
  22. function shellcheck_filefilter
  23. {
  24. local filename=$1
  25. if [[ ${filename} =~ \.sh$ ]]; then
  26. add_test shellcheck
  27. SHELLCHECK_SPECIFICFILES="${SHELLCHECK_SPECIFICFILES} ./${filename}"
  28. fi
  29. if [[ ! ${filename} =~ \. ]]; then
  30. add_test shellcheck
  31. fi
  32. }
  33. function shellcheck_private_findbash
  34. {
  35. local i
  36. local value
  37. local list
  38. while read line; do
  39. value=$(find "${line}" ! -name '*.cmd' -type f \
  40. | ${GREP} -E -v '(.orig$|.rej$)')
  41. list="${list} ${value}"
  42. done < <(find . -type d -name bin -o -type d -name sbin -o -type d -name libexec -o -type d -name shellprofile.d)
  43. # shellcheck disable=SC2086
  44. echo ${list} ${SHELLCHECK_SPECIFICFILES} | tr ' ' '\n' | sort -u
  45. }
  46. function shellcheck_preapply
  47. {
  48. local i
  49. verify_needed_test shellcheck
  50. if [[ $? == 0 ]]; then
  51. return 0
  52. fi
  53. big_console_header "shellcheck plugin: prepatch"
  54. if [[ -z "${SHELLCHECK}" ]]; then
  55. hadoop_error "shellcheck is not available."
  56. fi
  57. start_clock
  58. # shellcheck disable=SC2016
  59. SHELLCHECK_VERSION=$(shellcheck --version | ${GREP} version: | ${AWK} '{print $NF}')
  60. echo "Running shellcheck against all identifiable shell scripts"
  61. pushd "${BASEDIR}" >/dev/null
  62. for i in $(shellcheck_private_findbash); do
  63. if [[ -f ${i} ]]; then
  64. ${SHELLCHECK} -f gcc "${i}" >> "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt"
  65. fi
  66. done
  67. popd > /dev/null
  68. # keep track of how much as elapsed for us already
  69. SHELLCHECK_TIMER=$(stop_clock)
  70. return 0
  71. }
  72. function shellcheck_postapply
  73. {
  74. local i
  75. verify_needed_test shellcheck
  76. if [[ $? == 0 ]]; then
  77. return 0
  78. fi
  79. big_console_header "shellcheck plugin: postpatch"
  80. if [[ -z "${SHELLCHECK}" ]]; then
  81. hadoop_error "shellcheck is not available."
  82. fi
  83. start_clock
  84. # add our previous elapsed to our new timer
  85. # by setting the clock back
  86. offset_clock "${SHELLCHECK_TIMER}"
  87. echo "Running shellcheck against all identifiable shell scripts"
  88. # we re-check this in case one has been added
  89. for i in $(shellcheck_private_findbash); do
  90. ${SHELLCHECK} -f gcc "${i}" >> "${PATCH_DIR}/patchshellcheck-result.txt"
  91. done
  92. # shellcheck disable=SC2016
  93. numPrepatch=$(wc -l "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt" | ${AWK} '{print $1}')
  94. # shellcheck disable=SC2016
  95. numPostpatch=$(wc -l "${PATCH_DIR}/patchshellcheck-result.txt" | ${AWK} '{print $1}')
  96. ${DIFF} -u "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt" \
  97. "${PATCH_DIR}/patchshellcheck-result.txt" \
  98. | ${GREP} '^+\.' \
  99. > "${PATCH_DIR}/diffpatchshellcheck.txt"
  100. # shellcheck disable=SC2016
  101. diffPostpatch=$(wc -l "${PATCH_DIR}/diffpatchshellcheck.txt" | ${AWK} '{print $1}')
  102. if [[ ${diffPostpatch} -gt 0 ]] ; then
  103. add_jira_table -1 shellcheck "The applied patch generated "\
  104. "${diffPostpatch} new shellcheck (v${SHELLCHECK_VERSION}) issues (total was ${numPrepatch}, now ${numPostpatch})."
  105. add_jira_footer shellcheck "@@BASE@@/diffpatchshellcheck.txt"
  106. return 1
  107. fi
  108. add_jira_table +1 shellcheck "There were no new shellcheck (v${SHELLCHECK_VERSION}) issues."
  109. return 0
  110. }