使用GitHub API,如何查找特定版本/标签下文件树的sha1值?

huangapple go评论58阅读模式
英文:

With the GitHub API, how do I find out the sha1 for the file tree as it existed at a given version/tag?

问题

GitHub API文档解释了如何使用以下端点检索存储库的文件树

/repos/{owner}/{repo}/git/trees/{tree_sha}

假设有一个发布的版本标签(比如,v1.0),我如何找出相应的tree_sha值,以便我可以使用API端点获取存储库在该版本的文件树?

英文:

The GitHub API documentation explains how to retrieve a repo's tree of files using the endpoint

/repos/{owner}/{repo}/git/trees/{tree_sha}

Given the version tag of a release (say, v1.0), how do I figure out the corresponding tree_sha value so that I can use the API endpoint to get the tree of files at that version of the repo?

答案1

得分: 2

你可以使用 "References" API 来查找标签(例如 "V1.0"),像这样:

$ gh api /repos/cli/cli/git/ref/tags/v2.27.0
{
  "ref": "refs/tags/v2.27.0",
  "node_id": "MDM6UmVmMjEyNjEzMDQ5OnJlZnMvdGFncy92Mi4yNy4w",
  "url": "https://api.github.com/repos/cli/cli/git/refs/tags/v2.27.0",
  "object": {
    "sha": "89caedf1817c21e4bdb9b136a46444225ec4c982",
    "type": "commit",
    "url": "https://api.github.com/repos/cli/cli/git/commits/89caedf1817c21e4bdb9b136a46444225ec4c982"
  }
}

这将为您提供提交的 SHA。您可以使用 commits API 获取有关提交的信息,这将为您提供该提交的树 SHA:

$ gh api /repos/cli/cli/git/commits/89caedf1817c21e4bdb9b136a46444225ec4c982
{
  [...]
  "tree": {
    "sha": "3d16aa8873d6b1201705fa450f14f2f43c61412f",
    "url": "https://api.github.com/repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f"
  },
  [...]
}

现在,您可以请求该树:

$ gh api /repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f
{
  "sha": "3d16aa8873d6b1201705fa450f14f2f43c61412f",
  "url": "https://api.github.com/repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f",
  "tree": [
    {
      "path": ".devcontainer",
      "mode": "040000",
      "type": "tree",
      "sha": "aa03b789b096f7e044451a9069aa11abdd6e3882",
      "url": "https://api.github.com/repos/cli/cli/git/trees/aa03b789b096f7e044451a9069aa11abdd6e3882"
    },
    [...]
  ],
  "truncated": false
}
英文:

You can look up a tag (like "V1.0") using the "References" API like this (I'm using the gh cli tool, but you can access the same APIs using your language of choice):

$ gh api /repos/cli/cli/git/ref/tags/v2.27.0
{
  "ref": "refs/tags/v2.27.0",
  "node_id": "MDM6UmVmMjEyNjEzMDQ5OnJlZnMvdGFncy92Mi4yNy4w",
  "url": "https://api.github.com/repos/cli/cli/git/refs/tags/v2.27.0",
  "object": {
    "sha": "89caedf1817c21e4bdb9b136a46444225ec4c982",
    "type": "commit",
    "url": "https://api.github.com/repos/cli/cli/git/commits/89caedf1817c21e4bdb9b136a46444225ec4c982"
  }
}

That gets you the commit sha. You can get information about the commit using the commits API, which will give you the tree sha for that commit:

$ gh api /repos/cli/cli/git/commits/89caedf1817c21e4bdb9b136a46444225ec4c982
{
  [...]
  "tree": {
    "sha": "3d16aa8873d6b1201705fa450f14f2f43c61412f",
    "url": "https://api.github.com/repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f"
  },
  [...]
}

And now you can ask for that tree:

$ gh api /repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f
{
  "sha": "3d16aa8873d6b1201705fa450f14f2f43c61412f",
  "url": "https://api.github.com/repos/cli/cli/git/trees/3d16aa8873d6b1201705fa450f14f2f43c61412f",
  "tree": [
    {
      "path": ".devcontainer",
      "mode": "040000",
      "type": "tree",
      "sha": "aa03b789b096f7e044451a9069aa11abdd6e3882",
      "url": "https://api.github.com/repos/cli/cli/git/trees/aa03b789b096f7e044451a9069aa11abdd6e3882"
    },
    [...]
  ],
  "truncated": false
}

huangapple
  • 本文由 发表于 2023年4月20日 03:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定