Von*_*onC 21
官方提交比较API是比较两个提交:
GET /repos/:owner/:repo/compare/:base...:head
Run Code Online (Sandbox Code Playgroud)
双方
:base
并:head
可以是分支名称中:在同一个网络中的其他信息库回购或分支名称:repo
.对于后一种情况,请使用以下格式user:branch
:
GET /repos/:owner/:repo/compare/user1:branchname...user2:branchname
Run Code Online (Sandbox Code Playgroud)
请注意,您也可以使用标签.
例如:
https://api.github.com/repos/git/git/compare/v2.2.0-rc1...v2.2.0-rc2
注意两个标签之间的...
',而不是' ..
.
您需要先获得最旧的标签,然后再使用较新的标签.
这给出了一个状态:
"status": "behind",
"ahead_by": 1,
"behind_by": 2,
"total_commits": 1,
Run Code Online (Sandbox Code Playgroud)
对于每次提交,有关文件的信息:
"files": [
{
"sha": "bbcd538c8e72b8c175046e27cc8f907076331401",
"filename": "file1.txt",
"status": "added",
"additions": 103,
"deletions": 21,
"changes": 124,
"blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
"raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e",
"patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
}
]
Run Code Online (Sandbox Code Playgroud)
但:
响应将包括最多250次提交的比较.如果使用较大的提交范围,则可以使用Commit List API枚举范围内的所有提交.
为了与极大的差异进行比较,您可能会收到一个错误响应,表明差异太长而无法生成.您通常可以使用较小的提交范围来解决此错误.
调查官方API附带的答案,可以找到一种几乎未提及的从Github获取差异的方法。尝试这个:
wget -H 'Accept: application/vnd.github.v3.diff' \
http://github.com/github/linguist/compare/96d29b76...a20631af.diff
wget -H 'Accept: application/vnd.github.v3.diff' \
http://github.com/github/linguist/compare/a20631af...96d29b76.diff
Run Code Online (Sandbox Code Playgroud)
这是您作为示例提供的链接, .diff
附带了。和反向diff相同。
给定的标头可确保请求由Github的v3 API处理。目前是默认设置,但将来可能会更改。请参阅媒体类型。
为什么要两次下载?
Github仅提供从旧版本到较新版本的线性差异。如果请求的差异确实是线性的并且从较旧版本到较新版本,则第二次下载将为空。
如果请求的差异是线性的,但从较新版本到较旧版本,则首次下载为空。相反,整个差异位于第二次下载中。视乎要达成的目标,通常可以将其套用到较新的版本,也可以反向套用(patch -R
)较旧的版本。
如果请求的提交对之间没有线性关系,则两次下载都将以非零内容进行回答。一个从公共锚到第一个提交,另一个从该公共锚到另一个提交。通常应用一个diff,另一个相反的diff git diff 96d29b76..a20631af
也会得到应用diff输出的结果。
据我所知,这些原始差异不受Github的API限制。540次提交和1002个文件更改的请求进行得很顺利。
注意:也可以添加.patch
而不是.diff
。然后,每个文件仍会得到一个大文件,但文件中的每个提交都会得到一组单独的补丁。
如果您使用 API 访问私有存储库,Traumflug 的答案是不正确的。实际上,我认为这个答案不需要标题,因为无论如何它在公共存储库中没有它就可以工作。
您不应将 放在.diff
网址末尾,而应使用子api
域名。如果您特别想要比较,则只需将适当的媒体类型标头放入请求中(以及用于身份验证的令牌)。
例如:
wget -H 'Accept: application/vnd.github.v3.diff' \
https://api.github.com/repos/github/linguist/compare/96d29b76...a20631af?access_token=123
Run Code Online (Sandbox Code Playgroud)
GitHub 的文档非常令人困惑,因为它说它只适用于分支名称,但它也接受提交 shas。此外,返回的 JSON 包含一个diff_url
直接链接到 diff 的链接,但如果存储库是私有的,则不起作用,这不是很有帮助。
归档时间: |
|
查看次数: |
12636 次 |
最近记录: |