Can I get the diff data for one file between two commits via the GH API?

ε祈祈猫儿з 提交于 2019-12-08 01:17:59

问题


I know how to get the commit for a file via the API, along with the SHA and all that nice stuff.

But, suppose I just want the diff of a file in 1 commit or the diff of the same file across two commits.

For instance, in this commit, say I wanted just the , :counter_sql in the activerecord/lib/active_record/associations.rb.

How do I get at that diff data via the API?

I am using Octokit.rb.

Edit 1

It seems that this is possible per this blog post, but I am just not sure how to do it with Octokit.

Edit 2

So, I am kinda figuring it out little by little.

To get the diff data between two commits, I can compare two commits like this.

So, assuming I have the two SHAs for both commits in two variables a and b, I would do something like this:

client = Octokit::Client.new(access_token: ENV["MY_ACCESS_TOKEN"])
comparison = client.compare("rails/rails", a, b, path: "activerecord/lib/active_record/associations.rb")

This issue is that this results in a diff between both commits, which includes changes to many other files and a lot of information I don't want.

All I want is the diff from this specific file across these two commits.

I haven't figured out how to do that yet.

Thoughts?


回答1:


No, not possible.

You can use the Compare API to get the diff between two commits, which will include all files, not just the file you're interested in, as you observed. So you'd need to do some filtering on your end.

Another approach might be to get the contents of the file at commit X and then at commit Y, and then compute the diff between those two versions of files on your end (there's no GitHub API for diffing in general). You can use to Contents API to fetch the raw versions of files.

https://developer.github.com/v3/repos/contents/#get-contents



来源:https://stackoverflow.com/questions/38417195/can-i-get-the-diff-data-for-one-file-between-two-commits-via-the-gh-api

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