Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #!/bin/bash
  2. INFO=$(
  3. cat <<-'EOF'
  4. Uses shellcheck to fix any issues in a repo which shellcheck knows how to
  5. fix automatically and then commits the changes.
  6. EOF
  7. )
  8.  
  9. set -o errexit
  10. set -o nounset
  11.  
  12. shopt -s globstar
  13.  
  14. declare -a DEPS
  15. DEPS=(
  16. 'git'
  17. 'sponge'
  18. 'shellcheck'
  19. )
  20.  
  21. declare -a AUTOFIX
  22. AUTOFIX=(
  23. '2006'
  24. '2086'
  25. '2251'
  26. )
  27.  
  28. declare -A AUTOFIX_MSGS
  29. # shellcheck disable=SC2016
  30. AUTOFIX_MSGS=(
  31. ['2006']='Use $(...) notation instead of legacy backticked `...`'
  32. ['2086']='Double quote to prevent globbing and word splitting.'
  33. ['2251']='This ! is not on a condition and skips errexit. Use && exit 1 instead, or make sure $? is checked.'
  34. )
  35.  
  36. function autofix() {
  37. local code
  38. local msg
  39.  
  40. code=$1
  41. msg=$2
  42.  
  43. printf 'Checking files for SC%s\n' "${code}"
  44. for src_file in **/*; do
  45. # skip non-regular files
  46. if ! [[ -f "${src_file}" ]]; then
  47. continue
  48. fi
  49.  
  50. # grab mimetype
  51. if ! mime_type=$(file --mime-type -b "${src_file}"); then
  52. printf 'Unable to get mimetype of %s\n' "${src_file}"
  53. exit 1
  54. fi
  55.  
  56. # skip anything that is not a shellscript
  57. if [[ "${mime_type}" != 'text/x-shellscript' ]]; then
  58. continue
  59. fi
  60.  
  61. # if there is nothing to fix, skip
  62. if shellcheck -i "${code}" -f quiet "${src_file}"; then
  63. continue
  64. fi
  65.  
  66. printf 'Auto fixing SC%s in %s\n' "${code}" "${src_file}"
  67. shellcheck -i "${code}" -f diff "${src_file}" | sponge | git apply
  68. done
  69. if [[ -n "$(git status --porcelain)" ]]; then
  70. printf 'Committing fixes for SC%s\n' "${code}"
  71. git add -A
  72. git commit -m "SC${code}: ${msg}"
  73. fi
  74. }
  75.  
  76. usage() {
  77. printf '%s\n' "${INFO}"
  78. printf 'usage: %s\n' "${0##*/}"
  79. printf ' -d <DIR> Repo directory to autofix\n'
  80. printf ' -h This message\n'
  81. }
  82.  
  83. main() {
  84. while getopts ':d:h' opt; do
  85. case "${opt}" in
  86. d)
  87. repo=$OPTARG
  88. ;;
  89. h)
  90. usage
  91. exit
  92. ;;
  93. \?)
  94. usage 1>&2
  95. exit 1
  96. ;;
  97. :)
  98. echo "Option -$OPTARG requires an argument" >&2
  99. exit 1
  100. ;;
  101. esac
  102. done
  103. shift $((OPTIND - 1))
  104.  
  105. for dep in "${DEPS[@]}"; do
  106. if ! command -v "${dep}" >/dev/null 2>&1; then
  107. printf 'Command %s required!\n' "${dep}"
  108. exit 1
  109. fi
  110. done
  111.  
  112. if [[ ! -v 'repo' || -z "${repo}" ]]; then
  113. usage 1>&2
  114. exit 1
  115. fi
  116.  
  117. pushd "${repo}" >/dev/null
  118. for fix_num in "${AUTOFIX[@]}"; do
  119. autofix "${fix_num}" "${AUTOFIX_MSGS["$fix_num"]}"
  120. done
  121. popd >/dev/null
  122. printf 'Auto fixing complete, git log:\n'
  123. git log --oneline origin..
  124. }
  125.  
  126. main "${@}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement