Class: Overcommit::HookContext::CommitMsg

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook_context/commit_msg.rb

Overview

Contains helpers related to contextual information used by commit-msg hooks.

Instance Method Summary collapse

Methods inherited from Base

#all_files, #cleanup_environment, #execute_hook, #hook_class_name, #hook_script_name, #hook_type_name, #initialize, #input_lines, #input_string, #modified_files, #setup_environment

Constructor Details

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

Instance Method Details

#comment_characterObject


28
29
30
# File 'lib/overcommit/hook_context/commit_msg.rb', line 28

def comment_character
  @comment_character ||= Overcommit::GitConfig.comment_character
end

#commit_messageObject

User commit message stripped of comments and diff (from verbose output).


11
12
13
# File 'lib/overcommit/hook_context/commit_msg.rb', line 11

def commit_message
  commit_message_lines.join
end

#commit_message_fileObject


32
33
34
# File 'lib/overcommit/hook_context/commit_msg.rb', line 32

def commit_message_file
  @args[0]
end

#commit_message_linesObject


22
23
24
25
26
# File 'lib/overcommit/hook_context/commit_msg.rb', line 22

def commit_message_lines
  raw_commit_message_lines.
    take_while { |line| !line.start_with?('diff --git') }.
    reject     { |line| line.start_with?(comment_character) }
end

#empty_message?Boolean

Returns:

  • (Boolean)

6
7
8
# File 'lib/overcommit/hook_context/commit_msg.rb', line 6

def empty_message?
  commit_message.strip.empty?
end

#post_fail_messageObject


36
37
38
39
40
# File 'lib/overcommit/hook_context/commit_msg.rb', line 36

def post_fail_message
  "Failed commit message:\n#{commit_message_lines.join.chomp}\n\n" \
  "Try again with your existing commit message by running:\n" \
  "git commit --edit --file=#{commit_message_file}"
end

#update_commit_message(message) ⇒ Object

Updates the commit message to the specified text.


16
17
18
19
20
# File 'lib/overcommit/hook_context/commit_msg.rb', line 16

def update_commit_message(message)
  ::File.open(commit_message_file, 'w') do |file|
    file.write(message)
  end
end