Sha256: 199b566cd0948ba8ca759c50fd0bbf20d35ce29bfbf08efe181830d4022e87d2

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

module Stove
  class Plugin::Git < Plugin::Base
    id 'git'
    description 'Tag and push to a git remote'

    validate(:repository) do
      File.directory?(File.join(Dir.pwd, '.git'))
    end

    validate(:clean) do
      git_null('status --porcelain').strip.empty?
    end

    validate(:up_to_date) do
      git_null('fetch')
      local_sha  = git_null("rev-parse #{branch}").strip
      remote_sha = git_null("rev-parse #{remote}/#{branch}").strip

      log.debug("Local SHA: #{local_sha}")
      log.debug("Remote SHA: #{remote_sha}")

      local_sha == remote_sha
    end

    run('Tagging new release') do
      annotation_type = options[:sign] ? '-s' : '-a'
      tag = cookbook.tag_version

      git %|tag #{annotation_type} #{tag} -m "Release #{tag}"|
      git %|push #{remote} #{branch}|
      git %|push #{remote} #{tag}|
    end

    private

    def git(command, errors = true)
      log.debug("the command matches")
      log.debug("Running `git #{command}', errors: #{errors}")
      Dir.chdir(cookbook.path) do
        response = %x|git #{command}|

        if errors && !$?.success?
          raise Error::GitTaggingFailed.new(command: command) if command =~ /^tag/
          raise Error::GitFailed.new(command: command)
        end

        response
      end
    end

    def git_null(command)
      null = case RbConfig::CONFIG['host_os']
             when /mswin|mingw|cygwin/
               'NUL'
             else
               '/dev/null'
             end

      git("#{command} 2>#{null}", false)
    end

    def remote
      options[:remote]
    end

    def branch
      options[:branch]
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
chef-stove-7.1.5 lib/stove/plugins/git.rb
stove-7.1.5 lib/stove/plugins/git.rb
chef-stove-7.1.1 lib/stove/plugins/git.rb
stove-7.1.0 lib/stove/plugins/git.rb
stove-7.0.1 lib/stove/plugins/git.rb
stove-7.0.0 lib/stove/plugins/git.rb
stove-6.1.1 lib/stove/plugins/git.rb
stove-6.1.0 lib/stove/plugins/git.rb
stove-6.0.0 lib/stove/plugins/git.rb