Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

158 rader
3.9 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/*|people/*|*/generated/*)
  69. : # These files aren't ours, don't fix
  70. ;;
  71. *.c|*.cpp|*.h|*.l|*.y)
  72. clean=true
  73. # Check for CR LF
  74. ncrs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
  75. total_crs="$(($total_crs + $ncrs))"
  76. if [ "$ncrs" -gt 0 ]; then
  77. clean=false
  78. if [ "$fix" = true ]; then
  79. $d2u -q "$file"
  80. info "$file has $ncrs CR characters"
  81. else
  82. error "$file has $ncrs CR characters"
  83. fi
  84. fi
  85. # Check for trailing spaces
  86. nspaces="$(sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
  87. total_spaces="$(($total_spaces + $nspaces))"
  88. if [ "$nspaces" -gt 0 ]; then
  89. clean=false
  90. if [ "$fix" = true ]; then
  91. sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
  92. info "$file has $nspaces trailing spaces"
  93. else
  94. error "$file has $nspaces trailing spaces"
  95. fi
  96. fi
  97. # Check for tabs
  98. ntabs="$(tr -cd '\t' < "$file" | wc -c)"
  99. total_tabs="$(($total_tabs + $ntabs))"
  100. if [ "$ntabs" -gt 0 ]; then
  101. clean=false
  102. if [ "$fix" = true ]; then
  103. sed -i 's/\t/ /g' "$file"
  104. info "$file has $ntabs tabs"
  105. else
  106. error "$file has $ntabs tabs"
  107. fi
  108. fi
  109. total_files="$(($total_files + 1))"
  110. if [ "$clean" != true ]; then
  111. total_errors="$(($total_errors + 1))"
  112. fi
  113. ;;
  114. esac
  115. done
  116. IFS="$OIFS"
  117. if [ "$total_errors" -gt 0 ]; then
  118. if [ "$commit" = "true" ]; then
  119. # EITHER: commit all modified files
  120. svn commit --username lolbot --non-interactive -F - << EOF
  121. fixed $total_errors files out of $total_files:
  122. - fixed $total_crs CR characters
  123. - fixed $total_spaces trailing spaces
  124. - fixed $total_tabs tabs
  125. EOF
  126. elif [ "$fix" = "true" ]; then
  127. # OR: report in stdout
  128. info "fixed $total_errors files out of $total_files:"
  129. if [ "$total_crs" -gt 0 ]; then
  130. info " - fixed $total_crs CR characters"
  131. fi
  132. if [ "$total_spaces" -gt 0 ]; then
  133. info " - fixed $total_spaces trailing spaces"
  134. fi
  135. if [ "$total_tabs" -gt 0 ]; then
  136. info " - fixed $total_tabs tabs"
  137. fi
  138. else
  139. # OR: warn about how to fix errors
  140. info "re-run with -w to fix errors"
  141. fi
  142. fi