Class: R10K::Source::Git

Inherits:
Base
  • Object
show all
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.

Since:

  • 1.3.0

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Attributes inherited from Base

#basedir, #name, #prefix, #puppetfile_name

Instance Method Summary collapse

Methods inherited from Base

#accept

Methods included from Logging

add_outputters, debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(name, basedir, options = {}) ⇒ Git

Initialize the given source.

Parameters:

  • name (String)

    The identifier for this source.

  • basedir (String)

    The base directory where the generated environments will be created.

  • options (Hash) (defaults to: {})

    An additional set of options for this source.

Options Hash (options):

  • :prefix (Boolean, String)

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

  • :remote (String)

    The URL to the base directory of the SVN repository

  • :remote (Hash)

    Additional settings that configure how the source should behave.

Since:

  • 1.3.0



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/r10k/source/git.rb', line 58

def initialize(name, basedir, options = {})
  super

  @environments = []

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

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

Instance Attribute Details

#cacheObject (readonly)

Since:

  • 1.3.0



25
26
27
# File 'lib/r10k/source/git.rb', line 25

def cache
  @cache
end

#filter_commandObject (readonly)

Since:

  • 1.3.0



44
45
46
# File 'lib/r10k/source/git.rb', line 44

def filter_command
  @filter_command
end

#ignore_branch_prefixesObject (readonly)

Since:

  • 1.3.0



40
41
42
# File 'lib/r10k/source/git.rb', line 40

def ignore_branch_prefixes
  @ignore_branch_prefixes
end

#invalid_branchesObject (readonly)

Since:

  • 1.3.0



35
36
37
# File 'lib/r10k/source/git.rb', line 35

def invalid_branches
  @invalid_branches
end

#remoteObject (readonly)

Since:

  • 1.3.0



20
21
22
# File 'lib/r10k/source/git.rb', line 20

def remote
  @remote
end

#settingsObject (readonly)

Since:

  • 1.3.0



30
31
32
# File 'lib/r10k/source/git.rb', line 30

def settings
  @settings
end

Instance Method Details

#desired_contentsArray<String>

Note:

This is required by Util::Basedir

List all environments that should exist in the basedir for this source

Returns:

  • (Array<String>)

Since:

  • 1.3.0



132
133
134
# File 'lib/r10k/source/git.rb', line 132

def desired_contents
  environments.map {|env| env.dirname }
end

#environmentsArray<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.

Returns:

Since:

  • 1.3.0



86
87
88
89
90
91
92
93
94
# File 'lib/r10k/source/git.rb', line 86

def environments
  if not @cache.cached?
    []
  elsif @environments.empty?
    @environments = generate_environments()
  else
    @environments
  end
end

#filter_branches_by_command(branches, command) ⇒ Object

Since:

  • 1.3.0



148
149
150
151
152
153
154
155
156
# File 'lib/r10k/source/git.rb', line 148

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

Since:

  • 1.3.0



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/r10k/source/git.rb', line 136

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_environmentsObject

Since:

  • 1.3.0



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/r10k/source/git.rb', line 101

def generate_environments
  envs = []
  environment_names.each do |en|
    if en.valid?
      envs << R10K::Environment::Git.new(en.name,
                                         @basedir,
                                         en.dirname,
                                         {remote: remote,
                                          ref: en.original_name,
                                          puppetfile_name: puppetfile_name,
                                          overrides: @options[:overrides]})
    elsif en.correct?
     logger.warn _("Environment %{env_name} contained non-word characters, correcting name to %{corrected_env_name}") % {env_name: en.name.inspect, corrected_env_name: en.dirname}
      envs << R10K::Environment::Git.new(en.name,
                                         @basedir,
                                         en.dirname,
                                         {remote: remote,
                                          ref: en.original_name,
                                          puppetfile_name: puppetfile_name,
                                          overrides: @options[:overrides]})
    elsif en.validate?
     logger.error _("Environment %{env_name} contained non-word characters, ignoring it.") % {env_name: en.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.

Since:

  • 1.3.0



74
75
76
77
78
79
# File 'lib/r10k/source/git.rb', line 74

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

#reload!Object

Since:

  • 1.3.0



96
97
98
99
# File 'lib/r10k/source/git.rb', line 96

def reload!
  @cache.sync!
  @environments = generate_environments()
end