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.
 
 
 

161 lines
4.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"
  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 what kind of Vcs directory this is
  44. if [ -f "$top_srcdir/.git/index" ]; then
  45. info "detected Git repository"
  46. repo=git
  47. elif [ -f "$top_srcdir/.svn/format" ]; then
  48. info "detected SVN repository"
  49. repo=svn
  50. else
  51. info "not in a Vcs directory, nothing to do"
  52. exit 0
  53. fi
  54. total_errors=0
  55. total_files=0
  56. total_crs=0
  57. total_spaces=0
  58. total_tabs=0
  59. OIFS="$IFS"
  60. IFS=$'\n'
  61. if [ "$repo" = git ]; then
  62. FILES="`git ls-files`"
  63. else
  64. FILES="`svn ls -R`"
  65. fi
  66. for file in $FILES; do
  67. case "$file" in
  68. src/bullet/*|contrib/*|*/generated/*)
  69. : # These files aren't ours, don't fix
  70. ;;
  71. people/peeweek/*|people/touky/*)
  72. : # Don't harass these people
  73. ;;
  74. *.c|*.cpp|*.h|*.l|*.y)
  75. clean=true
  76. # Check for CR LF
  77. ncrs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  78. total_crs="$(($total_crs + $ncrs))"
  79. if [ "$ncrs" -gt 0 ]; then
  80. clean=false
  81. if [ "$fix" = true ]; then
  82. $d2u -q "$file"
  83. info "$file has $ncrs CR characters"
  84. else
  85. error "$file has $ncrs CR characters"
  86. fi
  87. fi
  88. # Check for trailing spaces
  89. nspaces="$(sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  90. total_spaces="$(($total_spaces + $nspaces))"
  91. if [ "$nspaces" -gt 0 ]; then
  92. clean=false
  93. if [ "$fix" = true ]; then
  94. sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
  95. info "$file has $nspaces trailing spaces"
  96. else
  97. error "$file has $nspaces trailing spaces"
  98. fi
  99. fi
  100. # Check for tabs
  101. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  102. total_tabs="$(($total_tabs + $ntabs))"
  103. if [ "$ntabs" -gt 0 ]; then
  104. clean=false
  105. if [ "$fix" = true ]; then
  106. sed -i 's/\t/ /g' "$file"
  107. info "$file has $ntabs tabs"
  108. else
  109. error "$file has $ntabs tabs"
  110. fi
  111. fi
  112. total_files="$(($total_files + 1))"
  113. if [ "$clean" != true ]; then
  114. total_errors="$(($total_errors + 1))"
  115. fi
  116. ;;
  117. esac
  118. done
  119. IFS="$OIFS"
  120. if [ "$total_errors" -gt 0 ]; then
  121. if [ "$commit" = "true" ]; then
  122. # EITHER: commit all modified files
  123. svn commit --username lolbot --non-interactive -F - << EOF
  124. fixed $total_errors files out of $total_files:
  125. - fixed $total_crs CR characters
  126. - fixed $total_spaces trailing spaces
  127. - fixed $total_tabs tabs
  128. EOF
  129. elif [ "$fix" = "true" ]; then
  130. # OR: report in stdout
  131. info "fixed $total_errors files out of $total_files:"
  132. if [ "$total_crs" -gt 0 ]; then
  133. info " - fixed $total_crs CR characters"
  134. fi
  135. if [ "$total_spaces" -gt 0 ]; then
  136. info " - fixed $total_spaces trailing spaces"
  137. fi
  138. if [ "$total_tabs" -gt 0 ]; then
  139. info " - fixed $total_tabs tabs"
  140. fi
  141. else
  142. # OR: warn about how to fix errors
  143. info "re-run with -w to fix errors"
  144. fi
  145. fi