我有两个提交。 This one和this one。我正在使用Octokit。
我主要对两次提交中与文件activerecord/lib/active_record/associations.rb
相关的差异感兴趣。
我在集合中有这两个提交,你可以在这里看到:
[284] pry(main)> commits2.first.sha
=> "c6e01f5b60c4864f0e92149d1e81077519f503d5"
[285] pry(main)> commits2.second.sha
=> "581f12b7b18a6e5205bfabb814f6e9997bf92be8"
请注意,这两个SHA对应于我在上面发布的链接中的SHA,并且两者都是唯一的(即彼此不同)。
然而,当我获得每个提交的内容,特别是与我感兴趣的路径相关时,我得到两个提交的相同结果。
pry(main)> repo
=> "rails/rails"
pry(main)> path
=> "activerecord/lib/active_record/associations.rb"
c1 = client.contents(repo, path: path, sha: commits2.first.sha)
pry(main)> c1.sha
=> "3729e22e6423348aa9282cd17b49c09793ca3a6f"
然后我为第二次提交做同样的事情,我得到以下内容:
pry(main)> c2 = client.contents(repo, path: path, sha: commits2.second.sha)
pry(main)> c2.sha
=> "3729e22e6423348aa9282cd17b49c09793ca3a6f"
注意SHA是相同的。
c1
和c2
的内容也相同。你可以在下面看到它们:
pry(main)> c1
=> {:name=>"associations.rb",
:path=>"activerecord/lib/active_record/associations.rb",
:sha=>"3729e22e6423348aa9282cd17b49c09793ca3a6f",
:size=>94931,
:url=>
"https://api.github.com/repos/rails/rails/contents/activerecord/lib/active_record/associations.rb?ref=master",
:html_url=>
"https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb",
:git_url=>
"https://api.github.com/repos/rails/rails/git/blobs/3729e22e6423348aa9282cd17b49c09793ca3a6f",
:download_url=>
"https://raw.githubusercontent.com/rails/rails/master/activerecord/lib/active_record/associations.rb",
:type=>"file",
:content=>
"cmVxdWlyZSAnYWN0aXZlX3N1cHBvcnQvY29yZV9leHQvZW51bWVyYWJsZScK\ncmVxdWlyZSAnYWN0aXZlX3N1cHBvcnQvY29yZV9leHQvc3RyaW5nL2NvbnZl\ncnNpb25zJwpyZXF1aXJlICdhY3RpdmVfc3VwcG9ydC9jb3JlX2V4dC9tb2R1\nbGUvcmVtb3ZlX21ldGhvZCcKcmVxdWlyZSAnYWN0aXZlX3JlY29yZC9lcnJv\ncnMnCgptb2R1bGUgQWN0aXZlUmVjb3JkCiAgY2xhc3MgQXNzb2NpYXRpb25O\nb3RGb3VuZEVycm9yIDwgQ29uZmlndXJhdGlvbkVycm9yICM6bm9kb2M6CiAg\nICBkZWYgaW5pdGlhbGl6ZShyZWNvcmQgPSBua
我为了简洁而截断了content
。
为什么会这样?如果您手动查看我在顶部发布的两个链接,并向下滚动到associations.rb
代码,您将看到两个链接都对该文件进行了更改。
那么为什么BOTH提交的content
是相同的呢?
如此困惑:|
修改1
为了回应Tim的评论,也许这些提交被挑选出来并且它们确实相同,我已经做了两次提交(this one& this one),但结果似乎是一样的:
[299] pry(main)> commits2.third.html_url
=> "https://github.com/rails/rails/commit/1d2c6ee8f3f7e4b6881f30e6b53582738c2b3ace"
[300] pry(main)> commits2.third.sha
=> "1d2c6ee8f3f7e4b6881f30e6b53582738c2b3ace"
[301] pry(main)> commits2.fourth.html_url
=> "https://github.com/rails/rails/commit/3c01e01ded4e8e87d48e844881c88f6e47cebdf0"
[302] pry(main)> commits2.fourth.sha
=> "3c01e01ded4e8e87d48e844881c88f6e47cebdf0"
pry(main)> c3 = client.contents(repo, path: path, sha: commits2.third.sha)
pry(main)> c3.sha
=> "3729e22e6423348aa9282cd17b49c09793ca3a6f"
pry(main)> c4 = client.contents(repo, path: path, sha: commits2.fourth.sha)
pry(main)> c4.sha
=> "3729e22e6423348aa9282cd17b49c09793ca3a6f"
答案 0 :(得分:2)
GitHub API Documentation建议您使用ref
选项引用所需的提交/分支/代码,而使用sha
。
因此正确的代码是:
c1 = client.contents(repo, path: path, ref: commits2.first.sha)
c2 = client.contents(repo, path: path, ref: commits2.second.sha)