You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

224 line
5.2 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"
  20. exit 1
  21. ;;
  22. esac
  23. shift
  24. done
  25. error() { if [ "$quiet" != true ]; then echo "E: $1"; fi }
  26. info() { if [ "$quiet" != true ]; then echo "I: $1"; 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. elif [ -f "$top_srcdir/.svn/format" ]; then
  67. info "detected SVN repository"
  68. repo=svn
  69. else
  70. info "not in a Vcs directory, nothing to do"
  71. exit 0
  72. fi
  73. total_crlfs=0
  74. total_spaces=0
  75. total_tabs=0
  76. OIFS="$IFS"
  77. IFS='
  78. '
  79. if [ "$repo" = git ]; then
  80. FILES="`git ls-files`"
  81. else
  82. FILES="`svn ls -R`"
  83. fi
  84. total_files=0
  85. for file in $FILES; do
  86. if [ -f "$file" ]; then
  87. total_files="$(($total_files + 1))"
  88. fi
  89. done
  90. total_errors=0
  91. for file in $FILES; do
  92. should_check_crlf=false
  93. should_check_spaces=false
  94. case "$file" in
  95. # These files we know how to handle
  96. *.c|*.cpp|*.m|*.mm|*.h|*.hh|*.lolfx|*.lua|*.l|*.y|*.sh|*.py)
  97. should_check_crlf=true
  98. should_check_spaces=true
  99. ;;
  100. *)
  101. continue
  102. ;;
  103. esac
  104. case "$file" in
  105. # These files aren't ours, don't fix
  106. src/bullet/*|\
  107. src/lua/*|\
  108. external/*|\
  109. */generated/*|\
  110. web/plugins/*)
  111. continue
  112. ;;
  113. esac
  114. case "$file" in
  115. # Don't harass these people, but fix their line endings
  116. people/peeweek/*|\
  117. people/touky/*|\
  118. people/benlitz/*|\
  119. people/sam/lua-*)
  120. should_check_spaces=false
  121. ;;
  122. esac
  123. clean=true
  124. # Check for CR LF
  125. if [ "$can_check_crlf" = true -a "$should_check_crlf" = true ]; then
  126. ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  127. total_crlfs="$(($total_crlfs + $ncrlfs))"
  128. if [ "$ncrlfs" -gt 0 ]; then
  129. clean=false
  130. if [ "$fix" = true ]; then
  131. $d2u -q "$file"
  132. info "$file has $ncrlfs CR characters"
  133. else
  134. error "$file has $ncrlfs CR characters"
  135. fi
  136. fi
  137. fi
  138. # Check for trailing spaces
  139. if [ "$should_check_spaces" = true ]; then
  140. nspaces="$($SED 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  141. total_spaces="$(($total_spaces + $nspaces))"
  142. if [ "$nspaces" -gt 0 ]; then
  143. clean=false
  144. if [ "$fix" = true ]; then
  145. $SED -i 's/[[:space:]][[:space:]]*$//g' "$file"
  146. info "$file has $nspaces trailing spaces"
  147. else
  148. error "$file has $nspaces trailing spaces"
  149. fi
  150. fi
  151. fi
  152. # Check for tabs
  153. if [ "$should_check_spaces" = true ]; then
  154. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  155. total_tabs="$(($total_tabs + $ntabs))"
  156. if [ "$ntabs" -gt 0 ]; then
  157. clean=false
  158. if [ "$fix" = true ]; then
  159. $SED -i 's/\t/ /g' "$file"
  160. info "$file has $ntabs tabs"
  161. else
  162. error "$file has $ntabs tabs"
  163. fi
  164. fi
  165. fi
  166. if [ "$clean" != true ]; then
  167. total_errors="$(($total_errors + 1))"
  168. fi
  169. done
  170. IFS="$OIFS"
  171. if [ "$total_errors" -gt 0 ]; then
  172. if [ "$commit" = "true" ]; then
  173. # EITHER: commit all modified files
  174. svn commit --username lolbot --non-interactive -F - << EOF
  175. fixed $total_errors files out of $total_files:
  176. - removed $total_crlfs CR characters
  177. - removed $total_spaces trailing whitespaces
  178. - replaced $total_tabs tabs with spaces
  179. EOF
  180. elif [ "$fix" = "true" ]; then
  181. # OR: report in stdout
  182. info "fixed $total_errors files out of $total_files:"
  183. if [ "$total_crlfs" -gt 0 ]; then
  184. info " - fixed $total_crlfs CR characters"
  185. fi
  186. if [ "$total_spaces" -gt 0 ]; then
  187. info " - fixed $total_spaces trailing spaces"
  188. fi
  189. if [ "$total_tabs" -gt 0 ]; then
  190. info " - fixed $total_tabs tabs"
  191. fi
  192. info "re-run with -c to commit fixes"
  193. else
  194. # OR: warn about how to fix errors
  195. info "re-run with -w to fix errors"
  196. fi
  197. else
  198. info "all $total_files source files appear to be OK, congratulations"
  199. fi