Error using Github actions with git clang-format

40 Views Asked by At

Whenever a pull request to main is created, I want an action to check that the code follows our format guidelines for the repo.

Unfortunately im having trouble integrating this since the command keeps returning errors.


on:
  pull_request:
    branches:
      - main

jobs:
  clang_format_check:
    name: Check Clang Format
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        
      - name: Set up clang-format
        run: sudo apt-get install clang-format
        
      - name: Get commit ID of main branch
        run: |
          MAIN_COMMIT=$(git rev-parse main)
          echo "Commit ID of main branch: $MAIN_COMMIT"
          
      - name: Check code formatting
        run: |
          git clang-format --style=firmware/.clang-format --diff $MAIN_COMMIT..HEAD '*.cpp' '*.h'
          
      - name: Check for changes
        id: check_changes
        run: |
          git diff --exit-code || (echo "Code is not formatted properly. Please run clang-format and commit the changes." && exit 1)
          
      - name: Complete
        if: steps.check_changes.outcome == 'success'
        run: echo "Code is properly formatted." 

On my windows pc, git rev-parse main works fine but on github action I keep getting errors:

fatal: ambiguous argument 'main': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
0

There are 0 best solutions below