github apit获取与特定文件相关的提交信息 [英] github apit get commit info related to specific file
问题描述
如何获取包含特定文件的最新提交?我目前正在像这样从仓库中检索特定目录中的所有文件
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"
}
}
我考虑过使用sha并以此为基础进行提交,但不是最近一次提交的sha
I thought of using the sha and pulling in a commit based on that but its not the sha for the most recent commit
推荐答案
而不是/contents
,您需要 /commits
端点.这将为您提供对存储库所做的提交的列表(就像git log
一样),并接受path
参数,该参数将响应限制为对接触一个文件的提交的响应.如果还设置了per_page
参数,则可以进一步将其限制为仅最近的提交.例如,
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).
这篇关于github apit获取与特定文件相关的提交信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!