diff options
author | Stan Hu <stanhu@gmail.com> | 2017-11-29 00:03:43 -0500 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2017-11-30 23:21:44 -0800 |
commit | 66127221fea44fd0dcf35b2ff592f5efe21c530f (patch) | |
tree | 83ac21a2b26e18bda7bfe5517776657c9ba02cf6 | |
parent | 4ab1a1bd1767b6ea408a20dd1af7a0d29af82bfd (diff) | |
download | gitlab-ce-sh-fix-root-ref-repository.tar.gz |
Gracefully handle case when repository's root ref does not existsh-fix-root-ref-repository
This was failing regularly with an Error 500 when the API branches endpoint
was used.
Closes #40615
-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') |