Commit 98535fc1 authored by blackst0ne's avatar blackst0ne
Browse files

Add checks for branch existence before changing HEAD

parent 5d7cd617492c
...@@ -1046,13 +1046,18 @@ def unarchive! ...@@ -1046,13 +1046,18 @@ def unarchive!
end end
def change_head(branch) def change_head(branch)
repository.before_change_head if repository.branch_exists?(branch)
repository.rugged.references.create('HEAD', repository.before_change_head
"refs/heads/#{branch}", repository.rugged.references.create('HEAD',
force: true) "refs/heads/#{branch}",
repository.copy_gitattributes(branch) force: true)
repository.after_change_head repository.copy_gitattributes(branch)
reload_default_branch repository.after_change_head
reload_default_branch
else
errors.add(:base, "Could not change HEAD: branch '#{branch}' does not exist")
false
end
end end
def forked_from?(project) def forked_from?(project)
......
...@@ -10,7 +10,7 @@ def execute ...@@ -10,7 +10,7 @@ def execute
end end
if changing_default_branch? if changing_default_branch?
project.change_head(params[:default_branch]) return error("Could not set the default branch") unless project.change_head(params[:default_branch])
end end
if project.update_attributes(params.except(:default_branch)) if project.update_attributes(params.except(:default_branch))
......
---
title: Add checks for branch existence before changing HEAD
merge_request: 13359
author: Vitaliy @blackst0ne Klachkov
...@@ -1832,6 +1832,11 @@ def create_build(new_pipeline = pipeline, name = 'test') ...@@ -1832,6 +1832,11 @@ def create_build(new_pipeline = pipeline, name = 'test')
describe '#change_head' do describe '#change_head' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
it 'returns error if branch does not exist' do
expect(project.change_head('unexisted-branch')).to be false
expect(project.errors.size).to eq(1)
end
it 'calls the before_change_head and after_change_head methods' do it 'calls the before_change_head and after_change_head methods' do
expect(project.repository).to receive(:before_change_head) expect(project.repository).to receive(:before_change_head)
expect(project.repository).to receive(:after_change_head) expect(project.repository).to receive(:after_change_head)
......
...@@ -101,6 +101,13 @@ ...@@ -101,6 +101,13 @@
expect(Project.find(project.id).default_branch).to eq 'feature' expect(Project.find(project.id).default_branch).to eq 'feature'
end end
it 'does not change a default branch' do
# The branch 'unexisted-branch' does not exist.
update_project(project, admin, default_branch: 'unexisted-branch')
expect(Project.find(project.id).default_branch).to eq 'master'
end
end end
context 'when updating a project that contains container images' do context 'when updating a project that contains container images' do
......
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