获取远程Git存储库

时间:2018-02-23 13:28:06

标签: git github github-api

使用以下GitHub API,可以获取存储库中提交的元数据,从最新到最旧的

排序
https://api.github.com/repos/git/git/commits

有没有办法获取类似的元数据,但是按照提交的反向时间顺序,即从存储库中最早的提交开始?

注意:我想获取此类元数据,而无需下载完整的存储库。

由于

1 个答案:

答案 0 :(得分:0)

使用GraphQL API解决方法是可能的。此方法与getting the first commit in a repo基本相同:

获取last commit并返回totalCountendCursor

{
  repository(name: "linux", owner: "torvalds") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          history(first: 1) {
            nodes {
              message
              committedDate
              authoredDate
              oid
              author {
                email
                name
              }
            }
            totalCount
            pageInfo {
              endCursor
            }
          }
        }
      }
    }
  }
}

它为光标和pageInfo对象返回类似的内容:

"totalCount": 950329,
"pageInfo": {
  "endCursor": "b961f8dc8976c091180839f4483d67b7c2ca2578 0"
}

我没有有关游标字符串格式b961f8dc8976c091180839f4483d67b7c2ca2578 0的任何资料,但是我已经在其他一些存储库中进行了超过1000次提交的测试,看来它总是像这样格式化:

<static hash> <incremented_number>

为了从第一次提交迭代到最新一次,您需要从totalCount - 1 - <number_perpage>*<page>开始,从第1页开始:

例如,为了从linux系统信息库中获取前20次提交:

{
  repository(name: "linux", owner: "torvalds") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          history(first: 20, after: "fc4f28bb3daf3265d6bc5f73b497306985bb23ab 950308") {
            nodes {
              message
              committedDate
              authoredDate
              oid
              author {
                email
                name
              }
            }
            totalCount
            pageInfo {
              endCursor
            }
          }
        }
      }
    }
  }
}

请注意,此回购中的总提交计数随时间变化,因此您需要在运行查询之前获取总计数值。

这是一个示例,它迭代了Linux存储库的前300次提交(从最早的开始):

import requests

token = "YOUR_ACCESS_TOKEN"

name = "linux"
owner = "torvalds"
branch = "master"

iteration = 3
per_page = 100
commits = []

query = """
query ($name: String!, $owner: String!, $branch: String!){
    repository(name: $name, owner: $owner) {
        ref(qualifiedName: $branch) {
            target {
                ... on Commit {
                    history(first: %s, after: %s) {
                        nodes {
                            message
                            committedDate
                            authoredDate
                            oid
                            author {
                                email
                                name
                            }
                        }
                        totalCount
                        pageInfo {
                            endCursor
                        }
                    }
                }
            }
        }
    }
}
"""

def getHistory(cursor):
    r = requests.post("https://api.github.com/graphql",
        headers = {
            "Authorization": f"Bearer {token}"
        },
        json = {
            "query": query % (per_page, cursor),
            "variables": {
                "name": name,
                "owner": owner,
                "branch": branch
            }
        })
    return r.json()["data"]["repository"]["ref"]["target"]["history"]

#in the first request, cursor is null
history = getHistory("null")
totalCount = history["totalCount"]
if (totalCount > 1):
    cursor = history["pageInfo"]["endCursor"].split(" ")
    for i in range(1, iteration + 1):
        cursor[1] = str(totalCount - 1 - i*per_page)
        history = getHistory(f"\"{' '.join(cursor)}\"")
        commits += history["nodes"][::-1]
else:
    commits = history["nodes"]

print(commits)