ci: amend single commits when auto-formatting queue-main branches

Modify the formatting workflow to check the number of commits since
the merge-base with origin/main. If there's exactly one commit
since the merge-base, amend that commit instead of adding a new
formatting commit. For multiple commits, continue to add a new
commit as before.

Co-Authored-By: sketch <hello@sketch.dev>
diff --git a/.github/workflows/formatting.yml b/.github/workflows/formatting.yml
index 2920665..f995456 100644
--- a/.github/workflows/formatting.yml
+++ b/.github/workflows/formatting.yml
@@ -76,11 +76,31 @@
           git config --global user.name "Autoformatter"
           git config --global user.email "bot@sketch.dev"
           git add .
-          git commit -m "all: fix formatting"
-
-          # If this is a queue-main branch, push the changes
+          
+          # If this is a queue-main branch, check commit count and decide whether to amend
           if [[ "${{ github.ref }}" == refs/heads/queue-main-* ]]; then
+            # Ensure we have origin/main available
+            git fetch origin main
+            
+            # Find merge-base with origin/main
+            MERGE_BASE=$(git merge-base HEAD origin/main)
+            
+            # Count commits since merge-base
+            COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..HEAD)
+            
+            if [ "$COMMIT_COUNT" -eq 1 ]; then
+              echo "Found exactly one commit since merge-base with origin/main. Amending the commit."
+              git commit --amend --no-edit
+            else
+              echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit."
+              git commit -m "all: fix formatting"
+            fi
+            
+            # Push the changes
             git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/boldsoftware/sketch.git HEAD
+          else
+            # For non-queue-main branches, simply add a new commit
+            git commit -m "all: fix formatting"
           fi
         
       - name: Get commit SHA