Skip to content

Commit 12cc449

Browse files
committed
Update line length check
1 parent 416029c commit 12cc449

2 files changed

Lines changed: 149 additions & 108 deletions

File tree

.github/workflows/line-length-check.yml

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -17,111 +17,5 @@ jobs:
1717

1818
- name: Check line length in PR changes
1919
run: |
20-
# Get the base branch (usually main/master)
21-
BASE_BRANCH="${{ github.event.pull_request.base.ref }}"
22-
23-
echo "Checking line length (max 80 characters) for changed files in " \
24-
"src/, examples/, and native/ directories..."
25-
echo "================================================================"
26-
27-
# Create temporary files with unique names
28-
changed_files=$(mktemp)
29-
violations_file=$(mktemp)
30-
31-
# Get all changed files in this PR and filter for target directories
32-
git diff --name-only "origin/$BASE_BRANCH"...HEAD | \
33-
grep -E '^(src/|examples/|native/)' > "$changed_files" || true
34-
35-
# Initialize violation count
36-
violation_count=0
37-
38-
# Check each changed file
39-
while IFS= read -r file; do
40-
if [[ -f "$file" ]]; then
41-
# Skip WolfSSLProvider.java...
42-
# that legitimately exceed 80 characters
43-
if [[ "$file" == "src/java/com/wolfssl/provider/jsse/WolfSSLProvider.java" ]]; then
44-
echo " Skipping $file (contains security service mappings)"
45-
continue
46-
fi
47-
48-
# Skip certificate and key files
49-
if [[ "$file" =~ \.(pem|crt|cer|der|key)$ ]]; then
50-
echo " Skipping $file (certificate/key file)"
51-
continue
52-
fi
53-
54-
# Skip shell scripts (often have long command lines)
55-
if [[ "$file" =~ \.sh$ ]]; then
56-
echo " Skipping $file (shell script)"
57-
continue
58-
fi
59-
60-
echo "Checking: $file"
61-
62-
# Get added lines with actual file line numbers and check their length
63-
new_line_num=0
64-
git diff "origin/$BASE_BRANCH"...HEAD "$file" | \
65-
while IFS= read -r line; do
66-
# Track line numbers from diff headers - format: @@ -old_start,old_count +new_start,new_count @@
67-
if [[ "$line" =~ ^@@.*\+([0-9]+) ]]; then
68-
# Extract starting line number for new file (after +)
69-
# Subtract 1 because we'll increment before processing first line
70-
new_line_num=$((${BASH_REMATCH[1]} - 1))
71-
elif [[ "$line" =~ ^(\+[^+].*) ]]; then
72-
# This is an added line (not a +++ header)
73-
# Increment line number BEFORE processing (since this line exists in new file)
74-
new_line_num=$((new_line_num + 1))
75-
added_line="${line:1}" # Remove leading +
76-
char_count=${#added_line}
77-
78-
# Skip JNI method signatures and calls to avoid false positives
79-
# These are auto-generated names that can't be shortened
80-
if [[ $char_count -gt 80 ]]; then
81-
# Check if this is a JNI method signature, call, or parameter line that should be ignored
82-
if [[ "$added_line" =~ JNIEXPORT.*JNICALL.*Java_com_wolfssl_ ]] || \
83-
[[ "$added_line" =~ Java_com_wolfssl_.*\( ]] || \
84-
[[ "$added_line" =~ ^[[:space:]]*return[[:space:]]+Java_com_wolfssl_.* ]] || \
85-
[[ "$added_line" =~ ^[[:space:]]*\(JNIEnv\*[[:space:]]+env.*\) ]] || \
86-
[[ "$added_line" =~ ^[[:space:]]*JNIEnv\*[[:space:]]+env.* ]]; then
87-
echo " $file:$new_line_num - Skipping JNI method signature/call/parameters ($char_count characters)"
88-
echo " Line: $added_line"
89-
else
90-
echo " $file:$new_line_num - Line too long ($char_count characters)"
91-
echo " Line: $added_line"
92-
echo "violation" >> "$violations_file"
93-
fi
94-
fi
95-
elif [[ "$line" =~ ^[[:space:]] ]]; then
96-
# Context line (unchanged) - increment new file line number
97-
new_line_num=$((new_line_num + 1))
98-
# Removed lines (starting with -) don't affect new file line numbers
99-
fi
100-
done
101-
fi
102-
done < "$changed_files"
103-
104-
# Count violations
105-
if [[ -f "$violations_file" ]]; then
106-
violation_count=$(grep -c "violation" "$violations_file" || echo 0)
107-
else
108-
violation_count=0
109-
fi
110-
111-
echo "================================================================"
112-
113-
if [[ $violation_count -gt 0 ]]; then
114-
echo " Found $violation_count line(s) exceeding 80 " \
115-
"characters in PR changes"
116-
echo ""
117-
echo "Please ensure all lines are 80 characters or less " \
118-
"as per coding standards."
119-
echo "You can check line length in your editor or use this command:"
120-
echo " grep -n '.\{81,\}' <filename>"
121-
rm -f "$violations_file" "$changed_files"
122-
exit 1
123-
else
124-
echo "All changed lines are within the 80 character limit"
125-
rm -f "$violations_file" "$changed_files"
126-
exit 0
127-
fi
20+
BASE_REF="${{ github.event.pull_request.base.ref }}"
21+
./scripts/line-length-check-added-lines.sh "origin/$BASE_REF"
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Check for >80-char lines introduced by the current diff, ignoring existing
4+
# long lines.
5+
#
6+
# To run automatically, create one of these files:
7+
# .git/hooks/pre-commit (local checks before commit)
8+
# .git/hooks/pre-push (checks before pushing to remote)
9+
#
10+
# Example hook file content:
11+
# #!/usr/bin/env bash
12+
# set -euo pipefail
13+
# if [[ -z "${BASE_REF:-}" ]]; then
14+
# BASE_REF="master"
15+
# fi
16+
# if [[ -f "scripts/line-length-check-added-lines.sh" ]]; then
17+
# scripts/line-length-check-added-lines.sh "$BASE_REF"
18+
# else
19+
# echo "line-length-check-added-lines.sh not found, skipping line length check"
20+
# fi
21+
22+
set -euo pipefail
23+
24+
BASE_INPUT="${1:-${BASE_REF:-}}"
25+
if [[ -z "$BASE_INPUT" ]]; then
26+
echo "Usage: $0 <base-ref>" >&2
27+
echo "Or set BASE_REF in the environment." >&2
28+
exit 2
29+
fi
30+
BASE_REF="$BASE_INPUT"
31+
32+
echo "Checking line length (max 80 characters) against base ref: $BASE_REF"
33+
34+
all_files="$(mktemp -t all_files.XXXXXX)"
35+
long_lines="$(mktemp -t long_lines.XXXXXX)"
36+
violations_file="$(mktemp -t violations_file.XXXXXX)"
37+
38+
cleanup() {
39+
rm -f "$all_files" "$long_lines" "$violations_file" "${added_lines_file:-}"
40+
}
41+
trap cleanup EXIT
42+
43+
# Scan long lines across repo (tracked files), then filter to the diff.
44+
# Limit to src/, examples/, and native/ directories.
45+
git ls-files -- src examples native > "$all_files"
46+
47+
violation_count=0
48+
49+
while IFS= read -r file; do
50+
if [[ -f "$file" ]]; then
51+
if [[ "$file" == \
52+
"src/java/com/wolfssl/provider/jsse/WolfSSLProvider.java" ]]; then
53+
continue
54+
fi
55+
56+
if [[ "$file" =~ \.(pem|crt|cer|der|key)$ ]]; then
57+
continue
58+
fi
59+
60+
if [[ "$file" =~ \.sh$ ]]; then
61+
continue
62+
fi
63+
64+
if ! LC_ALL=C grep -Iq . "$file"; then
65+
continue
66+
fi
67+
68+
LC_ALL=C awk -v file="$file" \
69+
'length($0) > 80 { print file ":" FNR ":" $0 }' "$file" \
70+
>> "$long_lines"
71+
fi
72+
done < "$all_files"
73+
74+
while IFS= read -r entry; do
75+
file="${entry%%:*}"
76+
rest="${entry#*:}"
77+
line_num="${rest%%:*}"
78+
line_text="${rest#*:}"
79+
80+
if [[ "$file" != "${last_file:-}" ]]; then
81+
rm -f "${added_lines_file:-}"
82+
added_lines_file="$(mktemp -t added_lines_file.XXXXXX)"
83+
git diff -U0 "$BASE_REF"...HEAD -- "$file" | \
84+
awk '
85+
/^@@/ {
86+
if (match($0, /\+[0-9]+/)) {
87+
n = substr($0, RSTART + 1, RLENGTH - 1)
88+
}
89+
next
90+
}
91+
/^\+/ && !/^\+\+\+/ {
92+
print n
93+
n++
94+
}
95+
' > "$added_lines_file"
96+
last_file="$file"
97+
fi
98+
99+
if grep -qx "$line_num" "$added_lines_file"; then
100+
jni_skip=0
101+
if [[ "$line_text" =~ JNIEXPORT.*JNICALL.*Java_com_wolfssl_ ]]; then
102+
jni_skip=1
103+
elif [[ "$line_text" =~ Java_com_wolfssl_.*\( ]]; then
104+
jni_skip=1
105+
elif [[ "$line_text" =~ \
106+
^[[:space:]]*return[[:space:]]+Java_com_wolfssl_.* ]]; then
107+
jni_skip=1
108+
elif [[ "$line_text" =~ \
109+
^[[:space:]]*\(JNIEnv\*[[:space:]]+env.*\) ]]; then
110+
jni_skip=1
111+
elif [[ "$line_text" =~ ^[[:space:]]*JNIEnv\*[[:space:]]+env.* ]]; then
112+
jni_skip=1
113+
fi
114+
115+
if [[ $jni_skip -eq 1 ]]; then
116+
if [[ "${VERBOSE:-0}" == "1" ]]; then
117+
printf " %s:%s - Skipping JNI line (%d chars)\n" \
118+
"$file" "$line_num" "${#line_text}"
119+
echo " Line: $line_text"
120+
fi
121+
else
122+
printf " %s:%s - Line too long (%d chars)\n" \
123+
"$file" "$line_num" "${#line_text}"
124+
echo " Line: $line_text"
125+
echo "violation" >> "$violations_file"
126+
fi
127+
fi
128+
done < "$long_lines"
129+
130+
if [[ -f "$violations_file" ]]; then
131+
violation_count=$(grep -c "violation" "$violations_file" || true)
132+
violation_count=${violation_count:-0}
133+
else
134+
violation_count=0
135+
fi
136+
137+
if [[ $violation_count -gt 0 ]]; then
138+
echo " Found $violation_count line(s) exceeding 80 chars in PR changes"
139+
echo ""
140+
echo "Please ensure all lines are 80 characters or less."
141+
echo "Check line length in your editor or use:"
142+
echo " grep -n '.\{81,\}' <filename>"
143+
exit 1
144+
else
145+
echo "All changed lines are within the 80 character limit"
146+
exit 0
147+
fi

0 commit comments

Comments
 (0)