|
| 1 | +name: Line Length Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ '*' ] |
| 6 | + |
| 7 | +jobs: |
| 8 | + line-length-check: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + name: Check 80 character line limit |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + fetch-depth: 0 |
| 17 | + |
| 18 | + - name: Check line length in PR changes |
| 19 | + 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 jni/ directories..." |
| 25 | + echo "================================================================" |
| 26 | +
|
| 27 | + # Get all changed files in this PR and filter for target directories |
| 28 | + git diff --name-only "origin/$BASE_BRANCH"...HEAD | \ |
| 29 | + grep -E '^(src/|examples/|jni/)' > changed_files.txt || true |
| 30 | +
|
| 31 | + # Create a temporary file to collect violations |
| 32 | + violation_count=0 |
| 33 | + echo > violations.tmp |
| 34 | +
|
| 35 | + # Check each changed file |
| 36 | + while IFS= read -r file; do |
| 37 | + if [[ -f "$file" ]]; then |
| 38 | + echo "Checking: $file" |
| 39 | +
|
| 40 | + # Get added lines and check their length |
| 41 | + git diff "origin/$BASE_BRANCH"...HEAD "$file" | \ |
| 42 | + grep '^+' | grep -v '^+++' | \ |
| 43 | + while read -r added_line; do |
| 44 | + # Remove the leading + |
| 45 | + actual_line="${added_line:1}" |
| 46 | + char_count=${#actual_line} |
| 47 | +
|
| 48 | + if [[ $char_count -gt 80 ]]; then |
| 49 | + echo "❌ $file - Line too long ($char_count characters)" |
| 50 | + echo " Line: $actual_line" |
| 51 | + echo "violation" >> violations.tmp |
| 52 | + fi |
| 53 | + done |
| 54 | + fi |
| 55 | + done < changed_files.txt |
| 56 | +
|
| 57 | + # Count violations |
| 58 | + if [[ -f violations.tmp ]]; then |
| 59 | + violation_count=$(grep -c "violation" violations.tmp || echo 0) |
| 60 | + else |
| 61 | + violation_count=0 |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "================================================================" |
| 65 | +
|
| 66 | + if [[ $violation_count -gt 0 ]]; then |
| 67 | + echo "❌ Found $violation_count line(s) exceeding 80 " \ |
| 68 | + "characters in PR changes" |
| 69 | + echo "" |
| 70 | + echo "Please ensure all lines are 80 characters or less " \ |
| 71 | + "as per coding standards." |
| 72 | + echo "You can check line length in your editor or use this command:" |
| 73 | + echo " grep -n '.\{81,\}' <filename>" |
| 74 | + rm -f violations.tmp changed_files.txt |
| 75 | + exit 1 |
| 76 | + else |
| 77 | + echo "✅ All changed lines are within the 80 character limit" |
| 78 | + rm -f violations.tmp changed_files.txt |
| 79 | + exit 0 |
| 80 | + fi |
0 commit comments