【问题标题】:How to find out the pull requests which include a change in a particular file?如何找出包含特定文件更改的拉取请求?
【发布时间】:2017-03-28 11:29:00
【问题描述】:

在 Github 上开发一个大型开源存储库。有超过 300 个拉取请求 (PR) 等待队列合并到主分支。

我想在文件中添加功能,在此之前,我需要确保不存在进行相同更改的 PR。

那么如何找出包含特定文件更改的拉取请求?

【问题讨论】:

标签: git github


【解决方案1】:

您可以尝试获取本地存储库中的所有 PR 分支,然后搜索修改文件的提交。

做到这一点,做到:

  1. 将项目存储库添加为upstream 远程。

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

  2. 打开.git\config 文件并将fetch = +refs/pull/*/head:refs/remotes/upstream/pr/* 行添加到[upstream] 部分。它最终应该是这样的:

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

  3. 执行git fetch --all 将获取所有遥控器

  4. 搜索所需文件的更新:

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

甚至更好地满足您的需求...

Find a Git branch containing changes to a given file

【讨论】:

  • 谢谢,@Philippe。我按照步骤操作,但没有得到预期的结果。只显示主分支。我应该把第2步放在哪里? upstream 或 origin?
  • 知道了,我使用这个命令git log --all --format=%d $FILENAME|grep pr 来获取所有相关的PR 编号,但无法确定哪些仍然打开(未合并或关闭)。
  • 在您的帮助下我得到了它。下一个问题是,在我获得 PR 编号后,如何显示该文件中每个打开的 PR 的更改?
【解决方案2】:

从@Philippe 和@knight9631 获得帮助,我得到了预期的结果。

先决条件

请按照@Philippe 的回复中所述更改upstream。

$ 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

获取尚未合并的相关PR。

运行下面的脚本。

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

演示

$ bash PR.sh abc.json

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

注意事项:

获取 Github API 时需要添加授权令牌。否则你很容易打到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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2014-10-30
    • 2012-09-22
    • 2015-10-19
    • 2020-06-09
    • 2015-04-26
    • 2019-03-07
    相关资源
    最近更新 更多