gpt4 book ai didi

github - 通过 API 在 GitHub 存储库中查找最旧的提交

转载 作者:行者123 更新时间:2023-12-03 14:19:27 24 4
gpt4 key购买 nike

确定 GitHub 存储库中的初始提交何时进行的最有效方法是什么?存储库有一个 created_at属性,但对于包含导入历史的存储库,最旧的提交可能要早得多。

使用命令行时,类似这样的事情会起作用:
git rev-list --max-parents=0 HEAD
但是我在 GitHub API 中没有看到等效项。

最佳答案

使用 GraphQL API ,有一种解决方法可以在特定分支中获取最旧的提交(初始提交)。
先拿到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": 931886,
"pageInfo": {
"endCursor": "b961f8dc8976c091180839f4483d67b7c2ca2578 0"
}
我没有关于光标字符串格式的任何来源 b961f8dc8976c091180839f4483d67b7c2ca2578 0但我已经用其他一些存储库进行了超过 1000 次提交的测试,它似乎总是格式化为:
<static hash> <incremented_number>
所以你只需从 totalCount 中减去 2 (如果 totalCount 是 > 1)并获取最旧的提交(或初始提交,如果您愿意):
{
repository(name: "linux", owner: "torvalds") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 1, after: "b961f8dc8976c091180839f4483d67b7c2ca2578 931884") {
nodes {
message
committedDate
authoredDate
oid
author {
email
name
}
}
totalCount
pageInfo {
endCursor
}
}
}
}
}
}
}
它给出了以下输出(Linus Torvalds 的初始提交):
{
"data": {
"repository": {
"ref": {
"target": {
"history": {
"nodes": [
{
"message": "Linux-2.6.12-rc2\n\nInitial git repository build. I'm not bothering with the full history,\neven though we have it. We can create a separate \"historical\" git\narchive of that later if we want to, and in the meantime it's about\n3.2GB when imported into git - space that would just make the early\ngit days unnecessarily complicated, when we don't have a lot of good\ninfrastructure for it.\n\nLet it rip!",
"committedDate": "2005-04-16T22:20:36Z",
"authoredDate": "2005-04-16T22:20:36Z",
"oid": "1da177e4c3f41524e886b7f1b8a0c1fc7321cac2",
"author": {
"email": "torvalds@ppc970.osdl.org",
"name": "Linus Torvalds"
}
}
],
"totalCount": 931886,
"pageInfo": {
"endCursor": "b961f8dc8976c091180839f4483d67b7c2ca2578 931885"
}
}
}
}
}
}
}
中的一个简单实现使用此方法获取第一次提交:
import requests

token = "YOUR_TOKEN"

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

query = """
query ($name: String!, $owner: String!, $branch: String!){
repository(name: $name, owner: $owner) {
ref(qualifiedName: $branch) {
target {
... on Commit {
history(first: 1, 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 % 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(" ")
cursor[1] = str(totalCount - 2)
history = getHistory(f"\"{' '.join(cursor)}\"")
print(history["nodes"][0])
else:
print("got oldest commit (initial commit)")
print(history["nodes"][0])
您可以在 中找到示例在 this post

关于github - 通过 API 在 GitHub 存储库中查找最旧的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25112141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com