git: e0231d3cd363 - main - github: optimize style workflow

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 06 Dec 2024 01:08:34 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=e0231d3cd363bb33142270315aa817977b632910

commit e0231d3cd363bb33142270315aa817977b632910
Author:     Ahmad Khalifa <ahmadkhalifa570@gmail.com>
AuthorDate: 2024-12-01 13:42:53 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-12-06 00:59:51 +0000

    github: optimize style workflow
    
    Only fetch the commits we need instead of fetching the entire history.
    
    Unfortunately there doesn't seem to be a way to add 1 to the number of
    commits without an extra step, so do it in a new step and pass the
    information onto $GITHUB_ENV so it can be used later.
    
    Signed-off-by: Ahmad Khalifa <ahmadkhalifa570@gmail.com>
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1538
---
 .github/workflows/style.yml | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml
index 08d4242df5ed..0e7c1d8ca6d8 100644
--- a/.github/workflows/style.yml
+++ b/.github/workflows/style.yml
@@ -1,10 +1,6 @@
 name: Style Checker
 
-# Runs my simple style(9) checker on any pushes or pull requests.  It could be
-# optimized by fetching the pull request head branch back to main revisions and
-# running on that. That would reduce the run time from 3-4 minutes down to 30-40
-# seconds. Getting the right series of clone + fetches to get that iteratively
-# is proving elusive, so optimizations welcome.
+# Runs my simple style(9) checker on pull requests.
 
 on:
   pull_request: # maybe pull_request_target
@@ -19,10 +15,15 @@ jobs:
     name: Style Checker
     runs-on: ubuntu-latest
     steps:
+      # Unfortunately there doesn't seem to be a way to
+      # do this without an extra step.
+      - name: Get depth
+        run: |
+          echo "DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV
       - name: checkout
         uses: actions/checkout@v4
         with:
-          fetch-depth: 0
+          fetch-depth: ${{ env.DEPTH }}
           ref: ${{ github.event.pull_request.head.sha }}
       - name: Install packages
         run: |
@@ -30,5 +31,5 @@ jobs:
           sudo apt-get -yq --no-install-suggests --no-install-recommends install perl
       - name: Run checker
         run: |
-          sha=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
+          sha=$(git rev-parse HEAD~${{ github.event.pull_request.commits }})
           tools/build/checkstyle9.pl --github ${sha}..${{ github.event.pull_request.head.sha }}