From 66127221fea44fd0dcf35b2ff592f5efe21c530f Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 29 Nov 2017 00:03:43 -0500 Subject: [PATCH] Gracefully handle case when repository's root ref does not exist This was failing regularly with an Error 500 when the API branches endpoint was used. Closes #40615 --- changelogs/unreleased/sh-fix-root-ref-repository.yml | 5 +++++ lib/gitlab/git/repository.rb | 6 +++++- spec/lib/gitlab/git/repository_spec.rb | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/sh-fix-root-ref-repository.yml 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')