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.
 
 
 

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