Class: CookbookOmnifetch::GitLocation
- Inherits:
-
BaseLocation
- Object
- BaseLocation
- CookbookOmnifetch::GitLocation
- Defined in:
- lib/cookbook-omnifetch/git.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#branch ⇒ Object
readonly
Returns the value of attribute branch.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
-
#rel ⇒ Object
readonly
Returns the value of attribute rel.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes inherited from BaseLocation
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cache_key ⇒ Object
- #cached_cookbook ⇒ Object
-
#initialize(dependency, options = {}) ⇒ GitLocation
constructor
A new instance of GitLocation.
-
#install ⇒ Object
Install this git cookbook into the cookbook store.
-
#install_path ⇒ Pathname?
The path where this cookbook would live in the store, if it were installed.
- #installed? ⇒ Boolean
- #lock_data ⇒ Object
- #to_lock ⇒ Object
- #to_s ⇒ Object
Methods inherited from BaseLocation
Constructor Details
#initialize(dependency, options = {}) ⇒ GitLocation
Returns a new instance of GitLocation.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cookbook-omnifetch/git.rb', line 15 def initialize(dependency, = {}) super @uri = [:git] @branch = [:branch] @tag = [:tag] @ref = [:ref] @revision = [:revision] @rel = [:rel] # The revision to parse @rev_parse = [:ref] || [:branch] || [:tag] || "master" end |
Instance Attribute Details
#branch ⇒ Object (readonly)
Returns the value of attribute branch.
9 10 11 |
# File 'lib/cookbook-omnifetch/git.rb', line 9 def branch @branch end |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
11 12 13 |
# File 'lib/cookbook-omnifetch/git.rb', line 11 def ref @ref end |
#rel ⇒ Object (readonly)
Returns the value of attribute rel.
13 14 15 |
# File 'lib/cookbook-omnifetch/git.rb', line 13 def rel @rel end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
12 13 14 |
# File 'lib/cookbook-omnifetch/git.rb', line 12 def revision @revision end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
10 11 12 |
# File 'lib/cookbook-omnifetch/git.rb', line 10 def tag @tag end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
8 9 10 |
# File 'lib/cookbook-omnifetch/git.rb', line 8 def uri @uri end |
Instance Method Details
#==(other) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/cookbook-omnifetch/git.rb', line 92 def ==(other) other.is_a?(GitLocation) && other.uri == uri && other.branch == branch && other.tag == tag && other.shortref == shortref && other.rel == rel end |
#cache_key ⇒ Object
141 142 143 |
# File 'lib/cookbook-omnifetch/git.rb', line 141 def cache_key "#{dependency.name}-#{revision}" end |
#cached_cookbook ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/cookbook-omnifetch/git.rb', line 84 def cached_cookbook if installed? @cached_cookbook ||= CookbookOmnifetch.cached_cookbook_class.from_path(install_path) else nil end end |
#install ⇒ Object
Install this git cookbook into the cookbook store. This method leverages a cached git copy and a scratch directory to prevent bad cookbooks from making their way into the cookbook store.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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/cookbook-omnifetch/git.rb', line 39 def install scratch_path = Pathname.new(Dir.mktmpdir) if cached? Dir.chdir(cache_path) do git %{fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"} end else git %{clone #{uri} "#{cache_path}" --bare --no-hardlinks} end Dir.chdir(cache_path) do @revision ||= git %{rev-parse #{@rev_parse}} end # Clone into a scratch directory for validations git %{clone --no-checkout "#{cache_path}" "#{scratch_path}"} # Make sure the scratch directory is up-to-date and account for rel paths Dir.chdir(scratch_path) do git %{fetch --force --tags "#{cache_path}"} git %{reset --hard #{@revision}} if rel git %{filter-branch --subdirectory-filter "#{rel}" --force} end end # Validate the scratched path is a valid cookbook validate_cached!(scratch_path) # If we got this far, we should copy FileUtils.rm_rf(install_path) if install_path.exist? FileUtils.cp_r(scratch_path, install_path) # Remove the git history FileUtils.rm_rf(File.join(install_path, ".git")) install_path.chmod(0777 & ~File.umask) ensure # Ensure the scratch directory is cleaned up FileUtils.rm_rf(scratch_path) if scratch_path end |
#install_path ⇒ Pathname?
The path where this cookbook would live in the store, if it were installed.
136 137 138 139 |
# File 'lib/cookbook-omnifetch/git.rb', line 136 def install_path CookbookOmnifetch.storage_path .join(cache_key) end |
#installed? ⇒ Boolean
30 31 32 |
# File 'lib/cookbook-omnifetch/git.rb', line 30 def installed? (!!revision) && install_path.exist? end |
#lock_data ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cookbook-omnifetch/git.rb', line 111 def lock_data out = {} out["git"] = uri out["revision"] = revision out["ref"] = shortref if shortref out["branch"] = branch if branch out["tag"] = tag if tag out["rel"] = rel if rel out end |
#to_lock ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/cookbook-omnifetch/git.rb', line 122 def to_lock out = " git: #{uri}\n" out << " revision: #{revision}\n" out << " ref: #{shortref}\n" if shortref out << " branch: #{branch}\n" if branch out << " tag: #{tag}\n" if tag out << " rel: #{rel}\n" if rel out end |
#to_s ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/cookbook-omnifetch/git.rb', line 101 def to_s info = tag || branch || shortref || @rev_parse if rel "#{uri} (at #{info}/#{rel})" else "#{uri} (at #{info})" end end |