1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#!/bin/bash
# a tool to detect whitespace issues in source files or executable files
# with the -F option, it should repair the files
# This should be a superset of what jcheck is looking for
showHelp() {
cat <<-EOF
Usage: $0 [-avSF] [-r <spec>]
-a : use repo manifest for check
-v : verbose
-V : very verbose (debug)
-S : read file list from stdin
-F : fix files
-r <spec> : mercurial revision spec
-x : check for the executable bit (not on windows)
EOF
exit 1
}
# directory this script is in
MYDIR="$( dirname $0 )"
verbose=''
veryverbose=''
allrepo=''
fixfiles=''
readstdin=''
revisionspec=''
executablecheck=''
tofixargs="-F"
while getopts ":avFSVxr:" opt;
do
case $opt in
a)
allrepo='true'
tofixargs="${tofixargs} -a"
;;
S)
readstdin='true'
;;
v)
verbose='true'
;;
V)
verbose='true'
veryverbose='true'
;;
F)
fixfiles='true'
;;
r)
revisionspec="$OPTARG"
;;
x)
executablecheck='true'
tofixargs="${tofixargs} -x"
;;
\?)
echo "Invalid argument $OPTARG"
showHelp
;;
:)
echo "Option -$OPTARG requires an argument." >&2
showHelp
;;
esac
done
match_extension() {
case "$1" in
*.java|*.cc|*.c|*.h|*.cpp|*.hpp|*.jsl|*.fxml|*.css|*.m|*.mm|.*frag|.vert|*.hlsl|*.gradle|*.groovy)
return 0
;;
esac
return 1
}
fix_file() {
endstatus=1
if [ "$executablecheck" -a -x "${file}" ]
then
chmod a-x "${file}"
echo "${file}: execute corrected"
endstatus=0
fi
match_extension "${file}"
if [ $? -eq 0 ]
then
grep -U -q -e $'\t' -e '[[:blank:]]$' -e $'\r' ${file}
if [ $? -eq 0 ]
then
expand -4 < "${file}" | tr -d "\r" | sed -e 's/[[:blank:]]*$//' > "${file}.fws"
if [ -f "${file}.fws" ]
then
rm -f "${file}"
mv "${file}.fws" "${file}"
echo "${file}: fixed"
else
echo "Failed to fix ${file}"
exit -1
fi
endstatus=0
else
[ "$verbose" ] &&
echo "${file}: no change"
fi
fi
return $endstatus
}
check_file() {
file="$1"
message=':'
if [ "$executablecheck" -a -x "${file}" ]
then
message="executable:"
fi
match_extension "${file}"
if [ $? -eq 0 ]
then
grep -Uqs -e $'\t' -e '[[:blank:]]$' -e $'\r' "${file}"
if [ $? -eq 0 ]
then
grep -Uqs -e $'\t' "${file}"
[ $? -eq 0 ] &&
message="${message}tabs:"
grep -Uqs -e '[[:blank:]]$' "${file}"
[ $? -eq 0 ] &&
message="${message}trailingWhitespace:"
grep -Uqs -e $'\r' "${file}"
[ $? -eq 0 ] &&
message="${message}DOS:"
fi
fi
if [ "$message" != ":" ]
then
echo "${file} ${message}"
return 0
else
[ "$verbose" ] && echo "${file} :OK"
return 1
fi
}
process_files() {
let fails=0
while read file
do
if [ "$fixfiles" ]
then
fix_file "${file}"
[ $? -eq 0 ] && let fails=fails+1
else
check_file "${file}"
[ $? -eq 0 ] && let fails=fails+1
fi
done
# show a message if we are not correcting the files
if [ "$fails" -ne 0 -a -z "$fixfiles" ]
then
echo
echo "Found $fails whitespace or executable issues"
echo
echo "To correct, use"
echo " bash ${MYDIR}/checkWhiteSpace ${tofixargs}"
echo
exit 1
elif [ "$fails" -ne 0 -a "$fixfiles" ]
then
echo
echo "Corrected $fails whitespace or executable issues"
echo
exit 1
else
exit 0
fi
}
if [ "$allrepo" ]
then
hg man | process_files
exit $?
fi
if [ "$readstdin" ]
then
process_files
exit $?
fi
# check for "loose" files and check them
loosefiles=`hg status -m -a -n `
if [ ! -z "$loosefiles" -a -z "$revisionspec" ]
then
[ "$verbose" ] && echo "Checking uncommitted files"
hg status -m -a -n | process_files
exit $?
fi
if [ -z "$revisionspec" ]
then
# check for either an applied set of MQ patches
# or the outgoing changeset
hg qtop -s > /dev/null 2>&1
if [ $? -eq 0 ]
then
[ "$verbose" ] && echo "Checking MQ files"
revisionspec="qbase:qtip"
fi
# no loose files, no MQ, lets use outgoing changes
if [ -z "$revisionspec" ]
then
revisionspec="outgoing()"
fi
fi
[ "$verbose" ] && echo "Using a revision range of $revisionspec"
hg log -r "$revisionspec" --style ${MYDIR}/files.style | process_files
exit $?
|