diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 84394f26db..acc4a29d0a 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -1,6 +1,6 @@ name: Pre-commit -on: [push, pull_request] +on: [pull_request] jobs: pre-commit: @@ -24,5 +24,22 @@ jobs: python -m pip install --upgrade pip pip install pre-commit + - name: Fetch base branch + run: git fetch origin ${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }} + + - name: Fetch head commit + run: git fetch origin ${{ github.event.pull_request.head.ref }}:refs/remotes/origin/${{ github.event.pull_request.head.ref }} + + - name: Get changed files + id: changed-files + run: | + git diff --name-only origin/${{ github.event.pull_request.base.ref }} ${{ github.event.pull_request.head.sha }} > changed_files.txt + cat changed_files.txt + - name: Run pre-commit - run: pre-commit run --all-files + run: | + if [ -s changed_files.txt ]; then + pre-commit run --files $(cat changed_files.txt | tr '\n' ' ') + else + echo "No changed files to run pre-commit on." + fi