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.
 
 
 

184 lines
4.6 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. # Find out where the top directory is and go there
  28. top_srcdir="$(cd "$(dirname $0)"; cd ..; pwd)"
  29. cd "$top_srcdir"
  30. # Check for working tools
  31. #if [ "$(echo foo | grep -c foo)" != 1 ]; then
  32. # error "grep -c does not appear to work, cancelling"
  33. # exit 0
  34. #fi
  35. if d2u -h >/dev/null 2>&1; then
  36. d2u=d2u
  37. elif dos2unix -h >/dev/null 2>&1; then
  38. d2u=dos2unix
  39. else
  40. error "d2u or dos2unix not found, cancelling"
  41. exit 0
  42. fi
  43. # Find out whether we need to care about CRLF
  44. case "$(uname 2>/dev/null)" in
  45. MINGW*)
  46. check_crlf=false
  47. ;;
  48. *)
  49. check_crlf=true
  50. ;;
  51. esac
  52. # Find out what kind of Vcs directory this is
  53. if [ -f "$top_srcdir/.git/index" ]; then
  54. info "detected Git repository"
  55. repo=git
  56. elif [ -f "$top_srcdir/.svn/format" ]; then
  57. info "detected SVN repository"
  58. repo=svn
  59. else
  60. info "not in a Vcs directory, nothing to do"
  61. exit 0
  62. fi
  63. total_crlfs=0
  64. total_spaces=0
  65. total_tabs=0
  66. OIFS="$IFS"
  67. IFS=$'\n'
  68. if [ "$repo" = git ]; then
  69. FILES="`git ls-files`"
  70. else
  71. FILES="`svn ls -R`"
  72. fi
  73. total_files=0
  74. for file in $FILES; do
  75. if [ -f "$file" ]; then
  76. total_files="$(($total_files + 1))"
  77. fi
  78. done
  79. total_errors=0
  80. for file in $FILES; do
  81. case "$file" in
  82. # These files aren't ours, don't fix
  83. src/bullet/*|external/*|*/generated/*|web/plugins/*)
  84. :
  85. ;;
  86. # Don't harass these people
  87. people/peeweek/*|people/touky/*)
  88. :
  89. ;;
  90. # These files we know how to handle
  91. *.c|*.cpp|*.m|*.mm|*.h|*.hh|*.lolfx|*.l|*.y|*.sh|*.py)
  92. clean=true
  93. # Check for CR LF
  94. if [ "$check_crlf" = true ]; then
  95. ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  96. total_crlfs="$(($total_crlfs + $ncrlfs))"
  97. if [ "$ncrlfs" -gt 0 ]; then
  98. clean=false
  99. if [ "$fix" = true ]; then
  100. $d2u -q "$file"
  101. info "$file has $ncrlfs CR characters"
  102. else
  103. error "$file has $ncrlfs CR characters"
  104. fi
  105. fi
  106. fi
  107. # Check for trailing spaces
  108. nspaces="$(LANG=C sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  109. total_spaces="$(($total_spaces + $nspaces))"
  110. if [ "$nspaces" -gt 0 ]; then
  111. clean=false
  112. if [ "$fix" = true ]; then
  113. LANG=C sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
  114. info "$file has $nspaces trailing spaces"
  115. else
  116. error "$file has $nspaces trailing spaces"
  117. fi
  118. fi
  119. # Check for tabs
  120. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  121. total_tabs="$(($total_tabs + $ntabs))"
  122. if [ "$ntabs" -gt 0 ]; then
  123. clean=false
  124. if [ "$fix" = true ]; then
  125. LANG=C sed -i 's/\t/ /g' "$file"
  126. info "$file has $ntabs tabs"
  127. else
  128. error "$file has $ntabs tabs"
  129. fi
  130. fi
  131. if [ "$clean" != true ]; then
  132. total_errors="$(($total_errors + 1))"
  133. fi
  134. ;;
  135. esac
  136. done
  137. IFS="$OIFS"
  138. if [ "$total_errors" -gt 0 ]; then
  139. if [ "$commit" = "true" ]; then
  140. # EITHER: commit all modified files
  141. svn commit --username lolbot --non-interactive -F - << EOF
  142. fixed $total_errors files out of $total_files:
  143. - removed $total_crlfs CR characters
  144. - removed $total_spaces trailing whitespaces
  145. - replaced $total_tabs tabs with spaces
  146. EOF
  147. elif [ "$fix" = "true" ]; then
  148. # OR: report in stdout
  149. info "fixed $total_errors files out of $total_files:"
  150. if [ "$total_crlfs" -gt 0 ]; then
  151. info " - fixed $total_crlfs CR characters"
  152. fi
  153. if [ "$total_spaces" -gt 0 ]; then
  154. info " - fixed $total_spaces trailing spaces"
  155. fi
  156. if [ "$total_tabs" -gt 0 ]; then
  157. info " - fixed $total_tabs tabs"
  158. fi
  159. info "re-run with -c to commit fixes"
  160. else
  161. # OR: warn about how to fix errors
  162. info "re-run with -w to fix errors"
  163. fi
  164. else
  165. info "all $total_files source files appear to be OK, congratulations"
  166. fi