Class: Overcommit::Hook::PrepareCommitMsg::ReplaceBranch

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/prepare_commit_msg/replace_branch.rb

Overview

Prepends the commit message with a message based on the branch name. It’s possible to reference parts of the branch name through the captures in the ‘branch_pattern` regex.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#modify_commit_message

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#branch_patternObject



29
30
31
32
33
34
35
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 29

def branch_pattern
  @branch_pattern ||=
    begin
      pattern = config['branch_pattern']
      Regexp.new((pattern || '').empty? ? '\A.*\w+[-_](\d+).*\z' : pattern)
    end
end

#new_templateObject



25
26
27
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 25

def new_template
  @new_template ||= Overcommit::GitRepo.current_branch.gsub(branch_pattern, replacement_text)
end

#replacement_textObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 37

def replacement_text
  @replacement_text ||=
    begin
      if File.exist?(replacement_text_config)
        File.read(replacement_text_config)
      else
        replacement_text_config
      end
    end
end

#replacement_text_configObject



48
49
50
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 48

def replacement_text_config
  @replacement_text_config ||= config['replacement_text']
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 8

def run
  return :pass unless !commit_message_source ||
    commit_message_source == :commit # NOTE: avoid 'merge' and 'rebase'
  Overcommit::Utils.log.debug(
    "Checking if '#{Overcommit::GitRepo.current_branch}' matches #{branch_pattern}"
  )
  if branch_pattern.match?(Overcommit::GitRepo.current_branch)
    Overcommit::Utils.log.debug("Writing #{commit_message_filename} with #{new_template}")
    modify_commit_message do |old_contents|
      "#{new_template}\n#{old_contents}"
    end
    :pass
  else
    :warn
  end
end