Skip to content

Commit 1664bf0

Browse files
committed
Simplify the lengthy commit hook script
Simplify the lengthy script by removing redundant logic and optimizing operations. The function 'get_match_position' has been streamlined by eliminating unnecessary if-else conditions, making it more concise while maintaining functionality. Multiple 'sed' operations have been merged to avoid redundant calls, improving efficiency. Additionally, unused comment lines have been removed to enhance readability. The function 'get_all_match_positions' has been updated to return results in the format 'target: line' only, eliminating the need to process column positions. Consequently, the spell-checking loop has been revised to handle this new output format directly, simplifying the logic and improving maintainability. Change-Id: I199e17099c133059419de11aaa9e4db836551ed1
1 parent de7af21 commit 1664bf0

File tree

1 file changed

+23
-49
lines changed

1 file changed

+23
-49
lines changed

scripts/commit-msg.hook

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -124,49 +124,35 @@ read_commit_message() {
124124
done < $COMMIT_MSG_FILE
125125
}
126126

127-
### Get the position (line and column) of the first occurrence of a target string.
128-
# Usage: get_match_position "$text" "$target" [start_line] [start_col]
129-
# Parameters:
130-
# text - multiline text to search in.
131-
# target - string to search for.
132-
# start_line - (optional) starting line number (default: 1).
133-
# start_col - (optional) starting column number (default: 1).
127+
#
128+
# Get the position (line and column) of the first occurrence of a target string.
129+
#
130+
134131
get_match_position() {
135132
local text="$1" target="$2"
136133
local start_line="${3:-1}" start_col="${4:-1}"
137134
awk -v t="$target" -v sl="$start_line" -v sc="$start_col" '{
138-
if (NR < sl) next;
139-
if (NR == sl) {
140-
pos = index(substr($0, sc), t);
141-
if (pos) { print NR, pos+sc-1; exit }
142-
} else {
143-
pos = index($0, t);
144-
if (pos) { print NR, pos; exit }
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
145140
}
146141
}' <<< "$text"
147142
}
148143

149-
### For each target (one per line) in a multiline string,
150-
### search the text and call add_warning with the target's first match.
151-
# Parameters:
152-
# $1 - multiline text to search in.
153-
# $2 - targets (each line is a target string).
154-
get_all_match_positions() {
155-
local text="$1"
156-
local targets="$2"
157-
local start_line=1
158-
local start_col=1
159-
local target result line col
144+
#
145+
# Get positions (line,column) for each target word from aspell output.
146+
#
160147

148+
get_all_match_positions() {
149+
local text="$1" targets="$2"
150+
local start_line=1 start_col=1
161151
while IFS= read -r target; do
162-
[ -z "$target" ] && continue
163152
result=$(get_match_position "$text" "$target" "$start_line" "$start_col")
164-
165153
[ -z "$result" ] && continue
166-
read line col <<< "$result"
167-
echo "$target: $line,$col"
168-
169-
# Update global progress for subsequent searches.
154+
read -r line col <<< "$result"
155+
echo "$target: $line"
170156
start_line="$line"
171157
start_col=$((col + 1))
172158
done <<< "$targets"
@@ -396,10 +382,9 @@ done
396382
# 12. Avoid abusive language in commit message content
397383
# ------------------------------------------------------------------------------
398384

399-
FULL_COMMIT_MSG=$(sed '/^#/d;/^[[:space:]]*$/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | \
400-
sed -E "s@${URL_REGEX#^}@@g")
401-
FULL_COMMIT_MSG_WITH_EMPTY=$(sed '/^#/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | \
385+
FULL_COMMIT_MSG=$(sed '/^#/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | \
402386
sed -E "s@${URL_REGEX#^}@@g")
387+
403388
# Extended list of abusive words (case-insensitive).
404389
# Adjust the list as needed.
405390
ABUSIVE_WORDS_REGEX='\b(fuck|fucking|dick|shit|bitch|asshole|cunt|motherfucker|damn|crap|dumbass|piss)\b'
@@ -417,29 +402,18 @@ done
417402
add_warning 1 "Commit message appears to be written in Chinese: $MISSPELLED_WORDS"
418403
fi
419404

420-
# Remove quoted text and commit hashes from $FULL_COMMIT_MSG for spell checking.
421-
# Handles commit references like "commit 7d05741" (short) or full 40-char hashes.
422405
MSG_FOR_SPELLCHECK=$(echo "$FULL_COMMIT_MSG" | sed -E \
423406
-e "s/(['\"][^'\"]*['\"])//g" \
424407
-e "s/\bcommit[[:space:]]+[0-9a-fA-F]{7,40}\b/commit/g")
425-
MSG_SPELLCHECK_FOR_LINE_FINDING=$(echo "$FULL_COMMIT_MSG_WITH_EMPTY" | sed -E \
426-
-e "s/(['\"][^'\"]*['\"])//g" \
427-
-e "s/\bcommit[[:space:]]+[0-9a-fA-F]{7,40}\b/commit/g")
428-
408+
429409
# Use aspell to list misspelled words according to American English, ignoring quoted text.
430410
MISSPELLED_WORDS=$(echo "$MSG_FOR_SPELLCHECK" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
431411
if [ -n "$MISSPELLED_WORDS" ]; then
432-
results=$(get_all_match_positions "$MSG_SPELLCHECK_FOR_LINE_FINDING" "$MISSPELLED_WORDS")
412+
results=$(get_all_match_positions "$MSG_FOR_SPELLCHECK" "$MISSPELLED_WORDS")
433413

434-
while IFS= read -r result; do
435-
# Expected format: "target: line,column"
436-
local target=$(echo "$result" | cut -d: -f1)
437-
local pos=$(echo "$result" | cut -d: -f2 | tr -d ' ')
438-
local line=$(echo "$pos" | cut -d, -f1)
439-
440-
add_warning "$line" "Avoid using non-American English words: $target"
414+
while read -r result; do
415+
add_warning "${result#*:}" "Avoid using non-American English words: ${result%%:*}"
441416
done <<< "$results"
442-
443417
fi
444418
}
445419

0 commit comments

Comments
 (0)