Reputation: 1
I have to find a way to collect all the code diffs in a pull request from command line. From github UI I know I can easily view the diffs and the branch names, however, I have to automate some task from command line. I tried to use git diff branch1..branch2
to get all the modified files and code changes in the PR. But how do I figure out branch1 and branch2 from command line?
Upvotes: 0
Views: 1776
Reputation: 11595
GitHub expose some technical refs from the pull requests.
For an open PR (without conflict I think), you can fetch the refs/pull/<id>/merge
ref to get the resulting merge, and then check the diff between it and it's first parent:
# Assuming the 'origin' remote is the one containing the PR
# (and not the fork of the PR source)
git fetch origin refs/pull/123/merge
git diff FETCH_HEAD^1..FETCH_HEAD
For a closed PR, this ref is no more available, but there is still the refs/pull/<id>/head
ref. Then you have to look at the commit graph to find the merge commit (or use some magic to get the child commit):
git fetch origin refs/pull/123/head
MERGE_COMMIT=$(git log --format='%H %P' --all | grep "$(git rev-parse FETCH_HEAD)\$" | cut -f1 -d' ')
git diff FETCH_HEAD..${MERGE_COMMIT}
There is also another solution without using git: getting the diff from GitHub API (https://docs.github.com/en/rest/reference/pulls#get-a-pull-request):
curl -L -u <username> -H "Accept: application/vnd.github.v3.diff" https://api.github.com/repos/<owner>/<repo>/pulls/<id>
Upvotes: 2