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

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

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

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.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubygems/source/git.rb', line 51

def initialize(name, repository, reference, submodules = false)
  require_relative "../uri"
  @uri = Gem::Uri.parse(repository)
  @name            = name
  @repository      = repository
  @reference       = reference || "HEAD"
  @need_submodules = submodules

  @remote   = true
  @root_dir = Gem.dir
end

Instance Attribute Details

#nameObject (readonly)

The name of the gem created by this git gem.



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

def name
  @name
end

#need_submodulesObject (readonly)

Does this repository need submodules checked out too?



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

def need_submodules
  @need_submodules
end

#referenceObject (readonly)

The commit reference used for checking out this git gem.



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

def reference
  @reference
end

#remoteObject

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



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

def remote
  @remote
end

#repositoryObject (readonly)

The git repository this gem is sourced from.



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

def repository
  @repository
end

#root_dirObject

The directory for cache and git gem installation



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

def root_dir
  @root_dir
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object

:nodoc:



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

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

#base_dirObject

Directory where git gems get unpacked and so-forth.



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

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

#cacheObject

Creates a local cache repository for the git gem.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubygems/source/git.rb', line 119

def cache # :nodoc:
  return unless @remote

  if File.exist? repo_cache_dir
    Dir.chdir repo_cache_dir do
      system git_command, "fetch", "--quiet", "--force", "--tags",
             @repository, "refs/heads/*:refs/heads/*"
    end
  else
    system git_command, "clone", "--quiet", "--bare", "--no-hardlinks",
           @repository, repo_cache_dir
  end
end

#checkoutObject

Checks out the files for the repository into the install_dir.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubygems/source/git.rb', line 90

def checkout # :nodoc:
  cache

  return false unless File.exist? repo_cache_dir

  unless File.exist? install_dir
    system git_command, "clone", "--quiet", "--no-checkout",
           repo_cache_dir, install_dir
  end

  Dir.chdir install_dir do
    system git_command, "fetch", "--quiet", "--force", "--tags", install_dir

    success = system git_command, "reset", "--quiet", "--hard", rev_parse

    if @need_submodules
      require "open3"
      _, status = Open3.capture2e(git_command, "submodule", "update", "--quiet", "--init", "--recursive")

      success &&= status.success?
    end

    success
  end
end

#dir_shortrefObject

A short reference for use in git gem directories



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

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

#download(full_spec, path) ⇒ Object

Nothing to download for git gems



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

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

#git_commandObject



83
84
85
# File 'lib/rubygems/source/git.rb', line 83

def git_command
  ENV.fetch("git", "git")
end

#install_dirObject

The directory where the git gem will be installed.



156
157
158
159
160
# File 'lib/rubygems/source/git.rb', line 156

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

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

#pretty_print(q) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rubygems/source/git.rb', line 162

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

      q.breakable
      q.text @reference
    end
  end
end

#repo_cache_dirObject

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



177
178
179
# File 'lib/rubygems/source/git.rb', line 177

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.

Raises:



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rubygems/source/git.rb', line 184

def rev_parse # :nodoc:
  hash = nil

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

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

  hash
end

#specsObject

Loads all gemspecs in the repository



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rubygems/source/git.rb', line 201

def specs
  checkout

  return [] unless install_dir

  Dir.chdir install_dir do
    Dir["{,*,*/*}.gemspec"].filter_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
  end
end

#uri_hashObject

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



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rubygems/source/git.rb', line 231

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

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

  OpenSSL::Digest::SHA1.hexdigest normalized
end