Skip to content

Commit 449c972

Browse files
jawshooahsds
authored andcommitted
Handle case where git rev-list returns an error
1 parent e4d71fc commit 449c972

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/overcommit/exceptions.rb

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class GitConfigError < StandardError; end
1111
# Raised when there was a problem reading submodule information for a repo.
1212
class GitSubmoduleError < StandardError; end
1313

14+
# Raised when there was a problem reading git revision information with `rev-list`.
15+
class GitRevListError < StandardError; end
16+
1417
# Raised when a {HookContext} is unable to setup the environment before a run.
1518
class HookSetupFailed < StandardError; end
1619

lib/overcommit/hook_context/pre_push.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ def to_s
4141
private
4242

4343
def overwritten_commits
44-
`git rev-list #{remote_sha1} ^#{local_sha1}`.split("\n")
44+
return @overwritten_commits if defined? @overwritten_commits
45+
result = Overcommit::Subprocess.spawn(%W[git rev-list #{remote_sha1} ^#{local_sha1}])
46+
if result.success?
47+
result.stdout.split("\n")
48+
else
49+
raise Overcommit::Exceptions::GitRevListError,
50+
"Unable to check if commits on the remote ref will be overwritten: #{result.stderr}"
51+
end
4552
end
4653
end
4754
end

spec/overcommit/hook_context/pre_push_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@
8989

9090
it { should == true }
9191
end
92+
93+
context 'when remote ref head does not exist locally' do
94+
let(:git_error_msg) { "fatal: bad object #{remote_sha1}" }
95+
96+
before do
97+
pushed_ref.stub(created?: false, deleted?: false)
98+
result = double(success?: false, stderr: git_error_msg)
99+
Overcommit::Subprocess.stub(:spawn).and_return(result)
100+
end
101+
102+
it 'should raise' do
103+
expect { subject }.to raise_error(Overcommit::Exceptions::GitRevListError,
104+
/#{git_error_msg}/)
105+
end
106+
end
92107
end
93108

94109
describe '#created?' do

0 commit comments

Comments
 (0)