Commit 02c6f7ec authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Use WriteRef to create/delete branches w/o hooks

The GitLab::Git::Repository class had two methods which aren't really
used in production. `#create_branch` and `#remove_branch` now leverage
WriteRef. The behaviour does change now, in the case of `#create_branch`
it doesn't throw an error anymore if the branch already exists. That's
actually not a problem as all call sites are import related, in which
case there's only one branch to be imported.

`#delete_branch` is only used in tests. The runtime errors aren't
depended on in that case. Again, this change in behaviour doesn't
influence GitLab-Rails.
parent d4cfdf22
......@@ -18,6 +18,7 @@ module Gitlab
GITALY_INTERNAL_URL = 'ssh://gitaly/internal.git'
GITLAB_PROJECTS_TIMEOUT = Gitlab.config.gitlab_shell.git_timeout
EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'
REF_REMOVAL_UPDATE_REV = '0' * 40
NoRepository = Class.new(StandardError)
InvalidRepository = Class.new(StandardError)
......@@ -636,10 +637,9 @@ module Gitlab
end
# Delete the specified branch from the repository
# Note: No Git hooks are executed for this action
def delete_branch(branch_name)
wrapped_gitaly_errors do
gitaly_ref_client.delete_branch(branch_name)
end
write_ref(branch_name, REF_REMOVAL_UPDATE_REV)
rescue CommandError => e
raise DeleteBranchError, e
end
......@@ -651,14 +651,13 @@ module Gitlab
end
# Create a new branch named **ref+ based on **stat_point+, HEAD by default
# Note: No Git hooks are executed for this action
#
# Examples:
# create_branch("feature")
# create_branch("other-feature", "master")
def create_branch(ref, start_point = "HEAD")
wrapped_gitaly_errors do
gitaly_ref_client.create_branch(ref, start_point)
end
write_ref(ref, start_point)
end
# If `mirror_refmap` is present the remote is set as mirror with that mapping
......
......@@ -48,6 +48,14 @@ module Gitlab
@project.repository.create_branch(@merge_request.target_branch, @merge_request.target_branch_sha)
end
def fetch_ref
target_ref = Gitlab::Git::BRANCH_REF_PREFIX + @merge_request.source_branch
unless @project.repository.fetch_source_branch!(@project.repository, @diff_head_sha, target_ref)
Rails.logger.warn("Import/Export warning: Failed to create #{target_ref} for MR: #{@merge_request.iid}") # rubocop:disable Gitlab/RailsLogger
end
end
def branch_exists?(branch_name)
@project.repository.raw.branch_exists?(branch_name)
end
......
......@@ -420,55 +420,6 @@ describe Gitlab::Git::Repository, :seed_helper do
end
end
describe "#delete_branch" do
let(:repository) { mutable_repository }
after do
ensure_seeds
end
it "removes the branch from the repo" do
branch_name = "to-be-deleted-soon"
repository.create_branch(branch_name)
expect(repository_rugged.branches[branch_name]).not_to be_nil
repository.delete_branch(branch_name)
expect(repository_rugged.branches[branch_name]).to be_nil
end
context "when branch does not exist" do
it "raises a DeleteBranchError exception" do
expect { repository.delete_branch("this-branch-does-not-exist") }.to raise_error(Gitlab::Git::Repository::DeleteBranchError)
end
end
end
describe "#create_branch" do
let(:repository) { mutable_repository }
after do
ensure_seeds
end
it "creates a new branch" do
expect(repository.create_branch('new_branch', 'master')).not_to be_nil
end
it "creates a new branch with the right name" do
expect(repository.create_branch('another_branch', 'master').name).to eq('another_branch')
end
it "fails if we create an existing branch" do
repository.create_branch('duplicated_branch', 'master')
expect {repository.create_branch('duplicated_branch', 'master')}.to raise_error("Branch duplicated_branch already exists")
end
it "fails if we create a branch from a non existing ref" do
expect {repository.create_branch('branch_based_in_wrong_ref', 'master_2_the_revenge')}.to raise_error("Invalid reference master_2_the_revenge")
end
end
describe '#delete_refs' do
let(:repository) { mutable_repository }
......
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