From 183d6704ec2a052bf0629b88c91a7845e8461e46 Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Wed, 25 Jun 2025 22:15:22 -0400 Subject: [PATCH 1/2] Add GitHub Actions workflow to check for trailing spaces - Add check-trailing-spaces.yml workflow that runs on pull requests - Only checks lines that were added/modified in the PR using git diff -U0 - Checks Python, Markdown, JSON, YAML, and other text files - Provides clear error messages and fix instructions for changed lines - Update CONTRIBUTING guide with code quality requirements Fixes #104 --- .github/workflows/check-trailing-spaces.yml | 99 +++++++++++++++++++++ CONTRIBUTING | 15 +++- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/check-trailing-spaces.yml diff --git a/.github/workflows/check-trailing-spaces.yml b/.github/workflows/check-trailing-spaces.yml new file mode 100644 index 00000000..ca9de790 --- /dev/null +++ b/.github/workflows/check-trailing-spaces.yml @@ -0,0 +1,99 @@ +name: Check Trailing Spaces + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + check-trailing-spaces: + runs-on: ubuntu-latest + name: Check for trailing spaces + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch full history for git diff + + - name: Check for trailing spaces in changed lines + run: | + echo "Checking for trailing spaces in changed lines..." + + # Get the base branch (usually main or master) + BASE_BRANCH="${{ github.event.pull_request.base.ref }}" + echo "Base branch: $BASE_BRANCH" + + # Get the list of changed files with specific extensions + echo "Getting changed files..." + CHANGED_FILES=$(git diff -U0 --name-only origin/$BASE_BRANCH...HEAD | grep -E '\.(py|md|json|yaml|yml|txt|sh|js|ts|jsx|tsx|css|html|xml)$' || true) + + if [ -z "$CHANGED_FILES" ]; then + echo "✅ No relevant files changed in this PR" + exit 0 + fi + + echo "Changed files to check:" + echo "$CHANGED_FILES" + echo "" + + # Initialize variables + files_with_trailing_spaces="" + has_trailing_spaces=false + + # Check each changed file + for file in $CHANGED_FILES; do + if [ ! -f "$file" ]; then + echo "Skipping deleted file: $file" + continue + fi + + echo "Checking changed lines in: $file" + + # Get the diff for added/modified lines only (lines starting with +) + # and check if any of them have trailing spaces + LINES_WITH_TRAILING_SPACES=$(git diff -U0 origin/$BASE_BRANCH...HEAD "$file" | \ + grep '^+' | \ + grep -v '^+++' | \ + grep '[[:space:]]$' || true) + + if [ -n "$LINES_WITH_TRAILING_SPACES" ]; then + echo "❌ Trailing spaces found in changed lines of: $file" + files_with_trailing_spaces="${files_with_trailing_spaces}$file\n" + has_trailing_spaces=true + + # Show the problematic lines + echo " Changed lines with trailing spaces:" + echo "$LINES_WITH_TRAILING_SPACES" | head -5 + echo "" + + # Show specific line numbers in the file + echo " Line numbers in $file:" + git diff -U0 origin/$BASE_BRANCH...HEAD "$file" | \ + grep -n '^+.*[[:space:]]$' | \ + grep -v '^[0-9]*:+++ ' | \ + head -5 + echo "" + else + echo "✅ No trailing spaces in changed lines of: $file" + fi + done + + # Check if any trailing spaces were found + if [ "$has_trailing_spaces" = true ]; then + echo "::error::Trailing spaces detected in changed lines of the following files:" + echo -e "$files_with_trailing_spaces" + echo "" + echo "Please remove trailing spaces from the lines you modified." + echo "You can use the following command to remove trailing spaces:" + echo " sed -i 's/[[:space:]]*$//' " + echo "" + echo "Or to remove trailing spaces from specific files:" + for file in $CHANGED_FILES; do + if [ -f "$file" ]; then + echo " sed -i 's/[[:space:]]*$//' $file" + fi + done + exit 1 + else + echo "✅ No trailing spaces found in changed lines!" + fi diff --git a/CONTRIBUTING b/CONTRIBUTING index bc23aaed..90bc4a69 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -30,4 +30,17 @@ This project follows All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more -information on using pull requests. \ No newline at end of file +information on using pull requests. + +### Code quality checks + +All pull requests must pass automated code quality checks, including: + +- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in code files. If trailing spaces are detected, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your files before submitting: + ```bash + # Remove trailing spaces from a specific file + sed -i 's/[[:space:]]*$//' filename + + # Remove trailing spaces from all relevant files + find . -type f \( -name '*.py' -o -name '*.md' -o -name '*.json' -o -name '*.yaml' -o -name '*.yml' \) -exec sed -i 's/[[:space:]]*$//' {} + + ``` \ No newline at end of file From 0011ed00d9a145b93813bc4c3cf9e94234b72439 Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Wed, 25 Jun 2025 22:30:32 -0400 Subject: [PATCH 2/2] Update CONTRIBUTING to clarify CI only checks changed lines - Clarify that trailing spaces check only applies to changed lines using git diff -U0 - Remove suggestion to fix entire files to avoid unrelated changes - Add command to see which changed lines have trailing spaces - Add note explaining the focused approach --- CONTRIBUTING | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING b/CONTRIBUTING index 90bc4a69..72dc8ffa 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -36,11 +36,13 @@ information on using pull requests. All pull requests must pass automated code quality checks, including: -- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in code files. If trailing spaces are detected, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your files before submitting: +- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in **changed lines only** (using `git diff -U0`). If trailing spaces are detected in lines you modified, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your changes before submitting: ```bash # Remove trailing spaces from a specific file sed -i 's/[[:space:]]*$//' filename - # Remove trailing spaces from all relevant files - find . -type f \( -name '*.py' -o -name '*.md' -o -name '*.json' -o -name '*.yaml' -o -name '*.yml' \) -exec sed -i 's/[[:space:]]*$//' {} + - ``` \ No newline at end of file + # To see which lines have trailing spaces in your changes: + git diff -U0 | grep '^+' | grep -v '^+++' | grep '[[:space:]]$' + ``` + + **Note**: Our CI only checks the lines you've modified, not the entire file. This keeps your pull request focused on your changes and avoids introducing unrelated formatting changes. \ No newline at end of file