使用Github API获取文件diff



. net项目,我需要检测和解析对不同拉取请求之间的存储库中的特定单个文本文件所做的更改。

我已经成功地访问了拉请求和使用Github API提交,但我不知道如何检索在上次提交中更改的行?

是否可以使用API?最好的方法是什么?如果不是,我应该尝试阅读最后两个文件版本,并在本地实现不同的算法?谢谢!

拉取请求包含一个diff_url条目,如

"diff_url": "https://github.com/octocat/Hello-World/pull/1347.diff"

您可以为任何提交执行此操作。例如,要获取commit 7fd1a60b01f91b3在octocat的Hello-World上的差值,请输入https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.diff。

这也适用于分支。这是大师在八爪猫的Hello-World。https://github.com/octocat/Hello-World/commit/master.diff。

一般形式为:

https://github.com/<owner>/<repo>/commit/<commit>.diff

对于私有存储库:

curl -H "Accept: application/vnd.github.v3.diff" https://<personal access token>:x-oauth-basic@api.github.com/repos/<org>/<repo>/pulls/<pull request>

也适用于正常的cURL -u参数

见:https://docs.github.com/en/rest/reference/pulls get-a-pull-request

症结在请求的媒体类型中。您可以传递值为

Accept标头

application/vnd.github.diff

见文档。作为完整的参考,上面的GET请求和授权头到https://api.github.com/repos/{orgName}/{repoName}/pulls/{prId}就可以了。

最新更新