github apit get commit info related to specific file

 ̄綄美尐妖づ 提交于 2019-12-08 11:44:39

问题


how do I get the most recent commit containing a specific file? I am currently retrieving all the files from a specific directory in my repo like so

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

which returns a series of objects that look like this

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

}

I thought of using the sha and pulling in a commit based on that but its not the sha for the most recent commit


回答1:


Instead of /contents, you want the /commits endpoint. That gives you a list of commits made to the repository (just like git log), and accepts a path parameter that limits the response to commits touching one file. If you also set the per_page parameter, you can further limit it to only the most recent commit. For example,

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...
  }
]

The top-level sha field (658915f) is the commit hash, and matches what's visible at the top of the README.md page on GitHub (the pasted one may be out of date - try the API link for the current state).



来源:https://stackoverflow.com/questions/33977354/github-apit-get-commit-info-related-to-specific-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!