问题
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