Class: R10K::Source::Git
- Includes:
- Logging
- Defined in:
- lib/r10k/source/git.rb
Overview
This class implements a source for Git environments.
A Git source generates environments by locally caching the given Git repository and enumerating the branches for the Git repository. Branches are mapped to environments without modification.
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
- #cache ⇒ Object readonly
- #filter_command ⇒ Object readonly
- #ignore_branch_prefixes ⇒ Object readonly
- #invalid_branches ⇒ Object readonly
- #remote ⇒ Object readonly
- #settings ⇒ Object readonly
Attributes inherited from Base
#basedir, #name, #prefix, #puppetfile_name
Instance Method Summary collapse
-
#desired_contents ⇒ Array<String>
List all environments that should exist in the basedir for this source.
-
#environments ⇒ Array<R10K::Environment::Git>
Load the git remote and create environments for each branch.
- #filter_branches_by_command(branches, command) ⇒ Object
- #filter_branches_by_regexp(branches, ignore_prefixes) ⇒ Object
- #generate_environments ⇒ Object
-
#initialize(name, basedir, options = {}) ⇒ Git
constructor
Initialize the given source.
-
#preload! ⇒ void
(also: #fetch_remote)
Update the git cache for this git source to get the latest list of environments.
Methods included from Logging
debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Methods inherited from Base
Constructor Details
#initialize(name, basedir, options = {}) ⇒ Git
Initialize the given source.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/r10k/source/git.rb', line 60 def initialize(name, basedir, = {}) super @environments = [] @remote = [:remote] @invalid_branches = ([:invalid_branches] || 'correct_and_warn') @ignore_branch_prefixes = [:ignore_branch_prefixes] @filter_command = [:filter_command] @cache = R10K::Git.cache.generate(@remote) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
27 28 29 |
# File 'lib/r10k/source/git.rb', line 27 def cache @cache end |
#filter_command ⇒ Object (readonly)
46 47 48 |
# File 'lib/r10k/source/git.rb', line 46 def filter_command @filter_command end |
#ignore_branch_prefixes ⇒ Object (readonly)
42 43 44 |
# File 'lib/r10k/source/git.rb', line 42 def ignore_branch_prefixes @ignore_branch_prefixes end |
#invalid_branches ⇒ Object (readonly)
37 38 39 |
# File 'lib/r10k/source/git.rb', line 37 def invalid_branches @invalid_branches end |
#remote ⇒ Object (readonly)
22 23 24 |
# File 'lib/r10k/source/git.rb', line 22 def remote @remote end |
#settings ⇒ Object (readonly)
32 33 34 |
# File 'lib/r10k/source/git.rb', line 32 def settings @settings end |
Instance Method Details
#desired_contents ⇒ Array<String>
This is required by Util::Basedir
List all environments that should exist in the basedir for this source
119 120 121 |
# File 'lib/r10k/source/git.rb', line 119 def desired_contents environments.map {|env| env.dirname } end |
#environments ⇒ Array<R10K::Environment::Git>
Load the git remote and create environments for each branch. If the cache has not been fetched, this will return an empty list.
88 89 90 91 92 93 94 95 96 |
# File 'lib/r10k/source/git.rb', line 88 def environments if not @cache.cached? [] elsif @environments.empty? @environments = generate_environments() else @environments end end |
#filter_branches_by_command(branches, command) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/r10k/source/git.rb', line 135 def filter_branches_by_command(branches, command) branches.select do |branch| result = system({'GIT_DIR' => @cache.git_dir.to_s, 'R10K_BRANCH' => branch, 'R10K_NAME' => @name.to_s}, command) unless result logger.warn _("Branch `%{name}:%{branch}` filtered out by filter_command %{cmd}") % {name: @name, branch: branch, cmd: command} end result end end |
#filter_branches_by_regexp(branches, ignore_prefixes) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/r10k/source/git.rb', line 123 def filter_branches_by_regexp(branches, ignore_prefixes) filter = Regexp.new("^#{Regexp.union(ignore_prefixes)}") branches = branches.reject do |branch| result = filter.match(branch) if result logger.warn _("Branch %{branch} filtered out by ignore_branch_prefixes %{ibp}") % {branch: branch, ibp: @ignore_branch_prefixes} end result end branches end |
#generate_environments ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/r10k/source/git.rb', line 98 def generate_environments envs = [] branch_names.each do |bn| if bn.valid? envs << R10K::Environment::Git.new(bn.name, @basedir, bn.dirname, {:remote => remote, :ref => bn.name, :puppetfile_name => puppetfile_name }) elsif bn.correct? logger.warn _("Environment %{env_name} contained non-word characters, correcting name to %{corrected_env_name}") % {env_name: bn.name.inspect, corrected_env_name: bn.dirname} envs << R10K::Environment::Git.new(bn.name, @basedir, bn.dirname, {:remote => remote, :ref => bn.name, :puppetfile_name => puppetfile_name}) elsif bn.validate? logger.error _("Environment %{env_name} contained non-word characters, ignoring it.") % {env_name: bn.name.inspect} end end envs end |
#preload! ⇒ void Also known as: fetch_remote
This method returns an undefined value.
Update the git cache for this git source to get the latest list of environments.
76 77 78 79 80 81 |
# File 'lib/r10k/source/git.rb', line 76 def preload! logger.debug _("Fetching '%{remote}' to determine current branches.") % {remote: @remote} @cache.sync rescue => e raise R10K::Error.wrap(e, _("Unable to determine current branches for Git source '%{name}' (%{basedir})") % {name: @name, basedir: @basedir}) end |