可以在两次提交之间获取已更改文件的列表。 这样的事情 网络版comparison between two commits,但使用GitHub Api。
答案 0 :(得分:19)
官方提交比较API为Compare two commits:
GET /repos/:owner/:repo/compare/:base...:head
:base
和:head
都可以是以下的分支名称:repo或与:repo
在同一网络中的其他存储库中的分支名称。对于后一种情况,请使用格式user:branch
:
GET /repos/:owner/:repo/compare/user1:branchname...user2:branchname
请注意,您也可以使用标签或提交SHA。 例如:
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,
对于每次提交,有关文件的信息:
"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"
}
]
但是:
响应将包含最多250次提交的比较。如果您正在使用更大的提交范围,则可以使用Commit List API枚举范围内的所有提交。
为了与极大的差异进行比较,您可能会收到一个错误响应,表明差异太长而无法生成。您通常可以使用较小的提交范围来解决此错误。
答案 1 :(得分:3)
Investigating answers coming with the official API, one can find a barely mentioned way to get diffs from Github. Try this:
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
This is the link you provided as an example, with .diff
appended. And the reverse diff of the same.
The header given makes sure the request is handled by the Github's v3 API. That's currently the default, but might change in the future. See Media Types.
Why two downloads?
Github serves linear diffs from older to newer versions, only. If the requested diff is indeed linear and from an older to a newer version, the second download will be empty.
If the requested diff is linear, but from a newer to an older version, the first download is empty. Instead, the whole diff is in the second download. Depending on what one want to achieve, one can normally apply it to the newer version or reverse-apply (patch -R
) it to the older version.
If there is no linear relationship between the pair of requested commits, both downloads get answered with non-zero content. One from the common anchestor to the first commit and another, reversed one from this common anchestor to the other commit. Applying one diff normally and the other one reversed gives what applying the output of git diff 96d29b76..a20631af
would give, too.
As far as I can tell, these raw diffs aren't subject to Github's API limitations. Requests for 540 commits with 1002 file changes went flawlessly.
Note: one can also append .patch
instead of .diff
. Then one still gets one big file for each, but a set of individual patches for each commit inside this file.
答案 2 :(得分:3)
这是另一个在我的公共仓库DataApp--ParamCompare上使用HEAD
和HEAD~1
引用的实际可执行示例,这将有助于阐明替换后的:owner
和:repo
表示法带有清晰的参数。
curl -X GET https://api.github.com/repos/jxramos/DataApp--ParamCompare/compare/HEAD~1...HEAD
作为健全性检查,可以在https://github.com/jxramos/DataApp--ParamCompare/compare/HEAD~1...HEAD上看到等效的浏览器表示形式
通常,该格式如下所示,以为api路由提供备用参数语法:
https://api.github.com/repos/<owner_name>/<repo_name>/compare/HEAD~1...HEAD
一个人也可以调用url,例如
https://api.github.com/repos/jxramos/DataApp--ParamCompare/compare/80f0bb42606888ce7fc66b4402fcc90a1709c9e8...255fe089543f5569f90af54168af904e88fc150f
应该有一个等效的graphql意思是缩减并在files
列表下选择那些结果,以选择所有filename
值以借出git diff --name-only
直接从远程输入输出。如果知道了,我将更新此答案。
答案 3 :(得分:1)
如果您使用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
Gitlab的文档非常令人困惑,因为它说只适用于分支名称,但它也接受commit shas。此外,返回的JSON包含diff_url
,它只是diff的直接链接,但如果repo是私有的则不起作用,这不是很有帮助。