Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
4b1f228c504e
Commit
98535fc1
authored
Aug 08, 2017
by
blackst0ne
Browse files
Add checks for branch existence before changing HEAD
parent
5d7cd617492c
Changes
5
Show whitespace changes
Inline
Side-by-side
app/models/project.rb
View file @
4b1f228c
...
...
@@ -1046,6 +1046,7 @@ def unarchive!
end
def
change_head
(
branch
)
if
repository
.
branch_exists?
(
branch
)
repository
.
before_change_head
repository
.
rugged
.
references
.
create
(
'HEAD'
,
"refs/heads/
#{
branch
}
"
,
...
...
@@ -1053,6 +1054,10 @@ def change_head(branch)
repository
.
copy_gitattributes
(
branch
)
repository
.
after_change_head
reload_default_branch
else
errors
.
add
(
:base
,
"Could not change HEAD: branch '
#{
branch
}
' does not exist"
)
false
end
end
def
forked_from?
(
project
)
...
...
app/services/projects/update_service.rb
View file @
4b1f228c
...
...
@@ -10,7 +10,7 @@ def execute
end
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
if
project
.
update_attributes
(
params
.
except
(
:default_branch
))
...
...
changelogs/unreleased/36010-api-v4-allows-setting-a-branch-that-doesn-t-exist-as-the-default-one.yml
0 → 100644
View file @
4b1f228c
---
title
:
Add checks for branch existence before changing HEAD
merge_request
:
13359
author
:
Vitaliy @blackst0ne Klachkov
spec/models/project_spec.rb
View file @
4b1f228c
...
...
@@ -1832,6 +1832,11 @@ def create_build(new_pipeline = pipeline, name = 'test')
describe
'#change_head'
do
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
expect
(
project
.
repository
).
to
receive
(
:before_change_head
)
expect
(
project
.
repository
).
to
receive
(
:after_change_head
)
...
...
spec/services/projects/update_service_spec.rb
View file @
4b1f228c
...
...
@@ -101,6 +101,13 @@
expect
(
Project
.
find
(
project
.
id
).
default_branch
).
to
eq
'feature'
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
context
'when updating a project that contains container images'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment