diff options
-rw-r--r-- | changelogs/unreleased/sh-fix-root-ref-repository.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/git/repository.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/git/repository_spec.rb | 10 |
3 files changed, 20 insertions, 1 deletions
diff --git a/changelogs/unreleased/sh-fix-root-ref-repository.yml b/changelogs/unreleased/sh-fix-root-ref-repository.yml new file mode 100644 index 00000000000..0670db84fa6 --- /dev/null +++ b/changelogs/unreleased/sh-fix-root-ref-repository.yml @@ -0,0 +1,5 @@ +--- +title: "Gracefully handle case when repository's root ref does not exist" +merge_request: +author: +type: fixed diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index d399636bb28..4657e2cc6ae 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -1253,7 +1253,11 @@ module Gitlab # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/695 def git_merged_branch_names(branch_names = []) - root_sha = find_branch(root_ref).target + return [] unless root_ref + + root_sha = find_branch(root_ref)&.target + + return [] unless root_sha git_arguments = %W[branch --merged #{root_sha} diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb index 2f49bd1bcf2..08dd6ea80ff 100644 --- a/spec/lib/gitlab/git/repository_spec.rb +++ b/spec/lib/gitlab/git/repository_spec.rb @@ -1210,6 +1210,16 @@ describe Gitlab::Git::Repository, seed_helper: true do end end + context 'when no root ref is available' do + it 'returns empty list' do + project = create(:project, :empty_repo) + + names = project.repository.merged_branch_names(%w[feature]) + + expect(names).to be_empty + end + end + context 'when no branch names are specified' do before do repository.create_branch('identical', 'master') |