Class: Overcommit::Hook::PrepareCommitMsg::ReplaceBranch
- Defined in:
- lib/overcommit/hook/prepare_commit_msg/replace_branch.rb
Overview
Prepends the commit message with a message based on the branch name.
What to prepend
It’s possible to reference parts of the branch name through the captures in the ‘branch_pattern` regex.
For instance, if your current branch is ‘123-topic` then this config
branch_pattern: '(\d+)-(\w+)'
replacement_text: '[#\1] '
would make this hook prepend commit messages with ‘[#123] `.
Similarly, a replacement text of ‘[1]` would result in `[123]`.
When to run this hook
You can configure this to run only for specific types of commits by setting the ‘skipped_commit_types`. The allowed types are
-
‘message’ - if message is given via ‘-m`, `-F`
-
‘template’ - if ‘-t` is given or `commit.template` is set
-
‘commit’ - if ‘-c`, `-C`, or `–amend` is given
-
‘merge’ - if merging
-
‘squash’ - if squashing
Constant Summary collapse
- DEFAULT_BRANCH_PATTERN =
/\A(\d+)-(\w+).*\z/.freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #branch_pattern ⇒ Object
- #new_template ⇒ Object
- #replacement_text ⇒ Object
- #replacement_text_config ⇒ Object
- #run ⇒ Object
- #skip? ⇒ Boolean
- #skipped_commit_types ⇒ Object
Methods inherited from Base
Methods inherited from Base
#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform
Constructor Details
This class inherits a constructor from Overcommit::Hook::Base
Instance Method Details
#branch_pattern ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 60 def branch_pattern @branch_pattern ||= begin pattern = config['branch_pattern'] Regexp.new((pattern || '').empty? ? DEFAULT_BRANCH_PATTERN : pattern) end end |
#new_template ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 52 def new_template @new_template ||= begin curr_branch = Overcommit::GitRepo.current_branch curr_branch.gsub(branch_pattern, replacement_text) end end |
#replacement_text ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 68 def replacement_text @replacement_text ||= begin if File.exist?(replacement_text_config) File.read(replacement_text_config).chomp else replacement_text_config end end end |
#replacement_text_config ⇒ Object
79 80 81 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 79 def replacement_text_config @replacement_text_config ||= config['replacement_text'] end |
#run ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 34 def run return :pass if skip? Overcommit::Utils.log.debug( "Checking if '#{Overcommit::GitRepo.current_branch}' matches #{branch_pattern}" ) return :warn unless branch_pattern.match?(Overcommit::GitRepo.current_branch) Overcommit::Utils.log.debug("Writing #{} with #{new_template}") do |old_contents| "#{new_template}#{old_contents}" end :pass end |
#skip? ⇒ Boolean
87 88 89 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 87 def skip? super || skipped_commit_types.include?() end |
#skipped_commit_types ⇒ Object
83 84 85 |
# File 'lib/overcommit/hook/prepare_commit_msg/replace_branch.rb', line 83 def skipped_commit_types @skipped_commit_types ||= config['skipped_commit_types'].map(&:to_sym) end |