Search code examples
gitgithubgithub-actionspull-requestgit-workflow

Get list of files changed by GitHub Pull Request using git


As a part of PR validation, I need to detect what files have changed in PRs created from forked repositories to my main repository.

Currently, I am using this code:

- uses: actions/checkout@v3
  with:
    fetch-depth: 0
- name: Get changed files
  id: changed-files
  uses: tj-actions/changed-files@v34

But the problem is that it also returns all files modified inside my main repository that were done after PR was opened on top of just Pull Request changes as checkout is doing a merge there. More detailed description of this problem here

I am looking for the best option to get ONLY the files listed in PR "Files changes" tab. I cannot work with GitHub CLI (which will be the easiest) as I don't want to share secrets with forked repositories for that, so I have to rely only on git commands.


Solution

  • I ended up using just this for PRs:

    - name: Checkout
      uses: actions/checkout@v3
      with:
         fetch-depth: 2
    - name: Get changes
      run: git diff --name-only -r HEAD^1 HEAD | xargs
    

    Seems like it's enough, as checkout is doing a merge commit when checking out pull requests.