Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

235 rindas
5.8 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. # Don't harass these people, but fix their line endings
  109. people/peeweek/*|\
  110. people/touky/*|\
  111. people/benlitz/*|\
  112. people/sam/lua-*)
  113. should_check_spaces=false
  114. ;;
  115. esac
  116. clean=true
  117. # Check for CR LF
  118. if [ "$can_check_crlf" = true -a "$should_check_crlf" = true ]; then
  119. ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  120. total_crlfs="$(($total_crlfs + $ncrlfs))"
  121. if [ "$ncrlfs" -gt 0 ]; then
  122. clean=false
  123. if [ "$fix" = true ]; then
  124. $d2u -q "$file"
  125. info "$file has $ncrlfs CR characters"
  126. else
  127. error "$file has $ncrlfs CR characters"
  128. fi
  129. fi
  130. fi
  131. # Check for LF SVN prop
  132. if [ "$repo" = svn -a "$should_check_props" = true ]; then
  133. if [ "$(svn propget svn:eol-style "$file")" != "LF" ]; then
  134. clean=false
  135. if [ "$fix" = true ]; then
  136. svn propset svn:eol-style LF "$file"
  137. info "$file is missing svn:eol-style property"
  138. else
  139. error "$file is missing svn:eol-style property"
  140. fi
  141. total_props="$(($total_props + 1))"
  142. fi
  143. fi
  144. # Check for trailing spaces
  145. if [ "$should_check_spaces" = true ]; then
  146. nspaces="$($SED 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  147. total_spaces="$(($total_spaces + $nspaces))"
  148. if [ "$nspaces" -gt 0 ]; then
  149. clean=false
  150. if [ "$fix" = true ]; then
  151. $SED -i 's/[[:space:]][[:space:]]*$//g' "$file"
  152. info "$file has $nspaces trailing spaces"
  153. else
  154. error "$file has $nspaces trailing spaces"
  155. fi
  156. fi
  157. fi
  158. # Check for tabs
  159. if [ "$should_check_spaces" = true ]; then
  160. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  161. total_tabs="$(($total_tabs + $ntabs))"
  162. if [ "$ntabs" -gt 0 ]; then
  163. clean=false
  164. if [ "$fix" = true ]; then
  165. $SED -i 's/\t/ /g' "$file"
  166. info "$file has $ntabs tabs"
  167. else
  168. error "$file has $ntabs tabs"
  169. fi
  170. fi
  171. fi
  172. if [ "$clean" != true ]; then
  173. total_errors="$(($total_errors + 1))"
  174. fi
  175. done
  176. IFS="$OIFS"
  177. if [ "$total_errors" -gt 0 ]; then
  178. if [ "$commit" = "true" ]; then
  179. # EITHER: commit all modified files
  180. svn commit --username lolbot --non-interactive -F - << EOF
  181. fixed $total_errors files out of $total_files:
  182. - removed $total_crlfs CR characters
  183. - removed $total_spaces trailing whitespaces
  184. - replaced $total_tabs tabs with spaces
  185. - fixed $total_props svn:eol-style properties
  186. EOF
  187. elif [ "$fix" = "true" ]; then
  188. # OR: report in stdout
  189. info "fixed $total_errors files out of $total_files:"
  190. if [ "$total_crlfs" -gt 0 ]; then
  191. info " - fixed $total_crlfs CR characters"
  192. fi
  193. if [ "$total_spaces" -gt 0 ]; then
  194. info " - fixed $total_spaces trailing spaces"
  195. fi
  196. if [ "$total_tabs" -gt 0 ]; then
  197. info " - fixed $total_tabs tabs"
  198. fi
  199. if [ "$total_props" -gt 0 ]; then
  200. info " - fixed $total_props svn:eol-style properties"
  201. fi
  202. info "re-run with -c to commit fixes"
  203. else
  204. # OR: warn about how to fix errors
  205. info "re-run with -w to fix errors"
  206. fi
  207. else
  208. info "all $total_files source files appear to be OK, congratulations"
  209. fi