From 69fb0579f56f1eb060f88b444cd99dbd7ddb3efa Mon Sep 17 00:00:00 2001 From: Drew Blessing Date: Tue, 5 Jul 2016 15:44:59 -0500 Subject: [PATCH] Fix merge request 500 when first commit does not have a parent --- CHANGELOG-EE | 1 + app/models/merge_request.rb | 3 ++- spec/models/merge_request_spec.rb | 18 +++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG-EE b/CHANGELOG-EE index d5fabbb0cb9877..fa48f15ae86a6c 100644 --- a/CHANGELOG-EE +++ b/CHANGELOG-EE @@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date. v 8.10.0 (unreleased) - Rename Git Hooks to Push Rules + - Fix merge request 500 when first commit does not have a parent. !525 v 8.9.4 - Improve how File Lock feature works with nested items. !497 diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index c56844cd73a00a..81d6a151fc5d3e 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -629,7 +629,8 @@ def in_locked_state end def source_sha_parent - source_project.repository.commit(first_commit.sha).parents.first.sha + parents = source_project.repository.commit(first_commit.sha).parents + parents.first.sha unless parents.empty? end def ff_merge_possible? diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 1ff436fb67c223..8ff5fdd199cc03 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -331,9 +331,21 @@ end end - describe "#source_sha_parent" do - it "returns the sha of the parent commit of the first commit in the MR" do - expect(subject.source_sha_parent).to eq("ae73cb07c9eeaf35924a10f713b364d32b2dd34f") + describe '#source_sha_parent' do + it 'returns the sha of the parent commit of the first commit in the MR' do + expect(subject.source_sha_parent).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') + end + + it 'returns nil when the first commit in the MR has no parents' do + allow_any_instance_of(Commit).to receive(:parents).and_return([]) + + expect(subject.source_sha_parent).to be_nil + end + + it 'does not raise an error when the first commit in the MR has no parents' do + allow_any_instance_of(Commit).to receive(:parents).and_return([]) + + expect { subject.source_sha_parent }.not_to raise_error end end -- GitLab