获取远程 Git 存储库中前 N 次提交的元数据

Get the metadata for the first N commits in a remote Git repository

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

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

有没有办法获得类似的元数据,但以提交的时间倒序排列,即从存储库中最旧的提交开始?

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

谢谢

使用 GraphQL API. This method is essentially the same as getting the first commit in a repo:

的变通方法是可能的

获取 和 return 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
            }
          }
        }
      }
    }
  }
}

它 return 类似于光标和 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)