github apit获取与特定文件相关的提交信息

时间:2015-11-28 22:51:20

标签: github github-api

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

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

1 个答案:

答案 0 :(得分:0)

而不是/contents,您需要/commits端点。这将为您提供对存储库的提交列表(就像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​​链接对于当前的状态)。