Skip to content

Commit

Permalink
Merge search logic and separate variables
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dennis40816 committed Feb 26, 2025
1 parent 1664bf0 commit 8a2428c
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions scripts/commit-msg.hook
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,42 @@ read_commit_message() {
}

#
# Get the position (line and column) of the first occurrence of a target string.
# Get positions (line,column) for each target word from aspell output.
#

get_match_position() {
local text="$1" target="$2"
local start_line="${3:-1}" start_col="${4:-1}"
awk -v t="$target" -v sl="$start_line" -v sc="$start_col" '{
if (NR < sl) next
pos = index(NR == sl ? substr($0, sc) : $0, t)
if (pos) {
print NR, (NR == sl ? pos + sc - 1 : pos)
exit
}
}' <<< "$text"
}

#
# Get positions (line,column) for each target word from aspell output.
# Get positions (line, column) for each target word in a multiline string.
# Output format: "target: line"
#

get_all_match_positions() {
local text="$1" targets="$2"
local start_line=1 start_col=1
local text="$1"
local targets="$2"
local start_line=1
local start_col=1

while IFS= read -r target; do
result=$(get_match_position "$text" "$target" "$start_line" "$start_col")
# search for the target string
local result
result=$(
awk -v t="$target" -v sl="$start_line" -v sc="$start_col" '{
if (NR < sl) next
pos = index(NR == sl ? substr($0, sc) : $0, t)
if (pos) {
print NR, (NR == sl ? pos + sc - 1 : pos)
exit
}
}' <<< "$text"
)

# skip if the target is not found
[ -z "$result" ] && continue

# output and update states
local line col
read -r line col <<< "$result"
echo "$target: $line"
start_line="$line"
start_col=$((col + 1))

done <<< "$targets"
}

Expand Down Expand Up @@ -382,8 +388,9 @@ done
# 12. Avoid abusive language in commit message content
# ------------------------------------------------------------------------------

FULL_COMMIT_MSG=$(sed '/^#/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | \
sed -E "s@${URL_REGEX#^}@@g")
FULL_COMMIT_MSG_WITH_SPACE=$(sed '/^#/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | \
sed -E "s@${URL_REGEX#^}@@g")
FULL_COMMIT_MSG=$(echo "$FULL_COMMIT_MSG_WITH_SPACE" | sed '/^[[:space:]]*$/d')

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

MSG_FOR_SPELLCHECK=$(echo "$FULL_COMMIT_MSG" | sed -E \
MSG_FOR_SPELLCHECK_LINE_FINDING=$(echo "$FULL_COMMIT_MSG_WITH_SPACE" | sed -E \
-e "s/(['\"][^'\"]*['\"])//g" \
-e "s/\bcommit[[:space:]]+[0-9a-fA-F]{7,40}\b/commit/g")
MSG_FOR_SPELLCHECK=$(echo "$MSG_FOR_SPELLCHECK_LINE_FINDING" | sed '/^[[:space:]]*$/d')


# Use aspell to list misspelled words according to American English, ignoring quoted text.
MISSPELLED_WORDS=$(echo "$MSG_FOR_SPELLCHECK" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
if [ -n "$MISSPELLED_WORDS" ]; then
results=$(get_all_match_positions "$MSG_FOR_SPELLCHECK" "$MISSPELLED_WORDS")
results=$(get_all_match_positions "$MSG_FOR_SPELLCHECK_LINE_FINDING" "$MISSPELLED_WORDS")

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

0 comments on commit 8a2428c

Please sign in to comment.