5

Work on a big open source repository in Github. There are more than 300 pull requests (PR) waiting the queue to be merged to master branch.

I'd like to add features in a file, before to do that, I need to make sure there are no exist PRs making the same change.

So how to find out the pull requests which include a change in a particular file?

BMW
  • 42,880
  • 12
  • 99
  • 116
  • 2
    Try Github API: [list-pull-requests](https://developer.github.com/v3/pulls/#list-pull-requests) and [list-pull-requests-files](https://developer.github.com/v3/pulls/#list-pull-requests-files), you could filter all the PRs including changes in the particular file. – Yaohui.W Mar 25 '17 at 13:49

2 Answers2

1

You could try to fetch all the PR branches in your local repository and then search for commits modifying the files.

Do achieve that, do:

  1. Add the project repository as the upstream remote.

    git remote add upstream https://github.com/[orga]/[project].git

  2. Open the .git\config file and add the line fetch = +refs/pull/*/head:refs/remotes/upstream/pr/* to the [upstream] section. It should ends up looking like this:

    [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = https://github.com/[orga]/[project].git fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

  3. Do a git fetch --all that will fetch all the remotes

  4. Search for updates on the files you want:

List all commits (across all branches) for a given file

or even better for your need...

Find a Git branch containing changes to a given file

Community
  • 1
  • 1
Philippe
  • 28,207
  • 6
  • 54
  • 78
  • Thanks, @Philippe. I follow the steps, but didn't get the expect result. Only show master branch. Where should I put step 2 in? `upstream` or `origin`? – BMW Mar 28 '17 at 01:29
  • Got it, I use this command `git log --all --format=%d $FILENAME|grep pr` to get all related PR numbers, but can't identify which one are still opened (not merged or closed). – BMW Mar 28 '17 at 01:45
  • I get it worked with your help. The next question is, after I get the PR numbers, how to show the changes from each open PRs in that file? – BMW Mar 28 '17 at 02:22
1

Get help from @Philippe and @knight9631, I got the expect result.

Prerequisite

Do upstream change as described in @Philippe's reply.

$ git remote add upstream https://github.com/[orga]/[project].git

# add fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to .git/config in session origin

$ git fetch --all

Get the related PRs which are still not merged.

Run below script.

FILENAME=$1

git log --all --format=%d $FILENAME|awk -F "[\/|\)]" '/pr/{print $3}' |sort -n |while read line
do
  state=$(curl -s https://api.github.com/repos/ansible/ansible/pulls/$line|jq -r .state)
  if [[ $state == "open" ]]; then
    echo "PR $line hasn't been merged"
  fi
done

Demo

$ bash PR.sh abc.json

PR 22857 hasn't been merged
PR 19231 hasn't been merged
PR 22981 hasn't been merged

Notes:

You need to add authorization token when getting Github API. Otherwise you will easily hit the rate-limiting

TOKEN="<your_own_token"

state=$(curl -s -H "Authorization: token $TOKEN" https://api.github.com/repos/ansible/ansible/pulls/$line|jq -r .state)
BMW
  • 42,880
  • 12
  • 99
  • 116