0

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>...]'
8
  • Can you have a look at this action step where you don't need to install it as linux command github.com/marketplace/actions/… Commented Mar 23, 2024 at 7:14
  • git rev-parse main works fine but on github action I keep getting errors You don't seem to push the branch main yet. Commented Mar 23, 2024 at 16:31
  • @273K Thank you for your response. I am a total beginner when it comes to github actions. I am not sure what you mean by push the branch main yet. The main branch is our default branch on github. I am trying to get it to run git clang-format to only update between the current branch's commit & main (where we are trying to merge to) Commented Mar 23, 2024 at 16:40
  • The error is clearly saying there is no branch name in the remote repo. Commented Mar 23, 2024 at 16:51
  • to run git clang-format to only update between the current branch's commit & main This is also your mistake. PRs can merge to any branch, not only main. Commented Mar 23, 2024 at 16:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.