Stage Changes in Pre-Commit Hook | Generated by AI

Home 2025.08

To integrate the changes from the update_notes_link.py script into the current commit (instead of creating a separate commit) during a pre-commit hook, you’ll need to modify the hook to stage the updated files without committing them separately. This way, the changes from the Python script will be included in the user’s commit. Below, I’ll provide a revised pre-commit hook script that achieves this.

Key Changes

Pre-Commit Hook Script

Here’s the updated pre-commit hook script that stages changes without creating a separate commit:

#!/bin/bash

# Run the update_notes_link.py script and capture its output
OUTPUT=$(python scripts/generate/update_notes_link.py 2>&1)
EXIT_CODE=$?

# Check if the Python script executed successfully
if [ $EXIT_CODE -ne 0 ]; then
    echo "Error: update_notes_link.py failed with exit code $EXIT_CODE"
    echo "$OUTPUT"
    exit 1
fi

# Check if the specific file was updated
if echo "$OUTPUT" | grep -q "Updated original/2025-01-11-notes-en.md"; then
    echo "Notes links updated, staging changes for the current commit."

    # Stage the relevant files
    git add original/2025-01-11-notes-en.md
    git add _posts/en/*.md

    # Verify that files were staged
    if ! git diff --cached --quiet; then
        echo "Changes staged successfully."
    else
        echo "No changes to stage."
    fi
else
    echo "No updates to original/2025-01-11-notes-en.md, no additional files staged."
fi

# Exit with success to allow the commit to proceed
exit 0

Explanation of the Script

Setting Up the Hook

  1. Create the Hook:
    • Place the script in .git/hooks/pre-commit in your repository.
  2. Make It Executable:
    chmod +x .git/hooks/pre-commit
    
  3. Test the Hook:
    • Modify a file or ensure the Python script will update original/2025-01-11-notes-en.md.
    • Run git commit -m "Your commit message".
    • Verify that the updated files are included in the commit by checking git diff --cached before committing or git show after committing.

Using the pre-commit Framework (Optional)

If you prefer using the pre-commit framework, you can define the same logic in a .pre-commit-config.yaml file. This approach is more portable and allows you to specify which files trigger the hook.

  1. Install pre-commit:
    pip install pre-commit
    
  2. Create .pre-commit-config.yaml:
repos:
- repo: local
  hooks:
  - id: update-notes-links
    name: Update Notes Links
    entry: bash -c '
      OUTPUT=$(python scripts/generate/update_notes_link.py 2>&1);
      EXIT_CODE=$?;
      if [ $EXIT_CODE -ne 0 ]; then
        echo "Error: update_notes_link.py failed with exit code $EXIT_CODE";
        echo "$OUTPUT";
        exit 1;
      fi;
      if echo "$OUTPUT" | grep -q "Updated original/2025-01-11-notes-en.md"; then
        echo "Notes links updated, staging changes for the current commit.";
        git add original/2025-01-11-notes-en.md;
        git add _posts/en/*.md;
        if ! git diff --cached --quiet; then
          echo "Changes staged successfully.";
        else
          echo "No changes to stage.";
        fi;
      else
        echo "No updates to original/2025-01-11-notes-en.md, no additional files staged.";
      fi'
    language: script
    files: ^(original/2025-01-11-notes-en\.md|_posts/en/.*\.md)$
    stages: [commit]
  1. Install the Hook:
    pre-commit install
    
  2. Test the Hook:
    • Commit changes to files matching the files regex (e.g., original/2025-01-11-notes-en.md or _posts/en/*.md).
    • Verify that the hook runs, stages changes if applicable, and includes them in your commit.

Key Differences from Original GitHub Actions

Notes

Verification

To verify:

  1. Modify a file or ensure the Python script updates original/2025-01-11-notes-en.md.
  2. Run git commit -m "Test commit".
  3. Check git show to confirm that the updated files are included in the commit with your original message.
  4. Ensure no additional commits are created (e.g., no chore(notes): Update notes links commit).

If you need further tweaks, such as handling specific error cases or adding more files to stage, let me know!


Back Donate