class Overcommit::Hook::PrePush::ProtectedBranches

Prevents updates to specified branches. Accepts a 'destructive_only' option globally or per branch to only prevent destructive updates.

Constants

Pattern

Public Instance Methods

run() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 8
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 Instance Methods

branch_configurations() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 53
def branch_configurations
  config['branches'].to_a + config['branch_patterns'].to_a
end
fetch_patterns() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 43
def fetch_patterns
  branch_configurations.map do |pattern|
    if pattern.is_a?(Hash)
      Pattern.new(pattern.keys.first, pattern['destructive_only'])
    else
      Pattern.new(pattern, global_destructive_only?)
    end
  end
end
find_pattern(remote_ref) click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 30
def find_pattern(remote_ref)
  ref_name = remote_ref[%r{refs/heads/(.*)}, 1]
  return if ref_name.nil?

  patterns.find do |pattern|
    File.fnmatch(pattern.to_s, ref_name)
  end
end
global_destructive_only?() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 57
def global_destructive_only?
  config['destructive_only'].nil? || config['destructive_only']
end
illegal_pushes() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 20
def illegal_pushes
  @illegal_pushes ||= pushed_refs.select do |pushed_ref|
    protected?(pushed_ref)
  end
end
patterns() click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 39
def patterns
  @patterns ||= fetch_patterns
end
protected?(ref) click to toggle source
# File lib/overcommit/hook/pre_push/protected_branches.rb, line 26
def protected?(ref)
  find_pattern(ref.remote_ref)&.destructive?(ref)
end