Class: R10K::Source::Git

Inherits:
Base
  • Object
show all
Includes:
Logging, Util::Purgeable
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

Defined Under Namespace

Classes: BranchName

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Base

#basedir, #name, #prefix

Instance Method Summary collapse

Methods included from Util::Purgeable

#logger, #pending_contents, #purge!, #stale_contents

Methods included from Logging

formatter, included, level, level=, levels, #logger, #logger_name, outputter, parse_level

Methods inherited from Base

#accept

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)

    Whether to prefix the source name to the environment directory names. Defaults to false.

  • :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



51
52
53
54
55
56
57
58
59
60
# 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

Instance Attribute Details

#cacheObject (readonly)

Since:

  • 1.3.0



28
29
30
# File 'lib/r10k/source/git.rb', line 28

def cache
  @cache
end

#invalid_branchesObject (readonly)

Since:

  • 1.3.0



38
39
40
# File 'lib/r10k/source/git.rb', line 38

def invalid_branches
  @invalid_branches
end

#remoteObject (readonly)

Since:

  • 1.3.0



23
24
25
# File 'lib/r10k/source/git.rb', line 23

def remote
  @remote
end

#settingsObject (readonly)

Since:

  • 1.3.0



33
34
35
# File 'lib/r10k/source/git.rb', line 33

def settings
  @settings
end

Instance Method Details

#current_contentsObject

Since:

  • 1.3.0



109
110
111
112
113
114
115
116
117
# File 'lib/r10k/source/git.rb', line 109

def current_contents
  dir = self.managed_directory
  glob_part = @prefix ? @name.to_s() + '_*' : '*'
  glob_exp = File.join(dir, glob_part)

  Dir.glob(glob_exp).map do |fname|
    File.basename fname
  end
end

#desired_contentsArray<String>

Note:

This implements a required method for the Purgeable mixin

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

Returns:

  • (Array<String>)

Since:

  • 1.3.0



122
123
124
# File 'lib/r10k/source/git.rb', line 122

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



75
76
77
78
79
80
81
82
83
# File 'lib/r10k/source/git.rb', line 75

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

#generate_environmentsObject

Since:

  • 1.3.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/r10k/source/git.rb', line 85

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 #{bn.name.inspect} contained non-word characters, correcting name to #{bn.dirname}"
      envs << R10K::Environment::Git.new(bn.name, @basedir, bn.dirname,
                                     {:remote => remote, :ref => bn.name})
    elsif bn.validate?
     logger.error "Environment #{bn.name.inspect} contained non-word characters, ignoring it."
    end
  end

  envs
end

#managed_directoryObject

Since:

  • 1.3.0



105
106
107
# File 'lib/r10k/source/git.rb', line 105

def managed_directory
  @basedir
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



65
66
67
68
# File 'lib/r10k/source/git.rb', line 65

def preload!
  logger.debug "Determining current branches for Git source #{@remote.inspect}"
  @cache.sync
end