2013-11-18 19:34:13 -05:00
|
|
|
require 'digest'
|
|
|
|
require 'rubygems/util'
|
|
|
|
|
|
|
|
##
|
|
|
|
# A git gem for use in a gem dependencies file.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# source =
|
|
|
|
# Gem::Source::Git.new 'rake', 'git@example:rake.git', 'rake-10.1.0', false
|
|
|
|
#
|
|
|
|
# spec = source.load_spec 'rake'
|
|
|
|
#
|
|
|
|
# source.checkout
|
|
|
|
|
|
|
|
class Gem::Source::Git < Gem::Source
|
|
|
|
|
|
|
|
##
|
|
|
|
# The name of the gem created by this git gem.
|
|
|
|
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
##
|
|
|
|
# The commit reference used for checking out this git gem.
|
|
|
|
|
|
|
|
attr_reader :reference
|
|
|
|
|
|
|
|
##
|
|
|
|
# The git repository this gem is sourced from.
|
|
|
|
|
|
|
|
attr_reader :repository
|
|
|
|
|
2013-11-30 18:27:52 -05:00
|
|
|
##
|
|
|
|
# The directory for cache and git gem installation
|
|
|
|
|
|
|
|
attr_accessor :root_dir
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# Does this repository need submodules checked out too?
|
|
|
|
|
|
|
|
attr_reader :need_submodules
|
|
|
|
|
|
|
|
##
|
2013-11-21 18:27:30 -05:00
|
|
|
# Creates a new git gem source for a gems from loaded from +repository+ at
|
|
|
|
# the given +reference+. The +name+ is only used to track the repository
|
|
|
|
# back to a gem dependencies file, it has no real significance as a git
|
|
|
|
# repository may contain multiple gems. If +submodules+ is true, submodules
|
|
|
|
# will be checked out when the gem is installed.
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
def initialize name, repository, reference, submodules = false
|
|
|
|
super(nil)
|
|
|
|
|
|
|
|
@name = name
|
|
|
|
@repository = repository
|
|
|
|
@reference = reference
|
|
|
|
@need_submodules = submodules
|
|
|
|
|
2013-11-30 18:27:52 -05:00
|
|
|
@root_dir = Gem.dir
|
|
|
|
@git = ENV['git'] || 'git'
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def <=> other
|
|
|
|
case other
|
|
|
|
when Gem::Source::Git then
|
|
|
|
0
|
2013-11-30 18:27:52 -05:00
|
|
|
when Gem::Source::Installed,
|
|
|
|
Gem::Source::Lock then
|
2013-11-18 19:34:13 -05:00
|
|
|
-1
|
|
|
|
when Gem::Source then
|
|
|
|
1
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def == other # :nodoc:
|
|
|
|
super and
|
|
|
|
@name == other.name and
|
|
|
|
@repository == other.repository and
|
|
|
|
@reference == other.reference and
|
|
|
|
@need_submodules == other.need_submodules
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Checks out the files for the repository into the install_dir.
|
|
|
|
|
|
|
|
def checkout # :nodoc:
|
|
|
|
cache
|
|
|
|
|
|
|
|
unless File.exist? install_dir then
|
|
|
|
system @git, 'clone', '--quiet', '--no-checkout',
|
|
|
|
repo_cache_dir, install_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir install_dir do
|
|
|
|
system @git, 'fetch', '--quiet', '--force', '--tags', install_dir
|
|
|
|
|
|
|
|
success = system @git, 'reset', '--quiet', '--hard', @reference
|
|
|
|
|
|
|
|
success &&=
|
2013-11-25 14:14:49 -05:00
|
|
|
Gem::Util.silent_system @git, 'submodule', 'update',
|
|
|
|
'--quiet', '--init', '--recursive' if @need_submodules
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a local cache repository for the git gem.
|
|
|
|
|
|
|
|
def cache # :nodoc:
|
|
|
|
if File.exist? repo_cache_dir then
|
|
|
|
Dir.chdir repo_cache_dir do
|
|
|
|
system @git, 'fetch', '--quiet', '--force', '--tags',
|
|
|
|
@repository, 'refs/heads/*:refs/heads/*'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
system @git, 'clone', '--quiet', '--bare', '--no-hardlinks',
|
|
|
|
@repository, repo_cache_dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-30 18:27:52 -05:00
|
|
|
##
|
|
|
|
# Directory where git gems get unpacked and so-forth.
|
|
|
|
|
|
|
|
def base_dir # :nodoc:
|
|
|
|
File.join @root_dir, 'bundler'
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# A short reference for use in git gem directories
|
|
|
|
|
|
|
|
def dir_shortref # :nodoc:
|
|
|
|
rev_parse[0..11]
|
|
|
|
end
|
|
|
|
|
2013-11-22 13:53:21 -05:00
|
|
|
##
|
|
|
|
# Nothing to download for git gems
|
|
|
|
|
|
|
|
def download full_spec, path # :nodoc:
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# The directory where the git gem will be installed.
|
|
|
|
|
|
|
|
def install_dir # :nodoc:
|
2013-11-30 18:27:52 -05:00
|
|
|
File.join base_dir, 'gems', "#{@name}-#{dir_shortref}"
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The directory where the git gem's repository will be cached.
|
|
|
|
|
|
|
|
def repo_cache_dir # :nodoc:
|
2013-11-30 18:27:52 -05:00
|
|
|
File.join @root_dir, 'cache', 'bundler', 'git', "#{@name}-#{uri_hash}"
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Converts the git reference for the repository into a commit hash.
|
|
|
|
|
|
|
|
def rev_parse # :nodoc:
|
|
|
|
Dir.chdir repo_cache_dir do
|
|
|
|
Gem::Util.popen(@git, 'rev-parse', @reference).strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Loads all gemspecs in the repository
|
|
|
|
|
|
|
|
def specs
|
|
|
|
checkout
|
|
|
|
|
|
|
|
Dir.chdir install_dir do
|
|
|
|
Dir['{,*,*/*}.gemspec'].map do |spec_file|
|
|
|
|
directory = File.dirname spec_file
|
|
|
|
file = File.basename spec_file
|
|
|
|
|
|
|
|
Dir.chdir directory do
|
2013-11-25 14:14:49 -05:00
|
|
|
spec = Gem::Specification.load file
|
2013-11-30 18:27:52 -05:00
|
|
|
if spec then
|
|
|
|
loaded_from = File.expand_path file
|
|
|
|
spec.loaded_from = loaded_from
|
|
|
|
spec.base_dir = base_dir
|
|
|
|
|
|
|
|
spec.extension_install_dir =
|
|
|
|
File.join base_dir, 'extensions', Gem::Platform.local.to_s,
|
|
|
|
Gem.extension_api_version, "#{name}-#{dir_shortref}"
|
|
|
|
|
|
|
|
spec.full_gem_path = File.dirname loaded_from if spec
|
|
|
|
end
|
2013-11-25 14:14:49 -05:00
|
|
|
spec
|
2013-11-21 18:27:30 -05:00
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# A hash for the git gem based on the git repository URI.
|
|
|
|
|
|
|
|
def uri_hash # :nodoc:
|
|
|
|
normalized =
|
|
|
|
if @repository =~ %r%^\w+://(\w+@)?% then
|
|
|
|
uri = URI(@repository).normalize.to_s.sub %r%/$%,''
|
|
|
|
uri.sub(/\A(\w+)/) { $1.downcase }
|
|
|
|
else
|
|
|
|
@repository
|
|
|
|
end
|
|
|
|
|
|
|
|
Digest::SHA1.hexdigest normalized
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|