Class: Gem::Source::Git

Inherits:
Gem::Source show all
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

Constants inherited from Gem::Source

FILES

Instance Attribute Summary collapse

Attributes inherited from Gem::Source

#uri

Instance Method Summary collapse

Methods inherited from Gem::Source

#api_uri, #cache_dir, #dependency_resolver_set, #fetch_spec, #hash, #load_specs, #update_cache?

Constructor Details

#initialize(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.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubygems/source/git.rb', line 48

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

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

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

Instance Attribute Details

#nameObject (readonly)

The name of the gem created by this git gem.



19
20
21
# File 'lib/rubygems/source/git.rb', line 19

def name
  @name
end

#need_submodulesObject (readonly)

Does this repository need submodules checked out too?



39
40
41
# File 'lib/rubygems/source/git.rb', line 39

def need_submodules
  @need_submodules
end

#referenceObject (readonly)

The commit reference used for checking out this git gem.



24
25
26
# File 'lib/rubygems/source/git.rb', line 24

def reference
  @reference
end

#repositoryObject (readonly)

The git repository this gem is sourced from.



29
30
31
# File 'lib/rubygems/source/git.rb', line 29

def repository
  @repository
end

#root_dirObject

The directory for cache and git gem installation



34
35
36
# File 'lib/rubygems/source/git.rb', line 34

def root_dir
  @root_dir
end

Instance Method Details

#<=>(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubygems/source/git.rb', line 60

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

#==(other) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
# File 'lib/rubygems/source/git.rb', line 74

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

#base_dirObject

Directory where git gems get unpacked and so-forth.



124
125
126
# File 'lib/rubygems/source/git.rb', line 124

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

#cacheObject

Creates a local cache repository for the git gem.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubygems/source/git.rb', line 109

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

#checkoutObject

Checks out the files for the repository into the install_dir.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubygems/source/git.rb', line 85

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 &&=
      Gem::Util.silent_system @git, 'submodule', 'update',
             '--quiet', '--init', '--recursive' if @need_submodules

    success
  end
end

#dir_shortrefObject

A short reference for use in git gem directories



131
132
133
# File 'lib/rubygems/source/git.rb', line 131

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

#download(full_spec, path) ⇒ Object

Nothing to download for git gems



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

def download full_spec, path # :nodoc:
end

#install_dirObject

The directory where the git gem will be installed.



144
145
146
# File 'lib/rubygems/source/git.rb', line 144

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

#pretty_print(q) ⇒ Object

:nodoc:



148
149
150
151
152
153
154
155
156
# File 'lib/rubygems/source/git.rb', line 148

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

    q.breakable
    q.text @reference
  end
end

#repo_cache_dirObject

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



161
162
163
# File 'lib/rubygems/source/git.rb', line 161

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

#rev_parseObject

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



168
169
170
171
172
# File 'lib/rubygems/source/git.rb', line 168

def rev_parse # :nodoc:
  Dir.chdir repo_cache_dir do
    Gem::Util.popen(@git, 'rev-parse', @reference).strip
  end
end

#specsObject

Loads all gemspecs in the repository



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rubygems/source/git.rb', line 177

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

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



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rubygems/source/git.rb', line 205

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