Class: R10K::Module::Git

Inherits:
Base
  • Object
show all
Includes:
Util::Setopts
Defined in:
lib/r10k/module/git.rb

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Attributes inherited from Base

#dirname, #environment, #name, #origin, #owner, #path, #spec_deletable, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Methods inherited from Base

#accept, #delete_spec_dir, #full_path, #maybe_delete_spec_dir, #should_sync?

Constructor Details

#initialize(title, dirname, opts, environment = nil) ⇒ Git

Returns a new instance of Git.



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
82
83
84
85
86
87
88
# File 'lib/r10k/module/git.rb', line 56

def initialize(title, dirname, opts, environment=nil)

  super
  setopts(opts, {
    # Standard option interface
    :version                 => :desired_ref,
    :source                  => :remote,
    :type                    => ::R10K::Util::Setopts::Ignore,

    # Type-specific options
    :branch                  => :desired_ref,
    :tag                     => :desired_ref,
    :commit                  => :desired_ref,
    :ref                     => :desired_ref,
    :git                     => :remote,
    :default_branch          => :default_branch,
    :default_branch_override => :default_override_ref,
  }, :raise_on_unhandled => false)

  @default_ref = @default_branch.nil? ? @overrides.dig(:modules, :default_ref) : @default_branch
  force = @overrides[:force]
  @force = force == false ? false : true

  if @desired_ref == :control_branch
    if @environment && @environment.respond_to?(:ref)
      @desired_ref = @environment.ref
    else
      logger.warn _("Cannot track control repo branch for content '%{name}' when not part of a git-backed environment, will use default if available." % {name: name})
    end
  end

  @repo = R10K::Git::StatefulRepository.new(@remote, @dirname, @name)
end

Instance Attribute Details

#default_override_refObject (readonly)

Returns the value of attribute default_override_ref.



47
48
49
# File 'lib/r10k/module/git.rb', line 47

def default_override_ref
  @default_override_ref
end

#default_refObject (readonly)

Returns the value of attribute default_ref.



42
43
44
# File 'lib/r10k/module/git.rb', line 42

def default_ref
  @default_ref
end

#desired_refObject (readonly)

Returns the value of attribute desired_ref.



37
38
39
# File 'lib/r10k/module/git.rb', line 37

def desired_ref
  @desired_ref
end

#remoteObject (readonly)

@!attribute remote

@api private
@return [String]


52
53
54
# File 'lib/r10k/module/git.rb', line 52

def remote
  @remote
end

#repoObject (readonly)

Returns the value of attribute repo.



32
33
34
# File 'lib/r10k/module/git.rb', line 32

def repo
  @repo
end

Class Method Details

.implement?(name, args) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/r10k/module/git.rb', line 10

def self.implement?(name, args)
  args.has_key?(:git) || args[:type].to_s == 'git'
end

.statically_defined_version(name, args) ⇒ Object

Will be called if self.implement? above returns true. Will return the version info, if version is statically defined in the modules declaration.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/r10k/module/git.rb', line 17

def self.statically_defined_version(name, args)
  if !args[:type] && (args[:ref] || args[:tag] || args[:commit])
    if args[:ref] && args[:ref].to_s.match(/[0-9a-f]{40}/)
      args[:ref]
    else
      args[:tag] || args[:commit]
    end
  elsif args[:type].to_s == 'git' && args[:version] && args[:version].to_s.match(/[0-9a-f]{40}/)
    args[:version]
  end
end

Instance Method Details

#cachedirObject



119
120
121
# File 'lib/r10k/module/git.rb', line 119

def cachedir
  @repo.cache.sanitized_dirname
end

#propertiesObject



94
95
96
97
98
99
100
# File 'lib/r10k/module/git.rb', line 94

def properties
  {
    :expected => version,
    :actual   => (@repo.head || "(unresolvable)"),
    :type     => :git,
  }
end

#statusObject



115
116
117
# File 'lib/r10k/module/git.rb', line 115

def status
  @repo.status(version)
end

#sync(opts = {}) ⇒ Boolean

Returns true if the module was updated, false otherwise.

Parameters:

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

    Deprecated

Returns:

  • (Boolean)

    true if the module was updated, false otherwise



104
105
106
107
108
109
110
111
112
113
# File 'lib/r10k/module/git.rb', line 104

def sync(opts={})
  force = opts[:force] || @force
  if should_sync?
    updated = @repo.sync(version, force, @exclude_spec)
  else
    updated = false
  end
  maybe_delete_spec_dir
  updated
end

#validate_ref_definedObject



123
124
125
126
127
128
129
# File 'lib/r10k/module/git.rb', line 123

def validate_ref_defined
  if @desired_ref.nil? && @default_ref.nil? && @default_override_ref.nil?
    msg = "No ref defined for module #{@name}. Add a ref to the module definition "
    msg << "or set git:default_ref in the r10k.yaml config to configure a global default ref."
    raise ArgumentError, msg
  end
end

#versionObject



90
91
92
# File 'lib/r10k/module/git.rb', line 90

def version
  validate_ref(@desired_ref, @default_ref, @default_override_ref)
end