# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# Date 1224320772 -7200
# Node ID f0ce608e320e360021419137604c66a09d66762f
# Parent 6dbd5184c6a99e936a7de899e64c114e30c0f94f
Reworked unifier script (ticket #138)
The new script can be used either to check and to unify the source
files. It can also be used as a hook script that checks all the modified
source files. For more information run the script with --help option.
Other changes:
- The Makefile.am files are also checked/unified.
- Long lines are checked only for .h and .cc files.
- The way how we enumerate the code files is changed because the
manifest command also enumerates the removed files, which raised
error in the script.
- Add --check and --modified options for checking.
- Add --help option for getting help.
diff --git a/scripts/unify-sources.sh b/scripts/unify-sources.sh
a
|
b
|
|
25 | 25 | * |
26 | 26 | */ |
27 | 27 | " |
28 | | awk 'BEGIN { pm=0; } |
| 28 | awk 'BEGIN { pm=0; } |
29 | 29 | pm==3 { print } |
30 | 30 | /\/\* / && pm==0 { pm=1;} |
31 | 31 | /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;} |
32 | 32 | /\*\// && pm==1 { pm=2;} |
33 | 33 | ' $1 |
34 | | ) >$TMP_FILE |
35 | | |
36 | | HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` |
37 | | |
38 | | rm $FILE_NAME |
39 | | mv $TMP_FILE $FILE_NAME |
| 34 | ) >$TMP_FILE |
| 35 | |
| 36 | if [[ $CHECK_MODE -gt 0 ]]; |
| 37 | then |
| 38 | if ! diff -q $TMP_FILE $FILE_NAME >/dev/null ; |
| 39 | then |
| 40 | echo "header failed" |
| 41 | FILE_FAILED=YES |
| 42 | fi |
| 43 | else |
| 44 | HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` |
| 45 | rm $FILE_NAME |
| 46 | mv $TMP_FILE $FILE_NAME |
| 47 | fi |
40 | 48 | } |
41 | 49 | |
42 | 50 | function update_tabs() { |
43 | | TMP_FILE=`mktemp` |
44 | | FILE_NAME=$1 |
| 51 | if echo $1 | grep -q -v -E 'Makefile\.am$' ; |
| 52 | then |
| 53 | OLD_PATTERN=`echo -e '\t'` |
| 54 | NEW_PATTERN=' ' |
| 55 | else |
| 56 | OLD_PATTERN=' ' |
| 57 | NEW_PATTERN=`echo -e '\t'` |
| 58 | fi |
45 | 59 | |
46 | | cat $1 | |
47 | | sed -e 's/\t/ /g' >$TMP_FILE |
48 | | |
49 | | TABS_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` |
50 | | |
51 | | rm $FILE_NAME |
52 | | mv $TMP_FILE $FILE_NAME |
| 60 | if [[ $CHECK_MODE -gt 0 ]]; |
| 61 | then |
| 62 | if grep -q -E "$OLD_PATTERN" $1 ; |
| 63 | then |
| 64 | echo "tabs failed:" |
| 65 | grep -n -E "$OLD_PATTERN" $1 |
| 66 | FILE_FAILED=YES |
| 67 | fi |
| 68 | else |
| 69 | TMP_FILE=`mktemp` |
| 70 | cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE |
| 71 | TABS_CH=`diff -q $TMP_FILE $1 >/dev/null&&echo NO||echo YES` |
| 72 | rm $1 |
| 73 | mv $TMP_FILE $1 |
| 74 | fi |
53 | 75 | } |
54 | 76 | |
55 | | function remove_trailing_space() { |
56 | | TMP_FILE=`mktemp` |
57 | | FILE_NAME=$1 |
58 | | |
59 | | cat $1 | |
60 | | sed -e 's/ \+$//g' >$TMP_FILE |
61 | | |
62 | | SPACES_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES` |
63 | | |
64 | | rm $FILE_NAME |
65 | | mv $TMP_FILE $FILE_NAME |
| 77 | function remove_trailing_spaces() { |
| 78 | if [[ $CHECK_MODE -gt 0 ]]; |
| 79 | then |
| 80 | if grep -q -E ' $' $1 ; |
| 81 | then |
| 82 | echo "trailing spaces failed:" |
| 83 | grep -n -E ' $' $1 |
| 84 | FILE_FAILED=YES |
| 85 | fi |
| 86 | else |
| 87 | TMP_FILE=`mktemp` |
| 88 | cat $1 | sed -e 's/ \+$//g' >$TMP_FILE |
| 89 | SPACES_CH=`diff -q $TMP_FILE $1 >/dev/null&&echo NO||echo YES` |
| 90 | rm $1 |
| 91 | mv $TMP_FILE $1 |
| 92 | fi |
66 | 93 | } |
67 | 94 | |
68 | 95 | function long_line_test() { |
69 | | cat $1 |grep -q -E '.{81,}' |
| 96 | cat $1 | grep -q -E '.{81,}' |
70 | 97 | } |
71 | 98 | |
72 | 99 | function update_file() { |
73 | | echo -n ' update' $i ... |
| 100 | echo -n ' update' $1... |
74 | 101 | |
75 | | update_header $1 |
| 102 | if echo $1 | grep -q -v -E 'Makefile\.am$' ; |
| 103 | then |
| 104 | update_header $1 |
| 105 | fi |
76 | 106 | update_tabs $1 |
77 | | remove_trailing_space $1 |
| 107 | remove_trailing_spaces $1 |
78 | 108 | |
79 | | CHANGED=NO; |
| 109 | CHANGED=NO |
80 | 110 | if [[ $HEADER_CH = YES ]]; |
81 | 111 | then |
82 | | echo -n ' [header updated]' |
83 | | CHANGED=YES; |
| 112 | echo -n ' [header updated]' |
| 113 | CHANGED=YES |
84 | 114 | fi |
85 | 115 | if [[ $TABS_CH = YES ]]; |
86 | 116 | then |
87 | | echo -n ' [tabs removed]' |
88 | | CHANGED=YES; |
| 117 | if echo $1 | grep -q -v -E 'Makefile\.am$' ; |
| 118 | then |
| 119 | echo -n ' [tabs removed]' |
| 120 | else |
| 121 | echo -n ' [spaces replaced with tabs]' |
| 122 | fi |
| 123 | CHANGED=YES |
89 | 124 | fi |
90 | 125 | if [[ $SPACES_CH = YES ]]; |
91 | 126 | then |
92 | | echo -n ' [trailing spaces removed]' |
93 | | CHANGED=YES; |
| 127 | echo -n ' [trailing spaces removed]' |
| 128 | CHANGED=YES |
94 | 129 | fi |
95 | | if long_line_test $1 ; |
| 130 | if echo $1 | grep -q -v -E '\.dox|Makefile\.am$' ; |
96 | 131 | then |
97 | | echo -n ' [LONG LINES]' |
98 | | ((LONG_LINE_FILES++)) |
| 132 | if long_line_test $1 ; |
| 133 | then |
| 134 | echo -n ' [LONG LINES]' |
| 135 | ((LONG_LINE_FILES++)) |
| 136 | fi |
99 | 137 | fi |
100 | 138 | echo |
101 | 139 | if [[ $CHANGED = YES ]]; |
102 | 140 | then |
103 | | ((CHANGED_FILES++)) |
| 141 | ((CHANGED_FILES++)) |
104 | 142 | fi |
| 143 | } |
| 144 | |
| 145 | function check_file() { |
| 146 | echo ' check' $1... |
| 147 | |
| 148 | FILE_FAILED=NO |
| 149 | if echo $1 | grep -q -v -E 'Makefile\.am$' ; |
| 150 | then |
| 151 | update_header $1 |
| 152 | fi |
| 153 | update_tabs $1 |
| 154 | remove_trailing_spaces $1 |
| 155 | if echo $1 | grep -q -v -E '\.dox|Makefile\.am$' ; |
| 156 | then |
| 157 | if long_line_test $1 ; |
| 158 | then |
| 159 | echo 'long lines failed:' |
| 160 | grep -n -E '.{81,}' $1 |
| 161 | FILE_FAILED=YES |
| 162 | fi |
| 163 | fi |
| 164 | |
| 165 | if [[ $FILE_FAILED = YES ]]; |
| 166 | then |
| 167 | GLOBAL_FAILED=YES |
| 168 | ((FAILED_FILES++)) |
| 169 | fi |
| 170 | } |
| 171 | |
| 172 | function list_files() { |
| 173 | hg status -a -m -c | |
| 174 | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq |
| 175 | } |
| 176 | |
| 177 | function changed_files() { |
| 178 | { |
| 179 | if [ -n "$HG_PARENT1" ] |
| 180 | then |
| 181 | hg status --rev $HG_PARENT1:$HG_NODE -a -m |
| 182 | fi |
| 183 | if [ -n "$HG_PARENT2" ] |
| 184 | then |
| 185 | hg status --rev $HG_PARENT2:$HG_NODE -a -m |
| 186 | fi |
| 187 | } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq |
105 | 188 | } |
106 | 189 | |
107 | 190 | CHANGED_FILES=0 |
108 | 191 | TOTAL_FILES=0 |
109 | 192 | LONG_LINE_FILES=0 |
110 | | if [ $# == 0 ]; then |
| 193 | FAILED_FILES=0 |
| 194 | GLOBAL_FAILED=NO |
| 195 | CHECK_MODE=0 |
| 196 | |
| 197 | if [ $# == 0 ]; |
| 198 | then |
111 | 199 | echo Update all source files... |
112 | | for i in `hg manifest|grep -E '\.(cc|h|dox)$'` |
| 200 | while read i |
113 | 201 | do |
114 | | update_file $HGROOT/$i |
115 | | ((TOTAL_FILES++)) |
116 | | done |
| 202 | update_file "$HGROOT/$i" |
| 203 | ((TOTAL_FILES++)) |
| 204 | done < <(list_files) |
117 | 205 | echo ' done.' |
| 206 | elif [[ ($1 == "--modified") || ($1 == "-modified") || ($1 == "-m") ]]; |
| 207 | then |
| 208 | CHECK_MODE=2 |
| 209 | echo Check all modified source files... |
| 210 | while read i |
| 211 | do |
| 212 | check_file "$HGROOT/$i" |
| 213 | ((TOTAL_FILES++)) |
| 214 | done < <(changed_files) |
| 215 | echo ' done.' |
| 216 | elif [[ ($1 == "--check") || ($1 == "-check") || ($1 == "-c") ]]; |
| 217 | then |
| 218 | CHECK_MODE=1 |
| 219 | if [[ $# == 1 ]]; |
| 220 | then |
| 221 | echo Check all source files... |
| 222 | while read i |
| 223 | do |
| 224 | check_file "$HGROOT/$i" |
| 225 | ((TOTAL_FILES++)) |
| 226 | done < <(list_files) |
| 227 | echo ' done.' |
| 228 | else |
| 229 | echo Check files... |
| 230 | shift |
| 231 | for i in $@ |
| 232 | do |
| 233 | check_file $i |
| 234 | ((TOTAL_FILES++)) |
| 235 | done |
| 236 | fi |
| 237 | elif [[ ($1 == "--help") || ($1 == "-help") || ($1 == "-h") ]]; |
| 238 | then |
| 239 | echo "Usage: |
| 240 | $0 [(--check|-c)|(--modified|-m)|(--help|-h)] [files] |
| 241 | Where: |
| 242 | --check|-c |
| 243 | Check the given files, but do not modify them. |
| 244 | --modified|-m |
| 245 | Check only the modified source files, but do not modify them. |
| 246 | This option can be used as a hg hook script. E.g. to check all your |
| 247 | commited source files with this script add the following section to |
| 248 | the appropriate .hg/hgrc file. |
| 249 | |
| 250 | [hooks] |
| 251 | pretxncommit.checksources = scripts/unify-sources.sh --modified |
| 252 | |
| 253 | --help|-h |
| 254 | Print this help message. |
| 255 | files |
| 256 | The files to check/unify. If no file names are given, all source |
| 257 | will be checked/unified (except for using --modified, which checks |
| 258 | only the modified source files). |
| 259 | If no option is given, the script unifies the given files, but if |
| 260 | --check or --modified is given, it only checks them." |
| 261 | exit |
118 | 262 | else |
119 | | for i in $* |
| 263 | echo Update files... |
| 264 | for i in $@ |
120 | 265 | do |
121 | | update_file $i |
122 | | ((TOTAL_FILES++)) |
| 266 | update_file $i |
| 267 | ((TOTAL_FILES++)) |
123 | 268 | done |
124 | 269 | fi |
125 | | echo $CHANGED_FILES out of $TOTAL_FILES files has been changed. |
126 | | if [[ $LONG_LINE_FILES -gt 1 ]]; then |
| 270 | |
| 271 | if [[ $CHECK_MODE -gt 0 ]]; |
| 272 | then |
| 273 | if [[ $FAILED_FILES -gt 0 ]]; |
| 274 | then |
| 275 | echo $FAILED_FILES out of $TOTAL_FILES files has failed. |
| 276 | else |
| 277 | echo All $TOTAL_FILES files passed. |
| 278 | fi |
| 279 | else |
| 280 | echo $CHANGED_FILES out of $TOTAL_FILES files has been changed. |
| 281 | fi |
| 282 | |
| 283 | if [[ $LONG_LINE_FILES -gt 1 ]]; |
| 284 | then |
127 | 285 | echo |
128 | | echo WARNING: $LONG_LINE_FILES files contains long lines! |
| 286 | echo WARNING: $LONG_LINE_FILES files contain long lines! |
129 | 287 | echo |
130 | | elif [[ $LONG_LINE_FILES -gt 0 ]]; then |
| 288 | elif [[ $LONG_LINE_FILES -gt 0 ]]; |
| 289 | then |
131 | 290 | echo |
132 | 291 | echo WARNING: a file contains long lines! |
133 | 292 | echo |
134 | 293 | fi |
| 294 | |
| 295 | [[ $GLOBAL_FAILED == NO ]] |