Class: Drupid::DownloadStrategy::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/drupid/download_strategy.rb

Instance Attribute Summary

Attributes inherited from Base

#dest, #name, #staged_path, #url

Instance Method Summary collapse

Methods included from Utils

#blah, #bzr, #compare_paths, #curl, #cvs, #debug, #dont_debug, #git, #hg, #ignore_interrupts, #interactive_shell, #odie, #ofail, #ohai, #owarn, #runBabyRun, #svn, #tempdir, #uncompress, #which, #writeFile

Constructor Details

#initialize(url, dest, name, download_specs = {}) ⇒ Git

Returns a new instance of Git.


253
254
255
256
# File 'lib/drupid/download_strategy.rb', line 253

def initialize url, dest, name, download_specs = {}
  super
  @clone = @dest + @name
end

Instance Method Details

#fetchObject


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/drupid/download_strategy.rb', line 266

def fetch
  raise "You must install Git." unless which "git"

  blah "Cloning #{@url}"

  if @clone.exist?
    Dir.chdir(@clone) do
      # Check for interrupted clone from a previous install
      unless system 'git', 'status', '-s'
        blah "Removing invalid .git repo from cache"
        FileUtils.rm_rf @clone
      end
    end
  end

  unless @clone.exist?
    clone_args = ['clone']
    clone_args << '--depth' << '1' if support_depth?

    if @specs.has_key?('branch')
      clone_args << '--branch' << @specs['branch']
    elsif @specs.has_key?('tag')
      clone_args << '--branch' << @specs['tag']
    end

    clone_args << @url << @clone
    git(*clone_args)
  else
    blah "Updating #{@clone}"
    Dir.chdir(@clone) do
      git 'config', 'remote.origin.url', @url

      rof =
        if @specs.has_key?('branch')
          "+refs/heads/#{@specs['branch']}:refs/remotes/origin/#{@specs['branch']}"
        elsif @specs.has_key?('tag')
          "+refs/tags/#{@specs['tag']}:refs/tags/#{@specs['tag']}"
        else
          '+refs/heads/master:refs/remotes/origin/master'
        end
      git 'config', 'remote.origin.fetch', rof

      git_args = %w[fetch origin]
      git(*git_args)
    end
  end
end

#host_supports_depth?Boolean

Returns:

  • (Boolean)

262
263
264
# File 'lib/drupid/download_strategy.rb', line 262

def host_supports_depth?
  @url =~ %r(git://) or @url =~ %r(https://github.com/)
end

#stage(wd = @dest) ⇒ Object

Stages this download into the specified directory. Invokes #fetch to retrieve the file if needed.


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/drupid/download_strategy.rb', line 316

def stage wd = @dest
  fetch unless @clone.exist?
  dont_debug { wd.mkpath }
  debug "Staging into #{wd}"
  target = wd + @clone.basename
  Dir.chdir @clone do
    if @specs.has_key?('branch')
      git 'checkout', "origin/#{@specs['branch']}", '--'
    elsif @specs.has_key?('tag')
      git 'checkout', @specs['tag'], '--'
    elsif @specs.has_key?('revision')
      git 'checkout', @specs['revision'], '--'
    else
      # otherwise the checkout-index won't checkout HEAD
      # https://github.com/mxcl/homebrew/issues/7124
      # must specify origin/HEAD, otherwise it resets to the current local HEAD
      git 'reset', '--hard', 'origin/HEAD'
    end
    # http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export
    git 'checkout-index', '-a', '-f', "--prefix=#{target}/"
    # check for submodules
    if File.exist?('.gitmodules')
      git 'submodule', 'init'
      git 'submodule', 'update'
      sub_cmd = "git checkout-index -a -f \"--prefix=#{target}/$path/\""
      git 'submodule', '--quiet', 'foreach', '--recursive', sub_cmd
    end
  end
  @staged_path = target
end

#support_depth?Boolean

Returns:

  • (Boolean)

258
259
260
# File 'lib/drupid/download_strategy.rb', line 258

def support_depth?
  !(@specs.has_key?('revision')) and host_supports_depth?
end