Gemfile 中新块 "git_source(:github)" 的含义

Meaning of new block "git_source(:github)" in Gemfile

最近我创建了一个新的 Rails 5 应用程序,没有 git 存储库。自动生成的 Gemfile 包含一个我以前从未见过的新块:

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

这是什么意思?是否每个新应用都必须这样做?

它是 Bundler 中一个错误的解决方法,该错误可能导致 github 的源通过 HTTP 而不是 HTTPS 加载 - 这使得它容易受到中间人攻击。

git_source 添加一个您可以使用的源,以便从 git 存储库下载 gem 而不是 rubygems.org.[=16= 的包]

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

会做到这样当你声明时:

gem 'foo_bar', :github => 'foo/bar'

Bundler 会尝试从 https://github.com/foo/bar.git 下载 gem。

因为 fixing this would be a breaking change 因为它会使任何现有的 Gemfile.lock 无效,所以它已在 Bundler 2.x 中修复。届时删除此解决方法应该是安全的。

Bundler :github 指令将从 git://github.com/#{repo_name}.git (source) 获取,它使用不安全的 http 协议。

这将在未来的 Bundler 版本中修复,但此片段已添加到 Gemfile 的顶部以确保 https 在 Bundler 1 中使用。

如果您不想将此代码添加到您的 gem 文件,但仍想从 github 安全地访问 gem,您可以使用以下方法:

gem 'foo_bar', git: 'https://github.com/foo/bar.git