Skip to content

Commit 8a2428c

Browse files
committed
Merge search logic and separate variables
This commit merge the search logic from 'get_match_position' into 'get_all_match_positions'. Also, directly modifying 'FULL_COMMIT_MSG' is not acceptable. Therefore, separate 'FULL_COMMIT_MSG_WITH_SPACE' and 'MSG_FOR_SPELLCHECK_LINE_FINDING' variables are created for subsequent line number look-ups. Change-Id: I5f98b3989acbf4c22c1eb7bd300bf66fd6e62826
1 parent 1664bf0 commit 8a2428c

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

scripts/commit-msg.hook

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,36 +125,42 @@ read_commit_message() {
125125
}
126126

127127
#
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.
129129
#
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-
144130
#
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"
146133
#
147-
148134
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+
151140
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
153155
[ -z "$result" ] && continue
156+
157+
# output and update states
158+
local line col
154159
read -r line col <<< "$result"
155160
echo "$target: $line"
156161
start_line="$line"
157162
start_col=$((col + 1))
163+
158164
done <<< "$targets"
159165
}
160166

@@ -382,8 +388,9 @@ done
382388
# 12. Avoid abusive language in commit message content
383389
# ------------------------------------------------------------------------------
384390

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')
387394

388395
# Extended list of abusive words (case-insensitive).
389396
# Adjust the list as needed.
@@ -402,14 +409,16 @@ done
402409
add_warning 1 "Commit message appears to be written in Chinese: $MISSPELLED_WORDS"
403410
fi
404411

405-
MSG_FOR_SPELLCHECK=$(echo "$FULL_COMMIT_MSG" | sed -E \
412+
MSG_FOR_SPELLCHECK_LINE_FINDING=$(echo "$FULL_COMMIT_MSG_WITH_SPACE" | sed -E \
406413
-e "s/(['\"][^'\"]*['\"])//g" \
407414
-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+
408417

409418
# Use aspell to list misspelled words according to American English, ignoring quoted text.
410419
MISSPELLED_WORDS=$(echo "$MSG_FOR_SPELLCHECK" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
411420
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")
413422

414423
while read -r result; do
415424
add_warning "${result#*:}" "Avoid using non-American English words: ${result%%:*}"

0 commit comments

Comments
 (0)