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
This commit is contained in:
Stan Hu 2017-11-29 00:03:43 -05:00
parent 4ab1a1bd17
commit 66127221fe
3 changed files with 20 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
title: "Gracefully handle case when repository's root ref does not exist"
merge_request:
author:
type: fixed

View File

@ -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}

View File

@ -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')