Class: Berkshelf::GitLocation

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

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

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



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

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint
  @uri                = options[:git]
  @branch             = options[:branch] || options[:tag] || 'master'
  @ref                = options[:ref]
  @rel                = options[:rel]

  Git.validate_uri!(@uri)
end

Instance Attribute Details

#branchObject Also known as: tag

Returns the value of attribute branch.



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

def branch
  @branch
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#refObject

Returns the value of attribute ref.



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

def ref
  @ref
end

#relObject

Returns the value of attribute rel.



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

def rel
  @rel
end

#uriObject

Returns the value of attribute uri.



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

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



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

def tmpdir
  @tmpdir ||= Berkshelf.mktmpdir
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:



55
56
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
# File 'lib/berkshelf/locations/git_location.rb', line 55

def download(destination)
  if cached?(destination)
    @ref ||= Berkshelf::Git.rev_parse(revision_path(destination))
    return local_revision(destination)
  end

  Berkshelf::Git.checkout(clone, ref || branch) if ref || branch
  @ref = Berkshelf::Git.rev_parse(clone)

  tmp_path = rel ? File.join(clone, rel) : clone
  unless File.chef_cookbook?(tmp_path)
    msg = "Cookbook '#{name}' not found at git: #{uri}"
    msg << " with branch '#{branch}'" if branch
    msg << " with ref '#{ref}'" if ref
    msg << " at path '#{rel}'" if rel
    raise CookbookNotFound, msg
  end

  cb_path = File.join(destination, "#{name}-#{ref}")
  FileUtils.rm_rf(cb_path)
  FileUtils.mv(tmp_path, cb_path)

  cached = CachedCookbook.from_store_path(cb_path)
  validate_cached(cached)

  cached
end

#to_hashObject



83
84
85
86
87
88
# File 'lib/berkshelf/locations/git_location.rb', line 83

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

#to_sObject



90
91
92
93
94
95
# File 'lib/berkshelf/locations/git_location.rb', line 90

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