Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

210 linhas
5.0 KiB

  1. #!/bin/sh
  2. set -e
  3. fix=false
  4. quiet=false
  5. while [ "$#" -gt 0 ]; do
  6. case "$1" in
  7. -q)
  8. quiet=true
  9. ;;
  10. -w)
  11. fix=true
  12. ;;
  13. -c)
  14. commit=true
  15. fix=true
  16. quiet=true
  17. ;;
  18. *)
  19. echo "E: invalid argument $1" >&2
  20. exit 1
  21. ;;
  22. esac
  23. shift
  24. done
  25. error() { if [ "$quiet" != true ]; then echo "E: $1" >&2; fi }
  26. info() { if [ "$quiet" != true ]; then echo "I: $1" >&2; fi }
  27. # Ensure the system tools do not attempt to perform multibyte conversions
  28. export LANG=C
  29. # Find out where the top directory is and go there
  30. top_srcdir="$(cd "$(dirname $0)"; cd ..; pwd)"
  31. cd "$top_srcdir"
  32. # Check for working tools
  33. if [ "$(echo foo | grep -c foo)" != 1 ]; then
  34. error "grep -c does not appear to work, cancelling"
  35. exit 0
  36. fi
  37. SED=sed
  38. if gsed --version >/dev/null 2>&1; then
  39. SED=gsed
  40. fi
  41. if [ "$(echo 'x\x' | $SED 's/.*[^x\t]//')" != x ]; then
  42. error "sed does not appear to work, cancelling"
  43. exit 0
  44. fi
  45. if d2u -h >/dev/null 2>&1; then
  46. d2u=d2u
  47. elif dos2unix -h >/dev/null 2>&1; then
  48. d2u=dos2unix
  49. else
  50. error "d2u or dos2unix not found, cancelling"
  51. exit 0
  52. fi
  53. # Find out whether we need to care about CRLF
  54. case "$(uname 2>/dev/null)" in
  55. MINGW*)
  56. can_check_crlf=false
  57. ;;
  58. *)
  59. can_check_crlf=true
  60. ;;
  61. esac
  62. # Find out what kind of Vcs directory this is
  63. if [ -f "$top_srcdir/.git/index" ]; then
  64. info "detected Git repository"
  65. repo=git
  66. else
  67. info "not in a Git directory, nothing to do"
  68. exit 0
  69. fi
  70. total_crlfs=0
  71. total_spaces=0
  72. total_tabs=0
  73. OIFS="$IFS"
  74. IFS='
  75. '
  76. FILES="`git ls-files`"
  77. total_files=0
  78. for file in $FILES; do
  79. if [ -f "$file" ]; then
  80. total_files="$(($total_files + 1))"
  81. fi
  82. done
  83. total_errors=0
  84. for file in $FILES; do
  85. should_check_crlf=false
  86. should_check_spaces=false
  87. should_check_props=false
  88. case "$file" in
  89. # These files we know how to handle
  90. *.c|*.cpp|*.m|*.mm|*.h|*.hh|*.lolfx|*.lua|*.l|*.y|*.sh|*.py)
  91. should_check_crlf=true
  92. should_check_spaces=true
  93. should_check_props=true
  94. ;;
  95. *)
  96. continue
  97. ;;
  98. esac
  99. case "$file" in
  100. # Don't harass these people, but fix their line endings
  101. people/peeweek/*|\
  102. people/touky/*|\
  103. people/benlitz/*|\
  104. people/sam/lua-*)
  105. should_check_spaces=false
  106. ;;
  107. esac
  108. clean=true
  109. # Check for CR LF
  110. if [ "$can_check_crlf" = true -a "$should_check_crlf" = true ]; then
  111. ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  112. total_crlfs="$(($total_crlfs + $ncrlfs))"
  113. if [ "$ncrlfs" -gt 0 ]; then
  114. clean=false
  115. if [ "$fix" = true ]; then
  116. $d2u -q "$file"
  117. info "$file has $ncrlfs CR characters"
  118. else
  119. error "$file has $ncrlfs CR characters"
  120. fi
  121. fi
  122. fi
  123. # Check for trailing spaces
  124. if [ "$should_check_spaces" = true ]; then
  125. nspaces="$($SED 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  126. total_spaces="$(($total_spaces + $nspaces))"
  127. if [ "$nspaces" -gt 0 ]; then
  128. clean=false
  129. if [ "$fix" = true ]; then
  130. $SED -i 's/[[:space:]][[:space:]]*$//g' "$file"
  131. info "$file has $nspaces trailing spaces"
  132. else
  133. error "$file has $nspaces trailing spaces"
  134. fi
  135. fi
  136. fi
  137. # Check for tabs
  138. if [ "$should_check_spaces" = true ]; then
  139. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  140. total_tabs="$(($total_tabs + $ntabs))"
  141. if [ "$ntabs" -gt 0 ]; then
  142. clean=false
  143. if [ "$fix" = true ]; then
  144. $SED -i 's/\t/ /g' "$file"
  145. info "$file has $ntabs tabs"
  146. else
  147. error "$file has $ntabs tabs"
  148. fi
  149. fi
  150. fi
  151. if [ "$clean" != true ]; then
  152. total_errors="$(($total_errors + 1))"
  153. fi
  154. done
  155. IFS="$OIFS"
  156. if [ "$total_errors" -gt 0 ]; then
  157. if [ "$commit" = "true" ]; then
  158. # EITHER: commit all modified files
  159. git commit --author 'Lolbot <lolbot@zoy.org>' -a -F - << EOF
  160. fixed $total_errors files out of $total_files:
  161. - removed $total_crlfs CR characters
  162. - removed $total_spaces trailing whitespaces
  163. - replaced $total_tabs tabs with spaces
  164. EOF
  165. elif [ "$fix" = "true" ]; then
  166. # OR: report in stdout
  167. info "fixed $total_errors files out of $total_files:"
  168. if [ "$total_crlfs" -gt 0 ]; then
  169. info " - fixed $total_crlfs CR characters"
  170. fi
  171. if [ "$total_spaces" -gt 0 ]; then
  172. info " - fixed $total_spaces trailing spaces"
  173. fi
  174. if [ "$total_tabs" -gt 0 ]; then
  175. info " - fixed $total_tabs tabs"
  176. fi
  177. info "re-run with -c to commit fixes"
  178. else
  179. # OR: warn about how to fix errors
  180. info "re-run with -w to fix errors"
  181. exit 1
  182. fi
  183. else
  184. info "all $total_files source files appear to be OK, congratulations"
  185. fi