Commit f0e2eef5 authored by Will Chandler's avatar Will Chandler

Catch Git::CommandError in Branches::CreateService

When creating a branch, one source of failure is when the new branch
name matches a component of an existing branches name. For example,
`feature` and `feature/widget`. In this scenario the Gitaly
`UserCreateBranch` RPC returns a `Gitlab::Git::CommandError`, which
is uncaught and results in a 500 error.

The specific reason for the error is the following:

- Gitaly checks if the requested branch name exists by executing a `git
  for-each-ref <BRANCH_NAME>`
- This returns `feature/widget` because `feature` is a component of
  `feature/widget`.
- Gitaly compares this against the requested branch name, finds they
  don't match, and returns `ErrReferenceAmbiguous` to Rails.

This commit catches the `Gitlab::Git::CommandError` and advises the user
which branch failed to be created.

Changelog: fixed
parent 161347e1
......@@ -9,7 +9,11 @@ module Branches
return result if result[:status] == :error
begin
new_branch = repository.add_branch(current_user, branch_name, ref)
rescue Gitlab::Git::CommandError => e
return error("Failed to create branch '#{branch_name}': #{e}")
end
if new_branch
success(new_branch)
......
......@@ -38,6 +38,7 @@ RSpec.describe Branches::CreateService do
end
it 'returns an error with a reference name' do
error_message = 'Failed to create branch \'new-feature\': invalid reference name unknown'
result = service.execute('new-feature', 'unknown')
expect(result[:status]).to eq(:error)
......@@ -45,6 +46,18 @@ RSpec.describe Branches::CreateService do
end
end
context 'when an ambiguous branch name is provided' do
it 'returns an error that branch could not be created' do
err_msg = 'Failed to create branch \'feature\': 13:reference is ambiguous.'
service.execute('feature/widget', 'master')
result = service.execute('feature', 'master')
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq(err_msg)
end
end
it 'logs and returns an error if there is a PreReceiveError exception' do
error_message = 'pre receive error'
raw_message = "GitLab: #{error_message}"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment