我有两个提交。这个和这个。我正在使用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=>
"cmVxdWlyZSAnYWN0aXZlX3N1cHBvcnQvY29yZV9leHQvZW51bWVyYWJsZScKncmVxdWlyZSAnYWN0aXZlX3N1cHBvcnQvY29yZV9leHQvc3RyaW5nL2NvbnZlncnNpb25zJwpyZXF1aXJlICdhY3RpdmVfc3VwcG9ydC9jb3JlX2V4dC9tb2R1nbGUvcmVtb3ZlX21ldGhvZCcKcmVxdWlyZSAnYWN0aXZlX3JlY29yZC9lcnJvncnMnCgptb2R1bGUgQWN0aXZlUmVjb3JkCiAgY2xhc3MgQXNzb2NpYXRpb25Onb3RGb3VuZEVycm9yIDwgQ29uZmlndXJhdGlvbkVycm9yICM6bm9kb2M6CiAgnICBkZWYgaW5pdGlhbGl6ZShyZWNvcmQgPSBua
为了简洁起见,我截断了content
。
为什么会这样?如果您手动查看我在顶部发布的两个链接,并向下滚动到associations.rb
代码,您将看到两者都对该文件进行了更改。
那么为什么两个提交的content
是相同的呢?
太困惑了:|
编辑 1
为了回应 Tim 的评论,也许提交是精心挑选的并且它们确实相同,我又做了两次提交(这个和这个),但结果似乎是一样的:
[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"
GitHub API 文档建议您必须使用 ref
选项引用所需的提交/分支/标记,而您使用的是 sha
.
因此,正确的代码将是这样的:
c1 = client.contents(repo, path: path, ref: commits2.first.sha)
c2 = client.contents(repo, path: path, ref: commits2.second.sha)