Sha256: 65108c03294a6a9ffbf294bc178d012a3226fb6b5324be792bf9175f3e24647b

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

module Overcommit::Hook::PrePush
  # Prevents destructive updates to specified branches.
  class ProtectedBranches < Base
    def run
      return :pass unless illegal_pushes.any?

      messages = illegal_pushes.map do |pushed_ref|
        "Deleting or force-pushing to #{pushed_ref.remote_ref} is not allowed."
      end

      [:fail, messages.join("\n")]
    end

    private

    def illegal_pushes
      @illegal_pushes ||= pushed_refs.select do |pushed_ref|
        protected?(pushed_ref.remote_ref) && allow_non_destructive?(pushed_ref)
      end
    end

    def protected?(remote_ref)
      ref_name = remote_ref[%r{refs/heads/(.*)}, 1]
      return false if ref_name.nil?
      protected_branch_patterns.any? do |pattern|
        File.fnmatch(pattern, ref_name)
      end
    end

    def protected_branch_patterns
      @protected_branch_patterns ||= Array(config['branches']).
        concat(Array(config['branch_patterns']))
    end

    def destructive_only?
      config['destructive_only'].nil? || config['destructive_only']
    end

    def allow_non_destructive?(ref)
      if destructive_only?
        ref.destructive?
      else
        true
      end
    end
  end
end

Version data entries

13 entries across 11 versions & 2 rubygems

Version Path
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_push/protected_branches.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_push/protected_branches.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_push/protected_branches.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.46.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.45.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.44.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.43.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.42.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.41.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.40.0 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.39.1 lib/overcommit/hook/pre_push/protected_branches.rb
overcommit-0.39.0 lib/overcommit/hook/pre_push/protected_branches.rb