使用GitHub API,我如何找到文件树的sha1,因为它存在于给定的版本/标签中?

dkqlctbz  于 2023-04-28  发布在  Git
关注(0)|答案(1)|浏览(77)

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

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

给定一个版本的版本标签(比如v1.0),我如何计算出相应的tree_sha值,以便我可以使用API端点来获取该版本的repo文件树?

gywdnpxw

gywdnpxw1#

您可以查找一个标记(如“V1.0”)使用“References”API,如下所示(我使用的是gh cli工具,但您可以使用您选择的语言访问相同的API):

$ 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"
  }
}

这让你得到了承诺。你可以使用commits API获取提交的信息,它会给予你该提交的树结构:

$ 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
}

相关问题