|
| 1 | +# check_trailing_spaces |
| 2 | + |
| 3 | +Check files for trailing spaces, empty lines at EOF, and missing newline at EOF. |
| 4 | + |
| 5 | +## 🛠 Prep Work |
| 6 | + |
| 7 | +This action requires access to the repository's file system. For PR-based checks it also uses the GitHub API |
| 8 | +to get the list of changed files, so a valid GitHub Token is required. |
| 9 | + |
| 10 | +## 🚀 Basic Usage |
| 11 | + |
| 12 | +See [action.yml](action.yml) |
| 13 | + |
| 14 | +```yaml |
| 15 | +steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Check trailing spaces |
| 20 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 21 | +``` |
| 22 | +
|
| 23 | +## 📥 Inputs |
| 24 | +
|
| 25 | +| Name | Description | Default | Required | |
| 26 | +|------------------------------|---------------------------------------------------------------------------------------------------------------------|-------------------------------------------|----------| |
| 27 | +| token | GitHub Token used to authenticate API requests. Defaults to the built-in `GITHUB_TOKEN`. | `${{ github.token }}` | `false` | |
| 28 | +| pr_number | The pull request number to check. Defaults to the current PR number. | `${{ github.event.pull_request.number }}` | `false` | |
| 29 | +| changed_files | Newline-separated list of files to check. When provided, skips the GitHub API call. Supports filenames with spaces. | `''` | `false` | |
| 30 | +| check_all_files | Set to `"true"` to check all files in the repository instead of only changed PR files. | `'false'` | `false` | |
| 31 | +| check_empty_line_at_eof | Check that the last line in a file is not empty. | `'true'` | `false` | |
| 32 | +| check_missing_newline_at_eof | Check that the last line in the file ends in a newline character. | `'true'` | `false` | |
| 33 | +| source_directory | Directory to check when `check_all_files` is `true`. Defaults to the current directory. | `'.'` | `false` | |
| 34 | +| ignore_patterns | Newline-separated list of glob patterns for files to ignore (e.g., `*.md\n*.json`). | `''` | `false` | |
| 35 | + |
| 36 | +## 📤 Outputs |
| 37 | + |
| 38 | +This action does not produce outputs. It will fail the workflow if any checks fail, with detailed logs showing |
| 39 | +which files and lines are affected. |
| 40 | + |
| 41 | +## ✅ Checks Performed |
| 42 | + |
| 43 | +### Trailing Spaces |
| 44 | + |
| 45 | +Detects lines that end with one or more spaces or tab characters. The output will list the file path and line |
| 46 | +number for each violation. |
| 47 | + |
| 48 | +### Empty Line at EOF |
| 49 | + |
| 50 | +Detects files where the last line is blank (i.e., the file ends with two or more consecutive newlines). This is |
| 51 | +distinct from having a single trailing newline (which is correct and required). |
| 52 | + |
| 53 | +### Missing Newline at EOF |
| 54 | + |
| 55 | +Detects files where the very last character is not a newline (`\n`). Many editors and tools expect files to end |
| 56 | +with a newline character. |
| 57 | + |
| 58 | +## 📋 Example Workflows |
| 59 | + |
| 60 | +### Check PR changed files (default behavior) |
| 61 | + |
| 62 | +```yaml |
| 63 | +name: Check Trailing Spaces |
| 64 | +
|
| 65 | +on: |
| 66 | + pull_request: |
| 67 | +
|
| 68 | +jobs: |
| 69 | + check: |
| 70 | + runs-on: ubuntu-latest |
| 71 | + steps: |
| 72 | + - name: Checkout |
| 73 | + uses: actions/checkout@v4 |
| 74 | +
|
| 75 | + - name: Check trailing spaces |
| 76 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 77 | +``` |
| 78 | + |
| 79 | +### Check all files in the repository |
| 80 | + |
| 81 | +```yaml |
| 82 | +steps: |
| 83 | + - name: Checkout |
| 84 | + uses: actions/checkout@v4 |
| 85 | +
|
| 86 | + - name: Check all files for trailing spaces |
| 87 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 88 | + with: |
| 89 | + check_all_files: 'true' |
| 90 | + source_directory: '.' |
| 91 | +``` |
| 92 | + |
| 93 | +### Skip certain file types |
| 94 | + |
| 95 | +```yaml |
| 96 | +steps: |
| 97 | + - name: Checkout |
| 98 | + uses: actions/checkout@v4 |
| 99 | +
|
| 100 | + - name: Check trailing spaces (skip binary/generated files) |
| 101 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 102 | + with: |
| 103 | + check_all_files: 'true' |
| 104 | + ignore_patterns: | |
| 105 | + *.png |
| 106 | + *.jpg |
| 107 | + *.min.js |
| 108 | + *.min.css |
| 109 | +``` |
| 110 | + |
| 111 | +### Use with pre-fetched changed files |
| 112 | + |
| 113 | +```yaml |
| 114 | +steps: |
| 115 | + - name: Checkout |
| 116 | + uses: actions/checkout@v4 |
| 117 | +
|
| 118 | + - name: Get changed files |
| 119 | + id: changed_files |
| 120 | + uses: LizardByte/actions/actions/get_changed_files@master |
| 121 | +
|
| 122 | + - name: Check trailing spaces |
| 123 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 124 | + with: |
| 125 | + changed_files: ${{ steps.changed_files.outputs.changed_files }} |
| 126 | +``` |
| 127 | + |
| 128 | +### Disable specific checks |
| 129 | + |
| 130 | +```yaml |
| 131 | +steps: |
| 132 | + - name: Checkout |
| 133 | + uses: actions/checkout@v4 |
| 134 | +
|
| 135 | + - name: Check only trailing spaces (skip EOF checks) |
| 136 | + uses: LizardByte/actions/actions/check_trailing_spaces@master |
| 137 | + with: |
| 138 | + check_empty_line_at_eof: 'false' |
| 139 | + check_missing_newline_at_eof: 'false' |
| 140 | +``` |
| 141 | + |
| 142 | +## 📝 Notes |
| 143 | + |
| 144 | +- Only text files are checked. Binary files are automatically skipped. |
| 145 | +- The `.git` directory is always excluded when scanning all files. |
| 146 | +- When running on a pull request without providing `changed_files`, this action will automatically |
| 147 | + use the GitHub API to get the list of changed files. |
| 148 | +- If no PR number is available and `check_all_files` is `false`, the action falls back to checking all files. |
| 149 | + |
| 150 | +## 🔗 See Also |
| 151 | + |
| 152 | +- [get_changed_files](../get_changed_files) - Get a list of changed files in a repository. |
0 commit comments