【问题标题】:List of branches a commit appears on提交出现的分支列表
【发布时间】:2014-07-16 22:49:46
【问题描述】:

使用 GitHub API (v3) 我想弄清楚提交出现在哪些分支上。我没有找到直接查询这个的方法,无论是通过 repo 提交还是提交数据对象。另一种解决方案是列出所有分支,并与它们的 HEAD 进行比较;我猜如果提交不在给定的分支上,比较会失败。

当前的 API 是否支持此功能,而我只是错过了它?如果没有,您有(更好的)解决方法吗?

【问题讨论】:

标签: github github-api


【解决方案1】:

这是无法直接通过 GitHub API 实现的。

解决方法 1:

  1. 获取list of all branches
  2. 对于每个分支,获取list of commits on that branch
  3. 检查提交是否在每个分支的提交列表中

解决方法 2(我认为这会起作用,但不能 100% 确定我是否错过了案例):

  1. 获取list of all branches
  2. 对于每个分支,compare the branch with the SHA:

    https://api.github.com/repos/:user/:repo/compare/:branch...:sha_of_commit

  3. 如果响应中status属性的值为divergedahead,则提交不在分支中。如果status 属性的值为behindidentical,则提交在分支中。

【讨论】:

    【解决方案2】:

    我没有检查过 GitHub API 是否直接支持这点,但使用普通 Git 可以轻松做到这一点:

    git branch --all --contains <commit>
    

    这将列出本地存储库中包含给定提交的所有分支(本地和远程)。

    【讨论】:

    • 不幸的是,如果可能的话,我需要使用 GitHub API 来执行此操作。
    【解决方案3】:

    没有直接的端点来按提交 ID 列出分支。但是您可以使用其他端点的组合来解决这个问题。

    条件 1 使用branches-where-head endpoint 获取给定提交 ID 为头部的分支的名称。

    条件 2 如果条件 1 返回空,则使用提交 ID 获取拉信息对象并使用 pulls info endpoint 提取分支信息。 提示: 返回对象包含属性 - base.ref 和 head.ref 具有分支名称。如果 base.ref 是主分支的名称,请使用 head.ref,它应该为您提供包含提交 ID 的分支的名称。

    【讨论】:

      【解决方案4】:

      如果有人使用 PyGitHub (https://pygithub.readthedocs.io/en/latest/introduction.html) 库进行 API 调用 -

      先获取所有分支的列表,然后

      g = Github(<accesskey>)
      repo = g.get_repo(<repository>)
      is_commit_in_branch = repo.compare('stable-2.11', <commit_id>).status
      # If it returns behind or identical, then the commit is in the branch.
      

      【讨论】:

        【解决方案5】:

        Git实验室 API

        跟随Ivan Zuzak's solution number 2,了解提交是否在分支上:

        使用 GitLab 的 repository compare API,并比较 from 分支,to 提交

        GET /projects/:id/repository/compare?from=<branch>&to=<sha_of_commit>
        

        如果commits 列表为空,那么是的,提交在该分支上。

        在 Python 中,使用 python-gitlab:

        def is_commit_on_branch(project, commit, branch):
            c = project.repository_compare(branch, commit)
            return not c['commits']
        

        【讨论】:

          猜你喜欢
          • 2012-05-02
          • 1970-01-01
          • 2016-07-19
          • 2013-04-24
          • 2010-11-28
          • 1970-01-01
          • 2011-01-29
          相关资源
          最近更新 更多