2019-10-06 15:45:34 +00:00
|
|
|
module Bundler
|
|
|
|
class Source
|
|
|
|
class Git < Path
|
|
|
|
class GitNotInstalledError < GitError
|
|
|
|
def initialize
|
|
|
|
msg = "You need to install git to be able to use gems from git repositories. "
|
|
|
|
msg << "For help installing git, please refer to GitHub's tutorial at https://help.github.com/articles/set-up-git"
|
|
|
|
super msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GitNotAllowedError < GitError
|
|
|
|
def initialize(command)
|
|
|
|
msg = "Bundler is trying to run a `git #{command}` at runtime. You probably need to run `bundle install`. However, "
|
|
|
|
msg << "this error message could probably be more useful. Please submit a ticket at http://github.com/bundler/bundler/issues "
|
|
|
|
msg << "with steps to reproduce as well as the following\n\nCALLER: #{caller.join("\n")}"
|
|
|
|
super msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GitCommandError < GitError
|
|
|
|
def initialize(command, path = nil)
|
|
|
|
msg = "Git error: command `git #{command}` in directory #{Dir.pwd} has failed."
|
|
|
|
msg << "\nIf this error persists you could try removing the cache directory '#{path}'" if path && path.exist?
|
|
|
|
super msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The GitProxy is responsible to interact with git repositories.
|
|
|
|
# All actions required by the Git source is encapsulated in this
|
|
|
|
# object.
|
|
|
|
class GitProxy
|
|
|
|
attr_accessor :path, :uri, :ref
|
|
|
|
attr_writer :revision
|
|
|
|
|
|
|
|
def initialize(path, uri, ref, revision = nil, git = nil)
|
2019-10-06 15:52:01 +00:00
|
|
|
@path = path
|
|
|
|
@uri = uri
|
|
|
|
@ref = ref
|
2019-10-06 15:45:34 +00:00
|
|
|
@revision = revision
|
|
|
|
@git = git
|
|
|
|
raise GitNotInstalledError.new if allow? && !Bundler.git_present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def revision
|
|
|
|
@revision ||= allowed_in_path { git("rev-parse #{ref}").strip }
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch
|
|
|
|
@branch ||= allowed_in_path do
|
|
|
|
git("branch") =~ /^\* (.*)$/ && $1.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def contains?(commit)
|
|
|
|
allowed_in_path do
|
|
|
|
result = git_null("branch --contains #{commit}")
|
|
|
|
$? == 0 && result =~ /^\* (.*)$/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def checkout
|
2019-10-06 15:52:01 +00:00
|
|
|
@org_pwd = Dir.pwd
|
2019-10-06 15:45:34 +00:00
|
|
|
if path.exist?
|
2019-10-06 15:52:01 +00:00
|
|
|
#return if has_revision_cached?
|
|
|
|
@EXT_LOCK
|
|
|
|
if defined?(Gem::Ext::Builder::CHDIR_MONITOR)
|
|
|
|
@EXT_LOCK = Gem::Ext::Builder::CHDIR_MONITOR
|
|
|
|
else
|
|
|
|
require "monitor"
|
|
|
|
@EXT_LOCK = Monitor.new
|
|
|
|
end
|
|
|
|
@EXT_LOCK.synchronize do
|
|
|
|
@old_pwd = Dir.pwd
|
|
|
|
Bundler.ui.confirm "Updating #{uri}"
|
|
|
|
FileUtils.cd(path)
|
|
|
|
git_retry %|fetch --force --quiet --tags #{uri_escaped} "refs/heads/*:refs/heads/*"|
|
2019-10-08 13:47:35 +00:00
|
|
|
if path.to_s.include?("cache")
|
|
|
|
@commit_num = `git log -1 --pretty=format:"%H"`[0..11] if revision.nil?
|
|
|
|
@commit_num = revision[0..11] if !revision.nil?
|
|
|
|
@new_path = path.dirname
|
|
|
|
@old_folder_name = path.basename.to_s + '/'
|
|
|
|
@fold_name =''
|
|
|
|
@fold_name = path.basename.to_s
|
|
|
|
@fold_name = @fold_name.split('-')[0..-2].join('-')
|
|
|
|
@new_path += (@fold_name + '-' + @commit_num)
|
|
|
|
path = @new_path
|
|
|
|
FileUtils.cd('..')
|
|
|
|
FileUtils.mv( @old_folder_name , path.basename.to_s+'/') if ( Dir.exist?(path.basename.to_s+'/')) == false
|
|
|
|
FileUtils.rmdir @old_folder_name.to_s if ( Dir.exist?(@old_folder_name.to_s+'/') && (@old_folder_name.to_s != path.basename.to_s))
|
|
|
|
FileUtils.cd(@new_path.to_s)
|
|
|
|
@path_destination = Pathname.new(path.dirname)
|
|
|
|
@path_destination= @path_destination.parent.parent.parent + Pathname.new("bundler/gems/#{path.basename}")
|
|
|
|
if (Dir.exist?(@path_destination.to_s)) == false
|
|
|
|
begin
|
|
|
|
FileUtils.cd(@path_destination.dirname)
|
|
|
|
rescue
|
|
|
|
FileUtils.mkdir_p(@path_destination.dirname)
|
|
|
|
FileUtils.cd(@path_destination.dirname)
|
|
|
|
end
|
|
|
|
FileUtils.mkdir(@path_destination.basename)
|
|
|
|
`cp -r "#{path}"/. "#{@path_destination}"/.git/`
|
|
|
|
FileUtils.cd(@path_destination.basename)
|
|
|
|
`git init`
|
|
|
|
`git checkout "#{ref}"`
|
|
|
|
end
|
|
|
|
@gemspec_name = ''
|
|
|
|
FileUtils.cd(@path_destination.to_s)
|
|
|
|
`ls`.split.each{|name| (@gemspec_name = name.to_s and break) if (name.include? '.gemspec') == true}
|
|
|
|
Dir.chdir(Bundler.root.to_s) do
|
|
|
|
Bundler::load_gemspec("#{@path_destination}/#{@gemspec_name}") rescue puts "you don't have .gemspec file at #{@path_destination}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@gemspec_name = ''
|
|
|
|
FileUtils.cd(path.to_s)
|
|
|
|
`ls`.split.each{|name| (@gemspec_name = name.to_s and break) if (name.include? '.gemspec') == true}
|
|
|
|
Dir.chdir(Bundler.root.to_s) do
|
|
|
|
Bundler::load_gemspec("#{path}/#{@gemspec_name}") rescue puts "you don't have .gemspec file at #{path}"
|
2019-10-06 15:52:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
FileUtils.cd(@old_pwd)
|
|
|
|
end
|
2019-10-06 15:45:34 +00:00
|
|
|
else
|
|
|
|
Bundler.ui.info "Fetching #{uri}"
|
2019-10-06 15:52:01 +00:00
|
|
|
if path != nil
|
|
|
|
FileUtils.mkdir_p(path.dirname)
|
|
|
|
git_retry %|clone #{uri_escaped} "#{path}" --bare --no-hardlinks --quiet|
|
2019-10-08 13:47:35 +00:00
|
|
|
if path.to_s.include?("cache")
|
|
|
|
FileUtils.cd(path)
|
|
|
|
git_retry %|fetch --all|
|
|
|
|
@commit_num = `git log -1 --pretty=format:"%H"`[0..11] if revision.nil?
|
|
|
|
@commit_num = revision[0..11] if !revision.nil?
|
|
|
|
@new_path = path.dirname
|
|
|
|
@old_folder_name = path.basename.to_s + '/'
|
|
|
|
@fold_name =''
|
|
|
|
@fold_name = path.basename.to_s
|
|
|
|
@fold_name = @fold_name.split('-')[0..-2].join('-')
|
|
|
|
@new_path += (@fold_name + '-' + @commit_num)
|
|
|
|
path = @new_path
|
|
|
|
FileUtils.cd('..')
|
|
|
|
FileUtils.mv( @old_folder_name , path.basename.to_s+'/') if (Dir.exist? (path.basename.to_s+'/')) == false
|
|
|
|
FileUtils.rmdir @old_folder_name.to_s if ( Dir.exist?(@old_folder_name.to_s+'/') && (@old_folder_name.to_s != path.basename.to_s))
|
|
|
|
FileUtils.cd(@new_path.to_s)
|
|
|
|
@path_destination = Pathname.new(path.dirname)
|
|
|
|
@path_destination= @path_destination.parent.parent.parent + Pathname.new("bundler/gems/#{path.basename}")
|
|
|
|
if (Dir.exist?(@path_destination.to_s)) == false
|
|
|
|
begin
|
|
|
|
FileUtils.cd(@path_destination.dirname)
|
|
|
|
rescue
|
|
|
|
FileUtils.mkdir_p(@path_destination.dirname)
|
|
|
|
FileUtils.cd(@path_destination.dirname)
|
|
|
|
end
|
|
|
|
FileUtils.mkdir(@path_destination.basename)
|
|
|
|
`cp -r "#{path}"/. "#{@path_destination}"/.git/`
|
|
|
|
FileUtils.cd(@path_destination.basename)
|
|
|
|
`git init`
|
|
|
|
`git checkout "#{ref}"`
|
|
|
|
end
|
|
|
|
@gemspec_name = ''
|
|
|
|
FileUtils.cd(@path_destination.to_s)
|
|
|
|
`ls`.split.each{|name| (@gemspec_name = name.to_s and break) if (name.include? '.gemspec') == true}
|
|
|
|
@EXT_LOCK.synchronize do
|
|
|
|
Dir.chdir(Bundler.root.to_s) do
|
|
|
|
Bundler::load_gemspec("#{@path_destination}/#{@gemspec_name}") rescue puts "you don't have .gemspec file at #{@path_destination}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@gemspec_name = ''
|
|
|
|
FileUtils.cd(path.to_s)
|
|
|
|
`ls`.split.each{|name| (@gemspec_name = name.to_s and break) if (name.include? '.gemspec') == true}
|
|
|
|
@EXT_LOCK.synchronize do
|
|
|
|
Dir.chdir(Bundler.root.to_s) do
|
|
|
|
Bundler::load_gemspec("#{path}/#{@gemspec_name}") rescue puts "you don't have .gemspec file at #{path}"
|
|
|
|
end
|
2019-10-06 15:52:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-06 15:45:34 +00:00
|
|
|
end
|
2019-10-06 15:52:01 +00:00
|
|
|
FileUtils.cd(@org_pwd)
|
2019-10-06 15:45:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to(destination, submodules=false)
|
|
|
|
unless File.exist?(destination.join(".git"))
|
|
|
|
FileUtils.mkdir_p(destination.dirname)
|
|
|
|
FileUtils.rm_rf(destination)
|
|
|
|
git_retry %|clone --no-checkout --quiet "#{path}" "#{destination}"|
|
|
|
|
File.chmod((0777 & ~File.umask), destination)
|
|
|
|
end
|
|
|
|
|
|
|
|
SharedHelpers.chdir(destination) do
|
|
|
|
git_retry %|fetch --force --quiet --tags "#{path}"|
|
|
|
|
git "reset --hard #{@revision}"
|
|
|
|
|
|
|
|
if submodules
|
|
|
|
git_retry "submodule update --init --recursive"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# TODO: Do not rely on /dev/null.
|
|
|
|
# Given that open3 is not cross platform until Ruby 1.9.3,
|
|
|
|
# the best solution is to pipe to /dev/null if it exists.
|
|
|
|
# If it doesn't, everything will work fine, but the user
|
|
|
|
# will get the $stderr messages as well.
|
|
|
|
def git_null(command)
|
|
|
|
git("#{command} 2>#{Bundler::NULL}", false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_retry(command)
|
|
|
|
Bundler::Retry.new("git #{command}", GitNotAllowedError).attempts do
|
|
|
|
git(command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def git(command, check_errors=true)
|
|
|
|
raise GitNotAllowedError.new(command) unless allow?
|
|
|
|
out = SharedHelpers.with_clean_git_env { %x{git #{command}} }
|
|
|
|
raise GitCommandError.new(command, path) if check_errors && !$?.success?
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_revision_cached?
|
|
|
|
return unless @revision
|
|
|
|
in_path { git("cat-file -e #{@revision}") }
|
|
|
|
true
|
|
|
|
rescue GitError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Escape the URI for git commands
|
|
|
|
def uri_escaped
|
|
|
|
if Bundler::WINDOWS
|
|
|
|
# Windows quoting requires double quotes only, with double quotes
|
|
|
|
# inside the string escaped by being doubled.
|
|
|
|
'"' + uri.gsub('"') {|s| '""'} + '"'
|
|
|
|
else
|
|
|
|
# Bash requires single quoted strings, with the single quotes escaped
|
|
|
|
# by ending the string, escaping the quote, and restarting the string.
|
|
|
|
"'" + uri.gsub("'") {|s| "'\\''"} + "'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow?
|
2019-10-06 15:52:01 +00:00
|
|
|
#@git ? @git.allow_git_ops? : true
|
|
|
|
true
|
2019-10-06 15:45:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_path(&blk)
|
|
|
|
checkout unless path.exist?
|
|
|
|
SharedHelpers.chdir(path, &blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_in_path
|
|
|
|
return in_path { yield } if allow?
|
|
|
|
raise GitError, "The git source #{uri} is not yet checked out. Please run `bundle install` before trying to start your application"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|