From ac33f40ea4d9dd6ba44ba643a55ec1a31b41843e Mon Sep 17 00:00:00 2001 From: Henry Blyth Date: Fri, 3 Nov 2017 12:58:22 +0000 Subject: [PATCH 1/2] Print commit message when commit_msg hooks fail So the user can copy-paste their commit message when trying to commit again, and not lose it. Removes frustration for users who craft their commit messages with care. The user can always get their failed message from `.git/COMMIT_EDITMSG` (depending on the version of git, I think), but only if they copy before running `git commit` again, which they may forget to do sometimes. --- lib/overcommit/hook_context/commit_msg.rb | 4 ++++ lib/overcommit/hook_runner.rb | 8 +++++++- spec/overcommit/hook_context/commit_msg_spec.rb | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/overcommit/hook_context/commit_msg.rb b/lib/overcommit/hook_context/commit_msg.rb index c161131d..ed97bbdc 100644 --- a/lib/overcommit/hook_context/commit_msg.rb +++ b/lib/overcommit/hook_context/commit_msg.rb @@ -31,6 +31,10 @@ def commit_message_file @args[0] end + def post_fail_message + "Failed commit message:\n" + commit_message_lines.join + end + private def raw_commit_message_lines diff --git a/lib/overcommit/hook_runner.rb b/lib/overcommit/hook_runner.rb index 11b7d2d0..d9a540e2 100644 --- a/lib/overcommit/hook_runner.rb +++ b/lib/overcommit/hook_runner.rb @@ -74,7 +74,13 @@ def run_hooks print_results - !(@failed || @interrupted) + hook_failed = @failed || @interrupted + + if hook_failed && @context.respond_to?(:post_fail_message) + puts @context.post_fail_message + end + + !hook_failed else @printer.nothing_to_run true # Run was successful diff --git a/spec/overcommit/hook_context/commit_msg_spec.rb b/spec/overcommit/hook_context/commit_msg_spec.rb index 53b8312d..ac11aa90 100644 --- a/spec/overcommit/hook_context/commit_msg_spec.rb +++ b/spec/overcommit/hook_context/commit_msg_spec.rb @@ -85,4 +85,12 @@ it { should == false } end end + + describe '#post_fail_message' do + subject { context.post_fail_message } + + it 'returns printable log of commit message' do + subject.should == "Failed commit message:\nSome commit message\n" + end + end end From 200f6cf10dfd3ca49ac8de69873e2bbd7d4a46b4 Mon Sep 17 00:00:00 2001 From: Henry Blyth Date: Fri, 2 Feb 2018 09:30:21 +0000 Subject: [PATCH 2/2] Move post_fail_message to base class; use printer --- lib/overcommit/hook_context/base.rb | 7 +++++++ lib/overcommit/hook_runner.rb | 5 +++-- lib/overcommit/printer.rb | 6 ++++++ spec/overcommit/hook_context/base_spec.rb | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/hook_context/base.rb b/lib/overcommit/hook_context/base.rb index ecdd349f..9d01ff2f 100644 --- a/lib/overcommit/hook_context/base.rb +++ b/lib/overcommit/hook_context/base.rb @@ -102,6 +102,13 @@ def input_lines @input_lines ||= input_string.split("\n") end + # Returns a message to display on failure. + # + # @return [String] + def post_fail_message + nil + end + private def filter_modified_files(modified_files) diff --git a/lib/overcommit/hook_runner.rb b/lib/overcommit/hook_runner.rb index d9a540e2..ed757082 100644 --- a/lib/overcommit/hook_runner.rb +++ b/lib/overcommit/hook_runner.rb @@ -76,8 +76,9 @@ def run_hooks hook_failed = @failed || @interrupted - if hook_failed && @context.respond_to?(:post_fail_message) - puts @context.post_fail_message + if hook_failed + message = @context.post_fail_message + @printer.after(message) unless message.nil? end !hook_failed diff --git a/lib/overcommit/printer.rb b/lib/overcommit/printer.rb index 58c5550a..cd4593c4 100644 --- a/lib/overcommit/printer.rb +++ b/lib/overcommit/printer.rb @@ -77,6 +77,12 @@ def run_succeeded end end + def after(message) + log.newline + log.log message + log.newline + end + private def print_header(hook) diff --git a/spec/overcommit/hook_context/base_spec.rb b/spec/overcommit/hook_context/base_spec.rb index 97695f90..d5028379 100644 --- a/spec/overcommit/hook_context/base_spec.rb +++ b/spec/overcommit/hook_context/base_spec.rb @@ -23,4 +23,10 @@ it { should == ['line 1', 'line 2'] } end + + describe '#post_fail_message' do + subject { context.post_fail_message } + + it { should be_nil } + end end