@@ -125,36 +125,42 @@ read_commit_message() {
125
125
}
126
126
127
127
#
128
- # Get the position (line and column) of the first occurrence of a target string .
128
+ # Get positions (line, column) for each target word from aspell output .
129
129
#
130
-
131
- get_match_position () {
132
- local text=" $1 " target=" $2 "
133
- local start_line=" ${3:- 1} " start_col=" ${4:- 1} "
134
- awk -v t=" $target " -v sl=" $start_line " -v sc=" $start_col " ' {
135
- if (NR < sl) next
136
- pos = index(NR == sl ? substr($0, sc) : $0, t)
137
- if (pos) {
138
- print NR, (NR == sl ? pos + sc - 1 : pos)
139
- exit
140
- }
141
- }' <<< " $text"
142
- }
143
-
144
130
#
145
- # Get positions (line,column) for each target word from aspell output.
131
+ # Get positions (line, column) for each target word in a multiline string.
132
+ # Output format: "target: line"
146
133
#
147
-
148
134
get_all_match_positions () {
149
- local text=" $1 " targets=" $2 "
150
- local start_line=1 start_col=1
135
+ local text=" $1 "
136
+ local targets=" $2 "
137
+ local start_line=1
138
+ local start_col=1
139
+
151
140
while IFS= read -r target; do
152
- result=$( get_match_position " $text " " $target " " $start_line " " $start_col " )
141
+ # search for the target string
142
+ local result
143
+ result=$(
144
+ awk -v t=" $target " -v sl=" $start_line " -v sc=" $start_col " ' {
145
+ if (NR < sl) next
146
+ pos = index(NR == sl ? substr($0, sc) : $0, t)
147
+ if (pos) {
148
+ print NR, (NR == sl ? pos + sc - 1 : pos)
149
+ exit
150
+ }
151
+ }' <<< " $text"
152
+ )
153
+
154
+ # skip if the target is not found
153
155
[ -z " $result " ] && continue
156
+
157
+ # output and update states
158
+ local line col
154
159
read -r line col <<< " $result"
155
160
echo " $target : $line "
156
161
start_line=" $line "
157
162
start_col=$(( col + 1 ))
163
+
158
164
done <<< " $targets"
159
165
}
160
166
382
388
# 12. Avoid abusive language in commit message content
383
389
# ------------------------------------------------------------------------------
384
390
385
- FULL_COMMIT_MSG=$( sed ' /^#/d;/^[[:space:]]*Change-Id:/d' " $COMMIT_MSG_FILE " | \
386
- sed -E " s@${URL_REGEX# ^} @@g" )
391
+ FULL_COMMIT_MSG_WITH_SPACE=$( sed ' /^#/d;/^[[:space:]]*Change-Id:/d' " $COMMIT_MSG_FILE " | \
392
+ sed -E " s@${URL_REGEX# ^} @@g" )
393
+ FULL_COMMIT_MSG=$( echo " $FULL_COMMIT_MSG_WITH_SPACE " | sed ' /^[[:space:]]*$/d' )
387
394
388
395
# Extended list of abusive words (case-insensitive).
389
396
# Adjust the list as needed.
@@ -402,14 +409,16 @@ done
402
409
add_warning 1 " Commit message appears to be written in Chinese: $MISSPELLED_WORDS "
403
410
fi
404
411
405
- MSG_FOR_SPELLCHECK =$( echo " $FULL_COMMIT_MSG " | sed -E \
412
+ MSG_FOR_SPELLCHECK_LINE_FINDING =$( echo " $FULL_COMMIT_MSG_WITH_SPACE " | sed -E \
406
413
-e " s/(['\" ][^'\" ]*['\" ])//g" \
407
414
-e " s/\bcommit[[:space:]]+[0-9a-fA-F]{7,40}\b/commit/g" )
415
+ MSG_FOR_SPELLCHECK=$( echo " $MSG_FOR_SPELLCHECK_LINE_FINDING " | sed ' /^[[:space:]]*$/d' )
416
+
408
417
409
418
# Use aspell to list misspelled words according to American English, ignoring quoted text.
410
419
MISSPELLED_WORDS=$( echo " $MSG_FOR_SPELLCHECK " | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
411
420
if [ -n " $MISSPELLED_WORDS " ]; then
412
- results=$( get_all_match_positions " $MSG_FOR_SPELLCHECK " " $MISSPELLED_WORDS " )
421
+ results=$( get_all_match_positions " $MSG_FOR_SPELLCHECK_LINE_FINDING " " $MISSPELLED_WORDS " )
413
422
414
423
while read -r result; do
415
424
add_warning " ${result#*: } " " Avoid using non-American English words: ${result%%:* } "
0 commit comments