From 7b80b0c914e78f75fdceba63886ed446606d2666 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Fri, 3 Jan 2020 17:37:30 -0800 Subject: [PATCH] Gracefully handle case where no standard input is provided Fixes #700. --- CHANGELOG.md | 4 ++++ lib/overcommit/hook_context/pre_push.rb | 4 ++-- spec/overcommit/hook_context/pre_push_spec.rb | 10 +++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed9a5ac..16498f95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Overcommit Changelog +## master (unreleased) + +* Fix case where no standard input is provided to `pre-push` hooks + ## 0.52.0 * Fix `Mdl` to properly parse JSON output from `mdl` diff --git a/lib/overcommit/hook_context/pre_push.rb b/lib/overcommit/hook_context/pre_push.rb index 1f37db43..0b2e72a1 100644 --- a/lib/overcommit/hook_context/pre_push.rb +++ b/lib/overcommit/hook_context/pre_push.rb @@ -17,8 +17,8 @@ def remote_ref_deletion? return @remote_ref_deletion if defined?(@remote_ref_deletion) @remote_ref_deletion ||= input_lines. - first. - split(' '). + first&. + split(' ')&. first == '(deleted)' end diff --git a/spec/overcommit/hook_context/pre_push_spec.rb b/spec/overcommit/hook_context/pre_push_spec.rb index 8f0a07a2..101e0f1a 100644 --- a/spec/overcommit/hook_context/pre_push_spec.rb +++ b/spec/overcommit/hook_context/pre_push_spec.rb @@ -26,8 +26,10 @@ describe '#remote_ref_deletion?' do subject { context.remote_ref_deletion? } + let(:standard_input) { "#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n" } + before do - input.stub(:read).and_return("#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n") + input.stub(:read).and_return(standard_input) end context 'when pushing new branch to remote ref' do @@ -56,6 +58,12 @@ it { should == true } end + + context 'when no standard input is provided' do + let(:standard_input) { '' } + + it { should == false } + end end describe '#pushed_refs' do