class R10K::Source::Git

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.

@since 1.3.0

Attributes

cache[R]

@!attribute [r] cache

@api private
@return [R10K::Git::Cache] The git cache associated with this source
invalid_branches[R]

@!attribute [r] #invalid_branches

@return [String] How Git branch names that cannot be cleanly mapped to
  Puppet environments will be handled.
remote[R]

@!attribute [r] remote

@return [String] The URL to the remote git repository
settings[R]

@!attribute [r] settings

@return [Hash<Symbol, Object>] Additional settings that configure how
  the source should behave.

Public Class Methods

new(name, basedir, options = {}) click to toggle source

Initialize the given source.

@param name [String] The identifier for this source. @param basedir [String] The base directory where the generated environments will be created. @param options [Hash] An additional set of options for this source.

@option options [Boolean, String] :prefix If a String this becomes the prefix.

If true, will use the source name as the prefix.
Defaults to false for no environment prefix.

@option options [String] :remote The URL to the base directory of the SVN repository @option options [Hash] :remote Additional settings that configure how the

source should behave.
Calls superclass method R10K::Source::Base.new
# File lib/r10k/source/git.rb, line 51
def initialize(name, basedir, options = {})
  super

  @environments = []

  @remote           = options[:remote]
  @invalid_branches = (options[:invalid_branches] || 'correct_and_warn')

  @cache  = R10K::Git.cache.generate(@remote)
end

Public Instance Methods

desired_contents() click to toggle source

List all environments that should exist in the basedir for this source @note This is required by {R10K::Util::Basedir} @return [Array<String>]

# File lib/r10k/source/git.rb, line 108
def desired_contents
  environments.map {|env| env.dirname }
end
environments() click to toggle source

Load the git remote and create environments for each branch. If the cache has not been fetched, this will return an empty list.

@return [Array<R10K::Environment::Git>]

# File lib/r10k/source/git.rb, line 77
def environments
  if not @cache.cached?
    []
  elsif @environments.empty?
    @environments = generate_environments()
  else
    @environments
  end
end
fetch_remote()
Alias for: preload!
generate_environments() click to toggle source
# File lib/r10k/source/git.rb, line 87
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})
    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})
    elsif bn.validate?
     logger.error _("Environment %{env_name} contained non-word characters, ignoring it.") % {env_name: bn.name.inspect}
    end
  end

  envs
end
preload!() click to toggle source

Update the git cache for this git source to get the latest list of environments.

@return [void]

# File lib/r10k/source/git.rb, line 65
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
Also aliased as: fetch_remote

Private Instance Methods

branch_names() click to toggle source
# File lib/r10k/source/git.rb, line 114
def branch_names
  opts = {:prefix => @prefix, :invalid => @invalid_branches, :source => @name}
  @cache.branches.map do |branch|
    R10K::Environment::Name.new(branch, opts)
  end
end