【问题标题】:github apit get commit info related to specific filegithub apit 获取与特定文件相关的提交信息
【发布时间】:2016-03-02 20:01:53
【问题描述】:

如何获取包含特定文件的最新提交?我目前正在从我的仓库中的特定目录中检索所有文件,就像这样

https://api.github.com/repos/' + this.fullRepoUrl + '/contents' + this.path

返回一系列看起来像这样的对象

{
    "name": "preview-whats-to-come.md",
    "path": "posts/preview-whats-to-come.md",
    "sha": "08bf61b7b1a8895cd1415f93f40315f4c5ef8bf9",
    "size": 1861,
    "url":       "https://api.github.com/repos/user/repo/contents/posts/preview-whats-to-come.md?ref=master",
    "html_url": "https://github.com/user/repo/blob/master/posts/preview-whats-to-come.md",
    "git_url": "https://api.github.com/repos/user/repo/git/blobs/08bf61b7b1a8895cd1415f93f40315f4c5ef8bf9",
    "download_url": "https://raw.githubusercontent.com/user/repo/master/posts/preview-whats-to-come.md",
    "type": "file",
    "_links": {
        "self": "https://api.github.com/repos/user/repo/contents/posts/preview-whats-to-come.md?ref=master",
        "git": "https://api.github.com/repos/user/repo/git/blobs/08bf61b7b1a8895cd1415f93f40315f4c5ef8bf9",
        "html": "https://github.com/user/repo/blob/master/posts/preview-whats-to-come.md"
}

}

我考虑过使用 sha 并基于它拉入一个提交,但它不是最近提交的 sha

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    您需要/commits 端点,而不是/contents。这为您提供了对存储库的提交列表(就像git log),并接受一个path 参数,该参数将响应限制为触及一个文件的提交。如果您还设置了per_page 参数,则可以进一步将其限制为仅最近一次提交。例如,

    https://api.github.com/repos/octokit/octokit.rb/commits?path=README.md&per_page=1

    [
      {
        "sha": "658915fa87e88ac11cd6211fcceca3df49eb650f",
        "commit": {
          "message": "Added a link to the releases page in the readme\n\nOctokit uses Github releases to document changes in each release, rather than a changelog file. To avoid confusion, I've added a link to the releases page under the \"Versioning\" section.\n\nThis fixes #639",
          "tree": {
            "sha": "5721d4c257226c77799b232ae5293fd1a0d77aaa",
            "url": "https://api.github.com/repos/octokit/octokit.rb/git/trees/5721d4c257226c77799b232ae5293fd1a0d77aaa"
          },
          "url": "https://api.github.com/repos/octokit/octokit.rb/git/commits/658915fa87e88ac11cd6211fcceca3df49eb650f",
          "comment_count": 0
        },
        "parents": [
          {
            "sha": "615f96a7c06c32e76e1768d29ef0b40ec53da57d",
            "url": "https://api.github.com/repos/octokit/octokit.rb/commits/615f96a7c06c32e76e1768d29ef0b40ec53da57d",
            "html_url": "https://github.com/octokit/octokit.rb/commit/615f96a7c06c32e76e1768d29ef0b40ec53da57d"
          }
        ]
        ...snip...
      }
    ]
    

    顶级 sha 字段 (658915f) 是提交哈希,与 GitHub 上 the README.md page 顶部的可见内容相匹配(粘贴的可能已过期 - 尝试当前状态的 API 链接)。

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 2020-12-16
      • 2013-08-10
      • 2011-09-01
      • 1970-01-01
      • 2019-12-06
      • 2015-09-08
      • 1970-01-01
      • 2016-08-30
      相关资源
      最近更新 更多