shellcheck.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 2>/dev/null)}
  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 [[ ! -x "${SHELLCHECK}" ]]; then
  55. hadoop_error "shellcheck is not available."
  56. return 0
  57. fi
  58. start_clock
  59. # shellcheck disable=SC2016
  60. SHELLCHECK_VERSION=$(shellcheck --version | ${GREP} version: | ${AWK} '{print $NF}')
  61. echo "Running shellcheck against all identifiable shell scripts"
  62. pushd "${BASEDIR}" >/dev/null
  63. for i in $(shellcheck_private_findbash); do
  64. if [[ -f ${i} ]]; then
  65. ${SHELLCHECK} -f gcc "${i}" >> "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt"
  66. fi
  67. done
  68. popd > /dev/null
  69. # keep track of how much as elapsed for us already
  70. SHELLCHECK_TIMER=$(stop_clock)
  71. return 0
  72. }
  73. function shellcheck_postapply
  74. {
  75. local i
  76. verify_needed_test shellcheck
  77. if [[ $? == 0 ]]; then
  78. return 0
  79. fi
  80. big_console_header "shellcheck plugin: postpatch"
  81. if [[ ! -x "${SHELLCHECK}" ]]; then
  82. hadoop_error "shellcheck is not available."
  83. add_jira_table 0 shellcheck "Shellcheck was not available."
  84. return 0
  85. fi
  86. start_clock
  87. # add our previous elapsed to our new timer
  88. # by setting the clock back
  89. offset_clock "${SHELLCHECK_TIMER}"
  90. echo "Running shellcheck against all identifiable shell scripts"
  91. # we re-check this in case one has been added
  92. for i in $(shellcheck_private_findbash); do
  93. ${SHELLCHECK} -f gcc "${i}" >> "${PATCH_DIR}/patchshellcheck-result.txt"
  94. done
  95. # shellcheck disable=SC2016
  96. numPrepatch=$(wc -l "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt" | ${AWK} '{print $1}')
  97. # shellcheck disable=SC2016
  98. numPostpatch=$(wc -l "${PATCH_DIR}/patchshellcheck-result.txt" | ${AWK} '{print $1}')
  99. ${DIFF} -u "${PATCH_DIR}/${PATCH_BRANCH}shellcheck-result.txt" \
  100. "${PATCH_DIR}/patchshellcheck-result.txt" \
  101. | ${GREP} '^+\.' \
  102. > "${PATCH_DIR}/diffpatchshellcheck.txt"
  103. # shellcheck disable=SC2016
  104. diffPostpatch=$(wc -l "${PATCH_DIR}/diffpatchshellcheck.txt" | ${AWK} '{print $1}')
  105. if [[ ${diffPostpatch} -gt 0
  106. && ${numPostpatch} -gt ${numPrepatch} ]] ; then
  107. add_jira_table -1 shellcheck "The applied patch generated "\
  108. "${diffPostpatch} new shellcheck (v${SHELLCHECK_VERSION}) issues (total was ${numPrepatch}, now ${numPostpatch})."
  109. add_jira_footer shellcheck "@@BASE@@/diffpatchshellcheck.txt"
  110. return 1
  111. fi
  112. add_jira_table +1 shellcheck "There were no new shellcheck (v${SHELLCHECK_VERSION}) issues."
  113. return 0
  114. }