123456789_123456789_123456789_123456789_123456789_

Class: Gem::Source::Git

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, ::Gem::Source, ::Gem::Text, Comparable
Inherits: Gem::Source
Defined in: lib/rubygems/source/git.rb

Overview

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

source.specs

Constant Summary

::Gem::Source - Inherited

FILES

Class Method Summary

::Gem::Source - Inherited

.new

Creates a new ::Gem::Source which will use the index located at #uri.

Instance Attribute Summary

  • #name readonly

    The name of the gem created by this git gem.

  • #need_submodules readonly

    Does this repository need submodules checked out too?

  • #reference readonly

    The commit reference used for checking out this git gem.

  • #remote rw

    When false the cache for this repository will not be updated.

  • #repository readonly

    The git repository this gem is sourced from.

  • #root_dir rw

    The directory for cache and git gem installation.

::Gem::Source - Inherited

#update_cache?

Returns true when it is possible and safe to update the cache directory.

#uri

The URI this source will fetch gems from.

Instance Method Summary

  • #<=>(other)
  • #specs

    Loads all gemspecs in the repository.

  • #==(other) Internal use only
  • #base_dir Internal use only

    Directory where git gems get unpacked and so-forth.

  • #cache Internal use only

    Creates a local cache repository for the git gem.

  • #checkout Internal use only

    Checks out the files for the repository into the install_dir.

  • #dir_shortref Internal use only

    A short reference for use in git gem directories.

  • #download(full_spec, path) Internal use only

    Nothing to download for git gems.

  • #install_dir Internal use only

    The directory where the git gem will be installed.

  • #pretty_print(q) Internal use only
  • #repo_cache_dir Internal use only

    The directory where the git gem’s repository will be cached.

  • #rev_parse Internal use only

    Converts the git reference for the repository into a commit hash.

  • #uri_hash Internal use only

    A hash for the git gem based on the git repository URI.

::Gem::Source - Inherited

#<=>

Sources are ordered by installation preference.

#cache_dir

Returns the local directory to write #uri to.

#download

Downloads spec and writes it to Gem.dir.

#fetch_spec

Fetches a specification for the given name_tuple.

#load_specs

Loads type kind of specs fetching from @uri if the on-disk cache is out of date.

#typo_squatting?, #enforce_trailing_slash, #==,
#dependency_resolver_set

Returns a Set that can fetch specifications from this source.

#eql?

Alias for #==.

#hash, #pretty_print

::Gem::Text - Included

#clean_text

Remove any non-printable characters and make the text suitable for printing.

#format_text

Wraps text to wrap characters and optionally indents by indent characters.

#levenshtein_distance

This code is based directly on the ::Gem::Text gem implementation Returns a value representing the “cost” of transforming str1 into str2.

#truncate_text, #min3

Constructor Details

.new(name, repository, reference, submodules = false) ⇒ Git

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.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 51

def initialize(name, repository, reference, submodules = false)
  super repository

  @name            = name
  @repository      = repository
  @reference       = reference
  @need_submodules = submodules

  @remote   = true
  @root_dir = Gem.dir
  @git      = ENV['git'] || 'git'
end

Instance Attribute Details

#name (readonly)

The name of the gem created by this git gem.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 17

attr_reader :name

#need_submodules (readonly)

Does this repository need submodules checked out too?

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 42

attr_reader :need_submodules

#reference (readonly)

The commit reference used for checking out this git gem.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 22

attr_reader :reference

#remote (rw)

When false the cache for this repository will not be updated.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 27

attr_accessor :remote

#repository (readonly)

The git repository this gem is sourced from.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 32

attr_reader :repository

#root_dir (rw)

The directory for cache and git gem installation

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 37

attr_accessor :root_dir

Instance Method Details

#<=>(other)

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 64

def <=>(other)
  case other
  when Gem::Source::Git then
    0
  when Gem::Source::Vendor,
       Gem::Source::Lock then
    -1
  when Gem::Source then
    1
  else
    nil
  end
end

#==(other)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 78

def ==(other) # :nodoc:
  super and
    @name            == other.name and
    @repository      == other.repository and
    @reference       == other.reference and
    @need_submodules == other.need_submodules
end

#base_dir

This method is for internal use only.

Directory where git gems get unpacked and so-forth.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 134

def base_dir # :nodoc:
  File.join @root_dir, 'bundler'
end

#cache

This method is for internal use only.

Creates a local cache repository for the git gem.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 117

def cache # :nodoc:
  return unless @remote

  if File.exist? repo_cache_dir
    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

#checkout

This method is for internal use only.

Checks out the files for the repository into the install_dir.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 89

def checkout # :nodoc:
  cache

  return false unless File.exist? repo_cache_dir

  unless File.exist? install_dir
    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', rev_parse

    if @need_submodules
      _, status = Open3.capture2e(@git, 'submodule', 'update', '--quiet', '--init', '--recursive')

      success &&= status.success?
    end

    success
  end
end

#dir_shortref

This method is for internal use only.

A short reference for use in git gem directories

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 141

def dir_shortref # :nodoc:
  rev_parse[0..11]
end

#download(full_spec, path)

This method is for internal use only.

Nothing to download for git gems

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 148

def download(full_spec, path) # :nodoc:
end

#install_dir

This method is for internal use only.

The directory where the git gem will be installed.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 154

def install_dir # :nodoc:
  return unless File.exist? repo_cache_dir

  File.join base_dir, 'gems', "#{@name}-#{dir_shortref}"
end

#pretty_print(q)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 160

def pretty_print(q) # :nodoc:
  q.group 2, '[Git: ', ']' do
    q.breakable
    q.text @repository

    q.breakable
    q.text @reference
  end
end

#repo_cache_dir

This method is for internal use only.

The directory where the git gem’s repository will be cached.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 173

def repo_cache_dir # :nodoc:
  File.join @root_dir, 'cache', 'bundler', 'git', "#{@name}-#{uri_hash}"
end

#rev_parse

This method is for internal use only.

Converts the git reference for the repository into a commit hash.

Raises:

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 180

def rev_parse # :nodoc:
  hash = nil

  Dir.chdir repo_cache_dir do
    hash = Gem::Util.popen(@git, 'rev-parse', @reference).strip
  end

  raise Gem::Exception,
        "unable to find reference #{@reference} in #{@repository}" unless
          $?.success?

  hash
end

#specs

Loads all gemspecs in the repository

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 197

def specs
  checkout

  return [] unless install_dir

  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
        spec = Gem::Specification.load file
        if spec
          spec.base_dir = base_dir

          spec.extension_dir =
            File.join base_dir, 'extensions', Gem::Platform.local.to_s,
              Gem.extension_api_version, "#{name}-#{dir_shortref}"

          spec.full_gem_path = File.dirname spec.loaded_from if spec
        end
        spec
      end
    end.compact
  end
end

#uri_hash

This method is for internal use only.

A hash for the git gem based on the git repository URI.

[ GitHub ]

  
# File 'lib/rubygems/source/git.rb', line 227

def uri_hash # :nodoc:
  require_relative '../openssl'

  normalized =
    if @repository =~ %r{^\w://(\w@)?}
      uri = URI(@repository).normalize.to_s.sub %r{/$},''
      uri.sub(/\A(\w+)/) { $1.downcase }
    else
      @repository
    end

  OpenSSL::Digest::SHA1.hexdigest normalized
end