-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathcommit_msg.rb
More file actions
55 lines (44 loc) · 1.49 KB
/
commit_msg.rb
File metadata and controls
55 lines (44 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
require_relative 'pre_commit'
require_relative 'helpers/stash_unstaged_changes'
require_relative 'helpers/file_modifications'
module Overcommit::HookContext
# Contains helpers related to contextual information used by commit-msg hooks.
class CommitMsg < Base
include Overcommit::HookContext::Helpers::StashUnstagedChanges
include Overcommit::HookContext::Helpers::FileModifications
def empty_message?
commit_message.strip.empty?
end
# User commit message stripped of comments and diff (from verbose output).
def commit_message
commit_message_lines.join
end
# Updates the commit message to the specified text.
def update_commit_message(message)
::File.open(commit_message_file, 'w') do |file|
file.write(message)
end
end
def commit_message_lines
raw_commit_message_lines.
take_while { |line| !line.start_with?('diff --git') }.
reject { |line| line.start_with?(comment_character) }
end
def comment_character
@comment_character ||= Overcommit::GitConfig.comment_character
end
def commit_message_file
@args[0]
end
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
private
def raw_commit_message_lines
::IO.readlines(commit_message_file)
end
end
end