Class: Berkshelf::GitLocation

Inherits:
Object
  • Object
show all
Includes:
Location
Defined in:
lib/berkshelf/locations/git_location.rb

Overview

Author:

Direct Known Subclasses

GithubLocation

Constant Summary

Constants included from Location

Location::OPSCODE_COMMUNITY_API

Instance Attribute Summary collapse

Attributes included from Location

#name, #version_constraint

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Location

#downloaded?, included, init, #to_json, #validate_cached

Constructor Details

#initialize(name, version_constraint, options = {}) ⇒ GitLocation

Returns a new instance of GitLocation.

Parameters:

  • name (#to_s)
  • version_constraint (Solve::Constraint)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :git (String)

    the Git URL to clone

  • :ref (String)

    the commit hash or an alias to a commit hash to clone

  • :branch (String)

    same as ref

  • :tag (String)

    same as tag

  • :rel (String)

    the path within the repository to find the cookbook



43
44
45
46
47
48
49
50
51
52
# File 'lib/berkshelf/locations/git_location.rb', line 43

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint
  @uri                = options[:git]
  @branch             = options[:branch] || options[:ref] || options[:tag] || "master"
  @rel                = options[:rel]
  @branch_name        = @branch.gsub("-", "_").gsub("/", "__")  # In case the remote is specified

  Git.validate_uri!(@uri)
end

Instance Attribute Details

#branchObject Also known as: ref, tag

Returns the value of attribute branch.



21
22
23
# File 'lib/berkshelf/locations/git_location.rb', line 21

def branch
  @branch
end

#branch_nameObject

Returns the value of attribute branch_name.



23
24
25
# File 'lib/berkshelf/locations/git_location.rb', line 23

def branch_name
  @branch_name
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/berkshelf/locations/git_location.rb', line 24

def options
  @options
end

#relObject

Returns the value of attribute rel.



22
23
24
# File 'lib/berkshelf/locations/git_location.rb', line 22

def rel
  @rel
end

#uriObject

Returns the value of attribute uri.



20
21
22
# File 'lib/berkshelf/locations/git_location.rb', line 20

def uri
  @uri
end

Class Method Details

.tmpdirString

Create a temporary directory for the cloned repository within Berkshelf’s temporary directory

Returns:

  • (String)

    the path to the created temporary directory



10
11
12
# File 'lib/berkshelf/locations/git_location.rb', line 10

def tmpdir
  @tmpdir ||= Berkshelf.mktmpdir
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/berkshelf/locations/git_location.rb', line 57

def download(destination)
  return local_revision(destination) if cached?(destination)

  ::Berkshelf::Git.checkout(clone, branch) if branch
  unless branch
    self.branch = ::Berkshelf::Git.rev_parse(clone)
  end

  tmp_path = rel ? File.join(clone, rel) : clone
  puts "File.chef_cookbook?(tmp_path): #{File.chef_cookbook?(tmp_path)}"
  unless File.chef_cookbook?(tmp_path)
    msg = "Cookbook '#{name}' not found at git: #{uri}"
    msg << " with branch '#{branch}'" if branch
    msg << " at path '#{rel}'" if rel
    raise CookbookNotFound, msg
  end
  
  cb_path = File.join(destination, "#{name}-#{branch_name}")
  FileUtils.rm_rf(cb_path)
  FileUtils.mv(tmp_path, cb_path)
  
  cached = CachedCookbook.from_store_path(cb_path)
  validate_cached(cached)

  set_downloaded_status(true)
  puts "cached.class: #{cached.class}"
  cached
end

#to_hashObject



86
87
88
89
90
91
# File 'lib/berkshelf/locations/git_location.rb', line 86

def to_hash
  super.tap do |h|
    h[:value]  = self.uri
    h[:branch] = self.branch if branch
  end
end

#to_sObject



93
94
95
96
97
# File 'lib/berkshelf/locations/git_location.rb', line 93

def to_s
  s = "#{self.class.location_key}: '#{uri}'"
  s << " with branch: '#{branch}'" if branch
  s
end